File Coverage

blib/lib/PDF/Builder/Basic/PDF/Filter/ASCII85Decode.pm
Criterion Covered Total %
statement 49 60 81.6
branch 13 22 59.0
condition 9 18 50.0
subroutine 5 5 100.0
pod 2 2 100.0
total 78 107 72.9


line stmt bran cond sub pod time code
1             package PDF::Builder::Basic::PDF::Filter::ASCII85Decode;
2              
3 45     45   133931 use base 'PDF::Builder::Basic::PDF::Filter';
  45         102  
  45         7656  
4              
5 45     45   314 use strict;
  45         135  
  45         1363  
6 45     45   256 use warnings;
  45         102  
  45         44453  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.027'; # manually update whenever code is changed
10              
11             =head1 NAME
12              
13             PDF::Builder::Basic::PDF::Filter::ASCII85Decode - Compress and uncompress stream filters for ASCII-85
14              
15             Inherits from L<PDF::Builder::Basic::PDF::Filter>
16              
17             =cut
18              
19             sub outfilt {
20 3     3 1 20 my ($self, $str, $isend) = @_;
21              
22 3         7 my ($res, $i, $j, $bb, @c);
23              
24 3 50 33     17 if (exists $self->{'outcache'} and $self->{'outcache'} ne "") {
25 0         0 $str = $self->{'outcache'} . $str;
26 0         0 $self->{'outcache'} = "";
27             }
28 3         13 for ($i = 0; $i + 4 <= length($str); $i += 4) {
29 32         55 $bb = unpack("N", substr($str, $i, 4));
30 32 50       48 if ($bb == 0) {
31 0         0 $res .= "z";
32 0         0 next;
33             }
34 32         50 for ($j = 0; $j < 4; $j++) {
35 128         184 $c[$j] = $bb - int($bb / 85) * 85 + 33; $bb /= 85;
  128         220  
36             }
37 32         64 $res .= pack("C5", $bb + 33, reverse @c);
38 32 50       67 $res .= "\n" if $i % 60 == 56;
39             }
40 3 100 66     20 if ($isend && $i < length($str)) {
    50          
    0          
41 1         6 $str = substr($str, $i);
42 1         8 $bb = unpack("N", $str . ("\000" x (4 - length($str))));
43 1         5 for ($j = 0; $j < 4; $j++) {
44 4         10 $c[$j] = $bb - int($bb / 85) * 85 + 33; $bb /= 85;
  4         10  
45             }
46 1         4 push @c, $bb + 33;
47 1         6 $res .= substr(pack("C5", reverse @c), 0, length($str) + 1) . '~>';
48             } elsif ($isend) {
49 2         3 $res .= '~>';
50             } elsif ($i + 4 > length($str)) {
51 0         0 $self->{'outcache'} = substr($str, $i);
52             }
53              
54 3         20 return $res;
55             }
56              
57             sub infilt {
58 2     2 1 4 my ($self, $str, $isend) = @_;
59              
60 2         4 my ($res, $i, $j, @c, $bb, $num);
61 2         4 $num = 0;
62 2 50 33     8 if (exists($self->{'incache'}) && $self->{'incache'} ne "") {
63 0         0 $str = $self->{'incache'} . $str;
64 0         0 $self->{'incache'} = "";
65             }
66 2         10 $str =~ s/(\r|\n)\n?//og;
67 2         6 for ($i = 0; $i < length($str); $i += 5) {
68 23 100 66     49 last if $isend and substr($str, $i, 6) eq '~>';
69 22         24 $bb = 0;
70 22 50 66     76 if (substr($str, $i, 1) eq "z") {
    100          
71 0         0 $i -= 4;
72 0         0 $res .= pack("N", 0);
73 0         0 next;
74             } elsif ($isend && substr($str, $i, 6) =~ m/^(.{2,4})\~\>$/o) {
75 1         3 $num = 5 - length($1);
76 1         4 @c = unpack("C5", $1 . ("u" x (4 - $num))); # pad with 84 to sort out rounding
77 1         2 $i = length($str);
78             } else {
79 21         33 @c = unpack("C5", substr($str, $i, 5));
80             }
81              
82 22         27 for ($j = 0; $j < 5; $j++) {
83 110         100 $bb *= 85;
84 110         143 $bb += $c[$j] - 33;
85             }
86 22         40 $res .= substr(pack("N", $bb), 0, 4 - $num);
87             }
88 2 50 33     5 if (!$isend && $i > length($str)) {
89 0         0 $self->{'incache'} = substr($str, $i - 5);
90             }
91              
92 2         8 return $res;
93             }
94              
95             1;