heatshrink_decoder.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "heatshrink_decoder.h"
  4. /* States for the polling state machine. */
  5. typedef enum {
  6. HSDS_TAG_BIT, /* tag bit */
  7. HSDS_YIELD_LITERAL, /* ready to yield literal byte */
  8. HSDS_BACKREF_INDEX_MSB, /* most significant byte of index */
  9. HSDS_BACKREF_INDEX_LSB, /* least significant byte of index */
  10. HSDS_BACKREF_COUNT_MSB, /* most significant byte of count */
  11. HSDS_BACKREF_COUNT_LSB, /* least significant byte of count */
  12. HSDS_YIELD_BACKREF, /* ready to yield back-reference */
  13. } HSD_state;
  14. #if HEATSHRINK_DEBUGGING_LOGS
  15. #include <stdio.h>
  16. #include <ctype.h>
  17. #include <assert.h>
  18. #define LOG(...) fprintf(stderr, __VA_ARGS__)
  19. #define ASSERT(X) assert(X)
  20. static const char *state_names[] = {
  21. "tag_bit",
  22. "yield_literal",
  23. "backref_index_msb",
  24. "backref_index_lsb",
  25. "backref_count_msb",
  26. "backref_count_lsb",
  27. "yield_backref",
  28. };
  29. #else
  30. #define LOG(...) /* no-op */
  31. #define ASSERT(X) /* no-op */
  32. #endif
  33. typedef struct {
  34. uint8_t *buf; /* output buffer */
  35. size_t buf_size; /* buffer size */
  36. size_t *output_size; /* bytes pushed to buffer, so far */
  37. } output_info;
  38. #define NO_BITS ((uint16_t)-1)
  39. /* Forward references. */
  40. static uint16_t get_bits(heatshrink_decoder *hsd, uint8_t count);
  41. static void push_byte(heatshrink_decoder *hsd, output_info *oi, uint8_t byte);
  42. #if HEATSHRINK_DYNAMIC_ALLOC
  43. heatshrink_decoder *heatshrink_decoder_alloc(uint8_t* buffer,
  44. uint16_t input_buffer_size,
  45. uint8_t window_sz2,
  46. uint8_t lookahead_sz2) {
  47. if ((window_sz2 < HEATSHRINK_MIN_WINDOW_BITS) ||
  48. (window_sz2 > HEATSHRINK_MAX_WINDOW_BITS) ||
  49. (input_buffer_size == 0) ||
  50. (lookahead_sz2 < HEATSHRINK_MIN_LOOKAHEAD_BITS) ||
  51. (lookahead_sz2 >= window_sz2)) {
  52. return NULL;
  53. }
  54. size_t sz = sizeof(heatshrink_decoder);
  55. heatshrink_decoder *hsd = HEATSHRINK_MALLOC(sz);
  56. if (hsd == NULL) { return NULL; }
  57. hsd->input_buffer_size = input_buffer_size;
  58. hsd->window_sz2 = window_sz2;
  59. hsd->lookahead_sz2 = lookahead_sz2;
  60. hsd->buffers = buffer;
  61. heatshrink_decoder_reset(hsd);
  62. LOG("-- allocated decoder with buffer size of %zu (%zu + %u + %u)\n",
  63. sz, sizeof(heatshrink_decoder), (1 << window_sz2), input_buffer_size);
  64. return hsd;
  65. }
  66. void heatshrink_decoder_free(heatshrink_decoder *hsd) {
  67. size_t sz = sizeof(heatshrink_decoder);
  68. HEATSHRINK_FREE(hsd, sz);
  69. (void)sz; /* may not be used by free */
  70. }
  71. #endif
  72. void heatshrink_decoder_reset(heatshrink_decoder *hsd) {
  73. hsd->state = HSDS_TAG_BIT;
  74. hsd->input_size = 0;
  75. hsd->input_index = 0;
  76. hsd->bit_index = 0x00;
  77. hsd->current_byte = 0x00;
  78. hsd->output_count = 0;
  79. hsd->output_index = 0;
  80. hsd->head_index = 0;
  81. }
  82. /* Copy SIZE bytes into the decoder's input buffer, if it will fit. */
  83. HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd,
  84. uint8_t *in_buf, size_t size, size_t *input_size) {
  85. if ((hsd == NULL) || (in_buf == NULL) || (input_size == NULL)) {
  86. return HSDR_SINK_ERROR_NULL;
  87. }
  88. size_t rem = HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd) - hsd->input_size;
  89. if (rem == 0) {
  90. *input_size = 0;
  91. return HSDR_SINK_FULL;
  92. }
  93. size = rem < size ? rem : size;
  94. LOG("-- sinking %zd bytes\n", size);
  95. /* copy into input buffer (at head of buffers) */
  96. memcpy(&hsd->buffers[hsd->input_size], in_buf, size);
  97. hsd->input_size += size;
  98. *input_size = size;
  99. return HSDR_SINK_OK;
  100. }
  101. /*****************
  102. * Decompression *
  103. *****************/
  104. #define BACKREF_COUNT_BITS(HSD) (HEATSHRINK_DECODER_LOOKAHEAD_BITS(HSD))
  105. #define BACKREF_INDEX_BITS(HSD) (HEATSHRINK_DECODER_WINDOW_BITS(HSD))
  106. // States
  107. static HSD_state st_tag_bit(heatshrink_decoder *hsd);
  108. static HSD_state st_yield_literal(heatshrink_decoder *hsd,
  109. output_info *oi);
  110. static HSD_state st_backref_index_msb(heatshrink_decoder *hsd);
  111. static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd);
  112. static HSD_state st_backref_count_msb(heatshrink_decoder *hsd);
  113. static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd);
  114. static HSD_state st_yield_backref(heatshrink_decoder *hsd,
  115. output_info *oi);
  116. HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd,
  117. uint8_t *out_buf, size_t out_buf_size, size_t *output_size) {
  118. if ((hsd == NULL) || (out_buf == NULL) || (output_size == NULL)) {
  119. return HSDR_POLL_ERROR_NULL;
  120. }
  121. *output_size = 0;
  122. output_info oi;
  123. oi.buf = out_buf;
  124. oi.buf_size = out_buf_size;
  125. oi.output_size = output_size;
  126. while (1) {
  127. LOG("-- poll, state is %d (%s), input_size %d\n",
  128. hsd->state, state_names[hsd->state], hsd->input_size);
  129. uint8_t in_state = hsd->state;
  130. switch (in_state) {
  131. case HSDS_TAG_BIT:
  132. hsd->state = st_tag_bit(hsd);
  133. break;
  134. case HSDS_YIELD_LITERAL:
  135. hsd->state = st_yield_literal(hsd, &oi);
  136. break;
  137. case HSDS_BACKREF_INDEX_MSB:
  138. hsd->state = st_backref_index_msb(hsd);
  139. break;
  140. case HSDS_BACKREF_INDEX_LSB:
  141. hsd->state = st_backref_index_lsb(hsd);
  142. break;
  143. case HSDS_BACKREF_COUNT_MSB:
  144. hsd->state = st_backref_count_msb(hsd);
  145. break;
  146. case HSDS_BACKREF_COUNT_LSB:
  147. hsd->state = st_backref_count_lsb(hsd);
  148. break;
  149. case HSDS_YIELD_BACKREF:
  150. hsd->state = st_yield_backref(hsd, &oi);
  151. break;
  152. default:
  153. return HSDR_POLL_ERROR_UNKNOWN;
  154. }
  155. /* If the current state cannot advance, check if input or output
  156. * buffer are exhausted. */
  157. if (hsd->state == in_state) {
  158. if (*output_size == out_buf_size) { return HSDR_POLL_MORE; }
  159. return HSDR_POLL_EMPTY;
  160. }
  161. }
  162. }
  163. static HSD_state st_tag_bit(heatshrink_decoder *hsd) {
  164. uint32_t bits = get_bits(hsd, 1); // get tag bit
  165. if (bits == NO_BITS) {
  166. return HSDS_TAG_BIT;
  167. } else if (bits) {
  168. return HSDS_YIELD_LITERAL;
  169. } else if (HEATSHRINK_DECODER_WINDOW_BITS(hsd) > 8) {
  170. return HSDS_BACKREF_INDEX_MSB;
  171. } else {
  172. hsd->output_index = 0;
  173. return HSDS_BACKREF_INDEX_LSB;
  174. }
  175. }
  176. static HSD_state st_yield_literal(heatshrink_decoder *hsd,
  177. output_info *oi) {
  178. /* Emit a repeated section from the window buffer, and add it (again)
  179. * to the window buffer. (Note that the repetition can include
  180. * itself.)*/
  181. if (*oi->output_size < oi->buf_size) {
  182. uint16_t byte = get_bits(hsd, 8);
  183. if (byte == NO_BITS) { return HSDS_YIELD_LITERAL; } /* out of input */
  184. uint8_t *buf = &hsd->buffers[HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd)];
  185. uint16_t mask = (1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd)) - 1;
  186. uint8_t c = byte & 0xFF;
  187. LOG("-- emitting literal byte 0x%02x ('%c')\n", c, isprint(c) ? c : '.');
  188. buf[hsd->head_index++ & mask] = c;
  189. push_byte(hsd, oi, c);
  190. return HSDS_TAG_BIT;
  191. } else {
  192. return HSDS_YIELD_LITERAL;
  193. }
  194. }
  195. static HSD_state st_backref_index_msb(heatshrink_decoder *hsd) {
  196. uint8_t bit_ct = BACKREF_INDEX_BITS(hsd);
  197. ASSERT(bit_ct > 8);
  198. uint16_t bits = get_bits(hsd, bit_ct - 8);
  199. LOG("-- backref index (msb), got 0x%04x (+1)\n", bits);
  200. if (bits == NO_BITS) { return HSDS_BACKREF_INDEX_MSB; }
  201. hsd->output_index = bits << 8;
  202. return HSDS_BACKREF_INDEX_LSB;
  203. }
  204. static HSD_state st_backref_index_lsb(heatshrink_decoder *hsd) {
  205. uint8_t bit_ct = BACKREF_INDEX_BITS(hsd);
  206. uint16_t bits = get_bits(hsd, bit_ct < 8 ? bit_ct : 8);
  207. LOG("-- backref index (lsb), got 0x%04x (+1)\n", bits);
  208. if (bits == NO_BITS) { return HSDS_BACKREF_INDEX_LSB; }
  209. hsd->output_index |= bits;
  210. hsd->output_index++;
  211. uint8_t br_bit_ct = BACKREF_COUNT_BITS(hsd);
  212. hsd->output_count = 0;
  213. return (br_bit_ct > 8) ? HSDS_BACKREF_COUNT_MSB : HSDS_BACKREF_COUNT_LSB;
  214. }
  215. static HSD_state st_backref_count_msb(heatshrink_decoder *hsd) {
  216. uint8_t br_bit_ct = BACKREF_COUNT_BITS(hsd);
  217. ASSERT(br_bit_ct > 8);
  218. uint16_t bits = get_bits(hsd, br_bit_ct - 8);
  219. LOG("-- backref count (msb), got 0x%04x (+1)\n", bits);
  220. if (bits == NO_BITS) { return HSDS_BACKREF_COUNT_MSB; }
  221. hsd->output_count = bits << 8;
  222. return HSDS_BACKREF_COUNT_LSB;
  223. }
  224. static HSD_state st_backref_count_lsb(heatshrink_decoder *hsd) {
  225. uint8_t br_bit_ct = BACKREF_COUNT_BITS(hsd);
  226. uint16_t bits = get_bits(hsd, br_bit_ct < 8 ? br_bit_ct : 8);
  227. LOG("-- backref count (lsb), got 0x%04x (+1)\n", bits);
  228. if (bits == NO_BITS) { return HSDS_BACKREF_COUNT_LSB; }
  229. hsd->output_count |= bits;
  230. hsd->output_count++;
  231. return HSDS_YIELD_BACKREF;
  232. }
  233. static HSD_state st_yield_backref(heatshrink_decoder *hsd,
  234. output_info *oi) {
  235. size_t count = oi->buf_size - *oi->output_size;
  236. if (count > 0) {
  237. size_t i = 0;
  238. if (hsd->output_count < count) count = hsd->output_count;
  239. uint8_t *buf = &hsd->buffers[HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(hsd)];
  240. uint16_t mask = (1 << HEATSHRINK_DECODER_WINDOW_BITS(hsd)) - 1;
  241. uint16_t neg_offset = hsd->output_index;
  242. LOG("-- emitting %zu bytes from -%u bytes back\n", count, neg_offset);
  243. ASSERT(neg_offset <= mask + 1);
  244. ASSERT(count <= (size_t)(1 << BACKREF_COUNT_BITS(hsd)));
  245. for (i=0; i<count; i++) {
  246. uint8_t c = buf[(hsd->head_index - neg_offset) & mask];
  247. push_byte(hsd, oi, c);
  248. buf[hsd->head_index & mask] = c;
  249. hsd->head_index++;
  250. LOG(" -- ++ 0x%02x\n", c);
  251. }
  252. hsd->output_count -= count;
  253. if (hsd->output_count == 0) { return HSDS_TAG_BIT; }
  254. }
  255. return HSDS_YIELD_BACKREF;
  256. }
  257. /* Get the next COUNT bits from the input buffer, saving incremental progress.
  258. * Returns NO_BITS on end of input, or if more than 15 bits are requested. */
  259. static uint16_t get_bits(heatshrink_decoder *hsd, uint8_t count) {
  260. uint16_t accumulator = 0;
  261. int i = 0;
  262. if (count > 15) { return NO_BITS; }
  263. LOG("-- popping %u bit(s)\n", count);
  264. /* If we aren't able to get COUNT bits, suspend immediately, because we
  265. * don't track how many bits of COUNT we've accumulated before suspend. */
  266. if (hsd->input_size == 0) {
  267. if (hsd->bit_index < (1 << (count - 1))) { return NO_BITS; }
  268. }
  269. for (i = 0; i < count; i++) {
  270. if (hsd->bit_index == 0x00) {
  271. if (hsd->input_size == 0) {
  272. LOG(" -- out of bits, suspending w/ accumulator of %u (0x%02x)\n",
  273. accumulator, accumulator);
  274. return NO_BITS;
  275. }
  276. hsd->current_byte = hsd->buffers[hsd->input_index++];
  277. LOG(" -- pulled byte 0x%02x\n", hsd->current_byte);
  278. if (hsd->input_index == hsd->input_size) {
  279. hsd->input_index = 0; /* input is exhausted */
  280. hsd->input_size = 0;
  281. }
  282. hsd->bit_index = 0x80;
  283. }
  284. accumulator <<= 1;
  285. if (hsd->current_byte & hsd->bit_index) {
  286. accumulator |= 0x01;
  287. if (0) {
  288. LOG(" -- got 1, accumulator 0x%04x, bit_index 0x%02x\n",
  289. accumulator, hsd->bit_index);
  290. }
  291. } else {
  292. if (0) {
  293. LOG(" -- got 0, accumulator 0x%04x, bit_index 0x%02x\n",
  294. accumulator, hsd->bit_index);
  295. }
  296. }
  297. hsd->bit_index >>= 1;
  298. }
  299. if (count > 1) { LOG(" -- accumulated %08x\n", accumulator); }
  300. return accumulator;
  301. }
  302. HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd) {
  303. if (hsd == NULL) { return HSDR_FINISH_ERROR_NULL; }
  304. switch (hsd->state) {
  305. case HSDS_TAG_BIT:
  306. return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
  307. /* If we want to finish with no input, but are in these states, it's
  308. * because the 0-bit padding to the last byte looks like a backref
  309. * marker bit followed by all 0s for index and count bits. */
  310. case HSDS_BACKREF_INDEX_LSB:
  311. case HSDS_BACKREF_INDEX_MSB:
  312. case HSDS_BACKREF_COUNT_LSB:
  313. case HSDS_BACKREF_COUNT_MSB:
  314. return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
  315. /* If the output stream is padded with 0xFFs (possibly due to being in
  316. * flash memory), also explicitly check the input size rather than
  317. * uselessly returning MORE but yielding 0 bytes when polling. */
  318. case HSDS_YIELD_LITERAL:
  319. return hsd->input_size == 0 ? HSDR_FINISH_DONE : HSDR_FINISH_MORE;
  320. default:
  321. return HSDR_FINISH_MORE;
  322. }
  323. }
  324. static void push_byte(heatshrink_decoder *hsd, output_info *oi, uint8_t byte) {
  325. LOG(" -- pushing byte: 0x%02x ('%c')\n", byte, isprint(byte) ? byte : '.');
  326. oi->buf[(*oi->output_size)++] = byte;
  327. (void)hsd;
  328. }