zip_dirent.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. zip_dirent.c -- read directory entry (local or central), clean dirent
  3. Copyright (c) 1999-2017 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include "zipint.h"
  36. static time_t _zip_d2u_time(zip_uint16_t, zip_uint16_t);
  37. static zip_string_t *_zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str);
  38. static zip_extra_field_t *_zip_ef_utf8(zip_uint16_t, zip_string_t *, zip_error_t *);
  39. void
  40. _zip_cdir_free(zip_cdir_t *cd)
  41. {
  42. zip_uint64_t i;
  43. if (!cd)
  44. return;
  45. for (i=0; i<cd->nentry; i++)
  46. _zip_entry_finalize(cd->entry+i);
  47. free(cd->entry);
  48. _zip_string_free(cd->comment);
  49. free(cd);
  50. }
  51. zip_cdir_t *
  52. _zip_cdir_new(zip_uint64_t nentry, zip_error_t *error)
  53. {
  54. zip_cdir_t *cd;
  55. zip_uint64_t i;
  56. if ((cd=(zip_cdir_t *)malloc(sizeof(*cd))) == NULL) {
  57. zip_error_set(error, ZIP_ER_MEMORY, 0);
  58. return NULL;
  59. }
  60. if (nentry == 0)
  61. cd->entry = NULL;
  62. else if ((nentry > SIZE_MAX/sizeof(*(cd->entry))) || (cd->entry=(zip_entry_t *)malloc(sizeof(*(cd->entry))*(size_t)nentry)) == NULL) {
  63. zip_error_set(error, ZIP_ER_MEMORY, 0);
  64. free(cd);
  65. return NULL;
  66. }
  67. for (i=0; i<nentry; i++)
  68. _zip_entry_init(cd->entry+i);
  69. cd->nentry = cd->nentry_alloc = nentry;
  70. cd->size = cd->offset = 0;
  71. cd->comment = NULL;
  72. return cd;
  73. }
  74. zip_int64_t
  75. _zip_cdir_write(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors)
  76. {
  77. zip_uint64_t offset, size;
  78. zip_string_t *comment;
  79. zip_uint8_t buf[EOCDLEN + EOCD64LEN + EOCD64LOCLEN];
  80. zip_buffer_t *buffer;
  81. zip_int64_t off;
  82. zip_uint64_t i;
  83. bool is_zip64;
  84. int ret;
  85. if ((off = zip_source_tell_write(za->src)) < 0) {
  86. _zip_error_set_from_source(&za->error, za->src);
  87. return -1;
  88. }
  89. offset = (zip_uint64_t)off;
  90. is_zip64 = false;
  91. for (i=0; i<survivors; i++) {
  92. zip_entry_t *entry = za->entry+filelist[i].idx;
  93. if ((ret=_zip_dirent_write(za, entry->changes ? entry->changes : entry->orig, ZIP_FL_CENTRAL)) < 0)
  94. return -1;
  95. if (ret)
  96. is_zip64 = true;
  97. }
  98. if ((off = zip_source_tell_write(za->src)) < 0) {
  99. _zip_error_set_from_source(&za->error, za->src);
  100. return -1;
  101. }
  102. size = (zip_uint64_t)off - offset;
  103. if (offset > ZIP_UINT32_MAX || survivors > ZIP_UINT16_MAX)
  104. is_zip64 = true;
  105. if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) {
  106. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  107. return -1;
  108. }
  109. if (is_zip64) {
  110. _zip_buffer_put(buffer, EOCD64_MAGIC, 4);
  111. _zip_buffer_put_64(buffer, EOCD64LEN-12);
  112. _zip_buffer_put_16(buffer, 45);
  113. _zip_buffer_put_16(buffer, 45);
  114. _zip_buffer_put_32(buffer, 0);
  115. _zip_buffer_put_32(buffer, 0);
  116. _zip_buffer_put_64(buffer, survivors);
  117. _zip_buffer_put_64(buffer, survivors);
  118. _zip_buffer_put_64(buffer, size);
  119. _zip_buffer_put_64(buffer, offset);
  120. _zip_buffer_put(buffer, EOCD64LOC_MAGIC, 4);
  121. _zip_buffer_put_32(buffer, 0);
  122. _zip_buffer_put_64(buffer, offset+size);
  123. _zip_buffer_put_32(buffer, 1);
  124. }
  125. _zip_buffer_put(buffer, EOCD_MAGIC, 4);
  126. _zip_buffer_put_32(buffer, 0);
  127. _zip_buffer_put_16(buffer, (zip_uint16_t)(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : survivors));
  128. _zip_buffer_put_16(buffer, (zip_uint16_t)(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : survivors));
  129. _zip_buffer_put_32(buffer, size >= ZIP_UINT32_MAX ? ZIP_UINT32_MAX : (zip_uint32_t)size);
  130. _zip_buffer_put_32(buffer, offset >= ZIP_UINT32_MAX ? ZIP_UINT32_MAX : (zip_uint32_t)offset);
  131. comment = za->comment_changed ? za->comment_changes : za->comment_orig;
  132. _zip_buffer_put_16(buffer, (zip_uint16_t)(comment ? comment->length : 0));
  133. if (!_zip_buffer_ok(buffer)) {
  134. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  135. _zip_buffer_free(buffer);
  136. return -1;
  137. }
  138. if (_zip_write(za, _zip_buffer_data(buffer), _zip_buffer_offset(buffer)) < 0) {
  139. _zip_buffer_free(buffer);
  140. return -1;
  141. }
  142. _zip_buffer_free(buffer);
  143. if (comment) {
  144. if (_zip_write(za, comment->raw, comment->length) < 0) {
  145. return -1;
  146. }
  147. }
  148. return (zip_int64_t)size;
  149. }
  150. zip_dirent_t *
  151. _zip_dirent_clone(const zip_dirent_t *sde)
  152. {
  153. zip_dirent_t *tde;
  154. if ((tde=(zip_dirent_t *)malloc(sizeof(*tde))) == NULL)
  155. return NULL;
  156. if (sde)
  157. memcpy(tde, sde, sizeof(*sde));
  158. else
  159. _zip_dirent_init(tde);
  160. tde->changed = 0;
  161. tde->cloned = 1;
  162. return tde;
  163. }
  164. void
  165. _zip_dirent_finalize(zip_dirent_t *zde)
  166. {
  167. if (!zde->cloned || zde->changed & ZIP_DIRENT_FILENAME) {
  168. _zip_string_free(zde->filename);
  169. zde->filename = NULL;
  170. }
  171. if (!zde->cloned || zde->changed & ZIP_DIRENT_EXTRA_FIELD) {
  172. _zip_ef_free(zde->extra_fields);
  173. zde->extra_fields = NULL;
  174. }
  175. if (!zde->cloned || zde->changed & ZIP_DIRENT_COMMENT) {
  176. _zip_string_free(zde->comment);
  177. zde->comment = NULL;
  178. }
  179. }
  180. void
  181. _zip_dirent_free(zip_dirent_t *zde)
  182. {
  183. if (zde == NULL)
  184. return;
  185. _zip_dirent_finalize(zde);
  186. free(zde);
  187. }
  188. void
  189. _zip_dirent_init(zip_dirent_t *de)
  190. {
  191. de->changed = 0;
  192. de->local_extra_fields_read = 0;
  193. de->cloned = 0;
  194. de->version_madeby = 20 | (ZIP_OPSYS_DEFAULT << 8);
  195. de->version_needed = 20; /* 2.0 */
  196. de->bitflags = 0;
  197. de->comp_method = ZIP_CM_DEFAULT;
  198. de->last_mod = 0;
  199. de->crc = 0;
  200. de->comp_size = 0;
  201. de->uncomp_size = 0;
  202. de->filename = NULL;
  203. de->extra_fields = NULL;
  204. de->comment = NULL;
  205. de->disk_number = 0;
  206. de->int_attrib = 0;
  207. de->ext_attrib = ZIP_EXT_ATTRIB_DEFAULT;
  208. de->offset = 0;
  209. }
  210. bool
  211. _zip_dirent_needs_zip64(const zip_dirent_t *de, zip_flags_t flags)
  212. {
  213. if (de->uncomp_size >= ZIP_UINT32_MAX || de->comp_size >= ZIP_UINT32_MAX
  214. || ((flags & ZIP_FL_CENTRAL) && de->offset >= ZIP_UINT32_MAX))
  215. return true;
  216. return false;
  217. }
  218. zip_dirent_t *
  219. _zip_dirent_new(void)
  220. {
  221. zip_dirent_t *de;
  222. if ((de=(zip_dirent_t *)malloc(sizeof(*de))) == NULL)
  223. return NULL;
  224. _zip_dirent_init(de);
  225. return de;
  226. }
  227. /* _zip_dirent_read(zde, fp, bufp, left, localp, error):
  228. Fills the zip directory entry zde.
  229. If buffer is non-NULL, data is taken from there; otherwise data is read from fp as needed.
  230. If local is true, it reads a local header instead of a central directory entry.
  231. Returns size of dirent read if successful. On error, error is filled in and -1 is returned.
  232. */
  233. zip_int64_t
  234. _zip_dirent_read(zip_dirent_t *zde, zip_source_t *src, zip_buffer_t *buffer, bool local, zip_error_t *error)
  235. {
  236. zip_uint8_t buf[CDENTRYSIZE];
  237. zip_uint16_t dostime, dosdate;
  238. zip_uint32_t size, variable_size;
  239. zip_uint16_t filename_len, comment_len, ef_len;
  240. bool from_buffer = (buffer != NULL);
  241. size = local ? LENTRYSIZE : CDENTRYSIZE;
  242. if (buffer) {
  243. if (_zip_buffer_left(buffer) < size) {
  244. zip_error_set(error, ZIP_ER_NOZIP, 0);
  245. return -1;
  246. }
  247. }
  248. else {
  249. if ((buffer = _zip_buffer_new_from_source(src, size, buf, error)) == NULL) {
  250. return -1;
  251. }
  252. }
  253. if (memcmp(_zip_buffer_get(buffer, 4), (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
  254. zip_error_set(error, ZIP_ER_NOZIP, 0);
  255. if (!from_buffer) {
  256. _zip_buffer_free(buffer);
  257. }
  258. return -1;
  259. }
  260. /* convert buffercontents to zip_dirent */
  261. _zip_dirent_init(zde);
  262. if (!local)
  263. zde->version_madeby = _zip_buffer_get_16(buffer);
  264. else
  265. zde->version_madeby = 0;
  266. zde->version_needed = _zip_buffer_get_16(buffer);
  267. zde->bitflags = _zip_buffer_get_16(buffer);
  268. zde->comp_method = _zip_buffer_get_16(buffer);
  269. /* convert to time_t */
  270. dostime = _zip_buffer_get_16(buffer);
  271. dosdate = _zip_buffer_get_16(buffer);
  272. zde->last_mod = _zip_d2u_time(dostime, dosdate);
  273. zde->crc = _zip_buffer_get_32(buffer);
  274. zde->comp_size = _zip_buffer_get_32(buffer);
  275. zde->uncomp_size = _zip_buffer_get_32(buffer);
  276. filename_len = _zip_buffer_get_16(buffer);
  277. ef_len = _zip_buffer_get_16(buffer);
  278. if (local) {
  279. comment_len = 0;
  280. zde->disk_number = 0;
  281. zde->int_attrib = 0;
  282. zde->ext_attrib = 0;
  283. zde->offset = 0;
  284. } else {
  285. comment_len = _zip_buffer_get_16(buffer);
  286. zde->disk_number = _zip_buffer_get_16(buffer);
  287. zde->int_attrib = _zip_buffer_get_16(buffer);
  288. zde->ext_attrib = _zip_buffer_get_32(buffer);
  289. zde->offset = _zip_buffer_get_32(buffer);
  290. }
  291. if (!_zip_buffer_ok(buffer)) {
  292. zip_error_set(error, ZIP_ER_INTERNAL, 0);
  293. if (!from_buffer) {
  294. _zip_buffer_free(buffer);
  295. }
  296. return -1;
  297. }
  298. zde->filename = NULL;
  299. zde->extra_fields = NULL;
  300. zde->comment = NULL;
  301. variable_size = (zip_uint32_t)filename_len+(zip_uint32_t)ef_len+(zip_uint32_t)comment_len;
  302. if (from_buffer) {
  303. if (_zip_buffer_left(buffer) < variable_size) {
  304. zip_error_set(error, ZIP_ER_INCONS, 0);
  305. return -1;
  306. }
  307. }
  308. else {
  309. _zip_buffer_free(buffer);
  310. if ((buffer = _zip_buffer_new_from_source(src, variable_size, NULL, error)) == NULL) {
  311. return -1;
  312. }
  313. }
  314. if (filename_len) {
  315. zde->filename = _zip_read_string(buffer, src, filename_len, 1, error);
  316. if (!zde->filename) {
  317. if (zip_error_code_zip(error) == ZIP_ER_EOF) {
  318. zip_error_set(error, ZIP_ER_INCONS, 0);
  319. }
  320. if (!from_buffer) {
  321. _zip_buffer_free(buffer);
  322. }
  323. return -1;
  324. }
  325. if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
  326. if (_zip_guess_encoding(zde->filename, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) {
  327. zip_error_set(error, ZIP_ER_INCONS, 0);
  328. if (!from_buffer) {
  329. _zip_buffer_free(buffer);
  330. }
  331. return -1;
  332. }
  333. }
  334. }
  335. if (ef_len) {
  336. zip_uint8_t *ef = _zip_read_data(buffer, src, ef_len, 0, error);
  337. if (ef == NULL) {
  338. if (!from_buffer) {
  339. _zip_buffer_free(buffer);
  340. }
  341. return -1;
  342. }
  343. if (!_zip_ef_parse(ef, ef_len, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, &zde->extra_fields, error)) {
  344. free(ef);
  345. if (!from_buffer) {
  346. _zip_buffer_free(buffer);
  347. }
  348. return -1;
  349. }
  350. free(ef);
  351. if (local)
  352. zde->local_extra_fields_read = 1;
  353. }
  354. if (comment_len) {
  355. zde->comment = _zip_read_string(buffer, src, comment_len, 0, error);
  356. if (!zde->comment) {
  357. if (!from_buffer) {
  358. _zip_buffer_free(buffer);
  359. }
  360. return -1;
  361. }
  362. if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
  363. if (_zip_guess_encoding(zde->comment, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) {
  364. zip_error_set(error, ZIP_ER_INCONS, 0);
  365. if (!from_buffer) {
  366. _zip_buffer_free(buffer);
  367. }
  368. return -1;
  369. }
  370. }
  371. }
  372. zde->filename = _zip_dirent_process_ef_utf_8(zde, ZIP_EF_UTF_8_NAME, zde->filename);
  373. zde->comment = _zip_dirent_process_ef_utf_8(zde, ZIP_EF_UTF_8_COMMENT, zde->comment);
  374. /* Zip64 */
  375. if (zde->uncomp_size == ZIP_UINT32_MAX || zde->comp_size == ZIP_UINT32_MAX || zde->offset == ZIP_UINT32_MAX) {
  376. zip_uint16_t got_len;
  377. zip_buffer_t *ef_buffer;
  378. const zip_uint8_t *ef = _zip_ef_get_by_id(zde->extra_fields, &got_len, ZIP_EF_ZIP64, 0, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, error);
  379. /* TODO: if got_len == 0 && !ZIP64_EOCD: no error, 0xffffffff is valid value */
  380. if (ef == NULL) {
  381. if (!from_buffer) {
  382. _zip_buffer_free(buffer);
  383. }
  384. return -1;
  385. }
  386. if ((ef_buffer = _zip_buffer_new((zip_uint8_t *)ef, got_len)) == NULL) {
  387. zip_error_set(error, ZIP_ER_MEMORY, 0);
  388. if (!from_buffer) {
  389. _zip_buffer_free(buffer);
  390. }
  391. return -1;
  392. }
  393. if (zde->uncomp_size == ZIP_UINT32_MAX)
  394. zde->uncomp_size = _zip_buffer_get_64(ef_buffer);
  395. else if (local) {
  396. /* From appnote.txt: This entry in the Local header MUST
  397. include BOTH original and compressed file size fields. */
  398. (void)_zip_buffer_skip(ef_buffer, 8); /* error is caught by _zip_buffer_eof() call */
  399. }
  400. if (zde->comp_size == ZIP_UINT32_MAX)
  401. zde->comp_size = _zip_buffer_get_64(ef_buffer);
  402. if (!local) {
  403. if (zde->offset == ZIP_UINT32_MAX)
  404. zde->offset = _zip_buffer_get_64(ef_buffer);
  405. if (zde->disk_number == ZIP_UINT16_MAX)
  406. zde->disk_number = _zip_buffer_get_32(buffer);
  407. }
  408. if (!_zip_buffer_eof(ef_buffer)) {
  409. zip_error_set(error, ZIP_ER_INCONS, 0);
  410. _zip_buffer_free(ef_buffer);
  411. if (!from_buffer) {
  412. _zip_buffer_free(buffer);
  413. }
  414. return -1;
  415. }
  416. _zip_buffer_free(ef_buffer);
  417. }
  418. if (!_zip_buffer_ok(buffer)) {
  419. zip_error_set(error, ZIP_ER_INTERNAL, 0);
  420. if (!from_buffer) {
  421. _zip_buffer_free(buffer);
  422. }
  423. return -1;
  424. }
  425. if (!from_buffer) {
  426. _zip_buffer_free(buffer);
  427. }
  428. /* zip_source_seek / zip_source_tell don't support values > ZIP_INT64_MAX */
  429. if (zde->offset > ZIP_INT64_MAX) {
  430. zip_error_set(error, ZIP_ER_SEEK, EFBIG);
  431. return -1;
  432. }
  433. zde->extra_fields = _zip_ef_remove_internal(zde->extra_fields);
  434. return (zip_int64_t)(size + variable_size);
  435. }
  436. static zip_string_t *
  437. _zip_dirent_process_ef_utf_8(const zip_dirent_t *de, zip_uint16_t id, zip_string_t *str)
  438. {
  439. zip_uint16_t ef_len;
  440. zip_uint32_t ef_crc;
  441. zip_buffer_t *buffer;
  442. const zip_uint8_t *ef = _zip_ef_get_by_id(de->extra_fields, &ef_len, id, 0, ZIP_EF_BOTH, NULL);
  443. if (ef == NULL || ef_len < 5 || ef[0] != 1) {
  444. return str;
  445. }
  446. if ((buffer = _zip_buffer_new((zip_uint8_t *)ef, ef_len)) == NULL) {
  447. return str;
  448. }
  449. _zip_buffer_get_8(buffer);
  450. ef_crc = _zip_buffer_get_32(buffer);
  451. if (_zip_string_crc32(str) == ef_crc) {
  452. zip_uint16_t len = (zip_uint16_t)_zip_buffer_left(buffer);
  453. zip_string_t *ef_str = _zip_string_new(_zip_buffer_get(buffer, len), len, ZIP_FL_ENC_UTF_8, NULL);
  454. if (ef_str != NULL) {
  455. _zip_string_free(str);
  456. str = ef_str;
  457. }
  458. }
  459. _zip_buffer_free(buffer);
  460. return str;
  461. }
  462. zip_int32_t
  463. _zip_dirent_size(zip_source_t *src, zip_uint16_t flags, zip_error_t *error)
  464. {
  465. zip_int32_t size;
  466. bool local = (flags & ZIP_EF_LOCAL) != 0;
  467. int i;
  468. zip_uint8_t b[6];
  469. zip_buffer_t *buffer;
  470. size = local ? LENTRYSIZE : CDENTRYSIZE;
  471. if (zip_source_seek(src, local ? 26 : 28, SEEK_CUR) < 0) {
  472. _zip_error_set_from_source(error, src);
  473. return -1;
  474. }
  475. if ((buffer = _zip_buffer_new_from_source(src, local ? 4 : 6, b, error)) == NULL) {
  476. return -1;
  477. }
  478. for (i=0; i<(local ? 2 : 3); i++) {
  479. size += _zip_buffer_get_16(buffer);
  480. }
  481. if (!_zip_buffer_eof(buffer)) {
  482. zip_error_set(error, ZIP_ER_INTERNAL, 0);
  483. _zip_buffer_free(buffer);
  484. return -1;
  485. }
  486. _zip_buffer_free(buffer);
  487. return size;
  488. }
  489. /* _zip_dirent_write
  490. Writes zip directory entry.
  491. If flags & ZIP_EF_LOCAL, it writes a local header instead of a central
  492. directory entry. If flags & ZIP_EF_FORCE_ZIP64, a ZIP64 extra field is written, even if not needed.
  493. Returns 0 if successful, 1 if successful and wrote ZIP64 extra field. On error, error is filled in and -1 is
  494. returned.
  495. */
  496. int
  497. _zip_dirent_write(zip_t *za, zip_dirent_t *de, zip_flags_t flags)
  498. {
  499. zip_uint16_t dostime, dosdate;
  500. zip_encoding_type_t com_enc, name_enc;
  501. zip_extra_field_t *ef;
  502. zip_extra_field_t *ef64;
  503. zip_uint32_t ef_total_size;
  504. bool is_zip64;
  505. bool is_really_zip64;
  506. zip_uint8_t buf[CDENTRYSIZE];
  507. zip_buffer_t *buffer;
  508. ef = NULL;
  509. name_enc = _zip_guess_encoding(de->filename, ZIP_ENCODING_UNKNOWN);
  510. com_enc = _zip_guess_encoding(de->comment, ZIP_ENCODING_UNKNOWN);
  511. if ((name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_ASCII) ||
  512. (name_enc == ZIP_ENCODING_ASCII && com_enc == ZIP_ENCODING_UTF8_KNOWN) ||
  513. (name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_UTF8_KNOWN))
  514. de->bitflags |= ZIP_GPBF_ENCODING_UTF_8;
  515. else {
  516. de->bitflags &= (zip_uint16_t)~ZIP_GPBF_ENCODING_UTF_8;
  517. if (name_enc == ZIP_ENCODING_UTF8_KNOWN) {
  518. ef = _zip_ef_utf8(ZIP_EF_UTF_8_NAME, de->filename, &za->error);
  519. if (ef == NULL)
  520. return -1;
  521. }
  522. if ((flags & ZIP_FL_LOCAL) == 0 && com_enc == ZIP_ENCODING_UTF8_KNOWN){
  523. zip_extra_field_t *ef2 = _zip_ef_utf8(ZIP_EF_UTF_8_COMMENT, de->comment, &za->error);
  524. if (ef2 == NULL) {
  525. _zip_ef_free(ef);
  526. return -1;
  527. }
  528. ef2->next = ef;
  529. ef = ef2;
  530. }
  531. }
  532. is_really_zip64 = _zip_dirent_needs_zip64(de, flags);
  533. is_zip64 = (flags & (ZIP_FL_LOCAL|ZIP_FL_FORCE_ZIP64)) == (ZIP_FL_LOCAL|ZIP_FL_FORCE_ZIP64) || is_really_zip64;
  534. if (is_zip64) {
  535. zip_uint8_t ef_zip64[EFZIP64SIZE];
  536. zip_buffer_t *ef_buffer = _zip_buffer_new(ef_zip64, sizeof(ef_zip64));
  537. if (ef_buffer == NULL) {
  538. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  539. _zip_ef_free(ef);
  540. return -1;
  541. }
  542. if (flags & ZIP_FL_LOCAL) {
  543. if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX) {
  544. _zip_buffer_put_64(ef_buffer, de->uncomp_size);
  545. _zip_buffer_put_64(ef_buffer, de->comp_size);
  546. }
  547. }
  548. else {
  549. if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX || de->offset > ZIP_UINT32_MAX) {
  550. if (de->uncomp_size >= ZIP_UINT32_MAX) {
  551. _zip_buffer_put_64(ef_buffer, de->uncomp_size);
  552. }
  553. if (de->comp_size >= ZIP_UINT32_MAX) {
  554. _zip_buffer_put_64(ef_buffer, de->comp_size);
  555. }
  556. if (de->offset >= ZIP_UINT32_MAX) {
  557. _zip_buffer_put_64(ef_buffer, de->offset);
  558. }
  559. }
  560. }
  561. if (!_zip_buffer_ok(ef_buffer)) {
  562. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  563. _zip_buffer_free(ef_buffer);
  564. _zip_ef_free(ef);
  565. return -1;
  566. }
  567. ef64 = _zip_ef_new(ZIP_EF_ZIP64, (zip_uint16_t)(_zip_buffer_offset(ef_buffer)), ef_zip64, ZIP_EF_BOTH);
  568. _zip_buffer_free(ef_buffer);
  569. ef64->next = ef;
  570. ef = ef64;
  571. }
  572. if ((buffer = _zip_buffer_new(buf, sizeof(buf))) == NULL) {
  573. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  574. _zip_ef_free(ef);
  575. return -1;
  576. }
  577. _zip_buffer_put(buffer, (flags & ZIP_FL_LOCAL) ? LOCAL_MAGIC : CENTRAL_MAGIC, 4);
  578. if ((flags & ZIP_FL_LOCAL) == 0) {
  579. _zip_buffer_put_16(buffer, (zip_uint16_t)(is_really_zip64 ? 45 : de->version_madeby));
  580. }
  581. _zip_buffer_put_16(buffer, (zip_uint16_t)(is_really_zip64 ? 45 : de->version_needed));
  582. _zip_buffer_put_16(buffer, de->bitflags&0xfff9); /* clear compression method specific flags */
  583. _zip_buffer_put_16(buffer, (zip_uint16_t)de->comp_method);
  584. _zip_u2d_time(de->last_mod, &dostime, &dosdate);
  585. _zip_buffer_put_16(buffer, dostime);
  586. _zip_buffer_put_16(buffer, dosdate);
  587. _zip_buffer_put_32(buffer, de->crc);
  588. if (((flags & ZIP_FL_LOCAL) == ZIP_FL_LOCAL) && ((de->comp_size >= ZIP_UINT32_MAX) || (de->uncomp_size >= ZIP_UINT32_MAX))) {
  589. /* In local headers, if a ZIP64 EF is written, it MUST contain
  590. * both compressed and uncompressed sizes (even if one of the
  591. * two is smaller than 0xFFFFFFFF); on the other hand, those
  592. * may only appear when the corresponding standard entry is
  593. * 0xFFFFFFFF. (appnote.txt 4.5.3) */
  594. _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
  595. _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
  596. }
  597. else {
  598. if (de->comp_size < ZIP_UINT32_MAX) {
  599. _zip_buffer_put_32(buffer, (zip_uint32_t)de->comp_size);
  600. }
  601. else {
  602. _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
  603. }
  604. if (de->uncomp_size < ZIP_UINT32_MAX) {
  605. _zip_buffer_put_32(buffer, (zip_uint32_t)de->uncomp_size);
  606. }
  607. else {
  608. _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
  609. }
  610. }
  611. _zip_buffer_put_16(buffer, _zip_string_length(de->filename));
  612. /* TODO: check for overflow */
  613. ef_total_size = (zip_uint32_t)_zip_ef_size(de->extra_fields, flags) + (zip_uint32_t)_zip_ef_size(ef, ZIP_EF_BOTH);
  614. _zip_buffer_put_16(buffer, (zip_uint16_t)ef_total_size);
  615. if ((flags & ZIP_FL_LOCAL) == 0) {
  616. _zip_buffer_put_16(buffer, _zip_string_length(de->comment));
  617. _zip_buffer_put_16(buffer, (zip_uint16_t)de->disk_number);
  618. _zip_buffer_put_16(buffer, de->int_attrib);
  619. _zip_buffer_put_32(buffer, de->ext_attrib);
  620. if (de->offset < ZIP_UINT32_MAX)
  621. _zip_buffer_put_32(buffer, (zip_uint32_t)de->offset);
  622. else
  623. _zip_buffer_put_32(buffer, ZIP_UINT32_MAX);
  624. }
  625. if (!_zip_buffer_ok(buffer)) {
  626. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  627. _zip_buffer_free(buffer);
  628. _zip_ef_free(ef);
  629. return -1;
  630. }
  631. if (_zip_write(za, buf, _zip_buffer_offset(buffer)) < 0) {
  632. _zip_buffer_free(buffer);
  633. _zip_ef_free(ef);
  634. return -1;
  635. }
  636. _zip_buffer_free(buffer);
  637. if (de->filename) {
  638. if (_zip_string_write(za, de->filename) < 0) {
  639. _zip_ef_free(ef);
  640. return -1;
  641. }
  642. }
  643. if (ef) {
  644. if (_zip_ef_write(za, ef, ZIP_EF_BOTH) < 0) {
  645. _zip_ef_free(ef);
  646. return -1;
  647. }
  648. }
  649. _zip_ef_free(ef);
  650. if (de->extra_fields) {
  651. if (_zip_ef_write(za, de->extra_fields, flags) < 0) {
  652. return -1;
  653. }
  654. }
  655. if ((flags & ZIP_FL_LOCAL) == 0) {
  656. if (de->comment) {
  657. if (_zip_string_write(za, de->comment) < 0) {
  658. return -1;
  659. }
  660. }
  661. }
  662. return is_zip64;
  663. }
  664. static time_t
  665. _zip_d2u_time(zip_uint16_t dtime, zip_uint16_t ddate)
  666. {
  667. struct tm tm;
  668. memset(&tm, 0, sizeof(tm));
  669. /* let mktime decide if DST is in effect */
  670. tm.tm_isdst = -1;
  671. tm.tm_year = ((ddate>>9)&127) + 1980 - 1900;
  672. tm.tm_mon = ((ddate>>5)&15) - 1;
  673. tm.tm_mday = ddate&31;
  674. tm.tm_hour = (dtime>>11)&31;
  675. tm.tm_min = (dtime>>5)&63;
  676. tm.tm_sec = (dtime<<1)&62;
  677. return mktime(&tm);
  678. }
  679. static zip_extra_field_t *
  680. _zip_ef_utf8(zip_uint16_t id, zip_string_t *str, zip_error_t *error)
  681. {
  682. const zip_uint8_t *raw;
  683. zip_uint32_t len;
  684. zip_buffer_t *buffer;
  685. zip_extra_field_t *ef;
  686. if ((raw=_zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL)) == NULL) {
  687. /* error already set */
  688. return NULL;
  689. }
  690. if (len+5 > ZIP_UINT16_MAX) {
  691. zip_error_set(error, ZIP_ER_INVAL, 0); /* TODO: better error code? */
  692. return NULL;
  693. }
  694. if ((buffer = _zip_buffer_new(NULL, len+5)) == NULL) {
  695. zip_error_set(error, ZIP_ER_MEMORY, 0);
  696. return NULL;
  697. }
  698. _zip_buffer_put_8(buffer, 1);
  699. _zip_buffer_put_32(buffer, _zip_string_crc32(str));
  700. _zip_buffer_put(buffer, raw, len);
  701. if (!_zip_buffer_ok(buffer)) {
  702. zip_error_set(error, ZIP_ER_INTERNAL, 0);
  703. _zip_buffer_free(buffer);
  704. return NULL;
  705. }
  706. ef = _zip_ef_new(id, (zip_uint16_t)(_zip_buffer_offset(buffer)), _zip_buffer_data(buffer), ZIP_EF_BOTH);
  707. _zip_buffer_free(buffer);
  708. return ef;
  709. }
  710. zip_dirent_t *
  711. _zip_get_dirent(zip_t *za, zip_uint64_t idx, zip_flags_t flags, zip_error_t *error)
  712. {
  713. if (error == NULL)
  714. error = &za->error;
  715. if (idx >= za->nentry) {
  716. zip_error_set(error, ZIP_ER_INVAL, 0);
  717. return NULL;
  718. }
  719. if ((flags & ZIP_FL_UNCHANGED) || za->entry[idx].changes == NULL) {
  720. if (za->entry[idx].orig == NULL) {
  721. zip_error_set(error, ZIP_ER_INVAL, 0);
  722. return NULL;
  723. }
  724. if (za->entry[idx].deleted && (flags & ZIP_FL_UNCHANGED) == 0) {
  725. zip_error_set(error, ZIP_ER_DELETED, 0);
  726. return NULL;
  727. }
  728. return za->entry[idx].orig;
  729. }
  730. else
  731. return za->entry[idx].changes;
  732. }
  733. void
  734. _zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate)
  735. {
  736. struct tm *tm;
  737. tm = localtime(&intime);
  738. if (tm->tm_year < 80) {
  739. tm->tm_year = 80;
  740. }
  741. *ddate = (zip_uint16_t)(((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5) + tm->tm_mday);
  742. *dtime = (zip_uint16_t)(((tm->tm_hour)<<11) + ((tm->tm_min)<<5) + ((tm->tm_sec)>>1));
  743. return;
  744. }