heatshrink_decoder.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #ifndef HEATSHRINK_DECODER_H
  2. #define HEATSHRINK_DECODER_H
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #include "heatshrink_common.h"
  6. #include "heatshrink_config.h"
  7. typedef enum {
  8. HSDR_SINK_OK, /* data sunk, ready to poll */
  9. HSDR_SINK_FULL, /* out of space in internal buffer */
  10. HSDR_SINK_ERROR_NULL=-1, /* NULL argument */
  11. } HSD_sink_res;
  12. typedef enum {
  13. HSDR_POLL_EMPTY, /* input exhausted */
  14. HSDR_POLL_MORE, /* more data remaining, call again w/ fresh output buffer */
  15. HSDR_POLL_ERROR_NULL=-1, /* NULL arguments */
  16. HSDR_POLL_ERROR_UNKNOWN=-2,
  17. } HSD_poll_res;
  18. typedef enum {
  19. HSDR_FINISH_DONE, /* output is done */
  20. HSDR_FINISH_MORE, /* more output remains */
  21. HSDR_FINISH_ERROR_NULL=-1, /* NULL arguments */
  22. } HSD_finish_res;
  23. #if HEATSHRINK_DYNAMIC_ALLOC
  24. #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(BUF) \
  25. ((BUF)->input_buffer_size)
  26. #define HEATSHRINK_DECODER_WINDOW_BITS(BUF) \
  27. ((BUF)->window_sz2)
  28. #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
  29. ((BUF)->lookahead_sz2)
  30. #else
  31. #define HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_) \
  32. HEATSHRINK_STATIC_INPUT_BUFFER_SIZE
  33. #define HEATSHRINK_DECODER_WINDOW_BITS(_) \
  34. (HEATSHRINK_STATIC_WINDOW_BITS)
  35. #define HEATSHRINK_DECODER_LOOKAHEAD_BITS(BUF) \
  36. (HEATSHRINK_STATIC_LOOKAHEAD_BITS)
  37. #endif
  38. typedef struct {
  39. uint16_t input_size; /* bytes in input buffer */
  40. uint16_t input_index; /* offset to next unprocessed input byte */
  41. uint16_t output_count; /* how many bytes to output */
  42. uint16_t output_index; /* index for bytes to output */
  43. uint16_t head_index; /* head of window buffer */
  44. uint8_t state; /* current state machine node */
  45. uint8_t current_byte; /* current byte of input */
  46. uint8_t bit_index; /* current bit index */
  47. #if HEATSHRINK_DYNAMIC_ALLOC
  48. /* Fields that are only used if dynamically allocated. */
  49. uint8_t window_sz2; /* window buffer bits */
  50. uint8_t lookahead_sz2; /* lookahead bits */
  51. uint16_t input_buffer_size; /* input buffer size */
  52. /* Input buffer, then expansion window buffer */
  53. uint8_t* buffers;
  54. #else
  55. /* Input buffer, then expansion window buffer */
  56. uint8_t buffers[(1 << HEATSHRINK_DECODER_WINDOW_BITS(_))
  57. + HEATSHRINK_DECODER_INPUT_BUFFER_SIZE(_)];
  58. #endif
  59. } heatshrink_decoder;
  60. #if HEATSHRINK_DYNAMIC_ALLOC
  61. /* Allocate a decoder with an input buffer of INPUT_BUFFER_SIZE bytes,
  62. * an expansion buffer size of 2^WINDOW_SZ2, and a lookahead
  63. * size of 2^lookahead_sz2. (The window buffer and lookahead sizes
  64. * must match the settings used when the data was compressed.)
  65. * Returns NULL on error. */
  66. heatshrink_decoder *heatshrink_decoder_alloc(uint8_t* buffer, uint16_t input_buffer_size,
  67. uint8_t expansion_buffer_sz2, uint8_t lookahead_sz2);
  68. /* Free a decoder. */
  69. void heatshrink_decoder_free(heatshrink_decoder *hsd);
  70. #endif
  71. /* Reset a decoder. */
  72. void heatshrink_decoder_reset(heatshrink_decoder *hsd);
  73. /* Sink at most SIZE bytes from IN_BUF into the decoder. *INPUT_SIZE is set to
  74. * indicate how many bytes were actually sunk (in case a buffer was filled). */
  75. HSD_sink_res heatshrink_decoder_sink(heatshrink_decoder *hsd,
  76. uint8_t *in_buf, size_t size, size_t *input_size);
  77. /* Poll for output from the decoder, copying at most OUT_BUF_SIZE bytes into
  78. * OUT_BUF (setting *OUTPUT_SIZE to the actual amount copied). */
  79. HSD_poll_res heatshrink_decoder_poll(heatshrink_decoder *hsd,
  80. uint8_t *out_buf, size_t out_buf_size, size_t *output_size);
  81. /* Notify the dencoder that the input stream is finished.
  82. * If the return value is HSDR_FINISH_MORE, there is still more output, so
  83. * call heatshrink_decoder_poll and repeat. */
  84. HSD_finish_res heatshrink_decoder_finish(heatshrink_decoder *hsd);
  85. #endif