trees.pas 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. unit trees;
  2. {$T-}
  3. {$define ORG_DEBUG}
  4. {
  5. trees.c -- output deflated data using Huffman coding
  6. Copyright (C) 1995-1998 Jean-loup Gailly
  7. Pascal tranlastion
  8. Copyright (C) 1998 by Jacques Nomssi Nzali
  9. For conditions of distribution and use, see copyright notice in readme.txt
  10. }
  11. {
  12. * ALGORITHM
  13. *
  14. * The "deflation" process uses several Huffman trees. The more
  15. * common source values are represented by shorter bit sequences.
  16. *
  17. * Each code tree is stored in a compressed form which is itself
  18. * a Huffman encoding of the lengths of all the code strings (in
  19. * ascending order by source values). The actual code strings are
  20. * reconstructed from the lengths in the inflate process, as described
  21. * in the deflate specification.
  22. *
  23. * REFERENCES
  24. *
  25. * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
  26. * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
  27. *
  28. * Storer, James A.
  29. * Data Compression: Methods and Theory, pp. 49-50.
  30. * Computer Science Press, 1988. ISBN 0-7167-8156-5.
  31. *
  32. * Sedgewick, R.
  33. * Algorithms, p290.
  34. * Addison-Wesley, 1983. ISBN 0-201-06672-6.
  35. }
  36. interface
  37. {$I zconf.inc}
  38. uses
  39. {$ifdef ZLIB_DEBUG}
  40. sysutils,
  41. {$endif}
  42. zbase
  43. ;
  44. { ===========================================================================
  45. Internal compression state. }
  46. const
  47. LENGTH_CODES = 29;
  48. { number of length codes, not counting the special END_BLOCK code }
  49. LITERALS = 256;
  50. { number of literal bytes 0..255 }
  51. L_CODES = (LITERALS+1+LENGTH_CODES);
  52. { number of Literal or Length codes, including the END_BLOCK code }
  53. D_CODES = 30;
  54. { number of distance codes }
  55. BL_CODES = 19;
  56. { number of codes used to transfer the bit lengths }
  57. HEAP_SIZE = (2*L_CODES+1);
  58. { maximum heap size }
  59. MAX_BITS = 15;
  60. { All codes must not exceed MAX_BITS bits }
  61. const
  62. INIT_STATE = 42;
  63. BUSY_STATE = 113;
  64. FINISH_STATE = 666;
  65. { Stream status }
  66. { Data structure describing a single value and its code string. }
  67. type
  68. ct_data_ptr = ^ct_data;
  69. ct_data = record
  70. fc : record
  71. case byte of
  72. 0:(freq : word); { frequency count }
  73. 1:(code : word); { bit string }
  74. end;
  75. dl : record
  76. case byte of
  77. 0:(dad : word); { father node in Huffman tree }
  78. 1:(len : word); { length of bit string }
  79. end;
  80. end;
  81. { Freq = fc.freq
  82. Code = fc.code
  83. Dad = dl.dad
  84. Len = dl.len }
  85. type
  86. ltree_type = array[0..HEAP_SIZE-1] of ct_data; { literal and length tree }
  87. dtree_type = array[0..2*D_CODES+1-1] of ct_data; { distance tree }
  88. htree_type = array[0..2*BL_CODES+1-1] of ct_data; { Huffman tree for bit lengths }
  89. { generic tree type }
  90. tree_type = array[0..(maxint div SizeOf(ct_data))-1] of ct_data;
  91. tree_ptr = ^ct_data;
  92. ltree_ptr = ^ltree_type;
  93. dtree_ptr = ^dtree_type;
  94. htree_ptr = ^htree_type;
  95. type
  96. static_tree_desc_ptr = ^static_tree_desc;
  97. static_tree_desc =
  98. record
  99. {const} static_tree : tree_ptr; { static tree or NIL }
  100. {const} extra_bits : pinteger; { extra bits for each code or NIL }
  101. extra_base : integer; { base index for extra_bits }
  102. elems : integer; { max number of elements in the tree }
  103. max_length : integer; { max bit length for the codes }
  104. end;
  105. tree_desc_ptr = ^tree_desc;
  106. tree_desc = record
  107. dyn_tree : tree_ptr; { the dynamic tree }
  108. max_code : integer; { largest code with non zero frequency }
  109. stat_desc : static_tree_desc_ptr; { the corresponding static tree }
  110. end;
  111. type
  112. Pos = word;
  113. Posf = Pos; {FAR}
  114. IPos = cardinal;
  115. pPosf = ^Posf;
  116. zPosfArray = array[0..(maxint div SizeOf(Posf))-1] of Posf;
  117. pzPosfArray = ^zPosfArray;
  118. { A Pos is an index in the character window. We use short instead of integer to
  119. save space in the various tables. IPos is used only for parameter passing.}
  120. type
  121. deflate_state_ptr = ^deflate_state;
  122. deflate_state = record
  123. strm : z_streamp; { pointer back to this zlib stream }
  124. status : integer; { as the name implies }
  125. pending_buf : Pbytearray; { output still pending }
  126. pending_buf_size : longint; { size of pending_buf }
  127. pending_out : Pbyte; { next pending byte to output to the stream }
  128. pending : integer; { nb of bytes in the pending buffer }
  129. noheader : integer; { suppress zlib header and adler32 }
  130. data_type : Byte; { UNKNOWN, BINARY or ASCII }
  131. method : Byte; { STORED (for zip only) or DEFLATED }
  132. last_flush : integer; { value of flush param for previous deflate call }
  133. { used by deflate.pas: }
  134. w_size : cardinal; { LZ77 window size (32K by default) }
  135. w_bits : cardinal; { log2(w_size) (8..16) }
  136. w_mask : cardinal; { w_size - 1 }
  137. window : Pbytearray;
  138. { Sliding window. Input bytes are read into the second half of the window,
  139. and move to the first half later to keep a dictionary of at least wSize
  140. bytes. With this organization, matches are limited to a distance of
  141. wSize-MAX_MATCH bytes, but this ensures that IO is always
  142. performed with a length multiple of the block size. Also, it limits
  143. the window size to 64K, which is quite useful on MSDOS.
  144. To do: use the user input buffer as sliding window. }
  145. window_size : longint;
  146. { Actual size of window: 2*wSize, except when the user input buffer
  147. is directly used as sliding window. }
  148. prev : pzPosfArray;
  149. { Link to older string with same hash index. To limit the size of this
  150. array to 64K, this link is maintained only for the last 32K strings.
  151. An index in this array is thus a window index modulo 32K. }
  152. head : pzPosfArray; { Heads of the hash chains or NIL. }
  153. ins_h : cardinal; { hash index of string to be inserted }
  154. hash_size : cardinal; { number of elements in hash table }
  155. hash_bits : cardinal; { log2(hash_size) }
  156. hash_mask : cardinal; { hash_size-1 }
  157. hash_shift : cardinal;
  158. { Number of bits by which ins_h must be shifted at each input
  159. step. It must be such that after MIN_MATCH steps, the oldest
  160. byte no longer takes part in the hash key, that is:
  161. hash_shift * MIN_MATCH >= hash_bits }
  162. block_start : longint;
  163. { Window position at the beginning of the current output block. Gets
  164. negative when the window is moved backwards. }
  165. match_length : cardinal; { length of best match }
  166. prev_match : IPos; { previous match }
  167. match_available : boolean; { set if previous match exists }
  168. strstart : cardinal; { start of string to insert }
  169. match_start : cardinal; { start of matching string }
  170. lookahead : cardinal; { number of valid bytes ahead in window }
  171. prev_length : cardinal;
  172. { Length of the best match at previous step. Matches not greater than this
  173. are discarded. This is used in the lazy match evaluation. }
  174. max_chain_length : cardinal;
  175. { To speed up deflation, hash chains are never searched beyond this
  176. length. A higher limit improves compression ratio but degrades the
  177. speed. }
  178. { moved to the end because Borland Pascal won't accept the following:
  179. max_lazy_match : cardinal;
  180. max_insert_length : cardinal absolute max_lazy_match;
  181. }
  182. level : integer; { compression level (1..9) }
  183. strategy : integer; { favor or force Huffman coding}
  184. good_match : cardinal;
  185. { Use a faster search when the previous match is longer than this }
  186. nice_match : integer; { Stop searching when current match exceeds this }
  187. { used by trees.pas: }
  188. { Didn't use ct_data typedef below to supress compiler warning }
  189. dyn_ltree : ltree_type; { literal and length tree }
  190. dyn_dtree : dtree_type; { distance tree }
  191. bl_tree : htree_type; { Huffman tree for bit lengths }
  192. l_desc : tree_desc; { desc. for literal tree }
  193. d_desc : tree_desc; { desc. for distance tree }
  194. bl_desc : tree_desc; { desc. for bit length tree }
  195. bl_count : array[0..MAX_BITS+1-1] of word;
  196. { number of codes at each bit length for an optimal tree }
  197. heap : array[0..2*L_CODES+1-1] of integer; { heap used to build the Huffman trees }
  198. heap_len : integer; { number of elements in the heap }
  199. heap_max : integer; { element of largest frequency }
  200. { The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
  201. The same heap array is used to build all trees. }
  202. depth : array[0..2*L_CODES+1-1] of byte;
  203. { Depth of each subtree used as tie breaker for trees of equal frequency }
  204. l_buf : Pbytearray; { buffer for literals or lengths }
  205. lit_bufsize : cardinal;
  206. { Size of match buffer for literals/lengths. There are 4 reasons for
  207. limiting lit_bufsize to 64K:
  208. - frequencies can be kept in 16 bit counters
  209. - if compression is not successful for the first block, all input
  210. data is still in the window so we can still emit a stored block even
  211. when input comes from standard input. (This can also be done for
  212. all blocks if lit_bufsize is not greater than 32K.)
  213. - if compression is not successful for a file smaller than 64K, we can
  214. even emit a stored file instead of a stored block (saving 5 bytes).
  215. This is applicable only for zip (not gzip or zlib).
  216. - creating new Huffman trees less frequently may not provide fast
  217. adaptation to changes in the input data statistics. (Take for
  218. example a binary file with poorly compressible code followed by
  219. a highly compressible string table.) Smaller buffer sizes give
  220. fast adaptation but have of course the overhead of transmitting
  221. trees more frequently.
  222. - I can't count above 4 }
  223. last_lit : cardinal; { running index in l_buf }
  224. d_buf : Pwordarray;
  225. { Buffer for distances. To simplify the code, d_buf and l_buf have
  226. the same number of elements. To use different lengths, an extra flag
  227. array would be necessary. }
  228. opt_len : longint; { bit length of current block with optimal trees }
  229. static_len : longint; { bit length of current block with static trees }
  230. compressed_len : longint; { total bit length of compressed file }
  231. matches : cardinal; { number of string matches in current block }
  232. last_eob_len : integer; { bit length of EOB code for last block }
  233. {$ifdef ZLIB_DEBUG}
  234. bits_sent : longint; { bit length of the compressed data }
  235. {$endif}
  236. bi_buf : word;
  237. { Output buffer. bits are inserted starting at the bottom (least
  238. significant bits). }
  239. bi_valid : integer;
  240. { Number of valid bits in bi_buf. All bits above the last valid bit
  241. are always zero. }
  242. case byte of
  243. 0:(max_lazy_match : cardinal);
  244. { Attempt to find a better match only when the current match is strictly
  245. smaller than this value. This mechanism is used only for compression
  246. levels >= 4. }
  247. 1:(max_insert_length : cardinal);
  248. { Insert new strings in the hash table only if the match length is not
  249. greater than this length. This saves time but degrades compression.
  250. max_insert_length is used only for compression levels <= 3. }
  251. end;
  252. procedure _tr_init (var s : deflate_state);
  253. function _tr_tally (var s : deflate_state;
  254. dist : cardinal;
  255. lc : cardinal) : boolean;
  256. function _tr_flush_block (var s : deflate_state;
  257. buf : Pbyte;
  258. stored_len : longint;
  259. eof : boolean) : longint;
  260. procedure _tr_align(var s : deflate_state);
  261. procedure _tr_stored_block(var s : deflate_state;
  262. buf : Pbyte;
  263. stored_len : longint;
  264. eof : boolean);
  265. implementation
  266. { #define GEN_TREES_H }
  267. {$ifndef GEN_TREES_H}
  268. { header created automatically with -DGEN_TREES_H }
  269. const
  270. DIST_CODE_LEN = 512; { see definition of array dist_code below }
  271. { The static literal tree. Since the bit lengths are imposed, there is no
  272. need for the L_CODES extra codes used during heap construction. However
  273. The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  274. below). }
  275. const
  276. static_ltree : array[0..L_CODES+2-1] of ct_data = (
  277. { fc:(freq, code) dl:(dad,len) }
  278. (fc:(freq: 12);dl:(len: 8)), (fc:(freq:140);dl:(len: 8)), (fc:(freq: 76);dl:(len: 8)),
  279. (fc:(freq:204);dl:(len: 8)), (fc:(freq: 44);dl:(len: 8)), (fc:(freq:172);dl:(len: 8)),
  280. (fc:(freq:108);dl:(len: 8)), (fc:(freq:236);dl:(len: 8)), (fc:(freq: 28);dl:(len: 8)),
  281. (fc:(freq:156);dl:(len: 8)), (fc:(freq: 92);dl:(len: 8)), (fc:(freq:220);dl:(len: 8)),
  282. (fc:(freq: 60);dl:(len: 8)), (fc:(freq:188);dl:(len: 8)), (fc:(freq:124);dl:(len: 8)),
  283. (fc:(freq:252);dl:(len: 8)), (fc:(freq: 2);dl:(len: 8)), (fc:(freq:130);dl:(len: 8)),
  284. (fc:(freq: 66);dl:(len: 8)), (fc:(freq:194);dl:(len: 8)), (fc:(freq: 34);dl:(len: 8)),
  285. (fc:(freq:162);dl:(len: 8)), (fc:(freq: 98);dl:(len: 8)), (fc:(freq:226);dl:(len: 8)),
  286. (fc:(freq: 18);dl:(len: 8)), (fc:(freq:146);dl:(len: 8)), (fc:(freq: 82);dl:(len: 8)),
  287. (fc:(freq:210);dl:(len: 8)), (fc:(freq: 50);dl:(len: 8)), (fc:(freq:178);dl:(len: 8)),
  288. (fc:(freq:114);dl:(len: 8)), (fc:(freq:242);dl:(len: 8)), (fc:(freq: 10);dl:(len: 8)),
  289. (fc:(freq:138);dl:(len: 8)), (fc:(freq: 74);dl:(len: 8)), (fc:(freq:202);dl:(len: 8)),
  290. (fc:(freq: 42);dl:(len: 8)), (fc:(freq:170);dl:(len: 8)), (fc:(freq:106);dl:(len: 8)),
  291. (fc:(freq:234);dl:(len: 8)), (fc:(freq: 26);dl:(len: 8)), (fc:(freq:154);dl:(len: 8)),
  292. (fc:(freq: 90);dl:(len: 8)), (fc:(freq:218);dl:(len: 8)), (fc:(freq: 58);dl:(len: 8)),
  293. (fc:(freq:186);dl:(len: 8)), (fc:(freq:122);dl:(len: 8)), (fc:(freq:250);dl:(len: 8)),
  294. (fc:(freq: 6);dl:(len: 8)), (fc:(freq:134);dl:(len: 8)), (fc:(freq: 70);dl:(len: 8)),
  295. (fc:(freq:198);dl:(len: 8)), (fc:(freq: 38);dl:(len: 8)), (fc:(freq:166);dl:(len: 8)),
  296. (fc:(freq:102);dl:(len: 8)), (fc:(freq:230);dl:(len: 8)), (fc:(freq: 22);dl:(len: 8)),
  297. (fc:(freq:150);dl:(len: 8)), (fc:(freq: 86);dl:(len: 8)), (fc:(freq:214);dl:(len: 8)),
  298. (fc:(freq: 54);dl:(len: 8)), (fc:(freq:182);dl:(len: 8)), (fc:(freq:118);dl:(len: 8)),
  299. (fc:(freq:246);dl:(len: 8)), (fc:(freq: 14);dl:(len: 8)), (fc:(freq:142);dl:(len: 8)),
  300. (fc:(freq: 78);dl:(len: 8)), (fc:(freq:206);dl:(len: 8)), (fc:(freq: 46);dl:(len: 8)),
  301. (fc:(freq:174);dl:(len: 8)), (fc:(freq:110);dl:(len: 8)), (fc:(freq:238);dl:(len: 8)),
  302. (fc:(freq: 30);dl:(len: 8)), (fc:(freq:158);dl:(len: 8)), (fc:(freq: 94);dl:(len: 8)),
  303. (fc:(freq:222);dl:(len: 8)), (fc:(freq: 62);dl:(len: 8)), (fc:(freq:190);dl:(len: 8)),
  304. (fc:(freq:126);dl:(len: 8)), (fc:(freq:254);dl:(len: 8)), (fc:(freq: 1);dl:(len: 8)),
  305. (fc:(freq:129);dl:(len: 8)), (fc:(freq: 65);dl:(len: 8)), (fc:(freq:193);dl:(len: 8)),
  306. (fc:(freq: 33);dl:(len: 8)), (fc:(freq:161);dl:(len: 8)), (fc:(freq: 97);dl:(len: 8)),
  307. (fc:(freq:225);dl:(len: 8)), (fc:(freq: 17);dl:(len: 8)), (fc:(freq:145);dl:(len: 8)),
  308. (fc:(freq: 81);dl:(len: 8)), (fc:(freq:209);dl:(len: 8)), (fc:(freq: 49);dl:(len: 8)),
  309. (fc:(freq:177);dl:(len: 8)), (fc:(freq:113);dl:(len: 8)), (fc:(freq:241);dl:(len: 8)),
  310. (fc:(freq: 9);dl:(len: 8)), (fc:(freq:137);dl:(len: 8)), (fc:(freq: 73);dl:(len: 8)),
  311. (fc:(freq:201);dl:(len: 8)), (fc:(freq: 41);dl:(len: 8)), (fc:(freq:169);dl:(len: 8)),
  312. (fc:(freq:105);dl:(len: 8)), (fc:(freq:233);dl:(len: 8)), (fc:(freq: 25);dl:(len: 8)),
  313. (fc:(freq:153);dl:(len: 8)), (fc:(freq: 89);dl:(len: 8)), (fc:(freq:217);dl:(len: 8)),
  314. (fc:(freq: 57);dl:(len: 8)), (fc:(freq:185);dl:(len: 8)), (fc:(freq:121);dl:(len: 8)),
  315. (fc:(freq:249);dl:(len: 8)), (fc:(freq: 5);dl:(len: 8)), (fc:(freq:133);dl:(len: 8)),
  316. (fc:(freq: 69);dl:(len: 8)), (fc:(freq:197);dl:(len: 8)), (fc:(freq: 37);dl:(len: 8)),
  317. (fc:(freq:165);dl:(len: 8)), (fc:(freq:101);dl:(len: 8)), (fc:(freq:229);dl:(len: 8)),
  318. (fc:(freq: 21);dl:(len: 8)), (fc:(freq:149);dl:(len: 8)), (fc:(freq: 85);dl:(len: 8)),
  319. (fc:(freq:213);dl:(len: 8)), (fc:(freq: 53);dl:(len: 8)), (fc:(freq:181);dl:(len: 8)),
  320. (fc:(freq:117);dl:(len: 8)), (fc:(freq:245);dl:(len: 8)), (fc:(freq: 13);dl:(len: 8)),
  321. (fc:(freq:141);dl:(len: 8)), (fc:(freq: 77);dl:(len: 8)), (fc:(freq:205);dl:(len: 8)),
  322. (fc:(freq: 45);dl:(len: 8)), (fc:(freq:173);dl:(len: 8)), (fc:(freq:109);dl:(len: 8)),
  323. (fc:(freq:237);dl:(len: 8)), (fc:(freq: 29);dl:(len: 8)), (fc:(freq:157);dl:(len: 8)),
  324. (fc:(freq: 93);dl:(len: 8)), (fc:(freq:221);dl:(len: 8)), (fc:(freq: 61);dl:(len: 8)),
  325. (fc:(freq:189);dl:(len: 8)), (fc:(freq:125);dl:(len: 8)), (fc:(freq:253);dl:(len: 8)),
  326. (fc:(freq: 19);dl:(len: 9)), (fc:(freq:275);dl:(len: 9)), (fc:(freq:147);dl:(len: 9)),
  327. (fc:(freq:403);dl:(len: 9)), (fc:(freq: 83);dl:(len: 9)), (fc:(freq:339);dl:(len: 9)),
  328. (fc:(freq:211);dl:(len: 9)), (fc:(freq:467);dl:(len: 9)), (fc:(freq: 51);dl:(len: 9)),
  329. (fc:(freq:307);dl:(len: 9)), (fc:(freq:179);dl:(len: 9)), (fc:(freq:435);dl:(len: 9)),
  330. (fc:(freq:115);dl:(len: 9)), (fc:(freq:371);dl:(len: 9)), (fc:(freq:243);dl:(len: 9)),
  331. (fc:(freq:499);dl:(len: 9)), (fc:(freq: 11);dl:(len: 9)), (fc:(freq:267);dl:(len: 9)),
  332. (fc:(freq:139);dl:(len: 9)), (fc:(freq:395);dl:(len: 9)), (fc:(freq: 75);dl:(len: 9)),
  333. (fc:(freq:331);dl:(len: 9)), (fc:(freq:203);dl:(len: 9)), (fc:(freq:459);dl:(len: 9)),
  334. (fc:(freq: 43);dl:(len: 9)), (fc:(freq:299);dl:(len: 9)), (fc:(freq:171);dl:(len: 9)),
  335. (fc:(freq:427);dl:(len: 9)), (fc:(freq:107);dl:(len: 9)), (fc:(freq:363);dl:(len: 9)),
  336. (fc:(freq:235);dl:(len: 9)), (fc:(freq:491);dl:(len: 9)), (fc:(freq: 27);dl:(len: 9)),
  337. (fc:(freq:283);dl:(len: 9)), (fc:(freq:155);dl:(len: 9)), (fc:(freq:411);dl:(len: 9)),
  338. (fc:(freq: 91);dl:(len: 9)), (fc:(freq:347);dl:(len: 9)), (fc:(freq:219);dl:(len: 9)),
  339. (fc:(freq:475);dl:(len: 9)), (fc:(freq: 59);dl:(len: 9)), (fc:(freq:315);dl:(len: 9)),
  340. (fc:(freq:187);dl:(len: 9)), (fc:(freq:443);dl:(len: 9)), (fc:(freq:123);dl:(len: 9)),
  341. (fc:(freq:379);dl:(len: 9)), (fc:(freq:251);dl:(len: 9)), (fc:(freq:507);dl:(len: 9)),
  342. (fc:(freq: 7);dl:(len: 9)), (fc:(freq:263);dl:(len: 9)), (fc:(freq:135);dl:(len: 9)),
  343. (fc:(freq:391);dl:(len: 9)), (fc:(freq: 71);dl:(len: 9)), (fc:(freq:327);dl:(len: 9)),
  344. (fc:(freq:199);dl:(len: 9)), (fc:(freq:455);dl:(len: 9)), (fc:(freq: 39);dl:(len: 9)),
  345. (fc:(freq:295);dl:(len: 9)), (fc:(freq:167);dl:(len: 9)), (fc:(freq:423);dl:(len: 9)),
  346. (fc:(freq:103);dl:(len: 9)), (fc:(freq:359);dl:(len: 9)), (fc:(freq:231);dl:(len: 9)),
  347. (fc:(freq:487);dl:(len: 9)), (fc:(freq: 23);dl:(len: 9)), (fc:(freq:279);dl:(len: 9)),
  348. (fc:(freq:151);dl:(len: 9)), (fc:(freq:407);dl:(len: 9)), (fc:(freq: 87);dl:(len: 9)),
  349. (fc:(freq:343);dl:(len: 9)), (fc:(freq:215);dl:(len: 9)), (fc:(freq:471);dl:(len: 9)),
  350. (fc:(freq: 55);dl:(len: 9)), (fc:(freq:311);dl:(len: 9)), (fc:(freq:183);dl:(len: 9)),
  351. (fc:(freq:439);dl:(len: 9)), (fc:(freq:119);dl:(len: 9)), (fc:(freq:375);dl:(len: 9)),
  352. (fc:(freq:247);dl:(len: 9)), (fc:(freq:503);dl:(len: 9)), (fc:(freq: 15);dl:(len: 9)),
  353. (fc:(freq:271);dl:(len: 9)), (fc:(freq:143);dl:(len: 9)), (fc:(freq:399);dl:(len: 9)),
  354. (fc:(freq: 79);dl:(len: 9)), (fc:(freq:335);dl:(len: 9)), (fc:(freq:207);dl:(len: 9)),
  355. (fc:(freq:463);dl:(len: 9)), (fc:(freq: 47);dl:(len: 9)), (fc:(freq:303);dl:(len: 9)),
  356. (fc:(freq:175);dl:(len: 9)), (fc:(freq:431);dl:(len: 9)), (fc:(freq:111);dl:(len: 9)),
  357. (fc:(freq:367);dl:(len: 9)), (fc:(freq:239);dl:(len: 9)), (fc:(freq:495);dl:(len: 9)),
  358. (fc:(freq: 31);dl:(len: 9)), (fc:(freq:287);dl:(len: 9)), (fc:(freq:159);dl:(len: 9)),
  359. (fc:(freq:415);dl:(len: 9)), (fc:(freq: 95);dl:(len: 9)), (fc:(freq:351);dl:(len: 9)),
  360. (fc:(freq:223);dl:(len: 9)), (fc:(freq:479);dl:(len: 9)), (fc:(freq: 63);dl:(len: 9)),
  361. (fc:(freq:319);dl:(len: 9)), (fc:(freq:191);dl:(len: 9)), (fc:(freq:447);dl:(len: 9)),
  362. (fc:(freq:127);dl:(len: 9)), (fc:(freq:383);dl:(len: 9)), (fc:(freq:255);dl:(len: 9)),
  363. (fc:(freq:511);dl:(len: 9)), (fc:(freq: 0);dl:(len: 7)), (fc:(freq: 64);dl:(len: 7)),
  364. (fc:(freq: 32);dl:(len: 7)), (fc:(freq: 96);dl:(len: 7)), (fc:(freq: 16);dl:(len: 7)),
  365. (fc:(freq: 80);dl:(len: 7)), (fc:(freq: 48);dl:(len: 7)), (fc:(freq:112);dl:(len: 7)),
  366. (fc:(freq: 8);dl:(len: 7)), (fc:(freq: 72);dl:(len: 7)), (fc:(freq: 40);dl:(len: 7)),
  367. (fc:(freq:104);dl:(len: 7)), (fc:(freq: 24);dl:(len: 7)), (fc:(freq: 88);dl:(len: 7)),
  368. (fc:(freq: 56);dl:(len: 7)), (fc:(freq:120);dl:(len: 7)), (fc:(freq: 4);dl:(len: 7)),
  369. (fc:(freq: 68);dl:(len: 7)), (fc:(freq: 36);dl:(len: 7)), (fc:(freq:100);dl:(len: 7)),
  370. (fc:(freq: 20);dl:(len: 7)), (fc:(freq: 84);dl:(len: 7)), (fc:(freq: 52);dl:(len: 7)),
  371. (fc:(freq:116);dl:(len: 7)), (fc:(freq: 3);dl:(len: 8)), (fc:(freq:131);dl:(len: 8)),
  372. (fc:(freq: 67);dl:(len: 8)), (fc:(freq:195);dl:(len: 8)), (fc:(freq: 35);dl:(len: 8)),
  373. (fc:(freq:163);dl:(len: 8)), (fc:(freq: 99);dl:(len: 8)), (fc:(freq:227);dl:(len: 8))
  374. );
  375. { The static distance tree. (Actually a trivial tree since all lens use
  376. 5 bits.) }
  377. static_dtree : array[0..D_CODES-1] of ct_data = (
  378. (fc:(freq: 0); dl:(len:5)), (fc:(freq:16); dl:(len:5)), (fc:(freq: 8); dl:(len:5)),
  379. (fc:(freq:24); dl:(len:5)), (fc:(freq: 4); dl:(len:5)), (fc:(freq:20); dl:(len:5)),
  380. (fc:(freq:12); dl:(len:5)), (fc:(freq:28); dl:(len:5)), (fc:(freq: 2); dl:(len:5)),
  381. (fc:(freq:18); dl:(len:5)), (fc:(freq:10); dl:(len:5)), (fc:(freq:26); dl:(len:5)),
  382. (fc:(freq: 6); dl:(len:5)), (fc:(freq:22); dl:(len:5)), (fc:(freq:14); dl:(len:5)),
  383. (fc:(freq:30); dl:(len:5)), (fc:(freq: 1); dl:(len:5)), (fc:(freq:17); dl:(len:5)),
  384. (fc:(freq: 9); dl:(len:5)), (fc:(freq:25); dl:(len:5)), (fc:(freq: 5); dl:(len:5)),
  385. (fc:(freq:21); dl:(len:5)), (fc:(freq:13); dl:(len:5)), (fc:(freq:29); dl:(len:5)),
  386. (fc:(freq: 3); dl:(len:5)), (fc:(freq:19); dl:(len:5)), (fc:(freq:11); dl:(len:5)),
  387. (fc:(freq:27); dl:(len:5)), (fc:(freq: 7); dl:(len:5)), (fc:(freq:23); dl:(len:5))
  388. );
  389. { Distance codes. The first 256 values correspond to the distances
  390. 3 .. 258, the last 256 values correspond to the top 8 bits of
  391. the 15 bit distances. }
  392. _dist_code : array[0..DIST_CODE_LEN-1] of byte = (
  393. 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
  394. 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
  395. 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  396. 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  397. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13,
  398. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  399. 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  400. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  401. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  402. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15,
  403. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  404. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  405. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17,
  406. 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22,
  407. 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  408. 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  409. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  410. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27,
  411. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  412. 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  413. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  414. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  415. 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  416. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  417. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  418. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
  419. );
  420. { length code for each normalized match length (0 == MIN_MATCH) }
  421. _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of byte = (
  422. 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12,
  423. 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16,
  424. 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19,
  425. 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  426. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22,
  427. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  428. 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  429. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  430. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  431. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26,
  432. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  433. 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  434. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
  435. );
  436. { First normalized length for each code (0 = MIN_MATCH) }
  437. base_length : array[0..LENGTH_CODES-1] of integer = (
  438. 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
  439. 64, 80, 96, 112, 128, 160, 192, 224, 0
  440. );
  441. { First normalized distance for each code (0 = distance of 1) }
  442. base_dist : array[0..D_CODES-1] of integer = (
  443. 0, 1, 2, 3, 4, 6, 8, 12, 16, 24,
  444. 32, 48, 64, 96, 128, 192, 256, 384, 512, 768,
  445. 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
  446. );
  447. {$endif}
  448. { Output a byte on the stream.
  449. IN assertion: there is enough room in pending_buf.
  450. macro put_byte(s, c)
  451. begin
  452. s^.pending_buf^[s^.pending] := (c);
  453. inc(s^.pending);
  454. end
  455. }
  456. const
  457. MIN_LOOKAHEAD = (MAX_MATCH+MIN_MATCH+1);
  458. { Minimum amount of lookahead, except at the end of the input file.
  459. See deflate.c for comments about the MIN_MATCH+1. }
  460. {macro d_code(dist)
  461. if (dist) < 256 then
  462. := _dist_code[dist]
  463. else
  464. := _dist_code[256+((dist) shr 7)]);
  465. Mapping from a distance to a distance code. dist is the distance - 1 and
  466. must not have side effects. _dist_code[256] and _dist_code[257] are never
  467. used. }
  468. {$ifndef ORG_DEBUG}
  469. { Inline versions of _tr_tally for speed: }
  470. #if defined(GEN_TREES_H) || !defined(STDC)
  471. extern byte _length_code[];
  472. extern byte _dist_code[];
  473. #else
  474. extern const byte _length_code[];
  475. extern const byte _dist_code[];
  476. #endif
  477. macro _tr_tally_lit(s, c, flush)
  478. var
  479. cc : byte;
  480. begin
  481. cc := (c);
  482. s^.d_buf[s^.last_lit] := 0;
  483. s^.l_buf[s^.last_lit] := cc;
  484. inc(s^.last_lit);
  485. inc(s^.dyn_ltree[cc].fc.Freq);
  486. flush := (s^.last_lit = s^.lit_bufsize-1);
  487. end;
  488. macro _tr_tally_dist(s, distance, length, flush) \
  489. var
  490. len : byte;
  491. dist : word;
  492. begin
  493. len := (length);
  494. dist := (distance);
  495. s^.d_buf[s^.last_lit] := dist;
  496. s^.l_buf[s^.last_lit] = len;
  497. inc(s^.last_lit);
  498. dec(dist);
  499. inc(s^.dyn_ltree[_length_code[len]+LITERALS+1].fc.Freq);
  500. inc(s^.dyn_dtree[d_code(dist)].Freq);
  501. flush := (s^.last_lit = s^.lit_bufsize-1);
  502. end;
  503. {$endif}
  504. { ===========================================================================
  505. Constants }
  506. const
  507. MAX_BL_BITS = 7;
  508. { Bit length codes must not exceed MAX_BL_BITS bits }
  509. const
  510. END_BLOCK = 256;
  511. { end of block literal code }
  512. const
  513. REP_3_6 = 16;
  514. { repeat previous bit length 3-6 times (2 bits of repeat count) }
  515. const
  516. REPZ_3_10 = 17;
  517. { repeat a zero length 3-10 times (3 bits of repeat count) }
  518. const
  519. REPZ_11_138 = 18;
  520. { repeat a zero length 11-138 times (7 bits of repeat count) }
  521. {local}
  522. const
  523. extra_lbits : array[0..LENGTH_CODES-1] of integer
  524. { extra bits for each length code }
  525. = (0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0);
  526. {local}
  527. const
  528. extra_dbits : array[0..D_CODES-1] of integer
  529. { extra bits for each distance code }
  530. = (0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13);
  531. {local}
  532. const
  533. extra_blbits : array[0..BL_CODES-1] of integer { extra bits for each bit length code }
  534. = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7);
  535. {local}
  536. const
  537. bl_order : array[0..BL_CODES-1] of byte
  538. = (16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15);
  539. { The lengths of the bit length codes are sent in order of decreasing
  540. probability, to avoid transmitting the lengths for unused bit length codes.
  541. }
  542. const
  543. Buf_size = (8 * 2*sizeof(char));
  544. { Number of bits used within bi_buf. (bi_buf might be implemented on
  545. more than 16 bits on some systems.) }
  546. { ===========================================================================
  547. Local data. These are initialized only once. }
  548. {$ifdef GEN_TREES_H)}
  549. { non ANSI compilers may not accept trees.h }
  550. const
  551. DIST_CODE_LEN = 512; { see definition of array dist_code below }
  552. {local}
  553. var
  554. static_ltree : array[0..L_CODES+2-1] of ct_data;
  555. { The static literal tree. Since the bit lengths are imposed, there is no
  556. need for the L_CODES extra codes used during heap construction. However
  557. The codes 286 and 287 are needed to build a canonical tree (see _tr_init
  558. below). }
  559. {local}
  560. static_dtree : array[0..D_CODES-1] of ct_data;
  561. { The static distance tree. (Actually a trivial tree since all codes use
  562. 5 bits.) }
  563. _dist_code : array[0..DIST_CODE_LEN-1] of byte;
  564. { Distance codes. The first 256 values correspond to the distances
  565. 3 .. 258, the last 256 values correspond to the top 8 bits of
  566. the 15 bit distances. }
  567. _length_code : array[0..MAX_MATCH-MIN_MATCH+1-1] of byte;
  568. { length code for each normalized match length (0 == MIN_MATCH) }
  569. {local}
  570. base_length : array[0..LENGTH_CODES-1] of integer;
  571. { First normalized length for each code (0 = MIN_MATCH) }
  572. {local}
  573. base_dist : array[0..D_CODES-1] of integer;
  574. { First normalized distance for each code (0 = distance of 1) }
  575. {$endif} { GEN_TREES_H }
  576. {local}
  577. const
  578. static_l_desc : static_tree_desc =
  579. (static_tree: {tree_ptr}@static_ltree[0]; { pointer to array of ct_data }
  580. extra_bits: {pzIntfArray}@extra_lbits[0]; { pointer to array of integer }
  581. extra_base: LITERALS+1;
  582. elems: L_CODES;
  583. max_length: MAX_BITS);
  584. {local}
  585. const
  586. static_d_desc : static_tree_desc =
  587. (static_tree: {tree_ptr}@static_dtree[0];
  588. extra_bits: {pzIntfArray}@extra_dbits[0];
  589. extra_base : 0;
  590. elems: D_CODES;
  591. max_length: MAX_BITS);
  592. {local}
  593. const
  594. static_bl_desc : static_tree_desc =
  595. (static_tree: {tree_ptr}(NIL);
  596. extra_bits: {pzIntfArray}@extra_blbits[0];
  597. extra_base : 0;
  598. elems: BL_CODES;
  599. max_length: MAX_BL_BITS);
  600. {$ifdef GEN_TREES_H}
  601. {local}
  602. procedure gen_trees_header;
  603. {$endif}
  604. (*
  605. { ===========================================================================
  606. Output a short LSB first on the stream.
  607. IN assertion: there is enough room in pendingBuf. }
  608. macro put_short(s, w)
  609. begin
  610. {put_byte(s, (byte)((w) & 0xff));}
  611. s.pending_buf^[s.pending] := byte((w) and $ff);
  612. inc(s.pending);
  613. {put_byte(s, (byte)((word)(w) >> 8));}
  614. s.pending_buf^[s.pending] := byte(word(w) shr 8);;
  615. inc(s.pending);
  616. end
  617. *)
  618. { ===========================================================================
  619. Send a value on a given number of bits.
  620. IN assertion: length <= 16 and value fits in length bits. }
  621. {$ifdef ORG_DEBUG}
  622. {local}
  623. procedure send_bits(var s : deflate_state;
  624. value : integer; { value to send }
  625. length : integer); { number of bits }
  626. begin
  627. {$ifdef ZLIB_DEBUG}
  628. Tracevv(' l '+IntToStr(length)+ ' v '+IntToStr(value));
  629. Assert((length > 0) and (length <= 15), 'invalid length');
  630. inc(s.bits_sent, longint(length));
  631. {$ENDIF}
  632. { If not enough room in bi_buf, use (valid) bits from bi_buf and
  633. (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  634. unused bits in value. }
  635. {$IFOPT Q+} {$Q-} {$DEFINE NoOverflowCheck} {$ENDIF}
  636. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  637. if (s.bi_valid > integer(Buf_size) - length) then
  638. begin
  639. s.bi_buf := s.bi_buf or integer(value shl s.bi_valid);
  640. {put_short(s, s.bi_buf);}
  641. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  642. inc(s.pending);
  643. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  644. inc(s.pending);
  645. s.bi_buf := word(value) shr (Buf_size - s.bi_valid);
  646. inc(s.bi_valid, length - Buf_size);
  647. end
  648. else
  649. begin
  650. s.bi_buf := s.bi_buf or integer(value shl s.bi_valid);
  651. inc(s.bi_valid, length);
  652. end;
  653. {$IFDEF NoOverflowCheck} {$Q+} {$UNDEF NoOverflowCheck} {$ENDIF}
  654. {$IFDEF NoRangeCheck} {$Q+} {$UNDEF NoRangeCheck} {$ENDIF}
  655. end;
  656. {$else} { !ZLIB_DEBUG }
  657. macro send_code(s, c, tree)
  658. begin
  659. send_bits(s, tree[c].Code, tree[c].Len);
  660. { Send a code of the given tree. c and tree must not have side effects }
  661. end
  662. macro send_bits(s, value, length) \
  663. begin integer len := length;\
  664. if (s^.bi_valid > (integer)Buf_size - len) begin\
  665. integer val := value;\
  666. s^.bi_buf |= (val << s^.bi_valid);\
  667. {put_short(s, s.bi_buf);}
  668. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  669. inc(s.pending);
  670. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  671. inc(s.pending);
  672. s^.bi_buf := (word)val >> (Buf_size - s^.bi_valid);\
  673. s^.bi_valid += len - Buf_size;\
  674. end else begin\
  675. s^.bi_buf |= (value) << s^.bi_valid;\
  676. s^.bi_valid += len;\
  677. end\
  678. end;
  679. {$endif} { ZLIB_DEBUG }
  680. { ===========================================================================
  681. Reverse the first len bits of a code, using straightforward code (a faster
  682. method would use a table)
  683. IN assertion: 1 <= len <= 15 }
  684. {local}
  685. function bi_reverse(code : cardinal; { the value to invert }
  686. len : integer) : cardinal; { its bit length }
  687. var
  688. res : cardinal; {register}
  689. begin
  690. res := 0;
  691. repeat
  692. res := res or (code and 1);
  693. code := code shr 1;
  694. res := res shl 1;
  695. dec(len);
  696. until (len <= 0);
  697. bi_reverse := res shr 1;
  698. end;
  699. { ===========================================================================
  700. Generate the codes for a given tree and bit counts (which need not be
  701. optimal).
  702. IN assertion: the array bl_count contains the bit length statistics for
  703. the given tree and the field len is set for all tree elements.
  704. OUT assertion: the field code is set for all tree elements of non
  705. zero code length. }
  706. {local}
  707. procedure gen_codes(tree : tree_ptr; { the tree to decorate }
  708. max_code : integer; { largest code with non zero frequency }
  709. var bl_count : array of word); { number of codes at each bit length }
  710. var
  711. next_code : array[0..MAX_BITS+1-1] of word; { next code value for each bit length }
  712. code : word; { running code value }
  713. bits : integer; { bit index }
  714. n : integer; { code index }
  715. var
  716. len : integer;
  717. begin
  718. code := 0;
  719. { The distribution counts are first used to generate the code values
  720. without bit reversal. }
  721. for bits := 1 to MAX_BITS do
  722. begin
  723. code := ((code + bl_count[bits-1]) shl 1);
  724. next_code[bits] := code;
  725. end;
  726. { Check that the bit counts in bl_count are consistent. The last code
  727. must be all ones. }
  728. {$IFDEF ZLIB_DEBUG}
  729. Assert (code + bl_count[MAX_BITS]-1 = (1 shl MAX_BITS)-1,
  730. 'inconsistent bit counts');
  731. Tracev(#13'gen_codes: max_code '+IntToStr(max_code));
  732. {$ENDIF}
  733. for n := 0 to max_code do
  734. begin
  735. len := tree[n].dl.Len;
  736. if (len = 0) then
  737. continue;
  738. { Now reverse the bits }
  739. tree[n].fc.Code := bi_reverse(next_code[len], len);
  740. inc(next_code[len]);
  741. {$ifdef ZLIB_DEBUG}
  742. if (n>31) and (n<128) then
  743. Tracecv(tree <> tree_ptr(@static_ltree),
  744. (^M'n #'+IntToStr(n)+' '+char(n)+' l '+IntToStr(len)+' c '+
  745. IntToStr(tree[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'))
  746. else
  747. Tracecv(tree <> tree_ptr(@static_ltree),
  748. (^M'n #'+IntToStr(n)+' l '+IntToStr(len)+' c '+
  749. IntToStr(tree[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'));
  750. {$ENDIF}
  751. end;
  752. end;
  753. { ===========================================================================
  754. Genererate the file trees.h describing the static trees. }
  755. {$ifdef GEN_TREES_H}
  756. macro SEPARATOR(i, last, width)
  757. if (i) = (last) then
  758. ( ^M');'^M^M
  759. else \
  760. if (i) mod (width) = (width)-1 then
  761. ','^M
  762. else
  763. ', '
  764. procedure gen_trees_header;
  765. var
  766. header : system.text;
  767. i : integer;
  768. begin
  769. system.assign(header, 'trees.inc');
  770. {$I-}
  771. ReWrite(header);
  772. {$I+}
  773. Assert (IOresult <> 0, 'Can''t open trees.h');
  774. WriteLn(header,
  775. '{ header created automatically with -DGEN_TREES_H }'^M);
  776. WriteLn(header, 'local const ct_data static_ltree[L_CODES+2] := (');
  777. for i := 0 to L_CODES+2-1 do
  778. begin
  779. WriteLn(header, '((%3u),(%3u))%s', static_ltree[i].Code,
  780. static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
  781. end;
  782. WriteLn(header, 'local const ct_data static_dtree[D_CODES] := (');
  783. for i := 0 to D_CODES-1 do
  784. begin
  785. WriteLn(header, '((%2u),(%2u))%s', static_dtree[i].Code,
  786. static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
  787. end;
  788. WriteLn(header, 'const byte _dist_code[DIST_CODE_LEN] := (');
  789. for i := 0 to DIST_CODE_LEN-1 do
  790. begin
  791. WriteLn(header, '%2u%s', _dist_code[i],
  792. SEPARATOR(i, DIST_CODE_LEN-1, 20));
  793. end;
  794. WriteLn(header, 'const byte _length_code[MAX_MATCH-MIN_MATCH+1]= (');
  795. for i := 0 to MAX_MATCH-MIN_MATCH+1-1 do
  796. begin
  797. WriteLn(header, '%2u%s', _length_code[i],
  798. SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
  799. end;
  800. WriteLn(header, 'local const integer base_length[LENGTH_CODES] := (');
  801. for i := 0 to LENGTH_CODES-1 do
  802. begin
  803. WriteLn(header, '%1u%s', base_length[i],
  804. SEPARATOR(i, LENGTH_CODES-1, 20));
  805. end;
  806. WriteLn(header, 'local const integer base_dist[D_CODES] := (');
  807. for i := 0 to D_CODES-1 do
  808. begin
  809. WriteLn(header, '%5u%s', base_dist[i],
  810. SEPARATOR(i, D_CODES-1, 10));
  811. end;
  812. close(header);
  813. end;
  814. {$endif} { GEN_TREES_H }
  815. { ===========================================================================
  816. Initialize the various 'constant' tables. }
  817. {local}
  818. procedure tr_static_init;
  819. {$ifdef GEN_TREES_H}
  820. const
  821. static_init_done : boolean = FALSE;
  822. var
  823. n : integer; { iterates over tree elements }
  824. bits : integer; { bit counter }
  825. length : integer; { length value }
  826. code : integer; { code value }
  827. dist : integer; { distance index }
  828. bl_count : array[0..MAX_BITS+1-1] of word;
  829. { number of codes at each bit length for an optimal tree }
  830. begin
  831. if (static_init_done) then
  832. exit;
  833. { Initialize the mapping length (0..255) -> length code (0..28) }
  834. length := 0;
  835. for code := 0 to LENGTH_CODES-1-1 do
  836. begin
  837. base_length[code] := length;
  838. for n := 0 to (1 shl extra_lbits[code])-1 do
  839. begin
  840. _length_code[length] := byte(code);
  841. inc(length);
  842. end;
  843. end;
  844. Assert (length = 256, 'tr_static_init: length <> 256');
  845. { Note that the length 255 (match length 258) can be represented
  846. in two different ways: code 284 + 5 bits or code 285, so we
  847. overwrite length_code[255] to use the best encoding: }
  848. _length_code[length-1] := byte(code);
  849. { Initialize the mapping dist (0..32K) -> dist code (0..29) }
  850. dist := 0;
  851. for code := 0 to 16-1 do
  852. begin
  853. base_dist[code] := dist;
  854. for n := 0 to (1 shl extra_dbits[code])-1 do
  855. begin
  856. _dist_code[dist] := byte(code);
  857. inc(dist);
  858. end;
  859. end;
  860. Assert (dist = 256, 'tr_static_init: dist <> 256');
  861. dist := dist shr 7; { from now on, all distances are divided by 128 }
  862. for code := 16 to D_CODES-1 do
  863. begin
  864. base_dist[code] := dist shl 7;
  865. for n := 0 to (1 shl (extra_dbits[code]-7))-1 do
  866. begin
  867. _dist_code[256 + dist] := byte(code);
  868. inc(dist);
  869. end;
  870. end;
  871. Assert (dist = 256, 'tr_static_init: 256+dist <> 512');
  872. { Construct the codes of the static literal tree }
  873. for bits := 0 to MAX_BITS do
  874. bl_count[bits] := 0;
  875. n := 0;
  876. while (n <= 143) do
  877. begin
  878. static_ltree[n].dl.Len := 8;
  879. inc(n);
  880. inc(bl_count[8]);
  881. end;
  882. while (n <= 255) do
  883. begin
  884. static_ltree[n].dl.Len := 9;
  885. inc(n);
  886. inc(bl_count[9]);
  887. end;
  888. while (n <= 279) do
  889. begin
  890. static_ltree[n].dl.Len := 7;
  891. inc(n);
  892. inc(bl_count[7]);
  893. end;
  894. while (n <= 287) do
  895. begin
  896. static_ltree[n].dl.Len := 8;
  897. inc(n);
  898. inc(bl_count[8]);
  899. end;
  900. { Codes 286 and 287 do not exist, but we must include them in the
  901. tree construction to get a canonical Huffman tree (longest code
  902. all ones) }
  903. gen_codes(tree_ptr(@static_ltree), L_CODES+1, bl_count);
  904. { The static distance tree is trivial: }
  905. for n := 0 to D_CODES-1 do
  906. begin
  907. static_dtree[n].dl.Len := 5;
  908. static_dtree[n].fc.Code := bi_reverse(cardinal(n), 5);
  909. end;
  910. static_init_done := TRUE;
  911. gen_trees_header; { save to include file }
  912. {$else}
  913. begin
  914. {$endif} { GEN_TREES_H) }
  915. end;
  916. { ===========================================================================
  917. Initialize a new block. }
  918. {local}
  919. procedure init_block(var s : deflate_state);
  920. var
  921. n : integer; { iterates over tree elements }
  922. begin
  923. { Initialize the trees. }
  924. for n := 0 to L_CODES-1 do
  925. s.dyn_ltree[n].fc.Freq := 0;
  926. for n := 0 to D_CODES-1 do
  927. s.dyn_dtree[n].fc.Freq := 0;
  928. for n := 0 to BL_CODES-1 do
  929. s.bl_tree[n].fc.Freq := 0;
  930. s.dyn_ltree[END_BLOCK].fc.Freq := 1;
  931. s.static_len := 0;
  932. s.opt_len := 0;
  933. s.matches := 0;
  934. s.last_lit := 0;
  935. end;
  936. const
  937. SMALLEST = 1;
  938. { Index within the heap array of least frequent node in the Huffman tree }
  939. { ===========================================================================
  940. Initialize the tree data structures for a new zlib stream. }
  941. procedure _tr_init(var s : deflate_state);
  942. begin
  943. tr_static_init;
  944. s.compressed_len := 0;
  945. s.l_desc.dyn_tree := tree_ptr(@s.dyn_ltree);
  946. s.l_desc.stat_desc := @static_l_desc;
  947. s.d_desc.dyn_tree := tree_ptr(@s.dyn_dtree);
  948. s.d_desc.stat_desc := @static_d_desc;
  949. s.bl_desc.dyn_tree := tree_ptr(@s.bl_tree);
  950. s.bl_desc.stat_desc := @static_bl_desc;
  951. s.bi_buf := 0;
  952. s.bi_valid := 0;
  953. s.last_eob_len := 8; { enough lookahead for inflate }
  954. {$ifdef ZLIB_DEBUG}
  955. s.bits_sent := 0;
  956. {$endif}
  957. { Initialize the first block of the first file: }
  958. init_block(s);
  959. end;
  960. { ===========================================================================
  961. Remove the smallest element from the heap and recreate the heap with
  962. one less element. Updates heap and heap_len.
  963. macro pqremove(s, tree, top)
  964. begin
  965. top := s.heap[SMALLEST];
  966. s.heap[SMALLEST] := s.heap[s.heap_len];
  967. dec(s.heap_len);
  968. pqdownheap(s, tree, SMALLEST);
  969. end
  970. }
  971. { ===========================================================================
  972. Compares to subtrees, using the tree depth as tie breaker when
  973. the subtrees have equal frequency. This minimizes the worst case length.
  974. macro smaller(tree, n, m, depth)
  975. ( (tree[n].Freq < tree[m].Freq) or
  976. ((tree[n].Freq = tree[m].Freq) and (depth[n] <= depth[m])) )
  977. }
  978. { ===========================================================================
  979. Restore the heap property by moving down the tree starting at node k,
  980. exchanging a node with the smallest of its two sons if necessary, stopping
  981. when the heap property is re-established (each father smaller than its
  982. two sons). }
  983. {local}
  984. procedure pqdownheap(var s : deflate_state;
  985. tree : tree_ptr; { the tree to restore }
  986. k : integer); { node to move down }
  987. var
  988. v : integer;
  989. j : integer;
  990. begin
  991. v := s.heap[k];
  992. j := k shl 1; { left son of k }
  993. while (j <= s.heap_len) do
  994. begin
  995. { Set j to the smallest of the two sons: }
  996. if (j < s.heap_len) and
  997. {smaller(tree, s.heap[j+1], s.heap[j], s.depth)}
  998. ( (tree[s.heap[j+1]].fc.Freq < tree[s.heap[j]].fc.Freq) or
  999. ((tree[s.heap[j+1]].fc.Freq = tree[s.heap[j]].fc.Freq) and
  1000. (s.depth[s.heap[j+1]] <= s.depth[s.heap[j]])) ) then
  1001. begin
  1002. inc(j);
  1003. end;
  1004. { Exit if v is smaller than both sons }
  1005. if {(smaller(tree, v, s.heap[j], s.depth))}
  1006. ( (tree[v].fc.Freq < tree[s.heap[j]].fc.Freq) or
  1007. ((tree[v].fc.Freq = tree[s.heap[j]].fc.Freq) and
  1008. (s.depth[v] <= s.depth[s.heap[j]])) ) then
  1009. break;
  1010. { Exchange v with the smallest son }
  1011. s.heap[k] := s.heap[j];
  1012. k := j;
  1013. { And continue down the tree, setting j to the left son of k }
  1014. j := j shl 1;
  1015. end;
  1016. s.heap[k] := v;
  1017. end;
  1018. { ===========================================================================
  1019. Compute the optimal bit lengths for a tree and update the total bit length
  1020. for the current block.
  1021. IN assertion: the fields freq and dad are set, heap[heap_max] and
  1022. above are the tree nodes sorted by increasing frequency.
  1023. OUT assertions: the field len is set to the optimal bit length, the
  1024. array bl_count contains the frequencies for each bit length.
  1025. The length opt_len is updated; static_len is also updated if stree is
  1026. not null. }
  1027. {local}
  1028. procedure gen_bitlen(var s : deflate_state;
  1029. var desc : tree_desc); { the tree descriptor }
  1030. var
  1031. tree : tree_ptr;
  1032. max_code : integer;
  1033. stree : tree_ptr; {const}
  1034. extra : pinteger; {const}
  1035. base : integer;
  1036. max_length : integer;
  1037. h : integer; { heap index }
  1038. n, m : integer; { iterate over the tree elements }
  1039. bits : integer; { bit length }
  1040. xbits : integer; { extra bits }
  1041. f : word; { frequency }
  1042. overflow : integer; { number of elements with bit length too large }
  1043. begin
  1044. tree := desc.dyn_tree;
  1045. max_code := desc.max_code;
  1046. stree := desc.stat_desc^.static_tree;
  1047. extra := desc.stat_desc^.extra_bits;
  1048. base := desc.stat_desc^.extra_base;
  1049. max_length := desc.stat_desc^.max_length;
  1050. overflow := 0;
  1051. for bits := 0 to MAX_BITS do
  1052. s.bl_count[bits] := 0;
  1053. { In a first pass, compute the optimal bit lengths (which may
  1054. overflow in the case of the bit length tree). }
  1055. tree[s.heap[s.heap_max]].dl.Len := 0; { root of the heap }
  1056. for h := s.heap_max+1 to HEAP_SIZE-1 do
  1057. begin
  1058. n := s.heap[h];
  1059. bits := tree[tree[n].dl.Dad].dl.Len + 1;
  1060. if (bits > max_length) then
  1061. begin
  1062. bits := max_length;
  1063. inc(overflow);
  1064. end;
  1065. tree[n].dl.Len := word(bits);
  1066. { We overwrite tree[n].dl.Dad which is no longer needed }
  1067. if (n > max_code) then
  1068. continue; { not a leaf node }
  1069. inc(s.bl_count[bits]);
  1070. xbits := 0;
  1071. if (n >= base) then
  1072. xbits := extra[n-base];
  1073. f := tree[n].fc.Freq;
  1074. inc(s.opt_len, longint(f) * (bits + xbits));
  1075. if (stree <> NIL) then
  1076. inc(s.static_len, longint(f) * (stree[n].dl.Len + xbits));
  1077. end;
  1078. if (overflow = 0) then
  1079. exit;
  1080. {$ifdef ZLIB_DEBUG}
  1081. Tracev(^M'bit length overflow');
  1082. {$endif}
  1083. { This happens for example on obj2 and pic of the Calgary corpus }
  1084. { Find the first bit length which could increase: }
  1085. repeat
  1086. bits := max_length-1;
  1087. while (s.bl_count[bits] = 0) do
  1088. dec(bits);
  1089. dec(s.bl_count[bits]); { move one leaf down the tree }
  1090. inc(s.bl_count[bits+1], 2); { move one overflow item as its brother }
  1091. dec(s.bl_count[max_length]);
  1092. { The brother of the overflow item also moves one step up,
  1093. but this does not affect bl_count[max_length] }
  1094. dec(overflow, 2);
  1095. until (overflow <= 0);
  1096. { Now recompute all bit lengths, scanning in increasing frequency.
  1097. h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  1098. lengths instead of fixing only the wrong ones. This idea is taken
  1099. from 'ar' written by Haruhiko Okumura.) }
  1100. h := HEAP_SIZE; { Delphi3: compiler warning w/o this }
  1101. for bits := max_length downto 1 do
  1102. begin
  1103. n := s.bl_count[bits];
  1104. while (n <> 0) do
  1105. begin
  1106. dec(h);
  1107. m := s.heap[h];
  1108. if (m > max_code) then
  1109. continue;
  1110. if (tree[m].dl.Len <> cardinal(bits)) then
  1111. begin
  1112. {$ifdef ZLIB_DEBUG}
  1113. Trace('code '+IntToStr(m)+' bits '+IntToStr(tree[m].dl.Len)
  1114. +'.'+IntToStr(bits));
  1115. {$ENDIF}
  1116. inc(s.opt_len, (cardinal(bits) - cardinal(tree[m].dl.Len))
  1117. * cardinal(tree[m].fc.Freq) );
  1118. tree[m].dl.Len := word(bits);
  1119. end;
  1120. dec(n);
  1121. end;
  1122. end;
  1123. end;
  1124. { ===========================================================================
  1125. Construct one Huffman tree and assigns the code bit strings and lengths.
  1126. Update the total bit length for the current block.
  1127. IN assertion: the field freq is set for all tree elements.
  1128. OUT assertions: the fields len and code are set to the optimal bit length
  1129. and corresponding code. The length opt_len is updated; static_len is
  1130. also updated if stree is not null. The field max_code is set. }
  1131. {local}
  1132. procedure build_tree(var s : deflate_state;
  1133. var desc : tree_desc); { the tree descriptor }
  1134. var
  1135. tree : tree_ptr;
  1136. stree : tree_ptr; {const}
  1137. elems : integer;
  1138. n, m : integer; { iterate over heap elements }
  1139. max_code : integer; { largest code with non zero frequency }
  1140. node : integer; { new node being created }
  1141. begin
  1142. tree := desc.dyn_tree;
  1143. stree := desc.stat_desc^.static_tree;
  1144. elems := desc.stat_desc^.elems;
  1145. max_code := -1;
  1146. { Construct the initial heap, with least frequent element in
  1147. heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  1148. heap[0] is not used. }
  1149. s.heap_len := 0;
  1150. s.heap_max := HEAP_SIZE;
  1151. for n := 0 to elems-1 do
  1152. begin
  1153. if (tree[n].fc.Freq <> 0) then
  1154. begin
  1155. max_code := n;
  1156. inc(s.heap_len);
  1157. s.heap[s.heap_len] := n;
  1158. s.depth[n] := 0;
  1159. end
  1160. else
  1161. begin
  1162. tree[n].dl.Len := 0;
  1163. end;
  1164. end;
  1165. { The pkzip format requires that at least one distance code exists,
  1166. and that at least one bit should be sent even if there is only one
  1167. possible code. So to avoid special checks later on we force at least
  1168. two codes of non zero frequency. }
  1169. while (s.heap_len < 2) do
  1170. begin
  1171. inc(s.heap_len);
  1172. if (max_code < 2) then
  1173. begin
  1174. inc(max_code);
  1175. s.heap[s.heap_len] := max_code;
  1176. node := max_code;
  1177. end
  1178. else
  1179. begin
  1180. s.heap[s.heap_len] := 0;
  1181. node := 0;
  1182. end;
  1183. tree[node].fc.Freq := 1;
  1184. s.depth[node] := 0;
  1185. dec(s.opt_len);
  1186. if (stree <> NIL) then
  1187. dec(s.static_len, stree[node].dl.Len);
  1188. { node is 0 or 1 so it does not have extra bits }
  1189. end;
  1190. desc.max_code := max_code;
  1191. { The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  1192. establish sub-heaps of increasing lengths: }
  1193. for n := s.heap_len div 2 downto 1 do
  1194. pqdownheap(s, tree, n);
  1195. { Construct the Huffman tree by repeatedly combining the least two
  1196. frequent nodes. }
  1197. node := elems; { next internal node of the tree }
  1198. repeat
  1199. {pqremove(s, tree, n);} { n := node of least frequency }
  1200. n := s.heap[SMALLEST];
  1201. s.heap[SMALLEST] := s.heap[s.heap_len];
  1202. dec(s.heap_len);
  1203. pqdownheap(s, tree, SMALLEST);
  1204. m := s.heap[SMALLEST]; { m := node of next least frequency }
  1205. dec(s.heap_max);
  1206. s.heap[s.heap_max] := n; { keep the nodes sorted by frequency }
  1207. dec(s.heap_max);
  1208. s.heap[s.heap_max] := m;
  1209. { Create a new node father of n and m }
  1210. tree[node].fc.Freq := tree[n].fc.Freq + tree[m].fc.Freq;
  1211. { maximum }
  1212. if (s.depth[n] >= s.depth[m]) then
  1213. s.depth[node] := byte (s.depth[n] + 1)
  1214. else
  1215. s.depth[node] := byte (s.depth[m] + 1);
  1216. tree[m].dl.Dad := word(node);
  1217. tree[n].dl.Dad := word(node);
  1218. {$ifdef DUMP_BL_TREE}
  1219. if (tree = tree_ptr(@s.bl_tree)) then
  1220. begin
  1221. WriteLn(#13'node ',node,'(',tree[node].fc.Freq,') sons ',n,
  1222. '(',tree[n].fc.Freq,') ', m, '(',tree[m].fc.Freq,')');
  1223. end;
  1224. {$endif}
  1225. { and insert the new node in the heap }
  1226. s.heap[SMALLEST] := node;
  1227. inc(node);
  1228. pqdownheap(s, tree, SMALLEST);
  1229. until (s.heap_len < 2);
  1230. dec(s.heap_max);
  1231. s.heap[s.heap_max] := s.heap[SMALLEST];
  1232. { At this point, the fields freq and dad are set. We can now
  1233. generate the bit lengths. }
  1234. gen_bitlen(s, desc);
  1235. { The field len is now set, we can generate the bit codes }
  1236. gen_codes (tree, max_code, s.bl_count);
  1237. end;
  1238. { ===========================================================================
  1239. Scan a literal or distance tree to determine the frequencies of the codes
  1240. in the bit length tree. }
  1241. {local}
  1242. procedure scan_tree(var s : deflate_state;
  1243. var tree : array of ct_data; { the tree to be scanned }
  1244. max_code : integer); { and its largest code of non zero frequency }
  1245. var
  1246. n : integer; { iterates over all tree elements }
  1247. prevlen : integer; { last emitted length }
  1248. curlen : integer; { length of current code }
  1249. nextlen : integer; { length of next code }
  1250. count : integer; { repeat count of the current code }
  1251. max_count : integer; { max repeat count }
  1252. min_count : integer; { min repeat count }
  1253. begin
  1254. prevlen := -1;
  1255. nextlen := tree[0].dl.Len;
  1256. count := 0;
  1257. max_count := 7;
  1258. min_count := 4;
  1259. if (nextlen = 0) then
  1260. begin
  1261. max_count := 138;
  1262. min_count := 3;
  1263. end;
  1264. tree[max_code+1].dl.Len := word($ffff); { guard }
  1265. for n := 0 to max_code do
  1266. begin
  1267. curlen := nextlen;
  1268. nextlen := tree[n+1].dl.Len;
  1269. inc(count);
  1270. if (count < max_count) and (curlen = nextlen) then
  1271. continue
  1272. else
  1273. if (count < min_count) then
  1274. inc(s.bl_tree[curlen].fc.Freq, count)
  1275. else
  1276. if (curlen <> 0) then
  1277. begin
  1278. if (curlen <> prevlen) then
  1279. inc(s.bl_tree[curlen].fc.Freq);
  1280. inc(s.bl_tree[REP_3_6].fc.Freq);
  1281. end
  1282. else
  1283. if (count <= 10) then
  1284. inc(s.bl_tree[REPZ_3_10].fc.Freq)
  1285. else
  1286. inc(s.bl_tree[REPZ_11_138].fc.Freq);
  1287. count := 0;
  1288. prevlen := curlen;
  1289. if (nextlen = 0) then
  1290. begin
  1291. max_count := 138;
  1292. min_count := 3;
  1293. end
  1294. else
  1295. if (curlen = nextlen) then
  1296. begin
  1297. max_count := 6;
  1298. min_count := 3;
  1299. end
  1300. else
  1301. begin
  1302. max_count := 7;
  1303. min_count := 4;
  1304. end;
  1305. end;
  1306. end;
  1307. { ===========================================================================
  1308. Send a literal or distance tree in compressed form, using the codes in
  1309. bl_tree. }
  1310. {local}
  1311. procedure send_tree(var s : deflate_state;
  1312. var tree : array of ct_data; { the tree to be scanned }
  1313. max_code : integer); { and its largest code of non zero frequency }
  1314. var
  1315. n : integer; { iterates over all tree elements }
  1316. prevlen : integer; { last emitted length }
  1317. curlen : integer; { length of current code }
  1318. nextlen : integer; { length of next code }
  1319. count : integer; { repeat count of the current code }
  1320. max_count : integer; { max repeat count }
  1321. min_count : integer; { min repeat count }
  1322. begin
  1323. prevlen := -1;
  1324. nextlen := tree[0].dl.Len;
  1325. count := 0;
  1326. max_count := 7;
  1327. min_count := 4;
  1328. { tree[max_code+1].dl.Len := -1; } { guard already set }
  1329. if (nextlen = 0) then
  1330. begin
  1331. max_count := 138;
  1332. min_count := 3;
  1333. end;
  1334. for n := 0 to max_code do
  1335. begin
  1336. curlen := nextlen;
  1337. nextlen := tree[n+1].dl.Len;
  1338. inc(count);
  1339. if (count < max_count) and (curlen = nextlen) then
  1340. continue
  1341. else
  1342. if (count < min_count) then
  1343. begin
  1344. repeat
  1345. {$ifdef ZLIB_DEBUG}
  1346. Tracevvv(#13'cd '+IntToStr(curlen));
  1347. {$ENDIF}
  1348. send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
  1349. dec(count);
  1350. until (count = 0);
  1351. end
  1352. else
  1353. if (curlen <> 0) then
  1354. begin
  1355. if (curlen <> prevlen) then
  1356. begin
  1357. {$ifdef ZLIB_DEBUG}
  1358. Tracevvv(#13'cd '+IntToStr(curlen));
  1359. {$ENDIF}
  1360. send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
  1361. dec(count);
  1362. end;
  1363. {$IFDEF ZLIB_DEBUG}
  1364. Assert((count >= 3) and (count <= 6), ' 3_6?');
  1365. {$ENDIF}
  1366. {$ifdef ZLIB_DEBUG}
  1367. Tracevvv(#13'cd '+IntToStr(REP_3_6));
  1368. {$ENDIF}
  1369. send_bits(s, s.bl_tree[REP_3_6].fc.Code, s.bl_tree[REP_3_6].dl.Len);
  1370. send_bits(s, count-3, 2);
  1371. end
  1372. else
  1373. if (count <= 10) then
  1374. begin
  1375. {$ifdef ZLIB_DEBUG}
  1376. Tracevvv(#13'cd '+IntToStr(REPZ_3_10));
  1377. {$ENDIF}
  1378. send_bits(s, s.bl_tree[REPZ_3_10].fc.Code, s.bl_tree[REPZ_3_10].dl.Len);
  1379. send_bits(s, count-3, 3);
  1380. end
  1381. else
  1382. begin
  1383. {$ifdef ZLIB_DEBUG}
  1384. Tracevvv(#13'cd '+IntToStr(REPZ_11_138));
  1385. {$ENDIF}
  1386. send_bits(s, s.bl_tree[REPZ_11_138].fc.Code, s.bl_tree[REPZ_11_138].dl.Len);
  1387. send_bits(s, count-11, 7);
  1388. end;
  1389. count := 0;
  1390. prevlen := curlen;
  1391. if (nextlen = 0) then
  1392. begin
  1393. max_count := 138;
  1394. min_count := 3;
  1395. end
  1396. else
  1397. if (curlen = nextlen) then
  1398. begin
  1399. max_count := 6;
  1400. min_count := 3;
  1401. end
  1402. else
  1403. begin
  1404. max_count := 7;
  1405. min_count := 4;
  1406. end;
  1407. end;
  1408. end;
  1409. { ===========================================================================
  1410. Construct the Huffman tree for the bit lengths and return the index in
  1411. bl_order of the last bit length code to send. }
  1412. {local}
  1413. function build_bl_tree(var s : deflate_state) : integer;
  1414. var
  1415. max_blindex : integer; { index of last bit length code of non zero freq }
  1416. begin
  1417. { Determine the bit length frequencies for literal and distance trees }
  1418. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  1419. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  1420. { Build the bit length tree: }
  1421. build_tree(s, s.bl_desc);
  1422. { opt_len now includes the length of the tree representations, except
  1423. the lengths of the bit lengths codes and the 5+5+4 bits for the counts. }
  1424. { Determine the number of bit length codes to send. The pkzip format
  1425. requires that at least 4 bit length codes be sent. (appnote.txt says
  1426. 3 but the actual value used is 4.) }
  1427. for max_blindex := BL_CODES-1 downto 3 do
  1428. begin
  1429. if (s.bl_tree[bl_order[max_blindex]].dl.Len <> 0) then
  1430. break;
  1431. end;
  1432. { Update opt_len to include the bit length tree and counts }
  1433. inc(s.opt_len, 3*(max_blindex+1) + 5+5+4);
  1434. {$ifdef ZLIB_DEBUG}
  1435. Tracev(^M'dyn trees: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1436. {$ENDIF}
  1437. build_bl_tree := max_blindex;
  1438. end;
  1439. { ===========================================================================
  1440. Send the header for a block using dynamic Huffman trees: the counts, the
  1441. lengths of the bit length codes, the literal tree and the distance tree.
  1442. IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. }
  1443. {local}
  1444. procedure send_all_trees(var s : deflate_state;
  1445. lcodes : integer;
  1446. dcodes : integer;
  1447. blcodes : integer); { number of codes for each tree }
  1448. var
  1449. rank : integer; { index in bl_order }
  1450. begin
  1451. {$IFDEF ZLIB_DEBUG}
  1452. Assert ((lcodes >= 257) and (dcodes >= 1) and (blcodes >= 4),
  1453. 'not enough codes');
  1454. Assert ((lcodes <= L_CODES) and (dcodes <= D_CODES)
  1455. and (blcodes <= BL_CODES), 'too many codes');
  1456. Tracev(^M'bl counts: ');
  1457. {$ENDIF}
  1458. send_bits(s, lcodes-257, 5); { not +255 as stated in appnote.txt }
  1459. send_bits(s, dcodes-1, 5);
  1460. send_bits(s, blcodes-4, 4); { not -3 as stated in appnote.txt }
  1461. for rank := 0 to blcodes-1 do
  1462. begin
  1463. {$ifdef ZLIB_DEBUG}
  1464. Tracev(^M'bl code '+IntToStr(bl_order[rank]));
  1465. {$ENDIF}
  1466. send_bits(s, s.bl_tree[bl_order[rank]].dl.Len, 3);
  1467. end;
  1468. {$ifdef ZLIB_DEBUG}
  1469. Tracev(^M'bl tree: sent '+IntToStr(s.bits_sent));
  1470. {$ENDIF}
  1471. send_tree(s, s.dyn_ltree, lcodes-1); { literal tree }
  1472. {$ifdef ZLIB_DEBUG}
  1473. Tracev(^M'lit tree: sent '+IntToStr(s.bits_sent));
  1474. {$ENDIF}
  1475. send_tree(s, s.dyn_dtree, dcodes-1); { distance tree }
  1476. {$ifdef ZLIB_DEBUG}
  1477. Tracev(^M'dist tree: sent '+IntToStr(s.bits_sent));
  1478. {$ENDIF}
  1479. end;
  1480. { ===========================================================================
  1481. Flush the bit buffer and align the output on a byte boundary }
  1482. {local}
  1483. procedure bi_windup(var s : deflate_state);
  1484. begin
  1485. if (s.bi_valid > 8) then
  1486. begin
  1487. {put_short(s, s.bi_buf);}
  1488. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  1489. inc(s.pending);
  1490. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  1491. inc(s.pending);
  1492. end
  1493. else
  1494. if (s.bi_valid > 0) then
  1495. begin
  1496. {put_byte(s, (Byte)s^.bi_buf);}
  1497. s.pending_buf^[s.pending] := Byte(s.bi_buf);
  1498. inc(s.pending);
  1499. end;
  1500. s.bi_buf := 0;
  1501. s.bi_valid := 0;
  1502. {$ifdef ZLIB_DEBUG}
  1503. s.bits_sent := (s.bits_sent+7) and (not 7);
  1504. {$endif}
  1505. end;
  1506. { ===========================================================================
  1507. Copy a stored block, storing first the length and its
  1508. one's complement if requested. }
  1509. {local}
  1510. procedure copy_block(var s : deflate_state;
  1511. buf : Pbyte; { the input data }
  1512. len : word; { its length }
  1513. header : boolean); { true if block header must be written }
  1514. begin
  1515. bi_windup(s); { align on byte boundary }
  1516. s.last_eob_len := 8; { enough lookahead for inflate }
  1517. if (header) then
  1518. begin
  1519. {put_short(s, (word)len);}
  1520. s.pending_buf^[s.pending] := byte(len and $ff);
  1521. inc(s.pending);
  1522. s.pending_buf^[s.pending] := byte(len shr 8);;
  1523. inc(s.pending);
  1524. {put_short(s, (word)~len);}
  1525. s.pending_buf^[s.pending] := byte((not len) and $ff);
  1526. inc(s.pending);
  1527. s.pending_buf^[s.pending] := byte((not len) shr 8);;
  1528. inc(s.pending);
  1529. {$ifdef ZLIB_DEBUG}
  1530. inc(s.bits_sent, 2*16);
  1531. {$endif}
  1532. end;
  1533. {$ifdef ZLIB_DEBUG}
  1534. inc(s.bits_sent, len shl 3);
  1535. {$endif}
  1536. move(buf^,s.pending_buf^[s.pending],len);
  1537. inc(s.pending,len);
  1538. end;
  1539. { ===========================================================================
  1540. Send a stored block }
  1541. procedure _tr_stored_block(var s : deflate_state;
  1542. buf : Pbyte; { input block }
  1543. stored_len : longint; { length of input block }
  1544. eof : boolean); { true if this is the last block for a file }
  1545. begin
  1546. send_bits(s, (STORED_BLOCK shl 1)+ord(eof), 3); { send block type }
  1547. s.compressed_len := (s.compressed_len + 3 + 7) and longint(not cardinal(7));
  1548. inc(s.compressed_len, (stored_len + 4) shl 3);
  1549. copy_block(s, buf, cardinal(stored_len), TRUE); { with header }
  1550. end;
  1551. { ===========================================================================
  1552. Flush the bit buffer, keeping at most 7 bits in it. }
  1553. {local}
  1554. procedure bi_flush(var s : deflate_state);
  1555. begin
  1556. if (s.bi_valid = 16) then
  1557. begin
  1558. {put_short(s, s.bi_buf);}
  1559. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  1560. inc(s.pending);
  1561. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  1562. inc(s.pending);
  1563. s.bi_buf := 0;
  1564. s.bi_valid := 0;
  1565. end
  1566. else
  1567. if (s.bi_valid >= 8) then
  1568. begin
  1569. {put_byte(s, (Byte)s^.bi_buf);}
  1570. s.pending_buf^[s.pending] := Byte(s.bi_buf);
  1571. inc(s.pending);
  1572. s.bi_buf := s.bi_buf shr 8;
  1573. dec(s.bi_valid, 8);
  1574. end;
  1575. end;
  1576. { ===========================================================================
  1577. Send one empty static block to give enough lookahead for inflate.
  1578. This takes 10 bits, of which 7 may remain in the bit buffer.
  1579. The current inflate code requires 9 bits of lookahead. If the
  1580. last two codes for the previous block (real code plus EOB) were coded
  1581. on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  1582. the last real code. In this case we send two empty static blocks instead
  1583. of one. (There are no problems if the previous block is stored or fixed.)
  1584. To simplify the code, we assume the worst case of last real code encoded
  1585. on one bit only. }
  1586. procedure _tr_align(var s : deflate_state);
  1587. begin
  1588. send_bits(s, STATIC_TREES shl 1, 3);
  1589. {$ifdef ZLIB_DEBUG}
  1590. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1591. {$ENDIF}
  1592. send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
  1593. inc(s.compressed_len, cardinal(10)); { 3 for block type, 7 for EOB }
  1594. bi_flush(s);
  1595. { Of the 10 bits for the empty block, we have already sent
  1596. (10 - bi_valid) bits. The lookahead for the last real code (before
  1597. the EOB of the previous block) was thus at least one plus the length
  1598. of the EOB plus what we have just sent of the empty static block. }
  1599. if (1 + s.last_eob_len + 10 - s.bi_valid < 9) then
  1600. begin
  1601. send_bits(s, STATIC_TREES shl 1, 3);
  1602. {$ifdef ZLIB_DEBUG}
  1603. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1604. {$ENDIF}
  1605. send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
  1606. inc(s.compressed_len, cardinal(10));
  1607. bi_flush(s);
  1608. end;
  1609. s.last_eob_len := 7;
  1610. end;
  1611. { ===========================================================================
  1612. Set the data type to ASCII or BINARY, using a crude approximation:
  1613. binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  1614. IN assertion: the fields freq of dyn_ltree are set and the total of all
  1615. frequencies does not exceed 64K (to fit in an integer on 16 bit machines). }
  1616. {local}
  1617. procedure set_data_type(var s : deflate_state);
  1618. var
  1619. n : integer;
  1620. ascii_freq : cardinal;
  1621. bin_freq : cardinal;
  1622. begin
  1623. n := 0;
  1624. ascii_freq := 0;
  1625. bin_freq := 0;
  1626. while (n < 7) do
  1627. begin
  1628. inc(bin_freq, s.dyn_ltree[n].fc.Freq);
  1629. inc(n);
  1630. end;
  1631. while (n < 128) do
  1632. begin
  1633. inc(ascii_freq, s.dyn_ltree[n].fc.Freq);
  1634. inc(n);
  1635. end;
  1636. while (n < LITERALS) do
  1637. begin
  1638. inc(bin_freq, s.dyn_ltree[n].fc.Freq);
  1639. inc(n);
  1640. end;
  1641. if (bin_freq > (ascii_freq shr 2)) then
  1642. s.data_type := Byte(Z_BINARY)
  1643. else
  1644. s.data_type := Byte(Z_ASCII);
  1645. end;
  1646. { ===========================================================================
  1647. Send the block data compressed using the given Huffman trees }
  1648. {local}
  1649. procedure compress_block(var s : deflate_state;
  1650. var ltree : array of ct_data; { literal tree }
  1651. var dtree : array of ct_data); { distance tree }
  1652. var
  1653. dist : cardinal; { distance of matched string }
  1654. lc : integer; { match length or unmatched char (if dist == 0) }
  1655. lx : cardinal; { running index in l_buf }
  1656. code : cardinal; { the code to send }
  1657. extra : integer; { number of extra bits to send }
  1658. begin
  1659. lx := 0;
  1660. if (s.last_lit <> 0) then
  1661. repeat
  1662. dist := s.d_buf^[lx];
  1663. lc := s.l_buf^[lx];
  1664. inc(lx);
  1665. if (dist = 0) then
  1666. begin
  1667. { send a literal byte }
  1668. {$ifdef ZLIB_DEBUG}
  1669. Tracevvv(#13'cd '+IntToStr(lc));
  1670. Tracecv((lc > 31) and (lc < 128), ' '+char(lc)+' ');
  1671. {$ENDIF}
  1672. send_bits(s, ltree[lc].fc.Code, ltree[lc].dl.Len);
  1673. end
  1674. else
  1675. begin
  1676. { Here, lc is the match length - MIN_MATCH }
  1677. code := _length_code[lc];
  1678. { send the length code }
  1679. {$ifdef ZLIB_DEBUG}
  1680. Tracevvv(#13'cd '+IntToStr(code+LITERALS+1));
  1681. {$ENDIF}
  1682. send_bits(s, ltree[code+LITERALS+1].fc.Code, ltree[code+LITERALS+1].dl.Len);
  1683. extra := extra_lbits[code];
  1684. if (extra <> 0) then
  1685. begin
  1686. dec(lc, base_length[code]);
  1687. send_bits(s, lc, extra); { send the extra length bits }
  1688. end;
  1689. dec(dist); { dist is now the match distance - 1 }
  1690. {code := d_code(dist);}
  1691. if (dist < 256) then
  1692. code := _dist_code[dist]
  1693. else
  1694. code := _dist_code[256+(dist shr 7)];
  1695. {$IFDEF ZLIB_DEBUG}
  1696. Assert (code < D_CODES, 'bad d_code');
  1697. {$ENDIF}
  1698. { send the distance code }
  1699. {$ifdef ZLIB_DEBUG}
  1700. Tracevvv(#13'cd '+IntToStr(code));
  1701. {$ENDIF}
  1702. send_bits(s, dtree[code].fc.Code, dtree[code].dl.Len);
  1703. extra := extra_dbits[code];
  1704. if (extra <> 0) then
  1705. begin
  1706. dec(dist, base_dist[code]);
  1707. send_bits(s, dist, extra); { send the extra distance bits }
  1708. end;
  1709. end; { literal or match pair ? }
  1710. { Check that the overlay between pending_buf and d_buf+l_buf is ok: }
  1711. {$IFDEF ZLIB_DEBUG}
  1712. Assert(s.pending < s.lit_bufsize + 2*lx, 'pendingBuf overflow');
  1713. {$ENDIF}
  1714. until (lx >= s.last_lit);
  1715. {$ifdef ZLIB_DEBUG}
  1716. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1717. {$ENDIF}
  1718. send_bits(s, ltree[END_BLOCK].fc.Code, ltree[END_BLOCK].dl.Len);
  1719. s.last_eob_len := ltree[END_BLOCK].dl.Len;
  1720. end;
  1721. { ===========================================================================
  1722. Determine the best encoding for the current block: dynamic trees, static
  1723. trees or store, and output the encoded block to the zip file. This function
  1724. returns the total compressed length for the file so far. }
  1725. function _tr_flush_block (var s : deflate_state;
  1726. buf : Pbyte; { input block, or NULL if too old }
  1727. stored_len : longint; { length of input block }
  1728. eof : boolean) : longint; { true if this is the last block for a file }
  1729. var
  1730. opt_lenb, static_lenb : longint; { opt_len and static_len in bytes }
  1731. max_blindex : integer; { index of last bit length code of non zero freq }
  1732. begin
  1733. max_blindex := 0;
  1734. { Build the Huffman trees unless a stored block is forced }
  1735. if (s.level > 0) then
  1736. begin
  1737. { Check if the file is ascii or binary }
  1738. if (s.data_type = Z_UNKNOWN) then
  1739. set_data_type(s);
  1740. { Construct the literal and distance trees }
  1741. build_tree(s, s.l_desc);
  1742. {$ifdef ZLIB_DEBUG}
  1743. Tracev(^M'lit data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1744. {$ENDIF}
  1745. build_tree(s, s.d_desc);
  1746. {$ifdef ZLIB_DEBUG}
  1747. Tracev(^M'dist data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1748. {$ENDIF}
  1749. { At this point, opt_len and static_len are the total bit lengths of
  1750. the compressed block data, excluding the tree representations. }
  1751. { Build the bit length tree for the above two trees, and get the index
  1752. in bl_order of the last bit length code to send. }
  1753. max_blindex := build_bl_tree(s);
  1754. { Determine the best encoding. Compute first the block length in bytes}
  1755. opt_lenb := (s.opt_len+3+7) shr 3;
  1756. static_lenb := (s.static_len+3+7) shr 3;
  1757. {$ifdef ZLIB_DEBUG}
  1758. Tracev(^M'opt %lu(%lu) stat %lu(%lu) stored %lu lit %u '+
  1759. '{opt_lenb, s.opt_len, static_lenb, s.static_len, stored_len,'+
  1760. 's.last_lit}');
  1761. {$ENDIF}
  1762. if (static_lenb <= opt_lenb) then
  1763. opt_lenb := static_lenb;
  1764. end
  1765. else
  1766. begin
  1767. {$IFDEF ZLIB_DEBUG}
  1768. Assert(buf <> nil, 'lost buf');
  1769. {$ENDIF}
  1770. static_lenb := stored_len + 5;
  1771. opt_lenb := static_lenb; { force a stored block }
  1772. end;
  1773. { If compression failed and this is the first and last block,
  1774. and if the .zip file can be seeked (to rewrite the local header),
  1775. the whole file is transformed into a stored file: }
  1776. {$ifdef STORED_FILE_OK}
  1777. {$ifdef FORCE_STORED_FILE}
  1778. if eof and (s.compressed_len = 0) then
  1779. begin { force stored file }
  1780. {$else}
  1781. if (stored_len <= opt_lenb) and eof and (s.compressed_len=cardinal(0))
  1782. and seekable()) do
  1783. begin
  1784. {$endif}
  1785. { Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: }
  1786. if buf=nil then
  1787. error ('block vanished');
  1788. copy_block(buf, cardinal(stored_len), 0); { without header }
  1789. s.compressed_len := stored_len shl 3;
  1790. s.method := STORED;
  1791. end
  1792. else
  1793. {$endif} { STORED_FILE_OK }
  1794. {$ifdef FORCE_STORED}
  1795. if buf<>nil then
  1796. begin { force stored block }
  1797. {$else}
  1798. if (stored_len+4 <= opt_lenb) and (buf <> nil) then
  1799. begin
  1800. { 4: two words for the lengths }
  1801. {$endif}
  1802. { The test buf <> NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1803. Otherwise we can't have processed more than WSIZE input bytes since
  1804. the last block flush, because compression would have been
  1805. successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1806. transform a block into a stored block. }
  1807. _tr_stored_block(s, buf, stored_len, eof);
  1808. {$ifdef FORCE_STATIC}
  1809. end
  1810. else
  1811. if (static_lenb >= 0) then
  1812. begin { force static trees }
  1813. {$else}
  1814. end
  1815. else
  1816. if (static_lenb = opt_lenb) then
  1817. begin
  1818. {$endif}
  1819. send_bits(s, (STATIC_TREES shl 1)+ord(eof), 3);
  1820. compress_block(s, static_ltree, static_dtree);
  1821. inc(s.compressed_len, 3 + s.static_len);
  1822. end
  1823. else
  1824. begin
  1825. send_bits(s, (DYN_TREES shl 1)+ord(eof), 3);
  1826. send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1,
  1827. max_blindex+1);
  1828. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  1829. inc(s.compressed_len, 3 + s.opt_len);
  1830. end;
  1831. {$ifdef ZLIB_DEBUG}
  1832. Assert (s.compressed_len = s.bits_sent, 'bad compressed size');
  1833. {$ENDIF}
  1834. init_block(s);
  1835. if (eof) then
  1836. begin
  1837. bi_windup(s);
  1838. inc(s.compressed_len, 7); { align on byte boundary }
  1839. end;
  1840. {$ifdef ZLIB_DEBUG}
  1841. Tracev(#13'comprlen %lu(%lu) {s.compressed_len shr 3,'+
  1842. 's.compressed_len-7*ord(eof)}');
  1843. {$ENDIF}
  1844. _tr_flush_block := s.compressed_len shr 3;
  1845. end;
  1846. { ===========================================================================
  1847. Save the match info and tally the frequency counts. Return true if
  1848. the current block must be flushed. }
  1849. function _tr_tally (var s : deflate_state;
  1850. dist : cardinal; { distance of matched string }
  1851. lc : cardinal) : boolean; { match length-MIN_MATCH or unmatched char (if dist=0) }
  1852. var
  1853. {$IFDEF ZLIB_DEBUG}
  1854. MAX_DIST : word;
  1855. {$ENDIF}
  1856. code : word;
  1857. {$ifdef TRUNCATE_BLOCK}
  1858. var
  1859. out_length : longint;
  1860. in_length : longint;
  1861. dcode : integer;
  1862. {$endif}
  1863. begin
  1864. s.d_buf^[s.last_lit] := word(dist);
  1865. s.l_buf^[s.last_lit] := byte(lc);
  1866. inc(s.last_lit);
  1867. if (dist = 0) then
  1868. begin
  1869. { lc is the unmatched char }
  1870. inc(s.dyn_ltree[lc].fc.Freq);
  1871. end
  1872. else
  1873. begin
  1874. inc(s.matches);
  1875. { Here, lc is the match length - MIN_MATCH }
  1876. dec(dist); { dist := match distance - 1 }
  1877. {macro d_code(dist)}
  1878. if (dist) < 256 then
  1879. code := _dist_code[dist]
  1880. else
  1881. code := _dist_code[256+(dist shr 7)];
  1882. {$IFDEF ZLIB_DEBUG}
  1883. {macro MAX_DIST(s) <=> ((s)^.w_size-MIN_LOOKAHEAD)
  1884. In order to simplify the code, particularly on 16 bit machines, match
  1885. distances are limited to MAX_DIST instead of WSIZE. }
  1886. MAX_DIST := word(s.w_size-MIN_LOOKAHEAD);
  1887. Assert((dist < word(MAX_DIST)) and
  1888. (word(lc) <= word(MAX_MATCH-MIN_MATCH)) and
  1889. (word(code) < word(D_CODES)), '_tr_tally: bad match');
  1890. {$ENDIF}
  1891. inc(s.dyn_ltree[_length_code[lc]+LITERALS+1].fc.Freq);
  1892. {s.dyn_dtree[d_code(dist)].Freq++;}
  1893. inc(s.dyn_dtree[code].fc.Freq);
  1894. end;
  1895. {$ifdef TRUNCATE_BLOCK}
  1896. { Try to guess if it is profitable to stop the current block here }
  1897. if (s.last_lit and $1fff = 0) and (s.level > 2) then
  1898. begin
  1899. { Compute an upper bound for the compressed length }
  1900. out_length := longint(s.last_lit)*cardinal(8);
  1901. in_length := longint(cardinal(s.strstart) - s.block_start);
  1902. for dcode := 0 to D_CODES-1 do
  1903. begin
  1904. inc(out_length, longint(s.dyn_dtree[dcode].fc.Freq *
  1905. (cardinal(5)+extra_dbits[dcode])) );
  1906. end;
  1907. out_length := out_length shr 3;
  1908. {$ifdef ZLIB_DEBUG}
  1909. Tracev(^M'last_lit %u, in %ld, out ~%ld(%ld%%) ');
  1910. { s.last_lit, in_length, out_length,
  1911. cardinal(100) - out_length*100 div in_length)); }
  1912. {$ENDIF}
  1913. if (s.matches < s.last_lit div 2) and (out_length < in_length div 2) then
  1914. begin
  1915. _tr_tally := TRUE;
  1916. exit;
  1917. end;
  1918. end;
  1919. {$endif}
  1920. _tr_tally := (s.last_lit = s.lit_bufsize-1);
  1921. { We avoid equality with lit_bufsize because of wraparound at 64K
  1922. on 16 bit machines and because stored blocks are restricted to
  1923. 64K-1 bytes. }
  1924. end;
  1925. end.