MeterLogger
Data Structures | Macros | Enumerations | Functions
heatshrink_decoder.h File Reference
#include <stddef.h>
#include "heatshrink_common.h"
#include "heatshrink_config.h"
Include dependency graph for heatshrink_decoder.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  heatshrink_decoder
 

Macros

#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF)   ((BUF)->input_buffer_size)
 
#define HEATSHRINK_DECODER_WINDOW_BITS(BUF)   ((BUF)->window_sz2)
 
#define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF)   ((BUF)->lookahead_sz2)
 

Enumerations

enum  HSD_sink_res { HSDR_SINK_OK, HSDR_SINK_FULL, HSDR_SINK_ERROR_NULL =-1 }
 
enum  HSD_poll_res { HSDR_POLL_EMPTY, HSDR_POLL_MORE, HSDR_POLL_ERROR_NULL =-1, HSDR_POLL_ERROR_UNKNOWN =-2 }
 
enum  HSD_finish_res { HSDR_FINISH_DONE, HSDR_FINISH_MORE, HSDR_FINISH_ERROR_NULL =-1 }
 

Functions

heatshrink_decoderheatshrink_decoder_alloc (uint16_t input_buffer_size, uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2)
 
void heatshrink_decoder_free (heatshrink_decoder *hsd)
 
void heatshrink_decoder_reset (heatshrink_decoder *hsd)
 
HSD_sink_res heatshrink_decoder_sink (heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size)
 
HSD_poll_res heatshrink_decoder_poll (heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size)
 
HSD_finish_res heatshrink_decoder_finish (heatshrink_decoder *hsd)
 

Macro Definition Documentation

◆ HEATSHRINK_DECODER_INPUT_BUFFER_SIZE

#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE (   BUF)    ((BUF)->input_buffer_size)

◆ HEATSHRINK_DECODER_LOOKAHEAD_BITS

#define HEATSHRINK_DECODER_LOOKAHEAD_BITS (   BUF)    ((BUF)->lookahead_sz2)

Definition at line 32 of file heatshrink_decoder.h.

◆ HEATSHRINK_DECODER_WINDOW_BITS

#define HEATSHRINK_DECODER_WINDOW_BITS (   BUF)    ((BUF)->window_sz2)

Enumeration Type Documentation

◆ HSD_finish_res

Enumerator
HSDR_FINISH_DONE 
HSDR_FINISH_MORE 
HSDR_FINISH_ERROR_NULL 

Definition at line 21 of file heatshrink_decoder.h.

