unzip.pp 97 KB

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