zip_dirent.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /*
  2. zip_dirent.c -- read directory entry (local or central), clean dirent
  3. Copyright (C) 1999-2013 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 <[email protected]>
  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 <errno.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include "zipint.h"
  37. static time_t _zip_d2u_time(zip_uint16_t, zip_uint16_t);
  38. static struct zip_string *_zip_read_string(const unsigned char **, FILE *, zip_uint16_t, int, struct zip_error *);
  39. static struct zip_string *_zip_dirent_process_ef_utf_8(const struct zip_dirent *, zip_uint16_t, struct zip_string *);
  40. static struct zip_extra_field *_zip_ef_utf8(zip_uint16_t, struct zip_string *, struct zip_error *);
  41. void
  42. _zip_cdir_free(struct zip_cdir *cd)
  43. {
  44. zip_uint64_t i;
  45. if (!cd)
  46. return;
  47. for (i=0; i<cd->nentry; i++)
  48. _zip_entry_finalize(cd->entry+i);
  49. free(cd->entry);
  50. _zip_string_free(cd->comment);
  51. free(cd);
  52. }
  53. int
  54. _zip_cdir_grow(struct zip_cdir *cd, zip_uint64_t nentry, struct zip_error *error)
  55. {
  56. struct zip_entry *entry;
  57. zip_uint64_t i;
  58. if (nentry < cd->nentry_alloc) {
  59. _zip_error_set(error, ZIP_ER_INTERNAL, 0);
  60. return -1;
  61. }
  62. if (nentry == cd->nentry_alloc)
  63. return 0;
  64. if ((entry=((struct zip_entry *)
  65. realloc(cd->entry, sizeof(*(cd->entry))*(size_t)nentry))) == NULL) {
  66. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  67. return -1;
  68. }
  69. for (i=cd->nentry_alloc; i<nentry; i++)
  70. _zip_entry_init(entry+i);
  71. cd->nentry_alloc = nentry;
  72. cd->entry = entry;
  73. return 0;
  74. }
  75. struct zip_cdir *
  76. _zip_cdir_new(zip_uint64_t nentry, struct zip_error *error)
  77. {
  78. struct zip_cdir *cd;
  79. zip_uint64_t i;
  80. if ((cd=(struct zip_cdir *)malloc(sizeof(*cd))) == NULL) {
  81. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  82. return NULL;
  83. }
  84. if (nentry == 0)
  85. cd->entry = NULL;
  86. else if ((cd->entry=(struct zip_entry *)malloc(sizeof(*(cd->entry))*(size_t)nentry)) == NULL) {
  87. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  88. free(cd);
  89. return NULL;
  90. }
  91. for (i=0; i<nentry; i++)
  92. _zip_entry_init(cd->entry+i);
  93. cd->nentry = cd->nentry_alloc = nentry;
  94. cd->size = cd->offset = 0;
  95. cd->comment = NULL;
  96. return cd;
  97. }
  98. zip_int64_t
  99. _zip_cdir_write(struct zip *za, const struct zip_filelist *filelist, zip_uint64_t survivors, FILE *fp)
  100. {
  101. off_t off;
  102. zip_uint64_t offset, size;
  103. struct zip_string *comment;
  104. zip_uint64_t i;
  105. int is_zip64;
  106. int ret;
  107. if ((off=ftello(fp)) < 0) {
  108. _zip_error_set(&za->error, ZIP_ER_READ, errno);
  109. return -1;
  110. }
  111. offset = (zip_uint64_t)off;
  112. is_zip64 = 0;
  113. for (i=0; i<survivors; i++) {
  114. struct zip_entry *entry = za->entry+filelist[i].idx;
  115. if ((ret=_zip_dirent_write(entry->changes ? entry->changes : entry->orig, fp, ZIP_FL_CENTRAL, &za->error)) < 0)
  116. return -1;
  117. if (ret)
  118. is_zip64 = 1;
  119. }
  120. if ((off=ftello(fp)) < 0) {
  121. _zip_error_set(&za->error, ZIP_ER_READ, errno);
  122. return -1;
  123. }
  124. size = (zip_uint64_t)off - offset;
  125. if (offset > ZIP_UINT32_MAX || survivors > ZIP_UINT16_MAX)
  126. is_zip64 = 1;
  127. if (is_zip64) {
  128. fwrite(EOCD64_MAGIC, 1, 4, fp);
  129. _zip_write8(EOCD64LEN-12, fp);
  130. _zip_write2(45, fp);
  131. _zip_write2(45, fp);
  132. _zip_write4(0, fp);
  133. _zip_write4(0, fp);
  134. _zip_write8(survivors, fp);
  135. _zip_write8(survivors, fp);
  136. _zip_write8(size, fp);
  137. _zip_write8(offset, fp);
  138. fwrite(EOCD64LOC_MAGIC, 1, 4, fp);
  139. _zip_write4(0, fp);
  140. _zip_write8(offset+size, fp);
  141. _zip_write4(1, fp);
  142. }
  143. /* clearerr(fp); */
  144. fwrite(EOCD_MAGIC, 1, 4, fp);
  145. _zip_write4(0, fp);
  146. _zip_write2(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : (zip_uint16_t)survivors, fp);
  147. _zip_write2(survivors >= ZIP_UINT16_MAX ? ZIP_UINT16_MAX : (zip_uint16_t)survivors, fp);
  148. _zip_write4(size >= ZIP_UINT32_MAX ? ZIP_UINT32_MAX : (zip_uint32_t)size, fp);
  149. _zip_write4(offset >= ZIP_UINT32_MAX ? ZIP_UINT32_MAX : (zip_uint32_t)offset, fp);
  150. comment = za->comment_changed ? za->comment_changes : za->comment_orig;
  151. _zip_write2(comment ? comment->length : 0, fp);
  152. if (comment)
  153. fwrite(comment->raw, 1, comment->length, fp);
  154. if (ferror(fp)) {
  155. _zip_error_set(&za->error, ZIP_ER_WRITE, errno);
  156. return -1;
  157. }
  158. return (zip_int64_t)size;
  159. }
  160. struct zip_dirent *
  161. _zip_dirent_clone(const struct zip_dirent *sde)
  162. {
  163. struct zip_dirent *tde;
  164. if ((tde=(struct zip_dirent *)malloc(sizeof(*tde))) == NULL)
  165. return NULL;
  166. if (sde)
  167. memcpy(tde, sde, sizeof(*sde));
  168. else
  169. _zip_dirent_init(tde);
  170. tde->changed = 0;
  171. tde->cloned = 1;
  172. return tde;
  173. }
  174. void
  175. _zip_dirent_finalize(struct zip_dirent *zde)
  176. {
  177. if (!zde->cloned || zde->changed & ZIP_DIRENT_FILENAME)
  178. _zip_string_free(zde->filename);
  179. if (!zde->cloned || zde->changed & ZIP_DIRENT_EXTRA_FIELD)
  180. _zip_ef_free(zde->extra_fields);
  181. if (!zde->cloned || zde->changed & ZIP_DIRENT_COMMENT)
  182. _zip_string_free(zde->comment);
  183. }
  184. void
  185. _zip_dirent_free(struct zip_dirent *zde)
  186. {
  187. if (zde == NULL)
  188. return;
  189. _zip_dirent_finalize(zde);
  190. free(zde);
  191. }
  192. void
  193. _zip_dirent_init(struct zip_dirent *de)
  194. {
  195. de->changed = 0;
  196. de->local_extra_fields_read = 0;
  197. de->cloned = 0;
  198. de->version_madeby = 20 | (ZIP_OPSYS_DEFAULT << 8);
  199. de->version_needed = 20; /* 2.0 */
  200. de->bitflags = 0;
  201. de->comp_method = ZIP_CM_DEFAULT;
  202. de->last_mod = 0;
  203. de->crc = 0;
  204. de->comp_size = 0;
  205. de->uncomp_size = 0;
  206. de->filename = NULL;
  207. de->extra_fields = NULL;
  208. de->comment = NULL;
  209. de->disk_number = 0;
  210. de->int_attrib = 0;
  211. de->ext_attrib = ZIP_EXT_ATTRIB_DEFAULT;
  212. de->offset = 0;
  213. }
  214. int
  215. _zip_dirent_needs_zip64(const struct zip_dirent *de, zip_flags_t flags)
  216. {
  217. if (de->uncomp_size >= ZIP_UINT32_MAX || de->comp_size >= ZIP_UINT32_MAX
  218. || ((flags & ZIP_FL_CENTRAL) && de->offset >= ZIP_UINT32_MAX))
  219. return 1;
  220. return 0;
  221. }
  222. struct zip_dirent *
  223. _zip_dirent_new(void)
  224. {
  225. struct zip_dirent *de;
  226. if ((de=(struct zip_dirent *)malloc(sizeof(*de))) == NULL)
  227. return NULL;
  228. _zip_dirent_init(de);
  229. return de;
  230. }
  231. /* _zip_dirent_read(zde, fp, bufp, left, localp, error):
  232. Fills the zip directory entry zde.
  233. If bufp is non-NULL, data is taken from there and bufp is advanced
  234. by the amount of data used; otherwise data is read from fp as needed.
  235. if leftp is non-NULL, no more bytes than specified by it are used,
  236. and *leftp is reduced by the number of bytes used.
  237. If local != 0, it reads a local header instead of a central
  238. directory entry.
  239. Returns 0 if successful. On error, error is filled in and -1 is
  240. returned.
  241. TODO: leftp and file position undefined on error.
  242. */
  243. int
  244. _zip_dirent_read(struct zip_dirent *zde, FILE *fp,
  245. const unsigned char **bufp, zip_uint64_t *leftp, int local,
  246. struct zip_error *error)
  247. {
  248. unsigned char buf[CDENTRYSIZE];
  249. const unsigned char *cur;
  250. zip_uint16_t dostime, dosdate;
  251. zip_uint32_t size;
  252. zip_uint16_t filename_len, comment_len, ef_len;
  253. if (local)
  254. size = LENTRYSIZE;
  255. else
  256. size = CDENTRYSIZE;
  257. if (leftp && (*leftp < size)) {
  258. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  259. return -1;
  260. }
  261. if (bufp) {
  262. /* use data from buffer */
  263. cur = *bufp;
  264. }
  265. else {
  266. /* read entry from disk */
  267. if ((fread(buf, 1, size, fp)<size)) {
  268. _zip_error_set(error, ZIP_ER_READ, errno);
  269. return -1;
  270. }
  271. cur = buf;
  272. }
  273. if (memcmp(cur, (local ? LOCAL_MAGIC : CENTRAL_MAGIC), 4) != 0) {
  274. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  275. return -1;
  276. }
  277. cur += 4;
  278. /* convert buffercontents to zip_dirent */
  279. _zip_dirent_init(zde);
  280. if (!local)
  281. zde->version_madeby = _zip_read2(&cur);
  282. else
  283. zde->version_madeby = 0;
  284. zde->version_needed = _zip_read2(&cur);
  285. zde->bitflags = _zip_read2(&cur);
  286. zde->comp_method = _zip_read2(&cur);
  287. /* convert to time_t */
  288. dostime = _zip_read2(&cur);
  289. dosdate = _zip_read2(&cur);
  290. zde->last_mod = _zip_d2u_time(dostime, dosdate);
  291. zde->crc = _zip_read4(&cur);
  292. zde->comp_size = _zip_read4(&cur);
  293. zde->uncomp_size = _zip_read4(&cur);
  294. filename_len = _zip_read2(&cur);
  295. ef_len = _zip_read2(&cur);
  296. if (local) {
  297. comment_len = 0;
  298. zde->disk_number = 0;
  299. zde->int_attrib = 0;
  300. zde->ext_attrib = 0;
  301. zde->offset = 0;
  302. } else {
  303. comment_len = _zip_read2(&cur);
  304. zde->disk_number = _zip_read2(&cur);
  305. zde->int_attrib = _zip_read2(&cur);
  306. zde->ext_attrib = _zip_read4(&cur);
  307. zde->offset = _zip_read4(&cur);
  308. }
  309. zde->filename = NULL;
  310. zde->extra_fields = NULL;
  311. zde->comment = NULL;
  312. size += filename_len+ef_len+comment_len;
  313. if (leftp && (*leftp < size)) {
  314. _zip_error_set(error, ZIP_ER_INCONS, 0);
  315. return -1;
  316. }
  317. if (filename_len) {
  318. zde->filename = _zip_read_string(bufp ? &cur : NULL, fp, filename_len, 1, error);
  319. if (!zde->filename)
  320. return -1;
  321. if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
  322. if (_zip_guess_encoding(zde->filename, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) {
  323. _zip_error_set(error, ZIP_ER_INCONS, 0);
  324. return -1;
  325. }
  326. }
  327. }
  328. if (ef_len) {
  329. zip_uint8_t *ef = _zip_read_data(bufp ? &cur : NULL, fp, ef_len, 0, error);
  330. if (ef == NULL)
  331. return -1;
  332. if ((zde->extra_fields=_zip_ef_parse(ef, ef_len, local ? ZIP_EF_LOCAL : ZIP_EF_CENTRAL, error)) == NULL) {
  333. free(ef);
  334. return -1;
  335. }
  336. free(ef);
  337. if (local)
  338. zde->local_extra_fields_read = 1;
  339. }
  340. if (comment_len) {
  341. zde->comment = _zip_read_string(bufp ? &cur : NULL, fp, comment_len, 0, error);
  342. if (!zde->comment)
  343. return -1;
  344. if (zde->bitflags & ZIP_GPBF_ENCODING_UTF_8) {
  345. if (_zip_guess_encoding(zde->comment, ZIP_ENCODING_UTF8_KNOWN) == ZIP_ENCODING_ERROR) {
  346. _zip_error_set(error, ZIP_ER_INCONS, 0);
  347. return -1;
  348. }
  349. }
  350. }
  351. zde->filename = _zip_dirent_process_ef_utf_8(zde, ZIP_EF_UTF_8_NAME, zde->filename);
  352. zde->comment = _zip_dirent_process_ef_utf_8(zde, ZIP_EF_UTF_8_COMMENT, zde->comment);
  353. /* Zip64 */
  354. if (zde->uncomp_size == ZIP_UINT32_MAX || zde->comp_size == ZIP_UINT32_MAX || zde->offset == ZIP_UINT32_MAX) {
  355. zip_uint16_t got_len, needed_len;
  356. 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);
  357. /* TODO: if got_len == 0 && !ZIP64_EOCD: no error, 0xffffffff is valid value */
  358. if (ef == NULL)
  359. return -1;
  360. if (local)
  361. needed_len = 16;
  362. else
  363. needed_len = ((zde->uncomp_size == ZIP_UINT32_MAX) + (zde->comp_size == ZIP_UINT32_MAX) + (zde->offset == ZIP_UINT32_MAX)) * 8
  364. + (zde->disk_number == ZIP_UINT16_MAX) * 4;
  365. if (got_len != needed_len) {
  366. _zip_error_set(error, ZIP_ER_INCONS, 0);
  367. return -1;
  368. }
  369. if (zde->uncomp_size == ZIP_UINT32_MAX)
  370. zde->uncomp_size = _zip_read8(&ef);
  371. else if (local)
  372. ef += 8;
  373. if (zde->comp_size == ZIP_UINT32_MAX)
  374. zde->comp_size = _zip_read8(&ef);
  375. if (!local) {
  376. if (zde->offset == ZIP_UINT32_MAX)
  377. zde->offset = _zip_read8(&ef);
  378. if (zde->disk_number == ZIP_UINT16_MAX)
  379. zde->disk_number = _zip_read4(&ef);
  380. }
  381. }
  382. if (!local) {
  383. if (zde->offset > ZIP_OFF_MAX) {
  384. _zip_error_set(error, ZIP_ER_SEEK, EFBIG);
  385. return -1;
  386. }
  387. }
  388. zde->extra_fields = _zip_ef_remove_internal(zde->extra_fields);
  389. if (bufp)
  390. *bufp = cur;
  391. if (leftp)
  392. *leftp -= size;
  393. return 0;
  394. }
  395. static struct zip_string *
  396. _zip_dirent_process_ef_utf_8(const struct zip_dirent *de, zip_uint16_t id, struct zip_string *str)
  397. {
  398. zip_uint16_t ef_len;
  399. zip_uint32_t ef_crc;
  400. const zip_uint8_t *ef = _zip_ef_get_by_id(de->extra_fields, &ef_len, id, 0, ZIP_EF_BOTH, NULL);
  401. if (ef == NULL || ef_len < 5 || ef[0] != 1)
  402. return str;
  403. ef++;
  404. ef_crc = _zip_read4(&ef);
  405. if (_zip_string_crc32(str) == ef_crc) {
  406. struct zip_string *ef_str = _zip_string_new(ef, ef_len-5, ZIP_ENCODING_UTF8_KNOWN, NULL);
  407. if (ef_str != NULL) {
  408. _zip_string_free(str);
  409. str = ef_str;
  410. }
  411. }
  412. return str;
  413. }
  414. zip_int32_t
  415. _zip_dirent_size(FILE *f, zip_uint16_t flags, struct zip_error *error)
  416. {
  417. zip_int32_t size;
  418. int local = (flags & ZIP_EF_LOCAL);
  419. int i;
  420. unsigned char b[6];
  421. const unsigned char *p;
  422. size = local ? LENTRYSIZE : CDENTRYSIZE;
  423. if (fseek(f, local ? 26 : 28, SEEK_CUR) < 0) {
  424. _zip_error_set(error, ZIP_ER_SEEK, errno);
  425. return -1;
  426. }
  427. if (fread(b, (local ? 4 : 6), 1, f) != 1) {
  428. _zip_error_set(error, ZIP_ER_READ, errno);
  429. return -1;
  430. }
  431. p = b;
  432. for (i=0; i<(local ? 2 : 3); i++) {
  433. size += _zip_read2(&p);
  434. }
  435. return size;
  436. }
  437. /* _zip_dirent_torrent_normalize(de);
  438. Set values suitable for torrentzip.
  439. */
  440. void
  441. _zip_dirent_torrent_normalize(struct zip_dirent *de)
  442. {
  443. static struct tm torrenttime;
  444. static time_t last_mod = 0;
  445. if (last_mod == 0) {
  446. #ifdef HAVE_STRUCT_TM_TM_ZONE
  447. time_t now;
  448. struct tm *l;
  449. #endif
  450. torrenttime.tm_sec = 0;
  451. torrenttime.tm_min = 32;
  452. torrenttime.tm_hour = 23;
  453. torrenttime.tm_mday = 24;
  454. torrenttime.tm_mon = 11;
  455. torrenttime.tm_year = 96;
  456. torrenttime.tm_wday = 0;
  457. torrenttime.tm_yday = 0;
  458. torrenttime.tm_isdst = 0;
  459. #ifdef HAVE_STRUCT_TM_TM_ZONE
  460. time(&now);
  461. l = localtime(&now);
  462. torrenttime.tm_gmtoff = l->tm_gmtoff;
  463. torrenttime.tm_zone = l->tm_zone;
  464. #endif
  465. last_mod = mktime(&torrenttime);
  466. }
  467. de->version_madeby = 0;
  468. de->version_needed = 20; /* 2.0 */
  469. de->bitflags = 2; /* maximum compression */
  470. de->comp_method = ZIP_CM_DEFLATE;
  471. de->last_mod = last_mod;
  472. de->disk_number = 0;
  473. de->int_attrib = 0;
  474. de->ext_attrib = 0;
  475. _zip_ef_free(de->extra_fields);
  476. de->extra_fields = NULL;
  477. _zip_string_free(de->comment);
  478. de->comment = NULL;
  479. }
  480. /* _zip_dirent_write(zde, fp, flags, error):
  481. Writes zip directory entry zde to file fp.
  482. If flags & ZIP_EF_LOCAL, it writes a local header instead of a central
  483. directory entry. If flags & ZIP_EF_FORCE_ZIP64, a ZIP64 extra field is written, even if not needed.
  484. Returns 0 if successful, 1 if successful and wrote ZIP64 extra field. On error, error is filled in and -1 is
  485. returned.
  486. */
  487. int
  488. _zip_dirent_write(struct zip_dirent *de, FILE *fp, zip_flags_t flags, struct zip_error *error)
  489. {
  490. unsigned short dostime, dosdate;
  491. enum zip_encoding_type com_enc, name_enc;
  492. struct zip_extra_field *ef;
  493. zip_uint8_t ef_zip64[24], *ef_zip64_p;
  494. int is_zip64;
  495. int is_really_zip64;
  496. ef = NULL;
  497. is_zip64 = 0;
  498. fwrite((flags & ZIP_FL_LOCAL) ? LOCAL_MAGIC : CENTRAL_MAGIC, 1, 4, fp);
  499. name_enc = _zip_guess_encoding(de->filename, ZIP_ENCODING_UNKNOWN);
  500. com_enc = _zip_guess_encoding(de->comment, ZIP_ENCODING_UNKNOWN);
  501. if ((name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_ASCII) ||
  502. (name_enc == ZIP_ENCODING_ASCII && com_enc == ZIP_ENCODING_UTF8_KNOWN) ||
  503. (name_enc == ZIP_ENCODING_UTF8_KNOWN && com_enc == ZIP_ENCODING_UTF8_KNOWN))
  504. de->bitflags |= ZIP_GPBF_ENCODING_UTF_8;
  505. else {
  506. de->bitflags &= ~ZIP_GPBF_ENCODING_UTF_8;
  507. if (name_enc == ZIP_ENCODING_UTF8_KNOWN) {
  508. ef = _zip_ef_utf8(ZIP_EF_UTF_8_NAME, de->filename, error);
  509. if (ef == NULL)
  510. return -1;
  511. }
  512. if ((flags & ZIP_FL_LOCAL) == 0 && com_enc == ZIP_ENCODING_UTF8_KNOWN){
  513. struct zip_extra_field *ef2 = _zip_ef_utf8(ZIP_EF_UTF_8_COMMENT, de->comment, error);
  514. if (ef2 == NULL) {
  515. _zip_ef_free(ef);
  516. return -1;
  517. }
  518. ef2->next = ef;
  519. ef = ef2;
  520. }
  521. }
  522. ef_zip64_p = ef_zip64;
  523. if (flags & ZIP_FL_LOCAL) {
  524. if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX) {
  525. _zip_poke8(de->uncomp_size, &ef_zip64_p);
  526. _zip_poke8(de->comp_size, &ef_zip64_p);
  527. }
  528. }
  529. else {
  530. if ((flags & ZIP_FL_FORCE_ZIP64) || de->comp_size > ZIP_UINT32_MAX || de->uncomp_size > ZIP_UINT32_MAX || de->offset > ZIP_UINT32_MAX) {
  531. if (de->comp_size >= ZIP_UINT32_MAX)
  532. _zip_poke8(de->comp_size, &ef_zip64_p);
  533. if (de->uncomp_size >= ZIP_UINT32_MAX)
  534. _zip_poke8(de->uncomp_size, &ef_zip64_p);
  535. if (de->offset >= ZIP_UINT32_MAX)
  536. _zip_poke8(de->offset, &ef_zip64_p);
  537. }
  538. }
  539. if (ef_zip64_p != ef_zip64) {
  540. struct zip_extra_field *ef64 = _zip_ef_new(ZIP_EF_ZIP64, (zip_uint16_t)(ef_zip64_p-ef_zip64), ef_zip64, ZIP_EF_BOTH);
  541. ef64->next = ef;
  542. ef = ef64;
  543. is_zip64 = 1;
  544. }
  545. if ((flags & (ZIP_FL_LOCAL|ZIP_FL_FORCE_ZIP64)) == (ZIP_FL_LOCAL|ZIP_FL_FORCE_ZIP64))
  546. is_really_zip64 = _zip_dirent_needs_zip64(de, flags);
  547. else
  548. is_really_zip64 = is_zip64;
  549. if ((flags & ZIP_FL_LOCAL) == 0)
  550. _zip_write2(is_really_zip64 ? 45 : de->version_madeby, fp);
  551. _zip_write2(is_really_zip64 ? 45 : de->version_needed, fp);
  552. _zip_write2(de->bitflags&0xfff9, fp); /* clear compression method specific flags */
  553. _zip_write2((zip_uint16_t)de->comp_method, fp); /* TODO: can it be ZIP_CM_DEFAULT? */
  554. _zip_u2d_time(de->last_mod, &dostime, &dosdate);
  555. _zip_write2(dostime, fp);
  556. _zip_write2(dosdate, fp);
  557. _zip_write4(de->crc, fp);
  558. if (de->comp_size < ZIP_UINT32_MAX)
  559. _zip_write4((zip_uint32_t)de->comp_size, fp);
  560. else
  561. _zip_write4(ZIP_UINT32_MAX, fp);
  562. if (de->uncomp_size < ZIP_UINT32_MAX)
  563. _zip_write4((zip_uint32_t)de->uncomp_size, fp);
  564. else
  565. _zip_write4(ZIP_UINT32_MAX, fp);
  566. _zip_write2(_zip_string_length(de->filename), fp);
  567. _zip_write2(_zip_ef_size(de->extra_fields, flags) + _zip_ef_size(ef, ZIP_EF_BOTH), fp);
  568. if ((flags & ZIP_FL_LOCAL) == 0) {
  569. _zip_write2(_zip_string_length(de->comment), fp);
  570. _zip_write2((zip_uint16_t)de->disk_number, fp);
  571. _zip_write2(de->int_attrib, fp);
  572. _zip_write4(de->ext_attrib, fp);
  573. if (de->offset < ZIP_UINT32_MAX)
  574. _zip_write4((zip_uint32_t)de->offset, fp);
  575. else
  576. _zip_write4(ZIP_UINT32_MAX, fp);
  577. }
  578. if (de->filename)
  579. _zip_string_write(de->filename, fp);
  580. if (ef)
  581. _zip_ef_write(ef, ZIP_EF_BOTH, fp);
  582. if (de->extra_fields)
  583. _zip_ef_write(de->extra_fields, flags, fp);
  584. if ((flags & ZIP_FL_LOCAL) == 0) {
  585. if (de->comment)
  586. _zip_string_write(de->comment, fp);
  587. }
  588. _zip_ef_free(ef);
  589. if (ferror(fp)) {
  590. _zip_error_set(error, ZIP_ER_WRITE, errno);
  591. return -1;
  592. }
  593. return is_zip64;
  594. }
  595. static time_t
  596. _zip_d2u_time(zip_uint16_t dtime, zip_uint16_t ddate)
  597. {
  598. struct tm tm;
  599. memset(&tm, 0, sizeof(tm));
  600. /* let mktime decide if DST is in effect */
  601. tm.tm_isdst = -1;
  602. tm.tm_year = ((ddate>>9)&127) + 1980 - 1900;
  603. tm.tm_mon = ((ddate>>5)&15) - 1;
  604. tm.tm_mday = ddate&31;
  605. tm.tm_hour = (dtime>>11)&31;
  606. tm.tm_min = (dtime>>5)&63;
  607. tm.tm_sec = (dtime<<1)&62;
  608. return mktime(&tm);
  609. }
  610. static struct zip_extra_field *
  611. _zip_ef_utf8(zip_uint16_t id, struct zip_string *str, struct zip_error *error)
  612. {
  613. const zip_uint8_t *raw;
  614. zip_uint8_t *data, *p;
  615. zip_uint32_t len;
  616. struct zip_extra_field *ef;
  617. raw = _zip_string_get(str, &len, ZIP_FL_ENC_RAW, NULL);
  618. if (len+5 > ZIP_UINT16_MAX) {
  619. /* TODO: error */
  620. }
  621. if ((data=(zip_uint8_t *)malloc(len+5)) == NULL) {
  622. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  623. return NULL;
  624. }
  625. p = data;
  626. *(p++) = 1;
  627. _zip_poke4(_zip_string_crc32(str), &p);
  628. memcpy(p, raw, len);
  629. p += len;
  630. ef = _zip_ef_new(id, (zip_uint16_t)(p-data), data, ZIP_EF_BOTH);
  631. free(data);
  632. return ef;
  633. }
  634. struct zip_dirent *
  635. _zip_get_dirent(struct zip *za, zip_uint64_t idx, zip_flags_t flags, struct zip_error *error)
  636. {
  637. if (error == NULL)
  638. error = &za->error;
  639. if (idx >= za->nentry) {
  640. _zip_error_set(error, ZIP_ER_INVAL, 0);
  641. return NULL;
  642. }
  643. if ((flags & ZIP_FL_UNCHANGED) || za->entry[idx].changes == NULL) {
  644. if (za->entry[idx].orig == NULL) {
  645. _zip_error_set(error, ZIP_ER_INVAL, 0);
  646. return NULL;
  647. }
  648. if (za->entry[idx].deleted && (flags & ZIP_FL_UNCHANGED) == 0) {
  649. _zip_error_set(error, ZIP_ER_DELETED, 0);
  650. return NULL;
  651. }
  652. return za->entry[idx].orig;
  653. }
  654. else
  655. return za->entry[idx].changes;
  656. }
  657. zip_uint16_t
  658. _zip_read2(const zip_uint8_t **a)
  659. {
  660. zip_uint16_t ret;
  661. ret = (zip_uint16_t)((*a)[0]+((*a)[1]<<8));
  662. *a += 2;
  663. return ret;
  664. }
  665. zip_uint32_t
  666. _zip_read4(const zip_uint8_t **a)
  667. {
  668. zip_uint32_t ret;
  669. ret = ((((((zip_uint32_t)(*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0];
  670. *a += 4;
  671. return ret;
  672. }
  673. zip_uint64_t
  674. _zip_read8(const zip_uint8_t **a)
  675. {
  676. zip_uint64_t x, y;
  677. x = ((((((zip_uint64_t)(*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0];
  678. *a += 4;
  679. y = ((((((zip_uint64_t)(*a)[3]<<8)+(*a)[2])<<8)+(*a)[1])<<8)+(*a)[0];
  680. *a += 4;
  681. return x+(y<<32);
  682. }
  683. zip_uint8_t *
  684. _zip_read_data(const zip_uint8_t **buf, FILE *fp, size_t len, int nulp, struct zip_error *error)
  685. {
  686. zip_uint8_t *r;
  687. if (len == 0 && nulp == 0)
  688. return NULL;
  689. r = (zip_uint8_t *)malloc(nulp ? len+1 : len);
  690. if (!r) {
  691. _zip_error_set(error, ZIP_ER_MEMORY, 0);
  692. return NULL;
  693. }
  694. if (buf) {
  695. memcpy(r, *buf, len);
  696. *buf += len;
  697. }
  698. else {
  699. if (fread(r, 1, len, fp)<len) {
  700. free(r);
  701. if (ferror(fp))
  702. _zip_error_set(error, ZIP_ER_READ, errno);
  703. else
  704. _zip_error_set(error, ZIP_ER_INCONS, 0);
  705. return NULL;
  706. }
  707. }
  708. if (nulp) {
  709. zip_uint8_t *o;
  710. /* replace any in-string NUL characters with spaces */
  711. r[len] = 0;
  712. for (o=r; o<r+len; o++)
  713. if (*o == '\0')
  714. *o = ' ';
  715. }
  716. return r;
  717. }
  718. static struct zip_string *
  719. _zip_read_string(const zip_uint8_t **buf, FILE *fp, zip_uint16_t len, int nulp, struct zip_error *error)
  720. {
  721. zip_uint8_t *raw;
  722. struct zip_string *s;
  723. if ((raw=_zip_read_data(buf, fp, len, nulp, error)) == NULL)
  724. return NULL;
  725. s = _zip_string_new(raw, len, ZIP_FL_ENC_GUESS, error);
  726. free(raw);
  727. return s;
  728. }
  729. void
  730. _zip_poke4(zip_uint32_t i, zip_uint8_t **p)
  731. {
  732. *((*p)++) = i&0xff;
  733. *((*p)++) = (i>>8)&0xff;
  734. *((*p)++) = (i>>16)&0xff;
  735. *((*p)++) = (i>>24)&0xff;
  736. }
  737. void
  738. _zip_poke8(zip_uint64_t i, zip_uint8_t **p)
  739. {
  740. *((*p)++) = i&0xff;
  741. *((*p)++) = (i>>8)&0xff;
  742. *((*p)++) = (i>>16)&0xff;
  743. *((*p)++) = (i>>24)&0xff;
  744. *((*p)++) = (i>>32)&0xff;
  745. *((*p)++) = (i>>40)&0xff;
  746. *((*p)++) = (i>>48)&0xff;
  747. *((*p)++) = (i>>56)&0xff;
  748. }
  749. void
  750. _zip_write2(zip_uint16_t i, FILE *fp)
  751. {
  752. putc(i&0xff, fp);
  753. putc((i>>8)&0xff, fp);
  754. return;
  755. }
  756. void
  757. _zip_write4(zip_uint32_t i, FILE *fp)
  758. {
  759. putc(i&0xff, fp);
  760. putc((i>>8)&0xff, fp);
  761. putc((i>>16)&0xff, fp);
  762. putc((i>>24)&0xff, fp);
  763. return;
  764. }
  765. void
  766. _zip_write8(zip_uint64_t i, FILE *fp)
  767. {
  768. putc(i&0xff, fp);
  769. putc((i>>8)&0xff, fp);
  770. putc((i>>16)&0xff, fp);
  771. putc((i>>24)&0xff, fp);
  772. putc((i>>32)&0xff, fp);
  773. putc((i>>40)&0xff, fp);
  774. putc((i>>48)&0xff, fp);
  775. putc((i>>56)&0xff, fp);
  776. return;
  777. }
  778. void
  779. _zip_u2d_time(time_t time, zip_uint16_t *dtime, zip_uint16_t *ddate)
  780. {
  781. struct tm *tm;
  782. tm = localtime(&time);
  783. *ddate = (zip_uint16_t)(((tm->tm_year+1900-1980)<<9) + ((tm->tm_mon+1)<<5) + tm->tm_mday);
  784. *dtime = (zip_uint16_t)(((tm->tm_hour)<<11) + ((tm->tm_min)<<5) + ((tm->tm_sec)>>1));
  785. return;
  786. }