File Coverage

blib/lib/PDF/API2/Basic/PDF/Filter/ASCIIHexDecode.pm
Criterion Covered Total %
statement 27 27 100.0
branch 8 8 100.0
condition 3 3 100.0
subroutine 5 5 100.0
pod 2 2 100.0
total 45 45 100.0


line stmt bran cond sub pod time code
1             package PDF::API2::Basic::PDF::Filter::ASCIIHexDecode;
2              
3 44     44   70242 use base 'PDF::API2::Basic::PDF::Filter';
  44         95  
  44         4649  
4              
5 44     44   299 use strict;
  44         94  
  44         973  
6 44     44   241 use warnings;
  44         116  
  44         14828  
7              
8             our $VERSION = '2.045'; # VERSION
9              
10             # Maintainer's Note: ASCIIHexDecode is described in the PDF 1.7 spec
11             # in section 7.4.2.
12              
13             sub outfilt {
14 4     4 1 100 my ($self, $string, $include_eod) = @_;
15              
16             # Each byte of the input string gets encoded as two hexadecimal
17             # characters.
18 4         24 $string =~ s/(.)/sprintf('%02x', ord($1))/ges;
  815         1931  
19              
20             # Wrap after 72 characters
21 4         32 $string =~ s/(.{72})/$1\n/g;
22              
23             # The EOD (end-of-document) marker is a greater-than sign
24 4 100       19 $string .= '>' if $include_eod;
25              
26 4         28 return $string;
27             }
28              
29             sub infilt {
30 8     8 1 435 my ($self, $string) = @_;
31              
32             # "All white-space characters shall be ignored."
33 8         63 $string =~ s/\s//og;
34              
35             # "A GREATER-THAN SIGN (3Eh) indicates EOD."
36 8         15 my $has_eod_marker = 0;
37 8 100       34 if (substr($string, -1, 1) eq '>') {
38 3         6 $has_eod_marker = 1;
39 3         9 chop $string;
40             }
41              
42             # "Any other characters [than 0-9, A-F, or a-f] shall cause an
43             # error."
44 8 100       41 die "Illegal character found in ASCII hex-encoded stream"
45             if $string =~ /[^0-9A-Fa-f]/;
46              
47             # "If the filter encounters the EOD marker after reading an odd
48             # number of hexadecimal digits, it shall behave as if a 0 (zero)
49             # followed the last digit."
50 7 100 100     41 if ($has_eod_marker and length($string) % 2 == 1) {
51 1         3 $string .= '0';
52             }
53              
54             # "The ASCIIHexDecode filter shall produce one byte of binary data
55             # for each pair of ASCII hexadecimal digits."
56 7         38 $string =~ s/([0-9A-Fa-f]{2})/pack("C", hex($1))/oge;
  194         532  
57              
58 7         35 return $string;
59             }
60              
61             1;