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 43     43   72309 use base 'PDF::API2::Basic::PDF::Filter';
  43         106  
  43         4939  
4              
5 43     43   297 use strict;
  43         97  
  43         929  
6 43     43   250 use warnings;
  43         135  
  43         15189  
7              
8             our $VERSION = '2.043'; # 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 107 my ($self, $string, $include_eod) = @_;
15              
16             # Each byte of the input string gets encoded as two hexadecimal
17             # characters.
18 4         30 $string =~ s/(.)/sprintf('%02x', ord($1))/ges;
  815         1918  
19              
20             # Wrap after 72 characters
21 4         44 $string =~ s/(.{72})/$1\n/g;
22              
23             # The EOD (end-of-document) marker is a greater-than sign
24 4 100       18 $string .= '>' if $include_eod;
25              
26 4         23 return $string;
27             }
28              
29             sub infilt {
30 8     8 1 319 my ($self, $string) = @_;
31              
32             # "All white-space characters shall be ignored."
33 8         65 $string =~ s/\s//og;
34              
35             # "A GREATER-THAN SIGN (3Eh) indicates EOD."
36 8         19 my $has_eod_marker = 0;
37 8 100       33 if (substr($string, -1, 1) eq '>') {
38 3         5 $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     45 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         45 $string =~ s/([0-9A-Fa-f]{2})/pack("C", hex($1))/oge;
  194         572  
57              
58 7         39 return $string;
59             }
60              
61             1;