module.archive.zip.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.archive.zip.php //
  12. // module for analyzing pkZip files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. class getid3_zip extends getid3_handler
  17. {
  18. public function Analyze() {
  19. $info = &$this->getid3->info;
  20. $info['fileformat'] = 'zip';
  21. $info['zip']['encoding'] = 'ISO-8859-1';
  22. $info['zip']['files'] = array();
  23. $info['zip']['compressed_size'] = 0;
  24. $info['zip']['uncompressed_size'] = 0;
  25. $info['zip']['entries_count'] = 0;
  26. if (!getid3_lib::intValueSupported($info['filesize'])) {
  27. $info['error'][] = 'File is larger than '.round(PHP_INT_MAX / 1073741824).'GB, not supported by PHP';
  28. return false;
  29. } else {
  30. $EOCDsearchData = '';
  31. $EOCDsearchCounter = 0;
  32. while ($EOCDsearchCounter++ < 512) {
  33. $this->fseek(-128 * $EOCDsearchCounter, SEEK_END);
  34. $EOCDsearchData = $this->fread(128).$EOCDsearchData;
  35. if (strstr($EOCDsearchData, 'PK'."\x05\x06")) {
  36. $EOCDposition = strpos($EOCDsearchData, 'PK'."\x05\x06");
  37. $this->fseek((-128 * $EOCDsearchCounter) + $EOCDposition, SEEK_END);
  38. $info['zip']['end_central_directory'] = $this->ZIPparseEndOfCentralDirectory();
  39. $this->fseek($info['zip']['end_central_directory']['directory_offset']);
  40. $info['zip']['entries_count'] = 0;
  41. while ($centraldirectoryentry = $this->ZIPparseCentralDirectory($this->getid3->fp)) {
  42. $info['zip']['central_directory'][] = $centraldirectoryentry;
  43. $info['zip']['entries_count']++;
  44. $info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
  45. $info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
  46. //if ($centraldirectoryentry['uncompressed_size'] > 0) { zero-byte files are valid
  47. if (!empty($centraldirectoryentry['filename'])) {
  48. $info['zip']['files'] = getid3_lib::array_merge_clobber($info['zip']['files'], getid3_lib::CreateDeepArray($centraldirectoryentry['filename'], '/', $centraldirectoryentry['uncompressed_size']));
  49. }
  50. }
  51. if ($info['zip']['entries_count'] == 0) {
  52. $info['error'][] = 'No Central Directory entries found (truncated file?)';
  53. return false;
  54. }
  55. if (!empty($info['zip']['end_central_directory']['comment'])) {
  56. $info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
  57. }
  58. if (isset($info['zip']['central_directory'][0]['compression_method'])) {
  59. $info['zip']['compression_method'] = $info['zip']['central_directory'][0]['compression_method'];
  60. }
  61. if (isset($info['zip']['central_directory'][0]['flags']['compression_speed'])) {
  62. $info['zip']['compression_speed'] = $info['zip']['central_directory'][0]['flags']['compression_speed'];
  63. }
  64. if (isset($info['zip']['compression_method']) && ($info['zip']['compression_method'] == 'store') && !isset($info['zip']['compression_speed'])) {
  65. $info['zip']['compression_speed'] = 'store';
  66. }
  67. // secondary check - we (should) already have all the info we NEED from the Central Directory above, but scanning each
  68. // Local File Header entry will
  69. foreach ($info['zip']['central_directory'] as $central_directory_entry) {
  70. $this->fseek($central_directory_entry['entry_offset']);
  71. if ($fileentry = $this->ZIPparseLocalFileHeader()) {
  72. $info['zip']['entries'][] = $fileentry;
  73. } else {
  74. $info['warning'][] = 'Error parsing Local File Header at offset '.$central_directory_entry['entry_offset'];
  75. }
  76. }
  77. if (!empty($info['zip']['files']['[Content_Types].xml']) &&
  78. !empty($info['zip']['files']['_rels']['.rels']) &&
  79. !empty($info['zip']['files']['docProps']['app.xml']) &&
  80. !empty($info['zip']['files']['docProps']['core.xml'])) {
  81. // http://technet.microsoft.com/en-us/library/cc179224.aspx
  82. $info['fileformat'] = 'zip.msoffice';
  83. if (!empty($ThisFileInfo['zip']['files']['ppt'])) {
  84. $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
  85. } elseif (!empty($ThisFileInfo['zip']['files']['xl'])) {
  86. $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  87. } elseif (!empty($ThisFileInfo['zip']['files']['word'])) {
  88. $info['mime_type'] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  89. }
  90. }
  91. return true;
  92. }
  93. }
  94. }
  95. if (!$this->getZIPentriesFilepointer()) {
  96. unset($info['zip']);
  97. $info['fileformat'] = '';
  98. $info['error'][] = 'Cannot find End Of Central Directory (truncated file?)';
  99. return false;
  100. }
  101. // central directory couldn't be found and/or parsed
  102. // scan through actual file data entries, recover as much as possible from probable trucated file
  103. if ($info['zip']['compressed_size'] > ($info['filesize'] - 46 - 22)) {
  104. $info['error'][] = 'Warning: Truncated file! - Total compressed file sizes ('.$info['zip']['compressed_size'].' bytes) is greater than filesize minus Central Directory and End Of Central Directory structures ('.($info['filesize'] - 46 - 22).' bytes)';
  105. }
  106. $info['error'][] = 'Cannot find End Of Central Directory - returned list of files in [zip][entries] array may not be complete';
  107. foreach ($info['zip']['entries'] as $key => $valuearray) {
  108. $info['zip']['files'][$valuearray['filename']] = $valuearray['uncompressed_size'];
  109. }
  110. return true;
  111. }
  112. public function getZIPHeaderFilepointerTopDown() {
  113. $info = &$this->getid3->info;
  114. $info['fileformat'] = 'zip';
  115. $info['zip']['compressed_size'] = 0;
  116. $info['zip']['uncompressed_size'] = 0;
  117. $info['zip']['entries_count'] = 0;
  118. rewind($this->getid3->fp);
  119. while ($fileentry = $this->ZIPparseLocalFileHeader()) {
  120. $info['zip']['entries'][] = $fileentry;
  121. $info['zip']['entries_count']++;
  122. }
  123. if ($info['zip']['entries_count'] == 0) {
  124. $info['error'][] = 'No Local File Header entries found';
  125. return false;
  126. }
  127. $info['zip']['entries_count'] = 0;
  128. while ($centraldirectoryentry = $this->ZIPparseCentralDirectory($this->getid3->fp)) {
  129. $info['zip']['central_directory'][] = $centraldirectoryentry;
  130. $info['zip']['entries_count']++;
  131. $info['zip']['compressed_size'] += $centraldirectoryentry['compressed_size'];
  132. $info['zip']['uncompressed_size'] += $centraldirectoryentry['uncompressed_size'];
  133. }
  134. if ($info['zip']['entries_count'] == 0) {
  135. $info['error'][] = 'No Central Directory entries found (truncated file?)';
  136. return false;
  137. }
  138. if ($EOCD = $this->ZIPparseEndOfCentralDirectory()) {
  139. $info['zip']['end_central_directory'] = $EOCD;
  140. } else {
  141. $info['error'][] = 'No End Of Central Directory entry found (truncated file?)';
  142. return false;
  143. }
  144. if (!empty($info['zip']['end_central_directory']['comment'])) {
  145. $info['zip']['comments']['comment'][] = $info['zip']['end_central_directory']['comment'];
  146. }
  147. return true;
  148. }
  149. public function getZIPentriesFilepointer() {
  150. $info = &$this->getid3->info;
  151. $info['zip']['compressed_size'] = 0;
  152. $info['zip']['uncompressed_size'] = 0;
  153. $info['zip']['entries_count'] = 0;
  154. rewind($this->getid3->fp);
  155. while ($fileentry = $this->ZIPparseLocalFileHeader()) {
  156. $info['zip']['entries'][] = $fileentry;
  157. $info['zip']['entries_count']++;
  158. $info['zip']['compressed_size'] += $fileentry['compressed_size'];
  159. $info['zip']['uncompressed_size'] += $fileentry['uncompressed_size'];
  160. }
  161. if ($info['zip']['entries_count'] == 0) {
  162. $info['error'][] = 'No Local File Header entries found';
  163. return false;
  164. }
  165. return true;
  166. }
  167. public function ZIPparseLocalFileHeader() {
  168. $LocalFileHeader['offset'] = $this->ftell();
  169. $ZIPlocalFileHeader = $this->fread(30);
  170. $LocalFileHeader['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 0, 4));
  171. if ($LocalFileHeader['raw']['signature'] != 0x04034B50) { // "PK\x03\x04"
  172. // invalid Local File Header Signature
  173. $this->fseek($LocalFileHeader['offset']); // seek back to where filepointer originally was so it can be handled properly
  174. return false;
  175. }
  176. $LocalFileHeader['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 4, 2));
  177. $LocalFileHeader['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 6, 2));
  178. $LocalFileHeader['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 8, 2));
  179. $LocalFileHeader['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 10, 2));
  180. $LocalFileHeader['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 12, 2));
  181. $LocalFileHeader['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 14, 4));
  182. $LocalFileHeader['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 18, 4));
  183. $LocalFileHeader['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 22, 4));
  184. $LocalFileHeader['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 26, 2));
  185. $LocalFileHeader['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPlocalFileHeader, 28, 2));
  186. $LocalFileHeader['extract_version'] = sprintf('%1.1f', $LocalFileHeader['raw']['extract_version'] / 10);
  187. $LocalFileHeader['host_os'] = $this->ZIPversionOSLookup(($LocalFileHeader['raw']['extract_version'] & 0xFF00) >> 8);
  188. $LocalFileHeader['compression_method'] = $this->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']);
  189. $LocalFileHeader['compressed_size'] = $LocalFileHeader['raw']['compressed_size'];
  190. $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['raw']['uncompressed_size'];
  191. $LocalFileHeader['flags'] = $this->ZIPparseGeneralPurposeFlags($LocalFileHeader['raw']['general_flags'], $LocalFileHeader['raw']['compression_method']);
  192. $LocalFileHeader['last_modified_timestamp'] = $this->DOStime2UNIXtime($LocalFileHeader['raw']['last_mod_file_date'], $LocalFileHeader['raw']['last_mod_file_time']);
  193. $FilenameExtrafieldLength = $LocalFileHeader['raw']['filename_length'] + $LocalFileHeader['raw']['extra_field_length'];
  194. if ($FilenameExtrafieldLength > 0) {
  195. $ZIPlocalFileHeader .= $this->fread($FilenameExtrafieldLength);
  196. if ($LocalFileHeader['raw']['filename_length'] > 0) {
  197. $LocalFileHeader['filename'] = substr($ZIPlocalFileHeader, 30, $LocalFileHeader['raw']['filename_length']);
  198. }
  199. if ($LocalFileHeader['raw']['extra_field_length'] > 0) {
  200. $LocalFileHeader['raw']['extra_field_data'] = substr($ZIPlocalFileHeader, 30 + $LocalFileHeader['raw']['filename_length'], $LocalFileHeader['raw']['extra_field_length']);
  201. }
  202. }
  203. if ($LocalFileHeader['compressed_size'] == 0) {
  204. // *Could* be a zero-byte file
  205. // But could also be a file written on the fly that didn't know compressed filesize beforehand.
  206. // Correct compressed filesize should be in the data_descriptor located after this file data, and also in Central Directory (at end of zip file)
  207. if (!empty($this->getid3->info['zip']['central_directory'])) {
  208. foreach ($this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
  209. if ($central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
  210. if ($central_directory_entry['compressed_size'] > 0) {
  211. // overwrite local zero value (but not ['raw']'compressed_size']) so that seeking for data_descriptor (and next file entry) works correctly
  212. $LocalFileHeader['compressed_size'] = $central_directory_entry['compressed_size'];
  213. }
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. $LocalFileHeader['data_offset'] = $this->ftell();
  220. $this->fseek($LocalFileHeader['compressed_size'], SEEK_CUR); // this should (but may not) match value in $LocalFileHeader['raw']['compressed_size'] -- $LocalFileHeader['compressed_size'] could have been overwritten above with value from Central Directory
  221. if ($LocalFileHeader['flags']['data_descriptor_used']) {
  222. $DataDescriptor = $this->fread(16);
  223. $LocalFileHeader['data_descriptor']['signature'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 0, 4));
  224. if ($LocalFileHeader['data_descriptor']['signature'] != 0x08074B50) { // "PK\x07\x08"
  225. $this->getid3->warning[] = 'invalid Local File Header Data Descriptor Signature at offset '.($this->ftell() - 16).' - expecting 08 07 4B 50, found '.getid3_lib::PrintHexBytes($LocalFileHeader['data_descriptor']['signature']);
  226. $this->fseek($LocalFileHeader['offset']); // seek back to where filepointer originally was so it can be handled properly
  227. return false;
  228. }
  229. $LocalFileHeader['data_descriptor']['crc_32'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 4, 4));
  230. $LocalFileHeader['data_descriptor']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 8, 4));
  231. $LocalFileHeader['data_descriptor']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($DataDescriptor, 12, 4));
  232. if (!$LocalFileHeader['raw']['compressed_size'] && $LocalFileHeader['data_descriptor']['compressed_size']) {
  233. foreach ($this->getid3->info['zip']['central_directory'] as $central_directory_entry) {
  234. if ($central_directory_entry['entry_offset'] == $LocalFileHeader['offset']) {
  235. if ($LocalFileHeader['data_descriptor']['compressed_size'] == $central_directory_entry['compressed_size']) {
  236. // $LocalFileHeader['compressed_size'] already set from Central Directory
  237. } else {
  238. $this->getid3->info['warning'][] = 'conflicting compressed_size from data_descriptor ('.$LocalFileHeader['data_descriptor']['compressed_size'].') vs Central Directory ('.$central_directory_entry['compressed_size'].') for file at offset '.$LocalFileHeader['offset'];
  239. }
  240. if ($LocalFileHeader['data_descriptor']['uncompressed_size'] == $central_directory_entry['uncompressed_size']) {
  241. $LocalFileHeader['uncompressed_size'] = $LocalFileHeader['data_descriptor']['uncompressed_size'];
  242. } else {
  243. $this->getid3->info['warning'][] = 'conflicting uncompressed_size from data_descriptor ('.$LocalFileHeader['data_descriptor']['uncompressed_size'].') vs Central Directory ('.$central_directory_entry['uncompressed_size'].') for file at offset '.$LocalFileHeader['offset'];
  244. }
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. return $LocalFileHeader;
  251. }
  252. public function ZIPparseCentralDirectory() {
  253. $CentralDirectory['offset'] = $this->ftell();
  254. $ZIPcentralDirectory = $this->fread(46);
  255. $CentralDirectory['raw']['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 0, 4));
  256. if ($CentralDirectory['raw']['signature'] != 0x02014B50) {
  257. // invalid Central Directory Signature
  258. $this->fseek($CentralDirectory['offset']); // seek back to where filepointer originally was so it can be handled properly
  259. return false;
  260. }
  261. $CentralDirectory['raw']['create_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 4, 2));
  262. $CentralDirectory['raw']['extract_version'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 6, 2));
  263. $CentralDirectory['raw']['general_flags'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 8, 2));
  264. $CentralDirectory['raw']['compression_method'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 10, 2));
  265. $CentralDirectory['raw']['last_mod_file_time'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 12, 2));
  266. $CentralDirectory['raw']['last_mod_file_date'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 14, 2));
  267. $CentralDirectory['raw']['crc_32'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 16, 4));
  268. $CentralDirectory['raw']['compressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 20, 4));
  269. $CentralDirectory['raw']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 24, 4));
  270. $CentralDirectory['raw']['filename_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 28, 2));
  271. $CentralDirectory['raw']['extra_field_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 30, 2));
  272. $CentralDirectory['raw']['file_comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 32, 2));
  273. $CentralDirectory['raw']['disk_number_start'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 34, 2));
  274. $CentralDirectory['raw']['internal_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 36, 2));
  275. $CentralDirectory['raw']['external_file_attrib'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 38, 4));
  276. $CentralDirectory['raw']['local_header_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPcentralDirectory, 42, 4));
  277. $CentralDirectory['entry_offset'] = $CentralDirectory['raw']['local_header_offset'];
  278. $CentralDirectory['create_version'] = sprintf('%1.1f', $CentralDirectory['raw']['create_version'] / 10);
  279. $CentralDirectory['extract_version'] = sprintf('%1.1f', $CentralDirectory['raw']['extract_version'] / 10);
  280. $CentralDirectory['host_os'] = $this->ZIPversionOSLookup(($CentralDirectory['raw']['extract_version'] & 0xFF00) >> 8);
  281. $CentralDirectory['compression_method'] = $this->ZIPcompressionMethodLookup($CentralDirectory['raw']['compression_method']);
  282. $CentralDirectory['compressed_size'] = $CentralDirectory['raw']['compressed_size'];
  283. $CentralDirectory['uncompressed_size'] = $CentralDirectory['raw']['uncompressed_size'];
  284. $CentralDirectory['flags'] = $this->ZIPparseGeneralPurposeFlags($CentralDirectory['raw']['general_flags'], $CentralDirectory['raw']['compression_method']);
  285. $CentralDirectory['last_modified_timestamp'] = $this->DOStime2UNIXtime($CentralDirectory['raw']['last_mod_file_date'], $CentralDirectory['raw']['last_mod_file_time']);
  286. $FilenameExtrafieldCommentLength = $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'] + $CentralDirectory['raw']['file_comment_length'];
  287. if ($FilenameExtrafieldCommentLength > 0) {
  288. $FilenameExtrafieldComment = $this->fread($FilenameExtrafieldCommentLength);
  289. if ($CentralDirectory['raw']['filename_length'] > 0) {
  290. $CentralDirectory['filename'] = substr($FilenameExtrafieldComment, 0, $CentralDirectory['raw']['filename_length']);
  291. }
  292. if ($CentralDirectory['raw']['extra_field_length'] > 0) {
  293. $CentralDirectory['raw']['extra_field_data'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'], $CentralDirectory['raw']['extra_field_length']);
  294. }
  295. if ($CentralDirectory['raw']['file_comment_length'] > 0) {
  296. $CentralDirectory['file_comment'] = substr($FilenameExtrafieldComment, $CentralDirectory['raw']['filename_length'] + $CentralDirectory['raw']['extra_field_length'], $CentralDirectory['raw']['file_comment_length']);
  297. }
  298. }
  299. return $CentralDirectory;
  300. }
  301. public function ZIPparseEndOfCentralDirectory() {
  302. $EndOfCentralDirectory['offset'] = $this->ftell();
  303. $ZIPendOfCentralDirectory = $this->fread(22);
  304. $EndOfCentralDirectory['signature'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 0, 4));
  305. if ($EndOfCentralDirectory['signature'] != 0x06054B50) {
  306. // invalid End Of Central Directory Signature
  307. $this->fseek($EndOfCentralDirectory['offset']); // seek back to where filepointer originally was so it can be handled properly
  308. return false;
  309. }
  310. $EndOfCentralDirectory['disk_number_current'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 4, 2));
  311. $EndOfCentralDirectory['disk_number_start_directory'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 6, 2));
  312. $EndOfCentralDirectory['directory_entries_this_disk'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 8, 2));
  313. $EndOfCentralDirectory['directory_entries_total'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 10, 2));
  314. $EndOfCentralDirectory['directory_size'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 12, 4));
  315. $EndOfCentralDirectory['directory_offset'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 16, 4));
  316. $EndOfCentralDirectory['comment_length'] = getid3_lib::LittleEndian2Int(substr($ZIPendOfCentralDirectory, 20, 2));
  317. if ($EndOfCentralDirectory['comment_length'] > 0) {
  318. $EndOfCentralDirectory['comment'] = $this->fread($EndOfCentralDirectory['comment_length']);
  319. }
  320. return $EndOfCentralDirectory;
  321. }
  322. public static function ZIPparseGeneralPurposeFlags($flagbytes, $compressionmethod) {
  323. // https://users.cs.jmu.edu/buchhofp/forensics/formats/pkzip-printable.html
  324. $ParsedFlags['encrypted'] = (bool) ($flagbytes & 0x0001);
  325. // 0x0002 -- see below
  326. // 0x0004 -- see below
  327. $ParsedFlags['data_descriptor_used'] = (bool) ($flagbytes & 0x0008);
  328. $ParsedFlags['enhanced_deflation'] = (bool) ($flagbytes & 0x0010);
  329. $ParsedFlags['compressed_patched_data'] = (bool) ($flagbytes & 0x0020);
  330. $ParsedFlags['strong_encryption'] = (bool) ($flagbytes & 0x0040);
  331. // 0x0080 - unused
  332. // 0x0100 - unused
  333. // 0x0200 - unused
  334. // 0x0400 - unused
  335. $ParsedFlags['language_encoding'] = (bool) ($flagbytes & 0x0800);
  336. // 0x1000 - reserved
  337. $ParsedFlags['mask_header_values'] = (bool) ($flagbytes & 0x2000);
  338. // 0x4000 - reserved
  339. // 0x8000 - reserved
  340. switch ($compressionmethod) {
  341. case 6:
  342. $ParsedFlags['dictionary_size'] = (($flagbytes & 0x0002) ? 8192 : 4096);
  343. $ParsedFlags['shannon_fano_trees'] = (($flagbytes & 0x0004) ? 3 : 2);
  344. break;
  345. case 8:
  346. case 9:
  347. switch (($flagbytes & 0x0006) >> 1) {
  348. case 0:
  349. $ParsedFlags['compression_speed'] = 'normal';
  350. break;
  351. case 1:
  352. $ParsedFlags['compression_speed'] = 'maximum';
  353. break;
  354. case 2:
  355. $ParsedFlags['compression_speed'] = 'fast';
  356. break;
  357. case 3:
  358. $ParsedFlags['compression_speed'] = 'superfast';
  359. break;
  360. }
  361. break;
  362. }
  363. return $ParsedFlags;
  364. }
  365. public static function ZIPversionOSLookup($index) {
  366. static $ZIPversionOSLookup = array(
  367. 0 => 'MS-DOS and OS/2 (FAT / VFAT / FAT32 file systems)',
  368. 1 => 'Amiga',
  369. 2 => 'OpenVMS',
  370. 3 => 'Unix',
  371. 4 => 'VM/CMS',
  372. 5 => 'Atari ST',
  373. 6 => 'OS/2 H.P.F.S.',
  374. 7 => 'Macintosh',
  375. 8 => 'Z-System',
  376. 9 => 'CP/M',
  377. 10 => 'Windows NTFS',
  378. 11 => 'MVS',
  379. 12 => 'VSE',
  380. 13 => 'Acorn Risc',
  381. 14 => 'VFAT',
  382. 15 => 'Alternate MVS',
  383. 16 => 'BeOS',
  384. 17 => 'Tandem',
  385. 18 => 'OS/400',
  386. 19 => 'OS/X (Darwin)',
  387. );
  388. return (isset($ZIPversionOSLookup[$index]) ? $ZIPversionOSLookup[$index] : '[unknown]');
  389. }
  390. public static function ZIPcompressionMethodLookup($index) {
  391. // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/ZIP.html
  392. static $ZIPcompressionMethodLookup = array(
  393. 0 => 'store',
  394. 1 => 'shrink',
  395. 2 => 'reduce-1',
  396. 3 => 'reduce-2',
  397. 4 => 'reduce-3',
  398. 5 => 'reduce-4',
  399. 6 => 'implode',
  400. 7 => 'tokenize',
  401. 8 => 'deflate',
  402. 9 => 'deflate64',
  403. 10 => 'Imploded (old IBM TERSE)',
  404. 11 => 'RESERVED[11]',
  405. 12 => 'BZIP2',
  406. 13 => 'RESERVED[13]',
  407. 14 => 'LZMA (EFS)',
  408. 15 => 'RESERVED[15]',
  409. 16 => 'RESERVED[16]',
  410. 17 => 'RESERVED[17]',
  411. 18 => 'IBM TERSE (new)',
  412. 19 => 'IBM LZ77 z Architecture (PFS)',
  413. 96 => 'JPEG recompressed',
  414. 97 => 'WavPack compressed',
  415. 98 => 'PPMd version I, Rev 1',
  416. );
  417. return (isset($ZIPcompressionMethodLookup[$index]) ? $ZIPcompressionMethodLookup[$index] : '[unknown]');
  418. }
  419. public static function DOStime2UNIXtime($DOSdate, $DOStime) {
  420. // wFatDate
  421. // Specifies the MS-DOS date. The date is a packed 16-bit value with the following format:
  422. // Bits Contents
  423. // 0-4 Day of the month (1-31)
  424. // 5-8 Month (1 = January, 2 = February, and so on)
  425. // 9-15 Year offset from 1980 (add 1980 to get actual year)
  426. $UNIXday = ($DOSdate & 0x001F);
  427. $UNIXmonth = (($DOSdate & 0x01E0) >> 5);
  428. $UNIXyear = (($DOSdate & 0xFE00) >> 9) + 1980;
  429. // wFatTime
  430. // Specifies the MS-DOS time. The time is a packed 16-bit value with the following format:
  431. // Bits Contents
  432. // 0-4 Second divided by 2
  433. // 5-10 Minute (0-59)
  434. // 11-15 Hour (0-23 on a 24-hour clock)
  435. $UNIXsecond = ($DOStime & 0x001F) * 2;
  436. $UNIXminute = (($DOStime & 0x07E0) >> 5);
  437. $UNIXhour = (($DOStime & 0xF800) >> 11);
  438. return gmmktime($UNIXhour, $UNIXminute, $UNIXsecond, $UNIXmonth, $UNIXday, $UNIXyear);
  439. }
  440. }