1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 // -------------------------------------------------------------------------*/
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 \
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,
27 static sjme_errorCode
sjme_inflate_bitFill(
28 sjme_attrInNotNull sjme_inflate
* inState
)
31 sjme_jint avail
, readCount
;
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? */
43 if (sjme_error_is(error
= sjme_circleBuffer_available(
44 inState
->inputBuffer
, &avail
)) ||
46 return sjme_error_default(error
);
48 /* It is as full as it can get. */
50 return SJME_ERROR_NONE
;
52 /* Setup target buffer to read into. */
53 buffer
= sjme_alloca(avail
);
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
);
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(
76 SJME_CIRCLE_BUFFER_TAIL
)))
77 return sjme_error_default(error
);
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
)
91 sjme_inflate
* inState
;
94 inState
= functionData
;
95 if (inStream
== NULL
|| inState
== NULL
|| readCount
== NULL
||
97 return SJME_ERROR_NULL_ARGUMENTS
;
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? */
108 if (sjme_error_is(error
= sjme_circleBuffer_stored(
109 inState
->inputBuffer
, &limit
)) ||
111 return sjme_error_default(error
);
113 /* If this is zero then we do not have enough to read from. */
116 /* However if EOF was hit, we stop. */
117 if (inState
->inputEof
)
120 return SJME_ERROR_NONE
;
123 /* Otherwise, we need more data! */
124 return SJME_ERROR_TOO_SHORT
;
127 /* Do not exceed the storage limit. */
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
);
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
;
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)
172 sjme_message_hexDump(writeBuf
, length
);
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
;
187 return SJME_ERROR_NULL_ARGUMENTS
;
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? */
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
;
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
);
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
,
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
,
248 return sjme_error_default(error
);
251 if ((++at
) >= maxLen
)
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
;
272 return SJME_ERROR_INDEX_OUT_OF_BOUNDS
;
274 /* How much is in the output buffer? */
276 if (sjme_error_is(error
= sjme_circleBuffer_stored(
277 inState
->outputBuffer
, &stored
)) ||
279 return sjme_error_default(error
);
281 /* How much can be drained? */
282 limit
= length
- (*drainOff
);
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
;
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
;
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. */
328 if (sjme_error_is(error
= sjme_bitStream_inputRead(
330 SJME_BITSTREAM_LSB
, &bit
, 1)) ||
332 return sjme_error_default(error
);
334 /* Traverse in the tree. */
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. */
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
;
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. */
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. */
397 /* Cannot be the first entry. */
399 return SJME_ERROR_INFLATE_INVALID_CODE_LENGTH
;
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
);
412 /* Repeat zero for 3-10 times */
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
);
426 /* Repeat zero for 11-138 times */
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
);
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
;
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];
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
;
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
,
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
]]++;
498 /* Find the numerical value of the smallest code for each code length. */
500 for (i
= 1; i
<= SJME_INFLATE_CODE_LEN_MAX_BITS
; i
++)
502 code
= (code
+ blCount
[i
- 1]) << 1;
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 */
518 nextCodeLen
= (nextCode
[len
])++;
520 #if defined(SJME_CONFIG_DEBUG) && defined(SJME_CONFIG_VERBOSE_INFLATE)
521 sjme_util_intToBinary(
522 binary
, sizeof(binary
) - 1,
524 sjme_message("Tree -> %s = %d",
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
,
534 return sjme_error_default(error
);
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
;
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
,
555 return sjme_error_default(error
);
559 if (sjme_error_is(error
= sjme_inflate_dynamicBitIn(
560 inState
, inState
->treeLit
, &code
)) ||
562 return sjme_error_default(error
);
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
;
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
,
582 return sjme_error_default(error
);
586 if (sjme_error_is(error
= sjme_inflate_dynamicBitIn(
587 inState
, inState
->treeDist
, &code
)) ||
589 return sjme_error_default(error
);
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
,
609 return sjme_error_default(error
);
611 /* Read in the first, or only, seven bits. */
613 if (sjme_error_is(error
= sjme_bitStream_inputRead(
615 SJME_BITSTREAM_MSB
, &nine
, 7)) ||
617 return sjme_error_default(error
);
619 /* Make this nine bits for consistency. */
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)
637 /* 0 - 143 / 00110000. - 10111111. */
638 else if (nine
>= 0x060 && nine
<= 0x17F)
645 /* 280 - 287 / 11000000. - 11000111. */
646 else if (nine
>= 0x180 && nine
<= 0x18F)
653 /* 144 - 255 / 110010000 - 111111111 */
654 else if (nine
>= 0x190 && nine
<= 0x1FF)
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. */
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
));
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
;
693 if (inState
== NULL
|| outDist
== NULL
)
694 return SJME_ERROR_NULL_ARGUMENTS
;
696 /* Fixed huffman distance codes are always 5 bits. */
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
);
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. */
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. */
726 return SJME_ERROR_INFLATE_INVALID_CODE
;
728 /* Distances always start at 1. */
731 /* Determine if any bits repeat. */
732 for (i
= 0; i
< code
; i
++)
734 /* Too short to repeat? */
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? */
749 /* How many bits to read? */
750 group
= (code
/ 2) - 1;
752 /* Read in those extra bits. */
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. */
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. */
787 return SJME_ERROR_NONE
;
790 /* Resultant length always starts at three. */
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. */
803 /* Code lengths are in groups of 4, which get shifted up by their */
805 group
= ((i
/ 4) - 1);
806 result
+= (1 << group
);
809 /* Do we need to read in any extra bits to the length value? */
812 /* How many bits to read? */
813 group
= (base
/ 4) - 1;
815 /* Read in those extra bits. */
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. */
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
;
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(
850 return sjme_error_default(error
);
852 /* Read in final block flag. */
854 if (sjme_error_is(error
= sjme_bitStream_inputRead(
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(
864 SJME_BITSTREAM_LSB
, &blockType
, 2)) ||
865 blockType
== INT32_MAX
)
866 return sjme_error_default(error
);
870 return SJME_ERROR_INFLATE_INVALID_BTYPE
;
872 /* Was the final block hit? */
874 inState
->finalHit
= SJME_JNI_TRUE
;
876 /* Which step to transition to? */
878 inState
->step
= SJME_INFLATE_STEP_LITERAL_SETUP
;
879 else if (blockType
== 1)
880 inState
->step
= SJME_INFLATE_STEP_FIXED_SETUP
;
882 inState
->step
= SJME_INFLATE_STEP_DYNAMIC_SETUP
;
885 return SJME_ERROR_NONE
;
888 sjme_errorCode
sjme_inflate_stepLiteralData(
889 sjme_attrInNotNull sjme_inflate
* inState
)
891 sjme_errorCode error
;
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
;
903 /* Can we actually read a byte? */
904 if (sjme_error_is(error
= sjme_inflate_bufferSaturation(
910 if (sjme_error_is(error
= sjme_bitStream_inputRead(
912 SJME_BITSTREAM_LSB
, &raw
, 8)) ||
916 /* Write out bits. */
917 if (sjme_error_is(error
= sjme_bitStream_outputWrite(
919 SJME_BITSTREAM_LSB
, raw
, 8)))
922 /* We consumed a byte. */
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. */
931 inState
->step
= SJME_INFLATE_STEP_CHECK_BTYPE
;
934 return SJME_ERROR_NONE
;
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
;
952 return SJME_ERROR_NULL_ARGUMENTS
;
954 /* Literal uncompressed data starts at the byte boundary. */
955 if (sjme_error_is(error
= sjme_bitStream_inputAlign(
958 return sjme_error_default(error
);
960 /* Can we actually read the lengths? */
961 if (sjme_error_is(error
= sjme_inflate_bufferSaturation(
963 return sjme_error_default(error
);
965 /* Read both the length and its complement. */
968 if (sjme_error_is(error
= sjme_bitStream_inputRead(
969 inState
->input
, SJME_BITSTREAM_LSB
,
971 return sjme_error_default(error
);
972 if (sjme_error_is(error
= sjme_bitStream_inputRead(
973 inState
->input
, SJME_BITSTREAM_LSB
,
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
;
987 return SJME_ERROR_NONE
;
990 static sjme_errorCode
sjme_inflate_stepDynamicSetup(
991 sjme_attrInNotNull sjme_inflate
* inState
)
993 sjme_errorCode error
;
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. */
1005 if (sjme_error_is(error
= sjme_bitStream_inputRead(
1006 inState
->input
, SJME_BITSTREAM_LSB
,
1009 return sjme_error_default(error
);
1011 /* Read in distance count. */
1013 if (sjme_error_is(error
= sjme_bitStream_inputRead(
1014 inState
->input
, SJME_BITSTREAM_LSB
,
1017 return sjme_error_default(error
);
1019 /* Read in code length count. */
1021 if (sjme_error_is(error
= sjme_bitStream_inputRead(
1022 inState
->input
, SJME_BITSTREAM_LSB
,
1025 return sjme_error_default(error
);
1027 /* Determine their actual values. */
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. */
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
,
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
,
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
,
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. */
1132 /* Read in next code. */
1134 if (sjme_error_is(error
= inState
->sub
.huffman
.readCode(inState
,
1135 &code
)) || code
== INT32_MAX
)
1136 return sjme_error_default(error
);
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
;
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
,
1157 return sjme_error_default(error
);
1162 /* Read the length value, which is based on the code and may */
1163 /* read in more values. */
1165 if (sjme_error_is(error
= sjme_inflate_readLength(inState
,
1167 length
== INT32_MAX
)
1168 return sjme_error_default(error
);
1170 /* Read in the distance. */
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
);
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(
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
;
1285 sjme_alloc_free(inState
);
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
;
1300 if (inState
== NULL
|| readCount
== NULL
|| outBuf
== NULL
)
1301 return SJME_ERROR_NULL_ARGUMENTS
;
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. */
1314 /* Can anything be drained? */
1315 if (inState
->outputBuffer
->ready
> 0)
1316 if (sjme_error_is(error
= sjme_inflate_drain(inState
, &drainOff
,
1319 inState
->failed
= error
;
1320 return sjme_error_default(error
);
1323 /* Did we actually finish? */
1324 if (inState
->step
== SJME_INFLATE_STEP_CHECK_BTYPE
&&
1326 inState
->step
= SJME_INFLATE_STEP_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)
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
,
1352 if (error
== SJME_ERROR_TOO_SHORT
)
1355 if (error
== SJME_ERROR_BUFFER_SATURATED
)
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
);
1370 case SJME_INFLATE_STEP_LITERAL_DATA
:
1371 error
= sjme_inflate_stepLiteralData(inState
);
1374 case SJME_INFLATE_STEP_LITERAL_SETUP
:
1375 error
= sjme_inflate_stepLiteralSetup(inState
);
1378 case SJME_INFLATE_STEP_DYNAMIC_SETUP
:
1379 error
= sjme_inflate_stepDynamicSetup(inState
);
1382 case SJME_INFLATE_STEP_FIXED_SETUP
:
1383 error
= sjme_inflate_stepFixedSetup(inState
);
1386 case SJME_INFLATE_STEP_INFLATE_FROM_TREE
:
1387 error
= sjme_inflate_stepInflateFromTree(inState
);
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
)
1398 /* Or we filled the output buffer as much as possible and */
1399 /* cannot fit anymore. */
1400 if (error
== SJME_ERROR_BUFFER_SATURATED
)
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. */
1432 if (sjme_error_is(error
= sjme_alloc(inPool
, sizeof(*result
),
1433 (sjme_pointer
*)&result
)) ||
1435 goto fail_allocResult
;
1437 /* Allocate the input buffer. */
1439 if (sjme_error_is(error
= sjme_circleBuffer_new(inPool
,
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
,
1449 SJME_CIRCLE_BUFFER_QUEUE
,
1450 SJME_INFLATE_IO_BUFFER_SIZE
)) || outputBuffer
== NULL
)
1451 goto fail_allocOutputBuffer
;
1453 /* Allocate window. */
1455 if (sjme_error_is(error
= sjme_circleBuffer_new(inPool
,
1457 SJME_CIRCLE_BUFFER_WINDOW
,
1458 SJME_INFLATE_WINDOW_SIZE
)) || window
== NULL
)
1459 goto fail_allocWindow
;
1461 /* Open input bit stream. */
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. */
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
;
1499 return SJME_ERROR_NONE
;
1504 sjme_closeable_close(SJME_AS_CLOSEABLE(output
));
1507 sjme_closeable_close(SJME_AS_CLOSEABLE(input
));
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
);
1519 sjme_alloc_free(result
);
1521 return sjme_error_default(error
);