heatshrink_encoder.h 3.5 KB

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