unzip.pas 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357
  1. {
  2. $Id$
  3. }
  4. UNIT Unzip;
  5. {
  6. Unzips deflated, imploded, shrunk and stored files
  7. ** COMPATIBLE WITH
  8. * Turbo Pascal v7.x (DOS)
  9. * Borland Pascal v7.x (Dos, DPMI, and Windows)
  10. * Delphi v1.x
  11. * Delphi v2.x
  12. * Delphi v3.x
  13. * Virtual Pascal v2.0 (OS/2, Win32)
  14. * Free Pascal Compiler (DOS, OS/2, Win32, Linux)
  15. }
  16. {
  17. Original version (1.x): Christian Ghisler
  18. C code by info-zip group, translated to pascal by Christian Ghisler
  19. based on unz51g.zip;
  20. Special thanks go to Mark Adler,who wrote the main inflate and
  21. explode code, and did NOT copyright it!!!
  22. v2.00: March 1998: Dr Abimbola Olowofoyeku (The African Chief)
  23. Homepage: http://ourworld.compuserve.com/homepages/African_Chief
  24. * modified to compile for Delphi v2.x and Delphi v3.x
  25. v2.01: April 1998: Dr Abimbola Olowofoyeku (The African Chief)
  26. * source files merged into a single source (this) file
  27. * several high level functions added - i.e.,
  28. FileUnzip()
  29. FileUnzipEx()
  30. ViewZip()
  31. UnzipSize()
  32. SetUnzipReportProc()
  33. SetUnzipQuestionProc()
  34. ChfUnzip_Init()
  35. * callbacks added
  36. * modified to support Virtual Pascal v2.0 (Win32)
  37. * Delphi component added (chfunzip.pas)
  38. v2.01a: December 1998: Tomas Hajny, [email protected]
  39. * extended to support other 32-bit compilers/platforms (OS/2, GO32, ...);
  40. search for (* TH ... *)
  41. v2.01b: December 1998: Peter Vreman
  42. * modifications needed for Linux
  43. }
  44. INTERFACE
  45. {$IFDEF FPC}
  46. {$DEFINE BIT32}
  47. {$ENDIF}
  48. {$IFDEF OS2}
  49. {$DEFINE BIT32}
  50. {$ENDIF}
  51. {$IFDEF WIN32}
  52. {$DEFINE BIT32}
  53. {$ENDIF}
  54. {$IFNDEF FPC}
  55. {$F+}
  56. {$ENDIF}
  57. {$R-} {No range checking}
  58. USES
  59. {$ifdef windows}
  60. wintypes,
  61. winprocs,
  62. {$ifdef Delphi}
  63. Messages,
  64. Sysutils,
  65. {$else Delphi}
  66. strings,
  67. windos,
  68. {$endif Delphi}
  69. {$else Windows}
  70. strings,
  71. crt,
  72. dos,
  73. {$endif Windows}
  74. ziptypes;
  75. {**********************************************************************}
  76. {**********************************************************************}
  77. {****** HIGH LEVEL FUNCTIONS: BY THE AFRICAN CHIEF ********************}
  78. {**********************************************************************}
  79. {**********************************************************************}
  80. FUNCTION FileUnzip
  81. ( SourceZipFile, TargetDirectory, FileSpecs : pChar;
  82. Report : UnzipReportProc;Question : UnzipQuestionProc ) : integer;
  83. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  84. {$ifdef DPMI} EXPORT; {$endif DPMI}
  85. {
  86. high level unzip
  87. usage:
  88. SourceZipFile: source zip file;
  89. TargetDirectory: target directory
  90. FileSpecs: "*.*", etc.
  91. Report: Report callback or Nil;
  92. Question: Question callback (for confirmation of whether to replace existing
  93. files) or Nil;
  94. * REFER to ZIPTYPES.PAS for information on callback functions
  95. e.g.,
  96. Count := FileUnzip('test.zip', 'c:\temp', '*.*', MyReportProc, Nil);
  97. }
  98. FUNCTION FileUnzipEx ( SourceZipFile, TargetDirectory, FileSpecs : pChar ) : integer;
  99. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  100. {$ifdef DPMI} EXPORT; {$endif DPMI}
  101. {
  102. high level unzip with no callback parameters;
  103. passes ZipReport & ZipQuestion internally, so you
  104. can use SetZipReportProc and SetZipQuestionProc before calling this;
  105. e.g.,
  106. Count := FileUnzipEx('test.zip', 'c:\temp', '*.*');
  107. }
  108. FUNCTION ViewZip ( SourceZipFile, FileSpecs : pChar; Report : UnzipReportProc ) : integer;
  109. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  110. {$ifdef DPMI} EXPORT; {$endif DPMI}
  111. {
  112. view contents of zip file
  113. usage:
  114. SourceZipFile: source zip file;
  115. FileSpecs: "*.*", etc.
  116. Report: callback procedure to process the reported contents of ZIP file;
  117. * REFER to ZIPTYPES.PAS for information on callback functions
  118. e.g.,
  119. ViewZip('test.zip', '*.*', MyReportProc);
  120. }
  121. FUNCTION SetUnZipReportProc ( aProc : UnzipReportProc ) : Pointer;
  122. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  123. {$ifdef DPMI} EXPORT; {$endif DPMI}
  124. {
  125. sets the internal unzip report procedure to aproc
  126. Returns: pointer to the original report procedure
  127. (return value should normally be ignored)
  128. e.g.,
  129. SetUnZipReportProc(MyReportProc);
  130. }
  131. FUNCTION SetUnZipQuestionProc ( aProc : UnzipQuestionProc ) : Pointer;
  132. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  133. {$ifdef DPMI} EXPORT; {$endif DPMI}
  134. {
  135. sets the internal unzip question procedure to aproc
  136. Returns: pointer to the original "question" procedure
  137. (return value should normally be ignored)
  138. e.g.,
  139. SetUnZipQuestionProc(QueryFileExistProc);
  140. }
  141. FUNCTION UnzipSize ( SourceZipFile : pChar;VAR Compressed : Longint ) : longint;
  142. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  143. {$ifdef DPMI} EXPORT; {$endif DPMI}
  144. { uncompressed and compressed zip size
  145. usage:
  146. SourceZipFile = the zip file
  147. Compressed = the compressed size of the files in the archive
  148. Returns: the uncompressed size of the ZIP archive
  149. e.g.,
  150. Var
  151. Size,CSize:longint;
  152. begin
  153. Size := UnzipSize('test.zip', CSize);
  154. end;
  155. }
  156. PROCEDURE ChfUnzip_Init;
  157. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  158. {$ifdef DPMI} EXPORT; {$endif DPMI}
  159. {
  160. initialise or reinitialise the shared data: !!! use with care !!!
  161. }
  162. FUNCTION SetNoRecurseDirs ( DontRecurse : Boolean ) : Boolean;
  163. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  164. {$ifdef DPMI} EXPORT; {$endif DPMI}
  165. {
  166. determine whether the UNZIP function should recreate
  167. the subdirectory structure;
  168. DontRecurse = TRUE : don't recurse
  169. DontRecurse = FALSE : recurse (default)
  170. }
  171. {**********************************************************************}
  172. {**********************************************************************}
  173. {************ LOW LEVEL FUNCTIONS: BY CHRISTIAN GHISLER ***************}
  174. {**********************************************************************}
  175. {**********************************************************************}
  176. FUNCTION GetSupportedMethods : longint;
  177. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  178. {$ifdef DPMI} EXPORT; {$endif DPMI}
  179. {Checks which pack methods are supported by the dll}
  180. {bit 8=1 -> Format 8 supported, etc.}
  181. FUNCTION UnzipFile ( in_name : pchar;out_name : pchar;offset : longint;hFileAction : word;cm_index : integer ) : integer;
  182. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  183. {$ifdef DPMI} EXPORT; {$endif DPMI}
  184. {usage:
  185. in_name: name of zip file with full path
  186. out_name: desired name for out file
  187. offset: header position of desired file in zipfile
  188. hFileAction: handle to dialog box showing advance of decompression (optional)
  189. cm_index: notification code sent in a wm_command message to the dialog
  190. to update percent-bar
  191. Return value: one of the above unzip_xxx codes
  192. Example for handling the cm_index message in a progress dialog:
  193. unzipfile(......,cm_showpercent);
  194. ...
  195. procedure TFileActionDialog.wmcommand(var msg:tmessage);
  196. var ppercent:^word;
  197. begin
  198. TDialog.WMCommand(msg);
  199. if msg.wparam=cm_showpercent then begin
  200. ppercent:=pointer(lparam);
  201. if ppercent<>nil then begin
  202. if (ppercent^>=0) and (ppercent^<=100) then
  203. SetProgressBar(ppercent^);
  204. if UserPressedAbort then
  205. ppercent^:=$ffff
  206. else
  207. ppercent^:=0;
  208. end;
  209. end;
  210. end;
  211. end;
  212. }
  213. FUNCTION GetFirstInZip ( zipfilename : pchar;VAR zprec : tZipRec ) : integer;
  214. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  215. {$ifdef DPMI} EXPORT; {$endif DPMI}
  216. {
  217. Get first entry from ZIP file
  218. e.g.,
  219. rc:=GetFirstInZip('test.zip', myZipRec);
  220. }
  221. FUNCTION GetNextInZip ( VAR Zprec : tZiprec ) : integer;
  222. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  223. {$ifdef DPMI} EXPORT; {$endif DPMI}
  224. {
  225. Get next entry from ZIP file
  226. e.g.,
  227. rc:=GetNextInZip(myZipRec);
  228. }
  229. FUNCTION IsZip ( filename : pchar ) : boolean;
  230. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  231. {$ifdef DPMI} EXPORT; {$endif DPMI}
  232. {
  233. VERY simple test for zip file
  234. e.g.,
  235. ItsaZipFile := IsZip('test.zip');
  236. }
  237. PROCEDURE CloseZipFile ( VAR Zprec : tZiprec ); {Only free buffer, file only open in Getfirstinzip}
  238. {$ifdef Windows}{$ifdef Win32}STDCALL;{$else}EXPORT;{$endif Win32}{$endif Windows}
  239. {$ifdef DPMI} EXPORT; {$endif DPMI}
  240. {
  241. free ZIP buffers
  242. e.g.,
  243. CloseZipFile(myZipRec);
  244. }
  245. IMPLEMENTATION
  246. VAR
  247. ZipReport : UnzipReportProc; {Global Status Report Callback}
  248. ZipQuestion : UnzipQuestionProc; {Global "Question" Callback}
  249. ZipRec : TReportRec; {Global ZIP record for callbacks}
  250. NoRecurseDirs : Boolean; {Global Recurse variable}
  251. {*************************************************************************}
  252. {$ifdef Delphi}
  253. PROCEDURE SetCurDir ( p : pChar );
  254. BEGIN
  255. Chdir ( strpas ( p ) );
  256. END;
  257. FUNCTION DosError : integer; {Delphi DosError kludge}
  258. BEGIN
  259. Result := Ioresult;
  260. END;
  261. FUNCTION SetFTime ( VAR f : File; CONST l : longint ) : integer;
  262. BEGIN
  263. {$ifdef Win32}Result := {$endif}FileSetDate ( TFileRec ( f ) .Handle, l );
  264. END;
  265. PROCEDURE CreateDir ( p : pchar );
  266. BEGIN
  267. mkdir ( strpas ( p ) );
  268. END;
  269. {/////////////////////////////////////////////////////////}
  270. {$endif Delphi}
  271. {.$I z_global.pas} {global constants, types and variables}
  272. {Include file for unzip.pas: global constants, types and variables}
  273. {C code by info-zip group, translated to pascal by Christian Ghisler}
  274. {based on unz51g.zip}
  275. CONST {Error codes returned by huft_build}
  276. huft_complete = 0; {Complete tree}
  277. huft_incomplete = 1; {Incomplete tree <- sufficient in some cases!}
  278. huft_error = 2; {bad tree constructed}
  279. huft_outofmem = 3; {not enough memory}
  280. (* TH - use of the new BIT32 conditional (was WIN32 only previously) *)
  281. MaxMax = {$ifdef BIT32}256 * 1024 {BIT32 = 256kb buffer}
  282. {$else}Maxint -1{$endif}; {16-bit = 32kb buffer}
  283. CONST wsize = $8000; {Size of sliding dictionary}
  284. INBUFSIZ = 1024 * 4; {Size of input buffer}
  285. CONST lbits : integer = 9;
  286. dbits : integer = 6;
  287. CONST b_max = 16;
  288. n_max = 288;
  289. BMAX = 16;
  290. TYPE push = ^ush;
  291. ush = word;
  292. pbyte = ^byte;
  293. pushlist = ^ushlist;
  294. ushlist = ARRAY [ 0..maxmax ] of ush; {only pseudo-size!!}
  295. pword = ^word;
  296. pwordarr = ^twordarr;
  297. twordarr = ARRAY [ 0..maxmax ] of word;
  298. iobuf = ARRAY [ 0..inbufsiz -1 ] of byte;
  299. TYPE pphuft = ^phuft;
  300. phuft = ^huft;
  301. phuftlist = ^huftlist;
  302. huft = PACKED RECORD
  303. e, {# of extra bits}
  304. b : byte; {# of bits in code}
  305. v_n : ush;
  306. v_t : phuftlist; {Linked List}
  307. END;
  308. huftlist = ARRAY [ 0..8190 ] of huft;
  309. TYPE li = PACKED RECORD
  310. lo, hi : word;
  311. END;
  312. {pkzip header in front of every file in archive}
  313. TYPE
  314. plocalheader = ^tlocalheader;
  315. tlocalheader = PACKED RECORD
  316. signature : ARRAY [ 0..3 ] of char; {'PK'#1#2}
  317. extract_ver,
  318. bit_flag,
  319. zip_type : word;
  320. file_timedate : longint;
  321. crc_32,
  322. compress_size,
  323. uncompress_size : longint;
  324. filename_len,
  325. extra_field_len : word;
  326. END;
  327. VAR slide : pchar; {Sliding dictionary for unzipping}
  328. inbuf : iobuf; {input buffer}
  329. inpos, readpos : integer; {position in input buffer, position read from file}
  330. {$ifdef windows}
  331. dlghandle : word; {optional: handle of a cancel and "%-done"-dialog}
  332. {$endif}
  333. dlgnotify : integer; {notification code to tell dialog how far the decompression is}
  334. VAR w : longint; {Current Position in slide}
  335. b : longint; {Bit Buffer}
  336. k : byte; {Bits in bit buffer}
  337. infile, {handle to zipfile}
  338. outfile : file; {handle to extracted file}
  339. compsize, {comressed size of file}
  340. reachedsize, {number of bytes read from zipfile}
  341. uncompsize : longint; {uncompressed size of file}
  342. crc32val : longint; {crc calculated from data}
  343. hufttype : word; {coding type=bit_flag from header}
  344. totalabort, {User pressed abort button, set in showpercent!}
  345. zipeof : boolean; {read over end of zip section for this file}
  346. inuse : boolean; {is unit already in use -> don't call it again!!!}
  347. {$ifdef windows}
  348. oldpercent : integer; {last percent value shown}
  349. lastusedtime : longint; {Time of last usage in timer ticks for timeout!}
  350. {$endif}
  351. (***************************************************************************)
  352. {.$I z_tables.pas} {Tables for bit masking, huffman codes and CRC checking}
  353. {include file for unzip.pas: Tables for bit masking, huffman codes and CRC checking}
  354. {C code by info-zip group, translated to Pascal by Christian Ghisler}
  355. {based on unz51g.zip}
  356. {b and mask_bits[i] gets lower i bits out of i}
  357. CONST mask_bits : ARRAY [ 0..16 ] of word =
  358. ( $0000,
  359. $0001, $0003, $0007, $000f, $001f, $003f, $007f, $00ff,
  360. $01ff, $03ff, $07ff, $0fff, $1fff, $3fff, $7fff, $ffff );
  361. { Tables for deflate from PKZIP's appnote.txt. }
  362. CONST border : ARRAY [ 0..18 ] of byte = { Order of the bit length code lengths }
  363. ( 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 );
  364. CONST cplens : ARRAY [ 0..30 ] of word = { Copy lengths for literal codes 257..285 }
  365. ( 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  366. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0 );
  367. { note: see note #13 above about the 258 in this list.}
  368. CONST cplext : ARRAY [ 0..30 ] of word = { Extra bits for literal codes 257..285 }
  369. ( 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  370. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 99, 99 ); { 99==invalid }
  371. CONST cpdist : ARRAY [ 0..29 ] of word = { Copy offsets for distance codes 0..29 }
  372. ( 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  373. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  374. 8193, 12289, 16385, 24577 );
  375. CONST cpdext : ARRAY [ 0..29 ] of word = { Extra bits for distance codes }
  376. ( 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  377. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  378. 12, 12, 13, 13 );
  379. { Tables for explode }
  380. CONST cplen2 : ARRAY [ 0..63 ] of word = ( 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  381. 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
  382. 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  383. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65 );
  384. CONST cplen3 : ARRAY [ 0..63 ] of word = ( 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
  385. 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  386. 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  387. 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66 );
  388. CONST extra : ARRAY [ 0..63 ] of word = ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  389. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  390. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  391. 8 );
  392. CONST cpdist4 : ARRAY [ 0..63 ] of word = ( 1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
  393. 769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
  394. 1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
  395. 2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
  396. 2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
  397. 3649, 3713, 3777, 3841, 3905, 3969, 4033 );
  398. CONST cpdist8 : ARRAY [ 0..63 ] of word = ( 1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
  399. 1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
  400. 2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
  401. 4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
  402. 5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
  403. 7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065 );
  404. {************************************ CRC-Calculation ************************************}
  405. CONST crc_32_tab : ARRAY [ 0..255 ] of longint =
  406. (
  407. $00000000, $77073096, $ee0e612c, $990951ba, $076dc419,
  408. $706af48f, $e963a535, $9e6495a3, $0edb8832, $79dcb8a4,
  409. $e0d5e91e, $97d2d988, $09b64c2b, $7eb17cbd, $e7b82d07,
  410. $90bf1d91, $1db71064, $6ab020f2, $f3b97148, $84be41de,
  411. $1adad47d, $6ddde4eb, $f4d4b551, $83d385c7, $136c9856,
  412. $646ba8c0, $fd62f97a, $8a65c9ec, $14015c4f, $63066cd9,
  413. $fa0f3d63, $8d080df5, $3b6e20c8, $4c69105e, $d56041e4,
  414. $a2677172, $3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
  415. $35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940, $32d86ce3,
  416. $45df5c75, $dcd60dcf, $abd13d59, $26d930ac, $51de003a,
  417. $c8d75180, $bfd06116, $21b4f4b5, $56b3c423, $cfba9599,
  418. $b8bda50f, $2802b89e, $5f058808, $c60cd9b2, $b10be924,
  419. $2f6f7c87, $58684c11, $c1611dab, $b6662d3d, $76dc4190,
  420. $01db7106, $98d220bc, $efd5102a, $71b18589, $06b6b51f,
  421. $9fbfe4a5, $e8b8d433, $7807c9a2, $0f00f934, $9609a88e,
  422. $e10e9818, $7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,
  423. $6b6b51f4, $1c6c6162, $856530d8, $f262004e, $6c0695ed,
  424. $1b01a57b, $8208f4c1, $f50fc457, $65b0d9c6, $12b7e950,
  425. $8bbeb8ea, $fcb9887c, $62dd1ddf, $15da2d49, $8cd37cf3,
  426. $fbd44c65, $4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,
  427. $4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb, $4369e96a,
  428. $346ed9fc, $ad678846, $da60b8d0, $44042d73, $33031de5,
  429. $aa0a4c5f, $dd0d7cc9, $5005713c, $270241aa, $be0b1010,
  430. $c90c2086, $5768b525, $206f85b3, $b966d409, $ce61e49f,
  431. $5edef90e, $29d9c998, $b0d09822, $c7d7a8b4, $59b33d17,
  432. $2eb40d81, $b7bd5c3b, $c0ba6cad, $edb88320, $9abfb3b6,
  433. $03b6e20c, $74b1d29a, $ead54739, $9dd277af, $04db2615,
  434. $73dc1683, $e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,
  435. $e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1, $f00f9344,
  436. $8708a3d2, $1e01f268, $6906c2fe, $f762575d, $806567cb,
  437. $196c3671, $6e6b06e7, $fed41b76, $89d32be0, $10da7a5a,
  438. $67dd4acc, $f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,
  439. $d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252, $d1bb67f1,
  440. $a6bc5767, $3fb506dd, $48b2364b, $d80d2bda, $af0a1b4c,
  441. $36034af6, $41047a60, $df60efc3, $a867df55, $316e8eef,
  442. $4669be79, $cb61b38c, $bc66831a, $256fd2a0, $5268e236,
  443. $cc0c7795, $bb0b4703, $220216b9, $5505262f, $c5ba3bbe,
  444. $b2bd0b28, $2bb45a92, $5cb36a04, $c2d7ffa7, $b5d0cf31,
  445. $2cd99e8b, $5bdeae1d, $9b64c2b0, $ec63f226, $756aa39c,
  446. $026d930a, $9c0906a9, $eb0e363f, $72076785, $05005713,
  447. $95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38, $92d28e9b,
  448. $e5d5be0d, $7cdcefb7, $0bdbdf21, $86d3d2d4, $f1d4e242,
  449. $68ddb3f8, $1fda836e, $81be16cd, $f6b9265b, $6fb077e1,
  450. $18b74777, $88085ae6, $ff0f6a70, $66063bca, $11010b5c,
  451. $8f659eff, $f862ae69, $616bffd3, $166ccf45, $a00ae278,
  452. $d70dd2ee, $4e048354, $3903b3c2, $a7672661, $d06016f7,
  453. $4969474d, $3e6e77db, $aed16a4a, $d9d65adc, $40df0b66,
  454. $37d83bf0, $a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,
  455. $bdbdf21c, $cabac28a, $53b39330, $24b4a3a6, $bad03605,
  456. $cdd70693, $54de5729, $23d967bf, $b3667a2e, $c4614ab8,
  457. $5d681b02, $2a6f2b94, $b40bbe37, $c30c8ea1, $5a05df1b,
  458. $2d02ef8d ); { end crc_32_tab[] }
  459. (***************************************************************************)
  460. {.$I z_generl.pas} {General functions used by both inflate and explode}
  461. {include for unzip.pas: General functions used by both inflate and explode}
  462. {C code by info-zip group, translated to Pascal by Christian Ghisler}
  463. {based on unz51g.zip}
  464. {*********************************** CRC Checking ********************************}
  465. PROCEDURE UpdateCRC ( VAR s : iobuf;len : word );
  466. VAR i : word;
  467. BEGIN
  468. {$ifndef assembler}
  469. FOR i := 0 TO Pred ( len ) DO BEGIN
  470. { update running CRC calculation with contents of a buffer }
  471. crc32val := crc_32_tab [ ( byte ( crc32val ) XOR s [ i ] ) AND $ff ] XOR ( crc32val SHR 8 );
  472. END;
  473. {$else}
  474. ASM
  475. les di, s
  476. mov ax, li.lo ( crc32val )
  477. mov dx, li.hi ( crc32val )
  478. mov si, offset crc_32_tab {Segment remains DS!!!}
  479. mov cx, len
  480. OR cx, cx
  481. jz @finished
  482. @again :
  483. mov bl, al {byte(crcval)}
  484. mov al, ah {shift DX:AX by 8 bits to the right}
  485. mov ah, dl
  486. mov dl, dh
  487. XOR dh, dh
  488. XOR bh, bh
  489. XOR bl, es : [ di ] {xor s^}
  490. inc di
  491. SHL bx, 1 {Offset: Index*4}
  492. SHL bx, 1
  493. XOR ax, [ si + bx ]
  494. XOR dx, [ si + bx + 2 ]
  495. dec cx
  496. jnz @again
  497. @finished :
  498. mov li.lo ( crc32val ), ax
  499. mov li.hi ( crc32val ), dx
  500. END;
  501. {$endif}
  502. END;
  503. {************************ keep other programs running ***************************}
  504. PROCEDURE messageloop;
  505. {$ifdef windows}
  506. VAR msg : tmsg;
  507. BEGIN
  508. lastusedtime := gettickcount;
  509. WHILE PeekMessage ( Msg, 0, 0, 0, PM_Remove ) DO
  510. IF ( dlghandle = 0 ) OR NOT IsDialogMessage ( dlghandle, msg ) THEN BEGIN
  511. TranslateMessage ( Msg );
  512. DispatchMessage ( Msg );
  513. END;
  514. END;
  515. {$else}
  516. VAR ch : word;
  517. BEGIN
  518. IF keypressed THEN BEGIN
  519. ch := byte ( readkey );
  520. IF ch = 0 THEN ch := 256 + byte ( readkey ); {Extended code}
  521. IF ch = dlgnotify THEN totalabort := TRUE;
  522. END
  523. END;
  524. {$endif}
  525. {************************* tell dialog to show % ******************************}
  526. {$ifdef windows}
  527. PROCEDURE showpercent; {use this with the low level functions only !!!}
  528. VAR percent : word;
  529. BEGIN
  530. IF compsize <> 0 THEN BEGIN
  531. percent := reachedsize * 100 DIV compsize;
  532. IF percent > 100 THEN percent := 100;
  533. IF ( percent <> oldpercent ) THEN BEGIN
  534. oldpercent := percent;
  535. IF dlghandle <> 0 THEN BEGIN {Use dialog box for aborting}
  536. {Sendmessage returns directly -> ppercent contains result}
  537. sendmessage ( dlghandle, wm_command, dlgnotify, longint ( @percent ) );
  538. totalabort := ( percent = $FFFF ); {Abort pressed!}
  539. END ELSE
  540. IF dlgnotify <> 0 THEN
  541. totalabort := getasynckeystate ( dlgnotify ) < 0; {break Key pressed!}
  542. END;
  543. END;
  544. END;
  545. {$endif}
  546. {************************** fill inbuf from infile *********************}
  547. PROCEDURE readbuf;
  548. BEGIN
  549. IF reachedsize > compsize + 2 THEN BEGIN {+2: last code is smaller than requested!}
  550. readpos := sizeof ( inbuf ); {Simulates reading -> no blocking}
  551. zipeof := TRUE
  552. END ELSE BEGIN
  553. messageloop; {Other programs, or in DOS: keypressed?}
  554. {$ifdef windows}
  555. showpercent; {Before, because it shows the data processed, not read!}
  556. {$endif}
  557. {$I-}
  558. blockread ( infile, inbuf, sizeof ( inbuf ), readpos );
  559. {$I+}
  560. IF ( ioresult <> 0 ) OR ( readpos = 0 ) THEN BEGIN {readpos=0: kein Fehler gemeldet!!!}
  561. readpos := sizeof ( inbuf ); {Simulates reading -> CRC error}
  562. zipeof := TRUE;
  563. END;
  564. inc ( reachedsize, readpos );
  565. dec ( readpos ); {Reason: index of inbuf starts at 0}
  566. END;
  567. inpos := 0;
  568. END;
  569. {**** read byte, only used by explode ****}
  570. PROCEDURE READBYTE ( VAR bt : byte );
  571. BEGIN
  572. IF inpos > readpos THEN readbuf;
  573. bt := inbuf [ inpos ];
  574. inc ( inpos );
  575. END;
  576. {*********** read at least n bits into the global variable b *************}
  577. PROCEDURE NEEDBITS ( n : byte );
  578. VAR nb : longint;
  579. BEGIN
  580. {$ifndef assembler}
  581. WHILE k < n DO BEGIN
  582. IF inpos > readpos THEN readbuf;
  583. nb := inbuf [ inpos ];
  584. inc ( inpos );
  585. b := b OR nb SHL k;
  586. inc ( k, 8 );
  587. END;
  588. {$else}
  589. ASM
  590. mov si, offset inbuf
  591. mov ch, n
  592. mov cl, k
  593. mov bx, inpos {bx=inpos}
  594. @again :
  595. cmp cl, ch
  596. JAE @finished {k>=n -> finished}
  597. cmp bx, readpos
  598. jg @readbuf
  599. @fullbuf :
  600. mov al, [ si + bx ] {dx:ax=nb}
  601. XOR ah, ah
  602. XOR dx, dx
  603. cmp cl, 8 {cl>=8 -> shift into DX or directly by 1 byte}
  604. JAE @bigger8
  605. SHL ax, cl {Normal shifting!}
  606. jmp @continue
  607. @bigger8 :
  608. mov di, cx {save cx}
  609. mov ah, al {shift by 8}
  610. XOR al, al
  611. sub cl, 8 {8 bits shifted}
  612. @rotate :
  613. OR cl, cl
  614. jz @continue1 {all shifted -> finished}
  615. SHL ah, 1 {al ist empty!}
  616. rcl dx, 1
  617. dec cl
  618. jmp @rotate
  619. @continue1 :
  620. mov cx, di
  621. @continue :
  622. OR li.hi ( b ), dx {b=b or nb shl k}
  623. OR li.lo ( b ), ax
  624. inc bx {inpos}
  625. add cl, 8 {inc k by 8 Bits}
  626. jmp @again
  627. @readbuf :
  628. push si
  629. push cx
  630. call readbuf {readbuf not critical, called only every 2000 bytes}
  631. pop cx
  632. pop si
  633. mov bx, inpos {New inpos}
  634. jmp @fullbuf
  635. @finished :
  636. mov k, cl
  637. mov inpos, bx
  638. END;
  639. {$endif}
  640. END;
  641. {***************** dump n bits no longer needed from global variable b *************}
  642. PROCEDURE DUMPBITS ( n : byte );
  643. BEGIN
  644. {$ifndef assembler}
  645. b := b SHR n;
  646. k := k -n;
  647. {$else}
  648. ASM
  649. mov cl, n
  650. mov ax, li.lo ( b )
  651. mov dx, li.hi ( b )
  652. mov ch, cl
  653. OR ch, ch
  654. jz @finished
  655. @rotate :
  656. SHR dx, 1 {Lower Bit in Carry}
  657. rcr ax, 1
  658. dec ch
  659. jnz @rotate
  660. @finished :
  661. mov li.lo ( b ), ax
  662. mov li.hi ( b ), dx
  663. sub k, cl
  664. END;
  665. {$endif}
  666. END;
  667. {********************* Flush w bytes directly from slide to file ******************}
  668. FUNCTION flush ( w : word ) : boolean;
  669. VAR n : nword; {True wenn OK}
  670. b : boolean;
  671. BEGIN
  672. {$I-}
  673. blockwrite ( outfile, slide [ 0 ], w, n );
  674. {$I+}
  675. b := ( n = w ) AND ( ioresult = 0 ); {True-> alles ok}
  676. UpdateCRC ( iobuf ( pointer ( @slide [ 0 ] ) ^ ), w );
  677. {--}
  678. {$IFDEF FPC}
  679. IF ( b = TRUE ) AND Assigned(ZipReport) {callback report for high level functions}
  680. {$ELSE}
  681. IF ( b = TRUE ) AND ( @ZipReport <> NIL ) {callback report for high level functions}
  682. {$ENDIF}
  683. THEN BEGIN
  684. WITH ZipRec DO BEGIN
  685. Status := file_unzipping;
  686. ZipReport ( n, @ZipRec ); {report the actual bytes written}
  687. END;
  688. END; {report}
  689. flush := b;
  690. END;
  691. {******************************* Break string into tokens ****************************}
  692. VAR
  693. _Token : PChar;
  694. FUNCTION StrTok ( Source : PChar; Token : CHAR ) : PChar;
  695. VAR P : PChar;
  696. BEGIN
  697. IF Source <> NIL THEN _Token := Source;
  698. IF _Token = NIL THEN BEGIN
  699. strTok := NIL;
  700. exit
  701. END;
  702. P := StrScan ( _Token, Token );
  703. StrTok := _Token;
  704. IF P <> NIL THEN BEGIN
  705. P^ := #0;
  706. Inc ( P );
  707. END;
  708. _Token := P;
  709. END;
  710. (***************************************************************************)
  711. {.$I z_huft.pas} {Huffman tree generating and destroying}
  712. {include for unzip.pas: Huffman tree generating and destroying}
  713. {C code by info-zip group, translated to Pascal by Christian Ghisler}
  714. {based on unz51g.zip}
  715. {*************** free huffman tables starting with table where t points to ************}
  716. PROCEDURE huft_free ( t : phuftlist );
  717. VAR p, q : phuftlist;
  718. z : integer;
  719. BEGIN
  720. p := pointer ( t );
  721. WHILE p <> NIL DO BEGIN
  722. dec ( longint ( p ), sizeof ( huft ) );
  723. q := p^ [ 0 ].v_t;
  724. z := p^ [ 0 ].v_n; {Size in Bytes, required by TP ***}
  725. freemem ( p, ( z + 1 ) * sizeof ( huft ) );
  726. p := q
  727. END;
  728. END;
  729. {*********** build huffman table from code lengths given by array b^ *******************}
  730. FUNCTION huft_build ( b : pword;n : word;s : word;d, e : pushlist;t : pphuft;VAR m : integer ) : integer;
  731. VAR a : word; {counter for codes of length k}
  732. c : ARRAY [ 0..b_max + 1 ] of word; {bit length count table}
  733. f : word; {i repeats in table every f entries}
  734. g, {max. code length}
  735. h : integer; {table level}
  736. i, {counter, current code}
  737. j : word; {counter}
  738. k : integer; {number of bits in current code}
  739. p : pword; {pointer into c, b and v}
  740. q : phuftlist; {points to current table}
  741. r : huft; {table entry for structure assignment}
  742. u : ARRAY [ 0..b_max ] of phuftlist;{table stack}
  743. v : ARRAY [ 0..n_max ] of word; {values in order of bit length}
  744. w : integer; {bits before this table}
  745. x : ARRAY [ 0..b_max + 1 ] of word; {bit offsets, then code stack}
  746. l : ARRAY [ -1..b_max + 1 ] of word; {l[h] bits in table of level h}
  747. xp : ^word; {pointer into x}
  748. y : integer; {number of dummy codes added}
  749. z : word; {number of entries in current table}
  750. tryagain : boolean; {bool for loop}
  751. pt : phuft; {for test against bad input}
  752. el : word; {length of eob code=code 256}
  753. BEGIN
  754. IF n > 256 THEN el := pword ( longint ( b ) + 256 * sizeof ( word ) ) ^
  755. ELSE el := BMAX;
  756. {generate counts for each bit length}
  757. fillchar ( c, sizeof ( c ), #0 );
  758. p := b; i := n; {p points to array of word}
  759. REPEAT
  760. IF p^ > b_max THEN BEGIN
  761. t^ := NIL;
  762. m := 0;
  763. huft_build := huft_error;
  764. exit
  765. END;
  766. inc ( c [ p^ ] );
  767. inc ( longint ( p ), sizeof ( word ) ); {point to next item}
  768. dec ( i );
  769. UNTIL i = 0;
  770. IF c [ 0 ] = n THEN BEGIN
  771. t^ := NIL;
  772. m := 0;
  773. huft_build := huft_complete;
  774. exit
  775. END;
  776. {find minimum and maximum length, bound m by those}
  777. j := 1;
  778. WHILE ( j <= b_max ) AND ( c [ j ] = 0 ) DO inc ( j );
  779. k := j;
  780. IF m < j THEN m := j;
  781. i := b_max;
  782. WHILE ( i > 0 ) AND ( c [ i ] = 0 ) DO dec ( i );
  783. g := i;
  784. IF m > i THEN m := i;
  785. {adjust last length count to fill out codes, if needed}
  786. y := 1 SHL j;
  787. WHILE j < i DO BEGIN
  788. y := y -c [ j ];
  789. IF y < 0 THEN BEGIN
  790. huft_build := huft_error;
  791. exit
  792. END;
  793. y := y SHL 1;
  794. inc ( j );
  795. END;
  796. dec ( y, c [ i ] );
  797. IF y < 0 THEN BEGIN
  798. huft_build := huft_error;
  799. exit
  800. END;
  801. inc ( c [ i ], y );
  802. {generate starting offsets into the value table for each length}
  803. x [ 1 ] := 0;
  804. j := 0;
  805. p := @c; inc ( longint ( p ), sizeof ( word ) );
  806. xp := @x;inc ( longint ( xp ), 2 * sizeof ( word ) );
  807. dec ( i );
  808. WHILE i <> 0 DO BEGIN
  809. inc ( j, p^ );
  810. xp^ := j;
  811. inc ( longint ( p ), 2 );
  812. inc ( longint ( xp ), 2 );
  813. dec ( i );
  814. END;
  815. {make table of values in order of bit length}
  816. p := b; i := 0;
  817. REPEAT
  818. j := p^;
  819. inc ( longint ( p ), sizeof ( word ) );
  820. IF j <> 0 THEN BEGIN
  821. v [ x [ j ] ] := i;
  822. inc ( x [ j ] );
  823. END;
  824. inc ( i );
  825. UNTIL i >= n;
  826. {generate huffman codes and for each, make the table entries}
  827. x [ 0 ] := 0; i := 0;
  828. p := @v;
  829. h := -1;
  830. l [ -1 ] := 0;
  831. w := 0;
  832. u [ 0 ] := NIL;
  833. q := NIL;
  834. z := 0;
  835. {go through the bit lengths (k already is bits in shortest code)}
  836. FOR k := k TO g DO BEGIN
  837. FOR a := c [ k ] DOWNTO 1 DO BEGIN
  838. {here i is the huffman code of length k bits for value p^}
  839. WHILE k > w + l [ h ] DO BEGIN
  840. inc ( w, l [ h ] ); {Length of tables to this position}
  841. inc ( h );
  842. z := g -w;
  843. IF z > m THEN z := m;
  844. j := k -w;
  845. f := 1 SHL j;
  846. IF f > a + 1 THEN BEGIN
  847. dec ( f, a + 1 );
  848. xp := @c [ k ];
  849. inc ( j );
  850. tryagain := TRUE;
  851. WHILE ( j < z ) AND tryagain DO BEGIN
  852. f := f SHL 1;
  853. inc ( longint ( xp ), sizeof ( word ) );
  854. IF f <= xp^ THEN tryagain := FALSE
  855. ELSE BEGIN
  856. dec ( f, xp^ );
  857. inc ( j );
  858. END;
  859. END;
  860. END;
  861. IF ( w + j > el ) AND ( w < el ) THEN
  862. j := el -w; {Make eob code end at table}
  863. IF w = 0 THEN BEGIN
  864. j := m; {*** Fix: main table always m bits!}
  865. END;
  866. z := 1 SHL j;
  867. l [ h ] := j;
  868. {allocate and link new table}
  869. getmem ( q, ( z + 1 ) * sizeof ( huft ) );
  870. IF q = NIL THEN BEGIN
  871. IF h <> 0 THEN huft_free ( pointer ( u [ 0 ] ) );
  872. huft_build := huft_outofmem;
  873. exit
  874. END;
  875. fillchar ( q^, ( z + 1 ) * sizeof ( huft ), #0 );
  876. q^ [ 0 ].v_n := z; {Size of table, needed in freemem ***}
  877. t^ := @q^ [ 1 ]; {first item starts at 1}
  878. t := @q^ [ 0 ].v_t;
  879. t^ := NIL;
  880. q := @q^ [ 1 ]; {pointer(longint(q)+sizeof(huft));} {???}
  881. u [ h ] := q;
  882. {connect to last table, if there is one}
  883. IF h <> 0 THEN BEGIN
  884. x [ h ] := i;
  885. r.b := l [ h -1 ];
  886. r.e := 16 + j;
  887. r.v_t := q;
  888. j := ( i AND ( ( 1 SHL w ) -1 ) ) SHR ( w -l [ h -1 ] );
  889. {test against bad input!}
  890. pt := phuft ( longint ( u [ h -1 ] ) -sizeof ( huft ) );
  891. IF j > pt^.v_n THEN BEGIN
  892. huft_free ( pointer ( u [ 0 ] ) );
  893. huft_build := huft_error;
  894. exit
  895. END;
  896. pt := @u [ h -1 ]^ [ j ];
  897. pt^ := r;
  898. END;
  899. END;
  900. {set up table entry in r}
  901. r.b := word ( k -w );
  902. r.v_t := NIL; {Unused} {***********}
  903. IF longint ( p ) >= longint ( @v [ n ] ) THEN r.e := 99
  904. ELSE IF p^ < s THEN BEGIN
  905. IF p^ < 256 THEN r.e := 16 ELSE r.e := 15;
  906. r.v_n := p^;
  907. inc ( longint ( p ), sizeof ( word ) );
  908. END ELSE BEGIN
  909. IF ( d = NIL ) OR ( e = NIL ) THEN BEGIN
  910. huft_free ( pointer ( u [ 0 ] ) );
  911. huft_build := huft_error;
  912. exit
  913. END;
  914. r.e := word ( e^ [ p^ -s ] );
  915. r.v_n := d^ [ p^ -s ];
  916. inc ( longint ( p ), sizeof ( word ) );
  917. END;
  918. {fill code like entries with r}
  919. f := 1 SHL ( k -w );
  920. j := i SHR w;
  921. WHILE j < z DO BEGIN
  922. q^ [ j ] := r;
  923. inc ( j, f );
  924. END;
  925. {backwards increment the k-bit code i}
  926. j := 1 SHL ( k -1 );
  927. WHILE ( i AND j ) <> 0 DO BEGIN
  928. {i:=i^j;}
  929. i := i XOR j;
  930. j := j SHR 1;
  931. END;
  932. i := i XOR j;
  933. {backup over finished tables}
  934. WHILE ( ( i AND ( ( 1 SHL w ) -1 ) ) <> x [ h ] ) DO BEGIN
  935. dec ( h );
  936. dec ( w, l [ h ] ); {Size of previous table!}
  937. END;
  938. END;
  939. END;
  940. IF ( y <> 0 ) AND ( g <> 1 ) THEN huft_build := huft_incomplete
  941. ELSE huft_build := huft_complete;
  942. END;
  943. (***************************************************************************)
  944. {.$I z_inflat.pas} {Inflate deflated file}
  945. {include for unzip.pas: Inflate deflated file}
  946. {C code by info-zip group, translated to Pascal by Christian Ghisler}
  947. {based on unz51g.zip}
  948. FUNCTION inflate_codes ( tl, td : phuftlist;bl, bd : integer ) : integer;
  949. VAR
  950. n, d, e1, {length and index for copy}
  951. ml, md : longint; {masks for bl and bd bits}
  952. t : phuft; {pointer to table entry}
  953. e : byte; {table entry flag/number of extra bits}
  954. BEGIN
  955. { inflate the coded data }
  956. ml := mask_bits [ bl ]; {precompute masks for speed}
  957. md := mask_bits [ bd ];
  958. WHILE NOT ( totalabort OR zipeof ) DO BEGIN
  959. NEEDBITS ( bl );
  960. t := @tl^ [ b AND ml ];
  961. e := t^.e;
  962. IF e > 16 THEN REPEAT {then it's a literal}
  963. IF e = 99 THEN BEGIN
  964. inflate_codes := unzip_ZipFileErr;
  965. exit
  966. END;
  967. DUMPBITS ( t^.b );
  968. dec ( e, 16 );
  969. NEEDBITS ( e );
  970. t := @t^.v_t^ [ b AND mask_bits [ e ] ];
  971. e := t^.e;
  972. UNTIL e <= 16;
  973. DUMPBITS ( t^.b );
  974. IF e = 16 THEN BEGIN
  975. slide [ w ] := char ( t^.v_n );
  976. inc ( w );
  977. IF w = WSIZE THEN BEGIN
  978. IF NOT flush ( w ) THEN BEGIN
  979. inflate_codes := unzip_WriteErr;
  980. exit;
  981. END;
  982. w := 0
  983. END;
  984. END ELSE BEGIN {it's an EOB or a length}
  985. IF e = 15 THEN BEGIN {Ende} {exit if end of block}
  986. inflate_codes := unzip_Ok;
  987. exit;
  988. END;
  989. NEEDBITS ( e ); {get length of block to copy}
  990. n := t^.v_n + ( b AND mask_bits [ e ] );
  991. DUMPBITS ( e );
  992. NEEDBITS ( bd ); {decode distance of block to copy}
  993. t := @td^ [ b AND md ];
  994. e := t^.e;
  995. IF e > 16 THEN REPEAT
  996. IF e = 99 THEN BEGIN
  997. inflate_codes := unzip_ZipFileErr;
  998. exit
  999. END;
  1000. DUMPBITS ( t^.b );
  1001. dec ( e, 16 );
  1002. NEEDBITS ( e );
  1003. t := @t^.v_t^ [ b AND mask_bits [ e ] ];
  1004. e := t^.e;
  1005. UNTIL e <= 16;
  1006. DUMPBITS ( t^.b );
  1007. NEEDBITS ( e );
  1008. d := w -t^.v_n -b AND mask_bits [ e ];
  1009. DUMPBITS ( e );
  1010. {do the copy}
  1011. REPEAT
  1012. d := d AND ( WSIZE -1 );
  1013. IF d > w THEN e1 := WSIZE -d
  1014. ELSE e1 := WSIZE -w;
  1015. IF e1 > n THEN e1 := n;
  1016. dec ( n, e1 );
  1017. IF ( longint(w) -d >= e1 ) THEN BEGIN
  1018. move ( slide [ d ], slide [ w ], e1 );
  1019. inc ( w, e1 );
  1020. inc ( d, e1 );
  1021. END ELSE REPEAT
  1022. slide [ w ] := slide [ d ];
  1023. inc ( w );
  1024. inc ( d );
  1025. dec ( e1 );
  1026. UNTIL ( e1 = 0 );
  1027. IF w = WSIZE THEN BEGIN
  1028. IF NOT flush ( w ) THEN BEGIN
  1029. inflate_codes := unzip_WriteErr;
  1030. exit;
  1031. END;
  1032. w := 0;
  1033. END;
  1034. UNTIL n = 0;
  1035. END;
  1036. END;
  1037. IF totalabort THEN
  1038. inflate_codes := unzip_userabort
  1039. ELSE
  1040. inflate_codes := unzip_readErr;
  1041. END;
  1042. {**************************** "decompress" stored block **************************}
  1043. FUNCTION inflate_stored : integer;
  1044. VAR n : word; {number of bytes in block}
  1045. BEGIN
  1046. {go to byte boundary}
  1047. n := k AND 7;
  1048. dumpbits ( n );
  1049. {get the length and its complement}
  1050. NEEDBITS ( 16 );
  1051. n := b AND $ffff;
  1052. DUMPBITS ( 16 );
  1053. NEEDBITS ( 16 );
  1054. IF ( n <> ( NOT b ) AND $ffff ) THEN BEGIN
  1055. inflate_stored := unzip_zipFileErr;
  1056. exit
  1057. END;
  1058. DUMPBITS ( 16 );
  1059. WHILE ( n > 0 ) AND NOT ( totalabort OR zipeof ) DO BEGIN {read and output the compressed data}
  1060. dec ( n );
  1061. NEEDBITS ( 8 );
  1062. slide [ w ] := char ( b );
  1063. inc ( w );
  1064. IF w = WSIZE THEN BEGIN
  1065. IF NOT flush ( w ) THEN BEGIN
  1066. inflate_stored := unzip_WriteErr;
  1067. exit
  1068. END;
  1069. w := 0;
  1070. END;
  1071. DUMPBITS ( 8 );
  1072. END;
  1073. IF totalabort THEN inflate_stored := unzip_UserAbort
  1074. ELSE IF zipeof THEN inflate_stored := unzip_readErr
  1075. ELSE inflate_stored := unzip_Ok;
  1076. END;
  1077. {**************************** decompress fixed block **************************}
  1078. FUNCTION inflate_fixed : integer;
  1079. VAR i : integer; {temporary variable}
  1080. tl, {literal/length code table}
  1081. td : phuftlist; {distance code table}
  1082. bl, bd : integer; {lookup bits for tl/bd}
  1083. l : ARRAY [ 0..287 ] of word; {length list for huft_build}
  1084. BEGIN
  1085. {set up literal table}
  1086. FOR i := 0 TO 143 DO l [ i ] := 8;
  1087. FOR i := 144 TO 255 DO l [ i ] := 9;
  1088. FOR i := 256 TO 279 DO l [ i ] := 7;
  1089. FOR i := 280 TO 287 DO l [ i ] := 8; {make a complete, but wrong code set}
  1090. bl := 7;
  1091. i := huft_build ( pword ( @l ), 288, 257, pushlist ( @cplens ), pushlist ( @cplext ), @tl, bl );
  1092. IF i <> huft_complete THEN BEGIN
  1093. inflate_fixed := i;
  1094. exit
  1095. END;
  1096. FOR i := 0 TO 29 DO l [ i ] := 5; {make an incomplete code set}
  1097. bd := 5;
  1098. i := huft_build ( pword ( @l ), 30, 0, pushlist ( @cpdist ), pushlist ( @cpdext ), @td, bd );
  1099. IF i > huft_incomplete THEN BEGIN
  1100. huft_free ( tl );
  1101. inflate_fixed := unzip_ZipFileErr;
  1102. exit
  1103. END;
  1104. inflate_fixed := inflate_codes ( tl, td, bl, bd );
  1105. huft_free ( tl );
  1106. huft_free ( td );
  1107. END;
  1108. {**************************** decompress dynamic block **************************}
  1109. FUNCTION inflate_dynamic : integer;
  1110. VAR i : integer; {temporary variables}
  1111. j,
  1112. l, {last length}
  1113. m, {mask for bit length table}
  1114. n : word; {number of lengths to get}
  1115. tl, {literal/length code table}
  1116. td : phuftlist; {distance code table}
  1117. bl, bd : integer; {lookup bits for tl/bd}
  1118. nb, nl, nd : word; {number of bit length/literal length/distance codes}
  1119. ll : ARRAY [ 0..288 + 32 -1 ] of word; {literal/length and distance code lengths}
  1120. BEGIN
  1121. {read in table lengths}
  1122. NEEDBITS ( 5 );
  1123. nl := 257 + word ( b ) AND $1f;
  1124. DUMPBITS ( 5 );
  1125. NEEDBITS ( 5 );
  1126. nd := 1 + word ( b ) AND $1f;
  1127. DUMPBITS ( 5 );
  1128. NEEDBITS ( 4 );
  1129. nb := 4 + word ( b ) AND $f;
  1130. DUMPBITS ( 4 );
  1131. IF ( nl > 288 ) OR ( nd > 32 ) THEN BEGIN
  1132. inflate_dynamic := 1;
  1133. exit
  1134. END;
  1135. fillchar ( ll, sizeof ( ll ), #0 );
  1136. {read in bit-length-code lengths}
  1137. FOR j := 0 TO nb -1 DO BEGIN
  1138. NEEDBITS ( 3 );
  1139. ll [ border [ j ] ] := b AND 7;
  1140. DUMPBITS ( 3 );
  1141. END;
  1142. FOR j := nb TO 18 DO ll [ border [ j ] ] := 0;
  1143. {build decoding table for trees--single level, 7 bit lookup}
  1144. bl := 7;
  1145. i := huft_build ( pword ( @ll ), 19, 19, NIL, NIL, @tl, bl );
  1146. IF i <> huft_complete THEN BEGIN
  1147. IF i = huft_incomplete THEN huft_free ( tl ); {other errors: already freed}
  1148. inflate_dynamic := unzip_ZipFileErr;
  1149. exit
  1150. END;
  1151. {read in literal and distance code lengths}
  1152. n := nl + nd;
  1153. m := mask_bits [ bl ];
  1154. i := 0; l := 0;
  1155. WHILE word ( i ) < n DO BEGIN
  1156. NEEDBITS ( bl );
  1157. td := @tl^ [ b AND m ];
  1158. j := phuft ( td ) ^.b;
  1159. DUMPBITS ( j );
  1160. j := phuft ( td ) ^.v_n;
  1161. IF j < 16 THEN BEGIN {length of code in bits (0..15)}
  1162. l := j; {ave last length in l}
  1163. ll [ i ] := l;
  1164. inc ( i )
  1165. END ELSE IF j = 16 THEN BEGIN {repeat last length 3 to 6 times}
  1166. NEEDBITS ( 2 );
  1167. j := 3 + b AND 3;
  1168. DUMPBITS ( 2 );
  1169. IF i + j > n THEN BEGIN
  1170. inflate_dynamic := 1;
  1171. exit
  1172. END;
  1173. WHILE j > 0 DO BEGIN
  1174. ll [ i ] := l;
  1175. dec ( j );
  1176. inc ( i );
  1177. END;
  1178. END ELSE IF j = 17 THEN BEGIN {3 to 10 zero length codes}
  1179. NEEDBITS ( 3 );
  1180. j := 3 + b AND 7;
  1181. DUMPBITS ( 3 );
  1182. IF i + j > n THEN BEGIN
  1183. inflate_dynamic := 1;
  1184. exit
  1185. END;
  1186. WHILE j > 0 DO BEGIN
  1187. ll [ i ] := 0;
  1188. inc ( i );
  1189. dec ( j );
  1190. END;
  1191. l := 0;
  1192. END ELSE BEGIN {j == 18: 11 to 138 zero length codes}
  1193. NEEDBITS ( 7 );
  1194. j := 11 + b AND $7f;
  1195. DUMPBITS ( 7 );
  1196. IF i + j > n THEN BEGIN
  1197. inflate_dynamic := unzip_zipfileErr;
  1198. exit
  1199. END;
  1200. WHILE j > 0 DO BEGIN
  1201. ll [ i ] := 0;
  1202. dec ( j );
  1203. inc ( i );
  1204. END;
  1205. l := 0;
  1206. END;
  1207. END;
  1208. huft_free ( tl ); {free decoding table for trees}
  1209. {build the decoding tables for literal/length and distance codes}
  1210. bl := lbits;
  1211. i := huft_build ( pword ( @ll ), nl, 257, pushlist ( @cplens ), pushlist ( @cplext ), @tl, bl );
  1212. IF i <> huft_complete THEN BEGIN
  1213. IF i = huft_incomplete THEN huft_free ( tl );
  1214. inflate_dynamic := unzip_ZipFileErr;
  1215. exit
  1216. END;
  1217. bd := dbits;
  1218. i := huft_build ( pword ( @ll [ nl ] ), nd, 0, pushlist ( @cpdist ), pushlist ( @cpdext ), @td, bd );
  1219. IF i > huft_incomplete THEN BEGIN {pkzip bug workaround}
  1220. IF i = huft_incomplete THEN huft_free ( td );
  1221. huft_free ( tl );
  1222. inflate_dynamic := unzip_ZipFileErr;
  1223. exit
  1224. END;
  1225. {decompress until an end-of-block code}
  1226. inflate_dynamic := inflate_codes ( tl, td, bl, bd );
  1227. huft_free ( tl );
  1228. huft_free ( td );
  1229. END;
  1230. {**************************** decompress a block ******************************}
  1231. FUNCTION inflate_block ( VAR e : integer ) : integer;
  1232. VAR t : word; {block type}
  1233. BEGIN
  1234. NEEDBITS ( 1 );
  1235. e := b AND 1;
  1236. DUMPBITS ( 1 );
  1237. NEEDBITS ( 2 );
  1238. t := b AND 3;
  1239. DUMPBITS ( 2 );
  1240. CASE t of
  1241. 2 : inflate_block := inflate_dynamic;
  1242. 0 : inflate_block := inflate_stored;
  1243. 1 : inflate_block := inflate_fixed;
  1244. ELSE
  1245. inflate_block := unzip_ZipFileErr; {bad block type}
  1246. END;
  1247. END;
  1248. {**************************** decompress an inflated entry **************************}
  1249. FUNCTION inflate : integer;
  1250. VAR e, {last block flag}
  1251. r : integer; {result code}
  1252. BEGIN
  1253. inpos := 0; {Input buffer position}
  1254. readpos := -1; {Nothing read}
  1255. {initialize window, bit buffer}
  1256. w := 0;
  1257. k := 0;
  1258. b := 0;
  1259. {decompress until the last block}
  1260. REPEAT
  1261. r := inflate_block ( e );
  1262. IF r <> 0 THEN BEGIN
  1263. inflate := r;
  1264. exit
  1265. END;
  1266. UNTIL e <> 0;
  1267. {flush out slide}
  1268. IF NOT flush ( w ) THEN inflate := unzip_WriteErr
  1269. ELSE inflate := unzip_Ok;
  1270. END;
  1271. (***************************************************************************)
  1272. {.$I z_copyst.pas} {Copy stored file}
  1273. {include for unzip.pas: Copy stored file}
  1274. {C code by info-zip group, translated to Pascal by Christian Ghisler}
  1275. {based on unz51g.zip}
  1276. {************************* copy stored file ************************************}
  1277. FUNCTION copystored : integer;
  1278. VAR readin : longint;
  1279. outcnt : nword;
  1280. BEGIN
  1281. WHILE ( reachedsize < compsize ) AND NOT totalabort DO BEGIN
  1282. readin := compsize -reachedsize;
  1283. IF readin > wsize THEN readin := wsize;
  1284. {$I-}
  1285. blockread ( infile, slide [ 0 ], readin, outcnt ); {Use slide as buffer}
  1286. {$I+}
  1287. IF ( outcnt <> readin ) OR ( ioresult <> 0 ) THEN BEGIN
  1288. copystored := unzip_ReadErr;
  1289. exit
  1290. END;
  1291. IF NOT flush ( outcnt ) THEN BEGIN {Flushoutput takes care of CRC too}
  1292. copystored := unzip_WriteErr;
  1293. exit
  1294. END;
  1295. inc ( reachedsize, outcnt );
  1296. messageloop; {Other programs, or in DOS: keypressed?}
  1297. {$ifdef windows}
  1298. showpercent;
  1299. {$endif}
  1300. END;
  1301. IF NOT totalabort THEN
  1302. copystored := unzip_Ok
  1303. ELSE
  1304. copystored := unzip_Userabort;
  1305. END;
  1306. (***************************************************************************)
  1307. {.$I z_explod.pas} {Explode imploded file}
  1308. {include for unzip.pas: Explode imploded file}
  1309. {C code by info-zip group, translated to Pascal by Christian Ghisler}
  1310. {based on unz51g.zip}
  1311. {************************************* explode ********************************}
  1312. {*********************************** read in tree *****************************}
  1313. FUNCTION get_tree ( l : pword;n : word ) : integer;
  1314. VAR i, k, j, b : word;
  1315. bytebuf : byte;
  1316. BEGIN
  1317. READBYTE ( bytebuf );
  1318. i := bytebuf;
  1319. inc ( i );
  1320. k := 0;
  1321. REPEAT
  1322. READBYTE ( bytebuf );
  1323. j := bytebuf;
  1324. b := ( j AND $F ) + 1;
  1325. j := ( ( j AND $F0 ) SHR 4 ) + 1;
  1326. IF ( k + j ) > n THEN BEGIN
  1327. get_tree := 4;
  1328. exit
  1329. END;
  1330. REPEAT
  1331. l^ := b;
  1332. inc ( longint ( l ), sizeof ( word ) );
  1333. inc ( k );
  1334. dec ( j );
  1335. UNTIL j = 0;
  1336. dec ( i );
  1337. UNTIL i = 0;
  1338. IF k <> n THEN get_tree := 4 ELSE get_tree := 0;
  1339. END;
  1340. {******************exploding, method: 8k slide, 3 trees ***********************}
  1341. FUNCTION explode_lit8 ( tb, tl, td : phuftlist;bb, bl, bd : integer ) : integer;
  1342. VAR s : longint;
  1343. e : word;
  1344. n, d : word;
  1345. w : word;
  1346. t : phuft;
  1347. mb, ml, md : word;
  1348. u : word;
  1349. BEGIN
  1350. b := 0; k := 0; w := 0;
  1351. u := 1;
  1352. mb := mask_bits [ bb ];
  1353. ml := mask_bits [ bl ];
  1354. md := mask_bits [ bd ];
  1355. s := uncompsize;
  1356. WHILE ( s > 0 ) AND NOT ( totalabort OR zipeof ) DO BEGIN
  1357. NEEDBITS ( 1 );
  1358. IF ( b AND 1 ) <> 0 THEN BEGIN {Litteral}
  1359. DUMPBITS ( 1 );
  1360. dec ( s );
  1361. NEEDBITS ( bb );
  1362. t := @tb^ [ ( NOT b ) AND mb ];
  1363. e := t^.e;
  1364. IF e > 16 THEN REPEAT
  1365. IF e = 99 THEN BEGIN
  1366. explode_lit8 := unzip_ZipFileErr;
  1367. exit
  1368. END;
  1369. DUMPBITS ( t^.b );
  1370. dec ( e, 16 );
  1371. NEEDBITS ( e );
  1372. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1373. e := t^.e;
  1374. UNTIL e <= 16;
  1375. DUMPBITS ( t^.b );
  1376. slide [ w ] := char ( t^.v_n );
  1377. inc ( w );
  1378. IF w = WSIZE THEN BEGIN
  1379. IF NOT flush ( w ) THEN BEGIN
  1380. explode_lit8 := unzip_WriteErr;
  1381. exit
  1382. END;
  1383. w := 0; u := 0;
  1384. END;
  1385. END ELSE BEGIN
  1386. DUMPBITS ( 1 );
  1387. NEEDBITS ( 7 );
  1388. d := b AND $7F;
  1389. DUMPBITS ( 7 );
  1390. NEEDBITS ( bd );
  1391. t := @td^ [ ( NOT b ) AND md ];
  1392. e := t^.e;
  1393. IF e > 16 THEN REPEAT
  1394. IF e = 99 THEN BEGIN
  1395. explode_lit8 := unzip_ZipFileErr;
  1396. exit
  1397. END;
  1398. DUMPBITS ( t^.b );
  1399. dec ( e, 16 );
  1400. NEEDBITS ( e );
  1401. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1402. e := t^.e;
  1403. UNTIL e <= 16;
  1404. DUMPBITS ( t^.b );
  1405. d := w -d -t^.v_n;
  1406. NEEDBITS ( bl );
  1407. t := @tl^ [ ( NOT b ) AND ml ];
  1408. e := t^.e;
  1409. IF e > 16 THEN REPEAT
  1410. IF e = 99 THEN BEGIN
  1411. explode_lit8 := unzip_ZipFileErr;
  1412. exit
  1413. END;
  1414. DUMPBITS ( t^.b );
  1415. dec ( e, 16 );
  1416. NEEDBITS ( e );
  1417. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1418. e := t^.e;
  1419. UNTIL e <= 16;
  1420. DUMPBITS ( t^.b );
  1421. n := t^.v_n;
  1422. IF e <> 0 THEN BEGIN
  1423. NEEDBITS ( 8 );
  1424. inc ( n, byte ( b ) AND $ff );
  1425. DUMPBITS ( 8 );
  1426. END;
  1427. dec ( s, n );
  1428. REPEAT
  1429. d := d AND pred ( WSIZE );
  1430. IF d > w THEN e := WSIZE -d ELSE e := WSIZE -w;
  1431. IF e > n THEN e := n;
  1432. dec ( n, e );
  1433. IF ( u <> 0 ) AND ( w <= d ) THEN BEGIN
  1434. fillchar ( slide [ w ], e, #0 );
  1435. inc ( w, e );
  1436. inc ( d, e );
  1437. END ELSE IF ( w -d >= e ) THEN BEGIN
  1438. move ( slide [ d ], slide [ w ], e );
  1439. inc ( w, e );
  1440. inc ( d, e );
  1441. END ELSE REPEAT
  1442. slide [ w ] := slide [ d ];
  1443. inc ( w );
  1444. inc ( d );
  1445. dec ( e );
  1446. UNTIL e = 0;
  1447. IF w = WSIZE THEN BEGIN
  1448. IF NOT flush ( w ) THEN BEGIN
  1449. explode_lit8 := unzip_WriteErr;
  1450. exit
  1451. END;
  1452. w := 0; u := 0;
  1453. END;
  1454. UNTIL n = 0;
  1455. END;
  1456. END;
  1457. IF totalabort THEN explode_lit8 := unzip_userabort
  1458. ELSE
  1459. IF NOT flush ( w ) THEN explode_lit8 := unzip_WriteErr
  1460. ELSE
  1461. IF zipeof THEN explode_lit8 := unzip_readErr
  1462. ELSE
  1463. explode_lit8 := unzip_Ok;
  1464. END;
  1465. {******************exploding, method: 4k slide, 3 trees ***********************}
  1466. FUNCTION explode_lit4 ( tb, tl, td : phuftlist;bb, bl, bd : integer ) : integer;
  1467. VAR s : longint;
  1468. e : word;
  1469. n, d : word;
  1470. w : word;
  1471. t : phuft;
  1472. mb, ml, md : word;
  1473. u : word;
  1474. BEGIN
  1475. b := 0; k := 0; w := 0;
  1476. u := 1;
  1477. mb := mask_bits [ bb ];
  1478. ml := mask_bits [ bl ];
  1479. md := mask_bits [ bd ];
  1480. s := uncompsize;
  1481. WHILE ( s > 0 ) AND NOT ( totalabort OR zipeof ) DO BEGIN
  1482. NEEDBITS ( 1 );
  1483. IF ( b AND 1 ) <> 0 THEN BEGIN {Litteral}
  1484. DUMPBITS ( 1 );
  1485. dec ( s );
  1486. NEEDBITS ( bb );
  1487. t := @tb^ [ ( NOT b ) AND mb ];
  1488. e := t^.e;
  1489. IF e > 16 THEN REPEAT
  1490. IF e = 99 THEN BEGIN
  1491. explode_lit4 := unzip_ZipFileErr;
  1492. exit
  1493. END;
  1494. DUMPBITS ( t^.b );
  1495. dec ( e, 16 );
  1496. NEEDBITS ( e );
  1497. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1498. e := t^.e;
  1499. UNTIL e <= 16;
  1500. DUMPBITS ( t^.b );
  1501. slide [ w ] := char ( t^.v_n );
  1502. inc ( w );
  1503. IF w = WSIZE THEN BEGIN
  1504. IF NOT flush ( w ) THEN BEGIN
  1505. explode_lit4 := unzip_WriteErr;
  1506. exit
  1507. END;
  1508. w := 0; u := 0;
  1509. END;
  1510. END ELSE BEGIN
  1511. DUMPBITS ( 1 );
  1512. NEEDBITS ( 6 );
  1513. d := b AND $3F;
  1514. DUMPBITS ( 6 );
  1515. NEEDBITS ( bd );
  1516. t := @td^ [ ( NOT b ) AND md ];
  1517. e := t^.e;
  1518. IF e > 16 THEN REPEAT
  1519. IF e = 99 THEN BEGIN
  1520. explode_lit4 := unzip_ZipFileErr;
  1521. exit
  1522. END;
  1523. DUMPBITS ( t^.b );
  1524. dec ( e, 16 );
  1525. NEEDBITS ( e );
  1526. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1527. e := t^.e;
  1528. UNTIL e <= 16;
  1529. DUMPBITS ( t^.b );
  1530. d := w -d -t^.v_n;
  1531. NEEDBITS ( bl );
  1532. t := @tl^ [ ( NOT b ) AND ml ];
  1533. e := t^.e;
  1534. IF e > 16 THEN REPEAT
  1535. IF e = 99 THEN BEGIN
  1536. explode_lit4 := unzip_ZipFileErr;
  1537. exit
  1538. END;
  1539. DUMPBITS ( t^.b );
  1540. dec ( e, 16 );
  1541. NEEDBITS ( e );
  1542. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1543. e := t^.e;
  1544. UNTIL e <= 16;
  1545. DUMPBITS ( t^.b );
  1546. n := t^.v_n;
  1547. IF e <> 0 THEN BEGIN
  1548. NEEDBITS ( 8 );
  1549. inc ( n, b AND $ff );
  1550. DUMPBITS ( 8 );
  1551. END;
  1552. dec ( s, n );
  1553. REPEAT
  1554. d := d AND pred ( WSIZE );
  1555. IF d > w THEN e := WSIZE -d ELSE e := WSIZE -w;
  1556. IF e > n THEN e := n;
  1557. dec ( n, e );
  1558. IF ( u <> 0 ) AND ( w <= d ) THEN BEGIN
  1559. fillchar ( slide [ w ], e, #0 );
  1560. inc ( w, e );
  1561. inc ( d, e );
  1562. END ELSE IF ( w -d >= e ) THEN BEGIN
  1563. move ( slide [ d ], slide [ w ], e );
  1564. inc ( w, e );
  1565. inc ( d, e );
  1566. END ELSE REPEAT
  1567. slide [ w ] := slide [ d ];
  1568. inc ( w );
  1569. inc ( d );
  1570. dec ( e );
  1571. UNTIL e = 0;
  1572. IF w = WSIZE THEN BEGIN
  1573. IF NOT flush ( w ) THEN BEGIN
  1574. explode_lit4 := unzip_WriteErr;
  1575. exit
  1576. END;
  1577. w := 0; u := 0;
  1578. END;
  1579. UNTIL n = 0;
  1580. END;
  1581. END;
  1582. IF totalabort THEN explode_lit4 := unzip_userabort
  1583. ELSE
  1584. IF NOT flush ( w ) THEN explode_lit4 := unzip_WriteErr
  1585. ELSE
  1586. IF zipeof THEN explode_lit4 := unzip_readErr
  1587. ELSE explode_lit4 := unzip_Ok;
  1588. END;
  1589. {******************exploding, method: 8k slide, 2 trees ***********************}
  1590. FUNCTION explode_nolit8 ( tl, td : phuftlist;bl, bd : integer ) : integer;
  1591. VAR s : longint;
  1592. e : word;
  1593. n, d : word;
  1594. w : word;
  1595. t : phuft;
  1596. ml, md : word;
  1597. u : word;
  1598. BEGIN
  1599. b := 0; k := 0; w := 0;
  1600. u := 1;
  1601. ml := mask_bits [ bl ];
  1602. md := mask_bits [ bd ];
  1603. s := uncompsize;
  1604. WHILE ( s > 0 ) AND NOT ( totalabort OR zipeof ) DO BEGIN
  1605. NEEDBITS ( 1 );
  1606. IF ( b AND 1 ) <> 0 THEN BEGIN {Litteral}
  1607. DUMPBITS ( 1 );
  1608. dec ( s );
  1609. NEEDBITS ( 8 );
  1610. slide [ w ] := char ( b );
  1611. inc ( w );
  1612. IF w = WSIZE THEN BEGIN
  1613. IF NOT flush ( w ) THEN BEGIN
  1614. explode_nolit8 := unzip_WriteErr;
  1615. exit
  1616. END;
  1617. w := 0; u := 0;
  1618. END;
  1619. DUMPBITS ( 8 );
  1620. END ELSE BEGIN
  1621. DUMPBITS ( 1 );
  1622. NEEDBITS ( 7 );
  1623. d := b AND $7F;
  1624. DUMPBITS ( 7 );
  1625. NEEDBITS ( bd );
  1626. t := @td^ [ ( NOT b ) AND md ];
  1627. e := t^.e;
  1628. IF e > 16 THEN REPEAT
  1629. IF e = 99 THEN BEGIN
  1630. explode_nolit8 := unzip_ZipFileErr;
  1631. exit
  1632. END;
  1633. DUMPBITS ( t^.b );
  1634. dec ( e, 16 );
  1635. NEEDBITS ( e );
  1636. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1637. e := t^.e;
  1638. UNTIL e <= 16;
  1639. DUMPBITS ( t^.b );
  1640. d := w -d -t^.v_n;
  1641. NEEDBITS ( bl );
  1642. t := @tl^ [ ( NOT b ) AND ml ];
  1643. e := t^.e;
  1644. IF e > 16 THEN REPEAT
  1645. IF e = 99 THEN BEGIN
  1646. explode_nolit8 := unzip_ZipFileErr;
  1647. exit
  1648. END;
  1649. DUMPBITS ( t^.b );
  1650. dec ( e, 16 );
  1651. NEEDBITS ( e );
  1652. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1653. e := t^.e;
  1654. UNTIL e <= 16;
  1655. DUMPBITS ( t^.b );
  1656. n := t^.v_n;
  1657. IF e <> 0 THEN BEGIN
  1658. NEEDBITS ( 8 );
  1659. inc ( n, b AND $ff );
  1660. DUMPBITS ( 8 );
  1661. END;
  1662. dec ( s, n );
  1663. REPEAT
  1664. d := d AND pred ( WSIZE );
  1665. IF d > w THEN e := WSIZE -d ELSE e := WSIZE -w;
  1666. IF e > n THEN e := n;
  1667. dec ( n, e );
  1668. IF ( u <> 0 ) AND ( w <= d ) THEN BEGIN
  1669. fillchar ( slide [ w ], e, #0 );
  1670. inc ( w, e );
  1671. inc ( d, e );
  1672. END ELSE IF ( w -d >= e ) THEN BEGIN
  1673. move ( slide [ d ], slide [ w ], e );
  1674. inc ( w, e );
  1675. inc ( d, e );
  1676. END ELSE REPEAT
  1677. slide [ w ] := slide [ d ];
  1678. inc ( w );
  1679. inc ( d );
  1680. dec ( e );
  1681. UNTIL e = 0;
  1682. IF w = WSIZE THEN BEGIN
  1683. IF NOT flush ( w ) THEN BEGIN
  1684. explode_nolit8 := unzip_WriteErr;
  1685. exit
  1686. END;
  1687. w := 0; u := 0;
  1688. END;
  1689. UNTIL n = 0;
  1690. END;
  1691. END;
  1692. IF totalabort THEN explode_nolit8 := unzip_userabort
  1693. ELSE
  1694. IF NOT flush ( w ) THEN explode_nolit8 := unzip_WriteErr
  1695. ELSE
  1696. IF zipeof THEN explode_nolit8 := unzip_readErr
  1697. ELSE explode_nolit8 := unzip_Ok;
  1698. END;
  1699. {******************exploding, method: 4k slide, 2 trees ***********************}
  1700. FUNCTION explode_nolit4 ( tl, td : phuftlist;bl, bd : integer ) : integer;
  1701. VAR s : longint;
  1702. e : word;
  1703. n, d : word;
  1704. w : word;
  1705. t : phuft;
  1706. ml, md : word;
  1707. u : word;
  1708. BEGIN
  1709. b := 0; k := 0; w := 0;
  1710. u := 1;
  1711. ml := mask_bits [ bl ];
  1712. md := mask_bits [ bd ];
  1713. s := uncompsize;
  1714. WHILE ( s > 0 ) AND NOT ( totalabort OR zipeof ) DO BEGIN
  1715. NEEDBITS ( 1 );
  1716. IF ( b AND 1 ) <> 0 THEN BEGIN {Litteral}
  1717. DUMPBITS ( 1 );
  1718. dec ( s );
  1719. NEEDBITS ( 8 );
  1720. slide [ w ] := char ( b );
  1721. inc ( w );
  1722. IF w = WSIZE THEN BEGIN
  1723. IF NOT flush ( w ) THEN BEGIN
  1724. explode_nolit4 := unzip_WriteErr;
  1725. exit
  1726. END;
  1727. w := 0; u := 0;
  1728. END;
  1729. DUMPBITS ( 8 );
  1730. END ELSE BEGIN
  1731. DUMPBITS ( 1 );
  1732. NEEDBITS ( 6 );
  1733. d := b AND $3F;
  1734. DUMPBITS ( 6 );
  1735. NEEDBITS ( bd );
  1736. t := @td^ [ ( NOT b ) AND md ];
  1737. e := t^.e;
  1738. IF e > 16 THEN REPEAT
  1739. IF e = 99 THEN BEGIN
  1740. explode_nolit4 := unzip_ZipFileErr;
  1741. exit
  1742. END;
  1743. DUMPBITS ( t^.b );
  1744. dec ( e, 16 );
  1745. NEEDBITS ( e );
  1746. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1747. e := t^.e;
  1748. UNTIL e <= 16;
  1749. DUMPBITS ( t^.b );
  1750. d := w -d -t^.v_n;
  1751. NEEDBITS ( bl );
  1752. t := @tl^ [ ( NOT b ) AND ml ];
  1753. e := t^.e;
  1754. IF e > 16 THEN REPEAT
  1755. IF e = 99 THEN BEGIN
  1756. explode_nolit4 := unzip_ZipFileErr;
  1757. exit
  1758. END;
  1759. DUMPBITS ( t^.b );
  1760. dec ( e, 16 );
  1761. NEEDBITS ( e );
  1762. t := @t^.v_t^ [ ( NOT b ) AND mask_bits [ e ] ];
  1763. e := t^.e;
  1764. UNTIL e <= 16;
  1765. DUMPBITS ( t^.b );
  1766. n := t^.v_n;
  1767. IF e <> 0 THEN BEGIN
  1768. NEEDBITS ( 8 );
  1769. inc ( n, b AND $ff );
  1770. DUMPBITS ( 8 );
  1771. END;
  1772. dec ( s, n );
  1773. REPEAT
  1774. d := d AND pred ( WSIZE );
  1775. IF d > w THEN e := WSIZE -d ELSE e := WSIZE -w;
  1776. IF e > n THEN e := n;
  1777. dec ( n, e );
  1778. IF ( u <> 0 ) AND ( w <= d ) THEN BEGIN
  1779. fillchar ( slide [ w ], e, #0 );
  1780. inc ( w, e );
  1781. inc ( d, e );
  1782. END ELSE IF ( w -d >= e ) THEN BEGIN
  1783. move ( slide [ d ], slide [ w ], e );
  1784. inc ( w, e );
  1785. inc ( d, e );
  1786. END ELSE REPEAT
  1787. slide [ w ] := slide [ d ];
  1788. inc ( w );
  1789. inc ( d );
  1790. dec ( e );
  1791. UNTIL e = 0;
  1792. IF w = WSIZE THEN BEGIN
  1793. IF NOT flush ( w ) THEN BEGIN
  1794. explode_nolit4 := unzip_WriteErr;
  1795. exit
  1796. END;
  1797. w := 0; u := 0;
  1798. END;
  1799. UNTIL n = 0;
  1800. END;
  1801. END;
  1802. IF totalabort THEN explode_nolit4 := unzip_userabort
  1803. ELSE
  1804. IF NOT flush ( w ) THEN explode_nolit4 := unzip_WriteErr
  1805. ELSE
  1806. IF zipeof THEN explode_nolit4 := unzip_readErr
  1807. ELSE explode_nolit4 := unzip_Ok;
  1808. END;
  1809. {****************************** explode *********************************}
  1810. FUNCTION explode : integer;
  1811. VAR r : integer;
  1812. tb, tl, td : phuftlist;
  1813. bb, bl, bd : integer;
  1814. l : ARRAY [ 0..255 ] of word;
  1815. BEGIN
  1816. inpos := 0;
  1817. readpos := -1; {Nothing read in}
  1818. bl := 7;
  1819. IF compsize > 200000 THEN bd := 8 ELSE bd := 7;
  1820. IF hufttype AND 4 <> 0 THEN BEGIN
  1821. bb := 9;
  1822. r := get_tree ( @l [ 0 ], 256 );
  1823. IF r <> 0 THEN BEGIN
  1824. explode := unzip_ZipFileErr;
  1825. exit
  1826. END;
  1827. r := huft_build ( @l, 256, 256, NIL, NIL, @tb, bb );
  1828. IF r <> 0 THEN BEGIN
  1829. IF r = huft_incomplete THEN huft_free ( tb );
  1830. explode := unzip_ZipFileErr;
  1831. exit
  1832. END;
  1833. r := get_tree ( @l [ 0 ], 64 );
  1834. IF r <> 0 THEN BEGIN
  1835. huft_free ( tb );
  1836. explode := unzip_ZipFileErr;
  1837. exit
  1838. END;
  1839. r := huft_build ( @l, 64, 0, pushlist ( @cplen3 ), pushlist ( @extra ), @tl, bl );
  1840. IF r <> 0 THEN BEGIN
  1841. IF r = huft_incomplete THEN huft_free ( tl );
  1842. huft_free ( tb );
  1843. explode := unzip_ZipFileErr;
  1844. exit
  1845. END;
  1846. r := get_tree ( @l [ 0 ], 64 );
  1847. IF r <> 0 THEN BEGIN
  1848. huft_free ( tb );
  1849. huft_free ( tl );
  1850. explode := unzip_ZipFileErr;
  1851. exit
  1852. END;
  1853. IF hufttype AND 2 <> 0 THEN BEGIN {8k}
  1854. r := huft_build ( @l, 64, 0, pushlist ( @cpdist8 ), pushlist ( @extra ), @td, bd );
  1855. IF r <> 0 THEN BEGIN
  1856. IF r = huft_incomplete THEN huft_free ( td );
  1857. huft_free ( tb );
  1858. huft_free ( tl );
  1859. explode := unzip_ZipFileErr;
  1860. exit
  1861. END;
  1862. r := explode_lit8 ( tb, tl, td, bb, bl, bd );
  1863. END ELSE BEGIN
  1864. r := huft_build ( @l, 64, 0, pushlist ( @cpdist4 ), pushlist ( @extra ), @td, bd );
  1865. IF r <> 0 THEN BEGIN
  1866. IF r = huft_incomplete THEN huft_free ( td );
  1867. huft_free ( tb );
  1868. huft_free ( tl );
  1869. explode := unzip_ZipFileErr;
  1870. exit
  1871. END;
  1872. r := explode_lit4 ( tb, tl, td, bb, bl, bd );
  1873. END;
  1874. huft_free ( td );
  1875. huft_free ( tl );
  1876. huft_free ( tb );
  1877. END ELSE BEGIN {No literal tree}
  1878. r := get_tree ( @l [ 0 ], 64 );
  1879. IF r <> 0 THEN BEGIN
  1880. explode := unzip_ZipFileErr;
  1881. exit
  1882. END;
  1883. r := huft_build ( @l, 64, 0, pushlist ( @cplen2 ), pushlist ( @extra ), @tl, bl );
  1884. IF r <> 0 THEN BEGIN
  1885. IF r = huft_incomplete THEN huft_free ( tl );
  1886. explode := unzip_ZipFileErr;
  1887. exit
  1888. END;
  1889. r := get_tree ( @l [ 0 ], 64 );
  1890. IF r <> 0 THEN BEGIN
  1891. huft_free ( tl );
  1892. explode := unzip_ZipFileErr;
  1893. exit
  1894. END;
  1895. IF hufttype AND 2 <> 0 THEN BEGIN {8k}
  1896. r := huft_build ( @l, 64, 0, pushlist ( @cpdist8 ), pushlist ( @extra ), @td, bd );
  1897. IF r <> 0 THEN BEGIN
  1898. IF r = huft_incomplete THEN huft_free ( td );
  1899. huft_free ( tl );
  1900. explode := unzip_ZipFileErr;
  1901. exit
  1902. END;
  1903. r := explode_nolit8 ( tl, td, bl, bd );
  1904. END ELSE BEGIN
  1905. r := huft_build ( @l, 64, 0, pushlist ( @cpdist4 ), pushlist ( @extra ), @td, bd );
  1906. IF r <> 0 THEN BEGIN
  1907. IF r = huft_incomplete THEN huft_free ( td );
  1908. huft_free ( tl );
  1909. explode := unzip_ZipFileErr;
  1910. exit
  1911. END;
  1912. r := explode_nolit4 ( tl, td, bl, bd );
  1913. END;
  1914. huft_free ( td );
  1915. huft_free ( tl );
  1916. END;
  1917. explode := r;
  1918. END;
  1919. (***************************************************************************)
  1920. {.$I z_shrunk.pas} {Unshrink function}
  1921. {*************************** unshrink **********************************}
  1922. {Written and NOT copyrighted by Christian Ghisler.
  1923. I have rewritten unshrink because the original
  1924. function was copyrighted by Mr. Smith of Info-zip
  1925. This funtion here is now completely FREE!!!!
  1926. The only right I claim on this code is that
  1927. noone else claims a copyright on it!}
  1928. CONST max_code = 8192;
  1929. max_stack = 8192;
  1930. initial_code_size = 9;
  1931. final_code_size = 13;
  1932. write_max = wsize -3 * ( max_code -256 ) -max_stack -2; {Rest of slide=write buffer}
  1933. {=766 bytes}
  1934. TYPE prev = ARRAY [ 257..max_code ] of integer;
  1935. pprev = ^prev;
  1936. cds = ARRAY [ 257..max_code ] of char;
  1937. pcds = ^cds;
  1938. stacktype = ARRAY [ 0..max_stack ] of char;
  1939. pstacktype = ^stacktype;
  1940. writebuftype = ARRAY [ 0..write_max ] of char; {write buffer}
  1941. pwritebuftype = ^writebuftype;
  1942. VAR previous_code : pprev; {previous code trie}
  1943. actual_code : pcds; {actual code trie}
  1944. stack : pstacktype; {Stack for output}
  1945. writebuf : pwritebuftype; {Write buffer}
  1946. next_free, {Next free code in trie}
  1947. write_ptr : integer; {Pointer to output buffer}
  1948. FUNCTION unshrink_flush : boolean;
  1949. VAR
  1950. n : nword;
  1951. b : boolean;
  1952. BEGIN
  1953. {$I-}
  1954. blockwrite ( outfile, writebuf^ [ 0 ], write_ptr, n );
  1955. {$I+}
  1956. b := ( n = write_ptr ) AND ( ioresult = 0 ); {True-> alles ok}
  1957. UpdateCRC ( iobuf ( pointer ( @writebuf^ [ 0 ] ) ^ ), write_ptr );
  1958. {--}
  1959. {$IFDEF FPC}
  1960. IF ( b = TRUE ) AND Assigned(ZipReport) {callback report for high level functions}
  1961. {$ELSE}
  1962. IF ( b = TRUE ) AND ( @ZipReport <> NIL ) {callback report for high level functions}
  1963. {$ENDIF}
  1964. THEN BEGIN
  1965. WITH ZipRec DO BEGIN
  1966. Status := file_unzipping;
  1967. ZipReport ( n, @ZipRec ); {report the actual bytes written}
  1968. END;
  1969. END; {report}
  1970. unshrink_flush := b;
  1971. END;
  1972. FUNCTION write_char ( c : char ) : boolean;
  1973. BEGIN
  1974. writebuf^ [ write_ptr ] := c;
  1975. inc ( write_ptr );
  1976. IF write_ptr > write_max THEN BEGIN
  1977. write_char := unshrink_flush;
  1978. write_ptr := 0;
  1979. END ELSE write_char := TRUE;
  1980. END;
  1981. PROCEDURE ClearLeafNodes;
  1982. VAR pc, {previous code}
  1983. i, {index}
  1984. act_max_code : integer; {max code to be searched for leaf nodes}
  1985. previous : pprev; {previous code trie}
  1986. BEGIN
  1987. previous := previous_code;
  1988. act_max_code := next_free -1;
  1989. FOR i := 257 TO act_max_code DO
  1990. previous^ [ i ] := previous^ [ i ] OR $8000;
  1991. FOR i := 257 TO act_max_code DO BEGIN
  1992. pc := previous^ [ i ] AND NOT $8000;
  1993. IF pc > 256 THEN
  1994. previous^ [ pc ] := previous^ [ pc ] AND ( NOT $8000 );
  1995. END;
  1996. {Build new free list}
  1997. pc := -1;
  1998. next_free := -1;
  1999. FOR i := 257 TO act_max_code DO
  2000. IF previous^ [ i ] AND $C000 <> 0 THEN BEGIN {Either free before or marked now}
  2001. IF pc <> -1 THEN previous^ [ pc ] := -i {Link last item to this item}
  2002. ELSE next_free := i;
  2003. pc := i;
  2004. END;
  2005. IF pc <> -1 THEN
  2006. previous^ [ pc ] := -act_max_code -1;
  2007. END;
  2008. FUNCTION unshrink : integer;
  2009. VAR incode : integer; {code read in}
  2010. lastincode : integer; {last code read in}
  2011. lastoutcode : char; {last code emitted}
  2012. code_size : byte; {Actual code size}
  2013. stack_ptr, {Stackpointer}
  2014. new_code, {Save new code read}
  2015. code_mask, {mask for coding}
  2016. i : integer; {Index}
  2017. bits_to_read : longint;
  2018. BEGIN
  2019. IF compsize = maxlongint THEN BEGIN {Compressed Size was not in header!}
  2020. unshrink := unzip_NotSupported;
  2021. exit
  2022. END;
  2023. inpos := 0; {Input buffer position}
  2024. readpos := -1; {Nothing read}
  2025. {initialize window, bit buffer}
  2026. w := 0;
  2027. k := 0;
  2028. b := 0;
  2029. {Initialize pointers for various buffers}
  2030. previous_code := @slide [ 0 ];
  2031. actual_code := @slide [ sizeof ( prev ) ];
  2032. stack := @slide [ sizeof ( prev ) + sizeof ( cds ) ];
  2033. writebuf := @slide [ sizeof ( prev ) + sizeof ( cds ) + sizeof ( stacktype ) ];
  2034. fillchar ( slide^, wsize, #0 );
  2035. {initialize free codes list}
  2036. FOR i := 257 TO max_code DO
  2037. previous_code^ [ i ] := - ( i + 1 );
  2038. next_free := 257;
  2039. stack_ptr := max_stack;
  2040. write_ptr := 0;
  2041. code_size := initial_code_size;
  2042. code_mask := mask_bits [ code_size ];
  2043. NEEDBITS ( code_size );
  2044. incode := b AND code_mask;
  2045. DUMPBITS ( code_size );
  2046. lastincode := incode;
  2047. lastoutcode := char ( incode );
  2048. IF NOT write_char ( lastoutcode ) THEN BEGIN
  2049. unshrink := unzip_writeErr;
  2050. exit
  2051. END;
  2052. bits_to_read := 8 * compsize -code_size; {Bits to be read}
  2053. WHILE NOT totalabort AND ( bits_to_read >= code_size ) DO BEGIN
  2054. NEEDBITS ( code_size );
  2055. incode := b AND code_mask;
  2056. DUMPBITS ( code_size );
  2057. dec ( bits_to_read, code_size );
  2058. IF incode = 256 THEN BEGIN {Special code}
  2059. NEEDBITS ( code_size );
  2060. incode := b AND code_mask;
  2061. DUMPBITS ( code_size );
  2062. dec ( bits_to_read, code_size );
  2063. CASE incode of
  2064. 1 : BEGIN
  2065. inc ( code_size );
  2066. IF code_size > final_code_size THEN BEGIN
  2067. unshrink := unzip_ZipFileErr;
  2068. exit
  2069. END;
  2070. code_mask := mask_bits [ code_size ];
  2071. END;
  2072. 2 : BEGIN
  2073. ClearLeafNodes;
  2074. END;
  2075. ELSE
  2076. unshrink := unzip_ZipFileErr;
  2077. exit
  2078. END;
  2079. END ELSE BEGIN
  2080. new_code := incode;
  2081. IF incode < 256 THEN BEGIN {Simple char}
  2082. lastoutcode := char ( incode );
  2083. IF NOT write_char ( lastoutcode ) THEN BEGIN
  2084. unshrink := unzip_writeErr;
  2085. exit
  2086. END;
  2087. END ELSE BEGIN
  2088. IF previous_code^ [ incode ] < 0 THEN BEGIN
  2089. stack^ [ stack_ptr ] := lastoutcode;
  2090. dec ( stack_ptr );
  2091. incode := lastincode;
  2092. END;
  2093. WHILE incode > 256 DO BEGIN
  2094. stack^ [ stack_ptr ] := actual_code^ [ incode ];
  2095. dec ( stack_ptr );
  2096. incode := previous_code^ [ incode ];
  2097. END;
  2098. lastoutcode := char ( incode );
  2099. IF NOT write_char ( lastoutcode ) THEN BEGIN
  2100. unshrink := unzip_writeErr;
  2101. exit
  2102. END;
  2103. FOR i := stack_ptr + 1 TO max_stack DO
  2104. IF NOT write_char ( stack^ [ i ] ) THEN BEGIN
  2105. unshrink := unzip_writeErr;
  2106. exit
  2107. END;
  2108. stack_ptr := max_stack;
  2109. END;
  2110. incode := next_free;
  2111. IF incode <= max_code THEN BEGIN
  2112. next_free := -previous_code^ [ incode ]; {Next node in free list}
  2113. previous_code^ [ incode ] := lastincode;
  2114. actual_code^ [ incode ] := lastoutcode;
  2115. END;
  2116. lastincode := new_code;
  2117. END;
  2118. END;
  2119. IF totalabort THEN
  2120. unshrink := unzip_UserAbort
  2121. ELSE IF unshrink_flush THEN
  2122. unshrink := unzip_ok
  2123. ELSE
  2124. unshrink := unzip_WriteErr;
  2125. END;
  2126. (***************************************************************************)
  2127. {***************************************************************************}
  2128. FUNCTION GetSupportedMethods : longint;
  2129. BEGIN
  2130. GetSupportedMethods := 1 + ( 1 SHL 1 ) + ( 1 SHL 6 ) + ( 1 SHL 8 );
  2131. {stored, shrunk, imploded and deflated}
  2132. END;
  2133. {******************** main low level function: unzipfile ********************}
  2134. {written and not copyrighted by Christian Ghisler}
  2135. FUNCTION unzipfile ( in_name : pchar;out_name : pchar;offset : longint;
  2136. hFileAction : word;cm_index : integer ) : integer;
  2137. VAR err : integer;
  2138. header : plocalheader;
  2139. buf : ARRAY [ 0..80 ] of char;
  2140. {$ifndef linux}
  2141. buf0 : ARRAY [ 0..3 ] of char;
  2142. {$endif}
  2143. timedate : longint;
  2144. originalcrc : longint; {crc from zip-header}
  2145. ziptype, aResult : integer;
  2146. p, p1 : pchar;
  2147. isadir : boolean;
  2148. oldcurdir : string [ 80 ];
  2149. BEGIN
  2150. {$ifdef windows}
  2151. IF inuse THEN BEGIN
  2152. {take care of crashed applications!}
  2153. IF ( lastusedtime <> 0 ) AND
  2154. ( abs ( gettickcount -lastusedtime ) > 30000 ) THEN BEGIN {1/2 minute timeout!!!}
  2155. {do not close files or free slide, they were already freed when application crashed!}
  2156. inuse := FALSE;
  2157. {memory for huffman trees is lost}
  2158. END ELSE BEGIN
  2159. unzipfile := unzip_inuse;
  2160. exit
  2161. END;
  2162. END;{inuse}
  2163. inuse := TRUE;
  2164. {$endif}
  2165. getmem ( slide, wsize );
  2166. fillchar ( slide [ 0 ], wsize, #0 );
  2167. assign ( infile, in_name );
  2168. filemode := 0;
  2169. {$I-}
  2170. reset ( infile, 1 );
  2171. {$I+}
  2172. IF ioresult <> 0 THEN BEGIN
  2173. freemem ( slide, wsize );
  2174. unzipfile := unzip_ReadErr;
  2175. inuse := FALSE;
  2176. exit
  2177. END;
  2178. {$I-}
  2179. seek ( infile, offset ); {seek to header position}
  2180. {$I+}
  2181. IF ioresult <> 0 THEN BEGIN
  2182. freemem ( slide, wsize );
  2183. close ( infile );
  2184. unzipfile := unzip_ZipFileErr;
  2185. inuse := FALSE;
  2186. exit
  2187. END;
  2188. header := @inbuf;
  2189. {$I-}
  2190. blockread ( infile, header^, sizeof ( header^ ) ); {read in local header}
  2191. {$I+}
  2192. IF ioresult <> 0 THEN BEGIN
  2193. freemem ( slide, wsize );
  2194. close ( infile );
  2195. unzipfile := unzip_ZipFileErr;
  2196. inuse := FALSE;
  2197. exit
  2198. END;
  2199. IF strlcomp ( header^.signature, 'PK'#3#4, 4 ) <> 0 THEN BEGIN
  2200. freemem ( slide, wsize );
  2201. close ( infile );
  2202. unzipfile := unzip_ZipFileErr;
  2203. inuse := FALSE;
  2204. exit
  2205. END;
  2206. {calculate offset of data}
  2207. offset := offset + header^.filename_len + header^.extra_field_len + sizeof ( tlocalheader );
  2208. timedate := header^.file_timedate;
  2209. IF ( hufttype AND 8 ) = 0 THEN BEGIN {Size and crc at the beginning}
  2210. compsize := header^.compress_size;
  2211. uncompsize := header^.uncompress_size;
  2212. originalcrc := header^.crc_32;
  2213. END ELSE BEGIN
  2214. compsize := maxlongint; {Don't get a sudden zipeof!}
  2215. uncompsize := maxlongint;
  2216. originalcrc := 0
  2217. END;
  2218. ziptype := header^.zip_type; {0=stored, 6=imploded, 8=deflated}
  2219. IF ( 1 SHL ziptype ) AND GetSupportedMethods = 0 THEN BEGIN {Not Supported!!!}
  2220. freemem ( slide, wsize );
  2221. close ( infile );
  2222. unzipfile := unzip_NotSupported;
  2223. inuse := FALSE;
  2224. exit;
  2225. END;
  2226. hufttype := header^.bit_flag;
  2227. IF ( hufttype AND 1 ) <> 0 THEN BEGIN {encrypted}
  2228. freemem ( slide, wsize );
  2229. close ( infile );
  2230. unzipfile := unzip_Encrypted;
  2231. inuse := FALSE;
  2232. exit;
  2233. END;
  2234. reachedsize := 0;
  2235. seek ( infile, offset );
  2236. assign ( outfile, out_name );
  2237. {$I-}
  2238. rewrite ( outfile, 1 );
  2239. {$I+}
  2240. err := ioresult;
  2241. {create directories not yet in path}
  2242. isadir := ( out_name [ strlen ( out_name ) -1 ] in ['/','\'] );
  2243. IF ( err = 3 ) OR isadir THEN BEGIN {path not found}
  2244. {$I-}
  2245. getdir ( 0, oldcurdir );
  2246. {$I+}
  2247. err := ioresult;
  2248. strcopy ( buf, out_name );
  2249. p1 := strrscan ( buf, DirSep );
  2250. IF p1 <> NIL THEN inc ( p1 ); {pointer to filename}
  2251. p := strtok ( buf, DirSep );
  2252. {$ifndef linux}
  2253. IF ( p <> NIL ) AND ( p [ 1 ] = ':' ) THEN BEGIN
  2254. strcopy ( buf0, 'c:\' ); {set drive}
  2255. buf0 [ 0 ] := p [ 0 ];
  2256. {$ifdef windows}
  2257. setcurdir ( buf0 );
  2258. {$else}
  2259. {$I-}
  2260. chdir ( buf0 );
  2261. {$I+}
  2262. err := ioresult;
  2263. {$endif}
  2264. p := strtok ( NIL, '\' );
  2265. END;
  2266. {$endif}
  2267. WHILE ( p <> NIL ) AND ( p <> p1 ) DO BEGIN
  2268. {$ifdef windows}
  2269. {$ifdef Delphi}
  2270. {$I-}
  2271. chdir ( strpas ( p ) );
  2272. {$I+}
  2273. err := ioresult;
  2274. {$else Delphi}
  2275. setcurdir ( p );
  2276. err := doserror;
  2277. {$endif Delphi}
  2278. {$else Windows}
  2279. {$I-}
  2280. chdir ( strpas ( p ) );
  2281. {$I+}
  2282. err := ioresult;
  2283. {$endif}
  2284. IF err <> 0 THEN BEGIN
  2285. {$ifdef windows}
  2286. createdir ( p );
  2287. err := doserror;
  2288. {$else}
  2289. {$I-}
  2290. mkdir ( strpas ( p ) );
  2291. {$I+}
  2292. err := ioresult;
  2293. {$endif}
  2294. IF err = 0 THEN
  2295. {$I-}
  2296. chdir ( strpas ( p ) );
  2297. {$I+}
  2298. err := ioresult;
  2299. END;
  2300. IF err = 0 THEN
  2301. p := strtok ( NIL, DirSep )
  2302. ELSE
  2303. p := NIL;
  2304. END;
  2305. {$I-}
  2306. chdir ( oldcurdir );
  2307. {$I+}
  2308. err := ioresult;
  2309. IF isadir THEN BEGIN
  2310. freemem ( slide, wsize );
  2311. unzipfile := unzip_Ok; {A directory -> ok}
  2312. close ( infile );
  2313. inuse := FALSE;
  2314. exit;
  2315. END;
  2316. {$I-}
  2317. rewrite ( outfile, 1 );
  2318. {$I+}
  2319. err := ioresult;
  2320. END;
  2321. IF err <> 0 THEN BEGIN
  2322. freemem ( slide, wsize );
  2323. unzipfile := unzip_WriteErr;
  2324. close ( infile );
  2325. inuse := FALSE;
  2326. exit
  2327. END;
  2328. totalabort := FALSE;
  2329. zipeof := FALSE;
  2330. {$ifdef windows}
  2331. dlghandle := hFileAction;
  2332. dlgnotify := cm_index;
  2333. messageloop;
  2334. oldpercent := 0;
  2335. {$endif}
  2336. crc32val := $FFFFFFFF;
  2337. {Unzip correct type}
  2338. CASE ziptype of
  2339. 0 : aResult := copystored;
  2340. 1 : aResult := unshrink;
  2341. 6 : aResult := explode;
  2342. 8 : aResult := inflate;
  2343. ELSE
  2344. aResult := unzip_NotSupported;
  2345. END;
  2346. unzipfile := aResult;
  2347. IF ( aResult = unzip_ok ) AND ( ( hufttype AND 8 ) <> 0 ) THEN BEGIN {CRC at the end}
  2348. dumpbits ( k AND 7 );
  2349. needbits ( 16 );
  2350. originalcrc := b AND $FFFF;
  2351. dumpbits ( 16 );
  2352. needbits ( 16 );
  2353. originalcrc := ( b AND $FFFF ) SHL 16;
  2354. dumpbits ( 16 );
  2355. END;
  2356. close ( infile );
  2357. close ( outfile );
  2358. crc32val := NOT ( crc32val ); {one's complement}
  2359. IF aResult <> 0 THEN BEGIN
  2360. erase ( outfile );
  2361. END ELSE IF ( originalcrc <> crc32val ) THEN BEGIN
  2362. unzipfile := unzip_CRCErr;
  2363. erase ( outfile );
  2364. END ELSE BEGIN
  2365. {$ifdef windows}
  2366. oldpercent := 100; {100 percent}
  2367. IF dlghandle <> 0 THEN
  2368. sendmessage ( dlghandle, wm_command, dlgnotify, longint ( @oldpercent ) );
  2369. {$endif}
  2370. filemode := 0;
  2371. reset ( outfile );
  2372. setftime ( outfile, timedate ); {set zipped time and date of oufile}
  2373. close ( outfile );
  2374. END;
  2375. freemem ( slide, wsize );
  2376. inuse := FALSE;
  2377. END;
  2378. {***************************************************************************}
  2379. {***************************************************************************}
  2380. {***************************************************************************}
  2381. { other functions; zipread.pas }
  2382. CONST mainheader : pchar = 'PK'#5#6;
  2383. maxbufsize = 64000; {Can be as low as 500 Bytes; however, }
  2384. {this would lead to extensive disk reading!}
  2385. {If one entry (including Extra field) is bigger}
  2386. {than maxbufsize, you cannot read it :-( }
  2387. TYPE
  2388. pheader = ^theader;
  2389. pmainheader = ^tmainheader;
  2390. tmainheader = PACKED RECORD
  2391. signature : ARRAY [ 0..3 ] of char; {'PK'#5#6}
  2392. thisdisk,
  2393. centralstartdisk,
  2394. entries_this_disk,
  2395. entries_central_dir : word;
  2396. headsize,
  2397. headstart : longint;
  2398. comment_len : longint;
  2399. unknown : word;
  2400. END;
  2401. theader = PACKED RECORD
  2402. signature : ARRAY [ 0..3 ] of char; {'PK'#1#2}
  2403. OSversion, {Operating system version}
  2404. OSmadeby : byte; {MSDOS (FAT): 0}
  2405. extract_ver,
  2406. bit_flag,
  2407. zip_type : word;
  2408. file_timedate : longint;
  2409. crc_32,
  2410. compress_size,
  2411. uncompress_size : longint;
  2412. filename_len,
  2413. extra_field_len,
  2414. file_comment_len,
  2415. disk_number_start,
  2416. internal_attr : word;
  2417. external_attr : ARRAY [ 0..3 ] of byte;
  2418. offset_local_header : longint;
  2419. END;
  2420. {*********** Fill out tZipRec structure with next entry *************}
  2421. FUNCTION filloutRec ( VAR zprec : tZipRec ) : integer;
  2422. VAR p : pchar;
  2423. incr : longint;
  2424. header : pheader;
  2425. offs : word;
  2426. old : char;
  2427. f : file;
  2428. extra, err : nword;
  2429. BEGIN
  2430. WITH zprec DO BEGIN
  2431. header := pheader ( @buf^ [ localstart ] );
  2432. IF ( bufsize = maxbufsize ) THEN BEGIN {Caution: header bigger than 64k!}
  2433. extra := sizeof ( file );
  2434. IF ( ( localstart + sizeof ( theader ) ) > bufsize ) OR
  2435. ( localstart + header^.filename_len + header^.extra_field_len +
  2436. header^.file_comment_len + sizeof ( theader ) > bufsize )
  2437. THEN BEGIN {Read over end of header}
  2438. move ( buf^ [ bufsize + 1 ], f, extra ); {Restore file}
  2439. move ( buf^ [ localstart ], buf^ [ 0 ], bufsize -localstart ); {Move end to beginning in buffer}
  2440. {$I-}
  2441. blockread ( f, buf^ [ bufsize -localstart ], localstart, err ); {Read in full central dir, up to maxbufsize Bytes}
  2442. {$I+}
  2443. IF ( ioresult <> 0 ) OR ( err + localstart < sizeof ( theader ) ) THEN BEGIN
  2444. filloutrec := unzip_nomoreitems;
  2445. exit
  2446. END;
  2447. move ( f, buf^ [ bufsize + 1 ], extra ); {Save changed file info!}
  2448. localstart := 0;
  2449. header := pheader ( @buf^ [ localstart ] );
  2450. END;
  2451. END;
  2452. IF ( localstart + 4 <= bufsize ) AND {Here is the ONLY correct finish!}
  2453. ( strlcomp ( header^.signature, mainheader, 4 ) = 0 ) THEN BEGIN {Main header}
  2454. filloutrec := unzip_nomoreitems;
  2455. exit
  2456. END;
  2457. IF ( localstart + sizeof ( header ) > bufsize ) OR
  2458. ( localstart + header^.filename_len + header^.extra_field_len +
  2459. header^.file_comment_len + sizeof ( theader ) > bufsize ) OR
  2460. ( strlcomp ( header^.signature, 'PK'#1#2, 4 ) <> 0 ) THEN BEGIN
  2461. filloutrec := unzip_nomoreitems;
  2462. exit
  2463. END;
  2464. size := header^.uncompress_size;
  2465. compressSize := header^.compress_size;
  2466. IF header^.osmadeby = 0 THEN
  2467. attr := header^.external_attr [ 0 ]
  2468. ELSE
  2469. attr := 0;
  2470. time := header^.file_timedate;
  2471. headeroffset := header^.offset_local_header; {Other header size}
  2472. Packmethod := header^.zip_type;
  2473. offs := localstart + header^.filename_len + sizeof ( header^ );
  2474. old := buf^ [ offs ];
  2475. buf^ [ offs ] := #0; {Repair signature of next block!}
  2476. strlcopy ( filename, pchar ( @buf^ [ localstart + sizeof ( header^ ) ] ), sizeof ( filename ) -1 );
  2477. buf^ [ offs ] := old;
  2478. {$ifndef linux}
  2479. REPEAT {Convert slash to backslash!}
  2480. p := strscan ( filename, '/' );
  2481. IF p <> NIL THEN p [ 0 ] := '\';
  2482. UNTIL p = NIL;
  2483. {$else}
  2484. REPEAT {Convert backslash to slash!}
  2485. p := strscan ( filename, '\' );
  2486. IF p <> NIL THEN p [ 0 ] := '/';
  2487. UNTIL p = NIL;
  2488. {$endif}
  2489. incr := header^.filename_len + header^.extra_field_len +
  2490. header^.file_comment_len + sizeof ( header^ );
  2491. IF incr <= 0 THEN BEGIN
  2492. filloutrec := unzip_InternalError;
  2493. exit
  2494. END;
  2495. localstart := localstart + incr;
  2496. filloutrec := unzip_ok;
  2497. END;
  2498. END;
  2499. {**************** Get first entry from ZIP file ********************}
  2500. FUNCTION GetFirstInZip ( zipfilename : pchar;VAR zprec : tZipRec ) : integer;
  2501. VAR bufstart, headerstart, start : longint;
  2502. err, i : integer;
  2503. mainh : pmainheader;
  2504. f : file;
  2505. extra : word; {Extra bytes for saving File!}
  2506. BEGIN
  2507. WITH zprec DO BEGIN
  2508. assign ( f, zipfilename );
  2509. filemode := 0; {Others may read or write};
  2510. {$I-}
  2511. reset ( f, 1 );
  2512. {$I+}
  2513. IF ioresult <> 0 THEN BEGIN
  2514. GetFirstInZip := unzip_FileError;
  2515. exit
  2516. END;
  2517. size := filesize ( f );
  2518. IF size = 0 THEN BEGIN
  2519. GetFirstInZip := unzip_FileError;
  2520. {$I-}
  2521. close ( f );
  2522. {$I+}
  2523. exit
  2524. END;
  2525. bufsize := 4096; {in 4k-blocks}
  2526. IF size > bufsize THEN BEGIN
  2527. bufstart := size -bufsize;
  2528. END ELSE BEGIN
  2529. bufstart := 0;
  2530. bufsize := size;
  2531. END;
  2532. getmem ( buf, bufsize + 1 ); {#0 at the end of filemname}
  2533. {Search from back of file to central directory start}
  2534. start := -1; {Nothing found}
  2535. REPEAT
  2536. {$I-}
  2537. seek ( f, bufstart );
  2538. {$I+}
  2539. IF ioresult <> 0 THEN BEGIN
  2540. GetFirstInZip := unzip_FileError;
  2541. freeMem ( buf, bufsize + 1 );
  2542. buf := NIL;
  2543. {$I-}
  2544. close ( f );
  2545. {$I+}
  2546. exit
  2547. END;
  2548. {$I-}
  2549. blockread ( f, buf^, bufsize, err );
  2550. {$I+}
  2551. IF ( ioresult <> 0 ) OR ( err <> bufsize ) THEN BEGIN
  2552. GetFirstInZip := unzip_FileError;
  2553. freeMem ( buf, bufsize + 1 );
  2554. buf := NIL;
  2555. {$I-}
  2556. close ( f );
  2557. {$I+}
  2558. exit
  2559. END;
  2560. IF bufstart = 0 THEN start := maxlongint;{Break}
  2561. FOR i := bufsize -22 DOWNTO 0 DO BEGIN {Search buffer backwards}
  2562. IF ( buf^ [ i ] = 'P' ) AND ( buf^ [ i + 1 ] = 'K' ) AND ( buf^ [ i + 2 ] = #5 ) AND ( buf^ [ i + 3 ] = #6 )
  2563. THEN BEGIN {Header found!!!}
  2564. start := bufstart + i;
  2565. break;
  2566. END;
  2567. END;
  2568. IF start = -1 THEN BEGIN {Nothing found yet}
  2569. dec ( bufstart, bufsize -22 ); {Full header in buffer!}
  2570. IF bufstart < 0 THEN bufstart := 0;
  2571. END;
  2572. UNTIL start >= 0;
  2573. IF ( start = maxlongint ) THEN BEGIN {Nothing found}
  2574. GetFirstInZip := unzip_FileError;
  2575. freeMem ( buf, bufsize + 1 );
  2576. buf := NIL;
  2577. {$I-}
  2578. close ( f );
  2579. {$I+}
  2580. exit
  2581. END;
  2582. mainh := pmainheader ( @buf^ [ start -bufstart ] );
  2583. headerstart := mainh^.headstart;
  2584. localstart := 0;
  2585. freeMem ( buf, bufsize + 1 );
  2586. IF ( localstart + sizeof ( theader ) > start ) THEN BEGIN
  2587. buf := NIL;
  2588. GetFirstInZip := unzip_InternalError;
  2589. {$I-}
  2590. close ( f );
  2591. {$I+}
  2592. exit
  2593. END;
  2594. bufstart := headerstart;
  2595. start := start -headerstart + 4; {size for central dir,Including main header signature}
  2596. IF start >= maxbufsize THEN BEGIN
  2597. bufsize := maxbufsize; {Max buffer size, limit of around 1000 items!}
  2598. extra := sizeof ( file ) {Save file information for later reading!}
  2599. END ELSE BEGIN
  2600. bufsize := start;
  2601. extra := 0
  2602. END;
  2603. getmem ( buf, bufsize + 1 + extra );
  2604. {$I-}
  2605. seek ( f, bufstart );
  2606. {$I+}
  2607. IF ioresult <> 0 THEN BEGIN
  2608. GetFirstInZip := unzip_FileError;
  2609. freeMem ( buf, bufsize + 1 + extra );
  2610. buf := NIL;
  2611. {$I-}
  2612. close ( f );
  2613. {$I+}
  2614. exit
  2615. END;
  2616. {$I-}
  2617. blockread ( f, buf^, bufsize, err ); {Read in full central dir, up to maxbufsize Bytes}
  2618. {$I+}
  2619. IF ioresult <> 0 THEN BEGIN
  2620. GetFirstInZip := unzip_FileError;
  2621. freeMem ( buf, bufsize + 1 + extra );
  2622. buf := NIL;
  2623. {$I-}
  2624. close ( f );
  2625. {$I+}
  2626. exit
  2627. END;
  2628. IF extra = 0 THEN
  2629. {$I-} close ( f ) {$I+}
  2630. ELSE move ( f, buf^ [ bufsize + 1 ], extra ); {Save file info!}
  2631. err := filloutRec ( zprec );
  2632. IF err <> unzip_ok THEN BEGIN
  2633. CloseZipFile ( zprec );
  2634. GetFirstInZip := err;
  2635. exit
  2636. END;
  2637. GetFirstInZip := err;
  2638. END;
  2639. END;
  2640. {**************** Get next entry from ZIP file ********************}
  2641. FUNCTION GetNextInZip ( VAR Zprec : tZiprec ) : integer;
  2642. VAR err : integer;
  2643. BEGIN
  2644. WITH zprec DO BEGIN
  2645. IF ( buf <> NIL ) THEN BEGIN {Main Header at the end}
  2646. err := filloutRec ( zprec );
  2647. IF err <> unzip_ok THEN BEGIN
  2648. CloseZipFile ( ZPRec );
  2649. END;
  2650. GetNextInZip := err;
  2651. END ELSE GetNextInZip := unzip_NoMoreItems;
  2652. END
  2653. END;
  2654. {**************** VERY simple test for zip file ********************}
  2655. FUNCTION isZip ( filename : pchar ) : boolean;
  2656. VAR
  2657. myname : tdirtype;
  2658. l, err : integer;
  2659. f : file;
  2660. buf : ARRAY [ 0..4 ] of char;
  2661. oldcurdir : string{$ifndef BIT32} [ 80 ]{$endif};
  2662. BEGIN
  2663. filemode := 0;
  2664. {$I-}
  2665. getdir ( 0, oldcurdir );
  2666. {$I+}
  2667. err := ioresult;
  2668. isZip := FALSE;
  2669. IF ( strscan ( filename, '.' ) <> NIL )
  2670. AND ( strpos ( filename, '.exe' ) = NIL ) THEN BEGIN
  2671. strcopy ( myname, filename );
  2672. l := strlen ( myname );
  2673. IF myname [ l -1 ] = DirSep THEN myname [ l -1 ] := #0;
  2674. {$I-}
  2675. chdir ( Strpas ( myname ) );
  2676. {$I+}
  2677. IF ioresult <> 0 THEN BEGIN
  2678. assign ( f, Strpas ( myname ) );
  2679. filemode := 0; {Others may read or write};
  2680. {$I-}
  2681. reset ( f, 1 );
  2682. {$I+}
  2683. IF ioresult = 0 THEN BEGIN
  2684. {$I-}
  2685. blockread ( f, buf, 4, err );
  2686. {$I+}
  2687. IF ( ioresult = 0 ) THEN BEGIN
  2688. IF ( err = 4 ) AND ( buf [ 0 ] = 'P' ) AND ( buf [ 1 ] = 'K' )
  2689. AND ( buf [ 2 ] = #3 ) AND ( buf [ 3 ] = #4 ) THEN isZip := TRUE
  2690. END;
  2691. {$I-}
  2692. close ( f );
  2693. {$I+}
  2694. err := ioresult; {only clears ioresult variable}
  2695. END;
  2696. END;
  2697. END;
  2698. {$I-}
  2699. chdir ( oldcurdir );
  2700. {$I+}
  2701. err := ioresult;
  2702. END;
  2703. {**************** free ZIP buffers ********************}
  2704. PROCEDURE CloseZipFile ( VAR Zprec : tZiprec ); {Only free buffer, file only open in Getfirstinzip}
  2705. VAR
  2706. f : file;
  2707. extra : word;
  2708. BEGIN
  2709. WITH zprec DO BEGIN
  2710. IF buf <> NIL THEN BEGIN
  2711. IF ( bufsize = maxbufsize ) THEN BEGIN {Caution: header bigger than 64k!}
  2712. extra := sizeof ( file );
  2713. move ( buf^ [ bufsize + 1 ], f, extra ); {Restore file}
  2714. {$I-}
  2715. close ( f );
  2716. {$I+}
  2717. IF ioresult <> 0 THEN ;
  2718. END ELSE extra := 0;
  2719. freemem ( buf, bufsize + 1 + extra );
  2720. buf := NIL
  2721. END;
  2722. END
  2723. END;
  2724. {***************************************************************************}
  2725. {***************************************************************************}
  2726. {********** routines by the African Chief **********************************}
  2727. {***************************************************************************}
  2728. {***************************************************************************}
  2729. {$ifndef Delphi}
  2730. FUNCTION FileExists ( CONST fname : string ) : boolean; {simple fileexist function}
  2731. VAR
  2732. f : file;
  2733. i : byte;
  2734. BEGIN
  2735. i := filemode;
  2736. filemode := 0;
  2737. assign ( f, fname );
  2738. {$i-}
  2739. Reset ( f, 1 );
  2740. filemode := i;
  2741. FileExists := ioresult = 0;
  2742. Close ( f ); IF ioresult <> 0 THEN;
  2743. {$i+}
  2744. END;
  2745. {$endif Delphi}
  2746. PROCEDURE DummyReport ( Retcode : longint;Rec : pReportRec );
  2747. {$ifdef Windows}{$ifdef win32}STDCALL;{$else}EXPORT;{$endif}{$endif}
  2748. {dummy report procedure}
  2749. BEGIN
  2750. END;
  2751. FUNCTION DummyQuestion( Rec : pReportRec ) : Boolean;
  2752. {$ifdef Windows}{$ifdef win32}STDCALL;{$else}EXPORT;{$endif}{$endif}
  2753. {dummy question procedure}
  2754. begin
  2755. DummyQuestion:=true;
  2756. end;
  2757. FUNCTION Matches ( s : String;CONST main : string ) : Boolean;
  2758. {rudimentary matching function;
  2759. accepts only '', '*.*', 'XXX.*' or '*.XXX'
  2760. }
  2761. FUNCTION extensiononly ( CONST s : string ) : string;{return just the extension}
  2762. VAR i : integer;
  2763. BEGIN
  2764. extensiononly := '';
  2765. i := pos ( '.', s );
  2766. IF i = 0 THEN exit;
  2767. extensiononly := copy ( s, succ ( i ), length ( s ) );
  2768. END;
  2769. FUNCTION nameonly ( CONST s : string ) : string;{return just the name}
  2770. VAR i : integer;
  2771. BEGIN
  2772. nameonly := s;
  2773. i := pos ( '.', s );
  2774. IF i = 0 THEN exit;
  2775. nameonly := copy ( s, 1, pred ( i ) );
  2776. END;
  2777. {!!!!!}
  2778. VAR
  2779. b : boolean;
  2780. i : integer;
  2781. BEGIN
  2782. Matches := TRUE;
  2783. IF ( s = '' ) OR ( s = AllFiles ) THEN exit; {'' or '*.*' = all files match}
  2784. s := upper ( s );
  2785. b := copy ( s, 1, 2 ) = '*.'; {e.g., *.PAS}
  2786. IF b THEN BEGIN
  2787. delete ( s, 1, 2 );
  2788. Matches := s = extensiononly ( upper ( main ) );
  2789. END ELSE BEGIN
  2790. i := length ( s );
  2791. b := s [ i ] = '*'; {e.g. TEST.*}
  2792. IF b THEN BEGIN
  2793. IF s [ pred ( i ) ] = '.' THEN delete ( s, pred ( i ), 2 );
  2794. i := length ( s );
  2795. IF s [ i ] in [ '*', '?' ] THEN dec ( i );{e.g. TEST*.*}
  2796. Matches := Copy ( s, 1, i ) = Copy ( nameonly ( upper ( main ) ), 1, i );
  2797. END ELSE Matches := s = upper ( main );
  2798. END;
  2799. END; { Matches }
  2800. {****************************************************}
  2801. FUNCTION FileUnzip ( SourceZipFile, TargetDirectory, FileSpecs : pChar;
  2802. Report : UnzipReportProc;Question : UnzipQuestionProc ) : integer;
  2803. VAR
  2804. rc : integer;
  2805. r : tziprec;
  2806. buf,
  2807. thename,
  2808. target : ARRAY [ 0..tFSize ] of char;
  2809. Count : integer;
  2810. rSize, cSize : longint;
  2811. s : string [ 255 ];
  2812. BEGIN
  2813. {$IFDEF FPC}
  2814. IF not assigned(Report) THEN
  2815. Report := @DummyReport;
  2816. IF not assigned(Question) THEN
  2817. Question := @DummyQuestion;
  2818. {$ELSE}
  2819. IF @Report = nil THEN
  2820. Report := DummyReport;
  2821. IF @Question = nil THEN
  2822. Question := DummyQuestion;
  2823. {$ENDIF}
  2824. Count := 0;
  2825. rSize := 0;
  2826. cSize := 0;
  2827. FileUnzip := unzip_MissingParameter;
  2828. IF ( StrPas ( SourceZipFile ) = '' ) OR ( StrPas ( TargetDirectory ) = '' ) THEN Exit;
  2829. Strcopy ( thename, SourceZipFile );
  2830. Strcopy ( target, TargetDirectory );
  2831. IF ( target [ 0 ] <> #0 ) AND ( target [ strlen ( target ) -1 ] <> DirSep )
  2832. THEN strcat ( target, DirSep );
  2833. FileUnzip := unzip_NotZipFile;
  2834. IF NOT iszip ( thename ) THEN exit;
  2835. FillChar ( ZipRec, Sizeof ( ZipRec ), #0 );
  2836. WITH ZipRec DO BEGIN
  2837. IsaDir := FALSE;
  2838. strcopy ( FileName, thename );
  2839. Size := UnZipSize ( SourceZipFile, CompressSize );
  2840. IF Size = 0 THEN ratio := 0 ELSE
  2841. Ratio := 100 -Round ( ( CompressSize / Size ) * 100 );
  2842. Status := unzip_starting;
  2843. Report ( Status, @ZipRec );
  2844. END; {start of ZIP file}
  2845. ZipReport := Report;
  2846. rc := getfirstinzip ( thename, r );
  2847. WHILE ( rc = unzip_ok )
  2848. DO BEGIN
  2849. IF ( Matches ( StrPas ( FileSpecs ), Strpas ( R.FileName ) ) )
  2850. THEN BEGIN
  2851. Inc ( rSize, r.Size );
  2852. Inc ( cSize, r.CompressSize );
  2853. strcopy ( buf, target );
  2854. IF NoRecurseDirs { no recursion }
  2855. THEN BEGIN
  2856. s := StripPath ( Strpas ( r.filename ) ) + #0;
  2857. Strcat ( buf, @s [ 1 ] );
  2858. END ELSE strcat ( buf, r.filename );
  2859. WITH ZipRec DO BEGIN { report start of file }
  2860. s := StrPas ( Buf );
  2861. IsaDir := s [ length ( s ) ] = DirSep;
  2862. Time := r.Time;
  2863. Size := r.Size;
  2864. CompressSize := r.CompressSize;
  2865. strcopy ( FileName, buf );
  2866. PackMethod := r.PackMethod;
  2867. Attr := r.Attr;
  2868. IF Size = 0 THEN ratio := 0 ELSE
  2869. Ratio := 100 -Round ( ( CompressSize /Size ) * 100 );
  2870. Status := file_starting;
  2871. IF ( IsaDir ) AND ( NoRecurseDirs )
  2872. THEN {} ELSE
  2873. ZipReport ( Status, @ZipRec );
  2874. END; { start of file }
  2875. IF ( FileExists ( StrPas ( buf ) ) )
  2876. AND ( Question ( @ZipRec ) = FALSE )
  2877. THEN BEGIN
  2878. rc := unzip_ok; { we are okay }
  2879. WITH ZipRec DO BEGIN
  2880. Status := file_unzipping;
  2881. PackMethod := 9; { skipped }
  2882. ZipReport ( Size, @ZipRec ); { report uncompressed size }
  2883. END;
  2884. END ELSE BEGIN
  2885. rc := unzipfile ( thename, buf, r.headeroffset, 0,
  2886. {$ifdef windows}vk_escape{$else}27{$endif} ); {Escape interrupts}
  2887. END;
  2888. IF rc = unzip_ok
  2889. THEN BEGIN
  2890. Inc ( Count );
  2891. WITH ZipRec DO BEGIN { report end of file }
  2892. Status := file_completed;
  2893. IF ( IsaDir ) AND ( NoRecurseDirs )
  2894. THEN {} ELSE
  2895. ZipReport ( Status, @ZipRec );
  2896. END; { end of file }
  2897. END ELSE BEGIN
  2898. ZipRec.Status := file_failure; {error}
  2899. CASE rc of
  2900. unzip_CRCErr,
  2901. unzip_WriteErr,
  2902. unzip_Encrypted,
  2903. unzip_NotSupported : ZipReport ( rc, @ZipRec );
  2904. unzip_ReadErr, unzip_Userabort,
  2905. unzip_FileError, unzip_InternalError,
  2906. unzip_InUse, unzip_ZipFileErr :
  2907. BEGIN
  2908. ZipRec.Status := unzip_SeriousError;
  2909. FileUnzip := unzip_SeriousError; {Serious error, force abort}
  2910. ZipReport ( unzip_SeriousError, @ZipRec );
  2911. closezipfile ( r );
  2912. ZipReport := NIL;
  2913. ZipQuestion := NIL;
  2914. exit;
  2915. END;
  2916. END; {case rc}
  2917. Continue;
  2918. {rc:=getnextinzip(r);}
  2919. END; {else}
  2920. END; { if Matches }
  2921. rc := getnextinzip ( r );
  2922. END; {while }
  2923. closezipfile ( r ); {Free memory used for central directory info}
  2924. WITH ZipRec DO BEGIN { report end of ZIP file }
  2925. Time := -1;
  2926. Attr := -1;
  2927. PackMethod := 0;
  2928. Size := rSize;
  2929. CompressSize := cSize;
  2930. strcopy ( FileName, thename );
  2931. IF Size = 0 THEN ratio := 0 ELSE
  2932. Ratio := 100 -Round ( ( CompressSize /Size ) * 100 );
  2933. Status := unzip_completed;
  2934. ZipReport ( Status, @ZipRec );
  2935. END; { end of ZIP file }
  2936. ZipReport := NIL;
  2937. ZipQuestion := NIL;
  2938. FileUnzip := Count;
  2939. END; { FileUnzip }
  2940. {***************************************************************************}
  2941. FUNCTION FileUnzipEx ( SourceZipFile, TargetDirectory, FileSpecs : pChar ) : integer;
  2942. BEGIN
  2943. FileUnzipEx :=
  2944. FileUnzip ( SourceZipFile, TargetDirectory, FileSpecs, ZipReport, ZipQuestion );
  2945. END; { FileUnzipEx }
  2946. {***************************************************************************}
  2947. FUNCTION Viewzip ( SourceZipFile, FileSpecs : pChar; Report : UnzipReportProc ) : integer;
  2948. VAR
  2949. rc : integer;
  2950. r : tziprec;
  2951. thename : ARRAY [ 0..tFSize ] of char;
  2952. Count : integer;
  2953. rSize, cSize : longint;
  2954. BEGIN
  2955. Count := 0;
  2956. rSize := 0;
  2957. cSize := 0;
  2958. Viewzip := unzip_MissingParameter;
  2959. {$IFDEF FPC}
  2960. IF ( StrPas ( SourceZipFile ) = '' ) or
  2961. not assigned(Report) THEN
  2962. exit;
  2963. {$ELSE}
  2964. IF ( StrPas ( SourceZipFile ) = '' ) OR ( @Report = NIL ) THEN Exit;
  2965. {$ENDIF}
  2966. Strcopy ( thename, SourceZipFile );
  2967. ViewZip := unzip_NotZipFile;
  2968. IF NOT iszip ( thename ) THEN exit;
  2969. FillChar ( ZipRec, Sizeof ( ZipRec ), #0 );
  2970. rc := getfirstinzip ( thename, r );
  2971. WHILE ( rc = unzip_ok )
  2972. DO BEGIN
  2973. IF ( Matches ( StrPas ( FileSpecs ), Strpas ( R.FileName ) ) ) THEN BEGIN
  2974. Inc ( rSize, r.Size );
  2975. Inc ( cSize, r.CompressSize );
  2976. WITH ZipRec DO BEGIN
  2977. Time := r.Time;
  2978. Size := r.Size;
  2979. CompressSize := r.CompressSize;
  2980. strcopy ( FileName, r.Filename );
  2981. PackMethod := r.PackMethod;
  2982. Attr := r.Attr;
  2983. IF Size = 0 THEN ratio := 0 ELSE
  2984. Ratio := 100 -Round ( ( CompressSize /Size ) * 100 );
  2985. END;
  2986. Inc ( Count );
  2987. Report ( rc, @ZipRec );
  2988. END; {matches}
  2989. rc := getnextinzip ( r );
  2990. END; {while }
  2991. closezipfile ( r );
  2992. WITH ZipRec DO BEGIN
  2993. Time := -1;
  2994. Attr := -1;
  2995. PackMethod := 0;
  2996. Size := rSize;
  2997. CompressSize := cSize;
  2998. strcopy ( FileName, thename );
  2999. IF Size = 0 THEN ratio := 0 ELSE
  3000. Ratio := 100 -Round ( ( CompressSize /Size ) * 100 );
  3001. END;
  3002. Report ( Count, @ZipRec );
  3003. ViewZip := Count;
  3004. END; { ViewZip }
  3005. {***************************************************************************}
  3006. FUNCTION UnZipSize ( SourceZipFile : pChar;VAR Compressed : Longint ) : longint;
  3007. VAR
  3008. rc : integer;
  3009. r : tziprec;
  3010. thename : ARRAY [ 0..tFSize ] of char;
  3011. Count : longint;
  3012. f : file;
  3013. BEGIN
  3014. Compressed := 0;
  3015. UnZipSize := 0;
  3016. IF ( StrPas ( SourceZipFile ) = '' ) THEN Exit;
  3017. System.Assign ( f, StrPas ( SourceZipFile ) );
  3018. count := filemode;
  3019. filemode := 0;
  3020. {$i-}
  3021. Reset ( f, 1 );
  3022. filemode := count;
  3023. IF ioresult <> 0 THEN exit;
  3024. Count := filesize ( f );
  3025. close ( f );
  3026. UnZipSize := count;
  3027. Compressed := count;
  3028. Strcopy ( thename, SourceZipFile );
  3029. IF NOT iszip ( thename ) THEN exit;
  3030. Count := 0;
  3031. Compressed := 0;
  3032. rc := getfirstinzip ( thename, r );
  3033. WHILE ( rc = unzip_ok )
  3034. DO BEGIN
  3035. Inc ( Count, r.Size );
  3036. Inc ( Compressed, r.CompressSize );
  3037. rc := getnextinzip ( r );
  3038. END; {while }
  3039. closezipfile ( r );
  3040. UnZipSize := Count;
  3041. END; { UnZipSize }
  3042. {***************************************************************************}
  3043. FUNCTION SetUnZipReportProc ( aProc : UnzipReportProc ) : Pointer;
  3044. BEGIN
  3045. {$IFDEF FPC}
  3046. SetUnZipReportProc := ZipReport; {save and return original}
  3047. {$ELSE}
  3048. SetUnZipReportProc := @ZipReport; {save and return original}
  3049. {$ENDIF}
  3050. ZipReport := aProc;
  3051. END; { SetUnZipReportProc }
  3052. {***************************************************************************}
  3053. FUNCTION SetUnZipQuestionProc ( aProc : UnzipQuestionProc ) : Pointer;
  3054. BEGIN
  3055. {$IFDEF FPC}
  3056. SetUnZipQuestionProc := ZipQuestion; {save and return original}
  3057. {$ELSE}
  3058. SetUnZipQuestionProc := @ZipQuestion; {save and return original}
  3059. {$ENDIF}
  3060. ZipQuestion := aProc;
  3061. END; { SetUnZipQuestionProc }
  3062. {***************************************************************************}
  3063. FUNCTION SetNoRecurseDirs ( DontRecurse : Boolean ) : Boolean;
  3064. BEGIN
  3065. SetNoRecurseDirs := NoRecurseDirs;
  3066. NoRecurseDirs := DontRecurse;
  3067. END; { SetNoRecurseDirs }
  3068. {***************************************************************************}
  3069. {***************************************************************************}
  3070. PROCEDURE ChfUnzip_Init;
  3071. BEGIN
  3072. slide := NIL; {unused}
  3073. {$ifdef windows}
  3074. inuse := FALSE; {Not yet in use!}
  3075. lastusedtime := 0; {Not yet used}
  3076. {$endif}
  3077. if inuse then; { to remove warning }
  3078. SetUnZipReportProc ( NIL );
  3079. SetUnZipQuestionProc ( NIL );
  3080. SetNoRecurseDirs ( FALSE );
  3081. END;
  3082. {***************************************************************************}
  3083. {***************************************************************************}
  3084. {***************************************************************************}
  3085. BEGIN
  3086. ChfUnzip_Init;
  3087. END.
  3088. {
  3089. $Log$
  3090. Revision 1.1 2000-07-13 06:30:22 michael
  3091. + Initial import
  3092. Revision 1.5 2000/02/24 17:47:47 peter
  3093. * last fixes for 0.99.14a release
  3094. Revision 1.4 2000/01/26 21:49:33 peter
  3095. * install.pas compilable by FPC again
  3096. * removed some notes from unzip.pas
  3097. * support installer creation under linux (install has name conflict)
  3098. Revision 1.3 1999/06/10 15:00:16 peter
  3099. * fixed to compile for not os2
  3100. * update install.dat
  3101. Revision 1.2 1999/06/10 07:28:28 hajny
  3102. * compilable with TP again
  3103. Revision 1.1 1999/02/19 16:45:26 peter
  3104. * moved to fpinst/ directory
  3105. + makefile
  3106. }