decode.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* API for Brotli decompression */
  6. #ifndef BROTLI_DEC_DECODE_H_
  7. #define BROTLI_DEC_DECODE_H_
  8. #include "../common/types.h"
  9. #if defined(__cplusplus) || defined(c_plusplus)
  10. extern "C" {
  11. #endif
  12. typedef struct BrotliStateStruct BrotliState;
  13. typedef enum {
  14. /* Decoding error, e.g. corrupt input or memory allocation problem */
  15. BROTLI_RESULT_ERROR = 0,
  16. /* Decoding successfully completed */
  17. BROTLI_RESULT_SUCCESS = 1,
  18. /* Partially done; should be called again with more input */
  19. BROTLI_RESULT_NEEDS_MORE_INPUT = 2,
  20. /* Partially done; should be called again with more output */
  21. BROTLI_RESULT_NEEDS_MORE_OUTPUT = 3
  22. } BrotliResult;
  23. #define BROTLI_ERROR_CODES_LIST(BROTLI_ERROR_CODE, SEPARATOR) \
  24. BROTLI_ERROR_CODE(_, NO_ERROR, 0) SEPARATOR \
  25. /* Same as BrotliResult values */ \
  26. BROTLI_ERROR_CODE(_, SUCCESS, 1) SEPARATOR \
  27. BROTLI_ERROR_CODE(_, NEEDS_MORE_INPUT, 2) SEPARATOR \
  28. BROTLI_ERROR_CODE(_, NEEDS_MORE_OUTPUT, 3) SEPARATOR \
  29. \
  30. /* Errors caused by invalid input */ \
  31. BROTLI_ERROR_CODE(_ERROR_FORMAT_, EXUBERANT_NIBBLE, -1) SEPARATOR \
  32. BROTLI_ERROR_CODE(_ERROR_FORMAT_, RESERVED, -2) SEPARATOR \
  33. BROTLI_ERROR_CODE(_ERROR_FORMAT_, EXUBERANT_META_NIBBLE, -3) SEPARATOR \
  34. BROTLI_ERROR_CODE(_ERROR_FORMAT_, SIMPLE_HUFFMAN_ALPHABET, -4) SEPARATOR \
  35. BROTLI_ERROR_CODE(_ERROR_FORMAT_, SIMPLE_HUFFMAN_SAME, -5) SEPARATOR \
  36. BROTLI_ERROR_CODE(_ERROR_FORMAT_, CL_SPACE, -6) SEPARATOR \
  37. BROTLI_ERROR_CODE(_ERROR_FORMAT_, HUFFMAN_SPACE, -7) SEPARATOR \
  38. BROTLI_ERROR_CODE(_ERROR_FORMAT_, CONTEXT_MAP_REPEAT, -8) SEPARATOR \
  39. BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_LENGTH_1, -9) SEPARATOR \
  40. BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_LENGTH_2, -10) SEPARATOR \
  41. BROTLI_ERROR_CODE(_ERROR_FORMAT_, TRANSFORM, -11) SEPARATOR \
  42. BROTLI_ERROR_CODE(_ERROR_FORMAT_, DICTIONARY, -12) SEPARATOR \
  43. BROTLI_ERROR_CODE(_ERROR_FORMAT_, WINDOW_BITS, -13) SEPARATOR \
  44. BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_1, -14) SEPARATOR \
  45. BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_2, -15) SEPARATOR \
  46. \
  47. /* -16..-20 codes are reserved */ \
  48. \
  49. /* Memory allocation problems */ \
  50. BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MODES, -21) SEPARATOR \
  51. /* Literal, insert and distance trees together */ \
  52. BROTLI_ERROR_CODE(_ERROR_ALLOC_, TREE_GROUPS, -22) SEPARATOR \
  53. /* -23..-24 codes are reserved for distinct tree groups */ \
  54. BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MAP, -25) SEPARATOR \
  55. BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_1, -26) SEPARATOR \
  56. BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_2, -27) SEPARATOR \
  57. /* -28..-29 codes are reserved for dynamic ringbuffer allocation */ \
  58. BROTLI_ERROR_CODE(_ERROR_ALLOC_, BLOCK_TYPE_TREES, -30) SEPARATOR \
  59. \
  60. /* "Impossible" states */ \
  61. BROTLI_ERROR_CODE(_ERROR_, UNREACHABLE, -31)
  62. typedef enum {
  63. #define _BROTLI_COMMA ,
  64. #define _BROTLI_ERROR_CODE_ENUM_ITEM(PREFIX, NAME, CODE) \
  65. BROTLI ## PREFIX ## NAME = CODE
  66. BROTLI_ERROR_CODES_LIST(_BROTLI_ERROR_CODE_ENUM_ITEM, _BROTLI_COMMA)
  67. #undef _BROTLI_ERROR_CODE_ENUM_ITEM
  68. #undef _BROTLI_COMMA
  69. } BrotliErrorCode;
  70. #define BROTLI_LAST_ERROR_CODE BROTLI_ERROR_UNREACHABLE
  71. /* Creates the instance of BrotliState and initializes it. |alloc_func| and
  72. |free_func| MUST be both zero or both non-zero. In the case they are both
  73. zero, default memory allocators are used. |opaque| is passed to |alloc_func|
  74. and |free_func| when they are called. */
  75. BrotliState* BrotliCreateState(
  76. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
  77. /* Deinitializes and frees BrotliState instance. */
  78. void BrotliDestroyState(BrotliState* state);
  79. /* Sets |*decoded_size| to the decompressed size of the given encoded stream.
  80. This function only works if the only compressed block, is last block.
  81. There is no limit on number of uncompressed and metadata blocks.
  82. Returns 1 on success, 0 on failure. */
  83. int BrotliDecompressedSize(size_t encoded_size,
  84. const uint8_t* encoded_buffer,
  85. size_t* decoded_size);
  86. /* Decompresses the data in |encoded_buffer| into |decoded_buffer|, and sets
  87. |*decoded_size| to the decompressed length. */
  88. BrotliResult BrotliDecompressBuffer(size_t encoded_size,
  89. const uint8_t* encoded_buffer,
  90. size_t* decoded_size,
  91. uint8_t* decoded_buffer);
  92. /* Decompresses the data. Supports partial input and output.
  93. Must be called with an allocated input buffer in |*next_in| and an allocated
  94. output buffer in |*next_out|. The values |*available_in| and |*available_out|
  95. must specify the allocated size in |*next_in| and |*next_out| respectively.
  96. After each call, |*available_in| will be decremented by the amount of input
  97. bytes consumed, and the |*next_in| pointer will be incremented by that
  98. amount. Similarly, |*available_out| will be decremented by the amount of
  99. output bytes written, and the |*next_out| pointer will be incremented by that
  100. amount. |total_out|, if it is not a null-pointer, will be set to the number
  101. of bytes decompressed since the last state initialization.
  102. Input is never overconsumed, so |next_in| and |available_in| could be passed
  103. to the next consumer after decoding is complete. */
  104. BrotliResult BrotliDecompressStream(size_t* available_in,
  105. const uint8_t** next_in,
  106. size_t* available_out,
  107. uint8_t** next_out,
  108. size_t* total_out,
  109. BrotliState* s);
  110. /* Fills the new state with a dictionary for LZ77, warming up the ringbuffer,
  111. e.g. for custom static dictionaries for data formats.
  112. Not to be confused with the built-in transformable dictionary of Brotli.
  113. |size| should be less or equal to 2^24 (16MiB), otherwise the dictionary will
  114. be ignored. The dictionary must exist in memory until decoding is done and
  115. is owned by the caller. To use:
  116. 1) Allocate and initialize state with BrotliCreateState
  117. 2) Use BrotliSetCustomDictionary
  118. 3) Use BrotliDecompressStream
  119. 4) Clean up and free state with BrotliDestroyState
  120. */
  121. void BrotliSetCustomDictionary(
  122. size_t size, const uint8_t* dict, BrotliState* s);
  123. /* Returns 1, if s is in a state where we have not read any input bytes yet,
  124. and 0 otherwise */
  125. int BrotliStateIsStreamStart(const BrotliState* s);
  126. /* Returns 1, if s is in a state where we reached the end of the input and
  127. produced all of the output, and 0 otherwise. */
  128. int BrotliStateIsStreamEnd(const BrotliState* s);
  129. /* Returns detailed error code after BrotliDecompressStream returns
  130. BROTLI_RESULT_ERROR. */
  131. BrotliErrorCode BrotliGetErrorCode(const BrotliState* s);
  132. const char* BrotliErrorString(BrotliErrorCode c);
  133. #if defined(__cplusplus) || defined(c_plusplus)
  134. } /* extern "C" */
  135. #endif
  136. #endif /* BROTLI_DEC_DECODE_H_ */