unzip.pp 91 KB

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