unzip.cpp 142 KB

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