XUnzip.cpp 142 KB

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