MeterLogger
heatshrink_encoder.h
Go to the documentation of this file.
1 #ifndef HEATSHRINK_ENCODER_H
2 #define HEATSHRINK_ENCODER_H
3 
4 #include <stddef.h>
5 #include "heatshrink_common.h"
6 #include "heatshrink_config.h"
7 
8 typedef enum {
9  HSER_SINK_OK, /* data sunk into input buffer */
10  HSER_SINK_ERROR_NULL=-1, /* NULL argument */
11  HSER_SINK_ERROR_MISUSE=-2, /* API misuse */
12 } HSE_sink_res;
13 
14 typedef enum {
15  HSER_POLL_EMPTY, /* input exhausted */
16  HSER_POLL_MORE, /* poll again for more output */
17  HSER_POLL_ERROR_NULL=-1, /* NULL argument */
18  HSER_POLL_ERROR_MISUSE=-2, /* API misuse */
19 } HSE_poll_res;
20 
21 typedef enum {
22  HSER_FINISH_DONE, /* encoding is complete */
23  HSER_FINISH_MORE, /* more output remaining; use poll */
24  HSER_FINISH_ERROR_NULL=-1, /* NULL argument */
26 
27 #if HEATSHRINK_DYNAMIC_ALLOC
28 #define HEATSHRINK_ENCODER_WINDOW_BITS(HSE) \
29  ((HSE)->window_sz2)
30 #define HEATSHRINK_ENCODER_LOOKAHEAD_BITS(HSE) \
31  ((HSE)->lookahead_sz2)
32 #define HEATSHRINK_ENCODER_INDEX(HSE) \
33  ((HSE)->search_index)
34 struct hs_index {
35  uint16_t size;
36  int16_t index[];
37 };
38 #else
39 #define HEATSHRINK_ENCODER_WINDOW_BITS(_) \
40  (HEATSHRINK_STATIC_WINDOW_BITS)
41 #define HEATSHRINK_ENCODER_LOOKAHEAD_BITS(_) \
42  (HEATSHRINK_STATIC_LOOKAHEAD_BITS)
43 #define HEATSHRINK_ENCODER_INDEX(HSE) \
44  (&(HSE)->search_index)
45 struct hs_index {
46  uint16_t size;
47  int16_t index[2 << HEATSHRINK_STATIC_WINDOW_BITS];
48 };
49 #endif
50 
51 typedef struct {
52  uint16_t input_size; /* bytes in input buffer */
53  uint16_t match_scan_index;
54  uint16_t match_length;
55  uint16_t match_pos;
56  uint16_t outgoing_bits; /* enqueued outgoing bits */
58  uint8_t flags;
59  uint8_t state; /* current state machine node */
60  uint8_t current_byte; /* current byte of output */
61  uint8_t bit_index; /* current bit index */
62 #if HEATSHRINK_DYNAMIC_ALLOC
63  uint8_t window_sz2; /* 2^n size of window */
64  uint8_t lookahead_sz2; /* 2^n size of lookahead */
65 #if HEATSHRINK_USE_INDEX
67 #endif
68  /* input buffer and / sliding window for expansion */
69  uint8_t buffer[];
70 #else
71  #if HEATSHRINK_USE_INDEX
72  struct hs_index search_index;
73  #endif
74  /* input buffer and / sliding window for expansion */
75  uint8_t buffer[2 << HEATSHRINK_ENCODER_WINDOW_BITS(_)];
76 #endif
78 
79 #if HEATSHRINK_DYNAMIC_ALLOC
80 /* Allocate a new encoder struct and its buffers.
81  * Returns NULL on error. */
83  uint8_t lookahead_sz2);
84 
85 /* Free an encoder. */
87 #endif
88 
89 /* Reset an encoder. */
91 
92 /* Sink up to SIZE bytes from IN_BUF into the encoder.
93  * INPUT_SIZE is set to the number of bytes actually sunk (in case a
94  * buffer was filled.). */
96  uint8_t *in_buf, size_t size, size_t *input_size);
97 
98 /* Poll for output from the encoder, copying at most OUT_BUF_SIZE bytes into
99  * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */
101  uint8_t *out_buf, size_t out_buf_size, size_t *output_size);
102 
103 /* Notify the encoder that the input stream is finished.
104  * If the return value is HSER_FINISH_MORE, there is still more output, so
105  * call heatshrink_encoder_poll and repeat. */
107 
108 #endif
uint16_t size
HSE_poll_res heatshrink_encoder_poll(heatshrink_encoder *hse, uint8_t *out_buf, size_t out_buf_size, size_t *output_size)
#define HEATSHRINK_ENCODER_WINDOW_BITS(HSE)
void heatshrink_encoder_free(heatshrink_encoder *hse)
HSE_sink_res
int16_t index[]
HSE_finish_res
heatshrink_encoder * heatshrink_encoder_alloc(uint8_t window_sz2, uint8_t lookahead_sz2)
HSE_finish_res heatshrink_encoder_finish(heatshrink_encoder *hse)
struct hs_index * search_index
void heatshrink_encoder_reset(heatshrink_encoder *hse)
HSE_sink_res heatshrink_encoder_sink(heatshrink_encoder *hse, uint8_t *in_buf, size_t size, size_t *input_size)
static heatshrink_encoder hse
HSE_poll_res