unzip51g.pp 92 KB

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