18 #if HEATSHRINK_DEBUGGING_LOGS 22 #define LOG(...) fprintf(stderr, __VA_ARGS__) 23 #define ASSERT(X) assert(X) 24 static const char *state_names[] = {
31 "check_for_more_input",
44 #define NO_BITS ((uint32_t)-1) 50 #if HEATSHRINK_DYNAMIC_ALLOC 53 uint8_t lookahead_sz2) {
56 (input_buffer_size == 0) ||
58 (lookahead_sz2 > window_sz2)) {
61 size_t buffers_sz = (1 << window_sz2) + input_buffer_size;
69 LOG(
"-- allocated decoder with buffer size of %zu (%zu + %u + %u)\n",
99 uint8_t *in_buf,
size_t size,
size_t *input_size) {
100 if ((hsd ==
NULL) || (in_buf ==
NULL) || (input_size ==
NULL)) {
110 size = rem < size ? rem : size;
111 LOG(
"-- sinking %zd bytes\n", size);
128 #define BACKREF_COUNT_BITS(HSD) (HEATSHRINK_DECODER_LOOKAHEAD_BITS(HSD)) 129 #define BACKREF_INDEX_BITS(HSD) (HEATSHRINK_DECODER_WINDOW_BITS(HSD)) 144 uint8_t *out_buf,
size_t out_buf_size,
size_t *output_size) {
145 if ((hsd ==
NULL) || (out_buf ==
NULL) || (output_size ==
NULL)) {
156 LOG(
"-- poll, state is %d (%s), input_size %d\n",
158 uint8_t in_state = hsd->
state;
192 if (hsd->
state == in_state) {
221 uint8_t c = byte & 0xFF;
222 LOG(
"-- emitting literal byte 0x%02x ('%c')\n", c,
isprint(c) ? c :
'.');
234 uint32_t bits =
get_bits(hsd, bit_ct - 8);
235 LOG(
"-- backref index (msb), got 0x%04x (+1)\n", bits);
243 uint32_t bits =
get_bits(hsd, bit_ct < 8 ? bit_ct : 8);
244 LOG(
"-- backref index (lsb), got 0x%04x (+1)\n", bits);
256 uint32_t bits =
get_bits(hsd, br_bit_ct - 8);
257 LOG(
"-- backref count (msb), got 0x%04x (+1)\n", bits);
265 uint32_t bits =
get_bits(hsd, br_bit_ct < 8 ? br_bit_ct : 8);
266 LOG(
"-- backref count (lsb), got 0x%04x (+1)\n", bits);
282 LOG(
"-- emitting %zu bytes from -%u bytes back\n", count, neg_offset);
283 ASSERT(neg_offset < mask + 1);
286 for (i=0; i<count; i++) {
287 uint8_t c = buf[(hsd->
head_index - neg_offset) & mask];
291 LOG(
" -- ++ 0x%02x\n", c);
307 if (count > 31) {
return NO_BITS; }
308 LOG(
"-- popping %u bit(s)\n", count);
316 for (i = 0; i < count; i++) {
319 LOG(
" -- out of bits, suspending w/ accumulator of %u (0x%02x)\n",
335 LOG(
" -- got 1, accumulator 0x%04x, bit_index 0x%02x\n",
340 LOG(
" -- got 0, accumulator 0x%04x, bit_index 0x%02x\n",
350 if (count > 1) {
LOG(
" -- accumulated %08x\n", res); }
356 switch (hsd->
state) {
381 LOG(
" -- pushing byte: 0x%02x ('%c')\n", byte,
isprint(byte) ? byte :
'.');
static HSD_state st_yield_literal(heatshrink_decoder *hsd, output_info *oi)
#define HEATSHRINK_FREE(P, SZ)
static HSD_state st_yield_backref(heatshrink_decoder *hsd, output_info *oi)
HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size)
static HSD_state st_backref_count_msb(heatshrink_decoder *hsd)
#define HEATSHRINK_MAX_WINDOW_BITS
static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd)
static void push_byte(heatshrink_decoder *hsd, output_info *oi, uint8_t byte)
#define HEATSHRINK_DECODER_WINDOW_BITS(BUF)
#define HEATSHRINK_MIN_WINDOW_BITS
heatshrink_decoder * heatshrink_decoder_alloc(uint16_t input_buffer_size, uint8_t window_sz2, uint8_t lookahead_sz2)
static HSD_state st_input_available(heatshrink_decoder *hsd)
static HSD_state st_check_for_input(heatshrink_decoder *hsd)
static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd)
void heatshrink_decoder_free(heatshrink_decoder *hsd)
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd)
#define BACKREF_COUNT_BITS(HSD)
static heatshrink_decoder hsd
void heatshrink_decoder_reset(heatshrink_decoder *hsd)
static HSD_state st_backref_index_msb(heatshrink_decoder *hsd)
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size)
static uint32_t get_bits(heatshrink_decoder *hsd, uint8_t count)
#define HEATSHRINK_MALLOC(SZ)
#define BACKREF_INDEX_BITS(HSD)
uint16_t input_buffer_size
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF)
#define HEATSHRINK_MIN_LOOKAHEAD_BITS