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 40     40   71813 use base 'PDF::Builder::Basic::PDF::Filter';
  40         104  
  40         4724  
4              
5 40     40   258 use strict;
  40         89  
  40         864  
6 40     40   191 use warnings;
  40         88  
  40         12925  
7              
8             our $VERSION = '3.023'; # 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 108 my ($self, $string, $include_eod) = @_;
22              
23             # Each byte of the input string gets encoded as two hexadecimal
24             # characters.
25 3         27 $string =~ s/(.)/sprintf('%02x', ord($1))/oge;
  812         2133  
26              
27             # The EOD (end-of-document) marker is a greater-than sign
28 3 100       21 $string .= '>' if $include_eod;
29              
30 3         20 return $string;
31             }
32              
33             sub infilt {
34 8     8 1 321 my ($self, $string) = @_;
35              
36             # "All white-space characters shall be ignored."
37 8         61 $string =~ s/\s//og;
38              
39             # "A GREATER-THAN SIGN (3Eh) indicates EOD."
40 8         18 my $has_eod_marker = 0;
41 8 100       33 if (substr($string, -1, 1) eq '>') {
42 3         6 $has_eod_marker = 1;
43 3         9 chop $string;
44             }
45              
46             # "Any other characters [than 0-9, A-F, or a-f] shall cause an
47             # error."
48 8 100       49 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     37 if ($has_eod_marker and length($string) % 2 == 1) {
55 1         3 $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         76 $string =~ s/([0-9A-Fa-f]{2})/pack("C", hex($1))/oge;
  194         566  
61              
62 7         39 return $string;
63             }
64              
65             1;