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