ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.archive.gzip.php
Go to the documentation of this file.
1 <?php
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
7 // See readme.txt for more details //
9 // //
10 // module.archive.gzip.php //
11 // written by Mike Mozolin <teddybearØmail*ru> //
12 // module for analyzing GZIP files //
13 // dependencies: NONE //
14 // ///
16 
17 class getid3_gzip {
18 
19  // public: Optional file list - disable for speed.
20  var $option_gzip_parse_contents = false; // decode gzipped files, if possible, and parse recursively (.tar.gz for example)
21 
22  function getid3_gzip(&$fd, &$ThisFileInfo) {
23  $ThisFileInfo['fileformat'] = 'gzip';
24 
25  $start_length = 10;
26  $unpack_header = 'a1id1/a1id2/a1cmethod/a1flags/a4mtime/a1xflags/a1os';
27  //+---+---+---+---+---+---+---+---+---+---+
28  //|ID1|ID2|CM |FLG| MTIME |XFL|OS |
29  //+---+---+---+---+---+---+---+---+---+---+
30  @fseek($fd, 0);
31  $buffer = @fread($fd, $ThisFileInfo['filesize']);
32 
33  $arr_members = explode("\x1F\x8B\x08", $buffer);
34  while (true) {
35  $is_wrong_members = false;
36  $num_members = intval(count($arr_members));
37  for ($i = 0; $i < $num_members; $i++) {
38  if (strlen($arr_members[$i]) == 0) {
39  continue;
40  }
41  $buf = "\x1F\x8B\x08".$arr_members[$i];
42 
43  $attr = unpack($unpack_header, substr($buf, 0, $start_length));
44  if (!$this->get_os_type(ord($attr['os']))) {
45  // Merge member with previous if wrong OS type
46  $arr_members[$i - 1] .= $buf;
47  $arr_members[$i] = '';
48  $is_wrong_members = true;
49  continue;
50  }
51  }
52  if (!$is_wrong_members) {
53  break;
54  }
55  }
56 
57  $ThisFileInfo['gzip']['files'] = array();
58 
59  $fpointer = 0;
60  $idx = 0;
61  for ($i = 0; $i < $num_members; $i++) {
62  if (strlen($arr_members[$i]) == 0) {
63  continue;
64  }
65  $thisThisFileInfo = &$ThisFileInfo['gzip']['member_header'][++$idx];
66 
67  $buff = "\x1F\x8B\x08".$arr_members[$i];
68 
69  $attr = unpack($unpack_header, substr($buff, 0, $start_length));
70  $thisThisFileInfo['filemtime'] = getid3_lib::LittleEndian2Int($attr['mtime']);
71  $thisThisFileInfo['raw']['id1'] = ord($attr['cmethod']);
72  $thisThisFileInfo['raw']['id2'] = ord($attr['cmethod']);
73  $thisThisFileInfo['raw']['cmethod'] = ord($attr['cmethod']);
74  $thisThisFileInfo['raw']['os'] = ord($attr['os']);
75  $thisThisFileInfo['raw']['xflags'] = ord($attr['xflags']);
76  $thisThisFileInfo['raw']['flags'] = ord($attr['flags']);
77 
78  $thisThisFileInfo['flags']['crc16'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x02);
79  $thisThisFileInfo['flags']['extra'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x04);
80  $thisThisFileInfo['flags']['filename'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x08);
81  $thisThisFileInfo['flags']['comment'] = (bool) ($thisThisFileInfo['raw']['flags'] & 0x10);
82 
83  $thisThisFileInfo['compression'] = $this->get_xflag_type($thisThisFileInfo['raw']['xflags']);
84 
85  $thisThisFileInfo['os'] = $this->get_os_type($thisThisFileInfo['raw']['os']);
86  if (!$thisThisFileInfo['os']) {
87  $ThisFileInfo['error'][] = 'Read error on gzip file';
88  return false;
89  }
90 
91  $fpointer = 10;
92  $arr_xsubfield = array();
93  // bit 2 - FLG.FEXTRA
94  //+---+---+=================================+
95  //| XLEN |...XLEN bytes of "extra field"...|
96  //+---+---+=================================+
97  if ($thisThisFileInfo['flags']['extra']) {
98  $w_xlen = substr($buff, $fpointer, 2);
99  $xlen = getid3_lib::LittleEndian2Int($w_xlen);
100  $fpointer += 2;
101 
102  $thisThisFileInfo['raw']['xfield'] = substr($buff, $fpointer, $xlen);
103  // Extra SubFields
104  //+---+---+---+---+==================================+
105  //|SI1|SI2| LEN |... LEN bytes of subfield data ...|
106  //+---+---+---+---+==================================+
107  $idx = 0;
108  while (true) {
109  if ($idx >= $xlen) {
110  break;
111  }
112  $si1 = ord(substr($buff, $fpointer + $idx++, 1));
113  $si2 = ord(substr($buff, $fpointer + $idx++, 1));
114  if (($si1 == 0x41) && ($si2 == 0x70)) {
115  $w_xsublen = substr($buff, $fpointer+$idx, 2);
116  $xsublen = getid3_lib::LittleEndian2Int($w_xsublen);
117  $idx += 2;
118  $arr_xsubfield[] = substr($buff, $fpointer+$idx, $xsublen);
119  $idx += $xsublen;
120  } else {
121  break;
122  }
123  }
124  $fpointer += $xlen;
125  }
126  // bit 3 - FLG.FNAME
127  //+=========================================+
128  //|...original file name, zero-terminated...|
129  //+=========================================+
130  // GZIP files may have only one file, with no filename, so assume original filename is current filename without .gz
131  $thisThisFileInfo['filename'] = eregi_replace('.gz$', '', $ThisFileInfo['filename']);
132  if ($thisThisFileInfo['flags']['filename']) {
133  while (true) {
134  if (ord($buff[$fpointer]) == 0) {
135  $fpointer++;
136  break;
137  }
138  $thisThisFileInfo['filename'] .= $buff[$fpointer];
139  $fpointer++;
140  }
141  }
142  // bit 4 - FLG.FCOMMENT
143  //+===================================+
144  //|...file comment, zero-terminated...|
145  //+===================================+
146  if ($thisThisFileInfo['flags']['comment']) {
147  while (true) {
148  if (ord($buff[$fpointer]) == 0) {
149  $fpointer++;
150  break;
151  }
152  $thisThisFileInfo['comment'] .= $buff[$fpointer];
153  $fpointer++;
154  }
155  }
156  // bit 1 - FLG.FHCRC
157  //+---+---+
158  //| CRC16 |
159  //+---+---+
160  if ($thisThisFileInfo['flags']['crc16']) {
161  $w_crc = substr($buff, $fpointer, 2);
162  $thisThisFileInfo['crc16'] = getid3_lib::LittleEndian2Int($w_crc);
163  $fpointer += 2;
164  }
165  // bit 0 - FLG.FTEXT
166  //if ($thisThisFileInfo['raw']['flags'] & 0x01) {
167  // Ignored...
168  //}
169  // bits 5, 6, 7 - reserved
170 
171  $thisThisFileInfo['crc32'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 8, 4));
172  $thisThisFileInfo['filesize'] = getid3_lib::LittleEndian2Int(substr($buff, strlen($buff) - 4));
173 
174  $ThisFileInfo['gzip']['files'] = getid3_lib::array_merge_clobber($ThisFileInfo['gzip']['files'], getid3_lib::CreateDeepArray($thisThisFileInfo['filename'], '/', $thisThisFileInfo['filesize']));
175 
176  if ($this->option_gzip_parse_contents) {
177  // Try to inflate GZip
178  $csize = 0;
179  $inflated = '';
180  $chkcrc32 = '';
181  if (function_exists('gzinflate')) {
182  $cdata = substr($buff, $fpointer);
183  $cdata = substr($cdata, 0, strlen($cdata) - 8);
184  $csize = strlen($cdata);
185  $inflated = gzinflate($cdata);
186 
187  // Calculate CRC32 for inflated content
188  $thisThisFileInfo['crc32_valid'] = (bool) (sprintf('%u', crc32($inflated)) == $thisThisFileInfo['crc32']);
189 
190  // determine format
191  $formattest = substr($inflated, 0, 32774);
192  $newgetID3 = new getID3();
193  $determined_format = $newgetID3->GetFileFormat($formattest);
194  unset($newgetID3);
195 
196  // file format is determined
197  switch (@$determined_format['module']) {
198  case 'tar':
199  // view TAR-file info
200  if (file_exists(GETID3_INCLUDEPATH.$determined_format['include']) && @include_once(GETID3_INCLUDEPATH.$determined_format['include'])) {
201  getid3_tar::read_tar($inflated, $ThisFileInfo['gzip']['member_header'][$idx]);
202  }
203  break;
204 
205  case '':
206  default:
207  // unknown or unhandled format
208  break;
209  }
210  }
211  }
212  }
213  return true;
214  }
215 
216  // Converts the OS type
217  function get_os_type($key) {
218  static $os_type = array(
219  '0' => 'FAT filesystem (MS-DOS, OS/2, NT/Win32)',
220  '1' => 'Amiga',
221  '2' => 'VMS (or OpenVMS)',
222  '3' => 'Unix',
223  '4' => 'VM/CMS',
224  '5' => 'Atari TOS',
225  '6' => 'HPFS filesystem (OS/2, NT)',
226  '7' => 'Macintosh',
227  '8' => 'Z-System',
228  '9' => 'CP/M',
229  '10' => 'TOPS-20',
230  '11' => 'NTFS filesystem (NT)',
231  '12' => 'QDOS',
232  '13' => 'Acorn RISCOS',
233  '255' => 'unknown'
234  );
235  return @$os_type[$key];
236  }
237 
238  // Converts the eXtra FLags
239  function get_xflag_type($key) {
240  static $xflag_type = array(
241  '0' => 'unknown',
242  '2' => 'maximum compression',
243  '4' => 'fastest algorithm'
244  );
245  return @$xflag_type[$key];
246  }
247 }
248 
249 ?>