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

Go to the source code of this file.

Data Structures

struct  hs_index
 
struct  heatshrink_encoder
 

Macros

#define HEATSHRINK_ENCODER_WINDOW_BITS(HSE)   ((HSE)->window_sz2)
 
#define HEATSHRINK_ENCODER_LOOKAHEAD_BITS(HSE)   ((HSE)->lookahead_sz2)
 
#define HEATSHRINK_ENCODER_INDEX(HSE)   ((HSE)->search_index)
 

Enumerations

enum  HSE_sink_res { HSER_SINK_OK, HSER_SINK_ERROR_NULL =-1, HSER_SINK_ERROR_MISUSE =-2 }
 
enum  HSE_poll_res { HSER_POLL_EMPTY, HSER_POLL_MORE, HSER_POLL_ERROR_NULL =-1, HSER_POLL_ERROR_MISUSE =-2 }
 
enum  HSE_finish_res { HSER_FINISH_DONE, HSER_FINISH_MORE, HSER_FINISH_ERROR_NULL =-1 }
 

Functions

heatshrink_encoderheatshrink_encoder_alloc (uint8_t window_sz2, uint8_t lookahead_sz2)
 
void heatshrink_encoder_free (heatshrink_encoder *hse)
 
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)
 
HSE_poll_res heatshrink_encoder_poll (heatshrink_encoder *hse, uint8_t *out_buf, size_t out_buf_size, size_t *output_size)
 
HSE_finish_res heatshrink_encoder_finish (heatshrink_encoder *hse)
 

Macro Definition Documentation

◆ HEATSHRINK_ENCODER_INDEX

#define HEATSHRINK_ENCODER_INDEX (   HSE)    ((HSE)->search_index)

Definition at line 32 of file heatshrink_encoder.h.

Referenced by do_indexing(), and find_longest_match().

◆ HEATSHRINK_ENCODER_LOOKAHEAD_BITS

#define HEATSHRINK_ENCODER_LOOKAHEAD_BITS (   HSE)    ((HSE)->lookahead_sz2)

Definition at line 30 of file heatshrink_encoder.h.

Referenced by get_lookahead_size(), and st_yield_br_index().

◆ HEATSHRINK_ENCODER_WINDOW_BITS

#define HEATSHRINK_ENCODER_WINDOW_BITS (   HSE)    ((HSE)->window_sz2)

Enumeration Type Documentation

◆ HSE_finish_res

Enumerator
HSER_FINISH_DONE 
HSER_FINISH_MORE 
HSER_FINISH_ERROR_NULL 

Definition at line 21 of file heatshrink_encoder.h.

