unzip.pas 95 KB

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