zdeflate.pas 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  1. Unit zDeflate;
  2. {$ifdef fpc}
  3. {$goto on}
  4. {$endif}
  5. { Orginal: deflate.h -- internal compression state
  6. deflate.c -- compress data using the deflation algorithm
  7. Copyright (C) 1995-1996 Jean-loup Gailly.
  8. Pascal tranlastion
  9. Copyright (C) 1998 by Jacques Nomssi Nzali
  10. For conditions of distribution and use, see copyright notice in readme.txt
  11. }
  12. { ALGORITHM
  13. The "deflation" process depends on being able to identify portions
  14. of the input text which are identical to earlier input (within a
  15. sliding window trailing behind the input currently being processed).
  16. The most straightforward technique turns out to be the fastest for
  17. most input files: try all possible matches and select the longest.
  18. The key feature of this algorithm is that insertions into the string
  19. dictionary are very simple and thus fast, and deletions are avoided
  20. completely. Insertions are performed at each input character, whereas
  21. string matches are performed only when the previous match ends. So it
  22. is preferable to spend more time in matches to allow very fast string
  23. insertions and avoid deletions. The matching algorithm for small
  24. strings is inspired from that of Rabin & Karp. A brute force approach
  25. is used to find longer strings when a small match has been found.
  26. A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  27. (by Leonid Broukhis).
  28. A previous version of this file used a more sophisticated algorithm
  29. (by Fiala and Greene) which is guaranteed to run in linear amortized
  30. time, but has a larger average cost, uses more memory and is patented.
  31. However the F&G algorithm may be faster for some highly redundant
  32. files if the parameter max_chain_length (described below) is too large.
  33. ACKNOWLEDGEMENTS
  34. The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  35. I found it in 'freeze' written by Leonid Broukhis.
  36. Thanks to many people for bug reports and testing.
  37. REFERENCES
  38. Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  39. Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  40. A description of the Rabin and Karp algorithm is given in the book
  41. "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  42. Fiala,E.R., and Greene,D.H.
  43. Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595}
  44. interface
  45. {$I zconf.inc}
  46. uses
  47. zutil, zbase;
  48. function deflateInit_(strm : z_streamp;
  49. level : int;
  50. const version : string;
  51. stream_size : int) : int;
  52. function deflateInit (var strm : z_stream; level : int) : int;
  53. { Initializes the internal stream state for compression. The fields
  54. zalloc, zfree and opaque must be initialized before by the caller.
  55. If zalloc and zfree are set to Z_NULL, deflateInit updates them to
  56. use default allocation functions.
  57. The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
  58. 1 gives best speed, 9 gives best compression, 0 gives no compression at
  59. all (the input data is simply copied a block at a time).
  60. Z_DEFAULT_COMPRESSION requests a default compromise between speed and
  61. compression (currently equivalent to level 6).
  62. deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
  63. enough memory, Z_STREAM_ERROR if level is not a valid compression level,
  64. Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
  65. with the version assumed by the caller (ZLIB_VERSION).
  66. msg is set to null if there is no error message. deflateInit does not
  67. perform any compression: this will be done by deflate(). }
  68. {EXPORT}
  69. function deflate (var strm : z_stream; flush : int) : int;
  70. { Performs one or both of the following actions:
  71. - Compress more input starting at next_in and update next_in and avail_in
  72. accordingly. If not all input can be processed (because there is not
  73. enough room in the output buffer), next_in and avail_in are updated and
  74. processing will resume at this point for the next call of deflate().
  75. - Provide more output starting at next_out and update next_out and avail_out
  76. accordingly. This action is forced if the parameter flush is non zero.
  77. Forcing flush frequently degrades the compression ratio, so this parameter
  78. should be set only when necessary (in interactive applications).
  79. Some output may be provided even if flush is not set.
  80. Before the call of deflate(), the application should ensure that at least
  81. one of the actions is possible, by providing more input and/or consuming
  82. more output, and updating avail_in or avail_out accordingly; avail_out
  83. should never be zero before the call. The application can consume the
  84. compressed output when it wants, for example when the output buffer is full
  85. (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
  86. and with zero avail_out, it must be called again after making room in the
  87. output buffer because there might be more output pending.
  88. If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression
  89. block is terminated and flushed to the output buffer so that the
  90. decompressor can get all input data available so far. For method 9, a future
  91. variant on method 8, the current block will be flushed but not terminated.
  92. Z_SYNC_FLUSH has the same effect as partial flush except that the compressed
  93. output is byte aligned (the compressor can clear its internal bit buffer)
  94. and the current block is always terminated; this can be useful if the
  95. compressor has to be restarted from scratch after an interruption (in which
  96. case the internal state of the compressor may be lost).
  97. If flush is set to Z_FULL_FLUSH, the compression block is terminated, a
  98. special marker is output and the compression dictionary is discarded; this
  99. is useful to allow the decompressor to synchronize if one compressed block
  100. has been damaged (see inflateSync below). Flushing degrades compression and
  101. so should be used only when necessary. Using Z_FULL_FLUSH too often can
  102. seriously degrade the compression. If deflate returns with avail_out == 0,
  103. this function must be called again with the same value of the flush
  104. parameter and more output space (updated avail_out), until the flush is
  105. complete (deflate returns with non-zero avail_out).
  106. If the parameter flush is set to Z_FINISH, all pending input is processed,
  107. all pending output is flushed and deflate returns with Z_STREAM_END if there
  108. was enough output space; if deflate returns with Z_OK, this function must be
  109. called again with Z_FINISH and more output space (updated avail_out) but no
  110. more input data, until it returns with Z_STREAM_END or an error. After
  111. deflate has returned Z_STREAM_END, the only possible operations on the
  112. stream are deflateReset or deflateEnd.
  113. Z_FINISH can be used immediately after deflateInit if all the compression
  114. is to be done in a single step. In this case, avail_out must be at least
  115. 0.1% larger than avail_in plus 12 bytes. If deflate does not return
  116. Z_STREAM_END, then it must be called again as described above.
  117. deflate() may update data_type if it can make a good guess about
  118. the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
  119. binary. This field is only for information purposes and does not affect
  120. the compression algorithm in any manner.
  121. deflate() returns Z_OK if some progress has been made (more input
  122. processed or more output produced), Z_STREAM_END if all input has been
  123. consumed and all output has been produced (only when flush is set to
  124. Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
  125. if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible. }
  126. function deflateEnd (var strm : z_stream) : int;
  127. { All dynamically allocated data structures for this stream are freed.
  128. This function discards any unprocessed input and does not flush any
  129. pending output.
  130. deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
  131. stream state was inconsistent, Z_DATA_ERROR if the stream was freed
  132. prematurely (some input or output was discarded). In the error case,
  133. msg may be set but then points to a static string (which must not be
  134. deallocated). }
  135. { Advanced functions }
  136. { The following functions are needed only in some special applications. }
  137. function deflateInit2_(var strm : z_stream;
  138. level : int;
  139. method : int;
  140. windowBits : int;
  141. memLevel : int;
  142. strategy : int;
  143. const version : string;
  144. stream_size : int) : int;
  145. {EXPORT}
  146. function deflateInit2 (var strm : z_stream;
  147. level : int;
  148. method : int;
  149. windowBits : int;
  150. memLevel : int;
  151. strategy : int) : int;
  152. { This is another version of deflateInit with more compression options. The
  153. fields next_in, zalloc, zfree and opaque must be initialized before by
  154. the caller.
  155. The method parameter is the compression method. It must be Z_DEFLATED in
  156. this version of the library. (Method 9 will allow a 64K history buffer and
  157. partial block flushes.)
  158. The windowBits parameter is the base two logarithm of the window size
  159. (the size of the history buffer). It should be in the range 8..15 for this
  160. version of the library (the value 16 will be allowed for method 9). Larger
  161. values of this parameter result in better compression at the expense of
  162. memory usage. The default value is 15 if deflateInit is used instead.
  163. The memLevel parameter specifies how much memory should be allocated
  164. for the internal compression state. memLevel=1 uses minimum memory but
  165. is slow and reduces compression ratio; memLevel=9 uses maximum memory
  166. for optimal speed. The default value is 8. See zconf.h for total memory
  167. usage as a function of windowBits and memLevel.
  168. The strategy parameter is used to tune the compression algorithm. Use the
  169. value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
  170. filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
  171. string match). Filtered data consists mostly of small values with a
  172. somewhat random distribution. In this case, the compression algorithm is
  173. tuned to compress them better. The effect of Z_FILTERED is to force more
  174. Huffman coding and less string matching; it is somewhat intermediate
  175. between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
  176. the compression ratio but not the correctness of the compressed output even
  177. if it is not set appropriately.
  178. If next_in is not null, the library will use this buffer to hold also
  179. some history information; the buffer must either hold the entire input
  180. data, or have at least 1<<(windowBits+1) bytes and be writable. If next_in
  181. is null, the library will allocate its own history buffer (and leave next_in
  182. null). next_out need not be provided here but must be provided by the
  183. application for the next call of deflate().
  184. If the history buffer is provided by the application, next_in must
  185. must never be changed by the application since the compressor maintains
  186. information inside this buffer from call to call; the application
  187. must provide more input only by increasing avail_in. next_in is always
  188. reset by the library in this case.
  189. deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
  190. not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
  191. an invalid method). msg is set to null if there is no error message.
  192. deflateInit2 does not perform any compression: this will be done by
  193. deflate(). }
  194. {EXPORT}
  195. function deflateSetDictionary (var strm : z_stream;
  196. dictionary : pBytef; {const bytes}
  197. dictLength : uint) : int;
  198. { Initializes the compression dictionary (history buffer) from the given
  199. byte sequence without producing any compressed output. This function must
  200. be called immediately after deflateInit or deflateInit2, before any call
  201. of deflate. The compressor and decompressor must use exactly the same
  202. dictionary (see inflateSetDictionary).
  203. The dictionary should consist of strings (byte sequences) that are likely
  204. to be encountered later in the data to be compressed, with the most commonly
  205. used strings preferably put towards the end of the dictionary. Using a
  206. dictionary is most useful when the data to be compressed is short and
  207. can be predicted with good accuracy; the data can then be compressed better
  208. than with the default empty dictionary. In this version of the library,
  209. only the last 32K bytes of the dictionary are used.
  210. Upon return of this function, strm->adler is set to the Adler32 value
  211. of the dictionary; the decompressor may later use this value to determine
  212. which dictionary has been used by the compressor. (The Adler32 value
  213. applies to the whole dictionary even if only a subset of the dictionary is
  214. actually used by the compressor.)
  215. deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
  216. parameter is invalid (such as NULL dictionary) or the stream state
  217. is inconsistent (for example if deflate has already been called for this
  218. stream). deflateSetDictionary does not perform any compression: this will
  219. be done by deflate(). }
  220. {EXPORT}
  221. function deflateCopy (dest : z_streamp;
  222. source : z_streamp) : int;
  223. { Sets the destination stream as a complete copy of the source stream. If
  224. the source stream is using an application-supplied history buffer, a new
  225. buffer is allocated for the destination stream. The compressed output
  226. buffer is always application-supplied. It's the responsibility of the
  227. application to provide the correct values of next_out and avail_out for the
  228. next call of deflate.
  229. This function can be useful when several compression strategies will be
  230. tried, for example when there are several ways of pre-processing the input
  231. data with a filter. The streams that will be discarded should then be freed
  232. by calling deflateEnd. Note that deflateCopy duplicates the internal
  233. compression state which can be quite large, so this strategy is slow and
  234. can consume lots of memory.
  235. deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
  236. enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
  237. (such as zalloc being NULL). msg is left unchanged in both source and
  238. destination. }
  239. {EXPORT}
  240. function deflateReset (var strm : z_stream) : int;
  241. { This function is equivalent to deflateEnd followed by deflateInit,
  242. but does not free and reallocate all the internal compression state.
  243. The stream will keep the same compression level and any other attributes
  244. that may have been set by deflateInit2.
  245. deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
  246. stream state was inconsistent (such as zalloc or state being NIL). }
  247. {EXPORT}
  248. function deflateParams (var strm : z_stream; level : int; strategy : int) : int;
  249. { Dynamically update the compression level and compression strategy.
  250. This can be used to switch between compression and straight copy of
  251. the input data, or to switch to a different kind of input data requiring
  252. a different strategy. If the compression level is changed, the input
  253. available so far is compressed with the old level (and may be flushed);
  254. the new level will take effect only at the next call of deflate().
  255. Before the call of deflateParams, the stream state must be set as for
  256. a call of deflate(), since the currently available input may have to
  257. be compressed and flushed. In particular, strm->avail_out must be non-zero.
  258. deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
  259. stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
  260. if strm->avail_out was zero. }
  261. const
  262. deflate_copyright : string = ' deflate 1.1.2 Copyright 1995-1998 Jean-loup Gailly ';
  263. { If you use the zlib library in a product, an acknowledgment is welcome
  264. in the documentation of your product. If for some reason you cannot
  265. include such an acknowledgment, I would appreciate that you keep this
  266. copyright string in the executable of your product. }
  267. implementation
  268. uses
  269. trees, adler;
  270. { ===========================================================================
  271. Function prototypes. }
  272. type
  273. block_state = (
  274. need_more, { block not completed, need more input or more output }
  275. block_done, { block flush performed }
  276. finish_started, { finish started, need only more output at next deflate }
  277. finish_done); { finish done, accept no more input or output }
  278. { Compression function. Returns the block state after the call. }
  279. type
  280. compress_func = function(var s : deflate_state; flush : int) : block_state;
  281. {local}
  282. procedure fill_window(var s : deflate_state); forward;
  283. {local}
  284. function deflate_stored(var s : deflate_state; flush : int) : block_state;{$ifndef fpc}far;{$endif} forward;
  285. {local}
  286. function deflate_fast(var s : deflate_state; flush : int) : block_state;{$ifndef fpc}far;{$endif} forward;
  287. {local}
  288. function deflate_slow(var s : deflate_state; flush : int) : block_state;{$ifndef fpc}far;{$endif} forward;
  289. {local}
  290. procedure lm_init(var s : deflate_state); forward;
  291. {local}
  292. procedure putShortMSB(var s : deflate_state; b : uInt); forward;
  293. {local}
  294. procedure flush_pending (var strm : z_stream); forward;
  295. {local}
  296. function read_buf(strm : z_streamp;
  297. buf : pBytef;
  298. size : unsigned) : int; forward;
  299. {$ifdef ASMV}
  300. procedure match_init; { asm code initialization }
  301. function longest_match(var deflate_state; cur_match : IPos) : uInt; forward;
  302. {$else}
  303. {local}
  304. function longest_match(var s : deflate_state; cur_match : IPos) : uInt;
  305. forward;
  306. {$endif}
  307. {$ifdef DEBUG}
  308. {local}
  309. procedure check_match(var s : deflate_state;
  310. start, match : IPos;
  311. length : int); forward;
  312. {$endif}
  313. { ==========================================================================
  314. local data }
  315. const
  316. ZNIL = 0;
  317. { Tail of hash chains }
  318. const
  319. TOO_FAR = 4096;
  320. { Matches of length 3 are discarded if their distance exceeds TOO_FAR }
  321. const
  322. MIN_LOOKAHEAD = (MAX_MATCH+MIN_MATCH+1);
  323. { Minimum amount of lookahead, except at the end of the input file.
  324. See deflate.c for comments about the MIN_MATCH+1. }
  325. {macro MAX_DIST(var s : deflate_state) : uInt;
  326. begin
  327. MAX_DIST := (s.w_size - MIN_LOOKAHEAD);
  328. end;
  329. In order to simplify the code, particularly on 16 bit machines, match
  330. distances are limited to MAX_DIST instead of WSIZE. }
  331. { Values for max_lazy_match, good_match and max_chain_length, depending on
  332. the desired pack level (0..9). The values given below have been tuned to
  333. exclude worst case performance for pathological files. Better values may be
  334. found for specific files. }
  335. type
  336. config = record
  337. good_length : ush; { reduce lazy search above this match length }
  338. max_lazy : ush; { do not perform lazy search above this match length }
  339. nice_length : ush; { quit search above this match length }
  340. max_chain : ush;
  341. func : compress_func;
  342. end;
  343. {local}
  344. const
  345. configuration_table : array[0..10-1] of config = (
  346. { good lazy nice chain }
  347. {0} (good_length:0; max_lazy:0; nice_length:0; max_chain:0; func:@deflate_stored), { store only }
  348. {1} (good_length:4; max_lazy:4; nice_length:8; max_chain:4; func:@deflate_fast), { maximum speed, no lazy matches }
  349. {2} (good_length:4; max_lazy:5; nice_length:16; max_chain:8; func:@deflate_fast),
  350. {3} (good_length:4; max_lazy:6; nice_length:32; max_chain:32; func:@deflate_fast),
  351. {4} (good_length:4; max_lazy:4; nice_length:16; max_chain:16; func:@deflate_slow), { lazy matches }
  352. {5} (good_length:8; max_lazy:16; nice_length:32; max_chain:32; func:@deflate_slow),
  353. {6} (good_length:8; max_lazy:16; nice_length:128; max_chain:128; func:@deflate_slow),
  354. {7} (good_length:8; max_lazy:32; nice_length:128; max_chain:256; func:@deflate_slow),
  355. {8} (good_length:32; max_lazy:128; nice_length:258; max_chain:1024; func:@deflate_slow),
  356. {9} (good_length:32; max_lazy:258; nice_length:258; max_chain:4096; func:@deflate_slow)); { maximum compression }
  357. { Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  358. For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  359. meaning. }
  360. const
  361. EQUAL = 0;
  362. { result of memcmp for equal strings }
  363. { ==========================================================================
  364. Update a hash value with the given input byte
  365. IN assertion: all calls to to UPDATE_HASH are made with consecutive
  366. input characters, so that a running hash key can be computed from the
  367. previous key instead of complete recalculation each time.
  368. macro UPDATE_HASH(s,h,c)
  369. h := (( (h) shl s^.hash_shift) xor (c)) and s^.hash_mask;
  370. }
  371. { ===========================================================================
  372. Insert string str in the dictionary and set match_head to the previous head
  373. of the hash chain (the most recent string with same hash key). Return
  374. the previous length of the hash chain.
  375. If this file is compiled with -DFASTEST, the compression level is forced
  376. to 1, and no hash chains are maintained.
  377. IN assertion: all calls to to INSERT_STRING are made with consecutive
  378. input characters and the first MIN_MATCH bytes of str are valid
  379. (except for the last MIN_MATCH-1 bytes of the input file). }
  380. procedure INSERT_STRING(var s : deflate_state;
  381. str : uInt;
  382. var match_head : IPos);
  383. begin
  384. {$ifdef FASTEST}
  385. {UPDATE_HASH(s, s.ins_h, s.window[(str) + (MIN_MATCH-1)])}
  386. s.ins_h := ((s.ins_h shl s.hash_shift) xor
  387. (s.window^[(str) + (MIN_MATCH-1)])) and s.hash_mask;
  388. match_head := s.head[s.ins_h]
  389. s.head[s.ins_h] := Pos(str);
  390. {$else}
  391. {UPDATE_HASH(s, s.ins_h, s.window[(str) + (MIN_MATCH-1)])}
  392. s.ins_h := ((s.ins_h shl s.hash_shift) xor
  393. (s.window^[(str) + (MIN_MATCH-1)])) and s.hash_mask;
  394. match_head := s.head^[s.ins_h];
  395. s.prev^[(str) and s.w_mask] := match_head;
  396. s.head^[s.ins_h] := Pos(str);
  397. {$endif}
  398. end;
  399. { =========================================================================
  400. Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  401. prev[] will be initialized on the fly.
  402. macro CLEAR_HASH(s)
  403. s^.head[s^.hash_size-1] := ZNIL;
  404. zmemzero(pBytef(s^.head), unsigned(s^.hash_size-1)*sizeof(s^.head^[0]));
  405. }
  406. { ======================================================================== }
  407. function deflateInit2_(var strm : z_stream;
  408. level : int;
  409. method : int;
  410. windowBits : int;
  411. memLevel : int;
  412. strategy : int;
  413. const version : string;
  414. stream_size : int) : int;
  415. var
  416. s : deflate_state_ptr;
  417. noheader : int;
  418. overlay : pushfArray;
  419. { We overlay pending_buf and d_buf+l_buf. This works since the average
  420. output size for (length,distance) codes is <= 24 bits. }
  421. begin
  422. noheader := 0;
  423. if (version = '') or (version[1] <> ZLIB_VERSION[1]) or
  424. (stream_size <> sizeof(z_stream)) then
  425. begin
  426. deflateInit2_ := Z_VERSION_ERROR;
  427. exit;
  428. end;
  429. {
  430. if (strm = Z_NULL) then
  431. begin
  432. deflateInit2_ := Z_STREAM_ERROR;
  433. exit;
  434. end;
  435. }
  436. { SetLength(strm.msg, 255); }
  437. strm.msg := '';
  438. if not Assigned(strm.zalloc) then
  439. begin
  440. {$ifdef fpc}
  441. strm.zalloc := @zcalloc;
  442. {$else}
  443. strm.zalloc := zcalloc;
  444. {$endif}
  445. strm.opaque := voidpf(0);
  446. end;
  447. if not Assigned(strm.zfree) then
  448. {$ifdef fpc}
  449. strm.zfree := @zcfree;
  450. {$else}
  451. strm.zfree := zcfree;
  452. {$endif}
  453. if (level = Z_DEFAULT_COMPRESSION) then
  454. level := 6;
  455. {$ifdef FASTEST}
  456. level := 1;
  457. {$endif}
  458. if (windowBits < 0) then { undocumented feature: suppress zlib header }
  459. begin
  460. noheader := 1;
  461. windowBits := -windowBits;
  462. end;
  463. if (memLevel < 1) or (memLevel > MAX_MEM_LEVEL) or (method <> Z_DEFLATED)
  464. or (windowBits < 8) or (windowBits > 15) or (level < 0)
  465. or (level > 9) or (strategy < 0) or (strategy > Z_HUFFMAN_ONLY) then
  466. begin
  467. deflateInit2_ := Z_STREAM_ERROR;
  468. exit;
  469. end;
  470. s := deflate_state_ptr (ZALLOC(strm, 1, sizeof(deflate_state)));
  471. if (s = Z_NULL) then
  472. begin
  473. deflateInit2_ := Z_MEM_ERROR;
  474. exit;
  475. end;
  476. strm.state := pInternal_state(s);
  477. s^.strm := @strm;
  478. s^.noheader := noheader;
  479. s^.w_bits := windowBits;
  480. s^.w_size := 1 shl s^.w_bits;
  481. s^.w_mask := s^.w_size - 1;
  482. s^.hash_bits := memLevel + 7;
  483. s^.hash_size := 1 shl s^.hash_bits;
  484. s^.hash_mask := s^.hash_size - 1;
  485. s^.hash_shift := ((s^.hash_bits+MIN_MATCH-1) div MIN_MATCH);
  486. s^.window := pzByteArray (ZALLOC(strm, s^.w_size, 2*sizeof(Byte)));
  487. s^.prev := pzPosfArray (ZALLOC(strm, s^.w_size, sizeof(Pos)));
  488. s^.head := pzPosfArray (ZALLOC(strm, s^.hash_size, sizeof(Pos)));
  489. s^.lit_bufsize := 1 shl (memLevel + 6); { 16K elements by default }
  490. overlay := pushfArray (ZALLOC(strm, s^.lit_bufsize, sizeof(ush)+2));
  491. s^.pending_buf := pzByteArray (overlay);
  492. s^.pending_buf_size := ulg(s^.lit_bufsize) * (sizeof(ush)+Long(2));
  493. if (s^.window = Z_NULL) or (s^.prev = Z_NULL) or (s^.head = Z_NULL)
  494. or (s^.pending_buf = Z_NULL) then
  495. begin
  496. {ERR_MSG(Z_MEM_ERROR);}
  497. strm.msg := z_errmsg[z_errbase-Z_MEM_ERROR];
  498. deflateEnd (strm);
  499. deflateInit2_ := Z_MEM_ERROR;
  500. exit;
  501. end;
  502. s^.d_buf := pushfArray( @overlay^[s^.lit_bufsize div sizeof(ush)] );
  503. s^.l_buf := puchfArray( @s^.pending_buf^[(1+sizeof(ush))*s^.lit_bufsize] );
  504. s^.level := level;
  505. s^.strategy := strategy;
  506. s^.method := Byte(method);
  507. deflateInit2_ := deflateReset(strm);
  508. end;
  509. { ========================================================================= }
  510. function deflateInit2(var strm : z_stream;
  511. level : int;
  512. method : int;
  513. windowBits : int;
  514. memLevel : int;
  515. strategy : int) : int;
  516. { a macro }
  517. begin
  518. deflateInit2 := deflateInit2_(strm, level, method, windowBits,
  519. memLevel, strategy, ZLIB_VERSION, sizeof(z_stream));
  520. end;
  521. { ========================================================================= }
  522. function deflateInit_(strm : z_streamp;
  523. level : int;
  524. const version : string;
  525. stream_size : int) : int;
  526. begin
  527. if (strm = Z_NULL) then
  528. deflateInit_ := Z_STREAM_ERROR
  529. else
  530. deflateInit_ := deflateInit2_(strm^, level, Z_DEFLATED, MAX_WBITS,
  531. DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size);
  532. { To do: ignore strm^.next_in if we use it as window }
  533. end;
  534. { ========================================================================= }
  535. function deflateInit(var strm : z_stream; level : int) : int;
  536. { deflateInit is a macro to allow checking the zlib version
  537. and the compiler's view of z_stream: }
  538. begin
  539. deflateInit := deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS,
  540. DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, ZLIB_VERSION, sizeof(z_stream));
  541. end;
  542. { ======================================================================== }
  543. function deflateSetDictionary (var strm : z_stream;
  544. dictionary : pBytef;
  545. dictLength : uInt) : int;
  546. var
  547. s : deflate_state_ptr;
  548. length : uInt;
  549. n : uInt;
  550. hash_head : IPos;
  551. var
  552. MAX_DIST : uInt; {macro}
  553. begin
  554. length := dictLength;
  555. hash_head := 0;
  556. if {(@strm = Z_NULL) or}
  557. (strm.state = Z_NULL) or (dictionary = Z_NULL)
  558. or (deflate_state_ptr(strm.state)^.status <> INIT_STATE) then
  559. begin
  560. deflateSetDictionary := Z_STREAM_ERROR;
  561. exit;
  562. end;
  563. s := deflate_state_ptr(strm.state);
  564. strm.adler := adler32(strm.adler, dictionary, dictLength);
  565. if (length < MIN_MATCH) then
  566. begin
  567. deflateSetDictionary := Z_OK;
  568. exit;
  569. end;
  570. MAX_DIST := (s^.w_size - MIN_LOOKAHEAD);
  571. if (length > MAX_DIST) then
  572. begin
  573. length := MAX_DIST;
  574. {$ifndef USE_DICT_HEAD}
  575. Inc(dictionary, dictLength - length); { use the tail of the dictionary }
  576. {$endif}
  577. end;
  578. zmemcpy( pBytef(s^.window), dictionary, length);
  579. s^.strstart := length;
  580. s^.block_start := long(length);
  581. { Insert all strings in the hash table (except for the last two bytes).
  582. s^.lookahead stays null, so s^.ins_h will be recomputed at the next
  583. call of fill_window. }
  584. s^.ins_h := s^.window^[0];
  585. {UPDATE_HASH(s, s^.ins_h, s^.window[1]);}
  586. s^.ins_h := ((s^.ins_h shl s^.hash_shift) xor (s^.window^[1]))
  587. and s^.hash_mask;
  588. for n := 0 to length - MIN_MATCH do
  589. begin
  590. INSERT_STRING(s^, n, hash_head);
  591. end;
  592. {if (hash_head <> 0) then
  593. hash_head := 0; - to make compiler happy }
  594. deflateSetDictionary := Z_OK;
  595. end;
  596. { ======================================================================== }
  597. function deflateReset (var strm : z_stream) : int;
  598. var
  599. s : deflate_state_ptr;
  600. begin
  601. if {(@strm = Z_NULL) or}
  602. (strm.state = Z_NULL)
  603. or (not Assigned(strm.zalloc)) or (not Assigned(strm.zfree)) then
  604. begin
  605. deflateReset := Z_STREAM_ERROR;
  606. exit;
  607. end;
  608. strm.total_out := 0;
  609. strm.total_in := 0;
  610. strm.msg := ''; { use zfree if we ever allocate msg dynamically }
  611. strm.data_type := Z_UNKNOWN;
  612. s := deflate_state_ptr(strm.state);
  613. s^.pending := 0;
  614. s^.pending_out := pBytef(s^.pending_buf);
  615. if (s^.noheader < 0) then
  616. begin
  617. s^.noheader := 0; { was set to -1 by deflate(..., Z_FINISH); }
  618. end;
  619. if s^.noheader <> 0 then
  620. s^.status := BUSY_STATE
  621. else
  622. s^.status := INIT_STATE;
  623. strm.adler := 1;
  624. s^.last_flush := Z_NO_FLUSH;
  625. _tr_init(s^);
  626. lm_init(s^);
  627. deflateReset := Z_OK;
  628. end;
  629. { ======================================================================== }
  630. function deflateParams(var strm : z_stream;
  631. level : int;
  632. strategy : int) : int;
  633. var
  634. s : deflate_state_ptr;
  635. func : compress_func;
  636. err : int;
  637. begin
  638. err := Z_OK;
  639. if {(@strm = Z_NULL) or} (strm.state = Z_NULL) then
  640. begin
  641. deflateParams := Z_STREAM_ERROR;
  642. exit;
  643. end;
  644. s := deflate_state_ptr(strm.state);
  645. if (level = Z_DEFAULT_COMPRESSION) then
  646. begin
  647. level := 6;
  648. end;
  649. if (level < 0) or (level > 9) or (strategy < 0)
  650. or (strategy > Z_HUFFMAN_ONLY) then
  651. begin
  652. deflateParams := Z_STREAM_ERROR;
  653. exit;
  654. end;
  655. func := configuration_table[s^.level].func;
  656. if (@func <> @configuration_table[level].func)
  657. and (strm.total_in <> 0) then
  658. begin
  659. { Flush the last buffer: }
  660. err := deflate(strm, Z_PARTIAL_FLUSH);
  661. end;
  662. if (s^.level <> level) then
  663. begin
  664. s^.level := level;
  665. s^.max_lazy_match := configuration_table[level].max_lazy;
  666. s^.good_match := configuration_table[level].good_length;
  667. s^.nice_match := configuration_table[level].nice_length;
  668. s^.max_chain_length := configuration_table[level].max_chain;
  669. end;
  670. s^.strategy := strategy;
  671. deflateParams := err;
  672. end;
  673. { =========================================================================
  674. Put a short in the pending buffer. The 16-bit value is put in MSB order.
  675. IN assertion: the stream state is correct and there is enough room in
  676. pending_buf. }
  677. {local}
  678. procedure putShortMSB (var s : deflate_state; b : uInt);
  679. begin
  680. s.pending_buf^[s.pending] := Byte(b shr 8);
  681. Inc(s.pending);
  682. s.pending_buf^[s.pending] := Byte(b and $ff);
  683. Inc(s.pending);
  684. end;
  685. { =========================================================================
  686. Flush as much pending output as possible. All deflate() output goes
  687. through this function so some applications may wish to modify it
  688. to avoid allocating a large strm^.next_out buffer and copying into it.
  689. (See also read_buf()). }
  690. {local}
  691. procedure flush_pending(var strm : z_stream);
  692. var
  693. len : unsigned;
  694. s : deflate_state_ptr;
  695. begin
  696. s := deflate_state_ptr(strm.state);
  697. len := s^.pending;
  698. if (len > strm.avail_out) then
  699. len := strm.avail_out;
  700. if (len = 0) then
  701. exit;
  702. zmemcpy(strm.next_out, s^.pending_out, len);
  703. Inc(strm.next_out, len);
  704. Inc(s^.pending_out, len);
  705. Inc(strm.total_out, len);
  706. Dec(strm.avail_out, len);
  707. Dec(s^.pending, len);
  708. if (s^.pending = 0) then
  709. begin
  710. s^.pending_out := pBytef(s^.pending_buf);
  711. end;
  712. end;
  713. { ========================================================================= }
  714. function deflate (var strm : z_stream; flush : int) : int;
  715. var
  716. old_flush : int; { value of flush param for previous deflate call }
  717. s : deflate_state_ptr;
  718. var
  719. header : uInt;
  720. level_flags : uInt;
  721. var
  722. bstate : block_state;
  723. begin
  724. if {(@strm = Z_NULL) or} (strm.state = Z_NULL)
  725. or (flush > Z_FINISH) or (flush < 0) then
  726. begin
  727. deflate := Z_STREAM_ERROR;
  728. exit;
  729. end;
  730. s := deflate_state_ptr(strm.state);
  731. if (strm.next_out = Z_NULL) or
  732. ((strm.next_in = Z_NULL) and (strm.avail_in <> 0)) or
  733. ((s^.status = FINISH_STATE) and (flush <> Z_FINISH)) then
  734. begin
  735. {ERR_RETURN(strm^, Z_STREAM_ERROR);}
  736. strm.msg := z_errmsg[z_errbase - Z_STREAM_ERROR];
  737. deflate := Z_STREAM_ERROR;
  738. exit;
  739. end;
  740. if (strm.avail_out = 0) then
  741. begin
  742. {ERR_RETURN(strm^, Z_BUF_ERROR);}
  743. strm.msg := z_errmsg[z_errbase - Z_BUF_ERROR];
  744. deflate := Z_BUF_ERROR;
  745. exit;
  746. end;
  747. s^.strm := @strm; { just in case }
  748. old_flush := s^.last_flush;
  749. s^.last_flush := flush;
  750. { Write the zlib header }
  751. if (s^.status = INIT_STATE) then
  752. begin
  753. header := (Z_DEFLATED + ((s^.w_bits-8) shl 4)) shl 8;
  754. level_flags := (s^.level-1) shr 1;
  755. if (level_flags > 3) then
  756. level_flags := 3;
  757. header := header or (level_flags shl 6);
  758. if (s^.strstart <> 0) then
  759. header := header or PRESET_DICT;
  760. Inc(header, 31 - (header mod 31));
  761. s^.status := BUSY_STATE;
  762. putShortMSB(s^, header);
  763. { Save the adler32 of the preset dictionary: }
  764. if (s^.strstart <> 0) then
  765. begin
  766. putShortMSB(s^, uInt(strm.adler shr 16));
  767. putShortMSB(s^, uInt(strm.adler and $ffff));
  768. end;
  769. strm.adler := long(1);
  770. end;
  771. { Flush as much pending output as possible }
  772. if (s^.pending <> 0) then
  773. begin
  774. flush_pending(strm);
  775. if (strm.avail_out = 0) then
  776. begin
  777. { Since avail_out is 0, deflate will be called again with
  778. more output space, but possibly with both pending and
  779. avail_in equal to zero. There won't be anything to do,
  780. but this is not an error situation so make sure we
  781. return OK instead of BUF_ERROR at next call of deflate: }
  782. s^.last_flush := -1;
  783. deflate := Z_OK;
  784. exit;
  785. end;
  786. { Make sure there is something to do and avoid duplicate consecutive
  787. flushes. For repeated and useless calls with Z_FINISH, we keep
  788. returning Z_STREAM_END instead of Z_BUFF_ERROR. }
  789. end
  790. else
  791. if (strm.avail_in = 0) and (flush <= old_flush)
  792. and (flush <> Z_FINISH) then
  793. begin
  794. {ERR_RETURN(strm^, Z_BUF_ERROR);}
  795. strm.msg := z_errmsg[z_errbase - Z_BUF_ERROR];
  796. deflate := Z_BUF_ERROR;
  797. exit;
  798. end;
  799. { User must not provide more input after the first FINISH: }
  800. if (s^.status = FINISH_STATE) and (strm.avail_in <> 0) then
  801. begin
  802. {ERR_RETURN(strm^, Z_BUF_ERROR);}
  803. strm.msg := z_errmsg[z_errbase - Z_BUF_ERROR];
  804. deflate := Z_BUF_ERROR;
  805. exit;
  806. end;
  807. { Start a new block or continue the current one. }
  808. if (strm.avail_in <> 0) or (s^.lookahead <> 0)
  809. or ((flush <> Z_NO_FLUSH) and (s^.status <> FINISH_STATE)) then
  810. begin
  811. bstate := configuration_table[s^.level].func(s^, flush);
  812. if (bstate = finish_started) or (bstate = finish_done) then
  813. s^.status := FINISH_STATE;
  814. if (bstate = need_more) or (bstate = finish_started) then
  815. begin
  816. if (strm.avail_out = 0) then
  817. s^.last_flush := -1; { avoid BUF_ERROR next call, see above }
  818. deflate := Z_OK;
  819. exit;
  820. { If flush != Z_NO_FLUSH && avail_out == 0, the next call
  821. of deflate should use the same flush parameter to make sure
  822. that the flush is complete. So we don't have to output an
  823. empty block here, this will be done at next call. This also
  824. ensures that for a very small output buffer, we emit at most
  825. one empty block. }
  826. end;
  827. if (bstate = block_done) then
  828. begin
  829. if (flush = Z_PARTIAL_FLUSH) then
  830. _tr_align(s^)
  831. else
  832. begin { FULL_FLUSH or SYNC_FLUSH }
  833. _tr_stored_block(s^, pcharf(NIL), Long(0), FALSE);
  834. { For a full flush, this empty block will be recognized
  835. as a special marker by inflate_sync(). }
  836. if (flush = Z_FULL_FLUSH) then
  837. begin
  838. {macro CLEAR_HASH(s);} { forget history }
  839. s^.head^[s^.hash_size-1] := ZNIL;
  840. zmemzero(pBytef(s^.head), unsigned(s^.hash_size-1)*sizeof(s^.head^[0]));
  841. end;
  842. end;
  843. flush_pending(strm);
  844. if (strm.avail_out = 0) then
  845. begin
  846. s^.last_flush := -1; { avoid BUF_ERROR at next call, see above }
  847. deflate := Z_OK;
  848. exit;
  849. end;
  850. end;
  851. end;
  852. {$IFDEF DEBUG}
  853. Assert(strm.avail_out > 0, 'bug2');
  854. {$ENDIF}
  855. if (flush <> Z_FINISH) then
  856. begin
  857. deflate := Z_OK;
  858. exit;
  859. end;
  860. if (s^.noheader <> 0) then
  861. begin
  862. deflate := Z_STREAM_END;
  863. exit;
  864. end;
  865. { Write the zlib trailer (adler32) }
  866. putShortMSB(s^, uInt(strm.adler shr 16));
  867. putShortMSB(s^, uInt(strm.adler and $ffff));
  868. flush_pending(strm);
  869. { If avail_out is zero, the application will call deflate again
  870. to flush the rest. }
  871. s^.noheader := -1; { write the trailer only once! }
  872. if s^.pending <> 0 then
  873. deflate := Z_OK
  874. else
  875. deflate := Z_STREAM_END;
  876. end;
  877. { ========================================================================= }
  878. function deflateEnd (var strm : z_stream) : int;
  879. var
  880. status : int;
  881. s : deflate_state_ptr;
  882. begin
  883. if {(@strm = Z_NULL) or} (strm.state = Z_NULL) then
  884. begin
  885. deflateEnd := Z_STREAM_ERROR;
  886. exit;
  887. end;
  888. s := deflate_state_ptr(strm.state);
  889. status := s^.status;
  890. if (status <> INIT_STATE) and (status <> BUSY_STATE) and
  891. (status <> FINISH_STATE) then
  892. begin
  893. deflateEnd := Z_STREAM_ERROR;
  894. exit;
  895. end;
  896. { Deallocate in reverse order of allocations: }
  897. TRY_FREE(strm, s^.pending_buf);
  898. TRY_FREE(strm, s^.head);
  899. TRY_FREE(strm, s^.prev);
  900. TRY_FREE(strm, s^.window);
  901. ZFREE(strm, s);
  902. strm.state := Z_NULL;
  903. if status = BUSY_STATE then
  904. deflateEnd := Z_DATA_ERROR
  905. else
  906. deflateEnd := Z_OK;
  907. end;
  908. { =========================================================================
  909. Copy the source state to the destination state.
  910. To simplify the source, this is not supported for 16-bit MSDOS (which
  911. doesn't have enough memory anyway to duplicate compression states). }
  912. { ========================================================================= }
  913. function deflateCopy (dest, source : z_streamp) : int;
  914. {$ifndef MAXSEG_64K}
  915. var
  916. ds : deflate_state_ptr;
  917. ss : deflate_state_ptr;
  918. overlay : pushfArray;
  919. {$endif}
  920. begin
  921. {$ifdef MAXSEG_64K}
  922. deflateCopy := Z_STREAM_ERROR;
  923. exit;
  924. {$else}
  925. if (source = Z_NULL) or (dest = Z_NULL) or (source^.state = Z_NULL) then
  926. begin
  927. deflateCopy := Z_STREAM_ERROR;
  928. exit;
  929. end;
  930. ss := deflate_state_ptr(source^.state);
  931. dest^ := source^;
  932. ds := deflate_state_ptr( ZALLOC(dest^, 1, sizeof(deflate_state)) );
  933. if (ds = Z_NULL) then
  934. begin
  935. deflateCopy := Z_MEM_ERROR;
  936. exit;
  937. end;
  938. dest^.state := pInternal_state(ds);
  939. ds^ := ss^;
  940. ds^.strm := dest;
  941. ds^.window := pzByteArray ( ZALLOC(dest^, ds^.w_size, 2*sizeof(Byte)) );
  942. ds^.prev := pzPosfArray ( ZALLOC(dest^, ds^.w_size, sizeof(Pos)) );
  943. ds^.head := pzPosfArray ( ZALLOC(dest^, ds^.hash_size, sizeof(Pos)) );
  944. overlay := pushfArray ( ZALLOC(dest^, ds^.lit_bufsize, sizeof(ush)+2) );
  945. ds^.pending_buf := pzByteArray ( overlay );
  946. if (ds^.window = Z_NULL) or (ds^.prev = Z_NULL) or (ds^.head = Z_NULL)
  947. or (ds^.pending_buf = Z_NULL) then
  948. begin
  949. deflateEnd (dest^);
  950. deflateCopy := Z_MEM_ERROR;
  951. exit;
  952. end;
  953. { following zmemcpy do not work for 16-bit MSDOS }
  954. zmemcpy(pBytef(ds^.window), pBytef(ss^.window), ds^.w_size * 2 * sizeof(Byte));
  955. zmemcpy(pBytef(ds^.prev), pBytef(ss^.prev), ds^.w_size * sizeof(Pos));
  956. zmemcpy(pBytef(ds^.head), pBytef(ss^.head), ds^.hash_size * sizeof(Pos));
  957. zmemcpy(pBytef(ds^.pending_buf), pBytef(ss^.pending_buf), uInt(ds^.pending_buf_size));
  958. ds^.pending_out := @ds^.pending_buf^[ptr2int(ss^.pending_out) - ptr2int(ss^.pending_buf)];
  959. ds^.d_buf := pushfArray (@overlay^[ds^.lit_bufsize div sizeof(ush)] );
  960. ds^.l_buf := puchfArray (@ds^.pending_buf^[(1+sizeof(ush))*ds^.lit_bufsize]);
  961. ds^.l_desc.dyn_tree := tree_ptr(@ds^.dyn_ltree);
  962. ds^.d_desc.dyn_tree := tree_ptr(@ds^.dyn_dtree);
  963. ds^.bl_desc.dyn_tree := tree_ptr(@ds^.bl_tree);
  964. deflateCopy := Z_OK;
  965. {$endif}
  966. end;
  967. { ===========================================================================
  968. Read a new buffer from the current input stream, update the adler32
  969. and total number of bytes read. All deflate() input goes through
  970. this function so some applications may wish to modify it to avoid
  971. allocating a large strm^.next_in buffer and copying from it.
  972. (See also flush_pending()). }
  973. {local}
  974. function read_buf(strm : z_streamp; buf : pBytef; size : unsigned) : int;
  975. var
  976. len : unsigned;
  977. begin
  978. len := strm^.avail_in;
  979. if (len > size) then
  980. len := size;
  981. if (len = 0) then
  982. begin
  983. read_buf := 0;
  984. exit;
  985. end;
  986. Dec(strm^.avail_in, len);
  987. if deflate_state_ptr(strm^.state)^.noheader = 0 then
  988. begin
  989. strm^.adler := adler32(strm^.adler, strm^.next_in, len);
  990. end;
  991. zmemcpy(buf, strm^.next_in, len);
  992. Inc(strm^.next_in, len);
  993. Inc(strm^.total_in, len);
  994. read_buf := int(len);
  995. end;
  996. { ===========================================================================
  997. Initialize the "longest match" routines for a new zlib stream }
  998. {local}
  999. procedure lm_init (var s : deflate_state);
  1000. begin
  1001. s.window_size := ulg( uLong(2)*s.w_size);
  1002. {macro CLEAR_HASH(s);}
  1003. s.head^[s.hash_size-1] := ZNIL;
  1004. zmemzero(pBytef(s.head), unsigned(s.hash_size-1)*sizeof(s.head^[0]));
  1005. { Set the default configuration parameters: }
  1006. s.max_lazy_match := configuration_table[s.level].max_lazy;
  1007. s.good_match := configuration_table[s.level].good_length;
  1008. s.nice_match := configuration_table[s.level].nice_length;
  1009. s.max_chain_length := configuration_table[s.level].max_chain;
  1010. s.strstart := 0;
  1011. s.block_start := long(0);
  1012. s.lookahead := 0;
  1013. s.prev_length := MIN_MATCH-1;
  1014. s.match_length := MIN_MATCH-1;
  1015. s.match_available := FALSE;
  1016. s.ins_h := 0;
  1017. {$ifdef ASMV}
  1018. match_init; { initialize the asm code }
  1019. {$endif}
  1020. end;
  1021. { ===========================================================================
  1022. Set match_start to the longest match starting at the given string and
  1023. return its length. Matches shorter or equal to prev_length are discarded,
  1024. in which case the result is equal to prev_length and match_start is
  1025. garbage.
  1026. IN assertions: cur_match is the head of the hash chain for the current
  1027. string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1028. OUT assertion: the match length is not greater than s^.lookahead. }
  1029. {$ifndef ASMV}
  1030. { For 80x86 and 680x0, an optimized version will be provided in match.asm or
  1031. match.S. The code will be functionally equivalent. }
  1032. {$ifndef FASTEST}
  1033. {local}
  1034. function longest_match(var s : deflate_state;
  1035. cur_match : IPos { current match }
  1036. ) : uInt;
  1037. label
  1038. nextstep;
  1039. var
  1040. chain_length : unsigned; { max hash chain length }
  1041. {register} scan : pBytef; { current string }
  1042. {register} match : pBytef; { matched string }
  1043. {register} len : int; { length of current match }
  1044. best_len : int; { best match length so far }
  1045. nice_match : int; { stop if match long enough }
  1046. limit : IPos;
  1047. prev : pzPosfArray;
  1048. wmask : uInt;
  1049. {$ifdef UNALIGNED_OK}
  1050. {register} strend : pBytef;
  1051. {register} scan_start : ush;
  1052. {register} scan_end : ush;
  1053. {$else}
  1054. {register} strend : pBytef;
  1055. {register} scan_end1 : Byte;
  1056. {register} scan_end : Byte;
  1057. {$endif}
  1058. var
  1059. MAX_DIST : uInt;
  1060. begin
  1061. chain_length := s.max_chain_length; { max hash chain length }
  1062. scan := @(s.window^[s.strstart]);
  1063. best_len := s.prev_length; { best match length so far }
  1064. nice_match := s.nice_match; { stop if match long enough }
  1065. MAX_DIST := s.w_size - MIN_LOOKAHEAD;
  1066. {In order to simplify the code, particularly on 16 bit machines, match
  1067. distances are limited to MAX_DIST instead of WSIZE. }
  1068. if s.strstart > IPos(MAX_DIST) then
  1069. limit := s.strstart - IPos(MAX_DIST)
  1070. else
  1071. limit := ZNIL;
  1072. { Stop when cur_match becomes <= limit. To simplify the code,
  1073. we prevent matches with the string of window index 0. }
  1074. prev := s.prev;
  1075. wmask := s.w_mask;
  1076. {$ifdef UNALIGNED_OK}
  1077. { Compare two bytes at a time. Note: this is not always beneficial.
  1078. Try with and without -DUNALIGNED_OK to check. }
  1079. strend := pBytef(@(s.window^[s.strstart + MAX_MATCH - 1]));
  1080. scan_start := pushf(scan)^;
  1081. scan_end := pushfArray(scan)^[best_len-1]; { fix }
  1082. {$else}
  1083. strend := pBytef(@(s.window^[s.strstart + MAX_MATCH]));
  1084. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  1085. scan_end1 := pzByteArray(scan)^[best_len-1];
  1086. {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
  1087. scan_end := pzByteArray(scan)^[best_len];
  1088. {$endif}
  1089. { The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1090. It is easy to get rid of this optimization if necessary. }
  1091. {$IFDEF DEBUG}
  1092. Assert((s.hash_bits >= 8) and (MAX_MATCH = 258), 'Code too clever');
  1093. {$ENDIF}
  1094. { Do not waste too much time if we already have a good match: }
  1095. if (s.prev_length >= s.good_match) then
  1096. begin
  1097. chain_length := chain_length shr 2;
  1098. end;
  1099. { Do not look for matches beyond the end of the input. This is necessary
  1100. to make deflate deterministic. }
  1101. if (uInt(nice_match) > s.lookahead) then
  1102. nice_match := s.lookahead;
  1103. {$IFDEF DEBUG}
  1104. Assert(ulg(s.strstart) <= s.window_size-MIN_LOOKAHEAD, 'need lookahead');
  1105. {$ENDIF}
  1106. repeat
  1107. {$IFDEF DEBUG}
  1108. Assert(cur_match < s.strstart, 'no future');
  1109. {$ENDIF}
  1110. match := @(s.window^[cur_match]);
  1111. { Skip to next match if the match length cannot increase
  1112. or if the match length is less than 2: }
  1113. {$undef DO_UNALIGNED_OK}
  1114. {$ifdef UNALIGNED_OK}
  1115. {$ifdef MAX_MATCH_IS_258}
  1116. {$define DO_UNALIGNED_OK}
  1117. {$endif}
  1118. {$endif}
  1119. {$ifdef DO_UNALIGNED_OK}
  1120. { This code assumes sizeof(unsigned short) = 2. Do not use
  1121. UNALIGNED_OK if your compiler uses a different size. }
  1122. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  1123. if (pushfArray(match)^[best_len-1] <> scan_end) or
  1124. (pushf(match)^ <> scan_start) then
  1125. goto nextstep; {continue;}
  1126. {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
  1127. { It is not necessary to compare scan[2] and match[2] since they are
  1128. always equal when the other bytes match, given that the hash keys
  1129. are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  1130. strstart+3, +5, ... up to strstart+257. We check for insufficient
  1131. lookahead only every 4th comparison; the 128th check will be made
  1132. at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  1133. necessary to put more guard bytes at the end of the window, or
  1134. to check more often for insufficient lookahead. }
  1135. {$IFDEF DEBUG}
  1136. Assert(pzByteArray(scan)^[2] = pzByteArray(match)^[2], 'scan[2]?');
  1137. {$ENDIF}
  1138. Inc(scan);
  1139. Inc(match);
  1140. repeat
  1141. Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
  1142. Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
  1143. Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
  1144. Inc(scan,2); Inc(match,2); if (pushf(scan)^<>pushf(match)^) then break;
  1145. until (ptr2int(scan) >= ptr2int(strend));
  1146. { The funny "do while" generates better code on most compilers }
  1147. { Here, scan <= window+strstart+257 }
  1148. {$IFDEF DEBUG}
  1149. Assert(ptr2int(scan) <=
  1150. ptr2int(@(s.window^[unsigned(s.window_size-1)])),
  1151. 'wild scan');
  1152. {$ENDIF}
  1153. if (scan^ = match^) then
  1154. Inc(scan);
  1155. len := (MAX_MATCH - 1) - int(ptr2int(strend)-ptr2int(scan));
  1156. scan := strend;
  1157. Dec(scan, (MAX_MATCH-1));
  1158. {$else} { UNALIGNED_OK }
  1159. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  1160. if (pzByteArray(match)^[best_len] <> scan_end) or
  1161. (pzByteArray(match)^[best_len-1] <> scan_end1) or
  1162. (match^ <> scan^) then
  1163. goto nextstep; {continue;}
  1164. {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
  1165. Inc(match);
  1166. if (match^ <> pzByteArray(scan)^[1]) then
  1167. goto nextstep; {continue;}
  1168. { The check at best_len-1 can be removed because it will be made
  1169. again later. (This heuristic is not always a win.)
  1170. It is not necessary to compare scan[2] and match[2] since they
  1171. are always equal when the other bytes match, given that
  1172. the hash keys are equal and that HASH_BITS >= 8. }
  1173. Inc(scan, 2);
  1174. Inc(match);
  1175. {$IFDEF DEBUG}
  1176. Assert( scan^ = match^, 'match[2]?');
  1177. {$ENDIF}
  1178. { We check for insufficient lookahead only every 8th comparison;
  1179. the 256th check will be made at strstart+258. }
  1180. repeat
  1181. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1182. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1183. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1184. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1185. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1186. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1187. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1188. Inc(scan); Inc(match); if (scan^ <> match^) then break;
  1189. until (ptr2int(scan) >= ptr2int(strend));
  1190. {$IFDEF DEBUG}
  1191. Assert(ptr2int(scan) <=
  1192. ptr2int(@(s.window^[unsigned(s.window_size-1)])),
  1193. 'wild scan');
  1194. {$ENDIF}
  1195. len := MAX_MATCH - int(ptr2int(strend) - ptr2int(scan));
  1196. scan := strend;
  1197. Dec(scan, MAX_MATCH);
  1198. {$endif} { UNALIGNED_OK }
  1199. if (len > best_len) then
  1200. begin
  1201. s.match_start := cur_match;
  1202. best_len := len;
  1203. if (len >= nice_match) then
  1204. break;
  1205. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  1206. {$ifdef UNALIGNED_OK}
  1207. scan_end := pzByteArray(scan)^[best_len-1];
  1208. {$else}
  1209. scan_end1 := pzByteArray(scan)^[best_len-1];
  1210. scan_end := pzByteArray(scan)^[best_len];
  1211. {$endif}
  1212. {$IFDEF NoRangeCheck} {$R+} {$UNDEF NoRangeCheck} {$ENDIF}
  1213. end;
  1214. nextstep:
  1215. cur_match := prev^[cur_match and wmask];
  1216. Dec(chain_length);
  1217. until (cur_match <= limit) or (chain_length = 0);
  1218. if (uInt(best_len) <= s.lookahead) then
  1219. longest_match := uInt(best_len)
  1220. else
  1221. longest_match := s.lookahead;
  1222. end;
  1223. {$endif} { ASMV }
  1224. {$else} { FASTEST }
  1225. { ---------------------------------------------------------------------------
  1226. Optimized version for level = 1 only }
  1227. {local}
  1228. function longest_match(var s : deflate_state;
  1229. cur_match : IPos { current match }
  1230. ) : uInt;
  1231. var
  1232. {register} scan : pBytef; { current string }
  1233. {register} match : pBytef; { matched string }
  1234. {register} len : int; { length of current match }
  1235. {register} strend : pBytef;
  1236. begin
  1237. scan := @s.window^[s.strstart];
  1238. strend := @s.window^[s.strstart + MAX_MATCH];
  1239. { The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1240. It is easy to get rid of this optimization if necessary. }
  1241. {$IFDEF DEBUG}
  1242. Assert((s.hash_bits >= 8) and (MAX_MATCH = 258), 'Code too clever');
  1243. Assert(ulg(s.strstart) <= s.window_size-MIN_LOOKAHEAD, 'need lookahead');
  1244. Assert(cur_match < s.strstart, 'no future');
  1245. {$ENDIF}
  1246. match := s.window + cur_match;
  1247. { Return failure if the match length is less than 2: }
  1248. if (match[0] <> scan[0]) or (match[1] <> scan[1]) then
  1249. begin
  1250. longest_match := MIN_MATCH-1;
  1251. exit;
  1252. end;
  1253. { The check at best_len-1 can be removed because it will be made
  1254. again later. (This heuristic is not always a win.)
  1255. It is not necessary to compare scan[2] and match[2] since they
  1256. are always equal when the other bytes match, given that
  1257. the hash keys are equal and that HASH_BITS >= 8. }
  1258. scan += 2, match += 2;
  1259. Assert(scan^ = match^, 'match[2]?');
  1260. { We check for insufficient lookahead only every 8th comparison;
  1261. the 256th check will be made at strstart+258. }
  1262. repeat
  1263. Inc(scan); Inc(match); if scan^<>match^ then break;
  1264. Inc(scan); Inc(match); if scan^<>match^ then break;
  1265. Inc(scan); Inc(match); if scan^<>match^ then break;
  1266. Inc(scan); Inc(match); if scan^<>match^ then break;
  1267. Inc(scan); Inc(match); if scan^<>match^ then break;
  1268. Inc(scan); Inc(match); if scan^<>match^ then break;
  1269. Inc(scan); Inc(match); if scan^<>match^ then break;
  1270. Inc(scan); Inc(match); if scan^<>match^ then break;
  1271. until (ptr2int(scan) >= ptr2int(strend));
  1272. Assert(scan <= s.window+unsigned(s.window_size-1), 'wild scan');
  1273. len := MAX_MATCH - int(strend - scan);
  1274. if (len < MIN_MATCH) then
  1275. begin
  1276. return := MIN_MATCH - 1;
  1277. exit;
  1278. end;
  1279. s.match_start := cur_match;
  1280. if len <= s.lookahead then
  1281. longest_match := len
  1282. else
  1283. longest_match := s.lookahead;
  1284. end;
  1285. {$endif} { FASTEST }
  1286. {$ifdef DEBUG}
  1287. { ===========================================================================
  1288. Check that the match at match_start is indeed a match. }
  1289. {local}
  1290. procedure check_match(var s : deflate_state;
  1291. start, match : IPos;
  1292. length : int);
  1293. begin
  1294. exit;
  1295. { check that the match is indeed a match }
  1296. if (zmemcmp(pBytef(@s.window^[match]),
  1297. pBytef(@s.window^[start]), length) <> EQUAL) then
  1298. begin
  1299. WriteLn(' start ',start,', match ',match ,' length ', length);
  1300. repeat
  1301. Write(char(s.window^[match]), char(s.window^[start]));
  1302. Inc(match);
  1303. Inc(start);
  1304. Dec(length);
  1305. Until (length = 0);
  1306. z_error('invalid match');
  1307. end;
  1308. if (z_verbose > 1) then
  1309. begin
  1310. Write('\\[',start-match,',',length,']');
  1311. repeat
  1312. Write(char(s.window^[start]));
  1313. Inc(start);
  1314. Dec(length);
  1315. Until (length = 0);
  1316. end;
  1317. end;
  1318. {$endif}
  1319. { ===========================================================================
  1320. Fill the window when the lookahead becomes insufficient.
  1321. Updates strstart and lookahead.
  1322. IN assertion: lookahead < MIN_LOOKAHEAD
  1323. OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1324. At least one byte has been read, or avail_in = 0; reads are
  1325. performed for at least two bytes (required for the zip translate_eol
  1326. option -- not supported here). }
  1327. {local}
  1328. procedure fill_window(var s : deflate_state);
  1329. var
  1330. {register} n, m : unsigned;
  1331. {register} p : pPosf;
  1332. more : unsigned; { Amount of free space at the end of the window. }
  1333. wsize : uInt;
  1334. begin
  1335. wsize := s.w_size;
  1336. repeat
  1337. more := unsigned(s.window_size -ulg(s.lookahead) -ulg(s.strstart));
  1338. { Deal with !@#$% 64K limit: }
  1339. if (more = 0) and (s.strstart = 0) and (s.lookahead = 0) then
  1340. more := wsize
  1341. else
  1342. if (more = unsigned(-1)) then
  1343. begin
  1344. { Very unlikely, but possible on 16 bit machine if strstart = 0
  1345. and lookahead = 1 (input done one byte at time) }
  1346. Dec(more);
  1347. { If the window is almost full and there is insufficient lookahead,
  1348. move the upper half to the lower one to make room in the upper half.}
  1349. end
  1350. else
  1351. if (s.strstart >= wsize+ {MAX_DIST}wsize-MIN_LOOKAHEAD) then
  1352. begin
  1353. zmemcpy( pBytef(s.window), pBytef(@(s.window^[wsize])),
  1354. unsigned(wsize));
  1355. Dec(s.match_start, wsize);
  1356. Dec(s.strstart, wsize); { we now have strstart >= MAX_DIST }
  1357. Dec(s.block_start, long(wsize));
  1358. { Slide the hash table (could be avoided with 32 bit values
  1359. at the expense of memory usage). We slide even when level = 0
  1360. to keep the hash table consistent if we switch back to level > 0
  1361. later. (Using level 0 permanently is not an optimal usage of
  1362. zlib, so we don't care about this pathological case.) }
  1363. n := s.hash_size;
  1364. p := @s.head^[n];
  1365. repeat
  1366. Dec(p);
  1367. m := p^;
  1368. if (m >= wsize) then
  1369. p^ := Pos(m-wsize)
  1370. else
  1371. p^ := Pos(ZNIL);
  1372. Dec(n);
  1373. Until (n=0);
  1374. n := wsize;
  1375. {$ifndef FASTEST}
  1376. p := @s.prev^[n];
  1377. repeat
  1378. Dec(p);
  1379. m := p^;
  1380. if (m >= wsize) then
  1381. p^ := Pos(m-wsize)
  1382. else
  1383. p^:= Pos(ZNIL);
  1384. { If n is not on any hash chain, prev^[n] is garbage but
  1385. its value will never be used. }
  1386. Dec(n);
  1387. Until (n=0);
  1388. {$endif}
  1389. Inc(more, wsize);
  1390. end;
  1391. if (s.strm^.avail_in = 0) then
  1392. exit;
  1393. {* If there was no sliding:
  1394. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1395. * more == window_size - lookahead - strstart
  1396. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1397. * => more >= window_size - 2*WSIZE + 2
  1398. * In the BIG_MEM or MMAP case (not yet supported),
  1399. * window_size == input_size + MIN_LOOKAHEAD &&
  1400. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1401. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1402. * If there was sliding, more >= WSIZE. So in all cases, more >= 2. }
  1403. {$IFDEF DEBUG}
  1404. Assert(more >= 2, 'more < 2');
  1405. {$ENDIF}
  1406. n := read_buf(s.strm, pBytef(@(s.window^[s.strstart + s.lookahead])),
  1407. more);
  1408. Inc(s.lookahead, n);
  1409. { Initialize the hash value now that we have some input: }
  1410. if (s.lookahead >= MIN_MATCH) then
  1411. begin
  1412. s.ins_h := s.window^[s.strstart];
  1413. {UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]);}
  1414. s.ins_h := ((s.ins_h shl s.hash_shift) xor s.window^[s.strstart+1])
  1415. and s.hash_mask;
  1416. {$ifdef MIN_MATCH <> 3}
  1417. Call UPDATE_HASH() MIN_MATCH-3 more times
  1418. {$endif}
  1419. end;
  1420. { If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1421. but this is not important since only literal bytes will be emitted. }
  1422. until (s.lookahead >= MIN_LOOKAHEAD) or (s.strm^.avail_in = 0);
  1423. end;
  1424. { ===========================================================================
  1425. Flush the current block, with given end-of-file flag.
  1426. IN assertion: strstart is set to the end of the current match. }
  1427. procedure FLUSH_BLOCK_ONLY(var s : deflate_state; eof : boolean); {macro}
  1428. begin
  1429. if (s.block_start >= Long(0)) then
  1430. _tr_flush_block(s, pcharf(@s.window^[unsigned(s.block_start)]),
  1431. ulg(long(s.strstart) - s.block_start), eof)
  1432. else
  1433. _tr_flush_block(s, pcharf(Z_NULL),
  1434. ulg(long(s.strstart) - s.block_start), eof);
  1435. s.block_start := s.strstart;
  1436. flush_pending(s.strm^);
  1437. {$IFDEF DEBUG}
  1438. Tracev('[FLUSH]');
  1439. {$ENDIF}
  1440. end;
  1441. { Same but force premature exit if necessary.
  1442. macro FLUSH_BLOCK(var s : deflate_state; eof : boolean) : boolean;
  1443. var
  1444. result : block_state;
  1445. begin
  1446. FLUSH_BLOCK_ONLY(s, eof);
  1447. if (s.strm^.avail_out = 0) then
  1448. begin
  1449. if eof then
  1450. result := finish_started
  1451. else
  1452. result := need_more;
  1453. exit;
  1454. end;
  1455. end;
  1456. }
  1457. { ===========================================================================
  1458. Copy without compression as much as possible from the input stream, return
  1459. the current block state.
  1460. This function does not insert new strings in the dictionary since
  1461. uncompressible data is probably not useful. This function is used
  1462. only for the level=0 compression option.
  1463. NOTE: this function should be optimized to avoid extra copying from
  1464. window to pending_buf. }
  1465. {local}
  1466. function deflate_stored(var s : deflate_state; flush : int) : block_state;
  1467. { Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1468. to pending_buf_size, and each stored block has a 5 byte header: }
  1469. var
  1470. max_block_size : ulg;
  1471. max_start : ulg;
  1472. begin
  1473. max_block_size := $ffff;
  1474. if (max_block_size > s.pending_buf_size - 5) then
  1475. max_block_size := s.pending_buf_size - 5;
  1476. { Copy as much as possible from input to output: }
  1477. while TRUE do
  1478. begin
  1479. { Fill the window as much as possible: }
  1480. if (s.lookahead <= 1) then
  1481. begin
  1482. {$IFDEF DEBUG}
  1483. Assert( (s.strstart < s.w_size + {MAX_DIST}s.w_size-MIN_LOOKAHEAD) or
  1484. (s.block_start >= long(s.w_size)), 'slide too late');
  1485. {$ENDIF}
  1486. fill_window(s);
  1487. if (s.lookahead = 0) and (flush = Z_NO_FLUSH) then
  1488. begin
  1489. deflate_stored := need_more;
  1490. exit;
  1491. end;
  1492. if (s.lookahead = 0) then
  1493. break; { flush the current block }
  1494. end;
  1495. {$IFDEF DEBUG}
  1496. Assert(s.block_start >= long(0), 'block gone');
  1497. {$ENDIF}
  1498. Inc(s.strstart, s.lookahead);
  1499. s.lookahead := 0;
  1500. { Emit a stored block if pending_buf will be full: }
  1501. max_start := s.block_start + max_block_size;
  1502. if (s.strstart = 0) or (ulg(s.strstart) >= max_start) then
  1503. begin
  1504. { strstart = 0 is possible when wraparound on 16-bit machine }
  1505. s.lookahead := uInt(s.strstart - max_start);
  1506. s.strstart := uInt(max_start);
  1507. {FLUSH_BLOCK(s, FALSE);}
  1508. FLUSH_BLOCK_ONLY(s, FALSE);
  1509. if (s.strm^.avail_out = 0) then
  1510. begin
  1511. deflate_stored := need_more;
  1512. exit;
  1513. end;
  1514. end;
  1515. { Flush if we may have to slide, otherwise block_start may become
  1516. negative and the data will be gone: }
  1517. if (s.strstart - uInt(s.block_start) >= {MAX_DIST}
  1518. s.w_size-MIN_LOOKAHEAD) then
  1519. begin
  1520. {FLUSH_BLOCK(s, FALSE);}
  1521. FLUSH_BLOCK_ONLY(s, FALSE);
  1522. if (s.strm^.avail_out = 0) then
  1523. begin
  1524. deflate_stored := need_more;
  1525. exit;
  1526. end;
  1527. end;
  1528. end;
  1529. {FLUSH_BLOCK(s, flush = Z_FINISH);}
  1530. FLUSH_BLOCK_ONLY(s, flush = Z_FINISH);
  1531. if (s.strm^.avail_out = 0) then
  1532. begin
  1533. if flush = Z_FINISH then
  1534. deflate_stored := finish_started
  1535. else
  1536. deflate_stored := need_more;
  1537. exit;
  1538. end;
  1539. if flush = Z_FINISH then
  1540. deflate_stored := finish_done
  1541. else
  1542. deflate_stored := block_done;
  1543. end;
  1544. { ===========================================================================
  1545. Compress as much as possible from the input stream, return the current
  1546. block state.
  1547. This function does not perform lazy evaluation of matches and inserts
  1548. new strings in the dictionary only for unmatched strings or for short
  1549. matches. It is used only for the fast compression options. }
  1550. {local}
  1551. function deflate_fast(var s : deflate_state; flush : int) : block_state;
  1552. var
  1553. hash_head : IPos; { head of the hash chain }
  1554. bflush : boolean; { set if current block must be flushed }
  1555. begin
  1556. hash_head := ZNIL;
  1557. while TRUE do
  1558. begin
  1559. { Make sure that we always have enough lookahead, except
  1560. at the end of the input file. We need MAX_MATCH bytes
  1561. for the next match, plus MIN_MATCH bytes to insert the
  1562. string following the next match. }
  1563. if (s.lookahead < MIN_LOOKAHEAD) then
  1564. begin
  1565. fill_window(s);
  1566. if (s.lookahead < MIN_LOOKAHEAD) and (flush = Z_NO_FLUSH) then
  1567. begin
  1568. deflate_fast := need_more;
  1569. exit;
  1570. end;
  1571. if (s.lookahead = 0) then
  1572. break; { flush the current block }
  1573. end;
  1574. { Insert the string window[strstart .. strstart+2] in the
  1575. dictionary, and set hash_head to the head of the hash chain: }
  1576. if (s.lookahead >= MIN_MATCH) then
  1577. begin
  1578. INSERT_STRING(s, s.strstart, hash_head);
  1579. end;
  1580. { Find the longest match, discarding those <= prev_length.
  1581. At this point we have always match_length < MIN_MATCH }
  1582. if (hash_head <> ZNIL) and
  1583. (s.strstart - hash_head <= (s.w_size-MIN_LOOKAHEAD){MAX_DIST}) then
  1584. begin
  1585. { To simplify the code, we prevent matches with the string
  1586. of window index 0 (in particular we have to avoid a match
  1587. of the string with itself at the start of the input file). }
  1588. if (s.strategy <> Z_HUFFMAN_ONLY) then
  1589. begin
  1590. s.match_length := longest_match (s, hash_head);
  1591. end;
  1592. { longest_match() sets match_start }
  1593. end;
  1594. if (s.match_length >= MIN_MATCH) then
  1595. begin
  1596. {$IFDEF DEBUG}
  1597. check_match(s, s.strstart, s.match_start, s.match_length);
  1598. {$ENDIF}
  1599. {_tr_tally_dist(s, s.strstart - s.match_start,
  1600. s.match_length - MIN_MATCH, bflush);}
  1601. bflush := _tr_tally(s, s.strstart - s.match_start,
  1602. s.match_length - MIN_MATCH);
  1603. Dec(s.lookahead, s.match_length);
  1604. { Insert new strings in the hash table only if the match length
  1605. is not too large. This saves time but degrades compression. }
  1606. {$ifndef FASTEST}
  1607. if (s.match_length <= s.max_insert_length)
  1608. and (s.lookahead >= MIN_MATCH) then
  1609. begin
  1610. Dec(s.match_length); { string at strstart already in hash table }
  1611. repeat
  1612. Inc(s.strstart);
  1613. INSERT_STRING(s, s.strstart, hash_head);
  1614. { strstart never exceeds WSIZE-MAX_MATCH, so there are
  1615. always MIN_MATCH bytes ahead. }
  1616. Dec(s.match_length);
  1617. until (s.match_length = 0);
  1618. Inc(s.strstart);
  1619. end
  1620. else
  1621. {$endif}
  1622. begin
  1623. Inc(s.strstart, s.match_length);
  1624. s.match_length := 0;
  1625. s.ins_h := s.window^[s.strstart];
  1626. {UPDATE_HASH(s, s.ins_h, s.window[s.strstart+1]);}
  1627. s.ins_h := (( s.ins_h shl s.hash_shift) xor
  1628. s.window^[s.strstart+1]) and s.hash_mask;
  1629. if MIN_MATCH <> 3 then { the linker removes this }
  1630. begin
  1631. {Call UPDATE_HASH() MIN_MATCH-3 more times}
  1632. end;
  1633. { If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1634. matter since it will be recomputed at next deflate call. }
  1635. end;
  1636. end
  1637. else
  1638. begin
  1639. { No match, output a literal byte }
  1640. {$IFDEF DEBUG}
  1641. Tracevv(char(s.window^[s.strstart]));
  1642. {$ENDIF}
  1643. {_tr_tally_lit (s, 0, s.window^[s.strstart], bflush);}
  1644. bflush := _tr_tally (s, 0, s.window^[s.strstart]);
  1645. Dec(s.lookahead);
  1646. Inc(s.strstart);
  1647. end;
  1648. if bflush then
  1649. begin {FLUSH_BLOCK(s, FALSE);}
  1650. FLUSH_BLOCK_ONLY(s, FALSE);
  1651. if (s.strm^.avail_out = 0) then
  1652. begin
  1653. deflate_fast := need_more;
  1654. exit;
  1655. end;
  1656. end;
  1657. end;
  1658. {FLUSH_BLOCK(s, flush = Z_FINISH);}
  1659. FLUSH_BLOCK_ONLY(s, flush = Z_FINISH);
  1660. if (s.strm^.avail_out = 0) then
  1661. begin
  1662. if flush = Z_FINISH then
  1663. deflate_fast := finish_started
  1664. else
  1665. deflate_fast := need_more;
  1666. exit;
  1667. end;
  1668. if flush = Z_FINISH then
  1669. deflate_fast := finish_done
  1670. else
  1671. deflate_fast := block_done;
  1672. end;
  1673. { ===========================================================================
  1674. Same as above, but achieves better compression. We use a lazy
  1675. evaluation for matches: a match is finally adopted only if there is
  1676. no better match at the next window position. }
  1677. {local}
  1678. function deflate_slow(var s : deflate_state; flush : int) : block_state;
  1679. var
  1680. hash_head : IPos; { head of hash chain }
  1681. bflush : boolean; { set if current block must be flushed }
  1682. var
  1683. max_insert : uInt;
  1684. begin
  1685. hash_head := ZNIL;
  1686. { Process the input block. }
  1687. while TRUE do
  1688. begin
  1689. { Make sure that we always have enough lookahead, except
  1690. at the end of the input file. We need MAX_MATCH bytes
  1691. for the next match, plus MIN_MATCH bytes to insert the
  1692. string following the next match. }
  1693. if (s.lookahead < MIN_LOOKAHEAD) then
  1694. begin
  1695. fill_window(s);
  1696. if (s.lookahead < MIN_LOOKAHEAD) and (flush = Z_NO_FLUSH) then
  1697. begin
  1698. deflate_slow := need_more;
  1699. exit;
  1700. end;
  1701. if (s.lookahead = 0) then
  1702. break; { flush the current block }
  1703. end;
  1704. { Insert the string window[strstart .. strstart+2] in the
  1705. dictionary, and set hash_head to the head of the hash chain: }
  1706. if (s.lookahead >= MIN_MATCH) then
  1707. begin
  1708. INSERT_STRING(s, s.strstart, hash_head);
  1709. end;
  1710. { Find the longest match, discarding those <= prev_length. }
  1711. s.prev_length := s.match_length;
  1712. s.prev_match := s.match_start;
  1713. s.match_length := MIN_MATCH-1;
  1714. if (hash_head <> ZNIL) and (s.prev_length < s.max_lazy_match) and
  1715. (s.strstart - hash_head <= {MAX_DIST}(s.w_size-MIN_LOOKAHEAD)) then
  1716. begin
  1717. { To simplify the code, we prevent matches with the string
  1718. of window index 0 (in particular we have to avoid a match
  1719. of the string with itself at the start of the input file). }
  1720. if (s.strategy <> Z_HUFFMAN_ONLY) then
  1721. begin
  1722. s.match_length := longest_match (s, hash_head);
  1723. end;
  1724. { longest_match() sets match_start }
  1725. if (s.match_length <= 5) and ((s.strategy = Z_FILTERED) or
  1726. ((s.match_length = MIN_MATCH) and
  1727. (s.strstart - s.match_start > TOO_FAR))) then
  1728. begin
  1729. { If prev_match is also MIN_MATCH, match_start is garbage
  1730. but we will ignore the current match anyway. }
  1731. s.match_length := MIN_MATCH-1;
  1732. end;
  1733. end;
  1734. { If there was a match at the previous step and the current
  1735. match is not better, output the previous match: }
  1736. if (s.prev_length >= MIN_MATCH)
  1737. and (s.match_length <= s.prev_length) then
  1738. begin
  1739. max_insert := s.strstart + s.lookahead - MIN_MATCH;
  1740. { Do not insert strings in hash table beyond this. }
  1741. {$ifdef DEBUG}
  1742. check_match(s, s.strstart-1, s.prev_match, s.prev_length);
  1743. {$endif}
  1744. {_tr_tally_dist(s, s->strstart -1 - s->prev_match,
  1745. s->prev_length - MIN_MATCH, bflush);}
  1746. bflush := _tr_tally(s, s.strstart -1 - s.prev_match,
  1747. s.prev_length - MIN_MATCH);
  1748. { Insert in hash table all strings up to the end of the match.
  1749. strstart-1 and strstart are already inserted. If there is not
  1750. enough lookahead, the last two strings are not inserted in
  1751. the hash table. }
  1752. Dec(s.lookahead, s.prev_length-1);
  1753. Dec(s.prev_length, 2);
  1754. repeat
  1755. Inc(s.strstart);
  1756. if (s.strstart <= max_insert) then
  1757. begin
  1758. INSERT_STRING(s, s.strstart, hash_head);
  1759. end;
  1760. Dec(s.prev_length);
  1761. until (s.prev_length = 0);
  1762. s.match_available := FALSE;
  1763. s.match_length := MIN_MATCH-1;
  1764. Inc(s.strstart);
  1765. if (bflush) then {FLUSH_BLOCK(s, FALSE);}
  1766. begin
  1767. FLUSH_BLOCK_ONLY(s, FALSE);
  1768. if (s.strm^.avail_out = 0) then
  1769. begin
  1770. deflate_slow := need_more;
  1771. exit;
  1772. end;
  1773. end;
  1774. end
  1775. else
  1776. if (s.match_available) then
  1777. begin
  1778. { If there was no match at the previous position, output a
  1779. single literal. If there was a match but the current match
  1780. is longer, truncate the previous match to a single literal. }
  1781. {$IFDEF DEBUG}
  1782. Tracevv(char(s.window^[s.strstart-1]));
  1783. {$ENDIF}
  1784. bflush := _tr_tally (s, 0, s.window^[s.strstart-1]);
  1785. if bflush then
  1786. begin
  1787. FLUSH_BLOCK_ONLY(s, FALSE);
  1788. end;
  1789. Inc(s.strstart);
  1790. Dec(s.lookahead);
  1791. if (s.strm^.avail_out = 0) then
  1792. begin
  1793. deflate_slow := need_more;
  1794. exit;
  1795. end;
  1796. end
  1797. else
  1798. begin
  1799. { There is no previous match to compare with, wait for
  1800. the next step to decide. }
  1801. s.match_available := TRUE;
  1802. Inc(s.strstart);
  1803. Dec(s.lookahead);
  1804. end;
  1805. end;
  1806. {$IFDEF DEBUG}
  1807. Assert (flush <> Z_NO_FLUSH, 'no flush?');
  1808. {$ENDIF}
  1809. if (s.match_available) then
  1810. begin
  1811. {$IFDEF DEBUG}
  1812. Tracevv(char(s.window^[s.strstart-1]));
  1813. bflush :=
  1814. {$ENDIF}
  1815. _tr_tally (s, 0, s.window^[s.strstart-1]);
  1816. s.match_available := FALSE;
  1817. end;
  1818. {FLUSH_BLOCK(s, flush = Z_FINISH);}
  1819. FLUSH_BLOCK_ONLY(s, flush = Z_FINISH);
  1820. if (s.strm^.avail_out = 0) then
  1821. begin
  1822. if flush = Z_FINISH then
  1823. deflate_slow := finish_started
  1824. else
  1825. deflate_slow := need_more;
  1826. exit;
  1827. end;
  1828. if flush = Z_FINISH then
  1829. deflate_slow := finish_done
  1830. else
  1831. deflate_slow := block_done;
  1832. end;
  1833. end.