Unzip.cpp 154 KB

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