Prusa MINI Firmware overview
heatshrink_decoder.h
Go to the documentation of this file.
1 /**
2  * libs/heatshrink/heatshrink_decoder.h
3  */
4 #pragma once
5 
6 #include <stdint.h>
7 #include <stddef.h>
8 #include "heatshrink_common.h"
9 #include "heatshrink_config.h"
10 
11 typedef enum {
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;
16 
17 typedef enum {
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;
23 
24 typedef enum {
25  HSDR_FINISH_DONE, /* output is done */
26  HSDR_FINISH_MORE, /* more output remains */
27  HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */
29 
30 #if HEATSHRINK_DYNAMIC_ALLOC
31 #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF) \
32  ((BUF)->input_buffer_size)
33 #define HEATSHRINK_DECODER_WINDOW_BITS(BUF) \
34  ((BUF)->window_sz2)
35 #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
36  ((BUF)->lookahead_sz2)
37 #else
38 #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_) \
39  HEATSHRINK_STATIC_INPUT_BUFFER_SIZE
40 #define HEATSHRINK_DECODER_WINDOW_BITS(_) \
41  (HEATSHRINK_STATIC_WINDOW_BITS)
42 #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
43  (HEATSHRINK_STATIC_LOOKAHEAD_BITS)
44 #endif
45 
46 typedef struct {
47  uint16_t input_size; /* bytes in input buffer */
48  uint16_t input_index; /* offset to next unprocessed input byte */
49  uint16_t output_count; /* how many bytes to output */
50  uint16_t output_index; /* index for bytes to output */
51  uint16_t head_index; /* head of window buffer */
52  uint8_t state; /* current state machine node */
53  uint8_t current_byte; /* current byte of input */
54  uint8_t bit_index; /* current bit index */
55 
56 #if HEATSHRINK_DYNAMIC_ALLOC
57  /* Fields that are only used if dynamically allocated. */
58  uint8_t window_sz2; /* window buffer bits */
59  uint8_t lookahead_sz2; /* lookahead bits */
60  uint16_t input_buffer_size; /* input buffer size */
61 
62  /* Input buffer, then expansion window buffer */
63  uint8_t buffers[];
64 #else
65  /* Input buffer, then expansion window buffer */
67 #endif
69 
70 #if HEATSHRINK_DYNAMIC_ALLOC
71 /* Allocate a decoder with an input buffer of INPUT_BUFFER_SIZE bytes,
72  * an expansion buffer size of 2^WINDOW_SZ2, and a lookahead
73  * size of 2^lookahead_sz2. (The window buffer and lookahead sizes
74  * must match the settings used when the data was compressed.)
75  * Returns NULL on error. */
76 heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size, uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2);
77 
78 /* Free a decoder. */
79 void heatshrink_decoder_free(heatshrink_decoder *hsd);
80 #endif
81 
82 /* Reset a decoder. */
84 
85 /* Sink at most SIZE bytes from IN_BUF into the decoder. *INPUT_SIZE is set to
86  * indicate how many bytes were actually sunk (in case a buffer was filled). */
87 HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size);
88 
89 /* Poll for output from the decoder, copying at most OUT_BUF_SIZE bytes into
90  * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */
91 HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size);
92 
93 /* Notify the dencoder that the input stream is finished.
94  * If the return value is HSDR_FINISH_MORE, there is still more output, so
95  * call heatshrink_decoder_poll and repeat. */
HEATSHRINK_DECODER_INPUT_BUFFER_SIZE
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)
Definition: heatshrink_decoder.h:38
heatshrink_decoder_reset
void heatshrink_decoder_reset(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:83
output_info::buf_size
size_t buf_size
Definition: heatshrink_decoder.cpp:43
heatshrink_config.h
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
BACKREF_COUNT_BITS
#define BACKREF_COUNT_BITS(HSD)
Definition: heatshrink_decoder.cpp:122
heatshrink_decoder::output_index
uint16_t output_index
Definition: heatshrink_decoder.h:50
heatshrink_decoder_finish
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:325
HSDR_POLL_EMPTY
Definition: heatshrink_decoder.h:18
heatshrink_decoder_finish
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:325
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
i
uint8_t i
Definition: screen_test_graph.c:72
heatshrink_decoder::buffers
uint8_t buffers[(1<< HEATSHRINK_DECODER_WINDOW_BITS(_))+HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)]
Definition: heatshrink_decoder.h:66
BACKREF_INDEX_BITS
#define BACKREF_INDEX_BITS(HSD)
Definition: heatshrink_decoder.cpp:123
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
get_bits
static uint16_t get_bits(heatshrink_decoder *hsd, uint8_t count)
Definition: heatshrink_decoder.cpp:282
st_tag_bit
static HSD_state st_tag_bit(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:181
HSD_state
HSD_state
Definition: heatshrink_decoder.cpp:11
HSD_poll_res
HSD_poll_res
Definition: heatshrink_decoder.h:17
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: heatshrink_decoder.cpp:134
st_yield_literal
static HSD_state st_yield_literal(heatshrink_decoder *hsd, output_info *oi)
Definition: heatshrink_decoder.cpp:195
heatshrink_decoder_sink
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size)
Definition: heatshrink_decoder.cpp:98
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_sink
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size)
Definition: heatshrink_decoder.cpp:98
push_byte
static void push_byte(heatshrink_decoder *hsd, output_info *oi, uint8_t byte)
Definition: heatshrink_decoder.cpp:350
heatshrink_decoder
Definition: heatshrink_decoder.h:46
heatshrink_common.h
heatshrink_decoder::bit_index
uint8_t bit_index
Definition: heatshrink_decoder.h:54
HEATSHRINK_MIN_LOOKAHEAD_BITS
#define HEATSHRINK_MIN_LOOKAHEAD_BITS
Definition: heatshrink_common.h:17
HSDS_BACKREF_COUNT_MSB
Definition: heatshrink_decoder.cpp:16
HSDS_YIELD_BACKREF
Definition: heatshrink_decoder.cpp:18
heatshrink_decoder.h
void
void
Definition: png.h:1083
output_info::output_size
size_t * output_size
Definition: heatshrink_decoder.cpp:44
HSDS_BACKREF_INDEX_LSB
Definition: heatshrink_decoder.cpp:15
HEATSHRINK_MIN_WINDOW_BITS
#define HEATSHRINK_MIN_WINDOW_BITS
Definition: heatshrink_common.h:14
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
heatshrink_decoder_reset
void heatshrink_decoder_reset(heatshrink_decoder *hsd)
Definition: heatshrink_decoder.cpp:83
HSDS_BACKREF_INDEX_MSB
Definition: heatshrink_decoder.cpp:14
heatshrink_decoder::current_byte
uint8_t current_byte
Definition: heatshrink_decoder.h:53
HEATSHRINK_MAX_WINDOW_BITS
#define HEATSHRINK_MAX_WINDOW_BITS
Definition: heatshrink_common.h:15
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
ASSERT
#define ASSERT(X)
Definition: heatshrink_decoder.cpp:38
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
NO_BITS
#define NO_BITS
Definition: heatshrink_decoder.cpp:47
HSDR_POLL_ERROR_NULL
Definition: heatshrink_decoder.h:20
HSDS_YIELD_LITERAL
Definition: heatshrink_decoder.cpp:13
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: heatshrink_decoder.cpp:134
HSDR_SINK_ERROR_NULL
Definition: heatshrink_decoder.h:14
size
static png_bytep size_t size
Definition: pngwrite.c:2170