spu: don't leave garbage in capture buffers
[pcsx_rearmed.git] / deps / libchdr / deps / zlib-1.2.13 / contrib / infback9 / infback9.c
CommitLineData
9e052883 1/* infback9.c -- inflate deflate64 data using a call-back interface
2 * Copyright (C) 1995-2008 Mark Adler
b24e7fce 3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
b24e7fce 6#include "zutil.h"
9e052883 7#include "infback9.h"
8#include "inftree9.h"
9#include "inflate9.h"
b24e7fce 10
9e052883 11#define WSIZE 65536UL
b24e7fce 12
13/*
14 strm provides memory allocation functions in zalloc and zfree, or
15 Z_NULL to use the library memory allocation functions.
16
9e052883 17 window is a user-supplied window and output buffer that is 64K bytes.
b24e7fce 18 */
9e052883 19int ZEXPORT inflateBack9Init_(strm, window, version, stream_size)
20z_stream FAR *strm;
b24e7fce 21unsigned char FAR *window;
22const char *version;
23int stream_size;
24{
25 struct inflate_state FAR *state;
26
27 if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
28 stream_size != (int)(sizeof(z_stream)))
29 return Z_VERSION_ERROR;
9e052883 30 if (strm == Z_NULL || window == Z_NULL)
b24e7fce 31 return Z_STREAM_ERROR;
32 strm->msg = Z_NULL; /* in case we return an error */
33 if (strm->zalloc == (alloc_func)0) {
b24e7fce 34 strm->zalloc = zcalloc;
35 strm->opaque = (voidpf)0;
b24e7fce 36 }
9e052883 37 if (strm->zfree == (free_func)0) strm->zfree = zcfree;
b24e7fce 38 state = (struct inflate_state FAR *)ZALLOC(strm, 1,
39 sizeof(struct inflate_state));
40 if (state == Z_NULL) return Z_MEM_ERROR;
41 Tracev((stderr, "inflate: allocated\n"));
9e052883 42 strm->state = (voidpf)state;
b24e7fce 43 state->window = window;
b24e7fce 44 return Z_OK;
45}
46
47/*
9e052883 48 Build and output length and distance decoding tables for fixed code
49 decoding.
b24e7fce 50 */
9e052883 51#ifdef MAKEFIXED
52#include <stdio.h>
53
54void makefixed9(void)
b24e7fce 55{
9e052883 56 unsigned sym, bits, low, size;
57 code *next, *lenfix, *distfix;
58 struct inflate_state state;
59 code fixed[544];
60
61 /* literal/length table */
62 sym = 0;
63 while (sym < 144) state.lens[sym++] = 8;
64 while (sym < 256) state.lens[sym++] = 9;
65 while (sym < 280) state.lens[sym++] = 7;
66 while (sym < 288) state.lens[sym++] = 8;
67 next = fixed;
68 lenfix = next;
69 bits = 9;
70 inflate_table9(LENS, state.lens, 288, &(next), &(bits), state.work);
71
72 /* distance table */
73 sym = 0;
74 while (sym < 32) state.lens[sym++] = 5;
75 distfix = next;
76 bits = 5;
77 inflate_table9(DISTS, state.lens, 32, &(next), &(bits), state.work);
78
79 /* write tables */
80 puts(" /* inffix9.h -- table for decoding deflate64 fixed codes");
81 puts(" * Generated automatically by makefixed9().");
82 puts(" */");
83 puts("");
84 puts(" /* WARNING: this file should *not* be used by applications.");
85 puts(" It is part of the implementation of this library and is");
86 puts(" subject to change. Applications should only use zlib.h.");
87 puts(" */");
88 puts("");
89 size = 1U << 9;
90 printf(" static const code lenfix[%u] = {", size);
91 low = 0;
92 for (;;) {
93 if ((low % 6) == 0) printf("\n ");
94 printf("{%u,%u,%d}", lenfix[low].op, lenfix[low].bits,
95 lenfix[low].val);
96 if (++low == size) break;
97 putchar(',');
b24e7fce 98 }
9e052883 99 puts("\n };");
100 size = 1U << 5;
101 printf("\n static const code distfix[%u] = {", size);
102 low = 0;
103 for (;;) {
104 if ((low % 5) == 0) printf("\n ");
105 printf("{%u,%u,%d}", distfix[low].op, distfix[low].bits,
106 distfix[low].val);
107 if (++low == size) break;
108 putchar(',');
109 }
110 puts("\n };");
b24e7fce 111}
9e052883 112#endif /* MAKEFIXED */
b24e7fce 113
114/* Macros for inflateBack(): */
115
b24e7fce 116/* Clear the input bit accumulator */
117#define INITBITS() \
118 do { \
119 hold = 0; \
120 bits = 0; \
121 } while (0)
122
123/* Assure that some input is available. If input is requested, but denied,
124 then return a Z_BUF_ERROR from inflateBack(). */
125#define PULL() \
126 do { \
127 if (have == 0) { \
128 have = in(in_desc, &next); \
129 if (have == 0) { \
130 next = Z_NULL; \
131 ret = Z_BUF_ERROR; \
132 goto inf_leave; \
133 } \
134 } \
135 } while (0)
136
137/* Get a byte of input into the bit accumulator, or return from inflateBack()
138 with an error if there is no input available. */
139#define PULLBYTE() \
140 do { \
141 PULL(); \
142 have--; \
143 hold += (unsigned long)(*next++) << bits; \
144 bits += 8; \
145 } while (0)
146
147/* Assure that there are at least n bits in the bit accumulator. If there is
148 not enough available input to do that, then return from inflateBack() with
149 an error. */
150#define NEEDBITS(n) \
151 do { \
152 while (bits < (unsigned)(n)) \
153 PULLBYTE(); \
154 } while (0)
155
9e052883 156/* Return the low n bits of the bit accumulator (n <= 16) */
b24e7fce 157#define BITS(n) \
158 ((unsigned)hold & ((1U << (n)) - 1))
159
160/* Remove n bits from the bit accumulator */
161#define DROPBITS(n) \
162 do { \
163 hold >>= (n); \
164 bits -= (unsigned)(n); \
165 } while (0)
166
167/* Remove zero to seven bits as needed to go to a byte boundary */
168#define BYTEBITS() \
169 do { \
170 hold >>= bits & 7; \
171 bits -= bits & 7; \
172 } while (0)
173
174/* Assure that some output space is available, by writing out the window
175 if it's full. If the write fails, return from inflateBack() with a
176 Z_BUF_ERROR. */
177#define ROOM() \
178 do { \
179 if (left == 0) { \
9e052883 180 put = window; \
181 left = WSIZE; \
182 wrap = 1; \
183 if (out(out_desc, put, (unsigned)left)) { \
b24e7fce 184 ret = Z_BUF_ERROR; \
185 goto inf_leave; \
186 } \
187 } \
188 } while (0)
189
190/*
191 strm provides the memory allocation functions and window buffer on input,
192 and provides information on the unused input on return. For Z_DATA_ERROR
193 returns, strm will also provide an error message.
194
195 in() and out() are the call-back input and output functions. When
196 inflateBack() needs more input, it calls in(). When inflateBack() has
197 filled the window with output, or when it completes with data in the
198 window, it calls out() to write out the data. The application must not
199 change the provided input until in() is called again or inflateBack()
200 returns. The application must not change the window/output buffer until
201 inflateBack() returns.
202
203 in() and out() are called with a descriptor parameter provided in the
204 inflateBack() call. This parameter can be a structure that provides the
205 information required to do the read or write, as well as accumulated
206 information on the input and output such as totals and check values.
207
208 in() should return zero on failure. out() should return non-zero on
209 failure. If either in() or out() fails, than inflateBack() returns a
210 Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it
211 was in() or out() that caused in the error. Otherwise, inflateBack()
212 returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format
213 error, or Z_MEM_ERROR if it could not allocate memory for the state.
214 inflateBack() can also return Z_STREAM_ERROR if the input parameters
215 are not correct, i.e. strm is Z_NULL or the state was not initialized.
216 */
9e052883 217int ZEXPORT inflateBack9(strm, in, in_desc, out, out_desc)
218z_stream FAR *strm;
b24e7fce 219in_func in;
220void FAR *in_desc;
221out_func out;
222void FAR *out_desc;
223{
224 struct inflate_state FAR *state;
225 z_const unsigned char FAR *next; /* next input */
226 unsigned char FAR *put; /* next output */
9e052883 227 unsigned have; /* available input */
228 unsigned long left; /* available output */
229 inflate_mode mode; /* current inflate mode */
230 int lastblock; /* true if processing last block */
231 int wrap; /* true if the window has wrapped */
232 unsigned char FAR *window; /* allocated sliding window, if needed */
b24e7fce 233 unsigned long hold; /* bit buffer */
234 unsigned bits; /* bits in bit buffer */
9e052883 235 unsigned extra; /* extra bits needed */
236 unsigned long length; /* literal or length of data to copy */
237 unsigned long offset; /* distance back to copy string from */
238 unsigned long copy; /* number of stored or match bytes to copy */
b24e7fce 239 unsigned char FAR *from; /* where to copy match bytes from */
9e052883 240 code const FAR *lencode; /* starting table for length/literal codes */
241 code const FAR *distcode; /* starting table for distance codes */
242 unsigned lenbits; /* index bits for lencode */
243 unsigned distbits; /* index bits for distcode */
b24e7fce 244 code here; /* current decoding table entry */
245 code last; /* parent table entry */
246 unsigned len; /* length to copy for repeats, bits to drop */
247 int ret; /* return code */
248 static const unsigned short order[19] = /* permutation of code lengths */
249 {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
9e052883 250#include "inffix9.h"
b24e7fce 251
252 /* Check that the strm exists and that the state was initialized */
253 if (strm == Z_NULL || strm->state == Z_NULL)
254 return Z_STREAM_ERROR;
255 state = (struct inflate_state FAR *)strm->state;
256
257 /* Reset the state */
258 strm->msg = Z_NULL;
9e052883 259 mode = TYPE;
260 lastblock = 0;
261 wrap = 0;
262 window = state->window;
b24e7fce 263 next = strm->next_in;
264 have = next != Z_NULL ? strm->avail_in : 0;
265 hold = 0;
266 bits = 0;
9e052883 267 put = window;
268 left = WSIZE;
269 lencode = Z_NULL;
270 distcode = Z_NULL;
b24e7fce 271
272 /* Inflate until end of block marked as last */
273 for (;;)
9e052883 274 switch (mode) {
b24e7fce 275 case TYPE:
276 /* determine and dispatch block type */
9e052883 277 if (lastblock) {
b24e7fce 278 BYTEBITS();
9e052883 279 mode = DONE;
b24e7fce 280 break;
281 }
282 NEEDBITS(3);
9e052883 283 lastblock = BITS(1);
b24e7fce 284 DROPBITS(1);
285 switch (BITS(2)) {
286 case 0: /* stored block */
287 Tracev((stderr, "inflate: stored block%s\n",
9e052883 288 lastblock ? " (last)" : ""));
289 mode = STORED;
b24e7fce 290 break;
291 case 1: /* fixed block */
9e052883 292 lencode = lenfix;
293 lenbits = 9;
294 distcode = distfix;
295 distbits = 5;
b24e7fce 296 Tracev((stderr, "inflate: fixed codes block%s\n",
9e052883 297 lastblock ? " (last)" : ""));
298 mode = LEN; /* decode codes */
b24e7fce 299 break;
300 case 2: /* dynamic block */
301 Tracev((stderr, "inflate: dynamic codes block%s\n",
9e052883 302 lastblock ? " (last)" : ""));
303 mode = TABLE;
b24e7fce 304 break;
305 case 3:
306 strm->msg = (char *)"invalid block type";
9e052883 307 mode = BAD;
b24e7fce 308 }
309 DROPBITS(2);
310 break;
311
312 case STORED:
313 /* get and verify stored block length */
314 BYTEBITS(); /* go to byte boundary */
315 NEEDBITS(32);
316 if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) {
317 strm->msg = (char *)"invalid stored block lengths";
9e052883 318 mode = BAD;
b24e7fce 319 break;
320 }
9e052883 321 length = (unsigned)hold & 0xffff;
322 Tracev((stderr, "inflate: stored length %lu\n",
323 length));
b24e7fce 324 INITBITS();
325
326 /* copy stored block from input to output */
9e052883 327 while (length != 0) {
328 copy = length;
b24e7fce 329 PULL();
330 ROOM();
331 if (copy > have) copy = have;
332 if (copy > left) copy = left;
333 zmemcpy(put, next, copy);
334 have -= copy;
335 next += copy;
336 left -= copy;
337 put += copy;
9e052883 338 length -= copy;
b24e7fce 339 }
340 Tracev((stderr, "inflate: stored end\n"));
9e052883 341 mode = TYPE;
b24e7fce 342 break;
343
344 case TABLE:
345 /* get dynamic table entries descriptor */
346 NEEDBITS(14);
347 state->nlen = BITS(5) + 257;
348 DROPBITS(5);
349 state->ndist = BITS(5) + 1;
350 DROPBITS(5);
351 state->ncode = BITS(4) + 4;
352 DROPBITS(4);
9e052883 353 if (state->nlen > 286) {
354 strm->msg = (char *)"too many length symbols";
355 mode = BAD;
b24e7fce 356 break;
357 }
b24e7fce 358 Tracev((stderr, "inflate: table sizes ok\n"));
359
360 /* get code length code lengths (not a typo) */
361 state->have = 0;
362 while (state->have < state->ncode) {
363 NEEDBITS(3);
364 state->lens[order[state->have++]] = (unsigned short)BITS(3);
365 DROPBITS(3);
366 }
367 while (state->have < 19)
368 state->lens[order[state->have++]] = 0;
369 state->next = state->codes;
9e052883 370 lencode = (code const FAR *)(state->next);
371 lenbits = 7;
372 ret = inflate_table9(CODES, state->lens, 19, &(state->next),
373 &(lenbits), state->work);
b24e7fce 374 if (ret) {
375 strm->msg = (char *)"invalid code lengths set";
9e052883 376 mode = BAD;
b24e7fce 377 break;
378 }
379 Tracev((stderr, "inflate: code lengths ok\n"));
380
381 /* get length and distance code code lengths */
382 state->have = 0;
383 while (state->have < state->nlen + state->ndist) {
384 for (;;) {
9e052883 385 here = lencode[BITS(lenbits)];
b24e7fce 386 if ((unsigned)(here.bits) <= bits) break;
387 PULLBYTE();
388 }
389 if (here.val < 16) {
9e052883 390 NEEDBITS(here.bits);
b24e7fce 391 DROPBITS(here.bits);
392 state->lens[state->have++] = here.val;
393 }
394 else {
395 if (here.val == 16) {
396 NEEDBITS(here.bits + 2);
397 DROPBITS(here.bits);
398 if (state->have == 0) {
399 strm->msg = (char *)"invalid bit length repeat";
9e052883 400 mode = BAD;
b24e7fce 401 break;
402 }
403 len = (unsigned)(state->lens[state->have - 1]);
404 copy = 3 + BITS(2);
405 DROPBITS(2);
406 }
407 else if (here.val == 17) {
408 NEEDBITS(here.bits + 3);
409 DROPBITS(here.bits);
410 len = 0;
411 copy = 3 + BITS(3);
412 DROPBITS(3);
413 }
414 else {
415 NEEDBITS(here.bits + 7);
416 DROPBITS(here.bits);
417 len = 0;
418 copy = 11 + BITS(7);
419 DROPBITS(7);
420 }
421 if (state->have + copy > state->nlen + state->ndist) {
422 strm->msg = (char *)"invalid bit length repeat";
9e052883 423 mode = BAD;
b24e7fce 424 break;
425 }
426 while (copy--)
427 state->lens[state->have++] = (unsigned short)len;
428 }
429 }
430
431 /* handle error breaks in while */
9e052883 432 if (mode == BAD) break;
b24e7fce 433
434 /* check for end-of-block code (better have one) */
435 if (state->lens[256] == 0) {
436 strm->msg = (char *)"invalid code -- missing end-of-block";
9e052883 437 mode = BAD;
b24e7fce 438 break;
439 }
440
441 /* build code tables -- note: do not change the lenbits or distbits
9e052883 442 values here (9 and 6) without reading the comments in inftree9.h
b24e7fce 443 concerning the ENOUGH constants, which depend on those values */
444 state->next = state->codes;
9e052883 445 lencode = (code const FAR *)(state->next);
446 lenbits = 9;
447 ret = inflate_table9(LENS, state->lens, state->nlen,
448 &(state->next), &(lenbits), state->work);
b24e7fce 449 if (ret) {
450 strm->msg = (char *)"invalid literal/lengths set";
9e052883 451 mode = BAD;
b24e7fce 452 break;
453 }
9e052883 454 distcode = (code const FAR *)(state->next);
455 distbits = 6;
456 ret = inflate_table9(DISTS, state->lens + state->nlen,
457 state->ndist, &(state->next), &(distbits),
458 state->work);
b24e7fce 459 if (ret) {
460 strm->msg = (char *)"invalid distances set";
9e052883 461 mode = BAD;
b24e7fce 462 break;
463 }
464 Tracev((stderr, "inflate: codes ok\n"));
9e052883 465 mode = LEN;
b24e7fce 466
467 case LEN:
b24e7fce 468 /* get a literal, length, or end-of-block code */
469 for (;;) {
9e052883 470 here = lencode[BITS(lenbits)];
b24e7fce 471 if ((unsigned)(here.bits) <= bits) break;
472 PULLBYTE();
473 }
474 if (here.op && (here.op & 0xf0) == 0) {
475 last = here;
476 for (;;) {
9e052883 477 here = lencode[last.val +
b24e7fce 478 (BITS(last.bits + last.op) >> last.bits)];
479 if ((unsigned)(last.bits + here.bits) <= bits) break;
480 PULLBYTE();
481 }
482 DROPBITS(last.bits);
483 }
484 DROPBITS(here.bits);
9e052883 485 length = (unsigned)here.val;
b24e7fce 486
487 /* process literal */
488 if (here.op == 0) {
489 Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ?
490 "inflate: literal '%c'\n" :
491 "inflate: literal 0x%02x\n", here.val));
492 ROOM();
9e052883 493 *put++ = (unsigned char)(length);
b24e7fce 494 left--;
9e052883 495 mode = LEN;
b24e7fce 496 break;
497 }
498
499 /* process end of block */
500 if (here.op & 32) {
501 Tracevv((stderr, "inflate: end of block\n"));
9e052883 502 mode = TYPE;
b24e7fce 503 break;
504 }
505
506 /* invalid code */
507 if (here.op & 64) {
508 strm->msg = (char *)"invalid literal/length code";
9e052883 509 mode = BAD;
b24e7fce 510 break;
511 }
512
513 /* length code -- get extra bits, if any */
9e052883 514 extra = (unsigned)(here.op) & 31;
515 if (extra != 0) {
516 NEEDBITS(extra);
517 length += BITS(extra);
518 DROPBITS(extra);
b24e7fce 519 }
9e052883 520 Tracevv((stderr, "inflate: length %lu\n", length));
b24e7fce 521
522 /* get distance code */
523 for (;;) {
9e052883 524 here = distcode[BITS(distbits)];
b24e7fce 525 if ((unsigned)(here.bits) <= bits) break;
526 PULLBYTE();
527 }
528 if ((here.op & 0xf0) == 0) {
529 last = here;
530 for (;;) {
9e052883 531 here = distcode[last.val +
b24e7fce 532 (BITS(last.bits + last.op) >> last.bits)];
533 if ((unsigned)(last.bits + here.bits) <= bits) break;
534 PULLBYTE();
535 }
536 DROPBITS(last.bits);
537 }
538 DROPBITS(here.bits);
539 if (here.op & 64) {
540 strm->msg = (char *)"invalid distance code";
9e052883 541 mode = BAD;
b24e7fce 542 break;
543 }
9e052883 544 offset = (unsigned)here.val;
b24e7fce 545
546 /* get distance extra bits, if any */
9e052883 547 extra = (unsigned)(here.op) & 15;
548 if (extra != 0) {
549 NEEDBITS(extra);
550 offset += BITS(extra);
551 DROPBITS(extra);
b24e7fce 552 }
9e052883 553 if (offset > WSIZE - (wrap ? 0: left)) {
b24e7fce 554 strm->msg = (char *)"invalid distance too far back";
9e052883 555 mode = BAD;
b24e7fce 556 break;
557 }
9e052883 558 Tracevv((stderr, "inflate: distance %lu\n", offset));
b24e7fce 559
560 /* copy match from window to output */
561 do {
562 ROOM();
9e052883 563 copy = WSIZE - offset;
b24e7fce 564 if (copy < left) {
565 from = put + copy;
566 copy = left - copy;
567 }
568 else {
9e052883 569 from = put - offset;
b24e7fce 570 copy = left;
571 }
9e052883 572 if (copy > length) copy = length;
573 length -= copy;
b24e7fce 574 left -= copy;
575 do {
576 *put++ = *from++;
577 } while (--copy);
9e052883 578 } while (length != 0);
b24e7fce 579 break;
580
581 case DONE:
582 /* inflate stream terminated properly -- write leftover output */
583 ret = Z_STREAM_END;
9e052883 584 if (left < WSIZE) {
585 if (out(out_desc, window, (unsigned)(WSIZE - left)))
b24e7fce 586 ret = Z_BUF_ERROR;
587 }
588 goto inf_leave;
589
590 case BAD:
591 ret = Z_DATA_ERROR;
592 goto inf_leave;
593
594 default: /* can't happen, but makes compilers happy */
595 ret = Z_STREAM_ERROR;
596 goto inf_leave;
597 }
598
599 /* Return unused input */
600 inf_leave:
601 strm->next_in = next;
602 strm->avail_in = have;
603 return ret;
604}
605
9e052883 606int ZEXPORT inflateBack9End(strm)
607z_stream FAR *strm;
b24e7fce 608{
609 if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0)
610 return Z_STREAM_ERROR;
611 ZFREE(strm, strm->state);
612 strm->state = Z_NULL;
613 Tracev((stderr, "inflate: end\n"));
614 return Z_OK;
615}