-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_file
executable file
·155 lines (133 loc) · 3.88 KB
/
decode_file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env perl
use strict;
use warnings;
use Readonly;
Readonly::Scalar my $BS => 16;
use Getopt::Long qw(:config no_ignore_case bundling);
use Algorithm::FEC;
use IPC::Run qw(run);
use MIME::Base64;
use Number::Range;
use IO::File;
use List::MoreUtils qw(uniq);
use Image::Size;
use File::Temp;
sub read_image {
my %args = @_;
my %data;
my $codes;
my (undef, $img_height) = imgsize($args{filename});
my $crop_h = int($img_height / 3.5);
foreach my $off_y (map { int($_ * $img_height / 4) } 0..3) {
my $codes_new;
run
["convert", @{$args{convert_options}}, "-crop", "100%x$crop_h+0+$off_y", "+repage", $args{filename}, "pnm:-"],
"|", ["zbarimg", "--quiet", "--raw", "--", "-"], ">", \$codes_new;
$codes .= $codes_new;
}
my @codes = uniq sort split(/\n/, $codes);
foreach my $code (@codes) {
if ($code =~ /^FI(.+)$/) {
# fileid
$data{fileid} = $1;
} elsif ($code =~ /^(OS|LE)([0-9a-f]+)$/) {
# offset/length
my $f = {OS => "offset", LE => "length"}->{$1};
if (defined($data{$f})) {
die("Tried to set \"$f\" field twice");
}
$data{$f} = oct("0x$2");
} elsif ($code =~ /^DBNONE\s*$/) {
# no deleted blocks
$data{deleted_blocks} = 1;
} elsif ($code =~ /^DB(.*)$/) {
# deleted blocks, fill them with null bytes.
foreach my $bid (Number::Range->new($1)->range()) {
push(@{$data{data_indices}}, $bid);
push(@{$data{data}} , "\0" x $BS);
}
$data{deleted_blocks} = 1;
} elsif ($code =~ /^([0-9a-f]+):([A-Za-z0-9+\/=]{1,40})$/) {
# data
my ($off, $b64) = ($1, $2);
$b64 .= "=" until (length($b64) % 4 == 0);
push(@{$data{data_indices}}, oct("0x$off"));
push(@{$data{data}} , decode_base64($b64));
} else {
# warn("Ignoring unknown barcode input \"$code\"");
}
}
foreach (qw(fileid offset length deleted_blocks)) {
die("Required header field \"$_\" not found in image") if (!exists($data{$_}));
}
delete($data{deleted_blocks});
if (@{$data{data} || []} < 256) {
die("Could only retrieve " . scalar(@{$data{data} || []}) . " data lines from image \"$args{filename}\" - need at least 256");
}
my $fec = Algorithm::FEC->new(256, 320, $BS);
my @f_d = @{$data{data}}[0..255];
my @f_i = @{$data{data_indices}}[0..255];
$fec->set_decode_blocks(\@f_d, \@f_i);
$fec->decode();
$data{data} = join("", @f_d);
delete($data{data_indices});
$data{data} = substr($data{data}, 0, $data{length});
return %data;
}
sub try_read_image {
my ($filename) = @_;
my %data;
foreach my $co (
["-contrast-stretch", "35%x30%"],
["-contrast-stretch", "30%x40%"],
["-contrast-stretch", "25%x50%"],
["-contrast-stretch", "20%x60%"],
["-contrast-stretch", "20%x70%"],
["-threshold", "75%"],
[],
) {
eval {
%data = read_image(filename => $filename, convert_options => $co);
};
if ($@) {
if ($@ =~ /^(?:Required header field|Could only retrieve)/) {
warn("[@{$co}] $@");
} else {
die($@);
}
} else {
return %data;
}
}
return ();
}
my %options = (
filename_prefix => "dp_",
);
GetOptions(\%options,
"filename_prefix|filename-prefix=s",
);
my $in_name;
my $tempfile;
if (!@ARGV) {
if (-t STDIN) {
die("No filename provided and STDIN is opened to a terminal.\nPlease provide a filename or image data");
} else {
$tempfile = File::Temp->new();
print $tempfile do { local $/; <STDIN>; };
close($tempfile);
$in_name = $tempfile->filename;
}
} else {
$in_name = $ARGV[0];
}
my %image_data = try_read_image($in_name) or die("unable to get any usable data from $in_name");
print STDERR "retrieved data from image!\n";
if ($image_data{fileid} =~ /([^A-Za-z0-9\-_.])/) {
die("file identifier \"$image_data{fileid}\" contains unsafe character $1");
}
my $filename = $options{filename_prefix} . $image_data{fileid};
my $outfile = IO::File->new($filename, O_WRONLY | O_CREAT)
or die("unable to open output $filename: $!");
seek($outfile, $image_data{offset}, 0);
print $outfile $image_data{data};