PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/phar/zip.c

http://github.com/infusion/PHP
C | 1527 lines | 1184 code | 206 blank | 137 comment | 351 complexity | 45fb05b50bac35a38828532526681b38 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | ZIP archive support for Phar |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2007-2011 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) ((php_uint16)((((php_uint16)var[0]) & 0xff) | \
  20. (((php_uint16)var[1]) & 0xff) << 8))
  21. #define PHAR_GET_32(var) ((php_uint32)((((php_uint32)var[0]) & 0xff) | \
  22. (((php_uint32)var[1]) & 0xff) << 8 | \
  23. (((php_uint32)var[2]) & 0xff) << 16 | \
  24. (((php_uint32)var[3]) & 0xff) << 24))
  25. static inline void phar_write_32(char buffer[4], php_uint32 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], php_uint32 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, (php_uint32) (value));
  38. # define PHAR_SET_16(var, value) phar_write_16(var, (php_uint16) (value));
  39. static int phar_zip_process_extra(php_stream *fp, phar_entry_info *entry, php_uint16 len TSRMLS_DC) /* {{{ */
  40. {
  41. union {
  42. phar_zip_extra_field_header header;
  43. phar_zip_unix3 unix3;
  44. } h;
  45. int 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 = sapi_get_request_time(TSRMLS_C);
  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. php_uint16 ctime, cdate;
  126. struct tm *tm, tmbuf;
  127. tm = php_localtime_r(&time, &tmbuf);
  128. cdate = ((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5) + tm->tm_mday;
  129. ctime = ((tm->tm_hour)<<11) + ((tm->tm_min)<<5) + ((tm->tm_sec)>>1);
  130. PHAR_SET_16(dtime, ctime);
  131. PHAR_SET_16(ddate, cdate);
  132. }
  133. /* }}} */
  134. /**
  135. * Does not check for a previously opened phar in the cache.
  136. *
  137. * Parse a new one and add it to the cache, returning either SUCCESS or
  138. * FAILURE, and setting pphar to the pointer to the manifest entry
  139. *
  140. * This is used by phar_open_from_fp to process a zip-based phar, but can be called
  141. * directly.
  142. */
  143. int phar_parse_zipfile(php_stream *fp, char *fname, int fname_len, char *alias, int alias_len, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  144. {
  145. phar_zip_dir_end locator;
  146. char buf[sizeof(locator) + 65536];
  147. long size;
  148. php_uint16 i;
  149. phar_archive_data *mydata = NULL;
  150. phar_entry_info entry = {0};
  151. char *p = buf, *ext, *actual_alias = NULL;
  152. char *metadata = NULL;
  153. size = php_stream_tell(fp);
  154. if (size > sizeof(locator) + 65536) {
  155. /* seek to max comment length + end of central directory record */
  156. size = sizeof(locator) + 65536;
  157. if (FAILURE == php_stream_seek(fp, -size, SEEK_END)) {
  158. php_stream_close(fp);
  159. if (error) {
  160. spprintf(error, 4096, "phar error: unable to search for end of central directory in zip-based phar \"%s\"", fname);
  161. }
  162. return FAILURE;
  163. }
  164. } else {
  165. php_stream_seek(fp, 0, SEEK_SET);
  166. }
  167. if (!php_stream_read(fp, buf, size)) {
  168. php_stream_close(fp);
  169. if (error) {
  170. spprintf(error, 4096, "phar error: unable to read in data to search for end of central directory in zip-based phar \"%s\"", fname);
  171. }
  172. return FAILURE;
  173. }
  174. while ((p=(char *) memchr(p + 1, 'P', (size_t) (size - (p + 1 - buf)))) != NULL) {
  175. if (!memcmp(p + 1, "K\5\6", 3)) {
  176. memcpy((void *)&locator, (void *) p, sizeof(locator));
  177. if (PHAR_GET_16(locator.centraldisk) != 0 || PHAR_GET_16(locator.disknumber) != 0) {
  178. /* split archives not handled */
  179. php_stream_close(fp);
  180. if (error) {
  181. spprintf(error, 4096, "phar error: split archives spanning multiple zips cannot be processed in zip-based phar \"%s\"", fname);
  182. }
  183. return FAILURE;
  184. }
  185. if (PHAR_GET_16(locator.counthere) != PHAR_GET_16(locator.count)) {
  186. if (error) {
  187. spprintf(error, 4096, "phar error: corrupt zip archive, conflicting file count in end of central directory record in zip-based phar \"%s\"", fname);
  188. }
  189. php_stream_close(fp);
  190. return FAILURE;
  191. }
  192. mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
  193. mydata->is_persistent = PHAR_G(persist);
  194. /* read in archive comment, if any */
  195. if (PHAR_GET_16(locator.comment_len)) {
  196. metadata = p + sizeof(locator);
  197. if (PHAR_GET_16(locator.comment_len) != size - (metadata - buf)) {
  198. if (error) {
  199. spprintf(error, 4096, "phar error: corrupt zip archive, zip file comment truncated in zip-based phar \"%s\"", fname);
  200. }
  201. php_stream_close(fp);
  202. pefree(mydata, mydata->is_persistent);
  203. return FAILURE;
  204. }
  205. mydata->metadata_len = PHAR_GET_16(locator.comment_len);
  206. if (phar_parse_metadata(&metadata, &mydata->metadata, PHAR_GET_16(locator.comment_len) TSRMLS_CC) == FAILURE) {
  207. mydata->metadata_len = 0;
  208. /* if not valid serialized data, it is a regular string */
  209. if (entry.is_persistent) {
  210. ALLOC_PERMANENT_ZVAL(mydata->metadata);
  211. } else {
  212. ALLOC_ZVAL(mydata->metadata);
  213. }
  214. INIT_ZVAL(*mydata->metadata);
  215. metadata = pestrndup(metadata, PHAR_GET_16(locator.comment_len), mydata->is_persistent);
  216. ZVAL_STRINGL(mydata->metadata, metadata, PHAR_GET_16(locator.comment_len), 0);
  217. }
  218. } else {
  219. mydata->metadata = NULL;
  220. }
  221. goto foundit;
  222. }
  223. }
  224. php_stream_close(fp);
  225. if (error) {
  226. spprintf(error, 4096, "phar error: end of central directory not found in zip-based phar \"%s\"", fname);
  227. }
  228. return FAILURE;
  229. foundit:
  230. mydata->fname = pestrndup(fname, fname_len, mydata->is_persistent);
  231. #ifdef PHP_WIN32
  232. phar_unixify_path_separators(mydata->fname, fname_len);
  233. #endif
  234. mydata->is_zip = 1;
  235. mydata->fname_len = fname_len;
  236. ext = strrchr(mydata->fname, '/');
  237. if (ext) {
  238. mydata->ext = memchr(ext, '.', (mydata->fname + fname_len) - ext);
  239. if (mydata->ext == ext) {
  240. mydata->ext = memchr(ext + 1, '.', (mydata->fname + fname_len) - ext - 1);
  241. }
  242. if (mydata->ext) {
  243. mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
  244. }
  245. }
  246. /* clean up on big-endian systems */
  247. /* seek to central directory */
  248. php_stream_seek(fp, PHAR_GET_32(locator.cdir_offset), SEEK_SET);
  249. /* read in central directory */
  250. zend_hash_init(&mydata->manifest, PHAR_GET_16(locator.count),
  251. zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)mydata->is_persistent);
  252. zend_hash_init(&mydata->mounted_dirs, 5,
  253. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  254. zend_hash_init(&mydata->virtual_dirs, PHAR_GET_16(locator.count) * 2,
  255. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  256. entry.phar = mydata;
  257. entry.is_zip = 1;
  258. entry.fp_type = PHAR_FP;
  259. entry.is_persistent = mydata->is_persistent;
  260. #define PHAR_ZIP_FAIL_FREE(errmsg, save) \
  261. zend_hash_destroy(&mydata->manifest); \
  262. mydata->manifest.arBuckets = 0; \
  263. zend_hash_destroy(&mydata->mounted_dirs); \
  264. mydata->mounted_dirs.arBuckets = 0; \
  265. zend_hash_destroy(&mydata->virtual_dirs); \
  266. mydata->virtual_dirs.arBuckets = 0; \
  267. php_stream_close(fp); \
  268. if (mydata->metadata) { \
  269. zval_dtor(mydata->metadata); \
  270. } \
  271. if (mydata->signature) { \
  272. efree(mydata->signature); \
  273. } \
  274. if (error) { \
  275. spprintf(error, 4096, "phar error: %s in zip-based phar \"%s\"", errmsg, mydata->fname); \
  276. } \
  277. pefree(mydata->fname, mydata->is_persistent); \
  278. if (mydata->alias) { \
  279. pefree(mydata->alias, mydata->is_persistent); \
  280. } \
  281. pefree(mydata, mydata->is_persistent); \
  282. efree(save); \
  283. return FAILURE;
  284. #define PHAR_ZIP_FAIL(errmsg) \
  285. zend_hash_destroy(&mydata->manifest); \
  286. mydata->manifest.arBuckets = 0; \
  287. zend_hash_destroy(&mydata->mounted_dirs); \
  288. mydata->mounted_dirs.arBuckets = 0; \
  289. zend_hash_destroy(&mydata->virtual_dirs); \
  290. mydata->virtual_dirs.arBuckets = 0; \
  291. php_stream_close(fp); \
  292. if (mydata->metadata) { \
  293. zval_dtor(mydata->metadata); \
  294. } \
  295. if (mydata->signature) { \
  296. efree(mydata->signature); \
  297. } \
  298. if (error) { \
  299. spprintf(error, 4096, "phar error: %s in zip-based phar \"%s\"", errmsg, mydata->fname); \
  300. } \
  301. pefree(mydata->fname, mydata->is_persistent); \
  302. if (mydata->alias) { \
  303. pefree(mydata->alias, mydata->is_persistent); \
  304. } \
  305. pefree(mydata, mydata->is_persistent); \
  306. return FAILURE;
  307. /* add each central directory item to the manifest */
  308. for (i = 0; i < PHAR_GET_16(locator.count); ++i) {
  309. phar_zip_central_dir_file zipentry;
  310. off_t beforeus = php_stream_tell(fp);
  311. if (sizeof(zipentry) != php_stream_read(fp, (char *) &zipentry, sizeof(zipentry))) {
  312. PHAR_ZIP_FAIL("unable to read central directory entry, truncated");
  313. }
  314. /* clean up for bigendian systems */
  315. if (memcmp("PK\1\2", zipentry.signature, 4)) {
  316. /* corrupted entry */
  317. PHAR_ZIP_FAIL("corrupted central directory entry, no magic signature");
  318. }
  319. if (entry.is_persistent) {
  320. entry.manifest_pos = i;
  321. }
  322. entry.compressed_filesize = PHAR_GET_32(zipentry.compsize);
  323. entry.uncompressed_filesize = PHAR_GET_32(zipentry.uncompsize);
  324. entry.crc32 = PHAR_GET_32(zipentry.crc32);
  325. /* do not PHAR_GET_16 either on the next line */
  326. entry.timestamp = phar_zip_d2u_time(zipentry.timestamp, zipentry.datestamp);
  327. entry.flags = PHAR_ENT_PERM_DEF_FILE;
  328. entry.header_offset = PHAR_GET_32(zipentry.offset);
  329. entry.offset = entry.offset_abs = PHAR_GET_32(zipentry.offset) + sizeof(phar_zip_file_header) + PHAR_GET_16(zipentry.filename_len) +
  330. PHAR_GET_16(zipentry.extra_len);
  331. if (PHAR_GET_16(zipentry.flags) & PHAR_ZIP_FLAG_ENCRYPTED) {
  332. PHAR_ZIP_FAIL("Cannot process encrypted zip files");
  333. }
  334. if (!PHAR_GET_16(zipentry.filename_len)) {
  335. PHAR_ZIP_FAIL("Cannot process zips created from stdin (zero-length filename)");
  336. }
  337. entry.filename_len = PHAR_GET_16(zipentry.filename_len);
  338. entry.filename = (char *) pemalloc(entry.filename_len + 1, entry.is_persistent);
  339. if (entry.filename_len != php_stream_read(fp, entry.filename, entry.filename_len)) {
  340. pefree(entry.filename, entry.is_persistent);
  341. PHAR_ZIP_FAIL("unable to read in filename from central directory, truncated");
  342. }
  343. entry.filename[entry.filename_len] = '\0';
  344. if (entry.filename[entry.filename_len - 1] == '/') {
  345. entry.is_dir = 1;
  346. entry.filename_len--;
  347. entry.flags |= PHAR_ENT_PERM_DEF_DIR;
  348. } else {
  349. entry.is_dir = 0;
  350. }
  351. if (entry.filename_len == sizeof(".phar/signature.bin")-1 && !strncmp(entry.filename, ".phar/signature.bin", sizeof(".phar/signature.bin")-1)) {
  352. size_t read;
  353. php_stream *sigfile;
  354. off_t now;
  355. char *sig;
  356. now = php_stream_tell(fp);
  357. pefree(entry.filename, entry.is_persistent);
  358. sigfile = php_stream_fopen_tmpfile();
  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. phar_stream_copy_to_stream(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. phar_stream_copy_to_stream(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) {
  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, &mydata->sig_len, error TSRMLS_CC)) {
  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. php_stream_close(sigfile);
  392. efree(sig);
  393. /* signature checked out, let's ensure this is the last file in the phar */
  394. if (i != PHAR_GET_16(locator.count) - 1) {
  395. PHAR_ZIP_FAIL("entries exist after signature, invalid phar");
  396. }
  397. continue;
  398. }
  399. phar_add_virtual_dirs(mydata, entry.filename, entry.filename_len TSRMLS_CC);
  400. if (PHAR_GET_16(zipentry.extra_len)) {
  401. off_t loc = php_stream_tell(fp);
  402. if (FAILURE == phar_zip_process_extra(fp, &entry, PHAR_GET_16(zipentry.extra_len) TSRMLS_CC)) {
  403. pefree(entry.filename, entry.is_persistent);
  404. PHAR_ZIP_FAIL("Unable to process extra field header for file in central directory");
  405. }
  406. php_stream_seek(fp, loc + PHAR_GET_16(zipentry.extra_len), SEEK_SET);
  407. }
  408. switch (PHAR_GET_16(zipentry.compressed)) {
  409. case PHAR_ZIP_COMP_NONE :
  410. /* compression flag already set */
  411. break;
  412. case PHAR_ZIP_COMP_DEFLATE :
  413. entry.flags |= PHAR_ENT_COMPRESSED_GZ;
  414. if (!PHAR_G(has_zlib)) {
  415. pefree(entry.filename, entry.is_persistent);
  416. PHAR_ZIP_FAIL("zlib extension is required");
  417. }
  418. break;
  419. case PHAR_ZIP_COMP_BZIP2 :
  420. entry.flags |= PHAR_ENT_COMPRESSED_BZ2;
  421. if (!PHAR_G(has_bz2)) {
  422. pefree(entry.filename, entry.is_persistent);
  423. PHAR_ZIP_FAIL("bzip2 extension is required");
  424. }
  425. break;
  426. case 1 :
  427. pefree(entry.filename, entry.is_persistent);
  428. PHAR_ZIP_FAIL("unsupported compression method (Shrunk) used in this zip");
  429. case 2 :
  430. case 3 :
  431. case 4 :
  432. case 5 :
  433. pefree(entry.filename, entry.is_persistent);
  434. PHAR_ZIP_FAIL("unsupported compression method (Reduce) used in this zip");
  435. case 6 :
  436. pefree(entry.filename, entry.is_persistent);
  437. PHAR_ZIP_FAIL("unsupported compression method (Implode) used in this zip");
  438. case 7 :
  439. pefree(entry.filename, entry.is_persistent);
  440. PHAR_ZIP_FAIL("unsupported compression method (Tokenize) used in this zip");
  441. case 9 :
  442. pefree(entry.filename, entry.is_persistent);
  443. PHAR_ZIP_FAIL("unsupported compression method (Deflate64) used in this zip");
  444. case 10 :
  445. pefree(entry.filename, entry.is_persistent);
  446. PHAR_ZIP_FAIL("unsupported compression method (PKWare Implode/old IBM TERSE) used in this zip");
  447. case 14 :
  448. pefree(entry.filename, entry.is_persistent);
  449. PHAR_ZIP_FAIL("unsupported compression method (LZMA) used in this zip");
  450. case 18 :
  451. pefree(entry.filename, entry.is_persistent);
  452. PHAR_ZIP_FAIL("unsupported compression method (IBM TERSE) used in this zip");
  453. case 19 :
  454. pefree(entry.filename, entry.is_persistent);
  455. PHAR_ZIP_FAIL("unsupported compression method (IBM LZ77) used in this zip");
  456. case 97 :
  457. pefree(entry.filename, entry.is_persistent);
  458. PHAR_ZIP_FAIL("unsupported compression method (WavPack) used in this zip");
  459. case 98 :
  460. pefree(entry.filename, entry.is_persistent);
  461. PHAR_ZIP_FAIL("unsupported compression method (PPMd) used in this zip");
  462. default :
  463. pefree(entry.filename, entry.is_persistent);
  464. PHAR_ZIP_FAIL("unsupported compression method (unknown) used in this zip");
  465. }
  466. /* get file metadata */
  467. if (PHAR_GET_16(zipentry.comment_len)) {
  468. if (PHAR_GET_16(zipentry.comment_len) != php_stream_read(fp, buf, PHAR_GET_16(zipentry.comment_len))) {
  469. pefree(entry.filename, entry.is_persistent);
  470. PHAR_ZIP_FAIL("unable to read in file comment, truncated");
  471. }
  472. p = buf;
  473. entry.metadata_len = PHAR_GET_16(zipentry.comment_len);
  474. if (phar_parse_metadata(&p, &(entry.metadata), PHAR_GET_16(zipentry.comment_len) TSRMLS_CC) == FAILURE) {
  475. entry.metadata_len = 0;
  476. /* if not valid serialized data, it is a regular string */
  477. if (entry.is_persistent) {
  478. ALLOC_PERMANENT_ZVAL(entry.metadata);
  479. } else {
  480. ALLOC_ZVAL(entry.metadata);
  481. }
  482. INIT_ZVAL(*entry.metadata);
  483. ZVAL_STRINGL(entry.metadata, pestrndup(buf, PHAR_GET_16(zipentry.comment_len), entry.is_persistent), PHAR_GET_16(zipentry.comment_len), 0);
  484. }
  485. } else {
  486. entry.metadata = NULL;
  487. }
  488. if (!actual_alias && entry.filename_len == sizeof(".phar/alias.txt")-1 && !strncmp(entry.filename, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
  489. php_stream_filter *filter;
  490. off_t saveloc;
  491. /* verify local file header */
  492. phar_zip_file_header local;
  493. /* archive alias found */
  494. saveloc = php_stream_tell(fp);
  495. php_stream_seek(fp, PHAR_GET_32(zipentry.offset), SEEK_SET);
  496. if (sizeof(local) != php_stream_read(fp, (char *) &local, sizeof(local))) {
  497. pefree(entry.filename, entry.is_persistent);
  498. PHAR_ZIP_FAIL("phar error: internal corruption of zip-based phar (cannot read local file header for alias)");
  499. }
  500. /* verify local header */
  501. 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)) {
  502. pefree(entry.filename, entry.is_persistent);
  503. PHAR_ZIP_FAIL("phar error: internal corruption of zip-based phar (local header of alias does not match central directory)");
  504. }
  505. /* construct actual offset to file start - local extra_len can be different from central extra_len */
  506. entry.offset = entry.offset_abs =
  507. sizeof(local) + entry.header_offset + PHAR_GET_16(local.filename_len) + PHAR_GET_16(local.extra_len);
  508. #if PHP_VERSION_ID < 50207
  509. /* work around Bug #46147 */
  510. fp->writepos = fp->readpos = 0;
  511. #endif
  512. php_stream_seek(fp, entry.offset, SEEK_SET);
  513. /* these next lines should be for php < 5.2.6 after 5.3 filters are fixed */
  514. fp->writepos = 0;
  515. fp->readpos = 0;
  516. php_stream_seek(fp, entry.offset, SEEK_SET);
  517. fp->writepos = 0;
  518. fp->readpos = 0;
  519. /* the above lines should be for php < 5.2.6 after 5.3 filters are fixed */
  520. mydata->alias_len = entry.uncompressed_filesize;
  521. if (entry.flags & PHAR_ENT_COMPRESSED_GZ) {
  522. filter = php_stream_filter_create("zlib.inflate", NULL, php_stream_is_persistent(fp) TSRMLS_CC);
  523. if (!filter) {
  524. pefree(entry.filename, entry.is_persistent);
  525. PHAR_ZIP_FAIL("unable to decompress alias, zlib filter creation failed");
  526. }
  527. php_stream_filter_append(&fp->readfilters, filter);
  528. #if PHP_MAJOR_VERSION >= 6
  529. if (!(entry.uncompressed_filesize = php_stream_copy_to_mem(fp, (void **) &actual_alias, entry.uncompressed_filesize, 0)) || !actual_alias) {
  530. #else
  531. if (!(entry.uncompressed_filesize = php_stream_copy_to_mem(fp, &actual_alias, entry.uncompressed_filesize, 0)) || !actual_alias) {
  532. #endif
  533. pefree(entry.filename, entry.is_persistent);
  534. #if PHP_VERSION_ID < 50207
  535. PHAR_ZIP_FAIL("unable to read in alias, truncated (PHP 5.2.7 and newer has a potential fix for this problem)");
  536. #endif
  537. PHAR_ZIP_FAIL("unable to read in alias, truncated");
  538. }
  539. php_stream_filter_flush(filter, 1);
  540. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  541. } else if (entry.flags & PHAR_ENT_COMPRESSED_BZ2) {
  542. filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp) TSRMLS_CC);
  543. if (!filter) {
  544. pefree(entry.filename, entry.is_persistent);
  545. PHAR_ZIP_FAIL("unable to read in alias, bzip2 filter creation failed");
  546. }
  547. php_stream_filter_append(&fp->readfilters, filter);
  548. #if PHP_MAJOR_VERSION >= 6
  549. if (!(entry.uncompressed_filesize = php_stream_copy_to_mem(fp, (void **) &actual_alias, entry.uncompressed_filesize, 0)) || !actual_alias) {
  550. #else
  551. if (!(entry.uncompressed_filesize = php_stream_copy_to_mem(fp, &actual_alias, entry.uncompressed_filesize, 0)) || !actual_alias) {
  552. #endif
  553. pefree(entry.filename, entry.is_persistent);
  554. #if PHP_VERSION_ID < 50207
  555. PHAR_ZIP_FAIL("unable to read in alias, truncated (PHP 5.2.7 and newer has a potential fix for this problem)");
  556. #endif
  557. PHAR_ZIP_FAIL("unable to read in alias, truncated");
  558. }
  559. php_stream_filter_flush(filter, 1);
  560. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  561. } else {
  562. #if PHP_MAJOR_VERSION >= 6
  563. if (!(entry.uncompressed_filesize = php_stream_copy_to_mem(fp, (void **) &actual_alias, entry.uncompressed_filesize, 0)) || !actual_alias) {
  564. #else
  565. if (!(entry.uncompressed_filesize = php_stream_copy_to_mem(fp, &actual_alias, entry.uncompressed_filesize, 0)) || !actual_alias) {
  566. #endif
  567. pefree(entry.filename, entry.is_persistent);
  568. PHAR_ZIP_FAIL("unable to read in alias, truncated");
  569. }
  570. }
  571. /* return to central directory parsing */
  572. php_stream_seek(fp, saveloc, SEEK_SET);
  573. }
  574. phar_set_inode(&entry TSRMLS_CC);
  575. zend_hash_add(&mydata->manifest, entry.filename, entry.filename_len, (void *)&entry,sizeof(phar_entry_info), NULL);
  576. }
  577. mydata->fp = fp;
  578. if (zend_hash_exists(&(mydata->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  579. mydata->is_data = 0;
  580. } else {
  581. mydata->is_data = 1;
  582. }
  583. zend_hash_add(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  584. if (actual_alias) {
  585. phar_archive_data **fd_ptr;
  586. if (!phar_validate_alias(actual_alias, mydata->alias_len)) {
  587. if (error) {
  588. spprintf(error, 4096, "phar error: invalid alias \"%s\" in zip-based phar \"%s\"", actual_alias, fname);
  589. }
  590. efree(actual_alias);
  591. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  592. return FAILURE;
  593. }
  594. mydata->is_temporary_alias = 0;
  595. if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), actual_alias, mydata->alias_len, (void **)&fd_ptr)) {
  596. if (SUCCESS != phar_free_alias(*fd_ptr, actual_alias, mydata->alias_len TSRMLS_CC)) {
  597. if (error) {
  598. spprintf(error, 4096, "phar error: Unable to add zip-based phar \"%s\" with implicit alias, alias is already in use", fname);
  599. }
  600. efree(actual_alias);
  601. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  602. return FAILURE;
  603. }
  604. }
  605. mydata->alias = entry.is_persistent ? pestrndup(actual_alias, mydata->alias_len, 1) : actual_alias;
  606. if (entry.is_persistent) {
  607. efree(actual_alias);
  608. }
  609. zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), actual_alias, mydata->alias_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  610. } else {
  611. phar_archive_data **fd_ptr;
  612. if (alias_len) {
  613. if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void **)&fd_ptr)) {
  614. if (SUCCESS != phar_free_alias(*fd_ptr, alias, alias_len TSRMLS_CC)) {
  615. if (error) {
  616. spprintf(error, 4096, "phar error: Unable to add zip-based phar \"%s\" with explicit alias, alias is already in use", fname);
  617. }
  618. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  619. return FAILURE;
  620. }
  621. }
  622. zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), actual_alias, mydata->alias_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  623. mydata->alias = pestrndup(alias, alias_len, mydata->is_persistent);
  624. mydata->alias_len = alias_len;
  625. } else {
  626. mydata->alias = pestrndup(mydata->fname, fname_len, mydata->is_persistent);
  627. mydata->alias_len = fname_len;
  628. }
  629. mydata->is_temporary_alias = 1;
  630. }
  631. if (pphar) {
  632. *pphar = mydata;
  633. }
  634. return SUCCESS;
  635. }
  636. /* }}} */
  637. /**
  638. * Create or open a zip-based phar for writing
  639. */
  640. int phar_open_or_create_zip(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  641. {
  642. phar_archive_data *phar;
  643. int ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error TSRMLS_CC);
  644. if (FAILURE == ret) {
  645. return FAILURE;
  646. }
  647. if (pphar) {
  648. *pphar = phar;
  649. }
  650. phar->is_data = is_data;
  651. if (phar->is_zip) {
  652. return ret;
  653. }
  654. if (phar->is_brandnew) {
  655. phar->internal_file_start = 0;
  656. phar->is_zip = 1;
  657. phar->is_tar = 0;
  658. return SUCCESS;
  659. }
  660. /* we've reached here - the phar exists and is a regular phar */
  661. if (error) {
  662. 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);
  663. }
  664. return FAILURE;
  665. }
  666. /* }}} */
  667. struct _phar_zip_pass {
  668. php_stream *filefp;
  669. php_stream *centralfp;
  670. php_stream *old;
  671. int free_fp;
  672. int free_ufp;
  673. char **error;
  674. };
  675. /* perform final modification of zip contents for each file in the manifest before saving */
  676. static int phar_zip_changed_apply(void *data, void *arg TSRMLS_DC) /* {{{ */
  677. {
  678. phar_entry_info *entry;
  679. phar_zip_file_header local;
  680. phar_zip_unix3 perms;
  681. phar_zip_central_dir_file central;
  682. struct _phar_zip_pass *p;
  683. php_uint32 newcrc32;
  684. off_t offset;
  685. int not_really_modified = 0;
  686. entry = (phar_entry_info *)data;
  687. p = (struct _phar_zip_pass*) arg;
  688. if (entry->is_mounted) {
  689. return ZEND_HASH_APPLY_KEEP;
  690. }
  691. if (entry->is_deleted) {
  692. if (entry->fp_refcount <= 0) {
  693. return ZEND_HASH_APPLY_REMOVE;
  694. } else {
  695. /* we can't delete this in-memory until it is closed */
  696. return ZEND_HASH_APPLY_KEEP;
  697. }
  698. }
  699. phar_add_virtual_dirs(entry->phar, entry->filename, entry->filename_len TSRMLS_CC);
  700. memset(&local, 0, sizeof(local));
  701. memset(&central, 0, sizeof(central));
  702. memset(&perms, 0, sizeof(perms));
  703. strncpy(local.signature, "PK\3\4", 4);
  704. strncpy(central.signature, "PK\1\2", 4);
  705. PHAR_SET_16(central.extra_len, sizeof(perms));
  706. PHAR_SET_16(local.extra_len, sizeof(perms));
  707. perms.tag[0] = 'n';
  708. perms.tag[1] = 'u';
  709. PHAR_SET_16(perms.size, sizeof(perms) - 4);
  710. PHAR_SET_16(perms.perms, entry->flags & PHAR_ENT_PERM_MASK);
  711. {
  712. php_uint32 crc = (php_uint32) ~0;
  713. CRC32(crc, perms.perms[0]);
  714. CRC32(crc, perms.perms[1]);
  715. PHAR_SET_32(perms.crc32, ~crc);
  716. }
  717. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  718. PHAR_SET_16(central.compressed, PHAR_ZIP_COMP_DEFLATE);
  719. PHAR_SET_16(local.compressed, PHAR_ZIP_COMP_DEFLATE);
  720. }
  721. if (entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
  722. PHAR_SET_16(central.compressed, PHAR_ZIP_COMP_BZIP2);
  723. PHAR_SET_16(local.compressed, PHAR_ZIP_COMP_BZIP2);
  724. }
  725. /* do not use PHAR_GET_16 on either field of the next line */
  726. phar_zip_u2d_time(entry->timestamp, local.timestamp, local.datestamp);
  727. memcpy(central.timestamp, local.timestamp, sizeof(local.timestamp));
  728. memcpy(central.datestamp, local.datestamp, sizeof(local.datestamp));
  729. PHAR_SET_16(central.filename_len, entry->filename_len + (entry->is_dir ? 1 : 0));
  730. PHAR_SET_16(local.filename_len, entry->filename_len + (entry->is_dir ? 1 : 0));
  731. PHAR_SET_32(central.offset, php_stream_tell(p->filefp));
  732. /* do extra field for perms later */
  733. if (entry->is_modified) {
  734. php_uint32 loc;
  735. php_stream_filter *filter;
  736. php_stream *efp;
  737. if (entry->is_dir) {
  738. entry->is_modified = 0;
  739. if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp) {
  740. php_stream_close(entry->fp);
  741. entry->fp = NULL;
  742. entry->fp_type = PHAR_FP;
  743. }
  744. goto continue_dir;
  745. }
  746. if (FAILURE == phar_open_entry_fp(entry, p->error, 0 TSRMLS_CC)) {
  747. spprintf(p->error, 0, "unable to open file contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  748. return ZEND_HASH_APPLY_STOP;
  749. }
  750. /* we can be modified and already be compressed, such as when chmod() is executed */
  751. if (entry->flags & PHAR_ENT_COMPRESSION_MASK && (entry->old_flags == entry->flags || !entry->old_flags)) {
  752. not_really_modified = 1;
  753. goto is_compressed;
  754. }
  755. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC)) {
  756. spprintf(p->error, 0, "unable to seek to start of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  757. return ZEND_HASH_APPLY_STOP;
  758. }
  759. efp = phar_get_efp(entry, 0 TSRMLS_CC);
  760. newcrc32 = ~0;
  761. for (loc = 0;loc < entry->uncompressed_filesize; ++loc) {
  762. CRC32(newcrc32, php_stream_getc(efp));
  763. }
  764. entry->crc32 = ~newcrc32;
  765. PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize);
  766. PHAR_SET_32(local.uncompsize, entry->uncompressed_filesize);
  767. if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
  768. /* not compressed */
  769. entry->compressed_filesize = entry->uncompressed_filesize;
  770. PHAR_SET_32(central.compsize, entry->uncompressed_filesize);
  771. PHAR_SET_32(local.compsize, entry->uncompressed_filesize);
  772. goto not_compressed;
  773. }
  774. filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0 TSRMLS_CC);
  775. if (!filter) {
  776. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  777. spprintf(p->error, 0, "unable to gzip compress file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  778. } else {
  779. spprintf(p->error, 0, "unable to bzip2 compress file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  780. }
  781. return ZEND_HASH_APPLY_STOP;
  782. }
  783. /* create new file that holds the compressed version */
  784. /* work around inability to specify freedom in write and strictness
  785. in read count */
  786. entry->cfp = php_stream_fopen_tmpfile();
  787. if (!entry->cfp) {
  788. spprintf(p->error, 0, "unable to create temporary file for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  789. return ZEND_HASH_APPLY_STOP;
  790. }
  791. php_stream_flush(efp);
  792. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC)) {
  793. spprintf(p->error, 0, "unable to seek to start of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  794. return ZEND_HASH_APPLY_STOP;
  795. }
  796. php_stream_filter_append((&entry->cfp->writefilters), filter);
  797. if (SUCCESS != phar_stream_copy_to_stream(efp, entry->cfp, entry->uncompressed_filesize, NULL)) {
  798. spprintf(p->error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, entry->phar->fname);
  799. return ZEND_HASH_APPLY_STOP;
  800. }
  801. php_stream_filter_flush(filter, 1);
  802. php_stream_flush(entry->cfp);
  803. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  804. php_stream_seek(entry->cfp, 0, SEEK_END);
  805. entry->compressed_filesize = (php_uint32) php_stream_tell(entry->cfp);
  806. PHAR_SET_32(central.compsize, entry->compressed_filesize);
  807. PHAR_SET_32(local.compsize, entry->compressed_filesize);
  808. /* generate crc on compressed file */
  809. php_stream_rewind(entry->cfp);
  810. entry->old_flags = entry->flags;
  811. entry->is_modified = 1;
  812. } else {
  813. is_compressed:
  814. PHAR_SET_32(central.uncompsize, entry->uncompressed_filesize);
  815. PHAR_SET_32(local.uncompsize, entry->uncompressed_filesize);
  816. PHAR_SET_32(central.compsize, entry->compressed_filesize);
  817. PHAR_SET_32(local.compsize, entry->compressed_filesize);
  818. if (-1 == php_stream_seek(p->old, entry->offset_abs, SEEK_SET)) {
  819. spprintf(p->error, 0, "unable to seek to start of file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  820. return ZEND_HASH_APPLY_STOP;
  821. }
  822. }
  823. not_compressed:
  824. PHAR_SET_32(central.crc32, entry->crc32);
  825. PHAR_SET_32(local.crc32, entry->crc32);
  826. continue_dir:
  827. /* set file metadata */
  828. if (entry->metadata) {
  829. php_serialize_data_t metadata_hash;
  830. if (entry->metadata_str.c) {
  831. smart_str_free(&entry->metadata_str);
  832. }
  833. entry->metadata_str.c = 0;
  834. entry->metadata_str.len = 0;
  835. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  836. php_var_serialize(&entry->metadata_str, &entry->metadata, &metadata_hash TSRMLS_CC);
  837. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  838. PHAR_SET_16(central.comment_len, entry->metadata_str.len);
  839. }
  840. entry->header_offset = php_stream_tell(p->filefp);
  841. offset = entry->header_offset + sizeof(local) + entry->filename_len + (entry->is_dir ? 1 : 0) + sizeof(perms);
  842. if (sizeof(local) != php_stream_write(p->filefp, (char *)&local, sizeof(local))) {
  843. spprintf(p->error, 0, "unable to write local file header of file \"%s\" to zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  844. return ZEND_HASH_APPLY_STOP;
  845. }
  846. if (sizeof(central) != php_stream_write(p->centralfp, (char *)&central, sizeof(central))) {
  847. spprintf(p->error, 0, "unable to write central directory entry for file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  848. return ZEND_HASH_APPLY_STOP;
  849. }
  850. if (entry->is_dir) {
  851. if (entry->filename_len != php_stream_write(p->filefp, entry->filename, entry->filename_len)) {
  852. 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);
  853. return ZEND_HASH_APPLY_STOP;
  854. }
  855. if (1 != php_stream_write(p->filefp, "/", 1)) {
  856. 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);
  857. return ZEND_HASH_APPLY_STOP;
  858. }
  859. if (entry->filename_len != php_stream_write(p->centralfp, entry->filename, entry->filename_len)) {
  860. 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);
  861. return ZEND_HASH_APPLY_STOP;
  862. }
  863. if (1 != php_stream_write(p->centralfp, "/", 1)) {
  864. 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);
  865. return ZEND_HASH_APPLY_STOP;
  866. }
  867. } else {
  868. if (entry->filename_len != php_stream_write(p->filefp, entry->filename, entry->filename_len)) {
  869. 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);
  870. return ZEND_HASH_APPLY_STOP;
  871. }
  872. if (entry->filename_len != php_stream_write(p->centralfp, entry->filename, entry->filename_len)) {
  873. 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);
  874. return ZEND_HASH_APPLY_STOP;
  875. }
  876. }
  877. if (sizeof(perms) != php_stream_write(p->filefp, (char *)&perms, sizeof(perms))) {
  878. 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);
  879. return ZEND_HASH_APPLY_STOP;
  880. }
  881. if (sizeof(perms) != php_stream_write(p->centralfp, (char *)&perms, sizeof(perms))) {
  882. 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);
  883. return ZEND_HASH_APPLY_STOP;
  884. }
  885. if (!not_really_modified && entry->is_modified) {
  886. if (entry->cfp) {
  887. if (SUCCESS != phar_stream_copy_to_stream(entry->cfp, p->filefp, entry->compressed_filesize, NULL)) {
  888. spprintf(p->error, 0, "unable to write compressed contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  889. return ZEND_HASH_APPLY_STOP;
  890. }
  891. php_stream_close(entry->cfp);
  892. entry->cfp = NULL;
  893. } else {
  894. if (FAILURE == phar_open_entry_fp(entry, p->error, 0 TSRMLS_CC)) {
  895. return ZEND_HASH_APPLY_STOP;
  896. }
  897. phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC);
  898. if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), p->filefp, entry->uncompressed_filesize, NULL)) {
  899. spprintf(p->error, 0, "unable to write contents of file \"%s\" in zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  900. return ZEND_HASH_APPLY_STOP;
  901. }
  902. }
  903. if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp && entry->fp_refcount == 0) {
  904. php_stream_close(entry->fp);
  905. }
  906. entry->is_modified = 0;
  907. } else {
  908. entry->is_modified = 0;
  909. if (entry->fp_refcount) {
  910. /* open file pointers refer to this fp, do not free the stream */
  911. switch (entry->fp_type) {
  912. case PHAR_FP:
  913. p->free_fp = 0;
  914. break;
  915. case PHAR_UFP:
  916. p->free_ufp = 0;
  917. default:
  918. break;
  919. }
  920. }
  921. if (!entry->is_dir && entry->compressed_filesize && SUCCESS != phar_stream_copy_to_stream(p->old, p->filefp, entry->compressed_filesize, NULL)) {
  922. spprintf(p->error, 0, "unable to copy contents of file \"%s\" while creating zip-based phar \"%s\"", entry->filename, entry->phar->fname);
  923. return ZEND_HASH_APPLY_STOP;
  924. }
  925. }
  926. entry->fp = NULL;
  927. entry->offset = entry->offset_abs = offset;
  928. entry->fp_type = PHAR_FP;
  929. if (entry->metadata_str.c) {
  930. if (entry->metadata_str.len != php_stream_write(p->centralfp, entry->metadata_str.c, entry->metadata_str.len)) {
  931. 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);
  932. smart_str_free(&entry->metadata_str);
  933. return ZEND_HASH_APPLY_STOP;
  934. }
  935. smart_str_free(&entry->metadata_str);
  936. }
  937. return ZEND_HASH_APPLY_KEEP;
  938. }
  939. /* }}} */
  940. static int phar_zip_applysignature(phar_archive_data *phar, struct _phar_zip_pass *pass,
  941. smart_str *metadata TSRMLS_DC) /* {{{ */
  942. {
  943. /* add signature for executable tars or tars explicitly set with setSignatureAlgorithm */
  944. if (!phar->is_data || phar->sig_flags) {
  945. int signature_length;
  946. char *signature, sigbuf[8];
  947. phar_entry_info entry = {0};
  948. php_stream *newfile;
  949. off_t tell, st;
  950. newfile = php_stream_fopen_tmpfile();
  951. st = tell = php_stream_tell(pass->filefp);
  952. /* copy the local files, central directory, and the zip comment to generate the hash */
  953. php_stream_seek(pass->filefp, 0, SEEK_SET);
  954. phar_stream_copy_to_stream(pass->filefp, newfile, tell, NULL);
  955. tell = php_stream_tell(pass->centralfp);
  956. php_stream_seek(pass->centralfp, 0, SEEK_SET);
  957. phar_stream_copy_to_stream(pass->centralfp, newfile, tell, NULL);
  958. if (metadata->c) {
  959. php_stream_write(newfile, metadata->c, metadata->len);
  960. }
  961. if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, pass->error TSRMLS_CC)) {
  962. if (pass->error) {
  963. char *save = *(pass->error);
  964. spprintf(pass->error, 0, "phar error: unable to write signature to zip-based phar: %s", save);
  965. efree(save);
  966. }
  967. php_stream_close(newfile);
  968. return FAILURE;
  969. }
  970. entry.filename = ".phar/signature.bin";
  971. entry.filename_len = sizeof(".phar/signature.bin")-1;
  972. entry.fp = php_stream_fopen_tmpfile();
  973. entry.fp_type = PHAR_MOD;
  974. entry.is_modified = 1;
  975. PHAR_SET_32(sigbuf, phar->sig_flags);
  976. PHAR_SET_32(sigbuf + 4, signature_length);
  977. if (8 != (int)php_stream_write(entry.fp, sigbuf, 8) || signature_length != (int)php_stream_write(entry.fp, signature, signature_length)) {
  978. efree(signature);
  979. if (pass->error) {
  980. spprintf(pass->error, 0, "phar error: unable to write signature to zip-based phar %s", phar->fname);
  981. }
  982. php_stream_close(newfile);
  983. return FAILURE;
  984. }
  985. efree(signature);
  986. entry.uncompressed_filesize = entry.compressed_filesize = signature_length + 8;
  987. entry.phar = phar;
  988. /* throw out return value and write the signature */
  989. phar_zip_changed_apply((void *)&entry, (void *)pass TSRMLS_CC);
  990. php_stream_close(newfile);
  991. if (pass->error && *(pass->error)) {
  992. /* error is set by writeheaders */
  993. php_stream_close(newfile);
  994. return FAILURE;
  995. }
  996. } /* signature */
  997. return SUCCESS;
  998. }
  999. /* }}} */
  1000. int phar_zip_flush(phar_archive_data *phar, char *user_stub, long len, int defaultstub, char **error TSRMLS_DC) /* {{{ */
  1001. {
  1002. char *pos;
  1003. smart_str main_metadata_str = {0};
  1004. static const char newstub[] = "<?php // zip-based phar archive stub file\n__HALT_COMPILER();";
  1005. char halt_stub[] = "__HALT_COMPILER();";
  1006. char *tmp;
  1007. php_stream *stubfile, *oldfile;
  1008. php_serialize_data_t metadata_hash;
  1009. int free_user_stub, closeoldfile = 0;
  1010. phar_entry_info entry = {0};
  1011. char *temperr = NULL;
  1012. struct _phar_zip_pass pass;
  1013. phar_zip_dir_end eocd;
  1014. php_uint32 cdir_size, cdir_offset;
  1015. pass.error = &temperr;
  1016. entry.flags = PHAR_ENT_PERM_DEF_FILE;
  1017. entry.timestamp = sapi_get_request_time(TSRMLS_C);
  1018. entry.is_modified = 1;
  1019. entry.is_zip = 1;
  1020. entry.phar = phar;
  1021. entry.fp_type = PHAR_MOD;
  1022. if (phar->is_persistent) {
  1023. if (error) {
  1024. spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname);
  1025. }
  1026. return EOF;
  1027. }
  1028. if (phar->is_data) {
  1029. goto nostub;
  1030. }
  1031. /* set alias */
  1032. if (!phar->is_temporary_alias && phar->alias_len) {
  1033. entry.fp = php_stream_fopen_tmpfile();
  1034. if (phar->alias_len != (int)php_stream_write(entry.fp, phar->alias, phar->alias_len)) {
  1035. if (error) {
  1036. spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname);
  1037. }
  1038. return EOF;
  1039. }
  1040. entry.uncompressed_filesize = entry.compressed_filesize = phar->alias_len;
  1041. entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1);
  1042. entry.filename_len = sizeof(".phar/alias.txt")-1;
  1043. if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
  1044. if (error) {
  1045. spprintf(error, 0, "unable to set alias in zip-based phar \"%s\"", phar->fname);
  1046. }
  1047. return EOF;
  1048. }
  1049. } else {
  1050. zend_hash_del(&phar->manifest, ".phar/alias.txt", sizeof(".phar/alias.txt")-1);
  1051. }
  1052. /* register alias */
  1053. if (phar->alias_len) {
  1054. if (FAILURE == phar_get_archive(&phar, phar->fname, phar->fname_len, phar->alias, phar->alias_len, error TSRMLS_CC)) {
  1055. return EOF;
  1056. }
  1057. }
  1058. /* set stub */
  1059. if (user_stub && !defaultstub) {
  1060. if (len < 0) {
  1061. /* resource passed in */
  1062. if (!(php_stream_from_zval_no_verify(stubfile, (zval **)user_stub))) {
  1063. if (error) {
  1064. spprintf(error, 0, "unable to access resource to copy stub to new zip-based phar \"%s\"", phar->fname);
  1065. }
  1066. return EOF;
  1067. }
  1068. if (len == -1) {
  1069. len = PHP_STREAM_COPY_ALL;
  1070. } else {
  1071. len = -len;
  1072. }
  1073. user_stub = 0;
  1074. #if PHP_MAJOR_VERSION >= 6
  1075. if (!(len = php_stream_copy_to_mem(stubfile, (void **) &user_stub, len, 0)) || !user_stub) {
  1076. #else
  1077. if (!(len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)) || !user_stub) {
  1078. #endif
  1079. if (error) {
  1080. spprintf(error, 0, "unable to read resource to copy stub to new zip-based phar \"%s\"", phar->fname);
  1081. }
  1082. return EOF;
  1083. }
  1084. free_user_stub = 1;
  1085. } else {
  1086. free_user_stub = 0;
  1087. }
  1088. tmp = estrndup(user_stub, len);
  1089. if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
  1090. efree(tmp);
  1091. if (error) {
  1092. spprintf(error, 0, "illegal stub for zip-based phar \"%s\"", phar->fname);
  1093. }
  1094. if (free_user_stub) {
  1095. efree(user_stub);
  1096. }
  1097. return EOF;
  1098. }
  1099. pos = user_stub + (pos - tmp);
  1100. efree(tmp);
  1101. len = pos - user_stub + 18;
  1102. entry.fp = php_stream_fopen_tmpfile();
  1103. entry.uncompressed_filesize = len + 5;
  1104. if ((size_t)len != php_stream_write(entry.fp, user_stub, len)
  1105. || 5 != php_stream_write(entry.fp, " ?>\r\n", 5)) {
  1106. if (error) {
  1107. spprintf(error, 0, "unable to create stub from string in new zip-based phar \"%s\"", phar->fname);
  1108. }
  1109. if (free_user_stub) {
  1110. efree(user_stub);
  1111. }
  1112. php_stream_close(entry.fp);
  1113. return EOF;
  1114. }
  1115. entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
  1116. entry.filename_len = sizeof(".phar/stub.php")-1;
  1117. if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
  1118. if (free_user_stub) {
  1119. efree(user_stub);
  1120. }
  1121. if (error) {
  1122. spprintf(error, 0, "unable to set stub in zip-based phar \"%s\"", phar->fname);
  1123. }
  1124. return EOF;
  1125. }
  1126. if (free_user_stub) {
  1127. efree(user_stub);
  1128. }
  1129. } else {
  1130. /* Either this is a brand new phar (add the stub), or the default stub is required (overwrite the stub) */
  1131. entry.fp = php_stream_fopen_tmpfile();
  1132. if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
  1133. php_stream_close(entry.fp);
  1134. if (error) {
  1135. spprintf(error, 0, "unable to %s stub in%szip-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname);
  1136. }
  1137. return EOF;
  1138. }
  1139. entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
  1140. entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
  1141. entry.filename_len = sizeof(".phar/stub.php")-1;
  1142. if (!defaultstub) {
  1143. if (!zend_hash_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  1144. if (SUCCESS != zend_hash_add(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
  1145. php_stream_close(entry.fp);
  1146. efree(entry.filename);
  1147. if (error) {
  1148. spprintf(error, 0, "unable to create stub in zip-based phar \"%s\"", phar->fname);
  1149. }
  1150. return EOF;
  1151. }
  1152. } else {
  1153. php_stream_close(entry.fp);
  1154. efree(entry.filename);
  1155. }
  1156. } else {
  1157. if (SUCCESS != zend_hash_update(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL)) {
  1158. php_stream_close(entry.fp);
  1159. efree(entry.filename);
  1160. if (error) {
  1161. spprintf(error, 0, "unable to overwrite stub in zip-based phar \"%s\"", phar->fname);
  1162. }
  1163. return EOF;
  1164. }
  1165. }
  1166. }
  1167. nostub:
  1168. if (phar->fp && !phar->is_brandnew) {
  1169. oldfile = phar->fp;
  1170. closeoldfile = 0;
  1171. php_stream_rewind(oldfile);
  1172. } else {
  1173. oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
  1174. closeoldfile = oldfile != NULL;
  1175. }
  1176. /* save modified files to the zip */
  1177. pass.old = oldfile;
  1178. pass.filefp = php_stream_fopen_tmpfile();
  1179. if (!pass.filefp) {
  1180. fperror:
  1181. if (closeoldfile) {
  1182. php_stream_close(oldfile);
  1183. }
  1184. if (error) {
  1185. spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to open temporary file", phar->fname);
  1186. }
  1187. return EOF;
  1188. }
  1189. pass.centralfp = php_stream_fopen_tmpfile();
  1190. if (!pass.centralfp) {
  1191. goto fperror;
  1192. }
  1193. pass.free_fp = pass.free_ufp = 1;
  1194. memset(&eocd, 0, sizeof(eocd));
  1195. strncpy(eocd.signature, "PK\5\6", 4);
  1196. if (!phar->is_data && !phar->sig_flags) {
  1197. phar->sig_flags = PHAR_SIG_SHA1;
  1198. }
  1199. if (phar->sig_flags) {
  1200. PHAR_SET_16(eocd.counthere, zend_hash_num_elements(&phar->manifest) + 1);
  1201. PHAR_SET_16(eocd.count, zend_hash_num_elements(&phar->manifest) + 1);
  1202. } else {
  1203. PHAR_SET_16(eocd.counthere, zend_hash_num_elements(&phar->manifest));
  1204. PHAR_SET_16(eocd.count, zend_hash_num_elements(&phar->manifest));
  1205. }
  1206. zend_hash_apply_with_argument(&phar->manifest, phar_zip_changed_apply, (void *) &pass TSRMLS_CC);
  1207. if (phar->metadata) {
  1208. /* set phar metadata */
  1209. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  1210. php_var_serialize(&main_metadata_str, &phar->metadata, &metadata_hash TSRMLS_CC);
  1211. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  1212. }
  1213. if (temperr) {
  1214. if (error) {
  1215. spprintf(error, 4096, "phar zip flush of \"%s\" failed: %s", phar->fname, temperr);
  1216. }
  1217. efree(temperr);
  1218. temperror:
  1219. php_stream_close(pass.centralfp);
  1220. nocentralerror:
  1221. if (phar->metadata) {
  1222. smart_str_free(&main_metadata_str);
  1223. }
  1224. php_stream_close(pass.filefp);
  1225. if (closeoldfile) {
  1226. php_stream_close(oldfile);
  1227. }
  1228. return EOF;
  1229. }
  1230. if (FAILURE == phar_zip_applysignature(phar, &pass, &main_metadata_str TSRMLS_CC)) {
  1231. goto temperror;
  1232. }
  1233. /* save zip */
  1234. cdir_size = php_stream_tell(pass.centralfp);
  1235. cdir_offset = php_stream_tell(pass.filefp);
  1236. PHAR_SET_32(eocd.cdir_size, cdir_size);
  1237. PHAR_SET_32(eocd.cdir_offset, cdir_offset);
  1238. php_stream_seek(pass.centralfp, 0, SEEK_SET);
  1239. {
  1240. size_t clen;
  1241. int ret = phar_stream_copy_to_stream(pass.centralfp, pass.filefp, PHP_STREAM_COPY_ALL, &clen);
  1242. if (SUCCESS != ret || clen != cdir_size) {
  1243. if (error) {
  1244. spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write central-directory", phar->fname);
  1245. }
  1246. goto temperror;
  1247. }
  1248. }
  1249. php_stream_close(pass.centralfp);
  1250. if (phar->metadata) {
  1251. /* set phar metadata */
  1252. PHAR_SET_16(eocd.comment_len, main_metadata_str.len);
  1253. if (sizeof(eocd) != php_stream_write(pass.filefp, (char *)&eocd, sizeof(eocd))) {
  1254. if (error) {
  1255. spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write end of central-directory", phar->fname);
  1256. }
  1257. goto nocentralerror;
  1258. }
  1259. if (main_metadata_str.len != php_stream_write(pass.filefp, main_metadata_str.c, main_metadata_str.len)) {
  1260. if (error) {
  1261. spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write metadata to zip comment", phar->fname);
  1262. }
  1263. goto nocentralerror;
  1264. }
  1265. smart_str_free(&main_metadata_str);
  1266. } else {
  1267. if (sizeof(eocd) != php_stream_write(pass.filefp, (char *)&eocd, sizeof(eocd))) {
  1268. if (error) {
  1269. spprintf(error, 4096, "phar zip flush of \"%s\" failed: unable to write end of central-directory", phar->fname);
  1270. }
  1271. goto nocentralerror;
  1272. }
  1273. }
  1274. if (phar->fp && pass.free_fp) {
  1275. php_stream_close(phar->fp);
  1276. }
  1277. if (phar->ufp) {
  1278. if (pass.free_ufp) {
  1279. php_stream_close(phar->ufp);
  1280. }
  1281. phar->ufp = NULL;
  1282. }
  1283. /* re-open */
  1284. phar->is_brandnew = 0;
  1285. if (phar->donotflush) {
  1286. /* deferred flush */
  1287. phar->fp = pass.filefp;
  1288. } else {
  1289. phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
  1290. if (!phar->fp) {
  1291. if (closeoldfile) {
  1292. php_stream_close(oldfile);
  1293. }
  1294. phar->fp = pass.filefp;
  1295. if (error) {
  1296. spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname);
  1297. }
  1298. return EOF;
  1299. }
  1300. php_stream_rewind(pass.filefp);
  1301. phar_stream_copy_to_stream(pass.filefp, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  1302. /* we could also reopen the file in "rb" mode but there is no need for that */
  1303. php_stream_close(pass.filefp);
  1304. }
  1305. if (closeoldfile) {
  1306. php_stream_close(oldfile);
  1307. }
  1308. return EOF;
  1309. }
  1310. /* }}} */
  1311. /*
  1312. * Local variables:
  1313. * tab-width: 4
  1314. * c-basic-offset: 4
  1315. * End:
  1316. * vim600: noet sw=4 ts=4 fdm=marker
  1317. * vim<600: noet sw=4 ts=4
  1318. */