002-arm-specific-optimisations-for-inflate.patch 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  1. From 6bac7a3e0ebcd3147294b73acb34606eba18ae7f Mon Sep 17 00:00:00 2001
  2. From: Simon Hosie <[email protected]>
  3. Date: Wed, 12 Apr 2017 12:52:33 -0700
  4. Subject: [PATCH 1/2] Prepare ARM-specific contrib directory.
  5. Change-Id: Id4cda552b39bfb39ab35ec499dbe122b43b6d1a1
  6. ---
  7. contrib/arm/inffast.c | 323 ++++++++++
  8. contrib/arm/inflate.c | 1561 +++++++++++++++++++++++++++++++++++++++++++++++++
  9. 2 files changed, 1884 insertions(+)
  10. create mode 100644 contrib/arm/inffast.c
  11. create mode 100644 contrib/arm/inflate.c
  12. --- /dev/null
  13. +++ b/contrib/arm/inffast.c
  14. @@ -0,0 +1,323 @@
  15. +/* inffast.c -- fast decoding
  16. + * Copyright (C) 1995-2017 Mark Adler
  17. + * For conditions of distribution and use, see copyright notice in zlib.h
  18. + */
  19. +
  20. +#include "zutil.h"
  21. +#include "inftrees.h"
  22. +#include "inflate.h"
  23. +#include "inffast.h"
  24. +
  25. +#ifdef ASMINF
  26. +# pragma message("Assembler code may have bugs -- use at your own risk")
  27. +#else
  28. +
  29. +/*
  30. + Decode literal, length, and distance codes and write out the resulting
  31. + literal and match bytes until either not enough input or output is
  32. + available, an end-of-block is encountered, or a data error is encountered.
  33. + When large enough input and output buffers are supplied to inflate(), for
  34. + example, a 16K input buffer and a 64K output buffer, more than 95% of the
  35. + inflate execution time is spent in this routine.
  36. +
  37. + Entry assumptions:
  38. +
  39. + state->mode == LEN
  40. + strm->avail_in >= 6
  41. + strm->avail_out >= 258
  42. + start >= strm->avail_out
  43. + state->bits < 8
  44. +
  45. + On return, state->mode is one of:
  46. +
  47. + LEN -- ran out of enough output space or enough available input
  48. + TYPE -- reached end of block code, inflate() to interpret next block
  49. + BAD -- error in block data
  50. +
  51. + Notes:
  52. +
  53. + - The maximum input bits used by a length/distance pair is 15 bits for the
  54. + length code, 5 bits for the length extra, 15 bits for the distance code,
  55. + and 13 bits for the distance extra. This totals 48 bits, or six bytes.
  56. + Therefore if strm->avail_in >= 6, then there is enough input to avoid
  57. + checking for available input while decoding.
  58. +
  59. + - The maximum bytes that a single length/distance pair can output is 258
  60. + bytes, which is the maximum length that can be coded. inflate_fast()
  61. + requires strm->avail_out >= 258 for each loop to avoid checking for
  62. + output space.
  63. + */
  64. +void ZLIB_INTERNAL inflate_fast(strm, start)
  65. +z_streamp strm;
  66. +unsigned start; /* inflate()'s starting value for strm->avail_out */
  67. +{
  68. + struct inflate_state FAR *state;
  69. + z_const unsigned char FAR *in; /* local strm->next_in */
  70. + z_const unsigned char FAR *last; /* have enough input while in < last */
  71. + unsigned char FAR *out; /* local strm->next_out */
  72. + unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
  73. + unsigned char FAR *end; /* while out < end, enough space available */
  74. +#ifdef INFLATE_STRICT
  75. + unsigned dmax; /* maximum distance from zlib header */
  76. +#endif
  77. + unsigned wsize; /* window size or zero if not using window */
  78. + unsigned whave; /* valid bytes in the window */
  79. + unsigned wnext; /* window write index */
  80. + unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
  81. + unsigned long hold; /* local strm->hold */
  82. + unsigned bits; /* local strm->bits */
  83. + code const FAR *lcode; /* local strm->lencode */
  84. + code const FAR *dcode; /* local strm->distcode */
  85. + unsigned lmask; /* mask for first level of length codes */
  86. + unsigned dmask; /* mask for first level of distance codes */
  87. + code here; /* retrieved table entry */
  88. + unsigned op; /* code bits, operation, extra bits, or */
  89. + /* window position, window bytes to copy */
  90. + unsigned len; /* match length, unused bytes */
  91. + unsigned dist; /* match distance */
  92. + unsigned char FAR *from; /* where to copy match from */
  93. +
  94. + /* copy state to local variables */
  95. + state = (struct inflate_state FAR *)strm->state;
  96. + in = strm->next_in;
  97. + last = in + (strm->avail_in - 5);
  98. + out = strm->next_out;
  99. + beg = out - (start - strm->avail_out);
  100. + end = out + (strm->avail_out - 257);
  101. +#ifdef INFLATE_STRICT
  102. + dmax = state->dmax;
  103. +#endif
  104. + wsize = state->wsize;
  105. + whave = state->whave;
  106. + wnext = state->wnext;
  107. + window = state->window;
  108. + hold = state->hold;
  109. + bits = state->bits;
  110. + lcode = state->lencode;
  111. + dcode = state->distcode;
  112. + lmask = (1U << state->lenbits) - 1;
  113. + dmask = (1U << state->distbits) - 1;
  114. +
  115. + /* decode literals and length/distances until end-of-block or not enough
  116. + input data or output space */
  117. + do {
  118. + if (bits < 15) {
  119. + hold += (unsigned long)(*in++) << bits;
  120. + bits += 8;
  121. + hold += (unsigned long)(*in++) << bits;
  122. + bits += 8;
  123. + }
  124. + here = lcode[hold & lmask];
  125. + dolen:
  126. + op = (unsigned)(here.bits);
  127. + hold >>= op;
  128. + bits -= op;
  129. + op = (unsigned)(here.op);
  130. + if (op == 0) { /* literal */
  131. + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  132. + "inflate: literal '%c'\n" :
  133. + "inflate: literal 0x%02x\n", here.val));
  134. + *out++ = (unsigned char)(here.val);
  135. + }
  136. + else if (op & 16) { /* length base */
  137. + len = (unsigned)(here.val);
  138. + op &= 15; /* number of extra bits */
  139. + if (op) {
  140. + if (bits < op) {
  141. + hold += (unsigned long)(*in++) << bits;
  142. + bits += 8;
  143. + }
  144. + len += (unsigned)hold & ((1U << op) - 1);
  145. + hold >>= op;
  146. + bits -= op;
  147. + }
  148. + Tracevv((stderr, "inflate: length %u\n", len));
  149. + if (bits < 15) {
  150. + hold += (unsigned long)(*in++) << bits;
  151. + bits += 8;
  152. + hold += (unsigned long)(*in++) << bits;
  153. + bits += 8;
  154. + }
  155. + here = dcode[hold & dmask];
  156. + dodist:
  157. + op = (unsigned)(here.bits);
  158. + hold >>= op;
  159. + bits -= op;
  160. + op = (unsigned)(here.op);
  161. + if (op & 16) { /* distance base */
  162. + dist = (unsigned)(here.val);
  163. + op &= 15; /* number of extra bits */
  164. + if (bits < op) {
  165. + hold += (unsigned long)(*in++) << bits;
  166. + bits += 8;
  167. + if (bits < op) {
  168. + hold += (unsigned long)(*in++) << bits;
  169. + bits += 8;
  170. + }
  171. + }
  172. + dist += (unsigned)hold & ((1U << op) - 1);
  173. +#ifdef INFLATE_STRICT
  174. + if (dist > dmax) {
  175. + strm->msg = (char *)"invalid distance too far back";
  176. + state->mode = BAD;
  177. + break;
  178. + }
  179. +#endif
  180. + hold >>= op;
  181. + bits -= op;
  182. + Tracevv((stderr, "inflate: distance %u\n", dist));
  183. + op = (unsigned)(out - beg); /* max distance in output */
  184. + if (dist > op) { /* see if copy from window */
  185. + op = dist - op; /* distance back in window */
  186. + if (op > whave) {
  187. + if (state->sane) {
  188. + strm->msg =
  189. + (char *)"invalid distance too far back";
  190. + state->mode = BAD;
  191. + break;
  192. + }
  193. +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  194. + if (len <= op - whave) {
  195. + do {
  196. + *out++ = 0;
  197. + } while (--len);
  198. + continue;
  199. + }
  200. + len -= op - whave;
  201. + do {
  202. + *out++ = 0;
  203. + } while (--op > whave);
  204. + if (op == 0) {
  205. + from = out - dist;
  206. + do {
  207. + *out++ = *from++;
  208. + } while (--len);
  209. + continue;
  210. + }
  211. +#endif
  212. + }
  213. + from = window;
  214. + if (wnext == 0) { /* very common case */
  215. + from += wsize - op;
  216. + if (op < len) { /* some from window */
  217. + len -= op;
  218. + do {
  219. + *out++ = *from++;
  220. + } while (--op);
  221. + from = out - dist; /* rest from output */
  222. + }
  223. + }
  224. + else if (wnext < op) { /* wrap around window */
  225. + from += wsize + wnext - op;
  226. + op -= wnext;
  227. + if (op < len) { /* some from end of window */
  228. + len -= op;
  229. + do {
  230. + *out++ = *from++;
  231. + } while (--op);
  232. + from = window;
  233. + if (wnext < len) { /* some from start of window */
  234. + op = wnext;
  235. + len -= op;
  236. + do {
  237. + *out++ = *from++;
  238. + } while (--op);
  239. + from = out - dist; /* rest from output */
  240. + }
  241. + }
  242. + }
  243. + else { /* contiguous in window */
  244. + from += wnext - op;
  245. + if (op < len) { /* some from window */
  246. + len -= op;
  247. + do {
  248. + *out++ = *from++;
  249. + } while (--op);
  250. + from = out - dist; /* rest from output */
  251. + }
  252. + }
  253. + while (len > 2) {
  254. + *out++ = *from++;
  255. + *out++ = *from++;
  256. + *out++ = *from++;
  257. + len -= 3;
  258. + }
  259. + if (len) {
  260. + *out++ = *from++;
  261. + if (len > 1)
  262. + *out++ = *from++;
  263. + }
  264. + }
  265. + else {
  266. + from = out - dist; /* copy direct from output */
  267. + do { /* minimum length is three */
  268. + *out++ = *from++;
  269. + *out++ = *from++;
  270. + *out++ = *from++;
  271. + len -= 3;
  272. + } while (len > 2);
  273. + if (len) {
  274. + *out++ = *from++;
  275. + if (len > 1)
  276. + *out++ = *from++;
  277. + }
  278. + }
  279. + }
  280. + else if ((op & 64) == 0) { /* 2nd level distance code */
  281. + here = dcode[here.val + (hold & ((1U << op) - 1))];
  282. + goto dodist;
  283. + }
  284. + else {
  285. + strm->msg = (char *)"invalid distance code";
  286. + state->mode = BAD;
  287. + break;
  288. + }
  289. + }
  290. + else if ((op & 64) == 0) { /* 2nd level length code */
  291. + here = lcode[here.val + (hold & ((1U << op) - 1))];
  292. + goto dolen;
  293. + }
  294. + else if (op & 32) { /* end-of-block */
  295. + Tracevv((stderr, "inflate: end of block\n"));
  296. + state->mode = TYPE;
  297. + break;
  298. + }
  299. + else {
  300. + strm->msg = (char *)"invalid literal/length code";
  301. + state->mode = BAD;
  302. + break;
  303. + }
  304. + } while (in < last && out < end);
  305. +
  306. + /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
  307. + len = bits >> 3;
  308. + in -= len;
  309. + bits -= len << 3;
  310. + hold &= (1U << bits) - 1;
  311. +
  312. + /* update state and return */
  313. + strm->next_in = in;
  314. + strm->next_out = out;
  315. + strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
  316. + strm->avail_out = (unsigned)(out < end ?
  317. + 257 + (end - out) : 257 - (out - end));
  318. + state->hold = hold;
  319. + state->bits = bits;
  320. + return;
  321. +}
  322. +
  323. +/*
  324. + inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
  325. + - Using bit fields for code structure
  326. + - Different op definition to avoid & for extra bits (do & for table bits)
  327. + - Three separate decoding do-loops for direct, window, and wnext == 0
  328. + - Special case for distance > 1 copies to do overlapped load and store copy
  329. + - Explicit branch predictions (based on measured branch probabilities)
  330. + - Deferring match copy and interspersed it with decoding subsequent codes
  331. + - Swapping literal/length else
  332. + - Swapping window/direct else
  333. + - Larger unrolled copy loops (three is about right)
  334. + - Moving len -= 3 statement into middle of loop
  335. + */
  336. +
  337. +#endif /* !ASMINF */
  338. --- /dev/null
  339. +++ b/contrib/arm/inflate.c
  340. @@ -0,0 +1,1561 @@
  341. +/* inflate.c -- zlib decompression
  342. + * Copyright (C) 1995-2016 Mark Adler
  343. + * For conditions of distribution and use, see copyright notice in zlib.h
  344. + */
  345. +
  346. +/*
  347. + * Change history:
  348. + *
  349. + * 1.2.beta0 24 Nov 2002
  350. + * - First version -- complete rewrite of inflate to simplify code, avoid
  351. + * creation of window when not needed, minimize use of window when it is
  352. + * needed, make inffast.c even faster, implement gzip decoding, and to
  353. + * improve code readability and style over the previous zlib inflate code
  354. + *
  355. + * 1.2.beta1 25 Nov 2002
  356. + * - Use pointers for available input and output checking in inffast.c
  357. + * - Remove input and output counters in inffast.c
  358. + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6
  359. + * - Remove unnecessary second byte pull from length extra in inffast.c
  360. + * - Unroll direct copy to three copies per loop in inffast.c
  361. + *
  362. + * 1.2.beta2 4 Dec 2002
  363. + * - Change external routine names to reduce potential conflicts
  364. + * - Correct filename to inffixed.h for fixed tables in inflate.c
  365. + * - Make hbuf[] unsigned char to match parameter type in inflate.c
  366. + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset)
  367. + * to avoid negation problem on Alphas (64 bit) in inflate.c
  368. + *
  369. + * 1.2.beta3 22 Dec 2002
  370. + * - Add comments on state->bits assertion in inffast.c
  371. + * - Add comments on op field in inftrees.h
  372. + * - Fix bug in reuse of allocated window after inflateReset()
  373. + * - Remove bit fields--back to byte structure for speed
  374. + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths
  375. + * - Change post-increments to pre-increments in inflate_fast(), PPC biased?
  376. + * - Add compile time option, POSTINC, to use post-increments instead (Intel?)
  377. + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used
  378. + * - Use local copies of stream next and avail values, as well as local bit
  379. + * buffer and bit count in inflate()--for speed when inflate_fast() not used
  380. + *
  381. + * 1.2.beta4 1 Jan 2003
  382. + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings
  383. + * - Move a comment on output buffer sizes from inffast.c to inflate.c
  384. + * - Add comments in inffast.c to introduce the inflate_fast() routine
  385. + * - Rearrange window copies in inflate_fast() for speed and simplification
  386. + * - Unroll last copy for window match in inflate_fast()
  387. + * - Use local copies of window variables in inflate_fast() for speed
  388. + * - Pull out common wnext == 0 case for speed in inflate_fast()
  389. + * - Make op and len in inflate_fast() unsigned for consistency
  390. + * - Add FAR to lcode and dcode declarations in inflate_fast()
  391. + * - Simplified bad distance check in inflate_fast()
  392. + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new
  393. + * source file infback.c to provide a call-back interface to inflate for
  394. + * programs like gzip and unzip -- uses window as output buffer to avoid
  395. + * window copying
  396. + *
  397. + * 1.2.beta5 1 Jan 2003
  398. + * - Improved inflateBack() interface to allow the caller to provide initial
  399. + * input in strm.
  400. + * - Fixed stored blocks bug in inflateBack()
  401. + *
  402. + * 1.2.beta6 4 Jan 2003
  403. + * - Added comments in inffast.c on effectiveness of POSTINC
  404. + * - Typecasting all around to reduce compiler warnings
  405. + * - Changed loops from while (1) or do {} while (1) to for (;;), again to
  406. + * make compilers happy
  407. + * - Changed type of window in inflateBackInit() to unsigned char *
  408. + *
  409. + * 1.2.beta7 27 Jan 2003
  410. + * - Changed many types to unsigned or unsigned short to avoid warnings
  411. + * - Added inflateCopy() function
  412. + *
  413. + * 1.2.0 9 Mar 2003
  414. + * - Changed inflateBack() interface to provide separate opaque descriptors
  415. + * for the in() and out() functions
  416. + * - Changed inflateBack() argument and in_func typedef to swap the length
  417. + * and buffer address return values for the input function
  418. + * - Check next_in and next_out for Z_NULL on entry to inflate()
  419. + *
  420. + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution.
  421. + */
  422. +
  423. +#include "zutil.h"
  424. +#include "inftrees.h"
  425. +#include "inflate.h"
  426. +#include "inffast.h"
  427. +
  428. +#ifdef MAKEFIXED
  429. +# ifndef BUILDFIXED
  430. +# define BUILDFIXED
  431. +# endif
  432. +#endif
  433. +
  434. +/* function prototypes */
  435. +local int inflateStateCheck OF((z_streamp strm));
  436. +local void fixedtables OF((struct inflate_state FAR *state));
  437. +local int updatewindow OF((z_streamp strm, const unsigned char FAR *end,
  438. + unsigned copy));
  439. +#ifdef BUILDFIXED
  440. + void makefixed OF((void));
  441. +#endif
  442. +local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf,
  443. + unsigned len));
  444. +
  445. +local int inflateStateCheck(strm)
  446. +z_streamp strm;
  447. +{
  448. + struct inflate_state FAR *state;
  449. + if (strm == Z_NULL ||
  450. + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0)
  451. + return 1;
  452. + state = (struct inflate_state FAR *)strm->state;
  453. + if (state == Z_NULL || state->strm != strm ||
  454. + state->mode < HEAD || state->mode > SYNC)
  455. + return 1;
  456. + return 0;
  457. +}
  458. +
  459. +int ZEXPORT inflateResetKeep(strm)
  460. +z_streamp strm;
  461. +{
  462. + struct inflate_state FAR *state;
  463. +
  464. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  465. + state = (struct inflate_state FAR *)strm->state;
  466. + strm->total_in = strm->total_out = state->total = 0;
  467. + strm->msg = Z_NULL;
  468. + if (state->wrap) /* to support ill-conceived Java test suite */
  469. + strm->adler = state->wrap & 1;
  470. + state->mode = HEAD;
  471. + state->last = 0;
  472. + state->havedict = 0;
  473. + state->dmax = 32768U;
  474. + state->head = Z_NULL;
  475. + state->hold = 0;
  476. + state->bits = 0;
  477. + state->lencode = state->distcode = state->next = state->codes;
  478. + state->sane = 1;
  479. + state->back = -1;
  480. + Tracev((stderr, "inflate: reset\n"));
  481. + return Z_OK;
  482. +}
  483. +
  484. +int ZEXPORT inflateReset(strm)
  485. +z_streamp strm;
  486. +{
  487. + struct inflate_state FAR *state;
  488. +
  489. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  490. + state = (struct inflate_state FAR *)strm->state;
  491. + state->wsize = 0;
  492. + state->whave = 0;
  493. + state->wnext = 0;
  494. + return inflateResetKeep(strm);
  495. +}
  496. +
  497. +int ZEXPORT inflateReset2(strm, windowBits)
  498. +z_streamp strm;
  499. +int windowBits;
  500. +{
  501. + int wrap;
  502. + struct inflate_state FAR *state;
  503. +
  504. + /* get the state */
  505. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  506. + state = (struct inflate_state FAR *)strm->state;
  507. +
  508. + /* extract wrap request from windowBits parameter */
  509. + if (windowBits < 0) {
  510. + wrap = 0;
  511. + windowBits = -windowBits;
  512. + }
  513. + else {
  514. + wrap = (windowBits >> 4) + 5;
  515. +#ifdef GUNZIP
  516. + if (windowBits < 48)
  517. + windowBits &= 15;
  518. +#endif
  519. + }
  520. +
  521. + /* set number of window bits, free window if different */
  522. + if (windowBits && (windowBits < 8 || windowBits > 15))
  523. + return Z_STREAM_ERROR;
  524. + if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) {
  525. + ZFREE(strm, state->window);
  526. + state->window = Z_NULL;
  527. + }
  528. +
  529. + /* update state and reset the rest of it */
  530. + state->wrap = wrap;
  531. + state->wbits = (unsigned)windowBits;
  532. + return inflateReset(strm);
  533. +}
  534. +
  535. +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size)
  536. +z_streamp strm;
  537. +int windowBits;
  538. +const char *version;
  539. +int stream_size;
  540. +{
  541. + int ret;
  542. + struct inflate_state FAR *state;
  543. +
  544. + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
  545. + stream_size != (int)(sizeof(z_stream)))
  546. + return Z_VERSION_ERROR;
  547. + if (strm == Z_NULL) return Z_STREAM_ERROR;
  548. + strm->msg = Z_NULL; /* in case we return an error */
  549. + if (strm->zalloc == (alloc_func)0) {
  550. +#ifdef Z_SOLO
  551. + return Z_STREAM_ERROR;
  552. +#else
  553. + strm->zalloc = zcalloc;
  554. + strm->opaque = (voidpf)0;
  555. +#endif
  556. + }
  557. + if (strm->zfree == (free_func)0)
  558. +#ifdef Z_SOLO
  559. + return Z_STREAM_ERROR;
  560. +#else
  561. + strm->zfree = zcfree;
  562. +#endif
  563. + state = (struct inflate_state FAR *)
  564. + ZALLOC(strm, 1, sizeof(struct inflate_state));
  565. + if (state == Z_NULL) return Z_MEM_ERROR;
  566. + Tracev((stderr, "inflate: allocated\n"));
  567. + strm->state = (struct internal_state FAR *)state;
  568. + state->strm = strm;
  569. + state->window = Z_NULL;
  570. + state->mode = HEAD; /* to pass state test in inflateReset2() */
  571. + ret = inflateReset2(strm, windowBits);
  572. + if (ret != Z_OK) {
  573. + ZFREE(strm, state);
  574. + strm->state = Z_NULL;
  575. + }
  576. + return ret;
  577. +}
  578. +
  579. +int ZEXPORT inflateInit_(strm, version, stream_size)
  580. +z_streamp strm;
  581. +const char *version;
  582. +int stream_size;
  583. +{
  584. + return inflateInit2_(strm, DEF_WBITS, version, stream_size);
  585. +}
  586. +
  587. +int ZEXPORT inflatePrime(strm, bits, value)
  588. +z_streamp strm;
  589. +int bits;
  590. +int value;
  591. +{
  592. + struct inflate_state FAR *state;
  593. +
  594. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  595. + state = (struct inflate_state FAR *)strm->state;
  596. + if (bits < 0) {
  597. + state->hold = 0;
  598. + state->bits = 0;
  599. + return Z_OK;
  600. + }
  601. + if (bits > 16 || state->bits + (uInt)bits > 32) return Z_STREAM_ERROR;
  602. + value &= (1L << bits) - 1;
  603. + state->hold += (unsigned)value << state->bits;
  604. + state->bits += (uInt)bits;
  605. + return Z_OK;
  606. +}
  607. +
  608. +/*
  609. + Return state with length and distance decoding tables and index sizes set to
  610. + fixed code decoding. Normally this returns fixed tables from inffixed.h.
  611. + If BUILDFIXED is defined, then instead this routine builds the tables the
  612. + first time it's called, and returns those tables the first time and
  613. + thereafter. This reduces the size of the code by about 2K bytes, in
  614. + exchange for a little execution time. However, BUILDFIXED should not be
  615. + used for threaded applications, since the rewriting of the tables and virgin
  616. + may not be thread-safe.
  617. + */
  618. +local void fixedtables(state)
  619. +struct inflate_state FAR *state;
  620. +{
  621. +#ifdef BUILDFIXED
  622. + static int virgin = 1;
  623. + static code *lenfix, *distfix;
  624. + static code fixed[544];
  625. +
  626. + /* build fixed huffman tables if first call (may not be thread safe) */
  627. + if (virgin) {
  628. + unsigned sym, bits;
  629. + static code *next;
  630. +
  631. + /* literal/length table */
  632. + sym = 0;
  633. + while (sym < 144) state->lens[sym++] = 8;
  634. + while (sym < 256) state->lens[sym++] = 9;
  635. + while (sym < 280) state->lens[sym++] = 7;
  636. + while (sym < 288) state->lens[sym++] = 8;
  637. + next = fixed;
  638. + lenfix = next;
  639. + bits = 9;
  640. + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work);
  641. +
  642. + /* distance table */
  643. + sym = 0;
  644. + while (sym < 32) state->lens[sym++] = 5;
  645. + distfix = next;
  646. + bits = 5;
  647. + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work);
  648. +
  649. + /* do this just once */
  650. + virgin = 0;
  651. + }
  652. +#else /* !BUILDFIXED */
  653. +# include "inffixed.h"
  654. +#endif /* BUILDFIXED */
  655. + state->lencode = lenfix;
  656. + state->lenbits = 9;
  657. + state->distcode = distfix;
  658. + state->distbits = 5;
  659. +}
  660. +
  661. +#ifdef MAKEFIXED
  662. +#include <stdio.h>
  663. +
  664. +/*
  665. + Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also
  666. + defines BUILDFIXED, so the tables are built on the fly. makefixed() writes
  667. + those tables to stdout, which would be piped to inffixed.h. A small program
  668. + can simply call makefixed to do this:
  669. +
  670. + void makefixed(void);
  671. +
  672. + int main(void)
  673. + {
  674. + makefixed();
  675. + return 0;
  676. + }
  677. +
  678. + Then that can be linked with zlib built with MAKEFIXED defined and run:
  679. +
  680. + a.out > inffixed.h
  681. + */
  682. +void makefixed()
  683. +{
  684. + unsigned low, size;
  685. + struct inflate_state state;
  686. +
  687. + fixedtables(&state);
  688. + puts(" /* inffixed.h -- table for decoding fixed codes");
  689. + puts(" * Generated automatically by makefixed().");
  690. + puts(" */");
  691. + puts("");
  692. + puts(" /* WARNING: this file should *not* be used by applications.");
  693. + puts(" It is part of the implementation of this library and is");
  694. + puts(" subject to change. Applications should only use zlib.h.");
  695. + puts(" */");
  696. + puts("");
  697. + size = 1U << 9;
  698. + printf(" static const code lenfix[%u] = {", size);
  699. + low = 0;
  700. + for (;;) {
  701. + if ((low % 7) == 0) printf("\n ");
  702. + printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op,
  703. + state.lencode[low].bits, state.lencode[low].val);
  704. + if (++low == size) break;
  705. + putchar(',');
  706. + }
  707. + puts("\n };");
  708. + size = 1U << 5;
  709. + printf("\n static const code distfix[%u] = {", size);
  710. + low = 0;
  711. + for (;;) {
  712. + if ((low % 6) == 0) printf("\n ");
  713. + printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits,
  714. + state.distcode[low].val);
  715. + if (++low == size) break;
  716. + putchar(',');
  717. + }
  718. + puts("\n };");
  719. +}
  720. +#endif /* MAKEFIXED */
  721. +
  722. +/*
  723. + Update the window with the last wsize (normally 32K) bytes written before
  724. + returning. If window does not exist yet, create it. This is only called
  725. + when a window is already in use, or when output has been written during this
  726. + inflate call, but the end of the deflate stream has not been reached yet.
  727. + It is also called to create a window for dictionary data when a dictionary
  728. + is loaded.
  729. +
  730. + Providing output buffers larger than 32K to inflate() should provide a speed
  731. + advantage, since only the last 32K of output is copied to the sliding window
  732. + upon return from inflate(), and since all distances after the first 32K of
  733. + output will fall in the output data, making match copies simpler and faster.
  734. + The advantage may be dependent on the size of the processor's data caches.
  735. + */
  736. +local int updatewindow(strm, end, copy)
  737. +z_streamp strm;
  738. +const Bytef *end;
  739. +unsigned copy;
  740. +{
  741. + struct inflate_state FAR *state;
  742. + unsigned dist;
  743. +
  744. + state = (struct inflate_state FAR *)strm->state;
  745. +
  746. + /* if it hasn't been done already, allocate space for the window */
  747. + if (state->window == Z_NULL) {
  748. + state->window = (unsigned char FAR *)
  749. + ZALLOC(strm, 1U << state->wbits,
  750. + sizeof(unsigned char));
  751. + if (state->window == Z_NULL) return 1;
  752. + }
  753. +
  754. + /* if window not in use yet, initialize */
  755. + if (state->wsize == 0) {
  756. + state->wsize = 1U << state->wbits;
  757. + state->wnext = 0;
  758. + state->whave = 0;
  759. + }
  760. +
  761. + /* copy state->wsize or less output bytes into the circular window */
  762. + if (copy >= state->wsize) {
  763. + zmemcpy(state->window, end - state->wsize, state->wsize);
  764. + state->wnext = 0;
  765. + state->whave = state->wsize;
  766. + }
  767. + else {
  768. + dist = state->wsize - state->wnext;
  769. + if (dist > copy) dist = copy;
  770. + zmemcpy(state->window + state->wnext, end - copy, dist);
  771. + copy -= dist;
  772. + if (copy) {
  773. + zmemcpy(state->window, end - copy, copy);
  774. + state->wnext = copy;
  775. + state->whave = state->wsize;
  776. + }
  777. + else {
  778. + state->wnext += dist;
  779. + if (state->wnext == state->wsize) state->wnext = 0;
  780. + if (state->whave < state->wsize) state->whave += dist;
  781. + }
  782. + }
  783. + return 0;
  784. +}
  785. +
  786. +/* Macros for inflate(): */
  787. +
  788. +/* check function to use adler32() for zlib or crc32() for gzip */
  789. +#ifdef GUNZIP
  790. +# define UPDATE(check, buf, len) \
  791. + (state->flags ? crc32(check, buf, len) : adler32(check, buf, len))
  792. +#else
  793. +# define UPDATE(check, buf, len) adler32(check, buf, len)
  794. +#endif
  795. +
  796. +/* check macros for header crc */
  797. +#ifdef GUNZIP
  798. +# define CRC2(check, word) \
  799. + do { \
  800. + hbuf[0] = (unsigned char)(word); \
  801. + hbuf[1] = (unsigned char)((word) >> 8); \
  802. + check = crc32(check, hbuf, 2); \
  803. + } while (0)
  804. +
  805. +# define CRC4(check, word) \
  806. + do { \
  807. + hbuf[0] = (unsigned char)(word); \
  808. + hbuf[1] = (unsigned char)((word) >> 8); \
  809. + hbuf[2] = (unsigned char)((word) >> 16); \
  810. + hbuf[3] = (unsigned char)((word) >> 24); \
  811. + check = crc32(check, hbuf, 4); \
  812. + } while (0)
  813. +#endif
  814. +
  815. +/* Load registers with state in inflate() for speed */
  816. +#define LOAD() \
  817. + do { \
  818. + put = strm->next_out; \
  819. + left = strm->avail_out; \
  820. + next = strm->next_in; \
  821. + have = strm->avail_in; \
  822. + hold = state->hold; \
  823. + bits = state->bits; \
  824. + } while (0)
  825. +
  826. +/* Restore state from registers in inflate() */
  827. +#define RESTORE() \
  828. + do { \
  829. + strm->next_out = put; \
  830. + strm->avail_out = left; \
  831. + strm->next_in = next; \
  832. + strm->avail_in = have; \
  833. + state->hold = hold; \
  834. + state->bits = bits; \
  835. + } while (0)
  836. +
  837. +/* Clear the input bit accumulator */
  838. +#define INITBITS() \
  839. + do { \
  840. + hold = 0; \
  841. + bits = 0; \
  842. + } while (0)
  843. +
  844. +/* Get a byte of input into the bit accumulator, or return from inflate()
  845. + if there is no input available. */
  846. +#define PULLBYTE() \
  847. + do { \
  848. + if (have == 0) goto inf_leave; \
  849. + have--; \
  850. + hold += (unsigned long)(*next++) << bits; \
  851. + bits += 8; \
  852. + } while (0)
  853. +
  854. +/* Assure that there are at least n bits in the bit accumulator. If there is
  855. + not enough available input to do that, then return from inflate(). */
  856. +#define NEEDBITS(n) \
  857. + do { \
  858. + while (bits < (unsigned)(n)) \
  859. + PULLBYTE(); \
  860. + } while (0)
  861. +
  862. +/* Return the low n bits of the bit accumulator (n < 16) */
  863. +#define BITS(n) \
  864. + ((unsigned)hold & ((1U << (n)) - 1))
  865. +
  866. +/* Remove n bits from the bit accumulator */
  867. +#define DROPBITS(n) \
  868. + do { \
  869. + hold >>= (n); \
  870. + bits -= (unsigned)(n); \
  871. + } while (0)
  872. +
  873. +/* Remove zero to seven bits as needed to go to a byte boundary */
  874. +#define BYTEBITS() \
  875. + do { \
  876. + hold >>= bits & 7; \
  877. + bits -= bits & 7; \
  878. + } while (0)
  879. +
  880. +/*
  881. + inflate() uses a state machine to process as much input data and generate as
  882. + much output data as possible before returning. The state machine is
  883. + structured roughly as follows:
  884. +
  885. + for (;;) switch (state) {
  886. + ...
  887. + case STATEn:
  888. + if (not enough input data or output space to make progress)
  889. + return;
  890. + ... make progress ...
  891. + state = STATEm;
  892. + break;
  893. + ...
  894. + }
  895. +
  896. + so when inflate() is called again, the same case is attempted again, and
  897. + if the appropriate resources are provided, the machine proceeds to the
  898. + next state. The NEEDBITS() macro is usually the way the state evaluates
  899. + whether it can proceed or should return. NEEDBITS() does the return if
  900. + the requested bits are not available. The typical use of the BITS macros
  901. + is:
  902. +
  903. + NEEDBITS(n);
  904. + ... do something with BITS(n) ...
  905. + DROPBITS(n);
  906. +
  907. + where NEEDBITS(n) either returns from inflate() if there isn't enough
  908. + input left to load n bits into the accumulator, or it continues. BITS(n)
  909. + gives the low n bits in the accumulator. When done, DROPBITS(n) drops
  910. + the low n bits off the accumulator. INITBITS() clears the accumulator
  911. + and sets the number of available bits to zero. BYTEBITS() discards just
  912. + enough bits to put the accumulator on a byte boundary. After BYTEBITS()
  913. + and a NEEDBITS(8), then BITS(8) would return the next byte in the stream.
  914. +
  915. + NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return
  916. + if there is no input available. The decoding of variable length codes uses
  917. + PULLBYTE() directly in order to pull just enough bytes to decode the next
  918. + code, and no more.
  919. +
  920. + Some states loop until they get enough input, making sure that enough
  921. + state information is maintained to continue the loop where it left off
  922. + if NEEDBITS() returns in the loop. For example, want, need, and keep
  923. + would all have to actually be part of the saved state in case NEEDBITS()
  924. + returns:
  925. +
  926. + case STATEw:
  927. + while (want < need) {
  928. + NEEDBITS(n);
  929. + keep[want++] = BITS(n);
  930. + DROPBITS(n);
  931. + }
  932. + state = STATEx;
  933. + case STATEx:
  934. +
  935. + As shown above, if the next state is also the next case, then the break
  936. + is omitted.
  937. +
  938. + A state may also return if there is not enough output space available to
  939. + complete that state. Those states are copying stored data, writing a
  940. + literal byte, and copying a matching string.
  941. +
  942. + When returning, a "goto inf_leave" is used to update the total counters,
  943. + update the check value, and determine whether any progress has been made
  944. + during that inflate() call in order to return the proper return code.
  945. + Progress is defined as a change in either strm->avail_in or strm->avail_out.
  946. + When there is a window, goto inf_leave will update the window with the last
  947. + output written. If a goto inf_leave occurs in the middle of decompression
  948. + and there is no window currently, goto inf_leave will create one and copy
  949. + output to the window for the next call of inflate().
  950. +
  951. + In this implementation, the flush parameter of inflate() only affects the
  952. + return code (per zlib.h). inflate() always writes as much as possible to
  953. + strm->next_out, given the space available and the provided input--the effect
  954. + documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers
  955. + the allocation of and copying into a sliding window until necessary, which
  956. + provides the effect documented in zlib.h for Z_FINISH when the entire input
  957. + stream available. So the only thing the flush parameter actually does is:
  958. + when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it
  959. + will return Z_BUF_ERROR if it has not reached the end of the stream.
  960. + */
  961. +
  962. +int ZEXPORT inflate(strm, flush)
  963. +z_streamp strm;
  964. +int flush;
  965. +{
  966. + struct inflate_state FAR *state;
  967. + z_const unsigned char FAR *next; /* next input */
  968. + unsigned char FAR *put; /* next output */
  969. + unsigned have, left; /* available input and output */
  970. + unsigned long hold; /* bit buffer */
  971. + unsigned bits; /* bits in bit buffer */
  972. + unsigned in, out; /* save starting available input and output */
  973. + unsigned copy; /* number of stored or match bytes to copy */
  974. + unsigned char FAR *from; /* where to copy match bytes from */
  975. + code here; /* current decoding table entry */
  976. + code last; /* parent table entry */
  977. + unsigned len; /* length to copy for repeats, bits to drop */
  978. + int ret; /* return code */
  979. +#ifdef GUNZIP
  980. + unsigned char hbuf[4]; /* buffer for gzip header crc calculation */
  981. +#endif
  982. + static const unsigned short order[19] = /* permutation of code lengths */
  983. + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  984. +
  985. + if (inflateStateCheck(strm) || strm->next_out == Z_NULL ||
  986. + (strm->next_in == Z_NULL && strm->avail_in != 0))
  987. + return Z_STREAM_ERROR;
  988. +
  989. + state = (struct inflate_state FAR *)strm->state;
  990. + if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */
  991. + LOAD();
  992. + in = have;
  993. + out = left;
  994. + ret = Z_OK;
  995. + for (;;)
  996. + switch (state->mode) {
  997. + case HEAD:
  998. + if (state->wrap == 0) {
  999. + state->mode = TYPEDO;
  1000. + break;
  1001. + }
  1002. + NEEDBITS(16);
  1003. +#ifdef GUNZIP
  1004. + if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */
  1005. + if (state->wbits == 0)
  1006. + state->wbits = 15;
  1007. + state->check = crc32(0L, Z_NULL, 0);
  1008. + CRC2(state->check, hold);
  1009. + INITBITS();
  1010. + state->mode = FLAGS;
  1011. + break;
  1012. + }
  1013. + state->flags = 0; /* expect zlib header */
  1014. + if (state->head != Z_NULL)
  1015. + state->head->done = -1;
  1016. + if (!(state->wrap & 1) || /* check if zlib header allowed */
  1017. +#else
  1018. + if (
  1019. +#endif
  1020. + ((BITS(8) << 8) + (hold >> 8)) % 31) {
  1021. + strm->msg = (char *)"incorrect header check";
  1022. + state->mode = BAD;
  1023. + break;
  1024. + }
  1025. + if (BITS(4) != Z_DEFLATED) {
  1026. + strm->msg = (char *)"unknown compression method";
  1027. + state->mode = BAD;
  1028. + break;
  1029. + }
  1030. + DROPBITS(4);
  1031. + len = BITS(4) + 8;
  1032. + if (state->wbits == 0)
  1033. + state->wbits = len;
  1034. + if (len > 15 || len > state->wbits) {
  1035. + strm->msg = (char *)"invalid window size";
  1036. + state->mode = BAD;
  1037. + break;
  1038. + }
  1039. + state->dmax = 1U << len;
  1040. + Tracev((stderr, "inflate: zlib header ok\n"));
  1041. + strm->adler = state->check = adler32(0L, Z_NULL, 0);
  1042. + state->mode = hold & 0x200 ? DICTID : TYPE;
  1043. + INITBITS();
  1044. + break;
  1045. +#ifdef GUNZIP
  1046. + case FLAGS:
  1047. + NEEDBITS(16);
  1048. + state->flags = (int)(hold);
  1049. + if ((state->flags & 0xff) != Z_DEFLATED) {
  1050. + strm->msg = (char *)"unknown compression method";
  1051. + state->mode = BAD;
  1052. + break;
  1053. + }
  1054. + if (state->flags & 0xe000) {
  1055. + strm->msg = (char *)"unknown header flags set";
  1056. + state->mode = BAD;
  1057. + break;
  1058. + }
  1059. + if (state->head != Z_NULL)
  1060. + state->head->text = (int)((hold >> 8) & 1);
  1061. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1062. + CRC2(state->check, hold);
  1063. + INITBITS();
  1064. + state->mode = TIME;
  1065. + case TIME:
  1066. + NEEDBITS(32);
  1067. + if (state->head != Z_NULL)
  1068. + state->head->time = hold;
  1069. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1070. + CRC4(state->check, hold);
  1071. + INITBITS();
  1072. + state->mode = OS;
  1073. + case OS:
  1074. + NEEDBITS(16);
  1075. + if (state->head != Z_NULL) {
  1076. + state->head->xflags = (int)(hold & 0xff);
  1077. + state->head->os = (int)(hold >> 8);
  1078. + }
  1079. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1080. + CRC2(state->check, hold);
  1081. + INITBITS();
  1082. + state->mode = EXLEN;
  1083. + case EXLEN:
  1084. + if (state->flags & 0x0400) {
  1085. + NEEDBITS(16);
  1086. + state->length = (unsigned)(hold);
  1087. + if (state->head != Z_NULL)
  1088. + state->head->extra_len = (unsigned)hold;
  1089. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1090. + CRC2(state->check, hold);
  1091. + INITBITS();
  1092. + }
  1093. + else if (state->head != Z_NULL)
  1094. + state->head->extra = Z_NULL;
  1095. + state->mode = EXTRA;
  1096. + case EXTRA:
  1097. + if (state->flags & 0x0400) {
  1098. + copy = state->length;
  1099. + if (copy > have) copy = have;
  1100. + if (copy) {
  1101. + if (state->head != Z_NULL &&
  1102. + state->head->extra != Z_NULL) {
  1103. + len = state->head->extra_len - state->length;
  1104. + zmemcpy(state->head->extra + len, next,
  1105. + len + copy > state->head->extra_max ?
  1106. + state->head->extra_max - len : copy);
  1107. + }
  1108. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1109. + state->check = crc32(state->check, next, copy);
  1110. + have -= copy;
  1111. + next += copy;
  1112. + state->length -= copy;
  1113. + }
  1114. + if (state->length) goto inf_leave;
  1115. + }
  1116. + state->length = 0;
  1117. + state->mode = NAME;
  1118. + case NAME:
  1119. + if (state->flags & 0x0800) {
  1120. + if (have == 0) goto inf_leave;
  1121. + copy = 0;
  1122. + do {
  1123. + len = (unsigned)(next[copy++]);
  1124. + if (state->head != Z_NULL &&
  1125. + state->head->name != Z_NULL &&
  1126. + state->length < state->head->name_max)
  1127. + state->head->name[state->length++] = (Bytef)len;
  1128. + } while (len && copy < have);
  1129. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1130. + state->check = crc32(state->check, next, copy);
  1131. + have -= copy;
  1132. + next += copy;
  1133. + if (len) goto inf_leave;
  1134. + }
  1135. + else if (state->head != Z_NULL)
  1136. + state->head->name = Z_NULL;
  1137. + state->length = 0;
  1138. + state->mode = COMMENT;
  1139. + case COMMENT:
  1140. + if (state->flags & 0x1000) {
  1141. + if (have == 0) goto inf_leave;
  1142. + copy = 0;
  1143. + do {
  1144. + len = (unsigned)(next[copy++]);
  1145. + if (state->head != Z_NULL &&
  1146. + state->head->comment != Z_NULL &&
  1147. + state->length < state->head->comm_max)
  1148. + state->head->comment[state->length++] = (Bytef)len;
  1149. + } while (len && copy < have);
  1150. + if ((state->flags & 0x0200) && (state->wrap & 4))
  1151. + state->check = crc32(state->check, next, copy);
  1152. + have -= copy;
  1153. + next += copy;
  1154. + if (len) goto inf_leave;
  1155. + }
  1156. + else if (state->head != Z_NULL)
  1157. + state->head->comment = Z_NULL;
  1158. + state->mode = HCRC;
  1159. + case HCRC:
  1160. + if (state->flags & 0x0200) {
  1161. + NEEDBITS(16);
  1162. + if ((state->wrap & 4) && hold != (state->check & 0xffff)) {
  1163. + strm->msg = (char *)"header crc mismatch";
  1164. + state->mode = BAD;
  1165. + break;
  1166. + }
  1167. + INITBITS();
  1168. + }
  1169. + if (state->head != Z_NULL) {
  1170. + state->head->hcrc = (int)((state->flags >> 9) & 1);
  1171. + state->head->done = 1;
  1172. + }
  1173. + strm->adler = state->check = crc32(0L, Z_NULL, 0);
  1174. + state->mode = TYPE;
  1175. + break;
  1176. +#endif
  1177. + case DICTID:
  1178. + NEEDBITS(32);
  1179. + strm->adler = state->check = ZSWAP32(hold);
  1180. + INITBITS();
  1181. + state->mode = DICT;
  1182. + case DICT:
  1183. + if (state->havedict == 0) {
  1184. + RESTORE();
  1185. + return Z_NEED_DICT;
  1186. + }
  1187. + strm->adler = state->check = adler32(0L, Z_NULL, 0);
  1188. + state->mode = TYPE;
  1189. + case TYPE:
  1190. + if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave;
  1191. + case TYPEDO:
  1192. + if (state->last) {
  1193. + BYTEBITS();
  1194. + state->mode = CHECK;
  1195. + break;
  1196. + }
  1197. + NEEDBITS(3);
  1198. + state->last = BITS(1);
  1199. + DROPBITS(1);
  1200. + switch (BITS(2)) {
  1201. + case 0: /* stored block */
  1202. + Tracev((stderr, "inflate: stored block%s\n",
  1203. + state->last ? " (last)" : ""));
  1204. + state->mode = STORED;
  1205. + break;
  1206. + case 1: /* fixed block */
  1207. + fixedtables(state);
  1208. + Tracev((stderr, "inflate: fixed codes block%s\n",
  1209. + state->last ? " (last)" : ""));
  1210. + state->mode = LEN_; /* decode codes */
  1211. + if (flush == Z_TREES) {
  1212. + DROPBITS(2);
  1213. + goto inf_leave;
  1214. + }
  1215. + break;
  1216. + case 2: /* dynamic block */
  1217. + Tracev((stderr, "inflate: dynamic codes block%s\n",
  1218. + state->last ? " (last)" : ""));
  1219. + state->mode = TABLE;
  1220. + break;
  1221. + case 3:
  1222. + strm->msg = (char *)"invalid block type";
  1223. + state->mode = BAD;
  1224. + }
  1225. + DROPBITS(2);
  1226. + break;
  1227. + case STORED:
  1228. + BYTEBITS(); /* go to byte boundary */
  1229. + NEEDBITS(32);
  1230. + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
  1231. + strm->msg = (char *)"invalid stored block lengths";
  1232. + state->mode = BAD;
  1233. + break;
  1234. + }
  1235. + state->length = (unsigned)hold & 0xffff;
  1236. + Tracev((stderr, "inflate: stored length %u\n",
  1237. + state->length));
  1238. + INITBITS();
  1239. + state->mode = COPY_;
  1240. + if (flush == Z_TREES) goto inf_leave;
  1241. + case COPY_:
  1242. + state->mode = COPY;
  1243. + case COPY:
  1244. + copy = state->length;
  1245. + if (copy) {
  1246. + if (copy > have) copy = have;
  1247. + if (copy > left) copy = left;
  1248. + if (copy == 0) goto inf_leave;
  1249. + zmemcpy(put, next, copy);
  1250. + have -= copy;
  1251. + next += copy;
  1252. + left -= copy;
  1253. + put += copy;
  1254. + state->length -= copy;
  1255. + break;
  1256. + }
  1257. + Tracev((stderr, "inflate: stored end\n"));
  1258. + state->mode = TYPE;
  1259. + break;
  1260. + case TABLE:
  1261. + NEEDBITS(14);
  1262. + state->nlen = BITS(5) + 257;
  1263. + DROPBITS(5);
  1264. + state->ndist = BITS(5) + 1;
  1265. + DROPBITS(5);
  1266. + state->ncode = BITS(4) + 4;
  1267. + DROPBITS(4);
  1268. +#ifndef PKZIP_BUG_WORKAROUND
  1269. + if (state->nlen > 286 || state->ndist > 30) {
  1270. + strm->msg = (char *)"too many length or distance symbols";
  1271. + state->mode = BAD;
  1272. + break;
  1273. + }
  1274. +#endif
  1275. + Tracev((stderr, "inflate: table sizes ok\n"));
  1276. + state->have = 0;
  1277. + state->mode = LENLENS;
  1278. + case LENLENS:
  1279. + while (state->have < state->ncode) {
  1280. + NEEDBITS(3);
  1281. + state->lens[order[state->have++]] = (unsigned short)BITS(3);
  1282. + DROPBITS(3);
  1283. + }
  1284. + while (state->have < 19)
  1285. + state->lens[order[state->have++]] = 0;
  1286. + state->next = state->codes;
  1287. + state->lencode = (const code FAR *)(state->next);
  1288. + state->lenbits = 7;
  1289. + ret = inflate_table(CODES, state->lens, 19, &(state->next),
  1290. + &(state->lenbits), state->work);
  1291. + if (ret) {
  1292. + strm->msg = (char *)"invalid code lengths set";
  1293. + state->mode = BAD;
  1294. + break;
  1295. + }
  1296. + Tracev((stderr, "inflate: code lengths ok\n"));
  1297. + state->have = 0;
  1298. + state->mode = CODELENS;
  1299. + case CODELENS:
  1300. + while (state->have < state->nlen + state->ndist) {
  1301. + for (;;) {
  1302. + here = state->lencode[BITS(state->lenbits)];
  1303. + if ((unsigned)(here.bits) <= bits) break;
  1304. + PULLBYTE();
  1305. + }
  1306. + if (here.val < 16) {
  1307. + DROPBITS(here.bits);
  1308. + state->lens[state->have++] = here.val;
  1309. + }
  1310. + else {
  1311. + if (here.val == 16) {
  1312. + NEEDBITS(here.bits + 2);
  1313. + DROPBITS(here.bits);
  1314. + if (state->have == 0) {
  1315. + strm->msg = (char *)"invalid bit length repeat";
  1316. + state->mode = BAD;
  1317. + break;
  1318. + }
  1319. + len = state->lens[state->have - 1];
  1320. + copy = 3 + BITS(2);
  1321. + DROPBITS(2);
  1322. + }
  1323. + else if (here.val == 17) {
  1324. + NEEDBITS(here.bits + 3);
  1325. + DROPBITS(here.bits);
  1326. + len = 0;
  1327. + copy = 3 + BITS(3);
  1328. + DROPBITS(3);
  1329. + }
  1330. + else {
  1331. + NEEDBITS(here.bits + 7);
  1332. + DROPBITS(here.bits);
  1333. + len = 0;
  1334. + copy = 11 + BITS(7);
  1335. + DROPBITS(7);
  1336. + }
  1337. + if (state->have + copy > state->nlen + state->ndist) {
  1338. + strm->msg = (char *)"invalid bit length repeat";
  1339. + state->mode = BAD;
  1340. + break;
  1341. + }
  1342. + while (copy--)
  1343. + state->lens[state->have++] = (unsigned short)len;
  1344. + }
  1345. + }
  1346. +
  1347. + /* handle error breaks in while */
  1348. + if (state->mode == BAD) break;
  1349. +
  1350. + /* check for end-of-block code (better have one) */
  1351. + if (state->lens[256] == 0) {
  1352. + strm->msg = (char *)"invalid code -- missing end-of-block";
  1353. + state->mode = BAD;
  1354. + break;
  1355. + }
  1356. +
  1357. + /* build code tables -- note: do not change the lenbits or distbits
  1358. + values here (9 and 6) without reading the comments in inftrees.h
  1359. + concerning the ENOUGH constants, which depend on those values */
  1360. + state->next = state->codes;
  1361. + state->lencode = (const code FAR *)(state->next);
  1362. + state->lenbits = 9;
  1363. + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next),
  1364. + &(state->lenbits), state->work);
  1365. + if (ret) {
  1366. + strm->msg = (char *)"invalid literal/lengths set";
  1367. + state->mode = BAD;
  1368. + break;
  1369. + }
  1370. + state->distcode = (const code FAR *)(state->next);
  1371. + state->distbits = 6;
  1372. + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist,
  1373. + &(state->next), &(state->distbits), state->work);
  1374. + if (ret) {
  1375. + strm->msg = (char *)"invalid distances set";
  1376. + state->mode = BAD;
  1377. + break;
  1378. + }
  1379. + Tracev((stderr, "inflate: codes ok\n"));
  1380. + state->mode = LEN_;
  1381. + if (flush == Z_TREES) goto inf_leave;
  1382. + case LEN_:
  1383. + state->mode = LEN;
  1384. + case LEN:
  1385. + if (have >= 6 && left >= 258) {
  1386. + RESTORE();
  1387. + inflate_fast(strm, out);
  1388. + LOAD();
  1389. + if (state->mode == TYPE)
  1390. + state->back = -1;
  1391. + break;
  1392. + }
  1393. + state->back = 0;
  1394. + for (;;) {
  1395. + here = state->lencode[BITS(state->lenbits)];
  1396. + if ((unsigned)(here.bits) <= bits) break;
  1397. + PULLBYTE();
  1398. + }
  1399. + if (here.op && (here.op & 0xf0) == 0) {
  1400. + last = here;
  1401. + for (;;) {
  1402. + here = state->lencode[last.val +
  1403. + (BITS(last.bits + last.op) >> last.bits)];
  1404. + if ((unsigned)(last.bits + here.bits) <= bits) break;
  1405. + PULLBYTE();
  1406. + }
  1407. + DROPBITS(last.bits);
  1408. + state->back += last.bits;
  1409. + }
  1410. + DROPBITS(here.bits);
  1411. + state->back += here.bits;
  1412. + state->length = (unsigned)here.val;
  1413. + if ((int)(here.op) == 0) {
  1414. + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
  1415. + "inflate: literal '%c'\n" :
  1416. + "inflate: literal 0x%02x\n", here.val));
  1417. + state->mode = LIT;
  1418. + break;
  1419. + }
  1420. + if (here.op & 32) {
  1421. + Tracevv((stderr, "inflate: end of block\n"));
  1422. + state->back = -1;
  1423. + state->mode = TYPE;
  1424. + break;
  1425. + }
  1426. + if (here.op & 64) {
  1427. + strm->msg = (char *)"invalid literal/length code";
  1428. + state->mode = BAD;
  1429. + break;
  1430. + }
  1431. + state->extra = (unsigned)(here.op) & 15;
  1432. + state->mode = LENEXT;
  1433. + case LENEXT:
  1434. + if (state->extra) {
  1435. + NEEDBITS(state->extra);
  1436. + state->length += BITS(state->extra);
  1437. + DROPBITS(state->extra);
  1438. + state->back += state->extra;
  1439. + }
  1440. + Tracevv((stderr, "inflate: length %u\n", state->length));
  1441. + state->was = state->length;
  1442. + state->mode = DIST;
  1443. + case DIST:
  1444. + for (;;) {
  1445. + here = state->distcode[BITS(state->distbits)];
  1446. + if ((unsigned)(here.bits) <= bits) break;
  1447. + PULLBYTE();
  1448. + }
  1449. + if ((here.op & 0xf0) == 0) {
  1450. + last = here;
  1451. + for (;;) {
  1452. + here = state->distcode[last.val +
  1453. + (BITS(last.bits + last.op) >> last.bits)];
  1454. + if ((unsigned)(last.bits + here.bits) <= bits) break;
  1455. + PULLBYTE();
  1456. + }
  1457. + DROPBITS(last.bits);
  1458. + state->back += last.bits;
  1459. + }
  1460. + DROPBITS(here.bits);
  1461. + state->back += here.bits;
  1462. + if (here.op & 64) {
  1463. + strm->msg = (char *)"invalid distance code";
  1464. + state->mode = BAD;
  1465. + break;
  1466. + }
  1467. + state->offset = (unsigned)here.val;
  1468. + state->extra = (unsigned)(here.op) & 15;
  1469. + state->mode = DISTEXT;
  1470. + case DISTEXT:
  1471. + if (state->extra) {
  1472. + NEEDBITS(state->extra);
  1473. + state->offset += BITS(state->extra);
  1474. + DROPBITS(state->extra);
  1475. + state->back += state->extra;
  1476. + }
  1477. +#ifdef INFLATE_STRICT
  1478. + if (state->offset > state->dmax) {
  1479. + strm->msg = (char *)"invalid distance too far back";
  1480. + state->mode = BAD;
  1481. + break;
  1482. + }
  1483. +#endif
  1484. + Tracevv((stderr, "inflate: distance %u\n", state->offset));
  1485. + state->mode = MATCH;
  1486. + case MATCH:
  1487. + if (left == 0) goto inf_leave;
  1488. + copy = out - left;
  1489. + if (state->offset > copy) { /* copy from window */
  1490. + copy = state->offset - copy;
  1491. + if (copy > state->whave) {
  1492. + if (state->sane) {
  1493. + strm->msg = (char *)"invalid distance too far back";
  1494. + state->mode = BAD;
  1495. + break;
  1496. + }
  1497. +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1498. + Trace((stderr, "inflate.c too far\n"));
  1499. + copy -= state->whave;
  1500. + if (copy > state->length) copy = state->length;
  1501. + if (copy > left) copy = left;
  1502. + left -= copy;
  1503. + state->length -= copy;
  1504. + do {
  1505. + *put++ = 0;
  1506. + } while (--copy);
  1507. + if (state->length == 0) state->mode = LEN;
  1508. + break;
  1509. +#endif
  1510. + }
  1511. + if (copy > state->wnext) {
  1512. + copy -= state->wnext;
  1513. + from = state->window + (state->wsize - copy);
  1514. + }
  1515. + else
  1516. + from = state->window + (state->wnext - copy);
  1517. + if (copy > state->length) copy = state->length;
  1518. + }
  1519. + else { /* copy from output */
  1520. + from = put - state->offset;
  1521. + copy = state->length;
  1522. + }
  1523. + if (copy > left) copy = left;
  1524. + left -= copy;
  1525. + state->length -= copy;
  1526. + do {
  1527. + *put++ = *from++;
  1528. + } while (--copy);
  1529. + if (state->length == 0) state->mode = LEN;
  1530. + break;
  1531. + case LIT:
  1532. + if (left == 0) goto inf_leave;
  1533. + *put++ = (unsigned char)(state->length);
  1534. + left--;
  1535. + state->mode = LEN;
  1536. + break;
  1537. + case CHECK:
  1538. + if (state->wrap) {
  1539. + NEEDBITS(32);
  1540. + out -= left;
  1541. + strm->total_out += out;
  1542. + state->total += out;
  1543. + if ((state->wrap & 4) && out)
  1544. + strm->adler = state->check =
  1545. + UPDATE(state->check, put - out, out);
  1546. + out = left;
  1547. + if ((state->wrap & 4) && (
  1548. +#ifdef GUNZIP
  1549. + state->flags ? hold :
  1550. +#endif
  1551. + ZSWAP32(hold)) != state->check) {
  1552. + strm->msg = (char *)"incorrect data check";
  1553. + state->mode = BAD;
  1554. + break;
  1555. + }
  1556. + INITBITS();
  1557. + Tracev((stderr, "inflate: check matches trailer\n"));
  1558. + }
  1559. +#ifdef GUNZIP
  1560. + state->mode = LENGTH;
  1561. + case LENGTH:
  1562. + if (state->wrap && state->flags) {
  1563. + NEEDBITS(32);
  1564. + if (hold != (state->total & 0xffffffffUL)) {
  1565. + strm->msg = (char *)"incorrect length check";
  1566. + state->mode = BAD;
  1567. + break;
  1568. + }
  1569. + INITBITS();
  1570. + Tracev((stderr, "inflate: length matches trailer\n"));
  1571. + }
  1572. +#endif
  1573. + state->mode = DONE;
  1574. + case DONE:
  1575. + ret = Z_STREAM_END;
  1576. + goto inf_leave;
  1577. + case BAD:
  1578. + ret = Z_DATA_ERROR;
  1579. + goto inf_leave;
  1580. + case MEM:
  1581. + return Z_MEM_ERROR;
  1582. + case SYNC:
  1583. + default:
  1584. + return Z_STREAM_ERROR;
  1585. + }
  1586. +
  1587. + /*
  1588. + Return from inflate(), updating the total counts and the check value.
  1589. + If there was no progress during the inflate() call, return a buffer
  1590. + error. Call updatewindow() to create and/or update the window state.
  1591. + Note: a memory error from inflate() is non-recoverable.
  1592. + */
  1593. + inf_leave:
  1594. + RESTORE();
  1595. + if (state->wsize || (out != strm->avail_out && state->mode < BAD &&
  1596. + (state->mode < CHECK || flush != Z_FINISH)))
  1597. + if (updatewindow(strm, strm->next_out, out - strm->avail_out)) {
  1598. + state->mode = MEM;
  1599. + return Z_MEM_ERROR;
  1600. + }
  1601. + in -= strm->avail_in;
  1602. + out -= strm->avail_out;
  1603. + strm->total_in += in;
  1604. + strm->total_out += out;
  1605. + state->total += out;
  1606. + if ((state->wrap & 4) && out)
  1607. + strm->adler = state->check =
  1608. + UPDATE(state->check, strm->next_out - out, out);
  1609. + strm->data_type = (int)state->bits + (state->last ? 64 : 0) +
  1610. + (state->mode == TYPE ? 128 : 0) +
  1611. + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0);
  1612. + if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK)
  1613. + ret = Z_BUF_ERROR;
  1614. + return ret;
  1615. +}
  1616. +
  1617. +int ZEXPORT inflateEnd(strm)
  1618. +z_streamp strm;
  1619. +{
  1620. + struct inflate_state FAR *state;
  1621. + if (inflateStateCheck(strm))
  1622. + return Z_STREAM_ERROR;
  1623. + state = (struct inflate_state FAR *)strm->state;
  1624. + if (state->window != Z_NULL) ZFREE(strm, state->window);
  1625. + ZFREE(strm, strm->state);
  1626. + strm->state = Z_NULL;
  1627. + Tracev((stderr, "inflate: end\n"));
  1628. + return Z_OK;
  1629. +}
  1630. +
  1631. +int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength)
  1632. +z_streamp strm;
  1633. +Bytef *dictionary;
  1634. +uInt *dictLength;
  1635. +{
  1636. + struct inflate_state FAR *state;
  1637. +
  1638. + /* check state */
  1639. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1640. + state = (struct inflate_state FAR *)strm->state;
  1641. +
  1642. + /* copy dictionary */
  1643. + if (state->whave && dictionary != Z_NULL) {
  1644. + zmemcpy(dictionary, state->window + state->wnext,
  1645. + state->whave - state->wnext);
  1646. + zmemcpy(dictionary + state->whave - state->wnext,
  1647. + state->window, state->wnext);
  1648. + }
  1649. + if (dictLength != Z_NULL)
  1650. + *dictLength = state->whave;
  1651. + return Z_OK;
  1652. +}
  1653. +
  1654. +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength)
  1655. +z_streamp strm;
  1656. +const Bytef *dictionary;
  1657. +uInt dictLength;
  1658. +{
  1659. + struct inflate_state FAR *state;
  1660. + unsigned long dictid;
  1661. + int ret;
  1662. +
  1663. + /* check state */
  1664. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1665. + state = (struct inflate_state FAR *)strm->state;
  1666. + if (state->wrap != 0 && state->mode != DICT)
  1667. + return Z_STREAM_ERROR;
  1668. +
  1669. + /* check for correct dictionary identifier */
  1670. + if (state->mode == DICT) {
  1671. + dictid = adler32(0L, Z_NULL, 0);
  1672. + dictid = adler32(dictid, dictionary, dictLength);
  1673. + if (dictid != state->check)
  1674. + return Z_DATA_ERROR;
  1675. + }
  1676. +
  1677. + /* copy dictionary to window using updatewindow(), which will amend the
  1678. + existing dictionary if appropriate */
  1679. + ret = updatewindow(strm, dictionary + dictLength, dictLength);
  1680. + if (ret) {
  1681. + state->mode = MEM;
  1682. + return Z_MEM_ERROR;
  1683. + }
  1684. + state->havedict = 1;
  1685. + Tracev((stderr, "inflate: dictionary set\n"));
  1686. + return Z_OK;
  1687. +}
  1688. +
  1689. +int ZEXPORT inflateGetHeader(strm, head)
  1690. +z_streamp strm;
  1691. +gz_headerp head;
  1692. +{
  1693. + struct inflate_state FAR *state;
  1694. +
  1695. + /* check state */
  1696. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1697. + state = (struct inflate_state FAR *)strm->state;
  1698. + if ((state->wrap & 2) == 0) return Z_STREAM_ERROR;
  1699. +
  1700. + /* save header structure */
  1701. + state->head = head;
  1702. + head->done = 0;
  1703. + return Z_OK;
  1704. +}
  1705. +
  1706. +/*
  1707. + Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found
  1708. + or when out of input. When called, *have is the number of pattern bytes
  1709. + found in order so far, in 0..3. On return *have is updated to the new
  1710. + state. If on return *have equals four, then the pattern was found and the
  1711. + return value is how many bytes were read including the last byte of the
  1712. + pattern. If *have is less than four, then the pattern has not been found
  1713. + yet and the return value is len. In the latter case, syncsearch() can be
  1714. + called again with more data and the *have state. *have is initialized to
  1715. + zero for the first call.
  1716. + */
  1717. +local unsigned syncsearch(have, buf, len)
  1718. +unsigned FAR *have;
  1719. +const unsigned char FAR *buf;
  1720. +unsigned len;
  1721. +{
  1722. + unsigned got;
  1723. + unsigned next;
  1724. +
  1725. + got = *have;
  1726. + next = 0;
  1727. + while (next < len && got < 4) {
  1728. + if ((int)(buf[next]) == (got < 2 ? 0 : 0xff))
  1729. + got++;
  1730. + else if (buf[next])
  1731. + got = 0;
  1732. + else
  1733. + got = 4 - got;
  1734. + next++;
  1735. + }
  1736. + *have = got;
  1737. + return next;
  1738. +}
  1739. +
  1740. +int ZEXPORT inflateSync(strm)
  1741. +z_streamp strm;
  1742. +{
  1743. + unsigned len; /* number of bytes to look at or looked at */
  1744. + unsigned long in, out; /* temporary to save total_in and total_out */
  1745. + unsigned char buf[4]; /* to restore bit buffer to byte string */
  1746. + struct inflate_state FAR *state;
  1747. +
  1748. + /* check parameters */
  1749. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1750. + state = (struct inflate_state FAR *)strm->state;
  1751. + if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR;
  1752. +
  1753. + /* if first time, start search in bit buffer */
  1754. + if (state->mode != SYNC) {
  1755. + state->mode = SYNC;
  1756. + state->hold <<= state->bits & 7;
  1757. + state->bits -= state->bits & 7;
  1758. + len = 0;
  1759. + while (state->bits >= 8) {
  1760. + buf[len++] = (unsigned char)(state->hold);
  1761. + state->hold >>= 8;
  1762. + state->bits -= 8;
  1763. + }
  1764. + state->have = 0;
  1765. + syncsearch(&(state->have), buf, len);
  1766. + }
  1767. +
  1768. + /* search available input */
  1769. + len = syncsearch(&(state->have), strm->next_in, strm->avail_in);
  1770. + strm->avail_in -= len;
  1771. + strm->next_in += len;
  1772. + strm->total_in += len;
  1773. +
  1774. + /* return no joy or set up to restart inflate() on a new block */
  1775. + if (state->have != 4) return Z_DATA_ERROR;
  1776. + in = strm->total_in; out = strm->total_out;
  1777. + inflateReset(strm);
  1778. + strm->total_in = in; strm->total_out = out;
  1779. + state->mode = TYPE;
  1780. + return Z_OK;
  1781. +}
  1782. +
  1783. +/*
  1784. + Returns true if inflate is currently at the end of a block generated by
  1785. + Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  1786. + implementation to provide an additional safety check. PPP uses
  1787. + Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored
  1788. + block. When decompressing, PPP checks that at the end of input packet,
  1789. + inflate is waiting for these length bytes.
  1790. + */
  1791. +int ZEXPORT inflateSyncPoint(strm)
  1792. +z_streamp strm;
  1793. +{
  1794. + struct inflate_state FAR *state;
  1795. +
  1796. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1797. + state = (struct inflate_state FAR *)strm->state;
  1798. + return state->mode == STORED && state->bits == 0;
  1799. +}
  1800. +
  1801. +int ZEXPORT inflateCopy(dest, source)
  1802. +z_streamp dest;
  1803. +z_streamp source;
  1804. +{
  1805. + struct inflate_state FAR *state;
  1806. + struct inflate_state FAR *copy;
  1807. + unsigned char FAR *window;
  1808. + unsigned wsize;
  1809. +
  1810. + /* check input */
  1811. + if (inflateStateCheck(source) || dest == Z_NULL)
  1812. + return Z_STREAM_ERROR;
  1813. + state = (struct inflate_state FAR *)source->state;
  1814. +
  1815. + /* allocate space */
  1816. + copy = (struct inflate_state FAR *)
  1817. + ZALLOC(source, 1, sizeof(struct inflate_state));
  1818. + if (copy == Z_NULL) return Z_MEM_ERROR;
  1819. + window = Z_NULL;
  1820. + if (state->window != Z_NULL) {
  1821. + window = (unsigned char FAR *)
  1822. + ZALLOC(source, 1U << state->wbits, sizeof(unsigned char));
  1823. + if (window == Z_NULL) {
  1824. + ZFREE(source, copy);
  1825. + return Z_MEM_ERROR;
  1826. + }
  1827. + }
  1828. +
  1829. + /* copy state */
  1830. + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream));
  1831. + zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state));
  1832. + copy->strm = dest;
  1833. + if (state->lencode >= state->codes &&
  1834. + state->lencode <= state->codes + ENOUGH - 1) {
  1835. + copy->lencode = copy->codes + (state->lencode - state->codes);
  1836. + copy->distcode = copy->codes + (state->distcode - state->codes);
  1837. + }
  1838. + copy->next = copy->codes + (state->next - state->codes);
  1839. + if (window != Z_NULL) {
  1840. + wsize = 1U << state->wbits;
  1841. + zmemcpy(window, state->window, wsize);
  1842. + }
  1843. + copy->window = window;
  1844. + dest->state = (struct internal_state FAR *)copy;
  1845. + return Z_OK;
  1846. +}
  1847. +
  1848. +int ZEXPORT inflateUndermine(strm, subvert)
  1849. +z_streamp strm;
  1850. +int subvert;
  1851. +{
  1852. + struct inflate_state FAR *state;
  1853. +
  1854. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1855. + state = (struct inflate_state FAR *)strm->state;
  1856. +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR
  1857. + state->sane = !subvert;
  1858. + return Z_OK;
  1859. +#else
  1860. + (void)subvert;
  1861. + state->sane = 1;
  1862. + return Z_DATA_ERROR;
  1863. +#endif
  1864. +}
  1865. +
  1866. +int ZEXPORT inflateValidate(strm, check)
  1867. +z_streamp strm;
  1868. +int check;
  1869. +{
  1870. + struct inflate_state FAR *state;
  1871. +
  1872. + if (inflateStateCheck(strm)) return Z_STREAM_ERROR;
  1873. + state = (struct inflate_state FAR *)strm->state;
  1874. + if (check)
  1875. + state->wrap |= 4;
  1876. + else
  1877. + state->wrap &= ~4;
  1878. + return Z_OK;
  1879. +}
  1880. +
  1881. +long ZEXPORT inflateMark(strm)
  1882. +z_streamp strm;
  1883. +{
  1884. + struct inflate_state FAR *state;
  1885. +
  1886. + if (inflateStateCheck(strm))
  1887. + return -(1L << 16);
  1888. + state = (struct inflate_state FAR *)strm->state;
  1889. + return (long)(((unsigned long)((long)state->back)) << 16) +
  1890. + (state->mode == COPY ? state->length :
  1891. + (state->mode == MATCH ? state->was - state->length : 0));
  1892. +}
  1893. +
  1894. +unsigned long ZEXPORT inflateCodesUsed(strm)
  1895. +z_streamp strm;
  1896. +{
  1897. + struct inflate_state FAR *state;
  1898. + if (inflateStateCheck(strm)) return (unsigned long)-1;
  1899. + state = (struct inflate_state FAR *)strm->state;
  1900. + return (unsigned long)(state->next - state->codes);
  1901. +}