heatshrink 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,604 @@
1
+ #include <stdlib.h>
2
+ #include <string.h>
3
+ #include <stdbool.h>
4
+ #include "heatshrink_encoder.h"
5
+
6
+ typedef enum {
7
+ HSES_NOT_FULL, /* input buffer not full enough */
8
+ HSES_FILLED, /* buffer is full */
9
+ HSES_SEARCH, /* searching for patterns */
10
+ HSES_YIELD_TAG_BIT, /* yield tag bit */
11
+ HSES_YIELD_LITERAL, /* emit literal byte */
12
+ HSES_YIELD_BR_INDEX, /* yielding backref index */
13
+ HSES_YIELD_BR_LENGTH, /* yielding backref length */
14
+ HSES_SAVE_BACKLOG, /* copying buffer to backlog */
15
+ HSES_FLUSH_BITS, /* flush bit buffer */
16
+ HSES_DONE, /* done */
17
+ } HSE_state;
18
+
19
+ #if HEATSHRINK_DEBUGGING_LOGS
20
+ #include <stdio.h>
21
+ #include <ctype.h>
22
+ #include <assert.h>
23
+ #define LOG(...) fprintf(stderr, __VA_ARGS__)
24
+ #define ASSERT(X) assert(X)
25
+ static const char *state_names[] = {
26
+ "not_full",
27
+ "filled",
28
+ "search",
29
+ "yield_tag_bit",
30
+ "yield_literal",
31
+ "yield_br_index",
32
+ "yield_br_length",
33
+ "save_backlog",
34
+ "flush_bits",
35
+ "done",
36
+ };
37
+ #else
38
+ #define LOG(...) /* no-op */
39
+ #define ASSERT(X) /* no-op */
40
+ #endif
41
+
42
+ // Encoder flags
43
+ enum {
44
+ FLAG_IS_FINISHING = 0x01,
45
+ };
46
+
47
+ typedef struct {
48
+ uint8_t *buf; /* output buffer */
49
+ size_t buf_size; /* buffer size */
50
+ size_t *output_size; /* bytes pushed to buffer, so far */
51
+ } output_info;
52
+
53
+ #define MATCH_NOT_FOUND ((uint16_t)-1)
54
+
55
+ static uint16_t get_input_offset(heatshrink_encoder *hse);
56
+ static uint16_t get_input_buffer_size(heatshrink_encoder *hse);
57
+ static uint16_t get_lookahead_size(heatshrink_encoder *hse);
58
+ static void add_tag_bit(heatshrink_encoder *hse, output_info *oi, uint8_t tag);
59
+ static int can_take_byte(output_info *oi);
60
+ static int is_finishing(heatshrink_encoder *hse);
61
+ static void save_backlog(heatshrink_encoder *hse);
62
+
63
+ /* Push COUNT (max 8) bits to the output buffer, which has room. */
64
+ static void push_bits(heatshrink_encoder *hse, uint8_t count, uint8_t bits,
65
+ output_info *oi);
66
+ static uint8_t push_outgoing_bits(heatshrink_encoder *hse, output_info *oi);
67
+ static void push_literal_byte(heatshrink_encoder *hse, output_info *oi);
68
+
69
+ #if HEATSHRINK_DYNAMIC_ALLOC
70
+ heatshrink_encoder *heatshrink_encoder_alloc(uint8_t window_sz2,
71
+ uint8_t lookahead_sz2) {
72
+ if ((window_sz2 < HEATSHRINK_MIN_WINDOW_BITS) ||
73
+ (window_sz2 > HEATSHRINK_MAX_WINDOW_BITS) ||
74
+ (lookahead_sz2 < HEATSHRINK_MIN_LOOKAHEAD_BITS) ||
75
+ (lookahead_sz2 >= window_sz2)) {
76
+ return NULL;
77
+ }
78
+
79
+ /* Note: 2 * the window size is used because the buffer needs to fit
80
+ * (1 << window_sz2) bytes for the current input, and an additional
81
+ * (1 << window_sz2) bytes for the previous buffer of input, which
82
+ * will be scanned for useful backreferences. */
83
+ size_t buf_sz = (2 << window_sz2);
84
+
85
+ heatshrink_encoder *hse = HEATSHRINK_MALLOC(sizeof(*hse) + buf_sz);
86
+ if (hse == NULL) { return NULL; }
87
+ hse->window_sz2 = window_sz2;
88
+ hse->lookahead_sz2 = lookahead_sz2;
89
+ heatshrink_encoder_reset(hse);
90
+
91
+ #if HEATSHRINK_USE_INDEX
92
+ size_t index_sz = buf_sz*sizeof(uint16_t);
93
+ hse->search_index = HEATSHRINK_MALLOC(index_sz + sizeof(struct hs_index));
94
+ if (hse->search_index == NULL) {
95
+ HEATSHRINK_FREE(hse, sizeof(*hse) + buf_sz);
96
+ return NULL;
97
+ }
98
+ hse->search_index->size = index_sz;
99
+ #endif
100
+
101
+ LOG("-- allocated encoder with buffer size of %zu (%u byte input size)\n",
102
+ buf_sz, get_input_buffer_size(hse));
103
+ return hse;
104
+ }
105
+
106
+ void heatshrink_encoder_free(heatshrink_encoder *hse) {
107
+ size_t buf_sz = (2 << HEATSHRINK_ENCODER_WINDOW_BITS(hse));
108
+ #if HEATSHRINK_USE_INDEX
109
+ size_t index_sz = sizeof(struct hs_index) + hse->search_index->size;
110
+ HEATSHRINK_FREE(hse->search_index, index_sz);
111
+ (void)index_sz;
112
+ #endif
113
+ HEATSHRINK_FREE(hse, sizeof(heatshrink_encoder) + buf_sz);
114
+ (void)buf_sz;
115
+ }
116
+ #endif
117
+
118
+ void heatshrink_encoder_reset(heatshrink_encoder *hse) {
119
+ size_t buf_sz = (2 << HEATSHRINK_ENCODER_WINDOW_BITS(hse));
120
+ memset(hse->buffer, 0, buf_sz);
121
+ hse->input_size = 0;
122
+ hse->state = HSES_NOT_FULL;
123
+ hse->match_scan_index = 0;
124
+ hse->flags = 0;
125
+ hse->bit_index = 0x80;
126
+ hse->current_byte = 0x00;
127
+ hse->match_length = 0;
128
+
129
+ hse->outgoing_bits = 0x0000;
130
+ hse->outgoing_bits_count = 0;
131
+
132
+ #ifdef LOOP_DETECT
133
+ hse->loop_detect = (uint32_t)-1;
134
+ #endif
135
+ }
136
+
137
+ HSE_sink_res heatshrink_encoder_sink(heatshrink_encoder *hse,
138
+ uint8_t *in_buf, size_t size, size_t *input_size) {
139
+ if ((hse == NULL) || (in_buf == NULL) || (input_size == NULL)) {
140
+ return HSER_SINK_ERROR_NULL;
141
+ }
142
+
143
+ /* Sinking more content after saying the content is done, tsk tsk */
144
+ if (is_finishing(hse)) { return HSER_SINK_ERROR_MISUSE; }
145
+
146
+ /* Sinking more content before processing is done */
147
+ if (hse->state != HSES_NOT_FULL) { return HSER_SINK_ERROR_MISUSE; }
148
+
149
+ uint16_t write_offset = get_input_offset(hse) + hse->input_size;
150
+ uint16_t ibs = get_input_buffer_size(hse);
151
+ uint16_t rem = ibs - hse->input_size;
152
+ uint16_t cp_sz = rem < size ? rem : size;
153
+
154
+ memcpy(&hse->buffer[write_offset], in_buf, cp_sz);
155
+ *input_size = cp_sz;
156
+ hse->input_size += cp_sz;
157
+
158
+ LOG("-- sunk %u bytes (of %zu) into encoder at %d, input buffer now has %u\n",
159
+ cp_sz, size, write_offset, hse->input_size);
160
+ if (cp_sz == rem) {
161
+ LOG("-- internal buffer is now full\n");
162
+ hse->state = HSES_FILLED;
163
+ }
164
+
165
+ return HSER_SINK_OK;
166
+ }
167
+
168
+
169
+ /***************
170
+ * Compression *
171
+ ***************/
172
+
173
+ static uint16_t find_longest_match(heatshrink_encoder *hse, uint16_t start,
174
+ uint16_t end, const uint16_t maxlen, uint16_t *match_length);
175
+ static void do_indexing(heatshrink_encoder *hse);
176
+
177
+ static HSE_state st_step_search(heatshrink_encoder *hse);
178
+ static HSE_state st_yield_tag_bit(heatshrink_encoder *hse,
179
+ output_info *oi);
180
+ static HSE_state st_yield_literal(heatshrink_encoder *hse,
181
+ output_info *oi);
182
+ static HSE_state st_yield_br_index(heatshrink_encoder *hse,
183
+ output_info *oi);
184
+ static HSE_state st_yield_br_length(heatshrink_encoder *hse,
185
+ output_info *oi);
186
+ static HSE_state st_save_backlog(heatshrink_encoder *hse);
187
+ static HSE_state st_flush_bit_buffer(heatshrink_encoder *hse,
188
+ output_info *oi);
189
+
190
+ HSE_poll_res heatshrink_encoder_poll(heatshrink_encoder *hse,
191
+ uint8_t *out_buf, size_t out_buf_size, size_t *output_size) {
192
+ if ((hse == NULL) || (out_buf == NULL) || (output_size == NULL)) {
193
+ return HSER_POLL_ERROR_NULL;
194
+ }
195
+ if (out_buf_size == 0) {
196
+ LOG("-- MISUSE: output buffer size is 0\n");
197
+ return HSER_POLL_ERROR_MISUSE;
198
+ }
199
+ *output_size = 0;
200
+
201
+ output_info oi;
202
+ oi.buf = out_buf;
203
+ oi.buf_size = out_buf_size;
204
+ oi.output_size = output_size;
205
+
206
+ while (1) {
207
+ LOG("-- polling, state %u (%s), flags 0x%02x\n",
208
+ hse->state, state_names[hse->state], hse->flags);
209
+
210
+ uint8_t in_state = hse->state;
211
+ switch (in_state) {
212
+ case HSES_NOT_FULL:
213
+ return HSER_POLL_EMPTY;
214
+ case HSES_FILLED:
215
+ do_indexing(hse);
216
+ hse->state = HSES_SEARCH;
217
+ break;
218
+ case HSES_SEARCH:
219
+ hse->state = st_step_search(hse);
220
+ break;
221
+ case HSES_YIELD_TAG_BIT:
222
+ hse->state = st_yield_tag_bit(hse, &oi);
223
+ break;
224
+ case HSES_YIELD_LITERAL:
225
+ hse->state = st_yield_literal(hse, &oi);
226
+ break;
227
+ case HSES_YIELD_BR_INDEX:
228
+ hse->state = st_yield_br_index(hse, &oi);
229
+ break;
230
+ case HSES_YIELD_BR_LENGTH:
231
+ hse->state = st_yield_br_length(hse, &oi);
232
+ break;
233
+ case HSES_SAVE_BACKLOG:
234
+ hse->state = st_save_backlog(hse);
235
+ break;
236
+ case HSES_FLUSH_BITS:
237
+ hse->state = st_flush_bit_buffer(hse, &oi);
238
+ case HSES_DONE:
239
+ return HSER_POLL_EMPTY;
240
+ default:
241
+ LOG("-- bad state %s\n", state_names[hse->state]);
242
+ return HSER_POLL_ERROR_MISUSE;
243
+ }
244
+
245
+ if (hse->state == in_state) {
246
+ /* Check if output buffer is exhausted. */
247
+ if (*output_size == out_buf_size) return HSER_POLL_MORE;
248
+ }
249
+ }
250
+ }
251
+
252
+ HSE_finish_res heatshrink_encoder_finish(heatshrink_encoder *hse) {
253
+ if (hse == NULL) { return HSER_FINISH_ERROR_NULL; }
254
+ LOG("-- setting is_finishing flag\n");
255
+ hse->flags |= FLAG_IS_FINISHING;
256
+ if (hse->state == HSES_NOT_FULL) { hse->state = HSES_FILLED; }
257
+ return hse->state == HSES_DONE ? HSER_FINISH_DONE : HSER_FINISH_MORE;
258
+ }
259
+
260
+ static HSE_state st_step_search(heatshrink_encoder *hse) {
261
+ uint16_t window_length = get_input_buffer_size(hse);
262
+ uint16_t lookahead_sz = get_lookahead_size(hse);
263
+ uint16_t msi = hse->match_scan_index;
264
+ LOG("## step_search, scan @ +%d (%d/%d), input size %d\n",
265
+ msi, hse->input_size + msi, 2*window_length, hse->input_size);
266
+
267
+ bool fin = is_finishing(hse);
268
+ if (msi > hse->input_size - (fin ? 1 : lookahead_sz)) {
269
+ /* Current search buffer is exhausted, copy it into the
270
+ * backlog and await more input. */
271
+ LOG("-- end of search @ %d\n", msi);
272
+ return fin ? HSES_FLUSH_BITS : HSES_SAVE_BACKLOG;
273
+ }
274
+
275
+ uint16_t input_offset = get_input_offset(hse);
276
+ uint16_t end = input_offset + msi;
277
+ uint16_t start = end - window_length;
278
+
279
+ uint16_t max_possible = lookahead_sz;
280
+ if (hse->input_size - msi < lookahead_sz) {
281
+ max_possible = hse->input_size - msi;
282
+ }
283
+
284
+ uint16_t match_length = 0;
285
+ uint16_t match_pos = find_longest_match(hse,
286
+ start, end, max_possible, &match_length);
287
+
288
+ if (match_pos == MATCH_NOT_FOUND) {
289
+ LOG("ss Match not found\n");
290
+ hse->match_scan_index++;
291
+ hse->match_length = 0;
292
+ return HSES_YIELD_TAG_BIT;
293
+ } else {
294
+ LOG("ss Found match of %d bytes at %d\n", match_length, match_pos);
295
+ hse->match_pos = match_pos;
296
+ hse->match_length = match_length;
297
+ ASSERT(match_pos < 1 << HEATSHRINK_ENCODER_WINDOW_BITS(hse) /*window_length*/);
298
+
299
+ return HSES_YIELD_TAG_BIT;
300
+ }
301
+ }
302
+
303
+ static HSE_state st_yield_tag_bit(heatshrink_encoder *hse,
304
+ output_info *oi) {
305
+ if (can_take_byte(oi)) {
306
+ if (hse->match_length == 0) {
307
+ add_tag_bit(hse, oi, HEATSHRINK_LITERAL_MARKER);
308
+ return HSES_YIELD_LITERAL;
309
+ } else {
310
+ add_tag_bit(hse, oi, HEATSHRINK_BACKREF_MARKER);
311
+ hse->outgoing_bits = hse->match_pos - 1;
312
+ hse->outgoing_bits_count = HEATSHRINK_ENCODER_WINDOW_BITS(hse);
313
+ return HSES_YIELD_BR_INDEX;
314
+ }
315
+ } else {
316
+ return HSES_YIELD_TAG_BIT; /* output is full, continue */
317
+ }
318
+ }
319
+
320
+ static HSE_state st_yield_literal(heatshrink_encoder *hse,
321
+ output_info *oi) {
322
+ if (can_take_byte(oi)) {
323
+ push_literal_byte(hse, oi);
324
+ return HSES_SEARCH;
325
+ } else {
326
+ return HSES_YIELD_LITERAL;
327
+ }
328
+ }
329
+
330
+ static HSE_state st_yield_br_index(heatshrink_encoder *hse,
331
+ output_info *oi) {
332
+ if (can_take_byte(oi)) {
333
+ LOG("-- yielding backref index %u\n", hse->match_pos);
334
+ if (push_outgoing_bits(hse, oi) > 0) {
335
+ return HSES_YIELD_BR_INDEX; /* continue */
336
+ } else {
337
+ hse->outgoing_bits = hse->match_length - 1;
338
+ hse->outgoing_bits_count = HEATSHRINK_ENCODER_LOOKAHEAD_BITS(hse);
339
+ return HSES_YIELD_BR_LENGTH; /* done */
340
+ }
341
+ } else {
342
+ return HSES_YIELD_BR_INDEX; /* continue */
343
+ }
344
+ }
345
+
346
+ static HSE_state st_yield_br_length(heatshrink_encoder *hse,
347
+ output_info *oi) {
348
+ if (can_take_byte(oi)) {
349
+ LOG("-- yielding backref length %u\n", hse->match_length);
350
+ if (push_outgoing_bits(hse, oi) > 0) {
351
+ return HSES_YIELD_BR_LENGTH;
352
+ } else {
353
+ hse->match_scan_index += hse->match_length;
354
+ hse->match_length = 0;
355
+ return HSES_SEARCH;
356
+ }
357
+ } else {
358
+ return HSES_YIELD_BR_LENGTH;
359
+ }
360
+ }
361
+
362
+ static HSE_state st_save_backlog(heatshrink_encoder *hse) {
363
+ LOG("-- saving backlog\n");
364
+ save_backlog(hse);
365
+ return HSES_NOT_FULL;
366
+ }
367
+
368
+ static HSE_state st_flush_bit_buffer(heatshrink_encoder *hse,
369
+ output_info *oi) {
370
+ if (hse->bit_index == 0x80) {
371
+ LOG("-- done!\n");
372
+ return HSES_DONE;
373
+ } else if (can_take_byte(oi)) {
374
+ LOG("-- flushing remaining byte (bit_index == 0x%02x)\n", hse->bit_index);
375
+ oi->buf[(*oi->output_size)++] = hse->current_byte;
376
+ LOG("-- done!\n");
377
+ return HSES_DONE;
378
+ } else {
379
+ return HSES_FLUSH_BITS;
380
+ }
381
+ }
382
+
383
+ static void add_tag_bit(heatshrink_encoder *hse, output_info *oi, uint8_t tag) {
384
+ LOG("-- adding tag bit: %d\n", tag);
385
+ push_bits(hse, 1, tag, oi);
386
+ }
387
+
388
+ static uint16_t get_input_offset(heatshrink_encoder *hse) {
389
+ return get_input_buffer_size(hse);
390
+ }
391
+
392
+ static uint16_t get_input_buffer_size(heatshrink_encoder *hse) {
393
+ return (1 << HEATSHRINK_ENCODER_WINDOW_BITS(hse));
394
+ (void)hse;
395
+ }
396
+
397
+ static uint16_t get_lookahead_size(heatshrink_encoder *hse) {
398
+ return (1 << HEATSHRINK_ENCODER_LOOKAHEAD_BITS(hse));
399
+ (void)hse;
400
+ }
401
+
402
+ static void do_indexing(heatshrink_encoder *hse) {
403
+ #if HEATSHRINK_USE_INDEX
404
+ /* Build an index array I that contains flattened linked lists
405
+ * for the previous instances of every byte in the buffer.
406
+ *
407
+ * For example, if buf[200] == 'x', then index[200] will either
408
+ * be an offset i such that buf[i] == 'x', or a negative offset
409
+ * to indicate end-of-list. This significantly speeds up matching,
410
+ * while only using sizeof(uint16_t)*sizeof(buffer) bytes of RAM.
411
+ *
412
+ * Future optimization options:
413
+ * 1. Since any negative value represents end-of-list, the other
414
+ * 15 bits could be used to improve the index dynamically.
415
+ *
416
+ * 2. Likewise, the last lookahead_sz bytes of the index will
417
+ * not be usable, so temporary data could be stored there to
418
+ * dynamically improve the index.
419
+ * */
420
+ struct hs_index *hsi = HEATSHRINK_ENCODER_INDEX(hse);
421
+ int16_t last[256];
422
+ memset(last, 0xFF, sizeof(last));
423
+
424
+ uint8_t * const data = hse->buffer;
425
+ int16_t * const index = hsi->index;
426
+
427
+ const uint16_t input_offset = get_input_offset(hse);
428
+ const uint16_t end = input_offset + hse->input_size;
429
+
430
+ for (uint16_t i=0; i<end; i++) {
431
+ uint8_t v = data[i];
432
+ int16_t lv = last[v];
433
+ index[i] = lv;
434
+ last[v] = i;
435
+ }
436
+ #else
437
+ (void)hse;
438
+ #endif
439
+ }
440
+
441
+ static int is_finishing(heatshrink_encoder *hse) {
442
+ return hse->flags & FLAG_IS_FINISHING;
443
+ }
444
+
445
+ static int can_take_byte(output_info *oi) {
446
+ return *oi->output_size < oi->buf_size;
447
+ }
448
+
449
+ /* Return the longest match for the bytes at buf[end:end+maxlen] between
450
+ * buf[start] and buf[end-1]. If no match is found, return -1. */
451
+ static uint16_t find_longest_match(heatshrink_encoder *hse, uint16_t start,
452
+ uint16_t end, const uint16_t maxlen, uint16_t *match_length) {
453
+ LOG("-- scanning for match of buf[%u:%u] between buf[%u:%u] (max %u bytes)\n",
454
+ end, end + maxlen, start, end + maxlen - 1, maxlen);
455
+ uint8_t *buf = hse->buffer;
456
+
457
+ uint16_t match_maxlen = 0;
458
+ uint16_t match_index = MATCH_NOT_FOUND;
459
+
460
+ uint16_t len = 0;
461
+ uint8_t * const needlepoint = &buf[end];
462
+ #if HEATSHRINK_USE_INDEX
463
+ struct hs_index *hsi = HEATSHRINK_ENCODER_INDEX(hse);
464
+ int16_t pos = hsi->index[end];
465
+
466
+ while (pos - (int16_t)start >= 0) {
467
+ uint8_t * const pospoint = &buf[pos];
468
+ len = 0;
469
+
470
+ /* Only check matches that will potentially beat the current maxlen.
471
+ * This is redundant with the index if match_maxlen is 0, but the
472
+ * added branch overhead to check if it == 0 seems to be worse. */
473
+ if (pospoint[match_maxlen] != needlepoint[match_maxlen]) {
474
+ pos = hsi->index[pos];
475
+ continue;
476
+ }
477
+
478
+ for (len = 1; len < maxlen; len++) {
479
+ if (pospoint[len] != needlepoint[len]) break;
480
+ }
481
+
482
+ if (len > match_maxlen) {
483
+ match_maxlen = len;
484
+ match_index = pos;
485
+ if (len == maxlen) { break; } /* won't find better */
486
+ }
487
+ pos = hsi->index[pos];
488
+ }
489
+ #else
490
+ for (int16_t pos=end - 1; pos - (int16_t)start >= 0; pos--) {
491
+ uint8_t * const pospoint = &buf[pos];
492
+ if ((pospoint[match_maxlen] == needlepoint[match_maxlen])
493
+ && (*pospoint == *needlepoint)) {
494
+ for (len=1; len<maxlen; len++) {
495
+ if (0) {
496
+ LOG(" --> cmp buf[%d] == 0x%02x against %02x (start %u)\n",
497
+ pos + len, pospoint[len], needlepoint[len], start);
498
+ }
499
+ if (pospoint[len] != needlepoint[len]) { break; }
500
+ }
501
+ if (len > match_maxlen) {
502
+ match_maxlen = len;
503
+ match_index = pos;
504
+ if (len == maxlen) { break; } /* don't keep searching */
505
+ }
506
+ }
507
+ }
508
+ #endif
509
+
510
+ const size_t break_even_point =
511
+ (1 + HEATSHRINK_ENCODER_WINDOW_BITS(hse) +
512
+ HEATSHRINK_ENCODER_LOOKAHEAD_BITS(hse));
513
+
514
+ /* Instead of comparing break_even_point against 8*match_maxlen,
515
+ * compare match_maxlen against break_even_point/8 to avoid
516
+ * overflow. Since MIN_WINDOW_BITS and MIN_LOOKAHEAD_BITS are 4 and
517
+ * 3, respectively, break_even_point/8 will always be at least 1. */
518
+ if (match_maxlen > (break_even_point / 8)) {
519
+ LOG("-- best match: %u bytes at -%u\n",
520
+ match_maxlen, end - match_index);
521
+ *match_length = match_maxlen;
522
+ return end - match_index;
523
+ }
524
+ LOG("-- none found\n");
525
+ return MATCH_NOT_FOUND;
526
+ }
527
+
528
+ static uint8_t push_outgoing_bits(heatshrink_encoder *hse, output_info *oi) {
529
+ uint8_t count = 0;
530
+ uint8_t bits = 0;
531
+ if (hse->outgoing_bits_count > 8) {
532
+ count = 8;
533
+ bits = hse->outgoing_bits >> (hse->outgoing_bits_count - 8);
534
+ } else {
535
+ count = hse->outgoing_bits_count;
536
+ bits = hse->outgoing_bits;
537
+ }
538
+
539
+ if (count > 0) {
540
+ LOG("-- pushing %d outgoing bits: 0x%02x\n", count, bits);
541
+ push_bits(hse, count, bits, oi);
542
+ hse->outgoing_bits_count -= count;
543
+ }
544
+ return count;
545
+ }
546
+
547
+ /* Push COUNT (max 8) bits to the output buffer, which has room.
548
+ * Bytes are set from the lowest bits, up. */
549
+ static void push_bits(heatshrink_encoder *hse, uint8_t count, uint8_t bits,
550
+ output_info *oi) {
551
+ ASSERT(count <= 8);
552
+ LOG("++ push_bits: %d bits, input of 0x%02x\n", count, bits);
553
+
554
+ /* If adding a whole byte and at the start of a new output byte,
555
+ * just push it through whole and skip the bit IO loop. */
556
+ if (count == 8 && hse->bit_index == 0x80) {
557
+ oi->buf[(*oi->output_size)++] = bits;
558
+ } else {
559
+ for (int i=count - 1; i>=0; i--) {
560
+ bool bit = bits & (1 << i);
561
+ if (bit) { hse->current_byte |= hse->bit_index; }
562
+ if (0) {
563
+ LOG(" -- setting bit %d at bit index 0x%02x, byte => 0x%02x\n",
564
+ bit ? 1 : 0, hse->bit_index, hse->current_byte);
565
+ }
566
+ hse->bit_index >>= 1;
567
+ if (hse->bit_index == 0x00) {
568
+ hse->bit_index = 0x80;
569
+ LOG(" > pushing byte 0x%02x\n", hse->current_byte);
570
+ oi->buf[(*oi->output_size)++] = hse->current_byte;
571
+ hse->current_byte = 0x00;
572
+ }
573
+ }
574
+ }
575
+ }
576
+
577
+ static void push_literal_byte(heatshrink_encoder *hse, output_info *oi) {
578
+ uint16_t processed_offset = hse->match_scan_index - 1;
579
+ uint16_t input_offset = get_input_offset(hse) + processed_offset;
580
+ uint8_t c = hse->buffer[input_offset];
581
+ LOG("-- yielded literal byte 0x%02x ('%c') from +%d\n",
582
+ c, isprint(c) ? c : '.', input_offset);
583
+ push_bits(hse, 8, c, oi);
584
+ }
585
+
586
+ static void save_backlog(heatshrink_encoder *hse) {
587
+ size_t input_buf_sz = get_input_buffer_size(hse);
588
+
589
+ uint16_t msi = hse->match_scan_index;
590
+
591
+ /* Copy processed data to beginning of buffer, so it can be
592
+ * used for future matches. Don't bother checking whether the
593
+ * input is less than the maximum size, because if it isn't,
594
+ * we're done anyway. */
595
+ uint16_t rem = input_buf_sz - msi; // unprocessed bytes
596
+ uint16_t shift_sz = input_buf_sz + rem;
597
+
598
+ memmove(&hse->buffer[0],
599
+ &hse->buffer[input_buf_sz - rem],
600
+ shift_sz);
601
+
602
+ hse->match_scan_index = 0;
603
+ hse->input_size -= input_buf_sz - rem;
604
+ }