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

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