deflate.c 40 KB

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