deflate.c 40 KB

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