21  {
22  HSDR_FINISH_DONE, /* output is done */
23  HSDR_FINISH_MORE, /* more output remains */
24  HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */
HSD_finish_res

◆ HSD_poll_res

Enumerator
HSDR_POLL_EMPTY 
HSDR_POLL_MORE 
HSDR_POLL_ERROR_NULL 
HSDR_POLL_ERROR_UNKNOWN 

Definition at line 14 of file heatshrink_decoder.h.

14  {
15  HSDR_POLL_EMPTY, /* input exhausted */
16  HSDR_POLL_MORE, /* more data remaining, call again w/ fresh output buffer */
17  HSDR_POLL_ERROR_NULL=-1, /* NULL arguments */
19 } HSD_poll_res;
HSD_poll_res

◆ HSD_sink_res

Enumerator
HSDR_SINK_OK 
HSDR_SINK_FULL 
HSDR_SINK_ERROR_NULL 

Definition at line 8 of file heatshrink_decoder.h.

8  {
9  HSDR_SINK_OK, /* data sunk, ready to poll */
10  HSDR_SINK_FULL, /* out of space in internal buffer */
11  HSDR_SINK_ERROR_NULL=-1, /* NULL argument */
12 } HSD_sink_res;
HSD_sink_res

Function Documentation

◆ heatshrink_decoder_alloc()

heatshrink_decoder* heatshrink_decoder_alloc ( uint16_t  input_buffer_size,
uint8_t  expansion_buffer_sz2,
uint8_t  lookahead_sz2 
)

Definition at line 51 of file heatshrink_decoder.c.

References heatshrink_decoder_reset(), HEATSHRINK_MALLOC, HEATSHRINK_MAX_WINDOW_BITS, HEATSHRINK_MIN_LOOKAHEAD_BITS, HEATSHRINK_MIN_WINDOW_BITS, hsd, heatshrink_decoder::input_buffer_size, LOG, heatshrink_decoder::lookahead_sz2, NULL, and heatshrink_decoder::window_sz2.

Referenced by compress_and_expand_and_check(), data_with_simple_repetition_should_match_with_absurdly_tiny_buffers(), data_without_duplication_should_match_with_absurdly_tiny_buffers(), decode(), decoder_alloc_should_reject_excessively_small_window(), decoder_alloc_should_reject_zero_byte_input_buffer(), decoder_finish_should_note_when_done(), decoder_finish_should_reject_null_input(), decoder_poll_should_expand_short_literal(), decoder_poll_should_expand_short_literal_and_backref(), decoder_poll_should_expand_short_literal_and_backref_when_fed_input_byte_by_byte(), decoder_poll_should_expand_short_self_overlapping_backref(), decoder_poll_should_reject_null_output_buffer(), decoder_poll_should_reject_null_output_size_pointer(), decoder_poll_should_return_empty_if_empty(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_backref_expansion(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_literal_expansion(), decoder_should_not_get_stuck_with_finish_yielding_MORE_but_0_bytes_output_from_poll(), decoder_sink_should_reject_excessively_large_input(), decoder_sink_should_reject_null_count_pointer(), decoder_sink_should_reject_null_input_pointer(), decoder_sink_should_sink_data_when_preconditions_hold(), and espFsOpen().

53  {
54  if ((window_sz2 < HEATSHRINK_MIN_WINDOW_BITS) ||
55  (window_sz2 > HEATSHRINK_MAX_WINDOW_BITS) ||
56  (input_buffer_size == 0) ||
57  (lookahead_sz2 < HEATSHRINK_MIN_LOOKAHEAD_BITS) ||
58  (lookahead_sz2 > window_sz2)) {
59  return NULL;
60  }
61  size_t buffers_sz = (1 << window_sz2) + input_buffer_size;
62  size_t sz = sizeof(heatshrink_decoder) + buffers_sz;
64  if (hsd == NULL) { return NULL; }
65  hsd->input_buffer_size = input_buffer_size;
66  hsd->window_sz2 = window_sz2;
67  hsd->lookahead_sz2 = lookahead_sz2;
69  LOG("-- allocated decoder with buffer size of %zu (%zu + %u + %u)\n",
70  sz, sizeof(heatshrink_decoder), (1 << window_sz2), input_buffer_size);
71  return hsd;
72 }
#define HEATSHRINK_MAX_WINDOW_BITS
#define NULL
Definition: def.h:47
#define HEATSHRINK_MIN_WINDOW_BITS
#define LOG(...)
static heatshrink_decoder hsd
void heatshrink_decoder_reset(heatshrink_decoder *hsd)
#define HEATSHRINK_MALLOC(SZ)
#define HEATSHRINK_MIN_LOOKAHEAD_BITS
Here is the call graph for this function:
Here is the caller graph for this function:

◆ heatshrink_decoder_finish()

HSD_finish_res heatshrink_decoder_finish ( heatshrink_decoder hsd)

Definition at line 354 of file heatshrink_decoder.c.

References HSDR_FINISH_DONE, HSDR_FINISH_ERROR_NULL, HSDR_FINISH_MORE, HSDS_BACKREF_COUNT_LSB, HSDS_BACKREF_COUNT_MSB, HSDS_BACKREF_INDEX_LSB, HSDS_BACKREF_INDEX_MSB, HSDS_EMPTY, HSDS_YIELD_LITERAL, heatshrink_decoder::input_size, NULL, and heatshrink_decoder::state.

Referenced by compress_and_expand_and_check(), decode(), decoder_finish_should_note_when_done(), decoder_finish_should_reject_null_input(), decoder_poll_should_expand_short_literal_and_backref_when_fed_input_byte_by_byte(), decoder_should_not_get_stuck_with_finish_yielding_MORE_but_0_bytes_output_from_poll(), decoder_sink_read(), and espFsRead().

354  {
355  if (hsd == NULL) { return HSDR_FINISH_ERROR_NULL; }
356  switch (hsd->state) {
357  case HSDS_EMPTY:
358  return HSDR_FINISH_DONE;
359 
360  /* If we want to finish with no input, but are in these states, it's
361  * because the 0-bit padding to the last byte looks like a backref
362  * marker bit followed by all 0s for index and count bits. */
367  return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
368 
369  /* If the output stream is padded with 0xFFs (possibly due to being in
370  * flash memory), also explicitly check the input size rather than
371  * uselessly returning MORE but yielding 0 bytes when polling. */
372  case HSDS_YIELD_LITERAL:
373  return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
374 
375  default:
376  return HSDR_FINISH_MORE;
377  }
378 }
#define NULL
Definition: def.h:47
Here is the caller graph for this function:

◆ heatshrink_decoder_free()

void heatshrink_decoder_free ( heatshrink_decoder hsd)

Definition at line 74 of file heatshrink_decoder.c.

References HEATSHRINK_FREE, heatshrink_decoder::input_buffer_size, and heatshrink_decoder::window_sz2.

Referenced by compress_and_expand_and_check(), data_with_simple_repetition_should_match_with_absurdly_tiny_buffers(), data_without_duplication_should_match_with_absurdly_tiny_buffers(), decode(), decoder_finish_should_note_when_done(), decoder_finish_should_reject_null_input(), decoder_poll_should_expand_short_literal(), decoder_poll_should_expand_short_literal_and_backref(), decoder_poll_should_expand_short_literal_and_backref_when_fed_input_byte_by_byte(), decoder_poll_should_expand_short_self_overlapping_backref(), decoder_poll_should_reject_null_output_buffer(), decoder_poll_should_reject_null_output_size_pointer(), decoder_poll_should_return_empty_if_empty(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_backref_expansion(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_literal_expansion(), decoder_should_not_get_stuck_with_finish_yielding_MORE_but_0_bytes_output_from_poll(), decoder_sink_should_reject_excessively_large_input(), decoder_sink_should_reject_null_count_pointer(), decoder_sink_should_reject_null_input_pointer(), decoder_sink_should_sink_data_when_preconditions_hold(), and espFsClose().

74  {
75  size_t buffers_sz = (1 << hsd->window_sz2) + hsd->input_buffer_size;
76  size_t sz = sizeof(heatshrink_decoder) + buffers_sz;
77  HEATSHRINK_FREE(hsd, sz);
78  (void)sz; /* may not be used by free */
79 }
#define HEATSHRINK_FREE(P, SZ)
Here is the caller graph for this function:

◆ heatshrink_decoder_poll()

HSD_poll_res heatshrink_decoder_poll ( heatshrink_decoder hsd,
uint8_t *  out_buf,
size_t  out_buf_size,
size_t output_size 
)

Definition at line 143 of file heatshrink_decoder.c.

References output_info::buf, output_info::buf_size, HSDR_POLL_EMPTY, HSDR_POLL_ERROR_NULL, HSDR_POLL_ERROR_UNKNOWN, HSDR_POLL_MORE, HSDS_BACKREF_COUNT_LSB, HSDS_BACKREF_COUNT_MSB, HSDS_BACKREF_INDEX_LSB, HSDS_BACKREF_INDEX_MSB, HSDS_CHECK_FOR_MORE_INPUT, HSDS_EMPTY, HSDS_INPUT_AVAILABLE, HSDS_YIELD_BACKREF, HSDS_YIELD_LITERAL, heatshrink_decoder::input_size, LOG, NULL, output_info::output_size, st_backref_count_lsb(), st_backref_count_msb(), st_backref_index_lsb(), st_backref_index_msb(), st_check_for_input(), st_input_available(), st_yield_backref(), st_yield_literal(), and heatshrink_decoder::state.

Referenced by compress_and_expand_and_check(), data_with_simple_repetition_should_match_with_absurdly_tiny_buffers(), data_without_duplication_should_match_with_absurdly_tiny_buffers(), decoder_finish_should_note_when_done(), decoder_poll_should_expand_short_literal(), decoder_poll_should_expand_short_literal_and_backref(), decoder_poll_should_expand_short_literal_and_backref_when_fed_input_byte_by_byte(), decoder_poll_should_expand_short_self_overlapping_backref(), decoder_poll_should_reject_null_hsd(), decoder_poll_should_reject_null_output_buffer(), decoder_poll_should_reject_null_output_size_pointer(), decoder_poll_should_return_empty_if_empty(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_backref_expansion(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_literal_expansion(), decoder_should_not_get_stuck_with_finish_yielding_MORE_but_0_bytes_output_from_poll(), decoder_sink_read(), and espFsRead().

144  {
145  if ((hsd == NULL) || (out_buf == NULL) || (output_size == NULL)) {
146  return HSDR_POLL_ERROR_NULL;
147  }
148  *output_size = 0;
149 
150  output_info oi;
151  oi.buf = out_buf;
152  oi.buf_size = out_buf_size;
153  oi.output_size = output_size;
154 
155  while (1) {
156  LOG("-- poll, state is %d (%s), input_size %d\n",
157  hsd->state, state_names[hsd->state], hsd->input_size);
158  uint8_t in_state = hsd->state;
159  switch (in_state) {
160  case HSDS_EMPTY:
161  return HSDR_POLL_EMPTY;
163  hsd->state = st_input_available(hsd);
164  break;
165  case HSDS_YIELD_LITERAL:
166  hsd->state = st_yield_literal(hsd, &oi);
167  break;
169  hsd->state = st_backref_index_msb(hsd);
170  break;
172  hsd->state = st_backref_index_lsb(hsd);
173  break;
175  hsd->state = st_backref_count_msb(hsd);
176  break;
178  hsd->state = st_backref_count_lsb(hsd);
179  break;
180  case HSDS_YIELD_BACKREF:
181  hsd->state = st_yield_backref(hsd, &oi);
182  break;
184  hsd->state = st_check_for_input(hsd);
185  break;
186  default:
188  }
189 
190  /* If the current state cannot advance, check if input or output
191  * buffer are exhausted. */
192  if (hsd->state == in_state) {
193  if (*output_size == out_buf_size) { return HSDR_POLL_MORE; }
194  return HSDR_POLL_EMPTY;
195  }
196  }
197 }
static HSD_state st_yield_literal(heatshrink_decoder *hsd, output_info *oi)
static HSD_state st_yield_backref(heatshrink_decoder *hsd, output_info *oi)
static HSD_state st_backref_count_msb(heatshrink_decoder *hsd)
static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd)
#define NULL
Definition: def.h:47
static HSD_state st_input_available(heatshrink_decoder *hsd)
static HSD_state st_check_for_input(heatshrink_decoder *hsd)
#define LOG(...)
static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd)
static HSD_state st_backref_index_msb(heatshrink_decoder *hsd)
size_t * output_size
Here is the call graph for this function:
Here is the caller graph for this function:

◆ heatshrink_decoder_reset()

void heatshrink_decoder_reset ( heatshrink_decoder hsd)

Definition at line 82 of file heatshrink_decoder.c.

References heatshrink_decoder::bit_accumulator, heatshrink_decoder::bit_index, heatshrink_decoder::buffers, heatshrink_decoder::current_byte, heatshrink_decoder::head_index, HEATSHRINK_DECODER_INPUT_BUFFER_SIZE, HEATSHRINK_DECODER_WINDOW_BITS, HSDS_EMPTY, heatshrink_decoder::input_index, heatshrink_decoder::input_size, memset, heatshrink_decoder::output_count, heatshrink_decoder::output_index, and heatshrink_decoder::state.

Referenced by compress_and_expand_and_check(), decoder_should_not_get_stuck_with_finish_yielding_MORE_but_0_bytes_output_from_poll(), and heatshrink_decoder_alloc().

82  {
83  size_t buf_sz = 1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd);
84  size_t input_sz = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd);
85  memset(hsd->buffers, 0, buf_sz + input_sz);
86  hsd->state = HSDS_EMPTY;
87  hsd->input_size = 0;
88  hsd->input_index = 0;
89  hsd->bit_index = 0x00;
90  hsd->current_byte = 0x00;
91  hsd->output_count = 0;
92  hsd->output_index = 0;
93  hsd->head_index = 0;
94  hsd->bit_accumulator = 0x00000000;
95 }
#define memset(x, a, b)
Definition: platform.h:21
#define HEATSHRINK_DECODER_WINDOW_BITS(BUF)
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF)
Here is the caller graph for this function:

◆ heatshrink_decoder_sink()

HSD_sink_res heatshrink_decoder_sink ( heatshrink_decoder hsd,
uint8_t *  in_buf,
size_t  size,
size_t input_size 
)

Definition at line 98 of file heatshrink_decoder.c.

References heatshrink_decoder::buffers, HEATSHRINK_DECODER_INPUT_BUFFER_SIZE, HSDR_SINK_ERROR_NULL, HSDR_SINK_FULL, HSDR_SINK_OK, HSDS_EMPTY, HSDS_INPUT_AVAILABLE, heatshrink_decoder::input_index, heatshrink_decoder::input_size, LOG, memcpy, NULL, and heatshrink_decoder::state.

Referenced by compress_and_expand_and_check(), data_with_simple_repetition_should_match_with_absurdly_tiny_buffers(), data_without_duplication_should_match_with_absurdly_tiny_buffers(), decoder_finish_should_note_when_done(), decoder_poll_should_expand_short_literal(), decoder_poll_should_expand_short_literal_and_backref(), decoder_poll_should_expand_short_literal_and_backref_when_fed_input_byte_by_byte(), decoder_poll_should_expand_short_self_overlapping_backref(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_backref_expansion(), decoder_poll_should_suspend_if_out_of_space_in_output_buffer_during_literal_expansion(), decoder_should_not_get_stuck_with_finish_yielding_MORE_but_0_bytes_output_from_poll(), decoder_sink_read(), decoder_sink_should_reject_excessively_large_input(), decoder_sink_should_reject_null_count_pointer(), decoder_sink_should_reject_null_hsd_pointer(), decoder_sink_should_reject_null_input_pointer(), decoder_sink_should_sink_data_when_preconditions_hold(), and espFsRead().

99  {
100  if ((hsd == NULL) || (in_buf == NULL) || (input_size == NULL)) {
101  return HSDR_SINK_ERROR_NULL;
102  }
103 
104  size_t rem = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd) - hsd->input_size;
105  if (rem == 0) {
106  *input_size = 0;
107  return HSDR_SINK_FULL;
108  }
109 
110  size = rem < size ? rem : size;
111  LOG("-- sinking %zd bytes\n", size);
112  /* copy into input buffer (at head of buffers) */
113  memcpy(&hsd->buffers[hsd->input_size], in_buf, size);
114  hsd->input_size += size;
115  if (hsd->state == HSDS_EMPTY) {
117  hsd->input_index = 0;
118  }
119  *input_size = size;
120  return HSDR_SINK_OK;
121 }
#define NULL
Definition: def.h:47
#define LOG(...)
#define memcpy(x, a, b)
Definition: platform.h:22
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF)
Here is the caller graph for this function: