vogl_miniz_zip.c 158 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121
  1. /**************************************************************************
  2. *
  3. * Copyright 2013-2014 RAD Game Tools and Valve Software
  4. * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. **************************************************************************/
  26. // File: vogl_miniz_zip.cpp
  27. #include "vogl_miniz_zip.h"
  28. // ------------------- .ZIP archive reading
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. #ifndef MINIZ_NO_ARCHIVE_APIS
  33. #ifdef MINIZ_NO_STDIO
  34. #define MZ_FILE void *
  35. #else
  36. #include <sys/stat.h>
  37. #if defined(COMPILER_MSVC) || defined(COMPILER_MINGW)
  38. static FILE *mz_fopen(const char *pFilename, const char *pMode)
  39. {
  40. FILE *pFile = NULL;
  41. fopen_s(&pFile, pFilename, pMode);
  42. return pFile;
  43. }
  44. static FILE *mz_freopen(const char *pPath, const char *pMode, FILE *pStream)
  45. {
  46. FILE *pFile = NULL;
  47. if (freopen_s(&pFile, pPath, pMode, pStream))
  48. return NULL;
  49. return pFile;
  50. }
  51. #ifndef MINIZ_NO_TIME
  52. #include <sys/utime.h>
  53. #endif
  54. #define MZ_FOPEN mz_fopen
  55. #define MZ_FCLOSE fclose
  56. #define MZ_FREAD fread
  57. #define MZ_FWRITE fwrite
  58. #define MZ_FTELL64 _ftelli64
  59. #define MZ_FSEEK64 _fseeki64
  60. #define MZ_FILE_STAT_STRUCT _stat
  61. #define MZ_FILE_STAT _stat
  62. #define MZ_FFLUSH fflush
  63. #define MZ_FREOPEN mz_freopen
  64. #define MZ_DELETE_FILE remove
  65. #elif defined(__MINGW32__)
  66. #ifndef MINIZ_NO_TIME
  67. #include <sys/utime.h>
  68. #endif
  69. #define MZ_FOPEN(f, m) fopen(f, m)
  70. #define MZ_FCLOSE fclose
  71. #define MZ_FREAD fread
  72. #define MZ_FWRITE fwrite
  73. #define MZ_FTELL64 ftello64
  74. #define MZ_FSEEK64 fseeko64
  75. #define MZ_FILE_STAT_STRUCT _stat
  76. #define MZ_FILE_STAT _stat
  77. #define MZ_FFLUSH fflush
  78. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  79. #define MZ_DELETE_FILE remove
  80. #elif defined(__TINYC__)
  81. #ifndef MINIZ_NO_TIME
  82. #include <sys/utime.h>
  83. #endif
  84. #define MZ_FOPEN(f, m) fopen(f, m)
  85. #define MZ_FCLOSE fclose
  86. #define MZ_FREAD fread
  87. #define MZ_FWRITE fwrite
  88. #define MZ_FTELL64 ftell
  89. #define MZ_FSEEK64 fseek
  90. #define MZ_FILE_STAT_STRUCT stat
  91. #define MZ_FILE_STAT stat
  92. #define MZ_FFLUSH fflush
  93. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  94. #define MZ_DELETE_FILE remove
  95. #elif defined(COMPILER_GCCLIKE) && _LARGEFILE64_SOURCE
  96. #ifndef MINIZ_NO_TIME
  97. #include <utime.h>
  98. #endif
  99. #define MZ_FOPEN(f, m) fopen64(f, m)
  100. #define MZ_FCLOSE fclose
  101. #define MZ_FREAD fread
  102. #define MZ_FWRITE fwrite
  103. #define MZ_FTELL64 ftello64
  104. #define MZ_FSEEK64 fseeko64
  105. #define MZ_FILE_STAT_STRUCT stat64
  106. #define MZ_FILE_STAT stat64
  107. #define MZ_FFLUSH fflush
  108. #define MZ_FREOPEN(p, m, s) freopen64(p, m, s)
  109. #define MZ_DELETE_FILE remove
  110. #else
  111. #pragma message("Using fopen, ftello, fseeko, stat() etc. path for file I/O - this path may not support large files.")
  112. #ifndef MINIZ_NO_TIME
  113. #include <utime.h>
  114. #endif
  115. #define MZ_FOPEN(f, m) fopen(f, m)
  116. #define MZ_FCLOSE fclose
  117. #define MZ_FREAD fread
  118. #define MZ_FWRITE fwrite
  119. #define MZ_FTELL64 ftello
  120. #define MZ_FSEEK64 fseeko
  121. #define MZ_FILE_STAT_STRUCT stat
  122. #define MZ_FILE_STAT stat
  123. #define MZ_FFLUSH fflush
  124. #define MZ_FREOPEN(f, m, s) freopen(f, m, s)
  125. #define MZ_DELETE_FILE remove
  126. #endif // #ifdef COMPILER_MSVC
  127. #endif // #ifdef MINIZ_NO_STDIO
  128. #define MZ_TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) - 'A' + 'a') : (c))
  129. // Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, miniz.c doesn't use structs for any of this stuff.
  130. enum
  131. {
  132. // ZIP archive identifiers and record sizes
  133. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50,
  134. MZ_ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50,
  135. MZ_ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,
  136. MZ_ZIP_LOCAL_DIR_HEADER_SIZE = 30,
  137. MZ_ZIP_CENTRAL_DIR_HEADER_SIZE = 46,
  138. MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,
  139. // ZIP64 archive identifier and record sizes
  140. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50,
  141. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50,
  142. MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56,
  143. MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20,
  144. MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID = 0x0001,
  145. MZ_ZIP_DATA_DESCRIPTOR_ID = 0x08074b50,
  146. // Central directory header record offsets
  147. MZ_ZIP_CDH_SIG_OFS = 0,
  148. MZ_ZIP_CDH_VERSION_MADE_BY_OFS = 4,
  149. MZ_ZIP_CDH_VERSION_NEEDED_OFS = 6,
  150. MZ_ZIP_CDH_BIT_FLAG_OFS = 8,
  151. MZ_ZIP_CDH_METHOD_OFS = 10,
  152. MZ_ZIP_CDH_FILE_TIME_OFS = 12,
  153. MZ_ZIP_CDH_FILE_DATE_OFS = 14,
  154. MZ_ZIP_CDH_CRC32_OFS = 16,
  155. MZ_ZIP_CDH_COMPRESSED_SIZE_OFS = 20,
  156. MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24,
  157. MZ_ZIP_CDH_FILENAME_LEN_OFS = 28,
  158. MZ_ZIP_CDH_EXTRA_LEN_OFS = 30,
  159. MZ_ZIP_CDH_COMMENT_LEN_OFS = 32,
  160. MZ_ZIP_CDH_DISK_START_OFS = 34,
  161. MZ_ZIP_CDH_INTERNAL_ATTR_OFS = 36,
  162. MZ_ZIP_CDH_EXTERNAL_ATTR_OFS = 38,
  163. MZ_ZIP_CDH_LOCAL_HEADER_OFS = 42,
  164. // Local directory header offsets
  165. MZ_ZIP_LDH_SIG_OFS = 0,
  166. MZ_ZIP_LDH_VERSION_NEEDED_OFS = 4,
  167. MZ_ZIP_LDH_BIT_FLAG_OFS = 6,
  168. MZ_ZIP_LDH_METHOD_OFS = 8,
  169. MZ_ZIP_LDH_FILE_TIME_OFS = 10,
  170. MZ_ZIP_LDH_FILE_DATE_OFS = 12,
  171. MZ_ZIP_LDH_CRC32_OFS = 14,
  172. MZ_ZIP_LDH_COMPRESSED_SIZE_OFS = 18,
  173. MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS = 22,
  174. MZ_ZIP_LDH_FILENAME_LEN_OFS = 26,
  175. MZ_ZIP_LDH_EXTRA_LEN_OFS = 28,
  176. // End of central directory offsets
  177. MZ_ZIP_ECDH_SIG_OFS = 0,
  178. MZ_ZIP_ECDH_NUM_THIS_DISK_OFS = 4,
  179. MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS = 6,
  180. MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,
  181. MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10,
  182. MZ_ZIP_ECDH_CDIR_SIZE_OFS = 12,
  183. MZ_ZIP_ECDH_CDIR_OFS_OFS = 16,
  184. MZ_ZIP_ECDH_COMMENT_SIZE_OFS = 20,
  185. // ZIP64 End of central directory locator offsets
  186. MZ_ZIP64_ECDL_SIG_OFS = 0, // 4 bytes
  187. MZ_ZIP64_ECDL_NUM_DISK_CDIR_OFS = 4, // 4 bytes
  188. MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS = 8, // 8 bytes
  189. MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS = 16, // 4 bytes
  190. // ZIP64 End of central directory header offsets
  191. MZ_ZIP64_ECDH_SIG_OFS = 0, // 4 bytes
  192. MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS = 4, // 8 bytes
  193. MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS = 12, // 2 bytes
  194. MZ_ZIP64_ECDH_VERSION_NEEDED_OFS = 14, // 2 bytes
  195. MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS = 16, // 4 bytes
  196. MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS = 20, // 4 bytes
  197. MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 24, // 8 bytes
  198. MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, // 8 bytes
  199. MZ_ZIP64_ECDH_CDIR_SIZE_OFS = 40, // 8 bytes
  200. MZ_ZIP64_ECDH_CDIR_OFS_OFS = 48, // 8 bytes
  201. MZ_ZIP_VERSION_MADE_BY_DOS_FILESYSTEM_ID = 0,
  202. MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG = 0x10,
  203. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED = 1,
  204. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG = 32,
  205. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION = 64,
  206. MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED = 8192
  207. };
  208. typedef struct
  209. {
  210. void *m_p;
  211. size_t m_size, m_capacity;
  212. mz_uint m_element_size;
  213. } mz_zip_array;
  214. struct mz_zip_internal_state_tag
  215. {
  216. mz_zip_array m_central_dir;
  217. mz_zip_array m_central_dir_offsets;
  218. mz_zip_array m_sorted_central_dir_offsets;
  219. // The flags passed in when the archive is initially opened.
  220. uint32_t m_init_flags;
  221. // MZ_TRUE if the archive has a zip64 end of central directory headers, etc.
  222. mz_bool m_zip64;
  223. // MZ_TRUE if we found zip64 extended info in the central directory (m_zip64 will also be slammed to true too, even if we didn't find a zip64 end of central dir header, etc.)
  224. mz_bool m_zip64_has_extended_info_fields;
  225. // These fields are used by the file, FILE, memory, and memory/heap read/write helpers.
  226. MZ_FILE *m_pFile;
  227. mz_uint64 m_file_archive_start_ofs;
  228. void *m_pMem;
  229. size_t m_mem_size;
  230. size_t m_mem_capacity;
  231. };
  232. #define MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(array_ptr, element_size) (array_ptr)->m_element_size = element_size
  233. #if defined(DEBUG) || defined(_DEBUG) || defined(VOGL_BUILD_DEBUG)
  234. static MZ_FORCEINLINE mz_uint mz_zip_array_range_check(const mz_zip_array *pArray, mz_uint index)
  235. {
  236. MZ_ASSERT(index < pArray->m_size);
  237. return index;
  238. }
  239. #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[mz_zip_array_range_check(array_ptr, index)]
  240. #else
  241. #define MZ_ZIP_ARRAY_ELEMENT(array_ptr, element_type, index) ((element_type *)((array_ptr)->m_p))[index]
  242. #endif
  243. static MZ_FORCEINLINE void mz_zip_array_init(mz_zip_array *pArray, mz_uint32 element_size)
  244. {
  245. memset(pArray, 0, sizeof(mz_zip_array));
  246. pArray->m_element_size = element_size;
  247. }
  248. static MZ_FORCEINLINE void mz_zip_array_clear(mz_zip_archive *pZip, mz_zip_array *pArray)
  249. {
  250. pZip->m_pFree(pZip->m_pAlloc_opaque, pArray->m_p);
  251. memset(pArray, 0, sizeof(mz_zip_array));
  252. }
  253. static mz_bool mz_zip_array_ensure_capacity(mz_zip_archive *pZip, mz_zip_array *pArray, size_t min_new_capacity, mz_uint growing)
  254. {
  255. void *pNew_p;
  256. size_t new_capacity = min_new_capacity;
  257. MZ_ASSERT(pArray->m_element_size);
  258. if (pArray->m_capacity >= min_new_capacity)
  259. return MZ_TRUE;
  260. if (growing)
  261. {
  262. new_capacity = MZ_MAX(1, pArray->m_capacity);
  263. while (new_capacity < min_new_capacity)
  264. new_capacity *= 2;
  265. }
  266. if (NULL == (pNew_p = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pArray->m_p, pArray->m_element_size, new_capacity)))
  267. return MZ_FALSE;
  268. pArray->m_p = pNew_p;
  269. pArray->m_capacity = new_capacity;
  270. return MZ_TRUE;
  271. }
  272. static MZ_FORCEINLINE mz_bool mz_zip_array_reserve(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_capacity, mz_uint growing)
  273. {
  274. if (new_capacity > pArray->m_capacity)
  275. {
  276. if (!mz_zip_array_ensure_capacity(pZip, pArray, new_capacity, growing))
  277. return MZ_FALSE;
  278. }
  279. return MZ_TRUE;
  280. }
  281. static MZ_FORCEINLINE mz_bool mz_zip_array_resize(mz_zip_archive *pZip, mz_zip_array *pArray, size_t new_size, mz_uint growing)
  282. {
  283. if (new_size > pArray->m_capacity)
  284. {
  285. if (!mz_zip_array_ensure_capacity(pZip, pArray, new_size, growing))
  286. return MZ_FALSE;
  287. }
  288. pArray->m_size = new_size;
  289. return MZ_TRUE;
  290. }
  291. static MZ_FORCEINLINE mz_bool mz_zip_array_ensure_room(mz_zip_archive *pZip, mz_zip_array *pArray, size_t n)
  292. {
  293. return mz_zip_array_reserve(pZip, pArray, pArray->m_size + n, MZ_TRUE);
  294. }
  295. static MZ_FORCEINLINE mz_bool mz_zip_array_push_back(mz_zip_archive *pZip, mz_zip_array *pArray, const void *pElements, size_t n)
  296. {
  297. size_t orig_size = pArray->m_size;
  298. if (!mz_zip_array_resize(pZip, pArray, orig_size + n, MZ_TRUE))
  299. return MZ_FALSE;
  300. memcpy((mz_uint8 *)pArray->m_p + orig_size * pArray->m_element_size, pElements, n * pArray->m_element_size);
  301. return MZ_TRUE;
  302. }
  303. #ifndef MINIZ_NO_TIME
  304. static MZ_TIME_T mz_zip_dos_to_time_t(int dos_time, int dos_date)
  305. {
  306. struct tm tm;
  307. memset(&tm, 0, sizeof(tm));
  308. tm.tm_isdst = -1;
  309. tm.tm_year = ((dos_date >> 9) & 127) + 1980 - 1900;
  310. tm.tm_mon = ((dos_date >> 5) & 15) - 1;
  311. tm.tm_mday = dos_date & 31;
  312. tm.tm_hour = (dos_time >> 11) & 31;
  313. tm.tm_min = (dos_time >> 5) & 63;
  314. tm.tm_sec = (dos_time << 1) & 62;
  315. return mktime(&tm);
  316. }
  317. static void mz_zip_time_t_to_dos_time(MZ_TIME_T time, mz_uint16 *pDOS_time, mz_uint16 *pDOS_date)
  318. {
  319. #ifdef COMPILER_MSVC
  320. struct tm tm_struct;
  321. struct tm *tm = &tm_struct;
  322. errno_t err = localtime_s(tm, &time);
  323. if (err)
  324. {
  325. *pDOS_date = 0;
  326. *pDOS_time = 0;
  327. return;
  328. }
  329. #else
  330. struct tm *tm = localtime(&time);
  331. #endif // #ifdef COMPILER_MSVC
  332. *pDOS_time = (mz_uint16)(((tm->tm_hour) << 11) + ((tm->tm_min) << 5) + ((tm->tm_sec) >> 1));
  333. *pDOS_date = (mz_uint16)(((tm->tm_year + 1900 - 1980) << 9) + ((tm->tm_mon + 1) << 5) + tm->tm_mday);
  334. }
  335. #ifndef MINIZ_NO_STDIO
  336. static mz_bool mz_zip_get_file_modified_time(const char *pFilename, MZ_TIME_T *pTime)
  337. {
  338. struct MZ_FILE_STAT_STRUCT file_stat;
  339. // On Linux with x86 glibc, this call will fail on large files (I think >= 0x80000000 bytes) unless you compiled with _LARGEFILE64_SOURCE. Argh.
  340. if (MZ_FILE_STAT(pFilename, &file_stat) != 0)
  341. return MZ_FALSE;
  342. *pTime = file_stat.st_mtime;
  343. return MZ_TRUE;
  344. }
  345. static mz_bool mz_zip_set_file_times(const char *pFilename, MZ_TIME_T access_time, MZ_TIME_T modified_time)
  346. {
  347. struct utimbuf t;
  348. memset(&t, 0, sizeof(t));
  349. t.actime = access_time;
  350. t.modtime = modified_time;
  351. return !utime(pFilename, &t);
  352. }
  353. #endif // #ifndef MINIZ_NO_STDIO
  354. #endif // #ifndef MINIZ_NO_TIME
  355. static MZ_FORCEINLINE mz_bool mz_zip_set_error(mz_zip_archive *pZip, mz_zip_error err_num)
  356. {
  357. if (pZip)
  358. pZip->m_last_error = err_num;
  359. return MZ_FALSE;
  360. }
  361. static mz_bool mz_zip_reader_init_internal(mz_zip_archive *pZip, mz_uint flags)
  362. {
  363. (void)flags;
  364. if ((!pZip) || (pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
  365. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  366. if (!pZip->m_pAlloc)
  367. pZip->m_pAlloc = miniz_def_alloc_func;
  368. if (!pZip->m_pFree)
  369. pZip->m_pFree = miniz_def_free_func;
  370. if (!pZip->m_pRealloc)
  371. pZip->m_pRealloc = miniz_def_realloc_func;
  372. pZip->m_archive_size = 0;
  373. pZip->m_central_directory_file_ofs = 0;
  374. pZip->m_total_files = 0;
  375. pZip->m_last_error = MZ_ZIP_NO_ERROR;
  376. if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
  377. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  378. memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
  379. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
  380. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
  381. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
  382. pZip->m_pState->m_init_flags = flags;
  383. pZip->m_pState->m_zip64 = MZ_FALSE;
  384. pZip->m_pState->m_zip64_has_extended_info_fields = MZ_FALSE;
  385. pZip->m_zip_mode = MZ_ZIP_MODE_READING;
  386. return MZ_TRUE;
  387. }
  388. static MZ_FORCEINLINE mz_bool mz_zip_reader_filename_less(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, mz_uint r_index)
  389. {
  390. const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;
  391. const mz_uint8 *pR = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, r_index));
  392. mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS), r_len = MZ_READ_LE16(pR + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  393. mz_uint8 l = 0, r = 0;
  394. pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  395. pR += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  396. pE = pL + MZ_MIN(l_len, r_len);
  397. while (pL < pE)
  398. {
  399. if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
  400. break;
  401. pL++;
  402. pR++;
  403. }
  404. return (pL == pE) ? (l_len < r_len) : (l < r);
  405. }
  406. #define MZ_SWAP_UINT32(a, b) \
  407. do \
  408. { \
  409. mz_uint32 t = a; \
  410. a = b; \
  411. b = t; \
  412. } \
  413. MZ_MACRO_END
  414. // Heap sort of lowercased filenames, used to help accelerate plain central directory searches by mz_zip_locate_file(). (Could also use qsort(), but it could allocate memory.)
  415. static void mz_zip_reader_sort_central_dir_offsets_by_filename(mz_zip_archive *pZip)
  416. {
  417. mz_zip_internal_state *pState = pZip->m_pState;
  418. const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
  419. const mz_zip_array *pCentral_dir = &pState->m_central_dir;
  420. mz_uint32 *pIndices;
  421. mz_uint32 start, end;
  422. const mz_uint32 size = pZip->m_total_files;
  423. if (size <= 1U)
  424. return;
  425. pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
  426. start = (size - 2U) >> 1U;
  427. for (;;)
  428. {
  429. mz_uint64 child, root = start;
  430. for (;;)
  431. {
  432. if ((child = (root << 1U) + 1U) >= size)
  433. break;
  434. child += (((child + 1U) < size) && (mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U])));
  435. if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
  436. break;
  437. MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
  438. root = child;
  439. }
  440. if (!start)
  441. break;
  442. start--;
  443. }
  444. end = size - 1;
  445. while (end > 0)
  446. {
  447. mz_uint64 child, root = 0;
  448. MZ_SWAP_UINT32(pIndices[end], pIndices[0]);
  449. for (;;)
  450. {
  451. if ((child = (root << 1U) + 1U) >= end)
  452. break;
  453. child += (((child + 1U) < end) && mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[child], pIndices[child + 1U]));
  454. if (!mz_zip_reader_filename_less(pCentral_dir, pCentral_dir_offsets, pIndices[root], pIndices[child]))
  455. break;
  456. MZ_SWAP_UINT32(pIndices[root], pIndices[child]);
  457. root = child;
  458. }
  459. end--;
  460. }
  461. }
  462. static mz_bool mz_zip_reader_locate_header_sig(mz_zip_archive *pZip, mz_uint32 record_sig, mz_uint32 record_size, mz_int64 *pOfs)
  463. {
  464. mz_int64 cur_file_ofs;
  465. mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
  466. mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
  467. // Basic sanity checks - reject files which are too small
  468. if (pZip->m_archive_size < record_size)
  469. return MZ_FALSE;
  470. // Find the record by scanning the file from the end towards the beginning.
  471. cur_file_ofs = MZ_MAX((mz_int64)pZip->m_archive_size - (mz_int64)sizeof(buf_u32), 0);
  472. for (;;)
  473. {
  474. int i, n = (int)MZ_MIN(sizeof(buf_u32), pZip->m_archive_size - cur_file_ofs);
  475. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, n) != (mz_uint)n)
  476. return MZ_FALSE;
  477. for (i = n - 4; i >= 0; --i)
  478. {
  479. mz_uint s = MZ_READ_LE32(pBuf + i);
  480. if (s == record_sig)
  481. {
  482. if ((pZip->m_archive_size - (cur_file_ofs + i)) >= record_size)
  483. break;
  484. }
  485. }
  486. if (i >= 0)
  487. {
  488. cur_file_ofs += i;
  489. break;
  490. }
  491. // Give up if we've searched the entire file, or we've gone back "too far" (~64kb)
  492. if ((!cur_file_ofs) || ((pZip->m_archive_size - cur_file_ofs) >= (MZ_UINT16_MAX + record_size)))
  493. return MZ_FALSE;
  494. cur_file_ofs = MZ_MAX(cur_file_ofs - (sizeof(buf_u32) - 3), 0);
  495. }
  496. *pOfs = cur_file_ofs;
  497. return MZ_TRUE;
  498. }
  499. static mz_bool mz_zip_reader_read_central_dir(mz_zip_archive *pZip, mz_uint flags)
  500. {
  501. mz_uint cdir_size = 0, cdir_entries_on_this_disk = 0, num_this_disk = 0, cdir_disk_index = 0;
  502. mz_uint64 cdir_ofs = 0;
  503. mz_int64 cur_file_ofs = 0;
  504. const mz_uint8 *p;
  505. mz_uint32 buf_u32[4096 / sizeof(mz_uint32)];
  506. mz_uint8 *pBuf = (mz_uint8 *)buf_u32;
  507. mz_bool sort_central_dir = ((flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0);
  508. mz_uint32 zip64_end_of_central_dir_locator_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
  509. mz_uint8 *pZip64_locator = (mz_uint8 *)zip64_end_of_central_dir_locator_u32;
  510. mz_uint32 zip64_end_of_central_dir_header_u32[(MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
  511. mz_uint8 *pZip64_end_of_central_dir = (mz_uint8 *)zip64_end_of_central_dir_header_u32;
  512. mz_uint64 zip64_end_of_central_dir_ofs = 0;
  513. // Basic sanity checks - reject files which are too small, and check the first 4 bytes of the file to make sure a local header is there.
  514. if (pZip->m_archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  515. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  516. if (!mz_zip_reader_locate_header_sig(pZip, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE, &cur_file_ofs))
  517. return mz_zip_set_error(pZip, MZ_ZIP_FAILED_FINDING_CENTRAL_DIR);
  518. // Read and verify the end of central directory record.
  519. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  520. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  521. if (MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_SIG_OFS) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG)
  522. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  523. if (cur_file_ofs >= (MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))
  524. {
  525. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs - MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, pZip64_locator, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)
  526. {
  527. if (MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG)
  528. {
  529. zip64_end_of_central_dir_ofs = MZ_READ_LE64(pZip64_locator + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS);
  530. if (zip64_end_of_central_dir_ofs > (pZip->m_archive_size - MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE))
  531. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  532. if (pZip->m_pRead(pZip->m_pIO_opaque, zip64_end_of_central_dir_ofs, pZip64_end_of_central_dir, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
  533. {
  534. if (MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIG_OFS) == MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG)
  535. {
  536. pZip->m_pState->m_zip64 = MZ_TRUE;
  537. }
  538. }
  539. }
  540. }
  541. }
  542. pZip->m_total_files = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS);
  543. cdir_entries_on_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
  544. num_this_disk = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_THIS_DISK_OFS);
  545. cdir_disk_index = MZ_READ_LE16(pBuf + MZ_ZIP_ECDH_NUM_DISK_CDIR_OFS);
  546. cdir_size = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_SIZE_OFS);
  547. cdir_ofs = MZ_READ_LE32(pBuf + MZ_ZIP_ECDH_CDIR_OFS_OFS);
  548. if (pZip->m_pState->m_zip64)
  549. {
  550. mz_uint32 zip64_total_num_of_disks = MZ_READ_LE32(pZip64_locator + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS);
  551. mz_uint64 zip64_cdir_total_entries = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS);
  552. mz_uint64 zip64_cdir_total_entries_on_this_disk = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS);
  553. mz_uint64 zip64_size_of_end_of_central_dir_record = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS);
  554. mz_uint64 zip64_size_of_central_directory = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_SIZE_OFS);
  555. if (zip64_size_of_end_of_central_dir_record < (MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - 12))
  556. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  557. if (zip64_total_num_of_disks != 1U)
  558. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  559. // Check for miniz's practical limits
  560. if (zip64_cdir_total_entries > MZ_UINT32_MAX)
  561. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  562. pZip->m_total_files = (mz_uint32)zip64_cdir_total_entries;
  563. if (zip64_cdir_total_entries_on_this_disk > MZ_UINT32_MAX)
  564. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  565. cdir_entries_on_this_disk = (mz_uint32)zip64_cdir_total_entries_on_this_disk;
  566. // Check for miniz's current practical limits (sorry, this should be enough for millions of files)
  567. if (zip64_size_of_central_directory > MZ_UINT32_MAX)
  568. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  569. cdir_size = (mz_uint32)zip64_size_of_central_directory;
  570. num_this_disk = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_THIS_DISK_OFS);
  571. cdir_disk_index = MZ_READ_LE32(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_NUM_DISK_CDIR_OFS);
  572. cdir_ofs = MZ_READ_LE64(pZip64_end_of_central_dir + MZ_ZIP64_ECDH_CDIR_OFS_OFS);
  573. }
  574. if (pZip->m_total_files != cdir_entries_on_this_disk)
  575. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  576. if (((num_this_disk | cdir_disk_index) != 0) && ((num_this_disk != 1) || (cdir_disk_index != 1)))
  577. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  578. if (cdir_size < pZip->m_total_files * MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)
  579. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  580. if ((cdir_ofs + (mz_uint64)cdir_size) > pZip->m_archive_size)
  581. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  582. pZip->m_central_directory_file_ofs = cdir_ofs;
  583. if (pZip->m_total_files)
  584. {
  585. mz_uint i, n;
  586. // Read the entire central directory into a heap block, and allocate another heap block to hold the unsorted central dir file record offsets, and possibly another to hold the sorted indices.
  587. if ((!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir, cdir_size, MZ_FALSE)) ||
  588. (!mz_zip_array_resize(pZip, &pZip->m_pState->m_central_dir_offsets, pZip->m_total_files, MZ_FALSE)))
  589. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  590. if (sort_central_dir)
  591. {
  592. if (!mz_zip_array_resize(pZip, &pZip->m_pState->m_sorted_central_dir_offsets, pZip->m_total_files, MZ_FALSE))
  593. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  594. }
  595. if (pZip->m_pRead(pZip->m_pIO_opaque, cdir_ofs, pZip->m_pState->m_central_dir.m_p, cdir_size) != cdir_size)
  596. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  597. // Now create an index into the central directory file records, do some basic sanity checking on each record
  598. p = (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p;
  599. for (n = cdir_size, i = 0; i < pZip->m_total_files; ++i)
  600. {
  601. mz_uint total_header_size, disk_index, bit_flags, filename_size, ext_data_size;
  602. mz_uint64 comp_size, decomp_size, local_header_ofs;
  603. if ((n < MZ_ZIP_CENTRAL_DIR_HEADER_SIZE) || (MZ_READ_LE32(p) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG))
  604. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  605. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, i) = (mz_uint32)(p - (const mz_uint8 *)pZip->m_pState->m_central_dir.m_p);
  606. if (sort_central_dir)
  607. MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_sorted_central_dir_offsets, mz_uint32, i) = i;
  608. comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
  609. decomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
  610. local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
  611. filename_size = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  612. ext_data_size = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
  613. if ((!pZip->m_pState->m_zip64_has_extended_info_fields) &&
  614. (ext_data_size) &&
  615. (MZ_MAX(MZ_MAX(comp_size, decomp_size), local_header_ofs) == MZ_UINT32_MAX))
  616. {
  617. // Attempt to find zip64 extended information field in the entry's extra data
  618. mz_uint32 extra_size_remaining = ext_data_size;
  619. if (extra_size_remaining)
  620. {
  621. const mz_uint8 *pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size;
  622. do
  623. {
  624. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  625. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  626. mz_uint32 field_id = MZ_READ_LE16(pExtra_data);
  627. mz_uint32 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  628. if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)
  629. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  630. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
  631. {
  632. // Ok, the archive didn't have any zip64 headers but it uses a zip64 extended information field so mark it as zip64 anyway (this can occur with infozip's zip util when it reads compresses files from stdin).
  633. pZip->m_pState->m_zip64 = MZ_TRUE;
  634. pZip->m_pState->m_zip64_has_extended_info_fields = MZ_TRUE;
  635. break;
  636. }
  637. pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
  638. extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
  639. } while (extra_size_remaining);
  640. }
  641. }
  642. // I've seen archives that aren't marked as zip64 that uses zip64 ext data, argh
  643. if ((comp_size != MZ_UINT32_MAX) && (decomp_size != MZ_UINT32_MAX))
  644. {
  645. if (((!MZ_READ_LE32(p + MZ_ZIP_CDH_METHOD_OFS)) && (decomp_size != comp_size)) || (decomp_size && !comp_size))
  646. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  647. }
  648. disk_index = MZ_READ_LE16(p + MZ_ZIP_CDH_DISK_START_OFS);
  649. if ((disk_index == MZ_UINT16_MAX) || ((disk_index != num_this_disk) && (disk_index != 1)))
  650. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_MULTIDISK);
  651. if (comp_size != MZ_UINT32_MAX)
  652. {
  653. if (((mz_uint64)MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS) + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > pZip->m_archive_size)
  654. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  655. }
  656. bit_flags = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  657. if (bit_flags & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_LOCAL_DIR_IS_MASKED)
  658. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  659. if ((total_header_size = MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS)) > n)
  660. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  661. n -= total_header_size;
  662. p += total_header_size;
  663. }
  664. }
  665. if (sort_central_dir)
  666. mz_zip_reader_sort_central_dir_offsets_by_filename(pZip);
  667. return MZ_TRUE;
  668. }
  669. void mz_zip_zero_struct(mz_zip_archive *pZip)
  670. {
  671. if (pZip)
  672. MZ_CLEAR_OBJ(*pZip);
  673. }
  674. static mz_bool mz_zip_reader_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
  675. {
  676. mz_bool status = MZ_TRUE;
  677. if (!pZip)
  678. return MZ_FALSE;
  679. if ((!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
  680. {
  681. if (set_last_error)
  682. pZip->m_last_error = MZ_ZIP_INVALID_PARAMETER;
  683. return MZ_FALSE;
  684. }
  685. if (pZip->m_pState)
  686. {
  687. mz_zip_internal_state *pState = pZip->m_pState;
  688. pZip->m_pState = NULL;
  689. mz_zip_array_clear(pZip, &pState->m_central_dir);
  690. mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
  691. mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
  692. #ifndef MINIZ_NO_STDIO
  693. if (pState->m_pFile)
  694. {
  695. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)
  696. {
  697. if (MZ_FCLOSE(pState->m_pFile) == EOF)
  698. {
  699. if (set_last_error)
  700. pZip->m_last_error = MZ_ZIP_FILE_CLOSE_FAILED;
  701. status = MZ_FALSE;
  702. }
  703. }
  704. pState->m_pFile = NULL;
  705. }
  706. #endif // #ifndef MINIZ_NO_STDIO
  707. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  708. }
  709. pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
  710. return status;
  711. }
  712. mz_bool mz_zip_reader_end(mz_zip_archive *pZip)
  713. {
  714. return mz_zip_reader_end_internal(pZip, MZ_TRUE);
  715. }
  716. static size_t mz_zip_mem_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
  717. {
  718. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  719. size_t s = (file_ofs >= pZip->m_archive_size) ? 0 : (size_t)MZ_MIN(pZip->m_archive_size - file_ofs, n);
  720. memcpy(pBuf, (const mz_uint8 *)pZip->m_pState->m_pMem + file_ofs, s);
  721. return s;
  722. }
  723. mz_bool mz_zip_reader_init(mz_zip_archive *pZip, mz_uint64 size, mz_uint flags)
  724. {
  725. if ((!pZip) || (!pZip->m_pRead))
  726. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  727. if (!mz_zip_reader_init_internal(pZip, flags))
  728. return MZ_FALSE;
  729. pZip->m_zip_type = MZ_ZIP_TYPE_USER;
  730. pZip->m_archive_size = size;
  731. if (!mz_zip_reader_read_central_dir(pZip, flags))
  732. {
  733. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  734. return MZ_FALSE;
  735. }
  736. return MZ_TRUE;
  737. }
  738. mz_bool mz_zip_reader_init_mem(mz_zip_archive *pZip, const void *pMem, size_t size, mz_uint flags)
  739. {
  740. if (!pMem)
  741. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  742. if (size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  743. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  744. if (!mz_zip_reader_init_internal(pZip, flags))
  745. return MZ_FALSE;
  746. pZip->m_zip_type = MZ_ZIP_TYPE_MEMORY;
  747. pZip->m_archive_size = size;
  748. pZip->m_pRead = mz_zip_mem_read_func;
  749. pZip->m_pIO_opaque = pZip;
  750. #ifdef __cplusplus
  751. pZip->m_pState->m_pMem = const_cast<void *>(pMem);
  752. #else
  753. pZip->m_pState->m_pMem = (void *)pMem;
  754. #endif
  755. pZip->m_pState->m_mem_size = size;
  756. if (!mz_zip_reader_read_central_dir(pZip, flags))
  757. {
  758. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  759. return MZ_FALSE;
  760. }
  761. return MZ_TRUE;
  762. }
  763. #ifndef MINIZ_NO_STDIO
  764. static size_t mz_zip_file_read_func(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n)
  765. {
  766. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  767. mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
  768. file_ofs += pZip->m_pState->m_file_archive_start_ofs;
  769. if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET))))
  770. return 0;
  771. return MZ_FREAD(pBuf, 1, n, pZip->m_pState->m_pFile);
  772. }
  773. mz_bool mz_zip_reader_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint flags, mz_uint64 file_start_ofs, mz_uint64 archive_size)
  774. {
  775. if ((!pZip) || (!pFilename) || ((archive_size) && (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)))
  776. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  777. mz_uint64 file_size;
  778. MZ_FILE *pFile = MZ_FOPEN(pFilename, "rb");
  779. if (!pFile)
  780. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  781. file_size = archive_size;
  782. if (!file_size)
  783. {
  784. if (MZ_FSEEK64(pFile, 0, SEEK_END))
  785. {
  786. MZ_FCLOSE(pFile);
  787. return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  788. }
  789. file_size = MZ_FTELL64(pFile);
  790. }
  791. // TODO: Better sanity check archive_size and the # of actual remaining bytes
  792. if (file_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  793. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  794. if (!mz_zip_reader_init_internal(pZip, flags))
  795. {
  796. MZ_FCLOSE(pFile);
  797. return MZ_FALSE;
  798. }
  799. pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
  800. pZip->m_pRead = mz_zip_file_read_func;
  801. pZip->m_pIO_opaque = pZip;
  802. pZip->m_pState->m_pFile = pFile;
  803. pZip->m_archive_size = file_size;
  804. pZip->m_pState->m_file_archive_start_ofs = file_start_ofs;
  805. if (!mz_zip_reader_read_central_dir(pZip, flags))
  806. {
  807. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  808. return MZ_FALSE;
  809. }
  810. return MZ_TRUE;
  811. }
  812. mz_bool mz_zip_reader_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint64 archive_size, mz_uint flags)
  813. {
  814. mz_uint64 cur_file_ofs;
  815. if ((!pZip) || (!pFile))
  816. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  817. cur_file_ofs = MZ_FTELL64(pFile);
  818. if (!archive_size)
  819. {
  820. if (MZ_FSEEK64(pFile, 0, SEEK_END))
  821. return mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  822. archive_size = MZ_FTELL64(pFile) - cur_file_ofs;
  823. if (archive_size < MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  824. return mz_zip_set_error(pZip, MZ_ZIP_NOT_AN_ARCHIVE);
  825. }
  826. if (!mz_zip_reader_init_internal(pZip, flags))
  827. return MZ_FALSE;
  828. pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
  829. pZip->m_pRead = mz_zip_file_read_func;
  830. pZip->m_pIO_opaque = pZip;
  831. pZip->m_pState->m_pFile = pFile;
  832. pZip->m_archive_size = archive_size;
  833. pZip->m_pState->m_file_archive_start_ofs = cur_file_ofs;
  834. if (!mz_zip_reader_read_central_dir(pZip, flags))
  835. {
  836. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  837. return MZ_FALSE;
  838. }
  839. return MZ_TRUE;
  840. }
  841. #endif // #ifndef MINIZ_NO_STDIO
  842. static MZ_FORCEINLINE const mz_uint8 *mz_zip_get_cdh(mz_zip_archive *pZip, mz_uint file_index)
  843. {
  844. if ((!pZip) || (!pZip->m_pState) || (file_index >= pZip->m_total_files))
  845. return NULL;
  846. return &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index));
  847. }
  848. mz_bool mz_zip_is_file_encrypted(mz_zip_archive *pZip, mz_uint file_index)
  849. {
  850. mz_uint m_bit_flag;
  851. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  852. if (!p)
  853. {
  854. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  855. return MZ_FALSE;
  856. }
  857. m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  858. return (m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION)) != 0;
  859. }
  860. mz_bool mz_zip_is_file_supported(mz_zip_archive *pZip, mz_uint file_index)
  861. {
  862. mz_uint bit_flag;
  863. mz_uint method;
  864. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  865. if (!p)
  866. {
  867. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  868. return MZ_FALSE;
  869. }
  870. method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
  871. bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  872. if ((method != 0) && (method != MZ_DEFLATED))
  873. {
  874. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  875. return MZ_FALSE;
  876. }
  877. if (bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION))
  878. {
  879. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  880. return MZ_FALSE;
  881. }
  882. if (bit_flag & MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG)
  883. {
  884. mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  885. return MZ_FALSE;
  886. }
  887. return MZ_TRUE;
  888. }
  889. mz_bool mz_zip_is_file_a_directory(mz_zip_archive *pZip, mz_uint file_index)
  890. {
  891. mz_uint filename_len, attribute_mapping_id, external_attr;
  892. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  893. if (!p)
  894. {
  895. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  896. return MZ_FALSE;
  897. }
  898. filename_len = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  899. if (filename_len)
  900. {
  901. if (*(p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len - 1) == '/')
  902. return MZ_TRUE;
  903. }
  904. // Bugfix: This code was also checking if the internal attribute was non-zero, which wasn't correct.
  905. // Most/all zip writers (hopefully) set DOS file/directory attributes in the low 16-bits, so check for the DOS directory flag and ignore the source OS ID in the created by field.
  906. // FIXME: Remove this check? Is it necessary - we already check the filename.
  907. attribute_mapping_id = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS) >> 8;
  908. (void)attribute_mapping_id;
  909. external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
  910. if ((external_attr & MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG) != 0)
  911. {
  912. return MZ_TRUE;
  913. }
  914. return MZ_FALSE;
  915. }
  916. static mz_bool mz_zip_file_stat_internal(mz_zip_archive *pZip, mz_uint file_index, const mz_uint8 *pCentral_dir_header, mz_zip_archive_file_stat *pStat, mz_bool *pFound_zip64_extra_data)
  917. {
  918. mz_uint n;
  919. const mz_uint8 *p = pCentral_dir_header;
  920. if (pFound_zip64_extra_data)
  921. *pFound_zip64_extra_data = MZ_FALSE;
  922. if ((!p) || (!pStat))
  923. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  924. // Extract fields from the central directory record.
  925. pStat->m_file_index = file_index;
  926. pStat->m_central_dir_ofs = MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index);
  927. pStat->m_version_made_by = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_MADE_BY_OFS);
  928. pStat->m_version_needed = MZ_READ_LE16(p + MZ_ZIP_CDH_VERSION_NEEDED_OFS);
  929. pStat->m_bit_flag = MZ_READ_LE16(p + MZ_ZIP_CDH_BIT_FLAG_OFS);
  930. pStat->m_method = MZ_READ_LE16(p + MZ_ZIP_CDH_METHOD_OFS);
  931. #ifndef MINIZ_NO_TIME
  932. pStat->m_time = mz_zip_dos_to_time_t(MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_TIME_OFS), MZ_READ_LE16(p + MZ_ZIP_CDH_FILE_DATE_OFS));
  933. #endif
  934. pStat->m_crc32 = MZ_READ_LE32(p + MZ_ZIP_CDH_CRC32_OFS);
  935. pStat->m_comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
  936. pStat->m_uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
  937. pStat->m_internal_attr = MZ_READ_LE16(p + MZ_ZIP_CDH_INTERNAL_ATTR_OFS);
  938. pStat->m_external_attr = MZ_READ_LE32(p + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS);
  939. pStat->m_local_header_ofs = MZ_READ_LE32(p + MZ_ZIP_CDH_LOCAL_HEADER_OFS);
  940. // Copy as much of the filename and comment as possible.
  941. n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  942. n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILENAME_SIZE - 1);
  943. memcpy(pStat->m_filename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
  944. pStat->m_filename[n] = '\0';
  945. n = MZ_READ_LE16(p + MZ_ZIP_CDH_COMMENT_LEN_OFS);
  946. n = MZ_MIN(n, MZ_ZIP_MAX_ARCHIVE_FILE_COMMENT_SIZE - 1);
  947. pStat->m_comment_size = n;
  948. memcpy(pStat->m_comment, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS) + MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS), n);
  949. pStat->m_comment[n] = '\0';
  950. // Set some flags for convienance
  951. pStat->m_is_directory = mz_zip_is_file_a_directory(pZip, file_index);
  952. pStat->m_is_encrypted = mz_zip_is_file_encrypted(pZip, file_index);
  953. pStat->m_is_supported = mz_zip_is_file_supported(pZip, file_index);
  954. // See if we need to read any zip64 extended information fields.
  955. // Confusingly, these zip64 fields can be present even on non-zip64 archives (Debian zip on a huge files from stdin piped to stdout creates them).
  956. if (MZ_MAX(MZ_MAX(pStat->m_comp_size, pStat->m_uncomp_size), pStat->m_local_header_ofs) == MZ_UINT32_MAX)
  957. {
  958. // Attempt to find zip64 extended information field in the entry's extra data
  959. mz_uint32 extra_size_remaining = MZ_READ_LE16(p + MZ_ZIP_CDH_EXTRA_LEN_OFS);
  960. if (extra_size_remaining)
  961. {
  962. const mz_uint8 *pExtra_data = p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  963. do
  964. {
  965. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  966. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  967. mz_uint32 field_id = MZ_READ_LE16(pExtra_data);
  968. mz_uint32 field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  969. if ((field_data_size + sizeof(mz_uint16) * 2) > extra_size_remaining)
  970. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  971. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
  972. {
  973. const mz_uint8 *pField_data = pExtra_data + sizeof(mz_uint16) * 2;
  974. mz_uint32 field_data_remaining = field_data_size;
  975. if (pFound_zip64_extra_data)
  976. *pFound_zip64_extra_data = MZ_TRUE;
  977. if (pStat->m_uncomp_size == MZ_UINT32_MAX)
  978. {
  979. if (field_data_remaining < sizeof(mz_uint64))
  980. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  981. pStat->m_uncomp_size = MZ_READ_LE64(pField_data);
  982. pField_data += sizeof(mz_uint64);
  983. field_data_remaining -= sizeof(mz_uint64);
  984. }
  985. if (pStat->m_comp_size == MZ_UINT32_MAX)
  986. {
  987. if (field_data_remaining < sizeof(mz_uint64))
  988. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  989. pStat->m_comp_size = MZ_READ_LE64(pField_data);
  990. pField_data += sizeof(mz_uint64);
  991. field_data_remaining -= sizeof(mz_uint64);
  992. }
  993. if (pStat->m_local_header_ofs == MZ_UINT32_MAX)
  994. {
  995. if (field_data_remaining < sizeof(mz_uint64))
  996. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  997. pStat->m_local_header_ofs = MZ_READ_LE64(pField_data);
  998. pField_data += sizeof(mz_uint64);
  999. field_data_remaining -= sizeof(mz_uint64);
  1000. }
  1001. break;
  1002. }
  1003. pExtra_data += sizeof(mz_uint16) * 2 + field_data_size;
  1004. extra_size_remaining = extra_size_remaining - sizeof(mz_uint16) * 2 - field_data_size;
  1005. } while (extra_size_remaining);
  1006. }
  1007. }
  1008. return MZ_TRUE;
  1009. }
  1010. static MZ_FORCEINLINE mz_bool mz_zip_string_equal(const char *pA, const char *pB, mz_uint len, mz_uint flags)
  1011. {
  1012. mz_uint i;
  1013. if (flags & MZ_ZIP_FLAG_CASE_SENSITIVE)
  1014. return 0 == memcmp(pA, pB, len);
  1015. for (i = 0; i < len; ++i)
  1016. if (MZ_TOLOWER(pA[i]) != MZ_TOLOWER(pB[i]))
  1017. return MZ_FALSE;
  1018. return MZ_TRUE;
  1019. }
  1020. static MZ_FORCEINLINE int mz_zip_filename_compare(const mz_zip_array *pCentral_dir_array, const mz_zip_array *pCentral_dir_offsets, mz_uint l_index, const char *pR, mz_uint r_len)
  1021. {
  1022. const mz_uint8 *pL = &MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_array, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(pCentral_dir_offsets, mz_uint32, l_index)), *pE;
  1023. mz_uint l_len = MZ_READ_LE16(pL + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  1024. mz_uint8 l = 0, r = 0;
  1025. pL += MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  1026. pE = pL + MZ_MIN(l_len, r_len);
  1027. while (pL < pE)
  1028. {
  1029. if ((l = MZ_TOLOWER(*pL)) != (r = MZ_TOLOWER(*pR)))
  1030. break;
  1031. pL++;
  1032. pR++;
  1033. }
  1034. return (pL == pE) ? (int)(l_len - r_len) : (l - r);
  1035. }
  1036. static mz_bool mz_zip_locate_file_binary_search(mz_zip_archive *pZip, const char *pFilename, mz_uint32 *pIndex)
  1037. {
  1038. mz_zip_internal_state *pState = pZip->m_pState;
  1039. const mz_zip_array *pCentral_dir_offsets = &pState->m_central_dir_offsets;
  1040. const mz_zip_array *pCentral_dir = &pState->m_central_dir;
  1041. mz_uint32 *pIndices = &MZ_ZIP_ARRAY_ELEMENT(&pState->m_sorted_central_dir_offsets, mz_uint32, 0);
  1042. const uint32_t size = pZip->m_total_files;
  1043. const mz_uint filename_len = (mz_uint)strlen(pFilename);
  1044. if (pIndex)
  1045. *pIndex = 0;
  1046. if (size)
  1047. {
  1048. // yes I could use uint32_t's, but then we would have to add some special case checks in the loop, argh, and
  1049. // honestly the major expense here on 32-bit CPU's will still be the filename compare
  1050. mz_int64 l = 0, h = (mz_int64)size - 1;
  1051. while (l <= h)
  1052. {
  1053. mz_int64 m = l + ((h - l) >> 1);
  1054. uint32_t file_index = pIndices[(uint32_t)m];
  1055. int comp = mz_zip_filename_compare(pCentral_dir, pCentral_dir_offsets, file_index, pFilename, filename_len);
  1056. if (!comp)
  1057. {
  1058. if (pIndex)
  1059. *pIndex = file_index;
  1060. return MZ_TRUE;
  1061. }
  1062. else if (comp < 0)
  1063. l = m + 1;
  1064. else
  1065. h = m - 1;
  1066. }
  1067. }
  1068. return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
  1069. }
  1070. mz_bool mz_zip_locate_file(mz_zip_archive *pZip, const char *pName, const char *pComment, mz_uint flags, mz_uint32 *pIndex)
  1071. {
  1072. mz_uint file_index;
  1073. size_t name_len, comment_len;
  1074. if (pIndex)
  1075. *pIndex = 0;
  1076. if ((!pZip) || (!pZip->m_pState) || (!pName))
  1077. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1078. // See if we can use a binary search
  1079. if (((pZip->m_pState->m_init_flags & MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY) == 0) &&
  1080. (pZip->m_zip_mode == MZ_ZIP_MODE_READING) &&
  1081. ((flags & (MZ_ZIP_FLAG_IGNORE_PATH | MZ_ZIP_FLAG_CASE_SENSITIVE)) == 0) && (!pComment) && (pZip->m_pState->m_sorted_central_dir_offsets.m_size))
  1082. {
  1083. return mz_zip_locate_file_binary_search(pZip, pName, pIndex);
  1084. }
  1085. // Locate the entry by scanning the entire central directory
  1086. name_len = strlen(pName);
  1087. if (name_len > MZ_UINT16_MAX)
  1088. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1089. comment_len = pComment ? strlen(pComment) : 0;
  1090. if (comment_len > MZ_UINT16_MAX)
  1091. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1092. for (file_index = 0; file_index < pZip->m_total_files; file_index++)
  1093. {
  1094. const mz_uint8 *pHeader = &MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir, mz_uint8, MZ_ZIP_ARRAY_ELEMENT(&pZip->m_pState->m_central_dir_offsets, mz_uint32, file_index));
  1095. mz_uint filename_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  1096. const char *pFilename = (const char *)pHeader + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE;
  1097. if (filename_len < name_len)
  1098. continue;
  1099. if (comment_len)
  1100. {
  1101. mz_uint file_extra_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_EXTRA_LEN_OFS), file_comment_len = MZ_READ_LE16(pHeader + MZ_ZIP_CDH_COMMENT_LEN_OFS);
  1102. const char *pFile_comment = pFilename + filename_len + file_extra_len;
  1103. if ((file_comment_len != comment_len) || (!mz_zip_string_equal(pComment, pFile_comment, file_comment_len, flags)))
  1104. continue;
  1105. }
  1106. if ((flags & MZ_ZIP_FLAG_IGNORE_PATH) && (filename_len))
  1107. {
  1108. int ofs = filename_len - 1;
  1109. do
  1110. {
  1111. if ((pFilename[ofs] == '/') || (pFilename[ofs] == '\\') || (pFilename[ofs] == ':'))
  1112. break;
  1113. } while (--ofs >= 0);
  1114. ofs++;
  1115. pFilename += ofs;
  1116. filename_len -= ofs;
  1117. }
  1118. if ((filename_len == name_len) && (mz_zip_string_equal(pName, pFilename, filename_len, flags)))
  1119. {
  1120. *pIndex = file_index;
  1121. return MZ_TRUE;
  1122. }
  1123. }
  1124. return mz_zip_set_error(pZip, MZ_ZIP_FILE_NOT_FOUND);
  1125. }
  1126. mz_bool mz_zip_extract_to_mem_no_alloc(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size)
  1127. {
  1128. int status = TINFL_STATUS_DONE;
  1129. mz_uint64 needed_size, cur_file_ofs, comp_remaining, out_buf_ofs = 0, read_buf_size, read_buf_ofs = 0, read_buf_avail;
  1130. mz_zip_archive_file_stat file_stat;
  1131. void *pRead_buf;
  1132. mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
  1133. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  1134. tinfl_decompressor inflator;
  1135. if ((!pZip) || (!pZip->m_pState) || ((buf_size) && (!pBuf)) || ((user_read_buf_size) && (!pUser_read_buf)) || (!pZip->m_pRead))
  1136. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1137. if (!mz_zip_file_stat(pZip, file_index, &file_stat))
  1138. return MZ_FALSE;
  1139. // A directory or zero length file
  1140. if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
  1141. return MZ_TRUE;
  1142. // Encryption and patch files are not supported.
  1143. if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
  1144. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  1145. // This function only supports decompressing stored and deflate.
  1146. if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
  1147. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  1148. // Ensure supplied output buffer is large enough.
  1149. needed_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? file_stat.m_comp_size : file_stat.m_uncomp_size;
  1150. if (buf_size < needed_size)
  1151. return mz_zip_set_error(pZip, MZ_ZIP_BUF_TOO_SMALL);
  1152. // Read and parse the local directory entry.
  1153. cur_file_ofs = file_stat.m_local_header_ofs;
  1154. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  1155. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1156. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  1157. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1158. cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  1159. if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
  1160. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1161. if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method))
  1162. {
  1163. // The file is stored or the caller has requested the compressed data.
  1164. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pBuf, (size_t)needed_size) != needed_size)
  1165. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1166. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1167. if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) == 0)
  1168. {
  1169. if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)
  1170. return mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
  1171. }
  1172. #endif
  1173. return MZ_TRUE;
  1174. }
  1175. // Decompress the file either directly from memory or from a file input buffer.
  1176. tinfl_init(&inflator);
  1177. if (pZip->m_pState->m_pMem)
  1178. {
  1179. // Read directly from the archive in memory.
  1180. pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;
  1181. read_buf_size = read_buf_avail = file_stat.m_comp_size;
  1182. comp_remaining = 0;
  1183. }
  1184. else if (pUser_read_buf)
  1185. {
  1186. // Use a user provided read buffer.
  1187. if (!user_read_buf_size)
  1188. return MZ_FALSE;
  1189. pRead_buf = (mz_uint8 *)pUser_read_buf;
  1190. read_buf_size = user_read_buf_size;
  1191. read_buf_avail = 0;
  1192. comp_remaining = file_stat.m_comp_size;
  1193. }
  1194. else
  1195. {
  1196. // Temporarily allocate a read buffer.
  1197. read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
  1198. if (((sizeof(size_t) == sizeof(mz_uint32))) && (read_buf_size > 0x7FFFFFFF))
  1199. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  1200. if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))
  1201. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1202. read_buf_avail = 0;
  1203. comp_remaining = file_stat.m_comp_size;
  1204. }
  1205. do
  1206. {
  1207. // The size_t cast here should be OK because we've verified that the output buffer is >= file_stat.m_uncomp_size above
  1208. size_t in_buf_size, out_buf_size = (size_t)(file_stat.m_uncomp_size - out_buf_ofs);
  1209. if ((!read_buf_avail) && (!pZip->m_pState->m_pMem))
  1210. {
  1211. read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
  1212. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
  1213. {
  1214. status = TINFL_STATUS_FAILED;
  1215. mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  1216. break;
  1217. }
  1218. cur_file_ofs += read_buf_avail;
  1219. comp_remaining -= read_buf_avail;
  1220. read_buf_ofs = 0;
  1221. }
  1222. in_buf_size = (size_t)read_buf_avail;
  1223. status = tinfl_decompress(&inflator, (mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pBuf, (mz_uint8 *)pBuf + out_buf_ofs, &out_buf_size, TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | (comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0));
  1224. read_buf_avail -= in_buf_size;
  1225. read_buf_ofs += in_buf_size;
  1226. out_buf_ofs += out_buf_size;
  1227. } while (status == TINFL_STATUS_NEEDS_MORE_INPUT);
  1228. if (status == TINFL_STATUS_DONE)
  1229. {
  1230. // Make sure the entire file was decompressed, and check its CRC.
  1231. if (out_buf_ofs != file_stat.m_uncomp_size)
  1232. {
  1233. mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
  1234. status = TINFL_STATUS_FAILED;
  1235. }
  1236. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1237. else if (mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, (size_t)file_stat.m_uncomp_size) != file_stat.m_crc32)
  1238. {
  1239. mz_zip_set_error(pZip, MZ_ZIP_CRC_CHECK_FAILED);
  1240. status = TINFL_STATUS_FAILED;
  1241. }
  1242. #endif
  1243. }
  1244. if ((!pZip->m_pState->m_pMem) && (!pUser_read_buf))
  1245. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  1246. return status == TINFL_STATUS_DONE;
  1247. }
  1248. mz_bool mz_zip_extract_file_to_mem_no_alloc(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags, void *pUser_read_buf, size_t user_read_buf_size)
  1249. {
  1250. mz_uint32 file_index;
  1251. if (!mz_zip_locate_file(pZip, pFilename, NULL, flags, &file_index))
  1252. return MZ_FALSE;
  1253. return mz_zip_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, pUser_read_buf, user_read_buf_size);
  1254. }
  1255. mz_bool mz_zip_extract_to_mem(mz_zip_archive *pZip, mz_uint file_index, void *pBuf, size_t buf_size, mz_uint flags)
  1256. {
  1257. return mz_zip_extract_to_mem_no_alloc(pZip, file_index, pBuf, buf_size, flags, NULL, 0);
  1258. }
  1259. mz_bool mz_zip_extract_file_to_mem(mz_zip_archive *pZip, const char *pFilename, void *pBuf, size_t buf_size, mz_uint flags)
  1260. {
  1261. return mz_zip_extract_file_to_mem_no_alloc(pZip, pFilename, pBuf, buf_size, flags, NULL, 0);
  1262. }
  1263. void *mz_zip_extract_to_heap(mz_zip_archive *pZip, mz_uint file_index, size_t *pSize, mz_uint flags)
  1264. {
  1265. mz_uint64 comp_size, uncomp_size, alloc_size;
  1266. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  1267. void *pBuf;
  1268. if (pSize)
  1269. *pSize = 0;
  1270. if (!p)
  1271. {
  1272. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1273. return NULL;
  1274. }
  1275. comp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS);
  1276. uncomp_size = MZ_READ_LE32(p + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS);
  1277. alloc_size = (flags & MZ_ZIP_FLAG_COMPRESSED_DATA) ? comp_size : uncomp_size;
  1278. if (((sizeof(size_t) == sizeof(mz_uint32))) && (alloc_size > 0x7FFFFFFF))
  1279. {
  1280. mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  1281. return NULL;
  1282. }
  1283. if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)alloc_size)))
  1284. {
  1285. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1286. return NULL;
  1287. }
  1288. if (!mz_zip_extract_to_mem(pZip, file_index, pBuf, (size_t)alloc_size, flags))
  1289. {
  1290. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  1291. return NULL;
  1292. }
  1293. if (pSize)
  1294. *pSize = (size_t)alloc_size;
  1295. return pBuf;
  1296. }
  1297. void *mz_zip_extract_file_to_heap(mz_zip_archive *pZip, const char *pFilename, size_t *pSize, mz_uint flags)
  1298. {
  1299. mz_uint32 file_index;
  1300. if (!mz_zip_locate_file(pZip, pFilename, NULL, flags, &file_index))
  1301. {
  1302. if (pSize)
  1303. *pSize = 0;
  1304. return MZ_FALSE;
  1305. }
  1306. return mz_zip_extract_to_heap(pZip, file_index, pSize, flags);
  1307. }
  1308. mz_bool mz_zip_extract_to_callback(mz_zip_archive *pZip, mz_uint file_index, mz_file_write_func pCallback, void *pOpaque, mz_uint flags)
  1309. {
  1310. int status = TINFL_STATUS_DONE;
  1311. mz_uint file_crc32 = MZ_CRC32_INIT;
  1312. mz_uint64 read_buf_size, read_buf_ofs = 0, read_buf_avail, comp_remaining, out_buf_ofs = 0, cur_file_ofs;
  1313. mz_zip_archive_file_stat file_stat;
  1314. void *pRead_buf = NULL;
  1315. void *pWrite_buf = NULL;
  1316. mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
  1317. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  1318. if ((!pZip) || (!pZip->m_pState) || (!pCallback) || (!pZip->m_pRead))
  1319. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1320. if (!mz_zip_file_stat(pZip, file_index, &file_stat))
  1321. return MZ_FALSE;
  1322. // A directory or zero length file
  1323. if ((file_stat.m_is_directory) || (!file_stat.m_comp_size))
  1324. return MZ_TRUE;
  1325. // Encryption and patch files are not supported.
  1326. if (file_stat.m_bit_flag & (MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_IS_ENCRYPTED | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_USES_STRONG_ENCRYPTION | MZ_ZIP_GENERAL_PURPOSE_BIT_FLAG_COMPRESSED_PATCH_FLAG))
  1327. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  1328. // This function only supports decompressing stored and deflate.
  1329. if ((!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
  1330. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  1331. // Read and do some minimal validation of the local directory entry (this doesn't crack the zip64 stuff, which we already have from the central dir)
  1332. cur_file_ofs = file_stat.m_local_header_ofs;
  1333. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  1334. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1335. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  1336. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1337. cur_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS) + MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  1338. if ((cur_file_ofs + file_stat.m_comp_size) > pZip->m_archive_size)
  1339. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1340. // Decompress the file either directly from memory or from a file input buffer.
  1341. if (pZip->m_pState->m_pMem)
  1342. {
  1343. pRead_buf = (mz_uint8 *)pZip->m_pState->m_pMem + cur_file_ofs;
  1344. read_buf_size = read_buf_avail = file_stat.m_comp_size;
  1345. comp_remaining = 0;
  1346. }
  1347. else
  1348. {
  1349. read_buf_size = MZ_MIN(file_stat.m_comp_size, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
  1350. if (NULL == (pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)read_buf_size)))
  1351. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1352. read_buf_avail = 0;
  1353. comp_remaining = file_stat.m_comp_size;
  1354. }
  1355. if ((flags & MZ_ZIP_FLAG_COMPRESSED_DATA) || (!file_stat.m_method))
  1356. {
  1357. // The file is stored or the caller has requested the compressed data.
  1358. if (pZip->m_pState->m_pMem)
  1359. {
  1360. if (((sizeof(size_t) == sizeof(mz_uint32))) && (file_stat.m_comp_size > MZ_UINT32_MAX))
  1361. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  1362. if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)file_stat.m_comp_size) != file_stat.m_comp_size)
  1363. {
  1364. mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
  1365. status = TINFL_STATUS_FAILED;
  1366. }
  1367. else if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
  1368. {
  1369. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1370. file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)file_stat.m_comp_size);
  1371. #endif
  1372. }
  1373. cur_file_ofs += file_stat.m_comp_size;
  1374. out_buf_ofs += file_stat.m_comp_size;
  1375. comp_remaining = 0;
  1376. }
  1377. else
  1378. {
  1379. while (comp_remaining)
  1380. {
  1381. read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
  1382. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
  1383. {
  1384. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1385. status = TINFL_STATUS_FAILED;
  1386. break;
  1387. }
  1388. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1389. if (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
  1390. {
  1391. file_crc32 = (mz_uint32)mz_crc32(file_crc32, (const mz_uint8 *)pRead_buf, (size_t)read_buf_avail);
  1392. }
  1393. #endif
  1394. if (pCallback(pOpaque, out_buf_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
  1395. {
  1396. mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
  1397. status = TINFL_STATUS_FAILED;
  1398. break;
  1399. }
  1400. cur_file_ofs += read_buf_avail;
  1401. out_buf_ofs += read_buf_avail;
  1402. comp_remaining -= read_buf_avail;
  1403. }
  1404. }
  1405. }
  1406. else
  1407. {
  1408. tinfl_decompressor inflator;
  1409. tinfl_init(&inflator);
  1410. if (NULL == (pWrite_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, TINFL_LZ_DICT_SIZE)))
  1411. {
  1412. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1413. status = TINFL_STATUS_FAILED;
  1414. }
  1415. else
  1416. {
  1417. do
  1418. {
  1419. mz_uint8 *pWrite_buf_cur = (mz_uint8 *)pWrite_buf + (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
  1420. size_t in_buf_size, out_buf_size = TINFL_LZ_DICT_SIZE - (out_buf_ofs & (TINFL_LZ_DICT_SIZE - 1));
  1421. if ((!read_buf_avail) && (!pZip->m_pState->m_pMem))
  1422. {
  1423. read_buf_avail = MZ_MIN(read_buf_size, comp_remaining);
  1424. if (pZip->m_pRead(pZip->m_pIO_opaque, cur_file_ofs, pRead_buf, (size_t)read_buf_avail) != read_buf_avail)
  1425. {
  1426. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1427. status = TINFL_STATUS_FAILED;
  1428. break;
  1429. }
  1430. cur_file_ofs += read_buf_avail;
  1431. comp_remaining -= read_buf_avail;
  1432. read_buf_ofs = 0;
  1433. }
  1434. in_buf_size = (size_t)read_buf_avail;
  1435. status = tinfl_decompress(&inflator, (const mz_uint8 *)pRead_buf + read_buf_ofs, &in_buf_size, (mz_uint8 *)pWrite_buf, pWrite_buf_cur, &out_buf_size, comp_remaining ? TINFL_FLAG_HAS_MORE_INPUT : 0);
  1436. read_buf_avail -= in_buf_size;
  1437. read_buf_ofs += in_buf_size;
  1438. if (out_buf_size)
  1439. {
  1440. if (pCallback(pOpaque, out_buf_ofs, pWrite_buf_cur, out_buf_size) != out_buf_size)
  1441. {
  1442. mz_zip_set_error(pZip, MZ_ZIP_WRITE_CALLBACK_FAILED);
  1443. status = TINFL_STATUS_FAILED;
  1444. break;
  1445. }
  1446. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1447. file_crc32 = (mz_uint32)mz_crc32(file_crc32, pWrite_buf_cur, out_buf_size);
  1448. #endif
  1449. if ((out_buf_ofs += out_buf_size) > file_stat.m_uncomp_size)
  1450. {
  1451. mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  1452. status = TINFL_STATUS_FAILED;
  1453. break;
  1454. }
  1455. }
  1456. } while ((status == TINFL_STATUS_NEEDS_MORE_INPUT) || (status == TINFL_STATUS_HAS_MORE_OUTPUT));
  1457. }
  1458. }
  1459. if ((status == TINFL_STATUS_DONE) && (!(flags & MZ_ZIP_FLAG_COMPRESSED_DATA)))
  1460. {
  1461. // Make sure the entire file was decompressed, and check its CRC.
  1462. if (out_buf_ofs != file_stat.m_uncomp_size)
  1463. {
  1464. mz_zip_set_error(pZip, MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE);
  1465. status = TINFL_STATUS_FAILED;
  1466. }
  1467. #ifndef MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS
  1468. else if (file_crc32 != file_stat.m_crc32)
  1469. {
  1470. mz_zip_set_error(pZip, MZ_ZIP_DECOMPRESSION_FAILED);
  1471. status = TINFL_STATUS_FAILED;
  1472. }
  1473. #endif
  1474. }
  1475. if (!pZip->m_pState->m_pMem)
  1476. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  1477. if (pWrite_buf)
  1478. pZip->m_pFree(pZip->m_pAlloc_opaque, pWrite_buf);
  1479. return status == TINFL_STATUS_DONE;
  1480. }
  1481. mz_bool mz_zip_extract_file_to_callback(mz_zip_archive *pZip, const char *pFilename, mz_file_write_func pCallback, void *pOpaque, mz_uint flags)
  1482. {
  1483. mz_uint32 file_index;
  1484. if (!mz_zip_locate_file(pZip, pFilename, NULL, flags, &file_index))
  1485. return MZ_FALSE;
  1486. return mz_zip_extract_to_callback(pZip, file_index, pCallback, pOpaque, flags);
  1487. }
  1488. #ifndef MINIZ_NO_STDIO
  1489. static size_t mz_zip_file_write_callback(void *pOpaque, mz_uint64 ofs, const void *pBuf, size_t n)
  1490. {
  1491. (void)ofs;
  1492. return MZ_FWRITE(pBuf, 1, n, (MZ_FILE *)pOpaque);
  1493. }
  1494. mz_bool mz_zip_extract_to_file(mz_zip_archive *pZip, mz_uint file_index, const char *pDst_filename, mz_uint flags)
  1495. {
  1496. mz_bool status;
  1497. mz_zip_archive_file_stat file_stat;
  1498. MZ_FILE *pFile;
  1499. if (!mz_zip_file_stat(pZip, file_index, &file_stat))
  1500. return MZ_FALSE;
  1501. if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
  1502. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  1503. pFile = MZ_FOPEN(pDst_filename, "wb");
  1504. if (!pFile)
  1505. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  1506. status = mz_zip_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);
  1507. if (MZ_FCLOSE(pFile) == EOF)
  1508. {
  1509. if (status)
  1510. mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
  1511. status = MZ_FALSE;
  1512. }
  1513. #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
  1514. if (status)
  1515. mz_zip_set_file_times(pDst_filename, file_stat.m_time, file_stat.m_time);
  1516. #endif
  1517. return status;
  1518. }
  1519. mz_bool mz_zip_extract_file_to_file(mz_zip_archive *pZip, const char *pArchive_filename, const char *pDst_filename, mz_uint flags)
  1520. {
  1521. mz_uint32 file_index;
  1522. if (!mz_zip_locate_file(pZip, pArchive_filename, NULL, flags, &file_index))
  1523. return MZ_FALSE;
  1524. return mz_zip_extract_to_file(pZip, file_index, pDst_filename, flags);
  1525. }
  1526. mz_bool mz_zip_extract_to_cfile(mz_zip_archive *pZip, mz_uint file_index, MZ_FILE *pFile, mz_uint flags)
  1527. {
  1528. mz_zip_archive_file_stat file_stat;
  1529. if (!mz_zip_file_stat(pZip, file_index, &file_stat))
  1530. return MZ_FALSE;
  1531. if ((file_stat.m_is_directory) || (!file_stat.m_is_supported))
  1532. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  1533. return mz_zip_extract_to_callback(pZip, file_index, mz_zip_file_write_callback, pFile, flags);
  1534. }
  1535. mz_bool mz_zip_extract_file_to_cfile(mz_zip_archive *pZip, const char *pArchive_filename, MZ_FILE *pFile, mz_uint flags)
  1536. {
  1537. mz_uint32 file_index;
  1538. if (!mz_zip_locate_file(pZip, pArchive_filename, NULL, flags, &file_index))
  1539. return MZ_FALSE;
  1540. return mz_zip_extract_to_cfile(pZip, file_index, pFile, flags);
  1541. }
  1542. #endif // #ifndef MINIZ_NO_STDIO
  1543. static size_t mz_zip_compute_crc32_callback(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
  1544. {
  1545. mz_uint32 *p = (mz_uint32 *)pOpaque;
  1546. (void)file_ofs;
  1547. *p = (mz_uint32)mz_crc32(*p, (const mz_uint8 *)pBuf, n);
  1548. return n;
  1549. }
  1550. mz_bool mz_zip_validate_file(mz_zip_archive *pZip, mz_uint file_index, mz_uint flags)
  1551. {
  1552. mz_zip_archive_file_stat file_stat;
  1553. mz_zip_internal_state *pState;
  1554. const mz_uint8 *pCentral_dir_header;
  1555. mz_bool found_zip64_ext_data_in_cdir = MZ_FALSE;
  1556. mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
  1557. mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
  1558. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  1559. mz_uint64 local_header_ofs = 0;
  1560. mz_uint32 local_header_filename_len, local_header_extra_len, local_header_crc32;
  1561. mz_uint64 local_header_comp_size, local_header_uncomp_size;
  1562. mz_uint32 uncomp_crc32 = MZ_CRC32_INIT;
  1563. mz_bool has_data_descriptor;
  1564. mz_uint32 local_header_bit_flags;
  1565. mz_zip_array file_data_array;
  1566. mz_zip_array_init(&file_data_array, 1);
  1567. if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
  1568. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1569. if (file_index > pZip->m_total_files)
  1570. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1571. pState = pZip->m_pState;
  1572. pCentral_dir_header = mz_zip_get_cdh(pZip, file_index);
  1573. if (!mz_zip_file_stat_internal(pZip, file_index, pCentral_dir_header, &file_stat, &found_zip64_ext_data_in_cdir))
  1574. return MZ_FALSE;
  1575. // A directory or zero length file
  1576. if ((file_stat.m_is_directory) || (!file_stat.m_uncomp_size))
  1577. return MZ_TRUE;
  1578. // Encryption and patch files are not supported.
  1579. if (file_stat.m_is_encrypted)
  1580. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_ENCRYPTION);
  1581. // This function only supports stored and deflate.
  1582. if ((file_stat.m_method != 0) && (file_stat.m_method != MZ_DEFLATED))
  1583. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_METHOD);
  1584. if (!file_stat.m_is_supported)
  1585. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_FEATURE);
  1586. // Read and parse the local directory entry.
  1587. local_header_ofs = file_stat.m_local_header_ofs;
  1588. if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  1589. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1590. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  1591. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1592. local_header_filename_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
  1593. local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  1594. local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
  1595. local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
  1596. local_header_crc32 = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_CRC32_OFS);
  1597. local_header_bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
  1598. has_data_descriptor = (local_header_bit_flags & 8) != 0;
  1599. if (local_header_filename_len != strlen(file_stat.m_filename))
  1600. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1601. if ((local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size) > pZip->m_archive_size)
  1602. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1603. if (!mz_zip_array_resize(pZip, &file_data_array, MZ_MAX(local_header_filename_len, local_header_extra_len), MZ_FALSE))
  1604. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1605. if (local_header_filename_len)
  1606. {
  1607. if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE, file_data_array.m_p, local_header_filename_len) != local_header_filename_len)
  1608. {
  1609. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1610. goto handle_failure;
  1611. }
  1612. // I've seen 1 archive that had the same pathname, but used backslashes in the local dir and forward slashes in the central dir. Do we care about this? For now, this case will fail validation.
  1613. if (memcmp(file_stat.m_filename, file_data_array.m_p, local_header_filename_len) != 0)
  1614. {
  1615. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  1616. goto handle_failure;
  1617. }
  1618. }
  1619. if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX)))
  1620. {
  1621. if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len, file_data_array.m_p, local_header_extra_len) != local_header_extra_len)
  1622. {
  1623. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1624. goto handle_failure;
  1625. }
  1626. mz_uint32 extra_size_remaining = local_header_extra_len;
  1627. const mz_uint8 *pExtra_data = (const mz_uint8 *)file_data_array.m_p;
  1628. do
  1629. {
  1630. mz_uint32 field_id, field_data_size, field_total_size;
  1631. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  1632. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1633. field_id = MZ_READ_LE16(pExtra_data);
  1634. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  1635. field_total_size = field_data_size + sizeof(mz_uint16) * 2;
  1636. if (field_total_size > extra_size_remaining)
  1637. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1638. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
  1639. {
  1640. const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);
  1641. if (field_data_size < sizeof(mz_uint64) * 2)
  1642. {
  1643. mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  1644. goto handle_failure;
  1645. }
  1646. local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
  1647. local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64));
  1648. found_zip64_ext_data_in_ldir = MZ_TRUE;
  1649. break;
  1650. }
  1651. pExtra_data += field_total_size;
  1652. extra_size_remaining -= field_total_size;
  1653. } while (extra_size_remaining);
  1654. }
  1655. // TODO: parse local header extra data when local_header_comp_size is 0xFFFFFFFF! (big_descriptor.zip)
  1656. // I've seen zips in the wild with the data descriptor bit set, but proper local header values and bogus data descriptors
  1657. if ((has_data_descriptor) && (!local_header_comp_size) && (!local_header_crc32))
  1658. {
  1659. mz_uint8 descriptor_buf[32];
  1660. mz_uint32 num_descriptor_uint32s = ((pState->m_zip64) || (found_zip64_ext_data_in_ldir)) ? 6 : 4;
  1661. if (pZip->m_pRead(pZip->m_pIO_opaque, local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_len + local_header_extra_len + file_stat.m_comp_size, descriptor_buf, sizeof(mz_uint32) * num_descriptor_uint32s) != (sizeof(mz_uint32) * num_descriptor_uint32s))
  1662. {
  1663. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  1664. goto handle_failure;
  1665. }
  1666. mz_bool has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
  1667. const mz_uint8 *pSrc = has_id ? (descriptor_buf + sizeof(mz_uint32)) : descriptor_buf;
  1668. mz_uint32 file_crc32 = MZ_READ_LE32(pSrc);
  1669. mz_uint64 comp_size = 0, uncomp_size = 0;
  1670. if ((pState->m_zip64) || (found_zip64_ext_data_in_ldir))
  1671. {
  1672. comp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32));
  1673. uncomp_size = MZ_READ_LE64(pSrc + sizeof(mz_uint32) + sizeof(mz_uint64));
  1674. }
  1675. else
  1676. {
  1677. comp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32));
  1678. uncomp_size = MZ_READ_LE32(pSrc + sizeof(mz_uint32) + sizeof(mz_uint32));
  1679. }
  1680. if ((file_crc32 != file_stat.m_crc32) || (comp_size != file_stat.m_comp_size) || (uncomp_size != file_stat.m_uncomp_size))
  1681. {
  1682. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  1683. goto handle_failure;
  1684. }
  1685. }
  1686. else
  1687. {
  1688. if ((local_header_crc32 != file_stat.m_crc32) || (local_header_comp_size != file_stat.m_comp_size) || (local_header_uncomp_size != file_stat.m_uncomp_size))
  1689. {
  1690. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  1691. goto handle_failure;
  1692. }
  1693. }
  1694. mz_zip_array_clear(pZip, &file_data_array);
  1695. if ((flags & MZ_ZIP_FLAG_VALIDATE_HEADERS_ONLY) == 0)
  1696. {
  1697. if (!mz_zip_extract_to_callback(pZip, file_index, mz_zip_compute_crc32_callback, &uncomp_crc32, 0))
  1698. return MZ_FALSE;
  1699. // 1 more check to be sure, although the extract checks too.
  1700. if (uncomp_crc32 != file_stat.m_crc32)
  1701. {
  1702. mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  1703. return MZ_FALSE;
  1704. }
  1705. }
  1706. return MZ_TRUE;
  1707. handle_failure:
  1708. mz_zip_array_clear(pZip, &file_data_array);
  1709. return MZ_FALSE;
  1710. }
  1711. mz_bool mz_zip_validate_archive(mz_zip_archive *pZip, mz_uint flags)
  1712. {
  1713. mz_zip_internal_state *pState;
  1714. if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || (!pZip->m_pRead))
  1715. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1716. pState = pZip->m_pState;
  1717. // Basic sanity checks
  1718. if (!pState->m_zip64)
  1719. {
  1720. if (pZip->m_total_files > MZ_UINT16_MAX)
  1721. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  1722. if (pZip->m_archive_size > MZ_UINT32_MAX)
  1723. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  1724. }
  1725. else
  1726. {
  1727. if (pZip->m_total_files >= MZ_UINT32_MAX)
  1728. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  1729. if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
  1730. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  1731. }
  1732. uint32_t i;
  1733. for (i = 0; i < pZip->m_total_files; i++)
  1734. {
  1735. if (MZ_ZIP_FLAG_VALIDATE_LOCATE_FILE_FLAG & flags)
  1736. {
  1737. mz_uint32 found_index;
  1738. mz_zip_archive_file_stat stat;
  1739. if (!mz_zip_file_stat(pZip, i, &stat))
  1740. return MZ_FALSE;
  1741. if (!mz_zip_locate_file(pZip, stat.m_filename, NULL, 0, &found_index))
  1742. return MZ_FALSE;
  1743. // This check can fail if there are duplicate filenames in the archive (which we don't check for when writing - that's up to the user)
  1744. if (found_index != i)
  1745. return mz_zip_set_error(pZip, MZ_ZIP_VALIDATION_FAILED);
  1746. }
  1747. if (!mz_zip_validate_file(pZip, i, flags))
  1748. return MZ_FALSE;
  1749. }
  1750. return MZ_TRUE;
  1751. }
  1752. mz_bool mz_zip_validate_mem_archive(const void *pMem, size_t size, mz_uint flags, mz_zip_error *pErr)
  1753. {
  1754. mz_bool success = MZ_TRUE;
  1755. mz_zip_archive zip;
  1756. mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
  1757. if ((!pMem) || (!size))
  1758. {
  1759. if (pErr)
  1760. *pErr = MZ_ZIP_INVALID_PARAMETER;
  1761. return MZ_FALSE;
  1762. }
  1763. mz_zip_zero_struct(&zip);
  1764. if (!mz_zip_reader_init_mem(&zip, pMem, size, flags))
  1765. {
  1766. if (pErr)
  1767. *pErr = zip.m_last_error;
  1768. return MZ_FALSE;
  1769. }
  1770. if (!mz_zip_validate_archive(&zip, flags))
  1771. {
  1772. actual_err = zip.m_last_error;
  1773. success = MZ_FALSE;
  1774. }
  1775. if (!mz_zip_reader_end_internal(&zip, success))
  1776. {
  1777. if (!actual_err)
  1778. actual_err = zip.m_last_error;
  1779. success = MZ_FALSE;
  1780. }
  1781. if (pErr)
  1782. *pErr = actual_err;
  1783. return success;
  1784. }
  1785. #ifndef MINIZ_NO_STDIO
  1786. mz_bool mz_zip_validate_file_archive(const char *pFilename, mz_uint flags, mz_zip_error *pErr)
  1787. {
  1788. mz_bool success = MZ_TRUE;
  1789. mz_zip_archive zip;
  1790. mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
  1791. if (!pFilename)
  1792. {
  1793. if (pErr)
  1794. *pErr = MZ_ZIP_INVALID_PARAMETER;
  1795. return MZ_FALSE;
  1796. }
  1797. mz_zip_zero_struct(&zip);
  1798. if (!mz_zip_reader_init_file(&zip, pFilename, flags, 0, 0))
  1799. {
  1800. if (pErr)
  1801. *pErr = zip.m_last_error;
  1802. return MZ_FALSE;
  1803. }
  1804. if (!mz_zip_validate_archive(&zip, flags))
  1805. {
  1806. actual_err = zip.m_last_error;
  1807. success = MZ_FALSE;
  1808. }
  1809. if (!mz_zip_reader_end_internal(&zip, success))
  1810. {
  1811. if (!actual_err)
  1812. actual_err = zip.m_last_error;
  1813. success = MZ_FALSE;
  1814. }
  1815. if (pErr)
  1816. *pErr = actual_err;
  1817. return success;
  1818. }
  1819. #endif // #ifndef MINIZ_NO_STDIO
  1820. // ------------------- .ZIP archive writing
  1821. #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  1822. static MZ_FORCEINLINE void mz_write_le16(mz_uint8 *p, mz_uint16 v)
  1823. {
  1824. p[0] = (mz_uint8)v;
  1825. p[1] = (mz_uint8)(v >> 8);
  1826. }
  1827. static MZ_FORCEINLINE void mz_write_le32(mz_uint8 *p, mz_uint32 v)
  1828. {
  1829. p[0] = (mz_uint8)v;
  1830. p[1] = (mz_uint8)(v >> 8);
  1831. p[2] = (mz_uint8)(v >> 16);
  1832. p[3] = (mz_uint8)(v >> 24);
  1833. }
  1834. static MZ_FORCEINLINE void mz_write_le64(mz_uint8 *p, mz_uint64 v)
  1835. {
  1836. mz_write_le32(p, (mz_uint32)v);
  1837. mz_write_le32(p + sizeof(mz_uint32), (mz_uint32)(v >> 32));
  1838. }
  1839. #define MZ_WRITE_LE16(p, v) mz_write_le16((mz_uint8 *)(p), (mz_uint16)(v))
  1840. #define MZ_WRITE_LE32(p, v) mz_write_le32((mz_uint8 *)(p), (mz_uint32)(v))
  1841. #define MZ_WRITE_LE64(p, v) mz_write_le64((mz_uint8 *)(p), (mz_uint64)(v))
  1842. static size_t mz_zip_heap_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
  1843. {
  1844. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  1845. mz_zip_internal_state *pState = pZip->m_pState;
  1846. mz_uint64 new_size = MZ_MAX(file_ofs + n, pState->m_mem_size);
  1847. if (!n)
  1848. return 0;
  1849. // An allocation this big is likely to just fail on 32-bit systems, so don't even go there.
  1850. if ((sizeof(size_t) == sizeof(mz_uint32)) && (new_size > 0x7FFFFFFF))
  1851. {
  1852. mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  1853. return 0;
  1854. }
  1855. if (new_size > pState->m_mem_capacity)
  1856. {
  1857. void *pNew_block;
  1858. size_t new_capacity = MZ_MAX(64, pState->m_mem_capacity);
  1859. while (new_capacity < new_size)
  1860. new_capacity *= 2;
  1861. if (NULL == (pNew_block = pZip->m_pRealloc(pZip->m_pAlloc_opaque, pState->m_pMem, 1, new_capacity)))
  1862. {
  1863. mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1864. return 0;
  1865. }
  1866. pState->m_pMem = pNew_block;
  1867. pState->m_mem_capacity = new_capacity;
  1868. }
  1869. memcpy((mz_uint8 *)pState->m_pMem + file_ofs, pBuf, n);
  1870. pState->m_mem_size = (size_t)new_size;
  1871. return n;
  1872. }
  1873. static mz_bool mz_zip_writer_end_internal(mz_zip_archive *pZip, mz_bool set_last_error)
  1874. {
  1875. mz_zip_internal_state *pState;
  1876. mz_bool status = MZ_TRUE;
  1877. if ((!pZip) || (!pZip->m_pState) || (!pZip->m_pAlloc) || (!pZip->m_pFree) || ((pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) && (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED)))
  1878. {
  1879. if (set_last_error)
  1880. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1881. return MZ_FALSE;
  1882. }
  1883. pState = pZip->m_pState;
  1884. pZip->m_pState = NULL;
  1885. mz_zip_array_clear(pZip, &pState->m_central_dir);
  1886. mz_zip_array_clear(pZip, &pState->m_central_dir_offsets);
  1887. mz_zip_array_clear(pZip, &pState->m_sorted_central_dir_offsets);
  1888. #ifndef MINIZ_NO_STDIO
  1889. if (pState->m_pFile)
  1890. {
  1891. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)
  1892. {
  1893. if (MZ_FCLOSE(pState->m_pFile) == EOF)
  1894. {
  1895. if (set_last_error)
  1896. mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
  1897. status = MZ_FALSE;
  1898. }
  1899. }
  1900. pState->m_pFile = NULL;
  1901. }
  1902. #endif // #ifndef MINIZ_NO_STDIO
  1903. if ((pZip->m_pWrite == mz_zip_heap_write_func) && (pState->m_pMem))
  1904. {
  1905. pZip->m_pFree(pZip->m_pAlloc_opaque, pState->m_pMem);
  1906. pState->m_pMem = NULL;
  1907. }
  1908. pZip->m_pFree(pZip->m_pAlloc_opaque, pState);
  1909. pZip->m_zip_mode = MZ_ZIP_MODE_INVALID;
  1910. return status;
  1911. }
  1912. mz_bool mz_zip_writer_init(mz_zip_archive *pZip, mz_uint64 existing_size, mz_uint flags)
  1913. {
  1914. mz_bool zip64 = (flags & MZ_ZIP_FLAG_WRITE_ZIP64) != 0;
  1915. if ((!pZip) || (pZip->m_pState) || (!pZip->m_pWrite) || (pZip->m_zip_mode != MZ_ZIP_MODE_INVALID))
  1916. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1917. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  1918. {
  1919. if (!pZip->m_pRead)
  1920. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1921. }
  1922. if (pZip->m_file_offset_alignment)
  1923. {
  1924. // Ensure user specified file offset alignment is a power of 2.
  1925. if (pZip->m_file_offset_alignment & (pZip->m_file_offset_alignment - 1))
  1926. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  1927. }
  1928. if (!pZip->m_pAlloc)
  1929. pZip->m_pAlloc = miniz_def_alloc_func;
  1930. if (!pZip->m_pFree)
  1931. pZip->m_pFree = miniz_def_free_func;
  1932. if (!pZip->m_pRealloc)
  1933. pZip->m_pRealloc = miniz_def_realloc_func;
  1934. pZip->m_archive_size = existing_size;
  1935. pZip->m_central_directory_file_ofs = 0;
  1936. pZip->m_total_files = 0;
  1937. if (NULL == (pZip->m_pState = (mz_zip_internal_state *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(mz_zip_internal_state))))
  1938. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1939. memset(pZip->m_pState, 0, sizeof(mz_zip_internal_state));
  1940. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir, sizeof(mz_uint8));
  1941. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_central_dir_offsets, sizeof(mz_uint32));
  1942. MZ_ZIP_ARRAY_SET_ELEMENT_SIZE(&pZip->m_pState->m_sorted_central_dir_offsets, sizeof(mz_uint32));
  1943. pZip->m_pState->m_zip64 = zip64;
  1944. pZip->m_pState->m_zip64_has_extended_info_fields = zip64;
  1945. pZip->m_zip_type = MZ_ZIP_TYPE_USER;
  1946. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
  1947. return MZ_TRUE;
  1948. }
  1949. mz_bool mz_zip_writer_init_heap(mz_zip_archive *pZip, size_t size_to_reserve_at_beginning, size_t initial_allocation_size, mz_uint flags)
  1950. {
  1951. pZip->m_pWrite = mz_zip_heap_write_func;
  1952. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  1953. pZip->m_pRead = mz_zip_mem_read_func;
  1954. pZip->m_pIO_opaque = pZip;
  1955. if (!mz_zip_writer_init(pZip, size_to_reserve_at_beginning, flags))
  1956. return MZ_FALSE;
  1957. pZip->m_zip_type = MZ_ZIP_TYPE_HEAP;
  1958. if (0 != (initial_allocation_size = MZ_MAX(initial_allocation_size, size_to_reserve_at_beginning)))
  1959. {
  1960. if (NULL == (pZip->m_pState->m_pMem = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, initial_allocation_size)))
  1961. {
  1962. mz_zip_writer_end_internal(pZip, MZ_FALSE);
  1963. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  1964. }
  1965. pZip->m_pState->m_mem_capacity = initial_allocation_size;
  1966. }
  1967. return MZ_TRUE;
  1968. }
  1969. #ifndef MINIZ_NO_STDIO
  1970. static size_t mz_zip_file_write_func(void *pOpaque, mz_uint64 file_ofs, const void *pBuf, size_t n)
  1971. {
  1972. mz_zip_archive *pZip = (mz_zip_archive *)pOpaque;
  1973. mz_int64 cur_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
  1974. file_ofs += pZip->m_pState->m_file_archive_start_ofs;
  1975. if (((mz_int64)file_ofs < 0) || (((cur_ofs != (mz_int64)file_ofs)) && (MZ_FSEEK64(pZip->m_pState->m_pFile, (mz_int64)file_ofs, SEEK_SET))))
  1976. {
  1977. mz_zip_set_error(pZip, MZ_ZIP_FILE_SEEK_FAILED);
  1978. return 0;
  1979. }
  1980. return MZ_FWRITE(pBuf, 1, n, pZip->m_pState->m_pFile);
  1981. }
  1982. mz_bool mz_zip_writer_init_file(mz_zip_archive *pZip, const char *pFilename, mz_uint64 size_to_reserve_at_beginning, mz_uint flags)
  1983. {
  1984. MZ_FILE *pFile;
  1985. pZip->m_pWrite = mz_zip_file_write_func;
  1986. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  1987. pZip->m_pRead = mz_zip_file_read_func;
  1988. pZip->m_pIO_opaque = pZip;
  1989. if (!mz_zip_writer_init(pZip, size_to_reserve_at_beginning, flags))
  1990. return MZ_FALSE;
  1991. if (NULL == (pFile = MZ_FOPEN(pFilename, (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING) ? "w+b" : "wb")))
  1992. {
  1993. mz_zip_writer_end(pZip);
  1994. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  1995. }
  1996. pZip->m_pState->m_pFile = pFile;
  1997. pZip->m_zip_type = MZ_ZIP_TYPE_FILE;
  1998. if (size_to_reserve_at_beginning)
  1999. {
  2000. mz_uint64 cur_ofs = 0;
  2001. char buf[4096];
  2002. MZ_CLEAR_OBJ(buf);
  2003. do
  2004. {
  2005. size_t n = (size_t)MZ_MIN(sizeof(buf), size_to_reserve_at_beginning);
  2006. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_ofs, buf, n) != n)
  2007. {
  2008. mz_zip_writer_end(pZip);
  2009. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2010. }
  2011. cur_ofs += n;
  2012. size_to_reserve_at_beginning -= n;
  2013. } while (size_to_reserve_at_beginning);
  2014. }
  2015. return MZ_TRUE;
  2016. }
  2017. mz_bool mz_zip_writer_init_cfile(mz_zip_archive *pZip, MZ_FILE *pFile, mz_uint flags)
  2018. {
  2019. pZip->m_pWrite = mz_zip_file_write_func;
  2020. if (flags & MZ_ZIP_FLAG_WRITE_ALLOW_READING)
  2021. pZip->m_pRead = mz_zip_file_read_func;
  2022. pZip->m_pIO_opaque = pZip;
  2023. if (!mz_zip_writer_init(pZip, 0, flags))
  2024. return MZ_FALSE;
  2025. pZip->m_pState->m_pFile = pFile;
  2026. pZip->m_pState->m_file_archive_start_ofs = MZ_FTELL64(pZip->m_pState->m_pFile);
  2027. pZip->m_zip_type = MZ_ZIP_TYPE_CFILE;
  2028. return MZ_TRUE;
  2029. }
  2030. #endif // #ifndef MINIZ_NO_STDIO
  2031. mz_bool mz_zip_writer_init_from_reader(mz_zip_archive *pZip, const char *pFilename, mz_uint flags)
  2032. {
  2033. mz_zip_internal_state *pState;
  2034. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_READING))
  2035. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2036. if (flags & MZ_ZIP_FLAG_WRITE_ZIP64)
  2037. {
  2038. // We don't support converting a non-zip64 file to zip64 - this seems like more trouble than it's worth. (What about the existing 32-bit data descriptors that could follow the compressed data?)
  2039. if (!pZip->m_pState->m_zip64)
  2040. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2041. }
  2042. // No sense in trying to write to an archive that's already at the support max size
  2043. if (pZip->m_pState->m_zip64)
  2044. {
  2045. if (pZip->m_total_files == MZ_UINT32_MAX)
  2046. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2047. }
  2048. else
  2049. {
  2050. if (pZip->m_total_files == MZ_UINT16_MAX)
  2051. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2052. if ((pZip->m_archive_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + MZ_ZIP_LOCAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX)
  2053. return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  2054. }
  2055. pState = pZip->m_pState;
  2056. if (pState->m_pFile)
  2057. {
  2058. #ifdef MINIZ_NO_STDIO
  2059. (void)pFilename;
  2060. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2061. #else
  2062. if (pZip->m_pIO_opaque != pZip)
  2063. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2064. if (pZip->m_zip_type == MZ_ZIP_TYPE_FILE)
  2065. {
  2066. if (!pFilename)
  2067. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2068. // Archive is being read from stdio and was originally opened only for reading. Try to reopen as writable.
  2069. if (NULL == (pState->m_pFile = MZ_FREOPEN(pFilename, "r+b", pState->m_pFile)))
  2070. {
  2071. // The mz_zip_archive is now in a bogus state because pState->m_pFile is NULL, so just close it.
  2072. mz_zip_reader_end_internal(pZip, MZ_FALSE);
  2073. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  2074. }
  2075. }
  2076. pZip->m_pWrite = mz_zip_file_write_func;
  2077. #endif // #ifdef MINIZ_NO_STDIO
  2078. }
  2079. else if (pState->m_pMem)
  2080. {
  2081. // Archive lives in a memory block. Assume it's from the heap that we can resize using the realloc callback.
  2082. if (pZip->m_pIO_opaque != pZip)
  2083. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2084. pState->m_mem_capacity = pState->m_mem_size;
  2085. pZip->m_pWrite = mz_zip_heap_write_func;
  2086. }
  2087. // Archive is being read via a user provided read function - make sure the user has specified a write function too.
  2088. else if (!pZip->m_pWrite)
  2089. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2090. // Start writing new files at the archive's current central directory location.
  2091. // TODO: We could add a flag that lets the user start writing immediately AFTER the existing central dir - this would be safer.
  2092. pZip->m_archive_size = pZip->m_central_directory_file_ofs;
  2093. pZip->m_central_directory_file_ofs = 0;
  2094. // Clear the sorted central dir offsets, they aren't useful or maintained now.
  2095. // Even though we're now in write mode, files can still be extracted and verified, but file locates will be slow.
  2096. // TODO: We could easily maintain the sorted central directory offsets.
  2097. mz_zip_array_clear(pZip, &pZip->m_pState->m_sorted_central_dir_offsets);
  2098. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING;
  2099. return MZ_TRUE;
  2100. }
  2101. // TODO: pArchive_name is a terrible name here!
  2102. mz_bool mz_zip_writer_add_mem(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, mz_uint level_and_flags)
  2103. {
  2104. return mz_zip_writer_add_mem_ex(pZip, pArchive_name, pBuf, buf_size, NULL, 0, level_and_flags, 0, 0);
  2105. }
  2106. typedef struct
  2107. {
  2108. mz_zip_archive *m_pZip;
  2109. mz_uint64 m_cur_archive_file_ofs;
  2110. mz_uint64 m_comp_size;
  2111. } mz_zip_writer_add_state;
  2112. static mz_bool mz_zip_writer_add_put_buf_callback(const void *pBuf, int len, void *pUser)
  2113. {
  2114. mz_zip_writer_add_state *pState = (mz_zip_writer_add_state *)pUser;
  2115. if ((int)pState->m_pZip->m_pWrite(pState->m_pZip->m_pIO_opaque, pState->m_cur_archive_file_ofs, pBuf, len) != len)
  2116. return MZ_FALSE;
  2117. pState->m_cur_archive_file_ofs += len;
  2118. pState->m_comp_size += len;
  2119. return MZ_TRUE;
  2120. }
  2121. #define MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 2)
  2122. #define MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE (sizeof(mz_uint16) * 2 + sizeof(mz_uint64) * 3)
  2123. static mz_uint32 mz_zip_writer_create_zip64_extra_data(mz_uint8 *pBuf, mz_uint64 *pUncomp_size, mz_uint64 *pComp_size, mz_uint64 *pLocal_header_ofs)
  2124. {
  2125. mz_uint8 *pDst = pBuf;
  2126. MZ_WRITE_LE16(pDst + 0, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);
  2127. MZ_WRITE_LE16(pDst + 2, 0);
  2128. pDst += sizeof(mz_uint16) * 2;
  2129. mz_uint32 field_size = 0;
  2130. if (pUncomp_size)
  2131. {
  2132. MZ_WRITE_LE64(pDst, *pUncomp_size);
  2133. pDst += sizeof(mz_uint64);
  2134. field_size += sizeof(mz_uint64);
  2135. }
  2136. if (pComp_size)
  2137. {
  2138. MZ_WRITE_LE64(pDst, *pComp_size);
  2139. pDst += sizeof(mz_uint64);
  2140. field_size += sizeof(mz_uint64);
  2141. }
  2142. if (pLocal_header_ofs)
  2143. {
  2144. MZ_WRITE_LE64(pDst, *pLocal_header_ofs);
  2145. pDst += sizeof(mz_uint64);
  2146. field_size += sizeof(mz_uint64);
  2147. }
  2148. MZ_WRITE_LE16(pBuf + 2, field_size);
  2149. return (mz_uint32)(pDst - pBuf);
  2150. }
  2151. static mz_bool mz_zip_writer_create_local_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst, mz_uint16 filename_size, mz_uint16 extra_size, mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32, mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date)
  2152. {
  2153. (void)pZip;
  2154. memset(pDst, 0, MZ_ZIP_LOCAL_DIR_HEADER_SIZE);
  2155. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_SIG_OFS, MZ_ZIP_LOCAL_DIR_HEADER_SIG);
  2156. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_VERSION_NEEDED_OFS, method ? 20 : 0);
  2157. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_BIT_FLAG_OFS, bit_flags);
  2158. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_METHOD_OFS, method);
  2159. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_TIME_OFS, dos_time);
  2160. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILE_DATE_OFS, dos_date);
  2161. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_CRC32_OFS, uncomp_crc32);
  2162. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));
  2163. MZ_WRITE_LE32(pDst + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));
  2164. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_FILENAME_LEN_OFS, filename_size);
  2165. MZ_WRITE_LE16(pDst + MZ_ZIP_LDH_EXTRA_LEN_OFS, extra_size);
  2166. return MZ_TRUE;
  2167. }
  2168. static mz_bool mz_zip_writer_create_central_dir_header(mz_zip_archive *pZip, mz_uint8 *pDst,
  2169. mz_uint16 filename_size, mz_uint16 extra_size, mz_uint16 comment_size,
  2170. mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,
  2171. mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
  2172. mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
  2173. {
  2174. (void)pZip;
  2175. memset(pDst, 0, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
  2176. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_SIG_OFS, MZ_ZIP_CENTRAL_DIR_HEADER_SIG);
  2177. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_VERSION_NEEDED_OFS, method ? 20 : 0);
  2178. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_BIT_FLAG_OFS, bit_flags);
  2179. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_METHOD_OFS, method);
  2180. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_TIME_OFS, dos_time);
  2181. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILE_DATE_OFS, dos_date);
  2182. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_CRC32_OFS, uncomp_crc32);
  2183. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_MIN(comp_size, MZ_UINT32_MAX));
  2184. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_MIN(uncomp_size, MZ_UINT32_MAX));
  2185. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_FILENAME_LEN_OFS, filename_size);
  2186. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_EXTRA_LEN_OFS, extra_size);
  2187. MZ_WRITE_LE16(pDst + MZ_ZIP_CDH_COMMENT_LEN_OFS, comment_size);
  2188. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_EXTERNAL_ATTR_OFS, ext_attributes);
  2189. MZ_WRITE_LE32(pDst + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_MIN(local_header_ofs, MZ_UINT32_MAX));
  2190. return MZ_TRUE;
  2191. }
  2192. static mz_bool mz_zip_writer_add_to_central_dir(mz_zip_archive *pZip, const char *pFilename, mz_uint16 filename_size,
  2193. const void *pExtra, mz_uint16 extra_size, const void *pComment, mz_uint16 comment_size,
  2194. mz_uint64 uncomp_size, mz_uint64 comp_size, mz_uint32 uncomp_crc32,
  2195. mz_uint16 method, mz_uint16 bit_flags, mz_uint16 dos_time, mz_uint16 dos_date,
  2196. mz_uint64 local_header_ofs, mz_uint32 ext_attributes)
  2197. {
  2198. mz_zip_internal_state *pState = pZip->m_pState;
  2199. mz_uint32 central_dir_ofs = (mz_uint32)pState->m_central_dir.m_size;
  2200. size_t orig_central_dir_size = pState->m_central_dir.m_size;
  2201. mz_uint8 central_dir_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
  2202. if (!pZip->m_pState->m_zip64)
  2203. {
  2204. if (local_header_ofs > 0xFFFFFFFF)
  2205. return mz_zip_set_error(pZip, MZ_ZIP_FILE_TOO_LARGE);
  2206. }
  2207. // miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet
  2208. if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + filename_size + extra_size + comment_size) >= MZ_UINT32_MAX)
  2209. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  2210. if (!mz_zip_writer_create_central_dir_header(pZip, central_dir_header, filename_size, extra_size, comment_size, uncomp_size, comp_size, uncomp_crc32, method, bit_flags, dos_time, dos_date, local_header_ofs, ext_attributes))
  2211. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  2212. if ((!mz_zip_array_push_back(pZip, &pState->m_central_dir, central_dir_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE)) ||
  2213. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pFilename, filename_size)) ||
  2214. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pExtra, extra_size)) ||
  2215. (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pComment, comment_size)) ||
  2216. (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &central_dir_ofs, 1)))
  2217. {
  2218. // Try to resize the central directory array back into its original state.
  2219. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2220. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2221. }
  2222. return MZ_TRUE;
  2223. }
  2224. static mz_bool mz_zip_writer_validate_archive_name(const char *pArchive_name)
  2225. {
  2226. // Basic ZIP archive filename validity checks: Valid filenames cannot start with a forward slash, cannot contain a drive letter, and cannot use DOS-style backward slashes.
  2227. if (*pArchive_name == '/')
  2228. return MZ_FALSE;
  2229. while (*pArchive_name)
  2230. {
  2231. if ((*pArchive_name == '\\') || (*pArchive_name == ':'))
  2232. return MZ_FALSE;
  2233. pArchive_name++;
  2234. }
  2235. return MZ_TRUE;
  2236. }
  2237. static mz_uint mz_zip_writer_compute_padding_needed_for_file_alignment(mz_zip_archive *pZip)
  2238. {
  2239. mz_uint32 n;
  2240. if (!pZip->m_file_offset_alignment)
  2241. return 0;
  2242. n = (mz_uint32)(pZip->m_archive_size & (pZip->m_file_offset_alignment - 1));
  2243. return (mz_uint)((pZip->m_file_offset_alignment - n) & (pZip->m_file_offset_alignment - 1));
  2244. }
  2245. static mz_bool mz_zip_writer_write_zeros(mz_zip_archive *pZip, mz_uint64 cur_file_ofs, mz_uint32 n)
  2246. {
  2247. char buf[4096];
  2248. memset(buf, 0, MZ_MIN(sizeof(buf), n));
  2249. while (n)
  2250. {
  2251. mz_uint32 s = MZ_MIN(sizeof(buf), n);
  2252. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_file_ofs, buf, s) != s)
  2253. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2254. cur_file_ofs += s;
  2255. n -= s;
  2256. }
  2257. return MZ_TRUE;
  2258. }
  2259. mz_bool mz_zip_writer_add_mem_ex(mz_zip_archive *pZip, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_uint64 uncomp_size, mz_uint32 uncomp_crc32)
  2260. {
  2261. mz_uint16 method = 0, dos_time = 0, dos_date = 0;
  2262. mz_uint level, ext_attributes = 0, num_alignment_padding_bytes;
  2263. mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, extra_data_file_ofs = 0, comp_size = 0;
  2264. size_t archive_name_size;
  2265. mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
  2266. tdefl_compressor *pComp = NULL;
  2267. mz_bool store_data_uncompressed;
  2268. mz_zip_internal_state *pState;
  2269. mz_uint8 *pExtra_data = NULL;
  2270. mz_uint32 extra_size = 0;
  2271. mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
  2272. if ((int)level_and_flags < 0)
  2273. level_and_flags = MZ_DEFAULT_LEVEL;
  2274. level = level_and_flags & 0xF;
  2275. store_data_uncompressed = ((!level) || (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA));
  2276. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || ((buf_size) && (!pBuf)) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))
  2277. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2278. pState = pZip->m_pState;
  2279. if (pState->m_zip64)
  2280. {
  2281. if (pZip->m_total_files == MZ_UINT32_MAX)
  2282. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2283. }
  2284. else
  2285. {
  2286. if (pZip->m_total_files == MZ_UINT16_MAX)
  2287. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2288. if ((buf_size > 0xFFFFFFFF) || (uncomp_size > 0xFFFFFFFF))
  2289. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2290. }
  2291. if ((!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)) && (uncomp_size))
  2292. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2293. if (!mz_zip_writer_validate_archive_name(pArchive_name))
  2294. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  2295. #ifndef MINIZ_NO_TIME
  2296. {
  2297. MZ_TIME_T cur_time;
  2298. time(&cur_time);
  2299. mz_zip_time_t_to_dos_time(cur_time, &dos_time, &dos_date);
  2300. }
  2301. #endif // #ifndef MINIZ_NO_TIME
  2302. archive_name_size = strlen(pArchive_name);
  2303. if (archive_name_size > MZ_UINT16_MAX)
  2304. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  2305. num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
  2306. // miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet
  2307. if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)
  2308. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  2309. if (!pState->m_zip64)
  2310. {
  2311. // Bail early if the archive would obviously become too large
  2312. if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)
  2313. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2314. }
  2315. if ((archive_name_size) && (pArchive_name[archive_name_size - 1] == '/'))
  2316. {
  2317. // Set DOS Subdirectory attribute bit.
  2318. ext_attributes |= MZ_ZIP_DOS_DIR_ATTRIBUTE_BITFLAG;
  2319. // Subdirectories cannot contain data.
  2320. if ((buf_size) || (uncomp_size))
  2321. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2322. }
  2323. // Try to do any allocations before writing to the archive, so if an allocation fails the file remains unmodified. (A good idea if we're doing an in-place modification.)
  2324. if ((!mz_zip_array_ensure_room(pZip, &pState->m_central_dir, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + (pState->m_zip64 ? MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE : 0))) || (!mz_zip_array_ensure_room(pZip, &pState->m_central_dir_offsets, 1)))
  2325. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2326. if ((!store_data_uncompressed) && (buf_size))
  2327. {
  2328. if (NULL == (pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor))))
  2329. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2330. }
  2331. if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes + sizeof(local_dir_header)))
  2332. {
  2333. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2334. return MZ_FALSE;
  2335. }
  2336. local_dir_header_ofs += num_alignment_padding_bytes;
  2337. if (pZip->m_file_offset_alignment)
  2338. {
  2339. MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
  2340. }
  2341. cur_archive_file_ofs += num_alignment_padding_bytes + sizeof(local_dir_header);
  2342. MZ_CLEAR_OBJ(local_dir_header);
  2343. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
  2344. {
  2345. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2346. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2347. }
  2348. cur_archive_file_ofs += archive_name_size;
  2349. extra_data_file_ofs = cur_archive_file_ofs;
  2350. if (pState->m_zip64)
  2351. {
  2352. // Reserve space for an extra data field for zip64
  2353. cur_archive_file_ofs += MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE;
  2354. }
  2355. if (!(level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA))
  2356. {
  2357. uncomp_crc32 = (mz_uint32)mz_crc32(MZ_CRC32_INIT, (const mz_uint8 *)pBuf, buf_size);
  2358. uncomp_size = buf_size;
  2359. if (uncomp_size <= 3)
  2360. {
  2361. level = 0;
  2362. store_data_uncompressed = MZ_TRUE;
  2363. }
  2364. }
  2365. if (store_data_uncompressed)
  2366. {
  2367. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pBuf, buf_size) != buf_size)
  2368. {
  2369. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2370. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2371. }
  2372. cur_archive_file_ofs += buf_size;
  2373. comp_size = buf_size;
  2374. if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
  2375. method = MZ_DEFLATED;
  2376. }
  2377. else if (buf_size)
  2378. {
  2379. mz_zip_writer_add_state state;
  2380. state.m_pZip = pZip;
  2381. state.m_cur_archive_file_ofs = cur_archive_file_ofs;
  2382. state.m_comp_size = 0;
  2383. if ((tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY) ||
  2384. (tdefl_compress_buffer(pComp, pBuf, buf_size, TDEFL_FINISH) != TDEFL_STATUS_DONE))
  2385. {
  2386. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2387. return mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
  2388. }
  2389. comp_size = state.m_comp_size;
  2390. cur_archive_file_ofs = state.m_cur_archive_file_ofs;
  2391. method = MZ_DEFLATED;
  2392. }
  2393. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2394. pComp = NULL;
  2395. if (pState->m_zip64)
  2396. {
  2397. // Write the local header+zip64 extra data
  2398. mz_uint32 local_dir_entry_extra_data_size = mz_zip_writer_create_zip64_extra_data(extra_data, &uncomp_size, &comp_size, NULL);
  2399. MZ_ASSERT(local_dir_entry_extra_data_size <= sizeof(extra_data));
  2400. if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, local_dir_entry_extra_data_size, MZ_UINT32_MAX, MZ_UINT32_MAX, uncomp_crc32, method, 0, dos_time, dos_date))
  2401. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  2402. if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
  2403. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2404. if (pZip->m_pWrite(pZip->m_pIO_opaque, extra_data_file_ofs, extra_data, local_dir_entry_extra_data_size) != local_dir_entry_extra_data_size)
  2405. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2406. // See if we need to create zip64 extra data
  2407. if (MZ_MAX(local_dir_header_ofs, MZ_MAX(comp_size, uncomp_size)) >= MZ_UINT32_MAX)
  2408. {
  2409. pExtra_data = extra_data;
  2410. extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, (comp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
  2411. }
  2412. }
  2413. else
  2414. {
  2415. if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
  2416. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2417. if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date))
  2418. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  2419. if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
  2420. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2421. }
  2422. if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, extra_size, pComment, comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes))
  2423. return MZ_FALSE;
  2424. pZip->m_total_files++;
  2425. pZip->m_archive_size = cur_archive_file_ofs;
  2426. return MZ_TRUE;
  2427. }
  2428. #ifndef MINIZ_NO_STDIO
  2429. mz_bool mz_zip_writer_add_cfile(mz_zip_archive *pZip, const char *pArchive_name, MZ_FILE *pSrc_file, mz_uint64 size_to_add, const MZ_TIME_T *pFile_time, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags)
  2430. {
  2431. mz_uint uncomp_crc32 = MZ_CRC32_INIT, level, num_alignment_padding_bytes;
  2432. mz_uint16 method = 0, dos_time = 0, dos_date = 0, ext_attributes = 0;
  2433. mz_uint64 local_dir_header_ofs = pZip->m_archive_size, cur_archive_file_ofs = pZip->m_archive_size, extra_data_file_ofs = 0, uncomp_size = size_to_add, comp_size = 0;
  2434. size_t archive_name_size;
  2435. mz_uint8 local_dir_header[MZ_ZIP_LOCAL_DIR_HEADER_SIZE];
  2436. mz_uint8 *pExtra_data = NULL;
  2437. mz_uint32 extra_size = 0;
  2438. mz_uint8 extra_data[MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE];
  2439. mz_zip_internal_state *pState;
  2440. if ((int)level_and_flags < 0)
  2441. level_and_flags = MZ_DEFAULT_LEVEL;
  2442. level = level_and_flags & 0xF;
  2443. // Sanity checks
  2444. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pArchive_name) || ((comment_size) && (!pComment)) || (level > MZ_UBER_COMPRESSION))
  2445. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2446. pState = pZip->m_pState;
  2447. if ((!pState->m_zip64) && (uncomp_size > MZ_UINT32_MAX))
  2448. {
  2449. // Source file is too large for non-zip64
  2450. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2451. }
  2452. // We could support this, but why?
  2453. if (level_and_flags & MZ_ZIP_FLAG_COMPRESSED_DATA)
  2454. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2455. if (!mz_zip_writer_validate_archive_name(pArchive_name))
  2456. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  2457. if (pState->m_zip64)
  2458. {
  2459. if (pZip->m_total_files == MZ_UINT32_MAX)
  2460. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2461. }
  2462. else
  2463. {
  2464. if (pZip->m_total_files == MZ_UINT16_MAX)
  2465. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2466. }
  2467. archive_name_size = strlen(pArchive_name);
  2468. if (archive_name_size > MZ_UINT16_MAX)
  2469. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_FILENAME);
  2470. num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
  2471. // miniz doesn't support central dirs >= MZ_UINT32_MAX bytes yet
  2472. if (((mz_uint64)pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP64_MAX_CENTRAL_EXTRA_FIELD_SIZE + comment_size) >= MZ_UINT32_MAX)
  2473. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  2474. if (!pState->m_zip64)
  2475. {
  2476. // Bail early if the archive would obviously become too large
  2477. if ((pZip->m_archive_size + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + archive_name_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + archive_name_size + comment_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > 0xFFFFFFFF)
  2478. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2479. }
  2480. #ifndef MINIZ_NO_TIME
  2481. if (pFile_time)
  2482. {
  2483. mz_zip_time_t_to_dos_time(*pFile_time, &dos_time, &dos_date);
  2484. }
  2485. #endif
  2486. if (uncomp_size <= 3)
  2487. level = 0;
  2488. if (!mz_zip_writer_write_zeros(pZip, cur_archive_file_ofs, num_alignment_padding_bytes + sizeof(local_dir_header)))
  2489. {
  2490. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2491. }
  2492. local_dir_header_ofs += num_alignment_padding_bytes;
  2493. if (pZip->m_file_offset_alignment)
  2494. {
  2495. MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
  2496. }
  2497. cur_archive_file_ofs += num_alignment_padding_bytes + sizeof(local_dir_header);
  2498. MZ_CLEAR_OBJ(local_dir_header);
  2499. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pArchive_name, archive_name_size) != archive_name_size)
  2500. {
  2501. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2502. }
  2503. cur_archive_file_ofs += archive_name_size;
  2504. extra_data_file_ofs = cur_archive_file_ofs;
  2505. if (pState->m_zip64)
  2506. {
  2507. // Reserve space for an extra data field for zip64
  2508. cur_archive_file_ofs += MZ_ZIP64_MAX_LOCAL_EXTRA_FIELD_SIZE;
  2509. }
  2510. if (uncomp_size)
  2511. {
  2512. mz_uint64 uncomp_remaining = uncomp_size;
  2513. void *pRead_buf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, MZ_ZIP_MAX_IO_BUF_SIZE);
  2514. if (!pRead_buf)
  2515. {
  2516. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2517. }
  2518. if (!level)
  2519. {
  2520. while (uncomp_remaining)
  2521. {
  2522. mz_uint n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, uncomp_remaining);
  2523. if ((MZ_FREAD(pRead_buf, 1, n, pSrc_file) != n) || (pZip->m_pWrite(pZip->m_pIO_opaque, cur_archive_file_ofs, pRead_buf, n) != n))
  2524. {
  2525. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  2526. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2527. }
  2528. uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, n);
  2529. uncomp_remaining -= n;
  2530. cur_archive_file_ofs += n;
  2531. }
  2532. comp_size = uncomp_size;
  2533. }
  2534. else
  2535. {
  2536. mz_bool result = MZ_FALSE;
  2537. mz_zip_writer_add_state state;
  2538. tdefl_compressor *pComp = (tdefl_compressor *)pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, sizeof(tdefl_compressor));
  2539. if (!pComp)
  2540. {
  2541. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  2542. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2543. }
  2544. state.m_pZip = pZip;
  2545. state.m_cur_archive_file_ofs = cur_archive_file_ofs;
  2546. state.m_comp_size = 0;
  2547. if (tdefl_init(pComp, mz_zip_writer_add_put_buf_callback, &state, tdefl_create_comp_flags_from_zip_params(level, -15, MZ_DEFAULT_STRATEGY)) != TDEFL_STATUS_OKAY)
  2548. {
  2549. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2550. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  2551. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  2552. }
  2553. for (;;)
  2554. {
  2555. size_t in_buf_size = (mz_uint32)MZ_MIN(uncomp_remaining, (mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE);
  2556. tdefl_status status;
  2557. if (MZ_FREAD(pRead_buf, 1, in_buf_size, pSrc_file) != in_buf_size)
  2558. {
  2559. mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2560. break;
  2561. }
  2562. uncomp_crc32 = (mz_uint32)mz_crc32(uncomp_crc32, (const mz_uint8 *)pRead_buf, in_buf_size);
  2563. uncomp_remaining -= in_buf_size;
  2564. status = tdefl_compress_buffer(pComp, pRead_buf, in_buf_size, uncomp_remaining ? TDEFL_NO_FLUSH : TDEFL_FINISH);
  2565. if (status == TDEFL_STATUS_DONE)
  2566. {
  2567. result = MZ_TRUE;
  2568. break;
  2569. }
  2570. else if (status != TDEFL_STATUS_OKAY)
  2571. {
  2572. mz_zip_set_error(pZip, MZ_ZIP_COMPRESSION_FAILED);
  2573. break;
  2574. }
  2575. }
  2576. pZip->m_pFree(pZip->m_pAlloc_opaque, pComp);
  2577. if (!result)
  2578. {
  2579. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  2580. return MZ_FALSE;
  2581. }
  2582. comp_size = state.m_comp_size;
  2583. cur_archive_file_ofs = state.m_cur_archive_file_ofs;
  2584. method = MZ_DEFLATED;
  2585. }
  2586. pZip->m_pFree(pZip->m_pAlloc_opaque, pRead_buf);
  2587. }
  2588. if (pState->m_zip64)
  2589. {
  2590. // Write the local header+zip64 extra data
  2591. mz_uint32 local_dir_entry_extra_data_size = mz_zip_writer_create_zip64_extra_data(extra_data, &uncomp_size, &comp_size, NULL);
  2592. MZ_ASSERT(local_dir_entry_extra_data_size <= sizeof(extra_data));
  2593. if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, local_dir_entry_extra_data_size, MZ_UINT32_MAX, MZ_UINT32_MAX, uncomp_crc32, method, 0, dos_time, dos_date))
  2594. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  2595. if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
  2596. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2597. if (pZip->m_pWrite(pZip->m_pIO_opaque, extra_data_file_ofs, extra_data, local_dir_entry_extra_data_size) != local_dir_entry_extra_data_size)
  2598. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2599. // See if we need to create zip64 extra data
  2600. if (MZ_MAX(local_dir_header_ofs, MZ_MAX(comp_size, uncomp_size)) >= MZ_UINT32_MAX)
  2601. {
  2602. pExtra_data = extra_data;
  2603. extra_size = mz_zip_writer_create_zip64_extra_data(extra_data, (uncomp_size >= MZ_UINT32_MAX) ? &uncomp_size : NULL, (comp_size >= MZ_UINT32_MAX) ? &comp_size : NULL, (local_dir_header_ofs >= MZ_UINT32_MAX) ? &local_dir_header_ofs : NULL);
  2604. }
  2605. }
  2606. else
  2607. {
  2608. if ((comp_size > MZ_UINT32_MAX) || (cur_archive_file_ofs > MZ_UINT32_MAX))
  2609. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2610. if (!mz_zip_writer_create_local_dir_header(pZip, local_dir_header, (mz_uint16)archive_name_size, 0, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date))
  2611. return mz_zip_set_error(pZip, MZ_ZIP_INTERNAL_ERROR);
  2612. if (pZip->m_pWrite(pZip->m_pIO_opaque, local_dir_header_ofs, local_dir_header, sizeof(local_dir_header)) != sizeof(local_dir_header))
  2613. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2614. }
  2615. if (!mz_zip_writer_add_to_central_dir(pZip, pArchive_name, (mz_uint16)archive_name_size, pExtra_data, extra_size, pComment, comment_size, uncomp_size, comp_size, uncomp_crc32, method, 0, dos_time, dos_date, local_dir_header_ofs, ext_attributes))
  2616. return MZ_FALSE;
  2617. pZip->m_total_files++;
  2618. pZip->m_archive_size = cur_archive_file_ofs;
  2619. return MZ_TRUE;
  2620. }
  2621. mz_bool mz_zip_writer_add_file(mz_zip_archive *pZip, const char *pArchive_name, const char *pSrc_filename, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags)
  2622. {
  2623. MZ_FILE *pSrc_file = NULL;
  2624. mz_uint64 uncomp_size = 0;
  2625. MZ_TIME_T file_modified_time;
  2626. MZ_TIME_T *pFile_modified_time = NULL;
  2627. memset(&file_modified_time, 0, sizeof(file_modified_time));
  2628. #if !defined(MINIZ_NO_TIME) && !defined(MINIZ_NO_STDIO)
  2629. pFile_modified_time = &file_modified_time;
  2630. if (!mz_zip_get_file_modified_time(pSrc_filename, &file_modified_time))
  2631. return mz_zip_set_error(pZip, MZ_ZIP_FILE_STAT_FAILED);
  2632. #endif
  2633. pSrc_file = MZ_FOPEN(pSrc_filename, "rb");
  2634. if (!pSrc_file)
  2635. return mz_zip_set_error(pZip, MZ_ZIP_FILE_OPEN_FAILED);
  2636. MZ_FSEEK64(pSrc_file, 0, SEEK_END);
  2637. uncomp_size = MZ_FTELL64(pSrc_file);
  2638. MZ_FSEEK64(pSrc_file, 0, SEEK_SET);
  2639. mz_bool status = mz_zip_writer_add_cfile(pZip, pArchive_name, pSrc_file, uncomp_size, pFile_modified_time, pComment, comment_size, level_and_flags);
  2640. MZ_FCLOSE(pSrc_file);
  2641. return status;
  2642. }
  2643. #endif // #ifndef MINIZ_NO_STDIO
  2644. static mz_bool mz_zip_writer_update_zip64_extension_block(mz_zip_array *pNew_ext, mz_zip_archive *pZip, const mz_uint8 *pExt, uint32_t ext_len, mz_uint64 *pComp_size, mz_uint64 *pUncomp_size, mz_uint64 *pLocal_header_ofs, mz_uint32 *pDisk_start)
  2645. {
  2646. // + 64 should be enough for any new zip64 data
  2647. if (!mz_zip_array_reserve(pZip, pNew_ext, ext_len + 64, MZ_FALSE))
  2648. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2649. mz_zip_array_resize(pZip, pNew_ext, 0, MZ_FALSE);
  2650. if ((pUncomp_size) || (pComp_size) || (pLocal_header_ofs) || (pDisk_start))
  2651. {
  2652. mz_uint8 new_ext_block[64];
  2653. mz_uint8 *pDst = new_ext_block;
  2654. mz_write_le16(pDst, MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID);
  2655. mz_write_le16(pDst + sizeof(mz_uint16), 0);
  2656. pDst += sizeof(mz_uint16) * 2;
  2657. if (pUncomp_size)
  2658. {
  2659. mz_write_le64(pDst, *pUncomp_size);
  2660. pDst += sizeof(mz_uint64);
  2661. }
  2662. if (pComp_size)
  2663. {
  2664. mz_write_le64(pDst, *pComp_size);
  2665. pDst += sizeof(mz_uint64);
  2666. }
  2667. if (pLocal_header_ofs)
  2668. {
  2669. mz_write_le64(pDst, *pLocal_header_ofs);
  2670. pDst += sizeof(mz_uint64);
  2671. }
  2672. if (pDisk_start)
  2673. {
  2674. mz_write_le32(pDst, *pDisk_start);
  2675. pDst += sizeof(mz_uint32);
  2676. }
  2677. mz_write_le16(new_ext_block + sizeof(mz_uint16), (mz_uint16)((pDst - new_ext_block) - sizeof(mz_uint16) * 2));
  2678. if (!mz_zip_array_push_back(pZip, pNew_ext, new_ext_block, pDst - new_ext_block))
  2679. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2680. }
  2681. if ((pExt) && (ext_len))
  2682. {
  2683. mz_uint32 extra_size_remaining = ext_len;
  2684. const mz_uint8 *pExtra_data = pExt;
  2685. do
  2686. {
  2687. mz_uint32 field_id, field_data_size, field_total_size;
  2688. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  2689. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2690. field_id = MZ_READ_LE16(pExtra_data);
  2691. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  2692. field_total_size = field_data_size + sizeof(mz_uint16) * 2;
  2693. if (field_total_size > extra_size_remaining)
  2694. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2695. if (field_id != MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
  2696. {
  2697. if (!mz_zip_array_push_back(pZip, pNew_ext, pExtra_data, field_total_size))
  2698. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2699. }
  2700. pExtra_data += field_total_size;
  2701. extra_size_remaining -= field_total_size;
  2702. } while (extra_size_remaining);
  2703. }
  2704. return MZ_TRUE;
  2705. }
  2706. // TODO: This func is now pretty freakin complex due to zip64, split it up?
  2707. mz_bool mz_zip_writer_add_from_other_zip(mz_zip_archive *pZip, mz_zip_archive *pSource_zip, mz_uint src_file_index)
  2708. {
  2709. mz_uint n, bit_flags, num_alignment_padding_bytes, src_central_dir_following_data_size;
  2710. mz_uint64 src_archive_bytes_remaining, local_dir_header_ofs;
  2711. mz_uint64 cur_src_file_ofs, cur_dst_file_ofs;
  2712. mz_uint32 local_header_u32[(MZ_ZIP_LOCAL_DIR_HEADER_SIZE + sizeof(mz_uint32) - 1) / sizeof(mz_uint32)];
  2713. mz_uint8 *pLocal_header = (mz_uint8 *)local_header_u32;
  2714. mz_uint8 new_central_header[MZ_ZIP_CENTRAL_DIR_HEADER_SIZE];
  2715. size_t orig_central_dir_size;
  2716. mz_zip_internal_state *pState;
  2717. void *pBuf;
  2718. const mz_uint8 *pSrc_central_header;
  2719. mz_zip_archive_file_stat src_file_stat;
  2720. mz_uint32 src_filename_len, src_comment_len, src_ext_len;
  2721. mz_uint32 local_header_filename_size, local_header_extra_len;
  2722. mz_uint64 local_header_comp_size, local_header_uncomp_size;
  2723. mz_bool found_zip64_ext_data_in_ldir = MZ_FALSE;
  2724. // Sanity checks
  2725. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING) || (!pSource_zip->m_pRead))
  2726. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2727. pState = pZip->m_pState;
  2728. // Don't support copying files from zip64 archives to non-zip64, even though in some cases this is possible
  2729. if ((pSource_zip->m_pState->m_zip64) && (!pZip->m_pState->m_zip64))
  2730. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2731. // Get pointer to the source central dir header and crack it
  2732. if (NULL == (pSrc_central_header = mz_zip_get_cdh(pSource_zip, src_file_index)))
  2733. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  2734. if (MZ_READ_LE32(pSrc_central_header + MZ_ZIP_CDH_SIG_OFS) != MZ_ZIP_CENTRAL_DIR_HEADER_SIG)
  2735. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2736. src_filename_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  2737. src_comment_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_COMMENT_LEN_OFS);
  2738. src_ext_len = MZ_READ_LE16(pSrc_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS);
  2739. src_central_dir_following_data_size = src_filename_len + src_ext_len + src_comment_len;
  2740. // TODO: We don't support central dir's >= MZ_UINT32_MAX bytes right now (+32 fudge factor in case we need to add more extra data)
  2741. if ((pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + 32) >= MZ_UINT32_MAX)
  2742. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  2743. num_alignment_padding_bytes = mz_zip_writer_compute_padding_needed_for_file_alignment(pZip);
  2744. if (!pState->m_zip64)
  2745. {
  2746. if (pZip->m_total_files == MZ_UINT16_MAX)
  2747. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2748. }
  2749. else
  2750. {
  2751. // TODO: Our zip64 support still has some 32-bit limits that may not be worth fixing.
  2752. if (pZip->m_total_files == MZ_UINT32_MAX)
  2753. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  2754. }
  2755. if (!mz_zip_file_stat_internal(pSource_zip, src_file_index, pSrc_central_header, &src_file_stat, NULL))
  2756. return MZ_FALSE;
  2757. cur_src_file_ofs = src_file_stat.m_local_header_ofs;
  2758. cur_dst_file_ofs = pZip->m_archive_size;
  2759. // Read the source archive's local dir header
  2760. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  2761. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2762. if (MZ_READ_LE32(pLocal_header) != MZ_ZIP_LOCAL_DIR_HEADER_SIG)
  2763. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2764. cur_src_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
  2765. // Compute the total size we need to copy (filename+extra data+compressed data)
  2766. local_header_filename_size = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_FILENAME_LEN_OFS);
  2767. local_header_extra_len = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_EXTRA_LEN_OFS);
  2768. local_header_comp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_COMPRESSED_SIZE_OFS);
  2769. local_header_uncomp_size = MZ_READ_LE32(pLocal_header + MZ_ZIP_LDH_DECOMPRESSED_SIZE_OFS);
  2770. src_archive_bytes_remaining = local_header_filename_size + local_header_extra_len + src_file_stat.m_comp_size;
  2771. // Try to find a zip64 extended information field
  2772. if ((local_header_extra_len) && ((local_header_comp_size == MZ_UINT32_MAX) || (local_header_uncomp_size == MZ_UINT32_MAX)))
  2773. {
  2774. mz_zip_array file_data_array;
  2775. mz_zip_array_init(&file_data_array, 1);
  2776. if (!mz_zip_array_resize(pZip, &file_data_array, local_header_extra_len, MZ_FALSE))
  2777. {
  2778. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2779. }
  2780. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, src_file_stat.m_local_header_ofs + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + local_header_filename_size, file_data_array.m_p, local_header_extra_len) != local_header_extra_len)
  2781. {
  2782. mz_zip_array_clear(pZip, &file_data_array);
  2783. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2784. }
  2785. mz_uint32 extra_size_remaining = local_header_extra_len;
  2786. const mz_uint8 *pExtra_data = (const mz_uint8 *)file_data_array.m_p;
  2787. do
  2788. {
  2789. mz_uint32 field_id, field_data_size, field_total_size;
  2790. if (extra_size_remaining < (sizeof(mz_uint16) * 2))
  2791. {
  2792. mz_zip_array_clear(pZip, &file_data_array);
  2793. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2794. }
  2795. field_id = MZ_READ_LE16(pExtra_data);
  2796. field_data_size = MZ_READ_LE16(pExtra_data + sizeof(mz_uint16));
  2797. field_total_size = field_data_size + sizeof(mz_uint16) * 2;
  2798. if (field_total_size > extra_size_remaining)
  2799. {
  2800. mz_zip_array_clear(pZip, &file_data_array);
  2801. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2802. }
  2803. if (field_id == MZ_ZIP64_EXTENDED_INFORMATION_FIELD_HEADER_ID)
  2804. {
  2805. const mz_uint8 *pSrc_field_data = pExtra_data + sizeof(mz_uint32);
  2806. if (field_data_size < sizeof(mz_uint64) * 2)
  2807. {
  2808. mz_zip_array_clear(pZip, &file_data_array);
  2809. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_HEADER_OR_CORRUPTED);
  2810. }
  2811. local_header_uncomp_size = MZ_READ_LE64(pSrc_field_data);
  2812. local_header_comp_size = MZ_READ_LE64(pSrc_field_data + sizeof(mz_uint64)); // may be 0 if there's a descriptor
  2813. found_zip64_ext_data_in_ldir = MZ_TRUE;
  2814. break;
  2815. }
  2816. pExtra_data += field_total_size;
  2817. extra_size_remaining -= field_total_size;
  2818. } while (extra_size_remaining);
  2819. mz_zip_array_clear(pZip, &file_data_array);
  2820. }
  2821. if (!pState->m_zip64)
  2822. {
  2823. // Try to detect if the new archive will most likely wind up too big and bail early (+(sizeof(mz_uint32) * 4) is for the optional descriptor which could be present, +64 is a fudge factor).
  2824. // We also check when the archive is finalized so this doesn't need to be perfect.
  2825. mz_uint64 approx_new_archive_size = cur_dst_file_ofs + num_alignment_padding_bytes + MZ_ZIP_LOCAL_DIR_HEADER_SIZE + src_archive_bytes_remaining + (sizeof(mz_uint32) * 4) +
  2826. pState->m_central_dir.m_size + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_central_dir_following_data_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE + 64;
  2827. if (approx_new_archive_size >= MZ_UINT32_MAX)
  2828. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2829. }
  2830. // Write dest archive padding
  2831. if (!mz_zip_writer_write_zeros(pZip, cur_dst_file_ofs, num_alignment_padding_bytes))
  2832. return MZ_FALSE;
  2833. cur_dst_file_ofs += num_alignment_padding_bytes;
  2834. local_dir_header_ofs = cur_dst_file_ofs;
  2835. if (pZip->m_file_offset_alignment)
  2836. {
  2837. MZ_ASSERT((local_dir_header_ofs & (pZip->m_file_offset_alignment - 1)) == 0);
  2838. }
  2839. // The original zip's local header+ext block doesn't change, even with zip64, so we can just copy it over to the dest zip
  2840. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pLocal_header, MZ_ZIP_LOCAL_DIR_HEADER_SIZE) != MZ_ZIP_LOCAL_DIR_HEADER_SIZE)
  2841. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2842. cur_dst_file_ofs += MZ_ZIP_LOCAL_DIR_HEADER_SIZE;
  2843. // Copy over the source archive bytes to the dest archive, also ensure we have enough buf space to handle optional data descriptor
  2844. if (NULL == (pBuf = pZip->m_pAlloc(pZip->m_pAlloc_opaque, 1, (size_t)MZ_MAX(32U, MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining)))))
  2845. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2846. while (src_archive_bytes_remaining)
  2847. {
  2848. n = (mz_uint)MZ_MIN((mz_uint64)MZ_ZIP_MAX_IO_BUF_SIZE, src_archive_bytes_remaining);
  2849. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, n) != n)
  2850. {
  2851. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  2852. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2853. }
  2854. cur_src_file_ofs += n;
  2855. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)
  2856. {
  2857. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  2858. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2859. }
  2860. cur_dst_file_ofs += n;
  2861. src_archive_bytes_remaining -= n;
  2862. }
  2863. // Now deal with the optional data descriptor
  2864. bit_flags = MZ_READ_LE16(pLocal_header + MZ_ZIP_LDH_BIT_FLAG_OFS);
  2865. if (bit_flags & 8)
  2866. {
  2867. // Copy data descriptor
  2868. if ((pSource_zip->m_pState->m_zip64) || (found_zip64_ext_data_in_ldir))
  2869. {
  2870. // src is zip64, dest must be zip64
  2871. // name uint32_t's
  2872. // id 1 (optional in zip64?)
  2873. // crc 1
  2874. // comp_size 2
  2875. // uncomp_size 2
  2876. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, (sizeof(mz_uint32) * 6)) != (sizeof(mz_uint32) * 6))
  2877. {
  2878. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  2879. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2880. }
  2881. n = sizeof(mz_uint32) * ((MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID) ? 6 : 5);
  2882. }
  2883. else
  2884. {
  2885. // src is NOT zip64
  2886. mz_bool has_id;
  2887. if (pSource_zip->m_pRead(pSource_zip->m_pIO_opaque, cur_src_file_ofs, pBuf, sizeof(mz_uint32) * 4) != sizeof(mz_uint32) * 4)
  2888. {
  2889. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  2890. return mz_zip_set_error(pZip, MZ_ZIP_FILE_READ_FAILED);
  2891. }
  2892. has_id = (MZ_READ_LE32(pBuf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
  2893. if (pZip->m_pState->m_zip64)
  2894. {
  2895. // dest is zip64, so upgrade the data descriptor
  2896. const mz_uint32 *pSrc_descriptor = (const mz_uint32 *)((const mz_uint8 *)pBuf + (has_id ? sizeof(mz_uint32) : 0));
  2897. const mz_uint32 src_crc32 = pSrc_descriptor[0];
  2898. const mz_uint64 src_comp_size = pSrc_descriptor[1];
  2899. const mz_uint64 src_uncomp_size = pSrc_descriptor[2];
  2900. mz_write_le32((mz_uint8 *)pBuf, MZ_ZIP_DATA_DESCRIPTOR_ID);
  2901. mz_write_le32((mz_uint8 *)pBuf + sizeof(mz_uint32) * 1, src_crc32);
  2902. mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 2, src_comp_size);
  2903. mz_write_le64((mz_uint8 *)pBuf + sizeof(mz_uint32) * 4, src_uncomp_size);
  2904. n = sizeof(mz_uint32) * 6;
  2905. }
  2906. else
  2907. {
  2908. // dest is NOT zip64, just copy it as-is
  2909. n = sizeof(mz_uint32) * (has_id ? 4 : 3);
  2910. }
  2911. }
  2912. if (pZip->m_pWrite(pZip->m_pIO_opaque, cur_dst_file_ofs, pBuf, n) != n)
  2913. {
  2914. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  2915. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  2916. }
  2917. cur_src_file_ofs += n;
  2918. cur_dst_file_ofs += n;
  2919. }
  2920. pZip->m_pFree(pZip->m_pAlloc_opaque, pBuf);
  2921. // Finally, add the new central dir header
  2922. orig_central_dir_size = pState->m_central_dir.m_size;
  2923. memcpy(new_central_header, pSrc_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE);
  2924. if (pState->m_zip64)
  2925. {
  2926. // This is the painful part: We need to write a new central dir header + ext block with updated zip64 fields, and ensure the old fields (if any) are not included.
  2927. const mz_uint8 *pSrc_ext = pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len;
  2928. mz_zip_array new_ext_block;
  2929. mz_zip_array_init(&new_ext_block, sizeof(mz_uint8));
  2930. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_COMPRESSED_SIZE_OFS, MZ_UINT32_MAX);
  2931. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_DECOMPRESSED_SIZE_OFS, MZ_UINT32_MAX);
  2932. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, MZ_UINT32_MAX);
  2933. if (!mz_zip_writer_update_zip64_extension_block(&new_ext_block, pZip, pSrc_ext, src_ext_len, &src_file_stat.m_comp_size, &src_file_stat.m_uncomp_size, &local_dir_header_ofs, NULL))
  2934. {
  2935. mz_zip_array_clear(pZip, &new_ext_block);
  2936. return MZ_FALSE;
  2937. }
  2938. MZ_WRITE_LE16(new_central_header + MZ_ZIP_CDH_EXTRA_LEN_OFS, new_ext_block.m_size);
  2939. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))
  2940. {
  2941. mz_zip_array_clear(pZip, &new_ext_block);
  2942. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2943. }
  2944. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_filename_len))
  2945. {
  2946. mz_zip_array_clear(pZip, &new_ext_block);
  2947. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2948. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2949. }
  2950. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_ext_block.m_p, new_ext_block.m_size))
  2951. {
  2952. mz_zip_array_clear(pZip, &new_ext_block);
  2953. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2954. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2955. }
  2956. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE + src_filename_len + src_ext_len, src_comment_len))
  2957. {
  2958. mz_zip_array_clear(pZip, &new_ext_block);
  2959. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2960. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2961. }
  2962. mz_zip_array_clear(pZip, &new_ext_block);
  2963. }
  2964. else
  2965. {
  2966. // sanity checks
  2967. if (cur_dst_file_ofs > MZ_UINT32_MAX)
  2968. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2969. if (local_dir_header_ofs >= MZ_UINT32_MAX)
  2970. return mz_zip_set_error(pZip, MZ_ZIP_ARCHIVE_TOO_LARGE);
  2971. MZ_WRITE_LE32(new_central_header + MZ_ZIP_CDH_LOCAL_HEADER_OFS, local_dir_header_ofs);
  2972. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, new_central_header, MZ_ZIP_CENTRAL_DIR_HEADER_SIZE))
  2973. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2974. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir, pSrc_central_header + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, src_central_dir_following_data_size))
  2975. {
  2976. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2977. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2978. }
  2979. }
  2980. // This shouldn't trigger unless we screwed up during the initial sanity checks
  2981. if (pState->m_central_dir.m_size >= MZ_UINT32_MAX)
  2982. {
  2983. // TODO: Support central dirs >= 32-bits in size
  2984. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2985. return mz_zip_set_error(pZip, MZ_ZIP_UNSUPPORTED_CDIR_SIZE);
  2986. }
  2987. n = (mz_uint32)orig_central_dir_size;
  2988. if (!mz_zip_array_push_back(pZip, &pState->m_central_dir_offsets, &n, 1))
  2989. {
  2990. mz_zip_array_resize(pZip, &pState->m_central_dir, orig_central_dir_size, MZ_FALSE);
  2991. return mz_zip_set_error(pZip, MZ_ZIP_ALLOC_FAILED);
  2992. }
  2993. pZip->m_total_files++;
  2994. pZip->m_archive_size = cur_dst_file_ofs;
  2995. return MZ_TRUE;
  2996. }
  2997. mz_bool mz_zip_writer_finalize_archive(mz_zip_archive *pZip)
  2998. {
  2999. mz_zip_internal_state *pState;
  3000. mz_uint64 central_dir_ofs, central_dir_size;
  3001. mz_uint8 hdr[256];
  3002. if ((!pZip) || (!pZip->m_pState) || (pZip->m_zip_mode != MZ_ZIP_MODE_WRITING))
  3003. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  3004. pState = pZip->m_pState;
  3005. if (pState->m_zip64)
  3006. {
  3007. if ((pZip->m_total_files > MZ_UINT32_MAX) || (pState->m_central_dir.m_size >= MZ_UINT32_MAX))
  3008. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  3009. }
  3010. else
  3011. {
  3012. if ((pZip->m_total_files > MZ_UINT16_MAX) || ((pZip->m_archive_size + pState->m_central_dir.m_size + MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) > MZ_UINT32_MAX))
  3013. return mz_zip_set_error(pZip, MZ_ZIP_TOO_MANY_FILES);
  3014. }
  3015. central_dir_ofs = 0;
  3016. central_dir_size = 0;
  3017. if (pZip->m_total_files)
  3018. {
  3019. // Write central directory
  3020. central_dir_ofs = pZip->m_archive_size;
  3021. central_dir_size = pState->m_central_dir.m_size;
  3022. pZip->m_central_directory_file_ofs = central_dir_ofs;
  3023. if (pZip->m_pWrite(pZip->m_pIO_opaque, central_dir_ofs, pState->m_central_dir.m_p, (size_t)central_dir_size) != central_dir_size)
  3024. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  3025. pZip->m_archive_size += central_dir_size;
  3026. }
  3027. if (pState->m_zip64)
  3028. {
  3029. // Write zip64 end of central directory header
  3030. mz_uint64 rel_ofs_to_zip64_ecdr = pZip->m_archive_size;
  3031. MZ_CLEAR_OBJ(hdr);
  3032. MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDH_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG);
  3033. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_SIZE_OF_RECORD_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE - sizeof(mz_uint32) - sizeof(mz_uint64));
  3034. MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_MADE_BY_OFS, 0x031E); // TODO: always Unix
  3035. MZ_WRITE_LE16(hdr + MZ_ZIP64_ECDH_VERSION_NEEDED_OFS, 0x002D);
  3036. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, pZip->m_total_files);
  3037. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS, pZip->m_total_files);
  3038. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_SIZE_OFS, central_dir_size);
  3039. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDH_CDIR_OFS_OFS, central_dir_ofs);
  3040. if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
  3041. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  3042. pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE;
  3043. // Write zip64 end of central directory locator
  3044. MZ_CLEAR_OBJ(hdr);
  3045. MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_SIG_OFS, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG);
  3046. MZ_WRITE_LE64(hdr + MZ_ZIP64_ECDL_REL_OFS_TO_ZIP64_ECDR_OFS, rel_ofs_to_zip64_ecdr);
  3047. MZ_WRITE_LE32(hdr + MZ_ZIP64_ECDL_TOTAL_NUMBER_OF_DISKS_OFS, 1);
  3048. if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) != MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE)
  3049. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  3050. pZip->m_archive_size += MZ_ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE;
  3051. }
  3052. // Write end of central directory record
  3053. MZ_CLEAR_OBJ(hdr);
  3054. MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_SIG_OFS, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIG);
  3055. MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
  3056. MZ_WRITE_LE16(hdr + MZ_ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS, MZ_MIN(MZ_UINT16_MAX, pZip->m_total_files));
  3057. MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_SIZE_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_size));
  3058. MZ_WRITE_LE32(hdr + MZ_ZIP_ECDH_CDIR_OFS_OFS, MZ_MIN(MZ_UINT32_MAX, central_dir_ofs));
  3059. if (pZip->m_pWrite(pZip->m_pIO_opaque, pZip->m_archive_size, hdr, MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
  3060. return mz_zip_set_error(pZip, MZ_ZIP_FILE_WRITE_FAILED);
  3061. #ifndef MINIZ_NO_STDIO
  3062. if ((pState->m_pFile) && (MZ_FFLUSH(pState->m_pFile) == EOF))
  3063. return mz_zip_set_error(pZip, MZ_ZIP_FILE_CLOSE_FAILED);
  3064. #endif // #ifndef MINIZ_NO_STDIO
  3065. pZip->m_archive_size += MZ_ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE;
  3066. pZip->m_zip_mode = MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED;
  3067. return MZ_TRUE;
  3068. }
  3069. mz_bool mz_zip_writer_finalize_heap_archive(mz_zip_archive *pZip, void **ppBuf, size_t *pSize)
  3070. {
  3071. if ((!ppBuf) || (!pSize))
  3072. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  3073. *ppBuf = NULL;
  3074. *pSize = 0;
  3075. if ((!pZip) || (!pZip->m_pState))
  3076. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  3077. if (pZip->m_pWrite != mz_zip_heap_write_func)
  3078. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  3079. if (!mz_zip_writer_finalize_archive(pZip))
  3080. return MZ_FALSE;
  3081. *ppBuf = pZip->m_pState->m_pMem;
  3082. *pSize = pZip->m_pState->m_mem_size;
  3083. pZip->m_pState->m_pMem = NULL;
  3084. pZip->m_pState->m_mem_size = pZip->m_pState->m_mem_capacity = 0;
  3085. return MZ_TRUE;
  3086. }
  3087. mz_bool mz_zip_writer_end(mz_zip_archive *pZip)
  3088. {
  3089. return mz_zip_writer_end_internal(pZip, MZ_TRUE);
  3090. }
  3091. #ifndef MINIZ_NO_STDIO
  3092. mz_bool mz_zip_add_mem_to_archive_file_in_place(const char *pZip_filename, const char *pArchive_name, const void *pBuf, size_t buf_size, const void *pComment, mz_uint16 comment_size, mz_uint level_and_flags, mz_zip_error *pErr)
  3093. {
  3094. mz_bool status, created_new_archive = MZ_FALSE;
  3095. mz_zip_archive zip_archive;
  3096. struct MZ_FILE_STAT_STRUCT file_stat;
  3097. mz_zip_error actual_err = MZ_ZIP_NO_ERROR;
  3098. mz_zip_zero_struct(&zip_archive);
  3099. if ((int)level_and_flags < 0)
  3100. level_and_flags = MZ_DEFAULT_LEVEL;
  3101. if ((!pZip_filename) || (!pArchive_name) || ((buf_size) && (!pBuf)) || ((comment_size) && (!pComment)) || ((level_and_flags & 0xF) > MZ_UBER_COMPRESSION))
  3102. {
  3103. if (pErr)
  3104. *pErr = MZ_ZIP_INVALID_PARAMETER;
  3105. return MZ_FALSE;
  3106. }
  3107. if (!mz_zip_writer_validate_archive_name(pArchive_name))
  3108. {
  3109. if (pErr)
  3110. *pErr = MZ_ZIP_INVALID_FILENAME;
  3111. return MZ_FALSE;
  3112. }
  3113. // Important: The regular non-64 bit version of stat() can fail here if the file is very large, which could cause the archive to be overwritten.
  3114. // So be sure to compile with _LARGEFILE64_SOURCE 1
  3115. if (MZ_FILE_STAT(pZip_filename, &file_stat) != 0)
  3116. {
  3117. // Create a new archive.
  3118. if (!mz_zip_writer_init_file(&zip_archive, pZip_filename, 0, level_and_flags))
  3119. {
  3120. if (pErr)
  3121. *pErr = zip_archive.m_last_error;
  3122. return MZ_FALSE;
  3123. }
  3124. created_new_archive = MZ_TRUE;
  3125. }
  3126. else
  3127. {
  3128. // Append to an existing archive.
  3129. if (!mz_zip_reader_init_file(&zip_archive, pZip_filename, level_and_flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0))
  3130. {
  3131. if (pErr)
  3132. *pErr = zip_archive.m_last_error;
  3133. return MZ_FALSE;
  3134. }
  3135. if (!mz_zip_writer_init_from_reader(&zip_archive, pZip_filename, level_and_flags))
  3136. {
  3137. if (pErr)
  3138. *pErr = zip_archive.m_last_error;
  3139. mz_zip_reader_end_internal(&zip_archive, MZ_FALSE);
  3140. return MZ_FALSE;
  3141. }
  3142. }
  3143. status = mz_zip_writer_add_mem_ex(&zip_archive, pArchive_name, pBuf, buf_size, pComment, comment_size, level_and_flags, 0, 0);
  3144. actual_err = zip_archive.m_last_error;
  3145. // Always finalize, even if adding failed for some reason, so we have a valid central directory. (This may not always succeed, but we can try.)
  3146. if (!mz_zip_writer_finalize_archive(&zip_archive))
  3147. {
  3148. if (!actual_err)
  3149. actual_err = zip_archive.m_last_error;
  3150. status = MZ_FALSE;
  3151. }
  3152. if (!mz_zip_writer_end_internal(&zip_archive, status))
  3153. {
  3154. if (!actual_err)
  3155. actual_err = zip_archive.m_last_error;
  3156. status = MZ_FALSE;
  3157. }
  3158. if ((!status) && (created_new_archive))
  3159. {
  3160. // It's a new archive and something went wrong, so just delete it.
  3161. int ignoredStatus = MZ_DELETE_FILE(pZip_filename);
  3162. (void)ignoredStatus;
  3163. }
  3164. if (pErr)
  3165. *pErr = actual_err;
  3166. return status;
  3167. }
  3168. void *mz_zip_extract_archive_file_to_heap(const char *pZip_filename, const char *pArchive_name, const char *pComment, size_t *pSize, mz_uint flags, mz_zip_error *pErr)
  3169. {
  3170. mz_uint32 file_index;
  3171. mz_zip_archive zip_archive;
  3172. void *p = NULL;
  3173. if (pSize)
  3174. *pSize = 0;
  3175. if ((!pZip_filename) || (!pArchive_name))
  3176. {
  3177. if (pErr)
  3178. *pErr = MZ_ZIP_INVALID_PARAMETER;
  3179. return NULL;
  3180. }
  3181. mz_zip_zero_struct(&zip_archive);
  3182. if (!mz_zip_reader_init_file(&zip_archive, pZip_filename, flags | MZ_ZIP_FLAG_DO_NOT_SORT_CENTRAL_DIRECTORY, 0, 0))
  3183. {
  3184. if (pErr)
  3185. *pErr = zip_archive.m_last_error;
  3186. return NULL;
  3187. }
  3188. if (mz_zip_locate_file(&zip_archive, pArchive_name, pComment, flags, &file_index))
  3189. {
  3190. p = mz_zip_extract_to_heap(&zip_archive, file_index, pSize, flags);
  3191. }
  3192. mz_zip_reader_end_internal(&zip_archive, p != NULL);
  3193. if (pErr)
  3194. *pErr = zip_archive.m_last_error;
  3195. return p;
  3196. }
  3197. #endif // #ifndef MINIZ_NO_STDIO
  3198. #endif // #ifndef MINIZ_NO_ARCHIVE_WRITING_APIS
  3199. // ------------------- Misc utils
  3200. mz_zip_mode mz_zip_get_mode(mz_zip_archive *pZip)
  3201. {
  3202. return pZip ? pZip->m_zip_mode : MZ_ZIP_MODE_INVALID;
  3203. }
  3204. mz_zip_type mz_zip_get_type(mz_zip_archive *pZip)
  3205. {
  3206. return pZip ? pZip->m_zip_type : MZ_ZIP_TYPE_INVALID;
  3207. }
  3208. mz_zip_error mz_zip_set_last_error(mz_zip_archive *pZip, mz_zip_error err_num)
  3209. {
  3210. mz_zip_error prev_err;
  3211. if (!pZip)
  3212. return MZ_ZIP_INVALID_PARAMETER;
  3213. prev_err = pZip->m_last_error;
  3214. pZip->m_last_error = err_num;
  3215. return prev_err;
  3216. }
  3217. mz_zip_error mz_zip_peek_last_error(mz_zip_archive *pZip)
  3218. {
  3219. if (!pZip)
  3220. return MZ_ZIP_INVALID_PARAMETER;
  3221. return pZip->m_last_error;
  3222. }
  3223. mz_zip_error mz_zip_clear_last_error(mz_zip_archive *pZip)
  3224. {
  3225. return mz_zip_set_last_error(pZip, MZ_ZIP_NO_ERROR);
  3226. }
  3227. mz_zip_error mz_zip_get_last_error(mz_zip_archive *pZip)
  3228. {
  3229. mz_zip_error prev_err;
  3230. if (!pZip)
  3231. return MZ_ZIP_INVALID_PARAMETER;
  3232. prev_err = pZip->m_last_error;
  3233. pZip->m_last_error = MZ_ZIP_NO_ERROR;
  3234. return prev_err;
  3235. }
  3236. const char *mz_zip_get_error_string(mz_zip_error mz_err)
  3237. {
  3238. switch (mz_err)
  3239. {
  3240. case MZ_ZIP_NO_ERROR:
  3241. return "no error";
  3242. case MZ_ZIP_UNDEFINED_ERROR:
  3243. return "undefined error";
  3244. case MZ_ZIP_TOO_MANY_FILES:
  3245. return "too many files";
  3246. case MZ_ZIP_FILE_TOO_LARGE:
  3247. return "file too large";
  3248. case MZ_ZIP_UNSUPPORTED_METHOD:
  3249. return "unsupported method";
  3250. case MZ_ZIP_UNSUPPORTED_ENCRYPTION:
  3251. return "unsupported encryption";
  3252. case MZ_ZIP_UNSUPPORTED_FEATURE:
  3253. return "unsupported feature";
  3254. case MZ_ZIP_FAILED_FINDING_CENTRAL_DIR:
  3255. return "failed finding central directory";
  3256. case MZ_ZIP_NOT_AN_ARCHIVE:
  3257. return "not a ZIP archive";
  3258. case MZ_ZIP_INVALID_HEADER_OR_CORRUPTED:
  3259. return "invalid header or archive is corrupted";
  3260. case MZ_ZIP_UNSUPPORTED_MULTIDISK:
  3261. return "unsupported multidisk archive";
  3262. case MZ_ZIP_DECOMPRESSION_FAILED:
  3263. return "decompression failed or archive is corrupted";
  3264. case MZ_ZIP_COMPRESSION_FAILED:
  3265. return "compression failed";
  3266. case MZ_ZIP_UNEXPECTED_DECOMPRESSED_SIZE:
  3267. return "unexpected decompressed size";
  3268. case MZ_ZIP_CRC_CHECK_FAILED:
  3269. return "CRC-32 check failed";
  3270. case MZ_ZIP_UNSUPPORTED_CDIR_SIZE:
  3271. return "unsupported central directory size";
  3272. case MZ_ZIP_ALLOC_FAILED:
  3273. return "allocation failed";
  3274. case MZ_ZIP_FILE_OPEN_FAILED:
  3275. return "file open failed";
  3276. case MZ_ZIP_FILE_CREATE_FAILED:
  3277. return "file create failed";
  3278. case MZ_ZIP_FILE_WRITE_FAILED:
  3279. return "file write failed";
  3280. case MZ_ZIP_FILE_READ_FAILED:
  3281. return "file read failed";
  3282. case MZ_ZIP_FILE_CLOSE_FAILED:
  3283. return "file close failed";
  3284. case MZ_ZIP_FILE_SEEK_FAILED:
  3285. return "file seek failed";
  3286. case MZ_ZIP_FILE_STAT_FAILED:
  3287. return "file stat failed";
  3288. case MZ_ZIP_INVALID_PARAMETER:
  3289. return "invalid parameter";
  3290. case MZ_ZIP_INVALID_FILENAME:
  3291. return "invalid filename";
  3292. case MZ_ZIP_BUF_TOO_SMALL:
  3293. return "buffer too small";
  3294. case MZ_ZIP_INTERNAL_ERROR:
  3295. return "internal error";
  3296. case MZ_ZIP_FILE_NOT_FOUND:
  3297. return "file not found";
  3298. case MZ_ZIP_ARCHIVE_TOO_LARGE:
  3299. return "archive is too large";
  3300. case MZ_ZIP_VALIDATION_FAILED:
  3301. return "validation failed";
  3302. case MZ_ZIP_WRITE_CALLBACK_FAILED:
  3303. return "write calledback failed";
  3304. default:
  3305. break;
  3306. }
  3307. return "unknown error";
  3308. }
  3309. // Note: Just because the archive is not zip64 doesn't necessarily mean it doesn't have Zip64 extended information extra field, argh.
  3310. mz_bool mz_zip_is_zip64(mz_zip_archive *pZip)
  3311. {
  3312. if ((!pZip) || (!pZip->m_pState))
  3313. return MZ_FALSE;
  3314. return pZip->m_pState->m_zip64;
  3315. }
  3316. size_t mz_zip_get_central_dir_size(mz_zip_archive *pZip)
  3317. {
  3318. if ((!pZip) || (!pZip->m_pState))
  3319. return 0;
  3320. return pZip->m_pState->m_central_dir.m_size;
  3321. }
  3322. mz_uint mz_zip_get_num_files(mz_zip_archive *pZip)
  3323. {
  3324. return pZip ? pZip->m_total_files : 0;
  3325. }
  3326. mz_uint64 mz_zip_get_archive_size(mz_zip_archive *pZip)
  3327. {
  3328. if (!pZip)
  3329. return 0;
  3330. return pZip->m_archive_size;
  3331. }
  3332. mz_uint64 mz_zip_get_archive_file_start_offset(mz_zip_archive *pZip)
  3333. {
  3334. if ((!pZip) || (!pZip->m_pState))
  3335. return 0;
  3336. return pZip->m_pState->m_file_archive_start_ofs;
  3337. }
  3338. MZ_FILE *mz_zip_get_cfile(mz_zip_archive *pZip)
  3339. {
  3340. if ((!pZip) || (!pZip->m_pState))
  3341. return 0;
  3342. return pZip->m_pState->m_pFile;
  3343. }
  3344. size_t mz_zip_read_archive_data(mz_zip_archive *pZip, mz_uint64 file_ofs, void *pBuf, size_t n)
  3345. {
  3346. if ((!pZip) || (!pZip->m_pState) || (!pBuf) || (!pZip->m_pRead))
  3347. return mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  3348. return pZip->m_pRead(pZip->m_pIO_opaque, file_ofs, pBuf, n);
  3349. }
  3350. mz_uint mz_zip_get_filename(mz_zip_archive *pZip, mz_uint file_index, char *pFilename, mz_uint filename_buf_size)
  3351. {
  3352. mz_uint n;
  3353. const mz_uint8 *p = mz_zip_get_cdh(pZip, file_index);
  3354. if (!p)
  3355. {
  3356. if (filename_buf_size)
  3357. pFilename[0] = '\0';
  3358. mz_zip_set_error(pZip, MZ_ZIP_INVALID_PARAMETER);
  3359. return 0;
  3360. }
  3361. n = MZ_READ_LE16(p + MZ_ZIP_CDH_FILENAME_LEN_OFS);
  3362. if (filename_buf_size)
  3363. {
  3364. n = MZ_MIN(n, filename_buf_size - 1);
  3365. memcpy(pFilename, p + MZ_ZIP_CENTRAL_DIR_HEADER_SIZE, n);
  3366. pFilename[n] = '\0';
  3367. }
  3368. return n + 1;
  3369. }
  3370. mz_bool mz_zip_file_stat(mz_zip_archive *pZip, mz_uint file_index, mz_zip_archive_file_stat *pStat)
  3371. {
  3372. return mz_zip_file_stat_internal(pZip, file_index, mz_zip_get_cdh(pZip, file_index), pStat, NULL);
  3373. }
  3374. mz_bool mz_zip_end(mz_zip_archive *pZip)
  3375. {
  3376. if (!pZip)
  3377. return MZ_FALSE;
  3378. if (pZip->m_zip_mode == MZ_ZIP_MODE_READING)
  3379. return mz_zip_reader_end(pZip);
  3380. else if ((pZip->m_zip_mode == MZ_ZIP_MODE_WRITING) || (pZip->m_zip_mode == MZ_ZIP_MODE_WRITING_HAS_BEEN_FINALIZED))
  3381. return mz_zip_writer_end(pZip);
  3382. return MZ_FALSE;
  3383. }
  3384. #endif // #ifndef MINIZ_NO_ARCHIVE_APIS
  3385. #ifdef __cplusplus
  3386. }
  3387. #endif