5 * A PHP-Based RSS and Atom Feed Framework.
6 * Takes the hard work out of managing a complete RSS/Atom solution.
8 * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
11 * Redistribution and use in source and binary forms, with or without modification, are
12 * permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice, this list of
15 * conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above copyright notice, this list
18 * of conditions and the following disclaimer in the documentation and/or other materials
19 * provided with the distribution.
21 * * Neither the name of the SimplePie Team nor the names of its contributors may be used
22 * to endorse or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
26 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
28 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
39 * @author Geoffrey Sneddon
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
47 * Decode 'gzip' encoded HTTP data
51 * @link http://www.gzip.org/format.txt
53 class SimplePie_gzdecode
60 * @see gzdecode::$data
65 * Size of compressed data
73 * Minimum size of a valid gzip string
78 var $min_compressed_size = 18;
81 * Current position of pointer
100 * @see gzdecode::$compressed_data
133 * @see gzdecode::$extra_field
134 * @see gzdecode::$SI2
143 * @see gzdecode::$extra_field
144 * @see gzdecode::$SI1
150 * Extra field content
153 * @see gzdecode::$SI1
154 * @see gzdecode::$SI2
168 * Human readable comment
176 * Don't allow anything to be set
178 * @param string $name
179 * @param mixed $value
181 public function __set($name, $value)
183 trigger_error("Cannot write property $name", E_USER_ERROR);
187 * Set the compressed string and related properties
189 * @param string $data
191 public function __construct($data)
193 $this->compressed_data = $data;
194 $this->compressed_size = strlen($data);
198 * Decode the GZIP stream
200 * @return bool Successfulness
202 public function parse()
204 if ($this->compressed_size >= $this->min_compressed_size)
206 // Check ID1, ID2, and CM
207 if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08")
212 // Get the FLG (FLaGs)
213 $this->flags = ord($this->compressed_data[3]);
215 // FLG bits above (1 << 4) are reserved
216 if ($this->flags > 0x1F)
221 // Advance the pointer after the above
222 $this->position += 4;
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)
229 $mtime = strrev($mtime);
231 $this->MTIME = current(unpack('l', $mtime));
232 $this->position += 4;
234 // Get the XFL (eXtra FLags)
235 $this->XFL = ord($this->compressed_data[$this->position++]);
237 // Get the OS (Operating System)
238 $this->OS = ord($this->compressed_data[$this->position++]);
241 if ($this->flags & 4)
244 $this->SI1 = $this->compressed_data[$this->position++];
245 $this->SI2 = $this->compressed_data[$this->position++];
247 // SI2 set to zero is reserved for future use
248 if ($this->SI2 === "\x00")
253 // Get the length of the extra field
254 $len = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
255 $this->position += 2;
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)
261 // Set the extra field to the given data
262 $this->extra_field = substr($this->compressed_data, $this->position, $len);
263 $this->position += $len;
272 if ($this->flags & 8)
274 // Get the length of the filename
275 $len = strcspn($this->compressed_data, "\x00", $this->position);
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)
281 // Set the original filename to the given string
282 $this->filename = substr($this->compressed_data, $this->position, $len);
283 $this->position += $len + 1;
291 // Parse the FCOMMENT
292 if ($this->flags & 16)
294 // Get the length of the comment
295 $len = strcspn($this->compressed_data, "\x00", $this->position);
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)
301 // Set the original comment to the given string
302 $this->comment = substr($this->compressed_data, $this->position, $len);
303 $this->position += $len + 1;
312 if ($this->flags & 2)
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)
319 $crc = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
321 // Check the CRC matches
322 if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc)
324 $this->position += 2;
337 // Decompress the actual data
338 if (($this->data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false)
344 $this->position = $this->compressed_size - 8;
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))
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))
363 // Wow, against all odds, we've actually got a valid gzip string