unzip.pas 97 KB

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