XUnzip.cpp 142 KB

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