MeterLogger
heatshrink_decoder.h
Go to the documentation of this file.
1 #ifndef HEATSHRINK_DECODER_H
2 #define HEATSHRINK_DECODER_H
3 
4 #include <stddef.h>
5 #include "heatshrink_common.h"
6 #include "heatshrink_config.h"
7 
8 typedef enum {
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;
13 
14 typedef enum {
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;
20 
21 typedef enum {
22  HSDR_FINISH_DONE, /* output is done */
23  HSDR_FINISH_MORE, /* more output remains */
24  HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */
26 
27 #if HEATSHRINK_DYNAMIC_ALLOC
28 #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF) \
29  ((BUF)->input_buffer_size)
30 #define HEATSHRINK_DECODER_WINDOW_BITS(BUF) \
31  ((BUF)->window_sz2)
32 #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
33  ((BUF)->lookahead_sz2)
34 #else
35 #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_) \
36  HEATSHRINK_STATIC_INPUT_BUFFER_SIZE
37 #define HEATSHRINK_DECODER_WINDOW_BITS(_) \
38  (HEATSHRINK_STATIC_WINDOW_BITS)
39 #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
40  (HEATSHRINK_STATIC_LOOKAHEAD_BITS)
41 #endif
42 
43 typedef struct {
44  uint16_t input_size; /* bytes in input buffer */
45  uint16_t input_index; /* offset to next unprocessed input byte */
46  uint16_t output_count; /* how many bytes to output */
47  uint16_t output_index; /* index for bytes to output */
48  uint16_t head_index; /* head of window buffer */
49  uint16_t bit_accumulator;
50  uint8_t state; /* current state machine node */
51  uint8_t current_byte; /* current byte of input */
52  uint8_t bit_index; /* current bit index */
53 
54 #if HEATSHRINK_DYNAMIC_ALLOC
55  /* Fields that are only used if dynamically allocated. */
56  uint8_t window_sz2; /* window buffer bits */
57  uint8_t lookahead_sz2; /* lookahead bits */
58  uint16_t input_buffer_size; /* input buffer size */
59 
60  /* Input buffer, then expansion window buffer */
61  uint8_t buffers[];
62 #else
63  /* Input buffer, then expansion window buffer */
64  uint8_t buffers[(1 << HEATSHRINK_DECODER_WINDOW_BITS(_))
66 #endif
68 
69 #if HEATSHRINK_DYNAMIC_ALLOC
70 /* Allocate a decoder with an input buffer of INPUT_BUFFER_SIZE bytes,
71  * an expansion buffer size of 2^WINDOW_SZ2, and a lookahead
72  * size of 2^lookahead_sz2. (The window buffer and lookahead sizes
73  * must match the settings used when the data was compressed.)
74  * Returns NULL on error. */
75 heatshrink_decoder *heatshrink_decoder_alloc(uint16_t input_buffer_size,
76  uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2);
77 
78 /* Free a decoder. */
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). */
88  uint8_t *in_buf, size_t size, size_t *input_size);
89 
90 /* Poll for output from the decoder, copying at most OUT_BUF_SIZE bytes into
91  * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */
93  uint8_t *out_buf, size_t out_buf_size, size_t *output_size);
94 
95 /* Notify the dencoder that the input stream is finished.
96  * If the return value is HSDR_FINISH_MORE, there is still more output, so
97  * call heatshrink_decoder_poll and repeat. */
99 
100 #endif
void heatshrink_decoder_reset(heatshrink_decoder *hsd)
void heatshrink_decoder_free(heatshrink_decoder *hsd)
HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd)
#define HEATSHRINK_DECODER_WINDOW_BITS(BUF)
heatshrink_decoder * heatshrink_decoder_alloc(uint16_t input_buffer_size, uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2)
HSD_poll_res
HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd, uint8_t *in_buf, size_t size, size_t *input_size)
static heatshrink_decoder hsd
HSD_sink_res
HSD_finish_res
#define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF)
HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd, uint8_t *out_buf, size_t out_buf_size, size_t *output_size)