3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.archive.gzip.php //
12 // module for analyzing GZIP files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 // Module originally written by //
18 // Mike Mozolin <teddybearØmail*ru> //
20 /////////////////////////////////////////////////////////////////
23 class getid3_gzip
extends getid3_handler
26 * Optional file list - disable for speed.
28 * Decode gzipped files, if possible, and parse recursively (.tar.gz for example).
32 public $option_gzip_parse_contents = false;
37 public function Analyze() {
38 $info = &$this->getid3
->info
;
40 $info['fileformat'] = 'gzip';
43 $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
44 //+---+---+---+---+---+---+---+---+---+---+
45 //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
46 //+---+---+---+---+---+---+---+---+---+---+
48 if ($info['php_memory_limit'] && ($info['filesize'] > $info['php_memory_limit'])) {
49 $this->error('File is too large ('.number_format($info['filesize']).' bytes) to read into memory (limit: '.number_format($info['php_memory_limit'] / 1048576).'MB)');
53 $buffer = $this->fread($info['filesize']);
55 $arr_members = explode("\x1F\x8B\x08", $buffer);
58 $is_wrong_members = false;
59 $num_members = intval(count($arr_members));
60 for ($i = 0; $i < $num_members; $i++
) {
61 if (strlen($arr_members[$i]) == 0) {
64 $buf = "\x1F\x8B\x08".$arr_members[$i];
66 $attr = unpack($unpack_header, substr($buf, 0, $start_length));
67 if (!$this->get_os_type(ord($attr['os']))) {
68 // Merge member with previous if wrong OS type
69 $arr_members[($i - 1)] .= $buf;
70 $arr_members[$i] = '';
71 $is_wrong_members = true;
75 if (!$is_wrong_members) {
80 $info['gzip']['files'] = array();
84 for ($i = 0; $i < $num_members; $i++
) {
85 if (strlen($arr_members[$i]) == 0) {
88 $thisInfo = &$info['gzip']['member_header'][++
$idx];
90 $buff = "\x1F\x8B\x08".$arr_members[$i];
92 $attr = unpack($unpack_header, substr($buff, 0, $start_length));
93 $thisInfo['filemtime'] = getid3_lib
::LittleEndian2Int($attr['mtime']);
94 $thisInfo['raw']['id1'] = ord($attr['cmethod']);
95 $thisInfo['raw']['id2'] = ord($attr['cmethod']);
96 $thisInfo['raw']['cmethod'] = ord($attr['cmethod']);
97 $thisInfo['raw']['os'] = ord($attr['os']);
98 $thisInfo['raw']['xflags'] = ord($attr['xflags']);
99 $thisInfo['raw']['flags'] = ord($attr['flags']);
101 $thisInfo['flags']['crc16'] = (bool) ($thisInfo['raw']['flags'] & 0x02);
102 $thisInfo['flags']['extra'] = (bool) ($thisInfo['raw']['flags'] & 0x04);
103 $thisInfo['flags']['filename'] = (bool) ($thisInfo['raw']['flags'] & 0x08);
104 $thisInfo['flags']['comment'] = (bool) ($thisInfo['raw']['flags'] & 0x10);
106 $thisInfo['compression'] = $this->get_xflag_type($thisInfo['raw']['xflags']);
108 $thisInfo['os'] = $this->get_os_type($thisInfo['raw']['os']);
109 if (!$thisInfo['os']) {
110 $this->error('Read error on gzip file');
115 $arr_xsubfield = array();
116 // bit 2 - FLG.FEXTRA
117 //+---+---+=================================+
118 //| XLEN |...XLEN bytes of "extra field"...|
119 //+---+---+=================================+
120 if ($thisInfo['flags']['extra']) {
121 $w_xlen = substr($buff, $fpointer, 2);
122 $xlen = getid3_lib
::LittleEndian2Int($w_xlen);
125 $thisInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
127 //+---+---+---+---+==================================+
128 //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
129 //+---+---+---+---+==================================+
135 $si1 = ord(substr($buff, $fpointer +
$idx++
, 1));
136 $si2 = ord(substr($buff, $fpointer +
$idx++
, 1));
137 if (($si1 == 0x41) && ($si2 == 0x70)) {
138 $w_xsublen = substr($buff, $fpointer +
$idx, 2);
139 $xsublen = getid3_lib
::LittleEndian2Int($w_xsublen);
141 $arr_xsubfield[] = substr($buff, $fpointer +
$idx, $xsublen);
150 //+=========================================+
151 //|...original file name, zero-terminated...|
152 //+=========================================+
153 // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
154 $thisInfo['filename'] = preg_replace('#\\.gz$#i', '', $info['filename']);
155 if ($thisInfo['flags']['filename']) {
156 $thisInfo['filename'] = '';
158 if (ord($buff[$fpointer]) == 0) {
162 $thisInfo['filename'] .= $buff[$fpointer];
166 // bit 4 - FLG.FCOMMENT
167 //+===================================+
168 //|...file comment, zero-terminated...|
169 //+===================================+
170 if ($thisInfo['flags']['comment']) {
172 if (ord($buff[$fpointer]) == 0) {
176 $thisInfo['comment'] .= $buff[$fpointer];
184 if ($thisInfo['flags']['crc16']) {
185 $w_crc = substr($buff, $fpointer, 2);
186 $thisInfo['crc16'] = getid3_lib
::LittleEndian2Int($w_crc);
190 //if ($thisInfo['raw']['flags'] & 0x01) {
193 // bits 5, 6, 7 - reserved
195 $thisInfo['crc32'] = getid3_lib
::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
196 $thisInfo['filesize'] = getid3_lib
::LittleEndian2Int(substr($buff, strlen($buff) - 4));
198 $info['gzip']['files'] = getid3_lib
::array_merge_clobber($info['gzip']['files'], getid3_lib
::CreateDeepArray($thisInfo['filename'], '/', $thisInfo['filesize']));
200 if ($this->option_gzip_parse_contents
) {
201 // Try to inflate GZip
205 if (function_exists('gzinflate')) {
206 $cdata = substr($buff, $fpointer);
207 $cdata = substr($cdata, 0, strlen($cdata) - 8);
208 $csize = strlen($cdata);
209 $inflated = gzinflate($cdata);
211 // Calculate CRC32 for inflated content
212 $thisInfo['crc32_valid'] = sprintf('%u', crc32($inflated)) == $thisInfo['crc32'];
215 $formattest = substr($inflated, 0, 32774);
216 $getid3_temp = new getID3();
217 $determined_format = $getid3_temp->GetFileFormat($formattest);
220 // file format is determined
221 $determined_format['module'] = (isset($determined_format['module']) ?
$determined_format['module'] : '');
222 switch ($determined_format['module']) {
224 // view TAR-file info
225 if (file_exists(GETID3_INCLUDEPATH
.$determined_format['include']) && include_once(GETID3_INCLUDEPATH
.$determined_format['include'])) {
226 if (($temp_tar_filename = tempnam(GETID3_TEMP_DIR
, 'getID3')) === false) {
227 // can't find anywhere to create a temp file, abort
228 $this->error('Unable to create temp file to parse TAR inside GZIP file');
231 if ($fp_temp_tar = fopen($temp_tar_filename, 'w+b')) {
232 fwrite($fp_temp_tar, $inflated);
233 fclose($fp_temp_tar);
234 $getid3_temp = new getID3();
235 $getid3_temp->openfile($temp_tar_filename);
236 $getid3_tar = new getid3_tar($getid3_temp);
237 $getid3_tar->Analyze();
238 $info['gzip']['member_header'][$idx]['tar'] = $getid3_temp->info
['tar'];
239 unset($getid3_temp, $getid3_tar);
240 unlink($temp_tar_filename);
242 $this->error('Unable to fopen() temp file to parse TAR inside GZIP file');
250 // unknown or unhandled format
254 $this->warning('PHP is not compiled with gzinflate() support. Please enable PHP Zlib extension or recompile with the --with-zlib switch');
262 * Converts the OS type.
268 public function get_os_type($key) {
269 static $os_type = array(
270 '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
272 '2' => 'VMS (or OpenVMS)',
276 '6' => 'HPFS filesystem (OS/2, NT)',
281 '11' => 'NTFS filesystem (NT)',
283 '13' => 'Acorn RISCOS',
286 return (isset($os_type[$key]) ?
$os_type[$key] : '');
290 * Converts the eXtra FLags.
296 public function get_xflag_type($key) {
297 static $xflag_type = array(
299 '2' => 'maximum compression',
300 '4' => 'fastest algorithm'
302 return (isset($xflag_type[$key]) ?
$xflag_type[$key] : '');