|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is based on code of tecnickcom/TCPDF PDF library. |
|
5
|
|
|
* |
|
6
|
|
|
* Original author Nicola Asuni ([email protected]) and |
|
7
|
|
|
* contributors (https://github.com/tecnickcom/TCPDF/graphs/contributors). |
|
8
|
|
|
* |
|
9
|
|
|
* @see https://github.com/tecnickcom/TCPDF |
|
10
|
|
|
* |
|
11
|
|
|
* Original code was licensed on the terms of the LGPL v3. |
|
12
|
|
|
* |
|
13
|
|
|
* ------------------------------------------------------------------------------ |
|
14
|
|
|
* |
|
15
|
|
|
* @file This file is part of the PdfParser library. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Konrad Abicht <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* @date 2020-01-06 |
|
20
|
|
|
* |
|
21
|
|
|
* @license LGPLv3 |
|
22
|
|
|
* |
|
23
|
|
|
* @url <https://github.com/smalot/pdfparser> |
|
24
|
|
|
* |
|
25
|
|
|
* PdfParser is a pdf library written in PHP, extraction oriented. |
|
26
|
|
|
* Copyright (C) 2017 - Sébastien MALOT <[email protected]> |
|
27
|
|
|
* |
|
28
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
29
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
30
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
31
|
|
|
* (at your option) any later version. |
|
32
|
|
|
* |
|
33
|
|
|
* This program is distributed in the hope that it will be useful, |
|
34
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
35
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
36
|
|
|
* GNU Lesser General Public License for more details. |
|
37
|
|
|
* |
|
38
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
39
|
|
|
* along with this program. |
|
40
|
|
|
* If not, see <http://www.pdfparser.org/sites/default/LICENSE.txt>. |
|
41
|
|
|
*/ |
|
42
|
|
|
|
|
43
|
|
|
namespace Smalot\PdfParser\RawData; |
|
44
|
|
|
|
|
45
|
|
|
use Smalot\PdfParser\Exception\NotImplementedException; |
|
46
|
|
|
|
|
47
|
|
|
class FilterHelper |
|
48
|
|
|
{ |
|
49
|
|
|
protected $availableFilters = ['ASCIIHexDecode', 'ASCII85Decode', 'LZWDecode', 'FlateDecode', 'RunLengthDecode']; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Decode data using the specified filter type. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $filter Filter name |
|
55
|
|
|
* @param string $data Data to decode |
|
56
|
|
|
* |
|
57
|
|
|
* @return string Decoded data string |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
* @throws \Smalot\PdfParser\Exception\NotImplementedException if a certain decode function is not implemented yet |
|
61
|
|
|
*/ |
|
62
|
|
|
public function decodeFilter(string $filter, string $data, int $decodeMemoryLimit = 0): string |
|
63
|
|
|
{ |
|
64
|
|
|
switch ($filter) { |
|
65
|
|
|
case 'ASCIIHexDecode': |
|
66
|
|
|
return $this->decodeFilterASCIIHexDecode($data); |
|
67
|
|
|
|
|
68
|
|
|
case 'ASCII85Decode': |
|
69
|
|
|
return $this->decodeFilterASCII85Decode($data); |
|
70
|
|
|
|
|
71
|
|
|
case 'LZWDecode': |
|
72
|
|
|
return $this->decodeFilterLZWDecode($data); |
|
73
|
|
|
|
|
74
|
|
|
case 'FlateDecode': |
|
75
|
|
|
return $this->decodeFilterFlateDecode($data, $decodeMemoryLimit); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
case 'RunLengthDecode': |
|
78
|
|
|
return $this->decodeFilterRunLengthDecode($data); |
|
79
|
|
|
|
|
80
|
|
|
case 'CCITTFaxDecode': |
|
81
|
|
|
throw new NotImplementedException('Decode CCITTFaxDecode not implemented yet.'); |
|
82
|
|
|
case 'JBIG2Decode': |
|
83
|
|
|
throw new NotImplementedException('Decode JBIG2Decode not implemented yet.'); |
|
84
|
|
|
case 'DCTDecode': |
|
85
|
|
|
throw new NotImplementedException('Decode DCTDecode not implemented yet.'); |
|
86
|
|
|
case 'JPXDecode': |
|
87
|
|
|
throw new NotImplementedException('Decode JPXDecode not implemented yet.'); |
|
88
|
|
|
case 'Crypt': |
|
89
|
|
|
throw new NotImplementedException('Decode Crypt not implemented yet.'); |
|
90
|
|
|
default: |
|
91
|
|
|
return $data; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* ASCIIHexDecode |
|
97
|
|
|
* |
|
98
|
|
|
* Decodes data encoded in an ASCII hexadecimal representation, reproducing the original binary data. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $data Data to decode |
|
101
|
|
|
* |
|
102
|
|
|
* @return string data string |
|
103
|
|
|
* |
|
104
|
|
|
* @throws \Exception |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function decodeFilterASCIIHexDecode(string $data): string |
|
107
|
|
|
{ |
|
108
|
|
|
// all white-space characters shall be ignored |
|
109
|
|
|
$data = preg_replace('/[\s]/', '', $data); |
|
110
|
|
|
// check for EOD character: GREATER-THAN SIGN (3Eh) |
|
111
|
|
|
$eod = strpos($data, '>'); |
|
112
|
|
|
if (false !== $eod) { |
|
113
|
|
|
// remove EOD and extra data (if any) |
|
114
|
|
|
$data = substr($data, 0, $eod); |
|
115
|
|
|
$eod = true; |
|
116
|
|
|
} |
|
117
|
|
|
// get data length |
|
118
|
|
|
$data_length = \strlen($data); |
|
119
|
|
|
if (0 != ($data_length % 2)) { |
|
120
|
|
|
// odd number of hexadecimal digits |
|
121
|
|
|
if ($eod) { |
|
122
|
|
|
// EOD shall behave as if a 0 (zero) followed the last digit |
|
123
|
|
|
$data = substr($data, 0, -1).'0'.substr($data, -1); |
|
124
|
|
|
} else { |
|
125
|
|
|
throw new \Exception('decodeFilterASCIIHexDecode: invalid code'); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
// check for invalid characters |
|
129
|
|
|
if (preg_match('/[^a-fA-F\d]/', $data) > 0) { |
|
130
|
|
|
throw new \Exception('decodeFilterASCIIHexDecode: invalid code'); |
|
131
|
|
|
} |
|
132
|
|
|
// get one byte of binary data for each pair of ASCII hexadecimal digits |
|
133
|
|
|
$decoded = pack('H*', $data); |
|
134
|
|
|
|
|
135
|
|
|
return $decoded; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* ASCII85Decode |
|
140
|
|
|
* |
|
141
|
|
|
* Decodes data encoded in an ASCII base-85 representation, reproducing the original binary data. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $data Data to decode |
|
144
|
|
|
* |
|
145
|
|
|
* @return string data string |
|
146
|
|
|
* |
|
147
|
|
|
* @throws \Exception |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function decodeFilterASCII85Decode(string $data): string |
|
150
|
|
|
{ |
|
151
|
|
|
// initialize string to return |
|
152
|
|
|
$decoded = ''; |
|
153
|
|
|
// all white-space characters shall be ignored |
|
154
|
|
|
$data = preg_replace('/[\s]/', '', $data); |
|
155
|
|
|
// remove start sequence 2-character sequence <~ (3Ch)(7Eh) |
|
156
|
|
|
if (0 === strpos($data, '<~')) { |
|
157
|
|
|
// remove EOD and extra data (if any) |
|
158
|
|
|
$data = substr($data, 2); |
|
159
|
|
|
} |
|
160
|
|
|
// check for EOD: 2-character sequence ~> (7Eh)(3Eh) |
|
161
|
|
|
$eod = strpos($data, '~>'); |
|
162
|
|
|
if (\strlen($data) - 2 === $eod) { |
|
163
|
|
|
// remove EOD and extra data (if any) |
|
164
|
|
|
$data = substr($data, 0, $eod); |
|
165
|
|
|
} |
|
166
|
|
|
// data length |
|
167
|
|
|
$data_length = \strlen($data); |
|
168
|
|
|
// check for invalid characters |
|
169
|
|
|
if (preg_match('/[^\x21-\x75,\x74]/', $data) > 0) { |
|
170
|
|
|
throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
|
171
|
|
|
} |
|
172
|
|
|
// z sequence |
|
173
|
|
|
$zseq = \chr(0).\chr(0).\chr(0).\chr(0); |
|
174
|
|
|
// position inside a group of 4 bytes (0-3) |
|
175
|
|
|
$group_pos = 0; |
|
176
|
|
|
$tuple = 0; |
|
177
|
|
|
$pow85 = [85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1]; |
|
178
|
|
|
|
|
179
|
|
|
// for each byte |
|
180
|
|
|
for ($i = 0; $i < $data_length; ++$i) { |
|
181
|
|
|
// get char value |
|
182
|
|
|
$char = \ord($data[$i]); |
|
183
|
|
|
if (122 == $char) { // 'z' |
|
184
|
|
|
if (0 == $group_pos) { |
|
185
|
|
|
$decoded .= $zseq; |
|
186
|
|
|
} else { |
|
187
|
|
|
throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
|
188
|
|
|
} |
|
189
|
|
|
} else { |
|
190
|
|
|
// the value represented by a group of 5 characters should never be greater than 2^32 - 1 |
|
191
|
|
|
$tuple += (($char - 33) * $pow85[$group_pos]); |
|
192
|
|
|
if (4 == $group_pos) { |
|
193
|
|
|
// The following if-clause is an attempt to fix/suppress the following deprecation warning: |
|
194
|
|
|
// chr(): Providing a value not in-between 0 and 255 is deprecated, this is because a byte value |
|
195
|
|
|
// must be in the [0, 255] interval. The value used will be constrained using % 256 |
|
196
|
|
|
if ( |
|
197
|
|
|
255 < $tuple >> 24 |
|
198
|
|
|
|| 255 < $tuple >> 16 |
|
199
|
|
|
|| 255 < $tuple >> 8 |
|
200
|
|
|
) { |
|
201
|
|
|
$tuple = $tuple % 256; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
$decoded .= \chr($tuple >> 24) |
|
205
|
|
|
.\chr($tuple >> 16) |
|
206
|
|
|
.\chr($tuple >> 8) |
|
207
|
|
|
.\chr($tuple); |
|
208
|
|
|
$tuple = 0; |
|
209
|
|
|
$group_pos = 0; |
|
210
|
|
|
} else { |
|
211
|
|
|
++$group_pos; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
if ($group_pos > 1) { |
|
216
|
|
|
$tuple += $pow85[$group_pos - 1]; |
|
217
|
|
|
} |
|
218
|
|
|
// last tuple (if any) |
|
219
|
|
|
switch ($group_pos) { |
|
220
|
|
|
case 4: |
|
221
|
|
|
$decoded .= \chr($tuple >> 24).\chr($tuple >> 16).\chr($tuple >> 8); |
|
222
|
|
|
break; |
|
223
|
|
|
|
|
224
|
|
|
case 3: |
|
225
|
|
|
$decoded .= \chr($tuple >> 24).\chr($tuple >> 16); |
|
226
|
|
|
break; |
|
227
|
|
|
|
|
228
|
|
|
case 2: |
|
229
|
|
|
$decoded .= \chr($tuple >> 24); |
|
230
|
|
|
break; |
|
231
|
|
|
|
|
232
|
|
|
case 1: |
|
233
|
|
|
throw new \Exception('decodeFilterASCII85Decode: invalid code'); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return $decoded; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* FlateDecode |
|
241
|
|
|
* |
|
242
|
|
|
* Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data. |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $data Data to decode |
|
245
|
|
|
* @param int $decodeMemoryLimit Memory limit on deflation |
|
246
|
|
|
* |
|
247
|
|
|
* @return string data string |
|
248
|
|
|
* |
|
249
|
|
|
* @throws \Exception |
|
250
|
|
|
*/ |
|
251
|
|
|
protected function decodeFilterFlateDecode(string $data, int $decodeMemoryLimit): ?string |
|
252
|
|
|
{ |
|
253
|
|
|
// Uncatchable E_WARNING for "data error" is @ suppressed |
|
254
|
|
|
// so execution may proceed with an alternate decompression |
|
255
|
|
|
// method. |
|
256
|
|
|
$decoded = @gzuncompress($data, $decodeMemoryLimit); |
|
257
|
|
|
|
|
258
|
|
|
if (false === $decoded) { |
|
259
|
|
|
// If gzuncompress() failed, try again using the compress.zlib:// |
|
260
|
|
|
// wrapper to decode it in a file-based context. |
|
261
|
|
|
// See: https://www.php.net/manual/en/function.gzuncompress.php#79042 |
|
262
|
|
|
// Issue: https://github.com/smalot/pdfparser/issues/592 |
|
263
|
|
|
$ztmp = tmpfile(); |
|
264
|
|
|
if (false != $ztmp) { |
|
265
|
|
|
fwrite($ztmp, "\x1f\x8b\x08\x00\x00\x00\x00\x00".$data); |
|
266
|
|
|
$file = stream_get_meta_data($ztmp)['uri']; |
|
267
|
|
|
if (0 === $decodeMemoryLimit) { |
|
268
|
|
|
$decoded = file_get_contents('compress.zlib://'.$file); |
|
269
|
|
|
} else { |
|
270
|
|
|
$decoded = file_get_contents('compress.zlib://'.$file, false, null, 0, $decodeMemoryLimit); |
|
271
|
|
|
} |
|
272
|
|
|
fclose($ztmp); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
if (false === \is_string($decoded) || '' === $decoded) { |
|
277
|
|
|
// If the decoded string is empty, that means decoding failed. |
|
278
|
|
|
throw new \Exception('decodeFilterFlateDecode: invalid data'); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $decoded; |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* LZWDecode |
|
286
|
|
|
* |
|
287
|
|
|
* Decompresses data encoded using the LZW (Lempel-Ziv-Welch) adaptive compression method, reproducing the original text or binary data. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $data Data to decode |
|
290
|
|
|
* |
|
291
|
|
|
* @return string Data string |
|
292
|
|
|
*/ |
|
293
|
|
|
protected function decodeFilterLZWDecode(string $data): string |
|
294
|
|
|
{ |
|
295
|
|
|
// initialize string to return |
|
296
|
|
|
$decoded = ''; |
|
297
|
|
|
// data length |
|
298
|
|
|
$data_length = \strlen($data); |
|
299
|
|
|
// convert string to binary string |
|
300
|
|
|
$bitstring = ''; |
|
301
|
|
|
for ($i = 0; $i < $data_length; ++$i) { |
|
302
|
|
|
$bitstring .= \sprintf('%08b', \ord($data[$i])); |
|
303
|
|
|
} |
|
304
|
|
|
// get the number of bits |
|
305
|
|
|
$data_length = \strlen($bitstring); |
|
306
|
|
|
// initialize code length in bits |
|
307
|
|
|
$bitlen = 9; |
|
308
|
|
|
// initialize dictionary index |
|
309
|
|
|
$dix = 258; |
|
310
|
|
|
// initialize the dictionary (with the first 256 entries). |
|
311
|
|
|
$dictionary = []; |
|
312
|
|
|
for ($i = 0; $i < 256; ++$i) { |
|
313
|
|
|
$dictionary[$i] = \chr($i); |
|
314
|
|
|
} |
|
315
|
|
|
// previous val |
|
316
|
|
|
$prev_index = 0; |
|
317
|
|
|
// while we encounter EOD marker (257), read code_length bits |
|
318
|
|
|
while (($data_length > 0) && (257 != ($index = bindec(substr($bitstring, 0, $bitlen))))) { |
|
319
|
|
|
// remove read bits from string |
|
320
|
|
|
$bitstring = substr($bitstring, $bitlen); |
|
321
|
|
|
// update number of bits |
|
322
|
|
|
$data_length -= $bitlen; |
|
323
|
|
|
if (256 == $index) { // clear-table marker |
|
324
|
|
|
// reset code length in bits |
|
325
|
|
|
$bitlen = 9; |
|
326
|
|
|
// reset dictionary index |
|
327
|
|
|
$dix = 258; |
|
328
|
|
|
$prev_index = 256; |
|
329
|
|
|
// reset the dictionary (with the first 256 entries). |
|
330
|
|
|
$dictionary = []; |
|
331
|
|
|
for ($i = 0; $i < 256; ++$i) { |
|
332
|
|
|
$dictionary[$i] = \chr($i); |
|
333
|
|
|
} |
|
334
|
|
|
} elseif (256 == $prev_index) { |
|
335
|
|
|
// first entry |
|
336
|
|
|
$decoded .= $dictionary[$index]; |
|
337
|
|
|
$prev_index = $index; |
|
338
|
|
|
} else { |
|
339
|
|
|
// check if index exist in the dictionary |
|
340
|
|
|
if ($index < $dix) { |
|
341
|
|
|
// index exist on dictionary |
|
342
|
|
|
$decoded .= $dictionary[$index]; |
|
343
|
|
|
$dic_val = $dictionary[$prev_index].$dictionary[$index][0]; |
|
344
|
|
|
// store current index |
|
345
|
|
|
$prev_index = $index; |
|
346
|
|
|
} else { |
|
347
|
|
|
// index do not exist on dictionary |
|
348
|
|
|
$dic_val = $dictionary[$prev_index].$dictionary[$prev_index][0]; |
|
349
|
|
|
$decoded .= $dic_val; |
|
350
|
|
|
} |
|
351
|
|
|
// update dictionary |
|
352
|
|
|
$dictionary[$dix] = $dic_val; |
|
353
|
|
|
++$dix; |
|
354
|
|
|
// change bit length by case |
|
355
|
|
|
if (2047 == $dix) { |
|
356
|
|
|
$bitlen = 12; |
|
357
|
|
|
} elseif (1023 == $dix) { |
|
358
|
|
|
$bitlen = 11; |
|
359
|
|
|
} elseif (511 == $dix) { |
|
360
|
|
|
$bitlen = 10; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
return $decoded; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* RunLengthDecode |
|
370
|
|
|
* |
|
371
|
|
|
* Decompresses data encoded using a byte-oriented run-length encoding algorithm. |
|
372
|
|
|
* |
|
373
|
|
|
* @param string $data Data to decode |
|
374
|
|
|
*/ |
|
375
|
|
|
protected function decodeFilterRunLengthDecode(string $data): string |
|
376
|
|
|
{ |
|
377
|
|
|
// initialize string to return |
|
378
|
|
|
$decoded = ''; |
|
379
|
|
|
// data length |
|
380
|
|
|
$data_length = \strlen($data); |
|
381
|
|
|
$i = 0; |
|
382
|
|
|
while ($i < $data_length) { |
|
383
|
|
|
// get current byte value |
|
384
|
|
|
$byte = \ord($data[$i]); |
|
385
|
|
|
if (128 == $byte) { |
|
386
|
|
|
// a length value of 128 denote EOD |
|
387
|
|
|
break; |
|
388
|
|
|
} elseif ($byte < 128) { |
|
389
|
|
|
// if the length byte is in the range 0 to 127 |
|
390
|
|
|
// the following length + 1 (1 to 128) bytes shall be copied literally during decompression |
|
391
|
|
|
$decoded .= substr($data, $i + 1, $byte + 1); |
|
392
|
|
|
// move to next block |
|
393
|
|
|
$i += ($byte + 2); |
|
394
|
|
|
} else { |
|
395
|
|
|
// if length is in the range 129 to 255, |
|
396
|
|
|
// the following single byte shall be copied 257 - length (2 to 128) times during decompression |
|
397
|
|
|
$decoded .= str_repeat($data[$i + 1], 257 - $byte); |
|
398
|
|
|
// move to next block |
|
399
|
|
|
$i += 2; |
|
400
|
|
|
} |
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
return $decoded; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* @return array list of available filters |
|
408
|
|
|
*/ |
|
409
|
|
|
public function getAvailableFilters(): array |
|
410
|
|
|
{ |
|
411
|
|
|
return $this->availableFilters; |
|
412
|
|
|
} |
|
413
|
|
|
} |
|
414
|
|
|
|