view e2gallerypro/e2upload/Backend/Assets/getid3/module.archive.gzip.php @ 20:1038db2374ec judyates

change address
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Sep 2013 00:47:09 -0400
parents 3f6b44aa6b35
children
line wrap: on
line source
1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP version 5 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2 of the GPL license, |
8 // | that is bundled with this package in the file license.txt and is |
9 // | available through the world-wide-web at the following url: |
10 // | http://www.gnu.org/copyleft/gpl.html |
11 // +----------------------------------------------------------------------+
12 // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org |
13 // +----------------------------------------------------------------------+
14 // | Authors: James Heinrich <infoØgetid3*org> |
15 // | Allan Hansen <ahØartemis*dk> |
16 // +----------------------------------------------------------------------+
17 // | module.archive.gzip.php |
18 // | module for analyzing GZIP files |
19 // | dependencies: PHP compiled with zlib support (optional) |
20 // +----------------------------------------------------------------------+
21 // | Module originally written by Mike Mozolin <teddybearØmail*ru> |
22 // +----------------------------------------------------------------------+
23 //
24 // $Id: module.archive.gzip.php,v 1.4 2006/12/04 16:00:35 ah Exp $
28 class getid3_gzip extends getid3_handler
29 {
31 // public: Optional file list - disable for speed.
32 public $option_gzip_parse_contents = true; // decode gzipped files, if possible, and parse recursively (.tar.gz for example)
35 // Reads the gzip-file
36 function Analyze() {
38 $info = &$this->getid3->info;
40 $info['fileformat'] = 'gzip';
42 $start_length = 10;
43 $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
45 //+---+---+---+---+---+---+---+---+---+---+
46 //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
47 //+---+---+---+---+---+---+---+---+---+---+
49 @fseek($this->getid3->fp, 0);
50 $buffer = @fread($this->getid3->fp, $info['filesize']);
52 $arr_members = explode("\x1F\x8B\x08", $buffer);
54 while (true) {
55 $is_wrong_members = false;
56 $num_members = intval(count($arr_members));
57 for ($i = 0; $i < $num_members; $i++) {
58 if (strlen($arr_members[$i]) == 0) {
59 continue;
60 }
61 $buf = "\x1F\x8B\x08".$arr_members[$i];
63 $attr = unpack($unpack_header, substr($buf, 0, $start_length));
64 if (!$this->get_os_type(ord($attr['os']))) {
66 // Merge member with previous if wrong OS type
67 $arr_members[$i - 1] .= $buf;
68 $arr_members[$i] = '';
69 $is_wrong_members = true;
70 continue;
71 }
72 }
73 if (!$is_wrong_members) {
74 break;
75 }
76 }
78 $fpointer = 0;
79 $idx = 0;
80 for ($i = 0; $i < $num_members; $i++) {
81 if (strlen($arr_members[$i]) == 0) {
82 continue;
83 }
84 $info_gzip_member_header_idx = &$info['gzip']['member_header'][++$idx];
86 $buff = "\x1F\x8B\x08".$arr_members[$i];
88 $attr = unpack($unpack_header, substr($buff, 0, $start_length));
89 $info_gzip_member_header_idx['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
90 $info_gzip_member_header_idx['raw']['id1'] = ord($attr['cmethod']);
91 $info_gzip_member_header_idx['raw']['id2'] = ord($attr['cmethod']);
92 $info_gzip_member_header_idx['raw']['cmethod'] = ord($attr['cmethod']);
93 $info_gzip_member_header_idx['raw']['os'] = ord($attr['os']);
94 $info_gzip_member_header_idx['raw']['xflags'] = ord($attr['xflags']);
95 $info_gzip_member_header_idx['raw']['flags'] = ord($attr['flags']);
97 $info_gzip_member_header_idx['flags']['crc16'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x02);
98 $info_gzip_member_header_idx['flags']['extra'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x04);
99 $info_gzip_member_header_idx['flags']['filename'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x08);
100 $info_gzip_member_header_idx['flags']['comment'] = (bool) ($info_gzip_member_header_idx['raw']['flags'] & 0x10);
102 $info_gzip_member_header_idx['compression'] = $this->get_xflag_type($info_gzip_member_header_idx['raw']['xflags']);
104 $info_gzip_member_header_idx['os'] = $this->get_os_type($info_gzip_member_header_idx['raw']['os']);
105 if (!$info_gzip_member_header_idx['os']) {
106 $info['error'][] = 'Read error on gzip file';
107 return false;
108 }
110 $fpointer = 10;
111 $arr_xsubfield = array ();
113 // bit 2 - FLG.FEXTRA
114 //+---+---+=================================+
115 //| XLEN |...XLEN bytes of "extra field"...|
116 //+---+---+=================================+
118 if ($info_gzip_member_header_idx['flags']['extra']) {
119 $w_xlen = substr($buff, $fpointer, 2);
120 $xlen = getid3_lib::LittleEndian2Int($w_xlen);
121 $fpointer += 2;
123 $info_gzip_member_header_idx['raw']['xfield'] = substr($buff, $fpointer, $xlen);
125 // Extra SubFields
126 //+---+---+---+---+==================================+
127 //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
128 //+---+---+---+---+==================================+
130 $idx = 0;
131 while (true) {
132 if ($idx >= $xlen) {
133 break;
134 }
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);
140 $idx += 2;
141 $arr_xsubfield[] = substr($buff, $fpointer+$idx, $xsublen);
142 $idx += $xsublen;
143 } else {
144 break;
145 }
146 }
147 $fpointer += $xlen;
148 }
150 // bit 3 - FLG.FNAME
151 //+=========================================+
152 //|...original file name, zero-terminated...|
153 //+=========================================+
154 // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
156 $info_gzip_member_header_idx['filename'] = eregi_replace('.gz$', '', @$info['filename']);
157 if ($info_gzip_member_header_idx['flags']['filename']) {
158 while (true) {
159 if (ord($buff[$fpointer]) == 0) {
160 $fpointer++;
161 break;
162 }
163 $info_gzip_member_header_idx['filename'] .= $buff[$fpointer];
164 $fpointer++;
165 }
166 }
168 // bit 4 - FLG.FCOMMENT
169 //+===================================+
170 //|...file comment, zero-terminated...|
171 //+===================================+
173 if ($info_gzip_member_header_idx['flags']['comment']) {
174 while (true) {
175 if (ord($buff[$fpointer]) == 0) {
176 $fpointer++;
177 break;
178 }
179 $info_gzip_member_header_idx['comment'] .= $buff[$fpointer];
180 $fpointer++;
181 }
182 }
184 // bit 1 - FLG.FHCRC
185 //+---+---+
186 //| CRC16 |
187 //+---+---+
189 if ($info_gzip_member_header_idx['flags']['crc16']) {
190 $w_crc = substr($buff, $fpointer, 2);
191 $info_gzip_member_header_idx['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
192 $fpointer += 2;
193 }
195 // bit 0 - FLG.FTEXT
196 //if ($info_gzip_member_header_idx['raw']['flags'] & 0x01) {
197 // Ignored...
198 //}
199 // bits 5, 6, 7 - reserved
201 $info_gzip_member_header_idx['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
202 $info_gzip_member_header_idx['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
204 if ($this->option_gzip_parse_contents) {
206 // Try to inflate GZip
208 if (!function_exists('gzinflate')) {
209 $this->getid3->warning('PHP does not have zlib support - contents not parsed.');
210 return true;
211 }
213 $csize = 0;
214 $inflated = '';
215 $chkcrc32 = '';
217 $cdata = substr($buff, $fpointer);
218 $cdata = substr($cdata, 0, strlen($cdata) - 8);
219 $csize = strlen($cdata);
220 $inflated = gzinflate($cdata);
222 // Calculate CRC32 for inflated content
223 $info_gzip_member_header_idx['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $info_gzip_member_header_idx['crc32']);
226 //// Analyse contents
228 // write content to temp file
229 if (($temp_file_name = tempnam('*', 'getID3')) === false) {
230 throw new getid3_exception('Unable to create temporary file.');
231 }
233 if ($tmp = fopen($temp_file_name, 'wb')) {
234 fwrite($tmp, $inflated);
235 fclose($tmp);
237 // clone getid3 - we want same settings
238 $clone = clone $this->getid3;
239 unset($clone->info);
240 try {
241 $clone->Analyze($temp_file_name);
242 $info_gzip_member_header_idx['parsed_content'] = $clone->info;
243 }
244 catch (getid3_exception $e) {
245 // unable to parse contents
246 }
248 unlink($temp_file_name);
249 }
251 // Unknown/unhandled format
252 else {
254 }
255 }
256 }
257 return true;
258 }
261 // Converts the OS type
262 public static function get_os_type($key) {
263 static $os_type = array (
264 '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
265 '1' => 'Amiga',
266 '2' => 'VMS (or OpenVMS)',
267 '3' => 'Unix',
268 '4' => 'VM/CMS',
269 '5' => 'Atari TOS',
270 '6' => 'HPFS filesystem (OS/2, NT)',
271 '7' => 'Macintosh',
272 '8' => 'Z-System',
273 '9' => 'CP/M',
274 '10' => 'TOPS-20',
275 '11' => 'NTFS filesystem (NT)',
276 '12' => 'QDOS',
277 '13' => 'Acorn RISCOS',
278 '255' => 'unknown'
279 );
280 return @$os_type[$key];
281 }
284 // Converts the eXtra FLags
285 public static function get_xflag_type($key) {
286 static $xflag_type = array (
287 '0' => 'unknown',
288 '2' => 'maximum compression',
289 '4' => 'fastest algorithm'
290 );
291 return @$xflag_type[$key];
292 }
294 }
296 ?>