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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
<?php
// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue // SPDX-License-Identifier: BSD-3-Clause
declare(strict_types=1);
namespace SimplePie;
/** * Decode 'gzip' encoded HTTP data * * @link http://www.gzip.org/format.txt * @link https://www.php.net/manual/en/function.gzdecode.php * @deprecated since SimplePie 1.9.0, use `gzdecode` function instead. */ class Gzdecode { /** * Compressed data * * @access private * @var string * @see gzdecode::$data */ public $compressed_data;
/** * Size of compressed data * * @access private * @var int */ public $compressed_size;
/** * Minimum size of a valid gzip string * * @access private * @var int */ public $min_compressed_size = 18;
/** * Current position of pointer * * @access private * @var int */ public $position = 0;
/** * Flags (FLG) * * @access private * @var int */ public $flags;
/** * Uncompressed data * * @access public * @see gzdecode::$compressed_data * @var string */ public $data;
/** * Modified time * * @access public * @var int */ public $MTIME;
/** * Extra Flags * * @access public * @var int */ public $XFL;
/** * Operating System * * @access public * @var int */ public $OS;
/** * Subfield ID 1 * * @access public * @see gzdecode::$extra_field * @see gzdecode::$SI2 * @var string */ public $SI1;
/** * Subfield ID 2 * * @access public * @see gzdecode::$extra_field * @see gzdecode::$SI1 * @var string */ public $SI2;
/** * Extra field content * * @access public * @see gzdecode::$SI1 * @see gzdecode::$SI2 * @var string */ public $extra_field;
/** * Original filename * * @access public * @var string */ public $filename;
/** * Human readable comment * * @access public * @var string */ public $comment;
/** * Don't allow anything to be set * * @param string $name * @param mixed $value */ public function __set(string $name, $value) { throw new Exception("Cannot write property $name"); }
/** * Set the compressed string and related properties * * @param string $data */ public function __construct(string $data) { $this->compressed_data = $data; $this->compressed_size = strlen($data); }
/** * Decode the GZIP stream * * @return bool Successfulness */ public function parse() { if ($this->compressed_size >= $this->min_compressed_size) { $len = 0;
// Check ID1, ID2, and CM if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08") { return false; }
// Get the FLG (FLaGs) $this->flags = ord($this->compressed_data[3]);
// FLG bits above (1 << 4) are reserved if ($this->flags > 0x1F) { return false; }
// Advance the pointer after the above $this->position += 4;
// MTIME $mtime = substr($this->compressed_data, $this->position, 4); // Reverse the string if we're on a big-endian arch because l is the only signed long and is machine endianness if (current((array) unpack('S', "\x00\x01")) === 1) { $mtime = strrev($mtime); } $this->MTIME = current((array) unpack('l', $mtime)); $this->position += 4;
// Get the XFL (eXtra FLags) $this->XFL = ord($this->compressed_data[$this->position++]);
// Get the OS (Operating System) $this->OS = ord($this->compressed_data[$this->position++]);
// Parse the FEXTRA if ($this->flags & 4) { // Read subfield IDs $this->SI1 = $this->compressed_data[$this->position++]; $this->SI2 = $this->compressed_data[$this->position++];
// SI2 set to zero is reserved for future use if ($this->SI2 === "\x00") { return false; }
// Get the length of the extra field $len = current((array) unpack('v', substr($this->compressed_data, $this->position, 2))); $this->position += 2;
// Check the length of the string is still valid $this->min_compressed_size += $len + 4; if ($this->compressed_size >= $this->min_compressed_size) { // Set the extra field to the given data $this->extra_field = substr($this->compressed_data, $this->position, $len); $this->position += $len; } else { return false; } }
// Parse the FNAME if ($this->flags & 8) { // Get the length of the filename $len = strcspn($this->compressed_data, "\x00", $this->position);
// Check the length of the string is still valid $this->min_compressed_size += $len + 1; if ($this->compressed_size >= $this->min_compressed_size) { // Set the original filename to the given string $this->filename = substr($this->compressed_data, $this->position, $len); $this->position += $len + 1; } else { return false; } }
// Parse the FCOMMENT if ($this->flags & 16) { // Get the length of the comment $len = strcspn($this->compressed_data, "\x00", $this->position);
// Check the length of the string is still valid $this->min_compressed_size += $len + 1; if ($this->compressed_size >= $this->min_compressed_size) { // Set the original comment to the given string $this->comment = substr($this->compressed_data, $this->position, $len); $this->position += $len + 1; } else { return false; } }
// Parse the FHCRC if ($this->flags & 2) { // Check the length of the string is still valid $this->min_compressed_size += $len + 2; if ($this->compressed_size >= $this->min_compressed_size) { // Read the CRC $crc = current((array) unpack('v', substr($this->compressed_data, $this->position, 2)));
// Check the CRC matches if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc) { $this->position += 2; } else { return false; } } else { return false; } }
// Decompress the actual data if (($data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false) { return false; }
$this->data = $data; $this->position = $this->compressed_size - 8;
// Check CRC of data $crc = current((array) unpack('V', substr($this->compressed_data, $this->position, 4))); $this->position += 4; /*if (extension_loaded('hash') && sprintf('%u', current(unpack('V', hash('crc32b', $this->data)))) !== sprintf('%u', $crc)) { return false; }*/
// Check ISIZE of data $isize = current((array) unpack('V', substr($this->compressed_data, $this->position, 4))); $this->position += 4; if (sprintf('%u', strlen($this->data) & 0xFFFFFFFF) !== sprintf('%u', $isize)) { return false; }
// Wow, against all odds, we've actually got a valid gzip string return true; }
return false; } }
class_alias('SimplePie\Gzdecode', 'SimplePie_gzdecode');
|