deflate.c 40 KB

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