deflate.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. /* deflate.c -- compress data using the deflation algorithm
  2. * Copyright (C) 1995-2023 Jean-loup Gailly and Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. /*
  6. * ALGORITHM
  7. *
  8. * The "deflation" process depends on being able to identify portions
  9. * of the input text which are identical to earlier input (within a
  10. * sliding window trailing behind the input currently being processed).
  11. *
  12. * The most straightforward technique turns out to be the fastest for
  13. * most input files: try all possible matches and select the longest.
  14. * The key feature of this algorithm is that insertions into the string
  15. * dictionary are very simple and thus fast, and deletions are avoided
  16. * completely. Insertions are performed at each input character, whereas
  17. * string matches are performed only when the previous match ends. So it
  18. * is preferable to spend more time in matches to allow very fast string
  19. * insertions and avoid deletions. The matching algorithm for small
  20. * strings is inspired from that of Rabin & Karp. A brute force approach
  21. * is used to find longer strings when a small match has been found.
  22. * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
  23. * (by Leonid Broukhis).
  24. * A previous version of this file used a more sophisticated algorithm
  25. * (by Fiala and Greene) which is guaranteed to run in linear amortized
  26. * time, but has a larger average cost, uses more memory and is patented.
  27. * However the F&G algorithm may be faster for some highly redundant
  28. * files if the parameter max_chain_length (described below) is too large.
  29. *
  30. * ACKNOWLEDGEMENTS
  31. *
  32. * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
  33. * I found it in 'freeze' written by Leonid Broukhis.
  34. * Thanks to many people for bug reports and testing.
  35. *
  36. * REFERENCES
  37. *
  38. * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
  39. * Available in http://tools.ietf.org/html/rfc1951
  40. *
  41. * A description of the Rabin and Karp algorithm is given in the book
  42. * "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
  43. *
  44. * Fiala,E.R., and Greene,D.H.
  45. * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
  46. *
  47. */
  48. /* @(#) $Id$ */
  49. #include "deflate.h"
  50. #include <util/system/compiler.h>
  51. const char deflate_copyright[] =
  52. " deflate 1.3 Copyright 1995-2023 Jean-loup Gailly and Mark Adler ";
  53. /*
  54. If you use the zlib library in a product, an acknowledgment is welcome
  55. in the documentation of your product. If for some reason you cannot
  56. include such an acknowledgment, I would appreciate that you keep this
  57. copyright string in the executable of your product.
  58. */
  59. typedef enum {
  60. need_more, /* block not completed, need more input or more output */
  61. block_done, /* block flush performed */
  62. finish_started, /* finish started, need only more output at next deflate */
  63. finish_done /* finish done, accept no more input or output */
  64. } block_state;
  65. typedef block_state (*compress_func)(deflate_state *s, int flush);
  66. /* Compression function. Returns the block state after the call. */
  67. local block_state deflate_stored(deflate_state *s, int flush);
  68. local block_state deflate_fast(deflate_state *s, int flush);
  69. #ifndef FASTEST
  70. local block_state deflate_slow(deflate_state *s, int flush);
  71. #endif
  72. local block_state deflate_rle(deflate_state *s, int flush);
  73. local block_state deflate_huff(deflate_state *s, int flush);
  74. /* ===========================================================================
  75. * Local data
  76. */
  77. #define NIL 0
  78. /* Tail of hash chains */
  79. #ifndef TOO_FAR
  80. # define TOO_FAR 4096
  81. #endif
  82. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  83. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  84. * the desired pack level (0..9). The values given below have been tuned to
  85. * exclude worst case performance for pathological files. Better values may be
  86. * found for specific files.
  87. */
  88. typedef struct config_s {
  89. ush good_length; /* reduce lazy search above this match length */
  90. ush max_lazy; /* do not perform lazy search above this match length */
  91. ush nice_length; /* quit search above this match length */
  92. ush max_chain;
  93. compress_func func;
  94. } config;
  95. #ifdef FASTEST
  96. local const config configuration_table[2] = {
  97. /* good lazy nice chain */
  98. /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  99. /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
  100. #else
  101. local 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}, /* max 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}}; /* max compression */
  113. #endif
  114. /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  115. * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  116. * meaning.
  117. */
  118. /* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */
  119. #define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0))
  120. /* ===========================================================================
  121. * Update a hash value with the given input byte
  122. * IN assertion: all calls to UPDATE_HASH are made with consecutive input
  123. * characters, so that a running hash key can be computed from the previous
  124. * key instead of complete recalculation each time.
  125. */
  126. #define UPDATE_HASH(s,h,c) (h = (((h) << s->hash_shift) ^ (c)) & s->hash_mask)
  127. /* ===========================================================================
  128. * Insert string str in the dictionary and set match_head to the previous head
  129. * of the hash chain (the most recent string with same hash key). Return
  130. * the previous length of the hash chain.
  131. * If this file is compiled with -DFASTEST, the compression level is forced
  132. * to 1, and no hash chains are maintained.
  133. * IN assertion: all calls to INSERT_STRING are made with consecutive input
  134. * characters and the first MIN_MATCH bytes of str are valid (except for
  135. * the last MIN_MATCH-1 bytes of the input file).
  136. */
  137. #ifdef FASTEST
  138. #define INSERT_STRING(s, str, match_head) \
  139. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  140. match_head = s->head[s->ins_h], \
  141. s->head[s->ins_h] = (Pos)(str))
  142. #else
  143. #define INSERT_STRING(s, str, match_head) \
  144. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  145. match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
  146. s->head[s->ins_h] = (Pos)(str))
  147. #endif
  148. /* ===========================================================================
  149. * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  150. * prev[] will be initialized on the fly.
  151. */
  152. #define CLEAR_HASH(s) \
  153. do { \
  154. s->head[s->hash_size - 1] = NIL; \
  155. zmemzero((Bytef *)s->head, \
  156. (unsigned)(s->hash_size - 1)*sizeof(*s->head)); \
  157. } while (0)
  158. /* ===========================================================================
  159. * Slide the hash table when sliding the window down (could be avoided with 32
  160. * bit values at the expense of memory usage). We slide even when level == 0 to
  161. * keep the hash table consistent if we switch back to level > 0 later.
  162. */
  163. #if defined(__has_feature)
  164. # if __has_feature(memory_sanitizer)
  165. __attribute__((no_sanitize("memory")))
  166. # endif
  167. #endif
  168. local void slide_hash(deflate_state *s) {
  169. unsigned n, m;
  170. Posf *p;
  171. uInt wsize = s->w_size;
  172. n = s->hash_size;
  173. p = &s->head[n];
  174. do {
  175. m = *--p;
  176. *p = (Pos)(m >= wsize ? m - wsize : NIL);
  177. } while (--n);
  178. n = wsize;
  179. #ifndef FASTEST
  180. p = &s->prev[n];
  181. do {
  182. m = *--p;
  183. *p = (Pos)(m >= wsize ? m - wsize : NIL);
  184. /* If n is not on any hash chain, prev[n] is garbage but
  185. * its value will never be used.
  186. */
  187. } while (--n);
  188. #endif
  189. }
  190. /* ===========================================================================
  191. * Read a new buffer from the current input stream, update the adler32
  192. * and total number of bytes read. All deflate() input goes through
  193. * this function so some applications may wish to modify it to avoid
  194. * allocating a large strm->next_in buffer and copying from it.
  195. * (See also flush_pending()).
  196. */
  197. local unsigned read_buf(z_streamp strm, Bytef *buf, unsigned size) {
  198. unsigned len = strm->avail_in;
  199. if (len > size) len = size;
  200. if (len == 0) return 0;
  201. strm->avail_in -= len;
  202. zmemcpy(buf, strm->next_in, len);
  203. if (strm->state->wrap == 1) {
  204. strm->adler = adler32(strm->adler, buf, len);
  205. }
  206. #ifdef GZIP
  207. else if (strm->state->wrap == 2) {
  208. strm->adler = crc32(strm->adler, buf, len);
  209. }
  210. #endif
  211. strm->next_in += len;
  212. strm->total_in += len;
  213. return len;
  214. }
  215. /* ===========================================================================
  216. * Fill the window when the lookahead becomes insufficient.
  217. * Updates strstart and lookahead.
  218. *
  219. * IN assertion: lookahead < MIN_LOOKAHEAD
  220. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  221. * At least one byte has been read, or avail_in == 0; reads are
  222. * performed for at least two bytes (required for the zip translate_eol
  223. * option -- not supported here).
  224. */
  225. local void fill_window(deflate_state *s) {
  226. unsigned n;
  227. unsigned more; /* Amount of free space at the end of the window. */
  228. uInt wsize = s->w_size;
  229. Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead");
  230. do {
  231. more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  232. /* Deal with !@#$% 64K limit: */
  233. if (sizeof(int) <= 2) {
  234. if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  235. more = wsize;
  236. } else if (more == (unsigned)(-1)) {
  237. /* Very unlikely, but possible on 16 bit machine if
  238. * strstart == 0 && lookahead == 1 (input done a byte at time)
  239. */
  240. more--;
  241. }
  242. }
  243. /* If the window is almost full and there is insufficient lookahead,
  244. * move the upper half to the lower one to make room in the upper half.
  245. */
  246. if (s->strstart >= wsize + MAX_DIST(s)) {
  247. zmemcpy(s->window, s->window + wsize, (unsigned)wsize - more);
  248. s->match_start -= wsize;
  249. s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  250. s->block_start -= (long) wsize;
  251. if (s->insert > s->strstart)
  252. s->insert = s->strstart;
  253. slide_hash(s);
  254. more += wsize;
  255. }
  256. if (s->strm->avail_in == 0) break;
  257. /* If there was no sliding:
  258. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  259. * more == window_size - lookahead - strstart
  260. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  261. * => more >= window_size - 2*WSIZE + 2
  262. * In the BIG_MEM or MMAP case (not yet supported),
  263. * window_size == input_size + MIN_LOOKAHEAD &&
  264. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  265. * Otherwise, window_size == 2*WSIZE so more >= 2.
  266. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  267. */
  268. Assert(more >= 2, "more < 2");
  269. n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  270. s->lookahead += n;
  271. /* Initialize the hash value now that we have some input: */
  272. if (s->lookahead + s->insert >= MIN_MATCH) {
  273. uInt str = s->strstart - s->insert;
  274. s->ins_h = s->window[str];
  275. UPDATE_HASH(s, s->ins_h, s->window[str + 1]);
  276. #if MIN_MATCH != 3
  277. Call UPDATE_HASH() MIN_MATCH-3 more times
  278. #endif
  279. while (s->insert) {
  280. UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
  281. #ifndef FASTEST
  282. s->prev[str & s->w_mask] = s->head[s->ins_h];
  283. #endif
  284. s->head[s->ins_h] = (Pos)str;
  285. str++;
  286. s->insert--;
  287. if (s->lookahead + s->insert < MIN_MATCH)
  288. break;
  289. }
  290. }
  291. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  292. * but this is not important since only literal bytes will be emitted.
  293. */
  294. } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  295. /* If the WIN_INIT bytes after the end of the current data have never been
  296. * written, then zero those bytes in order to avoid memory check reports of
  297. * the use of uninitialized (or uninitialised as Julian writes) bytes by
  298. * the longest match routines. Update the high water mark for the next
  299. * time through here. WIN_INIT is set to MAX_MATCH since the longest match
  300. * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead.
  301. */
  302. if (s->high_water < s->window_size) {
  303. ulg curr = s->strstart + (ulg)(s->lookahead);
  304. ulg init;
  305. if (s->high_water < curr) {
  306. /* Previous high water mark below current data -- zero WIN_INIT
  307. * bytes or up to end of window, whichever is less.
  308. */
  309. init = s->window_size - curr;
  310. if (init > WIN_INIT)
  311. init = WIN_INIT;
  312. zmemzero(s->window + curr, (unsigned)init);
  313. s->high_water = curr + init;
  314. }
  315. else if (s->high_water < (ulg)curr + WIN_INIT) {
  316. /* High water mark at or above current data, but below current data
  317. * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up
  318. * to end of window, whichever is less.
  319. */
  320. init = (ulg)curr + WIN_INIT - s->high_water;
  321. if (init > s->window_size - s->high_water)
  322. init = s->window_size - s->high_water;
  323. zmemzero(s->window + s->high_water, (unsigned)init);
  324. s->high_water += init;
  325. }
  326. }
  327. Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  328. "not enough room for search");
  329. }
  330. /* ========================================================================= */
  331. int ZEXPORT deflateInit_(z_streamp strm, int level, const char *version,
  332. int stream_size) {
  333. return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
  334. Z_DEFAULT_STRATEGY, version, stream_size);
  335. /* To do: ignore strm->next_in if we use it as window */
  336. }
  337. /* ========================================================================= */
  338. int ZEXPORT deflateInit2_(z_streamp strm, int level, int method,
  339. int windowBits, int memLevel, int strategy,
  340. const char *version, int stream_size) {
  341. deflate_state *s;
  342. int wrap = 1;
  343. static const char my_version[] = ZLIB_VERSION;
  344. if (version == Z_NULL || version[0] != my_version[0] ||
  345. stream_size != sizeof(z_stream)) {
  346. return Z_VERSION_ERROR;
  347. }
  348. if (strm == Z_NULL) return Z_STREAM_ERROR;
  349. strm->msg = Z_NULL;
  350. if (strm->zalloc == (alloc_func)0) {
  351. #ifdef Z_SOLO
  352. return Z_STREAM_ERROR;
  353. #else
  354. strm->zalloc = zcalloc;
  355. strm->opaque = (voidpf)0;
  356. #endif
  357. }
  358. if (strm->zfree == (free_func)0)
  359. #ifdef Z_SOLO
  360. return Z_STREAM_ERROR;
  361. #else
  362. strm->zfree = zcfree;
  363. #endif
  364. #ifdef FASTEST
  365. if (level != 0) level = 1;
  366. #else
  367. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  368. #endif
  369. if (windowBits < 0) { /* suppress zlib wrapper */
  370. wrap = 0;
  371. if (windowBits < -15)
  372. return Z_STREAM_ERROR;
  373. windowBits = -windowBits;
  374. }
  375. #ifdef GZIP
  376. else if (windowBits > 15) {
  377. wrap = 2; /* write gzip wrapper instead */
  378. windowBits -= 16;
  379. }
  380. #endif
  381. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  382. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  383. strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) {
  384. return Z_STREAM_ERROR;
  385. }
  386. if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
  387. s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  388. if (s == Z_NULL) return Z_MEM_ERROR;
  389. strm->state = (struct internal_state FAR *)s;
  390. s->strm = strm;
  391. s->status = INIT_STATE; /* to pass state test in deflateReset() */
  392. s->wrap = wrap;
  393. s->gzhead = Z_NULL;
  394. s->w_bits = (uInt)windowBits;
  395. s->w_size = 1 << s->w_bits;
  396. s->w_mask = s->w_size - 1;
  397. s->hash_bits = (uInt)memLevel + 7;
  398. s->hash_size = 1 << s->hash_bits;
  399. s->hash_mask = s->hash_size - 1;
  400. s->hash_shift = ((s->hash_bits + MIN_MATCH-1) / MIN_MATCH);
  401. s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  402. memset(s->window, 0, s->w_size * 2*sizeof(Byte));
  403. s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
  404. memset(s->prev, 0, s->w_size * sizeof(Pos));
  405. s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
  406. s->high_water = 0; /* nothing written to s->window yet */
  407. s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  408. /* We overlay pending_buf and sym_buf. This works since the average size
  409. * for length/distance pairs over any compressed block is assured to be 31
  410. * bits or less.
  411. *
  412. * Analysis: The longest fixed codes are a length code of 8 bits plus 5
  413. * extra bits, for lengths 131 to 257. The longest fixed distance codes are
  414. * 5 bits plus 13 extra bits, for distances 16385 to 32768. The longest
  415. * possible fixed-codes length/distance pair is then 31 bits total.
  416. *
  417. * sym_buf starts one-fourth of the way into pending_buf. So there are
  418. * three bytes in sym_buf for every four bytes in pending_buf. Each symbol
  419. * in sym_buf is three bytes -- two for the distance and one for the
  420. * literal/length. As each symbol is consumed, the pointer to the next
  421. * sym_buf value to read moves forward three bytes. From that symbol, up to
  422. * 31 bits are written to pending_buf. The closest the written pending_buf
  423. * bits gets to the next sym_buf symbol to read is just before the last
  424. * code is written. At that time, 31*(n - 2) bits have been written, just
  425. * after 24*(n - 2) bits have been consumed from sym_buf. sym_buf starts at
  426. * 8*n bits into pending_buf. (Note that the symbol buffer fills when n - 1
  427. * symbols are written.) The closest the writing gets to what is unread is
  428. * then n + 14 bits. Here n is lit_bufsize, which is 16384 by default, and
  429. * can range from 128 to 32768.
  430. *
  431. * Therefore, at a minimum, there are 142 bits of space between what is
  432. * written and what is read in the overlain buffers, so the symbols cannot
  433. * be overwritten by the compressed data. That space is actually 139 bits,
  434. * due to the three-bit fixed-code block header.
  435. *
  436. * That covers the case where either Z_FIXED is specified, forcing fixed
  437. * codes, or when the use of fixed codes is chosen, because that choice
  438. * results in a smaller compressed block than dynamic codes. That latter
  439. * condition then assures that the above analysis also covers all dynamic
  440. * blocks. A dynamic-code block will only be chosen to be emitted if it has
  441. * fewer bits than a fixed-code block would for the same set of symbols.
  442. * Therefore its average symbol length is assured to be less than 31. So
  443. * the compressed data for a dynamic block also cannot overwrite the
  444. * symbols from which it is being constructed.
  445. */
  446. s->pending_buf = (uchf *) ZALLOC(strm, s->lit_bufsize, 4);
  447. s->pending_buf_size = (ulg)s->lit_bufsize * 4;
  448. if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  449. s->pending_buf == Z_NULL) {
  450. s->status = FINISH_STATE;
  451. strm->msg = ERR_MSG(Z_MEM_ERROR);
  452. deflateEnd (strm);
  453. return Z_MEM_ERROR;
  454. }
  455. s->sym_buf = s->pending_buf + s->lit_bufsize;
  456. s->sym_end = (s->lit_bufsize - 1) * 3;
  457. /* We avoid equality with lit_bufsize*3 because of wraparound at 64K
  458. * on 16 bit machines and because stored blocks are restricted to
  459. * 64K-1 bytes.
  460. */
  461. s->level = level;
  462. s->strategy = strategy;
  463. s->method = (Byte)method;
  464. return deflateReset(strm);
  465. }
  466. /* =========================================================================
  467. * Check for a valid deflate stream state. Return 0 if ok, 1 if not.
  468. */
  469. local int deflateStateCheck(z_streamp strm) {
  470. deflate_state *s;
  471. if (strm == Z_NULL ||
  472. strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
  473. return 1;
  474. s = strm->state;
  475. if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE &&
  476. #ifdef GZIP
  477. s->status != GZIP_STATE &&
  478. #endif
  479. s->status != EXTRA_STATE &&
  480. s->status != NAME_STATE &&
  481. s->status != COMMENT_STATE &&
  482. s->status != HCRC_STATE &&
  483. s->status != BUSY_STATE &&
  484. s->status != FINISH_STATE))
  485. return 1;
  486. return 0;
  487. }
  488. /* ========================================================================= */
  489. int ZEXPORT deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
  490. uInt dictLength) {
  491. deflate_state *s;
  492. uInt str, n;
  493. int wrap;
  494. unsigned avail;
  495. z_const unsigned char *next;
  496. if (deflateStateCheck(strm) || dictionary == Z_NULL)
  497. return Z_STREAM_ERROR;
  498. s = strm->state;
  499. wrap = s->wrap;
  500. if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead)
  501. return Z_STREAM_ERROR;
  502. /* when using zlib wrappers, compute Adler-32 for provided dictionary */
  503. if (wrap == 1)
  504. strm->adler = adler32(strm->adler, dictionary, dictLength);
  505. s->wrap = 0; /* avoid computing Adler-32 in read_buf */
  506. /* if dictionary would fill window, just replace the history */
  507. if (dictLength >= s->w_size) {
  508. if (wrap == 0) { /* already empty otherwise */
  509. CLEAR_HASH(s);
  510. s->strstart = 0;
  511. s->block_start = 0L;
  512. s->insert = 0;
  513. }
  514. dictionary += dictLength - s->w_size; /* use the tail */
  515. dictLength = s->w_size;
  516. }
  517. /* insert dictionary into window and hash */
  518. avail = strm->avail_in;
  519. next = strm->next_in;
  520. strm->avail_in = dictLength;
  521. strm->next_in = (z_const Bytef *)dictionary;
  522. fill_window(s);
  523. while (s->lookahead >= MIN_MATCH) {
  524. str = s->strstart;
  525. n = s->lookahead - (MIN_MATCH-1);
  526. do {
  527. UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]);
  528. #ifndef FASTEST
  529. s->prev[str & s->w_mask] = s->head[s->ins_h];
  530. #endif
  531. s->head[s->ins_h] = (Pos)str;
  532. str++;
  533. } while (--n);
  534. s->strstart = str;
  535. s->lookahead = MIN_MATCH-1;
  536. fill_window(s);
  537. }
  538. s->strstart += s->lookahead;
  539. s->block_start = (long)s->strstart;
  540. s->insert = s->lookahead;
  541. s->lookahead = 0;
  542. s->match_length = s->prev_length = MIN_MATCH-1;
  543. s->match_available = 0;
  544. strm->next_in = next;
  545. strm->avail_in = avail;
  546. s->wrap = wrap;
  547. return Z_OK;
  548. }
  549. /* ========================================================================= */
  550. int ZEXPORT deflateGetDictionary(z_streamp strm, Bytef *dictionary,
  551. uInt *dictLength) {
  552. deflate_state *s;
  553. uInt len;
  554. if (deflateStateCheck(strm))
  555. return Z_STREAM_ERROR;
  556. s = strm->state;
  557. len = s->strstart + s->lookahead;
  558. if (len > s->w_size)
  559. len = s->w_size;
  560. if (dictionary != Z_NULL && len)
  561. zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len);
  562. if (dictLength != Z_NULL)
  563. *dictLength = len;
  564. return Z_OK;
  565. }
  566. /* ========================================================================= */
  567. int ZEXPORT deflateResetKeep(z_streamp strm) {
  568. deflate_state *s;
  569. if (deflateStateCheck(strm)) {
  570. return Z_STREAM_ERROR;
  571. }
  572. strm->total_in = strm->total_out = 0;
  573. strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  574. strm->data_type = Z_UNKNOWN;
  575. s = (deflate_state *)strm->state;
  576. s->pending = 0;
  577. s->pending_out = s->pending_buf;
  578. if (s->wrap < 0) {
  579. s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
  580. }
  581. s->status =
  582. #ifdef GZIP
  583. s->wrap == 2 ? GZIP_STATE :
  584. #endif
  585. INIT_STATE;
  586. strm->adler =
  587. #ifdef GZIP
  588. s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
  589. #endif
  590. adler32(0L, Z_NULL, 0);
  591. s->last_flush = -2;
  592. _tr_init(s);
  593. return Z_OK;
  594. }
  595. /* ===========================================================================
  596. * Initialize the "longest match" routines for a new zlib stream
  597. */
  598. local void lm_init(deflate_state *s) {
  599. s->window_size = (ulg)2L*s->w_size;
  600. CLEAR_HASH(s);
  601. /* Set the default configuration parameters:
  602. */
  603. s->max_lazy_match = configuration_table[s->level].max_lazy;
  604. s->good_match = configuration_table[s->level].good_length;
  605. s->nice_match = configuration_table[s->level].nice_length;
  606. s->max_chain_length = configuration_table[s->level].max_chain;
  607. s->strstart = 0;
  608. s->block_start = 0L;
  609. s->lookahead = 0;
  610. s->insert = 0;
  611. s->match_length = s->prev_length = MIN_MATCH-1;
  612. s->match_available = 0;
  613. s->ins_h = 0;
  614. }
  615. /* ========================================================================= */
  616. int ZEXPORT deflateReset(z_streamp strm) {
  617. int ret;
  618. ret = deflateResetKeep(strm);
  619. if (ret == Z_OK)
  620. lm_init(strm->state);
  621. return ret;
  622. }
  623. /* ========================================================================= */
  624. int ZEXPORT deflateSetHeader(z_streamp strm, gz_headerp head) {
  625. if (deflateStateCheck(strm) || strm->state->wrap != 2)
  626. return Z_STREAM_ERROR;
  627. strm->state->gzhead = head;
  628. return Z_OK;
  629. }
  630. /* ========================================================================= */
  631. int ZEXPORT deflatePending(z_streamp strm, unsigned *pending, int *bits) {
  632. if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
  633. if (pending != Z_NULL)
  634. *pending = strm->state->pending;
  635. if (bits != Z_NULL)
  636. *bits = strm->state->bi_valid;
  637. return Z_OK;
  638. }
  639. /* ========================================================================= */
  640. int ZEXPORT deflatePrime(z_streamp strm, int bits, int value) {
  641. deflate_state *s;
  642. int put;
  643. if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
  644. s = strm->state;
  645. if (bits < 0 || bits > 16 ||
  646. s->sym_buf < s->pending_out + ((Buf_size + 7) >> 3))
  647. return Z_BUF_ERROR;
  648. do {
  649. put = Buf_size - s->bi_valid;
  650. if (put > bits)
  651. put = bits;
  652. s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid);
  653. s->bi_valid += put;
  654. _tr_flush_bits(s);
  655. value >>= put;
  656. bits -= put;
  657. } while (bits);
  658. return Z_OK;
  659. }
  660. /* ========================================================================= */
  661. int ZEXPORT deflateParams(z_streamp strm, int level, int strategy) {
  662. deflate_state *s;
  663. compress_func func;
  664. if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
  665. s = strm->state;
  666. #ifdef FASTEST
  667. if (level != 0) level = 1;
  668. #else
  669. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  670. #endif
  671. if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
  672. return Z_STREAM_ERROR;
  673. }
  674. func = configuration_table[s->level].func;
  675. if ((strategy != s->strategy || func != configuration_table[level].func) &&
  676. s->last_flush != -2) {
  677. /* Flush the last buffer: */
  678. int err = deflate(strm, Z_BLOCK);
  679. if (err == Z_STREAM_ERROR)
  680. return err;
  681. if (strm->avail_in || (s->strstart - s->block_start) + s->lookahead)
  682. return Z_BUF_ERROR;
  683. }
  684. if (s->level != level) {
  685. if (s->level == 0 && s->matches != 0) {
  686. if (s->matches == 1)
  687. slide_hash(s);
  688. else
  689. CLEAR_HASH(s);
  690. s->matches = 0;
  691. }
  692. s->level = level;
  693. s->max_lazy_match = configuration_table[level].max_lazy;
  694. s->good_match = configuration_table[level].good_length;
  695. s->nice_match = configuration_table[level].nice_length;
  696. s->max_chain_length = configuration_table[level].max_chain;
  697. }
  698. s->strategy = strategy;
  699. return Z_OK;
  700. }
  701. /* ========================================================================= */
  702. int ZEXPORT deflateTune(z_streamp strm, int good_length, int max_lazy,
  703. int nice_length, int max_chain) {
  704. deflate_state *s;
  705. if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
  706. s = strm->state;
  707. s->good_match = (uInt)good_length;
  708. s->max_lazy_match = (uInt)max_lazy;
  709. s->nice_match = nice_length;
  710. s->max_chain_length = (uInt)max_chain;
  711. return Z_OK;
  712. }
  713. /* =========================================================================
  714. * For the default windowBits of 15 and memLevel of 8, this function returns a
  715. * close to exact, as well as small, upper bound on the compressed size. This
  716. * is an expansion of ~0.03%, plus a small constant.
  717. *
  718. * For any setting other than those defaults for windowBits and memLevel, one
  719. * of two worst case bounds is returned. This is at most an expansion of ~4% or
  720. * ~13%, plus a small constant.
  721. *
  722. * Both the 0.03% and 4% derive from the overhead of stored blocks. The first
  723. * one is for stored blocks of 16383 bytes (memLevel == 8), whereas the second
  724. * is for stored blocks of 127 bytes (the worst case memLevel == 1). The
  725. * expansion results from five bytes of header for each stored block.
  726. *
  727. * The larger expansion of 13% results from a window size less than or equal to
  728. * the symbols buffer size (windowBits <= memLevel + 7). In that case some of
  729. * the data being compressed may have slid out of the sliding window, impeding
  730. * a stored block from being emitted. Then the only choice is a fixed or
  731. * dynamic block, where a fixed block limits the maximum expansion to 9 bits
  732. * per 8-bit byte, plus 10 bits for every block. The smallest block size for
  733. * which this can occur is 255 (memLevel == 2).
  734. *
  735. * Shifts are used to approximate divisions, for speed.
  736. */
  737. uLong ZEXPORT deflateBound(z_streamp strm, uLong sourceLen) {
  738. deflate_state *s;
  739. uLong fixedlen, storelen, wraplen;
  740. /* upper bound for fixed blocks with 9-bit literals and length 255
  741. (memLevel == 2, which is the lowest that may not use stored blocks) --
  742. ~13% overhead plus a small constant */
  743. fixedlen = sourceLen + (sourceLen >> 3) + (sourceLen >> 8) +
  744. (sourceLen >> 9) + 4;
  745. /* upper bound for stored blocks with length 127 (memLevel == 1) --
  746. ~4% overhead plus a small constant */
  747. storelen = sourceLen + (sourceLen >> 5) + (sourceLen >> 7) +
  748. (sourceLen >> 11) + 7;
  749. /* if can't get parameters, return larger bound plus a zlib wrapper */
  750. if (deflateStateCheck(strm))
  751. return (fixedlen > storelen ? fixedlen : storelen) + 6;
  752. /* compute wrapper length */
  753. s = strm->state;
  754. switch (s->wrap) {
  755. case 0: /* raw deflate */
  756. wraplen = 0;
  757. break;
  758. case 1: /* zlib wrapper */
  759. wraplen = 6 + (s->strstart ? 4 : 0);
  760. break;
  761. #ifdef GZIP
  762. case 2: /* gzip wrapper */
  763. wraplen = 18;
  764. if (s->gzhead != Z_NULL) { /* user-supplied gzip header */
  765. Bytef *str;
  766. if (s->gzhead->extra != Z_NULL)
  767. wraplen += 2 + s->gzhead->extra_len;
  768. str = s->gzhead->name;
  769. if (str != Z_NULL)
  770. do {
  771. wraplen++;
  772. } while (*str++);
  773. str = s->gzhead->comment;
  774. if (str != Z_NULL)
  775. do {
  776. wraplen++;
  777. } while (*str++);
  778. if (s->gzhead->hcrc)
  779. wraplen += 2;
  780. }
  781. break;
  782. #endif
  783. default: /* for compiler happiness */
  784. wraplen = 6;
  785. }
  786. /* if not default parameters, return one of the conservative bounds */
  787. if (s->w_bits != 15 || s->hash_bits != 8 + 7)
  788. return (s->w_bits <= s->hash_bits && s->level ? fixedlen : storelen) +
  789. wraplen;
  790. /* default settings: return tight bound for that case -- ~0.03% overhead
  791. plus a small constant */
  792. return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
  793. (sourceLen >> 25) + 13 - 6 + wraplen;
  794. }
  795. /* =========================================================================
  796. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  797. * IN assertion: the stream state is correct and there is enough room in
  798. * pending_buf.
  799. */
  800. local void putShortMSB(deflate_state *s, uInt b) {
  801. put_byte(s, (Byte)(b >> 8));
  802. put_byte(s, (Byte)(b & 0xff));
  803. }
  804. /* =========================================================================
  805. * Flush as much pending output as possible. All deflate() output, except for
  806. * some deflate_stored() output, goes through this function so some
  807. * applications may wish to modify it to avoid allocating a large
  808. * strm->next_out buffer and copying into it. (See also read_buf()).
  809. */
  810. local void flush_pending(z_streamp strm) {
  811. unsigned len;
  812. deflate_state *s = strm->state;
  813. _tr_flush_bits(s);
  814. len = s->pending;
  815. if (len > strm->avail_out) len = strm->avail_out;
  816. if (len == 0) return;
  817. zmemcpy(strm->next_out, s->pending_out, len);
  818. strm->next_out += len;
  819. s->pending_out += len;
  820. strm->total_out += len;
  821. strm->avail_out -= len;
  822. s->pending -= len;
  823. if (s->pending == 0) {
  824. s->pending_out = s->pending_buf;
  825. }
  826. }
  827. /* ===========================================================================
  828. * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1].
  829. */
  830. #define HCRC_UPDATE(beg) \
  831. do { \
  832. if (s->gzhead->hcrc && s->pending > (beg)) \
  833. strm->adler = crc32(strm->adler, s->pending_buf + (beg), \
  834. s->pending - (beg)); \
  835. } while (0)
  836. /* ========================================================================= */
  837. int ZEXPORT deflate(z_streamp strm, int flush) {
  838. int old_flush; /* value of flush param for previous deflate call */
  839. deflate_state *s;
  840. if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) {
  841. return Z_STREAM_ERROR;
  842. }
  843. s = strm->state;
  844. if (strm->next_out == Z_NULL ||
  845. (strm->avail_in != 0 && strm->next_in == Z_NULL) ||
  846. (s->status == FINISH_STATE && flush != Z_FINISH)) {
  847. ERR_RETURN(strm, Z_STREAM_ERROR);
  848. }
  849. if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  850. old_flush = s->last_flush;
  851. s->last_flush = flush;
  852. /* Flush as much pending output as possible */
  853. if (s->pending != 0) {
  854. flush_pending(strm);
  855. if (strm->avail_out == 0) {
  856. /* Since avail_out is 0, deflate will be called again with
  857. * more output space, but possibly with both pending and
  858. * avail_in equal to zero. There won't be anything to do,
  859. * but this is not an error situation so make sure we
  860. * return OK instead of BUF_ERROR at next call of deflate:
  861. */
  862. s->last_flush = -1;
  863. return Z_OK;
  864. }
  865. /* Make sure there is something to do and avoid duplicate consecutive
  866. * flushes. For repeated and useless calls with Z_FINISH, we keep
  867. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  868. */
  869. } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) &&
  870. flush != Z_FINISH) {
  871. ERR_RETURN(strm, Z_BUF_ERROR);
  872. }
  873. /* User must not provide more input after the first FINISH: */
  874. if (s->status == FINISH_STATE && strm->avail_in != 0) {
  875. ERR_RETURN(strm, Z_BUF_ERROR);
  876. }
  877. /* Write the header */
  878. if (s->status == INIT_STATE && s->wrap == 0)
  879. s->status = BUSY_STATE;
  880. if (s->status == INIT_STATE) {
  881. /* zlib header */
  882. uInt header = (Z_DEFLATED + ((s->w_bits - 8) << 4)) << 8;
  883. uInt level_flags;
  884. if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
  885. level_flags = 0;
  886. else if (s->level < 6)
  887. level_flags = 1;
  888. else if (s->level == 6)
  889. level_flags = 2;
  890. else
  891. level_flags = 3;
  892. header |= (level_flags << 6);
  893. if (s->strstart != 0) header |= PRESET_DICT;
  894. header += 31 - (header % 31);
  895. putShortMSB(s, header);
  896. /* Save the adler32 of the preset dictionary: */
  897. if (s->strstart != 0) {
  898. putShortMSB(s, (uInt)(strm->adler >> 16));
  899. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  900. }
  901. strm->adler = adler32(0L, Z_NULL, 0);
  902. s->status = BUSY_STATE;
  903. /* Compression must start with an empty pending buffer */
  904. flush_pending(strm);
  905. if (s->pending != 0) {
  906. s->last_flush = -1;
  907. return Z_OK;
  908. }
  909. }
  910. #ifdef GZIP
  911. if (s->status == GZIP_STATE) {
  912. /* gzip header */
  913. strm->adler = crc32(0L, Z_NULL, 0);
  914. put_byte(s, 31);
  915. put_byte(s, 139);
  916. put_byte(s, 8);
  917. if (s->gzhead == Z_NULL) {
  918. put_byte(s, 0);
  919. put_byte(s, 0);
  920. put_byte(s, 0);
  921. put_byte(s, 0);
  922. put_byte(s, 0);
  923. put_byte(s, s->level == 9 ? 2 :
  924. (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
  925. 4 : 0));
  926. put_byte(s, OS_CODE);
  927. s->status = BUSY_STATE;
  928. /* Compression must start with an empty pending buffer */
  929. flush_pending(strm);
  930. if (s->pending != 0) {
  931. s->last_flush = -1;
  932. return Z_OK;
  933. }
  934. }
  935. else {
  936. put_byte(s, (s->gzhead->text ? 1 : 0) +
  937. (s->gzhead->hcrc ? 2 : 0) +
  938. (s->gzhead->extra == Z_NULL ? 0 : 4) +
  939. (s->gzhead->name == Z_NULL ? 0 : 8) +
  940. (s->gzhead->comment == Z_NULL ? 0 : 16)
  941. );
  942. put_byte(s, (Byte)(s->gzhead->time & 0xff));
  943. put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
  944. put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
  945. put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
  946. put_byte(s, s->level == 9 ? 2 :
  947. (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
  948. 4 : 0));
  949. put_byte(s, s->gzhead->os & 0xff);
  950. if (s->gzhead->extra != Z_NULL) {
  951. put_byte(s, s->gzhead->extra_len & 0xff);
  952. put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
  953. }
  954. if (s->gzhead->hcrc)
  955. strm->adler = crc32(strm->adler, s->pending_buf,
  956. s->pending);
  957. s->gzindex = 0;
  958. s->status = EXTRA_STATE;
  959. }
  960. }
  961. if (s->status == EXTRA_STATE) {
  962. if (s->gzhead->extra != Z_NULL) {
  963. ulg beg = s->pending; /* start of bytes to update crc */
  964. uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex;
  965. while (s->pending + left > s->pending_buf_size) {
  966. uInt copy = s->pending_buf_size - s->pending;
  967. zmemcpy(s->pending_buf + s->pending,
  968. s->gzhead->extra + s->gzindex, copy);
  969. s->pending = s->pending_buf_size;
  970. HCRC_UPDATE(beg);
  971. s->gzindex += copy;
  972. flush_pending(strm);
  973. if (s->pending != 0) {
  974. s->last_flush = -1;
  975. return Z_OK;
  976. }
  977. beg = 0;
  978. left -= copy;
  979. }
  980. zmemcpy(s->pending_buf + s->pending,
  981. s->gzhead->extra + s->gzindex, left);
  982. s->pending += left;
  983. HCRC_UPDATE(beg);
  984. s->gzindex = 0;
  985. }
  986. s->status = NAME_STATE;
  987. }
  988. if (s->status == NAME_STATE) {
  989. if (s->gzhead->name != Z_NULL) {
  990. ulg beg = s->pending; /* start of bytes to update crc */
  991. int val;
  992. do {
  993. if (s->pending == s->pending_buf_size) {
  994. HCRC_UPDATE(beg);
  995. flush_pending(strm);
  996. if (s->pending != 0) {
  997. s->last_flush = -1;
  998. return Z_OK;
  999. }
  1000. beg = 0;
  1001. }
  1002. val = s->gzhead->name[s->gzindex++];
  1003. put_byte(s, val);
  1004. } while (val != 0);
  1005. HCRC_UPDATE(beg);
  1006. s->gzindex = 0;
  1007. }
  1008. s->status = COMMENT_STATE;
  1009. }
  1010. if (s->status == COMMENT_STATE) {
  1011. if (s->gzhead->comment != Z_NULL) {
  1012. ulg beg = s->pending; /* start of bytes to update crc */
  1013. int val;
  1014. do {
  1015. if (s->pending == s->pending_buf_size) {
  1016. HCRC_UPDATE(beg);
  1017. flush_pending(strm);
  1018. if (s->pending != 0) {
  1019. s->last_flush = -1;
  1020. return Z_OK;
  1021. }
  1022. beg = 0;
  1023. }
  1024. val = s->gzhead->comment[s->gzindex++];
  1025. put_byte(s, val);
  1026. } while (val != 0);
  1027. HCRC_UPDATE(beg);
  1028. }
  1029. s->status = HCRC_STATE;
  1030. }
  1031. if (s->status == HCRC_STATE) {
  1032. if (s->gzhead->hcrc) {
  1033. if (s->pending + 2 > s->pending_buf_size) {
  1034. flush_pending(strm);
  1035. if (s->pending != 0) {
  1036. s->last_flush = -1;
  1037. return Z_OK;
  1038. }
  1039. }
  1040. put_byte(s, (Byte)(strm->adler & 0xff));
  1041. put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
  1042. strm->adler = crc32(0L, Z_NULL, 0);
  1043. }
  1044. s->status = BUSY_STATE;
  1045. /* Compression must start with an empty pending buffer */
  1046. flush_pending(strm);
  1047. if (s->pending != 0) {
  1048. s->last_flush = -1;
  1049. return Z_OK;
  1050. }
  1051. }
  1052. #endif
  1053. /* Start a new block or continue the current one.
  1054. */
  1055. if (strm->avail_in != 0 || s->lookahead != 0 ||
  1056. (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  1057. block_state bstate;
  1058. bstate = s->level == 0 ? deflate_stored(s, flush) :
  1059. s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) :
  1060. s->strategy == Z_RLE ? deflate_rle(s, flush) :
  1061. (*(configuration_table[s->level].func))(s, flush);
  1062. if (bstate == finish_started || bstate == finish_done) {
  1063. s->status = FINISH_STATE;
  1064. }
  1065. if (bstate == need_more || bstate == finish_started) {
  1066. if (strm->avail_out == 0) {
  1067. s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  1068. }
  1069. return Z_OK;
  1070. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  1071. * of deflate should use the same flush parameter to make sure
  1072. * that the flush is complete. So we don't have to output an
  1073. * empty block here, this will be done at next call. This also
  1074. * ensures that for a very small output buffer, we emit at most
  1075. * one empty block.
  1076. */
  1077. }
  1078. if (bstate == block_done) {
  1079. if (flush == Z_PARTIAL_FLUSH) {
  1080. _tr_align(s);
  1081. } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */
  1082. _tr_stored_block(s, (char*)0, 0L, 0);
  1083. /* For a full flush, this empty block will be recognized
  1084. * as a special marker by inflate_sync().
  1085. */
  1086. if (flush == Z_FULL_FLUSH) {
  1087. CLEAR_HASH(s); /* forget history */
  1088. if (s->lookahead == 0) {
  1089. s->strstart = 0;
  1090. s->block_start = 0L;
  1091. s->insert = 0;
  1092. }
  1093. }
  1094. }
  1095. flush_pending(strm);
  1096. if (strm->avail_out == 0) {
  1097. s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  1098. return Z_OK;
  1099. }
  1100. }
  1101. }
  1102. if (flush != Z_FINISH) return Z_OK;
  1103. if (s->wrap <= 0) return Z_STREAM_END;
  1104. /* Write the trailer */
  1105. #ifdef GZIP
  1106. if (s->wrap == 2) {
  1107. put_byte(s, (Byte)(strm->adler & 0xff));
  1108. put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
  1109. put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
  1110. put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
  1111. put_byte(s, (Byte)(strm->total_in & 0xff));
  1112. put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
  1113. put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
  1114. put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
  1115. }
  1116. else
  1117. #endif
  1118. {
  1119. putShortMSB(s, (uInt)(strm->adler >> 16));
  1120. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  1121. }
  1122. flush_pending(strm);
  1123. /* If avail_out is zero, the application will call deflate again
  1124. * to flush the rest.
  1125. */
  1126. if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
  1127. return s->pending != 0 ? Z_OK : Z_STREAM_END;
  1128. }
  1129. /* ========================================================================= */
  1130. int ZEXPORT deflateEnd(z_streamp strm) {
  1131. int status;
  1132. if (deflateStateCheck(strm)) return Z_STREAM_ERROR;
  1133. status = strm->state->status;
  1134. /* Deallocate in reverse order of allocations: */
  1135. TRY_FREE(strm, strm->state->pending_buf);
  1136. TRY_FREE(strm, strm->state->head);
  1137. TRY_FREE(strm, strm->state->prev);
  1138. TRY_FREE(strm, strm->state->window);
  1139. ZFREE(strm, strm->state);
  1140. strm->state = Z_NULL;
  1141. return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  1142. }
  1143. /* =========================================================================
  1144. * Copy the source state to the destination state.
  1145. * To simplify the source, this is not supported for 16-bit MSDOS (which
  1146. * doesn't have enough memory anyway to duplicate compression states).
  1147. */
  1148. int ZEXPORT deflateCopy(z_streamp dest, z_streamp source) {
  1149. #ifdef MAXSEG_64K
  1150. (void)dest;
  1151. (void)source;
  1152. return Z_STREAM_ERROR;
  1153. #else
  1154. deflate_state *ds;
  1155. deflate_state *ss;
  1156. if (deflateStateCheck(source) || dest == Z_NULL) {
  1157. return Z_STREAM_ERROR;
  1158. }
  1159. ss = source->state;
  1160. zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
  1161. ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
  1162. if (ds == Z_NULL) return Z_MEM_ERROR;
  1163. dest->state = (struct internal_state FAR *) ds;
  1164. zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state));
  1165. ds->strm = dest;
  1166. ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
  1167. ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
  1168. ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
  1169. ds->pending_buf = (uchf *) ZALLOC(dest, ds->lit_bufsize, 4);
  1170. if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
  1171. ds->pending_buf == Z_NULL) {
  1172. deflateEnd (dest);
  1173. return Z_MEM_ERROR;
  1174. }
  1175. /* following zmemcpy do not work for 16-bit MSDOS */
  1176. zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  1177. zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos));
  1178. zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos));
  1179. zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  1180. ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  1181. ds->sym_buf = ds->pending_buf + ds->lit_bufsize;
  1182. ds->l_desc.dyn_tree = ds->dyn_ltree;
  1183. ds->d_desc.dyn_tree = ds->dyn_dtree;
  1184. ds->bl_desc.dyn_tree = ds->bl_tree;
  1185. return Z_OK;
  1186. #endif /* MAXSEG_64K */
  1187. }
  1188. #ifndef FASTEST
  1189. /* ===========================================================================
  1190. * Set match_start to the longest match starting at the given string and
  1191. * return its length. Matches shorter or equal to prev_length are discarded,
  1192. * in which case the result is equal to prev_length and match_start is
  1193. * garbage.
  1194. * IN assertions: cur_match is the head of the hash chain for the current
  1195. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  1196. * OUT assertion: the match length is not greater than s->lookahead.
  1197. */
  1198. Y_NO_SANITIZE("undefined")
  1199. local uInt longest_match(deflate_state *s, IPos cur_match) {
  1200. unsigned chain_length = s->max_chain_length;/* max hash chain length */
  1201. register Bytef *scan = s->window + s->strstart; /* current string */
  1202. register Bytef *match; /* matched string */
  1203. register int len; /* length of current match */
  1204. int best_len = (int)s->prev_length; /* best match length so far */
  1205. int nice_match = s->nice_match; /* stop if match long enough */
  1206. IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  1207. s->strstart - (IPos)MAX_DIST(s) : NIL;
  1208. /* Stop when cur_match becomes <= limit. To simplify the code,
  1209. * we prevent matches with the string of window index 0.
  1210. */
  1211. Posf *prev = s->prev;
  1212. uInt wmask = s->w_mask;
  1213. #ifdef UNALIGNED_OK
  1214. /* Compare two bytes at a time. Note: this is not always beneficial.
  1215. * Try with and without -DUNALIGNED_OK to check.
  1216. */
  1217. register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  1218. register ush scan_start = *(ushf*)scan;
  1219. register ush scan_end = *(ushf*)(scan + best_len - 1);
  1220. #else
  1221. register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  1222. register Byte scan_end1 = scan[best_len - 1];
  1223. register Byte scan_end = scan[best_len];
  1224. #endif
  1225. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1226. * It is easy to get rid of this optimization if necessary.
  1227. */
  1228. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1229. /* Do not waste too much time if we already have a good match: */
  1230. if (s->prev_length >= s->good_match) {
  1231. chain_length >>= 2;
  1232. }
  1233. /* Do not look for matches beyond the end of the input. This is necessary
  1234. * to make deflate deterministic.
  1235. */
  1236. if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead;
  1237. Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1238. "need lookahead");
  1239. do {
  1240. Assert(cur_match < s->strstart, "no future");
  1241. match = s->window + cur_match;
  1242. /* Skip to next match if the match length cannot increase
  1243. * or if the match length is less than 2. Note that the checks below
  1244. * for insufficient lookahead only occur occasionally for performance
  1245. * reasons. Therefore uninitialized memory will be accessed, and
  1246. * conditional jumps will be made that depend on those values.
  1247. * However the length of the match is limited to the lookahead, so
  1248. * the output of deflate is not affected by the uninitialized values.
  1249. */
  1250. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  1251. /* This code assumes sizeof(unsigned short) == 2. Do not use
  1252. * UNALIGNED_OK if your compiler uses a different size.
  1253. */
  1254. if (*(ushf*)(match + best_len - 1) != scan_end ||
  1255. *(ushf*)match != scan_start) continue;
  1256. /* It is not necessary to compare scan[2] and match[2] since they are
  1257. * always equal when the other bytes match, given that the hash keys
  1258. * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  1259. * strstart + 3, + 5, up to strstart + 257. We check for insufficient
  1260. * lookahead only every 4th comparison; the 128th check will be made
  1261. * at strstart + 257. If MAX_MATCH-2 is not a multiple of 8, it is
  1262. * necessary to put more guard bytes at the end of the window, or
  1263. * to check more often for insufficient lookahead.
  1264. */
  1265. Assert(scan[2] == match[2], "scan[2]?");
  1266. scan++, match++;
  1267. do {
  1268. } while (*(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
  1269. *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
  1270. *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
  1271. *(ushf*)(scan += 2) == *(ushf*)(match += 2) &&
  1272. scan < strend);
  1273. /* The funny "do {}" generates better code on most compilers */
  1274. /* Here, scan <= window + strstart + 257 */
  1275. Assert(scan <= s->window + (unsigned)(s->window_size - 1),
  1276. "wild scan");
  1277. if (*scan == *match) scan++;
  1278. len = (MAX_MATCH - 1) - (int)(strend - scan);
  1279. scan = strend - (MAX_MATCH-1);
  1280. #else /* UNALIGNED_OK */
  1281. if (match[best_len] != scan_end ||
  1282. match[best_len - 1] != scan_end1 ||
  1283. *match != *scan ||
  1284. *++match != scan[1]) continue;
  1285. /* The check at best_len - 1 can be removed because it will be made
  1286. * again later. (This heuristic is not always a win.)
  1287. * It is not necessary to compare scan[2] and match[2] since they
  1288. * are always equal when the other bytes match, given that
  1289. * the hash keys are equal and that HASH_BITS >= 8.
  1290. */
  1291. scan += 2, match++;
  1292. Assert(*scan == *match, "match[2]?");
  1293. /* We check for insufficient lookahead only every 8th comparison;
  1294. * the 256th check will be made at strstart + 258.
  1295. */
  1296. do {
  1297. } while (*++scan == *++match && *++scan == *++match &&
  1298. *++scan == *++match && *++scan == *++match &&
  1299. *++scan == *++match && *++scan == *++match &&
  1300. *++scan == *++match && *++scan == *++match &&
  1301. scan < strend);
  1302. Assert(scan <= s->window + (unsigned)(s->window_size - 1),
  1303. "wild scan");
  1304. len = MAX_MATCH - (int)(strend - scan);
  1305. scan = strend - MAX_MATCH;
  1306. #endif /* UNALIGNED_OK */
  1307. if (len > best_len) {
  1308. s->match_start = cur_match;
  1309. best_len = len;
  1310. if (len >= nice_match) break;
  1311. #ifdef UNALIGNED_OK
  1312. scan_end = *(ushf*)(scan + best_len - 1);
  1313. #else
  1314. scan_end1 = scan[best_len - 1];
  1315. scan_end = scan[best_len];
  1316. #endif
  1317. }
  1318. } while ((cur_match = prev[cur_match & wmask]) > limit
  1319. && --chain_length != 0);
  1320. if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  1321. return s->lookahead;
  1322. }
  1323. #else /* FASTEST */
  1324. /* ---------------------------------------------------------------------------
  1325. * Optimized version for FASTEST only
  1326. */
  1327. local uInt longest_match(deflate_state *s, IPos cur_match) {
  1328. register Bytef *scan = s->window + s->strstart; /* current string */
  1329. register Bytef *match; /* matched string */
  1330. register int len; /* length of current match */
  1331. register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  1332. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1333. * It is easy to get rid of this optimization if necessary.
  1334. */
  1335. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1336. Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD,
  1337. "need lookahead");
  1338. Assert(cur_match < s->strstart, "no future");
  1339. match = s->window + cur_match;
  1340. /* Return failure if the match length is less than 2:
  1341. */
  1342. if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  1343. /* The check at best_len - 1 can be removed because it will be made
  1344. * again later. (This heuristic is not always a win.)
  1345. * It is not necessary to compare scan[2] and match[2] since they
  1346. * are always equal when the other bytes match, given that
  1347. * the hash keys are equal and that HASH_BITS >= 8.
  1348. */
  1349. scan += 2, match += 2;
  1350. Assert(*scan == *match, "match[2]?");
  1351. /* We check for insufficient lookahead only every 8th comparison;
  1352. * the 256th check will be made at strstart + 258.
  1353. */
  1354. do {
  1355. } while (*++scan == *++match && *++scan == *++match &&
  1356. *++scan == *++match && *++scan == *++match &&
  1357. *++scan == *++match && *++scan == *++match &&
  1358. *++scan == *++match && *++scan == *++match &&
  1359. scan < strend);
  1360. Assert(scan <= s->window + (unsigned)(s->window_size - 1), "wild scan");
  1361. len = MAX_MATCH - (int)(strend - scan);
  1362. if (len < MIN_MATCH) return MIN_MATCH - 1;
  1363. s->match_start = cur_match;
  1364. return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
  1365. }
  1366. #endif /* FASTEST */
  1367. #ifdef ZLIB_DEBUG
  1368. #define EQUAL 0
  1369. /* result of memcmp for equal strings */
  1370. /* ===========================================================================
  1371. * Check that the match at match_start is indeed a match.
  1372. */
  1373. local void check_match(deflate_state *s, IPos start, IPos match, int length) {
  1374. /* check that the match is indeed a match */
  1375. if (zmemcmp(s->window + match,
  1376. s->window + start, length) != EQUAL) {
  1377. fprintf(stderr, " start %u, match %u, length %d\n",
  1378. start, match, length);
  1379. do {
  1380. fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  1381. } while (--length != 0);
  1382. z_error("invalid match");
  1383. }
  1384. if (z_verbose > 1) {
  1385. fprintf(stderr,"\\[%d,%d]", start - match, length);
  1386. do { putc(s->window[start++], stderr); } while (--length != 0);
  1387. }
  1388. }
  1389. #else
  1390. # define check_match(s, start, match, length)
  1391. #endif /* ZLIB_DEBUG */
  1392. /* ===========================================================================
  1393. * Flush the current block, with given end-of-file flag.
  1394. * IN assertion: strstart is set to the end of the current match.
  1395. */
  1396. #define FLUSH_BLOCK_ONLY(s, last) { \
  1397. _tr_flush_block(s, (s->block_start >= 0L ? \
  1398. (charf *)&s->window[(unsigned)s->block_start] : \
  1399. (charf *)Z_NULL), \
  1400. (ulg)((long)s->strstart - s->block_start), \
  1401. (last)); \
  1402. s->block_start = s->strstart; \
  1403. flush_pending(s->strm); \
  1404. Tracev((stderr,"[FLUSH]")); \
  1405. }
  1406. /* Same but force premature exit if necessary. */
  1407. #define FLUSH_BLOCK(s, last) { \
  1408. FLUSH_BLOCK_ONLY(s, last); \
  1409. if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \
  1410. }
  1411. /* Maximum stored block length in deflate format (not including header). */
  1412. #define MAX_STORED 65535
  1413. /* Minimum of a and b. */
  1414. #define MIN(a, b) ((a) > (b) ? (b) : (a))
  1415. /* ===========================================================================
  1416. * Copy without compression as much as possible from the input stream, return
  1417. * the current block state.
  1418. *
  1419. * In case deflateParams() is used to later switch to a non-zero compression
  1420. * level, s->matches (otherwise unused when storing) keeps track of the number
  1421. * of hash table slides to perform. If s->matches is 1, then one hash table
  1422. * slide will be done when switching. If s->matches is 2, the maximum value
  1423. * allowed here, then the hash table will be cleared, since two or more slides
  1424. * is the same as a clear.
  1425. *
  1426. * deflate_stored() is written to minimize the number of times an input byte is
  1427. * copied. It is most efficient with large input and output buffers, which
  1428. * maximizes the opportunities to have a single copy from next_in to next_out.
  1429. */
  1430. local block_state deflate_stored(deflate_state *s, int flush) {
  1431. /* Smallest worthy block size when not flushing or finishing. By default
  1432. * this is 32K. This can be as small as 507 bytes for memLevel == 1. For
  1433. * large input and output buffers, the stored block size will be larger.
  1434. */
  1435. unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size);
  1436. /* Copy as many min_block or larger stored blocks directly to next_out as
  1437. * possible. If flushing, copy the remaining available input to next_out as
  1438. * stored blocks, if there is enough space.
  1439. */
  1440. unsigned len, left, have, last = 0;
  1441. unsigned used = s->strm->avail_in;
  1442. do {
  1443. /* Set len to the maximum size block that we can copy directly with the
  1444. * available input data and output space. Set left to how much of that
  1445. * would be copied from what's left in the window.
  1446. */
  1447. len = MAX_STORED; /* maximum deflate stored block length */
  1448. have = (s->bi_valid + 42) >> 3; /* number of header bytes */
  1449. if (s->strm->avail_out < have) /* need room for header */
  1450. break;
  1451. /* maximum stored block length that will fit in avail_out: */
  1452. have = s->strm->avail_out - have;
  1453. left = s->strstart - s->block_start; /* bytes left in window */
  1454. if (len > (ulg)left + s->strm->avail_in)
  1455. len = left + s->strm->avail_in; /* limit len to the input */
  1456. if (len > have)
  1457. len = have; /* limit len to the output */
  1458. /* If the stored block would be less than min_block in length, or if
  1459. * unable to copy all of the available input when flushing, then try
  1460. * copying to the window and the pending buffer instead. Also don't
  1461. * write an empty block when flushing -- deflate() does that.
  1462. */
  1463. if (len < min_block && ((len == 0 && flush != Z_FINISH) ||
  1464. flush == Z_NO_FLUSH ||
  1465. len != left + s->strm->avail_in))
  1466. break;
  1467. /* Make a dummy stored block in pending to get the header bytes,
  1468. * including any pending bits. This also updates the debugging counts.
  1469. */
  1470. last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0;
  1471. _tr_stored_block(s, (char *)0, 0L, last);
  1472. /* Replace the lengths in the dummy stored block with len. */
  1473. s->pending_buf[s->pending - 4] = len;
  1474. s->pending_buf[s->pending - 3] = len >> 8;
  1475. s->pending_buf[s->pending - 2] = ~len;
  1476. s->pending_buf[s->pending - 1] = ~len >> 8;
  1477. /* Write the stored block header bytes. */
  1478. flush_pending(s->strm);
  1479. #ifdef ZLIB_DEBUG
  1480. /* Update debugging counts for the data about to be copied. */
  1481. s->compressed_len += len << 3;
  1482. s->bits_sent += len << 3;
  1483. #endif
  1484. /* Copy uncompressed bytes from the window to next_out. */
  1485. if (left) {
  1486. if (left > len)
  1487. left = len;
  1488. zmemcpy(s->strm->next_out, s->window + s->block_start, left);
  1489. s->strm->next_out += left;
  1490. s->strm->avail_out -= left;
  1491. s->strm->total_out += left;
  1492. s->block_start += left;
  1493. len -= left;
  1494. }
  1495. /* Copy uncompressed bytes directly from next_in to next_out, updating
  1496. * the check value.
  1497. */
  1498. if (len) {
  1499. read_buf(s->strm, s->strm->next_out, len);
  1500. s->strm->next_out += len;
  1501. s->strm->avail_out -= len;
  1502. s->strm->total_out += len;
  1503. }
  1504. } while (last == 0);
  1505. /* Update the sliding window with the last s->w_size bytes of the copied
  1506. * data, or append all of the copied data to the existing window if less
  1507. * than s->w_size bytes were copied. Also update the number of bytes to
  1508. * insert in the hash tables, in the event that deflateParams() switches to
  1509. * a non-zero compression level.
  1510. */
  1511. used -= s->strm->avail_in; /* number of input bytes directly copied */
  1512. if (used) {
  1513. /* If any input was used, then no unused input remains in the window,
  1514. * therefore s->block_start == s->strstart.
  1515. */
  1516. if (used >= s->w_size) { /* supplant the previous history */
  1517. s->matches = 2; /* clear hash */
  1518. zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size);
  1519. s->strstart = s->w_size;
  1520. s->insert = s->strstart;
  1521. }
  1522. else {
  1523. if (s->window_size - s->strstart <= used) {
  1524. /* Slide the window down. */
  1525. s->strstart -= s->w_size;
  1526. zmemcpy(s->window, s->window + s->w_size, s->strstart);
  1527. if (s->matches < 2)
  1528. s->matches++; /* add a pending slide_hash() */
  1529. if (s->insert > s->strstart)
  1530. s->insert = s->strstart;
  1531. }
  1532. zmemcpy(s->window + s->strstart, s->strm->next_in - used, used);
  1533. s->strstart += used;
  1534. s->insert += MIN(used, s->w_size - s->insert);
  1535. }
  1536. s->block_start = s->strstart;
  1537. }
  1538. if (s->high_water < s->strstart)
  1539. s->high_water = s->strstart;
  1540. /* If the last block was written to next_out, then done. */
  1541. if (last)
  1542. return finish_done;
  1543. /* If flushing and all input has been consumed, then done. */
  1544. if (flush != Z_NO_FLUSH && flush != Z_FINISH &&
  1545. s->strm->avail_in == 0 && (long)s->strstart == s->block_start)
  1546. return block_done;
  1547. /* Fill the window with any remaining input. */
  1548. have = s->window_size - s->strstart;
  1549. if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) {
  1550. /* Slide the window down. */
  1551. s->block_start -= s->w_size;
  1552. s->strstart -= s->w_size;
  1553. zmemcpy(s->window, s->window + s->w_size, s->strstart);
  1554. if (s->matches < 2)
  1555. s->matches++; /* add a pending slide_hash() */
  1556. have += s->w_size; /* more space now */
  1557. if (s->insert > s->strstart)
  1558. s->insert = s->strstart;
  1559. }
  1560. if (have > s->strm->avail_in)
  1561. have = s->strm->avail_in;
  1562. if (have) {
  1563. read_buf(s->strm, s->window + s->strstart, have);
  1564. s->strstart += have;
  1565. s->insert += MIN(have, s->w_size - s->insert);
  1566. }
  1567. if (s->high_water < s->strstart)
  1568. s->high_water = s->strstart;
  1569. /* There was not enough avail_out to write a complete worthy or flushed
  1570. * stored block to next_out. Write a stored block to pending instead, if we
  1571. * have enough input for a worthy block, or if flushing and there is enough
  1572. * room for the remaining input as a stored block in the pending buffer.
  1573. */
  1574. have = (s->bi_valid + 42) >> 3; /* number of header bytes */
  1575. /* maximum stored block length that will fit in pending: */
  1576. have = MIN(s->pending_buf_size - have, MAX_STORED);
  1577. min_block = MIN(have, s->w_size);
  1578. left = s->strstart - s->block_start;
  1579. if (left >= min_block ||
  1580. ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH &&
  1581. s->strm->avail_in == 0 && left <= have)) {
  1582. len = MIN(left, have);
  1583. last = flush == Z_FINISH && s->strm->avail_in == 0 &&
  1584. len == left ? 1 : 0;
  1585. _tr_stored_block(s, (charf *)s->window + s->block_start, len, last);
  1586. s->block_start += len;
  1587. flush_pending(s->strm);
  1588. }
  1589. /* We've done all we can with the available input and output. */
  1590. return last ? finish_started : need_more;
  1591. }
  1592. /* ===========================================================================
  1593. * Compress as much as possible from the input stream, return the current
  1594. * block state.
  1595. * This function does not perform lazy evaluation of matches and inserts
  1596. * new strings in the dictionary only for unmatched strings or for short
  1597. * matches. It is used only for the fast compression options.
  1598. */
  1599. local block_state deflate_fast(deflate_state *s, int flush) {
  1600. IPos hash_head; /* head of the hash chain */
  1601. int bflush; /* set if current block must be flushed */
  1602. for (;;) {
  1603. /* Make sure that we always have enough lookahead, except
  1604. * at the end of the input file. We need MAX_MATCH bytes
  1605. * for the next match, plus MIN_MATCH bytes to insert the
  1606. * string following the next match.
  1607. */
  1608. if (s->lookahead < MIN_LOOKAHEAD) {
  1609. fill_window(s);
  1610. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1611. return need_more;
  1612. }
  1613. if (s->lookahead == 0) break; /* flush the current block */
  1614. }
  1615. /* Insert the string window[strstart .. strstart + 2] in the
  1616. * dictionary, and set hash_head to the head of the hash chain:
  1617. */
  1618. hash_head = NIL;
  1619. if (s->lookahead >= MIN_MATCH) {
  1620. INSERT_STRING(s, s->strstart, hash_head);
  1621. }
  1622. /* Find the longest match, discarding those <= prev_length.
  1623. * At this point we have always match_length < MIN_MATCH
  1624. */
  1625. if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  1626. /* To simplify the code, we prevent matches with the string
  1627. * of window index 0 (in particular we have to avoid a match
  1628. * of the string with itself at the start of the input file).
  1629. */
  1630. s->match_length = longest_match (s, hash_head);
  1631. /* longest_match() sets match_start */
  1632. }
  1633. if (s->match_length >= MIN_MATCH) {
  1634. check_match(s, s->strstart, s->match_start, s->match_length);
  1635. _tr_tally_dist(s, s->strstart - s->match_start,
  1636. s->match_length - MIN_MATCH, bflush);
  1637. s->lookahead -= s->match_length;
  1638. /* Insert new strings in the hash table only if the match length
  1639. * is not too large. This saves time but degrades compression.
  1640. */
  1641. #ifndef FASTEST
  1642. if (s->match_length <= s->max_insert_length &&
  1643. s->lookahead >= MIN_MATCH) {
  1644. s->match_length--; /* string at strstart already in table */
  1645. do {
  1646. s->strstart++;
  1647. INSERT_STRING(s, s->strstart, hash_head);
  1648. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1649. * always MIN_MATCH bytes ahead.
  1650. */
  1651. } while (--s->match_length != 0);
  1652. s->strstart++;
  1653. } else
  1654. #endif
  1655. {
  1656. s->strstart += s->match_length;
  1657. s->match_length = 0;
  1658. s->ins_h = s->window[s->strstart];
  1659. UPDATE_HASH(s, s->ins_h, s->window[s->strstart + 1]);
  1660. #if MIN_MATCH != 3
  1661. Call UPDATE_HASH() MIN_MATCH-3 more times
  1662. #endif
  1663. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1664. * matter since it will be recomputed at next deflate call.
  1665. */
  1666. }
  1667. } else {
  1668. /* No match, output a literal byte */
  1669. Tracevv((stderr,"%c", s->window[s->strstart]));
  1670. _tr_tally_lit(s, s->window[s->strstart], bflush);
  1671. s->lookahead--;
  1672. s->strstart++;
  1673. }
  1674. if (bflush) FLUSH_BLOCK(s, 0);
  1675. }
  1676. s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
  1677. if (flush == Z_FINISH) {
  1678. FLUSH_BLOCK(s, 1);
  1679. return finish_done;
  1680. }
  1681. if (s->sym_next)
  1682. FLUSH_BLOCK(s, 0);
  1683. return block_done;
  1684. }
  1685. #ifndef FASTEST
  1686. /* ===========================================================================
  1687. * Same as above, but achieves better compression. We use a lazy
  1688. * evaluation for matches: a match is finally adopted only if there is
  1689. * no better match at the next window position.
  1690. */
  1691. local block_state deflate_slow(deflate_state *s, int flush) {
  1692. IPos hash_head; /* head of hash chain */
  1693. int bflush; /* set if current block must be flushed */
  1694. /* Process the input block. */
  1695. for (;;) {
  1696. /* Make sure that we always have enough lookahead, except
  1697. * at the end of the input file. We need MAX_MATCH bytes
  1698. * for the next match, plus MIN_MATCH bytes to insert the
  1699. * string following the next match.
  1700. */
  1701. if (s->lookahead < MIN_LOOKAHEAD) {
  1702. fill_window(s);
  1703. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1704. return need_more;
  1705. }
  1706. if (s->lookahead == 0) break; /* flush the current block */
  1707. }
  1708. /* Insert the string window[strstart .. strstart + 2] in the
  1709. * dictionary, and set hash_head to the head of the hash chain:
  1710. */
  1711. hash_head = NIL;
  1712. if (s->lookahead >= MIN_MATCH) {
  1713. INSERT_STRING(s, s->strstart, hash_head);
  1714. }
  1715. /* Find the longest match, discarding those <= prev_length.
  1716. */
  1717. s->prev_length = s->match_length, s->prev_match = s->match_start;
  1718. s->match_length = MIN_MATCH-1;
  1719. if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  1720. s->strstart - hash_head <= MAX_DIST(s)) {
  1721. /* To simplify the code, we prevent matches with the string
  1722. * of window index 0 (in particular we have to avoid a match
  1723. * of the string with itself at the start of the input file).
  1724. */
  1725. s->match_length = longest_match (s, hash_head);
  1726. /* longest_match() sets match_start */
  1727. if (s->match_length <= 5 && (s->strategy == Z_FILTERED
  1728. #if TOO_FAR <= 32767
  1729. || (s->match_length == MIN_MATCH &&
  1730. s->strstart - s->match_start > TOO_FAR)
  1731. #endif
  1732. )) {
  1733. /* If prev_match is also MIN_MATCH, match_start is garbage
  1734. * but we will ignore the current match anyway.
  1735. */
  1736. s->match_length = MIN_MATCH-1;
  1737. }
  1738. }
  1739. /* If there was a match at the previous step and the current
  1740. * match is not better, output the previous match:
  1741. */
  1742. if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  1743. uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  1744. /* Do not insert strings in hash table beyond this. */
  1745. check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
  1746. _tr_tally_dist(s, s->strstart - 1 - s->prev_match,
  1747. s->prev_length - MIN_MATCH, bflush);
  1748. /* Insert in hash table all strings up to the end of the match.
  1749. * strstart - 1 and strstart are already inserted. If there is not
  1750. * enough lookahead, the last two strings are not inserted in
  1751. * the hash table.
  1752. */
  1753. s->lookahead -= s->prev_length - 1;
  1754. s->prev_length -= 2;
  1755. do {
  1756. if (++s->strstart <= max_insert) {
  1757. INSERT_STRING(s, s->strstart, hash_head);
  1758. }
  1759. } while (--s->prev_length != 0);
  1760. s->match_available = 0;
  1761. s->match_length = MIN_MATCH-1;
  1762. s->strstart++;
  1763. if (bflush) FLUSH_BLOCK(s, 0);
  1764. } else if (s->match_available) {
  1765. /* If there was no match at the previous position, output a
  1766. * single literal. If there was a match but the current match
  1767. * is longer, truncate the previous match to a single literal.
  1768. */
  1769. Tracevv((stderr,"%c", s->window[s->strstart - 1]));
  1770. _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
  1771. if (bflush) {
  1772. FLUSH_BLOCK_ONLY(s, 0);
  1773. }
  1774. s->strstart++;
  1775. s->lookahead--;
  1776. if (s->strm->avail_out == 0) return need_more;
  1777. } else {
  1778. /* There is no previous match to compare with, wait for
  1779. * the next step to decide.
  1780. */
  1781. s->match_available = 1;
  1782. s->strstart++;
  1783. s->lookahead--;
  1784. }
  1785. }
  1786. Assert (flush != Z_NO_FLUSH, "no flush?");
  1787. if (s->match_available) {
  1788. Tracevv((stderr,"%c", s->window[s->strstart - 1]));
  1789. _tr_tally_lit(s, s->window[s->strstart - 1], bflush);
  1790. s->match_available = 0;
  1791. }
  1792. s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1;
  1793. if (flush == Z_FINISH) {
  1794. FLUSH_BLOCK(s, 1);
  1795. return finish_done;
  1796. }
  1797. if (s->sym_next)
  1798. FLUSH_BLOCK(s, 0);
  1799. return block_done;
  1800. }
  1801. #endif /* FASTEST */
  1802. /* ===========================================================================
  1803. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1804. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1805. * deflate switches away from Z_RLE.)
  1806. */
  1807. local block_state deflate_rle(deflate_state *s, int flush) {
  1808. int bflush; /* set if current block must be flushed */
  1809. uInt prev; /* byte at distance one to match */
  1810. Bytef *scan, *strend; /* scan goes up to strend for length of run */
  1811. for (;;) {
  1812. /* Make sure that we always have enough lookahead, except
  1813. * at the end of the input file. We need MAX_MATCH bytes
  1814. * for the longest run, plus one for the unrolled loop.
  1815. */
  1816. if (s->lookahead <= MAX_MATCH) {
  1817. fill_window(s);
  1818. if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) {
  1819. return need_more;
  1820. }
  1821. if (s->lookahead == 0) break; /* flush the current block */
  1822. }
  1823. /* See how many times the previous byte repeats */
  1824. s->match_length = 0;
  1825. if (s->lookahead >= MIN_MATCH && s->strstart > 0) {
  1826. scan = s->window + s->strstart - 1;
  1827. prev = *scan;
  1828. if (prev == *++scan && prev == *++scan && prev == *++scan) {
  1829. strend = s->window + s->strstart + MAX_MATCH;
  1830. do {
  1831. } while (prev == *++scan && prev == *++scan &&
  1832. prev == *++scan && prev == *++scan &&
  1833. prev == *++scan && prev == *++scan &&
  1834. prev == *++scan && prev == *++scan &&
  1835. scan < strend);
  1836. s->match_length = MAX_MATCH - (uInt)(strend - scan);
  1837. if (s->match_length > s->lookahead)
  1838. s->match_length = s->lookahead;
  1839. }
  1840. Assert(scan <= s->window + (uInt)(s->window_size - 1),
  1841. "wild scan");
  1842. }
  1843. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1844. if (s->match_length >= MIN_MATCH) {
  1845. check_match(s, s->strstart, s->strstart - 1, s->match_length);
  1846. _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush);
  1847. s->lookahead -= s->match_length;
  1848. s->strstart += s->match_length;
  1849. s->match_length = 0;
  1850. } else {
  1851. /* No match, output a literal byte */
  1852. Tracevv((stderr,"%c", s->window[s->strstart]));
  1853. _tr_tally_lit(s, s->window[s->strstart], bflush);
  1854. s->lookahead--;
  1855. s->strstart++;
  1856. }
  1857. if (bflush) FLUSH_BLOCK(s, 0);
  1858. }
  1859. s->insert = 0;
  1860. if (flush == Z_FINISH) {
  1861. FLUSH_BLOCK(s, 1);
  1862. return finish_done;
  1863. }
  1864. if (s->sym_next)
  1865. FLUSH_BLOCK(s, 0);
  1866. return block_done;
  1867. }
  1868. /* ===========================================================================
  1869. * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table.
  1870. * (It will be regenerated if this run of deflate switches away from Huffman.)
  1871. */
  1872. local block_state deflate_huff(deflate_state *s, int flush) {
  1873. int bflush; /* set if current block must be flushed */
  1874. for (;;) {
  1875. /* Make sure that we have a literal to write. */
  1876. if (s->lookahead == 0) {
  1877. fill_window(s);
  1878. if (s->lookahead == 0) {
  1879. if (flush == Z_NO_FLUSH)
  1880. return need_more;
  1881. break; /* flush the current block */
  1882. }
  1883. }
  1884. /* Output a literal byte */
  1885. s->match_length = 0;
  1886. Tracevv((stderr,"%c", s->window[s->strstart]));
  1887. _tr_tally_lit(s, s->window[s->strstart], bflush);
  1888. s->lookahead--;
  1889. s->strstart++;
  1890. if (bflush) FLUSH_BLOCK(s, 0);
  1891. }
  1892. s->insert = 0;
  1893. if (flush == Z_FINISH) {
  1894. FLUSH_BLOCK(s, 1);
  1895. return finish_done;
  1896. }
  1897. if (s->sym_next)
  1898. FLUSH_BLOCK(s, 0);
  1899. return block_done;
  1900. }