unzip.pas 94 KB

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