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 45     45   141978 use base 'PDF::Builder::Basic::PDF::Filter';
  45         105  
  45         8676  
4              
5 45     45   319 use strict;
  45         97  
  45         1486  
6 45     45   327 use warnings;
  45         169  
  45         21711  
7              
8             our $VERSION = '3.028'; # VERSION
9             our $LAST_UPDATE = '3.027'; # 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             Inherits from L<PDF::Builder::Basic::PDF::Filter>
16              
17             =cut
18              
19             # Maintainer's Note: ASCIIHexDecode is described in the PDF 1.7 spec
20             # in section 7.4.2.
21              
22             sub outfilt {
23 3     3 1 276393 my ($self, $string, $include_eod) = @_;
24              
25             # Each byte of the input string gets encoded as two hexadecimal
26             # characters.
27 3         25 $string =~ s/(.)/sprintf('%02x', ord($1))/oge;
  812         1749  
28              
29             # The EOD (end-of-document) marker is a greater-than sign
30 3 100       16 $string .= '>' if $include_eod;
31              
32 3         25 return $string;
33             }
34              
35             sub infilt {
36 8     8 1 450 my ($self, $string) = @_;
37              
38             # "All white-space characters shall be ignored."
39 8         86 $string =~ s/\s//og;
40              
41             # "A GREATER-THAN SIGN (3Eh) indicates EOD."
42 8         18 my $has_eod_marker = 0;
43 8 100       42 if (substr($string, -1, 1) eq '>') {
44 3         7 $has_eod_marker = 1;
45 3         8 chop $string;
46             }
47              
48             # "Any other characters [than 0-9, A-F, or a-f] shall cause an
49             # error."
50 8 100       42 die "Illegal character found in ASCII hex-encoded stream"
51             if $string =~ /[^0-9A-Fa-f]/;
52              
53             # "If the filter encounters the EOD marker after reading an odd
54             # number of hexadecimal digits, it shall behave as if a 0 (zero)
55             # followed the last digit."
56 7 100 100     35 if ($has_eod_marker and length($string) % 2 == 1) {
57 1         3 $string .= '0';
58             }
59              
60             # "The ASCIIHexDecode filter shall produce one byte of binary data
61             # for each pair of ASCII hexadecimal digits."
62 7         48 $string =~ s/([0-9A-Fa-f]{2})/pack("C", hex($1))/oge;
  194         685  
63              
64 7         73 return $string;
65             }
66              
67             1;