deflate.c 43 KB

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