deflate.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. /* deflate.c -- compress data using the deflation algorithm
  2. * Copyright (C) 1995-2005 Jean-loup Gailly.
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /*
  6. * ALGORITHM
  7. *
  8. * The "deflation" process depends on being able to identify portions
  9. * of the input text which are identical to earlier input (within a
  10. * sliding window trailing behind the input currently being processed).
  11. *
  12. * The most straightforward technique turns out to be the fastest for
  13. * most input files: try all possible matches and select the longest.
  14. * The key feature of this algorithm is that insertions into the string
  15. * dictionary are very simple and thus fast, and deletions are avoided
  16. * completely. Insertions are performed at each input character, whereas
  17. * string matches are performed only when the previous match ends. So it
  18. * is preferable to spend more time in matches to allow very fast string
  19. * insertions and avoid deletions. The matching algorithm for small
  20. * strings is inspired from that of Rabin & Karp. A brute force approach
  21. * is used to find longer strings when a small match has been found.
  22. * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  23. * (by Leonid Broukhis).
  24. * A previous version of this file used a more sophisticated algorithm
  25. * (by Fiala and Greene) which is guaranteed to run in linear amortized
  26. * time, but has a larger average cost, uses more memory and is patented.
  27. * However the F&G algorithm may be faster for some highly redundant
  28. * files if the parameter max_chain_length (described below) is too large.
  29. *
  30. * ACKNOWLEDGEMENTS
  31. *
  32. * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  33. * I found it in 'freeze' written by Leonid Broukhis.
  34. * Thanks to many people for bug reports and testing.
  35. *
  36. * REFERENCES
  37. *
  38. * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  39. * Available in http://www.ietf.org/rfc/rfc1951.txt
  40. *
  41. * A description of the Rabin and Karp algorithm is given in the book
  42. * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  43. *
  44. * Fiala,E.R., and Greene,D.H.
  45. * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  46. *
  47. */
  48. /* @(#) $Id: deflate.c,v 1.1 2007/06/07 17:54:37 jules_rms Exp $ */
  49. #include "deflate.h"
  50. const char deflate_copyright[] =
  51. " deflate 1.2.3 Copyright 1995-2005 Jean-loup Gailly ";
  52. /*
  53. If you use the zlib library in a product, an acknowledgment is welcome
  54. in the documentation of your product. If for some reason you cannot
  55. include such an acknowledgment, I would appreciate that you keep this
  56. copyright string in the executable of your product.
  57. */
  58. /* ===========================================================================
  59. * Function prototypes.
  60. */
  61. typedef enum {
  62. need_more, /* block not completed, need more input or more output */
  63. block_done, /* block flush performed */
  64. finish_started, /* finish started, need only more output at next deflate */
  65. finish_done /* finish done, accept no more input or output */
  66. } block_state;
  67. typedef block_state (*compress_func) OF((deflate_state *s, int flush));
  68. /* Compression function. Returns the block state after the call. */
  69. local void fill_window OF((deflate_state *s));
  70. local block_state deflate_stored OF((deflate_state *s, int flush));
  71. local block_state deflate_fast OF((deflate_state *s, int flush));
  72. #ifndef FASTEST
  73. local block_state deflate_slow OF((deflate_state *s, int flush));
  74. #endif
  75. local void lm_init OF((deflate_state *s));
  76. local void putShortMSB OF((deflate_state *s, uInt b));
  77. local void flush_pending OF((z_streamp strm));
  78. local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
  79. #ifndef FASTEST
  80. #ifdef ASMV
  81. void match_init OF((void)); /* asm code initialization */
  82. uInt longest_match OF((deflate_state *s, IPos cur_match));
  83. #else
  84. local uInt longest_match OF((deflate_state *s, IPos cur_match));
  85. #endif
  86. #endif
  87. local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
  88. #ifdef DEBUG
  89. local void check_match OF((deflate_state *s, IPos start, IPos match,
  90. int length));
  91. #endif
  92. /* ===========================================================================
  93. * Local data
  94. */
  95. #define NIL 0
  96. /* Tail of hash chains */
  97. #ifndef TOO_FAR
  98. # define TOO_FAR 4096
  99. #endif
  100. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  101. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  102. /* Minimum amount of lookahead, except at the end of the input file.
  103. * See deflate.c for comments about the MIN_MATCH+1.
  104. */
  105. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  106. * the desired pack level (0..9). The values given below have been tuned to
  107. * exclude worst case performance for pathological files. Better values may be
  108. * found for specific files.
  109. */
  110. typedef struct config_s {
  111. ush good_length; /* reduce lazy search above this match length */
  112. ush max_lazy; /* do not perform lazy search above this match length */
  113. ush nice_length; /* quit search above this match length */
  114. ush max_chain;
  115. compress_func func;
  116. } config;
  117. #ifdef FASTEST
  118. local const config configuration_table[2] = {
  119. /* good lazy nice chain */
  120. /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  121. /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
  122. #else
  123. local const config configuration_table[10] = {
  124. /* good lazy nice chain */
  125. /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  126. /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
  127. /* 2 */ {4, 5, 16, 8, deflate_fast},
  128. /* 3 */ {4, 6, 32, 32, deflate_fast},
  129. /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
  130. /* 5 */ {8, 16, 32, 32, deflate_slow},
  131. /* 6 */ {8, 16, 128, 128, deflate_slow},
  132. /* 7 */ {8, 32, 128, 256, deflate_slow},
  133. /* 8 */ {32, 128, 258, 1024, deflate_slow},
  134. /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
  135. #endif
  136. /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  137. * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  138. * meaning.
  139. */
  140. #define EQUAL 0
  141. /* result of memcmp for equal strings */
  142. #ifndef NO_DUMMY_DECL
  143. struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
  144. #endif
  145. /* ===========================================================================
  146. * Update a hash value with the given input byte
  147. * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  148. * input characters, so that a running hash key can be computed from the
  149. * previous key instead of complete recalculation each time.
  150. */
  151. #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  152. /* ===========================================================================
  153. * Insert string str in the dictionary and set match_head to the previous head
  154. * of the hash chain (the most recent string with same hash key). Return
  155. * the previous length of the hash chain.
  156. * If this file is compiled with -DFASTEST, the compression level is forced
  157. * to 1, and no hash chains are maintained.
  158. * IN assertion: all calls to to INSERT_STRING are made with consecutive
  159. * input characters and the first MIN_MATCH bytes of str are valid
  160. * (except for the last MIN_MATCH-1 bytes of the input file).
  161. */
  162. #ifdef FASTEST
  163. #define INSERT_STRING(s, str, match_head) \
  164. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  165. match_head = s->head[s->ins_h], \
  166. s->head[s->ins_h] = (Pos)(str))
  167. #else
  168. #define INSERT_STRING(s, str, match_head) \
  169. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  170. match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
  171. s->head[s->ins_h] = (Pos)(str))
  172. #endif
  173. /* ===========================================================================
  174. * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  175. * prev[] will be initialized on the fly.
  176. */
  177. #define CLEAR_HASH(s) \
  178. s->head[s->hash_size-1] = NIL; \
  179. zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  180. /* ========================================================================= */
  181. int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version, int stream_size)
  182. {
  183. return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
  184. Z_DEFAULT_STRATEGY, version, stream_size);
  185. /* To do: ignore strm->next_in if we use it as window */
  186. }
  187. /* ========================================================================= */
  188. int ZEXPORT deflateInit2_ (z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size)
  189. {
  190. deflate_state *s;
  191. int wrap = 1;
  192. static const char my_version[] = ZLIB_VERSION;
  193. ushf *overlay;
  194. /* We overlay pending_buf and d_buf+l_buf. This works since the average
  195. * output size for (length,distance) codes is <= 24 bits.
  196. */
  197. if (version == Z_NULL || version[0] != my_version[0] ||
  198. stream_size != sizeof(z_stream)) {
  199. return Z_VERSION_ERROR;
  200. }
  201. if (strm == Z_NULL) return Z_STREAM_ERROR;
  202. strm->msg = Z_NULL;
  203. if (strm->zalloc == (alloc_func)0) {
  204. strm->zalloc = zcalloc;
  205. strm->opaque = (voidpf)0;
  206. }
  207. if (strm->zfree == (free_func)0) strm->zfree = zcfree;
  208. #ifdef FASTEST
  209. if (level != 0) level = 1;
  210. #else
  211. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  212. #endif
  213. if (windowBits < 0) { /* suppress zlib wrapper */
  214. wrap = 0;
  215. windowBits = -windowBits;
  216. }
  217. #ifdef GZIP
  218. else if (windowBits > 15) {
  219. wrap = 2; /* write gzip wrapper instead */
  220. windowBits -= 16;
  221. }
  222. #endif
  223. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  224. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  225. strategy < 0 || strategy > Z_FIXED) {
  226. return Z_STREAM_ERROR;
  227. }
  228. if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
  229. s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  230. if (s == Z_NULL) return Z_MEM_ERROR;
  231. strm->state = (struct internal_state FAR *)s;
  232. s->strm = strm;
  233. s->wrap = wrap;
  234. s->gzhead = Z_NULL;
  235. s->w_bits = windowBits;
  236. s->w_size = 1 << s->w_bits;
  237. s->w_mask = s->w_size - 1;
  238. s->hash_bits = memLevel + 7;
  239. s->hash_size = 1 << s->hash_bits;
  240. s->hash_mask = s->hash_size - 1;
  241. s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  242. s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  243. s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
  244. s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
  245. s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  246. overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  247. s->pending_buf = (uchf *) overlay;
  248. s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  249. if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  250. s->pending_buf == Z_NULL) {
  251. s->status = FINISH_STATE;
  252. strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
  253. deflateEnd (strm);
  254. return Z_MEM_ERROR;
  255. }
  256. s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  257. s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  258. s->level = level;
  259. s->strategy = strategy;
  260. s->method = (Byte)method;
  261. return deflateReset(strm);
  262. }
  263. /* ========================================================================= */
  264. int ZEXPORT deflateSetDictionary (z_streamp strm, const Bytef *dictionary, uInt dictLength)
  265. {
  266. deflate_state *s;
  267. uInt length = dictLength;
  268. uInt n;
  269. IPos hash_head = 0;
  270. if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
  271. strm->state->wrap == 2 ||
  272. (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
  273. return Z_STREAM_ERROR;
  274. s = strm->state;
  275. if (s->wrap)
  276. strm->adler = adler32(strm->adler, dictionary, dictLength);
  277. if (length < MIN_MATCH) return Z_OK;
  278. if (length > MAX_DIST(s)) {
  279. length = MAX_DIST(s);
  280. dictionary += dictLength - length; /* use the tail of the dictionary */
  281. }
  282. zmemcpy(s->window, dictionary, length);
  283. s->strstart = length;
  284. s->block_start = (long)length;
  285. /* Insert all strings in the hash table (except for the last two bytes).
  286. * s->lookahead stays null, so s->ins_h will be recomputed at the next
  287. * call of fill_window.
  288. */
  289. s->ins_h = s->window[0];
  290. UPDATE_HASH(s, s->ins_h, s->window[1]);
  291. for (n = 0; n <= length - MIN_MATCH; n++) {
  292. INSERT_STRING(s, n, hash_head);
  293. }
  294. (void) hash_head; /* to make compiler happy */
  295. return Z_OK;
  296. }
  297. /* ========================================================================= */
  298. int ZEXPORT deflateReset (z_streamp strm)
  299. {
  300. deflate_state *s;
  301. if (strm == Z_NULL || strm->state == Z_NULL ||
  302. strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
  303. return Z_STREAM_ERROR;
  304. }
  305. strm->total_in = strm->total_out = 0;
  306. strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  307. strm->data_type = Z_UNKNOWN;
  308. s = (deflate_state *)strm->state;
  309. s->pending = 0;
  310. s->pending_out = s->pending_buf;
  311. if (s->wrap < 0) {
  312. s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
  313. }
  314. s->status = s->wrap ? INIT_STATE : BUSY_STATE;
  315. strm->adler =
  316. #ifdef GZIP
  317. s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
  318. #endif
  319. adler32(0L, Z_NULL, 0);
  320. s->last_flush = Z_NO_FLUSH;
  321. _tr_init(s);
  322. lm_init(s);
  323. return Z_OK;
  324. }
  325. /* ========================================================================= */
  326. int ZEXPORT deflateSetHeader (z_streamp strm, gz_headerp head)
  327. {
  328. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  329. if (strm->state->wrap != 2) return Z_STREAM_ERROR;
  330. strm->state->gzhead = head;
  331. return Z_OK;
  332. }
  333. /* ========================================================================= */
  334. int ZEXPORT deflatePrime (z_streamp strm, int bits, int value)
  335. {
  336. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  337. strm->state->bi_valid = bits;
  338. strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
  339. return Z_OK;
  340. }
  341. /* ========================================================================= */
  342. int ZEXPORT deflateParams (z_streamp strm, int level, int strategy)
  343. {
  344. deflate_state *s;
  345. compress_func func;
  346. int err = Z_OK;
  347. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  348. s = strm->state;
  349. #ifdef FASTEST
  350. if (level != 0) level = 1;
  351. #else
  352. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  353. #endif
  354. if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
  355. return Z_STREAM_ERROR;
  356. }
  357. func = configuration_table[s->level].func;
  358. if (func != configuration_table[level].func && strm->total_in != 0) {
  359. /* Flush the last buffer: */
  360. err = deflate(strm, Z_PARTIAL_FLUSH);
  361. }
  362. if (s->level != level) {
  363. s->level = level;
  364. s->max_lazy_match = configuration_table[level].max_lazy;
  365. s->good_match = configuration_table[level].good_length;
  366. s->nice_match = configuration_table[level].nice_length;
  367. s->max_chain_length = configuration_table[level].max_chain;
  368. }
  369. s->strategy = strategy;
  370. return err;
  371. }
  372. /* ========================================================================= */
  373. int ZEXPORT deflateTune (z_streamp strm, int good_length, int max_lazy, int nice_length, int max_chain)
  374. {
  375. deflate_state *s;
  376. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  377. s = strm->state;
  378. s->good_match = good_length;
  379. s->max_lazy_match = max_lazy;
  380. s->nice_match = nice_length;
  381. s->max_chain_length = max_chain;
  382. return Z_OK;
  383. }
  384. /* =========================================================================
  385. * For the default windowBits of 15 and memLevel of 8, this function returns
  386. * a close to exact, as well as small, upper bound on the compressed size.
  387. * They are coded as constants here for a reason--if the #define's are
  388. * changed, then this function needs to be changed as well. The return
  389. * value for 15 and 8 only works for those exact settings.
  390. *
  391. * For any setting other than those defaults for windowBits and memLevel,
  392. * the value returned is a conservative worst case for the maximum expansion
  393. * resulting from using fixed blocks instead of stored blocks, which deflate
  394. * can emit on compressed data for some combinations of the parameters.
  395. *
  396. * This function could be more sophisticated to provide closer upper bounds
  397. * for every combination of windowBits and memLevel, as well as wrap.
  398. * But even the conservative upper bound of about 14% expansion does not
  399. * seem onerous for output buffer allocation.
  400. */
  401. uLong ZEXPORT deflateBound (z_streamp strm, uLong sourceLen)
  402. {
  403. deflate_state *s;
  404. uLong destLen;
  405. /* conservative upper bound */
  406. destLen = sourceLen +
  407. ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
  408. /* if can't get parameters, return conservative bound */
  409. if (strm == Z_NULL || strm->state == Z_NULL)
  410. return destLen;
  411. /* if not default parameters, return conservative bound */
  412. s = strm->state;
  413. if (s->w_bits != 15 || s->hash_bits != 8 + 7)
  414. return destLen;
  415. /* default settings: return tight bound for that case */
  416. return compressBound(sourceLen);
  417. }
  418. /* =========================================================================
  419. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  420. * IN assertion: the stream state is correct and there is enough room in
  421. * pending_buf.
  422. */
  423. local void putShortMSB (deflate_state *s, uInt b)
  424. {
  425. put_byte(s, (Byte)(b >> 8));
  426. put_byte(s, (Byte)(b & 0xff));
  427. }
  428. /* =========================================================================
  429. * Flush as much pending output as possible. All deflate() output goes
  430. * through this function so some applications may wish to modify it
  431. * to avoid allocating a large strm->next_out buffer and copying into it.
  432. * (See also read_buf()).
  433. */
  434. local void flush_pending (z_streamp strm)
  435. {
  436. unsigned len = strm->state->pending;
  437. if (len > strm->avail_out) len = strm->avail_out;
  438. if (len == 0) return;
  439. zmemcpy(strm->next_out, strm->state->pending_out, len);
  440. strm->next_out += len;
  441. strm->state->pending_out += len;
  442. strm->total_out += len;
  443. strm->avail_out -= len;
  444. strm->state->pending -= len;
  445. if (strm->state->pending == 0) {
  446. strm->state->pending_out = strm->state->pending_buf;
  447. }
  448. }
  449. /* ========================================================================= */
  450. int ZEXPORT deflate (z_streamp strm, int flush)
  451. {
  452. int old_flush; /* value of flush param for previous deflate call */
  453. deflate_state *s;
  454. if (strm == Z_NULL || strm->state == Z_NULL ||
  455. flush > Z_FINISH || flush < 0) {
  456. return Z_STREAM_ERROR;
  457. }
  458. s = strm->state;
  459. if (strm->next_out == Z_NULL ||
  460. (strm->next_in == Z_NULL && strm->avail_in != 0) ||
  461. (s->status == FINISH_STATE && flush != Z_FINISH)) {
  462. ERR_RETURN(strm, Z_STREAM_ERROR);
  463. }
  464. if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  465. s->strm = strm; /* just in case */
  466. old_flush = s->last_flush;
  467. s->last_flush = flush;
  468. /* Write the header */
  469. if (s->status == INIT_STATE) {
  470. #ifdef GZIP
  471. if (s->wrap == 2) {
  472. strm->adler = crc32(0L, Z_NULL, 0);
  473. put_byte(s, 31);
  474. put_byte(s, 139);
  475. put_byte(s, 8);
  476. if (s->gzhead == NULL) {
  477. put_byte(s, 0);
  478. put_byte(s, 0);
  479. put_byte(s, 0);
  480. put_byte(s, 0);
  481. put_byte(s, 0);
  482. put_byte(s, s->level == 9 ? 2 :
  483. (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
  484. 4 : 0));
  485. put_byte(s, OS_CODE);
  486. s->status = BUSY_STATE;
  487. }
  488. else {
  489. put_byte(s, (s->gzhead->text ? 1 : 0) +
  490. (s->gzhead->hcrc ? 2 : 0) +
  491. (s->gzhead->extra == Z_NULL ? 0 : 4) +
  492. (s->gzhead->name == Z_NULL ? 0 : 8) +
  493. (s->gzhead->comment == Z_NULL ? 0 : 16)
  494. );
  495. put_byte(s, (Byte)(s->gzhead->time & 0xff));
  496. put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
  497. put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
  498. put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
  499. put_byte(s, s->level == 9 ? 2 :
  500. (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
  501. 4 : 0));
  502. put_byte(s, s->gzhead->os & 0xff);
  503. if (s->gzhead->extra != NULL) {
  504. put_byte(s, s->gzhead->extra_len & 0xff);
  505. put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
  506. }
  507. if (s->gzhead->hcrc)
  508. strm->adler = crc32(strm->adler, s->pending_buf,
  509. s->pending);
  510. s->gzindex = 0;
  511. s->status = EXTRA_STATE;
  512. }
  513. }
  514. else
  515. #endif
  516. {
  517. uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  518. uInt level_flags;
  519. if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
  520. level_flags = 0;
  521. else if (s->level < 6)
  522. level_flags = 1;
  523. else if (s->level == 6)
  524. level_flags = 2;
  525. else
  526. level_flags = 3;
  527. header |= (level_flags << 6);
  528. if (s->strstart != 0) header |= PRESET_DICT;
  529. header += 31 - (header % 31);
  530. s->status = BUSY_STATE;
  531. putShortMSB(s, header);
  532. /* Save the adler32 of the preset dictionary: */
  533. if (s->strstart != 0) {
  534. putShortMSB(s, (uInt)(strm->adler >> 16));
  535. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  536. }
  537. strm->adler = adler32(0L, Z_NULL, 0);
  538. }
  539. }
  540. #ifdef GZIP
  541. if (s->status == EXTRA_STATE) {
  542. if (s->gzhead->extra != NULL) {
  543. uInt beg = s->pending; /* start of bytes to update crc */
  544. while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
  545. if (s->pending == s->pending_buf_size) {
  546. if (s->gzhead->hcrc && s->pending > beg)
  547. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  548. s->pending - beg);
  549. flush_pending(strm);
  550. beg = s->pending;
  551. if (s->pending == s->pending_buf_size)
  552. break;
  553. }
  554. put_byte(s, s->gzhead->extra[s->gzindex]);
  555. s->gzindex++;
  556. }
  557. if (s->gzhead->hcrc && s->pending > beg)
  558. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  559. s->pending - beg);
  560. if (s->gzindex == s->gzhead->extra_len) {
  561. s->gzindex = 0;
  562. s->status = NAME_STATE;
  563. }
  564. }
  565. else
  566. s->status = NAME_STATE;
  567. }
  568. if (s->status == NAME_STATE) {
  569. if (s->gzhead->name != NULL) {
  570. uInt beg = s->pending; /* start of bytes to update crc */
  571. int val;
  572. do {
  573. if (s->pending == s->pending_buf_size) {
  574. if (s->gzhead->hcrc && s->pending > beg)
  575. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  576. s->pending - beg);
  577. flush_pending(strm);
  578. beg = s->pending;
  579. if (s->pending == s->pending_buf_size) {
  580. val = 1;
  581. break;
  582. }
  583. }
  584. val = s->gzhead->name[s->gzindex++];
  585. put_byte(s, val);
  586. } while (val != 0);
  587. if (s->gzhead->hcrc && s->pending > beg)
  588. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  589. s->pending - beg);
  590. if (val == 0) {
  591. s->gzindex = 0;
  592. s->status = COMMENT_STATE;
  593. }
  594. }
  595. else
  596. s->status = COMMENT_STATE;
  597. }
  598. if (s->status == COMMENT_STATE) {
  599. if (s->gzhead->comment != NULL) {
  600. uInt beg = s->pending; /* start of bytes to update crc */
  601. int val;
  602. do {
  603. if (s->pending == s->pending_buf_size) {
  604. if (s->gzhead->hcrc && s->pending > beg)
  605. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  606. s->pending - beg);
  607. flush_pending(strm);
  608. beg = s->pending;
  609. if (s->pending == s->pending_buf_size) {
  610. val = 1;
  611. break;
  612. }
  613. }
  614. val = s->gzhead->comment[s->gzindex++];
  615. put_byte(s, val);
  616. } while (val != 0);
  617. if (s->gzhead->hcrc && s->pending > beg)
  618. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  619. s->pending - beg);
  620. if (val == 0)
  621. s->status = HCRC_STATE;
  622. }
  623. else
  624. s->status = HCRC_STATE;
  625. }
  626. if (s->status == HCRC_STATE) {
  627. if (s->gzhead->hcrc) {
  628. if (s->pending + 2 > s->pending_buf_size)
  629. flush_pending(strm);
  630. if (s->pending + 2 <= s->pending_buf_size) {
  631. put_byte(s, (Byte)(strm->adler & 0xff));
  632. put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
  633. strm->adler = crc32(0L, Z_NULL, 0);
  634. s->status = BUSY_STATE;
  635. }
  636. }
  637. else
  638. s->status = BUSY_STATE;
  639. }
  640. #endif
  641. /* Flush as much pending output as possible */
  642. if (s->pending != 0) {
  643. flush_pending(strm);
  644. if (strm->avail_out == 0) {
  645. /* Since avail_out is 0, deflate will be called again with
  646. * more output space, but possibly with both pending and
  647. * avail_in equal to zero. There won't be anything to do,
  648. * but this is not an error situation so make sure we
  649. * return OK instead of BUF_ERROR at next call of deflate:
  650. */
  651. s->last_flush = -1;
  652. return Z_OK;
  653. }
  654. /* Make sure there is something to do and avoid duplicate consecutive
  655. * flushes. For repeated and useless calls with Z_FINISH, we keep
  656. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  657. */
  658. } else if (strm->avail_in == 0 && flush <= old_flush &&
  659. flush != Z_FINISH) {
  660. ERR_RETURN(strm, Z_BUF_ERROR);
  661. }
  662. /* User must not provide more input after the first FINISH: */
  663. if (s->status == FINISH_STATE && strm->avail_in != 0) {
  664. ERR_RETURN(strm, Z_BUF_ERROR);
  665. }
  666. /* Start a new block or continue the current one.
  667. */
  668. if (strm->avail_in != 0 || s->lookahead != 0 ||
  669. (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  670. block_state bstate;
  671. bstate = (*(configuration_table[s->level].func))(s, flush);
  672. if (bstate == finish_started || bstate == finish_done) {
  673. s->status = FINISH_STATE;
  674. }
  675. if (bstate == need_more || bstate == finish_started) {
  676. if (strm->avail_out == 0) {
  677. s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  678. }
  679. return Z_OK;
  680. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  681. * of deflate should use the same flush parameter to make sure
  682. * that the flush is complete. So we don't have to output an
  683. * empty block here, this will be done at next call. This also
  684. * ensures that for a very small output buffer, we emit at most
  685. * one empty block.
  686. */
  687. }
  688. if (bstate == block_done) {
  689. if (flush == Z_PARTIAL_FLUSH) {
  690. _tr_align(s);
  691. } else { /* FULL_FLUSH or SYNC_FLUSH */
  692. _tr_stored_block(s, (char*)0, 0L, 0);
  693. /* For a full flush, this empty block will be recognized
  694. * as a special marker by inflate_sync().
  695. */
  696. if (flush == Z_FULL_FLUSH) {
  697. CLEAR_HASH(s); /* forget history */
  698. }
  699. }
  700. flush_pending(strm);
  701. if (strm->avail_out == 0) {
  702. s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  703. return Z_OK;
  704. }
  705. }
  706. }
  707. Assert(strm->avail_out > 0, "bug2");
  708. if (flush != Z_FINISH) return Z_OK;
  709. if (s->wrap <= 0) return Z_STREAM_END;
  710. /* Write the trailer */
  711. #ifdef GZIP
  712. if (s->wrap == 2) {
  713. put_byte(s, (Byte)(strm->adler & 0xff));
  714. put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
  715. put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
  716. put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
  717. put_byte(s, (Byte)(strm->total_in & 0xff));
  718. put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
  719. put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
  720. put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
  721. }
  722. else
  723. #endif
  724. {
  725. putShortMSB(s, (uInt)(strm->adler >> 16));
  726. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  727. }
  728. flush_pending(strm);
  729. /* If avail_out is zero, the application will call deflate again
  730. * to flush the rest.
  731. */
  732. if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
  733. return s->pending != 0 ? Z_OK : Z_STREAM_END;
  734. }
  735. /* ========================================================================= */
  736. int ZEXPORT deflateEnd (z_streamp strm)
  737. {
  738. int status;
  739. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  740. status = strm->state->status;
  741. if (status != INIT_STATE &&
  742. status != EXTRA_STATE &&
  743. status != NAME_STATE &&
  744. status != COMMENT_STATE &&
  745. status != HCRC_STATE &&
  746. status != BUSY_STATE &&
  747. status != FINISH_STATE) {
  748. return Z_STREAM_ERROR;
  749. }
  750. /* Deallocate in reverse order of allocations: */
  751. TRY_FREE(strm, strm->state->pending_buf);
  752. TRY_FREE(strm, strm->state->head);
  753. TRY_FREE(strm, strm->state->prev);
  754. TRY_FREE(strm, strm->state->window);
  755. ZFREE(strm, strm->state);
  756. strm->state = Z_NULL;
  757. return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  758. }
  759. /* =========================================================================
  760. * Copy the source state to the destination state.
  761. * To simplify the source, this is not supported for 16-bit MSDOS (which
  762. * doesn't have enough memory anyway to duplicate compression states).
  763. */
  764. int ZEXPORT deflateCopy (z_streamp dest, z_streamp source)
  765. {
  766. #ifdef MAXSEG_64K
  767. return Z_STREAM_ERROR;
  768. #else
  769. deflate_state *ds;
  770. deflate_state *ss;
  771. ushf *overlay;
  772. if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
  773. return Z_STREAM_ERROR;
  774. }
  775. ss = source->state;
  776. zmemcpy(dest, source, sizeof(z_stream));
  777. ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
  778. if (ds == Z_NULL) return Z_MEM_ERROR;
  779. dest->state = (struct internal_state FAR *) ds;
  780. zmemcpy(ds, ss, sizeof(deflate_state));
  781. ds->strm = dest;
  782. ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
  783. ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
  784. ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
  785. overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
  786. ds->pending_buf = (uchf *) overlay;
  787. if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
  788. ds->pending_buf == Z_NULL) {
  789. deflateEnd (dest);
  790. return Z_MEM_ERROR;
  791. }
  792. /* following zmemcpy do not work for 16-bit MSDOS */
  793. zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  794. zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
  795. zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
  796. zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  797. ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  798. ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
  799. ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
  800. ds->l_desc.dyn_tree = ds->dyn_ltree;
  801. ds->d_desc.dyn_tree = ds->dyn_dtree;
  802. ds->bl_desc.dyn_tree = ds->bl_tree;
  803. return Z_OK;
  804. #endif /* MAXSEG_64K */
  805. }
  806. /* ===========================================================================
  807. * Read a new buffer from the current input stream, update the adler32
  808. * and total number of bytes read. All deflate() input goes through
  809. * this function so some applications may wish to modify it to avoid
  810. * allocating a large strm->next_in buffer and copying from it.
  811. * (See also flush_pending()).
  812. */
  813. local int read_buf (z_streamp strm, Bytef *buf, unsigned size)
  814. {
  815. unsigned len = strm->avail_in;
  816. if (len > size) len = size;
  817. if (len == 0) return 0;
  818. strm->avail_in -= len;
  819. if (strm->state->wrap == 1) {
  820. strm->adler = adler32(strm->adler, strm->next_in, len);
  821. }
  822. #ifdef GZIP
  823. else if (strm->state->wrap == 2) {
  824. strm->adler = crc32(strm->adler, strm->next_in, len);
  825. }
  826. #endif
  827. zmemcpy(buf, strm->next_in, len);
  828. strm->next_in += len;
  829. strm->total_in += len;
  830. return (int)len;
  831. }
  832. /* ===========================================================================
  833. * Initialize the "longest match" routines for a new zlib stream
  834. */
  835. local void lm_init (deflate_state *s)
  836. {
  837. s->window_size = (ulg)2L*s->w_size;
  838. CLEAR_HASH(s);
  839. /* Set the default configuration parameters:
  840. */
  841. s->max_lazy_match = configuration_table[s->level].max_lazy;
  842. s->good_match = configuration_table[s->level].good_length;
  843. s->nice_match = configuration_table[s->level].nice_length;
  844. s->max_chain_length = configuration_table[s->level].max_chain;
  845. s->strstart = 0;
  846. s->block_start = 0L;
  847. s->lookahead = 0;
  848. s->match_length = s->prev_length = MIN_MATCH-1;
  849. s->match_available = 0;
  850. s->ins_h = 0;
  851. #ifndef FASTEST
  852. #ifdef ASMV
  853. match_init(); /* initialize the asm code */
  854. #endif
  855. #endif
  856. }
  857. #ifndef FASTEST
  858. /* ===========================================================================
  859. * Set match_start to the longest match starting at the given string and
  860. * return its length. Matches shorter or equal to prev_length are discarded,
  861. * in which case the result is equal to prev_length and match_start is
  862. * garbage.
  863. * IN assertions: cur_match is the head of the hash chain for the current
  864. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  865. * OUT assertion: the match length is not greater than s->lookahead.
  866. */
  867. #ifndef ASMV
  868. /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  869. * match.S. The code will be functionally equivalent.
  870. */
  871. local uInt longest_match(deflate_state *s, IPos cur_match)
  872. {
  873. unsigned chain_length = s->max_chain_length;/* max hash chain length */
  874. register Bytef *scan = s->window + s->strstart; /* current string */
  875. register Bytef *match; /* matched string */
  876. register int len; /* length of current match */
  877. int best_len = s->prev_length; /* best match length so far */
  878. int nice_match = s->nice_match; /* stop if match long enough */
  879. IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  880. s->strstart - (IPos)MAX_DIST(s) : NIL;
  881. /* Stop when cur_match becomes <= limit. To simplify the code,
  882. * we prevent matches with the string of window index 0.
  883. */
  884. Posf *prev = s->prev;
  885. uInt wmask = s->w_mask;
  886. #ifdef UNALIGNED_OK
  887. /* Compare two bytes at a time. Note: this is not always beneficial.
  888. * Try with and without -DUNALIGNED_OK to check.
  889. */
  890. register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  891. register ush scan_start = *(ushf*)scan;
  892. register ush scan_end = *(ushf*)(scan+best_len-1);
  893. #else
  894. register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  895. register Byte scan_end1 = scan[best_len-1];
  896. register Byte scan_end = scan[best_len];
  897. #endif
  898. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  899. * It is easy to get rid of this optimization if necessary.
  900. */
  901. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  902. /* Do not waste too much time if we already have a good match: */
  903. if (s->prev_length >= s->good_match) {
  904. chain_length >>= 2;
  905. }
  906. /* Do not look for matches beyond the end of the input. This is necessary
  907. * to make deflate deterministic.
  908. */
  909. if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  910. Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  911. do {
  912. Assert(cur_match < s->strstart, "no future");
  913. match = s->window + cur_match;
  914. /* Skip to next match if the match length cannot increase
  915. * or if the match length is less than 2. Note that the checks below
  916. * for insufficient lookahead only occur occasionally for performance
  917. * reasons. Therefore uninitialized memory will be accessed, and
  918. * conditional jumps will be made that depend on those values.
  919. * However the length of the match is limited to the lookahead, so
  920. * the output of deflate is not affected by the uninitialized values.
  921. */
  922. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  923. /* This code assumes sizeof(unsigned short) == 2. Do not use
  924. * UNALIGNED_OK if your compiler uses a different size.
  925. */
  926. if (*(ushf*)(match+best_len-1) != scan_end ||
  927. *(ushf*)match != scan_start) continue;
  928. /* It is not necessary to compare scan[2] and match[2] since they are
  929. * always equal when the other bytes match, given that the hash keys
  930. * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  931. * strstart+3, +5, ... up to strstart+257. We check for insufficient
  932. * lookahead only every 4th comparison; the 128th check will be made
  933. * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  934. * necessary to put more guard bytes at the end of the window, or
  935. * to check more often for insufficient lookahead.
  936. */
  937. Assert(scan[2] == match[2], "scan[2]?");
  938. scan++, match++;
  939. do {
  940. } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  941. *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  942. *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  943. *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  944. scan < strend);
  945. /* The funny "do {}" generates better code on most compilers */
  946. /* Here, scan <= window+strstart+257 */
  947. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  948. if (*scan == *match) scan++;
  949. len = (MAX_MATCH - 1) - (int)(strend-scan);
  950. scan = strend - (MAX_MATCH-1);
  951. #else /* UNALIGNED_OK */
  952. if (match[best_len] != scan_end ||
  953. match[best_len-1] != scan_end1 ||
  954. *match != *scan ||
  955. *++match != scan[1]) continue;
  956. /* The check at best_len-1 can be removed because it will be made
  957. * again later. (This heuristic is not always a win.)
  958. * It is not necessary to compare scan[2] and match[2] since they
  959. * are always equal when the other bytes match, given that
  960. * the hash keys are equal and that HASH_BITS >= 8.
  961. */
  962. scan += 2, match++;
  963. Assert(*scan == *match, "match[2]?");
  964. /* We check for insufficient lookahead only every 8th comparison;
  965. * the 256th check will be made at strstart+258.
  966. */
  967. do {
  968. } while (*++scan == *++match && *++scan == *++match &&
  969. *++scan == *++match && *++scan == *++match &&
  970. *++scan == *++match && *++scan == *++match &&
  971. *++scan == *++match && *++scan == *++match &&
  972. scan < strend);
  973. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  974. len = MAX_MATCH - (int)(strend - scan);
  975. scan = strend - MAX_MATCH;
  976. #endif /* UNALIGNED_OK */
  977. if (len > best_len) {
  978. s->match_start = cur_match;
  979. best_len = len;
  980. if (len >= nice_match) break;
  981. #ifdef UNALIGNED_OK
  982. scan_end = *(ushf*)(scan+best_len-1);
  983. #else
  984. scan_end1 = scan[best_len-1];
  985. scan_end = scan[best_len];
  986. #endif
  987. }
  988. } while ((cur_match = prev[cur_match & wmask]) > limit
  989. && --chain_length != 0);
  990. if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  991. return s->lookahead;
  992. }
  993. #endif /* ASMV */
  994. #endif /* FASTEST */
  995. /* ---------------------------------------------------------------------------
  996. * Optimized version for level == 1 or strategy == Z_RLE only
  997. */
  998. local uInt longest_match_fast (deflate_state *s, IPos cur_match)
  999. {
  1000. register Bytef *scan = s->window + s->strstart; /* current string */
  1001. register Bytef *match; /* matched string */
  1002. register int len; /* length of current match */
  1003. register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  1004. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1005. * It is easy to get rid of this optimization if necessary.
  1006. */
  1007. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1008. Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  1009. Assert(cur_match < s->strstart, "no future");
  1010. match = s->window + cur_match;
  1011. /* Return failure if the match length is less than 2:
  1012. */
  1013. if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  1014. /* The check at best_len-1 can be removed because it will be made
  1015. * again later. (This heuristic is not always a win.)
  1016. * It is not necessary to compare scan[2] and match[2] since they
  1017. * are always equal when the other bytes match, given that
  1018. * the hash keys are equal and that HASH_BITS >= 8.
  1019. */
  1020. scan += 2, match += 2;
  1021. Assert(*scan == *match, "match[2]?");
  1022. /* We check for insufficient lookahead only every 8th comparison;
  1023. * the 256th check will be made at strstart+258.
  1024. */
  1025. do {
  1026. } while (*++scan == *++match && *++scan == *++match &&
  1027. *++scan == *++match && *++scan == *++match &&
  1028. *++scan == *++match && *++scan == *++match &&
  1029. *++scan == *++match && *++scan == *++match &&
  1030. scan < strend);
  1031. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1032. len = MAX_MATCH - (int)(strend - scan);
  1033. if (len < MIN_MATCH) return MIN_MATCH - 1;
  1034. s->match_start = cur_match;
  1035. return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
  1036. }
  1037. #ifdef DEBUG
  1038. /* ===========================================================================
  1039. * Check that the match at match_start is indeed a match.
  1040. */
  1041. local void check_match(deflate_state *s, IPos start, IPos match, int length)
  1042. {
  1043. /* check that the match is indeed a match */
  1044. if (zmemcmp(s->window + match,
  1045. s->window + start, length) != EQUAL) {
  1046. fprintf(stderr, " start %u, match %u, length %d\n",
  1047. start, match, length);
  1048. do {
  1049. fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  1050. } while (--length != 0);
  1051. z_error("invalid match");
  1052. }
  1053. if (z_verbose > 1) {
  1054. fprintf(stderr,"\\[%d,%d]", start-match, length);
  1055. do { putc(s->window[start++], stderr); } while (--length != 0);
  1056. }
  1057. }
  1058. #else
  1059. # define check_match(s, start, match, length)
  1060. #endif /* DEBUG */
  1061. /* ===========================================================================
  1062. * Fill the window when the lookahead becomes insufficient.
  1063. * Updates strstart and lookahead.
  1064. *
  1065. * IN assertion: lookahead < MIN_LOOKAHEAD
  1066. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1067. * At least one byte has been read, or avail_in == 0; reads are
  1068. * performed for at least two bytes (required for the zip translate_eol
  1069. * option -- not supported here).
  1070. */
  1071. local void fill_window (deflate_state *s)
  1072. {
  1073. register unsigned n, m;
  1074. register Posf *p;
  1075. unsigned more; /* Amount of free space at the end of the window. */
  1076. uInt wsize = s->w_size;
  1077. do {
  1078. more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  1079. /* Deal with !@#$% 64K limit: */
  1080. if (sizeof(int) <= 2) {
  1081. if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1082. more = wsize;
  1083. } else if (more == (unsigned)(-1)) {
  1084. /* Very unlikely, but possible on 16 bit machine if
  1085. * strstart == 0 && lookahead == 1 (input done a byte at time)
  1086. */
  1087. more--;
  1088. }
  1089. }
  1090. /* If the window is almost full and there is insufficient lookahead,
  1091. * move the upper half to the lower one to make room in the upper half.
  1092. */
  1093. if (s->strstart >= wsize+MAX_DIST(s)) {
  1094. zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
  1095. s->match_start -= wsize;
  1096. s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  1097. s->block_start -= (long) wsize;
  1098. /* Slide the hash table (could be avoided with 32 bit values
  1099. at the expense of memory usage). We slide even when level == 0
  1100. to keep the hash table consistent if we switch back to level > 0
  1101. later. (Using level 0 permanently is not an optimal usage of
  1102. zlib, so we don't care about this pathological case.)
  1103. */
  1104. /* %%% avoid this when Z_RLE */
  1105. n = s->hash_size;
  1106. p = &s->head[n];
  1107. do {
  1108. m = *--p;
  1109. *p = (Pos)(m >= wsize ? m-wsize : NIL);
  1110. } while (--n);
  1111. n = wsize;
  1112. #ifndef FASTEST
  1113. p = &s->prev[n];
  1114. do {
  1115. m = *--p;
  1116. *p = (Pos)(m >= wsize ? m-wsize : NIL);
  1117. /* If n is not on any hash chain, prev[n] is garbage but
  1118. * its value will never be used.
  1119. */
  1120. } while (--n);
  1121. #endif
  1122. more += wsize;
  1123. }
  1124. if (s->strm->avail_in == 0) return;
  1125. /* If there was no sliding:
  1126. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1127. * more == window_size - lookahead - strstart
  1128. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1129. * => more >= window_size - 2*WSIZE + 2
  1130. * In the BIG_MEM or MMAP case (not yet supported),
  1131. * window_size == input_size + MIN_LOOKAHEAD &&
  1132. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1133. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1134. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1135. */
  1136. Assert(more >= 2, "more < 2");
  1137. n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  1138. s->lookahead += n;
  1139. /* Initialize the hash value now that we have some input: */
  1140. if (s->lookahead >= MIN_MATCH) {
  1141. s->ins_h = s->window[s->strstart];
  1142. UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  1143. #if MIN_MATCH != 3
  1144. Call UPDATE_HASH() MIN_MATCH-3 more times
  1145. #endif
  1146. }
  1147. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1148. * but this is not important since only literal bytes will be emitted.
  1149. */
  1150. } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  1151. }
  1152. /* ===========================================================================
  1153. * Flush the current block, with given end-of-file flag.
  1154. * IN assertion: strstart is set to the end of the current match.
  1155. */
  1156. #define FLUSH_BLOCK_ONLY(s, eof) { \
  1157. _tr_flush_block(s, (s->block_start >= 0L ? \
  1158. (charf *)&s->window[(unsigned)s->block_start] : \
  1159. (charf *)Z_NULL), \
  1160. (ulg)((long)s->strstart - s->block_start), \
  1161. (eof)); \
  1162. s->block_start = s->strstart; \
  1163. flush_pending(s->strm); \
  1164. Tracev((stderr,"[FLUSH]")); \
  1165. }
  1166. /* Same but force premature exit if necessary. */
  1167. #define FLUSH_BLOCK(s, eof) { \
  1168. FLUSH_BLOCK_ONLY(s, eof); \
  1169. if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  1170. }
  1171. /* ===========================================================================
  1172. * Copy without compression as much as possible from the input stream, return
  1173. * the current block state.
  1174. * This function does not insert new strings in the dictionary since
  1175. * uncompressible data is probably not useful. This function is used
  1176. * only for the level=0 compression option.
  1177. * NOTE: this function should be optimized to avoid extra copying from
  1178. * window to pending_buf.
  1179. */
  1180. local block_state deflate_stored(deflate_state *s, int flush)
  1181. {
  1182. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1183. * to pending_buf_size, and each stored block has a 5 byte header:
  1184. */
  1185. ulg max_block_size = 0xffff;
  1186. ulg max_start;
  1187. if (max_block_size > s->pending_buf_size - 5) {
  1188. max_block_size = s->pending_buf_size - 5;
  1189. }
  1190. /* Copy as much as possible from input to output: */
  1191. for (;;) {
  1192. /* Fill the window as much as possible: */
  1193. if (s->lookahead <= 1) {
  1194. Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1195. s->block_start >= (long)s->w_size, "slide too late");
  1196. fill_window(s);
  1197. if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  1198. if (s->lookahead == 0) break; /* flush the current block */
  1199. }
  1200. Assert(s->block_start >= 0L, "block gone");
  1201. s->strstart += s->lookahead;
  1202. s->lookahead = 0;
  1203. /* Emit a stored block if pending_buf will be full: */
  1204. max_start = s->block_start + max_block_size;
  1205. if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  1206. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1207. s->lookahead = (uInt)(s->strstart - max_start);
  1208. s->strstart = (uInt)max_start;
  1209. FLUSH_BLOCK(s, 0);
  1210. }
  1211. /* Flush if we may have to slide, otherwise block_start may become
  1212. * negative and the data will be gone:
  1213. */
  1214. if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  1215. FLUSH_BLOCK(s, 0);
  1216. }
  1217. }
  1218. FLUSH_BLOCK(s, flush == Z_FINISH);
  1219. return flush == Z_FINISH ? finish_done : block_done;
  1220. }
  1221. /* ===========================================================================
  1222. * Compress as much as possible from the input stream, return the current
  1223. * block state.
  1224. * This function does not perform lazy evaluation of matches and inserts
  1225. * new strings in the dictionary only for unmatched strings or for short
  1226. * matches. It is used only for the fast compression options.
  1227. */
  1228. local block_state deflate_fast(deflate_state *s, int flush)
  1229. {
  1230. IPos hash_head = NIL; /* head of the hash chain */
  1231. int bflush; /* set if current block must be flushed */
  1232. for (;;) {
  1233. /* Make sure that we always have enough lookahead, except
  1234. * at the end of the input file. We need MAX_MATCH bytes
  1235. * for the next match, plus MIN_MATCH bytes to insert the
  1236. * string following the next match.
  1237. */
  1238. if (s->lookahead < MIN_LOOKAHEAD) {
  1239. fill_window(s);
  1240. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1241. return need_more;
  1242. }
  1243. if (s->lookahead == 0) break; /* flush the current block */
  1244. }
  1245. /* Insert the string window[strstart .. strstart+2] in the
  1246. * dictionary, and set hash_head to the head of the hash chain:
  1247. */
  1248. if (s->lookahead >= MIN_MATCH) {
  1249. INSERT_STRING(s, s->strstart, hash_head);
  1250. }
  1251. /* Find the longest match, discarding those <= prev_length.
  1252. * At this point we have always match_length < MIN_MATCH
  1253. */
  1254. if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  1255. /* To simplify the code, we prevent matches with the string
  1256. * of window index 0 (in particular we have to avoid a match
  1257. * of the string with itself at the start of the input file).
  1258. */
  1259. #ifdef FASTEST
  1260. if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
  1261. (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
  1262. s->match_length = longest_match_fast (s, hash_head);
  1263. }
  1264. #else
  1265. if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
  1266. s->match_length = longest_match (s, hash_head);
  1267. } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
  1268. s->match_length = longest_match_fast (s, hash_head);
  1269. }
  1270. #endif
  1271. /* longest_match() or longest_match_fast() sets match_start */
  1272. }
  1273. if (s->match_length >= MIN_MATCH) {
  1274. check_match(s, s->strstart, s->match_start, s->match_length);
  1275. _tr_tally_dist(s, s->strstart - s->match_start,
  1276. s->match_length - MIN_MATCH, bflush);
  1277. s->lookahead -= s->match_length;
  1278. /* Insert new strings in the hash table only if the match length
  1279. * is not too large. This saves time but degrades compression.
  1280. */
  1281. #ifndef FASTEST
  1282. if (s->match_length <= s->max_insert_length &&
  1283. s->lookahead >= MIN_MATCH) {
  1284. s->match_length--; /* string at strstart already in table */
  1285. do {
  1286. s->strstart++;
  1287. INSERT_STRING(s, s->strstart, hash_head);
  1288. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1289. * always MIN_MATCH bytes ahead.
  1290. */
  1291. } while (--s->match_length != 0);
  1292. s->strstart++;
  1293. } else
  1294. #endif
  1295. {
  1296. s->strstart += s->match_length;
  1297. s->match_length = 0;
  1298. s->ins_h = s->window[s->strstart];
  1299. UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  1300. #if MIN_MATCH != 3
  1301. Call UPDATE_HASH() MIN_MATCH-3 more times
  1302. #endif
  1303. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1304. * matter since it will be recomputed at next deflate call.
  1305. */
  1306. }
  1307. } else {
  1308. /* No match, output a literal byte */
  1309. Tracevv((stderr,"%c", s->window[s->strstart]));
  1310. _tr_tally_lit (s, s->window[s->strstart], bflush);
  1311. s->lookahead--;
  1312. s->strstart++;
  1313. }
  1314. if (bflush) FLUSH_BLOCK(s, 0);
  1315. }
  1316. FLUSH_BLOCK(s, flush == Z_FINISH);
  1317. return flush == Z_FINISH ? finish_done : block_done;
  1318. }
  1319. #ifndef FASTEST
  1320. /* ===========================================================================
  1321. * Same as above, but achieves better compression. We use a lazy
  1322. * evaluation for matches: a match is finally adopted only if there is
  1323. * no better match at the next window position.
  1324. */
  1325. local block_state deflate_slow(deflate_state *s, int flush)
  1326. {
  1327. IPos hash_head = NIL; /* head of hash chain */
  1328. int bflush; /* set if current block must be flushed */
  1329. /* Process the input block. */
  1330. for (;;) {
  1331. /* Make sure that we always have enough lookahead, except
  1332. * at the end of the input file. We need MAX_MATCH bytes
  1333. * for the next match, plus MIN_MATCH bytes to insert the
  1334. * string following the next match.
  1335. */
  1336. if (s->lookahead < MIN_LOOKAHEAD) {
  1337. fill_window(s);
  1338. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1339. return need_more;
  1340. }
  1341. if (s->lookahead == 0) break; /* flush the current block */
  1342. }
  1343. /* Insert the string window[strstart .. strstart+2] in the
  1344. * dictionary, and set hash_head to the head of the hash chain:
  1345. */
  1346. if (s->lookahead >= MIN_MATCH) {
  1347. INSERT_STRING(s, s->strstart, hash_head);
  1348. }
  1349. /* Find the longest match, discarding those <= prev_length.
  1350. */
  1351. s->prev_length = s->match_length, s->prev_match = s->match_start;
  1352. s->match_length = MIN_MATCH-1;
  1353. if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  1354. s->strstart - hash_head <= MAX_DIST(s)) {
  1355. /* To simplify the code, we prevent matches with the string
  1356. * of window index 0 (in particular we have to avoid a match
  1357. * of the string with itself at the start of the input file).
  1358. */
  1359. if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
  1360. s->match_length = longest_match (s, hash_head);
  1361. } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
  1362. s->match_length = longest_match_fast (s, hash_head);
  1363. }
  1364. /* longest_match() or longest_match_fast() sets match_start */
  1365. if (s->match_length <= 5 && (s->strategy == Z_FILTERED
  1366. #if TOO_FAR <= 32767
  1367. || (s->match_length == MIN_MATCH &&
  1368. s->strstart - s->match_start > TOO_FAR)
  1369. #endif
  1370. )) {
  1371. /* If prev_match is also MIN_MATCH, match_start is garbage
  1372. * but we will ignore the current match anyway.
  1373. */
  1374. s->match_length = MIN_MATCH-1;
  1375. }
  1376. }
  1377. /* If there was a match at the previous step and the current
  1378. * match is not better, output the previous match:
  1379. */
  1380. if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  1381. uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  1382. /* Do not insert strings in hash table beyond this. */
  1383. check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  1384. _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  1385. s->prev_length - MIN_MATCH, bflush);
  1386. /* Insert in hash table all strings up to the end of the match.
  1387. * strstart-1 and strstart are already inserted. If there is not
  1388. * enough lookahead, the last two strings are not inserted in
  1389. * the hash table.
  1390. */
  1391. s->lookahead -= s->prev_length-1;
  1392. s->prev_length -= 2;
  1393. do {
  1394. if (++s->strstart <= max_insert) {
  1395. INSERT_STRING(s, s->strstart, hash_head);
  1396. }
  1397. } while (--s->prev_length != 0);
  1398. s->match_available = 0;
  1399. s->match_length = MIN_MATCH-1;
  1400. s->strstart++;
  1401. if (bflush) FLUSH_BLOCK(s, 0);
  1402. } else if (s->match_available) {
  1403. /* If there was no match at the previous position, output a
  1404. * single literal. If there was a match but the current match
  1405. * is longer, truncate the previous match to a single literal.
  1406. */
  1407. Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1408. _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  1409. if (bflush) {
  1410. FLUSH_BLOCK_ONLY(s, 0);
  1411. }
  1412. s->strstart++;
  1413. s->lookahead--;
  1414. if (s->strm->avail_out == 0) return need_more;
  1415. } else {
  1416. /* There is no previous match to compare with, wait for
  1417. * the next step to decide.
  1418. */
  1419. s->match_available = 1;
  1420. s->strstart++;
  1421. s->lookahead--;
  1422. }
  1423. }
  1424. Assert (flush != Z_NO_FLUSH, "no flush?");
  1425. if (s->match_available) {
  1426. Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1427. _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  1428. s->match_available = 0;
  1429. }
  1430. FLUSH_BLOCK(s, flush == Z_FINISH);
  1431. return flush == Z_FINISH ? finish_done : block_done;
  1432. }
  1433. #endif /* FASTEST */
  1434. #if 0
  1435. /* ===========================================================================
  1436. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1437. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1438. * deflate switches away from Z_RLE.)
  1439. */
  1440. local block_state deflate_rle(s, flush)
  1441. deflate_state *s;
  1442. int flush;
  1443. {
  1444. int bflush; /* set if current block must be flushed */
  1445. uInt run; /* length of run */
  1446. uInt max; /* maximum length of run */
  1447. uInt prev; /* byte at distance one to match */
  1448. Bytef *scan; /* scan for end of run */
  1449. for (;;) {
  1450. /* Make sure that we always have enough lookahead, except
  1451. * at the end of the input file. We need MAX_MATCH bytes
  1452. * for the longest encodable run.
  1453. */
  1454. if (s->lookahead < MAX_MATCH) {
  1455. fill_window(s);
  1456. if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
  1457. return need_more;
  1458. }
  1459. if (s->lookahead == 0) break; /* flush the current block */
  1460. }
  1461. /* See how many times the previous byte repeats */
  1462. run = 0;
  1463. if (s->strstart > 0) { /* if there is a previous byte, that is */
  1464. max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
  1465. scan = s->window + s->strstart - 1;
  1466. prev = *scan++;
  1467. do {
  1468. if (*scan++ != prev)
  1469. break;
  1470. } while (++run < max);
  1471. }
  1472. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1473. if (run >= MIN_MATCH) {
  1474. check_match(s, s->strstart, s->strstart - 1, run);
  1475. _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
  1476. s->lookahead -= run;
  1477. s->strstart += run;
  1478. } else {
  1479. /* No match, output a literal byte */
  1480. Tracevv((stderr,"%c", s->window[s->strstart]));
  1481. _tr_tally_lit (s, s->window[s->strstart], bflush);
  1482. s->lookahead--;
  1483. s->strstart++;
  1484. }
  1485. if (bflush) FLUSH_BLOCK(s, 0);
  1486. }
  1487. FLUSH_BLOCK(s, flush == Z_FINISH);
  1488. return flush == Z_FINISH ? finish_done : block_done;
  1489. }
  1490. #endif