21  {
22  HSER_FINISH_DONE, /* encoding is complete */
23  HSER_FINISH_MORE, /* more output remaining; use poll */
24  HSER_FINISH_ERROR_NULL=-1, /* NULL argument */
HSE_finish_res

◆ HSE_poll_res

Enumerator
HSER_POLL_EMPTY 
HSER_POLL_MORE 
HSER_POLL_ERROR_NULL 
HSER_POLL_ERROR_MISUSE 

Definition at line 14 of file heatshrink_encoder.h.

14  {
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;
HSE_poll_res

◆ HSE_sink_res

Enumerator
HSER_SINK_OK 
HSER_SINK_ERROR_NULL 
HSER_SINK_ERROR_MISUSE 

Definition at line 8 of file heatshrink_encoder.h.

8  {
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;
HSE_sink_res

Function Documentation

◆ heatshrink_encoder_alloc()

heatshrink_encoder* heatshrink_encoder_alloc ( uint8_t  window_sz2,
uint8_t  lookahead_sz2 
)

Definition at line 78 of file heatshrink_encoder.c.

References get_input_buffer_size(), heatshrink_encoder_reset(), HEATSHRINK_FREE, HEATSHRINK_MALLOC, HEATSHRINK_MAX_WINDOW_BITS, HEATSHRINK_MIN_LOOKAHEAD_BITS, HEATSHRINK_MIN_WINDOW_BITS, hse, LOG, heatshrink_encoder::lookahead_sz2, NULL, heatshrink_encoder::search_index, hs_index::size, and heatshrink_encoder::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(), encode(), encoder_alloc_should_reject_invalid_arguments(), encoder_poll_should_detect_repeated_substring(), encoder_poll_should_detect_repeated_substring_and_preserve_trailing_literal(), encoder_poll_should_indicate_when_no_input_is_provided(), encoder_poll_should_reject_nulls(), encoder_should_emit_data_without_repetitions_as_literal_sequence(), encoder_should_emit_series_of_same_byte_as_literal_then_backref(), encoder_sink_should_accept_input_when_it_will_fit(), encoder_sink_should_accept_partial_input_when_some_will_fit(), encoder_sink_should_reject_nulls(), and gen().

79  {
80  if ((window_sz2 < HEATSHRINK_MIN_WINDOW_BITS) ||
81  (window_sz2 > HEATSHRINK_MAX_WINDOW_BITS) ||
82  (lookahead_sz2 < HEATSHRINK_MIN_LOOKAHEAD_BITS) ||
83  (lookahead_sz2 > window_sz2)) {
84  return NULL;
85  }
86 
87  /* Note: 2 * the window size is used because the buffer needs to fit
88  * (1 << window_sz2) bytes for the current input, and an additional
89  * (1 << window_sz2) bytes for the previous buffer of input, which
90  * will be scanned for useful backreferences. */
91  size_t buf_sz = (2 << window_sz2);
92 
93  heatshrink_encoder *hse = HEATSHRINK_MALLOC(sizeof(*hse) + buf_sz);
94  if (hse == NULL) { return NULL; }
95  hse->window_sz2 = window_sz2;
96  hse->lookahead_sz2 = lookahead_sz2;
98 
99 #if HEATSHRINK_USE_INDEX
100  size_t index_sz = buf_sz*sizeof(uint16_t);
101  hse->search_index = HEATSHRINK_MALLOC(index_sz + sizeof(struct hs_index));
102  if (hse->search_index == NULL) {
103  HEATSHRINK_FREE(hse, sizeof(*hse) + buf_sz);
104  return NULL;
105  }
106  hse->search_index->size = index_sz;
107 #endif
108 
109  LOG("-- allocated encoder with buffer size of %zu (%u byte input size)\n",
110  buf_sz, get_input_buffer_size(hse));
111  return hse;
112 }
#define HEATSHRINK_FREE(P, SZ)
uint16_t size
#define HEATSHRINK_MAX_WINDOW_BITS
#define NULL
Definition: def.h:47
#define HEATSHRINK_MIN_WINDOW_BITS
#define LOG(...)
static uint16_t get_input_buffer_size(heatshrink_encoder *hse)
#define HEATSHRINK_MALLOC(SZ)
struct hs_index * search_index
void heatshrink_encoder_reset(heatshrink_encoder *hse)
#define HEATSHRINK_MIN_LOOKAHEAD_BITS
static heatshrink_encoder hse
Here is the call graph for this function:
Here is the caller graph for this function:

◆ heatshrink_encoder_finish()

HSE_finish_res heatshrink_encoder_finish ( heatshrink_encoder hse)

Definition at line 260 of file heatshrink_encoder.c.

References FLAG_IS_FINISHING, heatshrink_encoder::flags, HSER_FINISH_DONE, HSER_FINISH_ERROR_NULL, HSER_FINISH_MORE, HSES_DONE, HSES_FILLED, HSES_NOT_FULL, LOG, NULL, and heatshrink_encoder::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(), encoder_finish_should_reject_nulls(), encoder_poll_should_detect_repeated_substring(), encoder_poll_should_detect_repeated_substring_and_preserve_trailing_literal(), encoder_should_emit_data_without_repetitions_as_literal_sequence(), encoder_should_emit_series_of_same_byte_as_literal_then_backref(), encoder_sink_read(), and gen().

260  {
261  if (hse == NULL) { return HSER_FINISH_ERROR_NULL; }
262  LOG("-- setting is_finishing flag\n");
263  hse->flags |= FLAG_IS_FINISHING;
264  if (hse->state == HSES_NOT_FULL) { hse->state = HSES_FILLED; }
266 }
#define NULL
Definition: def.h:47
#define LOG(...)
Here is the caller graph for this function:

◆ heatshrink_encoder_free()

void heatshrink_encoder_free ( heatshrink_encoder hse)

Definition at line 114 of file heatshrink_encoder.c.

References HEATSHRINK_ENCODER_WINDOW_BITS, and HEATSHRINK_FREE.

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(), encode(), encoder_poll_should_detect_repeated_substring(), encoder_poll_should_detect_repeated_substring_and_preserve_trailing_literal(), encoder_poll_should_indicate_when_no_input_is_provided(), encoder_poll_should_reject_nulls(), encoder_should_emit_data_without_repetitions_as_literal_sequence(), encoder_should_emit_series_of_same_byte_as_literal_then_backref(), encoder_sink_should_accept_input_when_it_will_fit(), encoder_sink_should_accept_partial_input_when_some_will_fit(), encoder_sink_should_reject_nulls(), and gen().

114  {
115  size_t buf_sz = (2 << HEATSHRINK_ENCODER_WINDOW_BITS(hse));
116 #if HEATSHRINK_USE_INDEX
117  size_t index_sz = sizeof(struct hs_index) + hse->search_index->size;
118  HEATSHRINK_FREE(hse->search_index, index_sz);
119  (void)index_sz;
120 #endif
121  HEATSHRINK_FREE(hse, sizeof(heatshrink_encoder) + buf_sz);
122  (void)buf_sz;
123 }
#define HEATSHRINK_FREE(P, SZ)
uint16_t size
#define HEATSHRINK_ENCODER_WINDOW_BITS(HSE)
struct hs_index * search_index
Here is the caller graph for this function:

◆ heatshrink_encoder_poll()

HSE_poll_res heatshrink_encoder_poll ( heatshrink_encoder hse,
uint8_t *  out_buf,
size_t  out_buf_size,
size_t output_size 
)

Definition at line 198 of file heatshrink_encoder.c.

References output_info::buf, output_info::buf_size, do_indexing(), heatshrink_encoder::flags, HSER_POLL_EMPTY, HSER_POLL_ERROR_MISUSE, HSER_POLL_ERROR_NULL, HSER_POLL_MORE, HSES_DONE, HSES_FILLED, HSES_FLUSH_BITS, HSES_NOT_FULL, HSES_SAVE_BACKLOG, HSES_SEARCH, HSES_YIELD_BR_INDEX, HSES_YIELD_BR_LENGTH, HSES_YIELD_LITERAL, HSES_YIELD_TAG_BIT, LOG, NULL, output_info::output_size, st_flush_bit_buffer(), st_save_backlog(), st_step_search(), st_yield_br_index(), st_yield_br_length(), st_yield_literal(), st_yield_tag_bit(), and heatshrink_encoder::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(), encoder_poll_should_detect_repeated_substring(), encoder_poll_should_detect_repeated_substring_and_preserve_trailing_literal(), encoder_poll_should_indicate_when_no_input_is_provided(), encoder_poll_should_reject_nulls(), encoder_should_emit_data_without_repetitions_as_literal_sequence(), encoder_should_emit_series_of_same_byte_as_literal_then_backref(), encoder_sink_read(), and gen().

199  {
200  if ((hse == NULL) || (out_buf == NULL) || (output_size == NULL)) {
201  return HSER_POLL_ERROR_NULL;
202  }
203  if (out_buf_size == 0) {
204  LOG("-- MISUSE: output buffer size is 0\n");
205  return HSER_POLL_ERROR_MISUSE;
206  }
207  *output_size = 0;
208 
209  output_info oi;
210  oi.buf = out_buf;
211  oi.buf_size = out_buf_size;
212  oi.output_size = output_size;
213 
214  while (1) {
215  LOG("-- polling, state %u (%s), flags 0x%02x\n",
216  hse->state, state_names[hse->state], hse->flags);
217 
218  uint8_t in_state = hse->state;
219  switch (in_state) {
220  case HSES_NOT_FULL:
221  return HSER_POLL_EMPTY;
222  case HSES_FILLED:
223  do_indexing(hse);
224  hse->state = HSES_SEARCH;
225  break;
226  case HSES_SEARCH:
227  hse->state = st_step_search(hse);
228  break;
229  case HSES_YIELD_TAG_BIT:
230  hse->state = st_yield_tag_bit(hse, &oi);
231  break;
232  case HSES_YIELD_LITERAL:
233  hse->state = st_yield_literal(hse, &oi);
234  break;
235  case HSES_YIELD_BR_INDEX:
236  hse->state = st_yield_br_index(hse, &oi);
237  break;
239  hse->state = st_yield_br_length(hse, &oi);
240  break;
241  case HSES_SAVE_BACKLOG:
242  hse->state = st_save_backlog(hse);
243  break;
244  case HSES_FLUSH_BITS:
245  hse->state = st_flush_bit_buffer(hse, &oi);
246  case HSES_DONE:
247  return HSER_POLL_EMPTY;
248  default:
249  LOG("-- bad state %s\n", state_names[hse->state]);
250  return HSER_POLL_ERROR_MISUSE;
251  }
252 
253  if (hse->state == in_state) {
254  /* Check if output buffer is exhausted. */
255  if (*output_size == out_buf_size) return HSER_POLL_MORE;
256  }
257  }
258 }
static void do_indexing(heatshrink_encoder *hse)
static HSE_state st_step_search(heatshrink_encoder *hse)
static HSE_state st_yield_br_length(heatshrink_encoder *hse, output_info *oi)
#define NULL
Definition: def.h:47
static HSE_state st_yield_literal(heatshrink_encoder *hse, output_info *oi)
static HSE_state st_yield_tag_bit(heatshrink_encoder *hse, output_info *oi)
#define LOG(...)
static HSE_state st_save_backlog(heatshrink_encoder *hse)
static HSE_state st_yield_br_index(heatshrink_encoder *hse, output_info *oi)
static HSE_state st_flush_bit_buffer(heatshrink_encoder *hse, output_info *oi)
size_t * output_size
Here is the call graph for this function:
Here is the caller graph for this function:

◆ heatshrink_encoder_reset()

void heatshrink_encoder_reset ( heatshrink_encoder hse)

Definition at line 126 of file heatshrink_encoder.c.

References heatshrink_encoder::bit_index, heatshrink_encoder::buffer, heatshrink_encoder::current_byte, heatshrink_encoder::flags, HEATSHRINK_ENCODER_WINDOW_BITS, HSES_NOT_FULL, heatshrink_encoder::input_size, heatshrink_encoder::match_length, heatshrink_encoder::match_scan_index, memset, heatshrink_encoder::outgoing_bits, heatshrink_encoder::outgoing_bits_count, and heatshrink_encoder::state.

Referenced by compress_and_expand_and_check(), and heatshrink_encoder_alloc().

126  {
127  size_t buf_sz = (2 << HEATSHRINK_ENCODER_WINDOW_BITS(hse));
128  memset(hse->buffer, 0, buf_sz);
129  hse->input_size = 0;
130  hse->state = HSES_NOT_FULL;
131  hse->match_scan_index = 0;
132  hse->flags = 0;
133  hse->bit_index = 0x80;
134  hse->current_byte = 0x00;
135  hse->match_length = 0;
136 
137  hse->outgoing_bits = 0x0000;
138  hse->outgoing_bits_count = 0;
139 
140  #ifdef LOOP_DETECT
141  hse->loop_detect = (uint32_t)-1;
142  #endif
143 }
#define memset(x, a, b)
Definition: platform.h:21
#define HEATSHRINK_ENCODER_WINDOW_BITS(HSE)
Here is the caller graph for this function:

◆ heatshrink_encoder_sink()

HSE_sink_res heatshrink_encoder_sink ( heatshrink_encoder hse,
uint8_t *  in_buf,
size_t  size,
size_t input_size 
)

Definition at line 145 of file heatshrink_encoder.c.

References heatshrink_encoder::buffer, do_indexing(), find_longest_match(), get_input_buffer_size(), get_input_offset(), hse, HSER_SINK_ERROR_MISUSE, HSER_SINK_ERROR_NULL, HSER_SINK_OK, HSES_FILLED, HSES_NOT_FULL, heatshrink_encoder::input_size, is_finishing(), LOG, memcpy, NULL, hs_index::size, st_flush_bit_buffer(), st_save_backlog(), st_step_search(), st_yield_br_index(), st_yield_br_length(), st_yield_literal(), st_yield_tag_bit(), and heatshrink_encoder::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(), encoder_poll_should_detect_repeated_substring(), encoder_poll_should_detect_repeated_substring_and_preserve_trailing_literal(), encoder_should_emit_data_without_repetitions_as_literal_sequence(), encoder_should_emit_series_of_same_byte_as_literal_then_backref(), encoder_sink_read(), encoder_sink_should_accept_input_when_it_will_fit(), encoder_sink_should_accept_partial_input_when_some_will_fit(), encoder_sink_should_reject_nulls(), and gen().

146  {
147  if ((hse == NULL) || (in_buf == NULL) || (input_size == NULL)) {
148  return HSER_SINK_ERROR_NULL;
149  }
150 
151  /* Sinking more content after saying the content is done, tsk tsk */
152  if (is_finishing(hse)) { return HSER_SINK_ERROR_MISUSE; }
153 
154  /* Sinking more content before processing is done */
155  if (hse->state != HSES_NOT_FULL) { return HSER_SINK_ERROR_MISUSE; }
156 
157  uint16_t write_offset = get_input_offset(hse) + hse->input_size;
158  uint16_t ibs = get_input_buffer_size(hse);
159  uint16_t rem = ibs - hse->input_size;
160  uint16_t cp_sz = rem < size ? rem : size;
161 
162  memcpy(&hse->buffer[write_offset], in_buf, cp_sz);
163  *input_size = cp_sz;
164  hse->input_size += cp_sz;
165 
166  LOG("-- sunk %u bytes (of %zu) into encoder at %d, input buffer now has %u\n",
167  cp_sz, size, write_offset, hse->input_size);
168  if (cp_sz == rem) {
169  LOG("-- internal buffer is now full\n");
170  hse->state = HSES_FILLED;
171  }
172 
173  return HSER_SINK_OK;
174 }
uint16_t size
static int is_finishing(heatshrink_encoder *hse)
#define NULL
Definition: def.h:47
#define LOG(...)
static uint16_t get_input_buffer_size(heatshrink_encoder *hse)
static uint16_t get_input_offset(heatshrink_encoder *hse)
#define memcpy(x, a, b)
Definition: platform.h:22
Here is the call graph for this function:
Here is the caller graph for this function: