decode.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /**
  6. * @file
  7. * API for Brotli decompression.
  8. */
  9. #ifndef BROTLI_DEC_DECODE_H_
  10. #define BROTLI_DEC_DECODE_H_
  11. #include <brotli/port.h>
  12. #include <brotli/types.h>
  13. #if defined(__cplusplus) || defined(c_plusplus)
  14. extern "C" {
  15. #endif
  16. /**
  17. * Opaque structure that holds decoder state.
  18. *
  19. * Allocated and initialized with ::BrotliDecoderCreateInstance.
  20. * Cleaned up and deallocated with ::BrotliDecoderDestroyInstance.
  21. */
  22. typedef struct BrotliDecoderStateStruct BrotliDecoderState;
  23. /**
  24. * Result type for ::BrotliDecoderDecompress and
  25. * ::BrotliDecoderDecompressStream functions.
  26. */
  27. typedef enum {
  28. /** Decoding error, e.g. corrupted input or memory allocation problem. */
  29. BROTLI_DECODER_RESULT_ERROR = 0,
  30. /** Decoding successfully completed */
  31. BROTLI_DECODER_RESULT_SUCCESS = 1,
  32. /** Partially done; should be called again with more input */
  33. BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT = 2,
  34. /** Partially done; should be called again with more output */
  35. BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT = 3
  36. } BrotliDecoderResult;
  37. /**
  38. * Template that evaluates items of ::BrotliDecoderErrorCode.
  39. *
  40. * Example: @code {.cpp}
  41. * // Log Brotli error code.
  42. * switch (brotliDecoderErrorCode) {
  43. * #define CASE_(PREFIX, NAME, CODE) \
  44. * case BROTLI_DECODER ## PREFIX ## NAME: \
  45. * LOG(INFO) << "error code:" << #NAME; \
  46. * break;
  47. * #define NEWLINE_
  48. * BROTLI_DECODER_ERROR_CODES_LIST(CASE_, NEWLINE_)
  49. * #undef CASE_
  50. * #undef NEWLINE_
  51. * default: LOG(FATAL) << "unknown brotli error code";
  52. * }
  53. * @endcode
  54. */
  55. #define BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE, SEPARATOR) \
  56. BROTLI_ERROR_CODE(_, NO_ERROR, 0) SEPARATOR \
  57. /* Same as BrotliDecoderResult values */ \
  58. BROTLI_ERROR_CODE(_, SUCCESS, 1) SEPARATOR \
  59. BROTLI_ERROR_CODE(_, NEEDS_MORE_INPUT, 2) SEPARATOR \
  60. BROTLI_ERROR_CODE(_, NEEDS_MORE_OUTPUT, 3) SEPARATOR \
  61. \
  62. /* Errors caused by invalid input */ \
  63. BROTLI_ERROR_CODE(_ERROR_FORMAT_, EXUBERANT_NIBBLE, -1) SEPARATOR \
  64. BROTLI_ERROR_CODE(_ERROR_FORMAT_, RESERVED, -2) SEPARATOR \
  65. BROTLI_ERROR_CODE(_ERROR_FORMAT_, EXUBERANT_META_NIBBLE, -3) SEPARATOR \
  66. BROTLI_ERROR_CODE(_ERROR_FORMAT_, SIMPLE_HUFFMAN_ALPHABET, -4) SEPARATOR \
  67. BROTLI_ERROR_CODE(_ERROR_FORMAT_, SIMPLE_HUFFMAN_SAME, -5) SEPARATOR \
  68. BROTLI_ERROR_CODE(_ERROR_FORMAT_, CL_SPACE, -6) SEPARATOR \
  69. BROTLI_ERROR_CODE(_ERROR_FORMAT_, HUFFMAN_SPACE, -7) SEPARATOR \
  70. BROTLI_ERROR_CODE(_ERROR_FORMAT_, CONTEXT_MAP_REPEAT, -8) SEPARATOR \
  71. BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_LENGTH_1, -9) SEPARATOR \
  72. BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_LENGTH_2, -10) SEPARATOR \
  73. BROTLI_ERROR_CODE(_ERROR_FORMAT_, TRANSFORM, -11) SEPARATOR \
  74. BROTLI_ERROR_CODE(_ERROR_FORMAT_, DICTIONARY, -12) SEPARATOR \
  75. BROTLI_ERROR_CODE(_ERROR_FORMAT_, WINDOW_BITS, -13) SEPARATOR \
  76. BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_1, -14) SEPARATOR \
  77. BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_2, -15) SEPARATOR \
  78. \
  79. /* -16..-17 codes are reserved */ \
  80. \
  81. BROTLI_ERROR_CODE(_ERROR_, COMPOUND_DICTIONARY, -18) SEPARATOR \
  82. BROTLI_ERROR_CODE(_ERROR_, DICTIONARY_NOT_SET, -19) SEPARATOR \
  83. BROTLI_ERROR_CODE(_ERROR_, INVALID_ARGUMENTS, -20) SEPARATOR \
  84. \
  85. /* Memory allocation problems */ \
  86. BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MODES, -21) SEPARATOR \
  87. /* Literal, insert and distance trees together */ \
  88. BROTLI_ERROR_CODE(_ERROR_ALLOC_, TREE_GROUPS, -22) SEPARATOR \
  89. /* -23..-24 codes are reserved for distinct tree groups */ \
  90. BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MAP, -25) SEPARATOR \
  91. BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_1, -26) SEPARATOR \
  92. BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_2, -27) SEPARATOR \
  93. /* -28..-29 codes are reserved for dynamic ring-buffer allocation */ \
  94. BROTLI_ERROR_CODE(_ERROR_ALLOC_, BLOCK_TYPE_TREES, -30) SEPARATOR \
  95. \
  96. /* "Impossible" states */ \
  97. BROTLI_ERROR_CODE(_ERROR_, UNREACHABLE, -31)
  98. /**
  99. * Error code for detailed logging / production debugging.
  100. *
  101. * See ::BrotliDecoderGetErrorCode and ::BROTLI_LAST_ERROR_CODE.
  102. */
  103. typedef enum {
  104. #define BROTLI_COMMA_ ,
  105. #define BROTLI_ERROR_CODE_ENUM_ITEM_(PREFIX, NAME, CODE) \
  106. BROTLI_DECODER ## PREFIX ## NAME = CODE
  107. BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_ENUM_ITEM_, BROTLI_COMMA_)
  108. } BrotliDecoderErrorCode;
  109. #undef BROTLI_ERROR_CODE_ENUM_ITEM_
  110. #undef BROTLI_COMMA_
  111. /**
  112. * The value of the last error code, negative integer.
  113. *
  114. * All other error code values are in the range from ::BROTLI_LAST_ERROR_CODE
  115. * to @c -1. There are also 4 other possible non-error codes @c 0 .. @c 3 in
  116. * ::BrotliDecoderErrorCode enumeration.
  117. */
  118. #define BROTLI_LAST_ERROR_CODE BROTLI_DECODER_ERROR_UNREACHABLE
  119. /** Options to be used with ::BrotliDecoderSetParameter. */
  120. typedef enum BrotliDecoderParameter {
  121. /**
  122. * Disable "canny" ring buffer allocation strategy.
  123. *
  124. * Ring buffer is allocated according to window size, despite the real size of
  125. * the content.
  126. */
  127. BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION = 0
  128. } BrotliDecoderParameter;
  129. /**
  130. * Sets the specified parameter to the given decoder instance.
  131. *
  132. * @param state decoder instance
  133. * @param param parameter to set
  134. * @param value new parameter value
  135. * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid
  136. * @returns ::BROTLI_TRUE if value is accepted
  137. */
  138. BROTLI_DEC_API BROTLI_BOOL BrotliDecoderSetParameter(
  139. BrotliDecoderState* state, BrotliDecoderParameter param, uint32_t value);
  140. /**
  141. * Creates an instance of ::BrotliDecoderState and initializes it.
  142. *
  143. * The instance can be used once for decoding and should then be destroyed with
  144. * ::BrotliDecoderDestroyInstance, it cannot be reused for a new decoding
  145. * session.
  146. *
  147. * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
  148. * case they are both zero, default memory allocators are used. @p opaque is
  149. * passed to @p alloc_func and @p free_func when they are called. @p free_func
  150. * should return without doing anything when asked to free a NULL pointer.
  151. *
  152. * @param alloc_func custom memory allocation function
  153. * @param free_func custom memory free function
  154. * @param opaque custom memory manager handle
  155. * @returns @c 0 if instance can not be allocated or initialized
  156. * @returns pointer to initialized ::BrotliDecoderState otherwise
  157. */
  158. BROTLI_DEC_API BrotliDecoderState* BrotliDecoderCreateInstance(
  159. brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
  160. /**
  161. * Deinitializes and frees ::BrotliDecoderState instance.
  162. *
  163. * @param state decoder instance to be cleaned up and deallocated
  164. */
  165. BROTLI_DEC_API void BrotliDecoderDestroyInstance(BrotliDecoderState* state);
  166. /**
  167. * Performs one-shot memory-to-memory decompression.
  168. *
  169. * Decompresses the data in @p encoded_buffer into @p decoded_buffer, and sets
  170. * @p *decoded_size to the decompressed length.
  171. *
  172. * @param encoded_size size of @p encoded_buffer
  173. * @param encoded_buffer compressed data buffer with at least @p encoded_size
  174. * addressable bytes
  175. * @param[in, out] decoded_size @b in: size of @p decoded_buffer; \n
  176. * @b out: length of decompressed data written to
  177. * @p decoded_buffer
  178. * @param decoded_buffer decompressed data destination buffer
  179. * @returns ::BROTLI_DECODER_RESULT_ERROR if input is corrupted, memory
  180. * allocation failed, or @p decoded_buffer is not large enough;
  181. * @returns ::BROTLI_DECODER_RESULT_SUCCESS otherwise
  182. */
  183. BROTLI_DEC_API BrotliDecoderResult BrotliDecoderDecompress(
  184. size_t encoded_size,
  185. const uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(encoded_size)],
  186. size_t* decoded_size,
  187. uint8_t decoded_buffer[BROTLI_ARRAY_PARAM(*decoded_size)]);
  188. /**
  189. * Decompresses the input stream to the output stream.
  190. *
  191. * The values @p *available_in and @p *available_out must specify the number of
  192. * bytes addressable at @p *next_in and @p *next_out respectively.
  193. * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
  194. *
  195. * After each call, @p *available_in will be decremented by the amount of input
  196. * bytes consumed, and the @p *next_in pointer will be incremented by that
  197. * amount. Similarly, @p *available_out will be decremented by the amount of
  198. * output bytes written, and the @p *next_out pointer will be incremented by
  199. * that amount.
  200. *
  201. * @p total_out, if it is not a null-pointer, will be set to the number
  202. * of bytes decompressed since the last @p state initialization.
  203. *
  204. * @note Input is never overconsumed, so @p next_in and @p available_in could be
  205. * passed to the next consumer after decoding is complete.
  206. *
  207. * @param state decoder instance
  208. * @param[in, out] available_in @b in: amount of available input; \n
  209. * @b out: amount of unused input
  210. * @param[in, out] next_in pointer to the next compressed byte
  211. * @param[in, out] available_out @b in: length of output buffer; \n
  212. * @b out: remaining size of output buffer
  213. * @param[in, out] next_out output buffer cursor;
  214. * can be @c NULL if @p available_out is @c 0
  215. * @param[out] total_out number of bytes decompressed so far; can be @c NULL
  216. * @returns ::BROTLI_DECODER_RESULT_ERROR if input is corrupted, memory
  217. * allocation failed, arguments were invalid, etc.;
  218. * use ::BrotliDecoderGetErrorCode to get detailed error code
  219. * @returns ::BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT decoding is blocked until
  220. * more input data is provided
  221. * @returns ::BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT decoding is blocked until
  222. * more output space is provided
  223. * @returns ::BROTLI_DECODER_RESULT_SUCCESS decoding is finished, no more
  224. * input might be consumed and no more output will be produced
  225. */
  226. BROTLI_DEC_API BrotliDecoderResult BrotliDecoderDecompressStream(
  227. BrotliDecoderState* state, size_t* available_in, const uint8_t** next_in,
  228. size_t* available_out, uint8_t** next_out, size_t* total_out);
  229. /**
  230. * Checks if decoder has more output.
  231. *
  232. * @param state decoder instance
  233. * @returns ::BROTLI_TRUE, if decoder has some unconsumed output
  234. * @returns ::BROTLI_FALSE otherwise
  235. */
  236. BROTLI_DEC_API BROTLI_BOOL BrotliDecoderHasMoreOutput(
  237. const BrotliDecoderState* state);
  238. /**
  239. * Acquires pointer to internal output buffer.
  240. *
  241. * This method is used to make language bindings easier and more efficient:
  242. * -# push data to ::BrotliDecoderDecompressStream,
  243. * until ::BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT is reported
  244. * -# use ::BrotliDecoderTakeOutput to peek bytes and copy to language-specific
  245. * entity
  246. *
  247. * Also this could be useful if there is an output stream that is able to
  248. * consume all the provided data (e.g. when data is saved to file system).
  249. *
  250. * @attention After every call to ::BrotliDecoderTakeOutput @p *size bytes of
  251. * output are considered consumed for all consecutive calls to the
  252. * instance methods; returned pointer becomes invalidated as well.
  253. *
  254. * @note Decoder output is not guaranteed to be contiguous. This means that
  255. * after the size-unrestricted call to ::BrotliDecoderTakeOutput,
  256. * immediate next call to ::BrotliDecoderTakeOutput may return more data.
  257. *
  258. * @param state decoder instance
  259. * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if
  260. * any amount could be handled; \n
  261. * @b out: amount of data pointed by returned pointer and
  262. * considered consumed; \n
  263. * out value is never greater than in value, unless it is @c 0
  264. * @returns pointer to output data
  265. */
  266. BROTLI_DEC_API const uint8_t* BrotliDecoderTakeOutput(
  267. BrotliDecoderState* state, size_t* size);
  268. /**
  269. * Checks if instance has already consumed input.
  270. *
  271. * Instance that returns ::BROTLI_FALSE is considered "fresh" and could be
  272. * reused.
  273. *
  274. * @param state decoder instance
  275. * @returns ::BROTLI_TRUE if decoder has already used some input bytes
  276. * @returns ::BROTLI_FALSE otherwise
  277. */
  278. BROTLI_DEC_API BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* state);
  279. /**
  280. * Checks if decoder instance reached the final state.
  281. *
  282. * @param state decoder instance
  283. * @returns ::BROTLI_TRUE if decoder is in a state where it reached the end of
  284. * the input and produced all of the output
  285. * @returns ::BROTLI_FALSE otherwise
  286. */
  287. BROTLI_DEC_API BROTLI_BOOL BrotliDecoderIsFinished(
  288. const BrotliDecoderState* state);
  289. /**
  290. * Acquires a detailed error code.
  291. *
  292. * Should be used only after ::BrotliDecoderDecompressStream returns
  293. * ::BROTLI_DECODER_RESULT_ERROR.
  294. *
  295. * See also ::BrotliDecoderErrorString
  296. *
  297. * @param state decoder instance
  298. * @returns last saved error code
  299. */
  300. BROTLI_DEC_API BrotliDecoderErrorCode BrotliDecoderGetErrorCode(
  301. const BrotliDecoderState* state);
  302. /**
  303. * Converts error code to a c-string.
  304. */
  305. BROTLI_DEC_API const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c);
  306. /**
  307. * Gets a decoder library version.
  308. *
  309. * Look at BROTLI_VERSION for more information.
  310. */
  311. BROTLI_DEC_API uint32_t BrotliDecoderVersion(void);
  312. #if defined(__cplusplus) || defined(c_plusplus)
  313. } /* extern "C" */
  314. #endif
  315. #endif /* BROTLI_DEC_DECODE_H_ */