unzip.pas 94 KB

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