Prusa MINI Firmware overview
heatshrink_decoder.h File Reference
#include <stdint.h>
#include <stddef.h>
#include "heatshrink_common.h"
#include "heatshrink_config.h"

Go to the source code of this file.

Classes

struct  heatshrink_decoder
 

Macros

#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)   HEATSHRINK_STATIC_INPUT_BUFFER_SIZE
 
#define HEATSHRINK_DECODER_WINDOW_BITS(_)   (HEATSHRINK_STATIC_WINDOW_BITS)
 
#define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF)   (HEATSHRINK_STATIC_LOOKAHEAD_BITS)
 

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

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 (   _)    HEATSHRINK_STATIC_INPUT_BUFFER_SIZE

◆ HEATSHRINK_DECODER_WINDOW_BITS

#define HEATSHRINK_DECODER_WINDOW_BITS (   _)    (HEATSHRINK_STATIC_WINDOW_BITS)

◆ HEATSHRINK_DECODER_LOOKAHEAD_BITS

#define HEATSHRINK_DECODER_LOOKAHEAD_BITS (   BUF)    (HEATSHRINK_STATIC_LOOKAHEAD_BITS)

Enumeration Type Documentation

◆ HSD_sink_res

libs/heatshrink/heatshrink_decoder.h

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

◆ HSD_poll_res

Enumerator
HSDR_POLL_EMPTY 
HSDR_POLL_MORE 
HSDR_POLL_ERROR_NULL 
HSDR_POLL_ERROR_UNKNOWN 
17  {
18  HSDR_POLL_EMPTY, /* input exhausted */
19  HSDR_POLL_MORE, /* more data remaining, call again w/ fresh output buffer */
20  HSDR_POLL_ERROR_NULL=-1, /* NULL arguments */
22 } HSD_poll_res;

◆ HSD_finish_res

Enumerator
HSDR_FINISH_DONE 
HSDR_FINISH_MORE 
HSDR_FINISH_ERROR_NULL 
24  {
25  HSDR_FINISH_DONE, /* output is done */
26  HSDR_FINISH_MORE, /* more output remains */
27  HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */

Function Documentation

◆ heatshrink_decoder_reset()

void heatshrink_decoder_reset ( heatshrink_decoder hsd)
83  {
84  size_t buf_sz = 1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd);
85  size_t input_sz = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd);
86  memset(hsd->buffers, 0, buf_sz + input_sz);
88  hsd->input_size = 0;
89  hsd->input_index = 0;
90  hsd->bit_index = 0x00;
91  hsd->current_byte = 0x00;
92  hsd->output_count = 0;
93  hsd->output_index = 0;
94  hsd->head_index = 0;
95 }

◆ heatshrink_decoder_sink()

HSD_sink_res heatshrink_decoder_sink ( heatshrink_decoder hsd,
uint8_t in_buf,
size_t  size,
size_t *  input_size 
)
99  {
100  if (hsd == nullptr || in_buf == nullptr || input_size == nullptr)
101  return HSDR_SINK_ERROR_NULL;
102 
104  if (rem == 0) {
105  *input_size = 0;
106  return HSDR_SINK_FULL;
107  }
108 
109  size = rem < size ? rem : size;
110  LOG("-- sinking %zd bytes\n", size);
111  /* copy into input buffer (at head of buffers) */
112  memcpy(&hsd->buffers[hsd->input_size], in_buf, size);
113  hsd->input_size += size;
114  *input_size = size;
115  return HSDR_SINK_OK;
116 }

