deflate.c 63 KB

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