• 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                                                                            \
87   /* -16..-17 codes are reserved */                                        \
88                                                                            \
89   BROTLI_ERROR_CODE(_ERROR_, COMPOUND_DICTIONARY, -18) SEPARATOR           \
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 } BrotliDecoderParameter;
140 
141 /**
142  * Sets the specified parameter to the given decoder instance.
143  *
144  * @param state decoder instance
145  * @param param parameter to set
146  * @param value new parameter value
147  * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid
148  * @returns ::BROTLI_TRUE if value is accepted
149  */
150 BROTLI_DEC_API BROTLI_BOOL BrotliDecoderSetParameter(
151     BrotliDecoderState* state, BrotliDecoderParameter param, uint32_t value);
152 
153 /**
154  * Creates an instance of ::BrotliDecoderState and initializes it.
155  *
156  * The instance can be used once for decoding and should then be destroyed with
157  * ::BrotliDecoderDestroyInstance, it cannot be reused for a new decoding
158  * session.
159  *
160  * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
161  * case they are both zero, default memory allocators are used. @p opaque is
162  * passed to @p alloc_func and @p free_func when they are called.
163  *
164  * @param alloc_func custom memory allocation function
165  * @param free_func custom memory fee function
166  * @param opaque custom memory manager handle
167  * @returns @c 0 if instance can not be allocated or initialized
168  * @returns pointer to initialized ::BrotliDecoderState otherwise
169  */
170 BROTLI_DEC_API BrotliDecoderState* BrotliDecoderCreateInstance(
171     brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
172 
173 /**
174  * Deinitializes and frees ::BrotliDecoderState instance.
175  *
176  * @param state decoder instance to be cleaned up and deallocated
177  */
178 BROTLI_DEC_API void BrotliDecoderDestroyInstance(BrotliDecoderState* state);
179 
180 /**
181  * Performs one-shot memory-to-memory decompression.
182  *
183  * Decompresses the data in @p encoded_buffer into @p decoded_buffer, and sets
184  * @p *decoded_size to the decompressed length.
185  *
186  * @param encoded_size size of @p encoded_buffer
187  * @param encoded_buffer compressed data buffer with at least @p encoded_size
188  *        addressable bytes
189  * @param[in, out] decoded_size @b in: size of @p decoded_buffer; \n
190  *                 @b out: length of decompressed data written to
191  *                 @p decoded_buffer
192  * @param decoded_buffer decompressed data destination buffer
193  * @returns ::BROTLI_DECODER_RESULT_ERROR if input is corrupted, memory
194  *          allocation failed, or @p decoded_buffer is not large enough;
195  * @returns ::BROTLI_DECODER_RESULT_SUCCESS otherwise
196  */
197 BROTLI_DEC_API BrotliDecoderResult BrotliDecoderDecompress(
198     size_t encoded_size,
199     const uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(encoded_size)],
200     size_t* decoded_size,
201     uint8_t decoded_buffer[BROTLI_ARRAY_PARAM(*decoded_size)]);
202 
203 /**
204  * Decompresses the input stream to the output stream.
205  *
206  * The values @p *available_in and @p *available_out must specify the number of
207  * bytes addressable at @p *next_in and @p *next_out respectively.
208  * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
209  *
210  * After each call, @p *available_in will be decremented by the amount of input
211  * bytes consumed, and the @p *next_in pointer will be incremented by that
212  * amount. Similarly, @p *available_out will be decremented by the amount of
213  * output bytes written, and the @p *next_out pointer will be incremented by
214  * that amount.
215  *
216  * @p total_out, if it is not a null-pointer, will be set to the number
217  * of bytes decompressed since the last @p state initialization.
218  *
219  * @note Input is never overconsumed, so @p next_in and @p available_in could be
220  * passed to the next consumer after decoding is complete.
221  *
222  * @param state decoder instance
223  * @param[in, out] available_in @b in: amount of available input; \n
224  *                 @b out: amount of unused input
225  * @param[in, out] next_in pointer to the next compressed byte
226  * @param[in, out] available_out @b in: length of output buffer; \n
227  *                 @b out: remaining size of output buffer
228  * @param[in, out] next_out output buffer cursor;
229  *                 can be @c NULL if @p available_out is @c 0
230  * @param[out] total_out number of bytes decompressed so far; can be @c NULL
231  * @returns ::BROTLI_DECODER_RESULT_ERROR if input is corrupted, memory
232  *          allocation failed, arguments were invalid, etc.;
233  *          use ::BrotliDecoderGetErrorCode to get detailed error code
234  * @returns ::BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT decoding is blocked until
235  *          more input data is provided
236  * @returns ::BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT decoding is blocked until
237  *          more output space is provided
238  * @returns ::BROTLI_DECODER_RESULT_SUCCESS decoding is finished, no more
239  *          input might be consumed and no more output will be produced
240  */
241 BROTLI_DEC_API BrotliDecoderResult BrotliDecoderDecompressStream(
242   BrotliDecoderState* state, size_t* available_in, const uint8_t** next_in,
243   size_t* available_out, uint8_t** next_out, size_t* total_out);
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  */
252 BROTLI_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  */
283 BROTLI_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  */
296 BROTLI_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  */
306 BROTLI_DEC_API BROTLI_BOOL BrotliDecoderIsFinished(
307     const BrotliDecoderState* state);
308 
309 /**
310  * Acquires a detailed error code.
311  *
312  * Should be used only after ::BrotliDecoderDecompressStream returns
313  * ::BROTLI_DECODER_RESULT_ERROR.
314  *
315  * See also ::BrotliDecoderErrorString
316  *
317  * @param state decoder instance
318  * @returns last saved error code
319  */
320 BROTLI_DEC_API BrotliDecoderErrorCode BrotliDecoderGetErrorCode(
321     const BrotliDecoderState* state);
322 
323 /**
324  * Converts error code to a c-string.
325  */
326 BROTLI_DEC_API const char* BrotliDecoderErrorString(BrotliDecoderErrorCode c);
327 
328 /**
329  * Gets a decoder library version.
330  *
331  * Look at BROTLI_VERSION for more information.
332  */
333 BROTLI_DEC_API uint32_t BrotliDecoderVersion(void);
334 
335 #if defined(__cplusplus) || defined(c_plusplus)
336 } /* extern "C" */
337 #endif
338 
339 #endif  /* BROTLI_DEC_DECODE_H_ */
340