PDF/Filter.pm

filter: Statementstmt 18.0% 38 / 211 Branchbran 0.0% 0 / 94 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 41.6% 10 / 24 Podpod 50.0% 1 / 2 totaltotal 13.7% 49 / 356 CC 54 SCAR 42.2
CC
54
Cov
12%
infilt
156.0
outfilt
110.0
infilt
90.0
CRAP 67.8
« PDF/Dict.pm ↑ Summary PDF/TTFont.pm »
1 package Text::PDF::Filter;
2  
3 =head1 NAME
4  
5 PDF::Filter - Abstract superclass for PDF stream filters
6  
7 =head1 SYNOPSIS
8  
9 $f = Text::PDF::Filter->new;
10 $str = $f->outfilt($str, 1);
11 print OUTFILE $str;
12
13 while (read(INFILE, $dat, 4096))
14 { $store .= $f->infilt($dat, 0); }
15 $store .= $f->infilt("", 1);
16  
17 =head1 DESCRIPTION
18  
19 A Filter object contains state information for the process of outputting
20 and inputting data through the filter. The precise state information stored
21 is up to the particular filter and may range from nothing to whole objects
22 created and destroyed.
23  
24 Each filter stores different state information for input and output and thus
25 may handle one input filtering process and one output filtering process at
26 the same time.
27  
28 =head1 METHODS
29  
30 =head2 Text::PDF::Filter->new
31  
32 Creates a new filter object with empty state information ready for processing
33 data both input and output.
34  
35 =head2 $dat = $f->infilt($str, $isend)
36  
37 Filters from output to input the data. Notice that $isend == 0 implies that there
38 is more data to come and so following it $f may contain state information
39 (usually due to the break-off point of $str not being tidy). Subsequent calls
40 will incorporate this stored state information.
41  
42 $isend == 1 implies that there is no more data to follow. The
43 final state of $f will be that the state information is empty. Error messages
44 are most likely to occur here since if there is required state information to
45 be stored following this data, then that would imply an error in the data.
46  
47 =head2 $str = $f->outfilt($dat, $isend)
48  
49 Filter stored data ready for output. Parallels C<infilt>.
50  
51 =cut
52  
53 sub new
54 {
55 0 ▶ my ($class) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($self) = {};
57  
58 0 bless $self, $class;
59 }
60  
61 sub release
62 {
63 0 ▶ my ($self) = @_;
Subroutine release: not called CC=6 SCAR=37.4
Pod undocumented
64  
65 # delete stuff that we know we can, here
66  
67 0 my @tofree = map { delete $self->{$_} } keys %{$self};
68  
69 0 ▶ while (my $item = shift @tofree)
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 0 0 0
70 {
71 0 my $ref = ref($item);
72 0 ▶ if (UNIVERSAL::can($item, 'release'))
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::can($item, 'release')) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($ref, 'HASH')) { } 0 0 0
73 0 { $item->release(); }
74 elsif ($ref eq 'ARRAY')
75 0 { push( @tofree, @{$item} ); }
76 elsif (UNIVERSAL::isa($ref, 'HASH'))
77 0 { release($item); }
78 }
79  
80 # check that everything has gone - it better had!
81 0 foreach my $key (keys %{$self})
82 0 { warn ref($self) . " still has '$key' key left after release.\n"; }
83 }
84  
85  
86 package Text::PDF::ASCII85Decode;
87  
88 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 @ISA = qw(Text::PDF::Filter);
91 # no warnings qw(uninitialized);
92  
93 =head1 NAME
94  
95 Text::PDF::ASCII85Decode - Ascii85 filter for PDF streams. Inherits from
96 L<Text::PDF::Filter>
97  
98 =cut
99  
100 sub outfilt
101 {
102 0 ▶ my ($self, $str, $isend) = @_;
Subroutine outfilt: not called CC=10 SCAR=47.0
103 0 my ($res, $i, $j, $b, @c);
104  
105 0 ▶ if ($self->{'outcache'} ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"outcache"} ne "") 0 0 0
106 {
107 0 $str = $self->{'outcache'} . $str;
108 0 $self->{'outcache'} = "";
109 }
110 0 ▶ for ($i = 0; $i < length($str); $i += 4)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
111 {
112 0 $b = unpack("N", substr($str, $i, 4));
113 0 ▶ if ($b == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 0) 0 0 0
114 {
115 0 $res .= "z";
116 0 next;
117 }
118 0 ▶ for ($j = 3; $j >= 0; $j--)
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 0 0 0
119 0 { $c[$j] = $b - int($b / 85) * 85 + 33; $b /= 85; }
120 0 $res .= pack("C5", $b + 33, @c);
121 0 ▶ $res .= "\n" if ($i % 60 == 56);
Branch: 1 branch
BranchTrueFalseTotal
if $i % 60 == 56 0 0 0
122 }
123 0 ▶ if ($isend && $i > length($str))
Branch: 2 branches
BranchTrueFalseTotal
if ($isend and $i > length $str) { } 0 0 0
elsif ($i > length $str) { } 0 0 0
Condition0%0/3
$isend and $i > length $str
!ll&&!rl&&r
000
MC/DC0%0/2
$isend and $i > length $str
$isend $i > length $str
Truth table0%0/3
A and B
A= $isendB= $i > length $str
ABresult
0X0
100
111
124 {
125 0 $b = unpack("N", substr($str, $i - 4) . "\000\000\000");
126 0 ▶ for ($j = 0; $j < 4; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 4) 0 0 0
127 0 { $c[$j] = $b - int($b / 85) * 85 + 33; $b /= 85; }
128 0 $res .= substr(pack("C5", @c, $b), 0, $i - length($str) + 1) . "->";
129 } elsif ($i > length($str))
130 0 { $self->{'outcache'} = substr($str, $i - 4); }
131 0 $res;
132 }
133  
134 sub infilt
135 {
136 0 ▶ my ($self, $str, $isend) = @_;
Subroutine infilt: not called CC=9 SCAR=45.0
137 0 my ($res, $i, $j, @c, $b, $num);
138  
139 0 ▶ if ($self->{'incache'} ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"incache"} ne "") 0 0 0
140 {
141 0 $str = $self->{'incache'} . $str;
142 0 $self->{'incache'} = "";
143 }
144 0 $str =~ s/(\r|\n)\n?//og;
145 0 ▶ for ($i = 0; $i < length($str); $i += 5)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 0 0 0
146 {
147 0 $b = 0;
148 0 ▶ if (substr($str, $i, 1) eq "z")
Branch: 2 branches
BranchTrueFalseTotal
if (substr($str, $i, 1) eq 'z') { } 0 0 0
elsif ($isend and substr($str, $i, 6) =~ /^(.{2,4})\~\>$/o) { } 0 0 0
149 {
150 0 $i -= 4;
151 0 $res .= pack("N", 0);
152 0 next;
153 }
154 ▶ elsif ($isend && substr($str, $i, 6) =~ m/^(.{2,4})\~\>$/o)
Condition0%0/3
$isend and substr($str, $i, 6) =~ /^(.{2,4})\~\>$/o
!ll&&!rl&&r
000
MC/DC0%0/2
$isend and substr($str, $i, 6) =~ /^(.{2,4})\~\>$/o
$isend substr($str, $i, 6) =~ /^(.{2,4})\~\>$/o
Truth table0%0/3
A and B
A= $isendB= substr($str, $i, 6) =~ /^(.{2,4})\~\>$/o
ABresult
0X0
100
111
155 {
156 0 $num = 5 - length($1);
157 0 @c = unpack("C5", $1 . ("u" x (4 - $num))); # pad with 84 to sort out rounding
158 0 $i = length($str);
159 } else
160 0 { @c = unpack("C5", substr($str, $i, 5)); }
161  
162 0 ▶ for ($j = 0; $j < 5; $j++)
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 5) 0 0 0
163 {
164 0 $b *= 85;
165 0 $b += $c[$j] - 33;
166 }
167 0 $res .= substr(pack("N", $b), 0, 4 - $num);
168 }
169 0 ▶ if (!$isend && $i > length($str))
Branch: 1 branch
BranchTrueFalseTotal
if (not $isend and $i > length $str) 0 0 0
Condition0%0/3
not $isend and $i > length $str
!ll&&!rl&&r
000
MC/DC0%0/2
not $isend and $i > length $str
not $isend $i > length $str
Truth table0%0/3
not(A) and B
A= not $isendB= $i > length $str
ABresult
0X0
100
111
170 0 { $self->{'incache'} = substr($str, $i - 5); }
171 0 $res;
172 }
173  
174  
175  
176 package Text::PDF::RunLengthDecode;
177  
178 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
180 1 @ISA = qw(Text::PDF::Filter);
181 # no warnings qw(uninitialized);
182  
183 =head1 NAME
184  
185 Text::PDF::RunLengthDecode - Run Length encoding filter for PDF streams. Inherits from
186 L<Text::PDF::Filter>
187  
188 =cut
189  
190 sub outfilt
191 {
192 0 ▶ my ($self, $str, $isend) = @_;
Subroutine outfilt: not called CC=6 SCAR=37.4
193 0 my ($res, $s, $r);
194  
195 # no state information, just slight inefficiency at block boundaries
196 0 ▶ while ($str ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne "") 0 0 0
197 {
198 0 ▶ if ($str =~ m/^(.*?)((.)\2{2,127})(.*?)$/so)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(.*?)((.)\2{2,127})(.*?)$/so) { } 0 0 0
199 {
200 0 $s = $1;
201 0 $r = $2;
202 0 $str = $3;
203 } else
204 {
205 0 $s = $str;
206 0 $r = '';
207 0 $str = '';
208 }
209 0 ▶ while (length($s) > 127)
Branch: 1 branch
BranchTrueFalseTotal
if (length $s > 127) 0 0 0
210 {
211 0 $res .= pack("C", 127) . substr($s, 0, 127);
212 0 substr($s, 0, 127) = '';
213 }
214 0 ▶ $res .= pack("C", length($s)) . $s if length($s) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $s > 0 0 0 0
215 0 $res .= pack("C", 257 - length($r));
216 }
217 0 ▶ $res .= "\x80" if ($isend);
Branch: 1 branch
BranchTrueFalseTotal
if $isend 0 0 0
218 0 $res;
219 }
220  
221 sub infilt
222 {
223 0 ▶ my ($self, $str, $isend) = @_;
Subroutine infilt: not called CC=9 SCAR=45.0
224 0 my ($res, $l, $d);
225  
226 0 ▶ if ($self->{'incache'} ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"incache"} ne "") 0 0 0
227 {
228 0 $str = $self->{'incache'} . $str;
229 0 $self->{'incache'} = "";
230 }
231 0 ▶ while ($str ne "")
Branch: 1 branch
BranchTrueFalseTotal
if ($str ne "") 0 0 0
232 {
233 0 $l = unpack("C", $str);
234 0 ▶ if ($l == 128)
Branch: 1 branch
BranchTrueFalseTotal
if ($l == 128) 0 0 0
235 {
236 0 $isend = 1;
237 0 return $res;
238 }
239 0 ▶ if ($l > 128)
Branch: 1 branch
BranchTrueFalseTotal
if ($l > 128) { } 0 0 0
240 {
241 0 ▶ if (length($str) < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (length $str < 2) 0 0 0
242 {
243 0 ▶ warn "Premature end to data in RunLengthEncoded data" if $isend;
Branch: 1 branch
BranchTrueFalseTotal
if $isend 0 0 0
244 0 $self->{'incache'} = $str;
245 0 return $res;
246 }
247 0 $res .= substr($str, 1, 1) x (257 - $l);
248 0 substr($str, 0, 2) = "";
249 } else
250 {
251 0 ▶ if (length($str) < $l + 1)
Branch: 1 branch
BranchTrueFalseTotal
if (length $str < $l + 1) 0 0 0
252 {
253 0 ▶ warn "Premature end to data in RunLengthEncoded data" if $isend;
Branch: 1 branch
BranchTrueFalseTotal
if $isend 0 0 0
254 0 $self->{'incache'} = $str;
255 0 return $res;
256 }
257 0 $res .= substr($str, 1, $l);
258 0 substr($str, 0, $l + 1) = "";
259 }
260 }
261 0 $res;
262 }
263  
264  
265  
266 package Text::PDF::ASCIIHexDecode;
267  
268 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
269 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
270 1 @ISA = qw(Text::PDF::Filter);
271 # no warnings qw(uninitialized);
272  
273 =head1 NAME
274  
275 Text::PDF::ASCIIHexDecode - Ascii Hex encoding (very inefficient) for PDF streams.
276 Inherits from L<Text::PDF::Filter>
277  
278 =cut
279  
280 sub outfilt
281 {
282 0 ▶ my ($self, $str, $isend) = @_;
Subroutine outfilt: not called CC=2 SCAR=17.9
283  
284 0 $str =~ s/(.)/sprintf("%02x", ord($1))/oge;
285 0 ▶ $str .= ">" if $isend;
Branch: 1 branch
BranchTrueFalseTotal
if $isend 0 0 0
286 0 $str;
287 }
288  
289 sub infilt
290 {
291 0 ▶ my ($self, $str, $isend) = @_;
Subroutine infilt: not called CC=3 SCAR=24.8
292  
293 0 $isend = ($str =~ s/>$//og);
294 0 $str =~ s/\s//oig;
295 0 ▶ $str =~ s/([0-9a-z])/pack("C", hex($1 . "0"))/oige if ($isend && length($str) & 1);
Branch: 1 branch
BranchTrueFalseTotal
if $isend and length $str & 1 0 0 0
Condition0%0/3
$isend and length $str & 1
!ll&&!rl&&r
000
MC/DC0%0/2
$isend and length $str & 1
$isend length $str & 1
Truth table0%0/3
A and B
A= $isendB= length $str & 1
ABresult
0X0
100
111
296 0 $str =~ s/([0-9a-z]{2})/pack("C", hex($1))/oige;
297 0 $str;
298 }
299  
300 package Text::PDF::FlateDecode;
301  
302 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
303 1 ▶use vars qw(@ISA $havezlib);
Subroutine BEGIN: called CC=1 SCAR=0.0
304 1 @ISA = qw(Text::PDF::Filter);
305 BEGIN
306 {
307 1 ▶ eval {require "Compress/Zlib.pm";};
Subroutine BEGIN: called CC=1 SCAR=0.0
308 1 $havezlib = !$@;
309 }
310  
311 sub new
312 {
313 0 ▶ return undef unless $havezlib;
Subroutine new: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $Text::PDF::FlateDecode::havezlib 0 0 0
314 0 my ($class) = @_;
315 0 my ($self) = {};
316  
317 0 $self->{'outfilt'} = Compress::Zlib::deflateInit();
318 0 $self->{'infilt'} = Compress::Zlib::inflateInit();
319 0 bless $self, $class;
320 }
321  
322 sub outfilt
323 {
324 0 ▶ my ($self, $str, $isend) = @_;
Subroutine outfilt: not called CC=2 SCAR=17.9
325 0 my ($res);
326  
327 0 $res = $self->{'outfilt'}->deflate($str);
328 0 ▶ $res .= $self->{'outfilt'}->flush() if ($isend);
Branch: 1 branch
BranchTrueFalseTotal
if $isend 0 0 0
329 0 $res;
330 }
331  
332 sub infilt
333 {
334 0 ▶ my ($self, $dat, $last) = @_;
Subroutine infilt: not called CC=1 SCAR=6.9
335 0 my ($res, $status) = $self->{'infilt'}->inflate("$dat");
336 0 $res;
337 }
338  
339 package Text::PDF::LZWDecode;
340  
341 1 ▶use vars qw(@ISA @basedict);
Subroutine BEGIN: called CC=1 SCAR=0.0
342 1 @ISA = qw(Text::PDF::FlateDecode);
343 1 @basedict = map {pack("C", $_)} (0 .. 255, 0, 0);
344 # no warnings qw(uninitialized);
345  
346 sub new
347 {
348 0 ▶ my ($class) = @_;
Subroutine new: not called CC=1 SCAR=6.9
349 0 my ($self) = {};
350  
351 0 $self->{'indict'} = [@basedict];
352 0 $self->{'count'} = 258;
353 0 $self->{'insize'} = 9;
354 0 $self->{'cache'} = 0;
355 0 $self->{'cache_size'} = 0;
356 # $self->{'outfilt'} = Compress::Zlib::deflateInit(); # patent precludes LZW encoding
357 0 bless $self, $class;
358 }
359  
360 sub infilt
361 {
362 0 ▶ my ($self, $dat, $last) = @_;
Subroutine infilt: not called CC=12 SCAR=50.5
363 0 my ($num, $res);
364  
365 0 $res = '';
366  
367 0 ▶ while ($dat ne '' || $self->{'cache_size'} >= $self->{'insize'})
Branch: 1 branch
BranchTrueFalseTotal
if ($dat ne "" or $self->{"cache_size"} >= $self->{"insize"}) 0 0 0
Condition0%0/3
$dat ne "" or $self->{"cache_size"} >= $self->{"insize"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dat ne "" or $self->{"cache_size"} >= $self->{"insize"}
$dat ne "" $self->{"cache_size"} >= $self->{"insize"}
Truth table0%0/3
A or B
A= $dat ne ""B= $self->{"cache_size"} >= $self->{"insize"}
ABresult
1X1
011
000
368 {
369 0 $num = $self->read_dat(\$dat);
370 0 ▶ last if $num < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num < 0 0 0 0
371 0 ▶ return $res if ($num == 257); # End of Data
Branch: 1 branch
BranchTrueFalseTotal
if $num == 257 0 0 0
372 0 ▶ if ($num == 256) # Clear table
Branch: 1 branch
BranchTrueFalseTotal
if ($num == 256) 0 0 0
373 {
374 0 $self->{'indict'} = [@basedict];
375 0 $self->{'insize'} = 9;
376 0 $self->{'count'} = 258;
377 0 next;
378 }
379 0 ▶ if ($self->{'count'} > 258)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"count"} > 258) 0 0 0
380 {
381 0 ($self->{'indict'}[$self->{'count'}-1]) .= substr($self->{'indict'}[$num], 0, 1);
382 }
383 0 ▶ if ($self->{'count'} < 4096)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"count"} < 4096) 0 0 0
384 {
385 0 $self->{'indict'}[$self->{'count'}] = $self->{'indict'}[$num];
386 0 $self->{'count'}++;
387 }
388 0 $res .= $self->{'indict'}[$num];
389 0 ▶ if ($self->{'count'} >= 4096)
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'count'} >= 4096) { } 0 0 0
elsif ($self->{'count'} == 512) { } 0 0 0
elsif ($self->{'count'} == 1024) { } 0 0 0
elsif ($self->{'count'} == 2048) { } 0 0 0
390 {
391 # don't do anything on table full, the encoder tells us when to clear
392 } elsif ($self->{'count'} == 512)
393 0 { $self->{'insize'} = 10; }
394 elsif ($self->{'count'} == 1024)
395 0 { $self->{'insize'} = 11; }
396 elsif ($self->{'count'} == 2048)
397 0 { $self->{'insize'} = 12; }
398 }
399 0 return $res;
400 }
401  
402 sub read_dat
403 {
404 0 ▶ my ($self, $rdat) = @_;
Subroutine read_dat: not called CC=3 SCAR=24.8
405 0 my ($res);
406  
407 0 ▶ while ($self->{'cache_size'} < $self->{'insize'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cache_size"} < $self->{"insize"}) 0 0 0
408 {
409 0 ▶ return -1 if $$rdat eq ''; # oops -- not enough data in this chunk
Branch: 1 branch
BranchTrueFalseTotal
if $$rdat eq "" 0 0 0
410 0 $self->{'cache'} = ($self->{'cache'} << 8) + unpack("C", $$rdat);
411 0 substr($$rdat, 0, 1) = '';
412 0 $self->{'cache_size'} += 8;
413 }
414  
415 0 $res = $self->{'cache'} >> ($self->{'cache_size'} - $self->{'insize'});
416 0 $self->{'cache'} &= (1 << ($self->{'cache_size'} - $self->{'insize'})) - 1;
417 0 $self->{'cache_size'} -= $self->{'insize'};
418 0 return $res;
419 }
420  
421 1 1;
422  
« PDF/Dict.pm ↑ Summary PDF/TTFont.pm »