mz_compat.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /* mz_compat.c -- Backwards compatible interface for older versions
  2. Version 2.8.9, July 4, 2019
  3. part of the MiniZip project
  4. Copyright (C) 2010-2019 Nathan Moinvaziri
  5. https://github.com/nmoinvaz/minizip
  6. Copyright (C) 1998-2010 Gilles Vollant
  7. https://www.winimage.com/zLibDll/minizip.html
  8. This program is distributed under the terms of the same license as zlib.
  9. See the accompanying LICENSE file for the full text of the license.
  10. */
  11. #include "mz.h"
  12. #include "mz_os.h"
  13. #include "mz_strm.h"
  14. #include "mz_strm_mem.h"
  15. #include "mz_strm_os.h"
  16. #include "mz_strm_zlib.h"
  17. #include "mz_zip.h"
  18. #include <stdio.h> /* SEEK */
  19. #include "mz_compat.h"
  20. /***************************************************************************/
  21. typedef struct mz_compat_s {
  22. void *stream;
  23. void *handle;
  24. uint64_t entry_index;
  25. int64_t entry_pos;
  26. int64_t total_out;
  27. } mz_compat;
  28. /***************************************************************************/
  29. static int32_t zipConvertAppendToStreamMode(int append)
  30. {
  31. int32_t mode = MZ_OPEN_MODE_WRITE;
  32. switch (append)
  33. {
  34. case APPEND_STATUS_CREATE:
  35. mode |= MZ_OPEN_MODE_CREATE;
  36. break;
  37. case APPEND_STATUS_CREATEAFTER:
  38. mode |= MZ_OPEN_MODE_CREATE | MZ_OPEN_MODE_APPEND;
  39. break;
  40. case APPEND_STATUS_ADDINZIP:
  41. mode |= MZ_OPEN_MODE_READ | MZ_OPEN_MODE_APPEND;
  42. break;
  43. }
  44. return mode;
  45. }
  46. zipFile zipOpen(const char *path, int append)
  47. {
  48. zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
  49. return zipOpen2(path, append, NULL, &pzlib);
  50. }
  51. zipFile zipOpen64(const void *path, int append)
  52. {
  53. zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
  54. return zipOpen2(path, append, NULL, &pzlib);
  55. }
  56. zipFile zipOpen2(const char *path, int append, const char **globalcomment,
  57. zlib_filefunc_def *pzlib_filefunc_def)
  58. {
  59. return zipOpen2_64(path, append, globalcomment, pzlib_filefunc_def);
  60. }
  61. zipFile zipOpen2_64(const void *path, int append, const char **globalcomment,
  62. zlib_filefunc64_def *pzlib_filefunc_def)
  63. {
  64. zipFile zip = NULL;
  65. int32_t mode = zipConvertAppendToStreamMode(append);
  66. void *stream = NULL;
  67. if (pzlib_filefunc_def)
  68. {
  69. if (mz_stream_create(&stream, (mz_stream_vtbl *)*pzlib_filefunc_def) == NULL)
  70. return NULL;
  71. }
  72. else
  73. {
  74. if (mz_stream_os_create(&stream) == NULL)
  75. return NULL;
  76. }
  77. if (mz_stream_open(stream, path, mode) != MZ_OK)
  78. {
  79. mz_stream_delete(&stream);
  80. return NULL;
  81. }
  82. zip = zipOpen_MZ(stream, append, globalcomment);
  83. if (zip == NULL)
  84. {
  85. mz_stream_delete(&stream);
  86. return NULL;
  87. }
  88. return zip;
  89. }
  90. zipFile zipOpen_MZ(void *stream, int append, const char **globalcomment)
  91. {
  92. mz_compat *compat = NULL;
  93. int32_t err = MZ_OK;
  94. int32_t mode = zipConvertAppendToStreamMode(append);
  95. void *handle = NULL;
  96. mz_zip_create(&handle);
  97. err = mz_zip_open(handle, stream, mode);
  98. if (err != MZ_OK)
  99. {
  100. mz_zip_delete(&handle);
  101. return NULL;
  102. }
  103. if (globalcomment != NULL)
  104. mz_zip_get_comment(handle, globalcomment);
  105. compat = (mz_compat *)MZ_ALLOC(sizeof(mz_compat));
  106. if (compat != NULL)
  107. {
  108. compat->handle = handle;
  109. compat->stream = stream;
  110. }
  111. else
  112. {
  113. mz_zip_delete(&handle);
  114. }
  115. return (zipFile)compat;
  116. }
  117. int zipOpenNewFileInZip5(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  118. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  119. uint16_t size_extrafield_global, const char *comment, uint16_t compression_method, int level,
  120. int raw, int windowBits, int memLevel, int strategy, const char *password,
  121. signed char aes, uint16_t version_madeby, uint16_t flag_base, int zip64)
  122. {
  123. mz_compat *compat = (mz_compat *)file;
  124. mz_zip_file file_info;
  125. uint64_t dos_date = 0;
  126. MZ_UNUSED(strategy);
  127. MZ_UNUSED(memLevel);
  128. MZ_UNUSED(windowBits);
  129. MZ_UNUSED(size_extrafield_local);
  130. MZ_UNUSED(extrafield_local);
  131. if (compat == NULL)
  132. return ZIP_PARAMERROR;
  133. memset(&file_info, 0, sizeof(file_info));
  134. if (zipfi != NULL)
  135. {
  136. if (zipfi->mz_dos_date != 0)
  137. dos_date = zipfi->mz_dos_date;
  138. else
  139. dos_date = mz_zip_tm_to_dosdate(&zipfi->tmz_date);
  140. file_info.modified_date = mz_zip_dosdate_to_time_t(dos_date);
  141. file_info.external_fa = zipfi->external_fa;
  142. file_info.internal_fa = zipfi->internal_fa;
  143. }
  144. if (filename == NULL)
  145. filename = "-";
  146. file_info.compression_method = compression_method;
  147. file_info.filename = filename;
  148. /* file_info.extrafield_local = extrafield_local; */
  149. /* file_info.extrafield_local_size = size_extrafield_local; */
  150. file_info.extrafield = extrafield_global;
  151. file_info.extrafield_size = size_extrafield_global;
  152. file_info.version_madeby = version_madeby;
  153. file_info.comment = comment;
  154. file_info.flag = flag_base;
  155. if (zip64)
  156. file_info.zip64 = MZ_ZIP64_FORCE;
  157. else
  158. file_info.zip64 = MZ_ZIP64_DISABLE;
  159. #ifdef HAVE_WZAES
  160. if ((aes && password != NULL) || (raw && (file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)))
  161. file_info.aes_version = MZ_AES_VERSION;
  162. #endif
  163. return mz_zip_entry_write_open(compat->handle, &file_info, (int16_t)level, (uint8_t)raw, password);
  164. }
  165. int zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
  166. {
  167. mz_compat *compat = (mz_compat *)file;
  168. int32_t written = 0;
  169. if (compat == NULL || len >= INT32_MAX)
  170. return ZIP_PARAMERROR;
  171. written = mz_zip_entry_write(compat->handle, buf, (int32_t)len);
  172. if ((written < 0) || ((uint32_t)written != len))
  173. return ZIP_ERRNO;
  174. return ZIP_OK;
  175. }
  176. int zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
  177. {
  178. return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
  179. }
  180. int zipCloseFileInZipRaw64(zipFile file, int64_t uncompressed_size, uint32_t crc32)
  181. {
  182. mz_compat *compat = (mz_compat *)file;
  183. if (compat == NULL)
  184. return ZIP_PARAMERROR;
  185. return mz_zip_entry_close_raw(compat->handle, uncompressed_size, crc32);
  186. }
  187. int zipCloseFileInZip(zipFile file)
  188. {
  189. return zipCloseFileInZip64(file);
  190. }
  191. int zipCloseFileInZip64(zipFile file)
  192. {
  193. mz_compat *compat = (mz_compat *)file;
  194. if (compat == NULL)
  195. return ZIP_PARAMERROR;
  196. return mz_zip_entry_close(compat->handle);
  197. }
  198. int zipClose(zipFile file, const char *global_comment)
  199. {
  200. return zipClose_64(file, global_comment);
  201. }
  202. int zipClose_64(zipFile file, const char *global_comment)
  203. {
  204. return zipClose2_64(file, global_comment, MZ_VERSION_MADEBY);
  205. }
  206. int zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
  207. {
  208. mz_compat *compat = (mz_compat *)file;
  209. int32_t err = MZ_OK;
  210. if (compat->handle != NULL)
  211. err = zipClose2_MZ(file, global_comment, version_madeby);
  212. if (compat->stream != NULL)
  213. {
  214. mz_stream_close(compat->stream);
  215. mz_stream_delete(&compat->stream);
  216. }
  217. MZ_FREE(compat);
  218. return err;
  219. }
  220. /* Only closes the zip handle, does not close the stream */
  221. int zipClose_MZ(zipFile file, const char *global_comment)
  222. {
  223. return zipClose2_MZ(file, global_comment, MZ_VERSION_MADEBY);
  224. }
  225. /* Only closes the zip handle, does not close the stream */
  226. int zipClose2_MZ(zipFile file, const char *global_comment, uint16_t version_madeby)
  227. {
  228. mz_compat *compat = (mz_compat *)file;
  229. int32_t err = MZ_OK;
  230. if (compat == NULL)
  231. return ZIP_PARAMERROR;
  232. if (compat->handle == NULL)
  233. return err;
  234. if (global_comment != NULL)
  235. mz_zip_set_comment(compat->handle, global_comment);
  236. mz_zip_set_version_madeby(compat->handle, version_madeby);
  237. err = mz_zip_close(compat->handle);
  238. mz_zip_delete(&compat->handle);
  239. return err;
  240. }
  241. void* zipGetStream(zipFile file)
  242. {
  243. mz_compat *compat = (mz_compat *)file;
  244. if (compat == NULL)
  245. return NULL;
  246. return (void *)compat->stream;
  247. }
  248. /***************************************************************************/
  249. unzFile unzOpen(const char *path)
  250. {
  251. return unzOpen64(path);
  252. }
  253. unzFile unzOpen64(const void *path)
  254. {
  255. zlib_filefunc64_def pzlib = mz_stream_os_get_interface();
  256. return unzOpen2(path, &pzlib);
  257. }
  258. unzFile unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc_def)
  259. {
  260. return unzOpen2_64(path, pzlib_filefunc_def);
  261. }
  262. unzFile unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
  263. {
  264. unzFile unz = NULL;
  265. void *stream = NULL;
  266. if (pzlib_filefunc_def)
  267. {
  268. if (mz_stream_create(&stream, (mz_stream_vtbl *)*pzlib_filefunc_def) == NULL)
  269. return NULL;
  270. }
  271. else
  272. {
  273. if (mz_stream_os_create(&stream) == NULL)
  274. return NULL;
  275. }
  276. if (mz_stream_open(stream, path, MZ_OPEN_MODE_READ) != MZ_OK)
  277. {
  278. mz_stream_delete(&stream);
  279. return NULL;
  280. }
  281. unz = unzOpen_MZ(stream);
  282. if (unz == NULL)
  283. {
  284. mz_stream_delete(&stream);
  285. return NULL;
  286. }
  287. return unz;
  288. }
  289. unzFile unzOpen_MZ(void *stream)
  290. {
  291. mz_compat *compat = NULL;
  292. int32_t err = MZ_OK;
  293. void *handle = NULL;
  294. mz_zip_create(&handle);
  295. err = mz_zip_open(handle, stream, MZ_OPEN_MODE_READ);
  296. if (err != MZ_OK)
  297. {
  298. mz_zip_delete(&handle);
  299. return NULL;
  300. }
  301. compat = (mz_compat *)MZ_ALLOC(sizeof(mz_compat));
  302. if (compat != NULL)
  303. {
  304. compat->handle = handle;
  305. compat->stream = stream;
  306. mz_zip_goto_first_entry(compat->handle);
  307. }
  308. else
  309. {
  310. mz_zip_delete(&handle);
  311. }
  312. return (unzFile)compat;
  313. }
  314. int unzClose(unzFile file)
  315. {
  316. mz_compat *compat = (mz_compat *)file;
  317. int32_t err = MZ_OK;
  318. if (compat == NULL)
  319. return UNZ_PARAMERROR;
  320. if (compat->handle != NULL)
  321. err = unzClose_MZ(file);
  322. if (compat->stream != NULL)
  323. {
  324. mz_stream_close(compat->stream);
  325. mz_stream_delete(&compat->stream);
  326. }
  327. MZ_FREE(compat);
  328. return err;
  329. }
  330. /* Only closes the zip handle, does not close the stream */
  331. int unzClose_MZ(unzFile file)
  332. {
  333. mz_compat *compat = (mz_compat *)file;
  334. int32_t err = MZ_OK;
  335. if (compat == NULL)
  336. return UNZ_PARAMERROR;
  337. err = mz_zip_close(compat->handle);
  338. mz_zip_delete(&compat->handle);
  339. return err;
  340. }
  341. int unzGetGlobalInfo(unzFile file, unz_global_info* pglobal_info32)
  342. {
  343. mz_compat *compat = (mz_compat *)file;
  344. unz_global_info64 global_info64;
  345. int32_t err = MZ_OK;
  346. memset(pglobal_info32, 0, sizeof(unz_global_info));
  347. if (compat == NULL)
  348. return UNZ_PARAMERROR;
  349. err = unzGetGlobalInfo64(file, &global_info64);
  350. if (err == MZ_OK)
  351. {
  352. pglobal_info32->number_entry = (uint32_t)global_info64.number_entry;
  353. pglobal_info32->size_comment = global_info64.size_comment;
  354. pglobal_info32->number_disk_with_CD = global_info64.number_disk_with_CD;
  355. }
  356. return err;
  357. }
  358. int unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
  359. {
  360. mz_compat *compat = (mz_compat *)file;
  361. const char *comment_ptr = NULL;
  362. int32_t err = MZ_OK;
  363. memset(pglobal_info, 0, sizeof(unz_global_info64));
  364. if (compat == NULL)
  365. return UNZ_PARAMERROR;
  366. err = mz_zip_get_comment(compat->handle, &comment_ptr);
  367. if (err == MZ_OK)
  368. pglobal_info->size_comment = (uint16_t)strlen(comment_ptr);
  369. if ((err == MZ_OK) || (err == MZ_EXIST_ERROR))
  370. err = mz_zip_get_number_entry(compat->handle, &pglobal_info->number_entry);
  371. if (err == MZ_OK)
  372. err = mz_zip_get_disk_number_with_cd(compat->handle, &pglobal_info->number_disk_with_CD);
  373. return err;
  374. }
  375. int unzGetGlobalComment(unzFile file, char *comment, uint16_t comment_size)
  376. {
  377. mz_compat *compat = (mz_compat *)file;
  378. const char *comment_ptr = NULL;
  379. int32_t err = MZ_OK;
  380. if (comment == NULL || comment_size == 0)
  381. return UNZ_PARAMERROR;
  382. err = mz_zip_get_comment(compat->handle, &comment_ptr);
  383. if (err == MZ_OK)
  384. {
  385. strncpy(comment, comment_ptr, comment_size - 1);
  386. comment[comment_size - 1] = 0;
  387. }
  388. return err;
  389. }
  390. int unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
  391. {
  392. mz_compat *compat = (mz_compat *)file;
  393. mz_zip_file *file_info = NULL;
  394. int32_t err = MZ_OK;
  395. void *stream = NULL;
  396. if (compat == NULL)
  397. return UNZ_PARAMERROR;
  398. if (method != NULL)
  399. *method = 0;
  400. if (level != NULL)
  401. *level = 0;
  402. compat->total_out = 0;
  403. err = mz_zip_entry_read_open(compat->handle, (uint8_t)raw, password);
  404. if (err == MZ_OK)
  405. err = mz_zip_entry_get_info(compat->handle, &file_info);
  406. if (err == MZ_OK)
  407. {
  408. if (method != NULL)
  409. {
  410. *method = file_info->compression_method;
  411. }
  412. if (level != NULL)
  413. {
  414. *level = 6;
  415. switch (file_info->flag & 0x06)
  416. {
  417. case MZ_ZIP_FLAG_DEFLATE_SUPER_FAST:
  418. *level = 1;
  419. break;
  420. case MZ_ZIP_FLAG_DEFLATE_FAST:
  421. *level = 2;
  422. break;
  423. case MZ_ZIP_FLAG_DEFLATE_MAX:
  424. *level = 9;
  425. break;
  426. }
  427. }
  428. }
  429. if (err == MZ_OK)
  430. err = mz_zip_get_stream(compat->handle, &stream);
  431. if (err == MZ_OK)
  432. compat->entry_pos = mz_stream_tell(stream);
  433. return err;
  434. }
  435. int unzOpenCurrentFile(unzFile file)
  436. {
  437. return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
  438. }
  439. int unzOpenCurrentFilePassword(unzFile file, const char *password)
  440. {
  441. return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
  442. }
  443. int unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
  444. {
  445. return unzOpenCurrentFile3(file, method, level, raw, NULL);
  446. }
  447. int unzReadCurrentFile(unzFile file, void *buf, uint32_t len)
  448. {
  449. mz_compat *compat = (mz_compat *)file;
  450. int32_t err = MZ_OK;
  451. if (compat == NULL || len >= INT32_MAX)
  452. return UNZ_PARAMERROR;
  453. err = mz_zip_entry_read(compat->handle, buf, (int32_t)len);
  454. if (err > 0)
  455. compat->total_out += (uint32_t)err;
  456. return err;
  457. }
  458. int unzCloseCurrentFile(unzFile file)
  459. {
  460. mz_compat *compat = (mz_compat *)file;
  461. int32_t err = MZ_OK;
  462. if (compat == NULL)
  463. return UNZ_PARAMERROR;
  464. err = mz_zip_entry_close(compat->handle);
  465. return err;
  466. }
  467. int unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
  468. uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
  469. {
  470. mz_compat *compat = (mz_compat *)file;
  471. mz_zip_file *file_info = NULL;
  472. uint16_t bytes_to_copy = 0;
  473. int32_t err = MZ_OK;
  474. if (compat == NULL)
  475. return UNZ_PARAMERROR;
  476. err = mz_zip_entry_get_info(compat->handle, &file_info);
  477. if ((err == MZ_OK) && (pfile_info != NULL))
  478. {
  479. pfile_info->version = file_info->version_madeby;
  480. pfile_info->version_needed = file_info->version_needed;
  481. pfile_info->flag = file_info->flag;
  482. pfile_info->compression_method = file_info->compression_method;
  483. pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
  484. //mz_zip_time_t_to_tm(file_info->modified_date, &pfile_info->tmu_date);
  485. //pfile_info->tmu_date.tm_year += 1900;
  486. pfile_info->crc = file_info->crc;
  487. pfile_info->size_filename = file_info->filename_size;
  488. pfile_info->size_file_extra = file_info->extrafield_size;
  489. pfile_info->size_file_comment = file_info->comment_size;
  490. pfile_info->disk_num_start = (uint16_t)file_info->disk_number;
  491. pfile_info->internal_fa = file_info->internal_fa;
  492. pfile_info->external_fa = file_info->external_fa;
  493. pfile_info->compressed_size = (uint32_t)file_info->compressed_size;
  494. pfile_info->uncompressed_size = (uint32_t)file_info->uncompressed_size;
  495. if (filename_size > 0 && filename != NULL && file_info->filename != NULL)
  496. {
  497. bytes_to_copy = filename_size;
  498. if (bytes_to_copy > file_info->filename_size)
  499. bytes_to_copy = file_info->filename_size;
  500. memcpy(filename, file_info->filename, bytes_to_copy);
  501. if (bytes_to_copy < filename_size)
  502. filename[bytes_to_copy] = 0;
  503. }
  504. if (extrafield_size > 0 && extrafield != NULL)
  505. {
  506. bytes_to_copy = extrafield_size;
  507. if (bytes_to_copy > file_info->extrafield_size)
  508. bytes_to_copy = file_info->extrafield_size;
  509. memcpy(extrafield, file_info->extrafield, bytes_to_copy);
  510. }
  511. if (comment_size > 0 && comment != NULL && file_info->comment != NULL)
  512. {
  513. bytes_to_copy = comment_size;
  514. if (bytes_to_copy > file_info->comment_size)
  515. bytes_to_copy = file_info->comment_size;
  516. memcpy(comment, file_info->comment, bytes_to_copy);
  517. if (bytes_to_copy < comment_size)
  518. comment[bytes_to_copy] = 0;
  519. }
  520. }
  521. return err;
  522. }
  523. int unzGetCurrentFileInfo64(unzFile file, unz_file_info64 * pfile_info, char *filename,
  524. uint16_t filename_size, void *extrafield, uint16_t extrafield_size, char *comment, uint16_t comment_size)
  525. {
  526. mz_compat *compat = (mz_compat *)file;
  527. mz_zip_file *file_info = NULL;
  528. uint16_t bytes_to_copy = 0;
  529. int32_t err = MZ_OK;
  530. if (compat == NULL)
  531. return UNZ_PARAMERROR;
  532. err = mz_zip_entry_get_info(compat->handle, &file_info);
  533. if ((err == MZ_OK) && (pfile_info != NULL))
  534. {
  535. pfile_info->version = file_info->version_madeby;
  536. pfile_info->version_needed = file_info->version_needed;
  537. pfile_info->flag = file_info->flag;
  538. pfile_info->compression_method = file_info->compression_method;
  539. pfile_info->mz_dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
  540. //mz_zip_time_t_to_tm(file_info->modified_date, &pfile_info->tmu_date);
  541. //pfile_info->tmu_date.tm_year += 1900;
  542. pfile_info->crc = file_info->crc;
  543. pfile_info->size_filename = file_info->filename_size;
  544. pfile_info->size_file_extra = file_info->extrafield_size;
  545. pfile_info->size_file_comment = file_info->comment_size;
  546. pfile_info->disk_num_start = file_info->disk_number;
  547. pfile_info->internal_fa = file_info->internal_fa;
  548. pfile_info->external_fa = file_info->external_fa;
  549. pfile_info->compressed_size = (uint64_t)file_info->compressed_size;
  550. pfile_info->uncompressed_size = (uint64_t)file_info->uncompressed_size;
  551. if (filename_size > 0 && filename != NULL && file_info->filename != NULL)
  552. {
  553. bytes_to_copy = filename_size;
  554. if (bytes_to_copy > file_info->filename_size)
  555. bytes_to_copy = file_info->filename_size;
  556. memcpy(filename, file_info->filename, bytes_to_copy);
  557. if (bytes_to_copy < filename_size)
  558. filename[bytes_to_copy] = 0;
  559. }
  560. if (extrafield_size > 0 && extrafield != NULL)
  561. {
  562. bytes_to_copy = extrafield_size;
  563. if (bytes_to_copy > file_info->extrafield_size)
  564. bytes_to_copy = file_info->extrafield_size;
  565. memcpy(extrafield, file_info->extrafield, bytes_to_copy);
  566. }
  567. if (comment_size > 0 && comment != NULL && file_info->comment != NULL)
  568. {
  569. bytes_to_copy = comment_size;
  570. if (bytes_to_copy > file_info->comment_size)
  571. bytes_to_copy = file_info->comment_size;
  572. memcpy(comment, file_info->comment, bytes_to_copy);
  573. if (bytes_to_copy < comment_size)
  574. comment[bytes_to_copy] = 0;
  575. }
  576. }
  577. return err;
  578. }
  579. int unzGoToFirstFile(unzFile file)
  580. {
  581. mz_compat *compat = (mz_compat *)file;
  582. if (compat == NULL)
  583. return UNZ_PARAMERROR;
  584. compat->entry_index = 0;
  585. return mz_zip_goto_first_entry(compat->handle);
  586. }
  587. int unzGoToNextFile(unzFile file)
  588. {
  589. mz_compat *compat = (mz_compat *)file;
  590. int32_t err = MZ_OK;
  591. if (compat == NULL)
  592. return UNZ_PARAMERROR;
  593. err = mz_zip_goto_next_entry(compat->handle);
  594. if (err != MZ_END_OF_LIST)
  595. compat->entry_index += 1;
  596. return err;
  597. }
  598. int unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
  599. {
  600. mz_compat *compat = (mz_compat *)file;
  601. mz_zip_file *file_info = NULL;
  602. uint64_t preserve_index = 0;
  603. int32_t err = MZ_OK;
  604. int32_t result = 0;
  605. if (compat == NULL)
  606. return UNZ_PARAMERROR;
  607. preserve_index = compat->entry_index;
  608. err = mz_zip_goto_first_entry(compat->handle);
  609. while (err == MZ_OK)
  610. {
  611. err = mz_zip_entry_get_info(compat->handle, &file_info);
  612. if (err != MZ_OK)
  613. break;
  614. if (filename_compare_func != NULL)
  615. result = filename_compare_func(file, filename, file_info->filename);
  616. else
  617. result = strcmp(filename, file_info->filename);
  618. if (result == 0)
  619. return MZ_OK;
  620. err = mz_zip_goto_next_entry(compat->handle);
  621. }
  622. compat->entry_index = preserve_index;
  623. return err;
  624. }
  625. /***************************************************************************/
  626. int unzGetFilePos(unzFile file, unz_file_pos *file_pos)
  627. {
  628. mz_compat *compat = (mz_compat *)file;
  629. int32_t offset = 0;
  630. if (compat == NULL || file_pos == NULL)
  631. return UNZ_PARAMERROR;
  632. offset = unzGetOffset(file);
  633. if (offset < 0)
  634. return offset;
  635. file_pos->pos_in_zip_directory = (uint32_t)offset;
  636. file_pos->num_of_file = (uint32_t)compat->entry_index;
  637. return MZ_OK;
  638. }
  639. int unzGoToFilePos(unzFile file, unz_file_pos *file_pos)
  640. {
  641. mz_compat *compat = (mz_compat *)file;
  642. unz64_file_pos file_pos64;
  643. if (compat == NULL || file_pos == NULL)
  644. return UNZ_PARAMERROR;
  645. file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
  646. file_pos64.num_of_file = file_pos->num_of_file;
  647. return unzGoToFilePos64(file, &file_pos64);
  648. }
  649. int unzGetFilePos64(unzFile file, unz64_file_pos *file_pos)
  650. {
  651. mz_compat *compat = (mz_compat *)file;
  652. int64_t offset = 0;
  653. if (compat == NULL || file_pos == NULL)
  654. return UNZ_PARAMERROR;
  655. offset = unzGetOffset64(file);
  656. if (offset < 0)
  657. return (int)offset;
  658. file_pos->pos_in_zip_directory = offset;
  659. file_pos->num_of_file = compat->entry_index;
  660. return UNZ_OK;
  661. }
  662. int unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos)
  663. {
  664. mz_compat *compat = (mz_compat *)file;
  665. int32_t err = MZ_OK;
  666. if (compat == NULL || file_pos == NULL)
  667. return UNZ_PARAMERROR;
  668. err = mz_zip_goto_entry(compat->handle, file_pos->pos_in_zip_directory);
  669. if (err == MZ_OK)
  670. compat->entry_index = file_pos->num_of_file;
  671. return err;
  672. }
  673. int32_t unzGetOffset(unzFile file)
  674. {
  675. return (int32_t)unzGetOffset64(file);
  676. }
  677. int64_t unzGetOffset64(unzFile file)
  678. {
  679. mz_compat *compat = (mz_compat *)file;
  680. if (compat == NULL)
  681. return UNZ_PARAMERROR;
  682. return mz_zip_get_entry(compat->handle);
  683. }
  684. int unzSetOffset(unzFile file, uint32_t pos)
  685. {
  686. return unzSetOffset64(file, pos);
  687. }
  688. int unzSetOffset64(unzFile file, int64_t pos)
  689. {
  690. mz_compat *compat = (mz_compat *)file;
  691. if (compat == NULL)
  692. return UNZ_PARAMERROR;
  693. return (int)mz_zip_goto_entry(compat->handle, pos);
  694. }
  695. int unzGetLocalExtrafield(unzFile file, void *buf, unsigned int len)
  696. {
  697. mz_compat *compat = (mz_compat *)file;
  698. mz_zip_file *file_info = NULL;
  699. int32_t err = MZ_OK;
  700. int32_t bytes_to_copy = 0;
  701. if (compat == NULL || buf == NULL || len >= INT32_MAX)
  702. return UNZ_PARAMERROR;
  703. err = mz_zip_entry_get_local_info(compat->handle, &file_info);
  704. if (err != MZ_OK)
  705. return err;
  706. bytes_to_copy = (int32_t)len;
  707. if (bytes_to_copy > file_info->extrafield_size)
  708. bytes_to_copy = file_info->extrafield_size;
  709. memcpy(buf, file_info->extrafield, bytes_to_copy);
  710. return MZ_OK;
  711. }
  712. int64_t unztell(unzFile file)
  713. {
  714. mz_compat *compat = (mz_compat *)file;
  715. if (compat == NULL)
  716. return UNZ_PARAMERROR;
  717. return (int64_t)compat->total_out;
  718. }
  719. int32_t unzTell(unzFile file)
  720. {
  721. mz_compat *compat = (mz_compat *)file;
  722. if (compat == NULL)
  723. return UNZ_PARAMERROR;
  724. return (int32_t)compat->total_out;
  725. }
  726. int64_t unzTell64(unzFile file)
  727. {
  728. mz_compat *compat = (mz_compat *)file;
  729. if (compat == NULL)
  730. return UNZ_PARAMERROR;
  731. return (int64_t)compat->total_out;
  732. }
  733. int unzSeek(unzFile file, int32_t offset, int origin)
  734. {
  735. return unzSeek64(file, offset, origin);
  736. }
  737. int unzSeek64(unzFile file, int64_t offset, int origin)
  738. {
  739. mz_compat *compat = (mz_compat *)file;
  740. mz_zip_file *file_info = NULL;
  741. int64_t position = 0;
  742. int32_t err = MZ_OK;
  743. void *stream = NULL;
  744. if (compat == NULL)
  745. return UNZ_PARAMERROR;
  746. err = mz_zip_entry_get_info(compat->handle, &file_info);
  747. if (err != MZ_OK)
  748. return err;
  749. if (file_info->compression_method != MZ_COMPRESS_METHOD_STORE)
  750. return UNZ_ERRNO;
  751. if (origin == SEEK_SET)
  752. position = offset;
  753. else if (origin == SEEK_CUR)
  754. position = compat->total_out + offset;
  755. else if (origin == SEEK_END)
  756. position = (int64_t)file_info->compressed_size + offset;
  757. else
  758. return UNZ_PARAMERROR;
  759. if (position > (int64_t)file_info->compressed_size)
  760. return UNZ_PARAMERROR;
  761. err = mz_zip_get_stream(compat->handle, &stream);
  762. if (err == MZ_OK)
  763. err = mz_stream_seek(stream, compat->entry_pos + position, MZ_SEEK_SET);
  764. if (err == MZ_OK)
  765. compat->total_out = position;
  766. return err;
  767. }
  768. int unzEndOfFile(unzFile file)
  769. {
  770. mz_compat *compat = (mz_compat *)file;
  771. mz_zip_file *file_info = NULL;
  772. int32_t err = MZ_OK;
  773. if (compat == NULL)
  774. return UNZ_PARAMERROR;
  775. err = mz_zip_entry_get_info(compat->handle, &file_info);
  776. if (err != MZ_OK)
  777. return err;
  778. if (compat->total_out == (int64_t)file_info->uncompressed_size)
  779. return 1;
  780. return 0;
  781. }
  782. void* unzGetStream(unzFile file)
  783. {
  784. mz_compat *compat = (mz_compat *)file;
  785. if (compat == NULL)
  786. return NULL;
  787. return (void *)compat->stream;
  788. }
  789. /***************************************************************************/
  790. void fill_fopen_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
  791. {
  792. if (pzlib_filefunc_def != NULL)
  793. *pzlib_filefunc_def = mz_stream_os_get_interface();
  794. }
  795. void fill_fopen64_filefunc(zlib_filefunc64_def *pzlib_filefunc_def)
  796. {
  797. if (pzlib_filefunc_def != NULL)
  798. *pzlib_filefunc_def = mz_stream_os_get_interface();
  799. }
  800. void fill_win32_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
  801. {
  802. if (pzlib_filefunc_def != NULL)
  803. *pzlib_filefunc_def = mz_stream_os_get_interface();
  804. }
  805. void fill_win32_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def)
  806. {
  807. if (pzlib_filefunc_def != NULL)
  808. *pzlib_filefunc_def = mz_stream_os_get_interface();
  809. }
  810. void fill_win32_filefunc64A(zlib_filefunc64_def *pzlib_filefunc_def)
  811. {
  812. if (pzlib_filefunc_def != NULL)
  813. *pzlib_filefunc_def = mz_stream_os_get_interface();
  814. }
  815. void fill_win32_filefunc64W(zlib_filefunc64_def *pzlib_filefunc_def)
  816. {
  817. /* NOTE: You should no longer pass in widechar string to open function */
  818. if (pzlib_filefunc_def != NULL)
  819. *pzlib_filefunc_def = mz_stream_os_get_interface();
  820. }
  821. void fill_memory_filefunc(zlib_filefunc_def *pzlib_filefunc_def)
  822. {
  823. if (pzlib_filefunc_def != NULL)
  824. *pzlib_filefunc_def = mz_stream_mem_get_interface();
  825. }