| 1 | package PDF::API2::Basic::PDF::Filter::RunLengthDecode; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 43 | ▶ | use base 'PDF::API2::Basic::PDF::Filter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 43 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 43 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 43 | our $VERSION = '2.044'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Maintainer's Note: RunLengthDecode is described in the PDF 1.7 spec | ||||||||||||||||||||||||||||
| 11 | # in section 7.4.5. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | sub outfilt { | ||||||||||||||||||||||||||||
| 14 | 2 | ▶ | my ($self, $input, $include_eod) = @_; | ||||||||||||||||||||||||||
|
Subroutine
outfilt: called CC=7 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 2 | my $output; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | while ($input ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 6 | my ($unrepeated, $repeated); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Look for a repeated character (which can be repeated up to | ||||||||||||||||||||||||||||
| 21 | # 127 times) | ||||||||||||||||||||||||||||
| 22 | 6 | ▶ | if ($input =~ m/^(.*?)((.)\3{1,127})(.*)$/so) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 6 | $unrepeated = $1; | |||||||||||||||||||||||||||
| 24 | 6 | $repeated = $2; | |||||||||||||||||||||||||||
| 25 | 6 | $input = $4; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | else { | ||||||||||||||||||||||||||||
| 28 | 0 | $unrepeated = $input; | |||||||||||||||||||||||||||
| 29 | 0 | $input = ''; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Print any non-repeating bytes at the beginning of the input | ||||||||||||||||||||||||||||
| 33 | # in chunks of up to 128 bytes, prefixed with a run-length (0 | ||||||||||||||||||||||||||||
| 34 | # to 127, signifying 1 to 128 bytes) | ||||||||||||||||||||||||||||
| 35 | 6 | ▶ | while (length($unrepeated) > 127) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $output .= pack('C', 127) . substr($unrepeated, 0, 128); | |||||||||||||||||||||||||||
| 37 | 0 | substr($unrepeated, 0, 128) = ''; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 6 | ▶ | $output .= pack('C', length($unrepeated) - 1) . $unrepeated if length($unrepeated) > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # Then print the number of times the repeated byte was | ||||||||||||||||||||||||||||
| 42 | # repeated (using the formula "257 - length" to give a result | ||||||||||||||||||||||||||||
| 43 | # in the 129-255 range) followed by the byte to be repeated | ||||||||||||||||||||||||||||
| 44 | 6 | ▶ | if (length($repeated)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 6 | $output .= pack('C', 257 - length($repeated)) . substr($repeated, 0, 1); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # A byte value of 128 signifies that we're done. | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | $output .= "\x80" if $include_eod; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 2 | return $output; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub infilt { | ||||||||||||||||||||||||||||
| 56 | 2 | ▶ | my ($self, $input, $is_terminated) = @_; | ||||||||||||||||||||||||||
|
Subroutine
infilt: called CC=9 SCAR=24.7
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | 2 | my ($output, $length); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # infilt may be called multiple times, and is expected to continue | ||||||||||||||||||||||||||||
| 60 | # where it left off | ||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if (exists $self->{'incache'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $input = $self->{'incache'} . $input; | |||||||||||||||||||||||||||
| 63 | 0 | delete $self->{'incache'}; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | while (length($input)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | # Read a length byte | ||||||||||||||||||||||||||||
| 68 | 11 | $length = unpack("C", $input); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # A "length" of 128 represents the end of the document | ||||||||||||||||||||||||||||
| 71 | 11 | ▶ | if ($length == 128) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1 | return $output; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # Any other length needs to be followed by at least one other byte | ||||||||||||||||||||||||||||
| 76 | 10 | ▶ | if (length($input) == 1 and not $is_terminated) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $input == 1 and not $is_terminated
MC/DC0%0/2
length $input == 1 and not $is_terminated
length $input == 1
not $is_terminated
Truth table33%1/3
A and not(B)
A= length $input == 1B= not $is_terminated
| |||||||||||||||||||||||||||||
| 77 | 0 | die "Premature end to RunLengthEncoded data"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # A length of 129-255 represents a repeated string | ||||||||||||||||||||||||||||
| 81 | # (number of repeats = 257 - length) | ||||||||||||||||||||||||||||
| 82 | 10 | ▶ | if ($length > 128) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 6 | ▶ | if (length($input) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | # Out of data. Defer until the next call. | ||||||||||||||||||||||||||||
| 85 | 0 | $self->{'incache'} = $input; | |||||||||||||||||||||||||||
| 86 | 0 | return $output; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 6 | $output .= substr($input, 1, 1) x (257 - $length); | |||||||||||||||||||||||||||
| 89 | 6 | substr($input, 0, 2) = ''; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Any other length (under 128) represents a non-repeated | ||||||||||||||||||||||||||||
| 93 | # stream of bytes (with a length of 0 to 127 representing 1 to | ||||||||||||||||||||||||||||
| 94 | # 128 bytes) | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | 4 | ▶ | if (length($input) < $length + 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | # Insufficient data. Defer until the next call. | ||||||||||||||||||||||||||||
| 98 | 0 | $self->{'incache'} = $input; | |||||||||||||||||||||||||||
| 99 | 0 | return $output; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 4 | $output .= substr($input, 1, $length + 1); | |||||||||||||||||||||||||||
| 102 | 4 | substr($input, 0, $length + 2) = ''; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | return $output; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 43 | 1; | |||||||||||||||||||||||||||