zip_open.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. zip_open.c -- open zip archive by name
  3. Copyright (C) 1999-2012 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 <sys/stat.h>
  31. #include <errno.h>
  32. #include <limits.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include "zipint.h"
  37. static void set_error(int *, const struct zip_error *, int);
  38. static struct zip *_zip_allocate_new(const char *, unsigned int, int *);
  39. static zip_int64_t _zip_checkcons(FILE *, struct zip_cdir *, struct zip_error *);
  40. static void _zip_check_torrentzip(struct zip *, const struct zip_cdir *);
  41. static struct zip_cdir *_zip_find_central_dir(FILE *, unsigned int, int *, off_t);
  42. static int _zip_file_exists(const char *, unsigned int, int *);
  43. static int _zip_headercomp(const struct zip_dirent *, const struct zip_dirent *);
  44. static unsigned char *_zip_memmem(const unsigned char *, size_t,
  45. const unsigned char *, size_t);
  46. static struct zip_cdir *_zip_readcdir(FILE *, off_t, unsigned char *, const unsigned char *,
  47. size_t, unsigned int, struct zip_error *);
  48. static struct zip_cdir *_zip_read_eocd(const unsigned char *, const unsigned char *, off_t,
  49. size_t, unsigned int, struct zip_error *);
  50. static struct zip_cdir *_zip_read_eocd64(FILE *, const unsigned char *, const unsigned char *,
  51. off_t, size_t, unsigned int, struct zip_error *);
  52. ZIP_EXTERN struct zip *
  53. zip_open(const char *fn, int _flags, int *zep)
  54. {
  55. FILE *fp;
  56. unsigned int flags;
  57. if (_flags < 0) {
  58. if (zep)
  59. *zep = ZIP_ER_INVAL;
  60. return NULL;
  61. }
  62. flags = (unsigned int)_flags;
  63. switch (_zip_file_exists(fn, flags, zep)) {
  64. case -1:
  65. return NULL;
  66. case 0:
  67. return _zip_allocate_new(fn, flags, zep);
  68. default:
  69. if (flags & ZIP_TRUNCATE) {
  70. FILE *f;
  71. if ((f = fopen(fn, "rb")) == NULL) {
  72. set_error(zep, NULL, ZIP_ER_OPEN);
  73. return NULL;
  74. }
  75. fclose(f);
  76. return _zip_allocate_new(fn, flags, zep);
  77. }
  78. break;
  79. }
  80. if ((fp=fopen(fn, "rb")) == NULL) {
  81. set_error(zep, NULL, ZIP_ER_OPEN);
  82. return NULL;
  83. }
  84. return _zip_open(fn, fp, flags, zep);
  85. }
  86. ZIP_EXTERN int
  87. zip_archive_set_tempdir(struct zip *za, const char *tempdir)
  88. {
  89. char *new_tempdir;
  90. if (tempdir) {
  91. if ((new_tempdir = strdup(tempdir)) == NULL) {
  92. _zip_error_set(&za->error, ZIP_ER_MEMORY, errno);
  93. return -1;
  94. }
  95. }
  96. else
  97. new_tempdir = NULL;
  98. free(za->tempdir);
  99. za->tempdir = new_tempdir;
  100. return 0;
  101. }
  102. struct zip *
  103. _zip_open(const char *fn, FILE *fp, unsigned int flags, int *zep)
  104. {
  105. struct zip *za;
  106. struct zip_cdir *cdir;
  107. off_t len;
  108. if (fseeko(fp, 0, SEEK_END) < 0) {
  109. *zep = ZIP_ER_SEEK;
  110. return NULL;
  111. }
  112. len = ftello(fp);
  113. /* treat empty files as empty archives */
  114. if (len == 0) {
  115. if ((za=_zip_allocate_new(fn, flags, zep)) == NULL)
  116. fclose(fp);
  117. else
  118. za->zp = fp;
  119. return za;
  120. }
  121. cdir = _zip_find_central_dir(fp, flags, zep, len);
  122. if (cdir == NULL) {
  123. fclose(fp);
  124. return NULL;
  125. }
  126. if ((za=_zip_allocate_new(fn, flags, zep)) == NULL) {
  127. _zip_cdir_free(cdir);
  128. fclose(fp);
  129. return NULL;
  130. }
  131. za->entry = cdir->entry;
  132. za->nentry = cdir->nentry;
  133. za->nentry_alloc = cdir->nentry_alloc;
  134. za->comment_orig = cdir->comment;
  135. za->zp = fp;
  136. _zip_check_torrentzip(za, cdir);
  137. za->ch_flags = za->flags;
  138. free(cdir);
  139. return za;
  140. }
  141. static void
  142. set_error(int *zep, const struct zip_error *err, int ze)
  143. {
  144. int se;
  145. if (err) {
  146. _zip_error_get(err, &ze, &se);
  147. if (zip_error_get_sys_type(ze) == ZIP_ET_SYS)
  148. errno = se;
  149. }
  150. if (zep)
  151. *zep = ze;
  152. }
  153. /* _zip_readcdir:
  154. tries to find a valid end-of-central-directory at the beginning of
  155. buf, and then the corresponding central directory entries.
  156. Returns a struct zip_cdir which contains the central directory
  157. entries, or NULL if unsuccessful. */
  158. static struct zip_cdir *
  159. _zip_readcdir(FILE *fp, off_t buf_offset, unsigned char *buf, const unsigned char *eocd, size_t buflen,
  160. unsigned int flags, struct zip_error *error)
  161. {
  162. struct zip_cdir *cd;
  163. const unsigned char *cdp;
  164. const unsigned char **bufp;
  165. zip_int64_t tail_len, comment_len;
  166. zip_uint64_t i, left;
  167. tail_len = buf + buflen - eocd - EOCDLEN;
  168. if (tail_len < 0) {
  169. /* not enough bytes left for comment */
  170. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  171. return NULL;
  172. }
  173. /* check for end-of-central-dir magic */
  174. if (memcmp(eocd, EOCD_MAGIC, 4) != 0) {
  175. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  176. return NULL;
  177. }
  178. if (memcmp(eocd+4, "\0\0\0\0", 4) != 0) {
  179. _zip_error_set(error, ZIP_ER_MULTIDISK, 0);
  180. return NULL;
  181. }
  182. if (eocd-EOCD64LOCLEN >= buf && memcmp(eocd-EOCD64LOCLEN, EOCD64LOC_MAGIC, 4) == 0)
  183. cd = _zip_read_eocd64(fp, eocd-EOCD64LOCLEN, buf, buf_offset, buflen, flags, error);
  184. else
  185. cd = _zip_read_eocd(eocd, buf, buf_offset, buflen, flags, error);
  186. if (cd == NULL)
  187. return NULL;
  188. cdp = eocd + 20;
  189. comment_len = _zip_read2(&cdp);
  190. if ((zip_uint64_t)cd->offset+(zip_uint64_t)cd->size > (zip_uint64_t)buf_offset + (zip_uint64_t)(eocd-buf)) {
  191. /* cdir spans past EOCD record */
  192. _zip_error_set(error, ZIP_ER_INCONS, 0);
  193. _zip_cdir_free(cd);
  194. return NULL;
  195. }
  196. if (tail_len < comment_len || ((flags & ZIP_CHECKCONS) && tail_len != comment_len)) {
  197. _zip_error_set(error, ZIP_ER_INCONS, 0);
  198. _zip_cdir_free(cd);
  199. return NULL;
  200. }
  201. if (comment_len) {
  202. if ((cd->comment=_zip_string_new(eocd+EOCDLEN, (zip_uint16_t)comment_len, ZIP_FL_ENC_GUESS, error)) == NULL) {
  203. _zip_cdir_free(cd);
  204. return NULL;
  205. }
  206. }
  207. if (cd->offset >= buf_offset) {
  208. /* if buffer already read in, use it */
  209. cdp = buf + (cd->offset - buf_offset);
  210. bufp = &cdp;
  211. }
  212. else {
  213. /* go to start of cdir and read it entry by entry */
  214. bufp = NULL;
  215. clearerr(fp);
  216. fseeko(fp, cd->offset, SEEK_SET);
  217. /* possible consistency check: cd->offset =
  218. len-(cd->size+cd->comment_len+EOCDLEN) ? */
  219. if (ferror(fp) || (ftello(fp) != cd->offset)) {
  220. /* seek error or offset of cdir wrong */
  221. if (ferror(fp))
  222. _zip_error_set(error, ZIP_ER_SEEK, errno);
  223. else
  224. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  225. _zip_cdir_free(cd);
  226. return NULL;
  227. }
  228. }
  229. left = (zip_uint64_t)cd->size;
  230. i=0;
  231. while (i<cd->nentry && left > 0) {
  232. if ((cd->entry[i].orig=_zip_dirent_new()) == NULL
  233. || (_zip_dirent_read(cd->entry[i].orig, fp, bufp, &left, 0, error)) < 0) {
  234. _zip_cdir_free(cd);
  235. return NULL;
  236. }
  237. i++;
  238. }
  239. if (i != cd->nentry || ((flags & ZIP_CHECKCONS) && left != 0)) {
  240. _zip_error_set(error, ZIP_ER_INCONS, 0);
  241. _zip_cdir_free(cd);
  242. return NULL;
  243. }
  244. return cd;
  245. }
  246. /* _zip_checkcons:
  247. Checks the consistency of the central directory by comparing central
  248. directory entries with local headers and checking for plausible
  249. file and header offsets. Returns -1 if not plausible, else the
  250. difference between the lowest and the highest fileposition reached */
  251. static zip_int64_t
  252. _zip_checkcons(FILE *fp, struct zip_cdir *cd, struct zip_error *error)
  253. {
  254. zip_uint64_t i;
  255. zip_uint64_t min, max, j;
  256. struct zip_dirent temp;
  257. if (cd->nentry) {
  258. max = cd->entry[0].orig->offset;
  259. min = cd->entry[0].orig->offset;
  260. }
  261. else
  262. min = max = 0;
  263. for (i=0; i<cd->nentry; i++) {
  264. if (cd->entry[i].orig->offset < min)
  265. min = cd->entry[i].orig->offset;
  266. if (min > (zip_uint64_t)cd->offset) {
  267. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  268. return -1;
  269. }
  270. j = cd->entry[i].orig->offset + cd->entry[i].orig->comp_size
  271. + _zip_string_length(cd->entry[i].orig->filename) + LENTRYSIZE;
  272. if (j > max)
  273. max = j;
  274. if (max > (zip_uint64_t)cd->offset) {
  275. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  276. return -1;
  277. }
  278. if (fseeko(fp, (off_t)cd->entry[i].orig->offset, SEEK_SET) != 0) {
  279. _zip_error_set(error, ZIP_ER_SEEK, errno);
  280. return -1;
  281. }
  282. if (_zip_dirent_read(&temp, fp, NULL, NULL, 1, error) == -1)
  283. return -1;
  284. if (_zip_headercomp(cd->entry[i].orig, &temp) != 0) {
  285. _zip_error_set(error, ZIP_ER_INCONS, 0);
  286. _zip_dirent_finalize(&temp);
  287. return -1;
  288. }
  289. cd->entry[i].orig->extra_fields = _zip_ef_merge(cd->entry[i].orig->extra_fields, temp.extra_fields);
  290. cd->entry[i].orig->local_extra_fields_read = 1;
  291. temp.extra_fields = NULL;
  292. _zip_dirent_finalize(&temp);
  293. }
  294. return (max-min) < ZIP_INT64_MAX ? (zip_int64_t)(max-min) : ZIP_INT64_MAX;
  295. }
  296. /* _zip_check_torrentzip:
  297. check whether ZA has a valid TORRENTZIP comment, i.e. is torrentzipped */
  298. static void
  299. _zip_check_torrentzip(struct zip *za, const struct zip_cdir *cdir)
  300. {
  301. uLong crc_got, crc_should;
  302. char buf[8+1];
  303. char *end;
  304. if (za->zp == NULL || cdir == NULL)
  305. return;
  306. if (_zip_string_length(cdir->comment) != TORRENT_SIG_LEN+8
  307. || strncmp((const char *)cdir->comment->raw, TORRENT_SIG, TORRENT_SIG_LEN) != 0)
  308. return;
  309. memcpy(buf, cdir->comment->raw+TORRENT_SIG_LEN, 8);
  310. buf[8] = '\0';
  311. errno = 0;
  312. crc_should = strtoul(buf, &end, 16);
  313. if ((crc_should == UINT_MAX && errno != 0) || (end && *end))
  314. return;
  315. if (_zip_filerange_crc(za->zp, cdir->offset, cdir->size, &crc_got, NULL) < 0)
  316. return;
  317. if (crc_got == crc_should)
  318. za->flags |= ZIP_AFL_TORRENT;
  319. }
  320. /* _zip_headercomp:
  321. compares a central directory entry and a local file header
  322. Return 0 if they are consistent, -1 if not. */
  323. static int
  324. _zip_headercomp(const struct zip_dirent *central, const struct zip_dirent *local)
  325. {
  326. if ((central->version_needed != local->version_needed)
  327. #if 0
  328. /* some zip-files have different values in local
  329. and global headers for the bitflags */
  330. || (central->bitflags != local->bitflags)
  331. #endif
  332. || (central->comp_method != local->comp_method)
  333. || (central->last_mod != local->last_mod)
  334. || !_zip_string_equal(central->filename, local->filename))
  335. return -1;
  336. if ((central->crc != local->crc) || (central->comp_size != local->comp_size)
  337. || (central->uncomp_size != local->uncomp_size)) {
  338. /* InfoZip stores valid values in local header even when data descriptor is used.
  339. This is in violation of the appnote. */
  340. if (((local->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) == 0
  341. || local->crc != 0 || local->comp_size != 0 || local->uncomp_size != 0))
  342. return -1;
  343. }
  344. return 0;
  345. }
  346. static struct zip *
  347. _zip_allocate_new(const char *fn, unsigned int flags, int *zep)
  348. {
  349. struct zip *za;
  350. struct zip_error error;
  351. if ((za=_zip_new(&error)) == NULL) {
  352. set_error(zep, &error, 0);
  353. return NULL;
  354. }
  355. if (fn == NULL)
  356. za->zn = NULL;
  357. else {
  358. za->zn = strdup(fn);
  359. if (!za->zn) {
  360. zip_discard(za);
  361. set_error(zep, NULL, ZIP_ER_MEMORY);
  362. return NULL;
  363. }
  364. }
  365. za->open_flags = flags;
  366. return za;
  367. }
  368. static int
  369. _zip_file_exists(const char *fn, unsigned int flags, int *zep)
  370. {
  371. struct stat st;
  372. if (fn == NULL) {
  373. set_error(zep, NULL, ZIP_ER_INVAL);
  374. return -1;
  375. }
  376. if (stat(fn, &st) != 0) {
  377. if (flags & ZIP_CREATE)
  378. return 0;
  379. else {
  380. set_error(zep, NULL, ZIP_ER_OPEN);
  381. return -1;
  382. }
  383. }
  384. else if ((flags & ZIP_EXCL)) {
  385. set_error(zep, NULL, ZIP_ER_EXISTS);
  386. return -1;
  387. }
  388. /* ZIP_CREATE gets ignored if file exists and not ZIP_EXCL,
  389. just like open() */
  390. return 1;
  391. }
  392. static struct zip_cdir *
  393. _zip_find_central_dir(FILE *fp, unsigned int flags, int *zep, off_t len)
  394. {
  395. struct zip_cdir *cdir, *cdirnew;
  396. unsigned char *buf, *match;
  397. off_t buf_offset;
  398. size_t buflen;
  399. zip_int64_t a, i;
  400. zip_int64_t best;
  401. struct zip_error zerr;
  402. if (len < (off_t)EOCDLEN) {
  403. set_error(zep, NULL, ZIP_ER_NOZIP);
  404. return NULL;
  405. }
  406. i = fseeko(fp, -(len < CDBUFSIZE ? len : CDBUFSIZE), SEEK_END);
  407. if (i == -1 && errno != EFBIG) {
  408. /* seek before start of file on my machine */
  409. set_error(zep, NULL, ZIP_ER_SEEK);
  410. return NULL;
  411. }
  412. buf_offset = ftello(fp);
  413. /* 64k is too much for stack */
  414. if ((buf=(unsigned char *)malloc(CDBUFSIZE)) == NULL) {
  415. set_error(zep, NULL, ZIP_ER_MEMORY);
  416. return NULL;
  417. }
  418. clearerr(fp);
  419. buflen = fread(buf, 1, CDBUFSIZE, fp);
  420. if (ferror(fp)) {
  421. set_error(zep, NULL, ZIP_ER_READ);
  422. free(buf);
  423. return NULL;
  424. }
  425. best = -1;
  426. cdir = NULL;
  427. match = buf+ (buflen < CDBUFSIZE ? 0 : EOCD64LOCLEN);
  428. _zip_error_set(&zerr, ZIP_ER_NOZIP, 0);
  429. while ((match=_zip_memmem(match, buflen-(size_t)(match-buf)-(EOCDLEN-4),
  430. (const unsigned char *)EOCD_MAGIC, 4))!=NULL) {
  431. /* found match -- check, if good */
  432. /* to avoid finding the same match all over again */
  433. match++;
  434. if ((cdirnew=_zip_readcdir(fp, buf_offset, buf, match-1, buflen, flags,
  435. &zerr)) == NULL)
  436. continue;
  437. if (cdir) {
  438. if (best <= 0)
  439. best = _zip_checkcons(fp, cdir, &zerr);
  440. a = _zip_checkcons(fp, cdirnew, &zerr);
  441. if (best < a) {
  442. _zip_cdir_free(cdir);
  443. cdir = cdirnew;
  444. best = a;
  445. }
  446. else
  447. _zip_cdir_free(cdirnew);
  448. }
  449. else {
  450. cdir = cdirnew;
  451. if (flags & ZIP_CHECKCONS)
  452. best = _zip_checkcons(fp, cdir, &zerr);
  453. else
  454. best = 0;
  455. }
  456. cdirnew = NULL;
  457. }
  458. free(buf);
  459. if (best < 0) {
  460. set_error(zep, &zerr, 0);
  461. _zip_cdir_free(cdir);
  462. return NULL;
  463. }
  464. return cdir;
  465. }
  466. static unsigned char *
  467. _zip_memmem(const unsigned char *big, size_t biglen, const unsigned char *little, size_t littlelen)
  468. {
  469. const unsigned char *p;
  470. if ((biglen < littlelen) || (littlelen == 0))
  471. return NULL;
  472. p = big-1;
  473. while ((p=(const unsigned char *)
  474. memchr(p+1, little[0], (size_t)(big-(p+1))+(size_t)(biglen-littlelen)+1)) != NULL) {
  475. if (memcmp(p+1, little+1, littlelen-1)==0)
  476. return (unsigned char *)p;
  477. }
  478. return NULL;
  479. }
  480. static struct zip_cdir *
  481. _zip_read_eocd(const unsigned char *eocd, const unsigned char *buf, off_t buf_offset, size_t buflen,
  482. unsigned int flags, struct zip_error *error)
  483. {
  484. struct zip_cdir *cd;
  485. const unsigned char *cdp;
  486. zip_uint64_t i, nentry, size, offset;
  487. if (eocd+EOCDLEN > buf+buflen) {
  488. _zip_error_set(error, ZIP_ER_INCONS, 0);
  489. return NULL;
  490. }
  491. cdp = eocd + 8;
  492. /* number of cdir-entries on this disk */
  493. i = _zip_read2(&cdp);
  494. /* number of cdir-entries */
  495. nentry = _zip_read2(&cdp);
  496. if (nentry != i) {
  497. _zip_error_set(error, ZIP_ER_NOZIP, 0);
  498. return NULL;
  499. }
  500. size = _zip_read4(&cdp);
  501. offset = _zip_read4(&cdp);
  502. if (size > ZIP_OFF_MAX || offset > ZIP_OFF_MAX || offset+size > ZIP_OFF_MAX) {
  503. _zip_error_set(error, ZIP_ER_SEEK, EFBIG);
  504. return NULL;
  505. }
  506. if (offset+size > (zip_uint64_t)(buf_offset + (eocd-buf))) {
  507. /* cdir spans past EOCD record */
  508. _zip_error_set(error, ZIP_ER_INCONS, 0);
  509. return NULL;
  510. }
  511. if ((flags & ZIP_CHECKCONS) && offset+size != (zip_uint64_t)(buf_offset + (eocd-buf))) {
  512. _zip_error_set(error, ZIP_ER_INCONS, 0);
  513. return NULL;
  514. }
  515. if ((cd=_zip_cdir_new(nentry, error)) == NULL)
  516. return NULL;
  517. cd->size = (off_t)size;
  518. cd->offset = (off_t)offset;
  519. return cd;
  520. }
  521. static struct zip_cdir *
  522. _zip_read_eocd64(FILE *f, const zip_uint8_t *eocd64loc, const zip_uint8_t *buf,
  523. off_t buf_offset, size_t buflen, unsigned int flags, struct zip_error *error)
  524. {
  525. struct zip_cdir *cd;
  526. zip_uint64_t offset;
  527. const zip_uint8_t *cdp;
  528. zip_uint8_t eocd[EOCD64LEN];
  529. zip_uint64_t eocd_offset;
  530. zip_uint64_t size, nentry, i;
  531. cdp = eocd64loc+8;
  532. eocd_offset = _zip_read8(&cdp);
  533. if (eocd_offset > ZIP_OFF_MAX || eocd_offset + EOCD64LEN > ZIP_OFF_MAX) {
  534. _zip_error_set(error, ZIP_ER_SEEK, EFBIG);
  535. return NULL;
  536. }
  537. if (eocd64loc < buf || (off_t)eocd_offset+EOCD64LEN > (buf_offset+(eocd64loc-buf))) {
  538. _zip_error_set(error, ZIP_ER_INCONS, 0);
  539. return NULL;
  540. }
  541. if ((off_t)eocd_offset >= buf_offset && (off_t)eocd_offset+EOCD64LEN <= buf_offset+(ssize_t)buflen)
  542. cdp = buf+((off_t)eocd_offset-buf_offset);
  543. else {
  544. if (fseeko(f, (off_t)eocd_offset, SEEK_SET) != 0) {
  545. _zip_error_set(error, ZIP_ER_SEEK, errno);
  546. return NULL;
  547. }
  548. clearerr(f);
  549. if (fread(eocd, 1, EOCD64LEN, f) < EOCD64LEN) {
  550. _zip_error_set(error, ZIP_ER_READ, errno);
  551. return NULL;
  552. }
  553. if (ferror(f)) {
  554. _zip_error_set(error, ZIP_ER_READ, errno);
  555. return NULL;
  556. }
  557. cdp = eocd;
  558. }
  559. if (memcmp(cdp, EOCD64_MAGIC, 4) != 0) {
  560. _zip_error_set(error, ZIP_ER_INCONS, 0);
  561. return NULL;
  562. }
  563. cdp += 4;
  564. size = _zip_read8(&cdp);
  565. if ((flags & ZIP_CHECKCONS) && size+eocd_offset+12 != (zip_uint64_t)(buf_offset+(eocd64loc-buf))) {
  566. _zip_error_set(error, ZIP_ER_INCONS, 0);
  567. return NULL;
  568. }
  569. cdp += 4; /* skip version made by/needed */
  570. cdp += 8; /* skip num disks */
  571. nentry = _zip_read8(&cdp);
  572. i = _zip_read8(&cdp);
  573. if (nentry != i) {
  574. _zip_error_set(error, ZIP_ER_MULTIDISK, 0);
  575. return NULL;
  576. }
  577. size = _zip_read8(&cdp);
  578. offset = _zip_read8(&cdp);
  579. if (size > ZIP_OFF_MAX || offset > ZIP_OFF_MAX || offset+size > ZIP_OFF_MAX) {
  580. _zip_error_set(error, ZIP_ER_SEEK, EFBIG);
  581. return NULL;
  582. }
  583. if ((flags & ZIP_CHECKCONS) && offset+size != eocd_offset) {
  584. _zip_error_set(error, ZIP_ER_INCONS, 0);
  585. return NULL;
  586. }
  587. if ((cd=_zip_cdir_new(nentry, error)) == NULL)
  588. return NULL;
  589. cd->size = (off_t)size;
  590. cd->offset = (off_t)offset;
  591. return cd;
  592. }