◆ 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 
)
135  {
136  if (hsd == nullptr || out_buf == nullptr || output_size == nullptr)
137  return HSDR_POLL_ERROR_NULL;
138 
139  *output_size = 0;
140 
141  output_info oi;
142  oi.buf = out_buf;
143  oi.buf_size = out_buf_size;
144  oi.output_size = output_size;
145 
146  while (1) {
147  LOG("-- poll, state is %d (%s), input_size %d\n", hsd->state, state_names[hsd->state], hsd->input_size);
148  uint8_t in_state = hsd->state;
149  switch (in_state) {
150  case HSDS_TAG_BIT:
151  hsd->state = st_tag_bit(hsd);
152  break;
153  case HSDS_YIELD_LITERAL:
154  hsd->state = st_yield_literal(hsd, &oi);
155  break;
158  break;
161  break;
164  break;
167  break;
168  case HSDS_YIELD_BACKREF:
169  hsd->state = st_yield_backref(hsd, &oi);
170  break;
171  default:
173  }
174 
175  // If the current state cannot advance, check if input or output
176  // buffer are exhausted.
177  if (hsd->state == in_state)
178  return (*output_size == out_buf_size) ? HSDR_POLL_MORE : HSDR_POLL_EMPTY;
179  }
Here is the call graph for this function:

◆ heatshrink_decoder_finish()

HSD_finish_res heatshrink_decoder_finish ( heatshrink_decoder hsd)
326  {
327  if (hsd == nullptr) { return HSDR_FINISH_ERROR_NULL; }
328  switch (hsd->state) {
329  case HSDS_TAG_BIT:
331 
332  /* If we want to finish with no input, but are in these states, it's
333  * because the 0-bit padding to the last byte looks like a backref
334  * marker bit followed by all 0s for index and count bits. */
340 
341  /* If the output stream is padded with 0xFFs (possibly due to being in
342  * flash memory), also explicitly check the input size rather than
343  * uselessly returning MORE but yielding 0 bytes when polling. */
344  case HSDS_YIELD_LITERAL:
346 
347  default: return HSDR_FINISH_MORE;
348  }
HEATSHRINK_DECODER_INPUT_BUFFER_SIZE
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)
Definition: heatshrink_decoder.h:38
output_info::buf_size
size_t buf_size
Definition: heatshrink_decoder.cpp:43
HSDR_FINISH_MORE
Definition: heatshrink_decoder.h:26
HSDR_POLL_ERROR_UNKNOWN
Definition: heatshrink_decoder.h:21
HSDR_SINK_OK
Definition: heatshrink_decoder.h:12
heatshrink_decoder::output_index
uint16_t output_index
Definition: heatshrink_decoder.h:50
HSDR_POLL_EMPTY
Definition: heatshrink_decoder.h:18
st_backref_index_msb
static HSD_state st_backref_index_msb(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:213
LOG
#define LOG(...)
Definition: heatshrink_decoder.cpp:37
HSDR_SINK_FULL
Definition: heatshrink_decoder.h:13
heatshrink_decoder::buffers
uint8_t buffers[(1<< HEATSHRINK_DECODER_WINDOW_BITS(_))+HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)]
Definition: heatshrink_decoder.h:66
heatshrink_decoder::state
uint8_t state
Definition: heatshrink_decoder.h:52
HSDR_POLL_MORE
Definition: heatshrink_decoder.h:19
HSDR_FINISH_ERROR_NULL
Definition: heatshrink_decoder.h:27
st_tag_bit
static HSD_state st_tag_bit(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:181
HSD_poll_res
HSD_poll_res
Definition: heatshrink_decoder.h:17
st_yield_literal
static HSD_state st_yield_literal(heatshrink_decoder *hsd, output_info *oi)
Definition: heatshrink_decoder.cpp:195
HSDR_FINISH_DONE
Definition: heatshrink_decoder.h:25
st_backref_index_lsb
static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:223
heatshrink_decoder::bit_index
uint8_t bit_index
Definition: heatshrink_decoder.h:54
HSDS_BACKREF_COUNT_MSB
Definition: heatshrink_decoder.cpp:16
HSDS_YIELD_BACKREF
Definition: heatshrink_decoder.cpp:18
output_info::output_size
size_t * output_size
Definition: heatshrink_decoder.cpp:44
HSDS_BACKREF_INDEX_LSB
Definition: heatshrink_decoder.cpp:15
HSD_sink_res
HSD_sink_res
Definition: heatshrink_decoder.h:11
st_yield_backref
static HSD_state st_yield_backref(heatshrink_decoder *hsd, output_info *oi)
Definition: heatshrink_decoder.cpp:255
heatshrink_decoder::head_index
uint16_t head_index
Definition: heatshrink_decoder.h:51
heatshrink_decoder::input_size
uint16_t input_size
Definition: heatshrink_decoder.h:47
heatshrink_decoder::output_count
uint16_t output_count
Definition: heatshrink_decoder.h:49
uint8_t
const uint8_t[]
Definition: 404_html.c:3
HSDS_BACKREF_INDEX_MSB
Definition: heatshrink_decoder.cpp:14
heatshrink_decoder::current_byte
uint8_t current_byte
Definition: heatshrink_decoder.h:53
HEATSHRINK_DECODER_WINDOW_BITS
#define HEATSHRINK_DECODER_WINDOW_BITS(_)
Definition: heatshrink_decoder.h:40
output_info
Definition: heatshrink_decoder.cpp:41
hsd
static heatshrink_decoder hsd
Definition: binary_protocol.h:53
heatshrink_decoder::input_index
uint16_t input_index
Definition: heatshrink_decoder.h:48
st_backref_count_msb
static HSD_state st_backref_count_msb(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:235
HSDS_TAG_BIT
Definition: heatshrink_decoder.cpp:12
output_info::buf
uint8_t * buf
Definition: heatshrink_decoder.cpp:42
st_backref_count_lsb
static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:245
HSDS_BACKREF_COUNT_LSB
Definition: heatshrink_decoder.cpp:17
HSD_finish_res
HSD_finish_res
Definition: heatshrink_decoder.h:24
HSDR_POLL_ERROR_NULL
Definition: heatshrink_decoder.h:20
HSDS_YIELD_LITERAL
Definition: heatshrink_decoder.cpp:13
HSDR_SINK_ERROR_NULL
Definition: heatshrink_decoder.h:14
size
static png_bytep size_t size
Definition: pngwrite.c:2170