Indentations break the feed.
[SquirrelJME.git] / nanocoat / lib / base / inflate.c
blobbf86d2266e6296d93af70833387970fe8c4336c2
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "sjme/inflate.h"
13 #include "sjme/util.h"
15 /** Extra nodes to put into the traverse for overly complicated trees. */
16 #define SJME_INFLATE_TRAVERSE_EXTRA \
17 (maxCount)
19 /** Shuffled code length bits. */
20 static const sjme_jubyte sjme_inflate_shuffleBits[SJME_INFLATE_NUM_CODE_LEN] =
22 16, 17, 18, 0, 8, 7, 9, 6,
23 10, 5, 11, 4, 12, 3, 13,
24 2, 14, 1, 15
27 static sjme_errorCode sjme_inflate_bitFill(
28 sjme_attrInNotNull sjme_inflate* inState)
30 sjme_errorCode error;
31 sjme_jint avail, readCount;
32 sjme_jubyte* buffer;
34 if (inState == NULL)
35 return SJME_ERROR_NULL_ARGUMENTS;
37 /* Pointless if the input hit EOF. */
38 if (inState->inputEof)
39 return SJME_ERROR_NONE;
41 /* How much space is left in the input buffer? */
42 avail = INT32_MAX;
43 if (sjme_error_is(error = sjme_circleBuffer_available(
44 inState->inputBuffer, &avail)) ||
45 avail == INT32_MAX)
46 return sjme_error_default(error);
48 /* It is as full as it can get. */
49 if (avail <= 0)
50 return SJME_ERROR_NONE;
52 /* Setup target buffer to read into. */
53 buffer = sjme_alloca(avail);
54 if (buffer == NULL)
55 return SJME_ERROR_OUT_OF_MEMORY;
56 memset(buffer, 0, avail);
58 /* Read everything in as much as possible. */
59 readCount = INT32_MAX;
60 if (sjme_error_is(error = sjme_stream_inputReadFully(
61 inState->source, &readCount, buffer, avail)) ||
62 readCount == INT32_MAX)
63 return sjme_error_default(error);
65 /* EOF? */
66 if (readCount < 0)
68 inState->inputEof = SJME_JNI_TRUE;
69 return SJME_ERROR_NONE;
72 /* Push onto the buffer. */
73 if (sjme_error_is(error = sjme_circleBuffer_push(
74 inState->inputBuffer,
75 buffer, readCount,
76 SJME_CIRCLE_BUFFER_TAIL)))
77 return sjme_error_default(error);
79 /* Success! */
80 return SJME_ERROR_NONE;
83 static sjme_errorCode sjme_inflate_bitRead(
84 sjme_attrInNotNull sjme_bitStream_input inStream,
85 sjme_attrInNullable sjme_pointer functionData,
86 sjme_attrOutNotNull sjme_jint* readCount,
87 sjme_attrOutNotNullBuf(length) sjme_pointer outBuf,
88 sjme_attrInPositiveNonZero sjme_jint length)
90 sjme_errorCode error;
91 sjme_inflate* inState;
92 sjme_jint limit;
94 inState = functionData;
95 if (inStream == NULL || inState == NULL || readCount == NULL ||
96 outBuf == NULL)
97 return SJME_ERROR_NULL_ARGUMENTS;
99 if (length <= 0)
100 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
102 /* Always fill in before read. */
103 if (sjme_error_is(error = sjme_inflate_bitFill(inState)))
104 return sjme_error_default(error);
106 /* How much is in the input buffer? */
107 limit = INT32_MAX;
108 if (sjme_error_is(error = sjme_circleBuffer_stored(
109 inState->inputBuffer, &limit)) ||
110 limit == INT32_MAX)
111 return sjme_error_default(error);
113 /* If this is zero then we do not have enough to read from. */
114 if (limit == 0)
116 /* However if EOF was hit, we stop. */
117 if (inState->inputEof)
119 *readCount = -1;
120 return SJME_ERROR_NONE;
123 /* Otherwise, we need more data! */
124 return SJME_ERROR_TOO_SHORT;
127 /* Do not exceed the storage limit. */
128 if (limit > length)
129 limit = length;
131 /* Pop from the buffer. */
132 if (sjme_error_is(error = sjme_circleBuffer_pop(
133 inState->inputBuffer, outBuf, limit,
134 SJME_CIRCLE_BUFFER_HEAD)))
135 return sjme_error_default(error);
137 /* Success! */
138 *readCount = limit;
139 return SJME_ERROR_NONE;
142 static sjme_errorCode sjme_inflate_bitWrite(
143 sjme_attrInNotNull sjme_bitStream_output outStream,
144 sjme_attrInNullable sjme_pointer functionData,
145 sjme_attrInNotNullBuf(length) sjme_buffer writeBuf,
146 sjme_attrInPositiveNonZero sjme_jint length)
148 sjme_errorCode error;
149 sjme_inflate* inState;
151 inState = functionData;
152 if (outStream == NULL || inState == NULL || writeBuf == NULL)
153 return SJME_ERROR_NULL_ARGUMENTS;
155 if (length <= 0)
156 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
158 /* Push onto the window. */
159 if (sjme_error_is(error = sjme_circleBuffer_push(
160 inState->window, writeBuf, length,
161 SJME_CIRCLE_BUFFER_TAIL)))
162 return sjme_error_default(error);
164 /* And onto the output buffer. */
165 if (sjme_error_is(error = sjme_circleBuffer_push(
166 inState->outputBuffer, writeBuf, length,
167 SJME_CIRCLE_BUFFER_TAIL)))
168 return sjme_error_default(error);
170 #if defined(SJME_CONFIG_DEBUG) && defined(SJME_CONFIG_VERBOSE_INFLATE)
171 /* Debug. */
172 sjme_message_hexDump(writeBuf, length);
173 #endif
175 /* Success! */
176 return SJME_ERROR_NONE;
179 static sjme_errorCode sjme_inflate_bufferSaturation(
180 sjme_attrInNotNull sjme_inflate* inState,
181 sjme_attrInPositiveNonZero sjme_jint bitsNeeded)
183 sjme_errorCode error;
184 sjme_jint ready;
186 if (inState == NULL)
187 return SJME_ERROR_NULL_ARGUMENTS;
189 if (bitsNeeded <= 0)
190 return SJME_ERROR_INVALID_ARGUMENT;
192 /* The output buffer is saturated? */
193 if (inState->outputBuffer->ready >= SJME_INFLATE_IO_BUFFER_SATURATED)
194 return SJME_ERROR_BUFFER_SATURATED;
196 /* How many bits are already in the window? */
197 ready = INT32_MAX;
198 if (sjme_error_is(error = sjme_bitStream_bitsReady(
199 SJME_AS_BITSTREAM(inState->input),
200 &ready)) || ready == INT32_MAX)
201 return sjme_error_default(error);
203 /* Add in all the input buffer bytes. */
204 ready += (inState->inputBuffer->ready * 8);
206 /* Do we have enough? */
207 if (ready >= bitsNeeded)
208 return SJME_ERROR_NONE;
209 return SJME_ERROR_TOO_SHORT;
212 static sjme_errorCode sjme_inflate_copyWindow(
213 sjme_attrInNotNull sjme_inflate* inState,
214 sjme_attrInValue sjme_jint length,
215 sjme_attrInValue sjme_jint dist)
217 sjme_errorCode error;
218 sjme_jint maxLen, i, at;
219 sjme_jubyte* buf;
221 if (inState == NULL)
222 return SJME_ERROR_NULL_ARGUMENTS;
224 /* The maximum length that is valid is the smaller of the two values, */
225 /* essentially the length could be 5 but the distance 2, we cannot */
226 /* read past the end of the window, we can only read 2 bytes. */
227 maxLen = (length < dist ? length : dist);
229 /* Allocate window buffer. */
230 buf = sjme_alloca(maxLen);
231 if (buf == NULL)
232 return SJME_ERROR_OUT_OF_MEMORY;
233 memset(buf, 0, maxLen);
235 /* Read in from the window. */
236 if (sjme_error_is(error = sjme_circleBuffer_get(inState->window,
237 buf, maxLen,
238 SJME_CIRCLE_BUFFER_TAIL, dist)))
239 return sjme_error_default(error);
241 /* Write in bytes from the window, this always wraps around the buffer. */
242 for (i = 0, at = 0; i < length; i++)
244 /* Write single byte. */
245 if (sjme_error_is(error = sjme_bitStream_outputWrite(
246 inState->output, SJME_BITSTREAM_LSB,
247 buf[at], 8)))
248 return sjme_error_default(error);
250 /* Overflowing? */
251 if ((++at) >= maxLen)
252 at = 0;
255 /* Success! */
256 return SJME_ERROR_NONE;
259 static sjme_errorCode sjme_inflate_drain(
260 sjme_attrInNotNull sjme_inflate* inState,
261 sjme_attrInOutNotNull sjme_jint* drainOff,
262 sjme_attrOutNotNullBuf(length) sjme_buffer outBuf,
263 sjme_attrInPositiveNonZero sjme_jint length)
265 sjme_errorCode error;
266 sjme_jint stored, limit;
268 if (inState == NULL || drainOff == NULL || outBuf == NULL)
269 return SJME_ERROR_NULL_ARGUMENTS;
271 if (length <= 0)
272 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
274 /* How much is in the output buffer? */
275 stored = INT32_MAX;
276 if (sjme_error_is(error = sjme_circleBuffer_stored(
277 inState->outputBuffer, &stored)) ||
278 stored == INT32_MAX)
279 return sjme_error_default(error);
281 /* How much can be drained? */
282 limit = length - (*drainOff);
283 if (limit > stored)
284 limit = stored;
286 /* Nothing to do? */
287 if (limit == 0)
288 return SJME_ERROR_NONE;
290 /* Pop off the head. */
291 if (sjme_error_is(error = sjme_circleBuffer_pop(
292 inState->outputBuffer,
293 SJME_POINTER_OFFSET(outBuf, (*drainOff)),
294 limit, SJME_CIRCLE_BUFFER_HEAD)))
295 return sjme_error_default(error);
297 /* Move up the drain offset, which is also the cumulative total output. */
298 (*drainOff) += limit;
300 /* Success! */
301 return SJME_ERROR_NONE;
304 static sjme_errorCode sjme_inflate_dynamicBitIn(
305 sjme_attrInNotNull sjme_inflate* inState,
306 sjme_attrInNotNull sjme_traverse_sjme_jint fromTree,
307 sjme_attrOutNotNull sjme_juint* outValue)
309 sjme_errorCode error;
310 sjme_traverse_iterator iterator;
311 sjme_juint bit;
312 sjme_juint* result;
314 if (inState == NULL || fromTree == NULL || outValue == NULL)
315 return SJME_ERROR_NULL_ARGUMENTS;
317 /* Start tree iteration. */
318 memset(&iterator, 0, sizeof(iterator));
319 if (sjme_error_is(error = sjme_traverse_iterate(
320 SJME_AS_TRAVERSE(fromTree), &iterator)))
321 return sjme_error_default(error);
323 /* Constantly read in bits until a leaf is hit. */
324 for (;;)
326 /* Read in bit. */
327 bit = INT32_MAX;
328 if (sjme_error_is(error = sjme_bitStream_inputRead(
329 inState->input,
330 SJME_BITSTREAM_LSB, &bit, 1)) ||
331 bit == INT32_MAX)
332 return sjme_error_default(error);
334 /* Traverse in the tree. */
335 result = NULL;
336 if (sjme_error_is(error = sjme_traverse_iterateNext(
337 fromTree, &iterator, &result, bit, 1, sjme_jint, 0)))
338 return sjme_error_default(error);
340 /* If a value was read, we are at a leaf. */
341 if (result != NULL)
343 *outValue = *result;
344 return SJME_ERROR_NONE;
348 /* Fail if no value was read. */
349 return SJME_ERROR_INFLATE_INVALID_CODE;
352 static sjme_errorCode sjme_inflate_dynamicBuildReadCode(
353 sjme_attrInNotNull sjme_inflate* inState,
354 sjme_attrOutNotNull sjme_juint* codes,
355 sjme_attrInOutNotNull sjme_jint* i,
356 sjme_attrInPositiveNonZero sjme_jint count,
357 sjme_attrInPositiveNonZero sjme_jint maxCount)
359 sjme_errorCode error;
360 sjme_traverse_sjme_jint treeCodeLen;
361 sjme_juint code, repFor, repVal;
362 sjme_jint x;
364 if (inState == NULL || codes == NULL || i == NULL)
365 return SJME_ERROR_NONE;
367 if (count <= 0 || maxCount <= 0)
368 return SJME_ERROR_INVALID_ARGUMENT;
370 /* Need the code length tree. */
371 treeCodeLen = inState->treeCodeLen;
372 if (treeCodeLen == NULL)
373 return SJME_ERROR_ILLEGAL_STATE;
375 /* Read in code from the tree. */
376 code = INT32_MAX;
377 if (sjme_error_is(error = sjme_inflate_dynamicBitIn(
378 inState, treeCodeLen, &code)) || code == INT32_MAX)
379 return sjme_error_default(error);
381 /* Input is used literally. */
382 if (code >= 0 && code < 16)
384 if ((*i) >= maxCount)
385 return SJME_ERROR_INFLATE_INDEX_OVERFLOW;
387 /* Uses the same input code. */
388 codes[(*i)++] = code;
389 return SJME_ERROR_NONE;
392 /* Repeat previous length 3-6 times. */
393 repVal = INT32_MAX;
394 repFor = INT32_MAX;
395 if (code == 16)
397 /* Cannot be the first entry. */
398 if ((*i) <= 0)
399 return SJME_ERROR_INFLATE_INVALID_CODE_LENGTH;
401 /* Repeat this... */
402 repVal = codes[(*i) - 1];
404 /* This many times. */
405 if (sjme_error_is(error = sjme_bitStream_inputRead(
406 inState->input, SJME_BITSTREAM_LSB,
407 &repFor, 2)) || repFor == INT32_MAX)
408 return sjme_error_default(error);
409 repFor += 3;
412 /* Repeat zero for 3-10 times */
413 else if (code == 17)
415 /* Repeat zero... */
416 repVal = 0;
418 /* This many times. */
419 if (sjme_error_is(error = sjme_bitStream_inputRead(
420 inState->input, SJME_BITSTREAM_LSB,
421 &repFor, 3)) || repFor == INT32_MAX)
422 return sjme_error_default(error);
423 repFor += 3;
426 /* Repeat zero for 11-138 times */
427 else if (code == 18)
429 /* Repeat zero... */
430 repVal = 0;
432 /* This many times. */
433 if (sjme_error_is(error = sjme_bitStream_inputRead(
434 inState->input, SJME_BITSTREAM_LSB,
435 &repFor, 7)) || repFor == INT32_MAX)
436 return sjme_error_default(error);
437 repFor += 11;
440 /* Invalid. */
441 if (repFor == INT32_MAX || repVal == INT32_MAX)
442 return SJME_ERROR_INFLATE_INVALID_CODE;
444 /* Store in repeated values. */
445 for (x = 0; x < repFor; x++)
447 if ((*i) >= maxCount)
448 return SJME_ERROR_INFLATE_INDEX_OVERFLOW;
450 /* Repeat the requested code. */
451 codes[(*i)++] = repVal;
454 /* Success! */
455 return SJME_ERROR_NONE;
458 static sjme_errorCode sjme_inflate_dynamicBuildTree(
459 sjme_attrInNotNull sjme_inflate* inState,
460 sjme_attrOutNotNull sjme_traverse_sjme_jint* outTree,
461 sjme_attrInNotNullBuf(count) sjme_juint* codeLens,
462 sjme_attrInPositiveNonZero sjme_jint count,
463 sjme_attrInPositiveNonZero sjme_jint maxCount)
465 #if defined(SJME_CONFIG_DEBUG) && defined(SJME_CONFIG_VERBOSE_INFLATE)
466 sjme_cchar binary[40];
467 #endif
468 sjme_errorCode error;
469 sjme_juint blCount[SJME_INFLATE_CODE_LEN_MAX_BITS + 1];
470 sjme_juint nextCode[SJME_INFLATE_CODE_LEN_MAX_BITS + 1];
471 sjme_juint code, len, nextCodeLen;
472 sjme_jint i;
474 if (inState == NULL || outTree == NULL || codeLens == NULL)
475 return SJME_ERROR_NULL_ARGUMENTS;
477 if (count <= 0 || maxCount <= 0 || count > maxCount)
478 return SJME_ERROR_INVALID_ARGUMENT;
480 /* Need to allocate the tree? */
481 if ((*outTree) == NULL)
482 if (sjme_error_is(error = sjme_traverse_new(
483 inState->inPool, outTree,
484 maxCount + SJME_INFLATE_TRAVERSE_EXTRA,
485 sjme_jint, 0)) ||
486 (*outTree) == NULL)
487 return sjme_error_default(error);
489 /* Wipe working arrays. */
490 memset(blCount, 0, sizeof(blCount));
491 memset(nextCode, 0, sizeof(nextCode));
493 /* Determine the bit length count for all the inputs */
494 for (i = 0; i < count; i++)
495 blCount[codeLens[i]]++;
496 blCount[0] = 0;
498 /* Find the numerical value of the smallest code for each code length. */
499 code = 0;
500 for (i = 1; i <= SJME_INFLATE_CODE_LEN_MAX_BITS; i++)
502 code = (code + blCount[i - 1]) << 1;
503 nextCode[i] = code;
506 /* Clear the target tree. */
507 if (sjme_error_is(error = sjme_traverse_clear(
508 SJME_AS_TRAVERSE((*outTree)))))
509 return sjme_error_default(error);
511 /* Assign all values to codes. */
512 for (i = 0; i < count; i++)
514 /* Add code length to the huffman tree */
515 len = codeLens[i];
516 if (len != 0)
518 nextCodeLen = (nextCode[len])++;
520 #if defined(SJME_CONFIG_DEBUG) && defined(SJME_CONFIG_VERBOSE_INFLATE)
521 sjme_util_intToBinary(
522 binary, sizeof(binary) - 1,
523 nextCodeLen, len);
524 sjme_message("Tree -> %s = %d",
525 binary, i);
526 #endif
528 /* Put into the tree. */
529 if (sjme_error_is(error = sjme_traverse_putM(
530 SJME_AS_TRAVERSE((*outTree)),
531 SJME_TRAVERSE_NORMAL,
532 &i, nextCodeLen, len,
533 sjme_jint, 0)))
534 return sjme_error_default(error);
538 /* Success! */
539 return SJME_ERROR_NONE;
542 static sjme_errorCode sjme_inflate_dynamicReadCode(
543 sjme_attrInNotNull sjme_inflate* inState,
544 sjme_attrOutNotNull sjme_juint* outCode)
546 sjme_errorCode error;
547 sjme_juint code;
549 if (inState == NULL || outCode == NULL)
550 return SJME_ERROR_NULL_ARGUMENTS;
552 /* At least 1 bit of data. */
553 if (sjme_error_is(error = sjme_inflate_bufferSaturation(inState,
554 1)))
555 return sjme_error_default(error);
557 /* Read in code. */
558 code = INT32_MAX;
559 if (sjme_error_is(error = sjme_inflate_dynamicBitIn(
560 inState, inState->treeLit, &code)) ||
561 code == INT32_MAX)
562 return sjme_error_default(error);
564 /* Give the code. */
565 *outCode = code;
566 return SJME_ERROR_NONE;
569 static sjme_errorCode sjme_inflate_dynamicReadDist(
570 sjme_attrInNotNull sjme_inflate* inState,
571 sjme_attrOutNotNull sjme_juint* outDist)
573 sjme_errorCode error;
574 sjme_juint code;
576 if (inState == NULL || outDist == NULL)
577 return SJME_ERROR_NULL_ARGUMENTS;
579 /* At least 1 bit of data. */
580 if (sjme_error_is(error = sjme_inflate_bufferSaturation(inState,
581 1)))
582 return sjme_error_default(error);
584 /* Read in code. */
585 code = INT32_MAX;
586 if (sjme_error_is(error = sjme_inflate_dynamicBitIn(
587 inState, inState->treeDist, &code)) ||
588 code == INT32_MAX)
589 return sjme_error_default(error);
591 /* Give the code. */
592 *outDist = code;
593 return SJME_ERROR_NONE;
596 static sjme_errorCode sjme_inflate_fixedReadCode(
597 sjme_attrInNotNull sjme_inflate* inState,
598 sjme_attrOutNotNull sjme_juint* outCode)
600 sjme_errorCode error;
601 sjme_juint nine, nextBits, codeBase, nineBase, fragment;
603 if (inState == NULL || outCode == NULL)
604 return SJME_ERROR_NULL_ARGUMENTS;
606 /* Make sure we at least have 7 bits available. */
607 if (sjme_error_is(error = sjme_inflate_bufferSaturation(inState,
608 7)))
609 return sjme_error_default(error);
611 /* Read in the first, or only, seven bits. */
612 nine = INT32_MAX;
613 if (sjme_error_is(error = sjme_bitStream_inputRead(
614 inState->input,
615 SJME_BITSTREAM_MSB, &nine, 7)) ||
616 nine == INT32_MAX)
617 return sjme_error_default(error);
619 /* Make this nine bits for consistency. */
620 nine <<= 2;
622 /* Based on the current bit set, determine the code and remaining */
623 /* bits we can read. */
624 /* The mask is {upper range - lower range}, and is added to the base. */
625 codeBase = INT32_MAX;
626 nextBits = INT32_MAX;
627 nineBase = INT32_MAX;
629 /* 256 - 279 / 0000000.. - 0010111.. */
630 if (nine >= 0x000 && nine <= 0x05F)
632 codeBase = 256;
633 nextBits = 0;
634 nineBase = 0x000;
637 /* 0 - 143 / 00110000. - 10111111. */
638 else if (nine >= 0x060 && nine <= 0x17F)
640 codeBase = 0;
641 nextBits = 1;
642 nineBase = 0x060;
645 /* 280 - 287 / 11000000. - 11000111. */
646 else if (nine >= 0x180 && nine <= 0x18F)
648 codeBase = 280;
649 nextBits = 1;
650 nineBase = 0x180;
653 /* 144 - 255 / 110010000 - 111111111 */
654 else if (nine >= 0x190 && nine <= 0x1FF)
656 codeBase = 144;
657 nextBits = 2;
658 nineBase = 0x190;
661 /* Invalid code? */
662 if (codeBase == INT32_MAX || nextBits == INT32_MAX ||
663 nineBase == INT32_MAX)
664 return SJME_ERROR_INFLATE_INVALID_CODE;
666 /* If there are more bits to read, read them in now. */
667 fragment = 0;
668 if (nextBits != 0)
670 /* Read in bits. */
671 fragment = INT32_MAX;
672 if (sjme_error_is(error = sjme_bitStream_inputRead(
673 inState->input, SJME_BITSTREAM_MSB,
674 &fragment, nextBits)) || fragment == INT32_MAX)
675 return sjme_error_default(error);
678 /* Recompose the code, remember that we shifted left twice to make */
679 /* a consistent 9 bits, if we did not need to do that, then undo it. */
680 *outCode = codeBase + ((((nine - nineBase) >> (2 - nextBits)) | fragment));
682 /* Success! */
683 return SJME_ERROR_NONE;
686 static sjme_errorCode sjme_inflate_fixedReadDist(
687 sjme_attrInNotNull sjme_inflate* inState,
688 sjme_attrOutNotNull sjme_juint* outDist)
690 sjme_errorCode error;
691 sjme_juint result;
693 if (inState == NULL || outDist == NULL)
694 return SJME_ERROR_NULL_ARGUMENTS;
696 /* Fixed huffman distance codes are always 5 bits. */
697 result = INT32_MAX;
698 if (sjme_error_is(error = sjme_bitStream_inputRead(
699 inState->input, SJME_BITSTREAM_MSB,
700 &result, 5)) || result == INT32_MAX)
701 return sjme_error_default(error);
703 /* Success! */
704 *outDist = result;
705 return SJME_ERROR_NONE;
708 static sjme_errorCode sjme_inflate_readDistance(
709 sjme_attrInNotNull sjme_inflate* inState,
710 sjme_attrOutNotNull sjme_juint* outDist)
712 sjme_errorCode error;
713 sjme_juint code, result, i, group;
715 if (inState == NULL || outDist == NULL)
716 return SJME_ERROR_NULL_ARGUMENTS;
718 /* Read in the base distance code. */
719 code = INT32_MAX;
720 if (sjme_error_is(error = inState->sub.huffman.readDist(
721 inState, &code)) || code == INT32_MAX)
722 return sjme_error_default(error);
724 /* Distance codes can only be so high. */
725 if (code > 29)
726 return SJME_ERROR_INFLATE_INVALID_CODE;
728 /* Distances always start at 1. */
729 result = 1;
731 /* Determine if any bits repeat. */
732 for (i = 0; i < code; i++)
734 /* Too short to repeat? */
735 if (i < 2)
737 result++;
738 continue;
741 /* Otherwise, repeats in groups of two. */
742 group = ((i / 2) - 1);
743 result += (1 << group);
746 /* Do we need to read in any extra bits to the distance value? */
747 if (code >= 4)
749 /* How many bits to read? */
750 group = (code / 2) - 1;
752 /* Read in those extra bits. */
753 i = INT32_MAX;
754 if (sjme_error_is(error = sjme_bitStream_inputRead(
755 inState->input, SJME_BITSTREAM_LSB,
756 &i, group)) || i == INT32_MAX)
757 return sjme_error_default(error);
759 /* Add in those bits. */
760 result += i;
763 /* Success! */
764 *outDist = result;
765 return SJME_ERROR_NONE;
768 static sjme_errorCode sjme_inflate_readLength(
769 sjme_attrInNotNull sjme_inflate* inState,
770 sjme_attrInRange(257, 285) sjme_juint code,
771 sjme_attrOutNotNull sjme_juint* outLength)
773 sjme_errorCode error;
774 sjme_juint result, base, i, group;
776 if (inState == NULL || outLength == NULL)
777 return SJME_ERROR_NULL_ARGUMENTS;
779 if (code < 257 || code > 285)
780 return SJME_ERROR_INVALID_ARGUMENT;
782 /* The max code is 258, note the fifty-eight and not eighty-five. */
783 /* Previously in my Java inflate code, getting these wrong was bad. */
784 if (code == 285)
786 *outLength = 258;
787 return SJME_ERROR_NONE;
790 /* Resultant length always starts at three. */
791 result = 3;
793 /* Determine if there are any repeating bits. */
794 for (base = code - 257, i = 0; i < base; i++)
796 /* Is too short to be in a group. */
797 if (i < 8)
799 result++;
800 continue;
803 /* Code lengths are in groups of 4, which get shifted up by their */
804 /* group index. */
805 group = ((i / 4) - 1);
806 result += (1 << group);
809 /* Do we need to read in any extra bits to the length value? */
810 if (base >= 8)
812 /* How many bits to read? */
813 group = (base / 4) - 1;
815 /* Read in those extra bits. */
816 i = INT32_MAX;
817 if (sjme_error_is(error = sjme_bitStream_inputRead(
818 inState->input, SJME_BITSTREAM_LSB,
819 &i, group)) || i == INT32_MAX)
820 return sjme_error_default(error);
822 /* Add in those bits. */
823 result += i;
826 /* Success! */
827 *outLength = result;
828 return SJME_ERROR_NONE;
831 static sjme_errorCode sjme_inflate_stepBType(
832 sjme_attrInNotNull sjme_inflate* inState)
834 sjme_errorCode error;
835 sjme_juint isFinal, blockType;
837 if (inState == NULL)
838 return SJME_ERROR_NULL_ARGUMENTS;
840 /* If final was hit, then we are done. */
841 if (inState->finalHit)
843 inState->step = SJME_INFLATE_STEP_FINISHED;
844 return SJME_ERROR_NONE;
847 /* Can we actually read the block? */
848 if (sjme_error_is(error = sjme_inflate_bufferSaturation(
849 inState, 3)))
850 return sjme_error_default(error);
852 /* Read in final block flag. */
853 isFinal = INT32_MAX;
854 if (sjme_error_is(error = sjme_bitStream_inputRead(
855 inState->input,
856 SJME_BITSTREAM_LSB, &isFinal, 1)) ||
857 isFinal == INT32_MAX)
858 return sjme_error_default(error);
860 /* Read in block type. */
861 blockType = INT32_MAX;
862 if (sjme_error_is(error = sjme_bitStream_inputRead(
863 inState->input,
864 SJME_BITSTREAM_LSB, &blockType, 2)) ||
865 blockType == INT32_MAX)
866 return sjme_error_default(error);
868 /* Invalid. */
869 if (blockType == 3)
870 return SJME_ERROR_INFLATE_INVALID_BTYPE;
872 /* Was the final block hit? */
873 if (isFinal)
874 inState->finalHit = SJME_JNI_TRUE;
876 /* Which step to transition to? */
877 if (blockType == 0)
878 inState->step = SJME_INFLATE_STEP_LITERAL_SETUP;
879 else if (blockType == 1)
880 inState->step = SJME_INFLATE_STEP_FIXED_SETUP;
881 else
882 inState->step = SJME_INFLATE_STEP_DYNAMIC_SETUP;
884 /* Success! */
885 return SJME_ERROR_NONE;
888 sjme_errorCode sjme_inflate_stepLiteralData(
889 sjme_attrInNotNull sjme_inflate* inState)
891 sjme_errorCode error;
892 sjme_jint left;
893 sjme_juint raw;
895 if (inState == NULL)
896 return SJME_ERROR_NULL_ARGUMENTS;
898 /* Copy in what we can, provided the buffers are not saturated */
899 /* and we have input data. */
900 left = inState->sub.literalLeft;
901 while (left > 0)
903 /* Can we actually read a byte? */
904 if (sjme_error_is(error = sjme_inflate_bufferSaturation(
905 inState, 8)))
906 goto fail_saturated;
908 /* Read in byte. */
909 raw = INT32_MAX;
910 if (sjme_error_is(error = sjme_bitStream_inputRead(
911 inState->input,
912 SJME_BITSTREAM_LSB, &raw, 8)) ||
913 raw == INT32_MAX)
914 goto fail_read;
916 /* Write out bits. */
917 if (sjme_error_is(error = sjme_bitStream_outputWrite(
918 inState->output,
919 SJME_BITSTREAM_LSB, raw, 8)))
920 goto fail_write;
922 /* We consumed a byte. */
923 left--;
926 /* Store for next run. */
927 inState->sub.literalLeft = left;
929 /* Did we read in all the data? Go back to the block type parse. */
930 if (left == 0)
931 inState->step = SJME_INFLATE_STEP_CHECK_BTYPE;
933 /* Success! */
934 return SJME_ERROR_NONE;
936 fail_write:
937 fail_read:
938 fail_saturated:
939 /* Store for next run. */
940 inState->sub.literalLeft = left;
942 return sjme_error_default(error);
945 sjme_errorCode sjme_inflate_stepLiteralSetup(
946 sjme_attrInNotNull sjme_inflate* inState)
948 sjme_errorCode error;
949 sjme_juint len, nel;
951 if (inState == NULL)
952 return SJME_ERROR_NULL_ARGUMENTS;
954 /* Literal uncompressed data starts at the byte boundary. */
955 if (sjme_error_is(error = sjme_bitStream_inputAlign(
956 inState->input,
957 8, NULL)))
958 return sjme_error_default(error);
960 /* Can we actually read the lengths? */
961 if (sjme_error_is(error = sjme_inflate_bufferSaturation(
962 inState, 32)))
963 return sjme_error_default(error);
965 /* Read both the length and its complement. */
966 len = INT32_MAX;
967 nel = INT32_MAX;
968 if (sjme_error_is(error = sjme_bitStream_inputRead(
969 inState->input, SJME_BITSTREAM_LSB,
970 &len, 16)))
971 return sjme_error_default(error);
972 if (sjme_error_is(error = sjme_bitStream_inputRead(
973 inState->input, SJME_BITSTREAM_LSB,
974 &nel, 16)))
975 return sjme_error_default(error);
977 /* Both of these should be the complement to each other. */
978 if (len != (nel ^ 0xFFFF))
979 return SJME_ERROR_INFLATE_INVALID_INVERT;
981 /* Setup for next step. */
982 memset(&inState->sub, 0, sizeof(inState->sub));
983 inState->sub.literalLeft = len;
984 inState->step = SJME_INFLATE_STEP_LITERAL_DATA;
986 /* Success! */
987 return SJME_ERROR_NONE;
990 static sjme_errorCode sjme_inflate_stepDynamicSetup(
991 sjme_attrInNotNull sjme_inflate* inState)
993 sjme_errorCode error;
994 sjme_jint i;
995 sjme_juint rawCodeLen[SJME_INFLATE_NUM_CODE_LEN];
996 sjme_juint rawLit[SJME_INFLATE_NUM_LIT_LENS];
997 sjme_juint rawDist[SJME_INFLATE_NUM_DIST_LENS];
998 sjme_juint hLit, hDist, hcLen, raw;
1000 if (inState == NULL)
1001 return SJME_ERROR_NULL_ARGUMENTS;
1003 /* Read in literal count. */
1004 hLit = INT32_MAX;
1005 if (sjme_error_is(error = sjme_bitStream_inputRead(
1006 inState->input, SJME_BITSTREAM_LSB,
1007 &hLit, 5)) ||
1008 hLit == INT32_MAX)
1009 return sjme_error_default(error);
1011 /* Read in distance count. */
1012 hDist = INT32_MAX;
1013 if (sjme_error_is(error = sjme_bitStream_inputRead(
1014 inState->input, SJME_BITSTREAM_LSB,
1015 &hDist, 5)) ||
1016 hDist == INT32_MAX)
1017 return sjme_error_default(error);
1019 /* Read in code length count. */
1020 hcLen = INT32_MAX;
1021 if (sjme_error_is(error = sjme_bitStream_inputRead(
1022 inState->input, SJME_BITSTREAM_LSB,
1023 &hcLen, 4)) ||
1024 hcLen == INT32_MAX)
1025 return sjme_error_default(error);
1027 /* Determine their actual values. */
1028 hLit += 257;
1029 hDist += 1;
1030 hcLen += 4;
1032 /* Read in raw code lengths. */
1033 memset(rawCodeLen, 0, sizeof(rawCodeLen));
1034 for (i = 0; i < hcLen; i++)
1036 /* Read in raw code length. */
1037 raw = INT32_MAX;
1038 if (sjme_error_is(error = sjme_bitStream_inputRead(
1039 inState->input, SJME_BITSTREAM_LSB,
1040 &raw, 3)) || raw == INT32_MAX)
1041 return sjme_error_default(error);
1043 /* Shuffle it in appropriately, the shuffle is "optimized" according */
1044 /* to RFC1951 so the most importing bits are set first. */
1045 rawCodeLen[sjme_inflate_shuffleBits[i]] = raw;
1048 /* Build code length tree. */
1049 if (sjme_error_is(error = sjme_inflate_dynamicBuildTree(
1050 inState, &inState->treeCodeLen,
1051 &rawCodeLen[0],
1052 SJME_INFLATE_NUM_CODE_LEN,
1053 SJME_INFLATE_NUM_CODE_LEN)))
1054 return sjme_error_default(error);
1056 /* Read literal values. */
1057 memset(rawLit, 0, sizeof(rawLit));
1058 for (i = 0; i < hLit;)
1060 /* Read in raw code. */
1061 if (sjme_error_is(error = sjme_inflate_dynamicBuildReadCode(
1062 inState, &rawLit[0], &i,
1063 hLit, SJME_INFLATE_NUM_LIT_LENS)))
1064 return sjme_error_default(error);
1067 /* Build literal tree. */
1068 if (sjme_error_is(error = sjme_inflate_dynamicBuildTree(
1069 inState, &inState->treeLit,
1070 &rawLit[0],
1071 hLit,
1072 SJME_INFLATE_NUM_LIT_LENS)))
1073 return sjme_error_default(error);
1075 /* Read distance values. */
1076 memset(rawDist, 0, sizeof(rawDist));
1077 for (i = 0; i < hDist;)
1079 /* Read in raw code. */
1080 if (sjme_error_is(error = sjme_inflate_dynamicBuildReadCode(
1081 inState, &rawDist[0], &i, hDist,
1082 SJME_INFLATE_NUM_DIST_LENS)))
1083 return sjme_error_default(error);
1086 /* Build Distance tree. */
1087 if (sjme_error_is(error = sjme_inflate_dynamicBuildTree(
1088 inState, &inState->treeDist,
1089 &rawDist[0],
1090 hDist,
1091 SJME_INFLATE_NUM_DIST_LENS)))
1092 return sjme_error_default(error);
1094 /* Set state for code reading. */
1095 memset(&inState->sub, 0, sizeof(inState->sub));
1096 inState->sub.huffman.readCode = sjme_inflate_dynamicReadCode;
1097 inState->sub.huffman.readDist = sjme_inflate_dynamicReadDist;
1099 /* Move onto decompression stage. */
1100 inState->step = SJME_INFLATE_STEP_INFLATE_FROM_TREE;
1101 return SJME_ERROR_NONE;
1104 static sjme_errorCode sjme_inflate_stepFixedSetup(
1105 sjme_attrInNotNull sjme_inflate* inState)
1107 if (inState == NULL)
1108 return SJME_ERROR_NULL_ARGUMENTS;
1110 /* Setup for new state, which is very simple. */
1111 memset(&inState->sub, 0, sizeof(inState->sub));
1112 inState->sub.huffman.readCode = sjme_inflate_fixedReadCode;
1113 inState->sub.huffman.readDist = sjme_inflate_fixedReadDist;
1115 /* Move onto decompression stage. */
1116 inState->step = SJME_INFLATE_STEP_INFLATE_FROM_TREE;
1117 return SJME_ERROR_NONE;
1120 static sjme_errorCode sjme_inflate_stepInflateFromTree(
1121 sjme_attrInNotNull sjme_inflate* inState)
1123 sjme_errorCode error;
1124 sjme_juint code, length, dist;
1126 if (inState == NULL)
1127 return SJME_ERROR_NULL_ARGUMENTS;
1129 /* Continual code reading loop, provided not saturated. */
1130 for (;;)
1132 /* Read in next code. */
1133 code = INT32_MAX;
1134 if (sjme_error_is(error = inState->sub.huffman.readCode(inState,
1135 &code)) || code == INT32_MAX)
1136 return sjme_error_default(error);
1138 /* Invalid. */
1139 if (code > 285)
1140 return SJME_ERROR_INFLATE_INVALID_CODE;
1142 /* Stop decompression. */
1143 else if (code == 256)
1145 /* Read the next block type. */
1146 inState->step = SJME_INFLATE_STEP_CHECK_BTYPE;
1147 break;
1150 /* Literal byte value. */
1151 else if (code >= 0 && code <= 255)
1153 /* Standard write. */
1154 if (sjme_error_is(error = sjme_bitStream_outputWrite(
1155 inState->output, SJME_BITSTREAM_LSB,
1156 code, 8)))
1157 return sjme_error_default(error);
1159 continue;
1162 /* Read the length value, which is based on the code and may */
1163 /* read in more values. */
1164 length = INT32_MAX;
1165 if (sjme_error_is(error = sjme_inflate_readLength(inState,
1166 code, &length)) ||
1167 length == INT32_MAX)
1168 return sjme_error_default(error);
1170 /* Read in the distance. */
1171 dist = INT32_MAX;
1172 if (sjme_error_is(error = sjme_inflate_readDistance(inState,
1173 &dist)) || dist == INT32_MAX)
1174 return sjme_error_default(error);
1176 /* Copy from the window. */
1177 if (sjme_error_is(error = sjme_inflate_copyWindow(
1178 inState, (sjme_jint)length, (sjme_jint)dist)))
1179 return sjme_error_default(error);
1182 /* Success! */
1183 return SJME_ERROR_NONE;
1186 sjme_errorCode sjme_inflate_destroy(
1187 sjme_attrInNotNull sjme_inflate* inState)
1189 sjme_errorCode error;
1191 if (inState == NULL)
1192 return SJME_ERROR_NULL_ARGUMENTS;
1194 /* Close the input bit stream. */
1195 if (inState->input != NULL)
1197 if (sjme_error_is(error = sjme_closeable_close(
1198 SJME_AS_CLOSEABLE(inState->input))))
1199 return sjme_error_default(error);
1201 inState->input = NULL;
1204 /* Close the output bit stream. */
1205 if (inState->output != NULL)
1207 if (sjme_error_is(error = sjme_closeable_close(
1208 SJME_AS_CLOSEABLE(inState->output))))
1209 return sjme_error_default(error);
1211 inState->output = NULL;
1214 /* Close the source stream. */
1215 if (inState->source != NULL)
1217 if (sjme_error_is(error = sjme_closeable_close(
1218 SJME_AS_CLOSEABLE(inState->source))))
1219 return sjme_error_default(error);
1221 inState->source = NULL;
1224 /* Destroy the input buffer. */
1225 if (inState->inputBuffer != NULL)
1227 if (sjme_error_is(error = sjme_circleBuffer_destroy(
1228 inState->inputBuffer)))
1229 return sjme_error_default(error);
1231 inState->inputBuffer = NULL;
1234 /* Destroy the output buffer. */
1235 if (inState->outputBuffer != NULL)
1237 if (sjme_error_is(error = sjme_circleBuffer_destroy(
1238 inState->outputBuffer)))
1239 return sjme_error_default(error);
1241 inState->outputBuffer = NULL;
1244 /* Destroy the output window. */
1245 if (inState->window != NULL)
1247 if (sjme_error_is(error = sjme_circleBuffer_destroy(
1248 inState->window)))
1249 return sjme_error_default(error);
1251 inState->window = NULL;
1254 /* Destroy code length tree. */
1255 if (inState->treeCodeLen != NULL)
1257 if (sjme_error_is(error = sjme_traverse_destroy(
1258 SJME_AS_TRAVERSE(inState->treeCodeLen))))
1259 return sjme_error_default(error);
1261 inState->treeCodeLen = NULL;
1264 /* Destroy literal tree. */
1265 if (inState->treeLit != NULL)
1267 if (sjme_error_is(error = sjme_traverse_destroy(
1268 SJME_AS_TRAVERSE(inState->treeLit))))
1269 return sjme_error_default(error);
1271 inState->treeLit = NULL;
1274 /* Destroy distance tree. */
1275 if (inState->treeDist != NULL)
1277 if (sjme_error_is(error = sjme_traverse_destroy(
1278 SJME_AS_TRAVERSE(inState->treeDist))))
1279 return sjme_error_default(error);
1281 inState->treeDist = NULL;
1284 /* Free self. */
1285 sjme_alloc_free(inState);
1287 /* Success! */
1288 return SJME_ERROR_NONE;
1291 sjme_errorCode sjme_inflate_inflate(
1292 sjme_attrInNotNull sjme_inflate* inState,
1293 sjme_attrOutNotNull sjme_jint* readCount,
1294 sjme_attrOutNotNullBuf(length) sjme_buffer outBuf,
1295 sjme_attrInPositiveNonZero sjme_jint length)
1297 sjme_errorCode error;
1298 sjme_jint drainOff;
1300 if (inState == NULL || readCount == NULL || outBuf == NULL)
1301 return SJME_ERROR_NULL_ARGUMENTS;
1303 if (length <= 0)
1304 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
1306 /* If this previously failed, then recover it. */
1307 if (sjme_error_is(inState->failed))
1308 return sjme_error_default(inState->failed);
1310 /* Constant inflation loop. */
1311 drainOff = 0;
1312 for (;;)
1314 /* Can anything be drained? */
1315 if (inState->outputBuffer->ready > 0)
1316 if (sjme_error_is(error = sjme_inflate_drain(inState, &drainOff,
1317 outBuf, length)))
1319 inState->failed = error;
1320 return sjme_error_default(error);
1323 /* Did we actually finish? */
1324 if (inState->step == SJME_INFLATE_STEP_CHECK_BTYPE &&
1325 inState->finalHit)
1326 inState->step = SJME_INFLATE_STEP_FINISHED;
1328 /* Finished? */
1329 if (inState->step == SJME_INFLATE_STEP_FINISHED)
1331 /* There is still data that can be read. */
1332 if (drainOff != 0 || inState->outputBuffer->ready > 0)
1333 break;
1335 /* True EOF */
1336 *readCount = -1;
1337 return SJME_ERROR_NONE;
1340 /* Fill in the input buffer as much as possible, this will */
1341 /* reduce any subsequent disk activity as it is done in bulk. */
1342 if (sjme_error_is(error = sjme_inflate_bitFill(inState)))
1344 inState->failed = error;
1345 return sjme_error_default(error);
1348 /* Check the ready state, stop if not yet ready */
1349 if (sjme_error_is(error = sjme_inflate_bufferSaturation(inState,
1350 1)))
1352 if (error == SJME_ERROR_TOO_SHORT)
1353 continue;
1355 if (error == SJME_ERROR_BUFFER_SATURATED)
1356 break;
1358 inState->failed = error;
1359 return sjme_error_default(error);
1362 /* Which step at inflation are we at? */
1363 error = SJME_ERROR_UNKNOWN;
1364 switch (inState->step)
1366 case SJME_INFLATE_STEP_CHECK_BTYPE:
1367 error = sjme_inflate_stepBType(inState);
1368 break;
1370 case SJME_INFLATE_STEP_LITERAL_DATA:
1371 error = sjme_inflate_stepLiteralData(inState);
1372 break;
1374 case SJME_INFLATE_STEP_LITERAL_SETUP:
1375 error = sjme_inflate_stepLiteralSetup(inState);
1376 break;
1378 case SJME_INFLATE_STEP_DYNAMIC_SETUP:
1379 error = sjme_inflate_stepDynamicSetup(inState);
1380 break;
1382 case SJME_INFLATE_STEP_FIXED_SETUP:
1383 error = sjme_inflate_stepFixedSetup(inState);
1384 break;
1386 case SJME_INFLATE_STEP_INFLATE_FROM_TREE:
1387 error = sjme_inflate_stepInflateFromTree(inState);
1388 break;
1391 /* Did inflation fail? */
1392 if (sjme_error_is(error))
1394 /* Not enough input data, need to fill more! */
1395 if (error == SJME_ERROR_TOO_SHORT)
1396 continue;
1398 /* Or we filled the output buffer as much as possible and */
1399 /* cannot fit anymore. */
1400 if (error == SJME_ERROR_BUFFER_SATURATED)
1401 break;
1403 /* Set as failed. */
1404 inState->failed = error;
1405 return sjme_error_default(error);
1409 /* The amount written is the amount drained. */
1410 *readCount = drainOff;
1411 return SJME_ERROR_NONE;
1414 sjme_errorCode sjme_inflate_new(
1415 sjme_attrInNotNull sjme_alloc_pool* inPool,
1416 sjme_attrInNotNull sjme_inflate** outState,
1417 sjme_attrInNotNull sjme_stream_input source)
1419 sjme_errorCode error;
1420 sjme_inflate* result;
1421 sjme_circleBuffer* inputBuffer;
1422 sjme_circleBuffer* outputBuffer;
1423 sjme_circleBuffer* window;
1424 sjme_bitStream_input input;
1425 sjme_bitStream_output output;
1427 if (inPool == NULL || outState == NULL || source == NULL)
1428 return SJME_ERROR_NULL_ARGUMENTS;
1430 /* Allocate resultant state. */
1431 result = NULL;
1432 if (sjme_error_is(error = sjme_alloc(inPool, sizeof(*result),
1433 (sjme_pointer*)&result)) ||
1434 result == NULL)
1435 goto fail_allocResult;
1437 /* Allocate the input buffer. */
1438 inputBuffer = NULL;
1439 if (sjme_error_is(error = sjme_circleBuffer_new(inPool,
1440 &inputBuffer,
1441 SJME_CIRCLE_BUFFER_QUEUE,
1442 SJME_INFLATE_IO_BUFFER_SIZE)) || inputBuffer == NULL)
1443 goto fail_allocInputBuffer;
1445 /* Allocate the output buffer. */
1446 outputBuffer = NULL;
1447 if (sjme_error_is(error = sjme_circleBuffer_new(inPool,
1448 &outputBuffer,
1449 SJME_CIRCLE_BUFFER_QUEUE,
1450 SJME_INFLATE_IO_BUFFER_SIZE)) || outputBuffer == NULL)
1451 goto fail_allocOutputBuffer;
1453 /* Allocate window. */
1454 window = NULL;
1455 if (sjme_error_is(error = sjme_circleBuffer_new(inPool,
1456 &window,
1457 SJME_CIRCLE_BUFFER_WINDOW,
1458 SJME_INFLATE_WINDOW_SIZE)) || window == NULL)
1459 goto fail_allocWindow;
1461 /* Open input bit stream. */
1462 input = NULL;
1463 if (sjme_error_is(error = sjme_bitStream_inputOpen(inPool,
1464 &input, sjme_inflate_bitRead,
1465 result, NULL)) || input == NULL)
1466 goto fail_openInput;
1468 /* Open output bit stream. */
1469 output = NULL;
1470 if (sjme_error_is(error = sjme_bitStream_outputOpen(inPool,
1471 &output, sjme_inflate_bitWrite,
1472 result, NULL)) || output == NULL)
1473 goto fail_openOutput;
1475 /* Store everything in the result now. */
1476 result->inPool = inPool;
1477 result->failed = SJME_ERROR_NONE;
1478 result->source = source;
1479 result->input = input;
1480 result->inputBuffer = inputBuffer;
1481 result->output = output;
1482 result->outputBuffer = outputBuffer;
1483 result->window = window;
1485 /* Since we hold onto the source, count it up. */
1486 if (sjme_error_is(error = sjme_alloc_weakRef(source, NULL)))
1487 goto fail_countSource;
1489 /* Along with the input... */
1490 if (sjme_error_is(error = sjme_alloc_weakRef(input, NULL)))
1491 goto fail_countSource;
1493 /* and output bit streams! */
1494 if (sjme_error_is(error = sjme_alloc_weakRef(output, NULL)))
1495 goto fail_countSource;
1497 /* Success! */
1498 *outState = result;
1499 return SJME_ERROR_NONE;
1501 fail_countSource:
1502 fail_openOutput:
1503 if (output != NULL)
1504 sjme_closeable_close(SJME_AS_CLOSEABLE(output));
1505 fail_openInput:
1506 if (input != NULL)
1507 sjme_closeable_close(SJME_AS_CLOSEABLE(input));
1508 fail_allocWindow:
1509 if (window != NULL)
1510 sjme_circleBuffer_destroy(window);
1511 fail_allocOutputBuffer:
1512 if (outputBuffer != NULL)
1513 sjme_circleBuffer_destroy(outputBuffer);
1514 fail_allocInputBuffer:
1515 if (inputBuffer != NULL)
1516 sjme_circleBuffer_destroy(inputBuffer);
1517 fail_allocResult:
1518 if (result != NULL)
1519 sjme_alloc_free(result);
1521 return sjme_error_default(error);