File Coverage

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


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