PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/phar/zip.c

http://github.com/php/php-src
C | 1542 lines | 1199 code | 205 blank | 138 comment | 356 complexity | 889fd50efa58186abdba70a2cfecdde9 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. +----------------------------------------------------------------------+
  3. | ZIP archive support for Phar |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Gregory Beaver <cellog@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "phar_internal.h"
  19. #define PHAR_GET_16(var) ((uint16_t)((((uint16_t)var[0]) & 0xff) | \
  20. (((uint16_t)var[1]) & 0xff) << 8))
  21. #define PHAR_GET_32(var) ((uint32_t)((((uint32_t)var[0]) & 0xff) | \
  22. (((uint32_t)var[1]) & 0xff) << 8 | \
  23. (((uint32_t)var[2]) & 0xff) << 16 | \
  24. (((uint32_t)var[3]) & 0xff) << 24))
  25. static inline void phar_write_32(char buffer[4], uint32_t value)
  26. {
  27. buffer[3] = (unsigned char) ((value & 0xff000000) >> 24);
  28. buffer[2] = (unsigned char) ((value & 0xff0000) >> 16);
  29. buffer[1] = (unsigned char) ((value & 0xff00) >> 8);
  30. buffer[0] = (unsigned char) (value & 0xff);
  31. }
  32. static inline void phar_write_16(char buffer[2], uint32_t value)
  33. {
  34. buffer[1] = (unsigned char) ((value & 0xff00) >> 8);
  35. buffer[0] = (unsigned char) (value & 0xff);
  36. }
  37. # define PHAR_SET_32(var, value) phar_write_32(var, (uint32_t) (value));
  38. # define PHAR_SET_16(var, value) phar_write_16(var, (uint16_t) (value));
  39. static int phar_zip_process_extra(php_stream *fp, phar_entry_info *entry, uint16_t len) /* {{{ */
  40. {
  41. union {
  42. phar_zip_extra_field_header header;
  43. phar_zip_unix3 unix3;
  44. } h;
  45. size_t read;
  46. do {
  47. if (sizeof(h.header) != php_stream_read(fp, (char *) &h.header, sizeof(h.header))) {
  48. return FAILURE;
  49. }
  50. if (h.header.tag[0] != 'n' || h.header.tag[1] != 'u') {
  51. /* skip to next header */
  52. php_stream_seek(fp, PHAR_GET_16(h.header.size), SEEK_CUR);
  53. len -= PHAR_GET_16(h.header.size) + 4;
  54. continue;
  55. }
  56. /* unix3 header found */
  57. read = php_stream_read(fp, (char *) &(h.unix3.crc32), sizeof(h.unix3) - sizeof(h.header));
  58. len -= read + 4;
  59. if (sizeof(h.unix3) - sizeof(h.header) != read) {
  60. return FAILURE;
  61. }
  62. if (PHAR_GET_16(h.unix3.size) > sizeof(h.unix3) - 4) {
  63. /* skip symlink filename - we may add this support in later */
  64. php_stream_seek(fp, PHAR_GET_16(h.unix3.size) - sizeof(h.unix3.size), SEEK_CUR);
  65. }
  66. /* set permissions */
  67. entry->flags &= PHAR_ENT_COMPRESSION_MASK;
  68. if (entry->is_dir) {
  69. entry->flags |= PHAR_GET_16(h.unix3.perms) & PHAR_ENT_PERM_MASK;
  70. } else {
  71. entry->flags |= PHAR_GET_16(h.unix3.perms) & PHAR_ENT_PERM_MASK;
  72. }
  73. } while (len);
  74. return SUCCESS;
  75. }
  76. /* }}} */
  77. /*
  78. extracted from libzip
  79. zip_dirent.c -- read directory entry (local or central), clean dirent
  80. Copyright (C) 1999, 2003, 2004, 2005 Dieter Baron and Thomas Klausner
  81. This function is part of libzip, a library to manipulate ZIP archives.
  82. The authors can be contacted at <nih@giga.or.at>
  83. Redistribution and use in source and binary forms, with or without
  84. modification, are permitted provided that the following conditions
  85. are met:
  86. 1. Redistributions of source code must retain the above copyright
  87. notice, this list of conditions and the following disclaimer.
  88. 2. Redistributions in binary form must reproduce the above copyright
  89. notice, this list of conditions and the following disclaimer in
  90. the documentation and/or other materials provided with the
  91. distribution.
  92. 3. The names of the authors may not be used to endorse or promote
  93. products derived from this software without specific prior
  94. written permission.
  95. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  96. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  97. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  98. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  99. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  100. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  101. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  102. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  103. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  104. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  105. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  106. */
  107. static time_t phar_zip_d2u_time(char *cdtime, char *cddate) /* {{{ */
  108. {
  109. int dtime = PHAR_GET_16(cdtime), ddate = PHAR_GET_16(cddate);
  110. struct tm *tm, tmbuf;
  111. time_t now;
  112. now = time(NULL);
  113. tm = php_localtime_r(&now, &tmbuf);
  114. tm->tm_year = ((ddate>>9)&127) + 1980 - 1900;
  115. tm->tm_mon = ((ddate>>5)&15) - 1;
  116. tm->tm_mday = ddate&31;
  117. tm->tm_hour = (dtime>>11)&31;
  118. tm->tm_min = (dtime>>5)&63;
  119. tm->tm_sec = (dtime<<1)&62;
  120. return mktime(tm);
  121. }
  122. /* }}} */
  123. static void phar_zip_u2d_time(time_t time, char *dtime, char *ddate) /* {{{ */
  124. {
  125. uint16_t ctime, cdate;
  126. struct tm *tm, tmbuf;
  127. tm = php_localtime_r(&time, &tmbuf);
  128. if (tm->tm_year >= 1980) {
  129. cdate = ((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5) + tm->tm_mday;
  130. ctime = ((tm->tm_hour)<<11) + ((tm->tm_min)<<5) + ((tm->tm_sec)>>1);
  131. } else {
  132. /* This is the earliest date/time supported by zip. */
  133. cdate = (1<<5) + 1; /* 1980-01-01 */
  134. ctime = 0; /* 00:00:00 */
  135. }
  136. PHAR_SET_16(dtime, ctime);
  137. PHAR_SET_16(ddate, cdate);
  138. }
  139. /* }}} */
  140. /**
  141. * Does not check for a previously opened phar in the cache.
  142. *
  143. * Parse a new one and add it to the cache, returning either SUCCESS or
  144. * FAILURE, and setting pphar to the pointer to the manifest entry
  145. *
  146. * This is used by phar_open_from_fp to process a zip-based phar, but can be called
  147. * directly.
  148. */
  149. int phar_parse_zipfile(php_stream *fp, char *fname, size_t fname_len, char *alias, size_t alias_len, phar_archive_data** pphar, char **error) /* {{{ */
  150. {
  151. phar_zip_dir_end locator;
  152. char buf[sizeof(locator) + 65536];
  153. zend_off_t size;
  154. uint16_t i;
  155. phar_archive_data *mydata = NULL;
  156. phar_entry_info entry = {0};
  157. char *p = buf, *ext, *actual_alias = NULL;
  158. char *metadata = NULL;
  159. size = php_stream_tell(fp);
  160. if (size > sizeof(locator) + 65536) {
  161. /* seek to max comment length + end of central directory record */
  162. size = sizeof(locator) + 65536;
  163. if (FAILURE == php_stream_seek(fp, -size, SEEK_END)) {
  164. php_stream_close(fp);
  165. if (error) {
  166. spprintf(error, 4096, "phar error: unable to search for end of central directory in zip-based phar \"%s\"", fname);
  167. }
  168. return FAILURE;
  169. }
  170. } else {
  171. php_stream_seek(fp, 0, SEEK_SET);
  172. }
  173. if (!php_stream_read(fp, buf, size)) {
  174. php_stream_close(fp);
  175. if (error) {
  176. spprintf(error, 4096, "phar error: unable to read in data to search for end of central directory in zip-based phar \"%s\"", fname);
  177. }
  178. return FAILURE;
  179. }
  180. while ((p=(char *) memchr(p + 1, 'P', (size_t) (size - (p + 1 - buf)))) != NULL) {
  181. if ((p - buf) + sizeof(locator) <= (size_t)size && !memcmp(p + 1, "K\5\6", 3)) {
  182. memcpy((void *)&locator, (void *) p, sizeof(locator));
  183. if (PHAR_GET_16(locator.centraldisk) != 0 || PHAR_GET_16(locator.disknumber) != 0) {
  184. /* split archives not handled */
  185. php_stream_close(fp);
  186. if (error) {
  187. spprintf(error, 4096, "phar error: split archives spanning multiple zips cannot be processed in zip-based phar \"%s\"", fname);
  188. }
  189. return FAILURE;
  190. }
  191. if (PHAR_GET_16(locator.counthere) != PHAR_GET_16(locator.count)) {
  192. if (error) {
  193. spprintf(error, 4096, "phar error: corrupt zip archive, conflicting file count in end of central directory record in zip-based phar \"%s\"", fname);
  194. }
  195. php_stream_close(fp);
  196. return FAILURE;
  197. }
  198. mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
  199. mydata->is_persistent = PHAR_G(persist);
  200. /* read in archive comment, if any */
  201. if (PHAR_GET_16(locator.comment_len)) {
  202. metadata = p + sizeof(locator);
  203. if (PHAR_GET_16(locator.comment_len) != size - (metadata - buf)) {
  204. if (error) {
  205. spprintf(error, 4096, "phar error: corrupt zip archive, zip file comment truncated in zip-based phar \"%s\"", fname);
  206. }
  207. php_stream_close(fp);
  208. pefree(mydata, mydata->is_persistent);
  209. return FAILURE;
  210. }
  211. mydata->metadata_len = PHAR_GET_16(locator.comment_len);
  212. if (phar_parse_metadata(&metadata, &mydata->metadata, PHAR_GET_16(locator.comment_len)) == FAILURE) {
  213. mydata->metadata_len = 0;
  214. /* if not valid serialized data, it is a regular string */
  215. ZVAL_NEW_STR(&mydata->metadata, zend_string_init(metadata, PHAR_GET_16(locator.comment_len), mydata->is_persistent));
  216. }
  217. } else {
  218. ZVAL_UNDEF(&mydata->metadata);
  219. }
  220. goto foundit;
  221. }
  222. }
  223. php_stream_close(fp);
  224. if (error) {
  225. spprintf(error, 4096, "phar error: end of central directory not found in zip-based phar \"%s\"", fname);
  226. }
  227. return FAILURE;
  228. foundit:
  229. mydata->fname = pestrndup(fname, fname_len, mydata->is_persistent);
  230. #ifdef PHP_WIN32
  231. phar_unixify_path_separators(mydata->fname, fname_len);
  232. #endif
  233. mydata->is_zip = 1;
  234. mydata->fname_len = fname_len;
  235. ext = strrchr(mydata->fname, '/');
  236. if (ext) {
  237. mydata->ext = memchr(ext, '.', (mydata->fname + fname_len) - ext);
  238. if (mydata->ext == ext) {
  239. mydata->ext = memchr(ext + 1, '.', (mydata->fname + fname_len) - ext - 1);
  240. }
  241. if (mydata->ext) {
  242. mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
  243. }
  244. }
  245. /* clean up on big-endian systems */
  246. /* seek to central directory */
  247. php_stream_seek(fp, PHAR_GET_32(locator.cdir_offset), SEEK_SET);
  248. /* read in central directory */
  249. zend_hash_init(&mydata->manifest, PHAR_GET_16(locator.count),
  250. zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)mydata->is_persistent);
  251. zend_hash_init(&mydata->mounted_dirs, 5,
  252. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  253. zend_hash_init(&mydata->virtual_dirs, PHAR_GET_16(locator.count) * 2,
  254. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  255. entry.phar = mydata;
  256. entry.is_zip = 1;
  257. entry.fp_type = PHAR_FP;
  258. entry.is_persistent = mydata->is_persistent;
  259. #define PHAR_ZIP_FAIL_FREE(errmsg, save) \
  260. zend_hash_destroy(&mydata->manifest); \
  261. HT_INVALIDATE(&mydata->manifest); \
  262. zend_hash_destroy(&mydata->mounted_dirs); \
  263. HT_INVALIDATE(&mydata->mounted_dirs); \
  264. zend_hash_destroy(&mydata->virtual_dirs); \
  265. HT_INVALIDATE(&mydata->virtual_dirs); \
  266. php_stream_close(fp); \
  267. zval_ptr_dtor(&mydata->metadata); \
  268. if (mydata->signature) { \
  269. efree(mydata->signature); \
  270. } \
  271. if (error) { \
  272. spprintf(error, 4096, "phar error: %s in zip-based phar \"%s\"", errmsg, mydata->fname); \
  273. } \
  274. pefree(mydata->fname, mydata->is_persistent); \
  275. if (mydata->alias) { \
  276. pefree(mydata->alias, mydata->is_persistent); \
  277. } \
  278. pefree(mydata, mydata->is_persistent); \
  279. efree(save); \
  280. return FAILURE;
  281. #define PHAR_ZIP_FAIL(errmsg) \
  282. zend_hash_destroy(&mydata->manifest); \
  283. HT_INVALIDATE(&mydata->manifest); \
  284. zend_hash_destroy(&mydata->mounted_dirs); \
  285. HT_INVALIDATE(&mydata->mounted_dirs); \
  286. zend_hash_destroy(&mydata->virtual_dirs); \
  287. HT_INVALIDATE(&mydata->virtual_dirs); \
  288. php_stream_close(fp); \
  289. zval_ptr_dtor(&mydata->metadata); \
  290. if (mydata->signature) { \
  291. efree(mydata->signature); \
  292. } \
  293. if (error) { \
  294. spprintf(error, 4096, "phar error: %s in zip-based phar \"%s\"", errmsg, mydata->fname); \
  295. } \
  296. pefree(mydata->fname, mydata->is_persistent); \
  297. if (mydata->alias) { \
  298. pefree(mydata->alias, mydata->is_persistent); \
  299. } \
  300. pefree(mydata, mydata->is_persistent); \
  301. return FAILURE;
  302. /* add each central directory item to the manifest */
  303. for (i = 0; i < PHAR_GET_16(locator.count); ++i) {
  304. phar_zip_central_dir_file zipentry;
  305. zend_off_t beforeus = php_stream_tell(fp);
  306. if (sizeof(zipentry) != php_stream_read(fp, (char *) &zipentry, sizeof(zipentry))) {
  307. PHAR_ZIP_FAIL("unable to read central directory entry, truncated");
  308. }
  309. /* clean up for bigendian systems */
  310. if (memcmp("PK\1\2", zipentry.signature, 4)) {
  311. /* corrupted entry */
  312. PHAR_ZIP_FAIL("corrupted central directory entry, no magic signature");
  313. }
  314. if (entry.is_persistent) {
  315. entry.manifest_pos = i;
  316. }
  317. entry.compressed_filesize = PHAR_GET_32(zipentry.compsize);
  318. entry.uncompressed_filesize = PHAR_GET_32(zipentry.uncompsize);
  319. entry.crc32 = PHAR_GET_32(zipentry.crc32);
  320. /* do not PHAR_GET_16 either on the next line */
  321. entry.timestamp = phar_zip_d2u_time(zipentry.timestamp, zipentry.datestamp);
  322. entry.flags = PHAR_ENT_PERM_DEF_FILE;
  323. entry.header_offset = PHAR_GET_32(zipentry.offset);
  324. entry.offset = entry.offset_abs = PHAR_GET_32(zipentry.offset) + sizeof(phar_zip_file_header) + PHAR_GET_16(zipentry.filename_len) +
  325. PHAR_GET_16(zipentry.extra_len);
  326. if (PHAR_GET_16(zipentry.flags) & PHAR_ZIP_FLAG_ENCRYPTED) {
  327. PHAR_ZIP_FAIL("Cannot process encrypted zip files");
  328. }
  329. if (!PHAR_GET_16(zipentry.filename_len)) {
  330. PHAR_ZIP_FAIL("Cannot process zips created from stdin (zero-length filename)");
  331. }
  332. entry.filename_len = PHAR_GET_16(zipentry.filename_len);
  333. entry.filename = (char *) pemalloc(entry.filename_len + 1, entry.is_persistent);
  334. if (entry.filename_len != php_stream_read(fp, entry.filename, entry.filename_len)) {
  335. pefree(entry.filename, entry.is_persistent);
  336. PHAR_ZIP_FAIL("unable to read in filename from central directory, truncated");
  337. }
  338. entry.filename[entry.filename_len] = '\0';
  339. if (entry.filename[entry.filename_len - 1] == '/') {
  340. entry.is_dir = 1;
  341. if(entry.filename_len > 1) {
  342. entry.filename_len--;
  343. }
  344. entry.flags |= PHAR_ENT_PERM_DEF_DIR;
  345. } else {
  346. entry.is_dir = 0;
  347. }
  348. if (entry.filename_len == sizeof(".phar/signature.bin")-1 && !strncmp(entry.filename, ".phar/signature.bin", sizeof(".phar/signature.bin")-1)) {
  349. size_t read;
  350. php_stream *sigfile;
  351. char *sig;
  352. size_t sig_len;
  353. php_stream_tell(fp);
  354. pefree(entry.filename, entry.is_persistent);
  355. sigfile = php_stream_fopen_tmpfile();
  356. if (!sigfile) {
  357. PHAR_ZIP_FAIL("couldn't open temporary file");
  358. }
  359. php_stream_seek(fp, 0, SEEK_SET);
  360. /* copy file contents + local headers and zip comment, if any, to be hashed for signature */
  361. php_stream_copy_to_stream_ex(fp, sigfile, entry.header_offset, NULL);
  362. /* seek to central directory */
  363. php_stream_seek(fp, PHAR_GET_32(locator.cdir_offset), SEEK_SET);
  364. /* copy central directory header */
  365. php_stream_copy_to_stream_ex(fp, sigfile, beforeus - PHAR_GET_32(locator.cdir_offset), NULL);
  366. if (metadata) {
  367. php_stream_write(sigfile, metadata, PHAR_GET_16(locator.comment_len));
  368. }
  369. php_stream_seek(fp, sizeof(phar_zip_file_header) + entry.header_offset + entry.filename_len + PHAR_GET_16(zipentry.extra_len), SEEK_SET);
  370. sig = (char *) emalloc(entry.uncompressed_filesize);
  371. read = php_stream_read(fp, sig, entry.uncompressed_filesize);
  372. if (read != entry.uncompressed_filesize || read <= 8) {
  373. php_stream_close(sigfile);
  374. efree(sig);
  375. PHAR_ZIP_FAIL("signature cannot be read");
  376. }
  377. mydata->sig_flags = PHAR_GET_32(sig);
  378. if (FAILURE == phar_verify_signature(sigfile, php_stream_tell(sigfile), mydata->sig_flags, sig + 8, entry.uncompressed_filesize - 8, fname, &mydata->signature, &sig_len, error)) {
  379. efree(sig);
  380. if (error) {
  381. char *save;
  382. php_stream_close(sigfile);
  383. spprintf(&save, 4096, "signature cannot be verified: %s", *error);
  384. efree(*error);
  385. PHAR_ZIP_FAIL_FREE(save, save);
  386. } else {
  387. php_stream_close(sigfile);
  388. PHAR_ZIP_FAIL("signature cannot be verified");
  389. }
  390. }
  391. mydata->sig_len = sig_len;
  392. php_stream_close(sigfile);
  393. efree(sig);
  394. /* signature checked out, let's ensure this is the last file in the phar */
  395. if (i != PHAR_GET_16(locator.count) - 1) {
  396. PHAR_ZIP_FAIL("entries exist after signature, invalid phar");
  397. }
  398. continue;
  399. }
  400. phar_add_virtual_dirs(mydata, entry.filename, entry.filename_len);
  401. if (PHAR_GET_16(zipentry.extra_len)) {
  402. zend_off_t loc = php_stream_tell(fp);
  403. if (FAILURE == phar_zip_process_extra(fp, &entry, PHAR_GET_16(zipentry.extra_len))) {
  404. pefree(entry.filename, entry.is_persistent);
  405. PHAR_ZIP_FAIL("Unable to process extra field header for file in central directory");
  406. }
  407. php_stream_seek(fp, loc + PHAR_GET_16(zipentry.extra_len), SEEK_SET);
  408. }
  409. switch (PHAR_GET_16(zipentry.compressed)) {
  410. case PHAR_ZIP_COMP_NONE :
  411. /* compression flag already set */
  412. break;
  413. case PHAR_ZIP_COMP_DEFLATE :
  414. entry.flags |= PHAR_ENT_COMPRESSED_GZ;
  415. if (!PHAR_G(has_zlib)) {
  416. pefree(entry.filename, entry.is_persistent);
  417. PHAR_ZIP_FAIL("zlib extension is required");
  418. }
  419. break;
  420. case PHAR_ZIP_COMP_BZIP2 :
  421. entry.flags |= PHAR_ENT_COMPRESSED_BZ2;
  422. if (!PHAR_G(has_bz2)) {
  423. pefree(entry.filename, entry.is_persistent);
  424. PHAR_ZIP_FAIL("bzip2 extension is required");
  425. }
  426. break;
  427. case 1 :
  428. pefree(entry.filename, entry.is_persistent);
  429. PHAR_ZIP_FAIL("unsupported compression method (Shrunk) used in this zip");
  430. case 2 :
  431. case 3 :
  432. case 4 :
  433. case 5 :
  434. pefree(entry.filename, entry.is_persistent);
  435. PHAR_ZIP_FAIL("unsupported compression method (Reduce) used in this zip");
  436. case 6 :
  437. pefree(entry.filename, entry.is_persistent);
  438. PHAR_ZIP_FAIL("unsupported compression method (Implode) used in this zip");
  439. case 7 :
  440. pefree(entry.filename, entry.is_persistent);
  441. PHAR_ZIP_FAIL("unsupported compression method (Tokenize) used in this zip");
  442. case 9 :
  443. pefree(entry.filename, entry.is_persistent);
  444. PHAR_ZIP_FAIL("unsupported compression method (Deflate64) used in this zip");
  445. case 10 :
  446. pefree(entry.filename, entry.is_persistent);
  447. PHAR_ZIP_FAIL("unsupported compression method (PKWare Implode/old IBM TERSE) used in this zip");
  448. case 14 :
  449. pefree(entry.filename, entry.is_persistent);
  450. PHAR_ZIP_FAIL("unsupported compression method (LZMA) used in this zip");
  451. case 18 :
  452. pefree(entry.filename, entry.is_persistent);
  453. PHAR_ZIP_FAIL("unsupported compression method (IBM TERSE) used in this zip");
  454. case 19 :
  455. pefree(entry.filename, entry.is_persistent);
  456. PHAR_ZIP_FAIL("unsupported compression method (IBM LZ77) used in this zip");
  457. case 97 :
  458. pefree(entry.filename, entry.is_persistent);
  459. PHAR_ZIP_FAIL("unsupported compression method (WavPack) used in this zip");
  460. case 98 :
  461. pefree(entry.filename, entry.is_persistent);
  462. PHAR_ZIP_FAIL("unsupported compression method (PPMd) used in this zip");
  463. default :
  464. pefree(entry.filename, entry.is_persistent);
  465. PHAR_ZIP_FAIL("unsupported compression method (unknown) used in this zip");
  466. }
  467. /* get file metadata */
  468. if (PHAR_GET_16(zipentry.comment_len)) {
  469. if (PHAR_GET_16(zipentry.comment_len) != php_stream_read(fp, buf, PHAR_GET_16(zipentry.comment_len))) {
  470. pefree(entry.filename, entry.is_persistent);
  471. PHAR_ZIP_FAIL("unable to read in file comment, truncated");
  472. }
  473. p = buf;
  474. entry.metadata_len = PHAR_GET_16(zipentry.comment_len);
  475. if (phar_parse_metadata(&p, &(entry.metadata), PHAR_GET_16(zipentry.comment_len)) == FAILURE) {
  476. entry.metadata_len = 0;
  477. /* if not valid serialized data, it is a regular string */
  478. ZVAL_NEW_STR(&entry.metadata, zend_string_init(buf, PHAR_GET_16(zipentry.comment_len), entry.is_persistent));
  479. }
  480. } else {
  481. ZVAL_UNDEF(&entry.metadata);
  482. }
  483. if (!actual_alias && entry.filename_len == sizeof(".phar/alias.txt")-1 && !strncmp(entry.filename, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
  484. php_stream_filter *filter;
  485. zend_off_t saveloc;
  486. /* verify local file header */
  487. phar_zip_file_header local;
  488. /* archive alias found */
  489. saveloc = php_stream_tell(fp);
  490. php_stream_seek(fp, PHAR_GET_32(zipentry.offset), SEEK_SET);
  491. if (sizeof(local) != php_stream_read(fp, (char *) &local, sizeof(local))) {
  492. pefree(entry.filename, entry.is_persistent);
  493. PHAR_ZIP_FAIL("phar error: internal corruption of zip-based phar (cannot read local file header for alias)");
  494. }
  495. /* verify local header */
  496. if (entry.filename_len != PHAR_GET_16(local.filename_len) || entry.crc32 != PHAR_GET_32(local.crc32) || entry.uncompressed_filesize != PHAR_GET_32(local.uncompsize) || entry.compressed_filesize != PHAR_GET_32(local.compsize)) {
  497. pefree(entry.filename, entry.is_persistent);
  498. PHAR_ZIP_FAIL("phar error: internal corruption of zip-based phar (local header of alias does not match central directory)");
  499. }
  500. /* construct actual offset to file start - local extra_len can be different from central extra_len */
  501. entry.offset = entry.offset_abs =
  502. sizeof(local) + entry.header_offset + PHAR_GET_16(local.filename_len) + PHAR_GET_16(local.extra_len);
  503. php_stream_seek(fp, entry.offset, SEEK_SET);
  504. /* these next lines should be for php < 5.2.6 after 5.3 filters are fixed */
  505. fp->writepos = 0;
  506. fp->readpos = 0;
  507. php_stream_seek(fp, entry.offset, SEEK_SET);
  508. fp->writepos = 0;
  509. fp->readpos = 0;
  510. /* the above lines should be for php < 5.2.6 after 5.3 filters are fixed */
  511. mydata->alias_len = entry.uncompressed_filesize;
  512. if (entry.flags & PHAR_ENT_COMPRESSED_GZ) {
  513. filter = php_stream_filter_create("zlib.inflate", NULL, php_stream_is_persistent(fp));
  514. if (!filter) {
  515. pefree(entry.filename, entry.is_persistent);
  516. PHAR_ZIP_FAIL("unable to decompress alias, zlib filter creation failed");
  517. }
  518. php_stream_filter_append(&fp->readfilters, filter);
  519. // TODO: refactor to avoid reallocation ???
  520. //??? entry.uncompressed_filesize = php_stream_copy_to_mem(fp, &actual_alias, entry.uncompressed_filesize, 0)
  521. {
  522. zend_string *str = php_stream_copy_to_mem(fp, entry.uncompressed_filesize, 0);
  523. if (str) {
  524. entry.uncompressed_filesize = ZSTR_LEN(str);
  525. actual_alias = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  526. zend_string_release_ex(str, 0);
  527. } else {
  528. actual_alias = NULL;
  529. entry.uncompressed_filesize = 0;
  530. }
  531. }
  532. if (!entry.uncompressed_filesize || !actual_alias) {
  533. pefree(entry.filename, entry.is_persistent);
  534. PHAR_ZIP_FAIL("unable to read in alias, truncated");
  535. }
  536. php_stream_filter_flush(filter, 1);
  537. php_stream_filter_remove(filter, 1);
  538. } else if (entry.flags & PHAR_ENT_COMPRESSED_BZ2) {
  539. filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp));
  540. if (!filter) {
  541. pefree(entry.filename, entry.is_persistent);
  542. PHAR_ZIP_FAIL("unable to read in alias, bzip2 filter creation failed");
  543. }
  544. php_stream_filter_append(&fp->readfilters, filter);
  545. // TODO: refactor to avoid reallocation ???
  546. //??? entry.uncompressed_filesize = php_stream_copy_to_mem(fp, &actual_alias, entry.uncompressed_filesize, 0)
  547. {
  548. zend_string *str = php_stream_copy_to_mem(fp, entry.uncompressed_filesize, 0);
  549. if (str) {
  550. entry.uncompressed_filesize = ZSTR_LEN(str);
  551. actual_alias = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  552. zend_string_release_ex(str, 0);
  553. } else {
  554. actual_alias = NULL;
  555. entry.uncompressed_filesize = 0;
  556. }
  557. }
  558. if (!entry.uncompressed_filesize || !actual_alias) {
  559. pefree(entry.filename, entry.is_persistent);
  560. PHAR_ZIP_FAIL("unable to read in alias, truncated");
  561. }
  562. php_stream_filter_flush(filter, 1);
  563. php_stream_filter_remove(filter, 1);
  564. } else {
  565. // TODO: refactor to avoid reallocation ???
  566. //??? entry.uncompressed_filesize = php_stream_copy_to_mem(fp, &actual_alias, entry.uncompressed_filesize, 0)
  567. {
  568. zend_string *str = php_stream_copy_to_mem(fp, entry.uncompressed_filesize, 0);
  569. if (str) {
  570. entry.uncompressed_filesize = ZSTR_LEN(str);
  571. actual_alias = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  572. zend_string_release_ex(str, 0);
  573. } else {
  574. actual_alias = NULL;
  575. entry.uncompressed_filesize = 0;
  576. }
  577. }
  578. if (!entry.uncompressed_filesize || !actual_alias) {
  579. pefree(entry.filename, entry.is_persistent);
  580. PHAR_ZIP_FAIL("unable to read in alias, truncated");
  581. }
  582. }
  583. /* return to central directory parsing */
  584. php_stream_seek(fp, saveloc, SEEK_SET);
  585. }
  586. phar_set_inode(&entry);
  587. zend_hash_str_add_mem(&mydata->manifest, entry.filename, entry.filename_len, (void *)&entry, sizeof(phar_entry_info));
  588. }
  589. mydata->fp = fp;
  590. if (zend_hash_str_exists(&(mydata->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  591. mydata->is_data = 0;
  592. } else {
  593. mydata->is_data = 1;
  594. }
  595. zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len, mydata);
  596. if (actual_alias) {
  597. phar_archive_data *fd_ptr;
  598. if (!phar_validate_alias(actual_alias, mydata->alias_len)) {
  599. if (error) {
  600. spprintf(error, 4096, "phar error: invalid alias \"%s\" in zip-based phar \"%s\"", actual_alias, fname);
  601. }
  602. efree(actual_alias);
  603. zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
  604. return FAILURE;
  605. }
  606. mydata->is_temporary_alias = 0;
  607. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), actual_alias, mydata->alias_len))) {
  608. if (SUCCESS != phar_free_alias(fd_ptr, actual_alias, mydata->alias_len)) {
  609. if (error) {
  610. spprintf(error, 4096, "phar error: Unable to add zip-based phar \"%s\" with implicit alias, alias is already in use", fname);
  611. }
  612. efree(actual_alias);
  613. zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
  614. return FAILURE;
  615. }
  616. }
  617. mydata->alias = entry.is_persistent ? pestrndup(actual_alias, mydata->alias_len, 1) : actual_alias;
  618. if (entry.is_persistent) {
  619. efree(actual_alias);
  620. }
  621. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), actual_alias, mydata->alias_len, mydata);
  622. } else {
  623. phar_archive_data *fd_ptr;
  624. if (alias_len) {
  625. if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
  626. if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
  627. if (error) {
  628. spprintf(error, 4096, "phar error: Unable to add zip-based phar \"%s\" with explicit alias, alias is already in use", fname);
  629. }
  630. zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
  631. return FAILURE;
  632. }
  633. }
  634. zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), actual_alias, mydata->alias_len, mydata);
  635. mydata->alias = pestrndup(alias, alias_len, mydata->is_persistent);
  636. mydata->alias_len = alias_len;
  637. } else {
  638. mydata->alias = pestrndup(mydata->fname, fname_len, mydata->is_persistent);
  639. mydata->alias_len = fname_len;
  640. }
  641. mydata->is_temporary_alias = 1;
  642. }
  643. if (pphar) {
  644. *pphar = mydata;
  645. }
  646. return SUCCESS;
  647. }
  648. /* }}} */
  649. /**
  650. * Create or open a zip-based phar for writing
  651. */
  652. int phar_open_or_create_zip(char *fname, size_t fname_len, char *alias, size_t alias_len, int is_data, uint32_t options, phar_archive_data** pphar, char **error) /* {{{ */
  653. {
  654. phar_archive_data *phar;
  655. int ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error);
  656. if (FAILURE == ret) {
  657. return FAILURE;
  658. }
  659. if (pphar) {
  660. *pphar = phar;
  661. }
  662. phar->is_data = is_data;
  663. if (phar->is_zip) {
  664. return ret;
  665. }
  666. if (phar->is_brandnew) {
  667. phar->internal_file_start = 0;
  668. phar->is_zip = 1;
  669. phar->is_tar = 0;
  670. return SUCCESS;
  671. }
  672. /* we've reached here - the phar exists and is a regular phar */
  673. if (error) {
  674. spprintf(error, 4096, "phar zip error: phar \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a zip-based phar", fname);
  675. }
  676. return FAILURE;
  677. }
  678. /* }}} */
  679. struct _phar_zip_pass {
  680. php_stream *filefp;
  681. php_stream *centralfp;
  682. php_stream *old;
  683. int free_fp;
  684. int free_ufp;
  685. char **error;
  686. };
  687. /* perform final modification of zip contents for each file in the manifest before saving */
  688. static int phar_zip_changed_apply_int(phar_entry_info *entry, void *arg) /* {{{ */
  689. {
  690. phar_zip_file_header local;
  691. phar_zip_unix3 perms;
  692. phar_zip_central_dir_file central;
  693. struct _phar_zip_pass *p;
  694. uint32_t newcrc32;
  695. zend_off_t offset;
  696. int not_really_modified = 0;
  697. p = (struct _phar_zip_pass*) arg;
  698. if (entry->is_mounted) {
  699. return ZEND_HASH_APPLY_KEEP;
  700. }
  701. if (entry->is_deleted) {
  702. if (entry->fp_refcount <= 0) {
  703. return ZEND_HASH_APPLY_REMOVE;
  704. } else {
  705. /* we can't delete this in-memory until it is closed */
  706. return ZEND_HASH_APPLY_KEEP;
  707. }
  708. }
  709. phar_add_virtual_dirs(entry->phar, entry->filename, entry->filename_len);
  710. memset(&local, 0, sizeof(local));
  711. memset(&central, 0, sizeof(central));
  712. memset(&perms, 0, sizeof(perms));
  713. memcpy(local.signature, "PK\3\4", 4);
  714. memcpy(central.signature, "PK\1\2", 4);
  715. PHAR_SET_16(central.extra_len, sizeof(perms));
  716. PHAR_SET_16(local.extra_len, sizeof(perms));
  717. perms.tag[0] = 'n';
  718. perms.tag[1] = 'u';
  719. PHAR_SET_16(perms.size, sizeof(perms) - 4);
  720. PHAR_SET_16(perms.perms, entry->flags & PHAR_ENT_PERM_MASK);
  721. {
  722. uint32_t crc = (uint32_t) ~0;
  723. CRC32(crc, perms.perms[0]);
  724. CRC32(crc, perms.perms[1]);
  725. PHAR_SET_32(perms.crc32, ~crc);
  726. }
  727. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  728. PHAR_SET_16(central.compressed, PHAR_ZIP_COMP_DEFLATE);
  729. PHAR_SET_16(local.compressed, PHAR_ZIP_COMP_DEFLATE);
  730. }
  731. if (entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
  732. PHAR_SET_16(central.compressed, PHAR_ZIP_COMP_BZIP2);
  733. PHAR_SET_16(local.compressed, PHAR_ZIP_COMP_BZIP2);
  734. }
  735. /* do not use PHAR_GET_16 on either field of the next line */
  736. phar_zip_u2d_time(entry->timestamp, local.timestamp, local.datestamp);
  737. memcpy(central.timestamp, local.timestamp, sizeof(local.timestamp));
  738. memcpy(central.datestamp, local.datestamp, sizeof(local.datestamp));
  739. PHAR_SET_16(central.filename_len, entry->filename_len + (entry->is_dir ? 1 : 0));
  740. PHAR_SET_16(local.filename_len, entry->filename_len + (entry->is_dir ? 1 : 0));
  741. PHAR_SET_32(central.offset, php_stream_tell(p->filefp));
  742. /* do extra field for perms later */
  743. if (entry->is_modified) {
  744. uint32_t loc;
  745. php_stream_filter *filter;
  746. php_stream *efp;
  747. if (entry->is_dir) {
  748. entry->is_modified = 0;
  749. if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp) {
  750. php_stream_close(entry->fp);
  751. entry->fp = NULL;
  752. entry->fp_type = PHAR_FP;
  753. }
  754. goto continue_dir;
  755. }
  756. if (FAILURE == phar_open_entry_fp(entry, p->error, 0)) {
  757. spprintf(p->error, 0, "unable to open file contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  758. return ZEND_HASH_APPLY_STOP;
  759. }
  760. /* we can be modified and already be compressed, such as when chmod() is executed */
  761. if (entry->flags & PHAR_ENT_COMPRESSION_MASK && (entry->old_flags == entry->flags || !entry->old_flags)) {
  762. not_really_modified = 1;
  763. goto is_compressed;
  764. }
  765. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
  766. spprintf(p->error, 0, "unable to seek to start of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  767. return ZEND_HASH_APPLY_STOP;
  768. }
  769. efp = phar_get_efp(entry, 0);
  770. newcrc32 = ~0;
  771. for (loc = 0;loc < entry->uncompressed_filesize; ++loc) {
  772. CRC32(newcrc32, php_stream_getc(efp));
  773. }
  774. entry->crc32 = ~newcrc32;
  775. PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize);
  776. PHAR_SET_32(local.uncompsize, entry->uncompressed_filesize);
  777. if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
  778. /* not compressed */
  779. entry->compressed_filesize = entry->uncompressed_filesize;
  780. PHAR_SET_32(central.compsize, entry->uncompressed_filesize);
  781. PHAR_SET_32(local.compsize, entry->uncompressed_filesize);
  782. goto not_compressed;
  783. }
  784. filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0);
  785. if (!filter) {
  786. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  787. spprintf(p->error, 0, "unable to gzip compress file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  788. } else {
  789. spprintf(p->error, 0, "unable to bzip2 compress file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  790. }
  791. return ZEND_HASH_APPLY_STOP;
  792. }
  793. /* create new file that holds the compressed version */
  794. /* work around inability to specify freedom in write and strictness
  795. in read count */
  796. entry->cfp = php_stream_fopen_tmpfile();
  797. if (!entry->cfp) {
  798. spprintf(p->error, 0, "unable to create temporary file for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  799. return ZEND_HASH_APPLY_STOP;
  800. }
  801. php_stream_flush(efp);
  802. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
  803. spprintf(p->error, 0, "unable to seek to start of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  804. return ZEND_HASH_APPLY_STOP;
  805. }
  806. php_stream_filter_append((&entry->cfp->writefilters), filter);
  807. if (SUCCESS != php_stream_copy_to_stream_ex(efp, entry->cfp, entry->uncompressed_filesize, NULL)) {
  808. spprintf(p->error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, entry->phar->fname);
  809. return ZEND_HASH_APPLY_STOP;
  810. }
  811. php_stream_filter_flush(filter, 1);
  812. php_stream_flush(entry->cfp);
  813. php_stream_filter_remove(filter, 1);
  814. php_stream_seek(entry->cfp, 0, SEEK_END);
  815. entry->compressed_filesize = (uint32_t) php_stream_tell(entry->cfp);
  816. PHAR_SET_32(central.compsize, entry->compressed_filesize);
  817. PHAR_SET_32(local.compsize, entry->compressed_filesize);
  818. /* generate crc on compressed file */
  819. php_stream_rewind(entry->cfp);
  820. entry->old_flags = entry->flags;
  821. entry->is_modified = 1;
  822. } else {
  823. is_compressed:
  824. PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize);
  825. PHAR_SET_32(local.uncompsize, entry->uncompressed_filesize);
  826. PHAR_SET_32(central.compsize, entry->compressed_filesize);
  827. PHAR_SET_32(local.compsize, entry->compressed_filesize);
  828. if (p->old) {
  829. if (-1 == php_stream_seek(p->old, entry->offset_abs, SEEK_SET)) {
  830. spprintf(p->error, 0, "unable to seek to start of file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  831. return ZEND_HASH_APPLY_STOP;
  832. }
  833. }
  834. }
  835. not_compressed:
  836. PHAR_SET_32(central.crc32, entry->crc32);
  837. PHAR_SET_32(local.crc32, entry->crc32);
  838. continue_dir:
  839. /* set file metadata */
  840. if (Z_TYPE(entry->metadata) != IS_UNDEF) {
  841. php_serialize_data_t metadata_hash;
  842. if (entry->metadata_str.s) {
  843. smart_str_free(&entry->metadata_str);
  844. }
  845. entry->metadata_str.s = NULL;
  846. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  847. php_var_serialize(&entry->metadata_str, &entry->metadata, &metadata_hash);
  848. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  849. PHAR_SET_16(central.comment_len, ZSTR_LEN(entry->metadata_str.s));
  850. }
  851. entry->header_offset = php_stream_tell(p->filefp);
  852. offset = entry->header_offset + sizeof(local) + entry->filename_len + (entry->is_dir ? 1 : 0) + sizeof(perms);
  853. if (sizeof(local) != php_stream_write(p->filefp, (char *)&local, sizeof(local))) {
  854. spprintf(p->error, 0, "unable to write local file header of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  855. return ZEND_HASH_APPLY_STOP;
  856. }
  857. if (sizeof(central) != php_stream_write(p->centralfp, (char *)&central, sizeof(central))) {
  858. spprintf(p->error, 0, "unable to write central directory entry for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  859. return ZEND_HASH_APPLY_STOP;
  860. }
  861. if (entry->is_dir) {
  862. if (entry->filename_len != php_stream_write(p->filefp, entry->filename, entry->filename_len)) {
  863. spprintf(p->error, 0, "unable to write filename to local directory entry for directory \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  864. return ZEND_HASH_APPLY_STOP;
  865. }
  866. if (1 != php_stream_write(p->filefp, "/", 1)) {
  867. spprintf(p->error, 0, "unable to write filename to local directory entry for directory \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  868. return ZEND_HASH_APPLY_STOP;
  869. }
  870. if (entry->filename_len != php_stream_write(p->centralfp, entry->filename, entry->filename_len)) {
  871. spprintf(p->error, 0, "unable to write filename to central directory entry for directory \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  872. return ZEND_HASH_APPLY_STOP;
  873. }
  874. if (1 != php_stream_write(p->centralfp, "/", 1)) {
  875. spprintf(p->error, 0, "unable to write filename to central directory entry for directory \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  876. return ZEND_HASH_APPLY_STOP;
  877. }
  878. } else {
  879. if (entry->filename_len != php_stream_write(p->filefp, entry->filename, entry->filename_len)) {
  880. spprintf(p->error, 0, "unable to write filename to local directory entry for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  881. return ZEND_HASH_APPLY_STOP;
  882. }
  883. if (entry->filename_len != php_stream_write(p->centralfp, entry->filename, entry->filename_len)) {
  884. spprintf(p->error, 0, "unable to write filename to central directory entry for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  885. return ZEND_HASH_APPLY_STOP;
  886. }
  887. }
  888. if (sizeof(perms) != php_stream_write(p->filefp, (char *)&perms, sizeof(perms))) {
  889. spprintf(p->error, 0, "unable to write local extra permissions file header of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  890. return ZEND_HASH_APPLY_STOP;
  891. }
  892. if (sizeof(perms) != php_stream_write(p->centralfp, (char *)&perms, sizeof(perms))) {
  893. spprintf(p->error, 0, "unable to write central extra permissions file header of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  894. return ZEND_HASH_APPLY_STOP;
  895. }
  896. if (!not_really_modified && entry->is_modified) {
  897. if (entry->cfp) {
  898. if (SUCCESS != php_stream_copy_to_stream_ex(entry->cfp, p->filefp, entry->compressed_filesize, NULL)) {
  899. spprintf(p->error, 0, "unable to write compressed contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  900. return ZEND_HASH_APPLY_STOP;
  901. }
  902. php_stream_close(entry->cfp);
  903. entry->cfp = NULL;
  904. } else {
  905. if (FAILURE == phar_open_entry_fp(entry, p->error, 0)) {
  906. return ZEND_HASH_APPLY_STOP;
  907. }
  908. phar_seek_efp(entry, 0, SEEK_SET, 0, 0);
  909. if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), p->filefp, entry->uncompressed_filesize, NULL)) {
  910. spprintf(p->error, 0, "unable to write contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  911. return ZEND_HASH_APPLY_STOP;
  912. }
  913. }
  914. if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp && entry->fp_refcount == 0) {
  915. php_stream_close(entry->fp);
  916. }
  917. entry->is_modified = 0;
  918. } else {
  919. entry->is_modified = 0;
  920. if (entry->fp_refcount) {
  921. /* open file pointers refer to this fp, do not free the stream */
  922. switch (entry->fp_type) {
  923. case PHAR_FP:
  924. p->free_fp = 0;
  925. break;
  926. case PHAR_UFP:
  927. p->free_ufp = 0;
  928. default:
  929. break;
  930. }
  931. }
  932. if (!entry->is_dir && entry->compressed_filesize && SUCCESS != php_stream_copy_to_stream_ex(p->old, p->filefp, entry->compressed_filesize, NULL)) {
  933. spprintf(p->error, 0, "unable to copy contents of file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  934. return ZEND_HASH_APPLY_STOP;
  935. }
  936. }
  937. entry->fp = NULL;
  938. entry->offset = entry->offset_abs = offset;
  939. entry->fp_type = PHAR_FP;
  940. if (entry->metadata_str.s) {
  941. if (ZSTR_LEN(entry->metadata_str.s) != php_stream_write(p->centralfp, ZSTR_VAL(entry->metadata_str.s), ZSTR_LEN(entry->metadata_str.s))) {
  942. spprintf(p->error, 0, "unable to write metadata as file comment for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  943. smart_str_free(&entry->metadata_str);
  944. return ZEND_HASH_APPLY_STOP;
  945. }
  946. smart_str_free(&entry->metadata_str);
  947. }
  948. return ZEND_HASH_APPLY_KEEP;
  949. }
  950. /* }}} */
  951. static int phar_zip_changed_apply(zval *zv, void *arg) /* {{{ */
  952. {
  953. return phar_zip_changed_apply_int(Z_PTR_P(zv), arg);
  954. }
  955. /* }}} */
  956. static int phar_zip_applysignature(phar_archive_data *phar, struct _phar_zip_pass *pass,
  957. smart_str *metadata) /* {{{ */
  958. {
  959. /* add signature for executable tars or tars explicitly set with setSignatureAlgorithm */
  960. if (!phar->is_data || phar->sig_flags) {
  961. size_t signature_length;
  962. char *signature, sigbuf[8];
  963. phar_entry_info entry = {0};
  964. php_stream *newfile;
  965. zend_off_t tell;
  966. newfile = php_stream_fopen_tmpfile();
  967. if (newfile == NULL) {
  968. spprintf(pass->error, 0, "phar error: unable to create temporary file for the signature file");
  969. return FAILURE;
  970. }
  971. tell = php_stream_tell(pass->filefp);
  972. /* copy the local files, central directory, and the zip comment to generate the hash */
  973. php_stream_seek(pass->filefp, 0, SEEK_SET);
  974. php_stream_copy_to_stream_ex(pass->filefp, newfile, tell, NULL);
  975. tell = php_stream_tell(pass->centralfp);
  976. php_stream_seek(pass->centralfp, 0, SEEK_SET);
  977. php_stream_copy_to_stream_ex(pass->centralfp, newfile, tell, NULL);
  978. if (metadata->s) {
  979. php_stream_write(newfile, ZSTR_VAL(metadata->s), ZSTR_LEN(metadata->s));
  980. }
  981. if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, pass->error)) {
  982. if (pass->error) {
  983. char *save = *(pass->error);
  984. spprintf(pass->error, 0, "phar error: unable to write signature to zip-based phar: %s", save);
  985. efree(save);
  986. }
  987. php_stream_close(newfile);
  988. return FAILURE;
  989. }
  990. entry.filename = ".phar/signature.bin";
  991. entry.filename_len = sizeof(".phar/signature.bin")-1;
  992. entry.fp = php_stream_fopen_tmpfile();
  993. entry.fp_type = PHAR_MOD;
  994. entry.is_modified = 1;
  995. if (entry.fp == NULL) {
  996. spprintf(pass->error, 0, "phar error: unable to create temporary file for signature");
  997. return FAILURE;
  998. }
  999. PHAR_SET_32(sigbuf, phar->sig_flags);
  1000. PHAR_SET_32(sigbuf + 4, signature_length);
  1001. if (Z_UL(8) != php_stream_write(entry.fp, sigbuf, 8) || signature_length != php_stream_write(entry.fp, signature, signature_length)) {
  1002. efree(signature);
  1003. if (pass->error) {
  1004. spprintf(pass->error, 0, "phar error: unable to write signature to zip-based phar %s", phar->fname);
  1005. }
  1006. php_stream_close(newfile);
  1007. return FAILURE;
  1008. }
  1009. efree(signature);
  1010. entry.uncompressed_filesize = entry.compressed_filesize = signature_length + 8;
  1011. entry.phar = phar;
  1012. /* throw out return value and write the signature */
  1013. phar_zip_changed_apply_int(&entry, (void *)pass);
  1014. php_stream_close(newfile);
  1015. if (pass->error && *(pass->error)) {
  1016. /* error is set by writeheaders */
  1017. return FAILURE;
  1018. }
  1019. } /* signature */
  1020. return SUCCESS;
  1021. }
  1022. /* }}} */
  1023. int phar_zip_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error) /* {{{ */
  1024. {
  1025. char *pos;
  1026. smart_str main_metadata_str = {0};
  1027. static const char newstub[] = "<?php // zip-based phar archive stub file\n__HALT_COMPILER();";
  1028. char halt_stub[] = "__HALT_COMPILER();";
  1029. char *tmp;
  1030. php_stream *stubfile, *oldfile;
  1031. php_serialize_data_t metadata_hash;
  1032. int free_user_stub, closeoldfile = 0;
  1033. phar_entry_info entry = {0};
  1034. char *temperr = NULL;
  1035. struct _phar_zip_pass pass;
  1036. phar_zip_dir_end eocd;
  1037. uint32_t cdir_size, cdir_offset;
  1038. pass.error = &temperr;
  1039. entry.flags = PHAR_ENT_PERM_DEF_FILE;
  1040. entry.timestamp = time(NULL);
  1041. entry.is_modified = 1;
  1042. entry.is_zip = 1;
  1043. entry.phar = phar;
  1044. entry.fp_type = PHAR_MOD;
  1045. if (phar->is_persistent) {
  1046. if (error) {
  1047. spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname);
  1048. }
  1049. return EOF;
  1050. }
  1051. if (phar->is_data) {
  1052. goto nostub;
  1053. }
  1054. /* set alias */
  1055. if (!phar->is_temporary_alias && phar->alias_len) {
  1056. entry.fp = php_stream_fopen_tmpfile();
  1057. if (entry.fp == NULL) {
  1058. spprintf(error, 0, "phar error: unable to create temporary file");
  1059. return EOF;
  1060. }
  1061. if (phar->alias_len != php_stream_write(entry.fp, phar->alias, phar->alias_len)) {
  1062. if (error) {
  1063. spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname);
  1064. }
  1065. return EOF;
  1066. }
  1067. entry.uncompressed_filesize = entry.compressed_filesize = phar->alias_len;
  1068. entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1);
  1069. entry.filename_len = sizeof(".phar/alias.txt")-1;
  1070. zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  1071. } else {
  1072. zend_hash_str_del(&phar->manifest, ".phar/alias.txt", sizeof(".phar/alias.txt")-1);
  1073. }
  1074. /* register alias */
  1075. if (phar->alias_len) {
  1076. if (FAILURE == phar_get_archive(&phar, phar->fname, phar->fname_len, phar->alias, phar->alias_len, error)) {
  1077. return EOF;
  1078. }
  1079. }
  1080. /* set stub */
  1081. if (user_stub && !defaultstub) {
  1082. if (len < 0) {
  1083. /* resource passed in */
  1084. if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
  1085. if (error) {
  1086. spprintf(error, 0, "unable to access resource to copy stub to new zip-based phar \"%s\"", phar->fname);
  1087. }
  1088. return EOF;
  1089. }
  1090. if (len == -1) {
  1091. len = PHP_STREAM_COPY_ALL;
  1092. } else {
  1093. len = -len;
  1094. }
  1095. user_stub = 0;
  1096. // TODO: refactor to avoid reallocation ???
  1097. //??? len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)
  1098. {
  1099. zend_string *str = php_stream_copy_to_mem(stubfile, len, 0);
  1100. if (str) {
  1101. len = ZSTR_LEN(str);
  1102. user_stub = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
  1103. zend_string_release_ex(str, 0);
  1104. } else {
  1105. user_stub = NULL;
  1106. len = 0;
  1107. }
  1108. }
  1109. if (!len || !user_stub) {
  1110. if (error) {
  1111. spprintf(error, 0, "unable to read resource to copy stub to new zip-based phar \"%s\"", phar->fname);
  1112. }
  1113. return EOF;
  1114. }
  1115. free_user_stub = 1;
  1116. } else {
  1117. free_user_stub = 0;
  1118. }
  1119. tmp = estrndup(user_stub, len);
  1120. if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
  1121. efree(tmp);
  1122. if (error) {
  1123. spprintf(error, 0, "illegal stub for zip-based phar \"%s\"", phar->fname);
  1124. }
  1125. if (free_user_stub) {
  1126. efree(user_stub);
  1127. }
  1128. return EOF;
  1129. }
  1130. pos = user_stub + (pos - tmp);
  1131. efree(tmp);
  1132. len = pos - user_stub + 18;
  1133. entry.fp = php_stream_fopen_tmpfile();
  1134. if (entry.fp == NULL) {
  1135. spprintf(error, 0, "phar error: unable to create temporary file");
  1136. return EOF;
  1137. }
  1138. entry.uncompressed_filesize = len + 5;
  1139. if ((size_t)len != php_stream_write(entry.fp, user_stub, len)
  1140. || 5 != php_stream_write(entry.fp, " ?>\r\n", 5)) {
  1141. if (error) {
  1142. spprintf(error, 0, "unable to create stub from string in new zip-based phar \"%s\"", phar->fname);
  1143. }
  1144. if (free_user_stub) {
  1145. efree(user_stub);
  1146. }
  1147. php_stream_close(entry.fp);
  1148. return EOF;
  1149. }
  1150. entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
  1151. entry.filename_len = sizeof(".phar/stub.php")-1;
  1152. zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  1153. if (free_user_stub) {
  1154. efree(user_stub);
  1155. }
  1156. } else {
  1157. /* Either this is a brand new phar (add the stub), or the default stub is required (overwrite the stub) */
  1158. entry.fp = php_stream_fopen_tmpfile();
  1159. if (entry.fp == NULL) {
  1160. spprintf(error, 0, "phar error: unable to create temporary file");
  1161. return EOF;
  1162. }
  1163. if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
  1164. php_stream_close(entry.fp);
  1165. if (error) {
  1166. spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname);
  1167. }
  1168. return EOF;
  1169. }
  1170. entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
  1171. entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
  1172. entry.filename_len = sizeof(".phar/stub.php")-1;
  1173. if (!defaultstub) {
  1174. if (!zend_hash_str_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  1175. if (NULL == zend_hash_str_add_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) {
  1176. php_stream_close(entry.fp);
  1177. efree(entry.filename);
  1178. if (error) {
  1179. spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname);
  1180. }
  1181. return EOF;
  1182. }
  1183. } else {
  1184. php_stream_close(entry.fp);
  1185. efree(entry.filename);
  1186. }
  1187. } else {
  1188. zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
  1189. }
  1190. }
  1191. nostub:
  1192. if (phar->fp && !phar->is_brandnew) {
  1193. oldfile = phar->fp;
  1194. closeoldfile = 0;
  1195. php_stream_rewind(oldfile);
  1196. } else {
  1197. oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
  1198. closeoldfile = oldfile != NULL;
  1199. }
  1200. /* save modified files to the zip */
  1201. pass.old = oldfile;
  1202. pass.filefp = php_stream_fopen_tmpfile();
  1203. if (!pass.filefp) {
  1204. fperror:
  1205. if (closeoldfile) {
  1206. php_stream_close(oldfile);
  1207. }
  1208. if (error) {
  1209. spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to open temporary file", phar->fname);
  1210. }
  1211. return EOF;
  1212. }
  1213. pass.centralfp = php_stream_fopen_tmpfile();
  1214. if (!pass.centralfp) {
  1215. goto fperror;
  1216. }
  1217. pass.free_fp = pass.free_ufp = 1;
  1218. memset(&eocd, 0, sizeof(eocd));
  1219. memcpy(eocd.signature, "PK\5\6", 4);
  1220. if (!phar->is_data && !phar->sig_flags) {
  1221. phar->sig_flags = PHAR_SIG_SHA1;
  1222. }
  1223. if (phar->sig_flags) {
  1224. PHAR_SET_16(eocd.counthere, zend_hash_num_elements(&phar->manifest) + 1);
  1225. PHAR_SET_16(eocd.count, zend_hash_num_elements(&phar->manifest) + 1);
  1226. } else {
  1227. PHAR_SET_16(eocd.counthere, zend_hash_num_elements(&phar->manifest))

Large files files are truncated, but you can click here to view the full file