decode.h 16 KB

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