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   68504 use base 'PDF::API2::Basic::PDF::Filter';
  43         100  
  43         5086  
4              
5 43     43   295 use strict;
  43         98  
  43         880  
6 43     43   274 use warnings;
  43         120  
  43         14536  
7              
8             our $VERSION = '2.044'; # 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 101 my ($self, $string, $include_eod) = @_;
15              
16             # Each byte of the input string gets encoded as two hexadecimal
17             # characters.
18 4         32 $string =~ s/(.)/sprintf('%02x', ord($1))/ges;
  815         2001  
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       16 $string .= '>' if $include_eod;
25              
26 4         24 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         98 $string =~ s/\s//og;
34              
35             # "A GREATER-THAN SIGN (3Eh) indicates EOD."
36 8         22 my $has_eod_marker = 0;
37 8 100       47 if (substr($string, -1, 1) eq '>') {
38 3         5 $has_eod_marker = 1;
39 3         8 chop $string;
40             }
41              
42             # "Any other characters [than 0-9, A-F, or a-f] shall cause an
43             # error."
44 8 100       40 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     42 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         40 $string =~ s/([0-9A-Fa-f]{2})/pack("C", hex($1))/oge;
  194         517  
57              
58 7         37 return $string;
59             }
60              
61             1;