mz_zip.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /* zip.c -- Zip manipulation
  2. Version 2.9.2, February 12, 2020
  3. part of the MiniZip project
  4. Copyright (C) 2010-2020 Nathan Moinvaziri
  5. https://github.com/nmoinvaz/minizip
  6. Copyright (C) 2009-2010 Mathias Svensson
  7. Modifications for Zip64 support
  8. http://result42.com
  9. Copyright (C) 2007-2008 Even Rouault
  10. Modifications of Unzip for Zip64
  11. Copyright (C) 1998-2010 Gilles Vollant
  12. https://www.winimage.com/zLibDll/minizip.html
  13. This program is distributed under the terms of the same license as zlib.
  14. See the accompanying LICENSE file for the full text of the license.
  15. */
  16. #include "mz.h"
  17. #include "mz_crypt.h"
  18. #include "mz_strm.h"
  19. #ifdef HAVE_BZIP2
  20. # include "mz_strm_bzip.h"
  21. #endif
  22. #ifdef HAVE_LIBCOMP
  23. # include "mz_strm_libcomp.h"
  24. #endif
  25. #ifdef HAVE_LZMA
  26. # include "mz_strm_lzma.h"
  27. #endif
  28. #include "mz_strm_mem.h"
  29. #ifdef HAVE_PKCRYPT
  30. # include "mz_strm_pkcrypt.h"
  31. #endif
  32. #ifdef HAVE_WZAES
  33. # include "mz_strm_wzaes.h"
  34. #endif
  35. #ifdef HAVE_ZLIB
  36. # include "mz_strm_zlib.h"
  37. #endif
  38. #include "mz_zip.h"
  39. #include <ctype.h> /* tolower */
  40. #include <stdio.h> /* snprintf */
  41. #if defined(_MSC_VER) || defined(__MINGW32__)
  42. # define localtime_r(t1,t2) (localtime_s(t2,t1) == 0 ? t1 : NULL)
  43. #endif
  44. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  45. # define snprintf _snprintf
  46. #endif
  47. /***************************************************************************/
  48. #define MZ_ZIP_MAGIC_LOCALHEADER (0x04034b50)
  49. #define MZ_ZIP_MAGIC_LOCALHEADERU8 { 0x50, 0x4b, 0x03, 0x04 }
  50. #define MZ_ZIP_MAGIC_CENTRALHEADER (0x02014b50)
  51. #define MZ_ZIP_MAGIC_CENTRALHEADERU8 { 0x50, 0x4b, 0x01, 0x02 }
  52. #define MZ_ZIP_MAGIC_ENDHEADER (0x06054b50)
  53. #define MZ_ZIP_MAGIC_ENDHEADERU8 { 0x50, 0x4b, 0x05, 0x06 }
  54. #define MZ_ZIP_MAGIC_ENDHEADER64 (0x06064b50)
  55. #define MZ_ZIP_MAGIC_ENDLOCHEADER64 (0x07064b50)
  56. #define MZ_ZIP_MAGIC_DATADESCRIPTOR (0x08074b50)
  57. #define MZ_ZIP_MAGIC_DATADESCRIPTORU8 { 0x50, 0x4b, 0x07, 0x08 }
  58. #define MZ_ZIP_SIZE_LD_ITEM (30)
  59. #define MZ_ZIP_SIZE_CD_ITEM (46)
  60. #define MZ_ZIP_SIZE_CD_LOCATOR64 (20)
  61. #define MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR (24)
  62. #define MZ_ZIP_OFFSET_CRC_SIZES (14)
  63. #ifndef MZ_ZIP_EOCD_MAX_BACK
  64. #define MZ_ZIP_EOCD_MAX_BACK (1 << 20)
  65. #endif
  66. /***************************************************************************/
  67. typedef struct mz_zip_s
  68. {
  69. mz_zip_file file_info;
  70. mz_zip_file local_file_info;
  71. void *stream; /* main stream */
  72. void *cd_stream; /* pointer to the stream with the cd */
  73. void *cd_mem_stream; /* memory stream for central directory */
  74. void *compress_stream; /* compression stream */
  75. void *crypt_stream; /* encryption stream */
  76. void *file_info_stream; /* memory stream for storing file info */
  77. void *local_file_info_stream; /* memory stream for storing local file info */
  78. int32_t open_mode;
  79. uint8_t recover;
  80. uint8_t data_descriptor;
  81. uint32_t disk_number_with_cd; /* number of the disk with the central dir */
  82. int64_t disk_offset_shift; /* correction for zips that have wrong offset start of cd */
  83. int64_t cd_start_pos; /* pos of the first file in the central dir stream */
  84. int64_t cd_current_pos; /* pos of the current file in the central dir */
  85. int64_t cd_offset; /* offset of start of central directory */
  86. int64_t cd_size; /* size of the central directory */
  87. uint32_t cd_signature; /* signature of central directory */
  88. uint8_t entry_scanned; /* entry header information read ok */
  89. uint8_t entry_opened; /* entry is open for read/write */
  90. uint8_t entry_raw; /* entry opened with raw mode */
  91. uint32_t entry_crc32; /* entry crc32 */
  92. uint64_t number_entry;
  93. uint16_t version_madeby;
  94. char *comment;
  95. } mz_zip;
  96. /***************************************************************************/
  97. #if 0
  98. # define mz_zip_print printf
  99. #else
  100. # define mz_zip_print(fmt,...)
  101. #endif
  102. /***************************************************************************/
  103. /* Locate the end of central directory */
  104. static int32_t mz_zip_search_eocd(void *stream, int64_t *central_pos)
  105. {
  106. int64_t file_size = 0;
  107. int64_t max_back = MZ_ZIP_EOCD_MAX_BACK;
  108. uint8_t find[4] = MZ_ZIP_MAGIC_ENDHEADERU8;
  109. int32_t err = MZ_OK;
  110. err = mz_stream_seek(stream, 0, MZ_SEEK_END);
  111. if (err != MZ_OK)
  112. return err;
  113. file_size = mz_stream_tell(stream);
  114. if (max_back <= 0 || max_back > file_size)
  115. max_back = file_size;
  116. return mz_stream_find_reverse(stream, (const void *)find, sizeof(find), max_back, central_pos);
  117. }
  118. /* Locate the end of central directory 64 of a zip file */
  119. static int32_t mz_zip_search_zip64_eocd(void *stream, const int64_t end_central_offset, int64_t *central_pos)
  120. {
  121. int64_t offset = 0;
  122. uint32_t value32 = 0;
  123. int32_t err = MZ_OK;
  124. *central_pos = 0;
  125. /* Zip64 end of central directory locator */
  126. err = mz_stream_seek(stream, end_central_offset - MZ_ZIP_SIZE_CD_LOCATOR64, MZ_SEEK_SET);
  127. /* Read locator signature */
  128. if (err == MZ_OK)
  129. {
  130. err = mz_stream_read_uint32(stream, &value32);
  131. if (value32 != MZ_ZIP_MAGIC_ENDLOCHEADER64)
  132. err = MZ_FORMAT_ERROR;
  133. }
  134. /* Number of the disk with the start of the zip64 end of central directory */
  135. if (err == MZ_OK)
  136. err = mz_stream_read_uint32(stream, &value32);
  137. /* Relative offset of the zip64 end of central directory record8 */
  138. if (err == MZ_OK)
  139. err = mz_stream_read_uint64(stream, (uint64_t *)&offset);
  140. /* Total number of disks */
  141. if (err == MZ_OK)
  142. err = mz_stream_read_uint32(stream, &value32);
  143. /* Goto end of central directory record */
  144. if (err == MZ_OK)
  145. err = mz_stream_seek(stream, (int64_t)offset, MZ_SEEK_SET);
  146. /* The signature */
  147. if (err == MZ_OK)
  148. {
  149. err = mz_stream_read_uint32(stream, &value32);
  150. if (value32 != MZ_ZIP_MAGIC_ENDHEADER64)
  151. err = MZ_FORMAT_ERROR;
  152. }
  153. if (err == MZ_OK)
  154. *central_pos = offset;
  155. return err;
  156. }
  157. /* Get info about the current file in the zip file */
  158. static int32_t mz_zip_entry_read_header(void *stream, uint8_t local, mz_zip_file *file_info, void *file_extra_stream)
  159. {
  160. uint64_t ntfs_time = 0;
  161. uint32_t reserved = 0;
  162. uint32_t magic = 0;
  163. uint32_t dos_date = 0;
  164. uint32_t field_pos = 0;
  165. uint16_t field_type = 0;
  166. uint16_t field_length = 0;
  167. uint32_t field_length_read = 0;
  168. uint16_t ntfs_attrib_id = 0;
  169. uint16_t ntfs_attrib_size = 0;
  170. uint16_t linkname_size;
  171. uint16_t value16 = 0;
  172. uint32_t value32 = 0;
  173. int64_t extrafield_pos = 0;
  174. int64_t comment_pos = 0;
  175. int64_t linkname_pos = 0;
  176. int64_t saved_pos = 0;
  177. int32_t err = MZ_OK;
  178. char *linkname = NULL;
  179. memset(file_info, 0, sizeof(mz_zip_file));
  180. /* Check the magic */
  181. err = mz_stream_read_uint32(stream, &magic);
  182. if (err == MZ_END_OF_STREAM)
  183. err = MZ_END_OF_LIST;
  184. else if (magic == MZ_ZIP_MAGIC_ENDHEADER || magic == MZ_ZIP_MAGIC_ENDHEADER64)
  185. err = MZ_END_OF_LIST;
  186. else if ((local) && (magic != MZ_ZIP_MAGIC_LOCALHEADER))
  187. err = MZ_FORMAT_ERROR;
  188. else if ((!local) && (magic != MZ_ZIP_MAGIC_CENTRALHEADER))
  189. err = MZ_FORMAT_ERROR;
  190. /* Read header fields */
  191. if (err == MZ_OK)
  192. {
  193. if (!local)
  194. err = mz_stream_read_uint16(stream, &file_info->version_madeby);
  195. if (err == MZ_OK)
  196. err = mz_stream_read_uint16(stream, &file_info->version_needed);
  197. if (err == MZ_OK)
  198. err = mz_stream_read_uint16(stream, &file_info->flag);
  199. if (err == MZ_OK)
  200. err = mz_stream_read_uint16(stream, &file_info->compression_method);
  201. if (err == MZ_OK)
  202. {
  203. err = mz_stream_read_uint32(stream, &dos_date);
  204. file_info->modified_date = mz_zip_dosdate_to_time_t(dos_date);
  205. }
  206. if (err == MZ_OK)
  207. err = mz_stream_read_uint32(stream, &file_info->crc);
  208. if (err == MZ_OK)
  209. {
  210. err = mz_stream_read_uint32(stream, &value32);
  211. file_info->compressed_size = value32;
  212. }
  213. if (err == MZ_OK)
  214. {
  215. err = mz_stream_read_uint32(stream, &value32);
  216. file_info->uncompressed_size = value32;
  217. }
  218. if (err == MZ_OK)
  219. err = mz_stream_read_uint16(stream, &file_info->filename_size);
  220. if (err == MZ_OK)
  221. err = mz_stream_read_uint16(stream, &file_info->extrafield_size);
  222. if (!local)
  223. {
  224. if (err == MZ_OK)
  225. err = mz_stream_read_uint16(stream, &file_info->comment_size);
  226. if (err == MZ_OK)
  227. {
  228. err = mz_stream_read_uint16(stream, &value16);
  229. file_info->disk_number = value16;
  230. }
  231. if (err == MZ_OK)
  232. err = mz_stream_read_uint16(stream, &file_info->internal_fa);
  233. if (err == MZ_OK)
  234. err = mz_stream_read_uint32(stream, &file_info->external_fa);
  235. if (err == MZ_OK)
  236. {
  237. err = mz_stream_read_uint32(stream, &value32);
  238. file_info->disk_offset = value32;
  239. }
  240. }
  241. }
  242. if (err == MZ_OK)
  243. err = mz_stream_seek(file_extra_stream, 0, MZ_SEEK_SET);
  244. /* Copy variable length data to memory stream for later retrieval */
  245. if ((err == MZ_OK) && (file_info->filename_size > 0))
  246. err = mz_stream_copy(file_extra_stream, stream, file_info->filename_size);
  247. mz_stream_write_uint8(file_extra_stream, 0);
  248. extrafield_pos = mz_stream_tell(file_extra_stream);
  249. if ((err == MZ_OK) && (file_info->extrafield_size > 0))
  250. err = mz_stream_copy(file_extra_stream, stream, file_info->extrafield_size);
  251. mz_stream_write_uint8(file_extra_stream, 0);
  252. comment_pos = mz_stream_tell(file_extra_stream);
  253. if ((err == MZ_OK) && (file_info->comment_size > 0))
  254. err = mz_stream_copy(file_extra_stream, stream, file_info->comment_size);
  255. mz_stream_write_uint8(file_extra_stream, 0);
  256. linkname_pos = mz_stream_tell(file_extra_stream);
  257. /* Overwrite if we encounter UNIX1 extra block */
  258. mz_stream_write_uint8(file_extra_stream, 0);
  259. if ((err == MZ_OK) && (file_info->extrafield_size > 0))
  260. {
  261. /* Seek to and parse the extra field */
  262. err = mz_stream_seek(file_extra_stream, extrafield_pos, MZ_SEEK_SET);
  263. while ((err == MZ_OK) && (field_pos + 4 <= file_info->extrafield_size))
  264. {
  265. err = mz_zip_extrafield_read(file_extra_stream, &field_type, &field_length);
  266. if (err != MZ_OK)
  267. break;
  268. field_pos += 4;
  269. /* Don't allow field length to exceed size of remaining extrafield */
  270. if (field_length > (file_info->extrafield_size - field_pos))
  271. field_length = (uint16_t)(file_info->extrafield_size - field_pos);
  272. /* Read ZIP64 extra field */
  273. if ((field_type == MZ_ZIP_EXTENSION_ZIP64) && (field_length >= 8))
  274. {
  275. if ((err == MZ_OK) && (file_info->uncompressed_size == UINT32_MAX))
  276. {
  277. err = mz_stream_read_int64(file_extra_stream, &file_info->uncompressed_size);
  278. if (file_info->uncompressed_size < 0)
  279. err = MZ_FORMAT_ERROR;
  280. }
  281. if ((err == MZ_OK) && (file_info->compressed_size == UINT32_MAX))
  282. {
  283. err = mz_stream_read_int64(file_extra_stream, &file_info->compressed_size);
  284. if (file_info->compressed_size < 0)
  285. err = MZ_FORMAT_ERROR;
  286. }
  287. if ((err == MZ_OK) && (file_info->disk_offset == UINT32_MAX))
  288. {
  289. err = mz_stream_read_int64(file_extra_stream, &file_info->disk_offset);
  290. if (file_info->disk_offset < 0)
  291. err = MZ_FORMAT_ERROR;
  292. }
  293. if ((err == MZ_OK) && (file_info->disk_number == UINT16_MAX))
  294. err = mz_stream_read_uint32(file_extra_stream, &file_info->disk_number);
  295. }
  296. /* Read NTFS extra field */
  297. else if ((field_type == MZ_ZIP_EXTENSION_NTFS) && (field_length > 4))
  298. {
  299. if (err == MZ_OK)
  300. err = mz_stream_read_uint32(file_extra_stream, &reserved);
  301. field_length_read = 4;
  302. while ((err == MZ_OK) && (field_length_read + 4 <= field_length))
  303. {
  304. err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_id);
  305. if (err == MZ_OK)
  306. err = mz_stream_read_uint16(file_extra_stream, &ntfs_attrib_size);
  307. field_length_read += 4;
  308. if ((err == MZ_OK) && (ntfs_attrib_id == 0x01) && (ntfs_attrib_size == 24))
  309. {
  310. err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
  311. mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->modified_date);
  312. if (err == MZ_OK)
  313. {
  314. err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
  315. mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->accessed_date);
  316. }
  317. if (err == MZ_OK)
  318. {
  319. err = mz_stream_read_uint64(file_extra_stream, &ntfs_time);
  320. mz_zip_ntfs_to_unix_time(ntfs_time, &file_info->creation_date);
  321. }
  322. }
  323. else if ((err == MZ_OK) && (field_length_read + ntfs_attrib_size <= field_length))
  324. {
  325. err = mz_stream_seek(file_extra_stream, ntfs_attrib_size, MZ_SEEK_CUR);
  326. }
  327. field_length_read += ntfs_attrib_size;
  328. }
  329. }
  330. /* Read UNIX1 extra field */
  331. else if ((field_type == MZ_ZIP_EXTENSION_UNIX1) && (field_length >= 12))
  332. {
  333. if (err == MZ_OK)
  334. {
  335. err = mz_stream_read_uint32(file_extra_stream, &value32);
  336. if (err == MZ_OK && file_info->accessed_date == 0)
  337. file_info->accessed_date = value32;
  338. }
  339. if (err == MZ_OK)
  340. {
  341. err = mz_stream_read_uint32(file_extra_stream, &value32);
  342. if (err == MZ_OK && file_info->modified_date == 0)
  343. file_info->modified_date = value32;
  344. }
  345. if (err == MZ_OK)
  346. err = mz_stream_read_uint16(file_extra_stream, &value16); /* User id */
  347. if (err == MZ_OK)
  348. err = mz_stream_read_uint16(file_extra_stream, &value16); /* Group id */
  349. /* Copy linkname to end of file extra stream so we can return null
  350. terminated string */
  351. linkname_size = field_length - 12;
  352. if ((err == MZ_OK) && (linkname_size > 0))
  353. {
  354. linkname = (char *)MZ_ALLOC(linkname_size);
  355. if (linkname != NULL)
  356. {
  357. if (mz_stream_read(file_extra_stream, linkname, linkname_size) != linkname_size)
  358. err = MZ_READ_ERROR;
  359. if (err == MZ_OK)
  360. {
  361. saved_pos = mz_stream_tell(file_extra_stream);
  362. mz_stream_seek(file_extra_stream, linkname_pos, MZ_SEEK_SET);
  363. mz_stream_write(file_extra_stream, linkname, linkname_size);
  364. mz_stream_write_uint8(file_extra_stream, 0);
  365. mz_stream_seek(file_extra_stream, saved_pos, MZ_SEEK_SET);
  366. }
  367. MZ_FREE(linkname);
  368. }
  369. }
  370. }
  371. #ifdef HAVE_WZAES
  372. /* Read AES extra field */
  373. else if ((field_type == MZ_ZIP_EXTENSION_AES) && (field_length == 7))
  374. {
  375. uint8_t value8 = 0;
  376. /* Verify version info */
  377. err = mz_stream_read_uint16(file_extra_stream, &value16);
  378. /* Support AE-1 and AE-2 */
  379. if (value16 != 1 && value16 != 2)
  380. err = MZ_FORMAT_ERROR;
  381. file_info->aes_version = value16;
  382. if (err == MZ_OK)
  383. err = mz_stream_read_uint8(file_extra_stream, &value8);
  384. if ((char)value8 != 'A')
  385. err = MZ_FORMAT_ERROR;
  386. if (err == MZ_OK)
  387. err = mz_stream_read_uint8(file_extra_stream, &value8);
  388. if ((char)value8 != 'E')
  389. err = MZ_FORMAT_ERROR;
  390. /* Get AES encryption strength and actual compression method */
  391. if (err == MZ_OK)
  392. {
  393. err = mz_stream_read_uint8(file_extra_stream, &value8);
  394. file_info->aes_encryption_mode = value8;
  395. }
  396. if (err == MZ_OK)
  397. {
  398. err = mz_stream_read_uint16(file_extra_stream, &value16);
  399. file_info->compression_method = value16;
  400. }
  401. }
  402. #endif
  403. else if (field_length > 0)
  404. {
  405. err = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
  406. }
  407. field_pos += field_length;
  408. }
  409. }
  410. /* Get pointers to variable length data */
  411. mz_stream_mem_get_buffer(file_extra_stream, (const void **)&file_info->filename);
  412. mz_stream_mem_get_buffer_at(file_extra_stream, extrafield_pos, (const void **)&file_info->extrafield);
  413. mz_stream_mem_get_buffer_at(file_extra_stream, comment_pos, (const void **)&file_info->comment);
  414. mz_stream_mem_get_buffer_at(file_extra_stream, linkname_pos, (const void **)&file_info->linkname);
  415. /* Set to empty string just in-case */
  416. if (file_info->filename == NULL)
  417. file_info->filename = "";
  418. if (file_info->extrafield == NULL)
  419. file_info->extrafield_size = 0;
  420. if (file_info->comment == NULL)
  421. file_info->comment = "";
  422. if (file_info->linkname == NULL)
  423. file_info->linkname = "";
  424. if (err == MZ_OK)
  425. {
  426. mz_zip_print("Zip - Entry - Read header - %s (local %" PRId8 ")\n",
  427. file_info->filename, local);
  428. mz_zip_print("Zip - Entry - Read header compress (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
  429. file_info->uncompressed_size, file_info->compressed_size, file_info->crc);
  430. if (!local)
  431. {
  432. mz_zip_print("Zip - Entry - Read header disk (disk %" PRIu32 " offset %" PRId64 ")\n",
  433. file_info->disk_number, file_info->disk_offset);
  434. }
  435. mz_zip_print("Zip - Entry - Read header variable (fnl %" PRId32 " efs %" PRId32 " cms %" PRId32 ")\n",
  436. file_info->filename_size, file_info->extrafield_size, file_info->comment_size);
  437. }
  438. return err;
  439. }
  440. static int32_t mz_zip_entry_read_descriptor(void *stream, uint8_t zip64, uint32_t *crc32, int64_t *compressed_size, int64_t *uncompressed_size)
  441. {
  442. uint32_t value32 = 0;
  443. int64_t value64 = 0;
  444. int32_t err = MZ_OK;
  445. err = mz_stream_read_uint32(stream, &value32);
  446. if (value32 != MZ_ZIP_MAGIC_DATADESCRIPTOR)
  447. err = MZ_FORMAT_ERROR;
  448. if (err == MZ_OK)
  449. err = mz_stream_read_uint32(stream, &value32);
  450. if ((err == MZ_OK) && (crc32 != NULL))
  451. *crc32 = value32;
  452. if (err == MZ_OK)
  453. {
  454. /* If zip 64 extension is enabled then read as 8 byte */
  455. if (!zip64)
  456. {
  457. err = mz_stream_read_uint32(stream, &value32);
  458. value64 = value32;
  459. }
  460. else
  461. {
  462. err = mz_stream_read_int64(stream, &value64);
  463. if (value64 < 0)
  464. err = MZ_FORMAT_ERROR;
  465. }
  466. if ((err == MZ_OK) && (compressed_size != NULL))
  467. *compressed_size = value64;
  468. }
  469. if (err == MZ_OK)
  470. {
  471. if (!zip64)
  472. {
  473. err = mz_stream_read_uint32(stream, &value32);
  474. value64 = value32;
  475. }
  476. else
  477. {
  478. err = mz_stream_read_int64(stream, &value64);
  479. if (value64 < 0)
  480. err = MZ_FORMAT_ERROR;
  481. }
  482. if ((err == MZ_OK) && (uncompressed_size != NULL))
  483. *uncompressed_size = value64;
  484. }
  485. return err;
  486. }
  487. static int32_t mz_zip_entry_write_crc_sizes(void *stream, uint8_t mask, mz_zip_file *file_info)
  488. {
  489. int32_t err = MZ_OK;
  490. if (mask)
  491. err = mz_stream_write_uint32(stream, 0);
  492. else
  493. err = mz_stream_write_uint32(stream, file_info->crc); /* crc */
  494. if (err == MZ_OK)
  495. {
  496. if (file_info->compressed_size >= UINT32_MAX) /* compr size */
  497. err = mz_stream_write_uint32(stream, UINT32_MAX);
  498. else
  499. err = mz_stream_write_uint32(stream, (uint32_t)file_info->compressed_size);
  500. }
  501. if (err == MZ_OK)
  502. {
  503. if (file_info->uncompressed_size >= UINT32_MAX) /* uncompr size */
  504. err = mz_stream_write_uint32(stream, UINT32_MAX);
  505. else if (mask)
  506. err = mz_stream_write_uint32(stream, 0);
  507. else
  508. err = mz_stream_write_uint32(stream, (uint32_t)file_info->uncompressed_size);
  509. }
  510. return err;
  511. }
  512. static int32_t mz_zip_entry_write_header(void *stream, uint8_t local, mz_zip_file *file_info)
  513. {
  514. uint64_t ntfs_time = 0;
  515. uint32_t reserved = 0;
  516. uint32_t dos_date = 0;
  517. uint16_t extrafield_size = 0;
  518. uint16_t field_type = 0;
  519. uint16_t field_length = 0;
  520. uint16_t field_length_zip64 = 0;
  521. uint16_t field_length_ntfs = 0;
  522. uint16_t field_length_aes = 0;
  523. uint16_t field_length_unix1 = 0;
  524. uint16_t filename_size = 0;
  525. uint16_t filename_length = 0;
  526. uint16_t linkname_size = 0;
  527. uint16_t version_needed = 0;
  528. int32_t comment_size = 0;
  529. int32_t err = MZ_OK;
  530. int32_t err_mem = MZ_OK;
  531. uint8_t zip64 = 0;
  532. uint8_t skip_aes = 0;
  533. uint8_t mask = 0;
  534. uint8_t write_end_slash = 0;
  535. const char *filename = NULL;
  536. char masked_name[64];
  537. void *file_extra_stream = NULL;
  538. if (file_info == NULL)
  539. return MZ_PARAM_ERROR;
  540. if ((local) && (file_info->flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO))
  541. mask = 1;
  542. /* Calculate extra field sizes */
  543. if (file_info->uncompressed_size >= UINT32_MAX)
  544. field_length_zip64 += 8;
  545. if (file_info->compressed_size >= UINT32_MAX)
  546. field_length_zip64 += 8;
  547. if (file_info->disk_offset >= UINT32_MAX)
  548. field_length_zip64 += 8;
  549. if (file_info->zip64 == MZ_ZIP64_AUTO)
  550. {
  551. /* If uncompressed size is unknown, assume zip64 for 64-bit data descriptors */
  552. zip64 = (local && file_info->uncompressed_size == 0) || (field_length_zip64 > 0);
  553. }
  554. else if (file_info->zip64 == MZ_ZIP64_FORCE)
  555. {
  556. zip64 = 1;
  557. }
  558. else if (file_info->zip64 == MZ_ZIP64_DISABLE)
  559. {
  560. /* Zip64 extension is required to zip file */
  561. if (field_length_zip64 > 0)
  562. return MZ_PARAM_ERROR;
  563. }
  564. if (zip64)
  565. {
  566. extrafield_size += 4;
  567. extrafield_size += field_length_zip64;
  568. }
  569. /* Calculate extra field size and check for duplicates */
  570. if (file_info->extrafield_size > 0)
  571. {
  572. mz_stream_mem_create(&file_extra_stream);
  573. mz_stream_mem_set_buffer(file_extra_stream, (void *)file_info->extrafield,
  574. file_info->extrafield_size);
  575. do
  576. {
  577. err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
  578. if (err_mem == MZ_OK)
  579. err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
  580. if (err_mem != MZ_OK)
  581. break;
  582. /* Prefer incoming aes extensions over ours */
  583. if (field_type == MZ_ZIP_EXTENSION_AES)
  584. skip_aes = 1;
  585. /* Prefer our zip64, ntfs, unix1 extension over incoming */
  586. if (field_type != MZ_ZIP_EXTENSION_ZIP64 && field_type != MZ_ZIP_EXTENSION_NTFS &&
  587. field_type != MZ_ZIP_EXTENSION_UNIX1)
  588. extrafield_size += 4 + field_length;
  589. if (err_mem == MZ_OK)
  590. err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
  591. }
  592. while (err_mem == MZ_OK);
  593. }
  594. #ifdef HAVE_WZAES
  595. if (!skip_aes)
  596. {
  597. if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
  598. {
  599. field_length_aes = 1 + 1 + 1 + 2 + 2;
  600. extrafield_size += 4 + field_length_aes;
  601. }
  602. }
  603. #else
  604. MZ_UNUSED(field_length_aes);
  605. MZ_UNUSED(skip_aes);
  606. #endif
  607. /* NTFS timestamps */
  608. if ((file_info->modified_date != 0) &&
  609. (file_info->accessed_date != 0) &&
  610. (file_info->creation_date != 0) && (!mask))
  611. {
  612. field_length_ntfs = 8 + 8 + 8 + 4 + 2 + 2;
  613. extrafield_size += 4 + field_length_ntfs;
  614. }
  615. /* Unix1 symbolic links */
  616. if (file_info->linkname != NULL && *file_info->linkname != 0)
  617. {
  618. linkname_size = (uint16_t)strlen(file_info->linkname);
  619. field_length_unix1 = 12 + linkname_size;
  620. extrafield_size += 4 + field_length_unix1;
  621. }
  622. if (local)
  623. err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_LOCALHEADER);
  624. else
  625. {
  626. err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_CENTRALHEADER);
  627. if (err == MZ_OK)
  628. err = mz_stream_write_uint16(stream, file_info->version_madeby);
  629. }
  630. /* Calculate version needed to extract */
  631. if (err == MZ_OK)
  632. {
  633. version_needed = file_info->version_needed;
  634. if (version_needed == 0)
  635. {
  636. version_needed = 20;
  637. if (zip64)
  638. version_needed = 45;
  639. #ifdef HAVE_WZAES
  640. if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
  641. version_needed = 51;
  642. #endif
  643. #ifdef HAVE_LZMA
  644. if (file_info->compression_method == MZ_COMPRESS_METHOD_LZMA)
  645. version_needed = 63;
  646. #endif
  647. }
  648. err = mz_stream_write_uint16(stream, version_needed);
  649. }
  650. if (err == MZ_OK)
  651. err = mz_stream_write_uint16(stream, file_info->flag);
  652. if (err == MZ_OK)
  653. {
  654. #ifdef HAVE_WZAES
  655. if ((file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
  656. err = mz_stream_write_uint16(stream, MZ_COMPRESS_METHOD_AES);
  657. else
  658. #endif
  659. err = mz_stream_write_uint16(stream, file_info->compression_method);
  660. }
  661. if (err == MZ_OK)
  662. {
  663. if (file_info->modified_date != 0 && !mask)
  664. dos_date = mz_zip_time_t_to_dos_date(file_info->modified_date);
  665. err = mz_stream_write_uint32(stream, dos_date);
  666. }
  667. if (err == MZ_OK)
  668. err = mz_zip_entry_write_crc_sizes(stream, mask, file_info);
  669. if (mask)
  670. {
  671. snprintf(masked_name, sizeof(masked_name), "%" PRIx32 "_%" PRIx64,
  672. file_info->disk_number, file_info->disk_offset);
  673. filename = masked_name;
  674. }
  675. else
  676. {
  677. filename = file_info->filename;
  678. }
  679. filename_length = (uint16_t)strlen(filename);
  680. filename_size += filename_length;
  681. if ((mz_zip_attrib_is_dir(file_info->external_fa, file_info->version_madeby) == MZ_OK) &&
  682. ((filename[filename_length - 1] != '/') && (filename[filename_length - 1] != '\\')))
  683. {
  684. filename_size += 1;
  685. write_end_slash = 1;
  686. }
  687. if (err == MZ_OK)
  688. err = mz_stream_write_uint16(stream, filename_size);
  689. if (err == MZ_OK)
  690. err = mz_stream_write_uint16(stream, extrafield_size);
  691. if (!local)
  692. {
  693. if (file_info->comment != NULL)
  694. {
  695. comment_size = (int32_t)strlen(file_info->comment);
  696. if (comment_size > UINT16_MAX)
  697. comment_size = UINT16_MAX;
  698. }
  699. if (err == MZ_OK)
  700. err = mz_stream_write_uint16(stream, (uint16_t)comment_size);
  701. if (err == MZ_OK)
  702. err = mz_stream_write_uint16(stream, (uint16_t)file_info->disk_number);
  703. if (err == MZ_OK)
  704. err = mz_stream_write_uint16(stream, file_info->internal_fa);
  705. if (err == MZ_OK)
  706. err = mz_stream_write_uint32(stream, file_info->external_fa);
  707. if (err == MZ_OK)
  708. {
  709. if (file_info->disk_offset >= UINT32_MAX)
  710. err = mz_stream_write_uint32(stream, UINT32_MAX);
  711. else
  712. err = mz_stream_write_uint32(stream, (uint32_t)file_info->disk_offset);
  713. }
  714. }
  715. if (err == MZ_OK)
  716. {
  717. if (mz_stream_write(stream, filename, filename_length) != filename_length)
  718. err = MZ_WRITE_ERROR;
  719. /* Ensure that directories have a slash appended to them for compatibility */
  720. if (err == MZ_OK && write_end_slash)
  721. err = mz_stream_write_uint8(stream, '/');
  722. }
  723. if (file_info->extrafield_size > 0)
  724. {
  725. err_mem = mz_stream_mem_seek(file_extra_stream, 0, MZ_SEEK_SET);
  726. while (err == MZ_OK && err_mem == MZ_OK)
  727. {
  728. err_mem = mz_stream_read_uint16(file_extra_stream, &field_type);
  729. if (err_mem == MZ_OK)
  730. err_mem = mz_stream_read_uint16(file_extra_stream, &field_length);
  731. if (err_mem != MZ_OK)
  732. break;
  733. /* Prefer our zip 64, ntfs, unix1 extensions over incoming */
  734. if (field_type == MZ_ZIP_EXTENSION_ZIP64 || field_type == MZ_ZIP_EXTENSION_NTFS ||
  735. field_type == MZ_ZIP_EXTENSION_UNIX1)
  736. {
  737. err_mem = mz_stream_seek(file_extra_stream, field_length, MZ_SEEK_CUR);
  738. continue;
  739. }
  740. err = mz_stream_write_uint16(stream, field_type);
  741. if (err == MZ_OK)
  742. err = mz_stream_write_uint16(stream, field_length);
  743. if (err == MZ_OK)
  744. err = mz_stream_copy(stream, file_extra_stream, field_length);
  745. }
  746. mz_stream_mem_delete(&file_extra_stream);
  747. }
  748. /* Write ZIP64 extra field */
  749. if ((err == MZ_OK) && (zip64))
  750. {
  751. err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_ZIP64, field_length_zip64);
  752. if ((err == MZ_OK) && (file_info->uncompressed_size >= UINT32_MAX))
  753. {
  754. if (mask)
  755. err = mz_stream_write_int64(stream, 0);
  756. else
  757. err = mz_stream_write_int64(stream, file_info->uncompressed_size);
  758. }
  759. if ((err == MZ_OK) && (file_info->compressed_size >= UINT32_MAX))
  760. err = mz_stream_write_int64(stream, file_info->compressed_size);
  761. if ((err == MZ_OK) && (file_info->disk_offset >= UINT32_MAX))
  762. err = mz_stream_write_int64(stream, file_info->disk_offset);
  763. }
  764. /* Write NTFS extra field */
  765. if ((err == MZ_OK) && (field_length_ntfs > 0))
  766. {
  767. err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_NTFS, field_length_ntfs);
  768. if (err == MZ_OK)
  769. err = mz_stream_write_uint32(stream, reserved);
  770. if (err == MZ_OK)
  771. err = mz_stream_write_uint16(stream, 0x01);
  772. if (err == MZ_OK)
  773. err = mz_stream_write_uint16(stream, field_length_ntfs - 8);
  774. if (err == MZ_OK)
  775. {
  776. mz_zip_unix_to_ntfs_time(file_info->modified_date, &ntfs_time);
  777. err = mz_stream_write_uint64(stream, ntfs_time);
  778. }
  779. if (err == MZ_OK)
  780. {
  781. mz_zip_unix_to_ntfs_time(file_info->accessed_date, &ntfs_time);
  782. err = mz_stream_write_uint64(stream, ntfs_time);
  783. }
  784. if (err == MZ_OK)
  785. {
  786. mz_zip_unix_to_ntfs_time(file_info->creation_date, &ntfs_time);
  787. err = mz_stream_write_uint64(stream, ntfs_time);
  788. }
  789. }
  790. /* Write UNIX extra block extra field */
  791. if ((err == MZ_OK) && (field_length_unix1 > 0))
  792. {
  793. err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_UNIX1, field_length_unix1);
  794. if (err == MZ_OK)
  795. err = mz_stream_write_uint32(stream, (uint32_t)file_info->accessed_date);
  796. if (err == MZ_OK)
  797. err = mz_stream_write_uint32(stream, (uint32_t)file_info->modified_date);
  798. if (err == MZ_OK) /* User id */
  799. err = mz_stream_write_uint16(stream, 0);
  800. if (err == MZ_OK) /* Group id */
  801. err = mz_stream_write_uint16(stream, 0);
  802. if (err == MZ_OK && linkname_size > 0)
  803. {
  804. if (mz_stream_write(stream, file_info->linkname, linkname_size) != linkname_size)
  805. err = MZ_WRITE_ERROR;
  806. }
  807. }
  808. #ifdef HAVE_WZAES
  809. /* Write AES extra field */
  810. if ((err == MZ_OK) && (!skip_aes) && (file_info->flag & MZ_ZIP_FLAG_ENCRYPTED) && (file_info->aes_version))
  811. {
  812. err = mz_zip_extrafield_write(stream, MZ_ZIP_EXTENSION_AES, field_length_aes);
  813. if (err == MZ_OK)
  814. err = mz_stream_write_uint16(stream, file_info->aes_version);
  815. if (err == MZ_OK)
  816. err = mz_stream_write_uint8(stream, 'A');
  817. if (err == MZ_OK)
  818. err = mz_stream_write_uint8(stream, 'E');
  819. if (err == MZ_OK)
  820. err = mz_stream_write_uint8(stream, file_info->aes_encryption_mode);
  821. if (err == MZ_OK)
  822. err = mz_stream_write_uint16(stream, file_info->compression_method);
  823. }
  824. #endif
  825. if ((err == MZ_OK) && (!local) && (file_info->comment != NULL))
  826. {
  827. if (mz_stream_write(stream, file_info->comment, file_info->comment_size) != file_info->comment_size)
  828. err = MZ_WRITE_ERROR;
  829. }
  830. return err;
  831. }
  832. static int32_t mz_zip_entry_write_descriptor(void *stream, uint8_t zip64, uint32_t crc32, int64_t compressed_size, int64_t uncompressed_size)
  833. {
  834. int32_t err = MZ_OK;
  835. err = mz_stream_write_uint32(stream, MZ_ZIP_MAGIC_DATADESCRIPTOR);
  836. if (err == MZ_OK)
  837. err = mz_stream_write_uint32(stream, crc32);
  838. /* Store data descriptor as 8 bytes if zip 64 extension enabled */
  839. if (err == MZ_OK)
  840. {
  841. /* Zip 64 extension is enabled when uncompressed size is > UINT32_MAX */
  842. if (!zip64)
  843. err = mz_stream_write_uint32(stream, (uint32_t)compressed_size);
  844. else
  845. err = mz_stream_write_int64(stream, compressed_size);
  846. }
  847. if (err == MZ_OK)
  848. {
  849. if (!zip64)
  850. err = mz_stream_write_uint32(stream, (uint32_t)uncompressed_size);
  851. else
  852. err = mz_stream_write_int64(stream, uncompressed_size);
  853. }
  854. return err;
  855. }
  856. static int32_t mz_zip_read_cd(void *handle)
  857. {
  858. mz_zip *zip = (mz_zip *)handle;
  859. uint64_t number_entry_cd64 = 0;
  860. uint64_t number_entry = 0;
  861. uint64_t number_entry_cd = 0;
  862. int64_t eocd_pos = 0;
  863. int64_t eocd_pos64 = 0;
  864. int64_t value64i = 0;
  865. uint16_t value16 = 0;
  866. uint32_t value32 = 0;
  867. uint64_t value64 = 0;
  868. uint16_t comment_size = 0;
  869. int32_t comment_read = 0;
  870. int32_t err = MZ_OK;
  871. if (zip == NULL)
  872. return MZ_PARAM_ERROR;
  873. /* Read and cache central directory records */
  874. err = mz_zip_search_eocd(zip->stream, &eocd_pos);
  875. if (err == MZ_OK)
  876. {
  877. /* The signature, already checked */
  878. err = mz_stream_read_uint32(zip->stream, &value32);
  879. /* Number of this disk */
  880. if (err == MZ_OK)
  881. err = mz_stream_read_uint16(zip->stream, &value16);
  882. /* Number of the disk with the start of the central directory */
  883. if (err == MZ_OK)
  884. err = mz_stream_read_uint16(zip->stream, &value16);
  885. zip->disk_number_with_cd = value16;
  886. /* Total number of entries in the central dir on this disk */
  887. if (err == MZ_OK)
  888. err = mz_stream_read_uint16(zip->stream, &value16);
  889. zip->number_entry = value16;
  890. /* Total number of entries in the central dir */
  891. if (err == MZ_OK)
  892. err = mz_stream_read_uint16(zip->stream, &value16);
  893. number_entry_cd = value16;
  894. if (number_entry_cd != zip->number_entry)
  895. err = MZ_FORMAT_ERROR;
  896. /* Size of the central directory */
  897. if (err == MZ_OK)
  898. err = mz_stream_read_uint32(zip->stream, &value32);
  899. if (err == MZ_OK)
  900. zip->cd_size = value32;
  901. /* Offset of start of central directory with respect to the starting disk number */
  902. if (err == MZ_OK)
  903. err = mz_stream_read_uint32(zip->stream, &value32);
  904. if (err == MZ_OK)
  905. zip->cd_offset = value32;
  906. /* Zip file global comment length */
  907. if (err == MZ_OK)
  908. err = mz_stream_read_uint16(zip->stream, &comment_size);
  909. if ((err == MZ_OK) && (comment_size > 0))
  910. {
  911. zip->comment = (char *)MZ_ALLOC(comment_size + 1);
  912. if (zip->comment != NULL)
  913. {
  914. comment_read = mz_stream_read(zip->stream, zip->comment, comment_size);
  915. /* Don't fail if incorrect comment length read, not critical */
  916. if (comment_read < 0)
  917. comment_read = 0;
  918. zip->comment[comment_read] = 0;
  919. }
  920. }
  921. if ((err == MZ_OK) && ((number_entry_cd == UINT16_MAX) || (zip->cd_offset == UINT32_MAX)))
  922. {
  923. /* Format should be Zip64, as the central directory or file size is too large */
  924. if (mz_zip_search_zip64_eocd(zip->stream, eocd_pos, &eocd_pos64) == MZ_OK)
  925. {
  926. eocd_pos = eocd_pos64;
  927. err = mz_stream_seek(zip->stream, eocd_pos, MZ_SEEK_SET);
  928. /* The signature, already checked */
  929. if (err == MZ_OK)
  930. err = mz_stream_read_uint32(zip->stream, &value32);
  931. /* Size of zip64 end of central directory record */
  932. if (err == MZ_OK)
  933. err = mz_stream_read_uint64(zip->stream, &value64);
  934. /* Version made by */
  935. if (err == MZ_OK)
  936. err = mz_stream_read_uint16(zip->stream, &zip->version_madeby);
  937. /* Version needed to extract */
  938. if (err == MZ_OK)
  939. err = mz_stream_read_uint16(zip->stream, &value16);
  940. /* Number of this disk */
  941. if (err == MZ_OK)
  942. err = mz_stream_read_uint32(zip->stream, &value32);
  943. /* Number of the disk with the start of the central directory */
  944. if (err == MZ_OK)
  945. err = mz_stream_read_uint32(zip->stream, &zip->disk_number_with_cd);
  946. /* Total number of entries in the central directory on this disk */
  947. if (err == MZ_OK)
  948. err = mz_stream_read_uint64(zip->stream, &number_entry);
  949. /* Total number of entries in the central directory */
  950. if (err == MZ_OK)
  951. err = mz_stream_read_uint64(zip->stream, &number_entry_cd64);
  952. if (number_entry == UINT32_MAX)
  953. zip->number_entry = number_entry_cd64;
  954. /* Size of the central directory */
  955. if (err == MZ_OK)
  956. {
  957. err = mz_stream_read_int64(zip->stream, &zip->cd_size);
  958. if (zip->cd_size < 0)
  959. err = MZ_FORMAT_ERROR;
  960. }
  961. /* Offset of start of central directory with respect to the starting disk number */
  962. if (err == MZ_OK)
  963. {
  964. err = mz_stream_read_int64(zip->stream, &zip->cd_offset);
  965. if (zip->cd_offset < 0)
  966. err = MZ_FORMAT_ERROR;
  967. }
  968. }
  969. else if ((zip->number_entry == UINT16_MAX) || (number_entry_cd != zip->number_entry) ||
  970. (zip->cd_size == UINT16_MAX) || (zip->cd_offset == UINT32_MAX))
  971. {
  972. err = MZ_FORMAT_ERROR;
  973. }
  974. }
  975. }
  976. if (err == MZ_OK)
  977. {
  978. mz_zip_print("Zip - Read cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
  979. zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
  980. /* Verify central directory signature exists at offset */
  981. err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
  982. if (err == MZ_OK)
  983. err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
  984. if ((err == MZ_OK) && (zip->cd_signature != MZ_ZIP_MAGIC_CENTRALHEADER))
  985. {
  986. /* If cd exists in large file and no zip-64 support, error for recover */
  987. if (eocd_pos > UINT32_MAX && eocd_pos64 == 0)
  988. err = MZ_FORMAT_ERROR;
  989. /* If cd not found attempt to seek backward to find it */
  990. if (err == MZ_OK)
  991. err = mz_stream_seek(zip->stream, eocd_pos - zip->cd_size, MZ_SEEK_SET);
  992. if (err == MZ_OK)
  993. err = mz_stream_read_uint32(zip->stream, &zip->cd_signature);
  994. if ((err == MZ_OK) && (zip->cd_signature == MZ_ZIP_MAGIC_CENTRALHEADER))
  995. {
  996. /* If found compensate for incorrect locations */
  997. value64i = zip->cd_offset;
  998. zip->cd_offset = eocd_pos - zip->cd_size;
  999. /* Assume disk has prepended data */
  1000. zip->disk_offset_shift = zip->cd_offset - value64i;
  1001. }
  1002. }
  1003. }
  1004. if (err == MZ_OK)
  1005. {
  1006. if (eocd_pos < zip->cd_offset)
  1007. {
  1008. /* End of central dir should always come after central dir */
  1009. err = MZ_FORMAT_ERROR;
  1010. }
  1011. else if (eocd_pos < zip->cd_offset + zip->cd_size)
  1012. {
  1013. /* Truncate size of cd if incorrect size or offset provided */
  1014. zip->cd_size = eocd_pos - zip->cd_offset;
  1015. }
  1016. }
  1017. return err;
  1018. }
  1019. static int32_t mz_zip_write_cd(void *handle)
  1020. {
  1021. mz_zip *zip = (mz_zip *)handle;
  1022. int64_t zip64_eocd_pos_inzip = 0;
  1023. int64_t disk_number = 0;
  1024. int64_t disk_size = 0;
  1025. int32_t comment_size = 0;
  1026. int32_t err = MZ_OK;
  1027. if (zip == NULL)
  1028. return MZ_PARAM_ERROR;
  1029. if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number) == MZ_OK)
  1030. zip->disk_number_with_cd = (uint32_t)disk_number;
  1031. if (mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size) == MZ_OK && disk_size > 0)
  1032. zip->disk_number_with_cd += 1;
  1033. mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
  1034. if ((zip->disk_number_with_cd > 0) && (zip->open_mode & MZ_OPEN_MODE_APPEND))
  1035. {
  1036. // Overwrite existing central directory if using split disks
  1037. mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
  1038. }
  1039. zip->cd_offset = mz_stream_tell(zip->stream);
  1040. mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_END);
  1041. zip->cd_size = (uint32_t)mz_stream_tell(zip->cd_mem_stream);
  1042. mz_stream_seek(zip->cd_mem_stream, 0, MZ_SEEK_SET);
  1043. err = mz_stream_copy(zip->stream, zip->cd_mem_stream, (int32_t)zip->cd_size);
  1044. mz_zip_print("Zip - Write cd (disk %" PRId32 " entries %" PRId64 " offset %" PRId64 " size %" PRId64 ")\n",
  1045. zip->disk_number_with_cd, zip->number_entry, zip->cd_offset, zip->cd_size);
  1046. if (zip->cd_size == 0 && zip->number_entry > 0)
  1047. {
  1048. // Zip does not contain central directory, open with recovery option
  1049. return MZ_FORMAT_ERROR;
  1050. }
  1051. /* Write the ZIP64 central directory header */
  1052. if (zip->cd_offset >= UINT32_MAX || zip->number_entry > UINT16_MAX)
  1053. {
  1054. zip64_eocd_pos_inzip = mz_stream_tell(zip->stream);
  1055. err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER64);
  1056. /* Size of this 'zip64 end of central directory' */
  1057. if (err == MZ_OK)
  1058. err = mz_stream_write_uint64(zip->stream, (uint64_t)44);
  1059. /* Version made by */
  1060. if (err == MZ_OK)
  1061. err = mz_stream_write_uint16(zip->stream, zip->version_madeby);
  1062. /* Version needed */
  1063. if (err == MZ_OK)
  1064. err = mz_stream_write_uint16(zip->stream, (uint16_t)45);
  1065. /* Number of this disk */
  1066. if (err == MZ_OK)
  1067. err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
  1068. /* Number of the disk with the start of the central directory */
  1069. if (err == MZ_OK)
  1070. err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
  1071. /* Total number of entries in the central dir on this disk */
  1072. if (err == MZ_OK)
  1073. err = mz_stream_write_uint64(zip->stream, zip->number_entry);
  1074. /* Total number of entries in the central dir */
  1075. if (err == MZ_OK)
  1076. err = mz_stream_write_uint64(zip->stream, zip->number_entry);
  1077. /* Size of the central directory */
  1078. if (err == MZ_OK)
  1079. err = mz_stream_write_int64(zip->stream, zip->cd_size);
  1080. /* Offset of start of central directory with respect to the starting disk number */
  1081. if (err == MZ_OK)
  1082. err = mz_stream_write_int64(zip->stream, zip->cd_offset);
  1083. if (err == MZ_OK)
  1084. err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDLOCHEADER64);
  1085. /* Number of the disk with the start of the central directory */
  1086. if (err == MZ_OK)
  1087. err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd);
  1088. /* Relative offset to the end of zip64 central directory */
  1089. if (err == MZ_OK)
  1090. err = mz_stream_write_int64(zip->stream, zip64_eocd_pos_inzip);
  1091. /* Number of the disk with the start of the central directory */
  1092. if (err == MZ_OK)
  1093. err = mz_stream_write_uint32(zip->stream, zip->disk_number_with_cd + 1);
  1094. }
  1095. /* Write the central directory header */
  1096. /* Signature */
  1097. if (err == MZ_OK)
  1098. err = mz_stream_write_uint32(zip->stream, MZ_ZIP_MAGIC_ENDHEADER);
  1099. /* Number of this disk */
  1100. if (err == MZ_OK)
  1101. err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
  1102. /* Number of the disk with the start of the central directory */
  1103. if (err == MZ_OK)
  1104. err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->disk_number_with_cd);
  1105. /* Total number of entries in the central dir on this disk */
  1106. if (err == MZ_OK)
  1107. {
  1108. if (zip->number_entry >= UINT16_MAX)
  1109. err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
  1110. else
  1111. err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
  1112. }
  1113. /* Total number of entries in the central dir */
  1114. if (err == MZ_OK)
  1115. {
  1116. if (zip->number_entry >= UINT16_MAX)
  1117. err = mz_stream_write_uint16(zip->stream, UINT16_MAX);
  1118. else
  1119. err = mz_stream_write_uint16(zip->stream, (uint16_t)zip->number_entry);
  1120. }
  1121. /* Size of the central directory */
  1122. if (err == MZ_OK)
  1123. err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_size);
  1124. /* Offset of start of central directory with respect to the starting disk number */
  1125. if (err == MZ_OK)
  1126. {
  1127. if (zip->cd_offset >= UINT32_MAX)
  1128. err = mz_stream_write_uint32(zip->stream, UINT32_MAX);
  1129. else
  1130. err = mz_stream_write_uint32(zip->stream, (uint32_t)zip->cd_offset);
  1131. }
  1132. /* Write global comment */
  1133. if (zip->comment != NULL)
  1134. {
  1135. comment_size = (int32_t)strlen(zip->comment);
  1136. if (comment_size > UINT16_MAX)
  1137. comment_size = UINT16_MAX;
  1138. }
  1139. if (err == MZ_OK)
  1140. err = mz_stream_write_uint16(zip->stream, (uint16_t)comment_size);
  1141. if (err == MZ_OK)
  1142. {
  1143. if (mz_stream_write(zip->stream, zip->comment, comment_size) != comment_size)
  1144. err = MZ_READ_ERROR;
  1145. }
  1146. return err;
  1147. }
  1148. static int32_t mz_zip_recover_cd(void *handle)
  1149. {
  1150. mz_zip *zip = (mz_zip *)handle;
  1151. mz_zip_file local_file_info;
  1152. void *local_file_info_stream = NULL;
  1153. void *cd_mem_stream = NULL;
  1154. uint64_t number_entry = 0;
  1155. int64_t descriptor_pos = 0;
  1156. int64_t next_header_pos = 0;
  1157. int64_t disk_offset = 0;
  1158. int64_t disk_number = 0;
  1159. int64_t compressed_pos = 0;
  1160. int64_t compressed_end_pos = 0;
  1161. int64_t compressed_size = 0;
  1162. int64_t uncompressed_size = 0;
  1163. uint8_t descriptor_magic[4] = MZ_ZIP_MAGIC_DATADESCRIPTORU8;
  1164. uint8_t local_header_magic[4] = MZ_ZIP_MAGIC_LOCALHEADERU8;
  1165. uint8_t central_header_magic[4] = MZ_ZIP_MAGIC_CENTRALHEADERU8;
  1166. uint32_t crc32 = 0;
  1167. int32_t disk_number_with_cd = 0;
  1168. int32_t err = MZ_OK;
  1169. uint8_t zip64 = 0;
  1170. uint8_t eof = 0;
  1171. mz_zip_print("Zip - Recover - Start\n");
  1172. mz_zip_get_cd_mem_stream(handle, &cd_mem_stream);
  1173. /* Determine if we are on a split disk or not */
  1174. mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, 0);
  1175. if (mz_stream_tell(zip->stream) < 0)
  1176. {
  1177. mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
  1178. mz_stream_seek(zip->stream, 0, MZ_SEEK_SET);
  1179. }
  1180. else
  1181. disk_number_with_cd = 1;
  1182. if (mz_stream_is_open(cd_mem_stream) != MZ_OK)
  1183. err = mz_stream_mem_open(cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
  1184. mz_stream_mem_create(&local_file_info_stream);
  1185. mz_stream_mem_open(local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
  1186. if (err == MZ_OK)
  1187. {
  1188. err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
  1189. INT64_MAX, &next_header_pos);
  1190. }
  1191. while (err == MZ_OK && !eof)
  1192. {
  1193. /* Get current offset and disk number for central dir record */
  1194. disk_offset = mz_stream_tell(zip->stream);
  1195. mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
  1196. /* Read local headers */
  1197. memset(&local_file_info, 0, sizeof(local_file_info));
  1198. err = mz_zip_entry_read_header(zip->stream, 1, &local_file_info, local_file_info_stream);
  1199. if (err != MZ_OK)
  1200. break;
  1201. local_file_info.disk_offset = disk_offset;
  1202. if (disk_number < 0)
  1203. disk_number = 0;
  1204. local_file_info.disk_number = (uint32_t)disk_number;
  1205. compressed_pos = mz_stream_tell(zip->stream);
  1206. if ((err == MZ_OK) && (local_file_info.compressed_size > 0))
  1207. {
  1208. mz_stream_seek(zip->stream, local_file_info.compressed_size, MZ_SEEK_CUR);
  1209. }
  1210. while (1)
  1211. {
  1212. /* Search for the next local header */
  1213. err = mz_stream_find(zip->stream, (const void *)local_header_magic, sizeof(local_header_magic),
  1214. INT64_MAX, &next_header_pos);
  1215. if (err == MZ_EXIST_ERROR)
  1216. {
  1217. mz_stream_seek(zip->stream, compressed_pos, MZ_SEEK_SET);
  1218. /* Search for central dir if no local header found */
  1219. err = mz_stream_find(zip->stream, (const void *)central_header_magic, sizeof(central_header_magic),
  1220. INT64_MAX, &next_header_pos);
  1221. if (err == MZ_EXIST_ERROR)
  1222. {
  1223. /* Get end of stream if no central header found */
  1224. mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
  1225. next_header_pos = mz_stream_tell(zip->stream);
  1226. }
  1227. eof = 1;
  1228. }
  1229. if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR || local_file_info.compressed_size == 0)
  1230. {
  1231. /* Search backwards for the descriptor, seeking too far back will be incorrect if compressed size is small */
  1232. err = mz_stream_find_reverse(zip->stream, (const void *)descriptor_magic, sizeof(descriptor_magic),
  1233. MZ_ZIP_SIZE_MAX_DATA_DESCRIPTOR, &descriptor_pos);
  1234. if (err == MZ_OK)
  1235. {
  1236. if (mz_zip_extrafield_contains(local_file_info.extrafield,
  1237. local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
  1238. zip64 = 1;
  1239. err = mz_zip_entry_read_descriptor(zip->stream, zip64, &crc32,
  1240. &compressed_size, &uncompressed_size);
  1241. if (err == MZ_OK)
  1242. {
  1243. if (local_file_info.crc == 0)
  1244. local_file_info.crc = crc32;
  1245. if (local_file_info.compressed_size == 0)
  1246. local_file_info.compressed_size = compressed_size;
  1247. if (local_file_info.uncompressed_size == 0)
  1248. local_file_info.uncompressed_size = uncompressed_size;
  1249. }
  1250. compressed_end_pos = descriptor_pos;
  1251. }
  1252. else if (local_file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
  1253. {
  1254. /* Wrong local file entry found, keep searching */
  1255. next_header_pos += 1;
  1256. mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
  1257. continue;
  1258. }
  1259. }
  1260. else
  1261. {
  1262. compressed_end_pos = next_header_pos;
  1263. }
  1264. break;
  1265. }
  1266. compressed_size = compressed_end_pos - compressed_pos;
  1267. if (compressed_size > UINT32_MAX)
  1268. {
  1269. /* Update sizes if 4GB file is written with no ZIP64 support */
  1270. if (local_file_info.uncompressed_size < UINT32_MAX)
  1271. {
  1272. local_file_info.compressed_size = compressed_size;
  1273. local_file_info.uncompressed_size = 0;
  1274. }
  1275. }
  1276. mz_zip_print("Zip - Recover - Entry %s (csize %" PRId64 " usize %" PRId64 " flags 0x%" PRIx16 ")\n",
  1277. local_file_info.filename, local_file_info.compressed_size, local_file_info.uncompressed_size,
  1278. local_file_info.flag);
  1279. /* Rewrite central dir with local headers and offsets */
  1280. err = mz_zip_entry_write_header(cd_mem_stream, 0, &local_file_info);
  1281. if (err == MZ_OK)
  1282. number_entry += 1;
  1283. err = mz_stream_seek(zip->stream, next_header_pos, MZ_SEEK_SET);
  1284. }
  1285. mz_stream_mem_delete(&local_file_info_stream);
  1286. mz_zip_print("Zip - Recover - Complete (cddisk %" PRId32 " entries %" PRId64 ")\n",
  1287. disk_number_with_cd, number_entry);
  1288. if (number_entry == 0)
  1289. return err;
  1290. /* Set new upper seek boundary for central dir mem stream */
  1291. disk_offset = mz_stream_tell(cd_mem_stream);
  1292. mz_stream_mem_set_buffer_limit(cd_mem_stream, (int32_t)disk_offset);
  1293. /* Set new central directory info */
  1294. mz_zip_set_cd_stream(handle, 0, cd_mem_stream);
  1295. mz_zip_set_number_entry(handle, number_entry);
  1296. mz_zip_set_disk_number_with_cd(handle, disk_number_with_cd);
  1297. return MZ_OK;
  1298. }
  1299. void *mz_zip_create(void **handle)
  1300. {
  1301. mz_zip *zip = NULL;
  1302. zip = (mz_zip *)MZ_ALLOC(sizeof(mz_zip));
  1303. if (zip != NULL)
  1304. {
  1305. memset(zip, 0, sizeof(mz_zip));
  1306. zip->data_descriptor = 1;
  1307. }
  1308. if (handle != NULL)
  1309. *handle = zip;
  1310. return zip;
  1311. }
  1312. void mz_zip_delete(void **handle)
  1313. {
  1314. mz_zip *zip = NULL;
  1315. if (handle == NULL)
  1316. return;
  1317. zip = (mz_zip *)*handle;
  1318. if (zip != NULL)
  1319. {
  1320. MZ_FREE(zip);
  1321. }
  1322. *handle = NULL;
  1323. }
  1324. int32_t mz_zip_open(void *handle, void *stream, int32_t mode)
  1325. {
  1326. mz_zip *zip = (mz_zip *)handle;
  1327. int32_t err = MZ_OK;
  1328. if (zip == NULL)
  1329. return MZ_PARAM_ERROR;
  1330. mz_zip_print("Zip - Open\n");
  1331. zip->stream = stream;
  1332. mz_stream_mem_create(&zip->cd_mem_stream);
  1333. if (mode & MZ_OPEN_MODE_WRITE)
  1334. {
  1335. mz_stream_mem_open(zip->cd_mem_stream, NULL, MZ_OPEN_MODE_CREATE);
  1336. zip->cd_stream = zip->cd_mem_stream;
  1337. }
  1338. else
  1339. {
  1340. zip->cd_stream = stream;
  1341. }
  1342. if ((mode & MZ_OPEN_MODE_READ) || (mode & MZ_OPEN_MODE_APPEND))
  1343. {
  1344. if ((mode & MZ_OPEN_MODE_CREATE) == 0)
  1345. {
  1346. err = mz_zip_read_cd(zip);
  1347. if (err != MZ_OK)
  1348. {
  1349. mz_zip_print("Zip - Error detected reading cd (%" PRId32 ")\n", err);
  1350. if (zip->recover && mz_zip_recover_cd(zip) == MZ_OK)
  1351. err = MZ_OK;
  1352. }
  1353. }
  1354. if ((err == MZ_OK) && (mode & MZ_OPEN_MODE_APPEND))
  1355. {
  1356. if (zip->cd_size > 0)
  1357. {
  1358. /* Store central directory in memory */
  1359. err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
  1360. if (err == MZ_OK)
  1361. err = mz_stream_copy(zip->cd_mem_stream, zip->stream, (int32_t)zip->cd_size);
  1362. if (err == MZ_OK)
  1363. err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
  1364. }
  1365. else
  1366. {
  1367. if (zip->cd_signature == MZ_ZIP_MAGIC_ENDHEADER)
  1368. {
  1369. /* If tiny zip then overwrite end header */
  1370. err = mz_stream_seek(zip->stream, zip->cd_offset, MZ_SEEK_SET);
  1371. }
  1372. else
  1373. {
  1374. /* If no central directory, append new zip to end of file */
  1375. err = mz_stream_seek(zip->stream, 0, MZ_SEEK_END);
  1376. }
  1377. }
  1378. if (zip->disk_number_with_cd > 0)
  1379. {
  1380. /* Move to last disk to begin appending */
  1381. mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, zip->disk_number_with_cd - 1);
  1382. }
  1383. }
  1384. else
  1385. {
  1386. zip->cd_start_pos = zip->cd_offset;
  1387. }
  1388. }
  1389. if (err != MZ_OK)
  1390. {
  1391. mz_zip_close(zip);
  1392. return err;
  1393. }
  1394. /* Memory streams used to store variable length file info data */
  1395. mz_stream_mem_create(&zip->file_info_stream);
  1396. mz_stream_mem_open(zip->file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
  1397. mz_stream_mem_create(&zip->local_file_info_stream);
  1398. mz_stream_mem_open(zip->local_file_info_stream, NULL, MZ_OPEN_MODE_CREATE);
  1399. zip->open_mode = mode;
  1400. return err;
  1401. }
  1402. int32_t mz_zip_close(void *handle)
  1403. {
  1404. mz_zip *zip = (mz_zip *)handle;
  1405. int32_t err = MZ_OK;
  1406. if (zip == NULL)
  1407. return MZ_PARAM_ERROR;
  1408. mz_zip_print("Zip - Close\n");
  1409. if (mz_zip_entry_is_open(handle) == MZ_OK)
  1410. err = mz_zip_entry_close(handle);
  1411. if ((err == MZ_OK) && (zip->open_mode & MZ_OPEN_MODE_WRITE))
  1412. err = mz_zip_write_cd(handle);
  1413. if (zip->cd_mem_stream != NULL)
  1414. {
  1415. mz_stream_close(zip->cd_mem_stream);
  1416. mz_stream_delete(&zip->cd_mem_stream);
  1417. }
  1418. if (zip->file_info_stream != NULL)
  1419. {
  1420. mz_stream_mem_close(zip->file_info_stream);
  1421. mz_stream_mem_delete(&zip->file_info_stream);
  1422. }
  1423. if (zip->local_file_info_stream != NULL)
  1424. {
  1425. mz_stream_mem_close(zip->local_file_info_stream);
  1426. mz_stream_mem_delete(&zip->local_file_info_stream);
  1427. }
  1428. if (zip->comment)
  1429. {
  1430. MZ_FREE(zip->comment);
  1431. zip->comment = NULL;
  1432. }
  1433. zip->stream = NULL;
  1434. zip->cd_stream = NULL;
  1435. return err;
  1436. }
  1437. int32_t mz_zip_get_comment(void *handle, const char **comment)
  1438. {
  1439. mz_zip *zip = (mz_zip *)handle;
  1440. if (zip == NULL || comment == NULL)
  1441. return MZ_PARAM_ERROR;
  1442. if (zip->comment == NULL)
  1443. return MZ_EXIST_ERROR;
  1444. *comment = zip->comment;
  1445. return MZ_OK;
  1446. }
  1447. int32_t mz_zip_set_comment(void *handle, const char *comment)
  1448. {
  1449. mz_zip *zip = (mz_zip *)handle;
  1450. int32_t comment_size = 0;
  1451. if (zip == NULL || comment == NULL)
  1452. return MZ_PARAM_ERROR;
  1453. if (zip->comment != NULL)
  1454. MZ_FREE(zip->comment);
  1455. comment_size = (int32_t)strlen(comment);
  1456. if (comment_size > UINT16_MAX)
  1457. return MZ_PARAM_ERROR;
  1458. zip->comment = (char *)MZ_ALLOC(comment_size+1);
  1459. if (zip->comment == NULL)
  1460. return MZ_MEM_ERROR;
  1461. memset(zip->comment, 0, comment_size+1);
  1462. strncpy(zip->comment, comment, comment_size);
  1463. return MZ_OK;
  1464. }
  1465. int32_t mz_zip_get_version_madeby(void *handle, uint16_t *version_madeby)
  1466. {
  1467. mz_zip *zip = (mz_zip *)handle;
  1468. if (zip == NULL || version_madeby == NULL)
  1469. return MZ_PARAM_ERROR;
  1470. *version_madeby = zip->version_madeby;
  1471. return MZ_OK;
  1472. }
  1473. int32_t mz_zip_set_version_madeby(void *handle, uint16_t version_madeby)
  1474. {
  1475. mz_zip *zip = (mz_zip *)handle;
  1476. if (zip == NULL)
  1477. return MZ_PARAM_ERROR;
  1478. zip->version_madeby = version_madeby;
  1479. return MZ_OK;
  1480. }
  1481. int32_t mz_zip_set_recover(void *handle, uint8_t recover)
  1482. {
  1483. mz_zip *zip = (mz_zip *)handle;
  1484. if (zip == NULL)
  1485. return MZ_PARAM_ERROR;
  1486. zip->recover = recover;
  1487. return MZ_OK;
  1488. }
  1489. int32_t mz_zip_set_data_descriptor(void *handle, uint8_t data_descriptor)
  1490. {
  1491. mz_zip *zip = (mz_zip *)handle;
  1492. if (zip == NULL)
  1493. return MZ_PARAM_ERROR;
  1494. zip->data_descriptor = data_descriptor;
  1495. return MZ_OK;
  1496. }
  1497. int32_t mz_zip_get_stream(void *handle, void **stream)
  1498. {
  1499. mz_zip *zip = (mz_zip *)handle;
  1500. if (zip == NULL || stream == NULL)
  1501. return MZ_PARAM_ERROR;
  1502. *stream = zip->stream;
  1503. if (*stream == NULL)
  1504. return MZ_EXIST_ERROR;
  1505. return MZ_OK;
  1506. }
  1507. int32_t mz_zip_set_cd_stream(void *handle, int64_t cd_start_pos, void *cd_stream)
  1508. {
  1509. mz_zip *zip = (mz_zip *)handle;
  1510. if (zip == NULL || cd_stream == NULL)
  1511. return MZ_PARAM_ERROR;
  1512. zip->cd_offset = 0;
  1513. zip->cd_stream = cd_stream;
  1514. zip->cd_start_pos = cd_start_pos;
  1515. return MZ_OK;
  1516. }
  1517. int32_t mz_zip_get_cd_mem_stream(void *handle, void **cd_mem_stream)
  1518. {
  1519. mz_zip *zip = (mz_zip *)handle;
  1520. if (zip == NULL || cd_mem_stream == NULL)
  1521. return MZ_PARAM_ERROR;
  1522. *cd_mem_stream = zip->cd_mem_stream;
  1523. if (*cd_mem_stream == NULL)
  1524. return MZ_EXIST_ERROR;
  1525. return MZ_OK;
  1526. }
  1527. int32_t mz_zip_set_number_entry(void *handle, uint64_t number_entry)
  1528. {
  1529. mz_zip *zip = (mz_zip *)handle;
  1530. if (zip == NULL)
  1531. return MZ_PARAM_ERROR;
  1532. zip->number_entry = number_entry;
  1533. return MZ_OK;
  1534. }
  1535. int32_t mz_zip_get_number_entry(void *handle, uint64_t *number_entry)
  1536. {
  1537. mz_zip *zip = (mz_zip *)handle;
  1538. if (zip == NULL || number_entry == NULL)
  1539. return MZ_PARAM_ERROR;
  1540. *number_entry = zip->number_entry;
  1541. return MZ_OK;
  1542. }
  1543. int32_t mz_zip_set_disk_number_with_cd(void *handle, uint32_t disk_number_with_cd)
  1544. {
  1545. mz_zip *zip = (mz_zip *)handle;
  1546. if (zip == NULL)
  1547. return MZ_PARAM_ERROR;
  1548. zip->disk_number_with_cd = disk_number_with_cd;
  1549. return MZ_OK;
  1550. }
  1551. int32_t mz_zip_get_disk_number_with_cd(void *handle, uint32_t *disk_number_with_cd)
  1552. {
  1553. mz_zip *zip = (mz_zip *)handle;
  1554. if (zip == NULL || disk_number_with_cd == NULL)
  1555. return MZ_PARAM_ERROR;
  1556. *disk_number_with_cd = zip->disk_number_with_cd;
  1557. return MZ_OK;
  1558. }
  1559. static int32_t mz_zip_entry_close_int(void *handle)
  1560. {
  1561. mz_zip *zip = (mz_zip *)handle;
  1562. if (zip->crypt_stream != NULL)
  1563. mz_stream_delete(&zip->crypt_stream);
  1564. zip->crypt_stream = NULL;
  1565. if (zip->compress_stream != NULL)
  1566. mz_stream_delete(&zip->compress_stream);
  1567. zip->compress_stream = NULL;
  1568. zip->entry_opened = 0;
  1569. return MZ_OK;
  1570. }
  1571. static int32_t mz_zip_entry_open_int(void *handle, uint8_t raw, int16_t compress_level, const char *password)
  1572. {
  1573. mz_zip *zip = (mz_zip *)handle;
  1574. int64_t max_total_in = 0;
  1575. int64_t header_size = 0;
  1576. int64_t footer_size = 0;
  1577. int32_t err = MZ_OK;
  1578. uint8_t use_crypt = 0;
  1579. if (zip == NULL)
  1580. return MZ_PARAM_ERROR;
  1581. switch (zip->file_info.compression_method)
  1582. {
  1583. case MZ_COMPRESS_METHOD_STORE:
  1584. case MZ_COMPRESS_METHOD_DEFLATE:
  1585. #ifdef HAVE_BZIP2
  1586. case MZ_COMPRESS_METHOD_BZIP2:
  1587. #endif
  1588. #ifdef HAVE_LZMA
  1589. case MZ_COMPRESS_METHOD_LZMA:
  1590. #endif
  1591. err = MZ_OK;
  1592. break;
  1593. default:
  1594. return MZ_SUPPORT_ERROR;
  1595. }
  1596. #ifndef HAVE_WZAES
  1597. if (zip->file_info.aes_version)
  1598. return MZ_SUPPORT_ERROR;
  1599. #endif
  1600. zip->entry_raw = raw;
  1601. if ((zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED) && (password != NULL))
  1602. {
  1603. if (zip->open_mode & MZ_OPEN_MODE_WRITE)
  1604. {
  1605. /* Encrypt only when we are not trying to write raw and password is supplied. */
  1606. if (!zip->entry_raw)
  1607. use_crypt = 1;
  1608. }
  1609. else if (zip->open_mode & MZ_OPEN_MODE_READ)
  1610. {
  1611. /* Decrypt only when password is supplied. Don't error when password */
  1612. /* is not supplied as we may want to read the raw encrypted data. */
  1613. use_crypt = 1;
  1614. }
  1615. }
  1616. if ((err == MZ_OK) && (use_crypt))
  1617. {
  1618. #ifdef HAVE_WZAES
  1619. if (zip->file_info.aes_version)
  1620. {
  1621. mz_stream_wzaes_create(&zip->crypt_stream);
  1622. mz_stream_wzaes_set_password(zip->crypt_stream, password);
  1623. mz_stream_wzaes_set_encryption_mode(zip->crypt_stream, zip->file_info.aes_encryption_mode);
  1624. }
  1625. else
  1626. #endif
  1627. {
  1628. #ifdef HAVE_PKCRYPT
  1629. uint8_t verify1 = 0;
  1630. uint8_t verify2 = 0;
  1631. /* Info-ZIP modification to ZipCrypto format: */
  1632. /* If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time. */
  1633. if (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR)
  1634. {
  1635. uint32_t dos_date = 0;
  1636. dos_date = mz_zip_time_t_to_dos_date(zip->file_info.modified_date);
  1637. verify1 = (uint8_t)((dos_date >> 16) & 0xff);
  1638. verify2 = (uint8_t)((dos_date >> 8) & 0xff);
  1639. }
  1640. else
  1641. {
  1642. verify1 = (uint8_t)((zip->file_info.crc >> 16) & 0xff);
  1643. verify2 = (uint8_t)((zip->file_info.crc >> 24) & 0xff);
  1644. }
  1645. mz_stream_pkcrypt_create(&zip->crypt_stream);
  1646. mz_stream_pkcrypt_set_password(zip->crypt_stream, password);
  1647. mz_stream_pkcrypt_set_verify(zip->crypt_stream, verify1, verify2);
  1648. #endif
  1649. }
  1650. }
  1651. if (err == MZ_OK)
  1652. {
  1653. if (zip->crypt_stream == NULL)
  1654. mz_stream_raw_create(&zip->crypt_stream);
  1655. mz_stream_set_base(zip->crypt_stream, zip->stream);
  1656. err = mz_stream_open(zip->crypt_stream, NULL, zip->open_mode);
  1657. }
  1658. if (err == MZ_OK)
  1659. {
  1660. if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE)
  1661. mz_stream_raw_create(&zip->compress_stream);
  1662. #if defined(HAVE_ZLIB) || defined(HAVE_LIBCOMP)
  1663. else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
  1664. mz_stream_zlib_create(&zip->compress_stream);
  1665. #endif
  1666. #ifdef HAVE_BZIP2
  1667. else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_BZIP2)
  1668. mz_stream_bzip_create(&zip->compress_stream);
  1669. #endif
  1670. #ifdef HAVE_LZMA
  1671. else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
  1672. mz_stream_lzma_create(&zip->compress_stream);
  1673. #endif
  1674. else
  1675. err = MZ_PARAM_ERROR;
  1676. }
  1677. if (err == MZ_OK)
  1678. {
  1679. if (zip->open_mode & MZ_OPEN_MODE_WRITE)
  1680. {
  1681. mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_COMPRESS_LEVEL, compress_level);
  1682. }
  1683. else
  1684. {
  1685. #ifndef HAVE_LIBCOMP
  1686. if (zip->entry_raw || zip->file_info.compression_method == MZ_COMPRESS_METHOD_STORE || zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
  1687. #endif
  1688. {
  1689. max_total_in = zip->file_info.compressed_size;
  1690. mz_stream_set_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
  1691. if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_HEADER_SIZE, &header_size) == MZ_OK)
  1692. max_total_in -= header_size;
  1693. if (mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_FOOTER_SIZE, &footer_size) == MZ_OK)
  1694. max_total_in -= footer_size;
  1695. mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, max_total_in);
  1696. }
  1697. if ((zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA) && (zip->file_info.flag & MZ_ZIP_FLAG_LZMA_EOS_MARKER) == 0)
  1698. {
  1699. mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN_MAX, zip->file_info.compressed_size);
  1700. mz_stream_set_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT_MAX, zip->file_info.uncompressed_size);
  1701. }
  1702. }
  1703. mz_stream_set_base(zip->compress_stream, zip->crypt_stream);
  1704. err = mz_stream_open(zip->compress_stream, NULL, zip->open_mode);
  1705. }
  1706. if (err == MZ_OK)
  1707. {
  1708. zip->entry_opened = 1;
  1709. zip->entry_crc32 = 0;
  1710. }
  1711. else
  1712. {
  1713. mz_zip_entry_close_int(handle);
  1714. }
  1715. return err;
  1716. }
  1717. int32_t mz_zip_entry_is_open(void *handle)
  1718. {
  1719. mz_zip *zip = (mz_zip *)handle;
  1720. if (zip == NULL)
  1721. return MZ_PARAM_ERROR;
  1722. if (zip->entry_opened == 0)
  1723. return MZ_EXIST_ERROR;
  1724. return MZ_OK;
  1725. }
  1726. static int32_t mz_zip_seek_to_local_header(void *handle)
  1727. {
  1728. mz_zip *zip = (mz_zip *)handle;
  1729. int64_t disk_size = 0;
  1730. uint32_t disk_number = zip->file_info.disk_number;
  1731. if (disk_number == zip->disk_number_with_cd)
  1732. {
  1733. mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_SIZE, &disk_size);
  1734. if ((disk_size == 0) || ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0))
  1735. disk_number = -1;
  1736. }
  1737. mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, disk_number);
  1738. mz_zip_print("Zip - Entry - Seek local (disk %" PRId32 " offset %" PRId64 ")\n",
  1739. disk_number, zip->file_info.disk_offset);
  1740. /* Guard against seek overflows */
  1741. if ((zip->disk_offset_shift > 0) &&
  1742. (zip->file_info.disk_offset > (INT64_MAX - zip->disk_offset_shift)))
  1743. return MZ_FORMAT_ERROR;
  1744. return mz_stream_seek(zip->stream, zip->file_info.disk_offset + zip->disk_offset_shift, MZ_SEEK_SET);
  1745. }
  1746. int32_t mz_zip_entry_read_open(void *handle, uint8_t raw, const char *password)
  1747. {
  1748. mz_zip *zip = (mz_zip *)handle;
  1749. int32_t err = MZ_OK;
  1750. int32_t err_shift = MZ_OK;
  1751. #if defined(MZ_ZIP_NO_ENCRYPTION)
  1752. if (password != NULL)
  1753. return MZ_SUPPORT_ERROR;
  1754. #endif
  1755. if (zip == NULL)
  1756. return MZ_PARAM_ERROR;
  1757. if ((zip->open_mode & MZ_OPEN_MODE_READ) == 0)
  1758. return MZ_PARAM_ERROR;
  1759. if (zip->entry_scanned == 0)
  1760. return MZ_PARAM_ERROR;
  1761. mz_zip_print("Zip - Entry - Read open (raw %" PRId32 ")\n", raw);
  1762. err = mz_zip_seek_to_local_header(handle);
  1763. if (err == MZ_OK)
  1764. err = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
  1765. if (err == MZ_FORMAT_ERROR && zip->disk_offset_shift > 0)
  1766. {
  1767. /* Perhaps we didn't compensated correctly for incorrect cd offset */
  1768. err_shift = mz_stream_seek(zip->stream, zip->file_info.disk_offset, MZ_SEEK_SET);
  1769. if (err_shift == MZ_OK)
  1770. err_shift = mz_zip_entry_read_header(zip->stream, 1, &zip->local_file_info, zip->local_file_info_stream);
  1771. if (err_shift == MZ_OK)
  1772. {
  1773. zip->disk_offset_shift = 0;
  1774. err = err_shift;
  1775. }
  1776. }
  1777. #ifdef MZ_ZIP_NO_DECOMPRESSION
  1778. if (!raw && zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
  1779. err = MZ_SUPPORT_ERROR;
  1780. #endif
  1781. if (err == MZ_OK)
  1782. err = mz_zip_entry_open_int(handle, raw, 0, password);
  1783. return err;
  1784. }
  1785. int32_t mz_zip_entry_write_open(void *handle, const mz_zip_file *file_info, int16_t compress_level, uint8_t raw, const char *password)
  1786. {
  1787. mz_zip *zip = (mz_zip *)handle;
  1788. int64_t filename_pos = -1;
  1789. int64_t extrafield_pos = 0;
  1790. int64_t comment_pos = 0;
  1791. int64_t linkname_pos = 0;
  1792. int64_t disk_number = 0;
  1793. uint8_t is_dir = 0;
  1794. int32_t err = MZ_OK;
  1795. #if defined(MZ_ZIP_NO_ENCRYPTION)
  1796. if (password != NULL)
  1797. return MZ_SUPPORT_ERROR;
  1798. #endif
  1799. if (zip == NULL || file_info == NULL || file_info->filename == NULL)
  1800. return MZ_PARAM_ERROR;
  1801. if (mz_zip_entry_is_open(handle) == MZ_OK)
  1802. {
  1803. err = mz_zip_entry_close(handle);
  1804. if (err != MZ_OK)
  1805. return err;
  1806. }
  1807. memcpy(&zip->file_info, file_info, sizeof(mz_zip_file));
  1808. mz_zip_print("Zip - Entry - Write open - %s (level %" PRId16 " raw %" PRId8 ")\n",
  1809. zip->file_info.filename, compress_level, raw);
  1810. mz_stream_seek(zip->file_info_stream, 0, MZ_SEEK_SET);
  1811. mz_stream_write(zip->file_info_stream, file_info, sizeof(mz_zip_file));
  1812. /* Copy filename, extrafield, and comment internally */
  1813. filename_pos = mz_stream_tell(zip->file_info_stream);
  1814. if (file_info->filename != NULL)
  1815. mz_stream_write(zip->file_info_stream, file_info->filename, (int32_t)strlen(file_info->filename));
  1816. mz_stream_write_uint8(zip->file_info_stream, 0);
  1817. extrafield_pos = mz_stream_tell(zip->file_info_stream);
  1818. if (file_info->extrafield != NULL)
  1819. mz_stream_write(zip->file_info_stream, file_info->extrafield, file_info->extrafield_size);
  1820. mz_stream_write_uint8(zip->file_info_stream, 0);
  1821. comment_pos = mz_stream_tell(zip->file_info_stream);
  1822. if (file_info->comment != NULL)
  1823. mz_stream_write(zip->file_info_stream, file_info->comment, file_info->comment_size);
  1824. mz_stream_write_uint8(zip->file_info_stream, 0);
  1825. linkname_pos = mz_stream_tell(zip->file_info_stream);
  1826. if (file_info->linkname != NULL)
  1827. mz_stream_write(zip->file_info_stream, file_info->linkname, (int32_t)strlen(file_info->linkname));
  1828. mz_stream_write_uint8(zip->file_info_stream, 0);
  1829. mz_stream_mem_get_buffer_at(zip->file_info_stream, filename_pos, (const void **)&zip->file_info.filename);
  1830. mz_stream_mem_get_buffer_at(zip->file_info_stream, extrafield_pos, (const void **)&zip->file_info.extrafield);
  1831. mz_stream_mem_get_buffer_at(zip->file_info_stream, comment_pos, (const void **)&zip->file_info.comment);
  1832. mz_stream_mem_get_buffer_at(zip->file_info_stream, linkname_pos, (const void **)&zip->file_info.linkname);
  1833. if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_DEFLATE)
  1834. {
  1835. if ((compress_level == 8) || (compress_level == 9))
  1836. zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_MAX;
  1837. if (compress_level == 2)
  1838. zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_FAST;
  1839. if (compress_level == 1)
  1840. zip->file_info.flag |= MZ_ZIP_FLAG_DEFLATE_SUPER_FAST;
  1841. }
  1842. #ifdef HAVE_LZMA
  1843. else if (zip->file_info.compression_method == MZ_COMPRESS_METHOD_LZMA)
  1844. zip->file_info.flag |= MZ_ZIP_FLAG_LZMA_EOS_MARKER;
  1845. #endif
  1846. if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
  1847. is_dir = 1;
  1848. if (!is_dir)
  1849. {
  1850. if (zip->data_descriptor)
  1851. zip->file_info.flag |= MZ_ZIP_FLAG_DATA_DESCRIPTOR;
  1852. if (password != NULL)
  1853. zip->file_info.flag |= MZ_ZIP_FLAG_ENCRYPTED;
  1854. }
  1855. mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &disk_number);
  1856. zip->file_info.disk_number = (uint32_t)disk_number;
  1857. zip->file_info.disk_offset = mz_stream_tell(zip->stream);
  1858. zip->file_info.crc = 0;
  1859. zip->file_info.compressed_size = 0;
  1860. #ifdef HAVE_WZAES
  1861. if (zip->file_info.aes_version && zip->file_info.aes_encryption_mode == 0)
  1862. zip->file_info.aes_encryption_mode = MZ_AES_ENCRYPTION_MODE_256;
  1863. #endif
  1864. if ((compress_level == 0) || (is_dir))
  1865. zip->file_info.compression_method = MZ_COMPRESS_METHOD_STORE;
  1866. #ifdef MZ_ZIP_NO_COMPRESSION
  1867. if (zip->file_info.compression_method != MZ_COMPRESS_METHOD_STORE)
  1868. err = MZ_SUPPORT_ERROR;
  1869. #endif
  1870. if (err == MZ_OK)
  1871. err = mz_zip_entry_write_header(zip->stream, 1, &zip->file_info);
  1872. if (err == MZ_OK)
  1873. err = mz_zip_entry_open_int(handle, raw, compress_level, password);
  1874. return err;
  1875. }
  1876. int32_t mz_zip_entry_read(void *handle, void *buf, int32_t len)
  1877. {
  1878. mz_zip *zip = (mz_zip *)handle;
  1879. int32_t read = 0;
  1880. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  1881. return MZ_PARAM_ERROR;
  1882. if (UINT_MAX == UINT16_MAX && len > UINT16_MAX) /* zlib limitation */
  1883. return MZ_PARAM_ERROR;
  1884. if (len == 0)
  1885. return MZ_PARAM_ERROR;
  1886. if (zip->file_info.compressed_size == 0)
  1887. return 0;
  1888. /* Read entire entry even if uncompressed_size = 0, otherwise */
  1889. /* aes encryption validation will fail if compressed_size > 0 */
  1890. read = mz_stream_read(zip->compress_stream, buf, len);
  1891. if (read > 0)
  1892. zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, read);
  1893. mz_zip_print("Zip - Entry - Read - %" PRId32 " (max %" PRId32 ")\n", read, len);
  1894. return read;
  1895. }
  1896. int32_t mz_zip_entry_write(void *handle, const void *buf, int32_t len)
  1897. {
  1898. mz_zip *zip = (mz_zip *)handle;
  1899. int32_t written = 0;
  1900. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  1901. return MZ_PARAM_ERROR;
  1902. written = mz_stream_write(zip->compress_stream, buf, len);
  1903. if (written > 0)
  1904. zip->entry_crc32 = mz_crypt_crc32_update(zip->entry_crc32, buf, written);
  1905. mz_zip_print("Zip - Entry - Write - %" PRId32 " (max %" PRId32 ")\n", written, len);
  1906. return written;
  1907. }
  1908. int32_t mz_zip_entry_read_close(void *handle, uint32_t *crc32, int64_t *compressed_size,
  1909. int64_t *uncompressed_size)
  1910. {
  1911. mz_zip *zip = (mz_zip *)handle;
  1912. int64_t total_in = 0;
  1913. int32_t err = MZ_OK;
  1914. uint8_t zip64 = 0;
  1915. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  1916. return MZ_PARAM_ERROR;
  1917. mz_stream_close(zip->compress_stream);
  1918. mz_zip_print("Zip - Entry - Read Close\n");
  1919. if (crc32 != NULL)
  1920. *crc32 = zip->file_info.crc;
  1921. if (compressed_size != NULL)
  1922. *compressed_size = zip->file_info.compressed_size;
  1923. if (uncompressed_size != NULL)
  1924. *uncompressed_size = zip->file_info.uncompressed_size;
  1925. mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &total_in);
  1926. if ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) &&
  1927. ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0) &&
  1928. (crc32 != NULL || compressed_size != NULL || uncompressed_size != NULL))
  1929. {
  1930. /* Check to see if data descriptor is zip64 bit format or not */
  1931. if (mz_zip_extrafield_contains(zip->local_file_info.extrafield,
  1932. zip->local_file_info.extrafield_size, MZ_ZIP_EXTENSION_ZIP64, NULL) == MZ_OK)
  1933. zip64 = 1;
  1934. err = mz_zip_seek_to_local_header(handle);
  1935. /* Seek to end of compressed stream since we might have over-read during compression */
  1936. if (err == MZ_OK)
  1937. err = mz_stream_seek(zip->stream, MZ_ZIP_SIZE_LD_ITEM +
  1938. (int64_t)zip->local_file_info.filename_size +
  1939. (int64_t)zip->local_file_info.extrafield_size +
  1940. total_in, MZ_SEEK_CUR);
  1941. /* Read data descriptor */
  1942. if (err == MZ_OK)
  1943. err = mz_zip_entry_read_descriptor(zip->stream, zip64,
  1944. crc32, compressed_size, uncompressed_size);
  1945. }
  1946. /* If entire entry was not read verification will fail */
  1947. if ((err == MZ_OK) && (total_in > 0) && (!zip->entry_raw))
  1948. {
  1949. #ifdef HAVE_WZAES
  1950. /* AES zip version AE-1 will expect a valid crc as well */
  1951. if (zip->file_info.aes_version <= 0x0001)
  1952. #endif
  1953. {
  1954. if (zip->entry_crc32 != zip->file_info.crc)
  1955. {
  1956. mz_zip_print("Zip - Entry - Crc failed (actual 0x%08" PRIx32 " expected 0x%08" PRIx32 ")\n",
  1957. zip->entry_crc32, zip->file_info.crc);
  1958. err = MZ_CRC_ERROR;
  1959. }
  1960. }
  1961. }
  1962. mz_zip_entry_close_int(handle);
  1963. return err;
  1964. }
  1965. int32_t mz_zip_entry_write_close(void *handle, uint32_t crc32, int64_t compressed_size,
  1966. int64_t uncompressed_size)
  1967. {
  1968. mz_zip *zip = (mz_zip *)handle;
  1969. int64_t end_disk_number = 0;
  1970. int32_t err = MZ_OK;
  1971. uint8_t zip64 = 0;
  1972. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  1973. return MZ_PARAM_ERROR;
  1974. mz_stream_close(zip->compress_stream);
  1975. if (!zip->entry_raw)
  1976. crc32 = zip->entry_crc32;
  1977. mz_zip_print("Zip - Entry - Write Close (crc 0x%08" PRIx32 " cs %" PRId64 " ucs %" PRId64 ")\n",
  1978. crc32, compressed_size, uncompressed_size);
  1979. /* If sizes are not set, then read them from the compression stream */
  1980. if (compressed_size < 0)
  1981. mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
  1982. if (uncompressed_size < 0)
  1983. mz_stream_get_prop_int64(zip->compress_stream, MZ_STREAM_PROP_TOTAL_IN, &uncompressed_size);
  1984. if (zip->file_info.flag & MZ_ZIP_FLAG_ENCRYPTED)
  1985. {
  1986. mz_stream_set_base(zip->crypt_stream, zip->stream);
  1987. err = mz_stream_close(zip->crypt_stream);
  1988. mz_stream_get_prop_int64(zip->crypt_stream, MZ_STREAM_PROP_TOTAL_OUT, &compressed_size);
  1989. }
  1990. if ((err == MZ_OK) && (zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR))
  1991. {
  1992. /* Determine if we need to write data descriptor in zip64 format,
  1993. if local extrafield was saved with zip64 extrafield */
  1994. if (zip->file_info.zip64 == MZ_ZIP64_AUTO)
  1995. {
  1996. if (zip->file_info.uncompressed_size >= UINT32_MAX)
  1997. zip64 = 1;
  1998. if (zip->file_info.compressed_size >= UINT32_MAX)
  1999. zip64 = 1;
  2000. if (zip->file_info.disk_offset >= UINT32_MAX)
  2001. zip64 = 1;
  2002. else if (zip->file_info.uncompressed_size == 0)
  2003. zip64 = 1;
  2004. }
  2005. else if (zip->file_info.zip64 == MZ_ZIP64_FORCE)
  2006. {
  2007. zip64 = 1;
  2008. }
  2009. if (zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO)
  2010. err = mz_zip_entry_write_descriptor(zip->stream,
  2011. zip64, 0, compressed_size, 0);
  2012. else
  2013. err = mz_zip_entry_write_descriptor(zip->stream,
  2014. zip64, crc32, compressed_size, uncompressed_size);
  2015. }
  2016. /* Write file info to central directory */
  2017. mz_zip_print("Zip - Entry - Write cd (ucs %" PRId64 " cs %" PRId64 " crc 0x%08" PRIx32 ")\n",
  2018. uncompressed_size, compressed_size, crc32);
  2019. zip->file_info.crc = crc32;
  2020. zip->file_info.compressed_size = compressed_size;
  2021. zip->file_info.uncompressed_size = uncompressed_size;
  2022. if (err == MZ_OK)
  2023. err = mz_zip_entry_write_header(zip->cd_mem_stream, 0, &zip->file_info);
  2024. /* Update local header with crc32 and sizes */
  2025. if ((err == MZ_OK) && ((zip->file_info.flag & MZ_ZIP_FLAG_DATA_DESCRIPTOR) == 0) &&
  2026. ((zip->file_info.flag & MZ_ZIP_FLAG_MASK_LOCAL_INFO) == 0))
  2027. {
  2028. /* Save the disk number and position we are to seek back after updating local header */
  2029. int64_t end_pos = mz_stream_tell(zip->stream);
  2030. mz_stream_get_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, &end_disk_number);
  2031. err = mz_zip_seek_to_local_header(handle);
  2032. if (err == MZ_OK)
  2033. {
  2034. /* Seek to crc32 and sizes offset in local header */
  2035. err = mz_stream_seek(zip->stream, MZ_ZIP_OFFSET_CRC_SIZES, MZ_SEEK_CUR);
  2036. }
  2037. if (err == MZ_OK)
  2038. err = mz_zip_entry_write_crc_sizes(zip->stream, 0, &zip->file_info);
  2039. mz_stream_set_prop_int64(zip->stream, MZ_STREAM_PROP_DISK_NUMBER, end_disk_number);
  2040. mz_stream_seek(zip->stream, end_pos, MZ_SEEK_SET);
  2041. }
  2042. zip->number_entry += 1;
  2043. mz_zip_entry_close_int(handle);
  2044. return err;
  2045. }
  2046. int32_t mz_zip_entry_close(void *handle)
  2047. {
  2048. return mz_zip_entry_close_raw(handle, UINT64_MAX, 0);
  2049. }
  2050. int32_t mz_zip_entry_close_raw(void *handle, int64_t uncompressed_size, uint32_t crc32)
  2051. {
  2052. mz_zip *zip = (mz_zip *)handle;
  2053. int32_t err = MZ_OK;
  2054. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  2055. return MZ_PARAM_ERROR;
  2056. if (zip->open_mode & MZ_OPEN_MODE_WRITE)
  2057. err = mz_zip_entry_write_close(handle, crc32, UINT64_MAX, uncompressed_size);
  2058. else
  2059. err = mz_zip_entry_read_close(handle, NULL, NULL, NULL);
  2060. return err;
  2061. }
  2062. int32_t mz_zip_entry_is_dir(void *handle)
  2063. {
  2064. mz_zip *zip = (mz_zip *)handle;
  2065. int32_t filename_length = 0;
  2066. if (zip == NULL)
  2067. return MZ_PARAM_ERROR;
  2068. if (zip->entry_scanned == 0)
  2069. return MZ_PARAM_ERROR;
  2070. if (mz_zip_attrib_is_dir(zip->file_info.external_fa, zip->file_info.version_madeby) == MZ_OK)
  2071. return MZ_OK;
  2072. filename_length = (int32_t)strlen(zip->file_info.filename);
  2073. if (filename_length > 0)
  2074. {
  2075. if ((zip->file_info.filename[filename_length - 1] == '/') ||
  2076. (zip->file_info.filename[filename_length - 1] == '\\'))
  2077. return MZ_OK;
  2078. }
  2079. return MZ_EXIST_ERROR;
  2080. }
  2081. int32_t mz_zip_entry_is_symlink(void *handle)
  2082. {
  2083. mz_zip *zip = (mz_zip *)handle;
  2084. if (zip == NULL)
  2085. return MZ_PARAM_ERROR;
  2086. if (zip->entry_scanned == 0)
  2087. return MZ_PARAM_ERROR;
  2088. if (mz_zip_attrib_is_symlink(zip->file_info.external_fa, zip->file_info.version_madeby) != MZ_OK)
  2089. return MZ_EXIST_ERROR;
  2090. if (zip->file_info.linkname == NULL || *zip->file_info.linkname == 0)
  2091. return MZ_EXIST_ERROR;
  2092. return MZ_OK;
  2093. }
  2094. int32_t mz_zip_entry_get_info(void *handle, mz_zip_file **file_info)
  2095. {
  2096. mz_zip *zip = (mz_zip *)handle;
  2097. if (zip == NULL)
  2098. return MZ_PARAM_ERROR;
  2099. if ((zip->open_mode & MZ_OPEN_MODE_WRITE) == 0)
  2100. {
  2101. if (!zip->entry_scanned)
  2102. return MZ_PARAM_ERROR;
  2103. }
  2104. *file_info = &zip->file_info;
  2105. return MZ_OK;
  2106. }
  2107. int32_t mz_zip_entry_get_local_info(void *handle, mz_zip_file **local_file_info)
  2108. {
  2109. mz_zip *zip = (mz_zip *)handle;
  2110. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  2111. return MZ_PARAM_ERROR;
  2112. *local_file_info = &zip->local_file_info;
  2113. return MZ_OK;
  2114. }
  2115. int32_t mz_zip_entry_set_extrafield(void *handle, const uint8_t *extrafield, uint16_t extrafield_size)
  2116. {
  2117. mz_zip *zip = (mz_zip *)handle;
  2118. if (zip == NULL || mz_zip_entry_is_open(handle) != MZ_OK)
  2119. return MZ_PARAM_ERROR;
  2120. zip->file_info.extrafield = extrafield;
  2121. zip->file_info.extrafield_size = extrafield_size;
  2122. return MZ_OK;
  2123. }
  2124. static int32_t mz_zip_goto_next_entry_int(void *handle)
  2125. {
  2126. mz_zip *zip = (mz_zip *)handle;
  2127. int32_t err = MZ_OK;
  2128. if (zip == NULL)
  2129. return MZ_PARAM_ERROR;
  2130. zip->entry_scanned = 0;
  2131. mz_stream_set_prop_int64(zip->cd_stream, MZ_STREAM_PROP_DISK_NUMBER, -1);
  2132. err = mz_stream_seek(zip->cd_stream, zip->cd_current_pos, MZ_SEEK_SET);
  2133. if (err == MZ_OK)
  2134. err = mz_zip_entry_read_header(zip->cd_stream, 0, &zip->file_info, zip->file_info_stream);
  2135. if (err == MZ_OK)
  2136. zip->entry_scanned = 1;
  2137. return err;
  2138. }
  2139. int64_t mz_zip_get_entry(void *handle)
  2140. {
  2141. mz_zip *zip = (mz_zip *)handle;
  2142. if (zip == NULL)
  2143. return MZ_PARAM_ERROR;
  2144. return zip->cd_current_pos;
  2145. }
  2146. int32_t mz_zip_goto_entry(void *handle, int64_t cd_pos)
  2147. {
  2148. mz_zip *zip = (mz_zip *)handle;
  2149. if (zip == NULL)
  2150. return MZ_PARAM_ERROR;
  2151. if (cd_pos < zip->cd_start_pos || cd_pos > zip->cd_start_pos + zip->cd_size)
  2152. return MZ_PARAM_ERROR;
  2153. zip->cd_current_pos = cd_pos;
  2154. return mz_zip_goto_next_entry_int(handle);
  2155. }
  2156. int32_t mz_zip_goto_first_entry(void *handle)
  2157. {
  2158. mz_zip *zip = (mz_zip *)handle;
  2159. if (zip == NULL)
  2160. return MZ_PARAM_ERROR;
  2161. zip->cd_current_pos = zip->cd_start_pos;
  2162. return mz_zip_goto_next_entry_int(handle);
  2163. }
  2164. int32_t mz_zip_goto_next_entry(void *handle)
  2165. {
  2166. mz_zip *zip = (mz_zip *)handle;
  2167. if (zip == NULL)
  2168. return MZ_PARAM_ERROR;
  2169. zip->cd_current_pos += (int64_t)MZ_ZIP_SIZE_CD_ITEM + zip->file_info.filename_size +
  2170. zip->file_info.extrafield_size + zip->file_info.comment_size;
  2171. return mz_zip_goto_next_entry_int(handle);
  2172. }
  2173. int32_t mz_zip_locate_entry(void *handle, const char *filename, uint8_t ignore_case)
  2174. {
  2175. mz_zip *zip = (mz_zip *)handle;
  2176. int32_t err = MZ_OK;
  2177. int32_t result = 0;
  2178. if (zip == NULL || filename == NULL)
  2179. return MZ_PARAM_ERROR;
  2180. /* If we are already on the current entry, no need to search */
  2181. if ((zip->entry_scanned) && (zip->file_info.filename != NULL))
  2182. {
  2183. result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
  2184. if (result == 0)
  2185. return MZ_OK;
  2186. }
  2187. /* Search all entries starting at the first */
  2188. err = mz_zip_goto_first_entry(handle);
  2189. while (err == MZ_OK)
  2190. {
  2191. result = mz_zip_path_compare(zip->file_info.filename, filename, ignore_case);
  2192. if (result == 0)
  2193. return MZ_OK;
  2194. err = mz_zip_goto_next_entry(handle);
  2195. }
  2196. return err;
  2197. }
  2198. int32_t mz_zip_locate_first_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
  2199. {
  2200. mz_zip *zip = (mz_zip *)handle;
  2201. int32_t err = MZ_OK;
  2202. int32_t result = 0;
  2203. /* Search first entry looking for match */
  2204. err = mz_zip_goto_first_entry(handle);
  2205. if (err != MZ_OK)
  2206. return err;
  2207. result = cb(handle, userdata, &zip->file_info);
  2208. if (result == 0)
  2209. return MZ_OK;
  2210. return mz_zip_locate_next_entry(handle, userdata, cb);
  2211. }
  2212. int32_t mz_zip_locate_next_entry(void *handle, void *userdata, mz_zip_locate_entry_cb cb)
  2213. {
  2214. mz_zip *zip = (mz_zip *)handle;
  2215. int32_t err = MZ_OK;
  2216. int32_t result = 0;
  2217. /* Search next entries looking for match */
  2218. err = mz_zip_goto_next_entry(handle);
  2219. while (err == MZ_OK)
  2220. {
  2221. result = cb(handle, userdata, &zip->file_info);
  2222. if (result == 0)
  2223. return MZ_OK;
  2224. err = mz_zip_goto_next_entry(handle);
  2225. }
  2226. return err;
  2227. }
  2228. /***************************************************************************/
  2229. int32_t mz_zip_attrib_is_dir(uint32_t attrib, int32_t version_madeby)
  2230. {
  2231. uint32_t posix_attrib = 0;
  2232. uint8_t system = MZ_HOST_SYSTEM(version_madeby);
  2233. int32_t err = MZ_OK;
  2234. err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
  2235. if (err == MZ_OK)
  2236. {
  2237. if ((posix_attrib & 0170000) == 0040000) /* S_ISDIR */
  2238. return MZ_OK;
  2239. }
  2240. return MZ_EXIST_ERROR;
  2241. }
  2242. int32_t mz_zip_attrib_is_symlink(uint32_t attrib, int32_t version_madeby)
  2243. {
  2244. uint32_t posix_attrib = 0;
  2245. uint8_t system = MZ_HOST_SYSTEM(version_madeby);
  2246. int32_t err = MZ_OK;
  2247. err = mz_zip_attrib_convert(system, attrib, MZ_HOST_SYSTEM_UNIX, &posix_attrib);
  2248. if (err == MZ_OK)
  2249. {
  2250. if ((posix_attrib & 0170000) == 0120000) /* S_ISLNK */
  2251. return MZ_OK;
  2252. }
  2253. return MZ_EXIST_ERROR;
  2254. }
  2255. int32_t mz_zip_attrib_convert(uint8_t src_sys, uint32_t src_attrib, uint8_t target_sys, uint32_t *target_attrib)
  2256. {
  2257. if (target_attrib == NULL)
  2258. return MZ_PARAM_ERROR;
  2259. *target_attrib = 0;
  2260. if ((src_sys == MZ_HOST_SYSTEM_MSDOS) || (src_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
  2261. {
  2262. if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
  2263. {
  2264. *target_attrib = src_attrib;
  2265. return MZ_OK;
  2266. }
  2267. if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
  2268. return mz_zip_attrib_win32_to_posix(src_attrib, target_attrib);
  2269. }
  2270. else if ((src_sys == MZ_HOST_SYSTEM_UNIX) || (src_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (src_sys == MZ_HOST_SYSTEM_RISCOS))
  2271. {
  2272. if ((target_sys == MZ_HOST_SYSTEM_UNIX) || (target_sys == MZ_HOST_SYSTEM_OSX_DARWIN) || (target_sys == MZ_HOST_SYSTEM_RISCOS))
  2273. {
  2274. /* If high bytes are set, it contains unix specific attributes */
  2275. if ((src_attrib >> 16) != 0)
  2276. src_attrib >>= 16;
  2277. *target_attrib = src_attrib;
  2278. return MZ_OK;
  2279. }
  2280. if ((target_sys == MZ_HOST_SYSTEM_MSDOS) || (target_sys == MZ_HOST_SYSTEM_WINDOWS_NTFS))
  2281. return mz_zip_attrib_posix_to_win32(src_attrib, target_attrib);
  2282. }
  2283. return MZ_SUPPORT_ERROR;
  2284. }
  2285. int32_t mz_zip_attrib_posix_to_win32(uint32_t posix_attrib, uint32_t *win32_attrib)
  2286. {
  2287. if (win32_attrib == NULL)
  2288. return MZ_PARAM_ERROR;
  2289. *win32_attrib = 0;
  2290. /* S_IWUSR | S_IWGRP | S_IWOTH | S_IXUSR | S_IXGRP | S_IXOTH */
  2291. if ((posix_attrib & 0000333) == 0 && (posix_attrib & 0000444) != 0)
  2292. *win32_attrib |= 0x01; /* FILE_ATTRIBUTE_READONLY */
  2293. /* S_IFLNK */
  2294. if ((posix_attrib & 0170000) == 0120000)
  2295. *win32_attrib |= 0x400; /* FILE_ATTRIBUTE_REPARSE_POINT */
  2296. /* S_IFDIR */
  2297. else if ((posix_attrib & 0170000) == 0040000)
  2298. *win32_attrib |= 0x10; /* FILE_ATTRIBUTE_DIRECTORY */
  2299. /* S_IFREG */
  2300. else
  2301. *win32_attrib |= 0x80; /* FILE_ATTRIBUTE_NORMAL */
  2302. return MZ_OK;
  2303. }
  2304. int32_t mz_zip_attrib_win32_to_posix(uint32_t win32_attrib, uint32_t *posix_attrib)
  2305. {
  2306. if (posix_attrib == NULL)
  2307. return MZ_PARAM_ERROR;
  2308. *posix_attrib = 0000444; /* S_IRUSR | S_IRGRP | S_IROTH */
  2309. /* FILE_ATTRIBUTE_READONLY */
  2310. if ((win32_attrib & 0x01) == 0)
  2311. *posix_attrib |= 0000222; /* S_IWUSR | S_IWGRP | S_IWOTH */
  2312. /* FILE_ATTRIBUTE_REPARSE_POINT */
  2313. if ((win32_attrib & 0x400) == 0x400)
  2314. *posix_attrib |= 0120000; /* S_IFLNK */
  2315. /* FILE_ATTRIBUTE_DIRECTORY */
  2316. else if ((win32_attrib & 0x10) == 0x10)
  2317. *posix_attrib |= 0040111; /* S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH */
  2318. else
  2319. *posix_attrib |= 0100000; /* S_IFREG */
  2320. return MZ_OK;
  2321. }
  2322. /***************************************************************************/
  2323. int32_t mz_zip_extrafield_find(void *stream, uint16_t type, uint16_t *length)
  2324. {
  2325. int32_t err = MZ_OK;
  2326. uint16_t field_type = 0;
  2327. uint16_t field_length = 0;
  2328. do
  2329. {
  2330. err = mz_stream_read_uint16(stream, &field_type);
  2331. if (err == MZ_OK)
  2332. err = mz_stream_read_uint16(stream, &field_length);
  2333. if (err != MZ_OK)
  2334. break;
  2335. if (type == field_type)
  2336. {
  2337. if (length != NULL)
  2338. *length = field_length;
  2339. return MZ_OK;
  2340. }
  2341. err = mz_stream_seek(stream, field_length, MZ_SEEK_CUR);
  2342. }
  2343. while (err == MZ_OK);
  2344. return MZ_EXIST_ERROR;
  2345. }
  2346. int32_t mz_zip_extrafield_contains(const uint8_t *extrafield, int32_t extrafield_size,
  2347. uint16_t type, uint16_t *length)
  2348. {
  2349. void *file_extra_stream = NULL;
  2350. int32_t err = MZ_OK;
  2351. if (extrafield == NULL || extrafield_size == 0)
  2352. return MZ_PARAM_ERROR;
  2353. mz_stream_mem_create(&file_extra_stream);
  2354. mz_stream_mem_set_buffer(file_extra_stream, (void *)extrafield, extrafield_size);
  2355. err = mz_zip_extrafield_find(file_extra_stream, type, length);
  2356. mz_stream_mem_delete(&file_extra_stream);
  2357. return err;
  2358. }
  2359. int32_t mz_zip_extrafield_read(void *stream, uint16_t *type, uint16_t *length)
  2360. {
  2361. int32_t err = MZ_OK;
  2362. if (type == NULL || length == NULL)
  2363. return MZ_PARAM_ERROR;
  2364. err = mz_stream_read_uint16(stream, type);
  2365. if (err == MZ_OK)
  2366. err = mz_stream_read_uint16(stream, length);
  2367. return err;
  2368. }
  2369. int32_t mz_zip_extrafield_write(void *stream, uint16_t type, uint16_t length)
  2370. {
  2371. int32_t err = MZ_OK;
  2372. err = mz_stream_write_uint16(stream, type);
  2373. if (err == MZ_OK)
  2374. err = mz_stream_write_uint16(stream, length);
  2375. return err;
  2376. }
  2377. /***************************************************************************/
  2378. static int32_t mz_zip_invalid_date(const struct tm *ptm)
  2379. {
  2380. #define datevalue_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
  2381. return (!datevalue_in_range(0, 127 + 80, ptm->tm_year) || /* 1980-based year, allow 80 extra */
  2382. !datevalue_in_range(0, 11, ptm->tm_mon) ||
  2383. !datevalue_in_range(1, 31, ptm->tm_mday) ||
  2384. !datevalue_in_range(0, 23, ptm->tm_hour) ||
  2385. !datevalue_in_range(0, 59, ptm->tm_min) ||
  2386. !datevalue_in_range(0, 59, ptm->tm_sec));
  2387. #undef datevalue_in_range
  2388. }
  2389. static void mz_zip_dosdate_to_raw_tm(uint64_t dos_date, struct tm *ptm)
  2390. {
  2391. uint64_t date = (uint64_t)(dos_date >> 16);
  2392. ptm->tm_mday = (uint16_t)(date & 0x1f);
  2393. ptm->tm_mon = (uint16_t)(((date & 0x1E0) / 0x20) - 1);
  2394. ptm->tm_year = (uint16_t)(((date & 0x0FE00) / 0x0200) + 80);
  2395. ptm->tm_hour = (uint16_t)((dos_date & 0xF800) / 0x800);
  2396. ptm->tm_min = (uint16_t)((dos_date & 0x7E0) / 0x20);
  2397. ptm->tm_sec = (uint16_t)(2 * (dos_date & 0x1f));
  2398. ptm->tm_isdst = -1;
  2399. }
  2400. int32_t mz_zip_dosdate_to_tm(uint64_t dos_date, struct tm *ptm)
  2401. {
  2402. if (ptm == NULL)
  2403. return MZ_PARAM_ERROR;
  2404. mz_zip_dosdate_to_raw_tm(dos_date, ptm);
  2405. if (mz_zip_invalid_date(ptm))
  2406. {
  2407. /* Invalid date stored, so don't return it */
  2408. memset(ptm, 0, sizeof(struct tm));
  2409. return MZ_FORMAT_ERROR;
  2410. }
  2411. return MZ_OK;
  2412. }
  2413. time_t mz_zip_dosdate_to_time_t(uint64_t dos_date)
  2414. {
  2415. struct tm ptm;
  2416. mz_zip_dosdate_to_raw_tm(dos_date, &ptm);
  2417. return mktime(&ptm);
  2418. }
  2419. int32_t mz_zip_time_t_to_tm(time_t unix_time, struct tm *ptm)
  2420. {
  2421. struct tm ltm;
  2422. if (ptm == NULL)
  2423. return MZ_PARAM_ERROR;
  2424. if (localtime_r(&unix_time, &ltm) == NULL) /* Returns a 1900-based year */
  2425. {
  2426. /* Invalid date stored, so don't return it */
  2427. memset(ptm, 0, sizeof(struct tm));
  2428. return MZ_INTERNAL_ERROR;
  2429. }
  2430. memcpy(ptm, &ltm, sizeof(struct tm));
  2431. return MZ_OK;
  2432. }
  2433. uint32_t mz_zip_time_t_to_dos_date(time_t unix_time)
  2434. {
  2435. struct tm ptm;
  2436. mz_zip_time_t_to_tm(unix_time, &ptm);
  2437. return mz_zip_tm_to_dosdate((const struct tm *)&ptm);
  2438. }
  2439. uint32_t mz_zip_tm_to_dosdate(const struct tm *ptm)
  2440. {
  2441. struct tm fixed_tm;
  2442. /* Years supported: */
  2443. /* [00, 79] (assumed to be between 2000 and 2079) */
  2444. /* [80, 207] (assumed to be between 1980 and 2107, typical output of old */
  2445. /* software that does 'year-1900' to get a double digit year) */
  2446. /* [1980, 2107] (due to format limitations, only years 1980-2107 can be stored.) */
  2447. memcpy(&fixed_tm, ptm, sizeof(struct tm));
  2448. if (fixed_tm.tm_year >= 1980) /* range [1980, 2107] */
  2449. fixed_tm.tm_year -= 1980;
  2450. else if (fixed_tm.tm_year >= 80) /* range [80, 207] */
  2451. fixed_tm.tm_year -= 80;
  2452. else /* range [00, 79] */
  2453. fixed_tm.tm_year += 20;
  2454. if (mz_zip_invalid_date(&fixed_tm))
  2455. return 0;
  2456. return (((uint32_t)fixed_tm.tm_mday + (32 * ((uint32_t)fixed_tm.tm_mon + 1)) + (512 * (uint32_t)fixed_tm.tm_year)) << 16) |
  2457. (((uint32_t)fixed_tm.tm_sec / 2) + (32 * (uint32_t)fixed_tm.tm_min) + (2048 * (uint32_t)fixed_tm.tm_hour));
  2458. }
  2459. int32_t mz_zip_ntfs_to_unix_time(uint64_t ntfs_time, time_t *unix_time)
  2460. {
  2461. *unix_time = (time_t)((ntfs_time - 116444736000000000LL) / 10000000);
  2462. return MZ_OK;
  2463. }
  2464. int32_t mz_zip_unix_to_ntfs_time(time_t unix_time, uint64_t *ntfs_time)
  2465. {
  2466. *ntfs_time = ((uint64_t)unix_time * 10000000) + 116444736000000000LL;
  2467. return MZ_OK;
  2468. }
  2469. /***************************************************************************/
  2470. int32_t mz_zip_path_compare(const char *path1, const char *path2, uint8_t ignore_case)
  2471. {
  2472. do
  2473. {
  2474. if ((*path1 == '\\' && *path2 == '/') ||
  2475. (*path2 == '\\' && *path1 == '/'))
  2476. {
  2477. /* Ignore comparison of path slashes */
  2478. }
  2479. else if (ignore_case)
  2480. {
  2481. if (tolower(*path1) != tolower(*path2))
  2482. break;
  2483. }
  2484. else if (*path1 != *path2)
  2485. {
  2486. break;
  2487. }
  2488. path1 += 1;
  2489. path2 += 1;
  2490. }
  2491. while (*path1 != 0 && *path2 != 0);
  2492. if (ignore_case)
  2493. return (int32_t)(tolower(*path1) - tolower(*path2));
  2494. return (int32_t)(*path1 - *path2);
  2495. }
  2496. /***************************************************************************/