SimplePie_gzdecode

Decode 'gzip' encoded HTTP data.

Defined (1)

The class is defined in the following location(s).

/wp-includes/SimplePie/gzdecode.php  
  1. class SimplePie_gzdecode 
  2. /** 
  3. * Compressed data 
  4. * @access private 
  5. * @var string 
  6. * @see gzdecode::$data 
  7. */ 
  8. var $compressed_data; 
  9.  
  10. /** 
  11. * Size of compressed data 
  12. * @access private 
  13. * @var int 
  14. */ 
  15. var $compressed_size; 
  16.  
  17. /** 
  18. * Minimum size of a valid gzip string 
  19. * @access private 
  20. * @var int 
  21. */ 
  22. var $min_compressed_size = 18; 
  23.  
  24. /** 
  25. * Current position of pointer 
  26. * @access private 
  27. * @var int 
  28. */ 
  29. var $position = 0; 
  30.  
  31. /** 
  32. * Flags (FLG) 
  33. * @access private 
  34. * @var int 
  35. */ 
  36. var $flags; 
  37.  
  38. /** 
  39. * Uncompressed data 
  40. * @access public 
  41. * @see gzdecode::$compressed_data 
  42. * @var string 
  43. */ 
  44. var $data; 
  45.  
  46. /** 
  47. * Modified time 
  48. * @access public 
  49. * @var int 
  50. */ 
  51. var $MTIME; 
  52.  
  53. /** 
  54. * Extra Flags 
  55. * @access public 
  56. * @var int 
  57. */ 
  58. var $XFL; 
  59.  
  60. /** 
  61. * Operating System 
  62. * @access public 
  63. * @var int 
  64. */ 
  65. var $OS; 
  66.  
  67. /** 
  68. * Subfield ID 1 
  69. * @access public 
  70. * @see gzdecode::$extra_field 
  71. * @see gzdecode::$SI2 
  72. * @var string 
  73. */ 
  74. var $SI1; 
  75.  
  76. /** 
  77. * Subfield ID 2 
  78. * @access public 
  79. * @see gzdecode::$extra_field 
  80. * @see gzdecode::$SI1 
  81. * @var string 
  82. */ 
  83. var $SI2; 
  84.  
  85. /** 
  86. * Extra field content 
  87. * @access public 
  88. * @see gzdecode::$SI1 
  89. * @see gzdecode::$SI2 
  90. * @var string 
  91. */ 
  92. var $extra_field; 
  93.  
  94. /** 
  95. * Original filename 
  96. * @access public 
  97. * @var string 
  98. */ 
  99. var $filename; 
  100.  
  101. /** 
  102. * Human readable comment 
  103. * @access public 
  104. * @var string 
  105. */ 
  106. var $comment; 
  107.  
  108. /** 
  109. * Don't allow anything to be set 
  110. * @param string $name 
  111. * @param mixed $value 
  112. */ 
  113. public function __set($name, $value) 
  114. trigger_error("Cannot write property $name", E_USER_ERROR); 
  115.  
  116. /** 
  117. * Set the compressed string and related properties 
  118. * @param string $data 
  119. */ 
  120. public function __construct($data) 
  121. $this->compressed_data = $data; 
  122. $this->compressed_size = strlen($data); 
  123.  
  124. /** 
  125. * Decode the GZIP stream 
  126. * @return bool Successfulness 
  127. */ 
  128. public function parse() 
  129. if ($this->compressed_size >= $this->min_compressed_size) 
  130. // Check ID1, ID2, and CM 
  131. if (substr($this->compressed_data, 0, 3) !== "\x1F\x8B\x08") 
  132. return false; 
  133.  
  134. // Get the FLG (FLaGs) 
  135. $this->flags = ord($this->compressed_data[3]); 
  136.  
  137. // FLG bits above (1 << 4) are reserved 
  138. if ($this->flags > 0x1F) 
  139. return false; 
  140.  
  141. // Advance the pointer after the above 
  142. $this->position += 4; 
  143.  
  144. // MTIME 
  145. $mtime = substr($this->compressed_data, $this->position, 4); 
  146. // Reverse the string if we're on a big-endian arch because l is the only signed long and is machine endianness 
  147. if (current(unpack('S', "\x00\x01")) === 1) 
  148. $mtime = strrev($mtime); 
  149. $this->MTIME = current(unpack('l', $mtime)); 
  150. $this->position += 4; 
  151.  
  152. // Get the XFL (eXtra FLags) 
  153. $this->XFL = ord($this->compressed_data[$this->position++]); 
  154.  
  155. // Get the OS (Operating System) 
  156. $this->OS = ord($this->compressed_data[$this->position++]); 
  157.  
  158. // Parse the FEXTRA 
  159. if ($this->flags & 4) 
  160. // Read subfield IDs 
  161. $this->SI1 = $this->compressed_data[$this->position++]; 
  162. $this->SI2 = $this->compressed_data[$this->position++]; 
  163.  
  164. // SI2 set to zero is reserved for future use 
  165. if ($this->SI2 === "\x00") 
  166. return false; 
  167.  
  168. // Get the length of the extra field 
  169. $len = current(unpack('v', substr($this->compressed_data, $this->position, 2))); 
  170. $this->position += 2; 
  171.  
  172. // Check the length of the string is still valid 
  173. $this->min_compressed_size += $len + 4; 
  174. if ($this->compressed_size >= $this->min_compressed_size) 
  175. // Set the extra field to the given data 
  176. $this->extra_field = substr($this->compressed_data, $this->position, $len); 
  177. $this->position += $len; 
  178. else 
  179. return false; 
  180.  
  181. // Parse the FNAME 
  182. if ($this->flags & 8) 
  183. // Get the length of the filename 
  184. $len = strcspn($this->compressed_data, "\x00", $this->position); 
  185.  
  186. // Check the length of the string is still valid 
  187. $this->min_compressed_size += $len + 1; 
  188. if ($this->compressed_size >= $this->min_compressed_size) 
  189. // Set the original filename to the given string 
  190. $this->filename = substr($this->compressed_data, $this->position, $len); 
  191. $this->position += $len + 1; 
  192. else 
  193. return false; 
  194.  
  195. // Parse the FCOMMENT 
  196. if ($this->flags & 16) 
  197. // Get the length of the comment 
  198. $len = strcspn($this->compressed_data, "\x00", $this->position); 
  199.  
  200. // Check the length of the string is still valid 
  201. $this->min_compressed_size += $len + 1; 
  202. if ($this->compressed_size >= $this->min_compressed_size) 
  203. // Set the original comment to the given string 
  204. $this->comment = substr($this->compressed_data, $this->position, $len); 
  205. $this->position += $len + 1; 
  206. else 
  207. return false; 
  208.  
  209. // Parse the FHCRC 
  210. if ($this->flags & 2) 
  211. // Check the length of the string is still valid 
  212. $this->min_compressed_size += $len + 2; 
  213. if ($this->compressed_size >= $this->min_compressed_size) 
  214. // Read the CRC 
  215. $crc = current(unpack('v', substr($this->compressed_data, $this->position, 2))); 
  216.  
  217. // Check the CRC matches 
  218. if ((crc32(substr($this->compressed_data, 0, $this->position)) & 0xFFFF) === $crc) 
  219. $this->position += 2; 
  220. else 
  221. return false; 
  222. else 
  223. return false; 
  224.  
  225. // Decompress the actual data 
  226. if (($this->data = gzinflate(substr($this->compressed_data, $this->position, -8))) === false) 
  227. return false; 
  228. else 
  229. $this->position = $this->compressed_size - 8; 
  230.  
  231. // Check CRC of data 
  232. $crc = current(unpack('V', substr($this->compressed_data, $this->position, 4))); 
  233. $this->position += 4; 
  234. /**if (extension_loaded('hash') && sprintf('%u', current(unpack('V', hash('crc32b', $this->data)))) !== sprintf('%u', $crc)) 
  235. return false; 
  236. }*/ 
  237.  
  238. // Check ISIZE of data 
  239. $isize = current(unpack('V', substr($this->compressed_data, $this->position, 4))); 
  240. $this->position += 4; 
  241. if (sprintf('%u', strlen($this->data) & 0xFFFFFFFF) !== sprintf('%u', $isize)) 
  242. return false; 
  243.  
  244. // Wow, against all odds, we've actually got a valid gzip string 
  245. return true; 
  246. else 
  247. return false;