• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
19 extern "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  */
28 typedef struct BrotliDecoderStateStruct BrotliDecoderState;
29 
30 /**
31  * Result type for ::BrotliDecoderDecompress and
32  * ::BrotliDecoderDecompressStream functions.
33  */
34 typedef 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   BROTLI_ERROR_CODE(_ERROR_FORMAT_, DISTANCE, -16) SEPARATOR               \
87                                                                            \
88   /* -17..-18 codes are reserved */                                        \
89                                                                            \
90   BROTLI_ERROR_CODE(_ERROR_, DICTIONARY_NOT_SET, -19) SEPARATOR            \
91   BROTLI_ERROR_CODE(_ERROR_, INVALID_ARGUMENTS, -20) SEPARATOR             \
92                                                                            \
93   /* Memory allocation problems */                                         \
94   BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MODES, -21) SEPARATOR           \
95   /* Literal, insert and distance trees together */                        \
96   BROTLI_ERROR_CODE(_ERROR_ALLOC_, TREE_GROUPS, -22) SEPARATOR             \
97   /* -23..-24 codes are reserved for distinct tree groups */               \
98   BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MAP, -25) SEPARATOR             \
99   BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_1, -26) SEPARATOR           \
100   BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_2, -27) SEPARATOR           \
101   /* -28..-29 codes are reserved for dynamic ring-buffer allocation */     \
102   BROTLI_ERROR_CODE(_ERROR_ALLOC_, BLOCK_TYPE_TREES, -30) SEPARATOR        \
103                                                                            \
104   /* "Impossible" states */                                                \
105   BROTLI_ERROR_CODE(_ERROR_, UNREACHABLE, -31)
106 
107 /**
108  * Error code for detailed logging / production debugging.
109  *
110  * See ::BrotliDecoderGetErrorCode and ::BROTLI_LAST_ERROR_CODE.
111  */
112 typedef enum {
113 #define BROTLI_COMMA_ ,
114 #define BROTLI_ERROR_CODE_ENUM_ITEM_(PREFIX, NAME, CODE) \
115     BROTLI_DECODER ## PREFIX ## NAME = CODE
116   BROTLI_DECODER_ERROR_CODES_LIST(BROTLI_ERROR_CODE_ENUM_ITEM_, BROTLI_COMMA_)
117 } BrotliDecoderErrorCode;
118 #undef BROTLI_ERROR_CODE_ENUM_ITEM_
119 #undef BROTLI_COMMA_
120 
121 /**
122  * The value of the last error code, negative integer.
123  *
124  * All other error code values are in the range from ::BROTLI_LAST_ERROR_CODE
125  * to @c -1. There are also 4 other possible non-error codes @c 0 .. @c 3 in
126  * ::BrotliDecoderErrorCode enumeration.
127  */
128 #define BROTLI_LAST_ERROR_CODE BROTLI_DECODER_ERROR_UNREACHABLE
129 
130 /** Options to be used with ::BrotliDecoderSetParameter. */
131 typedef enum BrotliDecoderParameter {
132   /**
133    * Disable "canny" ring buffer allocation strategy.
134    *
135    * Ring buffer is allocated according to window size, despite the real size of
136    * the content.
137    */
138   BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION = 0,
139   /**
140    * Flag that determines if "Large Window Brotli" is used.
141    */
142   BROTLI_DECODER_PARAM_LARGE_WINDOW = 1
143 } BrotliDecoderParameter;
144 
145 /**
146  * Sets the specified parameter to the given decoder instance.
147  *
148  * @param state decoder instance
149  * @param param parameter to set
150  * @param value new parameter value
151  * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid
152  * @returns ::BROTLI_TRUE if value is accepted
153  */
154 BROTLI_DEC_API BROTLI_BOOL BrotliDecoderSetParameter(
155     BrotliDecoderState* state, BrotliDecoderParameter param, uint32_t value);
156 
157 /**
158  * Creates an instance of ::BrotliDecoderState and initializes it.
159  *
160  * The instance can be used once for decoding and should then be destroyed with
161  * ::BrotliDecoderDestroyInstance, it cannot be reused for a new decoding
162  * session.
163  *
164  * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
165  * case they are both zero, default memory allocators are used. @p opaque is
166  * passed to @p alloc_func and @p free_func when they are called. @p free_func
167  * has to return without doing anything when asked to free a NULL pointer.
168  *
169  * @param alloc_func custom memory allocation function
170  * @param free_func custom memory free function
171  * @param opaque custom memory manager handle
172  * @returns @c 0 if instance can not be allocated or initialized
173  * @returns pointer to initialized ::BrotliDecoderState otherwise
174  */
175 BROTLI_DEC_API BrotliDecoderState* BrotliDecoderCreateInstance(
176     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
177 
178 /**
179  * Deinitializes and frees ::BrotliDecoderState instance.
180  *
181  * @param state decoder instance to be cleaned up and deallocated
182  */
183 BROTLI_DEC_API void BrotliDecoderDestroyInstance(BrotliDecoderState* state);
184 
185 /**
186  * Performs one-shot memory-to-memory decompression.
187  *
188  * Decompresses the data in @p encoded_buffer into @p decoded_buffer, and sets
189  * @p *decoded_size to the decompressed length.
190  *
191  * @param encoded_size size of @p encoded_buffer
192  * @param encoded_buffer compressed data buffer with at least @p encoded_size
193  *        addressable bytes
194  * @param[in, out] decoded_size @b in: size of @p decoded_buffer; \n
195  *                 @b out: length of decompressed data written to
196  *                 @p decoded_buffer
197  * @param decoded_buffer decompressed data destination buffer
198  * @returns ::BROTLI_DECODER_RESULT_ERROR if input is corrupted, memory
199  *          allocation failed, or @p decoded_buffer is not large enough;
200  * @returns ::BROTLI_DECODER_RESULT_SUCCESS otherwise
201  */
202 BROTLI_DEC_API BrotliDecoderResult BrotliDecoderDecompress(
203     size_t encoded_size,
204     const uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(encoded_size)],
205     size_t* decoded_size,
206     uint8_t decoded_buffer[BROTLI_ARRAY_PARAM(*decoded_size)]);
207 
208 /**
209  * Decompresses the input stream to the output stream.
210  *
211  * The values @p *available_in and @p *available_out must specify the number of
212  * bytes addressable at @p *next_in and @p *next_out respectively.
213  * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
214  *
215  * After each call, @p *available_in will be decremented by the amount of input
216  * bytes consumed, and the @p *next_in pointer will be incremented by that
217  * amount. Similarly, @p *available_out will be decremented by the amount of
218  * output bytes written, and the @p *next_out pointer will be incremented by
219  * that amount.
220  *
221  * @p total_out, if it is not a null-pointer, will be set to the number
222  * of bytes decompressed since the last @p state initialization.
223  *
224  * @note Input is never overconsumed, so @p next_in and @p available_in could be
225  * passed to the next consumer after decoding is complete.
226  *
227  * @param state decoder instance
228  * @param[in, out] available_in @b in: amount of available input; \n
229  *                 @b out: amount of unused input
230  * @param[in, out] next_in pointer to the next compressed byte
231  * @param[in, out] available_out @b in: length of output buffer; \n
232  *                 @b out: remaining size of output buffer
233  * @param[in, out] next_out output buffer cursor;
234  *                 can be @c NULL if @p available_out is @c 0
235  * @param[out] total_out number of bytes decompressed so far; can be @c NULL
236  * @returns ::BROTLI_DECODER_RESULT_ERROR if input is corrupted, memory
237  *          allocation failed, arguments were invalid, etc.;
238  *          use ::BrotliDecoderGetErrorCode to get detailed error code
239  * @returns ::BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT decoding is blocked until
240  *          more input data is provided
241  * @returns ::BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT decoding is blocked until
242  *          more output space is provided
243  * @returns ::BROTLI_DECODER_RESULT_SUCCESS decoding is finished, no more
244  *          input might be consumed and no more output will be produced
245  */
246 BROTLI_DEC_API BrotliDecoderResult BrotliDecoderDecompressStream(
247   BrotliDecoderState* state, size_t* available_in, const uint8_t** next_in,
248   size_t* available_out, uint8_t** next_out, size_t* total_out);
249 
250 /**
251  * Checks if decoder has more output.
252  *
253  * @param state decoder instance
254  * @returns ::BROTLI_TRUE, if decoder has some unconsumed output
255  * @returns ::BROTLI_FALSE otherwise
256  */
257 BROTLI_DEC_API BROTLI_BOOL BrotliDecoderHasMoreOutput(
258     const BrotliDecoderState* state);
259 
260 /**
261  * Acquires pointer to internal output buffer.
262  *
263  * This method is used to make language bindings easier and more efficient:
264  *  -# push data to ::BrotliDecoderDecompressStream,
265  *     until ::BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT is reported
266  *  -# use ::BrotliDecoderTakeOutput to peek bytes and copy to language-specific
267  *     entity
268  *
269  * Also this could be useful if there is an output stream that is able to
270  * consume all the provided data (e.g. when data is saved to file system).
271  *
272  * @attention After every call to ::BrotliDecoderTakeOutput @p *size bytes of
273  *            output are considered consumed for all consecutive calls to the
274  *            instance methods; returned pointer becomes invalidated as well.
275  *
276  * @note Decoder output is not guaranteed to be contiguous. This means that
277  *       after the size-unrestricted call to ::BrotliDecoderTakeOutput,
278  *       immediate next call to ::BrotliDecoderTakeOutput may return more data.
279  *
280  * @param state decoder instance
281  * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if
282  *                 any amount could be handled; \n
283  *                 @b out: amount of data pointed by returned pointer and
284  *                 considered consumed; \n
285  *                 out value is never greater than in value, unless it is @c 0
286  * @returns pointer to output data
287  */
288 BROTLI_DEC_API const uint8_t* BrotliDecoderTakeOutput(
289     BrotliDecoderState* state, size_t* size);
290 
291 /**
292  * Checks if instance has already consumed input.
293  *
294  * Instance that returns ::BROTLI_FALSE is considered "fresh" and could be
295  * reused.
296  *
297  * @param state decoder instance
298  * @returns ::BROTLI_TRUE if decoder has already used some input bytes
299  * @returns ::BROTLI_FALSE otherwise
300  */
301 BROTLI_DEC_API BROTLI_BOOL BrotliDecoderIsUsed(const BrotliDecoderState* state);
302 
303 /**
304  * Checks if decoder instance reached the final state.
305  *
306  * @param state decoder instance
307  * @returns ::BROTLI_TRUE if decoder is in a state where it reached the end of
308  *          the input and produced all of the output
309  * @returns ::BROTLI_FALSE otherwise
310  */
311 BROTLI_DEC_API BROTLI_BOOL BrotliDecoderIsFinished(
312     const BrotliDecoderState* state);
313 
314 /**
315  * Acquires a detailed error code.
316  *
317  * Should be used only after ::BrotliDecoderDecompressStream returns
318  * ::BROTLI_DECODER_RESULT_ERROR.
319  *
320  * See also ::BrotliDecoderErrorString
321  *
322  * @param state decoder instance
323  * @returns last saved error code
324  */
325 BROTLI_DEC_API BrotliDecoderErrorCode BrotliDecoderGetErrorCode(
326     const BrotliDecoderState* state);
327 
328 /**
329  * Converts error code to a c-string.
330  */
331 BROTLI_DEC_API const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c);
332 
333 /**
334  * Gets a decoder library version.
335  *
336  * Look at BROTLI_VERSION for more information.
337  */
338 BROTLI_DEC_API uint32_t BrotliDecoderVersion(void);
339 
340 #if defined(__cplusplus) || defined(c_plusplus)
341 } /* extern "C" */
342 #endif
343 
344 #endif  /* BROTLI_DEC_DECODE_H_ */
345