XUnzip.cpp 145 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445
  1. // XUnzip.cpp Version 1.3
  2. //
  3. // Authors: Mark Adler et al. (see below)
  4. //
  5. // Modified by: Lucian Wischik
  6. // lu@wischik.com
  7. //
  8. // Version 1.0 - Turned C files into just a single CPP file
  9. // - Made them compile cleanly as C++ files
  10. // - Gave them simpler APIs
  11. // - Added the ability to zip/unzip directly in memory without
  12. // any intermediate files
  13. //
  14. // Modified by: Hans Dietrich
  15. // hdietrich@gmail.com
  16. //
  17. // Version 1.3: - Corrected size bug introduced by 1.2
  18. //
  19. // Version 1.2: - Many bug fixes. See CodeProject article for list.
  20. //
  21. // Version 1.1: - Added Unicode support to CreateZip() and ZipAdd()
  22. // - Changed file names to avoid conflicts with Lucian's files
  23. //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Lucian Wischik's comments:
  27. // --------------------------
  28. // THIS FILE is almost entirely based upon code by Info-ZIP.
  29. // It has been modified by Lucian Wischik.
  30. // The original code may be found at http://www.info-zip.org
  31. // The original copyright text follows.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////////
  34. //
  35. // Original authors' comments:
  36. // ---------------------------
  37. // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
  38. // definitive version of this document should be available at
  39. // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
  40. //
  41. // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
  42. //
  43. // For the purposes of this copyright and license, "Info-ZIP" is defined as
  44. // the following set of individuals:
  45. //
  46. // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
  47. // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
  48. // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
  49. // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
  50. // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
  51. // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
  52. // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
  53. //
  54. // This software is provided "as is", without warranty of any kind, express
  55. // or implied. In no event shall Info-ZIP or its contributors be held liable
  56. // for any direct, indirect, incidental, special or consequential damages
  57. // arising out of the use of or inability to use this software.
  58. //
  59. // Permission is granted to anyone to use this software for any purpose,
  60. // including commercial applications, and to alter it and redistribute it
  61. // freely, subject to the following restrictions:
  62. //
  63. // 1. Redistributions of source code must retain the above copyright notice,
  64. // definition, disclaimer, and this list of conditions.
  65. //
  66. // 2. Redistributions in binary form (compiled executables) must reproduce
  67. // the above copyright notice, definition, disclaimer, and this list of
  68. // conditions in documentation and/or other materials provided with the
  69. // distribution. The sole exception to this condition is redistribution
  70. // of a standard UnZipSFX binary as part of a self-extracting archive;
  71. // that is permitted without inclusion of this license, as long as the
  72. // normal UnZipSFX banner has not been removed from the binary or disabled.
  73. //
  74. // 3. Altered versions--including, but not limited to, ports to new
  75. // operating systems, existing ports with new graphical interfaces, and
  76. // dynamic, shared, or static library versions--must be plainly marked
  77. // as such and must not be misrepresented as being the original source.
  78. // Such altered versions also must not be misrepresented as being
  79. // Info-ZIP releases--including, but not limited to, labeling of the
  80. // altered versions with the names "Info-ZIP" (or any variation thereof,
  81. // including, but not limited to, different capitalizations),
  82. // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
  83. // Info-ZIP. Such altered versions are further prohibited from
  84. // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
  85. // of the Info-ZIP URL(s).
  86. //
  87. // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
  88. // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
  89. // own source and binary releases.
  90. //
  91. ///////////////////////////////////////////////////////////////////////////////
  92. //#define _USE_32BIT_TIME_T //+++1.2
  93. #define STRICT
  94. #define WIN32_LEAN_AND_MEAN
  95. #include <windows.h>
  96. #include <time.h>
  97. #include <stdio.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100. #include <tchar.h>
  101. #pragma warning(disable : 4996) // disable bogus deprecation warning
  102. // THIS FILE is almost entirely based upon code by Jean-loup Gailly
  103. // and Mark Adler. It has been modified by Lucian Wischik.
  104. // The original code may be found at http://www.gzip.org/zlib/
  105. // The original copyright text follows.
  106. //
  107. //
  108. //
  109. // zlib.h -- interface of the 'zlib' general purpose compression library
  110. // version 1.1.3, July 9th, 1998
  111. //
  112. // Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
  113. //
  114. // This software is provided 'as-is', without any express or implied
  115. // warranty. In no event will the authors be held liable for any damages
  116. // arising from the use of this software.
  117. //
  118. // Permission is granted to anyone to use this software for any purpose,
  119. // including commercial applications, and to alter it and redistribute it
  120. // freely, subject to the following restrictions:
  121. //
  122. // 1. The origin of this software must not be misrepresented; you must not
  123. // claim that you wrote the original software. If you use this software
  124. // in a product, an acknowledgment in the product documentation would be
  125. // appreciated but is not required.
  126. // 2. Altered source versions must be plainly marked as such, and must not be
  127. // misrepresented as being the original software.
  128. // 3. This notice may not be removed or altered from any source distribution.
  129. //
  130. // Jean-loup Gailly Mark Adler
  131. // jloup@gzip.org madler@alumni.caltech.edu
  132. //
  133. //
  134. // The data format used by the zlib library is described by RFCs (Request for
  135. // Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
  136. // (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
  137. //
  138. //
  139. // The 'zlib' compression library provides in-memory compression and
  140. // decompression functions, including integrity checks of the uncompressed
  141. // data. This version of the library supports only one compression method
  142. // (deflation) but other algorithms will be added later and will have the same
  143. // stream interface.
  144. //
  145. // Compression can be done in a single step if the buffers are large
  146. // enough (for example if an input file is mmap'ed), or can be done by
  147. // repeated calls of the compression function. In the latter case, the
  148. // application must provide more input and/or consume the output
  149. // (providing more output space) before each call.
  150. //
  151. // The library also supports reading and writing files in gzip (.gz) format
  152. // with an interface similar to that of stdio.
  153. //
  154. // The library does not install any signal handler. The decoder checks
  155. // the consistency of the compressed data, so the library should never
  156. // crash even in case of corrupted input.
  157. //
  158. // for more info about .ZIP format, see ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
  159. // PkWare has also a specification at ftp://ftp.pkware.com/probdesc.zip
  160. #define zmalloc(len) malloc(len)
  161. #define zfree(p) free(p)
  162. /*
  163. void *zmalloc(unsigned int len)
  164. { char *buf = new char[len+32];
  165. for (int i=0; i<16; i++)
  166. { buf[i]=i;
  167. buf[len+31-i]=i;
  168. }
  169. *((unsigned int*)buf) = len;
  170. char c[1000]; wsprintf(c,"malloc 0x%lx - %lu",buf+16,len);
  171. OutputDebugString(c);
  172. return buf+16;
  173. }
  174. void zfree(void *buf)
  175. { char c[1000]; wsprintf(c,"free 0x%lx",buf);
  176. OutputDebugString(c);
  177. char *p = ((char*)buf)-16;
  178. unsigned int len = *((unsigned int*)p);
  179. bool blown=false;
  180. for (int i=0; i<16; i++)
  181. { char lo = p[i];
  182. char hi = p[len+31-i];
  183. if (hi!=i || (lo!=i && i>4)) blown=true;
  184. }
  185. if (blown)
  186. { OutputDebugString("BLOWN!!!");
  187. }
  188. delete[] p;
  189. }
  190. */
  191. DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that has been opened
  192. typedef DWORD ZRESULT;
  193. // return codes from any of the zip functions. Listed later.
  194. #define ZIP_HANDLE 1
  195. #define ZIP_FILENAME 2
  196. #define ZIP_MEMORY 3
  197. typedef struct
  198. { int index; // index of this file within the zip
  199. char name[MAX_PATH]; // filename within the zip
  200. DWORD attr; // attributes, as in GetFileAttributes.
  201. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  202. long comp_size; // sizes of item, compressed and uncompressed. These
  203. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  204. } ZIPENTRY;
  205. typedef struct
  206. { int index; // index of this file within the zip
  207. TCHAR name[MAX_PATH]; // filename within the zip
  208. DWORD attr; // attributes, as in GetFileAttributes.
  209. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  210. long comp_size; // sizes of item, compressed and uncompressed. These
  211. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  212. } ZIPENTRYW;
  213. // These are the result codes:
  214. #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
  215. #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
  216. // The following come from general system stuff (e.g. files not openable)
  217. #define ZR_GENMASK 0x0000FF00
  218. #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
  219. #define ZR_NOFILE 0x00000200 // couldn't create/open the file
  220. #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
  221. #define ZR_WRITE 0x00000400 // a general error writing to the file
  222. #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
  223. #define ZR_MORE 0x00000600 // there's still more data to be unzipped
  224. #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
  225. #define ZR_READ 0x00000800 // a general error reading the file
  226. // The following come from mistakes on the part of the caller
  227. #define ZR_CALLERMASK 0x00FF0000
  228. #define ZR_ARGS 0x00010000 // general mistake with the arguments
  229. #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  230. #define ZR_MEMSIZE 0x00030000 // the memory size is too small
  231. #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
  232. #define ZR_ENDED 0x00050000 // the zip creation has already been closed
  233. #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
  234. #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
  235. #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
  236. // The following come from bugs within the zip library itself
  237. #define ZR_BUGMASK 0xFF000000
  238. #define ZR_NOTINITED 0x01000000 // initialisation didn't work
  239. #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
  240. #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
  241. #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
  242. #pragma warning(disable : 4702) // unreachable code
  243. static ZRESULT zopenerror = ZR_OK; //+++1.2
  244. typedef struct tm_unz_s
  245. { unsigned int tm_sec; // seconds after the minute - [0,59]
  246. unsigned int tm_min; // minutes after the hour - [0,59]
  247. unsigned int tm_hour; // hours since midnight - [0,23]
  248. unsigned int tm_mday; // day of the month - [1,31]
  249. unsigned int tm_mon; // months since January - [0,11]
  250. unsigned int tm_year; // years - [1980..2044]
  251. } tm_unz;
  252. // unz_global_info structure contain global data about the ZIPfile
  253. typedef struct unz_global_info_s
  254. { unsigned long number_entry; // total number of entries in the central dir on this disk
  255. unsigned long size_comment; // size of the global comment of the zipfile
  256. } unz_global_info;
  257. // unz_file_info contain information about a file in the zipfile
  258. typedef struct unz_file_info_s
  259. { unsigned long version; // version made by 2 bytes
  260. unsigned long version_needed; // version needed to extract 2 bytes
  261. unsigned long flag; // general purpose bit flag 2 bytes
  262. unsigned long compression_method; // compression method 2 bytes
  263. unsigned long dosDate; // last mod file date in Dos fmt 4 bytes
  264. unsigned long crc; // crc-32 4 bytes
  265. unsigned long compressed_size; // compressed size 4 bytes
  266. unsigned long uncompressed_size; // uncompressed size 4 bytes
  267. unsigned long size_filename; // filename length 2 bytes
  268. unsigned long size_file_extra; // extra field length 2 bytes
  269. unsigned long size_file_comment; // file comment length 2 bytes
  270. unsigned long disk_num_start; // disk number start 2 bytes
  271. unsigned long internal_fa; // internal file attributes 2 bytes
  272. unsigned long external_fa; // external file attributes 4 bytes
  273. tm_unz tmu_date;
  274. } unz_file_info;
  275. #define UNZ_OK (0)
  276. #define UNZ_END_OF_LIST_OF_FILE (-100)
  277. #define UNZ_ERRNO (Z_ERRNO)
  278. #define UNZ_EOF (0)
  279. #define UNZ_PARAMERROR (-102)
  280. #define UNZ_BADZIPFILE (-103)
  281. #define UNZ_INTERNALERROR (-104)
  282. #define UNZ_CRCERROR (-105)
  283. #define ZLIB_VERSION "1.1.3"
  284. // Allowed flush values; see deflate() for details
  285. #define Z_NO_FLUSH 0
  286. #define Z_SYNC_FLUSH 2
  287. #define Z_FULL_FLUSH 3
  288. #define Z_FINISH 4
  289. // compression levels
  290. #define Z_NO_COMPRESSION 0
  291. #define Z_BEST_SPEED 1
  292. #define Z_BEST_COMPRESSION 9
  293. #define Z_DEFAULT_COMPRESSION (-1)
  294. // compression strategy; see deflateInit2() for details
  295. #define Z_FILTERED 1
  296. #define Z_HUFFMAN_ONLY 2
  297. #define Z_DEFAULT_STRATEGY 0
  298. // Possible values of the data_type field
  299. #define Z_BINARY 0
  300. #define Z_ASCII 1
  301. #define Z_UNKNOWN 2
  302. // The deflate compression method (the only one supported in this version)
  303. #define Z_DEFLATED 8
  304. // for initializing zalloc, zfree, opaque
  305. #define Z_NULL 0
  306. // case sensitivity when searching for filenames
  307. #define CASE_SENSITIVE 1
  308. #define CASE_INSENSITIVE 2
  309. // Return codes for the compression/decompression functions. Negative
  310. // values are errors, positive values are used for special but normal events.
  311. #define Z_OK 0
  312. #define Z_STREAM_END 1
  313. #define Z_NEED_DICT 2
  314. #define Z_ERRNO (-1)
  315. #define Z_STREAM_ERROR (-2)
  316. #define Z_DATA_ERROR (-3)
  317. #define Z_MEM_ERROR (-4)
  318. #define Z_BUF_ERROR (-5)
  319. #define Z_VERSION_ERROR (-6)
  320. // Basic data types
  321. typedef unsigned char Byte; // 8 bits
  322. typedef unsigned int uInt; // 16 bits or more
  323. typedef unsigned long uLong; // 32 bits or more
  324. typedef void *voidpf;
  325. typedef void *voidp;
  326. typedef long z_off_t;
  327. typedef voidpf (*alloc_func) (voidpf opaque, uInt items, uInt size);
  328. typedef void (*free_func) (voidpf opaque, voidpf address);
  329. struct internal_state;
  330. typedef struct z_stream_s {
  331. Byte *next_in; // next input byte
  332. uInt avail_in; // number of bytes available at next_in
  333. uLong total_in; // total nb of input bytes read so far
  334. Byte *next_out; // next output byte should be put there
  335. uInt avail_out; // remaining free space at next_out
  336. uLong total_out; // total nb of bytes output so far
  337. char *msg; // last error message, NULL if no error
  338. struct internal_state *state; // not visible by applications
  339. alloc_func zalloc; // used to allocate the internal state
  340. free_func zfree; // used to free the internal state
  341. voidpf opaque; // private data object passed to zalloc and zfree
  342. int data_type; // best guess about the data type: ascii or binary
  343. uLong adler; // adler32 value of the uncompressed data
  344. uLong reserved; // reserved for future use
  345. } z_stream;
  346. typedef z_stream *z_streamp;
  347. // The application must update next_in and avail_in when avail_in has
  348. // dropped to zero. It must update next_out and avail_out when avail_out
  349. // has dropped to zero. The application must initialize zalloc, zfree and
  350. // opaque before calling the init function. All other fields are set by the
  351. // compression library and must not be updated by the application.
  352. //
  353. // The opaque value provided by the application will be passed as the first
  354. // parameter for calls of zalloc and zfree. This can be useful for custom
  355. // memory management. The compression library attaches no meaning to the
  356. // opaque value.
  357. //
  358. // zalloc must return Z_NULL if there is not enough memory for the object.
  359. // If zlib is used in a multi-threaded application, zalloc and zfree must be
  360. // thread safe.
  361. //
  362. // The fields total_in and total_out can be used for statistics or
  363. // progress reports. After compression, total_in holds the total size of
  364. // the uncompressed data and may be saved for use in the decompressor
  365. // (particularly if the decompressor wants to decompress everything in
  366. // a single step).
  367. //
  368. // basic functions
  369. const char *zlibVersion ();
  370. // The application can compare zlibVersion and ZLIB_VERSION for consistency.
  371. // If the first character differs, the library code actually used is
  372. // not compatible with the zlib.h header file used by the application.
  373. // This check is automatically made by inflateInit.
  374. int inflate (z_streamp strm, int flush);
  375. //
  376. // inflate decompresses as much data as possible, and stops when the input
  377. // buffer becomes empty or the output buffer becomes full. It may some
  378. // introduce some output latency (reading input without producing any output)
  379. // except when forced to flush.
  380. //
  381. // The detailed semantics are as follows. inflate performs one or both of the
  382. // following actions:
  383. //
  384. // - Decompress more input starting at next_in and update next_in and avail_in
  385. // accordingly. If not all input can be processed (because there is not
  386. // enough room in the output buffer), next_in is updated and processing
  387. // will resume at this point for the next call of inflate().
  388. //
  389. // - Provide more output starting at next_out and update next_out and avail_out
  390. // accordingly. inflate() provides as much output as possible, until there
  391. // is no more input data or no more space in the output buffer (see below
  392. // about the flush parameter).
  393. //
  394. // Before the call of inflate(), the application should ensure that at least
  395. // one of the actions is possible, by providing more input and/or consuming
  396. // more output, and updating the next_* and avail_* values accordingly.
  397. // The application can consume the uncompressed output when it wants, for
  398. // example when the output buffer is full (avail_out == 0), or after each
  399. // call of inflate(). If inflate returns Z_OK and with zero avail_out, it
  400. // must be called again after making room in the output buffer because there
  401. // might be more output pending.
  402. //
  403. // If the parameter flush is set to Z_SYNC_FLUSH, inflate flushes as much
  404. // output as possible to the output buffer. The flushing behavior of inflate is
  405. // not specified for values of the flush parameter other than Z_SYNC_FLUSH
  406. // and Z_FINISH, but the current implementation actually flushes as much output
  407. // as possible anyway.
  408. //
  409. // inflate() should normally be called until it returns Z_STREAM_END or an
  410. // error. However if all decompression is to be performed in a single step
  411. // (a single call of inflate), the parameter flush should be set to
  412. // Z_FINISH. In this case all pending input is processed and all pending
  413. // output is flushed; avail_out must be large enough to hold all the
  414. // uncompressed data. (The size of the uncompressed data may have been saved
  415. // by the compressor for this purpose.) The next operation on this stream must
  416. // be inflateEnd to deallocate the decompression state. The use of Z_FINISH
  417. // is never required, but can be used to inform inflate that a faster routine
  418. // may be used for the single inflate() call.
  419. //
  420. // If a preset dictionary is needed at this point (see inflateSetDictionary
  421. // below), inflate sets strm-adler to the adler32 checksum of the
  422. // dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise
  423. // it sets strm->adler to the adler32 checksum of all output produced
  424. // so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or
  425. // an error code as described below. At the end of the stream, inflate()
  426. // checks that its computed adler32 checksum is equal to that saved by the
  427. // compressor and returns Z_STREAM_END only if the checksum is correct.
  428. //
  429. // inflate() returns Z_OK if some progress has been made (more input processed
  430. // or more output produced), Z_STREAM_END if the end of the compressed data has
  431. // been reached and all uncompressed output has been produced, Z_NEED_DICT if a
  432. // preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
  433. // corrupted (input stream not conforming to the zlib format or incorrect
  434. // adler32 checksum), Z_STREAM_ERROR if the stream structure was inconsistent
  435. // (for example if next_in or next_out was NULL), Z_MEM_ERROR if there was not
  436. // enough memory, Z_BUF_ERROR if no progress is possible or if there was not
  437. // enough room in the output buffer when Z_FINISH is used. In the Z_DATA_ERROR
  438. // case, the application may then call inflateSync to look for a good
  439. // compression block.
  440. //
  441. int inflateEnd (z_streamp strm);
  442. //
  443. // All dynamically allocated data structures for this stream are freed.
  444. // This function discards any unprocessed input and does not flush any
  445. // pending output.
  446. //
  447. // inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
  448. // was inconsistent. In the error case, msg may be set but then points to a
  449. // static string (which must not be deallocated).
  450. // Advanced functions
  451. // The following functions are needed only in some special applications.
  452. int inflateSetDictionary (z_streamp strm,
  453. const Byte *dictionary,
  454. uInt dictLength);
  455. //
  456. // Initializes the decompression dictionary from the given uncompressed byte
  457. // sequence. This function must be called immediately after a call of inflate
  458. // if this call returned Z_NEED_DICT. The dictionary chosen by the compressor
  459. // can be determined from the Adler32 value returned by this call of
  460. // inflate. The compressor and decompressor must use exactly the same
  461. // dictionary.
  462. //
  463. // inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
  464. // parameter is invalid (such as NULL dictionary) or the stream state is
  465. // inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
  466. // expected one (incorrect Adler32 value). inflateSetDictionary does not
  467. // perform any decompression: this will be done by subsequent calls of
  468. // inflate().
  469. int inflateSync (z_streamp strm);
  470. //
  471. // Skips invalid compressed data until a full flush point can be found, or until all
  472. // available input is skipped. No output is provided.
  473. //
  474. // inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
  475. // if no more input was provided, Z_DATA_ERROR if no flush point has been found,
  476. // or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
  477. // case, the application may save the current current value of total_in which
  478. // indicates where valid compressed data was found. In the error case, the
  479. // application may repeatedly call inflateSync, providing more input each time,
  480. // until success or end of the input data.
  481. int inflateReset (z_streamp strm);
  482. // This function is equivalent to inflateEnd followed by inflateInit,
  483. // but does not free and reallocate all the internal decompression state.
  484. // The stream will keep attributes that may have been set by inflateInit2.
  485. //
  486. // inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  487. // stream state was inconsistent (such as zalloc or state being NULL).
  488. //
  489. // checksum functions
  490. // These functions are not related to compression but are exported
  491. // anyway because they might be useful in applications using the
  492. // compression library.
  493. uLong adler32 (uLong adler, const Byte *buf, uInt len);
  494. // Update a running Adler-32 checksum with the bytes buf[0..len-1] and
  495. // return the updated checksum. If buf is NULL, this function returns
  496. // the required initial value for the checksum.
  497. // An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
  498. // much faster. Usage example:
  499. //
  500. // uLong adler = adler32(0L, Z_NULL, 0);
  501. //
  502. // while (read_buffer(buffer, length) != EOF) {
  503. // adler = adler32(adler, buffer, length);
  504. // }
  505. // if (adler != original_adler) error();
  506. uLong ucrc32 (uLong crc, const Byte *buf, uInt len);
  507. // Update a running crc with the bytes buf[0..len-1] and return the updated
  508. // crc. If buf is NULL, this function returns the required initial value
  509. // for the crc. Pre- and post-conditioning (one's complement) is performed
  510. // within this function so it shouldn't be done by the application.
  511. // Usage example:
  512. //
  513. // uLong crc = crc32(0L, Z_NULL, 0);
  514. //
  515. // while (read_buffer(buffer, length) != EOF) {
  516. // crc = crc32(crc, buffer, length);
  517. // }
  518. // if (crc != original_crc) error();
  519. const char *zError (int err);
  520. int inflateSyncPoint (z_streamp z);
  521. const uLong *get_crc_table (void);
  522. typedef unsigned char uch;
  523. typedef uch uchf;
  524. typedef unsigned short ush;
  525. typedef ush ushf;
  526. typedef unsigned long ulg;
  527. const char * const z_errmsg[10] = { // indexed by 2-zlib_error
  528. "need dictionary", // Z_NEED_DICT 2
  529. "stream end", // Z_STREAM_END 1
  530. "", // Z_OK 0
  531. "file error", // Z_ERRNO (-1)
  532. "stream error", // Z_STREAM_ERROR (-2)
  533. "data error", // Z_DATA_ERROR (-3)
  534. "insufficient memory", // Z_MEM_ERROR (-4)
  535. "buffer error", // Z_BUF_ERROR (-5)
  536. "incompatible version",// Z_VERSION_ERROR (-6)
  537. ""};
  538. #define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
  539. #define ERR_RETURN(strm,err) \
  540. return (strm->msg = (char*)ERR_MSG(err), (err))
  541. // To be used only when the state is known to be valid
  542. // common constants
  543. #define STORED_BLOCK 0
  544. #define STATIC_TREES 1
  545. #define DYN_TREES 2
  546. // The three kinds of block type
  547. #define MIN_MATCH 3
  548. #define MAX_MATCH 258
  549. // The minimum and maximum match lengths
  550. #define PRESET_DICT 0x20 // preset dictionary flag in zlib header
  551. // target dependencies
  552. #define OS_CODE 0x0b // Window 95 & Windows NT
  553. // functions
  554. #define zmemzero(dest, len) memset(dest, 0, len)
  555. // Diagnostic functions
  556. #undef Assert
  557. #undef Trace
  558. #undef Tracev
  559. #undef Tracevv
  560. #undef Tracec
  561. #undef Tracecv
  562. #ifdef DEBUG
  563. int z_verbose = 0;
  564. void z_error (char *m) {fprintf(stderr, "%s\n", m); exit(1);}
  565. #define Assert(cond,msg) {if(!(cond)) z_error(msg);}
  566. #define Trace(x) {if (z_verbose>=0) fprintf x ;}
  567. #define Tracev(x) {if (z_verbose>0) fprintf x ;}
  568. #define Tracevv(x) {if (z_verbose>1) fprintf x ;}
  569. #define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;}
  570. #define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;}
  571. #else
  572. #ifndef __noop
  573. #if _MSC_VER < 1300
  574. #define __noop ((void)0)
  575. #endif
  576. #endif
  577. #define Assert(cond,msg) __noop
  578. #define Trace(x) __noop
  579. #define Tracev(x) __noop
  580. #define Tracevv(x) __noop
  581. #define Tracec(c,x) __noop
  582. #define Tracecv(c,x) __noop
  583. #endif
  584. typedef uLong (*check_func) (uLong check, const Byte *buf, uInt len);
  585. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size);
  586. void zcfree (voidpf opaque, voidpf ptr);
  587. #define ZALLOC(strm, items, size) \
  588. (*((strm)->zalloc))((strm)->opaque, (items), (size))
  589. #define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
  590. //void ZFREE(z_streamp strm,voidpf addr)
  591. //{ *((strm)->zfree))((strm)->opaque, addr);
  592. //}
  593. #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
  594. // Huffman code lookup table entry--this entry is four bytes for machines
  595. // that have 16-bit pointers (e.g. PC's in the small or medium model).
  596. typedef struct inflate_huft_s inflate_huft;
  597. struct inflate_huft_s {
  598. union {
  599. struct {
  600. Byte Exop; // number of extra bits or operation
  601. Byte Bits; // number of bits in this code or subcode
  602. } what;
  603. uInt pad; // pad structure to a power of 2 (4 bytes for
  604. } word; // 16-bit, 8 bytes for 32-bit int's)
  605. uInt base; // literal, length base, distance base, or table offset
  606. };
  607. // Maximum size of dynamic tree. The maximum found in a long but non-
  608. // exhaustive search was 1004 huft structures (850 for length/literals
  609. // and 154 for distances, the latter actually the result of an
  610. // exhaustive search). The actual maximum is not known, but the
  611. // value below is more than safe.
  612. #define MANY 1440
  613. int inflate_trees_bits (
  614. uInt *, // 19 code lengths
  615. uInt *, // bits tree desired/actual depth
  616. inflate_huft * *, // bits tree result
  617. inflate_huft *, // space for trees
  618. z_streamp); // for messages
  619. int inflate_trees_dynamic (
  620. uInt, // number of literal/length codes
  621. uInt, // number of distance codes
  622. uInt *, // that many (total) code lengths
  623. uInt *, // literal desired/actual bit depth
  624. uInt *, // distance desired/actual bit depth
  625. inflate_huft * *, // literal/length tree result
  626. inflate_huft * *, // distance tree result
  627. inflate_huft *, // space for trees
  628. z_streamp); // for messages
  629. int inflate_trees_fixed (
  630. uInt *, // literal desired/actual bit depth
  631. uInt *, // distance desired/actual bit depth
  632. const inflate_huft * *, // literal/length tree result
  633. const inflate_huft * *, // distance tree result
  634. z_streamp); // for memory allocation
  635. struct inflate_blocks_state;
  636. typedef struct inflate_blocks_state inflate_blocks_statef;
  637. inflate_blocks_statef * inflate_blocks_new (
  638. z_streamp z,
  639. check_func c, // check function
  640. uInt w); // window size
  641. int inflate_blocks (
  642. inflate_blocks_statef *,
  643. z_streamp ,
  644. int); // initial return code
  645. void inflate_blocks_reset (
  646. inflate_blocks_statef *,
  647. z_streamp ,
  648. uLong *); // check value on output
  649. int inflate_blocks_free (
  650. inflate_blocks_statef *,
  651. z_streamp);
  652. void inflate_set_dictionary (
  653. inflate_blocks_statef *s,
  654. const Byte *d, // dictionary
  655. uInt n); // dictionary length
  656. int inflate_blocks_sync_point (
  657. inflate_blocks_statef *s);
  658. struct inflate_codes_state;
  659. typedef struct inflate_codes_state inflate_codes_statef;
  660. inflate_codes_statef *inflate_codes_new (
  661. uInt, uInt,
  662. const inflate_huft *, const inflate_huft *,
  663. z_streamp );
  664. int inflate_codes (
  665. inflate_blocks_statef *,
  666. z_streamp ,
  667. int);
  668. void inflate_codes_free (
  669. inflate_codes_statef *,
  670. z_streamp );
  671. typedef enum {
  672. IBM_TYPE, // get type bits (3, including end bit)
  673. IBM_LENS, // get lengths for stored
  674. IBM_STORED, // processing stored block
  675. IBM_TABLE, // get table lengths
  676. IBM_BTREE, // get bit lengths tree for a dynamic block
  677. IBM_DTREE, // get length, distance trees for a dynamic block
  678. IBM_CODES, // processing fixed or dynamic block
  679. IBM_DRY, // output remaining window bytes
  680. IBM_DONE, // finished last block, done
  681. IBM_BAD} // got a data error--stuck here
  682. inflate_block_mode;
  683. // inflate blocks semi-private state
  684. struct inflate_blocks_state {
  685. // mode
  686. inflate_block_mode mode; // current inflate_block mode
  687. // mode dependent information
  688. union {
  689. uInt left; // if STORED, bytes left to copy
  690. struct {
  691. uInt table; // table lengths (14 bits)
  692. uInt index; // index into blens (or border)
  693. uInt *blens; // bit lengths of codes
  694. uInt bb; // bit length tree depth
  695. inflate_huft *tb; // bit length decoding tree
  696. } trees; // if DTREE, decoding info for trees
  697. struct {
  698. inflate_codes_statef
  699. *codes;
  700. } decode; // if CODES, current state
  701. } sub; // submode
  702. uInt last; // true if this block is the last block
  703. // mode independent information
  704. uInt bitk; // bits in bit buffer
  705. uLong bitb; // bit buffer
  706. inflate_huft *hufts; // single malloc for tree space
  707. Byte *window; // sliding window
  708. Byte *end; // one byte after sliding window
  709. Byte *read; // window read pointer
  710. Byte *write; // window write pointer
  711. check_func checkfn; // check function
  712. uLong check; // check on output
  713. };
  714. // defines for inflate input/output
  715. // update pointers and return
  716. #define UPDBITS {s->bitb=b;s->bitk=k;}
  717. #define UPDIN {z->avail_in=n;z->total_in+=(uLong)(p-z->next_in);z->next_in=p;}
  718. #define UPDOUT {s->write=q;}
  719. #define UPDATE {UPDBITS UPDIN UPDOUT}
  720. #define LEAVE {UPDATE return inflate_flush(s,z,r);}
  721. // get bytes and bits
  722. #define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
  723. #define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
  724. #define NEXTBYTE (n--,*p++)
  725. #define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  726. #define DUMPBITS(j) {b>>=(j);k-=(j);}
  727. // output bytes
  728. #define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
  729. #define LOADOUT {q=s->write;m=(uInt)WAVAIL;m;}
  730. #define WRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
  731. #define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
  732. #define NEEDOUT {if(m==0){WRAP if(m==0){FLUSH WRAP if(m==0) LEAVE}}r=Z_OK;}
  733. #define OUTBYTE(a) {*q++=(Byte)(a);m--;}
  734. // load local pointers
  735. #define LOAD {LOADIN LOADOUT}
  736. // masks for lower bits (size given to avoid silly warnings with Visual C++)
  737. // And'ing with mask[n] masks the lower n bits
  738. const uInt inflate_mask[17] = {
  739. 0x0000,
  740. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  741. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  742. };
  743. // copy as much as possible from the sliding window to the output area
  744. int inflate_flush (inflate_blocks_statef *, z_streamp, int);
  745. int inflate_fast (uInt, uInt, const inflate_huft *, const inflate_huft *, inflate_blocks_statef *, z_streamp );
  746. const uInt fixed_bl = 9;
  747. const uInt fixed_bd = 5;
  748. const inflate_huft fixed_tl[] = {
  749. {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  750. {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},192},
  751. {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},160},
  752. {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},224},
  753. {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},144},
  754. {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},208},
  755. {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},176},
  756. {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},240},
  757. {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  758. {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},200},
  759. {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},168},
  760. {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},232},
  761. {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},152},
  762. {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},216},
  763. {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},184},
  764. {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},248},
  765. {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  766. {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},196},
  767. {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},164},
  768. {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},228},
  769. {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},148},
  770. {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},212},
  771. {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},180},
  772. {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},244},
  773. {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  774. {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},204},
  775. {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},172},
  776. {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},236},
  777. {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},156},
  778. {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},220},
  779. {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},188},
  780. {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},252},
  781. {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  782. {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},194},
  783. {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},162},
  784. {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},226},
  785. {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},146},
  786. {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},210},
  787. {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},178},
  788. {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},242},
  789. {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  790. {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},202},
  791. {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},170},
  792. {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},234},
  793. {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},154},
  794. {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},218},
  795. {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},186},
  796. {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},250},
  797. {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  798. {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},198},
  799. {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},166},
  800. {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},230},
  801. {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},150},
  802. {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},214},
  803. {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},182},
  804. {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},246},
  805. {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  806. {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},206},
  807. {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},174},
  808. {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},238},
  809. {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},158},
  810. {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},222},
  811. {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},190},
  812. {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},254},
  813. {{{96,7}},256}, {{{0,8}},80}, {{{0,8}},16}, {{{84,8}},115},
  814. {{{82,7}},31}, {{{0,8}},112}, {{{0,8}},48}, {{{0,9}},193},
  815. {{{80,7}},10}, {{{0,8}},96}, {{{0,8}},32}, {{{0,9}},161},
  816. {{{0,8}},0}, {{{0,8}},128}, {{{0,8}},64}, {{{0,9}},225},
  817. {{{80,7}},6}, {{{0,8}},88}, {{{0,8}},24}, {{{0,9}},145},
  818. {{{83,7}},59}, {{{0,8}},120}, {{{0,8}},56}, {{{0,9}},209},
  819. {{{81,7}},17}, {{{0,8}},104}, {{{0,8}},40}, {{{0,9}},177},
  820. {{{0,8}},8}, {{{0,8}},136}, {{{0,8}},72}, {{{0,9}},241},
  821. {{{80,7}},4}, {{{0,8}},84}, {{{0,8}},20}, {{{85,8}},227},
  822. {{{83,7}},43}, {{{0,8}},116}, {{{0,8}},52}, {{{0,9}},201},
  823. {{{81,7}},13}, {{{0,8}},100}, {{{0,8}},36}, {{{0,9}},169},
  824. {{{0,8}},4}, {{{0,8}},132}, {{{0,8}},68}, {{{0,9}},233},
  825. {{{80,7}},8}, {{{0,8}},92}, {{{0,8}},28}, {{{0,9}},153},
  826. {{{84,7}},83}, {{{0,8}},124}, {{{0,8}},60}, {{{0,9}},217},
  827. {{{82,7}},23}, {{{0,8}},108}, {{{0,8}},44}, {{{0,9}},185},
  828. {{{0,8}},12}, {{{0,8}},140}, {{{0,8}},76}, {{{0,9}},249},
  829. {{{80,7}},3}, {{{0,8}},82}, {{{0,8}},18}, {{{85,8}},163},
  830. {{{83,7}},35}, {{{0,8}},114}, {{{0,8}},50}, {{{0,9}},197},
  831. {{{81,7}},11}, {{{0,8}},98}, {{{0,8}},34}, {{{0,9}},165},
  832. {{{0,8}},2}, {{{0,8}},130}, {{{0,8}},66}, {{{0,9}},229},
  833. {{{80,7}},7}, {{{0,8}},90}, {{{0,8}},26}, {{{0,9}},149},
  834. {{{84,7}},67}, {{{0,8}},122}, {{{0,8}},58}, {{{0,9}},213},
  835. {{{82,7}},19}, {{{0,8}},106}, {{{0,8}},42}, {{{0,9}},181},
  836. {{{0,8}},10}, {{{0,8}},138}, {{{0,8}},74}, {{{0,9}},245},
  837. {{{80,7}},5}, {{{0,8}},86}, {{{0,8}},22}, {{{192,8}},0},
  838. {{{83,7}},51}, {{{0,8}},118}, {{{0,8}},54}, {{{0,9}},205},
  839. {{{81,7}},15}, {{{0,8}},102}, {{{0,8}},38}, {{{0,9}},173},
  840. {{{0,8}},6}, {{{0,8}},134}, {{{0,8}},70}, {{{0,9}},237},
  841. {{{80,7}},9}, {{{0,8}},94}, {{{0,8}},30}, {{{0,9}},157},
  842. {{{84,7}},99}, {{{0,8}},126}, {{{0,8}},62}, {{{0,9}},221},
  843. {{{82,7}},27}, {{{0,8}},110}, {{{0,8}},46}, {{{0,9}},189},
  844. {{{0,8}},14}, {{{0,8}},142}, {{{0,8}},78}, {{{0,9}},253},
  845. {{{96,7}},256}, {{{0,8}},81}, {{{0,8}},17}, {{{85,8}},131},
  846. {{{82,7}},31}, {{{0,8}},113}, {{{0,8}},49}, {{{0,9}},195},
  847. {{{80,7}},10}, {{{0,8}},97}, {{{0,8}},33}, {{{0,9}},163},
  848. {{{0,8}},1}, {{{0,8}},129}, {{{0,8}},65}, {{{0,9}},227},
  849. {{{80,7}},6}, {{{0,8}},89}, {{{0,8}},25}, {{{0,9}},147},
  850. {{{83,7}},59}, {{{0,8}},121}, {{{0,8}},57}, {{{0,9}},211},
  851. {{{81,7}},17}, {{{0,8}},105}, {{{0,8}},41}, {{{0,9}},179},
  852. {{{0,8}},9}, {{{0,8}},137}, {{{0,8}},73}, {{{0,9}},243},
  853. {{{80,7}},4}, {{{0,8}},85}, {{{0,8}},21}, {{{80,8}},258},
  854. {{{83,7}},43}, {{{0,8}},117}, {{{0,8}},53}, {{{0,9}},203},
  855. {{{81,7}},13}, {{{0,8}},101}, {{{0,8}},37}, {{{0,9}},171},
  856. {{{0,8}},5}, {{{0,8}},133}, {{{0,8}},69}, {{{0,9}},235},
  857. {{{80,7}},8}, {{{0,8}},93}, {{{0,8}},29}, {{{0,9}},155},
  858. {{{84,7}},83}, {{{0,8}},125}, {{{0,8}},61}, {{{0,9}},219},
  859. {{{82,7}},23}, {{{0,8}},109}, {{{0,8}},45}, {{{0,9}},187},
  860. {{{0,8}},13}, {{{0,8}},141}, {{{0,8}},77}, {{{0,9}},251},
  861. {{{80,7}},3}, {{{0,8}},83}, {{{0,8}},19}, {{{85,8}},195},
  862. {{{83,7}},35}, {{{0,8}},115}, {{{0,8}},51}, {{{0,9}},199},
  863. {{{81,7}},11}, {{{0,8}},99}, {{{0,8}},35}, {{{0,9}},167},
  864. {{{0,8}},3}, {{{0,8}},131}, {{{0,8}},67}, {{{0,9}},231},
  865. {{{80,7}},7}, {{{0,8}},91}, {{{0,8}},27}, {{{0,9}},151},
  866. {{{84,7}},67}, {{{0,8}},123}, {{{0,8}},59}, {{{0,9}},215},
  867. {{{82,7}},19}, {{{0,8}},107}, {{{0,8}},43}, {{{0,9}},183},
  868. {{{0,8}},11}, {{{0,8}},139}, {{{0,8}},75}, {{{0,9}},247},
  869. {{{80,7}},5}, {{{0,8}},87}, {{{0,8}},23}, {{{192,8}},0},
  870. {{{83,7}},51}, {{{0,8}},119}, {{{0,8}},55}, {{{0,9}},207},
  871. {{{81,7}},15}, {{{0,8}},103}, {{{0,8}},39}, {{{0,9}},175},
  872. {{{0,8}},7}, {{{0,8}},135}, {{{0,8}},71}, {{{0,9}},239},
  873. {{{80,7}},9}, {{{0,8}},95}, {{{0,8}},31}, {{{0,9}},159},
  874. {{{84,7}},99}, {{{0,8}},127}, {{{0,8}},63}, {{{0,9}},223},
  875. {{{82,7}},27}, {{{0,8}},111}, {{{0,8}},47}, {{{0,9}},191},
  876. {{{0,8}},15}, {{{0,8}},143}, {{{0,8}},79}, {{{0,9}},255}
  877. };
  878. const inflate_huft fixed_td[] = {
  879. {{{80,5}},1}, {{{87,5}},257}, {{{83,5}},17}, {{{91,5}},4097},
  880. {{{81,5}},5}, {{{89,5}},1025}, {{{85,5}},65}, {{{93,5}},16385},
  881. {{{80,5}},3}, {{{88,5}},513}, {{{84,5}},33}, {{{92,5}},8193},
  882. {{{82,5}},9}, {{{90,5}},2049}, {{{86,5}},129}, {{{192,5}},24577},
  883. {{{80,5}},2}, {{{87,5}},385}, {{{83,5}},25}, {{{91,5}},6145},
  884. {{{81,5}},7}, {{{89,5}},1537}, {{{85,5}},97}, {{{93,5}},24577},
  885. {{{80,5}},4}, {{{88,5}},769}, {{{84,5}},49}, {{{92,5}},12289},
  886. {{{82,5}},13}, {{{90,5}},3073}, {{{86,5}},193}, {{{192,5}},24577}
  887. };
  888. // copy as much as possible from the sliding window to the output area
  889. int inflate_flush(inflate_blocks_statef *s,z_streamp z,int r)
  890. {
  891. uInt n;
  892. Byte *p;
  893. Byte *q;
  894. // local copies of source and destination pointers
  895. p = z->next_out;
  896. q = s->read;
  897. // compute number of bytes to copy as far as end of window
  898. n = (uInt)((q <= s->write ? s->write : s->end) - q);
  899. if (n > z->avail_out) n = z->avail_out;
  900. if (n && r == Z_BUF_ERROR) r = Z_OK;
  901. // update counters
  902. z->avail_out -= n;
  903. z->total_out += n;
  904. // update check information
  905. if (s->checkfn != Z_NULL)
  906. z->adler = s->check = (*s->checkfn)(s->check, q, n);
  907. // copy as far as end of window
  908. if (n!=0) // check for n!=0 to avoid waking up CodeGuard
  909. { memcpy(p, q, n);
  910. p += n;
  911. q += n;
  912. }
  913. // see if more to copy at beginning of window
  914. if (q == s->end)
  915. {
  916. // wrap pointers
  917. q = s->window;
  918. if (s->write == s->end)
  919. s->write = s->window;
  920. // compute bytes to copy
  921. n = (uInt)(s->write - q);
  922. if (n > z->avail_out) n = z->avail_out;
  923. if (n && r == Z_BUF_ERROR) r = Z_OK;
  924. // update counters
  925. z->avail_out -= n;
  926. z->total_out += n;
  927. // update check information
  928. if (s->checkfn != Z_NULL)
  929. z->adler = s->check = (*s->checkfn)(s->check, q, n);
  930. // copy
  931. memcpy(p, q, n);
  932. p += n;
  933. q += n;
  934. }
  935. // update pointers
  936. z->next_out = p;
  937. s->read = q;
  938. // done
  939. return r;
  940. }
  941. // simplify the use of the inflate_huft type with some defines
  942. #define exop word.what.Exop
  943. #define bits word.what.Bits
  944. typedef enum { // waiting for "i:"=input, "o:"=output, "x:"=nothing
  945. START, // x: set up for LEN
  946. LEN, // i: get length/literal/eob next
  947. LENEXT, // i: getting length extra (have base)
  948. DIST, // i: get distance next
  949. DISTEXT, // i: getting distance extra
  950. COPY, // o: copying bytes in window, waiting for space
  951. LIT, // o: got literal, waiting for output space
  952. WASH, // o: got eob, possibly still output waiting
  953. END, // x: got eob and all data flushed
  954. BADCODE} // x: got error
  955. inflate_codes_mode;
  956. // inflate codes private state
  957. struct inflate_codes_state {
  958. // mode
  959. inflate_codes_mode mode; // current inflate_codes mode
  960. // mode dependent information
  961. uInt len;
  962. union {
  963. struct {
  964. const inflate_huft *tree; // pointer into tree
  965. uInt need; // bits needed
  966. } code; // if LEN or DIST, where in tree
  967. uInt lit; // if LIT, literal
  968. struct {
  969. uInt get; // bits to get for extra
  970. uInt dist; // distance back to copy from
  971. } copy; // if EXT or COPY, where and how much
  972. } sub; // submode
  973. // mode independent information
  974. Byte lbits; // ltree bits decoded per branch
  975. Byte dbits; // dtree bits decoder per branch
  976. const inflate_huft *ltree; // literal/length/eob tree
  977. const inflate_huft *dtree; // distance tree
  978. };
  979. inflate_codes_statef *inflate_codes_new(
  980. uInt bl, uInt bd,
  981. const inflate_huft *tl,
  982. const inflate_huft *td, // need separate declaration for Borland C++
  983. z_streamp z)
  984. {
  985. inflate_codes_statef *c;
  986. if ((c = (inflate_codes_statef *)
  987. ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
  988. {
  989. c->mode = START;
  990. c->lbits = (Byte)bl;
  991. c->dbits = (Byte)bd;
  992. c->ltree = tl;
  993. c->dtree = td;
  994. Tracev((stderr, "inflate: codes new\n"));
  995. }
  996. return c;
  997. }
  998. int inflate_codes(inflate_blocks_statef *s, z_streamp z, int r)
  999. {
  1000. uInt j; // temporary storage
  1001. const inflate_huft *t; // temporary pointer
  1002. uInt e; // extra bits or operation
  1003. uLong b; // bit buffer
  1004. uInt k; // bits in bit buffer
  1005. Byte *p; // input data pointer
  1006. uInt n; // bytes available there
  1007. Byte *q; // output window write pointer
  1008. uInt m; // bytes to end of window or read pointer
  1009. Byte *f; // pointer to copy strings from
  1010. inflate_codes_statef *c = s->sub.decode.codes; // codes state
  1011. // copy input/output information to locals (UPDATE macro restores)
  1012. LOAD
  1013. // process input and output based on current state
  1014. for(;;) switch (c->mode)
  1015. { // waiting for "i:"=input, "o:"=output, "x:"=nothing
  1016. case START: // x: set up for LEN
  1017. #ifndef SLOW
  1018. if (m >= 258 && n >= 10)
  1019. {
  1020. UPDATE
  1021. r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
  1022. LOAD
  1023. if (r != Z_OK)
  1024. {
  1025. c->mode = r == Z_STREAM_END ? WASH : BADCODE;
  1026. break;
  1027. }
  1028. }
  1029. #endif // !SLOW
  1030. c->sub.code.need = c->lbits;
  1031. c->sub.code.tree = c->ltree;
  1032. c->mode = LEN;
  1033. case LEN: // i: get length/literal/eob next
  1034. j = c->sub.code.need;
  1035. NEEDBITS(j)
  1036. t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  1037. DUMPBITS(t->bits)
  1038. e = (uInt)(t->exop);
  1039. if (e == 0) // literal
  1040. {
  1041. c->sub.lit = t->base;
  1042. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  1043. "inflate: literal '%c'\n" :
  1044. "inflate: literal 0x%02x\n", t->base));
  1045. c->mode = LIT;
  1046. break;
  1047. }
  1048. if (e & 16) // length
  1049. {
  1050. c->sub.copy.get = e & 15;
  1051. c->len = t->base;
  1052. c->mode = LENEXT;
  1053. break;
  1054. }
  1055. if ((e & 64) == 0) // next table
  1056. {
  1057. c->sub.code.need = e;
  1058. c->sub.code.tree = t + t->base;
  1059. break;
  1060. }
  1061. if (e & 32) // end of block
  1062. {
  1063. Tracevv((stderr, "inflate: end of block\n"));
  1064. c->mode = WASH;
  1065. break;
  1066. }
  1067. c->mode = BADCODE; // invalid code
  1068. z->msg = (char*)"invalid literal/length code";
  1069. r = Z_DATA_ERROR;
  1070. LEAVE
  1071. case LENEXT: // i: getting length extra (have base)
  1072. j = c->sub.copy.get;
  1073. NEEDBITS(j)
  1074. c->len += (uInt)b & inflate_mask[j];
  1075. DUMPBITS(j)
  1076. c->sub.code.need = c->dbits;
  1077. c->sub.code.tree = c->dtree;
  1078. Tracevv((stderr, "inflate: length %u\n", c->len));
  1079. c->mode = DIST;
  1080. case DIST: // i: get distance next
  1081. j = c->sub.code.need;
  1082. NEEDBITS(j)
  1083. t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
  1084. DUMPBITS(t->bits)
  1085. e = (uInt)(t->exop);
  1086. if (e & 16) // distance
  1087. {
  1088. c->sub.copy.get = e & 15;
  1089. c->sub.copy.dist = t->base;
  1090. c->mode = DISTEXT;
  1091. break;
  1092. }
  1093. if ((e & 64) == 0) // next table
  1094. {
  1095. c->sub.code.need = e;
  1096. c->sub.code.tree = t + t->base;
  1097. break;
  1098. }
  1099. c->mode = BADCODE; // invalid code
  1100. z->msg = (char*)"invalid distance code";
  1101. r = Z_DATA_ERROR;
  1102. LEAVE
  1103. case DISTEXT: // i: getting distance extra
  1104. j = c->sub.copy.get;
  1105. NEEDBITS(j)
  1106. c->sub.copy.dist += (uInt)b & inflate_mask[j];
  1107. DUMPBITS(j)
  1108. Tracevv((stderr, "inflate: distance %u\n", c->sub.copy.dist));
  1109. c->mode = COPY;
  1110. case COPY: // o: copying bytes in window, waiting for space
  1111. f = (uInt)(q - s->window) < c->sub.copy.dist ?
  1112. s->end - (c->sub.copy.dist - (q - s->window)) :
  1113. q - c->sub.copy.dist;
  1114. while (c->len)
  1115. {
  1116. NEEDOUT
  1117. OUTBYTE(*f++)
  1118. if (f == s->end)
  1119. f = s->window;
  1120. c->len--;
  1121. }
  1122. c->mode = START;
  1123. break;
  1124. case LIT: // o: got literal, waiting for output space
  1125. NEEDOUT
  1126. OUTBYTE(c->sub.lit)
  1127. c->mode = START;
  1128. break;
  1129. case WASH: // o: got eob, possibly more output
  1130. if (k > 7) // return unused byte, if any
  1131. {
  1132. Assert(k < 16, "inflate_codes grabbed too many bytes");
  1133. k -= 8;
  1134. n++;
  1135. p--; // can always return one
  1136. }
  1137. FLUSH
  1138. if (s->read != s->write)
  1139. LEAVE
  1140. c->mode = END;
  1141. case END:
  1142. r = Z_STREAM_END;
  1143. LEAVE
  1144. case BADCODE: // x: got error
  1145. r = Z_DATA_ERROR;
  1146. LEAVE
  1147. default:
  1148. r = Z_STREAM_ERROR;
  1149. LEAVE
  1150. }
  1151. }
  1152. void inflate_codes_free(inflate_codes_statef *c,z_streamp z)
  1153. { ZFREE(z, c);
  1154. Tracev((stderr, "inflate: codes free\n"));
  1155. }
  1156. // infblock.c -- interpret and process block types to last block
  1157. // Copyright (C) 1995-1998 Mark Adler
  1158. // For conditions of distribution and use, see copyright notice in zlib.h
  1159. //struct inflate_codes_state {int dummy;}; // for buggy compilers
  1160. // Table for deflate from PKZIP's appnote.txt.
  1161. const uInt border[] = { // Order of the bit length code lengths
  1162. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  1163. //
  1164. // Notes beyond the 1.93a appnote.txt:
  1165. //
  1166. // 1. Distance pointers never point before the beginning of the output stream.
  1167. // 2. Distance pointers can point back across blocks, up to 32k away.
  1168. // 3. There is an implied maximum of 7 bits for the bit length table and
  1169. // 15 bits for the actual data.
  1170. // 4. If only one code exists, then it is encoded using one bit. (Zero
  1171. // would be more efficient, but perhaps a little confusing.) If two
  1172. // codes exist, they are coded using one bit each (0 and 1).
  1173. // 5. There is no way of sending zero distance codes--a dummy must be
  1174. // sent if there are none. (History: a pre 2.0 version of PKZIP would
  1175. // store blocks with no distance codes, but this was discovered to be
  1176. // too harsh a criterion.) Valid only for 1.93a. 2.04c does allow
  1177. // zero distance codes, which is sent as one code of zero bits in
  1178. // length.
  1179. // 6. There are up to 286 literal/length codes. Code 256 represents the
  1180. // end-of-block. Note however that the static length tree defines
  1181. // 288 codes just to fill out the Huffman codes. Codes 286 and 287
  1182. // cannot be used though, since there is no length base or extra bits
  1183. // defined for them. Similarily, there are up to 30 distance codes.
  1184. // However, static trees define 32 codes (all 5 bits) to fill out the
  1185. // Huffman codes, but the last two had better not show up in the data.
  1186. // 7. Unzip can check dynamic Huffman blocks for complete code sets.
  1187. // The exception is that a single code would not be complete (see #4).
  1188. // 8. The five bits following the block type is really the number of
  1189. // literal codes sent minus 257.
  1190. // 9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
  1191. // (1+6+6). Therefore, to output three times the length, you output
  1192. // three codes (1+1+1), whereas to output four times the same length,
  1193. // you only need two codes (1+3). Hmm.
  1194. //10. In the tree reconstruction algorithm, Code = Code + Increment
  1195. // only if BitLength(i) is not zero. (Pretty obvious.)
  1196. //11. Correction: 4 Bits: # of Bit Length codes - 4 (4 - 19)
  1197. //12. Note: length code 284 can represent 227-258, but length code 285
  1198. // really is 258. The last length deserves its own, short code
  1199. // since it gets used a lot in very redundant files. The length
  1200. // 258 is special since 258 - 3 (the min match length) is 255.
  1201. //13. The literal/length and distance code bit lengths are read as a
  1202. // single stream of lengths. It is possible (and advantageous) for
  1203. // a repeat code (16, 17, or 18) to go across the boundary between
  1204. // the two sets of lengths.
  1205. void inflate_blocks_reset(inflate_blocks_statef *s, z_streamp z, uLong *c)
  1206. {
  1207. if (c != Z_NULL)
  1208. *c = s->check;
  1209. if (s->mode == IBM_BTREE || s->mode == IBM_DTREE)
  1210. ZFREE(z, s->sub.trees.blens);
  1211. if (s->mode == IBM_CODES)
  1212. inflate_codes_free(s->sub.decode.codes, z);
  1213. s->mode = IBM_TYPE;
  1214. s->bitk = 0;
  1215. s->bitb = 0;
  1216. s->read = s->write = s->window;
  1217. if (s->checkfn != Z_NULL)
  1218. z->adler = s->check = (*s->checkfn)(0L, (const Byte *)Z_NULL, 0);
  1219. Tracev((stderr, "inflate: blocks reset\n"));
  1220. }
  1221. inflate_blocks_statef *inflate_blocks_new(z_streamp z, check_func c, uInt w)
  1222. {
  1223. inflate_blocks_statef *s;
  1224. if ((s = (inflate_blocks_statef *)ZALLOC
  1225. (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
  1226. return s;
  1227. if ((s->hufts =
  1228. (inflate_huft *)ZALLOC(z, sizeof(inflate_huft), MANY)) == Z_NULL)
  1229. {
  1230. ZFREE(z, s);
  1231. return Z_NULL;
  1232. }
  1233. if ((s->window = (Byte *)ZALLOC(z, 1, w)) == Z_NULL)
  1234. {
  1235. ZFREE(z, s->hufts);
  1236. ZFREE(z, s);
  1237. return Z_NULL;
  1238. }
  1239. s->end = s->window + w;
  1240. s->checkfn = c;
  1241. s->mode = IBM_TYPE;
  1242. Tracev((stderr, "inflate: blocks allocated\n"));
  1243. inflate_blocks_reset(s, z, Z_NULL);
  1244. return s;
  1245. }
  1246. int inflate_blocks(inflate_blocks_statef *s, z_streamp z, int r)
  1247. {
  1248. uInt t; // temporary storage
  1249. uLong b; // bit buffer
  1250. uInt k; // bits in bit buffer
  1251. Byte *p; // input data pointer
  1252. uInt n; // bytes available there
  1253. Byte *q; // output window write pointer
  1254. uInt m; // bytes to end of window or read pointer
  1255. // copy input/output information to locals (UPDATE macro restores)
  1256. LOAD
  1257. // process input based on current state
  1258. for(;;) switch (s->mode)
  1259. {
  1260. case IBM_TYPE:
  1261. NEEDBITS(3)
  1262. t = (uInt)b & 7;
  1263. s->last = t & 1;
  1264. switch (t >> 1)
  1265. {
  1266. case 0: // stored
  1267. Tracev((stderr, "inflate: stored block%s\n",
  1268. s->last ? " (last)" : ""));
  1269. DUMPBITS(3)
  1270. t = k & 7; // go to byte boundary
  1271. DUMPBITS(t)
  1272. s->mode = IBM_LENS; // get length of stored block
  1273. break;
  1274. case 1: // fixed
  1275. Tracev((stderr, "inflate: fixed codes block%s\n",
  1276. s->last ? " (last)" : ""));
  1277. {
  1278. uInt bl, bd;
  1279. const inflate_huft *tl, *td;
  1280. inflate_trees_fixed(&bl, &bd, &tl, &td, z);
  1281. s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
  1282. if (s->sub.decode.codes == Z_NULL)
  1283. {
  1284. r = Z_MEM_ERROR;
  1285. LEAVE
  1286. }
  1287. }
  1288. DUMPBITS(3)
  1289. s->mode = IBM_CODES;
  1290. break;
  1291. case 2: // dynamic
  1292. Tracev((stderr, "inflate: dynamic codes block%s\n",
  1293. s->last ? " (last)" : ""));
  1294. DUMPBITS(3)
  1295. s->mode = IBM_TABLE;
  1296. break;
  1297. case 3: // illegal
  1298. DUMPBITS(3)
  1299. s->mode = IBM_BAD;
  1300. z->msg = (char*)"invalid block type";
  1301. r = Z_DATA_ERROR;
  1302. LEAVE
  1303. }
  1304. break;
  1305. case IBM_LENS:
  1306. NEEDBITS(32)
  1307. if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
  1308. {
  1309. s->mode = IBM_BAD;
  1310. z->msg = (char*)"invalid stored block lengths";
  1311. r = Z_DATA_ERROR;
  1312. LEAVE
  1313. }
  1314. s->sub.left = (uInt)b & 0xffff;
  1315. b = k = 0; // dump bits
  1316. Tracev((stderr, "inflate: stored length %u\n", s->sub.left));
  1317. s->mode = s->sub.left ? IBM_STORED : (s->last ? IBM_DRY : IBM_TYPE);
  1318. break;
  1319. case IBM_STORED:
  1320. if (n == 0)
  1321. LEAVE
  1322. NEEDOUT
  1323. t = s->sub.left;
  1324. if (t > n) t = n;
  1325. if (t > m) t = m;
  1326. memcpy(q, p, t);
  1327. p += t; n -= t;
  1328. q += t; m -= t;
  1329. if ((s->sub.left -= t) != 0)
  1330. break;
  1331. Tracev((stderr, "inflate: stored end, %lu total out\n",
  1332. z->total_out + (q >= s->read ? q - s->read :
  1333. (s->end - s->read) + (q - s->window))));
  1334. s->mode = s->last ? IBM_DRY : IBM_TYPE;
  1335. break;
  1336. case IBM_TABLE:
  1337. NEEDBITS(14)
  1338. s->sub.trees.table = t = (uInt)b & 0x3fff;
  1339. // remove this section to workaround bug in pkzip
  1340. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
  1341. {
  1342. s->mode = IBM_BAD;
  1343. z->msg = (char*)"too many length or distance symbols";
  1344. r = Z_DATA_ERROR;
  1345. LEAVE
  1346. }
  1347. // end remove
  1348. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  1349. if ((s->sub.trees.blens = (uInt*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
  1350. {
  1351. r = Z_MEM_ERROR;
  1352. LEAVE
  1353. }
  1354. DUMPBITS(14)
  1355. s->sub.trees.index = 0;
  1356. Tracev((stderr, "inflate: table sizes ok\n"));
  1357. s->mode = IBM_BTREE;
  1358. case IBM_BTREE:
  1359. while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
  1360. {
  1361. NEEDBITS(3)
  1362. s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
  1363. DUMPBITS(3)
  1364. }
  1365. while (s->sub.trees.index < 19)
  1366. s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
  1367. s->sub.trees.bb = 7;
  1368. t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
  1369. &s->sub.trees.tb, s->hufts, z);
  1370. if (t != Z_OK)
  1371. {
  1372. ZFREE(z, s->sub.trees.blens);
  1373. r = t;
  1374. if (r == Z_DATA_ERROR)
  1375. s->mode = IBM_BAD;
  1376. LEAVE
  1377. }
  1378. s->sub.trees.index = 0;
  1379. Tracev((stderr, "inflate: bits tree ok\n"));
  1380. s->mode = IBM_DTREE;
  1381. case IBM_DTREE:
  1382. while (t = s->sub.trees.table,
  1383. s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
  1384. {
  1385. inflate_huft *h;
  1386. uInt i, j, c;
  1387. t = s->sub.trees.bb;
  1388. NEEDBITS(t)
  1389. h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
  1390. t = h->bits;
  1391. c = h->base;
  1392. if (c < 16)
  1393. {
  1394. DUMPBITS(t)
  1395. s->sub.trees.blens[s->sub.trees.index++] = c;
  1396. }
  1397. else // c == 16..18
  1398. {
  1399. i = c == 18 ? 7 : c - 14;
  1400. j = c == 18 ? 11 : 3;
  1401. NEEDBITS(t + i)
  1402. DUMPBITS(t)
  1403. j += (uInt)b & inflate_mask[i];
  1404. DUMPBITS(i)
  1405. i = s->sub.trees.index;
  1406. t = s->sub.trees.table;
  1407. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  1408. (c == 16 && i < 1))
  1409. {
  1410. ZFREE(z, s->sub.trees.blens);
  1411. s->mode = IBM_BAD;
  1412. z->msg = (char*)"invalid bit length repeat";
  1413. r = Z_DATA_ERROR;
  1414. LEAVE
  1415. }
  1416. c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
  1417. do {
  1418. s->sub.trees.blens[i++] = c;
  1419. } while (--j);
  1420. s->sub.trees.index = i;
  1421. }
  1422. }
  1423. s->sub.trees.tb = Z_NULL;
  1424. {
  1425. uInt bl, bd;
  1426. inflate_huft *tl, *td;
  1427. inflate_codes_statef *c;
  1428. bl = 9; // must be <= 9 for lookahead assumptions
  1429. bd = 6; // must be <= 9 for lookahead assumptions
  1430. t = s->sub.trees.table;
  1431. t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
  1432. s->sub.trees.blens, &bl, &bd, &tl, &td,
  1433. s->hufts, z);
  1434. ZFREE(z, s->sub.trees.blens);
  1435. if (t != Z_OK)
  1436. {
  1437. if (t == (uInt)Z_DATA_ERROR)
  1438. s->mode = IBM_BAD;
  1439. r = t;
  1440. LEAVE
  1441. }
  1442. Tracev((stderr, "inflate: trees ok\n"));
  1443. if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
  1444. {
  1445. r = Z_MEM_ERROR;
  1446. LEAVE
  1447. }
  1448. s->sub.decode.codes = c;
  1449. }
  1450. s->mode = IBM_CODES;
  1451. case IBM_CODES:
  1452. UPDATE
  1453. if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
  1454. return inflate_flush(s, z, r);
  1455. r = Z_OK;
  1456. inflate_codes_free(s->sub.decode.codes, z);
  1457. LOAD
  1458. Tracev((stderr, "inflate: codes end, %lu total out\n",
  1459. z->total_out + (q >= s->read ? q - s->read :
  1460. (s->end - s->read) + (q - s->window))));
  1461. if (!s->last)
  1462. {
  1463. s->mode = IBM_TYPE;
  1464. break;
  1465. }
  1466. s->mode = IBM_DRY;
  1467. case IBM_DRY:
  1468. FLUSH
  1469. if (s->read != s->write)
  1470. LEAVE
  1471. s->mode = IBM_DONE;
  1472. case IBM_DONE:
  1473. r = Z_STREAM_END;
  1474. LEAVE
  1475. case IBM_BAD:
  1476. r = Z_DATA_ERROR;
  1477. LEAVE
  1478. default:
  1479. r = Z_STREAM_ERROR;
  1480. LEAVE
  1481. }
  1482. }
  1483. int inflate_blocks_free(inflate_blocks_statef *s, z_streamp z)
  1484. {
  1485. inflate_blocks_reset(s, z, Z_NULL);
  1486. ZFREE(z, s->window);
  1487. ZFREE(z, s->hufts);
  1488. ZFREE(z, s);
  1489. Tracev((stderr, "inflate: blocks freed\n"));
  1490. return Z_OK;
  1491. }
  1492. // inftrees.c -- generate Huffman trees for efficient decoding
  1493. // Copyright (C) 1995-1998 Mark Adler
  1494. // For conditions of distribution and use, see copyright notice in zlib.h
  1495. //
  1496. extern const char inflate_copyright[] =
  1497. " ";//inflate 1.1.3 Copyright 1995-1998 Mark Adler ";
  1498. // If you use the zlib library in a product, an acknowledgment is welcome
  1499. // in the documentation of your product. If for some reason you cannot
  1500. // include such an acknowledgment, I would appreciate that you keep this
  1501. // copyright string in the executable of your product.
  1502. int huft_build (
  1503. uInt *, // code lengths in bits
  1504. uInt, // number of codes
  1505. uInt, // number of "simple" codes
  1506. const uInt *, // list of base values for non-simple codes
  1507. const uInt *, // list of extra bits for non-simple codes
  1508. inflate_huft **,// result: starting table
  1509. uInt *, // maximum lookup bits (returns actual)
  1510. inflate_huft *, // space for trees
  1511. uInt *, // hufts used in space
  1512. uInt * ); // space for values
  1513. // Tables for deflate from PKZIP's appnote.txt.
  1514. const uInt cplens[31] = { // Copy lengths for literal codes 257..285
  1515. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  1516. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
  1517. // see note #13 above about 258
  1518. const uInt cplext[31] = { // Extra bits for literal codes 257..285
  1519. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  1520. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; // 112==invalid
  1521. const uInt cpdist[30] = { // Copy offsets for distance codes 0..29
  1522. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  1523. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  1524. 8193, 12289, 16385, 24577};
  1525. const uInt cpdext[30] = { // Extra bits for distance codes
  1526. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  1527. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  1528. 12, 12, 13, 13};
  1529. //
  1530. // Huffman code decoding is performed using a multi-level table lookup.
  1531. // The fastest way to decode is to simply build a lookup table whose
  1532. // size is determined by the longest code. However, the time it takes
  1533. // to build this table can also be a factor if the data being decoded
  1534. // is not very long. The most common codes are necessarily the
  1535. // shortest codes, so those codes dominate the decoding time, and hence
  1536. // the speed. The idea is you can have a shorter table that decodes the
  1537. // shorter, more probable codes, and then point to subsidiary tables for
  1538. // the longer codes. The time it costs to decode the longer codes is
  1539. // then traded against the time it takes to make longer tables.
  1540. //
  1541. // This results of this trade are in the variables lbits and dbits
  1542. // below. lbits is the number of bits the first level table for literal/
  1543. // length codes can decode in one step, and dbits is the same thing for
  1544. // the distance codes. Subsequent tables are also less than or equal to
  1545. // those sizes. These values may be adjusted either when all of the
  1546. // codes are shorter than that, in which case the longest code length in
  1547. // bits is used, or when the shortest code is *longer* than the requested
  1548. // table size, in which case the length of the shortest code in bits is
  1549. // used.
  1550. //
  1551. // There are two different values for the two tables, since they code a
  1552. // different number of possibilities each. The literal/length table
  1553. // codes 286 possible values, or in a flat code, a little over eight
  1554. // bits. The distance table codes 30 possible values, or a little less
  1555. // than five bits, flat. The optimum values for speed end up being
  1556. // about one bit more than those, so lbits is 8+1 and dbits is 5+1.
  1557. // The optimum values may differ though from machine to machine, and
  1558. // possibly even between compilers. Your mileage may vary.
  1559. //
  1560. // If BMAX needs to be larger than 16, then h and x[] should be uLong.
  1561. #define BMAX 15 // maximum bit length of any code
  1562. int huft_build(
  1563. uInt *b, // code lengths in bits (all assumed <= BMAX)
  1564. uInt n, // number of codes (assumed <= 288)
  1565. uInt s, // number of simple-valued codes (0..s-1)
  1566. const uInt *d, // list of base values for non-simple codes
  1567. const uInt *e, // list of extra bits for non-simple codes
  1568. inflate_huft * *t, // result: starting table
  1569. uInt *m, // maximum lookup bits, returns actual
  1570. inflate_huft *hp, // space for trees
  1571. uInt *hn, // hufts used in space
  1572. uInt *v) // working area: values in order of bit length
  1573. // Given a list of code lengths and a maximum table size, make a set of
  1574. // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
  1575. // if the given code set is incomplete (the tables are still built in this
  1576. // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
  1577. // lengths), or Z_MEM_ERROR if not enough memory.
  1578. {
  1579. uInt a; // counter for codes of length k
  1580. uInt c[BMAX+1]; // bit length count table
  1581. uInt f; // i repeats in table every f entries
  1582. int g; // maximum code length
  1583. int h; // table level
  1584. register uInt i; // counter, current code
  1585. register uInt j; // counter
  1586. register int k; // number of bits in current code
  1587. int l; // bits per table (returned in m)
  1588. uInt mask; // (1 << w) - 1, to avoid cc -O bug on HP
  1589. register uInt *p; // pointer into c[], b[], or v[]
  1590. inflate_huft *q; // points to current table
  1591. struct inflate_huft_s r; // table entry for structure assignment
  1592. inflate_huft *u[BMAX]; // table stack
  1593. register int w; // bits before this table == (l * h)
  1594. uInt x[BMAX+1]; // bit offsets, then code stack
  1595. uInt *xp; // pointer into x
  1596. int y; // number of dummy codes added
  1597. uInt z; // number of entries in current table
  1598. // Generate counts for each bit length
  1599. p = c;
  1600. #define C0 *p++ = 0;
  1601. #define C2 C0 C0 C0 C0
  1602. #define C4 C2 C2 C2 C2
  1603. C4; p; // clear c[]--assume BMAX+1 is 16
  1604. p = b; i = n;
  1605. do {
  1606. c[*p++]++; // assume all entries <= BMAX
  1607. } while (--i);
  1608. if (c[0] == n) // null input--all zero length codes
  1609. {
  1610. *t = (inflate_huft *)Z_NULL;
  1611. *m = 0;
  1612. return Z_OK;
  1613. }
  1614. // Find minimum and maximum length, bound *m by those
  1615. l = *m;
  1616. for (j = 1; j <= BMAX; j++)
  1617. if (c[j])
  1618. break;
  1619. k = j; // minimum code length
  1620. if ((uInt)l < j)
  1621. l = j;
  1622. for (i = BMAX; i; i--)
  1623. if (c[i])
  1624. break;
  1625. g = i; // maximum code length
  1626. if ((uInt)l > i)
  1627. l = i;
  1628. *m = l;
  1629. // Adjust last length count to fill out codes, if needed
  1630. for (y = 1 << j; j < i; j++, y <<= 1)
  1631. if ((y -= c[j]) < 0)
  1632. return Z_DATA_ERROR;
  1633. if ((y -= c[i]) < 0)
  1634. return Z_DATA_ERROR;
  1635. c[i] += y;
  1636. // Generate starting offsets into the value table for each length
  1637. x[1] = j = 0;
  1638. p = c + 1; xp = x + 2;
  1639. while (--i) { // note that i == g from above
  1640. *xp++ = (j += *p++);
  1641. }
  1642. // Make a table of values in order of bit lengths
  1643. p = b; i = 0;
  1644. do {
  1645. if ((j = *p++) != 0)
  1646. v[x[j]++] = i;
  1647. } while (++i < n);
  1648. n = x[g]; // set n to length of v
  1649. // Generate the Huffman codes and for each, make the table entries
  1650. x[0] = i = 0; // first Huffman code is zero
  1651. p = v; // grab values in bit order
  1652. h = -1; // no tables yet--level -1
  1653. w = -l; // bits decoded == (l * h)
  1654. u[0] = (inflate_huft *)Z_NULL; // just to keep compilers happy
  1655. q = (inflate_huft *)Z_NULL; // ditto
  1656. z = 0; // ditto
  1657. // go through the bit lengths (k already is bits in shortest code)
  1658. for (; k <= g; k++)
  1659. {
  1660. a = c[k];
  1661. while (a--)
  1662. {
  1663. // here i is the Huffman code of length k bits for value *p
  1664. // make tables up to required level
  1665. while (k > w + l)
  1666. {
  1667. h++;
  1668. w += l; // previous table always l bits
  1669. // compute minimum size table less than or equal to l bits
  1670. z = g - w;
  1671. z = z > (uInt)l ? l : z; // table size upper limit
  1672. if ((f = 1 << (j = k - w)) > a + 1) // try a k-w bit table
  1673. { // too few codes for k-w bit table
  1674. f -= a + 1; // deduct codes from patterns left
  1675. xp = c + k;
  1676. if (j < z)
  1677. while (++j < z) // try smaller tables up to z bits
  1678. {
  1679. if ((f <<= 1) <= *++xp)
  1680. break; // enough codes to use up j bits
  1681. f -= *xp; // else deduct codes from patterns
  1682. }
  1683. }
  1684. z = 1 << j; // table entries for j-bit table
  1685. // allocate new table
  1686. if (*hn + z > MANY) // (note: doesn't matter for fixed)
  1687. return Z_MEM_ERROR; // not enough memory
  1688. u[h] = q = hp + *hn;
  1689. *hn += z;
  1690. // connect to last table, if there is one
  1691. if (h)
  1692. {
  1693. x[h] = i; // save pattern for backing up
  1694. r.bits = (Byte)l; // bits to dump before this table
  1695. r.exop = (Byte)j; // bits in this table
  1696. j = i >> (w - l);
  1697. r.base = (uInt)(q - u[h-1] - j); // offset to this table
  1698. u[h-1][j] = r; // connect to last table
  1699. }
  1700. else
  1701. *t = q; // first table is returned result
  1702. }
  1703. // set up table entry in r
  1704. r.bits = (Byte)(k - w);
  1705. if (p >= v + n)
  1706. r.exop = 128 + 64; // out of values--invalid code
  1707. else if (*p < s)
  1708. {
  1709. r.exop = (Byte)(*p < 256 ? 0 : 32 + 64); // 256 is end-of-block
  1710. r.base = *p++; // simple code is just the value
  1711. }
  1712. else
  1713. {
  1714. r.exop = (Byte)(e[*p - s] + 16 + 64);// non-simple--look up in lists
  1715. r.base = d[*p++ - s];
  1716. }
  1717. // fill code-like entries with r
  1718. f = 1 << (k - w);
  1719. for (j = i >> w; j < z; j += f)
  1720. q[j] = r;
  1721. // backwards increment the k-bit code i
  1722. for (j = 1 << (k - 1); i & j; j >>= 1)
  1723. i ^= j;
  1724. i ^= j;
  1725. // backup over finished tables
  1726. mask = (1 << w) - 1; // needed on HP, cc -O bug
  1727. while ((i & mask) != x[h])
  1728. {
  1729. h--; // don't need to update q
  1730. w -= l;
  1731. mask = (1 << w) - 1;
  1732. }
  1733. }
  1734. }
  1735. // Return Z_BUF_ERROR if we were given an incomplete table
  1736. return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  1737. }
  1738. int inflate_trees_bits(
  1739. uInt *c, // 19 code lengths
  1740. uInt *bb, // bits tree desired/actual depth
  1741. inflate_huft * *tb, // bits tree result
  1742. inflate_huft *hp, // space for trees
  1743. z_streamp z) // for messages
  1744. {
  1745. int r;
  1746. uInt hn = 0; // hufts used in space
  1747. uInt *v; // work area for huft_build
  1748. if ((v = (uInt*)ZALLOC(z, 19, sizeof(uInt))) == Z_NULL)
  1749. return Z_MEM_ERROR;
  1750. r = huft_build(c, 19, 19, (uInt*)Z_NULL, (uInt*)Z_NULL,
  1751. tb, bb, hp, &hn, v);
  1752. if (r == Z_DATA_ERROR)
  1753. z->msg = (char*)"oversubscribed dynamic bit lengths tree";
  1754. else if (r == Z_BUF_ERROR || *bb == 0)
  1755. {
  1756. z->msg = (char*)"incomplete dynamic bit lengths tree";
  1757. r = Z_DATA_ERROR;
  1758. }
  1759. ZFREE(z, v);
  1760. return r;
  1761. }
  1762. int inflate_trees_dynamic(
  1763. uInt nl, // number of literal/length codes
  1764. uInt nd, // number of distance codes
  1765. uInt *c, // that many (total) code lengths
  1766. uInt *bl, // literal desired/actual bit depth
  1767. uInt *bd, // distance desired/actual bit depth
  1768. inflate_huft * *tl, // literal/length tree result
  1769. inflate_huft * *td, // distance tree result
  1770. inflate_huft *hp, // space for trees
  1771. z_streamp z) // for messages
  1772. {
  1773. int r;
  1774. uInt hn = 0; // hufts used in space
  1775. uInt *v; // work area for huft_build
  1776. // allocate work area
  1777. if ((v = (uInt*)ZALLOC(z, 288, sizeof(uInt))) == Z_NULL)
  1778. return Z_MEM_ERROR;
  1779. // build literal/length tree
  1780. r = huft_build(c, nl, 257, cplens, cplext, tl, bl, hp, &hn, v);
  1781. if (r != Z_OK || *bl == 0)
  1782. {
  1783. if (r == Z_DATA_ERROR)
  1784. z->msg = (char*)"oversubscribed literal/length tree";
  1785. else if (r != Z_MEM_ERROR)
  1786. {
  1787. z->msg = (char*)"incomplete literal/length tree";
  1788. r = Z_DATA_ERROR;
  1789. }
  1790. ZFREE(z, v);
  1791. return r;
  1792. }
  1793. // build distance tree
  1794. r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, hp, &hn, v);
  1795. if (r != Z_OK || (*bd == 0 && nl > 257))
  1796. {
  1797. if (r == Z_DATA_ERROR)
  1798. z->msg = (char*)"oversubscribed distance tree";
  1799. else if (r == Z_BUF_ERROR) {
  1800. z->msg = (char*)"incomplete distance tree";
  1801. r = Z_DATA_ERROR;
  1802. }
  1803. else if (r != Z_MEM_ERROR)
  1804. {
  1805. z->msg = (char*)"empty distance tree with lengths";
  1806. r = Z_DATA_ERROR;
  1807. }
  1808. ZFREE(z, v);
  1809. return r;
  1810. }
  1811. // done
  1812. ZFREE(z, v);
  1813. return Z_OK;
  1814. }
  1815. int inflate_trees_fixed(
  1816. uInt *bl, // literal desired/actual bit depth
  1817. uInt *bd, // distance desired/actual bit depth
  1818. const inflate_huft * * tl, // literal/length tree result
  1819. const inflate_huft * *td, // distance tree result
  1820. z_streamp ) // for memory allocation
  1821. {
  1822. *bl = fixed_bl;
  1823. *bd = fixed_bd;
  1824. *tl = fixed_tl;
  1825. *td = fixed_td;
  1826. return Z_OK;
  1827. }
  1828. // inffast.c -- process literals and length/distance pairs fast
  1829. // Copyright (C) 1995-1998 Mark Adler
  1830. // For conditions of distribution and use, see copyright notice in zlib.h
  1831. //
  1832. //struct inflate_codes_state {int dummy;}; // for buggy compilers
  1833. // macros for bit input with no checking and for returning unused bytes
  1834. #define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
  1835. #define UNGRAB {c=z->avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;}
  1836. // Called with number of bytes left to write in window at least 258
  1837. // (the maximum string length) and number of input bytes available
  1838. // at least ten. The ten bytes are six bytes for the longest length/
  1839. // distance pair plus four bytes for overloading the bit buffer.
  1840. int inflate_fast(
  1841. uInt bl, uInt bd,
  1842. const inflate_huft *tl,
  1843. const inflate_huft *td, // need separate declaration for Borland C++
  1844. inflate_blocks_statef *s,
  1845. z_streamp z)
  1846. {
  1847. const inflate_huft *t; // temporary pointer
  1848. uInt e; // extra bits or operation
  1849. uLong b; // bit buffer
  1850. uInt k; // bits in bit buffer
  1851. Byte *p; // input data pointer
  1852. uInt n; // bytes available there
  1853. Byte *q; // output window write pointer
  1854. uInt m; // bytes to end of window or read pointer
  1855. uInt ml; // mask for literal/length tree
  1856. uInt md; // mask for distance tree
  1857. uInt c; // bytes to copy
  1858. uInt d; // distance back to copy from
  1859. Byte *r; // copy source pointer
  1860. // load input, output, bit values
  1861. LOAD
  1862. // initialize masks
  1863. ml = inflate_mask[bl];
  1864. md = inflate_mask[bd];
  1865. // do until not enough input or output space for fast loop
  1866. do { // assume called with m >= 258 && n >= 10
  1867. // get literal/length code
  1868. GRABBITS(20) // max bits for literal/length code
  1869. if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
  1870. {
  1871. DUMPBITS(t->bits)
  1872. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  1873. "inflate: * literal '%c'\n" :
  1874. "inflate: * literal 0x%02x\n", t->base));
  1875. *q++ = (Byte)t->base;
  1876. m--;
  1877. continue;
  1878. }
  1879. for (;;) {
  1880. DUMPBITS(t->bits)
  1881. if (e & 16)
  1882. {
  1883. // get extra bits for length
  1884. e &= 15;
  1885. c = t->base + ((uInt)b & inflate_mask[e]);
  1886. DUMPBITS(e)
  1887. Tracevv((stderr, "inflate: * length %u\n", c));
  1888. // decode distance base of block to copy
  1889. GRABBITS(15); // max bits for distance code
  1890. e = (t = td + ((uInt)b & md))->exop;
  1891. for (;;) {
  1892. DUMPBITS(t->bits)
  1893. if (e & 16)
  1894. {
  1895. // get extra bits to add to distance base
  1896. e &= 15;
  1897. GRABBITS(e) // get extra bits (up to 13)
  1898. d = t->base + ((uInt)b & inflate_mask[e]);
  1899. DUMPBITS(e)
  1900. Tracevv((stderr, "inflate: * distance %u\n", d));
  1901. // do the copy
  1902. m -= c;
  1903. if ((uInt)(q - s->window) >= d) // offset before dest
  1904. { // just copy
  1905. r = q - d;
  1906. *q++ = *r++; c--; // minimum count is three,
  1907. *q++ = *r++; c--; // so unroll loop a little
  1908. }
  1909. else // else offset after destination
  1910. {
  1911. e = d - (uInt)(q - s->window); // bytes from offset to end
  1912. r = s->end - e; // pointer to offset
  1913. if (c > e) // if source crosses,
  1914. {
  1915. c -= e; // copy to end of window
  1916. do {
  1917. *q++ = *r++;
  1918. } while (--e);
  1919. r = s->window; // copy rest from start of window
  1920. }
  1921. }
  1922. do { // copy all or what's left
  1923. *q++ = *r++;
  1924. } while (--c);
  1925. break;
  1926. }
  1927. else if ((e & 64) == 0)
  1928. {
  1929. t += t->base;
  1930. e = (t += ((uInt)b & inflate_mask[e]))->exop;
  1931. }
  1932. else
  1933. {
  1934. z->msg = (char*)"invalid distance code";
  1935. UNGRAB
  1936. UPDATE
  1937. return Z_DATA_ERROR;
  1938. }
  1939. };
  1940. break;
  1941. }
  1942. if ((e & 64) == 0)
  1943. {
  1944. t += t->base;
  1945. if ((e = (t += ((uInt)b & inflate_mask[e]))->exop) == 0)
  1946. {
  1947. DUMPBITS(t->bits)
  1948. Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
  1949. "inflate: * literal '%c'\n" :
  1950. "inflate: * literal 0x%02x\n", t->base));
  1951. *q++ = (Byte)t->base;
  1952. m--;
  1953. break;
  1954. }
  1955. }
  1956. else if (e & 32)
  1957. {
  1958. Tracevv((stderr, "inflate: * end of block\n"));
  1959. UNGRAB
  1960. UPDATE
  1961. return Z_STREAM_END;
  1962. }
  1963. else
  1964. {
  1965. z->msg = (char*)"invalid literal/length code";
  1966. UNGRAB
  1967. UPDATE
  1968. return Z_DATA_ERROR;
  1969. }
  1970. };
  1971. } while (m >= 258 && n >= 10);
  1972. // not enough input or output--restore pointers and return
  1973. UNGRAB
  1974. UPDATE
  1975. return Z_OK;
  1976. }
  1977. // crc32.c -- compute the CRC-32 of a data stream
  1978. // Copyright (C) 1995-1998 Mark Adler
  1979. // For conditions of distribution and use, see copyright notice in zlib.h
  1980. // @(#) $Id$
  1981. // Table of CRC-32's of all single-byte values (made by make_crc_table)
  1982. const uLong crc_table[256] = {
  1983. 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
  1984. 0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
  1985. 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
  1986. 0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
  1987. 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
  1988. 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
  1989. 0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
  1990. 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  1991. 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
  1992. 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
  1993. 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
  1994. 0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
  1995. 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
  1996. 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
  1997. 0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
  1998. 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  1999. 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
  2000. 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
  2001. 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
  2002. 0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
  2003. 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
  2004. 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
  2005. 0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
  2006. 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  2007. 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
  2008. 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
  2009. 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
  2010. 0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
  2011. 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
  2012. 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
  2013. 0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
  2014. 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  2015. 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
  2016. 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
  2017. 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
  2018. 0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
  2019. 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
  2020. 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
  2021. 0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
  2022. 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  2023. 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
  2024. 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
  2025. 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
  2026. 0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
  2027. 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
  2028. 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
  2029. 0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
  2030. 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  2031. 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
  2032. 0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
  2033. 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
  2034. 0x2d02ef8dL
  2035. };
  2036. const uLong * get_crc_table()
  2037. { return (const uLong *)crc_table;
  2038. }
  2039. #define CRC_DO1(buf) crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
  2040. #define CRC_DO2(buf) CRC_DO1(buf); CRC_DO1(buf);
  2041. #define CRC_DO4(buf) CRC_DO2(buf); CRC_DO2(buf);
  2042. #define CRC_DO8(buf) CRC_DO4(buf); CRC_DO4(buf);
  2043. uLong ucrc32(uLong crc, const Byte *buf, uInt len)
  2044. { if (buf == Z_NULL) return 0L;
  2045. crc = crc ^ 0xffffffffL;
  2046. while (len >= 8) {CRC_DO8(buf); len -= 8;}
  2047. if (len) do {CRC_DO1(buf);} while (--len);
  2048. return crc ^ 0xffffffffL;
  2049. }
  2050. // adler32.c -- compute the Adler-32 checksum of a data stream
  2051. // Copyright (C) 1995-1998 Mark Adler
  2052. // For conditions of distribution and use, see copyright notice in zlib.h
  2053. // @(#) $Id$
  2054. #define BASE 65521L // largest prime smaller than 65536
  2055. #define NMAX 5552
  2056. // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
  2057. #define AD_DO1(buf,i) {s1 += buf[i]; s2 += s1;}
  2058. #define AD_DO2(buf,i) AD_DO1(buf,i); AD_DO1(buf,i+1);
  2059. #define AD_DO4(buf,i) AD_DO2(buf,i); AD_DO2(buf,i+2);
  2060. #define AD_DO8(buf,i) AD_DO4(buf,i); AD_DO4(buf,i+4);
  2061. #define AD_DO16(buf) AD_DO8(buf,0); AD_DO8(buf,8);
  2062. // =========================================================================
  2063. uLong adler32(uLong adler, const Byte *buf, uInt len)
  2064. {
  2065. unsigned long s1 = adler & 0xffff;
  2066. unsigned long s2 = (adler >> 16) & 0xffff;
  2067. int k;
  2068. if (buf == Z_NULL) return 1L;
  2069. while (len > 0) {
  2070. k = len < NMAX ? len : NMAX;
  2071. len -= k;
  2072. while (k >= 16) {
  2073. AD_DO16(buf);
  2074. buf += 16;
  2075. k -= 16;
  2076. }
  2077. if (k != 0) do {
  2078. s1 += *buf++;
  2079. s2 += s1;
  2080. } while (--k);
  2081. s1 %= BASE;
  2082. s2 %= BASE;
  2083. }
  2084. return (s2 << 16) | s1;
  2085. }
  2086. // zutil.c -- target dependent utility functions for the compression library
  2087. // Copyright (C) 1995-1998 Jean-loup Gailly.
  2088. // For conditions of distribution and use, see copyright notice in zlib.h
  2089. // @(#) $Id$
  2090. const char * zlibVersion()
  2091. {
  2092. return ZLIB_VERSION;
  2093. }
  2094. // exported to allow conversion of error code to string for compress() and
  2095. // uncompress()
  2096. const char * zError(int err)
  2097. { return ERR_MSG(err);
  2098. }
  2099. voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
  2100. {
  2101. if (opaque) items += size - size; // make compiler happy
  2102. return (voidpf)calloc(items, size);
  2103. }
  2104. void zcfree (voidpf opaque, voidpf ptr)
  2105. {
  2106. zfree(ptr);
  2107. if (opaque) return; // make compiler happy
  2108. }
  2109. // inflate.c -- zlib interface to inflate modules
  2110. // Copyright (C) 1995-1998 Mark Adler
  2111. // For conditions of distribution and use, see copyright notice in zlib.h
  2112. //struct inflate_blocks_state {int dummy;}; // for buggy compilers
  2113. typedef enum {
  2114. IM_METHOD, // waiting for method byte
  2115. IM_FLAG, // waiting for flag byte
  2116. IM_DICT4, // four dictionary check bytes to go
  2117. IM_DICT3, // three dictionary check bytes to go
  2118. IM_DICT2, // two dictionary check bytes to go
  2119. IM_DICT1, // one dictionary check byte to go
  2120. IM_DICT0, // waiting for inflateSetDictionary
  2121. IM_BLOCKS, // decompressing blocks
  2122. IM_CHECK4, // four check bytes to go
  2123. IM_CHECK3, // three check bytes to go
  2124. IM_CHECK2, // two check bytes to go
  2125. IM_CHECK1, // one check byte to go
  2126. IM_DONE, // finished check, done
  2127. IM_BAD} // got an error--stay here
  2128. inflate_mode;
  2129. // inflate private state
  2130. struct internal_state {
  2131. // mode
  2132. inflate_mode mode; // current inflate mode
  2133. // mode dependent information
  2134. union {
  2135. uInt method; // if IM_FLAGS, method byte
  2136. struct {
  2137. uLong was; // computed check value
  2138. uLong need; // stream check value
  2139. } check; // if CHECK, check values to compare
  2140. uInt marker; // if IM_BAD, inflateSync's marker bytes count
  2141. } sub; // submode
  2142. // mode independent information
  2143. int nowrap; // flag for no wrapper
  2144. uInt wbits; // log2(window size) (8..15, defaults to 15)
  2145. inflate_blocks_statef
  2146. *blocks; // current inflate_blocks state
  2147. };
  2148. int inflateReset(z_streamp z)
  2149. {
  2150. if (z == Z_NULL || z->state == Z_NULL)
  2151. return Z_STREAM_ERROR;
  2152. z->total_in = z->total_out = 0;
  2153. z->msg = Z_NULL;
  2154. z->state->mode = z->state->nowrap ? IM_BLOCKS : IM_METHOD;
  2155. inflate_blocks_reset(z->state->blocks, z, Z_NULL);
  2156. Tracev((stderr, "inflate: reset\n"));
  2157. return Z_OK;
  2158. }
  2159. int inflateEnd(z_streamp z)
  2160. {
  2161. if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
  2162. return Z_STREAM_ERROR;
  2163. if (z->state->blocks != Z_NULL)
  2164. inflate_blocks_free(z->state->blocks, z);
  2165. ZFREE(z, z->state);
  2166. z->state = Z_NULL;
  2167. Tracev((stderr, "inflate: end\n"));
  2168. return Z_OK;
  2169. }
  2170. int inflateInit2(z_streamp z)
  2171. { const char *version = ZLIB_VERSION; int stream_size = sizeof(z_stream);
  2172. if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || stream_size != sizeof(z_stream)) return Z_VERSION_ERROR;
  2173. int w = -15; // MAX_WBITS: 32K LZ77 window.
  2174. // Warning: reducing MAX_WBITS makes minigzip unable to extract .gz files created by gzip.
  2175. // The memory requirements for deflate are (in bytes):
  2176. // (1 << (windowBits+2)) + (1 << (memLevel+9))
  2177. // that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values)
  2178. // plus a few kilobytes for small objects. For example, if you want to reduce
  2179. // the default memory requirements from 256K to 128K, compile with
  2180. // make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
  2181. // Of course this will generally degrade compression (there's no free lunch).
  2182. //
  2183. // The memory requirements for inflate are (in bytes) 1 << windowBits
  2184. // that is, 32K for windowBits=15 (default value) plus a few kilobytes
  2185. // for small objects.
  2186. // initialize state
  2187. if (z == Z_NULL) return Z_STREAM_ERROR;
  2188. z->msg = Z_NULL;
  2189. if (z->zalloc == Z_NULL)
  2190. {
  2191. z->zalloc = zcalloc;
  2192. z->opaque = (voidpf)0;
  2193. }
  2194. if (z->zfree == Z_NULL) z->zfree = zcfree;
  2195. if ((z->state = (struct internal_state *)
  2196. ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
  2197. return Z_MEM_ERROR;
  2198. z->state->blocks = Z_NULL;
  2199. // handle undocumented nowrap option (no zlib header or check)
  2200. z->state->nowrap = 0;
  2201. if (w < 0)
  2202. {
  2203. w = - w;
  2204. z->state->nowrap = 1;
  2205. }
  2206. // set window size
  2207. if (w < 8 || w > 15)
  2208. {
  2209. inflateEnd(z);
  2210. return Z_STREAM_ERROR;
  2211. }
  2212. z->state->wbits = (uInt)w;
  2213. // create inflate_blocks state
  2214. if ((z->state->blocks =
  2215. inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
  2216. == Z_NULL)
  2217. {
  2218. inflateEnd(z);
  2219. return Z_MEM_ERROR;
  2220. }
  2221. Tracev((stderr, "inflate: allocated\n"));
  2222. // reset state
  2223. inflateReset(z);
  2224. return Z_OK;
  2225. }
  2226. #define IM_NEEDBYTE {if(z->avail_in==0)return r;r=f;}
  2227. #define IM_NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
  2228. int inflate(z_streamp z, int f)
  2229. {
  2230. int r;
  2231. uInt b;
  2232. if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL)
  2233. return Z_STREAM_ERROR;
  2234. f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
  2235. r = Z_BUF_ERROR;
  2236. for (;;) switch (z->state->mode)
  2237. {
  2238. case IM_METHOD:
  2239. IM_NEEDBYTE
  2240. if (((z->state->sub.method = IM_NEXTBYTE) & 0xf) != Z_DEFLATED)
  2241. {
  2242. z->state->mode = IM_BAD;
  2243. z->msg = (char*)"unknown compression method";
  2244. z->state->sub.marker = 5; // can't try inflateSync
  2245. break;
  2246. }
  2247. if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
  2248. {
  2249. z->state->mode = IM_BAD;
  2250. z->msg = (char*)"invalid window size";
  2251. z->state->sub.marker = 5; // can't try inflateSync
  2252. break;
  2253. }
  2254. z->state->mode = IM_FLAG;
  2255. case IM_FLAG:
  2256. IM_NEEDBYTE
  2257. b = IM_NEXTBYTE;
  2258. if (((z->state->sub.method << 8) + b) % 31)
  2259. {
  2260. z->state->mode = IM_BAD;
  2261. z->msg = (char*)"incorrect header check";
  2262. z->state->sub.marker = 5; // can't try inflateSync
  2263. break;
  2264. }
  2265. Tracev((stderr, "inflate: zlib header ok\n"));
  2266. if (!(b & PRESET_DICT))
  2267. {
  2268. z->state->mode = IM_BLOCKS;
  2269. break;
  2270. }
  2271. z->state->mode = IM_DICT4;
  2272. case IM_DICT4:
  2273. IM_NEEDBYTE
  2274. z->state->sub.check.need = (uLong)IM_NEXTBYTE << 24;
  2275. z->state->mode = IM_DICT3;
  2276. case IM_DICT3:
  2277. IM_NEEDBYTE
  2278. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 16;
  2279. z->state->mode = IM_DICT2;
  2280. case IM_DICT2:
  2281. IM_NEEDBYTE
  2282. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 8;
  2283. z->state->mode = IM_DICT1;
  2284. case IM_DICT1:
  2285. IM_NEEDBYTE; r;
  2286. z->state->sub.check.need += (uLong)IM_NEXTBYTE;
  2287. z->adler = z->state->sub.check.need;
  2288. z->state->mode = IM_DICT0;
  2289. return Z_NEED_DICT;
  2290. case IM_DICT0:
  2291. z->state->mode = IM_BAD;
  2292. z->msg = (char*)"need dictionary";
  2293. z->state->sub.marker = 0; // can try inflateSync
  2294. return Z_STREAM_ERROR;
  2295. case IM_BLOCKS:
  2296. r = inflate_blocks(z->state->blocks, z, r);
  2297. if (r == Z_DATA_ERROR)
  2298. {
  2299. z->state->mode = IM_BAD;
  2300. z->state->sub.marker = 0; // can try inflateSync
  2301. break;
  2302. }
  2303. if (r == Z_OK)
  2304. r = f;
  2305. if (r != Z_STREAM_END)
  2306. return r;
  2307. r = f;
  2308. inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
  2309. if (z->state->nowrap)
  2310. {
  2311. z->state->mode = IM_DONE;
  2312. break;
  2313. }
  2314. z->state->mode = IM_CHECK4;
  2315. case IM_CHECK4:
  2316. IM_NEEDBYTE
  2317. z->state->sub.check.need = (uLong)IM_NEXTBYTE << 24;
  2318. z->state->mode = IM_CHECK3;
  2319. case IM_CHECK3:
  2320. IM_NEEDBYTE
  2321. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 16;
  2322. z->state->mode = IM_CHECK2;
  2323. case IM_CHECK2:
  2324. IM_NEEDBYTE
  2325. z->state->sub.check.need += (uLong)IM_NEXTBYTE << 8;
  2326. z->state->mode = IM_CHECK1;
  2327. case IM_CHECK1:
  2328. IM_NEEDBYTE
  2329. z->state->sub.check.need += (uLong)IM_NEXTBYTE;
  2330. if (z->state->sub.check.was != z->state->sub.check.need)
  2331. {
  2332. z->state->mode = IM_BAD;
  2333. z->msg = (char*)"incorrect data check";
  2334. z->state->sub.marker = 5; // can't try inflateSync
  2335. break;
  2336. }
  2337. Tracev((stderr, "inflate: zlib check ok\n"));
  2338. z->state->mode = IM_DONE;
  2339. case IM_DONE:
  2340. return Z_STREAM_END;
  2341. case IM_BAD:
  2342. return Z_DATA_ERROR;
  2343. default:
  2344. return Z_STREAM_ERROR;
  2345. }
  2346. }
  2347. #ifdef _UNICODE
  2348. static int GetAnsiFileName(LPCWSTR name, char * buf, int nBufSize)
  2349. {
  2350. memset(buf, 0, nBufSize);
  2351. int n = WideCharToMultiByte(CP_ACP, // code page
  2352. 0, // performance and mapping flags
  2353. name, // wide-character string
  2354. -1, // number of chars in string
  2355. buf, // buffer for new string
  2356. nBufSize, // size of buffer
  2357. NULL, // default for unmappable chars
  2358. NULL); // set when default char used
  2359. return n;
  2360. }
  2361. static int GetUnicodeFileName(const char * name, LPWSTR buf, int nBufSize)
  2362. {
  2363. memset(buf, 0, nBufSize*sizeof(TCHAR));
  2364. int n = MultiByteToWideChar(CP_ACP, // code page
  2365. 0, // character-type options
  2366. name, // string to map
  2367. -1, // number of bytes in string
  2368. buf, // wide-character buffer
  2369. nBufSize); // size of buffer
  2370. return n;
  2371. }
  2372. #endif
  2373. // unzip.c -- IO on .zip files using zlib
  2374. // Version 0.15 beta, Mar 19th, 1998,
  2375. // Read unzip.h for more info
  2376. #define UNZ_BUFSIZE (16384)
  2377. #define UNZ_MAXFILENAMEINZIP (256)
  2378. #define SIZECENTRALDIRITEM (0x2e)
  2379. #define SIZEZIPLOCALHEADER (0x1e)
  2380. const char unz_copyright[] = " ";//unzip 0.15 Copyright 1998 Gilles Vollant ";
  2381. // unz_file_info_interntal contain internal info about a file in zipfile
  2382. typedef struct unz_file_info_internal_s
  2383. {
  2384. uLong offset_curfile;// relative offset of local header 4 bytes
  2385. } unz_file_info_internal;
  2386. typedef struct
  2387. { bool is_handle; // either a handle or memory
  2388. bool canseek;
  2389. // for handles:
  2390. HANDLE h; bool herr; unsigned long initial_offset;
  2391. // for memory:
  2392. void *buf; unsigned int len,pos; // if it's a memory block
  2393. } LUFILE;
  2394. LUFILE *lufopen(void *z,unsigned int len,DWORD flags,ZRESULT *err)
  2395. {
  2396. if (flags!=ZIP_HANDLE && flags!=ZIP_FILENAME && flags!=ZIP_MEMORY)
  2397. {
  2398. *err=ZR_ARGS;
  2399. return NULL;
  2400. }
  2401. //
  2402. HANDLE h=0; bool canseek=false; *err=ZR_OK;
  2403. if (flags==ZIP_HANDLE||flags==ZIP_FILENAME)
  2404. {
  2405. if (flags==ZIP_HANDLE)
  2406. {
  2407. HANDLE hf = z;
  2408. BOOL res = DuplicateHandle(GetCurrentProcess(),hf,GetCurrentProcess(),&h,0,FALSE,DUPLICATE_SAME_ACCESS);
  2409. if (!res)
  2410. {
  2411. *err=ZR_NODUPH;
  2412. return NULL;
  2413. }
  2414. }
  2415. else
  2416. {
  2417. h = CreateFile((const TCHAR *)z, GENERIC_READ, FILE_SHARE_READ,
  2418. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  2419. if (h == INVALID_HANDLE_VALUE)
  2420. {
  2421. *err = ZR_NOFILE;
  2422. return NULL;
  2423. }
  2424. }
  2425. DWORD type = GetFileType(h);
  2426. canseek = (type==FILE_TYPE_DISK);
  2427. }
  2428. LUFILE *lf = new LUFILE;
  2429. if (flags==ZIP_HANDLE||flags==ZIP_FILENAME)
  2430. {
  2431. lf->is_handle=true;
  2432. lf->canseek=canseek;
  2433. lf->h=h; lf->herr=false;
  2434. lf->initial_offset=0;
  2435. if (canseek)
  2436. lf->initial_offset = SetFilePointer(h,0,NULL,FILE_CURRENT);
  2437. }
  2438. else
  2439. {
  2440. lf->is_handle=false;
  2441. lf->canseek=true;
  2442. lf->buf=z;
  2443. lf->len=len;
  2444. lf->pos=0;
  2445. lf->initial_offset=0;
  2446. }
  2447. *err=ZR_OK;
  2448. return lf;
  2449. }
  2450. int lufclose(LUFILE *stream)
  2451. { if (stream==NULL) return EOF;
  2452. if (stream->is_handle) CloseHandle(stream->h);
  2453. delete stream;
  2454. return 0;
  2455. }
  2456. int luferror(LUFILE *stream)
  2457. { if (stream->is_handle && stream->herr) return 1;
  2458. else return 0;
  2459. }
  2460. long int luftell(LUFILE *stream)
  2461. { if (stream->is_handle && stream->canseek) return SetFilePointer(stream->h,0,NULL,FILE_CURRENT)-stream->initial_offset;
  2462. else if (stream->is_handle) return 0;
  2463. else return stream->pos;
  2464. }
  2465. int lufseek(LUFILE *stream, long offset, int whence)
  2466. { if (stream->is_handle && stream->canseek)
  2467. { if (whence==SEEK_SET) SetFilePointer(stream->h,stream->initial_offset+offset,0,FILE_BEGIN);
  2468. else if (whence==SEEK_CUR) SetFilePointer(stream->h,offset,NULL,FILE_CURRENT);
  2469. else if (whence==SEEK_END) SetFilePointer(stream->h,offset,NULL,FILE_END);
  2470. else return 19; // EINVAL
  2471. return 0;
  2472. }
  2473. else if (stream->is_handle) return 29; // ESPIPE
  2474. else
  2475. { if (whence==SEEK_SET) stream->pos=offset;
  2476. else if (whence==SEEK_CUR) stream->pos+=offset;
  2477. else if (whence==SEEK_END) stream->pos=stream->len+offset;
  2478. return 0;
  2479. }
  2480. }
  2481. size_t lufread(void *ptr,size_t size,size_t n,LUFILE *stream)
  2482. { unsigned int toread = (unsigned int)(size*n);
  2483. if (stream->is_handle)
  2484. { DWORD red; BOOL res = ReadFile(stream->h,ptr,toread,&red,NULL);
  2485. if (!res) stream->herr=true;
  2486. return red/size;
  2487. }
  2488. if (stream->pos+toread > stream->len) toread = stream->len-stream->pos;
  2489. memcpy(ptr, (char*)stream->buf + stream->pos, toread); DWORD red = toread;
  2490. stream->pos += red;
  2491. return red/size;
  2492. }
  2493. // file_in_zip_read_info_s contain internal information about a file in zipfile,
  2494. // when reading and decompress it
  2495. typedef struct
  2496. {
  2497. char *read_buffer; // internal buffer for compressed data
  2498. z_stream stream; // zLib stream structure for inflate
  2499. uLong pos_in_zipfile; // position in byte on the zipfile, for fseek
  2500. uLong stream_initialised; // flag set if stream structure is initialised
  2501. uLong offset_local_extrafield;// offset of the local extra field
  2502. uInt size_local_extrafield;// size of the local extra field
  2503. uLong pos_local_extrafield; // position in the local extra field in read
  2504. uLong crc32; // crc32 of all data uncompressed
  2505. uLong crc32_wait; // crc32 we must obtain after decompress all
  2506. uLong rest_read_compressed; // number of byte to be decompressed
  2507. uLong rest_read_uncompressed;//number of byte to be obtained after decomp
  2508. LUFILE* file; // io structore of the zipfile
  2509. uLong compression_method; // compression method (0==store)
  2510. uLong byte_before_the_zipfile;// byte before the zipfile, (>0 for sfx)
  2511. } file_in_zip_read_info_s;
  2512. // unz_s contain internal information about the zipfile
  2513. typedef struct
  2514. {
  2515. LUFILE* file; // io structore of the zipfile
  2516. unz_global_info gi; // public global information
  2517. uLong byte_before_the_zipfile;// byte before the zipfile, (>0 for sfx)
  2518. uLong num_file; // number of the current file in the zipfile
  2519. uLong pos_in_central_dir; // pos of the current file in the central dir
  2520. uLong current_file_ok; // flag about the usability of the current file
  2521. uLong central_pos; // position of the beginning of the central dir
  2522. uLong size_central_dir; // size of the central directory
  2523. uLong offset_central_dir; // offset of start of central directory with respect to the starting disk number
  2524. unz_file_info cur_file_info; // public info about the current file in zip
  2525. unz_file_info_internal cur_file_info_internal; // private info about it
  2526. file_in_zip_read_info_s* pfile_in_zip_read; // structure about the current file if we are decompressing it
  2527. } unz_s, *unzFile;
  2528. int unzStringFileNameCompare (const char* fileName1,const char* fileName2,int iCaseSensitivity);
  2529. // Compare two filename (fileName1,fileName2).
  2530. z_off_t unztell (unzFile file);
  2531. // Give the current position in uncompressed data
  2532. int unzeof (unzFile file);
  2533. // return 1 if the end of file was reached, 0 elsewhere
  2534. int unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len);
  2535. // Read extra field from the current file (opened by unzOpenCurrentFile)
  2536. // This is the local-header version of the extra field (sometimes, there is
  2537. // more info in the local-header version than in the central-header)
  2538. //
  2539. // if buf==NULL, it return the size of the local extra field
  2540. //
  2541. // if buf!=NULL, len is the size of the buffer, the extra header is copied in
  2542. // buf.
  2543. // the return value is the number of bytes copied in buf, or (if <0)
  2544. // the error code
  2545. // ===========================================================================
  2546. // Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  2547. // for end of file.
  2548. // IN assertion: the stream s has been sucessfully opened for reading.
  2549. int unzlocal_getByte(LUFILE *fin,int *pi)
  2550. { unsigned char c;
  2551. int err = (int)lufread(&c, 1, 1, fin);
  2552. if (err==1)
  2553. { *pi = (int)c;
  2554. return UNZ_OK;
  2555. }
  2556. else
  2557. { if (luferror(fin)) return UNZ_ERRNO;
  2558. else return UNZ_EOF;
  2559. }
  2560. }
  2561. // ===========================================================================
  2562. // Reads a long in LSB order from the given gz_stream. Sets
  2563. int unzlocal_getShort (LUFILE *fin,uLong *pX)
  2564. {
  2565. uLong x ;
  2566. int i;
  2567. int err;
  2568. err = unzlocal_getByte(fin,&i);
  2569. x = (uLong)i;
  2570. if (err==UNZ_OK)
  2571. err = unzlocal_getByte(fin,&i);
  2572. x += ((uLong)i)<<8;
  2573. if (err==UNZ_OK)
  2574. *pX = x;
  2575. else
  2576. *pX = 0;
  2577. return err;
  2578. }
  2579. int unzlocal_getLong (LUFILE *fin,uLong *pX)
  2580. {
  2581. uLong x ;
  2582. int i;
  2583. int err;
  2584. err = unzlocal_getByte(fin,&i);
  2585. x = (uLong)i;
  2586. if (err==UNZ_OK)
  2587. err = unzlocal_getByte(fin,&i);
  2588. x += ((uLong)i)<<8;
  2589. if (err==UNZ_OK)
  2590. err = unzlocal_getByte(fin,&i);
  2591. x += ((uLong)i)<<16;
  2592. if (err==UNZ_OK)
  2593. err = unzlocal_getByte(fin,&i);
  2594. x += ((uLong)i)<<24;
  2595. if (err==UNZ_OK)
  2596. *pX = x;
  2597. else
  2598. *pX = 0;
  2599. return err;
  2600. }
  2601. // My own strcmpi / strcasecmp
  2602. int strcmpcasenosensitive_internal (const char* fileName1,const char *fileName2)
  2603. {
  2604. for (;;)
  2605. {
  2606. char c1=*(fileName1++);
  2607. char c2=*(fileName2++);
  2608. if ((c1>='a') && (c1<='z'))
  2609. c1 -= (char)0x20;
  2610. if ((c2>='a') && (c2<='z'))
  2611. c2 -= (char)0x20;
  2612. if (c1=='\0')
  2613. return ((c2=='\0') ? 0 : -1);
  2614. if (c2=='\0')
  2615. return 1;
  2616. if (c1<c2)
  2617. return -1;
  2618. if (c1>c2)
  2619. return 1;
  2620. }
  2621. }
  2622. //
  2623. // Compare two filename (fileName1,fileName2).
  2624. // If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
  2625. // If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi or strcasecmp)
  2626. //
  2627. int unzStringFileNameCompare (const char*fileName1,const char*fileName2,int iCaseSensitivity)
  2628. { if (iCaseSensitivity==1) return strcmp(fileName1,fileName2);
  2629. else return strcmpcasenosensitive_internal(fileName1,fileName2);
  2630. }
  2631. #define BUFREADCOMMENT (0x400)
  2632. // Locate the Central directory of a zipfile (at the end, just before
  2633. // the global comment)
  2634. uLong unzlocal_SearchCentralDir(LUFILE *fin)
  2635. { if (lufseek(fin,0,SEEK_END) != 0) return 0;
  2636. uLong uSizeFile = luftell(fin);
  2637. uLong uMaxBack=0xffff; // maximum size of global comment
  2638. if (uMaxBack>uSizeFile) uMaxBack = uSizeFile;
  2639. unsigned char *buf = (unsigned char*)zmalloc(BUFREADCOMMENT+4);
  2640. if (buf==NULL) return 0;
  2641. uLong uPosFound=0;
  2642. uLong uBackRead = 4;
  2643. while (uBackRead<uMaxBack)
  2644. { uLong uReadSize,uReadPos ;
  2645. int i;
  2646. if (uBackRead+BUFREADCOMMENT>uMaxBack) uBackRead = uMaxBack;
  2647. else uBackRead+=BUFREADCOMMENT;
  2648. uReadPos = uSizeFile-uBackRead ;
  2649. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ? (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
  2650. if (lufseek(fin,uReadPos,SEEK_SET)!=0) break;
  2651. if (lufread(buf,(uInt)uReadSize,1,fin)!=1) break;
  2652. for (i=(int)uReadSize-3; (i--)>0;)
  2653. { if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  2654. { uPosFound = uReadPos+i; break;
  2655. }
  2656. }
  2657. if (uPosFound!=0) break;
  2658. }
  2659. if (buf) zfree(buf);
  2660. return uPosFound;
  2661. }
  2662. int unzGoToFirstFile (unzFile file);
  2663. int unzCloseCurrentFile (unzFile file);
  2664. // Open a Zip file.
  2665. // If the zipfile cannot be opened (file don't exist or in not valid), return NULL.
  2666. // Otherwise, the return value is a unzFile Handle, usable with other unzip functions
  2667. unzFile unzOpenInternal(LUFILE *fin)
  2668. {
  2669. zopenerror = ZR_OK; //+++1.2
  2670. if (fin==NULL) { zopenerror = ZR_ARGS; return NULL; } //+++1.2
  2671. if (unz_copyright[0]!=' ') {lufclose(fin); zopenerror = ZR_CORRUPT; return NULL; } //+++1.2
  2672. int err=UNZ_OK;
  2673. unz_s us;
  2674. uLong central_pos,uL;
  2675. central_pos = unzlocal_SearchCentralDir(fin);
  2676. if (central_pos==0) err=UNZ_ERRNO;
  2677. if (lufseek(fin,central_pos,SEEK_SET)!=0) err=UNZ_ERRNO;
  2678. // the signature, already checked
  2679. if (unzlocal_getLong(fin,&uL)!=UNZ_OK) err=UNZ_ERRNO;
  2680. // number of this disk
  2681. uLong number_disk; // number of the current dist, used for spanning ZIP, unsupported, always 0
  2682. if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK) err=UNZ_ERRNO;
  2683. // number of the disk with the start of the central directory
  2684. uLong number_disk_with_CD; // number the the disk with central dir, used for spaning ZIP, unsupported, always 0
  2685. if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK) err=UNZ_ERRNO;
  2686. // total number of entries in the central dir on this disk
  2687. if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK) err=UNZ_ERRNO;
  2688. // total number of entries in the central dir
  2689. uLong number_entry_CD; // total number of entries in the central dir (same than number_entry on nospan)
  2690. if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK) err=UNZ_ERRNO;
  2691. if ((number_entry_CD!=us.gi.number_entry) || (number_disk_with_CD!=0) || (number_disk!=0)) err=UNZ_BADZIPFILE;
  2692. // size of the central directory
  2693. if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK) err=UNZ_ERRNO;
  2694. // offset of start of central directory with respect to the starting disk number
  2695. if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK) err=UNZ_ERRNO;
  2696. // zipfile comment length
  2697. if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK) err=UNZ_ERRNO;
  2698. if ((central_pos+fin->initial_offset<us.offset_central_dir+us.size_central_dir) && (err==UNZ_OK)) err=UNZ_BADZIPFILE;
  2699. //if (err!=UNZ_OK) {lufclose(fin);return NULL;}
  2700. if (err!=UNZ_OK) {lufclose(fin); zopenerror = err; return NULL;} //+++1.2
  2701. us.file=fin;
  2702. us.byte_before_the_zipfile = central_pos+fin->initial_offset - (us.offset_central_dir+us.size_central_dir);
  2703. us.central_pos = central_pos;
  2704. us.pfile_in_zip_read = NULL;
  2705. fin->initial_offset = 0; // since the zipfile itself is expected to handle this
  2706. unz_s *s = (unz_s*)zmalloc(sizeof(unz_s));
  2707. *s=us;
  2708. unzGoToFirstFile((unzFile)s);
  2709. return (unzFile)s;
  2710. }
  2711. // Close a ZipFile opened with unzipOpen.
  2712. // If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
  2713. // these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  2714. // return UNZ_OK if there is no problem.
  2715. int unzClose (unzFile file)
  2716. {
  2717. unz_s* s;
  2718. if (file==NULL)
  2719. return UNZ_PARAMERROR;
  2720. s=(unz_s*)file;
  2721. if (s->pfile_in_zip_read!=NULL)
  2722. unzCloseCurrentFile(file);
  2723. lufclose(s->file);
  2724. if (s) zfree(s); // unused s=0;
  2725. return UNZ_OK;
  2726. }
  2727. // Write info about the ZipFile in the *pglobal_info structure.
  2728. // No preparation of the structure is needed
  2729. // return UNZ_OK if there is no problem.
  2730. int unzGetGlobalInfo (unzFile file,unz_global_info *pglobal_info)
  2731. {
  2732. unz_s* s;
  2733. if (file==NULL)
  2734. return UNZ_PARAMERROR;
  2735. s=(unz_s*)file;
  2736. *pglobal_info=s->gi;
  2737. return UNZ_OK;
  2738. }
  2739. // Translate date/time from Dos format to tm_unz (readable more easilty)
  2740. void unzlocal_DosDateToTmuDate (uLong ulDosDate, tm_unz* ptm)
  2741. {
  2742. uLong uDate;
  2743. uDate = (uLong)(ulDosDate>>16);
  2744. ptm->tm_mday = (uInt)(uDate&0x1f) ;
  2745. ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
  2746. ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
  2747. ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
  2748. ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
  2749. ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
  2750. }
  2751. // Get Info about the current file in the zipfile, with internal only info
  2752. int unzlocal_GetCurrentFileInfoInternal (unzFile file,
  2753. unz_file_info *pfile_info,
  2754. unz_file_info_internal
  2755. *pfile_info_internal,
  2756. char *szFileName,
  2757. uLong fileNameBufferSize,
  2758. void *extraField,
  2759. uLong extraFieldBufferSize,
  2760. char *szComment,
  2761. uLong commentBufferSize);
  2762. int unzlocal_GetCurrentFileInfoInternal (unzFile file, unz_file_info *pfile_info,
  2763. unz_file_info_internal *pfile_info_internal, char *szFileName,
  2764. uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize,
  2765. char *szComment, uLong commentBufferSize)
  2766. {
  2767. unz_s* s;
  2768. unz_file_info file_info;
  2769. unz_file_info_internal file_info_internal;
  2770. int err=UNZ_OK;
  2771. uLong uMagic;
  2772. long lSeek=0;
  2773. if (file==NULL)
  2774. return UNZ_PARAMERROR;
  2775. s=(unz_s*)file;
  2776. if (lufseek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile,SEEK_SET)!=0)
  2777. err=UNZ_ERRNO;
  2778. // we check the magic
  2779. if (err==UNZ_OK)
  2780. if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  2781. err=UNZ_ERRNO;
  2782. else if (uMagic!=0x02014b50)
  2783. err=UNZ_BADZIPFILE;
  2784. if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
  2785. err=UNZ_ERRNO;
  2786. if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
  2787. err=UNZ_ERRNO;
  2788. if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
  2789. err=UNZ_ERRNO;
  2790. if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
  2791. err=UNZ_ERRNO;
  2792. if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
  2793. err=UNZ_ERRNO;
  2794. unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
  2795. if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
  2796. err=UNZ_ERRNO;
  2797. if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
  2798. err=UNZ_ERRNO;
  2799. if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
  2800. err=UNZ_ERRNO;
  2801. if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
  2802. err=UNZ_ERRNO;
  2803. if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
  2804. err=UNZ_ERRNO;
  2805. if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
  2806. err=UNZ_ERRNO;
  2807. if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
  2808. err=UNZ_ERRNO;
  2809. if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
  2810. err=UNZ_ERRNO;
  2811. if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
  2812. err=UNZ_ERRNO;
  2813. if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
  2814. err=UNZ_ERRNO;
  2815. lSeek+=file_info.size_filename;
  2816. if ((err==UNZ_OK) && (szFileName!=NULL))
  2817. {
  2818. uLong uSizeRead ;
  2819. if (file_info.size_filename<fileNameBufferSize)
  2820. {
  2821. *(szFileName+file_info.size_filename)='\0';
  2822. uSizeRead = file_info.size_filename;
  2823. }
  2824. else
  2825. uSizeRead = fileNameBufferSize;
  2826. if ((file_info.size_filename>0) && (fileNameBufferSize>0))
  2827. if (lufread(szFileName,(uInt)uSizeRead,1,s->file)!=1)
  2828. err=UNZ_ERRNO;
  2829. lSeek -= uSizeRead;
  2830. }
  2831. if ((err==UNZ_OK) && (extraField!=NULL))
  2832. {
  2833. uLong uSizeRead ;
  2834. if (file_info.size_file_extra<extraFieldBufferSize)
  2835. uSizeRead = file_info.size_file_extra;
  2836. else
  2837. uSizeRead = extraFieldBufferSize;
  2838. if (lSeek!=0)
  2839. if (lufseek(s->file,lSeek,SEEK_CUR)==0)
  2840. lSeek=0;
  2841. else
  2842. err=UNZ_ERRNO;
  2843. if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
  2844. if (lufread(extraField,(uInt)uSizeRead,1,s->file)!=1)
  2845. err=UNZ_ERRNO;
  2846. lSeek += file_info.size_file_extra - uSizeRead;
  2847. }
  2848. else
  2849. lSeek+=file_info.size_file_extra;
  2850. if ((err==UNZ_OK) && (szComment!=NULL))
  2851. {
  2852. uLong uSizeRead ;
  2853. if (file_info.size_file_comment<commentBufferSize)
  2854. {
  2855. *(szComment+file_info.size_file_comment)='\0';
  2856. uSizeRead = file_info.size_file_comment;
  2857. }
  2858. else
  2859. uSizeRead = commentBufferSize;
  2860. if (lSeek!=0)
  2861. if (lufseek(s->file,lSeek,SEEK_CUR)==0)
  2862. {} // unused lSeek=0;
  2863. else
  2864. err=UNZ_ERRNO;
  2865. if ((file_info.size_file_comment>0) && (commentBufferSize>0))
  2866. if (lufread(szComment,(uInt)uSizeRead,1,s->file)!=1)
  2867. err=UNZ_ERRNO;
  2868. //unused lSeek+=file_info.size_file_comment - uSizeRead;
  2869. }
  2870. else {} //unused lSeek+=file_info.size_file_comment;
  2871. if ((err==UNZ_OK) && (pfile_info!=NULL))
  2872. *pfile_info=file_info;
  2873. if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
  2874. *pfile_info_internal=file_info_internal;
  2875. return err;
  2876. }
  2877. // Write info about the ZipFile in the *pglobal_info structure.
  2878. // No preparation of the structure is needed
  2879. // return UNZ_OK if there is no problem.
  2880. int unzGetCurrentFileInfo (unzFile file, unz_file_info *pfile_info,
  2881. char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize,
  2882. char *szComment, uLong commentBufferSize)
  2883. { return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,szFileName,fileNameBufferSize,
  2884. extraField,extraFieldBufferSize, szComment,commentBufferSize);
  2885. }
  2886. // Set the current file of the zipfile to the first file.
  2887. // return UNZ_OK if there is no problem
  2888. int unzGoToFirstFile (unzFile file)
  2889. {
  2890. int err;
  2891. unz_s* s;
  2892. if (file==NULL) return UNZ_PARAMERROR;
  2893. s=(unz_s*)file;
  2894. s->pos_in_central_dir=s->offset_central_dir;
  2895. s->num_file=0;
  2896. err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  2897. &s->cur_file_info_internal,
  2898. NULL,0,NULL,0,NULL,0);
  2899. s->current_file_ok = (err == UNZ_OK);
  2900. return err;
  2901. }
  2902. // Set the current file of the zipfile to the next file.
  2903. // return UNZ_OK if there is no problem
  2904. // return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  2905. int unzGoToNextFile (unzFile file)
  2906. {
  2907. unz_s* s;
  2908. int err;
  2909. if (file==NULL)
  2910. return UNZ_PARAMERROR;
  2911. s=(unz_s*)file;
  2912. if (!s->current_file_ok)
  2913. return UNZ_END_OF_LIST_OF_FILE;
  2914. if (s->num_file+1==s->gi.number_entry)
  2915. return UNZ_END_OF_LIST_OF_FILE;
  2916. s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
  2917. s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
  2918. s->num_file++;
  2919. err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  2920. &s->cur_file_info_internal,
  2921. NULL,0,NULL,0,NULL,0);
  2922. s->current_file_ok = (err == UNZ_OK);
  2923. return err;
  2924. }
  2925. // Try locate the file szFileName in the zipfile.
  2926. // For the iCaseSensitivity signification, see unzStringFileNameCompare
  2927. // return value :
  2928. // UNZ_OK if the file is found. It becomes the current file.
  2929. // UNZ_END_OF_LIST_OF_FILE if the file is not found
  2930. int unzLocateFile (unzFile file, const TCHAR *szFileName, int iCaseSensitivity)
  2931. {
  2932. unz_s* s;
  2933. int err;
  2934. uLong num_fileSaved;
  2935. uLong pos_in_central_dirSaved;
  2936. if (file==NULL)
  2937. return UNZ_PARAMERROR;
  2938. if (_tcslen(szFileName)>=UNZ_MAXFILENAMEINZIP)
  2939. return UNZ_PARAMERROR;
  2940. char szFileNameA[MAX_PATH];
  2941. #ifdef _UNICODE
  2942. GetAnsiFileName(szFileName, szFileNameA, MAX_PATH-1);
  2943. #else
  2944. strcpy(szFileNameA, szFileName);
  2945. #endif
  2946. // support Windows subdirectory by:daviyang35
  2947. int iLen=strlen(szFileNameA);
  2948. for (int i=0;i<iLen;i++)
  2949. {
  2950. if (szFileNameA[i]=='\\')
  2951. {
  2952. szFileNameA[i]='/';
  2953. }
  2954. }
  2955. s=(unz_s*)file;
  2956. if (!s->current_file_ok)
  2957. return UNZ_END_OF_LIST_OF_FILE;
  2958. num_fileSaved = s->num_file;
  2959. pos_in_central_dirSaved = s->pos_in_central_dir;
  2960. err = unzGoToFirstFile(file);
  2961. while (err == UNZ_OK)
  2962. {
  2963. char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
  2964. unzGetCurrentFileInfo(file,NULL,
  2965. szCurrentFileName,sizeof(szCurrentFileName)-1,
  2966. NULL,0,NULL,0);
  2967. if (unzStringFileNameCompare(szCurrentFileName,szFileNameA,iCaseSensitivity)==0)
  2968. return UNZ_OK;
  2969. err = unzGoToNextFile(file);
  2970. }
  2971. s->num_file = num_fileSaved ;
  2972. s->pos_in_central_dir = pos_in_central_dirSaved ;
  2973. return err;
  2974. }
  2975. // Read the local header of the current zipfile
  2976. // Check the coherency of the local header and info in the end of central
  2977. // directory about this file
  2978. // store in *piSizeVar the size of extra info in local header
  2979. // (filename and size of extra field data)
  2980. int unzlocal_CheckCurrentFileCoherencyHeader (unz_s *s,uInt *piSizeVar,
  2981. uLong *poffset_local_extrafield, uInt *psize_local_extrafield)
  2982. {
  2983. uLong uMagic,uData,uFlags;
  2984. uLong size_filename;
  2985. uLong size_extra_field;
  2986. int err=UNZ_OK;
  2987. *piSizeVar = 0;
  2988. *poffset_local_extrafield = 0;
  2989. *psize_local_extrafield = 0;
  2990. if (lufseek(s->file,s->cur_file_info_internal.offset_curfile + s->byte_before_the_zipfile,SEEK_SET)!=0)
  2991. return UNZ_ERRNO;
  2992. if (err==UNZ_OK)
  2993. if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  2994. err=UNZ_ERRNO;
  2995. else if (uMagic!=0x04034b50)
  2996. err=UNZ_BADZIPFILE;
  2997. if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  2998. err=UNZ_ERRNO;
  2999. // else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
  3000. // err=UNZ_BADZIPFILE;
  3001. if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
  3002. err=UNZ_ERRNO;
  3003. if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  3004. err=UNZ_ERRNO;
  3005. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
  3006. err=UNZ_BADZIPFILE;
  3007. if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
  3008. (s->cur_file_info.compression_method!=Z_DEFLATED))
  3009. err=UNZ_BADZIPFILE;
  3010. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // date/time
  3011. err=UNZ_ERRNO;
  3012. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // crc
  3013. err=UNZ_ERRNO;
  3014. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
  3015. ((uFlags & 8)==0))
  3016. err=UNZ_BADZIPFILE;
  3017. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // size compr
  3018. err=UNZ_ERRNO;
  3019. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
  3020. ((uFlags & 8)==0))
  3021. err=UNZ_BADZIPFILE;
  3022. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) // size uncompr
  3023. err=UNZ_ERRNO;
  3024. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
  3025. ((uFlags & 8)==0))
  3026. err=UNZ_BADZIPFILE;
  3027. if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
  3028. err=UNZ_ERRNO;
  3029. else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
  3030. err=UNZ_BADZIPFILE;
  3031. *piSizeVar += (uInt)size_filename;
  3032. if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
  3033. err=UNZ_ERRNO;
  3034. *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
  3035. SIZEZIPLOCALHEADER + size_filename;
  3036. *psize_local_extrafield = (uInt)size_extra_field;
  3037. *piSizeVar += (uInt)size_extra_field;
  3038. return err;
  3039. }
  3040. // Open for reading data the current file in the zipfile.
  3041. // If there is no error and the file is opened, the return value is UNZ_OK.
  3042. int unzOpenCurrentFile (unzFile file)
  3043. {
  3044. int err;
  3045. int Store;
  3046. uInt iSizeVar;
  3047. unz_s* s;
  3048. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3049. uLong offset_local_extrafield; // offset of the local extra field
  3050. uInt size_local_extrafield; // size of the local extra field
  3051. if (file==NULL)
  3052. return UNZ_PARAMERROR;
  3053. s=(unz_s*)file;
  3054. if (!s->current_file_ok)
  3055. return UNZ_PARAMERROR;
  3056. if (s->pfile_in_zip_read != NULL)
  3057. unzCloseCurrentFile(file);
  3058. if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
  3059. &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
  3060. return UNZ_BADZIPFILE;
  3061. pfile_in_zip_read_info = (file_in_zip_read_info_s*)zmalloc(sizeof(file_in_zip_read_info_s));
  3062. if (pfile_in_zip_read_info==NULL)
  3063. return UNZ_INTERNALERROR;
  3064. pfile_in_zip_read_info->read_buffer=(char*)zmalloc(UNZ_BUFSIZE);
  3065. pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
  3066. pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
  3067. pfile_in_zip_read_info->pos_local_extrafield=0;
  3068. if (pfile_in_zip_read_info->read_buffer==NULL)
  3069. {
  3070. if (pfile_in_zip_read_info!=0) zfree(pfile_in_zip_read_info); //unused pfile_in_zip_read_info=0;
  3071. return UNZ_INTERNALERROR;
  3072. }
  3073. pfile_in_zip_read_info->stream_initialised=0;
  3074. if ((s->cur_file_info.compression_method!=0) && (s->cur_file_info.compression_method!=Z_DEFLATED))
  3075. { // unused err=UNZ_BADZIPFILE;
  3076. }
  3077. Store = s->cur_file_info.compression_method==0;
  3078. pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
  3079. pfile_in_zip_read_info->crc32=0;
  3080. pfile_in_zip_read_info->compression_method =
  3081. s->cur_file_info.compression_method;
  3082. pfile_in_zip_read_info->file=s->file;
  3083. pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
  3084. pfile_in_zip_read_info->stream.total_out = 0;
  3085. if (!Store)
  3086. {
  3087. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  3088. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  3089. pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  3090. err=inflateInit2(&pfile_in_zip_read_info->stream);
  3091. if (err == Z_OK)
  3092. pfile_in_zip_read_info->stream_initialised=1;
  3093. // windowBits is passed < 0 to tell that there is no zlib header.
  3094. // Note that in this case inflate *requires* an extra "dummy" byte
  3095. // after the compressed stream in order to complete decompression and
  3096. // return Z_STREAM_END.
  3097. // In unzip, i don't wait absolutely Z_STREAM_END because I known the
  3098. // size of both compressed and uncompressed data
  3099. }
  3100. pfile_in_zip_read_info->rest_read_compressed =
  3101. s->cur_file_info.compressed_size ;
  3102. pfile_in_zip_read_info->rest_read_uncompressed =
  3103. s->cur_file_info.uncompressed_size ;
  3104. pfile_in_zip_read_info->pos_in_zipfile =
  3105. s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
  3106. iSizeVar;
  3107. pfile_in_zip_read_info->stream.avail_in = (uInt)0;
  3108. s->pfile_in_zip_read = pfile_in_zip_read_info;
  3109. return UNZ_OK;
  3110. }
  3111. // Read bytes from the current file.
  3112. // buf contain buffer where data must be copied
  3113. // len the size of buf.
  3114. // return the number of byte copied if somes bytes are copied
  3115. // return 0 if the end of file was reached
  3116. // return <0 with error code if there is an error
  3117. // (UNZ_ERRNO for IO error, or zLib error for uncompress error)
  3118. int unzReadCurrentFile (unzFile file, voidp buf, unsigned len)
  3119. { int err=UNZ_OK;
  3120. uInt iRead = 0;
  3121. unz_s *s = (unz_s*)file;
  3122. if (s==NULL) return UNZ_PARAMERROR;
  3123. file_in_zip_read_info_s* pfile_in_zip_read_info = s->pfile_in_zip_read;
  3124. if (pfile_in_zip_read_info==NULL) return UNZ_PARAMERROR;
  3125. if ((pfile_in_zip_read_info->read_buffer == NULL)) return UNZ_END_OF_LIST_OF_FILE;
  3126. if (len==0) return 0;
  3127. pfile_in_zip_read_info->stream.next_out = (Byte*)buf;
  3128. pfile_in_zip_read_info->stream.avail_out = (uInt)len;
  3129. if (len>pfile_in_zip_read_info->rest_read_uncompressed)
  3130. { pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
  3131. }
  3132. while (pfile_in_zip_read_info->stream.avail_out>0)
  3133. { if ((pfile_in_zip_read_info->stream.avail_in==0) && (pfile_in_zip_read_info->rest_read_compressed>0))
  3134. { uInt uReadThis = UNZ_BUFSIZE;
  3135. if (pfile_in_zip_read_info->rest_read_compressed<uReadThis) uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
  3136. if (uReadThis == 0) return UNZ_EOF;
  3137. if (lufseek(pfile_in_zip_read_info->file, pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile,SEEK_SET)!=0) return UNZ_ERRNO;
  3138. if (lufread(pfile_in_zip_read_info->read_buffer,uReadThis,1,pfile_in_zip_read_info->file)!=1) return UNZ_ERRNO;
  3139. pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
  3140. pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
  3141. pfile_in_zip_read_info->stream.next_in = (Byte*)pfile_in_zip_read_info->read_buffer;
  3142. pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
  3143. }
  3144. if (pfile_in_zip_read_info->compression_method==0)
  3145. { uInt uDoCopy,i ;
  3146. if (pfile_in_zip_read_info->stream.avail_out < pfile_in_zip_read_info->stream.avail_in)
  3147. { uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
  3148. }
  3149. else
  3150. { uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
  3151. }
  3152. for (i=0;i<uDoCopy;i++)
  3153. { *(pfile_in_zip_read_info->stream.next_out+i) = *(pfile_in_zip_read_info->stream.next_in+i);
  3154. }
  3155. pfile_in_zip_read_info->crc32 = ucrc32(pfile_in_zip_read_info->crc32,pfile_in_zip_read_info->stream.next_out,uDoCopy);
  3156. pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
  3157. pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
  3158. pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
  3159. pfile_in_zip_read_info->stream.next_out += uDoCopy;
  3160. pfile_in_zip_read_info->stream.next_in += uDoCopy;
  3161. pfile_in_zip_read_info->stream.total_out += uDoCopy;
  3162. iRead += uDoCopy;
  3163. }
  3164. else
  3165. { uLong uTotalOutBefore,uTotalOutAfter;
  3166. const Byte *bufBefore;
  3167. uLong uOutThis;
  3168. int flush=Z_SYNC_FLUSH;
  3169. uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
  3170. bufBefore = pfile_in_zip_read_info->stream.next_out;
  3171. err=inflate(&pfile_in_zip_read_info->stream,flush);
  3172. uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
  3173. uOutThis = uTotalOutAfter-uTotalOutBefore;
  3174. pfile_in_zip_read_info->crc32 = ucrc32(pfile_in_zip_read_info->crc32,bufBefore,(uInt)(uOutThis));
  3175. pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
  3176. iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
  3177. if (err==Z_STREAM_END) return (iRead==0) ? UNZ_EOF : iRead; //+++1.3
  3178. //if (err==Z_STREAM_END) return (iRead==len) ? UNZ_EOF : iRead; //+++1.2
  3179. if (err != Z_OK) break;
  3180. }
  3181. }
  3182. if (err==Z_OK) return iRead;
  3183. return iRead;
  3184. }
  3185. // Give the current position in uncompressed data
  3186. z_off_t unztell (unzFile file)
  3187. {
  3188. unz_s* s;
  3189. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3190. if (file==NULL)
  3191. return UNZ_PARAMERROR;
  3192. s=(unz_s*)file;
  3193. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3194. if (pfile_in_zip_read_info==NULL)
  3195. return UNZ_PARAMERROR;
  3196. return (z_off_t)pfile_in_zip_read_info->stream.total_out;
  3197. }
  3198. // return 1 if the end of file was reached, 0 elsewhere
  3199. int unzeof (unzFile file)
  3200. {
  3201. unz_s* s;
  3202. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3203. if (file==NULL)
  3204. return UNZ_PARAMERROR;
  3205. s=(unz_s*)file;
  3206. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3207. if (pfile_in_zip_read_info==NULL)
  3208. return UNZ_PARAMERROR;
  3209. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  3210. return 1;
  3211. else
  3212. return 0;
  3213. }
  3214. // Read extra field from the current file (opened by unzOpenCurrentFile)
  3215. // This is the local-header version of the extra field (sometimes, there is
  3216. // more info in the local-header version than in the central-header)
  3217. // if buf==NULL, it return the size of the local extra field that can be read
  3218. // if buf!=NULL, len is the size of the buffer, the extra header is copied in buf.
  3219. // the return value is the number of bytes copied in buf, or (if <0) the error code
  3220. int unzGetLocalExtrafield (unzFile file,voidp buf,unsigned len)
  3221. {
  3222. unz_s* s;
  3223. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3224. uInt read_now;
  3225. uLong size_to_read;
  3226. if (file==NULL)
  3227. return UNZ_PARAMERROR;
  3228. s=(unz_s*)file;
  3229. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3230. if (pfile_in_zip_read_info==NULL)
  3231. return UNZ_PARAMERROR;
  3232. size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
  3233. pfile_in_zip_read_info->pos_local_extrafield);
  3234. if (buf==NULL)
  3235. return (int)size_to_read;
  3236. if (len>size_to_read)
  3237. read_now = (uInt)size_to_read;
  3238. else
  3239. read_now = (uInt)len ;
  3240. if (read_now==0)
  3241. return 0;
  3242. if (lufseek(pfile_in_zip_read_info->file, pfile_in_zip_read_info->offset_local_extrafield + pfile_in_zip_read_info->pos_local_extrafield,SEEK_SET)!=0)
  3243. return UNZ_ERRNO;
  3244. if (lufread(buf,(uInt)size_to_read,1,pfile_in_zip_read_info->file)!=1)
  3245. return UNZ_ERRNO;
  3246. return (int)read_now;
  3247. }
  3248. // Close the file in zip opened with unzipOpenCurrentFile
  3249. // Return UNZ_CRCERROR if all the file was read but the CRC is not good
  3250. int unzCloseCurrentFile (unzFile file)
  3251. {
  3252. int err=UNZ_OK;
  3253. unz_s* s;
  3254. file_in_zip_read_info_s* pfile_in_zip_read_info;
  3255. if (file==NULL)
  3256. return UNZ_PARAMERROR;
  3257. s=(unz_s*)file;
  3258. pfile_in_zip_read_info=s->pfile_in_zip_read;
  3259. if (pfile_in_zip_read_info==NULL)
  3260. return UNZ_PARAMERROR;
  3261. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  3262. {
  3263. if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
  3264. err=UNZ_CRCERROR;
  3265. }
  3266. if (pfile_in_zip_read_info->read_buffer!=0)
  3267. { void *buf = pfile_in_zip_read_info->read_buffer;
  3268. zfree(buf);
  3269. pfile_in_zip_read_info->read_buffer=0;
  3270. }
  3271. pfile_in_zip_read_info->read_buffer = NULL;
  3272. if (pfile_in_zip_read_info->stream_initialised)
  3273. inflateEnd(&pfile_in_zip_read_info->stream);
  3274. pfile_in_zip_read_info->stream_initialised = 0;
  3275. if (pfile_in_zip_read_info!=0) zfree(pfile_in_zip_read_info); // unused pfile_in_zip_read_info=0;
  3276. s->pfile_in_zip_read=NULL;
  3277. return err;
  3278. }
  3279. // Get the global comment string of the ZipFile, in the szComment buffer.
  3280. // uSizeBuf is the size of the szComment buffer.
  3281. // return the number of byte copied or an error code <0
  3282. int unzGetGlobalComment (unzFile file, char *szComment, uLong uSizeBuf)
  3283. { //int err=UNZ_OK;
  3284. unz_s* s;
  3285. uLong uReadThis ;
  3286. if (file==NULL) return UNZ_PARAMERROR;
  3287. s=(unz_s*)file;
  3288. uReadThis = uSizeBuf;
  3289. if (uReadThis>s->gi.size_comment) uReadThis = s->gi.size_comment;
  3290. if (lufseek(s->file,s->central_pos+22,SEEK_SET)!=0) return UNZ_ERRNO;
  3291. if (uReadThis>0)
  3292. { *szComment='\0';
  3293. if (lufread(szComment,(uInt)uReadThis,1,s->file)!=1) return UNZ_ERRNO;
  3294. }
  3295. if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment)) *(szComment+s->gi.size_comment)='\0';
  3296. return (int)uReadThis;
  3297. }
  3298. int unzOpenCurrentFile (unzFile file);
  3299. int unzReadCurrentFile (unzFile file, void *buf, unsigned len);
  3300. int unzCloseCurrentFile (unzFile file);
  3301. FILETIME timet2filetime(time_t timer)
  3302. {
  3303. struct tm *tm = gmtime(&timer);
  3304. SYSTEMTIME st;
  3305. st.wYear = (WORD)(tm->tm_year+1900);
  3306. st.wMonth = (WORD)(tm->tm_mon+1);
  3307. st.wDay = (WORD)(tm->tm_mday);
  3308. st.wHour = (WORD)(tm->tm_hour);
  3309. st.wMinute = (WORD)(tm->tm_min);
  3310. st.wSecond = (WORD)(tm->tm_sec);
  3311. st.wMilliseconds=0;
  3312. FILETIME ft;
  3313. SystemTimeToFileTime(&st,&ft);
  3314. return ft;
  3315. }
  3316. ///////////////////////////////////////////////////////////////////////////////
  3317. ///////////////////////////////////////////////////////////////////////////////
  3318. ///////////////////////////////////////////////////////////////////////////////
  3319. class TUnzip
  3320. { public:
  3321. TUnzip() : uf(0), currentfile(-1), czei(-1) {}
  3322. unzFile uf; int currentfile; ZIPENTRY cze; int czei;
  3323. TCHAR rootdir[MAX_PATH];
  3324. ZRESULT Open(void *z,unsigned int len,DWORD flags);
  3325. ZRESULT Get(int index,ZIPENTRY *ze);
  3326. ZRESULT Find(const TCHAR *name,bool ic,int *index,ZIPENTRY *ze);
  3327. ZRESULT Unzip(int index,void *dst,unsigned int len,DWORD flags);
  3328. ZRESULT Close();
  3329. };
  3330. ZRESULT TUnzip::Open(void *z,unsigned int len,DWORD flags)
  3331. {
  3332. if (uf!=0 || currentfile!=-1)
  3333. return ZR_NOTINITED;
  3334. GetCurrentDirectory(MAX_PATH,rootdir);
  3335. _tcscat(rootdir,_T("\\"));
  3336. if (flags==ZIP_HANDLE)
  3337. {
  3338. DWORD type = GetFileType(z);
  3339. if (type!=FILE_TYPE_DISK)
  3340. return ZR_SEEK;
  3341. }
  3342. ZRESULT e;
  3343. LUFILE *f = lufopen(z,len,flags,&e);
  3344. if (f==NULL)
  3345. return e;
  3346. uf = unzOpenInternal(f);
  3347. //return ZR_OK;
  3348. return zopenerror; //+++1.2
  3349. }
  3350. ZRESULT TUnzip::Get(int index,ZIPENTRY *ze)
  3351. { if (index<-1 || index>=(int)uf->gi.number_entry)
  3352. return ZR_ARGS;
  3353. if (currentfile!=-1)
  3354. unzCloseCurrentFile(uf);
  3355. currentfile=-1;
  3356. if (index==czei && index!=-1) {memcpy(ze,&cze,sizeof(ZIPENTRY)); return ZR_OK;}
  3357. if (index==-1)
  3358. { ze->index = uf->gi.number_entry;
  3359. ze->name[0]=0;
  3360. ze->attr=0;
  3361. ze->atime.dwLowDateTime=0; ze->atime.dwHighDateTime=0;
  3362. ze->ctime.dwLowDateTime=0; ze->ctime.dwHighDateTime=0;
  3363. ze->mtime.dwLowDateTime=0; ze->mtime.dwHighDateTime=0;
  3364. ze->comp_size=0;
  3365. ze->unc_size=0;
  3366. return ZR_OK;
  3367. }
  3368. if (index<(int)uf->num_file) unzGoToFirstFile(uf);
  3369. while ((int)uf->num_file<index) unzGoToNextFile(uf);
  3370. unz_file_info ufi;
  3371. char fn[MAX_PATH];
  3372. unzGetCurrentFileInfo(uf,&ufi,fn,MAX_PATH,NULL,0,NULL,0);
  3373. // now get the extra header. We do this ourselves, instead of
  3374. // calling unzOpenCurrentFile &c., to avoid allocating more than necessary.
  3375. unsigned int extralen,iSizeVar; unsigned long offset;
  3376. int res = unzlocal_CheckCurrentFileCoherencyHeader(uf,&iSizeVar,&offset,&extralen);
  3377. if (res!=UNZ_OK) return ZR_CORRUPT;
  3378. if (lufseek(uf->file,offset,SEEK_SET)!=0) return ZR_READ;
  3379. char *extra = new char[extralen];
  3380. if (lufread(extra,1,(uInt)extralen,uf->file)!=extralen) {delete[] extra; return ZR_READ;}
  3381. //
  3382. ze->index=uf->num_file;
  3383. strcpy(ze->name,fn);
  3384. // zip has an 'attribute' 32bit value. Its lower half is windows stuff
  3385. // its upper half is standard unix attr.
  3386. unsigned long a = ufi.external_fa;
  3387. bool uisdir = (a&0x40000000)!=0;
  3388. //bool uwriteable= (a&0x08000000)!=0;
  3389. bool uwriteable= (a&0x00800000)!=0; // ***hd***
  3390. //bool ureadable= (a&0x01000000)!=0;
  3391. //bool uexecutable=(a&0x00400000)!=0;
  3392. bool wreadonly= (a&0x00000001)!=0;
  3393. bool whidden= (a&0x00000002)!=0;
  3394. bool wsystem= (a&0x00000004)!=0;
  3395. bool wisdir= (a&0x00000010)!=0;
  3396. bool warchive= (a&0x00000020)!=0;
  3397. ze->attr=FILE_ATTRIBUTE_NORMAL;
  3398. if (uisdir || wisdir) ze->attr |= FILE_ATTRIBUTE_DIRECTORY;
  3399. if (warchive) ze->attr|=FILE_ATTRIBUTE_ARCHIVE;
  3400. if (whidden) ze->attr|=FILE_ATTRIBUTE_HIDDEN;
  3401. if (!uwriteable||wreadonly) ze->attr|=FILE_ATTRIBUTE_READONLY;
  3402. if (wsystem) ze->attr|=FILE_ATTRIBUTE_SYSTEM;
  3403. ze->comp_size = ufi.compressed_size;
  3404. ze->unc_size = ufi.uncompressed_size;
  3405. //
  3406. WORD dostime = (WORD)(ufi.dosDate&0xFFFF);
  3407. WORD dosdate = (WORD)((ufi.dosDate>>16)&0xFFFF);
  3408. FILETIME ft;
  3409. DosDateTimeToFileTime(dosdate,dostime,&ft);
  3410. ze->atime=ft; ze->ctime=ft; ze->mtime=ft;
  3411. // the zip will always have at least that dostime. But if it also has
  3412. // an extra header, then we'll instead get the info from that.
  3413. unsigned int epos=0;
  3414. while (epos+4<extralen)
  3415. { char etype[3]; etype[0]=extra[epos+0]; etype[1]=extra[epos+1]; etype[2]=0;
  3416. int size = extra[epos+2];
  3417. if (strcmp(etype,"UT")!=0) {epos += 4+size; continue;}
  3418. int flags = extra[epos+4];
  3419. bool hasmtime = (flags&1)!=0;
  3420. bool hasatime = (flags&2)!=0;
  3421. bool hasctime = (flags&4)!=0;
  3422. epos+=5;
  3423. if (hasmtime)
  3424. { time_t mtime = *(__time32_t*)(extra+epos); epos+=4;
  3425. ze->mtime = timet2filetime(mtime);
  3426. }
  3427. if (hasatime)
  3428. { time_t atime = *(__time32_t*)(extra+epos); epos+=4;
  3429. ze->atime = timet2filetime(atime);
  3430. }
  3431. if (hasctime)
  3432. { time_t ctime = *(__time32_t*)(extra+epos);
  3433. ze->ctime = timet2filetime(ctime);
  3434. }
  3435. break;
  3436. }
  3437. //
  3438. if (extra!=0) delete[] extra;
  3439. memcpy(&cze,ze,sizeof(ZIPENTRY)); czei=index;
  3440. return ZR_OK;
  3441. }
  3442. ZRESULT TUnzip::Find(const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
  3443. {
  3444. int res = unzLocateFile(uf,name,ic?CASE_INSENSITIVE:CASE_SENSITIVE);
  3445. if (res!=UNZ_OK)
  3446. {
  3447. if (index!=0)
  3448. *index=-1;
  3449. if (ze!=NULL)
  3450. {
  3451. ZeroMemory(ze,sizeof(ZIPENTRY)); ze->index=-1;
  3452. }
  3453. return ZR_NOTFOUND;
  3454. }
  3455. if (currentfile!=-1)
  3456. unzCloseCurrentFile(uf); currentfile=-1;
  3457. int i = (int)uf->num_file;
  3458. if (index!=NULL)
  3459. *index=i;
  3460. if (ze!=NULL)
  3461. {
  3462. ZRESULT zres = Get(i,ze);
  3463. if (zres!=ZR_OK)
  3464. return zres;
  3465. }
  3466. return ZR_OK;
  3467. }
  3468. void EnsureDirectory(const TCHAR *rootdir, const TCHAR *dir)
  3469. {
  3470. if (dir==NULL || dir[0] == _T('\0'))
  3471. return;
  3472. const TCHAR *lastslash = dir, *c = lastslash;
  3473. while (*c != _T('\0'))
  3474. {
  3475. if (*c==_T('/') || *c==_T('\\'))
  3476. lastslash=c;
  3477. c++;
  3478. }
  3479. const TCHAR *name=lastslash;
  3480. if (lastslash!=dir)
  3481. {
  3482. TCHAR tmp[MAX_PATH];
  3483. _tcsncpy(tmp, dir, lastslash-dir);
  3484. tmp[lastslash-dir] = _T('\0');
  3485. EnsureDirectory(rootdir,tmp);
  3486. name++;
  3487. }
  3488. TCHAR cd[MAX_PATH];
  3489. _tcscpy(cd,rootdir);
  3490. //_tcscat(cd,name);
  3491. _tcscat(cd,dir); //+++1.2
  3492. CreateDirectory(cd,NULL);
  3493. }
  3494. ZRESULT TUnzip::Unzip(int index,void *dst,unsigned int len,DWORD flags)
  3495. {
  3496. if (flags!=ZIP_MEMORY && flags!=ZIP_FILENAME && flags!=ZIP_HANDLE)
  3497. return ZR_ARGS;
  3498. if (flags==ZIP_MEMORY)
  3499. {
  3500. if (index!=currentfile)
  3501. {
  3502. if (currentfile!=-1)
  3503. unzCloseCurrentFile(uf);
  3504. currentfile=-1;
  3505. if (index>=(int)uf->gi.number_entry)
  3506. return ZR_ARGS;
  3507. if (index<(int)uf->num_file)
  3508. unzGoToFirstFile(uf);
  3509. while ((int)uf->num_file<index)
  3510. unzGoToNextFile(uf);
  3511. unzOpenCurrentFile(uf);
  3512. currentfile=index;
  3513. }
  3514. int res = unzReadCurrentFile(uf,dst,len);
  3515. if (res>0)
  3516. return ZR_MORE;
  3517. unzCloseCurrentFile(uf);
  3518. currentfile=-1;
  3519. if (res==0)
  3520. return ZR_OK;
  3521. else
  3522. return ZR_FLATE;
  3523. }
  3524. // otherwise we're writing to a handle or a file
  3525. if (currentfile!=-1)
  3526. unzCloseCurrentFile(uf);
  3527. currentfile=-1;
  3528. if (index >= (int)uf->gi.number_entry)
  3529. return ZR_ARGS;
  3530. if (index < (int)uf->num_file)
  3531. unzGoToFirstFile(uf);
  3532. while ((int)uf->num_file<index)
  3533. unzGoToNextFile(uf);
  3534. ZIPENTRY ze;
  3535. Get(index,&ze);
  3536. // zipentry=directory is handled specially
  3537. if ((ze.attr & FILE_ATTRIBUTE_DIRECTORY) != 0)
  3538. {
  3539. if (flags==ZIP_HANDLE)
  3540. return ZR_OK; // don't do anything
  3541. #ifdef _UNICODE
  3542. TCHAR uname[MAX_PATH];
  3543. GetUnicodeFileName(ze.name, uname, MAX_PATH-1);
  3544. EnsureDirectory(rootdir, uname);
  3545. #else
  3546. EnsureDirectory(rootdir, ze.name);
  3547. #endif
  3548. return ZR_OK;
  3549. }
  3550. // otherwise, we write the zipentry to a file/handle
  3551. HANDLE h;
  3552. if (flags==ZIP_HANDLE)
  3553. h=dst;
  3554. else
  3555. {
  3556. const TCHAR *name = (const TCHAR *)dst;
  3557. const TCHAR *c = name;
  3558. while (*c)
  3559. {
  3560. if (*c == _T('/') || *c == _T('\\'))
  3561. name = c + 1;
  3562. c++;
  3563. }
  3564. // if it's a relative filename, ensure directories. We do this as a service
  3565. // to the caller so they can just unzip straight unto ze.name.
  3566. if (name != (const TCHAR *)dst)
  3567. {
  3568. TCHAR dir[MAX_PATH];
  3569. _tcscpy(dir,(const TCHAR*)dst);
  3570. dir[name-(const TCHAR*)dst-1] = _T('\0');
  3571. bool isabsolute = (dir[0]==_T('/') || dir[0]==_T('\\') || dir[1]==_T(':'));
  3572. isabsolute |= (_tcsstr(dir,_T("../"))!=0) | (_tcsstr(dir,_T("..\\"))!=0);
  3573. if (!isabsolute)
  3574. EnsureDirectory(rootdir,dir);
  3575. }
  3576. h = ::CreateFile((const TCHAR*)dst, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
  3577. ze.attr, NULL);
  3578. }
  3579. if (h == INVALID_HANDLE_VALUE)
  3580. return ZR_NOFILE;
  3581. unzOpenCurrentFile(uf);
  3582. BYTE buf[16384];
  3583. bool haderr=false;
  3584. for (;;)
  3585. {
  3586. int res = unzReadCurrentFile(uf,buf,16384);
  3587. if (res<0)
  3588. {
  3589. haderr=true;
  3590. break;
  3591. }
  3592. if (res==0)
  3593. break;
  3594. DWORD writ;
  3595. BOOL bres = WriteFile(h,buf,res,&writ,NULL);
  3596. if (!bres)
  3597. {
  3598. haderr=true;
  3599. break;
  3600. }
  3601. }
  3602. bool settime=false;
  3603. DWORD type = GetFileType(h);
  3604. if (type==FILE_TYPE_DISK && !haderr)
  3605. settime=true;
  3606. if (settime)
  3607. SetFileTime(h,&ze.ctime,&ze.atime,&ze.mtime);
  3608. if (flags!=ZIP_HANDLE)
  3609. CloseHandle(h);
  3610. unzCloseCurrentFile(uf);
  3611. if (haderr)
  3612. return ZR_WRITE;
  3613. return ZR_OK;
  3614. }
  3615. ZRESULT TUnzip::Close()
  3616. { if (currentfile!=-1) unzCloseCurrentFile(uf); currentfile=-1;
  3617. if (uf!=0) unzClose(uf); uf=0;
  3618. return ZR_OK;
  3619. }
  3620. ZRESULT lasterrorU=ZR_OK;
  3621. unsigned int FormatZipMessageU(ZRESULT code, char *buf,unsigned int len)
  3622. { if (code==ZR_RECENT) code=lasterrorU;
  3623. const char *msg="unknown zip result code";
  3624. switch (code)
  3625. { case ZR_OK: msg="Success"; break;
  3626. case ZR_NODUPH: msg="Culdn't duplicate handle"; break;
  3627. case ZR_NOFILE: msg="Couldn't create/open file"; break;
  3628. case ZR_NOALLOC: msg="Failed to allocate memory"; break;
  3629. case ZR_WRITE: msg="Error writing to file"; break;
  3630. case ZR_NOTFOUND: msg="File not found in the zipfile"; break;
  3631. case ZR_MORE: msg="Still more data to unzip"; break;
  3632. case ZR_CORRUPT: msg="Zipfile is corrupt or not a zipfile"; break;
  3633. case ZR_READ: msg="Error reading file"; break;
  3634. case ZR_ARGS: msg="Caller: faulty arguments"; break;
  3635. case ZR_PARTIALUNZ: msg="Caller: the file had already been partially unzipped"; break;
  3636. case ZR_NOTMMAP: msg="Caller: can only get memory of a memory zipfile"; break;
  3637. case ZR_MEMSIZE: msg="Caller: not enough space allocated for memory zipfile"; break;
  3638. case ZR_FAILED: msg="Caller: there was a previous error"; break;
  3639. case ZR_ENDED: msg="Caller: additions to the zip have already been ended"; break;
  3640. case ZR_ZMODE: msg="Caller: mixing creation and opening of zip"; break;
  3641. case ZR_NOTINITED: msg="Zip-bug: internal initialisation not completed"; break;
  3642. case ZR_SEEK: msg="Zip-bug: trying to seek the unseekable"; break;
  3643. case ZR_MISSIZE: msg="Zip-bug: the anticipated size turned out wrong"; break;
  3644. case ZR_NOCHANGE: msg="Zip-bug: tried to change mind, but not allowed"; break;
  3645. case ZR_FLATE: msg="Zip-bug: an internal error during flation"; break;
  3646. }
  3647. unsigned int mlen=(unsigned int)strlen(msg);
  3648. if (buf==0 || len==0) return mlen;
  3649. unsigned int n=mlen; if (n+1>len) n=len-1;
  3650. strncpy(buf,msg,n); buf[n]=0;
  3651. return mlen;
  3652. }
  3653. typedef struct
  3654. { DWORD flag;
  3655. TUnzip *unz;
  3656. } TUnzipHandleData;
  3657. HZIP OpenZipU(void *z,unsigned int len,DWORD flags)
  3658. {
  3659. TUnzip *unz = new TUnzip();
  3660. lasterrorU = unz->Open(z,len,flags);
  3661. if (lasterrorU!=ZR_OK)
  3662. {
  3663. delete unz;
  3664. return 0;
  3665. }
  3666. TUnzipHandleData *han = new TUnzipHandleData;
  3667. han->flag=1;
  3668. han->unz=unz;
  3669. return (HZIP)han;
  3670. }
  3671. ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze)
  3672. {
  3673. if (hz==0)
  3674. {
  3675. lasterrorU=ZR_ARGS;
  3676. return ZR_ARGS;
  3677. }
  3678. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3679. if (han->flag!=1)
  3680. {
  3681. lasterrorU=ZR_ZMODE;
  3682. return ZR_ZMODE;
  3683. }
  3684. TUnzip *unz = han->unz;
  3685. lasterrorU = unz->Get(index,ze);
  3686. return lasterrorU;
  3687. }
  3688. ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *zew)
  3689. {
  3690. if (hz==0)
  3691. {
  3692. lasterrorU=ZR_ARGS;
  3693. return ZR_ARGS;
  3694. }
  3695. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3696. if (han->flag!=1)
  3697. {
  3698. lasterrorU=ZR_ZMODE;
  3699. return ZR_ZMODE;
  3700. }
  3701. TUnzip *unz = han->unz;
  3702. ZIPENTRY ze;
  3703. lasterrorU = unz->Get(index,&ze);
  3704. if (lasterrorU == ZR_OK)
  3705. {
  3706. zew->index = ze.index;
  3707. zew->attr = ze.attr;
  3708. zew->atime = ze.atime;
  3709. zew->ctime = ze.ctime;
  3710. zew->mtime = ze.mtime;
  3711. zew->comp_size = ze.comp_size;
  3712. zew->unc_size = ze.unc_size;
  3713. #ifdef _UNICODE
  3714. GetUnicodeFileName(ze.name, zew->name, MAX_PATH-1);
  3715. #else
  3716. strcpy(zew->name, ze.name);
  3717. #endif
  3718. }
  3719. return lasterrorU;
  3720. }
  3721. ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze)
  3722. {
  3723. if (hz==0)
  3724. {
  3725. lasterrorU=ZR_ARGS;
  3726. return ZR_ARGS;
  3727. }
  3728. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3729. if (han->flag!=1)
  3730. {
  3731. lasterrorU=ZR_ZMODE;
  3732. return ZR_ZMODE;
  3733. }
  3734. TUnzip *unz = han->unz;
  3735. lasterrorU = unz->Find(name,ic,index,ze);
  3736. return lasterrorU;
  3737. }
  3738. ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *zew)
  3739. {
  3740. if (hz==0)
  3741. {
  3742. lasterrorU=ZR_ARGS;
  3743. return ZR_ARGS;
  3744. }
  3745. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3746. if (han->flag!=1)
  3747. {
  3748. lasterrorU=ZR_ZMODE;
  3749. return ZR_ZMODE;
  3750. }
  3751. TUnzip *unz = han->unz;
  3752. ZIPENTRY ze;
  3753. lasterrorU = unz->Find(name,ic,index,&ze);
  3754. if (lasterrorU == ZR_OK)
  3755. {
  3756. zew->index = ze.index;
  3757. zew->attr = ze.attr;
  3758. zew->atime = ze.atime;
  3759. zew->ctime = ze.ctime;
  3760. zew->mtime = ze.mtime;
  3761. zew->comp_size = ze.comp_size;
  3762. zew->unc_size = ze.unc_size;
  3763. #ifdef _UNICODE
  3764. GetUnicodeFileName(ze.name, zew->name, MAX_PATH-1);
  3765. #else
  3766. strcpy(zew->name, ze.name);
  3767. #endif
  3768. }
  3769. return lasterrorU;
  3770. }
  3771. ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags)
  3772. {
  3773. if (hz==0)
  3774. {
  3775. lasterrorU=ZR_ARGS;
  3776. return ZR_ARGS;
  3777. }
  3778. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3779. if (han->flag!=1)
  3780. {
  3781. lasterrorU=ZR_ZMODE;
  3782. return ZR_ZMODE;
  3783. }
  3784. TUnzip *unz = han->unz;
  3785. lasterrorU = unz->Unzip(index,dst,len,flags);
  3786. return lasterrorU;
  3787. }
  3788. ZRESULT CloseZipU(HZIP hz)
  3789. { if (hz==0) {lasterrorU=ZR_ARGS;return ZR_ARGS;}
  3790. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3791. if (han->flag!=1) {lasterrorU=ZR_ZMODE;return ZR_ZMODE;}
  3792. TUnzip *unz = han->unz;
  3793. lasterrorU = unz->Close();
  3794. delete unz;
  3795. delete han;
  3796. return lasterrorU;
  3797. }
  3798. bool IsZipHandleU(HZIP hz)
  3799. { if (hz==0) return true;
  3800. TUnzipHandleData *han = (TUnzipHandleData*)hz;
  3801. return (han->flag==1);
  3802. }