-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfilters.php
91 lines (78 loc) · 2.24 KB
/
filters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
include_once 'generic.php';
function decompress($data) {
return gzuncompress($data);
}
function compress($data) {
return gzcompress($data);
}
class FlateDecode {
function decode($data, $decode_parms) {
$data = decompress($data);
$predictor = 1;
if ($decode_parms) {
$predictor = $decode_parms['/Predictor'];
}
if (!$predictor) {
$predictor = 1;
}
if ($predictor != 1) {
$columns = $decode_parms['/Columns'];
if (($predictor >= 10) && ($predictor <= 15)) {
$output = '';
$rowlength = $columns + 1;
assert((strlen($data) % $rowlength) == 0);
$prev_rowdata = array_fill(0, $rowlength, 0);
for ($row=0; $row<strlen($data)/$rowlength; $row++) {
$rowdata = array();
$k = 0;
for ($j=$row*$rowlength; $j<($row+1)*$rowlength; $j++) {
$rowdata[$k] = ord($data[$j]);
$k += 1;
}
$filter_byte = $rowdata[0];
if ($filter_byte == 0) {
} else if ($filter_byte == 1) {
for ($j=2; $j<$rowlength; $j++) {
$rowdata[$j] = ($rowdata[$j] + $rowdata[$j-1]) % 256;
}
} else if ($filter_byte == 2) {
for ($j=1; $j<$rowlength; $j++) {
$rowdata[$j] = ($rowdata[$j] + $prev_rowdata[$j]) % 256;
}
} else {
die("Error reading PDF: unsupported PNG filter $filter_byte.");
}
$prev_rowdata = $rowdata;
for ($j=1; $j<count($rowdata); $j++) {
$output .= chr($rowdata[$j]);
}
}
$data = $output;
} else {
die("Error reading PDF: unsupported flatedecode predictor $predictor.");
}
}
return $data;
}
function encode($data) {
return compress($data);
}
}
function decode_stream_data($stream) {
$filters = $stream->data['/Filter'];
if (!$filters) {
$filters = array();
}
if (count($filters) && !is_a($filters[0], 'NameObject')) {
$filters = array($filters);
}
$data = $stream->stream;
foreach ($filters as $filter_type) {
if ($filter_type == '/FlateDecode') {
$data = FlateDecode::decode($data, $stream->data['/DecodeParms']);
}
}
return $data;
}
?>