trees.pas 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250
  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 = ^tree_type;
  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 : Pintegerarray; { 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 : Pchar;
  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 : Pchar;
  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)); { pointer to array of ct_data }
  580. extra_bits: {pzIntfArray}(@(extra_lbits)); { 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));
  588. extra_bits: {pzIntfArray}(@(extra_dbits));
  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);
  597. extra_base : 0;
  598. elems: BL_CODES;
  599. max_length: MAX_BL_BITS);
  600. (* ===========================================================================
  601. Local (static) routines in this file. }
  602. procedure tr_static_init;
  603. procedure init_block(var deflate_state);
  604. procedure pqdownheap(var s : deflate_state;
  605. var tree : ct_data;
  606. k : integer);
  607. procedure gen_bitlen(var s : deflate_state;
  608. var desc : tree_desc);
  609. procedure gen_codes(var tree : ct_data;
  610. max_code : integer;
  611. bl_count : pushf);
  612. procedure build_tree(var s : deflate_state;
  613. var desc : tree_desc);
  614. procedure scan_tree(var s : deflate_state;
  615. var tree : ct_data;
  616. max_code : integer);
  617. procedure send_tree(var s : deflate_state;
  618. var tree : ct_data;
  619. max_code : integer);
  620. function build_bl_tree(var deflate_state) : integer;
  621. procedure send_all_trees(var deflate_state;
  622. lcodes : integer;
  623. dcodes : integer;
  624. blcodes : integer);
  625. procedure compress_block(var s : deflate_state;
  626. var ltree : ct_data;
  627. var dtree : ct_data);
  628. procedure set_data_type(var s : deflate_state);
  629. function bi_reverse(value : cardinal;
  630. length : integer) : cardinal;
  631. procedure bi_windup(var deflate_state);
  632. procedure bi_flush(var deflate_state);
  633. procedure copy_block(var deflate_state;
  634. buf : Pchar;
  635. len : cardinal;
  636. header : integer);
  637. *)
  638. {$ifdef GEN_TREES_H}
  639. {local}
  640. procedure gen_trees_header;
  641. {$endif}
  642. (*
  643. { ===========================================================================
  644. Output a short LSB first on the stream.
  645. IN assertion: there is enough room in pendingBuf. }
  646. macro put_short(s, w)
  647. begin
  648. {put_byte(s, (byte)((w) & 0xff));}
  649. s.pending_buf^[s.pending] := byte((w) and $ff);
  650. inc(s.pending);
  651. {put_byte(s, (byte)((word)(w) >> 8));}
  652. s.pending_buf^[s.pending] := byte(word(w) shr 8);;
  653. inc(s.pending);
  654. end
  655. *)
  656. { ===========================================================================
  657. Send a value on a given number of bits.
  658. IN assertion: length <= 16 and value fits in length bits. }
  659. {$ifdef ORG_DEBUG}
  660. {local}
  661. procedure send_bits(var s : deflate_state;
  662. value : integer; { value to send }
  663. length : integer); { number of bits }
  664. begin
  665. {$ifdef ZLIB_DEBUG}
  666. Tracevv(' l '+IntToStr(length)+ ' v '+IntToStr(value));
  667. Assert((length > 0) and (length <= 15), 'invalid length');
  668. inc(s.bits_sent, longint(length));
  669. {$ENDIF}
  670. { If not enough room in bi_buf, use (valid) bits from bi_buf and
  671. (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
  672. unused bits in value. }
  673. {$IFOPT Q+} {$Q-} {$DEFINE NoOverflowCheck} {$ENDIF}
  674. {$IFOPT R+} {$R-} {$DEFINE NoRangeCheck} {$ENDIF}
  675. if (s.bi_valid > integer(Buf_size) - length) then
  676. begin
  677. s.bi_buf := s.bi_buf or integer(value shl s.bi_valid);
  678. {put_short(s, s.bi_buf);}
  679. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  680. inc(s.pending);
  681. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  682. inc(s.pending);
  683. s.bi_buf := word(value) shr (Buf_size - s.bi_valid);
  684. inc(s.bi_valid, length - Buf_size);
  685. end
  686. else
  687. begin
  688. s.bi_buf := s.bi_buf or integer(value shl s.bi_valid);
  689. inc(s.bi_valid, length);
  690. end;
  691. {$IFDEF NoOverflowCheck} {$Q+} {$UNDEF NoOverflowCheck} {$ENDIF}
  692. {$IFDEF NoRangeCheck} {$Q+} {$UNDEF NoRangeCheck} {$ENDIF}
  693. end;
  694. {$else} { !ZLIB_DEBUG }
  695. macro send_code(s, c, tree)
  696. begin
  697. send_bits(s, tree[c].Code, tree[c].Len);
  698. { Send a code of the given tree. c and tree must not have side effects }
  699. end
  700. macro send_bits(s, value, length) \
  701. begin integer len := length;\
  702. if (s^.bi_valid > (integer)Buf_size - len) begin\
  703. integer val := value;\
  704. s^.bi_buf |= (val << s^.bi_valid);\
  705. {put_short(s, s.bi_buf);}
  706. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  707. inc(s.pending);
  708. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  709. inc(s.pending);
  710. s^.bi_buf := (word)val >> (Buf_size - s^.bi_valid);\
  711. s^.bi_valid += len - Buf_size;\
  712. end else begin\
  713. s^.bi_buf |= (value) << s^.bi_valid;\
  714. s^.bi_valid += len;\
  715. end\
  716. end;
  717. {$endif} { ZLIB_DEBUG }
  718. { ===========================================================================
  719. Reverse the first len bits of a code, using straightforward code (a faster
  720. method would use a table)
  721. IN assertion: 1 <= len <= 15 }
  722. {local}
  723. function bi_reverse(code : cardinal; { the value to invert }
  724. len : integer) : cardinal; { its bit length }
  725. var
  726. res : cardinal; {register}
  727. begin
  728. res := 0;
  729. repeat
  730. res := res or (code and 1);
  731. code := code shr 1;
  732. res := res shl 1;
  733. dec(len);
  734. until (len <= 0);
  735. bi_reverse := res shr 1;
  736. end;
  737. { ===========================================================================
  738. Generate the codes for a given tree and bit counts (which need not be
  739. optimal).
  740. IN assertion: the array bl_count contains the bit length statistics for
  741. the given tree and the field len is set for all tree elements.
  742. OUT assertion: the field code is set for all tree elements of non
  743. zero code length. }
  744. {local}
  745. procedure gen_codes(tree : tree_ptr; { the tree to decorate }
  746. max_code : integer; { largest code with non zero frequency }
  747. var bl_count : array of word); { number of codes at each bit length }
  748. var
  749. next_code : array[0..MAX_BITS+1-1] of word; { next code value for each bit length }
  750. code : word; { running code value }
  751. bits : integer; { bit index }
  752. n : integer; { code index }
  753. var
  754. len : integer;
  755. begin
  756. code := 0;
  757. { The distribution counts are first used to generate the code values
  758. without bit reversal. }
  759. for bits := 1 to MAX_BITS do
  760. begin
  761. code := ((code + bl_count[bits-1]) shl 1);
  762. next_code[bits] := code;
  763. end;
  764. { Check that the bit counts in bl_count are consistent. The last code
  765. must be all ones. }
  766. {$IFDEF ZLIB_DEBUG}
  767. Assert (code + bl_count[MAX_BITS]-1 = (1 shl MAX_BITS)-1,
  768. 'inconsistent bit counts');
  769. Tracev(#13'gen_codes: max_code '+IntToStr(max_code));
  770. {$ENDIF}
  771. for n := 0 to max_code do
  772. begin
  773. len := tree^[n].dl.Len;
  774. if (len = 0) then
  775. continue;
  776. { Now reverse the bits }
  777. tree^[n].fc.Code := bi_reverse(next_code[len], len);
  778. inc(next_code[len]);
  779. {$ifdef ZLIB_DEBUG}
  780. if (n>31) and (n<128) then
  781. Tracecv(tree <> tree_ptr(@static_ltree),
  782. (^M'n #'+IntToStr(n)+' '+char(n)+' l '+IntToStr(len)+' c '+
  783. IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'))
  784. else
  785. Tracecv(tree <> tree_ptr(@static_ltree),
  786. (^M'n #'+IntToStr(n)+' l '+IntToStr(len)+' c '+
  787. IntToStr(tree^[n].fc.Code)+' ('+IntToStr(next_code[len]-1)+')'));
  788. {$ENDIF}
  789. end;
  790. end;
  791. { ===========================================================================
  792. Genererate the file trees.h describing the static trees. }
  793. {$ifdef GEN_TREES_H}
  794. macro SEPARATOR(i, last, width)
  795. if (i) = (last) then
  796. ( ^M');'^M^M
  797. else \
  798. if (i) mod (width) = (width)-1 then
  799. ','^M
  800. else
  801. ', '
  802. procedure gen_trees_header;
  803. var
  804. header : system.text;
  805. i : integer;
  806. begin
  807. system.assign(header, 'trees.inc');
  808. {$I-}
  809. ReWrite(header);
  810. {$I+}
  811. Assert (IOresult <> 0, 'Can''t open trees.h');
  812. WriteLn(header,
  813. '{ header created automatically with -DGEN_TREES_H }'^M);
  814. WriteLn(header, 'local const ct_data static_ltree[L_CODES+2] := (');
  815. for i := 0 to L_CODES+2-1 do
  816. begin
  817. WriteLn(header, '((%3u),(%3u))%s', static_ltree[i].Code,
  818. static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5));
  819. end;
  820. WriteLn(header, 'local const ct_data static_dtree[D_CODES] := (');
  821. for i := 0 to D_CODES-1 do
  822. begin
  823. WriteLn(header, '((%2u),(%2u))%s', static_dtree[i].Code,
  824. static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5));
  825. end;
  826. WriteLn(header, 'const byte _dist_code[DIST_CODE_LEN] := (');
  827. for i := 0 to DIST_CODE_LEN-1 do
  828. begin
  829. WriteLn(header, '%2u%s', _dist_code[i],
  830. SEPARATOR(i, DIST_CODE_LEN-1, 20));
  831. end;
  832. WriteLn(header, 'const byte _length_code[MAX_MATCH-MIN_MATCH+1]= (');
  833. for i := 0 to MAX_MATCH-MIN_MATCH+1-1 do
  834. begin
  835. WriteLn(header, '%2u%s', _length_code[i],
  836. SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20));
  837. end;
  838. WriteLn(header, 'local const integer base_length[LENGTH_CODES] := (');
  839. for i := 0 to LENGTH_CODES-1 do
  840. begin
  841. WriteLn(header, '%1u%s', base_length[i],
  842. SEPARATOR(i, LENGTH_CODES-1, 20));
  843. end;
  844. WriteLn(header, 'local const integer base_dist[D_CODES] := (');
  845. for i := 0 to D_CODES-1 do
  846. begin
  847. WriteLn(header, '%5u%s', base_dist[i],
  848. SEPARATOR(i, D_CODES-1, 10));
  849. end;
  850. close(header);
  851. end;
  852. {$endif} { GEN_TREES_H }
  853. { ===========================================================================
  854. Initialize the various 'constant' tables. }
  855. {local}
  856. procedure tr_static_init;
  857. {$ifdef GEN_TREES_H}
  858. const
  859. static_init_done : boolean = FALSE;
  860. var
  861. n : integer; { iterates over tree elements }
  862. bits : integer; { bit counter }
  863. length : integer; { length value }
  864. code : integer; { code value }
  865. dist : integer; { distance index }
  866. bl_count : array[0..MAX_BITS+1-1] of word;
  867. { number of codes at each bit length for an optimal tree }
  868. begin
  869. if (static_init_done) then
  870. exit;
  871. { Initialize the mapping length (0..255) -> length code (0..28) }
  872. length := 0;
  873. for code := 0 to LENGTH_CODES-1-1 do
  874. begin
  875. base_length[code] := length;
  876. for n := 0 to (1 shl extra_lbits[code])-1 do
  877. begin
  878. _length_code[length] := byte(code);
  879. inc(length);
  880. end;
  881. end;
  882. Assert (length = 256, 'tr_static_init: length <> 256');
  883. { Note that the length 255 (match length 258) can be represented
  884. in two different ways: code 284 + 5 bits or code 285, so we
  885. overwrite length_code[255] to use the best encoding: }
  886. _length_code[length-1] := byte(code);
  887. { Initialize the mapping dist (0..32K) -> dist code (0..29) }
  888. dist := 0;
  889. for code := 0 to 16-1 do
  890. begin
  891. base_dist[code] := dist;
  892. for n := 0 to (1 shl extra_dbits[code])-1 do
  893. begin
  894. _dist_code[dist] := byte(code);
  895. inc(dist);
  896. end;
  897. end;
  898. Assert (dist = 256, 'tr_static_init: dist <> 256');
  899. dist := dist shr 7; { from now on, all distances are divided by 128 }
  900. for code := 16 to D_CODES-1 do
  901. begin
  902. base_dist[code] := dist shl 7;
  903. for n := 0 to (1 shl (extra_dbits[code]-7))-1 do
  904. begin
  905. _dist_code[256 + dist] := byte(code);
  906. inc(dist);
  907. end;
  908. end;
  909. Assert (dist = 256, 'tr_static_init: 256+dist <> 512');
  910. { Construct the codes of the static literal tree }
  911. for bits := 0 to MAX_BITS do
  912. bl_count[bits] := 0;
  913. n := 0;
  914. while (n <= 143) do
  915. begin
  916. static_ltree[n].dl.Len := 8;
  917. inc(n);
  918. inc(bl_count[8]);
  919. end;
  920. while (n <= 255) do
  921. begin
  922. static_ltree[n].dl.Len := 9;
  923. inc(n);
  924. inc(bl_count[9]);
  925. end;
  926. while (n <= 279) do
  927. begin
  928. static_ltree[n].dl.Len := 7;
  929. inc(n);
  930. inc(bl_count[7]);
  931. end;
  932. while (n <= 287) do
  933. begin
  934. static_ltree[n].dl.Len := 8;
  935. inc(n);
  936. inc(bl_count[8]);
  937. end;
  938. { Codes 286 and 287 do not exist, but we must include them in the
  939. tree construction to get a canonical Huffman tree (longest code
  940. all ones) }
  941. gen_codes(tree_ptr(@static_ltree), L_CODES+1, bl_count);
  942. { The static distance tree is trivial: }
  943. for n := 0 to D_CODES-1 do
  944. begin
  945. static_dtree[n].dl.Len := 5;
  946. static_dtree[n].fc.Code := bi_reverse(cardinal(n), 5);
  947. end;
  948. static_init_done := TRUE;
  949. gen_trees_header; { save to include file }
  950. {$else}
  951. begin
  952. {$endif} { GEN_TREES_H) }
  953. end;
  954. { ===========================================================================
  955. Initialize a new block. }
  956. {local}
  957. procedure init_block(var s : deflate_state);
  958. var
  959. n : integer; { iterates over tree elements }
  960. begin
  961. { Initialize the trees. }
  962. for n := 0 to L_CODES-1 do
  963. s.dyn_ltree[n].fc.Freq := 0;
  964. for n := 0 to D_CODES-1 do
  965. s.dyn_dtree[n].fc.Freq := 0;
  966. for n := 0 to BL_CODES-1 do
  967. s.bl_tree[n].fc.Freq := 0;
  968. s.dyn_ltree[END_BLOCK].fc.Freq := 1;
  969. s.static_len := 0;
  970. s.opt_len := 0;
  971. s.matches := 0;
  972. s.last_lit := 0;
  973. end;
  974. const
  975. SMALLEST = 1;
  976. { Index within the heap array of least frequent node in the Huffman tree }
  977. { ===========================================================================
  978. Initialize the tree data structures for a new zlib stream. }
  979. procedure _tr_init(var s : deflate_state);
  980. begin
  981. tr_static_init;
  982. s.compressed_len := 0;
  983. s.l_desc.dyn_tree := tree_ptr(@s.dyn_ltree);
  984. s.l_desc.stat_desc := @static_l_desc;
  985. s.d_desc.dyn_tree := tree_ptr(@s.dyn_dtree);
  986. s.d_desc.stat_desc := @static_d_desc;
  987. s.bl_desc.dyn_tree := tree_ptr(@s.bl_tree);
  988. s.bl_desc.stat_desc := @static_bl_desc;
  989. s.bi_buf := 0;
  990. s.bi_valid := 0;
  991. s.last_eob_len := 8; { enough lookahead for inflate }
  992. {$ifdef ZLIB_DEBUG}
  993. s.bits_sent := 0;
  994. {$endif}
  995. { Initialize the first block of the first file: }
  996. init_block(s);
  997. end;
  998. { ===========================================================================
  999. Remove the smallest element from the heap and recreate the heap with
  1000. one less element. Updates heap and heap_len.
  1001. macro pqremove(s, tree, top)
  1002. begin
  1003. top := s.heap[SMALLEST];
  1004. s.heap[SMALLEST] := s.heap[s.heap_len];
  1005. dec(s.heap_len);
  1006. pqdownheap(s, tree, SMALLEST);
  1007. end
  1008. }
  1009. { ===========================================================================
  1010. Compares to subtrees, using the tree depth as tie breaker when
  1011. the subtrees have equal frequency. This minimizes the worst case length.
  1012. macro smaller(tree, n, m, depth)
  1013. ( (tree[n].Freq < tree[m].Freq) or
  1014. ((tree[n].Freq = tree[m].Freq) and (depth[n] <= depth[m])) )
  1015. }
  1016. { ===========================================================================
  1017. Restore the heap property by moving down the tree starting at node k,
  1018. exchanging a node with the smallest of its two sons if necessary, stopping
  1019. when the heap property is re-established (each father smaller than its
  1020. two sons). }
  1021. {local}
  1022. procedure pqdownheap(var s : deflate_state;
  1023. var tree : tree_type; { the tree to restore }
  1024. k : integer); { node to move down }
  1025. var
  1026. v : integer;
  1027. j : integer;
  1028. begin
  1029. v := s.heap[k];
  1030. j := k shl 1; { left son of k }
  1031. while (j <= s.heap_len) do
  1032. begin
  1033. { Set j to the smallest of the two sons: }
  1034. if (j < s.heap_len) and
  1035. {smaller(tree, s.heap[j+1], s.heap[j], s.depth)}
  1036. ( (tree[s.heap[j+1]].fc.Freq < tree[s.heap[j]].fc.Freq) or
  1037. ((tree[s.heap[j+1]].fc.Freq = tree[s.heap[j]].fc.Freq) and
  1038. (s.depth[s.heap[j+1]] <= s.depth[s.heap[j]])) ) then
  1039. begin
  1040. inc(j);
  1041. end;
  1042. { Exit if v is smaller than both sons }
  1043. if {(smaller(tree, v, s.heap[j], s.depth))}
  1044. ( (tree[v].fc.Freq < tree[s.heap[j]].fc.Freq) or
  1045. ((tree[v].fc.Freq = tree[s.heap[j]].fc.Freq) and
  1046. (s.depth[v] <= s.depth[s.heap[j]])) ) then
  1047. break;
  1048. { Exchange v with the smallest son }
  1049. s.heap[k] := s.heap[j];
  1050. k := j;
  1051. { And continue down the tree, setting j to the left son of k }
  1052. j := j shl 1;
  1053. end;
  1054. s.heap[k] := v;
  1055. end;
  1056. { ===========================================================================
  1057. Compute the optimal bit lengths for a tree and update the total bit length
  1058. for the current block.
  1059. IN assertion: the fields freq and dad are set, heap[heap_max] and
  1060. above are the tree nodes sorted by increasing frequency.
  1061. OUT assertions: the field len is set to the optimal bit length, the
  1062. array bl_count contains the frequencies for each bit length.
  1063. The length opt_len is updated; static_len is also updated if stree is
  1064. not null. }
  1065. {local}
  1066. procedure gen_bitlen(var s : deflate_state;
  1067. var desc : tree_desc); { the tree descriptor }
  1068. var
  1069. tree : tree_ptr;
  1070. max_code : integer;
  1071. stree : tree_ptr; {const}
  1072. extra : Pintegerarray; {const}
  1073. base : integer;
  1074. max_length : integer;
  1075. h : integer; { heap index }
  1076. n, m : integer; { iterate over the tree elements }
  1077. bits : integer; { bit length }
  1078. xbits : integer; { extra bits }
  1079. f : word; { frequency }
  1080. overflow : integer; { number of elements with bit length too large }
  1081. begin
  1082. tree := desc.dyn_tree;
  1083. max_code := desc.max_code;
  1084. stree := desc.stat_desc^.static_tree;
  1085. extra := desc.stat_desc^.extra_bits;
  1086. base := desc.stat_desc^.extra_base;
  1087. max_length := desc.stat_desc^.max_length;
  1088. overflow := 0;
  1089. for bits := 0 to MAX_BITS do
  1090. s.bl_count[bits] := 0;
  1091. { In a first pass, compute the optimal bit lengths (which may
  1092. overflow in the case of the bit length tree). }
  1093. tree^[s.heap[s.heap_max]].dl.Len := 0; { root of the heap }
  1094. for h := s.heap_max+1 to HEAP_SIZE-1 do
  1095. begin
  1096. n := s.heap[h];
  1097. bits := tree^[tree^[n].dl.Dad].dl.Len + 1;
  1098. if (bits > max_length) then
  1099. begin
  1100. bits := max_length;
  1101. inc(overflow);
  1102. end;
  1103. tree^[n].dl.Len := word(bits);
  1104. { We overwrite tree[n].dl.Dad which is no longer needed }
  1105. if (n > max_code) then
  1106. continue; { not a leaf node }
  1107. inc(s.bl_count[bits]);
  1108. xbits := 0;
  1109. if (n >= base) then
  1110. xbits := extra^[n-base];
  1111. f := tree^[n].fc.Freq;
  1112. inc(s.opt_len, longint(f) * (bits + xbits));
  1113. if (stree <> NIL) then
  1114. inc(s.static_len, longint(f) * (stree^[n].dl.Len + xbits));
  1115. end;
  1116. if (overflow = 0) then
  1117. exit;
  1118. {$ifdef ZLIB_DEBUG}
  1119. Tracev(^M'bit length overflow');
  1120. {$endif}
  1121. { This happens for example on obj2 and pic of the Calgary corpus }
  1122. { Find the first bit length which could increase: }
  1123. repeat
  1124. bits := max_length-1;
  1125. while (s.bl_count[bits] = 0) do
  1126. dec(bits);
  1127. dec(s.bl_count[bits]); { move one leaf down the tree }
  1128. inc(s.bl_count[bits+1], 2); { move one overflow item as its brother }
  1129. dec(s.bl_count[max_length]);
  1130. { The brother of the overflow item also moves one step up,
  1131. but this does not affect bl_count[max_length] }
  1132. dec(overflow, 2);
  1133. until (overflow <= 0);
  1134. { Now recompute all bit lengths, scanning in increasing frequency.
  1135. h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
  1136. lengths instead of fixing only the wrong ones. This idea is taken
  1137. from 'ar' written by Haruhiko Okumura.) }
  1138. h := HEAP_SIZE; { Delphi3: compiler warning w/o this }
  1139. for bits := max_length downto 1 do
  1140. begin
  1141. n := s.bl_count[bits];
  1142. while (n <> 0) do
  1143. begin
  1144. dec(h);
  1145. m := s.heap[h];
  1146. if (m > max_code) then
  1147. continue;
  1148. if (tree^[m].dl.Len <> cardinal(bits)) then
  1149. begin
  1150. {$ifdef ZLIB_DEBUG}
  1151. Trace('code '+IntToStr(m)+' bits '+IntToStr(tree^[m].dl.Len)
  1152. +'.'+IntToStr(bits));
  1153. {$ENDIF}
  1154. inc(s.opt_len, (cardinal(bits) - cardinal(tree^[m].dl.Len))
  1155. * cardinal(tree^[m].fc.Freq) );
  1156. tree^[m].dl.Len := word(bits);
  1157. end;
  1158. dec(n);
  1159. end;
  1160. end;
  1161. end;
  1162. { ===========================================================================
  1163. Construct one Huffman tree and assigns the code bit strings and lengths.
  1164. Update the total bit length for the current block.
  1165. IN assertion: the field freq is set for all tree elements.
  1166. OUT assertions: the fields len and code are set to the optimal bit length
  1167. and corresponding code. The length opt_len is updated; static_len is
  1168. also updated if stree is not null. The field max_code is set. }
  1169. {local}
  1170. procedure build_tree(var s : deflate_state;
  1171. var desc : tree_desc); { the tree descriptor }
  1172. var
  1173. tree : tree_ptr;
  1174. stree : tree_ptr; {const}
  1175. elems : integer;
  1176. n, m : integer; { iterate over heap elements }
  1177. max_code : integer; { largest code with non zero frequency }
  1178. node : integer; { new node being created }
  1179. begin
  1180. tree := desc.dyn_tree;
  1181. stree := desc.stat_desc^.static_tree;
  1182. elems := desc.stat_desc^.elems;
  1183. max_code := -1;
  1184. { Construct the initial heap, with least frequent element in
  1185. heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  1186. heap[0] is not used. }
  1187. s.heap_len := 0;
  1188. s.heap_max := HEAP_SIZE;
  1189. for n := 0 to elems-1 do
  1190. begin
  1191. if (tree^[n].fc.Freq <> 0) then
  1192. begin
  1193. max_code := n;
  1194. inc(s.heap_len);
  1195. s.heap[s.heap_len] := n;
  1196. s.depth[n] := 0;
  1197. end
  1198. else
  1199. begin
  1200. tree^[n].dl.Len := 0;
  1201. end;
  1202. end;
  1203. { The pkzip format requires that at least one distance code exists,
  1204. and that at least one bit should be sent even if there is only one
  1205. possible code. So to avoid special checks later on we force at least
  1206. two codes of non zero frequency. }
  1207. while (s.heap_len < 2) do
  1208. begin
  1209. inc(s.heap_len);
  1210. if (max_code < 2) then
  1211. begin
  1212. inc(max_code);
  1213. s.heap[s.heap_len] := max_code;
  1214. node := max_code;
  1215. end
  1216. else
  1217. begin
  1218. s.heap[s.heap_len] := 0;
  1219. node := 0;
  1220. end;
  1221. tree^[node].fc.Freq := 1;
  1222. s.depth[node] := 0;
  1223. dec(s.opt_len);
  1224. if (stree <> NIL) then
  1225. dec(s.static_len, stree^[node].dl.Len);
  1226. { node is 0 or 1 so it does not have extra bits }
  1227. end;
  1228. desc.max_code := max_code;
  1229. { The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  1230. establish sub-heaps of increasing lengths: }
  1231. for n := s.heap_len div 2 downto 1 do
  1232. pqdownheap(s, tree^, n);
  1233. { Construct the Huffman tree by repeatedly combining the least two
  1234. frequent nodes. }
  1235. node := elems; { next internal node of the tree }
  1236. repeat
  1237. {pqremove(s, tree, n);} { n := node of least frequency }
  1238. n := s.heap[SMALLEST];
  1239. s.heap[SMALLEST] := s.heap[s.heap_len];
  1240. dec(s.heap_len);
  1241. pqdownheap(s, tree^, SMALLEST);
  1242. m := s.heap[SMALLEST]; { m := node of next least frequency }
  1243. dec(s.heap_max);
  1244. s.heap[s.heap_max] := n; { keep the nodes sorted by frequency }
  1245. dec(s.heap_max);
  1246. s.heap[s.heap_max] := m;
  1247. { Create a new node father of n and m }
  1248. tree^[node].fc.Freq := tree^[n].fc.Freq + tree^[m].fc.Freq;
  1249. { maximum }
  1250. if (s.depth[n] >= s.depth[m]) then
  1251. s.depth[node] := byte (s.depth[n] + 1)
  1252. else
  1253. s.depth[node] := byte (s.depth[m] + 1);
  1254. tree^[m].dl.Dad := word(node);
  1255. tree^[n].dl.Dad := word(node);
  1256. {$ifdef DUMP_BL_TREE}
  1257. if (tree = tree_ptr(@s.bl_tree)) then
  1258. begin
  1259. WriteLn(#13'node ',node,'(',tree^[node].fc.Freq,') sons ',n,
  1260. '(',tree^[n].fc.Freq,') ', m, '(',tree^[m].fc.Freq,')');
  1261. end;
  1262. {$endif}
  1263. { and insert the new node in the heap }
  1264. s.heap[SMALLEST] := node;
  1265. inc(node);
  1266. pqdownheap(s, tree^, SMALLEST);
  1267. until (s.heap_len < 2);
  1268. dec(s.heap_max);
  1269. s.heap[s.heap_max] := s.heap[SMALLEST];
  1270. { At this point, the fields freq and dad are set. We can now
  1271. generate the bit lengths. }
  1272. gen_bitlen(s, desc);
  1273. { The field len is now set, we can generate the bit codes }
  1274. gen_codes (tree, max_code, s.bl_count);
  1275. end;
  1276. { ===========================================================================
  1277. Scan a literal or distance tree to determine the frequencies of the codes
  1278. in the bit length tree. }
  1279. {local}
  1280. procedure scan_tree(var s : deflate_state;
  1281. var tree : array of ct_data; { the tree to be scanned }
  1282. max_code : integer); { and its largest code of non zero frequency }
  1283. var
  1284. n : integer; { iterates over all tree elements }
  1285. prevlen : integer; { last emitted length }
  1286. curlen : integer; { length of current code }
  1287. nextlen : integer; { length of next code }
  1288. count : integer; { repeat count of the current code }
  1289. max_count : integer; { max repeat count }
  1290. min_count : integer; { min repeat count }
  1291. begin
  1292. prevlen := -1;
  1293. nextlen := tree[0].dl.Len;
  1294. count := 0;
  1295. max_count := 7;
  1296. min_count := 4;
  1297. if (nextlen = 0) then
  1298. begin
  1299. max_count := 138;
  1300. min_count := 3;
  1301. end;
  1302. tree[max_code+1].dl.Len := word($ffff); { guard }
  1303. for n := 0 to max_code do
  1304. begin
  1305. curlen := nextlen;
  1306. nextlen := tree[n+1].dl.Len;
  1307. inc(count);
  1308. if (count < max_count) and (curlen = nextlen) then
  1309. continue
  1310. else
  1311. if (count < min_count) then
  1312. inc(s.bl_tree[curlen].fc.Freq, count)
  1313. else
  1314. if (curlen <> 0) then
  1315. begin
  1316. if (curlen <> prevlen) then
  1317. inc(s.bl_tree[curlen].fc.Freq);
  1318. inc(s.bl_tree[REP_3_6].fc.Freq);
  1319. end
  1320. else
  1321. if (count <= 10) then
  1322. inc(s.bl_tree[REPZ_3_10].fc.Freq)
  1323. else
  1324. inc(s.bl_tree[REPZ_11_138].fc.Freq);
  1325. count := 0;
  1326. prevlen := curlen;
  1327. if (nextlen = 0) then
  1328. begin
  1329. max_count := 138;
  1330. min_count := 3;
  1331. end
  1332. else
  1333. if (curlen = nextlen) then
  1334. begin
  1335. max_count := 6;
  1336. min_count := 3;
  1337. end
  1338. else
  1339. begin
  1340. max_count := 7;
  1341. min_count := 4;
  1342. end;
  1343. end;
  1344. end;
  1345. { ===========================================================================
  1346. Send a literal or distance tree in compressed form, using the codes in
  1347. bl_tree. }
  1348. {local}
  1349. procedure send_tree(var s : deflate_state;
  1350. var tree : array of ct_data; { the tree to be scanned }
  1351. max_code : integer); { and its largest code of non zero frequency }
  1352. var
  1353. n : integer; { iterates over all tree elements }
  1354. prevlen : integer; { last emitted length }
  1355. curlen : integer; { length of current code }
  1356. nextlen : integer; { length of next code }
  1357. count : integer; { repeat count of the current code }
  1358. max_count : integer; { max repeat count }
  1359. min_count : integer; { min repeat count }
  1360. begin
  1361. prevlen := -1;
  1362. nextlen := tree[0].dl.Len;
  1363. count := 0;
  1364. max_count := 7;
  1365. min_count := 4;
  1366. { tree[max_code+1].dl.Len := -1; } { guard already set }
  1367. if (nextlen = 0) then
  1368. begin
  1369. max_count := 138;
  1370. min_count := 3;
  1371. end;
  1372. for n := 0 to max_code do
  1373. begin
  1374. curlen := nextlen;
  1375. nextlen := tree[n+1].dl.Len;
  1376. inc(count);
  1377. if (count < max_count) and (curlen = nextlen) then
  1378. continue
  1379. else
  1380. if (count < min_count) then
  1381. begin
  1382. repeat
  1383. {$ifdef ZLIB_DEBUG}
  1384. Tracevvv(#13'cd '+IntToStr(curlen));
  1385. {$ENDIF}
  1386. send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
  1387. dec(count);
  1388. until (count = 0);
  1389. end
  1390. else
  1391. if (curlen <> 0) then
  1392. begin
  1393. if (curlen <> prevlen) then
  1394. begin
  1395. {$ifdef ZLIB_DEBUG}
  1396. Tracevvv(#13'cd '+IntToStr(curlen));
  1397. {$ENDIF}
  1398. send_bits(s, s.bl_tree[curlen].fc.Code, s.bl_tree[curlen].dl.Len);
  1399. dec(count);
  1400. end;
  1401. {$IFDEF ZLIB_DEBUG}
  1402. Assert((count >= 3) and (count <= 6), ' 3_6?');
  1403. {$ENDIF}
  1404. {$ifdef ZLIB_DEBUG}
  1405. Tracevvv(#13'cd '+IntToStr(REP_3_6));
  1406. {$ENDIF}
  1407. send_bits(s, s.bl_tree[REP_3_6].fc.Code, s.bl_tree[REP_3_6].dl.Len);
  1408. send_bits(s, count-3, 2);
  1409. end
  1410. else
  1411. if (count <= 10) then
  1412. begin
  1413. {$ifdef ZLIB_DEBUG}
  1414. Tracevvv(#13'cd '+IntToStr(REPZ_3_10));
  1415. {$ENDIF}
  1416. send_bits(s, s.bl_tree[REPZ_3_10].fc.Code, s.bl_tree[REPZ_3_10].dl.Len);
  1417. send_bits(s, count-3, 3);
  1418. end
  1419. else
  1420. begin
  1421. {$ifdef ZLIB_DEBUG}
  1422. Tracevvv(#13'cd '+IntToStr(REPZ_11_138));
  1423. {$ENDIF}
  1424. send_bits(s, s.bl_tree[REPZ_11_138].fc.Code, s.bl_tree[REPZ_11_138].dl.Len);
  1425. send_bits(s, count-11, 7);
  1426. end;
  1427. count := 0;
  1428. prevlen := curlen;
  1429. if (nextlen = 0) then
  1430. begin
  1431. max_count := 138;
  1432. min_count := 3;
  1433. end
  1434. else
  1435. if (curlen = nextlen) then
  1436. begin
  1437. max_count := 6;
  1438. min_count := 3;
  1439. end
  1440. else
  1441. begin
  1442. max_count := 7;
  1443. min_count := 4;
  1444. end;
  1445. end;
  1446. end;
  1447. { ===========================================================================
  1448. Construct the Huffman tree for the bit lengths and return the index in
  1449. bl_order of the last bit length code to send. }
  1450. {local}
  1451. function build_bl_tree(var s : deflate_state) : integer;
  1452. var
  1453. max_blindex : integer; { index of last bit length code of non zero freq }
  1454. begin
  1455. { Determine the bit length frequencies for literal and distance trees }
  1456. scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
  1457. scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
  1458. { Build the bit length tree: }
  1459. build_tree(s, s.bl_desc);
  1460. { opt_len now includes the length of the tree representations, except
  1461. the lengths of the bit lengths codes and the 5+5+4 bits for the counts. }
  1462. { Determine the number of bit length codes to send. The pkzip format
  1463. requires that at least 4 bit length codes be sent. (appnote.txt says
  1464. 3 but the actual value used is 4.) }
  1465. for max_blindex := BL_CODES-1 downto 3 do
  1466. begin
  1467. if (s.bl_tree[bl_order[max_blindex]].dl.Len <> 0) then
  1468. break;
  1469. end;
  1470. { Update opt_len to include the bit length tree and counts }
  1471. inc(s.opt_len, 3*(max_blindex+1) + 5+5+4);
  1472. {$ifdef ZLIB_DEBUG}
  1473. Tracev(^M'dyn trees: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1474. {$ENDIF}
  1475. build_bl_tree := max_blindex;
  1476. end;
  1477. { ===========================================================================
  1478. Send the header for a block using dynamic Huffman trees: the counts, the
  1479. lengths of the bit length codes, the literal tree and the distance tree.
  1480. IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. }
  1481. {local}
  1482. procedure send_all_trees(var s : deflate_state;
  1483. lcodes : integer;
  1484. dcodes : integer;
  1485. blcodes : integer); { number of codes for each tree }
  1486. var
  1487. rank : integer; { index in bl_order }
  1488. begin
  1489. {$IFDEF ZLIB_DEBUG}
  1490. Assert ((lcodes >= 257) and (dcodes >= 1) and (blcodes >= 4),
  1491. 'not enough codes');
  1492. Assert ((lcodes <= L_CODES) and (dcodes <= D_CODES)
  1493. and (blcodes <= BL_CODES), 'too many codes');
  1494. Tracev(^M'bl counts: ');
  1495. {$ENDIF}
  1496. send_bits(s, lcodes-257, 5); { not +255 as stated in appnote.txt }
  1497. send_bits(s, dcodes-1, 5);
  1498. send_bits(s, blcodes-4, 4); { not -3 as stated in appnote.txt }
  1499. for rank := 0 to blcodes-1 do
  1500. begin
  1501. {$ifdef ZLIB_DEBUG}
  1502. Tracev(^M'bl code '+IntToStr(bl_order[rank]));
  1503. {$ENDIF}
  1504. send_bits(s, s.bl_tree[bl_order[rank]].dl.Len, 3);
  1505. end;
  1506. {$ifdef ZLIB_DEBUG}
  1507. Tracev(^M'bl tree: sent '+IntToStr(s.bits_sent));
  1508. {$ENDIF}
  1509. send_tree(s, s.dyn_ltree, lcodes-1); { literal tree }
  1510. {$ifdef ZLIB_DEBUG}
  1511. Tracev(^M'lit tree: sent '+IntToStr(s.bits_sent));
  1512. {$ENDIF}
  1513. send_tree(s, s.dyn_dtree, dcodes-1); { distance tree }
  1514. {$ifdef ZLIB_DEBUG}
  1515. Tracev(^M'dist tree: sent '+IntToStr(s.bits_sent));
  1516. {$ENDIF}
  1517. end;
  1518. { ===========================================================================
  1519. Flush the bit buffer and align the output on a byte boundary }
  1520. {local}
  1521. procedure bi_windup(var s : deflate_state);
  1522. begin
  1523. if (s.bi_valid > 8) then
  1524. begin
  1525. {put_short(s, s.bi_buf);}
  1526. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  1527. inc(s.pending);
  1528. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  1529. inc(s.pending);
  1530. end
  1531. else
  1532. if (s.bi_valid > 0) then
  1533. begin
  1534. {put_byte(s, (Byte)s^.bi_buf);}
  1535. s.pending_buf^[s.pending] := Byte(s.bi_buf);
  1536. inc(s.pending);
  1537. end;
  1538. s.bi_buf := 0;
  1539. s.bi_valid := 0;
  1540. {$ifdef ZLIB_DEBUG}
  1541. s.bits_sent := (s.bits_sent+7) and (not 7);
  1542. {$endif}
  1543. end;
  1544. { ===========================================================================
  1545. Copy a stored block, storing first the length and its
  1546. one's complement if requested. }
  1547. {local}
  1548. procedure copy_block(var s : deflate_state;
  1549. buf : Pchar; { the input data }
  1550. len : cardinal; { its length }
  1551. header : boolean); { true if block header must be written }
  1552. begin
  1553. bi_windup(s); { align on byte boundary }
  1554. s.last_eob_len := 8; { enough lookahead for inflate }
  1555. if (header) then
  1556. begin
  1557. {put_short(s, (word)len);}
  1558. s.pending_buf^[s.pending] := byte(word(len) and $ff);
  1559. inc(s.pending);
  1560. s.pending_buf^[s.pending] := byte(word(len) shr 8);;
  1561. inc(s.pending);
  1562. {put_short(s, (word)~len);}
  1563. s.pending_buf^[s.pending] := byte(word(not len) and $ff);
  1564. inc(s.pending);
  1565. s.pending_buf^[s.pending] := byte(word(not len) shr 8);;
  1566. inc(s.pending);
  1567. {$ifdef ZLIB_DEBUG}
  1568. inc(s.bits_sent, 2*16);
  1569. {$endif}
  1570. end;
  1571. {$ifdef ZLIB_DEBUG}
  1572. inc(s.bits_sent, longint(len shl 3));
  1573. {$endif}
  1574. while (len <> 0) do
  1575. begin
  1576. dec(len);
  1577. {put_byte(s, *buf++);}
  1578. s.pending_buf^[s.pending] := byte(buf^);
  1579. inc(buf);
  1580. inc(s.pending);
  1581. end;
  1582. end;
  1583. { ===========================================================================
  1584. Send a stored block }
  1585. procedure _tr_stored_block(var s : deflate_state;
  1586. buf : Pchar; { input block }
  1587. stored_len : longint; { length of input block }
  1588. eof : boolean); { true if this is the last block for a file }
  1589. begin
  1590. send_bits(s, (STORED_BLOCK shl 1)+ord(eof), 3); { send block type }
  1591. s.compressed_len := (s.compressed_len + 3 + 7) and longint(not cardinal(7));
  1592. inc(s.compressed_len, (stored_len + 4) shl 3);
  1593. copy_block(s, buf, cardinal(stored_len), TRUE); { with header }
  1594. end;
  1595. { ===========================================================================
  1596. Flush the bit buffer, keeping at most 7 bits in it. }
  1597. {local}
  1598. procedure bi_flush(var s : deflate_state);
  1599. begin
  1600. if (s.bi_valid = 16) then
  1601. begin
  1602. {put_short(s, s.bi_buf);}
  1603. s.pending_buf^[s.pending] := byte(s.bi_buf and $ff);
  1604. inc(s.pending);
  1605. s.pending_buf^[s.pending] := byte(word(s.bi_buf) shr 8);;
  1606. inc(s.pending);
  1607. s.bi_buf := 0;
  1608. s.bi_valid := 0;
  1609. end
  1610. else
  1611. if (s.bi_valid >= 8) then
  1612. begin
  1613. {put_byte(s, (Byte)s^.bi_buf);}
  1614. s.pending_buf^[s.pending] := Byte(s.bi_buf);
  1615. inc(s.pending);
  1616. s.bi_buf := s.bi_buf shr 8;
  1617. dec(s.bi_valid, 8);
  1618. end;
  1619. end;
  1620. { ===========================================================================
  1621. Send one empty static block to give enough lookahead for inflate.
  1622. This takes 10 bits, of which 7 may remain in the bit buffer.
  1623. The current inflate code requires 9 bits of lookahead. If the
  1624. last two codes for the previous block (real code plus EOB) were coded
  1625. on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
  1626. the last real code. In this case we send two empty static blocks instead
  1627. of one. (There are no problems if the previous block is stored or fixed.)
  1628. To simplify the code, we assume the worst case of last real code encoded
  1629. on one bit only. }
  1630. procedure _tr_align(var s : deflate_state);
  1631. begin
  1632. send_bits(s, STATIC_TREES shl 1, 3);
  1633. {$ifdef ZLIB_DEBUG}
  1634. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1635. {$ENDIF}
  1636. send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
  1637. inc(s.compressed_len, cardinal(10)); { 3 for block type, 7 for EOB }
  1638. bi_flush(s);
  1639. { Of the 10 bits for the empty block, we have already sent
  1640. (10 - bi_valid) bits. The lookahead for the last real code (before
  1641. the EOB of the previous block) was thus at least one plus the length
  1642. of the EOB plus what we have just sent of the empty static block. }
  1643. if (1 + s.last_eob_len + 10 - s.bi_valid < 9) then
  1644. begin
  1645. send_bits(s, STATIC_TREES shl 1, 3);
  1646. {$ifdef ZLIB_DEBUG}
  1647. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1648. {$ENDIF}
  1649. send_bits(s, static_ltree[END_BLOCK].fc.Code, static_ltree[END_BLOCK].dl.Len);
  1650. inc(s.compressed_len, cardinal(10));
  1651. bi_flush(s);
  1652. end;
  1653. s.last_eob_len := 7;
  1654. end;
  1655. { ===========================================================================
  1656. Set the data type to ASCII or BINARY, using a crude approximation:
  1657. binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
  1658. IN assertion: the fields freq of dyn_ltree are set and the total of all
  1659. frequencies does not exceed 64K (to fit in an integer on 16 bit machines). }
  1660. {local}
  1661. procedure set_data_type(var s : deflate_state);
  1662. var
  1663. n : integer;
  1664. ascii_freq : cardinal;
  1665. bin_freq : cardinal;
  1666. begin
  1667. n := 0;
  1668. ascii_freq := 0;
  1669. bin_freq := 0;
  1670. while (n < 7) do
  1671. begin
  1672. inc(bin_freq, s.dyn_ltree[n].fc.Freq);
  1673. inc(n);
  1674. end;
  1675. while (n < 128) do
  1676. begin
  1677. inc(ascii_freq, s.dyn_ltree[n].fc.Freq);
  1678. inc(n);
  1679. end;
  1680. while (n < LITERALS) do
  1681. begin
  1682. inc(bin_freq, s.dyn_ltree[n].fc.Freq);
  1683. inc(n);
  1684. end;
  1685. if (bin_freq > (ascii_freq shr 2)) then
  1686. s.data_type := Byte(Z_BINARY)
  1687. else
  1688. s.data_type := Byte(Z_ASCII);
  1689. end;
  1690. { ===========================================================================
  1691. Send the block data compressed using the given Huffman trees }
  1692. {local}
  1693. procedure compress_block(var s : deflate_state;
  1694. var ltree : array of ct_data; { literal tree }
  1695. var dtree : array of ct_data); { distance tree }
  1696. var
  1697. dist : cardinal; { distance of matched string }
  1698. lc : integer; { match length or unmatched char (if dist == 0) }
  1699. lx : cardinal; { running index in l_buf }
  1700. code : cardinal; { the code to send }
  1701. extra : integer; { number of extra bits to send }
  1702. begin
  1703. lx := 0;
  1704. if (s.last_lit <> 0) then
  1705. repeat
  1706. dist := s.d_buf^[lx];
  1707. lc := s.l_buf^[lx];
  1708. inc(lx);
  1709. if (dist = 0) then
  1710. begin
  1711. { send a literal byte }
  1712. {$ifdef ZLIB_DEBUG}
  1713. Tracevvv(#13'cd '+IntToStr(lc));
  1714. Tracecv((lc > 31) and (lc < 128), ' '+char(lc)+' ');
  1715. {$ENDIF}
  1716. send_bits(s, ltree[lc].fc.Code, ltree[lc].dl.Len);
  1717. end
  1718. else
  1719. begin
  1720. { Here, lc is the match length - MIN_MATCH }
  1721. code := _length_code[lc];
  1722. { send the length code }
  1723. {$ifdef ZLIB_DEBUG}
  1724. Tracevvv(#13'cd '+IntToStr(code+LITERALS+1));
  1725. {$ENDIF}
  1726. send_bits(s, ltree[code+LITERALS+1].fc.Code, ltree[code+LITERALS+1].dl.Len);
  1727. extra := extra_lbits[code];
  1728. if (extra <> 0) then
  1729. begin
  1730. dec(lc, base_length[code]);
  1731. send_bits(s, lc, extra); { send the extra length bits }
  1732. end;
  1733. dec(dist); { dist is now the match distance - 1 }
  1734. {code := d_code(dist);}
  1735. if (dist < 256) then
  1736. code := _dist_code[dist]
  1737. else
  1738. code := _dist_code[256+(dist shr 7)];
  1739. {$IFDEF ZLIB_DEBUG}
  1740. Assert (code < D_CODES, 'bad d_code');
  1741. {$ENDIF}
  1742. { send the distance code }
  1743. {$ifdef ZLIB_DEBUG}
  1744. Tracevvv(#13'cd '+IntToStr(code));
  1745. {$ENDIF}
  1746. send_bits(s, dtree[code].fc.Code, dtree[code].dl.Len);
  1747. extra := extra_dbits[code];
  1748. if (extra <> 0) then
  1749. begin
  1750. dec(dist, base_dist[code]);
  1751. send_bits(s, dist, extra); { send the extra distance bits }
  1752. end;
  1753. end; { literal or match pair ? }
  1754. { Check that the overlay between pending_buf and d_buf+l_buf is ok: }
  1755. {$IFDEF ZLIB_DEBUG}
  1756. Assert(s.pending < s.lit_bufsize + 2*lx, 'pendingBuf overflow');
  1757. {$ENDIF}
  1758. until (lx >= s.last_lit);
  1759. {$ifdef ZLIB_DEBUG}
  1760. Tracevvv(#13'cd '+IntToStr(END_BLOCK));
  1761. {$ENDIF}
  1762. send_bits(s, ltree[END_BLOCK].fc.Code, ltree[END_BLOCK].dl.Len);
  1763. s.last_eob_len := ltree[END_BLOCK].dl.Len;
  1764. end;
  1765. { ===========================================================================
  1766. Determine the best encoding for the current block: dynamic trees, static
  1767. trees or store, and output the encoded block to the zip file. This function
  1768. returns the total compressed length for the file so far. }
  1769. function _tr_flush_block (var s : deflate_state;
  1770. buf : Pchar; { input block, or NULL if too old }
  1771. stored_len : longint; { length of input block }
  1772. eof : boolean) : longint; { true if this is the last block for a file }
  1773. var
  1774. opt_lenb, static_lenb : longint; { opt_len and static_len in bytes }
  1775. max_blindex : integer; { index of last bit length code of non zero freq }
  1776. begin
  1777. max_blindex := 0;
  1778. { Build the Huffman trees unless a stored block is forced }
  1779. if (s.level > 0) then
  1780. begin
  1781. { Check if the file is ascii or binary }
  1782. if (s.data_type = Z_UNKNOWN) then
  1783. set_data_type(s);
  1784. { Construct the literal and distance trees }
  1785. build_tree(s, s.l_desc);
  1786. {$ifdef ZLIB_DEBUG}
  1787. Tracev(^M'lit data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1788. {$ENDIF}
  1789. build_tree(s, s.d_desc);
  1790. {$ifdef ZLIB_DEBUG}
  1791. Tracev(^M'dist data: dyn %ld, stat %ld {s.opt_len, s.static_len}');
  1792. {$ENDIF}
  1793. { At this point, opt_len and static_len are the total bit lengths of
  1794. the compressed block data, excluding the tree representations. }
  1795. { Build the bit length tree for the above two trees, and get the index
  1796. in bl_order of the last bit length code to send. }
  1797. max_blindex := build_bl_tree(s);
  1798. { Determine the best encoding. Compute first the block length in bytes}
  1799. opt_lenb := (s.opt_len+3+7) shr 3;
  1800. static_lenb := (s.static_len+3+7) shr 3;
  1801. {$ifdef ZLIB_DEBUG}
  1802. Tracev(^M'opt %lu(%lu) stat %lu(%lu) stored %lu lit %u '+
  1803. '{opt_lenb, s.opt_len, static_lenb, s.static_len, stored_len,'+
  1804. 's.last_lit}');
  1805. {$ENDIF}
  1806. if (static_lenb <= opt_lenb) then
  1807. opt_lenb := static_lenb;
  1808. end
  1809. else
  1810. begin
  1811. {$IFDEF ZLIB_DEBUG}
  1812. Assert(buf <> nil, 'lost buf');
  1813. {$ENDIF}
  1814. static_lenb := stored_len + 5;
  1815. opt_lenb := static_lenb; { force a stored block }
  1816. end;
  1817. { If compression failed and this is the first and last block,
  1818. and if the .zip file can be seeked (to rewrite the local header),
  1819. the whole file is transformed into a stored file: }
  1820. {$ifdef STORED_FILE_OK}
  1821. {$ifdef FORCE_STORED_FILE}
  1822. if eof and (s.compressed_len = 0) then
  1823. begin { force stored file }
  1824. {$else}
  1825. if (stored_len <= opt_lenb) and eof and (s.compressed_len=cardinal(0))
  1826. and seekable()) do
  1827. begin
  1828. {$endif}
  1829. { Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: }
  1830. if (buf = Pchar(0)) then
  1831. error ('block vanished');
  1832. copy_block(buf, cardinal(stored_len), 0); { without header }
  1833. s.compressed_len := stored_len shl 3;
  1834. s.method := STORED;
  1835. end
  1836. else
  1837. {$endif} { STORED_FILE_OK }
  1838. {$ifdef FORCE_STORED}
  1839. if (buf <> pchar(0)) then
  1840. begin { force stored block }
  1841. {$else}
  1842. if (stored_len+4 <= opt_lenb) and (buf <> nil) then
  1843. begin
  1844. { 4: two words for the lengths }
  1845. {$endif}
  1846. { The test buf <> NULL is only necessary if LIT_BUFSIZE > WSIZE.
  1847. Otherwise we can't have processed more than WSIZE input bytes since
  1848. the last block flush, because compression would have been
  1849. successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
  1850. transform a block into a stored block. }
  1851. _tr_stored_block(s, buf, stored_len, eof);
  1852. {$ifdef FORCE_STATIC}
  1853. end
  1854. else
  1855. if (static_lenb >= 0) then
  1856. begin { force static trees }
  1857. {$else}
  1858. end
  1859. else
  1860. if (static_lenb = opt_lenb) then
  1861. begin
  1862. {$endif}
  1863. send_bits(s, (STATIC_TREES shl 1)+ord(eof), 3);
  1864. compress_block(s, static_ltree, static_dtree);
  1865. inc(s.compressed_len, 3 + s.static_len);
  1866. end
  1867. else
  1868. begin
  1869. send_bits(s, (DYN_TREES shl 1)+ord(eof), 3);
  1870. send_all_trees(s, s.l_desc.max_code+1, s.d_desc.max_code+1,
  1871. max_blindex+1);
  1872. compress_block(s, s.dyn_ltree, s.dyn_dtree);
  1873. inc(s.compressed_len, 3 + s.opt_len);
  1874. end;
  1875. {$ifdef ZLIB_DEBUG}
  1876. Assert (s.compressed_len = s.bits_sent, 'bad compressed size');
  1877. {$ENDIF}
  1878. init_block(s);
  1879. if (eof) then
  1880. begin
  1881. bi_windup(s);
  1882. inc(s.compressed_len, 7); { align on byte boundary }
  1883. end;
  1884. {$ifdef ZLIB_DEBUG}
  1885. Tracev(#13'comprlen %lu(%lu) {s.compressed_len shr 3,'+
  1886. 's.compressed_len-7*ord(eof)}');
  1887. {$ENDIF}
  1888. _tr_flush_block := s.compressed_len shr 3;
  1889. end;
  1890. { ===========================================================================
  1891. Save the match info and tally the frequency counts. Return true if
  1892. the current block must be flushed. }
  1893. function _tr_tally (var s : deflate_state;
  1894. dist : cardinal; { distance of matched string }
  1895. lc : cardinal) : boolean; { match length-MIN_MATCH or unmatched char (if dist=0) }
  1896. var
  1897. {$IFDEF ZLIB_DEBUG}
  1898. MAX_DIST : word;
  1899. {$ENDIF}
  1900. code : word;
  1901. {$ifdef TRUNCATE_BLOCK}
  1902. var
  1903. out_length : longint;
  1904. in_length : longint;
  1905. dcode : integer;
  1906. {$endif}
  1907. begin
  1908. s.d_buf^[s.last_lit] := word(dist);
  1909. s.l_buf^[s.last_lit] := byte(lc);
  1910. inc(s.last_lit);
  1911. if (dist = 0) then
  1912. begin
  1913. { lc is the unmatched char }
  1914. inc(s.dyn_ltree[lc].fc.Freq);
  1915. end
  1916. else
  1917. begin
  1918. inc(s.matches);
  1919. { Here, lc is the match length - MIN_MATCH }
  1920. dec(dist); { dist := match distance - 1 }
  1921. {macro d_code(dist)}
  1922. if (dist) < 256 then
  1923. code := _dist_code[dist]
  1924. else
  1925. code := _dist_code[256+(dist shr 7)];
  1926. {$IFDEF ZLIB_DEBUG}
  1927. {macro MAX_DIST(s) <=> ((s)^.w_size-MIN_LOOKAHEAD)
  1928. In order to simplify the code, particularly on 16 bit machines, match
  1929. distances are limited to MAX_DIST instead of WSIZE. }
  1930. MAX_DIST := word(s.w_size-MIN_LOOKAHEAD);
  1931. Assert((dist < word(MAX_DIST)) and
  1932. (word(lc) <= word(MAX_MATCH-MIN_MATCH)) and
  1933. (word(code) < word(D_CODES)), '_tr_tally: bad match');
  1934. {$ENDIF}
  1935. inc(s.dyn_ltree[_length_code[lc]+LITERALS+1].fc.Freq);
  1936. {s.dyn_dtree[d_code(dist)].Freq++;}
  1937. inc(s.dyn_dtree[code].fc.Freq);
  1938. end;
  1939. {$ifdef TRUNCATE_BLOCK}
  1940. { Try to guess if it is profitable to stop the current block here }
  1941. if (s.last_lit and $1fff = 0) and (s.level > 2) then
  1942. begin
  1943. { Compute an upper bound for the compressed length }
  1944. out_length := longint(s.last_lit)*cardinal(8);
  1945. in_length := longint(cardinal(s.strstart) - s.block_start);
  1946. for dcode := 0 to D_CODES-1 do
  1947. begin
  1948. inc(out_length, longint(s.dyn_dtree[dcode].fc.Freq *
  1949. (cardinal(5)+extra_dbits[dcode])) );
  1950. end;
  1951. out_length := out_length shr 3;
  1952. {$ifdef ZLIB_DEBUG}
  1953. Tracev(^M'last_lit %u, in %ld, out ~%ld(%ld%%) ');
  1954. { s.last_lit, in_length, out_length,
  1955. cardinal(100) - out_length*100 div in_length)); }
  1956. {$ENDIF}
  1957. if (s.matches < s.last_lit div 2) and (out_length < in_length div 2) then
  1958. begin
  1959. _tr_tally := TRUE;
  1960. exit;
  1961. end;
  1962. end;
  1963. {$endif}
  1964. _tr_tally := (s.last_lit = s.lit_bufsize-1);
  1965. { We avoid equality with lit_bufsize because of wraparound at 64K
  1966. on 16 bit machines and because stored blocks are restricted to
  1967. 64K-1 bytes. }
  1968. end;
  1969. end.