unzip.c 143 KB

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