deflate.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /* +++ deflate.c */
  2. /* deflate.c -- compress data using the deflation algorithm
  3. * Copyright (C) 1995-1996 Jean-loup Gailly.
  4. * For conditions of distribution and use, see copyright notice in zlib.h
  5. */
  6. /*
  7. * ALGORITHM
  8. *
  9. * The "deflation" process depends on being able to identify portions
  10. * of the input text which are identical to earlier input (within a
  11. * sliding window trailing behind the input currently being processed).
  12. *
  13. * The most straightforward technique turns out to be the fastest for
  14. * most input files: try all possible matches and select the longest.
  15. * The key feature of this algorithm is that insertions into the string
  16. * dictionary are very simple and thus fast, and deletions are avoided
  17. * completely. Insertions are performed at each input character, whereas
  18. * string matches are performed only when the previous match ends. So it
  19. * is preferable to spend more time in matches to allow very fast string
  20. * insertions and avoid deletions. The matching algorithm for small
  21. * strings is inspired from that of Rabin & Karp. A brute force approach
  22. * is used to find longer strings when a small match has been found.
  23. * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  24. * (by Leonid Broukhis).
  25. * A previous version of this file used a more sophisticated algorithm
  26. * (by Fiala and Greene) which is guaranteed to run in linear amortized
  27. * time, but has a larger average cost, uses more memory and is patented.
  28. * However the F&G algorithm may be faster for some highly redundant
  29. * files if the parameter max_chain_length (described below) is too large.
  30. *
  31. * ACKNOWLEDGEMENTS
  32. *
  33. * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  34. * I found it in 'freeze' written by Leonid Broukhis.
  35. * Thanks to many people for bug reports and testing.
  36. *
  37. * REFERENCES
  38. *
  39. * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  40. * Available in ftp://ds.internic.net/rfc/rfc1951.txt
  41. *
  42. * A description of the Rabin and Karp algorithm is given in the book
  43. * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  44. *
  45. * Fiala,E.R., and Greene,D.H.
  46. * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  47. *
  48. */
  49. #include <linux/module.h>
  50. #include <linux/zutil.h>
  51. #include "defutil.h"
  52. /* ===========================================================================
  53. * Function prototypes.
  54. */
  55. typedef enum {
  56. need_more, /* block not completed, need more input or more output */
  57. block_done, /* block flush performed */
  58. finish_started, /* finish started, need only more output at next deflate */
  59. finish_done /* finish done, accept no more input or output */
  60. } block_state;
  61. typedef block_state (*compress_func) (deflate_state *s, int flush);
  62. /* Compression function. Returns the block state after the call. */
  63. static void fill_window (deflate_state *s);
  64. static block_state deflate_stored (deflate_state *s, int flush);
  65. static block_state deflate_fast (deflate_state *s, int flush);
  66. static block_state deflate_slow (deflate_state *s, int flush);
  67. static void lm_init (deflate_state *s);
  68. static void putShortMSB (deflate_state *s, uInt b);
  69. static void flush_pending (z_streamp strm);
  70. static int read_buf (z_streamp strm, Byte *buf, unsigned size);
  71. static uInt longest_match (deflate_state *s, IPos cur_match);
  72. #ifdef DEBUG_ZLIB
  73. static void check_match (deflate_state *s, IPos start, IPos match,
  74. int length);
  75. #endif
  76. /* ===========================================================================
  77. * Local data
  78. */
  79. #define NIL 0
  80. /* Tail of hash chains */
  81. #ifndef TOO_FAR
  82. # define TOO_FAR 4096
  83. #endif
  84. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  85. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  86. /* Minimum amount of lookahead, except at the end of the input file.
  87. * See deflate.c for comments about the MIN_MATCH+1.
  88. */
  89. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  90. * the desired pack level (0..9). The values given below have been tuned to
  91. * exclude worst case performance for pathological files. Better values may be
  92. * found for specific files.
  93. */
  94. typedef struct config_s {
  95. ush good_length; /* reduce lazy search above this match length */
  96. ush max_lazy; /* do not perform lazy search above this match length */
  97. ush nice_length; /* quit search above this match length */
  98. ush max_chain;
  99. compress_func func;
  100. } config;
  101. static const config configuration_table[10] = {
  102. /* good lazy nice chain */
  103. /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  104. /* 1 */ {4, 4, 8, 4, deflate_fast}, /* maximum speed, no lazy matches */
  105. /* 2 */ {4, 5, 16, 8, deflate_fast},
  106. /* 3 */ {4, 6, 32, 32, deflate_fast},
  107. /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
  108. /* 5 */ {8, 16, 32, 32, deflate_slow},
  109. /* 6 */ {8, 16, 128, 128, deflate_slow},
  110. /* 7 */ {8, 32, 128, 256, deflate_slow},
  111. /* 8 */ {32, 128, 258, 1024, deflate_slow},
  112. /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
  113. /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  114. * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  115. * meaning.
  116. */
  117. #define EQUAL 0
  118. /* result of memcmp for equal strings */
  119. /* ===========================================================================
  120. * Update a hash value with the given input byte
  121. * IN assertion: all calls to to UPDATE_HASH are made with consecutive
  122. * input characters, so that a running hash key can be computed from the
  123. * previous key instead of complete recalculation each time.
  124. */
  125. #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  126. /* ===========================================================================
  127. * Insert string str in the dictionary and set match_head to the previous head
  128. * of the hash chain (the most recent string with same hash key). Return
  129. * the previous length of the hash chain.
  130. * IN assertion: all calls to to INSERT_STRING are made with consecutive
  131. * input characters and the first MIN_MATCH bytes of str are valid
  132. * (except for the last MIN_MATCH-1 bytes of the input file).
  133. */
  134. #define INSERT_STRING(s, str, match_head) \
  135. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  136. s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
  137. s->head[s->ins_h] = (Pos)(str))
  138. /* ===========================================================================
  139. * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  140. * prev[] will be initialized on the fly.
  141. */
  142. #define CLEAR_HASH(s) \
  143. s->head[s->hash_size-1] = NIL; \
  144. memset((char *)s->head, 0, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  145. /* ========================================================================= */
  146. int zlib_deflateInit_(
  147. z_streamp strm,
  148. int level,
  149. const char *version,
  150. int stream_size
  151. )
  152. {
  153. return zlib_deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS,
  154. DEF_MEM_LEVEL,
  155. Z_DEFAULT_STRATEGY, version, stream_size);
  156. /* To do: ignore strm->next_in if we use it as window */
  157. }
  158. /* ========================================================================= */
  159. int zlib_deflateInit2_(
  160. z_streamp strm,
  161. int level,
  162. int method,
  163. int windowBits,
  164. int memLevel,
  165. int strategy,
  166. const char *version,
  167. int stream_size
  168. )
  169. {
  170. deflate_state *s;
  171. int noheader = 0;
  172. static char* my_version = ZLIB_VERSION;
  173. deflate_workspace *mem;
  174. ush *overlay;
  175. /* We overlay pending_buf and d_buf+l_buf. This works since the average
  176. * output size for (length,distance) codes is <= 24 bits.
  177. */
  178. if (version == NULL || version[0] != my_version[0] ||
  179. stream_size != sizeof(z_stream)) {
  180. return Z_VERSION_ERROR;
  181. }
  182. if (strm == NULL) return Z_STREAM_ERROR;
  183. strm->msg = NULL;
  184. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  185. mem = (deflate_workspace *) strm->workspace;
  186. if (windowBits < 0) { /* undocumented feature: suppress zlib header */
  187. noheader = 1;
  188. windowBits = -windowBits;
  189. }
  190. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  191. windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
  192. strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  193. return Z_STREAM_ERROR;
  194. }
  195. s = (deflate_state *) &(mem->deflate_memory);
  196. strm->state = (struct internal_state *)s;
  197. s->strm = strm;
  198. s->noheader = noheader;
  199. s->w_bits = windowBits;
  200. s->w_size = 1 << s->w_bits;
  201. s->w_mask = s->w_size - 1;
  202. s->hash_bits = memLevel + 7;
  203. s->hash_size = 1 << s->hash_bits;
  204. s->hash_mask = s->hash_size - 1;
  205. s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  206. s->window = (Byte *) mem->window_memory;
  207. s->prev = (Pos *) mem->prev_memory;
  208. s->head = (Pos *) mem->head_memory;
  209. s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  210. overlay = (ush *) mem->overlay_memory;
  211. s->pending_buf = (uch *) overlay;
  212. s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  213. s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  214. s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  215. s->level = level;
  216. s->strategy = strategy;
  217. s->method = (Byte)method;
  218. return zlib_deflateReset(strm);
  219. }
  220. /* ========================================================================= */
  221. #if 0
  222. int zlib_deflateSetDictionary(
  223. z_streamp strm,
  224. const Byte *dictionary,
  225. uInt dictLength
  226. )
  227. {
  228. deflate_state *s;
  229. uInt length = dictLength;
  230. uInt n;
  231. IPos hash_head = 0;
  232. if (strm == NULL || strm->state == NULL || dictionary == NULL)
  233. return Z_STREAM_ERROR;
  234. s = (deflate_state *) strm->state;
  235. if (s->status != INIT_STATE) return Z_STREAM_ERROR;
  236. strm->adler = zlib_adler32(strm->adler, dictionary, dictLength);
  237. if (length < MIN_MATCH) return Z_OK;
  238. if (length > MAX_DIST(s)) {
  239. length = MAX_DIST(s);
  240. #ifndef USE_DICT_HEAD
  241. dictionary += dictLength - length; /* use the tail of the dictionary */
  242. #endif
  243. }
  244. memcpy((char *)s->window, dictionary, length);
  245. s->strstart = length;
  246. s->block_start = (long)length;
  247. /* Insert all strings in the hash table (except for the last two bytes).
  248. * s->lookahead stays null, so s->ins_h will be recomputed at the next
  249. * call of fill_window.
  250. */
  251. s->ins_h = s->window[0];
  252. UPDATE_HASH(s, s->ins_h, s->window[1]);
  253. for (n = 0; n <= length - MIN_MATCH; n++) {
  254. INSERT_STRING(s, n, hash_head);
  255. }
  256. if (hash_head) hash_head = 0; /* to make compiler happy */
  257. return Z_OK;
  258. }
  259. #endif /* 0 */
  260. /* ========================================================================= */
  261. int zlib_deflateReset(
  262. z_streamp strm
  263. )
  264. {
  265. deflate_state *s;
  266. if (strm == NULL || strm->state == NULL)
  267. return Z_STREAM_ERROR;
  268. strm->total_in = strm->total_out = 0;
  269. strm->msg = NULL;
  270. strm->data_type = Z_UNKNOWN;
  271. s = (deflate_state *)strm->state;
  272. s->pending = 0;
  273. s->pending_out = s->pending_buf;
  274. if (s->noheader < 0) {
  275. s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
  276. }
  277. s->status = s->noheader ? BUSY_STATE : INIT_STATE;
  278. strm->adler = 1;
  279. s->last_flush = Z_NO_FLUSH;
  280. zlib_tr_init(s);
  281. lm_init(s);
  282. return Z_OK;
  283. }
  284. /* ========================================================================= */
  285. #if 0
  286. int zlib_deflateParams(
  287. z_streamp strm,
  288. int level,
  289. int strategy
  290. )
  291. {
  292. deflate_state *s;
  293. compress_func func;
  294. int err = Z_OK;
  295. if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
  296. s = (deflate_state *) strm->state;
  297. if (level == Z_DEFAULT_COMPRESSION) {
  298. level = 6;
  299. }
  300. if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
  301. return Z_STREAM_ERROR;
  302. }
  303. func = configuration_table[s->level].func;
  304. if (func != configuration_table[level].func && strm->total_in != 0) {
  305. /* Flush the last buffer: */
  306. err = zlib_deflate(strm, Z_PARTIAL_FLUSH);
  307. }
  308. if (s->level != level) {
  309. s->level = level;
  310. s->max_lazy_match = configuration_table[level].max_lazy;
  311. s->good_match = configuration_table[level].good_length;
  312. s->nice_match = configuration_table[level].nice_length;
  313. s->max_chain_length = configuration_table[level].max_chain;
  314. }
  315. s->strategy = strategy;
  316. return err;
  317. }
  318. #endif /* 0 */
  319. /* =========================================================================
  320. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  321. * IN assertion: the stream state is correct and there is enough room in
  322. * pending_buf.
  323. */
  324. static void putShortMSB(
  325. deflate_state *s,
  326. uInt b
  327. )
  328. {
  329. put_byte(s, (Byte)(b >> 8));
  330. put_byte(s, (Byte)(b & 0xff));
  331. }
  332. /* =========================================================================
  333. * Flush as much pending output as possible. All deflate() output goes
  334. * through this function so some applications may wish to modify it
  335. * to avoid allocating a large strm->next_out buffer and copying into it.
  336. * (See also read_buf()).
  337. */
  338. static void flush_pending(
  339. z_streamp strm
  340. )
  341. {
  342. deflate_state *s = (deflate_state *) strm->state;
  343. unsigned len = s->pending;
  344. if (len > strm->avail_out) len = strm->avail_out;
  345. if (len == 0) return;
  346. if (strm->next_out != NULL) {
  347. memcpy(strm->next_out, s->pending_out, len);
  348. strm->next_out += len;
  349. }
  350. s->pending_out += len;
  351. strm->total_out += len;
  352. strm->avail_out -= len;
  353. s->pending -= len;
  354. if (s->pending == 0) {
  355. s->pending_out = s->pending_buf;
  356. }
  357. }
  358. /* ========================================================================= */
  359. int zlib_deflate(
  360. z_streamp strm,
  361. int flush
  362. )
  363. {
  364. int old_flush; /* value of flush param for previous deflate call */
  365. deflate_state *s;
  366. if (strm == NULL || strm->state == NULL ||
  367. flush > Z_FINISH || flush < 0) {
  368. return Z_STREAM_ERROR;
  369. }
  370. s = (deflate_state *) strm->state;
  371. if ((strm->next_in == NULL && strm->avail_in != 0) ||
  372. (s->status == FINISH_STATE && flush != Z_FINISH)) {
  373. return Z_STREAM_ERROR;
  374. }
  375. if (strm->avail_out == 0) return Z_BUF_ERROR;
  376. s->strm = strm; /* just in case */
  377. old_flush = s->last_flush;
  378. s->last_flush = flush;
  379. /* Write the zlib header */
  380. if (s->status == INIT_STATE) {
  381. uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  382. uInt level_flags = (s->level-1) >> 1;
  383. if (level_flags > 3) level_flags = 3;
  384. header |= (level_flags << 6);
  385. if (s->strstart != 0) header |= PRESET_DICT;
  386. header += 31 - (header % 31);
  387. s->status = BUSY_STATE;
  388. putShortMSB(s, header);
  389. /* Save the adler32 of the preset dictionary: */
  390. if (s->strstart != 0) {
  391. putShortMSB(s, (uInt)(strm->adler >> 16));
  392. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  393. }
  394. strm->adler = 1L;
  395. }
  396. /* Flush as much pending output as possible */
  397. if (s->pending != 0) {
  398. flush_pending(strm);
  399. if (strm->avail_out == 0) {
  400. /* Since avail_out is 0, deflate will be called again with
  401. * more output space, but possibly with both pending and
  402. * avail_in equal to zero. There won't be anything to do,
  403. * but this is not an error situation so make sure we
  404. * return OK instead of BUF_ERROR at next call of deflate:
  405. */
  406. s->last_flush = -1;
  407. return Z_OK;
  408. }
  409. /* Make sure there is something to do and avoid duplicate consecutive
  410. * flushes. For repeated and useless calls with Z_FINISH, we keep
  411. * returning Z_STREAM_END instead of Z_BUFF_ERROR.
  412. */
  413. } else if (strm->avail_in == 0 && flush <= old_flush &&
  414. flush != Z_FINISH) {
  415. return Z_BUF_ERROR;
  416. }
  417. /* User must not provide more input after the first FINISH: */
  418. if (s->status == FINISH_STATE && strm->avail_in != 0) {
  419. return Z_BUF_ERROR;
  420. }
  421. /* Start a new block or continue the current one.
  422. */
  423. if (strm->avail_in != 0 || s->lookahead != 0 ||
  424. (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  425. block_state bstate;
  426. bstate = (*(configuration_table[s->level].func))(s, flush);
  427. if (bstate == finish_started || bstate == finish_done) {
  428. s->status = FINISH_STATE;
  429. }
  430. if (bstate == need_more || bstate == finish_started) {
  431. if (strm->avail_out == 0) {
  432. s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  433. }
  434. return Z_OK;
  435. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  436. * of deflate should use the same flush parameter to make sure
  437. * that the flush is complete. So we don't have to output an
  438. * empty block here, this will be done at next call. This also
  439. * ensures that for a very small output buffer, we emit at most
  440. * one empty block.
  441. */
  442. }
  443. if (bstate == block_done) {
  444. if (flush == Z_PARTIAL_FLUSH) {
  445. zlib_tr_align(s);
  446. } else if (flush == Z_PACKET_FLUSH) {
  447. /* Output just the 3-bit `stored' block type value,
  448. but not a zero length. */
  449. zlib_tr_stored_type_only(s);
  450. } else { /* FULL_FLUSH or SYNC_FLUSH */
  451. zlib_tr_stored_block(s, (char*)0, 0L, 0);
  452. /* For a full flush, this empty block will be recognized
  453. * as a special marker by inflate_sync().
  454. */
  455. if (flush == Z_FULL_FLUSH) {
  456. CLEAR_HASH(s); /* forget history */
  457. }
  458. }
  459. flush_pending(strm);
  460. if (strm->avail_out == 0) {
  461. s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  462. return Z_OK;
  463. }
  464. }
  465. }
  466. Assert(strm->avail_out > 0, "bug2");
  467. if (flush != Z_FINISH) return Z_OK;
  468. if (s->noheader) return Z_STREAM_END;
  469. /* Write the zlib trailer (adler32) */
  470. putShortMSB(s, (uInt)(strm->adler >> 16));
  471. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  472. flush_pending(strm);
  473. /* If avail_out is zero, the application will call deflate again
  474. * to flush the rest.
  475. */
  476. s->noheader = -1; /* write the trailer only once! */
  477. return s->pending != 0 ? Z_OK : Z_STREAM_END;
  478. }
  479. /* ========================================================================= */
  480. int zlib_deflateEnd(
  481. z_streamp strm
  482. )
  483. {
  484. int status;
  485. deflate_state *s;
  486. if (strm == NULL || strm->state == NULL) return Z_STREAM_ERROR;
  487. s = (deflate_state *) strm->state;
  488. status = s->status;
  489. if (status != INIT_STATE && status != BUSY_STATE &&
  490. status != FINISH_STATE) {
  491. return Z_STREAM_ERROR;
  492. }
  493. strm->state = NULL;
  494. return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  495. }
  496. /* =========================================================================
  497. * Copy the source state to the destination state.
  498. */
  499. #if 0
  500. int zlib_deflateCopy (
  501. z_streamp dest,
  502. z_streamp source
  503. )
  504. {
  505. #ifdef MAXSEG_64K
  506. return Z_STREAM_ERROR;
  507. #else
  508. deflate_state *ds;
  509. deflate_state *ss;
  510. ush *overlay;
  511. deflate_workspace *mem;
  512. if (source == NULL || dest == NULL || source->state == NULL) {
  513. return Z_STREAM_ERROR;
  514. }
  515. ss = (deflate_state *) source->state;
  516. *dest = *source;
  517. mem = (deflate_workspace *) dest->workspace;
  518. ds = &(mem->deflate_memory);
  519. dest->state = (struct internal_state *) ds;
  520. *ds = *ss;
  521. ds->strm = dest;
  522. ds->window = (Byte *) mem->window_memory;
  523. ds->prev = (Pos *) mem->prev_memory;
  524. ds->head = (Pos *) mem->head_memory;
  525. overlay = (ush *) mem->overlay_memory;
  526. ds->pending_buf = (uch *) overlay;
  527. memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  528. memcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
  529. memcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
  530. memcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  531. ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  532. ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
  533. ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
  534. ds->l_desc.dyn_tree = ds->dyn_ltree;
  535. ds->d_desc.dyn_tree = ds->dyn_dtree;
  536. ds->bl_desc.dyn_tree = ds->bl_tree;
  537. return Z_OK;
  538. #endif
  539. }
  540. #endif /* 0 */
  541. /* ===========================================================================
  542. * Read a new buffer from the current input stream, update the adler32
  543. * and total number of bytes read. All deflate() input goes through
  544. * this function so some applications may wish to modify it to avoid
  545. * allocating a large strm->next_in buffer and copying from it.
  546. * (See also flush_pending()).
  547. */
  548. static int read_buf(
  549. z_streamp strm,
  550. Byte *buf,
  551. unsigned size
  552. )
  553. {
  554. unsigned len = strm->avail_in;
  555. if (len > size) len = size;
  556. if (len == 0) return 0;
  557. strm->avail_in -= len;
  558. if (!((deflate_state *)(strm->state))->noheader) {
  559. strm->adler = zlib_adler32(strm->adler, strm->next_in, len);
  560. }
  561. memcpy(buf, strm->next_in, len);
  562. strm->next_in += len;
  563. strm->total_in += len;
  564. return (int)len;
  565. }
  566. /* ===========================================================================
  567. * Initialize the "longest match" routines for a new zlib stream
  568. */
  569. static void lm_init(
  570. deflate_state *s
  571. )
  572. {
  573. s->window_size = (ulg)2L*s->w_size;
  574. CLEAR_HASH(s);
  575. /* Set the default configuration parameters:
  576. */
  577. s->max_lazy_match = configuration_table[s->level].max_lazy;
  578. s->good_match = configuration_table[s->level].good_length;
  579. s->nice_match = configuration_table[s->level].nice_length;
  580. s->max_chain_length = configuration_table[s->level].max_chain;
  581. s->strstart = 0;
  582. s->block_start = 0L;
  583. s->lookahead = 0;
  584. s->match_length = s->prev_length = MIN_MATCH-1;
  585. s->match_available = 0;
  586. s->ins_h = 0;
  587. }
  588. /* ===========================================================================
  589. * Set match_start to the longest match starting at the given string and
  590. * return its length. Matches shorter or equal to prev_length are discarded,
  591. * in which case the result is equal to prev_length and match_start is
  592. * garbage.
  593. * IN assertions: cur_match is the head of the hash chain for the current
  594. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  595. * OUT assertion: the match length is not greater than s->lookahead.
  596. */
  597. /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  598. * match.S. The code will be functionally equivalent.
  599. */
  600. static uInt longest_match(
  601. deflate_state *s,
  602. IPos cur_match /* current match */
  603. )
  604. {
  605. unsigned chain_length = s->max_chain_length;/* max hash chain length */
  606. register Byte *scan = s->window + s->strstart; /* current string */
  607. register Byte *match; /* matched string */
  608. register int len; /* length of current match */
  609. int best_len = s->prev_length; /* best match length so far */
  610. int nice_match = s->nice_match; /* stop if match long enough */
  611. IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  612. s->strstart - (IPos)MAX_DIST(s) : NIL;
  613. /* Stop when cur_match becomes <= limit. To simplify the code,
  614. * we prevent matches with the string of window index 0.
  615. */
  616. Pos *prev = s->prev;
  617. uInt wmask = s->w_mask;
  618. #ifdef UNALIGNED_OK
  619. /* Compare two bytes at a time. Note: this is not always beneficial.
  620. * Try with and without -DUNALIGNED_OK to check.
  621. */
  622. register Byte *strend = s->window + s->strstart + MAX_MATCH - 1;
  623. register ush scan_start = *(ush*)scan;
  624. register ush scan_end = *(ush*)(scan+best_len-1);
  625. #else
  626. register Byte *strend = s->window + s->strstart + MAX_MATCH;
  627. register Byte scan_end1 = scan[best_len-1];
  628. register Byte scan_end = scan[best_len];
  629. #endif
  630. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  631. * It is easy to get rid of this optimization if necessary.
  632. */
  633. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  634. /* Do not waste too much time if we already have a good match: */
  635. if (s->prev_length >= s->good_match) {
  636. chain_length >>= 2;
  637. }
  638. /* Do not look for matches beyond the end of the input. This is necessary
  639. * to make deflate deterministic.
  640. */
  641. if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  642. Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  643. do {
  644. Assert(cur_match < s->strstart, "no future");
  645. match = s->window + cur_match;
  646. /* Skip to next match if the match length cannot increase
  647. * or if the match length is less than 2:
  648. */
  649. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  650. /* This code assumes sizeof(unsigned short) == 2. Do not use
  651. * UNALIGNED_OK if your compiler uses a different size.
  652. */
  653. if (*(ush*)(match+best_len-1) != scan_end ||
  654. *(ush*)match != scan_start) continue;
  655. /* It is not necessary to compare scan[2] and match[2] since they are
  656. * always equal when the other bytes match, given that the hash keys
  657. * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  658. * strstart+3, +5, ... up to strstart+257. We check for insufficient
  659. * lookahead only every 4th comparison; the 128th check will be made
  660. * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  661. * necessary to put more guard bytes at the end of the window, or
  662. * to check more often for insufficient lookahead.
  663. */
  664. Assert(scan[2] == match[2], "scan[2]?");
  665. scan++, match++;
  666. do {
  667. } while (*(ush*)(scan+=2) == *(ush*)(match+=2) &&
  668. *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  669. *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  670. *(ush*)(scan+=2) == *(ush*)(match+=2) &&
  671. scan < strend);
  672. /* The funny "do {}" generates better code on most compilers */
  673. /* Here, scan <= window+strstart+257 */
  674. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  675. if (*scan == *match) scan++;
  676. len = (MAX_MATCH - 1) - (int)(strend-scan);
  677. scan = strend - (MAX_MATCH-1);
  678. #else /* UNALIGNED_OK */
  679. if (match[best_len] != scan_end ||
  680. match[best_len-1] != scan_end1 ||
  681. *match != *scan ||
  682. *++match != scan[1]) continue;
  683. /* The check at best_len-1 can be removed because it will be made
  684. * again later. (This heuristic is not always a win.)
  685. * It is not necessary to compare scan[2] and match[2] since they
  686. * are always equal when the other bytes match, given that
  687. * the hash keys are equal and that HASH_BITS >= 8.
  688. */
  689. scan += 2, match++;
  690. Assert(*scan == *match, "match[2]?");
  691. /* We check for insufficient lookahead only every 8th comparison;
  692. * the 256th check will be made at strstart+258.
  693. */
  694. do {
  695. } while (*++scan == *++match && *++scan == *++match &&
  696. *++scan == *++match && *++scan == *++match &&
  697. *++scan == *++match && *++scan == *++match &&
  698. *++scan == *++match && *++scan == *++match &&
  699. scan < strend);
  700. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  701. len = MAX_MATCH - (int)(strend - scan);
  702. scan = strend - MAX_MATCH;
  703. #endif /* UNALIGNED_OK */
  704. if (len > best_len) {
  705. s->match_start = cur_match;
  706. best_len = len;
  707. if (len >= nice_match) break;
  708. #ifdef UNALIGNED_OK
  709. scan_end = *(ush*)(scan+best_len-1);
  710. #else
  711. scan_end1 = scan[best_len-1];
  712. scan_end = scan[best_len];
  713. #endif
  714. }
  715. } while ((cur_match = prev[cur_match & wmask]) > limit
  716. && --chain_length != 0);
  717. if ((uInt)best_len <= s->lookahead) return best_len;
  718. return s->lookahead;
  719. }
  720. #ifdef DEBUG_ZLIB
  721. /* ===========================================================================
  722. * Check that the match at match_start is indeed a match.
  723. */
  724. static void check_match(
  725. deflate_state *s,
  726. IPos start,
  727. IPos match,
  728. int length
  729. )
  730. {
  731. /* check that the match is indeed a match */
  732. if (memcmp((char *)s->window + match,
  733. (char *)s->window + start, length) != EQUAL) {
  734. fprintf(stderr, " start %u, match %u, length %d\n",
  735. start, match, length);
  736. do {
  737. fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  738. } while (--length != 0);
  739. z_error("invalid match");
  740. }
  741. if (z_verbose > 1) {
  742. fprintf(stderr,"\\[%d,%d]", start-match, length);
  743. do { putc(s->window[start++], stderr); } while (--length != 0);
  744. }
  745. }
  746. #else
  747. # define check_match(s, start, match, length)
  748. #endif
  749. /* ===========================================================================
  750. * Fill the window when the lookahead becomes insufficient.
  751. * Updates strstart and lookahead.
  752. *
  753. * IN assertion: lookahead < MIN_LOOKAHEAD
  754. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  755. * At least one byte has been read, or avail_in == 0; reads are
  756. * performed for at least two bytes (required for the zip translate_eol
  757. * option -- not supported here).
  758. */
  759. static void fill_window(
  760. deflate_state *s
  761. )
  762. {
  763. register unsigned n, m;
  764. register Pos *p;
  765. unsigned more; /* Amount of free space at the end of the window. */
  766. uInt wsize = s->w_size;
  767. do {
  768. more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  769. /* Deal with !@#$% 64K limit: */
  770. if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  771. more = wsize;
  772. } else if (more == (unsigned)(-1)) {
  773. /* Very unlikely, but possible on 16 bit machine if strstart == 0
  774. * and lookahead == 1 (input done one byte at time)
  775. */
  776. more--;
  777. /* If the window is almost full and there is insufficient lookahead,
  778. * move the upper half to the lower one to make room in the upper half.
  779. */
  780. } else if (s->strstart >= wsize+MAX_DIST(s)) {
  781. memcpy((char *)s->window, (char *)s->window+wsize,
  782. (unsigned)wsize);
  783. s->match_start -= wsize;
  784. s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  785. s->block_start -= (long) wsize;
  786. /* Slide the hash table (could be avoided with 32 bit values
  787. at the expense of memory usage). We slide even when level == 0
  788. to keep the hash table consistent if we switch back to level > 0
  789. later. (Using level 0 permanently is not an optimal usage of
  790. zlib, so we don't care about this pathological case.)
  791. */
  792. n = s->hash_size;
  793. p = &s->head[n];
  794. do {
  795. m = *--p;
  796. *p = (Pos)(m >= wsize ? m-wsize : NIL);
  797. } while (--n);
  798. n = wsize;
  799. p = &s->prev[n];
  800. do {
  801. m = *--p;
  802. *p = (Pos)(m >= wsize ? m-wsize : NIL);
  803. /* If n is not on any hash chain, prev[n] is garbage but
  804. * its value will never be used.
  805. */
  806. } while (--n);
  807. more += wsize;
  808. }
  809. if (s->strm->avail_in == 0) return;
  810. /* If there was no sliding:
  811. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  812. * more == window_size - lookahead - strstart
  813. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  814. * => more >= window_size - 2*WSIZE + 2
  815. * In the BIG_MEM or MMAP case (not yet supported),
  816. * window_size == input_size + MIN_LOOKAHEAD &&
  817. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  818. * Otherwise, window_size == 2*WSIZE so more >= 2.
  819. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  820. */
  821. Assert(more >= 2, "more < 2");
  822. n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  823. s->lookahead += n;
  824. /* Initialize the hash value now that we have some input: */
  825. if (s->lookahead >= MIN_MATCH) {
  826. s->ins_h = s->window[s->strstart];
  827. UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  828. #if MIN_MATCH != 3
  829. Call UPDATE_HASH() MIN_MATCH-3 more times
  830. #endif
  831. }
  832. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  833. * but this is not important since only literal bytes will be emitted.
  834. */
  835. } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  836. }
  837. /* ===========================================================================
  838. * Flush the current block, with given end-of-file flag.
  839. * IN assertion: strstart is set to the end of the current match.
  840. */
  841. #define FLUSH_BLOCK_ONLY(s, eof) { \
  842. zlib_tr_flush_block(s, (s->block_start >= 0L ? \
  843. (char *)&s->window[(unsigned)s->block_start] : \
  844. NULL), \
  845. (ulg)((long)s->strstart - s->block_start), \
  846. (eof)); \
  847. s->block_start = s->strstart; \
  848. flush_pending(s->strm); \
  849. Tracev((stderr,"[FLUSH]")); \
  850. }
  851. /* Same but force premature exit if necessary. */
  852. #define FLUSH_BLOCK(s, eof) { \
  853. FLUSH_BLOCK_ONLY(s, eof); \
  854. if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  855. }
  856. /* ===========================================================================
  857. * Copy without compression as much as possible from the input stream, return
  858. * the current block state.
  859. * This function does not insert new strings in the dictionary since
  860. * uncompressible data is probably not useful. This function is used
  861. * only for the level=0 compression option.
  862. * NOTE: this function should be optimized to avoid extra copying from
  863. * window to pending_buf.
  864. */
  865. static block_state deflate_stored(
  866. deflate_state *s,
  867. int flush
  868. )
  869. {
  870. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  871. * to pending_buf_size, and each stored block has a 5 byte header:
  872. */
  873. ulg max_block_size = 0xffff;
  874. ulg max_start;
  875. if (max_block_size > s->pending_buf_size - 5) {
  876. max_block_size = s->pending_buf_size - 5;
  877. }
  878. /* Copy as much as possible from input to output: */
  879. for (;;) {
  880. /* Fill the window as much as possible: */
  881. if (s->lookahead <= 1) {
  882. Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  883. s->block_start >= (long)s->w_size, "slide too late");
  884. fill_window(s);
  885. if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  886. if (s->lookahead == 0) break; /* flush the current block */
  887. }
  888. Assert(s->block_start >= 0L, "block gone");
  889. s->strstart += s->lookahead;
  890. s->lookahead = 0;
  891. /* Emit a stored block if pending_buf will be full: */
  892. max_start = s->block_start + max_block_size;
  893. if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  894. /* strstart == 0 is possible when wraparound on 16-bit machine */
  895. s->lookahead = (uInt)(s->strstart - max_start);
  896. s->strstart = (uInt)max_start;
  897. FLUSH_BLOCK(s, 0);
  898. }
  899. /* Flush if we may have to slide, otherwise block_start may become
  900. * negative and the data will be gone:
  901. */
  902. if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  903. FLUSH_BLOCK(s, 0);
  904. }
  905. }
  906. FLUSH_BLOCK(s, flush == Z_FINISH);
  907. return flush == Z_FINISH ? finish_done : block_done;
  908. }
  909. /* ===========================================================================
  910. * Compress as much as possible from the input stream, return the current
  911. * block state.
  912. * This function does not perform lazy evaluation of matches and inserts
  913. * new strings in the dictionary only for unmatched strings or for short
  914. * matches. It is used only for the fast compression options.
  915. */
  916. static block_state deflate_fast(
  917. deflate_state *s,
  918. int flush
  919. )
  920. {
  921. IPos hash_head = NIL; /* head of the hash chain */
  922. int bflush; /* set if current block must be flushed */
  923. for (;;) {
  924. /* Make sure that we always have enough lookahead, except
  925. * at the end of the input file. We need MAX_MATCH bytes
  926. * for the next match, plus MIN_MATCH bytes to insert the
  927. * string following the next match.
  928. */
  929. if (s->lookahead < MIN_LOOKAHEAD) {
  930. fill_window(s);
  931. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  932. return need_more;
  933. }
  934. if (s->lookahead == 0) break; /* flush the current block */
  935. }
  936. /* Insert the string window[strstart .. strstart+2] in the
  937. * dictionary, and set hash_head to the head of the hash chain:
  938. */
  939. if (s->lookahead >= MIN_MATCH) {
  940. INSERT_STRING(s, s->strstart, hash_head);
  941. }
  942. /* Find the longest match, discarding those <= prev_length.
  943. * At this point we have always match_length < MIN_MATCH
  944. */
  945. if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  946. /* To simplify the code, we prevent matches with the string
  947. * of window index 0 (in particular we have to avoid a match
  948. * of the string with itself at the start of the input file).
  949. */
  950. if (s->strategy != Z_HUFFMAN_ONLY) {
  951. s->match_length = longest_match (s, hash_head);
  952. }
  953. /* longest_match() sets match_start */
  954. }
  955. if (s->match_length >= MIN_MATCH) {
  956. check_match(s, s->strstart, s->match_start, s->match_length);
  957. bflush = zlib_tr_tally(s, s->strstart - s->match_start,
  958. s->match_length - MIN_MATCH);
  959. s->lookahead -= s->match_length;
  960. /* Insert new strings in the hash table only if the match length
  961. * is not too large. This saves time but degrades compression.
  962. */
  963. if (s->match_length <= s->max_insert_length &&
  964. s->lookahead >= MIN_MATCH) {
  965. s->match_length--; /* string at strstart already in hash table */
  966. do {
  967. s->strstart++;
  968. INSERT_STRING(s, s->strstart, hash_head);
  969. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  970. * always MIN_MATCH bytes ahead.
  971. */
  972. } while (--s->match_length != 0);
  973. s->strstart++;
  974. } else {
  975. s->strstart += s->match_length;
  976. s->match_length = 0;
  977. s->ins_h = s->window[s->strstart];
  978. UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  979. #if MIN_MATCH != 3
  980. Call UPDATE_HASH() MIN_MATCH-3 more times
  981. #endif
  982. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  983. * matter since it will be recomputed at next deflate call.
  984. */
  985. }
  986. } else {
  987. /* No match, output a literal byte */
  988. Tracevv((stderr,"%c", s->window[s->strstart]));
  989. bflush = zlib_tr_tally (s, 0, s->window[s->strstart]);
  990. s->lookahead--;
  991. s->strstart++;
  992. }
  993. if (bflush) FLUSH_BLOCK(s, 0);
  994. }
  995. FLUSH_BLOCK(s, flush == Z_FINISH);
  996. return flush == Z_FINISH ? finish_done : block_done;
  997. }
  998. /* ===========================================================================
  999. * Same as above, but achieves better compression. We use a lazy
  1000. * evaluation for matches: a match is finally adopted only if there is
  1001. * no better match at the next window position.
  1002. */
  1003. static block_state deflate_slow(
  1004. deflate_state *s,
  1005. int flush
  1006. )
  1007. {
  1008. IPos hash_head = NIL; /* head of hash chain */
  1009. int bflush; /* set if current block must be flushed */
  1010. /* Process the input block. */
  1011. for (;;) {
  1012. /* Make sure that we always have enough lookahead, except
  1013. * at the end of the input file. We need MAX_MATCH bytes
  1014. * for the next match, plus MIN_MATCH bytes to insert the
  1015. * string following the next match.
  1016. */
  1017. if (s->lookahead < MIN_LOOKAHEAD) {
  1018. fill_window(s);
  1019. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1020. return need_more;
  1021. }
  1022. if (s->lookahead == 0) break; /* flush the current block */
  1023. }
  1024. /* Insert the string window[strstart .. strstart+2] in the
  1025. * dictionary, and set hash_head to the head of the hash chain:
  1026. */
  1027. if (s->lookahead >= MIN_MATCH) {
  1028. INSERT_STRING(s, s->strstart, hash_head);
  1029. }
  1030. /* Find the longest match, discarding those <= prev_length.
  1031. */
  1032. s->prev_length = s->match_length, s->prev_match = s->match_start;
  1033. s->match_length = MIN_MATCH-1;
  1034. if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  1035. s->strstart - hash_head <= MAX_DIST(s)) {
  1036. /* To simplify the code, we prevent matches with the string
  1037. * of window index 0 (in particular we have to avoid a match
  1038. * of the string with itself at the start of the input file).
  1039. */
  1040. if (s->strategy != Z_HUFFMAN_ONLY) {
  1041. s->match_length = longest_match (s, hash_head);
  1042. }
  1043. /* longest_match() sets match_start */
  1044. if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
  1045. (s->match_length == MIN_MATCH &&
  1046. s->strstart - s->match_start > TOO_FAR))) {
  1047. /* If prev_match is also MIN_MATCH, match_start is garbage
  1048. * but we will ignore the current match anyway.
  1049. */
  1050. s->match_length = MIN_MATCH-1;
  1051. }
  1052. }
  1053. /* If there was a match at the previous step and the current
  1054. * match is not better, output the previous match:
  1055. */
  1056. if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  1057. uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  1058. /* Do not insert strings in hash table beyond this. */
  1059. check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  1060. bflush = zlib_tr_tally(s, s->strstart -1 - s->prev_match,
  1061. s->prev_length - MIN_MATCH);
  1062. /* Insert in hash table all strings up to the end of the match.
  1063. * strstart-1 and strstart are already inserted. If there is not
  1064. * enough lookahead, the last two strings are not inserted in
  1065. * the hash table.
  1066. */
  1067. s->lookahead -= s->prev_length-1;
  1068. s->prev_length -= 2;
  1069. do {
  1070. if (++s->strstart <= max_insert) {
  1071. INSERT_STRING(s, s->strstart, hash_head);
  1072. }
  1073. } while (--s->prev_length != 0);
  1074. s->match_available = 0;
  1075. s->match_length = MIN_MATCH-1;
  1076. s->strstart++;
  1077. if (bflush) FLUSH_BLOCK(s, 0);
  1078. } else if (s->match_available) {
  1079. /* If there was no match at the previous position, output a
  1080. * single literal. If there was a match but the current match
  1081. * is longer, truncate the previous match to a single literal.
  1082. */
  1083. Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1084. if (zlib_tr_tally (s, 0, s->window[s->strstart-1])) {
  1085. FLUSH_BLOCK_ONLY(s, 0);
  1086. }
  1087. s->strstart++;
  1088. s->lookahead--;
  1089. if (s->strm->avail_out == 0) return need_more;
  1090. } else {
  1091. /* There is no previous match to compare with, wait for
  1092. * the next step to decide.
  1093. */
  1094. s->match_available = 1;
  1095. s->strstart++;
  1096. s->lookahead--;
  1097. }
  1098. }
  1099. Assert (flush != Z_NO_FLUSH, "no flush?");
  1100. if (s->match_available) {
  1101. Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1102. zlib_tr_tally (s, 0, s->window[s->strstart-1]);
  1103. s->match_available = 0;
  1104. }
  1105. FLUSH_BLOCK(s, flush == Z_FINISH);
  1106. return flush == Z_FINISH ? finish_done : block_done;
  1107. }
  1108. int zlib_deflate_workspacesize(void)
  1109. {
  1110. return sizeof(deflate_workspace);
  1111. }