Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1834 lines
66 KiB

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