library/SimplePie/gzdecode.php Quellcode

gzdecode.php
gehe zur Dokumentation dieser Datei
1 <?php
54 {
63 
71 
79 
86  var $position = 0;
87 
94  var $flags;
95 
103  var $data;
104 
111  var $MTIME;
112 
119  var $XFL;
120 
127  var $OS;
128 
137  var $SI1;
138 
147  var $SI2;
148 
158 
166 
173  var $comment;
174 
181  public function __set($name, $value)
182  {
183  trigger_error("Cannot write property $name", E_USER_ERROR);
184  }
185 
191  public function __construct($data)
192  {
193  $this->compressed_data = $data;
194  $this->compressed_size = strlen($data);
195  }
196 
202  public function parse()
203  {
204  if ($this->compressed_size >= $this->min_compressed_size)
205  {
206  // Check ID1, ID2, and CM
207  if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08")
208  {
209  return false;
210  }
211 
212  // Get the FLG (FLaGs)
213  $this->flags = ord($this->compressed_data[3]);
214 
215  // FLG bits above (1 << 4) are reserved
216  if ($this->flags > 0x1F)
217  {
218  return false;
219  }
220 
221  // Advance the pointer after the above
222  $this->position += 4;
223 
224  // MTIME
225  $mtime = substr($this->compressed_data, $this->position, 4);
226  // Reverse the string if we're on a big-endian arch because l is the only signed long and is machine endianness
227  if (current(unpack('S', "\x00\x01")) === 1)
228  {
229  $mtime = strrev($mtime);
230  }
231  $this->MTIME = current(unpack('l', $mtime));
232  $this->position += 4;
233 
234  // Get the XFL (eXtra FLags)
235  $this->XFL = ord($this->compressed_data[$this->position++]);
236 
237  // Get the OS (Operating System)
238  $this->OS = ord($this->compressed_data[$this->position++]);
239 
240  // Parse the FEXTRA
241  if ($this->flags & 4)
242  {
243  // Read subfield IDs
244  $this->SI1 = $this->compressed_data[$this->position++];
245  $this->SI2 = $this->compressed_data[$this->position++];
246 
247  // SI2 set to zero is reserved for future use
248  if ($this->SI2 === "\x00")
249  {
250  return false;
251  }
252 
253  // Get the length of the extra field
254  $len = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
255  $this->position += 2;
256 
257  // Check the length of the string is still valid
258  $this->min_compressed_size += $len + 4;
259  if ($this->compressed_size >= $this->min_compressed_size)
260  {
261  // Set the extra field to the given data
262  $this->extra_field = substr($this->compressed_data, $this->position, $len);
263  $this->position += $len;
264  }
265  else
266  {
267  return false;
268  }
269  }
270 
271  // Parse the FNAME
272  if ($this->flags & 8)
273  {
274  // Get the length of the filename
275  $len = strcspn($this->compressed_data, "\x00", $this->position);
276 
277  // Check the length of the string is still valid
278  $this->min_compressed_size += $len + 1;
279  if ($this->compressed_size >= $this->min_compressed_size)
280  {
281  // Set the original filename to the given string
282  $this->filename = substr($this->compressed_data, $this->position, $len);
283  $this->position += $len + 1;
284  }
285  else
286  {
287  return false;
288  }
289  }
290 
291  // Parse the FCOMMENT
292  if ($this->flags & 16)
293  {
294  // Get the length of the comment
295  $len = strcspn($this->compressed_data, "\x00", $this->position);
296 
297  // Check the length of the string is still valid
298  $this->min_compressed_size += $len + 1;
299  if ($this->compressed_size >= $this->min_compressed_size)
300  {
301  // Set the original comment to the given string
302  $this->comment = substr($this->compressed_data, $this->position, $len);
303  $this->position += $len + 1;
304  }
305  else
306  {
307  return false;
308  }
309  }
310 
311  // Parse the FHCRC
312  if ($this->flags & 2)
313  {
314  // Check the length of the string is still valid
315  $this->min_compressed_size += $len + 2;
316  if ($this->compressed_size >= $this->min_compressed_size)
317  {
318  // Read the CRC
319  $crc = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
320 
321  // Check the CRC matches
322  if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc)
323  {
324  $this->position += 2;
325  }
326  else
327  {
328  return false;
329  }
330  }
331  else
332  {
333  return false;
334  }
335  }
336 
337  // Decompress the actual data
338  if (($this->data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false)
339  {
340  return false;
341  }
342  else
343  {
344  $this->position = $this->compressed_size - 8;
345  }
346 
347  // Check CRC of data
348  $crc = current(unpack('V', substr($this->compressed_data, $this->position, 4)));
349  $this->position += 4;
350  /*if (extension_loaded('hash') && sprintf('%u', current(unpack('V', hash('crc32b', $this->data)))) !== sprintf('%u', $crc))
351  {
352  return false;
353  }*/
354 
355  // Check ISIZE of data
356  $isize = current(unpack('V', substr($this->compressed_data, $this->position, 4)));
357  $this->position += 4;
358  if (sprintf('%u', strlen($this->data) & 0xFFFFFFFF) !== sprintf('%u', $isize))
359  {
360  return false;
361  }
362 
363  // Wow, against all odds, we've actually got a valid gzip string
364  return true;
365  }
366  else
367  {
368  return false;
369  }
370  }
371 }




Korrekturen, Hinweise und Ergänzungen

Bitte scheuen Sie sich nicht und melden Sie, was auf dieser Seite sachlich falsch oder irreführend ist, was ergänzt werden sollte, was fehlt usw. Dazu bitte oben aus dem Menü Seite den Eintrag Support Forum wählen. Es ist eine kostenlose Anmeldung erforderlich, um Anmerkungen zu posten. Unpassende Postings, Spam usw. werden kommentarlos entfernt.