zip.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784
  1. /*
  2. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  3. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  4. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  5. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  6. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  7. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  8. * OTHER DEALINGS IN THE SOFTWARE.
  9. */
  10. #define __STDC_WANT_LIB_EXT1__ 1
  11. #include <errno.h>
  12. #include <sys/stat.h>
  13. #include <time.h>
  14. #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
  15. defined(__MINGW32__)
  16. /* Win32, DOS, MSVC, MSVS */
  17. #include <direct.h>
  18. #define STRCLONE(STR) ((STR) ? _strdup(STR) : NULL)
  19. #define HAS_DEVICE(P) \
  20. ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) && \
  21. (P)[1] == ':')
  22. #define FILESYSTEM_PREFIX_LEN(P) (HAS_DEVICE(P) ? 2 : 0)
  23. #else
  24. #include <unistd.h> // needed for symlink()
  25. #define STRCLONE(STR) ((STR) ? strdup(STR) : NULL)
  26. #endif
  27. #ifdef __MINGW32__
  28. #include <sys/types.h>
  29. #include <unistd.h>
  30. #endif
  31. #include "miniz.h"
  32. #include "zip.h"
  33. #ifdef _MSC_VER
  34. #include <io.h>
  35. #define ftruncate(fd, sz) (-(_chsize_s((fd), (sz)) != 0))
  36. #define fileno _fileno
  37. #endif
  38. #if defined(__TINYC__) && (defined(_WIN32) || defined(_WIN64))
  39. #include <io.h>
  40. #define ftruncate(fd, sz) (-(_chsize_s((fd), (sz)) != 0))
  41. #define fileno _fileno
  42. #endif
  43. #ifndef HAS_DEVICE
  44. #define HAS_DEVICE(P) 0
  45. #endif
  46. #ifndef FILESYSTEM_PREFIX_LEN
  47. #define FILESYSTEM_PREFIX_LEN(P) 0
  48. #endif
  49. #ifndef ISSLASH
  50. #define ISSLASH(C) ((C) == '/' || (C) == '\\')
  51. #endif
  52. #define CLEANUP(ptr) \
  53. do { \
  54. if (ptr) { \
  55. free((void *)ptr); \
  56. ptr = NULL; \
  57. } \
  58. } while (0)
  59. #define UNX_IFDIR 0040000 /* Unix directory */
  60. #define UNX_IFREG 0100000 /* Unix regular file */
  61. #define UNX_IFSOCK 0140000 /* Unix socket (BSD, not SysV or Amiga) */
  62. #define UNX_IFLNK 0120000 /* Unix symbolic link (not SysV, Amiga) */
  63. #define UNX_IFBLK 0060000 /* Unix block special (not Amiga) */
  64. #define UNX_IFCHR 0020000 /* Unix character special (not Amiga) */
  65. #define UNX_IFIFO 0010000 /* Unix fifo (BCC, not MSC or Amiga) */
  66. struct zip_entry_t {
  67. ssize_t index;
  68. char *name;
  69. mz_uint64 uncomp_size;
  70. mz_uint64 comp_size;
  71. mz_uint32 uncomp_crc32;
  72. mz_uint64 offset;
  73. mz_uint8 header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
  74. mz_uint64 header_offset;
  75. mz_uint16 method;
  76. mz_zip_writer_add_state state;
  77. tdefl_compressor comp;
  78. mz_uint32 external_attr;
  79. time_t m_time;
  80. };
  81. struct zip_t {
  82. mz_zip_archive archive;
  83. mz_uint level;
  84. struct zip_entry_t entry;
  85. };
  86. enum zip_modify_t {
  87. MZ_KEEP = 0,
  88. MZ_DELETE = 1,
  89. MZ_MOVE = 2,
  90. };
  91. struct zip_entry_mark_t {
  92. ssize_t file_index;
  93. enum zip_modify_t type;
  94. mz_uint64 m_local_header_ofs;
  95. size_t lf_length;
  96. };
  97. static const char *const zip_errlist[30] = {
  98. NULL,
  99. "not initialized\0",
  100. "invalid entry name\0",
  101. "entry not found\0",
  102. "invalid zip mode\0",
  103. "invalid compression level\0",
  104. "no zip 64 support\0",
  105. "memset error\0",
  106. "cannot write data to entry\0",
  107. "cannot initialize tdefl compressor\0",
  108. "invalid index\0",
  109. "header not found\0",
  110. "cannot flush tdefl buffer\0",
  111. "cannot write entry header\0",
  112. "cannot create entry header\0",
  113. "cannot write to central dir\0",
  114. "cannot open file\0",
  115. "invalid entry type\0",
  116. "extracting data using no memory allocation\0",
  117. "file not found\0",
  118. "no permission\0",
  119. "out of memory\0",
  120. "invalid zip archive name\0",
  121. "make dir error\0",
  122. "symlink error\0",
  123. "close archive error\0",
  124. "capacity size too small\0",
  125. "fseek error\0",
  126. "fread error\0",
  127. "fwrite error\0",
  128. };
  129. const char *zip_strerror(int errnum) {
  130. errnum = -errnum;
  131. if (errnum <= 0 || errnum >= 30) {
  132. return NULL;
  133. }
  134. return zip_errlist[errnum];
  135. }
  136. static const char *zip_basename(const char *name) {
  137. char const *p;
  138. char const *base = name += FILESYSTEM_PREFIX_LEN(name);
  139. int all_slashes = 1;
  140. for (p = name; *p; p++) {
  141. if (ISSLASH(*p))
  142. base = p + 1;
  143. else
  144. all_slashes = 0;
  145. }
  146. /* If NAME is all slashes, arrange to return `/'. */
  147. if (*base == '\0' && ISSLASH(*name) && all_slashes)
  148. --base;
  149. return base;
  150. }
  151. static int zip_mkpath(char *path) {
  152. char *p;
  153. char npath[MAX_PATH + 1];
  154. int len = 0;
  155. int has_device = HAS_DEVICE(path);
  156. memset(npath, 0, MAX_PATH + 1);
  157. if (has_device) {
  158. // only on windows
  159. npath[0] = path[0];
  160. npath[1] = path[1];
  161. len = 2;
  162. }
  163. for (p = path + len; *p && len < MAX_PATH; p++) {
  164. if (ISSLASH(*p) && ((!has_device && len > 0) || (has_device && len > 2))) {
  165. #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
  166. defined(__MINGW32__)
  167. #else
  168. if ('\\' == *p) {
  169. *p = '/';
  170. }
  171. #endif
  172. if (MZ_MKDIR(npath) == -1) {
  173. if (errno != EEXIST) {
  174. return ZIP_EMKDIR;
  175. }
  176. }
  177. }
  178. npath[len++] = *p;
  179. }
  180. return 0;
  181. }
  182. static char *zip_strrpl(const char *str, size_t n, char oldchar, char newchar) {
  183. char c;
  184. size_t i;
  185. char *rpl = (char *)calloc((1 + n), sizeof(char));
  186. char *begin = rpl;
  187. if (!rpl) {
  188. return NULL;
  189. }
  190. for (i = 0; (i < n) && (c = *str++); ++i) {
  191. if (c == oldchar) {
  192. c = newchar;
  193. }
  194. *rpl++ = c;
  195. }
  196. return begin;
  197. }
  198. static char *zip_name_normalize(char *name, char *const nname, size_t len) {
  199. size_t offn = 0;
  200. size_t offnn = 0, ncpy = 0;
  201. if (name == NULL || nname == NULL || len <= 0) {
  202. return NULL;
  203. }
  204. // skip trailing '/'
  205. while (ISSLASH(*name))
  206. name++;
  207. for (; offn < len; offn++) {
  208. if (ISSLASH(name[offn])) {
  209. if (ncpy > 0 && strcmp(&nname[offnn], ".\0") &&
  210. strcmp(&nname[offnn], "..\0")) {
  211. offnn += ncpy;
  212. nname[offnn++] = name[offn]; // append '/'
  213. }
  214. ncpy = 0;
  215. } else {
  216. nname[offnn + ncpy] = name[offn];
  217. ncpy++;
  218. }
  219. }
  220. // at the end, extra check what we've already copied
  221. if (ncpy == 0 || !strcmp(&nname[offnn], ".\0") ||
  222. !strcmp(&nname[offnn], "..\0")) {
  223. nname[offnn] = 0;
  224. }
  225. return nname;
  226. }
  227. static mz_bool zip_name_match(const char *name1, const char *name2) {
  228. char *nname2 = NULL;
  229. #ifdef ZIP_RAW_ENTRYNAME
  230. nname2 = STRCLONE(name2);
  231. #else
  232. nname2 = zip_strrpl(name2, strlen(name2), '\\', '/');
  233. #endif
  234. if (!nname2) {
  235. return MZ_FALSE;
  236. }
  237. mz_bool res = (strcmp(name1, nname2) == 0) ? MZ_TRUE : MZ_FALSE;
  238. CLEANUP(nname2);
  239. return res;
  240. }
  241. static int zip_archive_truncate(mz_zip_archive *pzip) {
  242. mz_zip_internal_state *pState = pzip->m_pState;
  243. mz_uint64 file_size = pzip->m_archive_size;
  244. if ((pzip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem)) {
  245. return 0;
  246. }
  247. if (pzip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED) {
  248. if (pState->m_pFile) {
  249. int fd = fileno(pState->m_pFile);
  250. return ftruncate(fd, file_size);
  251. }
  252. }
  253. return 0;
  254. }
  255. static int zip_archive_extract(mz_zip_archive *zip_archive, const char *dir,
  256. int (*on_extract)(const char *filename,
  257. void *arg),
  258. void *arg) {
  259. int err = 0;
  260. mz_uint i, n;
  261. char path[MAX_PATH + 1];
  262. char symlink_to[MAX_PATH + 1];
  263. mz_zip_archive_file_stat info;
  264. size_t dirlen = 0, filename_size = MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE;
  265. mz_uint32 xattr = 0;
  266. memset(path, 0, sizeof(path));
  267. memset(symlink_to, 0, sizeof(symlink_to));
  268. dirlen = strlen(dir);
  269. if (dirlen + 1 > MAX_PATH) {
  270. return ZIP_EINVENTNAME;
  271. }
  272. memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
  273. #if defined(_MSC_VER)
  274. strcpy_s(path, MAX_PATH, dir);
  275. #else
  276. strcpy(path, dir);
  277. #endif
  278. if (!ISSLASH(path[dirlen - 1])) {
  279. #if defined(_WIN32) || defined(__WIN32__)
  280. path[dirlen] = '\\';
  281. #else
  282. path[dirlen] = '/';
  283. #endif
  284. ++dirlen;
  285. }
  286. if (filename_size > MAX_PATH - dirlen) {
  287. filename_size = MAX_PATH - dirlen;
  288. }
  289. // Get and print information about each file in the archive.
  290. n = mz_zip_reader_get_num_files(zip_archive);
  291. for (i = 0; i < n; ++i) {
  292. if (!mz_zip_reader_file_stat(zip_archive, i, &info)) {
  293. // Cannot get information about zip archive;
  294. err = ZIP_ENOENT;
  295. goto out;
  296. }
  297. if (!zip_name_normalize(info.m_filename, info.m_filename,
  298. strlen(info.m_filename))) {
  299. // Cannot normalize file name;
  300. err = ZIP_EINVENTNAME;
  301. goto out;
  302. }
  303. #if defined(_MSC_VER)
  304. strncpy_s(&path[dirlen], filename_size, info.m_filename,
  305. filename_size);
  306. #else
  307. strncpy(&path[dirlen], info.m_filename, filename_size);
  308. #endif
  309. err = zip_mkpath(path);
  310. if (err < 0) {
  311. // Cannot make a path
  312. goto out;
  313. }
  314. if ((((info.m_version_made_by >> 8) == 3) ||
  315. ((info.m_version_made_by >> 8) ==
  316. 19)) // if zip is produced on Unix or macOS (3 and 19 from
  317. // section 4.4.2.2 of zip standard)
  318. && info.m_external_attr &
  319. (0x20 << 24)) { // and has sym link attribute (0x80 is file, 0x40
  320. // is directory)
  321. #if defined(_WIN32) || defined(__WIN32__) || defined(_MSC_VER) || \
  322. defined(__MINGW32__)
  323. #else
  324. if (info.m_uncomp_size > MAX_PATH ||
  325. !mz_zip_reader_extract_to_mem_no_alloc(zip_archive, i, symlink_to,
  326. MAX_PATH, 0, NULL, 0)) {
  327. err = ZIP_EMEMNOALLOC;
  328. goto out;
  329. }
  330. symlink_to[info.m_uncomp_size] = '\0';
  331. if (symlink(symlink_to, path) != 0) {
  332. err = ZIP_ESYMLINK;
  333. goto out;
  334. }
  335. #endif
  336. } else {
  337. if (!mz_zip_reader_is_file_a_directory(zip_archive, i)) {
  338. if (!mz_zip_reader_extract_to_file(zip_archive, i, path, 0)) {
  339. // Cannot extract zip archive to file
  340. err = ZIP_ENOFILE;
  341. goto out;
  342. }
  343. }
  344. #if defined(_MSC_VER) || defined(PS4)
  345. (void)xattr; // unused
  346. #else
  347. xattr = (info.m_external_attr >> 16) & 0xFFFF;
  348. if (xattr > 0 && xattr <= MZ_UINT16_MAX) {
  349. if (CHMOD(path, (mode_t)xattr) < 0) {
  350. err = ZIP_ENOPERM;
  351. goto out;
  352. }
  353. }
  354. #endif
  355. }
  356. if (on_extract) {
  357. if (on_extract(path, arg) < 0) {
  358. goto out;
  359. }
  360. }
  361. }
  362. out:
  363. // Close the archive, freeing any resources it was using
  364. if (!mz_zip_reader_end(zip_archive)) {
  365. // Cannot end zip reader
  366. err = ZIP_ECLSZIP;
  367. }
  368. return err;
  369. }
  370. static inline void zip_archive_finalize(mz_zip_archive *pzip) {
  371. mz_zip_writer_finalize_archive(pzip);
  372. zip_archive_truncate(pzip);
  373. }
  374. static ssize_t zip_entry_mark(struct zip_t *zip,
  375. struct zip_entry_mark_t *entry_mark,
  376. const ssize_t n, char *const entries[],
  377. const size_t len) {
  378. ssize_t i = 0;
  379. ssize_t err = 0;
  380. if (!zip || !entry_mark || !entries) {
  381. return ZIP_ENOINIT;
  382. }
  383. mz_zip_archive_file_stat file_stat;
  384. mz_uint64 d_pos = ~0UL;
  385. for (i = 0; i < n; ++i) {
  386. if ((err = zip_entry_openbyindex(zip, i))) {
  387. return (ssize_t)err;
  388. }
  389. mz_bool name_matches = MZ_FALSE;
  390. {
  391. size_t j;
  392. for (j = 0; j < len; ++j) {
  393. if (zip_name_match(zip->entry.name, entries[j])) {
  394. name_matches = MZ_TRUE;
  395. break;
  396. }
  397. }
  398. }
  399. if (name_matches) {
  400. entry_mark[i].type = MZ_DELETE;
  401. } else {
  402. entry_mark[i].type = MZ_KEEP;
  403. }
  404. if (!mz_zip_reader_file_stat(&zip->archive, i, &file_stat)) {
  405. return ZIP_ENOENT;
  406. }
  407. zip_entry_close(zip);
  408. entry_mark[i].m_local_header_ofs = file_stat.m_local_header_ofs;
  409. entry_mark[i].file_index = (ssize_t)-1;
  410. entry_mark[i].lf_length = 0;
  411. if ((entry_mark[i].type) == MZ_DELETE &&
  412. (d_pos > entry_mark[i].m_local_header_ofs)) {
  413. d_pos = entry_mark[i].m_local_header_ofs;
  414. }
  415. }
  416. for (i = 0; i < n; ++i) {
  417. if ((entry_mark[i].m_local_header_ofs > d_pos) &&
  418. (entry_mark[i].type != MZ_DELETE)) {
  419. entry_mark[i].type = MZ_MOVE;
  420. }
  421. }
  422. return err;
  423. }
  424. static ssize_t zip_index_next(mz_uint64 *local_header_ofs_array,
  425. ssize_t cur_index) {
  426. ssize_t new_index = 0, i;
  427. for (i = cur_index - 1; i >= 0; --i) {
  428. if (local_header_ofs_array[cur_index] > local_header_ofs_array[i]) {
  429. new_index = i + 1;
  430. return new_index;
  431. }
  432. }
  433. return new_index;
  434. }
  435. static ssize_t zip_sort(mz_uint64 *local_header_ofs_array, ssize_t cur_index) {
  436. ssize_t nxt_index = zip_index_next(local_header_ofs_array, cur_index);
  437. if (nxt_index != cur_index) {
  438. mz_uint64 temp = local_header_ofs_array[cur_index];
  439. ssize_t i;
  440. for (i = cur_index; i > nxt_index; i--) {
  441. local_header_ofs_array[i] = local_header_ofs_array[i - 1];
  442. }
  443. local_header_ofs_array[nxt_index] = temp;
  444. }
  445. return nxt_index;
  446. }
  447. static int zip_index_update(struct zip_entry_mark_t *entry_mark,
  448. ssize_t last_index, ssize_t nxt_index) {
  449. ssize_t j;
  450. for (j = 0; j < last_index; j++) {
  451. if (entry_mark[j].file_index >= nxt_index) {
  452. entry_mark[j].file_index += 1;
  453. }
  454. }
  455. entry_mark[nxt_index].file_index = last_index;
  456. return 0;
  457. }
  458. static int zip_entry_finalize(struct zip_t *zip,
  459. struct zip_entry_mark_t *entry_mark,
  460. const ssize_t n) {
  461. ssize_t i = 0;
  462. mz_uint64 *local_header_ofs_array = (mz_uint64 *)calloc(n, sizeof(mz_uint64));
  463. if (!local_header_ofs_array) {
  464. return ZIP_EOOMEM;
  465. }
  466. for (i = 0; i < n; ++i) {
  467. local_header_ofs_array[i] = entry_mark[i].m_local_header_ofs;
  468. ssize_t index = zip_sort(local_header_ofs_array, i);
  469. if (index != i) {
  470. zip_index_update(entry_mark, i, index);
  471. }
  472. entry_mark[i].file_index = index;
  473. }
  474. size_t *length = (size_t *)calloc(n, sizeof(size_t));
  475. if (!length) {
  476. CLEANUP(local_header_ofs_array);
  477. return ZIP_EOOMEM;
  478. }
  479. for (i = 0; i < n - 1; i++) {
  480. length[i] =
  481. (size_t)(local_header_ofs_array[i + 1] - local_header_ofs_array[i]);
  482. }
  483. length[n - 1] =
  484. (size_t)(zip->archive.m_archive_size - local_header_ofs_array[n - 1]);
  485. for (i = 0; i < n; i++) {
  486. entry_mark[i].lf_length = length[entry_mark[i].file_index];
  487. }
  488. CLEANUP(length);
  489. CLEANUP(local_header_ofs_array);
  490. return 0;
  491. }
  492. static ssize_t zip_entry_set(struct zip_t *zip,
  493. struct zip_entry_mark_t *entry_mark, ssize_t n,
  494. char *const entries[], const size_t len) {
  495. ssize_t err = 0;
  496. if ((err = zip_entry_mark(zip, entry_mark, n, entries, len)) < 0) {
  497. return err;
  498. }
  499. if ((err = zip_entry_finalize(zip, entry_mark, n)) < 0) {
  500. return err;
  501. }
  502. return 0;
  503. }
  504. static ssize_t zip_file_move(MZ_FILE *m_pFile, const mz_uint64 to,
  505. const mz_uint64 from, const size_t length,
  506. mz_uint8 *move_buf, const size_t capacity_size) {
  507. if (length > capacity_size) {
  508. return ZIP_ECAPSIZE;
  509. }
  510. if (MZ_FSEEK64(m_pFile, (long)from, SEEK_SET)) {
  511. return ZIP_EFSEEK;
  512. }
  513. if (fread(move_buf, 1, length, m_pFile) != length) {
  514. return ZIP_EFREAD;
  515. }
  516. if (MZ_FSEEK64(m_pFile, (long)to, SEEK_SET)) {
  517. return ZIP_EFSEEK;
  518. }
  519. if (fwrite(move_buf, 1, length, m_pFile) != length) {
  520. return ZIP_EFWRITE;
  521. }
  522. return (ssize_t)length;
  523. }
  524. static ssize_t zip_files_move(MZ_FILE *m_pFile, mz_uint64 writen_num,
  525. mz_uint64 read_num, size_t length) {
  526. ssize_t n = 0;
  527. const size_t page_size = 1 << 12; // 4K
  528. mz_uint8 *move_buf = (mz_uint8 *)calloc(1, page_size);
  529. if (!move_buf) {
  530. return ZIP_EOOMEM;
  531. }
  532. ssize_t moved_length = 0;
  533. ssize_t move_count = 0;
  534. while ((mz_int64)length > 0) {
  535. move_count = (length >= page_size) ? page_size : length;
  536. n = zip_file_move(m_pFile, writen_num, read_num, move_count, move_buf,
  537. page_size);
  538. if (n < 0) {
  539. moved_length = n;
  540. goto cleanup;
  541. }
  542. if (n != move_count) {
  543. goto cleanup;
  544. }
  545. writen_num += move_count;
  546. read_num += move_count;
  547. length -= move_count;
  548. moved_length += move_count;
  549. }
  550. cleanup:
  551. CLEANUP(move_buf);
  552. return moved_length;
  553. }
  554. static int zip_central_dir_move(mz_zip_internal_state *pState, int begin,
  555. int end, int entry_num) {
  556. if (begin == entry_num) {
  557. return 0;
  558. }
  559. size_t l_size = 0;
  560. size_t r_size = 0;
  561. mz_uint32 d_size = 0;
  562. mz_uint8 *next = NULL;
  563. mz_uint8 *deleted = &MZ_ZIP_ARRAY_ELEMENT(
  564. &pState->m_central_dir, mz_uint8,
  565. MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, begin));
  566. l_size = (size_t)(deleted - (mz_uint8 *)(pState->m_central_dir.m_p));
  567. if (end == entry_num) {
  568. r_size = 0;
  569. } else {
  570. next = &MZ_ZIP_ARRAY_ELEMENT(
  571. &pState->m_central_dir, mz_uint8,
  572. MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, end));
  573. r_size = pState->m_central_dir.m_size -
  574. (mz_uint32)(next - (mz_uint8 *)(pState->m_central_dir.m_p));
  575. d_size = (mz_uint32)(next - deleted);
  576. }
  577. if (next && l_size == 0) {
  578. memmove(pState->m_central_dir.m_p, next, r_size);
  579. pState->m_central_dir.m_p = MZ_REALLOC(pState->m_central_dir.m_p, r_size);
  580. {
  581. int i;
  582. for (i = end; i < entry_num; i++) {
  583. MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -=
  584. d_size;
  585. }
  586. }
  587. }
  588. if (next && l_size * r_size != 0) {
  589. memmove(deleted, next, r_size);
  590. {
  591. int i;
  592. for (i = end; i < entry_num; i++) {
  593. MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i) -=
  594. d_size;
  595. }
  596. }
  597. }
  598. pState->m_central_dir.m_size = l_size + r_size;
  599. return 0;
  600. }
  601. static int zip_central_dir_delete(mz_zip_internal_state *pState,
  602. int *deleted_entry_index_array,
  603. int entry_num) {
  604. int i = 0;
  605. int begin = 0;
  606. int end = 0;
  607. int d_num = 0;
  608. while (i < entry_num) {
  609. while ((i < entry_num) && (!deleted_entry_index_array[i])) {
  610. i++;
  611. }
  612. begin = i;
  613. while ((i < entry_num) && (deleted_entry_index_array[i])) {
  614. i++;
  615. }
  616. end = i;
  617. zip_central_dir_move(pState, begin, end, entry_num);
  618. }
  619. i = 0;
  620. while (i < entry_num) {
  621. while ((i < entry_num) && (!deleted_entry_index_array[i])) {
  622. i++;
  623. }
  624. begin = i;
  625. if (begin == entry_num) {
  626. break;
  627. }
  628. while ((i < entry_num) && (deleted_entry_index_array[i])) {
  629. i++;
  630. }
  631. end = i;
  632. int k = 0, j;
  633. for (j = end; j < entry_num; j++) {
  634. MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32,
  635. begin + k) =
  636. (mz_uint32)MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets,
  637. mz_uint32, j);
  638. k++;
  639. }
  640. d_num += end - begin;
  641. }
  642. pState->m_central_dir_offsets.m_size =
  643. sizeof(mz_uint32) * (entry_num - d_num);
  644. return 0;
  645. }
  646. static ssize_t zip_entries_delete_mark(struct zip_t *zip,
  647. struct zip_entry_mark_t *entry_mark,
  648. int entry_num) {
  649. mz_uint64 writen_num = 0;
  650. mz_uint64 read_num = 0;
  651. size_t deleted_length = 0;
  652. size_t move_length = 0;
  653. int i = 0;
  654. size_t deleted_entry_num = 0;
  655. ssize_t n = 0;
  656. mz_bool *deleted_entry_flag_array =
  657. (mz_bool *)calloc(entry_num, sizeof(mz_bool));
  658. if (deleted_entry_flag_array == NULL) {
  659. return ZIP_EOOMEM;
  660. }
  661. mz_zip_internal_state *pState = zip->archive.m_pState;
  662. zip->archive.m_zip_mode = MZ_ZIP_MODE_WRITING;
  663. if ((!pState->m_pFile) || MZ_FSEEK64(pState->m_pFile, 0, SEEK_SET)) {
  664. CLEANUP(deleted_entry_flag_array);
  665. return ZIP_ENOENT;
  666. }
  667. while (i < entry_num) {
  668. while ((i < entry_num) && (entry_mark[i].type == MZ_KEEP)) {
  669. writen_num += entry_mark[i].lf_length;
  670. read_num = writen_num;
  671. i++;
  672. }
  673. while ((i < entry_num) && (entry_mark[i].type == MZ_DELETE)) {
  674. deleted_entry_flag_array[i] = MZ_TRUE;
  675. read_num += entry_mark[i].lf_length;
  676. deleted_length += entry_mark[i].lf_length;
  677. i++;
  678. deleted_entry_num++;
  679. }
  680. while ((i < entry_num) && (entry_mark[i].type == MZ_MOVE)) {
  681. move_length += entry_mark[i].lf_length;
  682. mz_uint8 *p = &MZ_ZIP_ARRAY_ELEMENT(
  683. &pState->m_central_dir, mz_uint8,
  684. MZ_ZIP_ARRAY_ELEMENT(&pState->m_central_dir_offsets, mz_uint32, i));
  685. if (!p) {
  686. CLEANUP(deleted_entry_flag_array);
  687. return ZIP_ENOENT;
  688. }
  689. mz_uint32 offset = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
  690. offset -= (mz_uint32)deleted_length;
  691. MZ_WRITE_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS, offset);
  692. i++;
  693. }
  694. n = zip_files_move(pState->m_pFile, writen_num, read_num, move_length);
  695. if (n != (ssize_t)move_length) {
  696. CLEANUP(deleted_entry_flag_array);
  697. return n;
  698. }
  699. writen_num += move_length;
  700. read_num += move_length;
  701. }
  702. zip->archive.m_archive_size -= (mz_uint64)deleted_length;
  703. zip->archive.m_total_files =
  704. (mz_uint32)entry_num - (mz_uint32)deleted_entry_num;
  705. zip_central_dir_delete(pState, deleted_entry_flag_array, entry_num);
  706. CLEANUP(deleted_entry_flag_array);
  707. return (ssize_t)deleted_entry_num;
  708. }
  709. struct zip_t *zip_open(const char *zipname, int level, char mode) {
  710. struct zip_t *zip = NULL;
  711. if (!zipname || strlen(zipname) < 1) {
  712. // zip_t archive name is empty or NULL
  713. goto cleanup;
  714. }
  715. if (level < 0)
  716. level = MZ_DEFAULT_LEVEL;
  717. if ((level & 0xF) > MZ_UBER_COMPRESSION) {
  718. // Wrong compression level
  719. goto cleanup;
  720. }
  721. zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
  722. if (!zip)
  723. goto cleanup;
  724. zip->level = (mz_uint)level;
  725. switch (mode) {
  726. case 'w':
  727. // Create a new archive.
  728. if (!mz_zip_writer_init_file_v2(&(zip->archive), zipname, 0,
  729. MZ_ZIP_FLAG_WRITE_ZIP64)) {
  730. // Cannot initialize zip_archive writer
  731. goto cleanup;
  732. }
  733. break;
  734. case 'r':
  735. case 'a':
  736. case 'd':
  737. if (!mz_zip_reader_init_file_v2_rpb(
  738. &(zip->archive), zipname,
  739. zip->level | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0)) {
  740. // An archive file does not exist or cannot initialize
  741. // zip_archive reader
  742. goto cleanup;
  743. }
  744. if ((mode == 'a' || mode == 'd')) {
  745. if (!mz_zip_writer_init_from_reader_v2_noreopen(&(zip->archive), zipname,
  746. 0)) {
  747. mz_zip_reader_end(&(zip->archive));
  748. goto cleanup;
  749. }
  750. }
  751. break;
  752. default:
  753. goto cleanup;
  754. }
  755. return zip;
  756. cleanup:
  757. CLEANUP(zip);
  758. return NULL;
  759. }
  760. void zip_close(struct zip_t *zip) {
  761. if (zip) {
  762. // Always finalize, even if adding failed for some reason, so we have a
  763. // valid central directory.
  764. mz_zip_writer_finalize_archive(&(zip->archive));
  765. zip_archive_truncate(&(zip->archive));
  766. mz_zip_writer_end(&(zip->archive));
  767. mz_zip_reader_end(&(zip->archive));
  768. CLEANUP(zip);
  769. }
  770. }
  771. int zip_is64(struct zip_t *zip) {
  772. if (!zip || !zip->archive.m_pState) {
  773. // zip_t handler or zip state is not initialized
  774. return ZIP_ENOINIT;
  775. }
  776. return (int)zip->archive.m_pState->m_zip64;
  777. }
  778. static int _zip_entry_open(struct zip_t *zip, const char *entryname,
  779. int case_sensitive) {
  780. size_t entrylen = 0;
  781. mz_zip_archive *pzip = NULL;
  782. mz_uint num_alignment_padding_bytes, level;
  783. mz_zip_archive_file_stat stats;
  784. int err = 0;
  785. mz_uint16 dos_time, dos_date;
  786. mz_uint32 extra_size = 0;
  787. mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
  788. mz_uint64 local_dir_header_ofs = 0;
  789. if (!zip) {
  790. return ZIP_ENOINIT;
  791. }
  792. local_dir_header_ofs = zip->archive.m_archive_size;
  793. if (!entryname) {
  794. return ZIP_EINVENTNAME;
  795. }
  796. entrylen = strlen(entryname);
  797. if (entrylen == 0) {
  798. return ZIP_EINVENTNAME;
  799. }
  800. /*
  801. .ZIP File Format Specification Version: 6.3.3
  802. 4.4.17.1 The name of the file, with optional relative path.
  803. The path stored MUST not contain a drive or
  804. device letter, or a leading slash. All slashes
  805. MUST be forward slashes '/' as opposed to
  806. backwards slashes '\' for compatibility with Amiga
  807. and UNIX file systems etc. If input came from standard
  808. input, there is no file name field.
  809. */
  810. if (zip->entry.name) {
  811. CLEANUP(zip->entry.name);
  812. }
  813. #ifdef ZIP_RAW_ENTRYNAME
  814. zip->entry.name = STRCLONE(entryname);
  815. #else
  816. zip->entry.name = zip_strrpl(entryname, entrylen, '\\', '/');
  817. #endif
  818. if (!zip->entry.name) {
  819. // Cannot parse zip entry name
  820. return ZIP_EINVENTNAME;
  821. }
  822. pzip = &(zip->archive);
  823. if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
  824. zip->entry.index = (ssize_t)mz_zip_reader_locate_file(
  825. pzip, zip->entry.name, NULL,
  826. case_sensitive ? MZ_ZIP_FLAG_CASE_SENSITIVE : 0);
  827. if (zip->entry.index < (ssize_t)0) {
  828. err = ZIP_ENOENT;
  829. goto cleanup;
  830. }
  831. if (!mz_zip_reader_file_stat(pzip, (mz_uint)zip->entry.index, &stats)) {
  832. err = ZIP_ENOENT;
  833. goto cleanup;
  834. }
  835. zip->entry.comp_size = stats.m_comp_size;
  836. zip->entry.uncomp_size = stats.m_uncomp_size;
  837. zip->entry.uncomp_crc32 = stats.m_crc32;
  838. zip->entry.offset = stats.m_central_dir_ofs;
  839. zip->entry.header_offset = stats.m_local_header_ofs;
  840. zip->entry.method = stats.m_method;
  841. zip->entry.external_attr = stats.m_external_attr;
  842. #ifndef MINIZ_NO_TIME
  843. zip->entry.m_time = stats.m_time;
  844. #endif
  845. return 0;
  846. }
  847. level = zip->level & 0xF;
  848. zip->entry.index = (ssize_t)zip->archive.m_total_files;
  849. zip->entry.comp_size = 0;
  850. zip->entry.uncomp_size = 0;
  851. zip->entry.uncomp_crc32 = MZ_CRC32_INIT;
  852. zip->entry.offset = zip->archive.m_archive_size;
  853. zip->entry.header_offset = zip->archive.m_archive_size;
  854. memset(zip->entry.header, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE * sizeof(mz_uint8));
  855. zip->entry.method = level ? MZ_DEFLATED : 0;
  856. // UNIX or APPLE
  857. #if MZ_PLATFORM == 3 || MZ_PLATFORM == 19
  858. // regular file with rw-r--r-- permissions
  859. zip->entry.external_attr = (mz_uint32)(0100644) << 16;
  860. #else
  861. zip->entry.external_attr = 0;
  862. #endif
  863. num_alignment_padding_bytes =
  864. mz_zip_writer_compute_padding_needed_for_file_alignment(pzip);
  865. if (!pzip->m_pState || (pzip->m_zip_mode != MZ_ZIP_MODE_WRITING)) {
  866. // Invalid zip mode
  867. err = ZIP_EINVMODE;
  868. goto cleanup;
  869. }
  870. if (zip->level & MZ_ZIP_FLAG_COMPRESSED_DATA) {
  871. // Invalid zip compression level
  872. err = ZIP_EINVLVL;
  873. goto cleanup;
  874. }
  875. if (!mz_zip_writer_write_zeros(pzip, zip->entry.offset,
  876. num_alignment_padding_bytes)) {
  877. // Cannot memset zip entry header
  878. err = ZIP_EMEMSET;
  879. goto cleanup;
  880. }
  881. local_dir_header_ofs += num_alignment_padding_bytes;
  882. zip->entry.m_time = time(NULL);
  883. #ifndef MINIZ_NO_TIME
  884. mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
  885. #endif
  886. // ZIP64 header with NULL sizes (sizes will be in the data descriptor, just
  887. // after file data)
  888. extra_size = mz_zip_writer_create_zip64_extra_data(
  889. extra_data, NULL, NULL,
  890. (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
  891. if (!mz_zip_writer_create_local_dir_header(
  892. pzip, zip->entry.header, (mz_uint16)entrylen, (mz_uint16)extra_size, 0, 0, 0,
  893. zip->entry.method,
  894. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 |
  895. MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR,
  896. dos_time, dos_date)) {
  897. // Cannot create zip entry header
  898. err = ZIP_EMEMSET;
  899. goto cleanup;
  900. }
  901. zip->entry.header_offset = zip->entry.offset + num_alignment_padding_bytes;
  902. if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.header_offset,
  903. zip->entry.header,
  904. sizeof(zip->entry.header)) != sizeof(zip->entry.header)) {
  905. // Cannot write zip entry header
  906. err = ZIP_EMEMSET;
  907. goto cleanup;
  908. }
  909. if (pzip->m_file_offset_alignment) {
  910. MZ_ASSERT(
  911. (zip->entry.header_offset & (pzip->m_file_offset_alignment - 1)) == 0);
  912. }
  913. zip->entry.offset += num_alignment_padding_bytes + sizeof(zip->entry.header);
  914. if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, zip->entry.name,
  915. entrylen) != entrylen) {
  916. // Cannot write data to zip entry
  917. err = ZIP_EWRTENT;
  918. goto cleanup;
  919. }
  920. zip->entry.offset += entrylen;
  921. if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, extra_data,
  922. extra_size) != extra_size) {
  923. // Cannot write ZIP64 data to zip entry
  924. err = ZIP_EWRTENT;
  925. goto cleanup;
  926. }
  927. zip->entry.offset += extra_size;
  928. if (level) {
  929. zip->entry.state.m_pZip = pzip;
  930. zip->entry.state.m_cur_archive_file_ofs = zip->entry.offset;
  931. zip->entry.state.m_comp_size = 0;
  932. if (tdefl_init(&(zip->entry.comp), mz_zip_writer_add_put_buf_callback,
  933. &(zip->entry.state),
  934. (int)tdefl_create_comp_flags_from_zip_params(
  935. (int)level, -15, MZ_DEFAULT_STRATEGY)) !=
  936. TDEFL_STATUS_OKAY) {
  937. // Cannot initialize the zip compressor
  938. err = ZIP_ETDEFLINIT;
  939. goto cleanup;
  940. }
  941. }
  942. return 0;
  943. cleanup:
  944. CLEANUP(zip->entry.name);
  945. return err;
  946. }
  947. int zip_entry_open(struct zip_t *zip, const char *entryname) {
  948. return _zip_entry_open(zip, entryname, 0);
  949. }
  950. int zip_entry_opencasesensitive(struct zip_t *zip, const char *entryname) {
  951. return _zip_entry_open(zip, entryname, 1);
  952. }
  953. int zip_entry_openbyindex(struct zip_t *zip, size_t index) {
  954. mz_zip_archive *pZip = NULL;
  955. mz_zip_archive_file_stat stats;
  956. mz_uint namelen;
  957. const mz_uint8 *pHeader;
  958. const char *pFilename;
  959. if (!zip) {
  960. // zip_t handler is not initialized
  961. return ZIP_ENOINIT;
  962. }
  963. pZip = &(zip->archive);
  964. if (pZip->m_zip_mode != MZ_ZIP_MODE_READING) {
  965. // open by index requires readonly mode
  966. return ZIP_EINVMODE;
  967. }
  968. if (index >= (size_t)pZip->m_total_files) {
  969. // index out of range
  970. return ZIP_EINVIDX;
  971. }
  972. if (!(pHeader = &MZ_ZIP_ARRAY_ELEMENT(
  973. &pZip->m_pState->m_central_dir, mz_uint8,
  974. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets,
  975. mz_uint32, index)))) {
  976. // cannot find header in central directory
  977. return ZIP_ENOHDR;
  978. }
  979. namelen = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  980. pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  981. /*
  982. .ZIP File Format Specification Version: 6.3.3
  983. 4.4.17.1 The name of the file, with optional relative path.
  984. The path stored MUST not contain a drive or
  985. device letter, or a leading slash. All slashes
  986. MUST be forward slashes '/' as opposed to
  987. backwards slashes '\' for compatibility with Amiga
  988. and UNIX file systems etc. If input came from standard
  989. input, there is no file name field.
  990. */
  991. if (zip->entry.name) {
  992. CLEANUP(zip->entry.name);
  993. }
  994. #ifdef ZIP_RAW_ENTRYNAME
  995. zip->entry.name = STRCLONE(pFilename);
  996. #else
  997. zip->entry.name = zip_strrpl(pFilename, namelen, '\\', '/');
  998. #endif
  999. if (!zip->entry.name) {
  1000. // local entry name is NULL
  1001. return ZIP_EINVENTNAME;
  1002. }
  1003. if (!mz_zip_reader_file_stat(pZip, (mz_uint)index, &stats)) {
  1004. return ZIP_ENOENT;
  1005. }
  1006. zip->entry.index = (ssize_t)index;
  1007. zip->entry.comp_size = stats.m_comp_size;
  1008. zip->entry.uncomp_size = stats.m_uncomp_size;
  1009. zip->entry.uncomp_crc32 = stats.m_crc32;
  1010. zip->entry.offset = stats.m_central_dir_ofs;
  1011. zip->entry.header_offset = stats.m_local_header_ofs;
  1012. zip->entry.method = stats.m_method;
  1013. zip->entry.external_attr = stats.m_external_attr;
  1014. #ifndef MINIZ_NO_TIME
  1015. zip->entry.m_time = stats.m_time;
  1016. #endif
  1017. return 0;
  1018. }
  1019. int zip_entry_close(struct zip_t *zip) {
  1020. mz_zip_archive *pzip = NULL;
  1021. mz_uint level;
  1022. tdefl_status done;
  1023. mz_uint16 entrylen;
  1024. mz_uint16 dos_time = 0, dos_date = 0;
  1025. int err = 0;
  1026. mz_uint8 *pExtra_data = NULL;
  1027. mz_uint32 extra_size = 0;
  1028. mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
  1029. mz_uint8 local_dir_footer[MZ_ZIP_DATA_DESCRIPTER_SIZE64];
  1030. mz_uint32 local_dir_footer_size = MZ_ZIP_DATA_DESCRIPTER_SIZE64;
  1031. if (!zip) {
  1032. // zip_t handler is not initialized
  1033. err = ZIP_ENOINIT;
  1034. goto cleanup;
  1035. }
  1036. pzip = &(zip->archive);
  1037. if (pzip->m_zip_mode == MZ_ZIP_MODE_READING) {
  1038. goto cleanup;
  1039. }
  1040. level = zip->level & 0xF;
  1041. if (level) {
  1042. done = tdefl_compress_buffer(&(zip->entry.comp), "", 0, TDEFL_FINISH);
  1043. if (done != TDEFL_STATUS_DONE && done != TDEFL_STATUS_OKAY) {
  1044. // Cannot flush compressed buffer
  1045. err = ZIP_ETDEFLBUF;
  1046. goto cleanup;
  1047. }
  1048. zip->entry.comp_size = zip->entry.state.m_comp_size;
  1049. zip->entry.offset = zip->entry.state.m_cur_archive_file_ofs;
  1050. zip->entry.method = MZ_DEFLATED;
  1051. }
  1052. entrylen = (mz_uint16)strlen(zip->entry.name);
  1053. #ifndef MINIZ_NO_TIME
  1054. mz_zip_time_t_to_dos_time(zip->entry.m_time, &dos_time, &dos_date);
  1055. #endif
  1056. MZ_WRITE_LE32(local_dir_footer + 0, MZ_ZIP_DATA_DESCRIPTOR_ID);
  1057. MZ_WRITE_LE32(local_dir_footer + 4, zip->entry.uncomp_crc32);
  1058. MZ_WRITE_LE64(local_dir_footer + 8, zip->entry.comp_size);
  1059. MZ_WRITE_LE64(local_dir_footer + 16, zip->entry.uncomp_size);
  1060. if (pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, local_dir_footer,
  1061. local_dir_footer_size) != local_dir_footer_size) {
  1062. // Cannot write zip entry header
  1063. err = ZIP_EWRTHDR;
  1064. goto cleanup;
  1065. }
  1066. zip->entry.offset += local_dir_footer_size;
  1067. pExtra_data = extra_data;
  1068. extra_size = mz_zip_writer_create_zip64_extra_data(
  1069. extra_data,
  1070. (zip->entry.uncomp_size >= MZ_UINT32_MAX) ? &zip->entry.uncomp_size
  1071. : NULL,
  1072. (zip->entry.comp_size >= MZ_UINT32_MAX) ? &zip->entry.comp_size : NULL,
  1073. (zip->entry.header_offset >= MZ_UINT32_MAX) ? &zip->entry.header_offset
  1074. : NULL);
  1075. if ((entrylen) && (zip->entry.name[entrylen - 1] == '/') &&
  1076. !zip->entry.uncomp_size) {
  1077. /* Set DOS Subdirectory attribute bit. */
  1078. zip->entry.external_attr |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
  1079. }
  1080. if (!mz_zip_writer_add_to_central_dir(
  1081. pzip, zip->entry.name, entrylen, pExtra_data, (mz_uint16)extra_size,
  1082. "", 0, zip->entry.uncomp_size, zip->entry.comp_size,
  1083. zip->entry.uncomp_crc32, zip->entry.method,
  1084. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_UTF8 |
  1085. MZ_ZIP_LDH_BIT_FLAG_HAS_LOCATOR,
  1086. dos_time, dos_date, zip->entry.header_offset,
  1087. zip->entry.external_attr, NULL, 0)) {
  1088. // Cannot write to zip central dir
  1089. err = ZIP_EWRTDIR;
  1090. goto cleanup;
  1091. }
  1092. pzip->m_total_files++;
  1093. pzip->m_archive_size = zip->entry.offset;
  1094. cleanup:
  1095. if (zip) {
  1096. zip->entry.m_time = 0;
  1097. CLEANUP(zip->entry.name);
  1098. }
  1099. return err;
  1100. }
  1101. const char *zip_entry_name(struct zip_t *zip) {
  1102. if (!zip) {
  1103. // zip_t handler is not initialized
  1104. return NULL;
  1105. }
  1106. return zip->entry.name;
  1107. }
  1108. ssize_t zip_entry_index(struct zip_t *zip) {
  1109. if (!zip) {
  1110. // zip_t handler is not initialized
  1111. return (ssize_t)ZIP_ENOINIT;
  1112. }
  1113. return zip->entry.index;
  1114. }
  1115. int zip_entry_isdir(struct zip_t *zip) {
  1116. if (!zip) {
  1117. // zip_t handler is not initialized
  1118. return ZIP_ENOINIT;
  1119. }
  1120. if (zip->entry.index < (ssize_t)0) {
  1121. // zip entry is not opened
  1122. return ZIP_EINVIDX;
  1123. }
  1124. return (int)mz_zip_reader_is_file_a_directory(&zip->archive,
  1125. (mz_uint)zip->entry.index);
  1126. }
  1127. unsigned long long zip_entry_size(struct zip_t *zip) {
  1128. return zip_entry_uncomp_size(zip);
  1129. }
  1130. unsigned long long zip_entry_uncomp_size(struct zip_t *zip) {
  1131. return zip ? zip->entry.uncomp_size : 0;
  1132. }
  1133. unsigned long long zip_entry_comp_size(struct zip_t *zip) {
  1134. return zip ? zip->entry.comp_size : 0;
  1135. }
  1136. unsigned int zip_entry_crc32(struct zip_t *zip) {
  1137. return zip ? zip->entry.uncomp_crc32 : 0;
  1138. }
  1139. int zip_entry_write(struct zip_t *zip, const void *buf, size_t bufsize) {
  1140. mz_uint level;
  1141. mz_zip_archive *pzip = NULL;
  1142. tdefl_status status;
  1143. if (!zip) {
  1144. // zip_t handler is not initialized
  1145. return ZIP_ENOINIT;
  1146. }
  1147. pzip = &(zip->archive);
  1148. if (buf && bufsize > 0) {
  1149. zip->entry.uncomp_size += bufsize;
  1150. zip->entry.uncomp_crc32 = (mz_uint32)mz_crc32(
  1151. zip->entry.uncomp_crc32, (const mz_uint8 *)buf, bufsize);
  1152. level = zip->level & 0xF;
  1153. if (!level) {
  1154. if ((pzip->m_pWrite(pzip->m_pIO_opaque, zip->entry.offset, buf,
  1155. bufsize) != bufsize)) {
  1156. // Cannot write buffer
  1157. return ZIP_EWRTENT;
  1158. }
  1159. zip->entry.offset += bufsize;
  1160. zip->entry.comp_size += bufsize;
  1161. } else {
  1162. status = tdefl_compress_buffer(&(zip->entry.comp), buf, bufsize,
  1163. TDEFL_NO_FLUSH);
  1164. if (status != TDEFL_STATUS_DONE && status != TDEFL_STATUS_OKAY) {
  1165. // Cannot compress buffer
  1166. return ZIP_ETDEFLBUF;
  1167. }
  1168. }
  1169. }
  1170. return 0;
  1171. }
  1172. int zip_entry_fwrite(struct zip_t *zip, const char *filename) {
  1173. int err = 0;
  1174. size_t n = 0;
  1175. MZ_FILE *stream = NULL;
  1176. mz_uint8 buf[MZ_ZIP_MAX_IO_BUF_SIZE];
  1177. struct MZ_FILE_STAT_STRUCT file_stat;
  1178. mz_uint16 modes;
  1179. if (!zip) {
  1180. // zip_t handler is not initialized
  1181. return ZIP_ENOINIT;
  1182. }
  1183. memset(buf, 0, MZ_ZIP_MAX_IO_BUF_SIZE);
  1184. memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT));
  1185. if (MZ_FILE_STAT(filename, &file_stat) != 0) {
  1186. // problem getting information - check errno
  1187. return ZIP_ENOENT;
  1188. }
  1189. #if defined(_WIN32) || defined(__WIN32__) || defined(DJGPP)
  1190. (void)modes; // unused
  1191. #else
  1192. /* Initialize with permission bits--which are not implementation-optional */
  1193. modes = file_stat.st_mode &
  1194. (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
  1195. if (S_ISDIR(file_stat.st_mode))
  1196. modes |= UNX_IFDIR;
  1197. if (S_ISREG(file_stat.st_mode))
  1198. modes |= UNX_IFREG;
  1199. if (S_ISLNK(file_stat.st_mode))
  1200. modes |= UNX_IFLNK;
  1201. if (S_ISBLK(file_stat.st_mode))
  1202. modes |= UNX_IFBLK;
  1203. if (S_ISCHR(file_stat.st_mode))
  1204. modes |= UNX_IFCHR;
  1205. if (S_ISFIFO(file_stat.st_mode))
  1206. modes |= UNX_IFIFO;
  1207. if (S_ISSOCK(file_stat.st_mode))
  1208. modes |= UNX_IFSOCK;
  1209. zip->entry.external_attr = (modes << 16) | !(file_stat.st_mode & S_IWUSR);
  1210. if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
  1211. zip->entry.external_attr |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
  1212. }
  1213. #endif
  1214. zip->entry.m_time = file_stat.st_mtime;
  1215. if (!(stream = MZ_FOPEN(filename, "rb"))) {
  1216. // Cannot open filename
  1217. return ZIP_EOPNFILE;
  1218. }
  1219. while ((n = fread(buf, sizeof(mz_uint8), MZ_ZIP_MAX_IO_BUF_SIZE, stream)) >
  1220. 0) {
  1221. if (zip_entry_write(zip, buf, n) < 0) {
  1222. err = ZIP_EWRTENT;
  1223. break;
  1224. }
  1225. }
  1226. fclose(stream);
  1227. return err;
  1228. }
  1229. ssize_t zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize) {
  1230. mz_zip_archive *pzip = NULL;
  1231. mz_uint idx;
  1232. size_t size = 0;
  1233. if (!zip) {
  1234. // zip_t handler is not initialized
  1235. return (ssize_t)ZIP_ENOINIT;
  1236. }
  1237. pzip = &(zip->archive);
  1238. if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
  1239. zip->entry.index < (ssize_t)0) {
  1240. // the entry is not found or we do not have read access
  1241. return (ssize_t)ZIP_ENOENT;
  1242. }
  1243. idx = (mz_uint)zip->entry.index;
  1244. if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
  1245. // the entry is a directory
  1246. return (ssize_t)ZIP_EINVENTTYPE;
  1247. }
  1248. *buf = mz_zip_reader_extract_to_heap(pzip, idx, &size, 0);
  1249. if (*buf && bufsize) {
  1250. *bufsize = size;
  1251. }
  1252. return (ssize_t)size;
  1253. }
  1254. ssize_t zip_entry_noallocread(struct zip_t *zip, void *buf, size_t bufsize) {
  1255. mz_zip_archive *pzip = NULL;
  1256. if (!zip) {
  1257. // zip_t handler is not initialized
  1258. return (ssize_t)ZIP_ENOINIT;
  1259. }
  1260. pzip = &(zip->archive);
  1261. if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
  1262. zip->entry.index < (ssize_t)0) {
  1263. // the entry is not found or we do not have read access
  1264. return (ssize_t)ZIP_ENOENT;
  1265. }
  1266. if (!mz_zip_reader_extract_to_mem_no_alloc(pzip, (mz_uint)zip->entry.index,
  1267. buf, bufsize, 0, NULL, 0)) {
  1268. return (ssize_t)ZIP_EMEMNOALLOC;
  1269. }
  1270. return (ssize_t)zip->entry.uncomp_size;
  1271. }
  1272. int zip_entry_fread(struct zip_t *zip, const char *filename) {
  1273. mz_zip_archive *pzip = NULL;
  1274. mz_uint idx;
  1275. mz_uint32 xattr = 0;
  1276. mz_zip_archive_file_stat info;
  1277. if (!zip) {
  1278. // zip_t handler is not initialized
  1279. return ZIP_ENOINIT;
  1280. }
  1281. memset((void *)&info, 0, sizeof(mz_zip_archive_file_stat));
  1282. pzip = &(zip->archive);
  1283. if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
  1284. zip->entry.index < (ssize_t)0) {
  1285. // the entry is not found or we do not have read access
  1286. return ZIP_ENOENT;
  1287. }
  1288. idx = (mz_uint)zip->entry.index;
  1289. if (mz_zip_reader_is_file_a_directory(pzip, idx)) {
  1290. // the entry is a directory
  1291. return ZIP_EINVENTTYPE;
  1292. }
  1293. if (!mz_zip_reader_extract_to_file(pzip, idx, filename, 0)) {
  1294. return ZIP_ENOFILE;
  1295. }
  1296. #if defined(_MSC_VER) || defined(PS4)
  1297. (void)xattr; // unused
  1298. #else
  1299. if (!mz_zip_reader_file_stat(pzip, idx, &info)) {
  1300. // Cannot get information about zip archive;
  1301. return ZIP_ENOFILE;
  1302. }
  1303. xattr = (info.m_external_attr >> 16) & 0xFFFF;
  1304. if (xattr > 0 && xattr <= MZ_UINT16_MAX) {
  1305. if (CHMOD(filename, (mode_t)xattr) < 0) {
  1306. return ZIP_ENOPERM;
  1307. }
  1308. }
  1309. #endif
  1310. return 0;
  1311. }
  1312. int zip_entry_extract(struct zip_t *zip,
  1313. size_t (*on_extract)(void *arg, uint64_t offset,
  1314. const void *buf, size_t bufsize),
  1315. void *arg) {
  1316. mz_zip_archive *pzip = NULL;
  1317. mz_uint idx;
  1318. if (!zip) {
  1319. // zip_t handler is not initialized
  1320. return ZIP_ENOINIT;
  1321. }
  1322. pzip = &(zip->archive);
  1323. if (pzip->m_zip_mode != MZ_ZIP_MODE_READING ||
  1324. zip->entry.index < (ssize_t)0) {
  1325. // the entry is not found or we do not have read access
  1326. return ZIP_ENOENT;
  1327. }
  1328. idx = (mz_uint)zip->entry.index;
  1329. return (mz_zip_reader_extract_to_callback(pzip, idx, on_extract, arg, 0))
  1330. ? 0
  1331. : ZIP_EINVIDX;
  1332. }
  1333. ssize_t zip_entries_total(struct zip_t *zip) {
  1334. if (!zip) {
  1335. // zip_t handler is not initialized
  1336. return ZIP_ENOINIT;
  1337. }
  1338. return (ssize_t)zip->archive.m_total_files;
  1339. }
  1340. ssize_t zip_entries_delete(struct zip_t *zip, char *const entries[],
  1341. size_t len) {
  1342. ssize_t n = 0;
  1343. ssize_t err = 0;
  1344. struct zip_entry_mark_t *entry_mark = NULL;
  1345. if (zip == NULL || (entries == NULL && len != 0)) {
  1346. return ZIP_ENOINIT;
  1347. }
  1348. if (entries == NULL && len == 0) {
  1349. return 0;
  1350. }
  1351. n = zip_entries_total(zip);
  1352. entry_mark = (struct zip_entry_mark_t *)calloc(
  1353. (size_t)n, sizeof(struct zip_entry_mark_t));
  1354. if (!entry_mark) {
  1355. return ZIP_EOOMEM;
  1356. }
  1357. zip->archive.m_zip_mode = MZ_ZIP_MODE_READING;
  1358. err = zip_entry_set(zip, entry_mark, n, entries, len);
  1359. if (err < 0) {
  1360. CLEANUP(entry_mark);
  1361. return err;
  1362. }
  1363. err = zip_entries_delete_mark(zip, entry_mark, (int)n);
  1364. CLEANUP(entry_mark);
  1365. return err;
  1366. }
  1367. int zip_stream_extract(const char *stream, size_t size, const char *dir,
  1368. int (*on_extract)(const char *filename, void *arg),
  1369. void *arg) {
  1370. mz_zip_archive zip_archive;
  1371. if (!stream || !dir) {
  1372. // Cannot parse zip archive stream
  1373. return ZIP_ENOINIT;
  1374. }
  1375. if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
  1376. // Cannot memset zip archive
  1377. return ZIP_EMEMSET;
  1378. }
  1379. if (!mz_zip_reader_init_mem(&zip_archive, stream, size, 0)) {
  1380. // Cannot initialize zip_archive reader
  1381. return ZIP_ENOINIT;
  1382. }
  1383. return zip_archive_extract(&zip_archive, dir, on_extract, arg);
  1384. }
  1385. struct zip_t *zip_stream_open(const char *stream, size_t size, int level,
  1386. char mode) {
  1387. struct zip_t *zip = (struct zip_t *)calloc((size_t)1, sizeof(struct zip_t));
  1388. if (!zip) {
  1389. return NULL;
  1390. }
  1391. if (level < 0) {
  1392. level = MZ_DEFAULT_LEVEL;
  1393. }
  1394. if ((level & 0xF) > MZ_UBER_COMPRESSION) {
  1395. // Wrong compression level
  1396. goto cleanup;
  1397. }
  1398. zip->level = (mz_uint)level;
  1399. if ((stream != NULL) && (size > 0) && (mode == 'r')) {
  1400. if (!mz_zip_reader_init_mem(&(zip->archive), stream, size, 0)) {
  1401. goto cleanup;
  1402. }
  1403. } else if ((stream == NULL) && (size == 0) && (mode == 'w')) {
  1404. // Create a new archive.
  1405. if (!mz_zip_writer_init_heap(&(zip->archive), 0, 1024)) {
  1406. // Cannot initialize zip_archive writer
  1407. goto cleanup;
  1408. }
  1409. } else {
  1410. goto cleanup;
  1411. }
  1412. return zip;
  1413. cleanup:
  1414. CLEANUP(zip);
  1415. return NULL;
  1416. }
  1417. ssize_t zip_stream_copy(struct zip_t *zip, void **buf, size_t *bufsize) {
  1418. size_t n;
  1419. if (!zip) {
  1420. return (ssize_t)ZIP_ENOINIT;
  1421. }
  1422. zip_archive_finalize(&(zip->archive));
  1423. n = (size_t)zip->archive.m_archive_size;
  1424. if (bufsize != NULL) {
  1425. *bufsize = n;
  1426. }
  1427. *buf = calloc(sizeof(unsigned char), n);
  1428. memcpy(*buf, zip->archive.m_pState->m_pMem, n);
  1429. return (ssize_t)n;
  1430. }
  1431. void zip_stream_close(struct zip_t *zip) {
  1432. if (zip) {
  1433. mz_zip_writer_end(&(zip->archive));
  1434. mz_zip_reader_end(&(zip->archive));
  1435. CLEANUP(zip);
  1436. }
  1437. }
  1438. int zip_create(const char *zipname, const char *filenames[], size_t len) {
  1439. int err = 0;
  1440. size_t i;
  1441. mz_zip_archive zip_archive;
  1442. struct MZ_FILE_STAT_STRUCT file_stat;
  1443. mz_uint32 ext_attributes = 0;
  1444. mz_uint16 modes;
  1445. if (!zipname || strlen(zipname) < 1) {
  1446. // zip_t archive name is empty or NULL
  1447. return ZIP_EINVZIPNAME;
  1448. }
  1449. // Create a new archive.
  1450. if (!memset(&(zip_archive), 0, sizeof(zip_archive))) {
  1451. // Cannot memset zip archive
  1452. return ZIP_EMEMSET;
  1453. }
  1454. if (!mz_zip_writer_init_file(&zip_archive, zipname, 0)) {
  1455. // Cannot initialize zip_archive writer
  1456. return ZIP_ENOINIT;
  1457. }
  1458. if (!memset((void *)&file_stat, 0, sizeof(struct MZ_FILE_STAT_STRUCT))) {
  1459. return ZIP_EMEMSET;
  1460. }
  1461. for (i = 0; i < len; ++i) {
  1462. const char *name = filenames[i];
  1463. if (!name) {
  1464. err = ZIP_EINVENTNAME;
  1465. break;
  1466. }
  1467. if (MZ_FILE_STAT(name, &file_stat) != 0) {
  1468. // problem getting information - check errno
  1469. err = ZIP_ENOFILE;
  1470. break;
  1471. }
  1472. #if defined(_WIN32) || defined(__WIN32__) || defined(DJGPP)
  1473. (void)modes; // unused
  1474. #else
  1475. /* Initialize with permission bits--which are not implementation-optional */
  1476. modes = file_stat.st_mode &
  1477. (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX);
  1478. if (S_ISDIR(file_stat.st_mode))
  1479. modes |= UNX_IFDIR;
  1480. if (S_ISREG(file_stat.st_mode))
  1481. modes |= UNX_IFREG;
  1482. if (S_ISLNK(file_stat.st_mode))
  1483. modes |= UNX_IFLNK;
  1484. if (S_ISBLK(file_stat.st_mode))
  1485. modes |= UNX_IFBLK;
  1486. if (S_ISCHR(file_stat.st_mode))
  1487. modes |= UNX_IFCHR;
  1488. if (S_ISFIFO(file_stat.st_mode))
  1489. modes |= UNX_IFIFO;
  1490. if (S_ISSOCK(file_stat.st_mode))
  1491. modes |= UNX_IFSOCK;
  1492. ext_attributes = (modes << 16) | !(file_stat.st_mode & S_IWUSR);
  1493. if ((file_stat.st_mode & S_IFMT) == S_IFDIR) {
  1494. ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
  1495. }
  1496. #endif
  1497. if (!mz_zip_writer_add_file(&zip_archive, zip_basename(name), name, "", 0,
  1498. ZIP_DEFAULT_COMPRESSION_LEVEL,
  1499. ext_attributes)) {
  1500. // Cannot add file to zip_archive
  1501. err = ZIP_ENOFILE;
  1502. break;
  1503. }
  1504. }
  1505. mz_zip_writer_finalize_archive(&zip_archive);
  1506. mz_zip_writer_end(&zip_archive);
  1507. return err;
  1508. }
  1509. int zip_extract(const char *zipname, const char *dir,
  1510. int (*on_extract)(const char *filename, void *arg), void *arg) {
  1511. mz_zip_archive zip_archive;
  1512. if (!zipname || !dir) {
  1513. // Cannot parse zip archive name
  1514. return ZIP_EINVZIPNAME;
  1515. }
  1516. if (!memset(&zip_archive, 0, sizeof(mz_zip_archive))) {
  1517. // Cannot memset zip archive
  1518. return ZIP_EMEMSET;
  1519. }
  1520. // Now try to open the archive.
  1521. if (!mz_zip_reader_init_file(&zip_archive, zipname, 0)) {
  1522. // Cannot initialize zip_archive reader
  1523. return ZIP_ENOINIT;
  1524. }
  1525. return zip_archive_extract(&zip_archive, dir, on_extract, arg);
  1526. }