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   144257 use base 'PDF::API2::Basic::PDF::Filter';
  44         96  
  44         6424  
4              
5 44     44   289 use strict;
  44         85  
  44         1129  
6 44     44   224 use warnings;
  44         96  
  44         20367  
7              
8             our $VERSION = '2.048'; # 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 270516 my ($self, $string, $include_eod) = @_;
15              
16             # Each byte of the input string gets encoded as two hexadecimal
17             # characters.
18 4         28 $string =~ s/(.)/sprintf('%02x', ord($1))/ges;
  815         2267  
19              
20             # Wrap after 72 characters
21 4         40 $string =~ s/(.{72})/$1\n/g;
22              
23             # The EOD (end-of-document) marker is a greater-than sign
24 4 100       45 $string .= '>' if $include_eod;
25              
26 4         39 return $string;
27             }
28              
29             sub infilt {
30 8     8 1 438 my ($self, $string) = @_;
31              
32             # "All white-space characters shall be ignored."
33 8         86 $string =~ s/\s//og;
34              
35             # "A GREATER-THAN SIGN (3Eh) indicates EOD."
36 8         26 my $has_eod_marker = 0;
37 8 100       41 if (substr($string, -1, 1) eq '>') {
38 3         7 $has_eod_marker = 1;
39 3         7 chop $string;
40             }
41              
42             # "Any other characters [than 0-9, A-F, or a-f] shall cause an
43             # error."
44 8 100       47 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     39 if ($has_eod_marker and length($string) % 2 == 1) {
51 1         4 $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         50 $string =~ s/([0-9A-Fa-f]{2})/pack("C", hex($1))/oge;
  194         639  
57              
58 7         46 return $string;
59             }
60              
61             1;