unzip51g.pp 93 KB

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