]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/simplepie/SimplePie/gzdecode.php
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-2009, 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-2010 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
43 * @todo phpDoc comments
52 class SimplePie_gzdecode
58 * @see gzdecode::$data
63 * Size of compressed data
70 * Minimum size of a valid gzip string
74 var $min_compressed_size = 18;
77 * Current position of pointer
94 * @see gzdecode::$compressed_data
123 * @see gzdecode::$extra_field
124 * @see gzdecode::$SI2
132 * @see gzdecode::$extra_field
133 * @see gzdecode::$SI1
138 * Extra field content
141 * @see gzdecode::$SI1
142 * @see gzdecode::$SI2
154 * Human readable comment
161 * Don't allow anything to be set
165 public function __set($name, $value)
167 trigger_error("Cannot write property $name", E_USER_ERROR);
171 * Set the compressed string and related properties
175 public function __construct($data)
177 $this->compressed_data = $data;
178 $this->compressed_size = strlen($data);
182 * Decode the GZIP stream
186 public function parse()
188 if ($this->compressed_size >= $this->min_compressed_size)
190 // Check ID1, ID2, and CM
191 if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08
")
196 // Get the FLG (FLaGs)
197 $this->flags = ord($this->compressed_data[3]);
199 // FLG bits above (1 << 4) are reserved
200 if ($this->flags > 0x1F)
205 // Advance the pointer after the above
206 $this->position += 4;
209 $mtime = substr($this->compressed_data, $this->position, 4);
210 // Reverse the string if we're on a big-endian arch because l is the only signed long and is machine endianness
211 if (current(unpack('S', "\x00\x01
")) === 1)
213 $mtime = strrev($mtime);
215 $this->MTIME = current(unpack('l', $mtime));
216 $this->position += 4;
218 // Get the XFL (eXtra FLags)
219 $this->XFL = ord($this->compressed_data[$this->position++]);
221 // Get the OS (Operating System)
222 $this->OS = ord($this->compressed_data[$this->position++]);
225 if ($this->flags & 4)
228 $this->SI1 = $this->compressed_data[$this->position++];
229 $this->SI2 = $this->compressed_data[$this->position++];
231 // SI2 set to zero is reserved for future use
232 if ($this->SI2 === "\x00
")
237 // Get the length of the extra field
238 $len = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
239 $this->position += 2;
241 // Check the length of the string is still valid
242 $this->min_compressed_size += $len + 4;
243 if ($this->compressed_size >= $this->min_compressed_size)
245 // Set the extra field to the given data
246 $this->extra_field = substr($this->compressed_data, $this->position, $len);
247 $this->position += $len;
256 if ($this->flags & 8)
258 // Get the length of the filename
259 $len = strcspn($this->compressed_data, "\x00
", $this->position);
261 // Check the length of the string is still valid
262 $this->min_compressed_size += $len + 1;
263 if ($this->compressed_size >= $this->min_compressed_size)
265 // Set the original filename to the given string
266 $this->filename = substr($this->compressed_data, $this->position, $len);
267 $this->position += $len + 1;
275 // Parse the FCOMMENT
276 if ($this->flags & 16)
278 // Get the length of the comment
279 $len = strcspn($this->compressed_data, "\x00
", $this->position);
281 // Check the length of the string is still valid
282 $this->min_compressed_size += $len + 1;
283 if ($this->compressed_size >= $this->min_compressed_size)
285 // Set the original comment to the given string
286 $this->comment = substr($this->compressed_data, $this->position, $len);
287 $this->position += $len + 1;
296 if ($this->flags & 2)
298 // Check the length of the string is still valid
299 $this->min_compressed_size += $len + 2;
300 if ($this->compressed_size >= $this->min_compressed_size)
303 $crc = current(unpack('v', substr($this->compressed_data, $this->position, 2)));
305 // Check the CRC matches
306 if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc)
308 $this->position += 2;
321 // Decompress the actual data
322 if (($this->data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false)
328 $this->position = $this->compressed_size - 8;
332 $crc = current(unpack('V', substr($this->compressed_data, $this->position, 4)));
333 $this->position += 4;
334 /*if (extension_loaded('hash') && sprintf('%u', current(unpack('V', hash('crc32b', $this->data)))) !== sprintf('%u', $crc))
339 // Check ISIZE of data
340 $isize = current(unpack('V', substr($this->compressed_data, $this->position, 4)));
341 $this->position += 4;
342 if (sprintf('%u', strlen($this->data) & 0xFFFFFFFF) !== sprintf('%u', $isize))
347 // Wow, against all odds, we've actually got a valid gzip string