deflate.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  1. /* $OpenBSD: deflate.c,v 1.2 2011/07/16 07:25:29 jsing Exp $ */
  2. /* deflate.c -- compress data using the deflation algorithm
  3. * Copyright (C) 1995-2005 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 http://www.ietf.org/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 "deflate.h"
  50. /*
  51. If you use the zlib library in a product, an acknowledgment is welcome
  52. in the documentation of your product. If for some reason you cannot
  53. include such an acknowledgment, I would appreciate that you keep this
  54. copyright string in the executable of your product.
  55. */
  56. /* ===========================================================================
  57. * Function prototypes.
  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) OF((deflate_state *s, int flush));
  66. /* Compression function. Returns the block state after the call. */
  67. local void fill_window OF((deflate_state *s));
  68. local block_state deflate_stored OF((deflate_state *s, int flush));
  69. local block_state deflate_fast OF((deflate_state *s, int flush));
  70. #ifndef FASTEST
  71. local block_state deflate_slow OF((deflate_state *s, int flush));
  72. #endif
  73. local void lm_init OF((deflate_state *s));
  74. local void putShortMSB OF((deflate_state *s, uInt b));
  75. local void flush_pending OF((z_streamp strm));
  76. local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size));
  77. #ifndef FASTEST
  78. #ifdef ASMV
  79. void match_init OF((void)); /* asm code initialization */
  80. uInt longest_match OF((deflate_state *s, IPos cur_match));
  81. #else
  82. local uInt longest_match OF((deflate_state *s, IPos cur_match));
  83. #endif
  84. #endif
  85. local uInt longest_match_fast OF((deflate_state *s, IPos cur_match));
  86. #ifdef DEBUG_LIBZ
  87. local void check_match OF((deflate_state *s, IPos start, IPos match,
  88. int length));
  89. #endif
  90. /* ===========================================================================
  91. * Local data
  92. */
  93. #define NIL 0
  94. /* Tail of hash chains */
  95. #ifndef TOO_FAR
  96. # define TOO_FAR 4096
  97. #endif
  98. /* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
  99. #define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
  100. /* Minimum amount of lookahead, except at the end of the input file.
  101. * See deflate.c for comments about the MIN_MATCH+1.
  102. */
  103. /* Values for max_lazy_match, good_match and max_chain_length, depending on
  104. * the desired pack level (0..9). The values given below have been tuned to
  105. * exclude worst case performance for pathological files. Better values may be
  106. * found for specific files.
  107. */
  108. typedef struct config_s {
  109. ush good_length; /* reduce lazy search above this match length */
  110. ush max_lazy; /* do not perform lazy search above this match length */
  111. ush nice_length; /* quit search above this match length */
  112. ush max_chain;
  113. compress_func func;
  114. } config;
  115. #ifdef FASTEST
  116. local const config configuration_table[2] = {
  117. /* good lazy nice chain */
  118. /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  119. /* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */
  120. #else
  121. local const config configuration_table[10] = {
  122. /* good lazy nice chain */
  123. /* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */
  124. /* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */
  125. /* 2 */ {4, 5, 16, 8, deflate_fast},
  126. /* 3 */ {4, 6, 32, 32, deflate_fast},
  127. /* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */
  128. /* 5 */ {8, 16, 32, 32, deflate_slow},
  129. /* 6 */ {8, 16, 128, 128, deflate_slow},
  130. /* 7 */ {8, 32, 128, 256, deflate_slow},
  131. /* 8 */ {32, 128, 258, 1024, deflate_slow},
  132. /* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */
  133. #endif
  134. /* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
  135. * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
  136. * meaning.
  137. */
  138. #define EQUAL 0
  139. /* result of memcmp for equal strings */
  140. #ifndef NO_DUMMY_DECL
  141. struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
  142. #endif
  143. /* ===========================================================================
  144. * Update a hash value with the given input byte
  145. * IN assertion: all calls to UPDATE_HASH are made with consecutive
  146. * input characters, so that a running hash key can be computed from the
  147. * previous key instead of complete recalculation each time.
  148. */
  149. #define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
  150. /* ===========================================================================
  151. * Insert string str in the dictionary and set match_head to the previous head
  152. * of the hash chain (the most recent string with same hash key). Return
  153. * the previous length of the hash chain.
  154. * If this file is compiled with -DFASTEST, the compression level is forced
  155. * to 1, and no hash chains are maintained.
  156. * IN assertion: all calls to INSERT_STRING are made with consecutive
  157. * input characters and the first MIN_MATCH bytes of str are valid
  158. * (except for the last MIN_MATCH-1 bytes of the input file).
  159. */
  160. #ifdef FASTEST
  161. #define INSERT_STRING(s, str, match_head) \
  162. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  163. match_head = s->head[s->ins_h], \
  164. s->head[s->ins_h] = (Pos)(str))
  165. #else
  166. #define INSERT_STRING(s, str, match_head) \
  167. (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
  168. match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \
  169. s->head[s->ins_h] = (Pos)(str))
  170. #endif
  171. /* ===========================================================================
  172. * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
  173. * prev[] will be initialized on the fly.
  174. */
  175. #define CLEAR_HASH(s) \
  176. s->head[s->hash_size-1] = NIL; \
  177. zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
  178. /* ========================================================================= */
  179. int ZEXPORT deflateInit_(strm, level, version, stream_size)
  180. z_streamp strm;
  181. int level;
  182. const char *version;
  183. int stream_size;
  184. {
  185. return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
  186. Z_DEFAULT_STRATEGY, version, stream_size);
  187. /* To do: ignore strm->next_in if we use it as window */
  188. }
  189. /* ========================================================================= */
  190. int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
  191. version, stream_size)
  192. z_streamp strm;
  193. int level;
  194. int method;
  195. int windowBits;
  196. int memLevel;
  197. int strategy;
  198. const char *version;
  199. int stream_size;
  200. {
  201. deflate_state *s;
  202. int wrap = 1;
  203. static const char my_version[] = ZLIB_VERSION;
  204. ushf *overlay;
  205. /* We overlay pending_buf and d_buf+l_buf. This works since the average
  206. * output size for (length,distance) codes is <= 24 bits.
  207. */
  208. if (version == Z_NULL || version[0] != my_version[0] ||
  209. stream_size != sizeof(z_stream)) {
  210. return Z_VERSION_ERROR;
  211. }
  212. if (strm == Z_NULL) return Z_STREAM_ERROR;
  213. strm->msg = Z_NULL;
  214. if (strm->zalloc == (alloc_func)0) {
  215. strm->zalloc = zcalloc;
  216. strm->opaque = (voidpf)0;
  217. }
  218. if (strm->zfree == (free_func)0) strm->zfree = zcfree;
  219. #ifdef FASTEST
  220. if (level != 0) level = 1;
  221. #else
  222. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  223. #endif
  224. if (windowBits < 0) { /* suppress zlib wrapper */
  225. wrap = 0;
  226. windowBits = -windowBits;
  227. }
  228. #ifdef GZIP
  229. else if (windowBits > 15) {
  230. wrap = 2; /* write gzip wrapper instead */
  231. windowBits -= 16;
  232. }
  233. #endif
  234. if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
  235. windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
  236. strategy < 0 || strategy > Z_FIXED) {
  237. return Z_STREAM_ERROR;
  238. }
  239. if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */
  240. s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
  241. if (s == Z_NULL) return Z_MEM_ERROR;
  242. strm->state = (struct internal_state FAR *)s;
  243. s->strm = strm;
  244. s->wrap = wrap;
  245. s->gzhead = Z_NULL;
  246. s->w_bits = windowBits;
  247. s->w_size = 1 << s->w_bits;
  248. s->w_mask = s->w_size - 1;
  249. s->hash_bits = memLevel + 7;
  250. s->hash_size = 1 << s->hash_bits;
  251. s->hash_mask = s->hash_size - 1;
  252. s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
  253. s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
  254. s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos));
  255. s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos));
  256. s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
  257. overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
  258. s->pending_buf = (uchf *) overlay;
  259. s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
  260. if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
  261. s->pending_buf == Z_NULL) {
  262. s->status = FINISH_STATE;
  263. strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
  264. deflateEnd (strm);
  265. return Z_MEM_ERROR;
  266. }
  267. s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
  268. s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
  269. s->level = level;
  270. s->strategy = strategy;
  271. s->method = (Byte)method;
  272. return deflateReset(strm);
  273. }
  274. /* ========================================================================= */
  275. int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength)
  276. z_streamp strm;
  277. const Bytef *dictionary;
  278. uInt dictLength;
  279. {
  280. deflate_state *s;
  281. uInt length = dictLength;
  282. uInt n;
  283. IPos hash_head = 0;
  284. if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL ||
  285. strm->state->wrap == 2 ||
  286. (strm->state->wrap == 1 && strm->state->status != INIT_STATE))
  287. return Z_STREAM_ERROR;
  288. s = strm->state;
  289. if (s->wrap)
  290. strm->adler = adler32(strm->adler, dictionary, dictLength);
  291. if (length < MIN_MATCH) return Z_OK;
  292. if (length > MAX_DIST(s)) {
  293. length = MAX_DIST(s);
  294. dictionary += dictLength - length; /* use the tail of the dictionary */
  295. }
  296. zmemcpy(s->window, dictionary, length);
  297. s->strstart = length;
  298. s->block_start = (long)length;
  299. /* Insert all strings in the hash table (except for the last two bytes).
  300. * s->lookahead stays null, so s->ins_h will be recomputed at the next
  301. * call of fill_window.
  302. */
  303. s->ins_h = s->window[0];
  304. UPDATE_HASH(s, s->ins_h, s->window[1]);
  305. for (n = 0; n <= length - MIN_MATCH; n++) {
  306. INSERT_STRING(s, n, hash_head);
  307. }
  308. if (hash_head) hash_head = 0; /* to make compiler happy */
  309. return Z_OK;
  310. }
  311. /* ========================================================================= */
  312. int ZEXPORT deflateReset (strm)
  313. z_streamp strm;
  314. {
  315. deflate_state *s;
  316. if (strm == Z_NULL || strm->state == Z_NULL ||
  317. strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) {
  318. return Z_STREAM_ERROR;
  319. }
  320. strm->total_in = strm->total_out = 0;
  321. strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
  322. strm->data_type = Z_UNKNOWN;
  323. s = (deflate_state *)strm->state;
  324. s->pending = 0;
  325. s->pending_out = s->pending_buf;
  326. if (s->wrap < 0) {
  327. s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */
  328. }
  329. s->status = s->wrap ? INIT_STATE : BUSY_STATE;
  330. strm->adler =
  331. #ifdef GZIP
  332. s->wrap == 2 ? crc32(0L, Z_NULL, 0) :
  333. #endif
  334. adler32(0L, Z_NULL, 0);
  335. s->last_flush = Z_NO_FLUSH;
  336. _tr_init(s);
  337. lm_init(s);
  338. return Z_OK;
  339. }
  340. /* ========================================================================= */
  341. int ZEXPORT deflateSetHeader (strm, head)
  342. z_streamp strm;
  343. gz_headerp head;
  344. {
  345. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  346. if (strm->state->wrap != 2) return Z_STREAM_ERROR;
  347. strm->state->gzhead = head;
  348. return Z_OK;
  349. }
  350. /* ========================================================================= */
  351. int ZEXPORT deflatePrime (strm, bits, value)
  352. z_streamp strm;
  353. int bits;
  354. int value;
  355. {
  356. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  357. strm->state->bi_valid = bits;
  358. strm->state->bi_buf = (ush)(value & ((1 << bits) - 1));
  359. return Z_OK;
  360. }
  361. /* ========================================================================= */
  362. int ZEXPORT deflateParams(strm, level, strategy)
  363. z_streamp strm;
  364. int level;
  365. int strategy;
  366. {
  367. deflate_state *s;
  368. compress_func func;
  369. int err = Z_OK;
  370. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  371. s = strm->state;
  372. #ifdef FASTEST
  373. if (level != 0) level = 1;
  374. #else
  375. if (level == Z_DEFAULT_COMPRESSION) level = 6;
  376. #endif
  377. if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) {
  378. return Z_STREAM_ERROR;
  379. }
  380. func = configuration_table[s->level].func;
  381. if (func != configuration_table[level].func && strm->total_in != 0) {
  382. /* Flush the last buffer: */
  383. err = deflate(strm, Z_PARTIAL_FLUSH);
  384. }
  385. if (s->level != level) {
  386. s->level = level;
  387. s->max_lazy_match = configuration_table[level].max_lazy;
  388. s->good_match = configuration_table[level].good_length;
  389. s->nice_match = configuration_table[level].nice_length;
  390. s->max_chain_length = configuration_table[level].max_chain;
  391. }
  392. s->strategy = strategy;
  393. return err;
  394. }
  395. /* ========================================================================= */
  396. int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain)
  397. z_streamp strm;
  398. int good_length;
  399. int max_lazy;
  400. int nice_length;
  401. int max_chain;
  402. {
  403. deflate_state *s;
  404. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  405. s = strm->state;
  406. s->good_match = good_length;
  407. s->max_lazy_match = max_lazy;
  408. s->nice_match = nice_length;
  409. s->max_chain_length = max_chain;
  410. return Z_OK;
  411. }
  412. /* =========================================================================
  413. * For the default windowBits of 15 and memLevel of 8, this function returns
  414. * a close to exact, as well as small, upper bound on the compressed size.
  415. * They are coded as constants here for a reason--if the #define's are
  416. * changed, then this function needs to be changed as well. The return
  417. * value for 15 and 8 only works for those exact settings.
  418. *
  419. * For any setting other than those defaults for windowBits and memLevel,
  420. * the value returned is a conservative worst case for the maximum expansion
  421. * resulting from using fixed blocks instead of stored blocks, which deflate
  422. * can emit on compressed data for some combinations of the parameters.
  423. *
  424. * This function could be more sophisticated to provide closer upper bounds
  425. * for every combination of windowBits and memLevel, as well as wrap.
  426. * But even the conservative upper bound of about 14% expansion does not
  427. * seem onerous for output buffer allocation.
  428. */
  429. uLong ZEXPORT deflateBound(strm, sourceLen)
  430. z_streamp strm;
  431. uLong sourceLen;
  432. {
  433. deflate_state *s;
  434. uLong destLen;
  435. /* conservative upper bound */
  436. destLen = sourceLen +
  437. ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 11;
  438. /* if can't get parameters, return conservative bound */
  439. if (strm == Z_NULL || strm->state == Z_NULL)
  440. return destLen;
  441. /* if not default parameters, return conservative bound */
  442. s = strm->state;
  443. if (s->w_bits != 15 || s->hash_bits != 8 + 7)
  444. return destLen;
  445. /* default settings: return tight bound for that case */
  446. return compressBound(sourceLen);
  447. }
  448. /* =========================================================================
  449. * Put a short in the pending buffer. The 16-bit value is put in MSB order.
  450. * IN assertion: the stream state is correct and there is enough room in
  451. * pending_buf.
  452. */
  453. local void putShortMSB (s, b)
  454. deflate_state *s;
  455. uInt b;
  456. {
  457. put_byte(s, (Byte)(b >> 8));
  458. put_byte(s, (Byte)(b & 0xff));
  459. }
  460. /* =========================================================================
  461. * Flush as much pending output as possible. All deflate() output goes
  462. * through this function so some applications may wish to modify it
  463. * to avoid allocating a large strm->next_out buffer and copying into it.
  464. * (See also read_buf()).
  465. */
  466. local void flush_pending(strm)
  467. z_streamp strm;
  468. {
  469. unsigned len = strm->state->pending;
  470. if (len > strm->avail_out) len = strm->avail_out;
  471. if (len == 0) return;
  472. zmemcpy(strm->next_out, strm->state->pending_out, len);
  473. strm->next_out += len;
  474. strm->state->pending_out += len;
  475. strm->total_out += len;
  476. strm->avail_out -= len;
  477. strm->state->pending -= len;
  478. if (strm->state->pending == 0) {
  479. strm->state->pending_out = strm->state->pending_buf;
  480. }
  481. }
  482. /* ========================================================================= */
  483. int ZEXPORT deflate (strm, flush)
  484. z_streamp strm;
  485. int flush;
  486. {
  487. int old_flush; /* value of flush param for previous deflate call */
  488. deflate_state *s;
  489. if (strm == Z_NULL || strm->state == Z_NULL ||
  490. flush > Z_FINISH || flush < 0) {
  491. return Z_STREAM_ERROR;
  492. }
  493. s = strm->state;
  494. if (strm->next_out == Z_NULL ||
  495. (strm->next_in == Z_NULL && strm->avail_in != 0) ||
  496. (s->status == FINISH_STATE && flush != Z_FINISH)) {
  497. ERR_RETURN(strm, Z_STREAM_ERROR);
  498. }
  499. if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
  500. s->strm = strm; /* just in case */
  501. old_flush = s->last_flush;
  502. s->last_flush = flush;
  503. /* Write the header */
  504. if (s->status == INIT_STATE) {
  505. #ifdef GZIP
  506. if (s->wrap == 2) {
  507. strm->adler = crc32(0L, Z_NULL, 0);
  508. put_byte(s, 31);
  509. put_byte(s, 139);
  510. put_byte(s, 8);
  511. if (s->gzhead == NULL) {
  512. put_byte(s, 0);
  513. put_byte(s, 0);
  514. put_byte(s, 0);
  515. put_byte(s, 0);
  516. put_byte(s, 0);
  517. put_byte(s, s->level == 9 ? 2 :
  518. (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
  519. 4 : 0));
  520. put_byte(s, OS_CODE);
  521. s->status = BUSY_STATE;
  522. }
  523. else {
  524. put_byte(s, (s->gzhead->text ? 1 : 0) +
  525. (s->gzhead->hcrc ? 2 : 0) +
  526. (s->gzhead->extra == Z_NULL ? 0 : 4) +
  527. (s->gzhead->name == Z_NULL ? 0 : 8) +
  528. (s->gzhead->comment == Z_NULL ? 0 : 16)
  529. );
  530. put_byte(s, (Byte)(s->gzhead->time & 0xff));
  531. put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff));
  532. put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff));
  533. put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff));
  534. put_byte(s, s->level == 9 ? 2 :
  535. (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ?
  536. 4 : 0));
  537. put_byte(s, s->gzhead->os & 0xff);
  538. if (s->gzhead->extra != NULL) {
  539. put_byte(s, s->gzhead->extra_len & 0xff);
  540. put_byte(s, (s->gzhead->extra_len >> 8) & 0xff);
  541. }
  542. if (s->gzhead->hcrc)
  543. strm->adler = crc32(strm->adler, s->pending_buf,
  544. s->pending);
  545. s->gzindex = 0;
  546. s->status = EXTRA_STATE;
  547. }
  548. }
  549. else
  550. #endif
  551. {
  552. uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
  553. uInt level_flags;
  554. if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2)
  555. level_flags = 0;
  556. else if (s->level < 6)
  557. level_flags = 1;
  558. else if (s->level == 6)
  559. level_flags = 2;
  560. else
  561. level_flags = 3;
  562. header |= (level_flags << 6);
  563. if (s->strstart != 0) header |= PRESET_DICT;
  564. header += 31 - (header % 31);
  565. s->status = BUSY_STATE;
  566. putShortMSB(s, header);
  567. /* Save the adler32 of the preset dictionary: */
  568. if (s->strstart != 0) {
  569. putShortMSB(s, (uInt)(strm->adler >> 16));
  570. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  571. }
  572. strm->adler = adler32(0L, Z_NULL, 0);
  573. }
  574. }
  575. #ifdef GZIP
  576. if (s->status == EXTRA_STATE) {
  577. if (s->gzhead->extra != NULL) {
  578. uInt beg = s->pending; /* start of bytes to update crc */
  579. while (s->gzindex < (s->gzhead->extra_len & 0xffff)) {
  580. if (s->pending == s->pending_buf_size) {
  581. if (s->gzhead->hcrc && s->pending > beg)
  582. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  583. s->pending - beg);
  584. flush_pending(strm);
  585. beg = s->pending;
  586. if (s->pending == s->pending_buf_size)
  587. break;
  588. }
  589. put_byte(s, s->gzhead->extra[s->gzindex]);
  590. s->gzindex++;
  591. }
  592. if (s->gzhead->hcrc && s->pending > beg)
  593. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  594. s->pending - beg);
  595. if (s->gzindex == s->gzhead->extra_len) {
  596. s->gzindex = 0;
  597. s->status = NAME_STATE;
  598. }
  599. }
  600. else
  601. s->status = NAME_STATE;
  602. }
  603. if (s->status == NAME_STATE) {
  604. if (s->gzhead->name != NULL) {
  605. uInt beg = s->pending; /* start of bytes to update crc */
  606. int val;
  607. do {
  608. if (s->pending == s->pending_buf_size) {
  609. if (s->gzhead->hcrc && s->pending > beg)
  610. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  611. s->pending - beg);
  612. flush_pending(strm);
  613. beg = s->pending;
  614. if (s->pending == s->pending_buf_size) {
  615. val = 1;
  616. break;
  617. }
  618. }
  619. val = s->gzhead->name[s->gzindex++];
  620. put_byte(s, val);
  621. } while (val != 0);
  622. if (s->gzhead->hcrc && s->pending > beg)
  623. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  624. s->pending - beg);
  625. if (val == 0) {
  626. s->gzindex = 0;
  627. s->status = COMMENT_STATE;
  628. }
  629. }
  630. else
  631. s->status = COMMENT_STATE;
  632. }
  633. if (s->status == COMMENT_STATE) {
  634. if (s->gzhead->comment != NULL) {
  635. uInt beg = s->pending; /* start of bytes to update crc */
  636. int val;
  637. do {
  638. if (s->pending == s->pending_buf_size) {
  639. if (s->gzhead->hcrc && s->pending > beg)
  640. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  641. s->pending - beg);
  642. flush_pending(strm);
  643. beg = s->pending;
  644. if (s->pending == s->pending_buf_size) {
  645. val = 1;
  646. break;
  647. }
  648. }
  649. val = s->gzhead->comment[s->gzindex++];
  650. put_byte(s, val);
  651. } while (val != 0);
  652. if (s->gzhead->hcrc && s->pending > beg)
  653. strm->adler = crc32(strm->adler, s->pending_buf + beg,
  654. s->pending - beg);
  655. if (val == 0)
  656. s->status = HCRC_STATE;
  657. }
  658. else
  659. s->status = HCRC_STATE;
  660. }
  661. if (s->status == HCRC_STATE) {
  662. if (s->gzhead->hcrc) {
  663. if (s->pending + 2 > s->pending_buf_size)
  664. flush_pending(strm);
  665. if (s->pending + 2 <= s->pending_buf_size) {
  666. put_byte(s, (Byte)(strm->adler & 0xff));
  667. put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
  668. strm->adler = crc32(0L, Z_NULL, 0);
  669. s->status = BUSY_STATE;
  670. }
  671. }
  672. else
  673. s->status = BUSY_STATE;
  674. }
  675. #endif
  676. /* Flush as much pending output as possible */
  677. if (s->pending != 0) {
  678. flush_pending(strm);
  679. if (strm->avail_out == 0) {
  680. /* Since avail_out is 0, deflate will be called again with
  681. * more output space, but possibly with both pending and
  682. * avail_in equal to zero. There won't be anything to do,
  683. * but this is not an error situation so make sure we
  684. * return OK instead of BUF_ERROR at next call of deflate:
  685. */
  686. s->last_flush = -1;
  687. return Z_OK;
  688. }
  689. /* Make sure there is something to do and avoid duplicate consecutive
  690. * flushes. For repeated and useless calls with Z_FINISH, we keep
  691. * returning Z_STREAM_END instead of Z_BUF_ERROR.
  692. */
  693. } else if (strm->avail_in == 0 && flush <= old_flush &&
  694. flush != Z_FINISH) {
  695. ERR_RETURN(strm, Z_BUF_ERROR);
  696. }
  697. /* User must not provide more input after the first FINISH: */
  698. if (s->status == FINISH_STATE && strm->avail_in != 0) {
  699. ERR_RETURN(strm, Z_BUF_ERROR);
  700. }
  701. /* Start a new block or continue the current one.
  702. */
  703. if (strm->avail_in != 0 || s->lookahead != 0 ||
  704. (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
  705. block_state bstate;
  706. bstate = (*(configuration_table[s->level].func))(s, flush);
  707. if (bstate == finish_started || bstate == finish_done) {
  708. s->status = FINISH_STATE;
  709. }
  710. if (bstate == need_more || bstate == finish_started) {
  711. if (strm->avail_out == 0) {
  712. s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
  713. }
  714. return Z_OK;
  715. /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
  716. * of deflate should use the same flush parameter to make sure
  717. * that the flush is complete. So we don't have to output an
  718. * empty block here, this will be done at next call. This also
  719. * ensures that for a very small output buffer, we emit at most
  720. * one empty block.
  721. */
  722. }
  723. if (bstate == block_done) {
  724. if (flush == Z_PARTIAL_FLUSH) {
  725. _tr_align(s);
  726. } else { /* FULL_FLUSH or SYNC_FLUSH */
  727. _tr_stored_block(s, (char*)0, 0L, 0);
  728. /* For a full flush, this empty block will be recognized
  729. * as a special marker by inflate_sync().
  730. */
  731. if (flush == Z_FULL_FLUSH) {
  732. CLEAR_HASH(s); /* forget history */
  733. }
  734. }
  735. flush_pending(strm);
  736. if (strm->avail_out == 0) {
  737. s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
  738. return Z_OK;
  739. }
  740. }
  741. }
  742. Assert(strm->avail_out > 0, "bug2");
  743. if (flush != Z_FINISH) return Z_OK;
  744. if (s->wrap <= 0) return Z_STREAM_END;
  745. /* Write the trailer */
  746. #ifdef GZIP
  747. if (s->wrap == 2) {
  748. put_byte(s, (Byte)(strm->adler & 0xff));
  749. put_byte(s, (Byte)((strm->adler >> 8) & 0xff));
  750. put_byte(s, (Byte)((strm->adler >> 16) & 0xff));
  751. put_byte(s, (Byte)((strm->adler >> 24) & 0xff));
  752. put_byte(s, (Byte)(strm->total_in & 0xff));
  753. put_byte(s, (Byte)((strm->total_in >> 8) & 0xff));
  754. put_byte(s, (Byte)((strm->total_in >> 16) & 0xff));
  755. put_byte(s, (Byte)((strm->total_in >> 24) & 0xff));
  756. }
  757. else
  758. #endif
  759. {
  760. putShortMSB(s, (uInt)(strm->adler >> 16));
  761. putShortMSB(s, (uInt)(strm->adler & 0xffff));
  762. }
  763. flush_pending(strm);
  764. /* If avail_out is zero, the application will call deflate again
  765. * to flush the rest.
  766. */
  767. if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */
  768. return s->pending != 0 ? Z_OK : Z_STREAM_END;
  769. }
  770. /* ========================================================================= */
  771. int ZEXPORT deflateEnd (strm)
  772. z_streamp strm;
  773. {
  774. int status;
  775. if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
  776. status = strm->state->status;
  777. if (status != INIT_STATE &&
  778. status != EXTRA_STATE &&
  779. status != NAME_STATE &&
  780. status != COMMENT_STATE &&
  781. status != HCRC_STATE &&
  782. status != BUSY_STATE &&
  783. status != FINISH_STATE) {
  784. return Z_STREAM_ERROR;
  785. }
  786. /* Deallocate in reverse order of allocations: */
  787. TRY_FREE(strm, strm->state->pending_buf);
  788. TRY_FREE(strm, strm->state->head);
  789. TRY_FREE(strm, strm->state->prev);
  790. TRY_FREE(strm, strm->state->window);
  791. ZFREE(strm, strm->state);
  792. strm->state = Z_NULL;
  793. return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
  794. }
  795. /* =========================================================================
  796. * Copy the source state to the destination state.
  797. * To simplify the source, this is not supported for 16-bit MSDOS (which
  798. * doesn't have enough memory anyway to duplicate compression states).
  799. */
  800. int ZEXPORT deflateCopy (dest, source)
  801. z_streamp dest;
  802. z_streamp source;
  803. {
  804. #ifdef MAXSEG_64K
  805. return Z_STREAM_ERROR;
  806. #else
  807. deflate_state *ds;
  808. deflate_state *ss;
  809. ushf *overlay;
  810. if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) {
  811. return Z_STREAM_ERROR;
  812. }
  813. ss = source->state;
  814. zmemcpy(dest, source, sizeof(z_stream));
  815. ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
  816. if (ds == Z_NULL) return Z_MEM_ERROR;
  817. dest->state = (struct internal_state FAR *) ds;
  818. zmemcpy(ds, ss, sizeof(deflate_state));
  819. ds->strm = dest;
  820. ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
  821. ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos));
  822. ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos));
  823. overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
  824. ds->pending_buf = (uchf *) overlay;
  825. if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
  826. ds->pending_buf == Z_NULL) {
  827. deflateEnd (dest);
  828. return Z_MEM_ERROR;
  829. }
  830. /* following zmemcpy do not work for 16-bit MSDOS */
  831. zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
  832. zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
  833. zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
  834. zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
  835. ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
  836. ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
  837. ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
  838. ds->l_desc.dyn_tree = ds->dyn_ltree;
  839. ds->d_desc.dyn_tree = ds->dyn_dtree;
  840. ds->bl_desc.dyn_tree = ds->bl_tree;
  841. return Z_OK;
  842. #endif /* MAXSEG_64K */
  843. }
  844. /* ===========================================================================
  845. * Read a new buffer from the current input stream, update the adler32
  846. * and total number of bytes read. All deflate() input goes through
  847. * this function so some applications may wish to modify it to avoid
  848. * allocating a large strm->next_in buffer and copying from it.
  849. * (See also flush_pending()).
  850. */
  851. local int read_buf(strm, buf, size)
  852. z_streamp strm;
  853. Bytef *buf;
  854. unsigned size;
  855. {
  856. unsigned len = strm->avail_in;
  857. if (len > size) len = size;
  858. if (len == 0) return 0;
  859. strm->avail_in -= len;
  860. if (strm->state->wrap == 1) {
  861. strm->adler = adler32(strm->adler, strm->next_in, len);
  862. }
  863. #ifdef GZIP
  864. else if (strm->state->wrap == 2) {
  865. strm->adler = crc32(strm->adler, strm->next_in, len);
  866. }
  867. #endif
  868. zmemcpy(buf, strm->next_in, len);
  869. strm->next_in += len;
  870. strm->total_in += len;
  871. return (int)len;
  872. }
  873. /* ===========================================================================
  874. * Initialize the "longest match" routines for a new zlib stream
  875. */
  876. local void lm_init (s)
  877. deflate_state *s;
  878. {
  879. s->window_size = (ulg)2L*s->w_size;
  880. CLEAR_HASH(s);
  881. /* Set the default configuration parameters:
  882. */
  883. s->max_lazy_match = configuration_table[s->level].max_lazy;
  884. s->good_match = configuration_table[s->level].good_length;
  885. s->nice_match = configuration_table[s->level].nice_length;
  886. s->max_chain_length = configuration_table[s->level].max_chain;
  887. s->strstart = 0;
  888. s->block_start = 0L;
  889. s->lookahead = 0;
  890. s->match_length = s->prev_length = MIN_MATCH-1;
  891. s->match_available = 0;
  892. s->ins_h = 0;
  893. #ifndef FASTEST
  894. #ifdef ASMV
  895. match_init(); /* initialize the asm code */
  896. #endif
  897. #endif
  898. }
  899. #ifndef FASTEST
  900. /* ===========================================================================
  901. * Set match_start to the longest match starting at the given string and
  902. * return its length. Matches shorter or equal to prev_length are discarded,
  903. * in which case the result is equal to prev_length and match_start is
  904. * garbage.
  905. * IN assertions: cur_match is the head of the hash chain for the current
  906. * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
  907. * OUT assertion: the match length is not greater than s->lookahead.
  908. */
  909. #ifndef ASMV
  910. /* For 80x86 and 680x0, an optimized version will be provided in match.asm or
  911. * match.S. The code will be functionally equivalent.
  912. */
  913. local uInt longest_match(s, cur_match)
  914. deflate_state *s;
  915. IPos cur_match; /* current match */
  916. {
  917. unsigned chain_length = s->max_chain_length;/* max hash chain length */
  918. register Bytef *scan = s->window + s->strstart; /* current string */
  919. register Bytef *match; /* matched string */
  920. register int len; /* length of current match */
  921. int best_len = s->prev_length; /* best match length so far */
  922. int nice_match = s->nice_match; /* stop if match long enough */
  923. IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
  924. s->strstart - (IPos)MAX_DIST(s) : NIL;
  925. /* Stop when cur_match becomes <= limit. To simplify the code,
  926. * we prevent matches with the string of window index 0.
  927. */
  928. Posf *prev = s->prev;
  929. uInt wmask = s->w_mask;
  930. #ifdef UNALIGNED_OK
  931. /* Compare two bytes at a time. Note: this is not always beneficial.
  932. * Try with and without -DUNALIGNED_OK to check.
  933. */
  934. register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
  935. register ush scan_start = *(ushf*)scan;
  936. register ush scan_end = *(ushf*)(scan+best_len-1);
  937. #else
  938. register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  939. register Byte scan_end1 = scan[best_len-1];
  940. register Byte scan_end = scan[best_len];
  941. #endif
  942. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  943. * It is easy to get rid of this optimization if necessary.
  944. */
  945. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  946. /* Do not waste too much time if we already have a good match: */
  947. if (s->prev_length >= s->good_match) {
  948. chain_length >>= 2;
  949. }
  950. /* Do not look for matches beyond the end of the input. This is necessary
  951. * to make deflate deterministic.
  952. */
  953. if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
  954. Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  955. do {
  956. Assert(cur_match < s->strstart, "no future");
  957. match = s->window + cur_match;
  958. /* Skip to next match if the match length cannot increase
  959. * or if the match length is less than 2. Note that the checks below
  960. * for insufficient lookahead only occur occasionally for performance
  961. * reasons. Therefore uninitialized memory will be accessed, and
  962. * conditional jumps will be made that depend on those values.
  963. * However the length of the match is limited to the lookahead, so
  964. * the output of deflate is not affected by the uninitialized values.
  965. */
  966. #if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
  967. /* This code assumes sizeof(unsigned short) == 2. Do not use
  968. * UNALIGNED_OK if your compiler uses a different size.
  969. */
  970. if (*(ushf*)(match+best_len-1) != scan_end ||
  971. *(ushf*)match != scan_start) continue;
  972. /* It is not necessary to compare scan[2] and match[2] since they are
  973. * always equal when the other bytes match, given that the hash keys
  974. * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
  975. * strstart+3, +5, ... up to strstart+257. We check for insufficient
  976. * lookahead only every 4th comparison; the 128th check will be made
  977. * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
  978. * necessary to put more guard bytes at the end of the window, or
  979. * to check more often for insufficient lookahead.
  980. */
  981. Assert(scan[2] == match[2], "scan[2]?");
  982. scan++, match++;
  983. do {
  984. } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  985. *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  986. *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  987. *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
  988. scan < strend);
  989. /* The funny "do {}" generates better code on most compilers */
  990. /* Here, scan <= window+strstart+257 */
  991. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  992. if (*scan == *match) scan++;
  993. len = (MAX_MATCH - 1) - (int)(strend-scan);
  994. scan = strend - (MAX_MATCH-1);
  995. #else /* UNALIGNED_OK */
  996. if (match[best_len] != scan_end ||
  997. match[best_len-1] != scan_end1 ||
  998. *match != *scan ||
  999. *++match != scan[1]) continue;
  1000. /* The check at best_len-1 can be removed because it will be made
  1001. * again later. (This heuristic is not always a win.)
  1002. * It is not necessary to compare scan[2] and match[2] since they
  1003. * are always equal when the other bytes match, given that
  1004. * the hash keys are equal and that HASH_BITS >= 8.
  1005. */
  1006. scan += 2, match++;
  1007. Assert(*scan == *match, "match[2]?");
  1008. /* We check for insufficient lookahead only every 8th comparison;
  1009. * the 256th check will be made at strstart+258.
  1010. */
  1011. do {
  1012. } while (*++scan == *++match && *++scan == *++match &&
  1013. *++scan == *++match && *++scan == *++match &&
  1014. *++scan == *++match && *++scan == *++match &&
  1015. *++scan == *++match && *++scan == *++match &&
  1016. scan < strend);
  1017. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1018. len = MAX_MATCH - (int)(strend - scan);
  1019. scan = strend - MAX_MATCH;
  1020. #endif /* UNALIGNED_OK */
  1021. if (len > best_len) {
  1022. s->match_start = cur_match;
  1023. best_len = len;
  1024. if (len >= nice_match) break;
  1025. #ifdef UNALIGNED_OK
  1026. scan_end = *(ushf*)(scan+best_len-1);
  1027. #else
  1028. scan_end1 = scan[best_len-1];
  1029. scan_end = scan[best_len];
  1030. #endif
  1031. }
  1032. } while ((cur_match = prev[cur_match & wmask]) > limit
  1033. && --chain_length != 0);
  1034. if ((uInt)best_len <= s->lookahead) return (uInt)best_len;
  1035. return s->lookahead;
  1036. }
  1037. #endif /* ASMV */
  1038. #endif /* FASTEST */
  1039. /* ---------------------------------------------------------------------------
  1040. * Optimized version for level == 1 or strategy == Z_RLE only
  1041. */
  1042. local uInt longest_match_fast(s, cur_match)
  1043. deflate_state *s;
  1044. IPos cur_match; /* current match */
  1045. {
  1046. register Bytef *scan = s->window + s->strstart; /* current string */
  1047. register Bytef *match; /* matched string */
  1048. register int len; /* length of current match */
  1049. register Bytef *strend = s->window + s->strstart + MAX_MATCH;
  1050. /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
  1051. * It is easy to get rid of this optimization if necessary.
  1052. */
  1053. Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
  1054. Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
  1055. Assert(cur_match < s->strstart, "no future");
  1056. match = s->window + cur_match;
  1057. /* Return failure if the match length is less than 2:
  1058. */
  1059. if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1;
  1060. /* The check at best_len-1 can be removed because it will be made
  1061. * again later. (This heuristic is not always a win.)
  1062. * It is not necessary to compare scan[2] and match[2] since they
  1063. * are always equal when the other bytes match, given that
  1064. * the hash keys are equal and that HASH_BITS >= 8.
  1065. */
  1066. scan += 2, match += 2;
  1067. Assert(*scan == *match, "match[2]?");
  1068. /* We check for insufficient lookahead only every 8th comparison;
  1069. * the 256th check will be made at strstart+258.
  1070. */
  1071. do {
  1072. } while (*++scan == *++match && *++scan == *++match &&
  1073. *++scan == *++match && *++scan == *++match &&
  1074. *++scan == *++match && *++scan == *++match &&
  1075. *++scan == *++match && *++scan == *++match &&
  1076. scan < strend);
  1077. Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
  1078. len = MAX_MATCH - (int)(strend - scan);
  1079. if (len < MIN_MATCH) return MIN_MATCH - 1;
  1080. s->match_start = cur_match;
  1081. return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead;
  1082. }
  1083. #ifdef DEBUG_LIBZ
  1084. /* ===========================================================================
  1085. * Check that the match at match_start is indeed a match.
  1086. */
  1087. local void check_match(s, start, match, length)
  1088. deflate_state *s;
  1089. IPos start, match;
  1090. int length;
  1091. {
  1092. /* check that the match is indeed a match */
  1093. if (zmemcmp(s->window + match,
  1094. s->window + start, length) != EQUAL) {
  1095. fprintf(stderr, " start %u, match %u, length %d\n",
  1096. start, match, length);
  1097. do {
  1098. fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
  1099. } while (--length != 0);
  1100. z_error("invalid match");
  1101. }
  1102. if (z_verbose > 1) {
  1103. fprintf(stderr,"\\[%d,%d]", start-match, length);
  1104. do { putc(s->window[start++], stderr); } while (--length != 0);
  1105. }
  1106. }
  1107. #else
  1108. # define check_match(s, start, match, length)
  1109. #endif /* DEBUG_LIBZ */
  1110. /* ===========================================================================
  1111. * Fill the window when the lookahead becomes insufficient.
  1112. * Updates strstart and lookahead.
  1113. *
  1114. * IN assertion: lookahead < MIN_LOOKAHEAD
  1115. * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
  1116. * At least one byte has been read, or avail_in == 0; reads are
  1117. * performed for at least two bytes (required for the zip translate_eol
  1118. * option -- not supported here).
  1119. */
  1120. local void fill_window(s)
  1121. deflate_state *s;
  1122. {
  1123. register unsigned n, m;
  1124. register Posf *p;
  1125. unsigned more; /* Amount of free space at the end of the window. */
  1126. uInt wsize = s->w_size;
  1127. do {
  1128. more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
  1129. /* Deal with !@#$% 64K limit: */
  1130. if (sizeof(int) <= 2) {
  1131. if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
  1132. more = wsize;
  1133. } else if (more == (unsigned)(-1)) {
  1134. /* Very unlikely, but possible on 16 bit machine if
  1135. * strstart == 0 && lookahead == 1 (input done a byte at time)
  1136. */
  1137. more--;
  1138. }
  1139. }
  1140. /* If the window is almost full and there is insufficient lookahead,
  1141. * move the upper half to the lower one to make room in the upper half.
  1142. */
  1143. if (s->strstart >= wsize+MAX_DIST(s)) {
  1144. zmemcpy(s->window, s->window+wsize, (unsigned)wsize);
  1145. s->match_start -= wsize;
  1146. s->strstart -= wsize; /* we now have strstart >= MAX_DIST */
  1147. s->block_start -= (long) wsize;
  1148. /* Slide the hash table (could be avoided with 32 bit values
  1149. at the expense of memory usage). We slide even when level == 0
  1150. to keep the hash table consistent if we switch back to level > 0
  1151. later. (Using level 0 permanently is not an optimal usage of
  1152. zlib, so we don't care about this pathological case.)
  1153. */
  1154. /* %%% avoid this when Z_RLE */
  1155. n = s->hash_size;
  1156. p = &s->head[n];
  1157. do {
  1158. m = *--p;
  1159. *p = (Pos)(m >= wsize ? m-wsize : NIL);
  1160. } while (--n);
  1161. n = wsize;
  1162. #ifndef FASTEST
  1163. p = &s->prev[n];
  1164. do {
  1165. m = *--p;
  1166. *p = (Pos)(m >= wsize ? m-wsize : NIL);
  1167. /* If n is not on any hash chain, prev[n] is garbage but
  1168. * its value will never be used.
  1169. */
  1170. } while (--n);
  1171. #endif
  1172. more += wsize;
  1173. }
  1174. if (s->strm->avail_in == 0) return;
  1175. /* If there was no sliding:
  1176. * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
  1177. * more == window_size - lookahead - strstart
  1178. * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
  1179. * => more >= window_size - 2*WSIZE + 2
  1180. * In the BIG_MEM or MMAP case (not yet supported),
  1181. * window_size == input_size + MIN_LOOKAHEAD &&
  1182. * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
  1183. * Otherwise, window_size == 2*WSIZE so more >= 2.
  1184. * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
  1185. */
  1186. Assert(more >= 2, "more < 2");
  1187. n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more);
  1188. s->lookahead += n;
  1189. /* Initialize the hash value now that we have some input: */
  1190. if (s->lookahead >= MIN_MATCH) {
  1191. s->ins_h = s->window[s->strstart];
  1192. UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  1193. #if MIN_MATCH != 3
  1194. Call UPDATE_HASH() MIN_MATCH-3 more times
  1195. #endif
  1196. }
  1197. /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
  1198. * but this is not important since only literal bytes will be emitted.
  1199. */
  1200. } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
  1201. }
  1202. /* ===========================================================================
  1203. * Flush the current block, with given end-of-file flag.
  1204. * IN assertion: strstart is set to the end of the current match.
  1205. */
  1206. #define FLUSH_BLOCK_ONLY(s, eof) { \
  1207. _tr_flush_block(s, (s->block_start >= 0L ? \
  1208. (charf *)&s->window[(unsigned)s->block_start] : \
  1209. (charf *)Z_NULL), \
  1210. (ulg)((long)s->strstart - s->block_start), \
  1211. (eof)); \
  1212. s->block_start = s->strstart; \
  1213. flush_pending(s->strm); \
  1214. Tracev((stderr,"[FLUSH]")); \
  1215. }
  1216. /* Same but force premature exit if necessary. */
  1217. #define FLUSH_BLOCK(s, eof) { \
  1218. FLUSH_BLOCK_ONLY(s, eof); \
  1219. if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
  1220. }
  1221. /* ===========================================================================
  1222. * Copy without compression as much as possible from the input stream, return
  1223. * the current block state.
  1224. * This function does not insert new strings in the dictionary since
  1225. * uncompressible data is probably not useful. This function is used
  1226. * only for the level=0 compression option.
  1227. * NOTE: this function should be optimized to avoid extra copying from
  1228. * window to pending_buf.
  1229. */
  1230. local block_state deflate_stored(s, flush)
  1231. deflate_state *s;
  1232. int flush;
  1233. {
  1234. /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
  1235. * to pending_buf_size, and each stored block has a 5 byte header:
  1236. */
  1237. ulg max_block_size = 0xffff;
  1238. ulg max_start;
  1239. if (max_block_size > s->pending_buf_size - 5) {
  1240. max_block_size = s->pending_buf_size - 5;
  1241. }
  1242. /* Copy as much as possible from input to output: */
  1243. for (;;) {
  1244. /* Fill the window as much as possible: */
  1245. if (s->lookahead <= 1) {
  1246. Assert(s->strstart < s->w_size+MAX_DIST(s) ||
  1247. s->block_start >= (long)s->w_size, "slide too late");
  1248. fill_window(s);
  1249. if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
  1250. if (s->lookahead == 0) break; /* flush the current block */
  1251. }
  1252. Assert(s->block_start >= 0L, "block gone");
  1253. s->strstart += s->lookahead;
  1254. s->lookahead = 0;
  1255. /* Emit a stored block if pending_buf will be full: */
  1256. max_start = s->block_start + max_block_size;
  1257. if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
  1258. /* strstart == 0 is possible when wraparound on 16-bit machine */
  1259. s->lookahead = (uInt)(s->strstart - max_start);
  1260. s->strstart = (uInt)max_start;
  1261. FLUSH_BLOCK(s, 0);
  1262. }
  1263. /* Flush if we may have to slide, otherwise block_start may become
  1264. * negative and the data will be gone:
  1265. */
  1266. if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
  1267. FLUSH_BLOCK(s, 0);
  1268. }
  1269. }
  1270. FLUSH_BLOCK(s, flush == Z_FINISH);
  1271. return flush == Z_FINISH ? finish_done : block_done;
  1272. }
  1273. /* ===========================================================================
  1274. * Compress as much as possible from the input stream, return the current
  1275. * block state.
  1276. * This function does not perform lazy evaluation of matches and inserts
  1277. * new strings in the dictionary only for unmatched strings or for short
  1278. * matches. It is used only for the fast compression options.
  1279. */
  1280. local block_state deflate_fast(s, flush)
  1281. deflate_state *s;
  1282. int flush;
  1283. {
  1284. IPos hash_head = NIL; /* head of the hash chain */
  1285. int bflush; /* set if current block must be flushed */
  1286. for (;;) {
  1287. /* Make sure that we always have enough lookahead, except
  1288. * at the end of the input file. We need MAX_MATCH bytes
  1289. * for the next match, plus MIN_MATCH bytes to insert the
  1290. * string following the next match.
  1291. */
  1292. if (s->lookahead < MIN_LOOKAHEAD) {
  1293. fill_window(s);
  1294. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1295. return need_more;
  1296. }
  1297. if (s->lookahead == 0) break; /* flush the current block */
  1298. }
  1299. /* Insert the string window[strstart .. strstart+2] in the
  1300. * dictionary, and set hash_head to the head of the hash chain:
  1301. */
  1302. if (s->lookahead >= MIN_MATCH) {
  1303. INSERT_STRING(s, s->strstart, hash_head);
  1304. }
  1305. /* Find the longest match, discarding those <= prev_length.
  1306. * At this point we have always match_length < MIN_MATCH
  1307. */
  1308. if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
  1309. /* To simplify the code, we prevent matches with the string
  1310. * of window index 0 (in particular we have to avoid a match
  1311. * of the string with itself at the start of the input file).
  1312. */
  1313. #ifdef FASTEST
  1314. if ((s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) ||
  1315. (s->strategy == Z_RLE && s->strstart - hash_head == 1)) {
  1316. s->match_length = longest_match_fast (s, hash_head);
  1317. }
  1318. #else
  1319. if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
  1320. s->match_length = longest_match (s, hash_head);
  1321. } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
  1322. s->match_length = longest_match_fast (s, hash_head);
  1323. }
  1324. #endif
  1325. /* longest_match() or longest_match_fast() sets match_start */
  1326. }
  1327. if (s->match_length >= MIN_MATCH) {
  1328. check_match(s, s->strstart, s->match_start, s->match_length);
  1329. _tr_tally_dist(s, s->strstart - s->match_start,
  1330. s->match_length - MIN_MATCH, bflush);
  1331. s->lookahead -= s->match_length;
  1332. /* Insert new strings in the hash table only if the match length
  1333. * is not too large. This saves time but degrades compression.
  1334. */
  1335. #ifndef FASTEST
  1336. if (s->match_length <= s->max_insert_length &&
  1337. s->lookahead >= MIN_MATCH) {
  1338. s->match_length--; /* string at strstart already in table */
  1339. do {
  1340. s->strstart++;
  1341. INSERT_STRING(s, s->strstart, hash_head);
  1342. /* strstart never exceeds WSIZE-MAX_MATCH, so there are
  1343. * always MIN_MATCH bytes ahead.
  1344. */
  1345. } while (--s->match_length != 0);
  1346. s->strstart++;
  1347. } else
  1348. #endif
  1349. {
  1350. s->strstart += s->match_length;
  1351. s->match_length = 0;
  1352. s->ins_h = s->window[s->strstart];
  1353. UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
  1354. #if MIN_MATCH != 3
  1355. Call UPDATE_HASH() MIN_MATCH-3 more times
  1356. #endif
  1357. /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
  1358. * matter since it will be recomputed at next deflate call.
  1359. */
  1360. }
  1361. } else {
  1362. /* No match, output a literal byte */
  1363. Tracevv((stderr,"%c", s->window[s->strstart]));
  1364. _tr_tally_lit (s, s->window[s->strstart], bflush);
  1365. s->lookahead--;
  1366. s->strstart++;
  1367. }
  1368. if (bflush) FLUSH_BLOCK(s, 0);
  1369. }
  1370. FLUSH_BLOCK(s, flush == Z_FINISH);
  1371. return flush == Z_FINISH ? finish_done : block_done;
  1372. }
  1373. #ifndef FASTEST
  1374. /* ===========================================================================
  1375. * Same as above, but achieves better compression. We use a lazy
  1376. * evaluation for matches: a match is finally adopted only if there is
  1377. * no better match at the next window position.
  1378. */
  1379. local block_state deflate_slow(s, flush)
  1380. deflate_state *s;
  1381. int flush;
  1382. {
  1383. IPos hash_head = NIL; /* head of hash chain */
  1384. int bflush; /* set if current block must be flushed */
  1385. /* Process the input block. */
  1386. for (;;) {
  1387. /* Make sure that we always have enough lookahead, except
  1388. * at the end of the input file. We need MAX_MATCH bytes
  1389. * for the next match, plus MIN_MATCH bytes to insert the
  1390. * string following the next match.
  1391. */
  1392. if (s->lookahead < MIN_LOOKAHEAD) {
  1393. fill_window(s);
  1394. if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
  1395. return need_more;
  1396. }
  1397. if (s->lookahead == 0) break; /* flush the current block */
  1398. }
  1399. /* Insert the string window[strstart .. strstart+2] in the
  1400. * dictionary, and set hash_head to the head of the hash chain:
  1401. */
  1402. if (s->lookahead >= MIN_MATCH) {
  1403. INSERT_STRING(s, s->strstart, hash_head);
  1404. }
  1405. /* Find the longest match, discarding those <= prev_length.
  1406. */
  1407. s->prev_length = s->match_length, s->prev_match = s->match_start;
  1408. s->match_length = MIN_MATCH-1;
  1409. if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
  1410. s->strstart - hash_head <= MAX_DIST(s)) {
  1411. /* To simplify the code, we prevent matches with the string
  1412. * of window index 0 (in particular we have to avoid a match
  1413. * of the string with itself at the start of the input file).
  1414. */
  1415. if (s->strategy != Z_HUFFMAN_ONLY && s->strategy != Z_RLE) {
  1416. s->match_length = longest_match (s, hash_head);
  1417. } else if (s->strategy == Z_RLE && s->strstart - hash_head == 1) {
  1418. s->match_length = longest_match_fast (s, hash_head);
  1419. }
  1420. /* longest_match() or longest_match_fast() sets match_start */
  1421. if (s->match_length <= 5 && (s->strategy == Z_FILTERED
  1422. #if TOO_FAR <= 32767
  1423. || (s->match_length == MIN_MATCH &&
  1424. s->strstart - s->match_start > TOO_FAR)
  1425. #endif
  1426. )) {
  1427. /* If prev_match is also MIN_MATCH, match_start is garbage
  1428. * but we will ignore the current match anyway.
  1429. */
  1430. s->match_length = MIN_MATCH-1;
  1431. }
  1432. }
  1433. /* If there was a match at the previous step and the current
  1434. * match is not better, output the previous match:
  1435. */
  1436. if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
  1437. uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
  1438. /* Do not insert strings in hash table beyond this. */
  1439. check_match(s, s->strstart-1, s->prev_match, s->prev_length);
  1440. _tr_tally_dist(s, s->strstart -1 - s->prev_match,
  1441. s->prev_length - MIN_MATCH, bflush);
  1442. /* Insert in hash table all strings up to the end of the match.
  1443. * strstart-1 and strstart are already inserted. If there is not
  1444. * enough lookahead, the last two strings are not inserted in
  1445. * the hash table.
  1446. */
  1447. s->lookahead -= s->prev_length-1;
  1448. s->prev_length -= 2;
  1449. do {
  1450. if (++s->strstart <= max_insert) {
  1451. INSERT_STRING(s, s->strstart, hash_head);
  1452. }
  1453. } while (--s->prev_length != 0);
  1454. s->match_available = 0;
  1455. s->match_length = MIN_MATCH-1;
  1456. s->strstart++;
  1457. if (bflush) FLUSH_BLOCK(s, 0);
  1458. } else if (s->match_available) {
  1459. /* If there was no match at the previous position, output a
  1460. * single literal. If there was a match but the current match
  1461. * is longer, truncate the previous match to a single literal.
  1462. */
  1463. Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1464. _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  1465. if (bflush) {
  1466. FLUSH_BLOCK_ONLY(s, 0);
  1467. }
  1468. s->strstart++;
  1469. s->lookahead--;
  1470. if (s->strm->avail_out == 0) return need_more;
  1471. } else {
  1472. /* There is no previous match to compare with, wait for
  1473. * the next step to decide.
  1474. */
  1475. s->match_available = 1;
  1476. s->strstart++;
  1477. s->lookahead--;
  1478. }
  1479. }
  1480. Assert (flush != Z_NO_FLUSH, "no flush?");
  1481. if (s->match_available) {
  1482. Tracevv((stderr,"%c", s->window[s->strstart-1]));
  1483. _tr_tally_lit(s, s->window[s->strstart-1], bflush);
  1484. s->match_available = 0;
  1485. }
  1486. FLUSH_BLOCK(s, flush == Z_FINISH);
  1487. return flush == Z_FINISH ? finish_done : block_done;
  1488. }
  1489. #endif /* FASTEST */
  1490. #if 0
  1491. /* ===========================================================================
  1492. * For Z_RLE, simply look for runs of bytes, generate matches only of distance
  1493. * one. Do not maintain a hash table. (It will be regenerated if this run of
  1494. * deflate switches away from Z_RLE.)
  1495. */
  1496. local block_state deflate_rle(s, flush)
  1497. deflate_state *s;
  1498. int flush;
  1499. {
  1500. int bflush; /* set if current block must be flushed */
  1501. uInt run; /* length of run */
  1502. uInt max; /* maximum length of run */
  1503. uInt prev; /* byte at distance one to match */
  1504. Bytef *scan; /* scan for end of run */
  1505. for (;;) {
  1506. /* Make sure that we always have enough lookahead, except
  1507. * at the end of the input file. We need MAX_MATCH bytes
  1508. * for the longest encodable run.
  1509. */
  1510. if (s->lookahead < MAX_MATCH) {
  1511. fill_window(s);
  1512. if (s->lookahead < MAX_MATCH && flush == Z_NO_FLUSH) {
  1513. return need_more;
  1514. }
  1515. if (s->lookahead == 0) break; /* flush the current block */
  1516. }
  1517. /* See how many times the previous byte repeats */
  1518. run = 0;
  1519. if (s->strstart > 0) { /* if there is a previous byte, that is */
  1520. max = s->lookahead < MAX_MATCH ? s->lookahead : MAX_MATCH;
  1521. scan = s->window + s->strstart - 1;
  1522. prev = *scan++;
  1523. do {
  1524. if (*scan++ != prev)
  1525. break;
  1526. } while (++run < max);
  1527. }
  1528. /* Emit match if have run of MIN_MATCH or longer, else emit literal */
  1529. if (run >= MIN_MATCH) {
  1530. check_match(s, s->strstart, s->strstart - 1, run);
  1531. _tr_tally_dist(s, 1, run - MIN_MATCH, bflush);
  1532. s->lookahead -= run;
  1533. s->strstart += run;
  1534. } else {
  1535. /* No match, output a literal byte */
  1536. Tracevv((stderr,"%c", s->window[s->strstart]));
  1537. _tr_tally_lit (s, s->window[s->strstart], bflush);
  1538. s->lookahead--;
  1539. s->strstart++;
  1540. }
  1541. if (bflush) FLUSH_BLOCK(s, 0);
  1542. }
  1543. FLUSH_BLOCK(s, flush == Z_FINISH);
  1544. return flush == Z_FINISH ? finish_done : block_done;
  1545. }
  1546. #endif