trees.pas 76 KB

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