brotli 0.1.8 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.travis.yml +7 -3
  4. data/brotli.gemspec +1 -1
  5. data/ext/brotli/brotli.c +4 -4
  6. data/ext/brotli/brotli.h +2 -2
  7. data/ext/brotli/extconf.rb +9 -16
  8. data/lib/brotli/version.rb +1 -1
  9. data/vendor/brotli/{common → c/common}/constants.h +11 -1
  10. data/vendor/brotli/c/common/dictionary.bin +432 -0
  11. data/vendor/brotli/c/common/dictionary.c +5905 -0
  12. data/vendor/brotli/c/common/dictionary.h +64 -0
  13. data/vendor/brotli/c/common/version.h +19 -0
  14. data/vendor/brotli/{dec → c/dec}/bit_reader.c +2 -2
  15. data/vendor/brotli/{dec → c/dec}/bit_reader.h +11 -34
  16. data/vendor/brotli/{dec → c/dec}/context.h +1 -1
  17. data/vendor/brotli/{dec → c/dec}/decode.c +389 -356
  18. data/vendor/brotli/{dec → c/dec}/huffman.c +24 -23
  19. data/vendor/brotli/{dec → c/dec}/huffman.h +1 -1
  20. data/vendor/brotli/{dec → c/dec}/port.h +19 -10
  21. data/vendor/brotli/{dec → c/dec}/prefix.h +1 -1
  22. data/vendor/brotli/{dec → c/dec}/state.c +23 -19
  23. data/vendor/brotli/{dec → c/dec}/state.h +18 -17
  24. data/vendor/brotli/{dec → c/dec}/transform.h +2 -2
  25. data/vendor/brotli/c/enc/backward_references.c +134 -0
  26. data/vendor/brotli/c/enc/backward_references.h +39 -0
  27. data/vendor/brotli/{enc/backward_references.c → c/enc/backward_references_hq.c} +144 -232
  28. data/vendor/brotli/{enc/backward_references.h → c/enc/backward_references_hq.h} +28 -31
  29. data/vendor/brotli/{enc → c/enc}/backward_references_inc.h +37 -31
  30. data/vendor/brotli/{enc → c/enc}/bit_cost.c +1 -1
  31. data/vendor/brotli/{enc → c/enc}/bit_cost.h +1 -1
  32. data/vendor/brotli/{enc → c/enc}/bit_cost_inc.h +0 -0
  33. data/vendor/brotli/{enc → c/enc}/block_encoder_inc.h +0 -0
  34. data/vendor/brotli/{enc → c/enc}/block_splitter.c +2 -4
  35. data/vendor/brotli/{enc → c/enc}/block_splitter.h +1 -1
  36. data/vendor/brotli/{enc → c/enc}/block_splitter_inc.h +6 -7
  37. data/vendor/brotli/{enc → c/enc}/brotli_bit_stream.c +22 -26
  38. data/vendor/brotli/{enc → c/enc}/brotli_bit_stream.h +1 -5
  39. data/vendor/brotli/{enc → c/enc}/cluster.c +1 -1
  40. data/vendor/brotli/{enc → c/enc}/cluster.h +1 -1
  41. data/vendor/brotli/{enc → c/enc}/cluster_inc.h +2 -0
  42. data/vendor/brotli/{enc → c/enc}/command.h +34 -17
  43. data/vendor/brotli/{enc → c/enc}/compress_fragment.c +97 -53
  44. data/vendor/brotli/{enc → c/enc}/compress_fragment.h +5 -2
  45. data/vendor/brotli/{enc → c/enc}/compress_fragment_two_pass.c +106 -51
  46. data/vendor/brotli/{enc → c/enc}/compress_fragment_two_pass.h +5 -2
  47. data/vendor/brotli/{enc → c/enc}/context.h +3 -3
  48. data/vendor/brotli/c/enc/dictionary_hash.c +1120 -0
  49. data/vendor/brotli/c/enc/dictionary_hash.h +24 -0
  50. data/vendor/brotli/{enc → c/enc}/encode.c +442 -240
  51. data/vendor/brotli/{enc → c/enc}/entropy_encode.c +9 -9
  52. data/vendor/brotli/{enc → c/enc}/entropy_encode.h +4 -4
  53. data/vendor/brotli/{enc → c/enc}/entropy_encode_static.h +4 -4
  54. data/vendor/brotli/{enc → c/enc}/fast_log.h +3 -3
  55. data/vendor/brotli/{enc → c/enc}/find_match_length.h +8 -8
  56. data/vendor/brotli/c/enc/hash.h +446 -0
  57. data/vendor/brotli/{enc → c/enc}/hash_forgetful_chain_inc.h +72 -68
  58. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +266 -0
  59. data/vendor/brotli/c/enc/hash_longest_match_inc.h +258 -0
  60. data/vendor/brotli/{enc → c/enc}/hash_longest_match_quickly_inc.h +81 -77
  61. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +326 -0
  62. data/vendor/brotli/{enc → c/enc}/histogram.c +4 -2
  63. data/vendor/brotli/{enc → c/enc}/histogram.h +1 -1
  64. data/vendor/brotli/{enc → c/enc}/histogram_inc.h +0 -0
  65. data/vendor/brotli/{enc → c/enc}/literal_cost.c +4 -7
  66. data/vendor/brotli/{enc → c/enc}/literal_cost.h +2 -2
  67. data/vendor/brotli/{enc → c/enc}/memory.c +1 -1
  68. data/vendor/brotli/{enc → c/enc}/memory.h +3 -2
  69. data/vendor/brotli/{enc → c/enc}/metablock.c +136 -123
  70. data/vendor/brotli/{enc → c/enc}/metablock.h +2 -12
  71. data/vendor/brotli/{enc → c/enc}/metablock_inc.h +0 -0
  72. data/vendor/brotli/{enc → c/enc}/port.h +49 -33
  73. data/vendor/brotli/{enc → c/enc}/prefix.h +4 -2
  74. data/vendor/brotli/{enc → c/enc}/quality.h +47 -17
  75. data/vendor/brotli/{enc → c/enc}/ringbuffer.h +6 -6
  76. data/vendor/brotli/{enc → c/enc}/static_dict.c +26 -22
  77. data/vendor/brotli/{enc → c/enc}/static_dict.h +3 -1
  78. data/vendor/brotli/c/enc/static_dict_lut.h +5864 -0
  79. data/vendor/brotli/{enc → c/enc}/utf8_util.c +1 -1
  80. data/vendor/brotli/{enc → c/enc}/utf8_util.h +2 -2
  81. data/vendor/brotli/{enc → c/enc}/write_bits.h +3 -3
  82. data/vendor/brotli/c/include/brotli/decode.h +339 -0
  83. data/vendor/brotli/c/include/brotli/encode.h +402 -0
  84. data/vendor/brotli/c/include/brotli/port.h +146 -0
  85. data/vendor/brotli/c/include/brotli/types.h +90 -0
  86. metadata +80 -79
  87. data/vendor/brotli/common/dictionary.c +0 -9474
  88. data/vendor/brotli/common/dictionary.h +0 -29
  89. data/vendor/brotli/common/port.h +0 -107
  90. data/vendor/brotli/common/types.h +0 -58
  91. data/vendor/brotli/dec/decode.h +0 -188
  92. data/vendor/brotli/enc/compressor.cc +0 -139
  93. data/vendor/brotli/enc/compressor.h +0 -161
  94. data/vendor/brotli/enc/dictionary_hash.h +0 -4121
  95. data/vendor/brotli/enc/encode.h +0 -221
  96. data/vendor/brotli/enc/encode_parallel.cc +0 -289
  97. data/vendor/brotli/enc/encode_parallel.h +0 -27
  98. data/vendor/brotli/enc/hash.h +0 -717
  99. data/vendor/brotli/enc/hash_longest_match_inc.h +0 -241
  100. data/vendor/brotli/enc/static_dict_lut.h +0 -11241
  101. data/vendor/brotli/enc/streams.cc +0 -114
  102. data/vendor/brotli/enc/streams.h +0 -121
@@ -8,7 +8,7 @@
8
8
 
9
9
  #include "./utf8_util.h"
10
10
 
11
- #include "../common/types.h"
11
+ #include <brotli/types.h>
12
12
 
13
13
  #if defined(__cplusplus) || defined(c_plusplus)
14
14
  extern "C" {
@@ -9,7 +9,7 @@
9
9
  #ifndef BROTLI_ENC_UTF8_UTIL_H_
10
10
  #define BROTLI_ENC_UTF8_UTIL_H_
11
11
 
12
- #include "../common/types.h"
12
+ #include <brotli/types.h>
13
13
  #include "./port.h"
14
14
 
15
15
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -19,7 +19,7 @@ extern "C" {
19
19
  static const double kMinUTF8Ratio = 0.75;
20
20
 
21
21
  /* Returns 1 if at least min_fraction of the bytes between pos and
22
- pos + length in the (data, mask) ringbuffer is UTF8-encoded, otherwise
22
+ pos + length in the (data, mask) ring-buffer is UTF8-encoded, otherwise
23
23
  returns 0. */
24
24
  BROTLI_INTERNAL BROTLI_BOOL BrotliIsMostlyUTF8(
25
25
  const uint8_t* data, const size_t pos, const size_t mask,
@@ -12,7 +12,7 @@
12
12
  #include <assert.h>
13
13
  #include <stdio.h> /* printf */
14
14
 
15
- #include "../common/types.h"
15
+ #include <brotli/types.h>
16
16
  #include "./port.h"
17
17
 
18
18
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -40,7 +40,7 @@ static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,
40
40
  uint64_t bits,
41
41
  size_t * BROTLI_RESTRICT pos,
42
42
  uint8_t * BROTLI_RESTRICT array) {
43
- #ifdef IS_LITTLE_ENDIAN
43
+ #ifdef BROTLI_LITTLE_ENDIAN
44
44
  /* This branch of the code can write up to 56 bits at a time,
45
45
  7 bits are lost by being perhaps already in *p and at least
46
46
  1 bit is needed to initialize the bit-stream ahead (i.e. if 7
@@ -54,7 +54,7 @@ static BROTLI_INLINE void BrotliWriteBits(size_t n_bits,
54
54
  assert((bits >> n_bits) == 0);
55
55
  assert(n_bits <= 56);
56
56
  v |= bits << (*pos & 7);
57
- BROTLI_UNALIGNED_STORE64(p, v); /* Set some bits. */
57
+ BROTLI_UNALIGNED_STORE64LE(p, v); /* Set some bits. */
58
58
  *pos += n_bits;
59
59
  #else
60
60
  /* implicit & 0xff is assumed for uint8_t arithmetics */
@@ -0,0 +1,339 @@
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_ */
@@ -0,0 +1,402 @@
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 compression.
10
+ */
11
+
12
+ #ifndef BROTLI_ENC_ENCODE_H_
13
+ #define BROTLI_ENC_ENCODE_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
+ /** Minimal value for ::BROTLI_PARAM_LGWIN parameter. */
23
+ #define BROTLI_MIN_WINDOW_BITS 10
24
+ /**
25
+ * Maximal value for ::BROTLI_PARAM_LGWIN parameter.
26
+ *
27
+ * @note equal to @c BROTLI_MAX_DISTANCE_BITS constant.
28
+ */
29
+ #define BROTLI_MAX_WINDOW_BITS 24
30
+ /** Minimal value for ::BROTLI_PARAM_LGBLOCK parameter. */
31
+ #define BROTLI_MIN_INPUT_BLOCK_BITS 16
32
+ /** Maximal value for ::BROTLI_PARAM_LGBLOCK parameter. */
33
+ #define BROTLI_MAX_INPUT_BLOCK_BITS 24
34
+ /** Minimal value for ::BROTLI_PARAM_QUALITY parameter. */
35
+ #define BROTLI_MIN_QUALITY 0
36
+ /** Maximal value for ::BROTLI_PARAM_QUALITY parameter. */
37
+ #define BROTLI_MAX_QUALITY 11
38
+
39
+ /** Options for ::BROTLI_PARAM_MODE parameter. */
40
+ typedef enum BrotliEncoderMode {
41
+ /**
42
+ * Default compression mode.
43
+ *
44
+ * In this mode compressor does not know anything in advance about the
45
+ * properties of the input.
46
+ */
47
+ BROTLI_MODE_GENERIC = 0,
48
+ /** Compression mode for UTF-8 formatted text input. */
49
+ BROTLI_MODE_TEXT = 1,
50
+ /** Compression mode used in WOFF 2.0. */
51
+ BROTLI_MODE_FONT = 2
52
+ } BrotliEncoderMode;
53
+
54
+ /** Default value for ::BROTLI_PARAM_QUALITY parameter. */
55
+ #define BROTLI_DEFAULT_QUALITY 11
56
+ /** Default value for ::BROTLI_PARAM_LGWIN parameter. */
57
+ #define BROTLI_DEFAULT_WINDOW 22
58
+ /** Default value for ::BROTLI_PARAM_MODE parameter. */
59
+ #define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERIC
60
+
61
+ /** Operations that can be performed by streaming encoder. */
62
+ typedef enum BrotliEncoderOperation {
63
+ /**
64
+ * Process input.
65
+ *
66
+ * Encoder may postpone producing output, until it has processed enough input.
67
+ */
68
+ BROTLI_OPERATION_PROCESS = 0,
69
+ /**
70
+ * Produce output for all processed input.
71
+ *
72
+ * Actual flush is performed when input stream is depleted and there is enough
73
+ * space in output stream. This means that client should repeat
74
+ * ::BROTLI_OPERATION_FLUSH operation until @p available_in becomes @c 0, and
75
+ * ::BrotliEncoderHasMoreOutput returns ::BROTLI_FALSE. If output is acquired
76
+ * via ::BrotliEncoderTakeOutput, then operation should be repeated after
77
+ * output buffer is drained.
78
+ *
79
+ * @warning Until flush is complete, client @b SHOULD @b NOT swap,
80
+ * reduce or extend input stream.
81
+ *
82
+ * When flush is complete, output data will be sufficient for decoder to
83
+ * reproduce all the given input.
84
+ */
85
+ BROTLI_OPERATION_FLUSH = 1,
86
+ /**
87
+ * Finalize the stream.
88
+ *
89
+ * Actual finalization is performed when input stream is depleted and there is
90
+ * enough space in output stream. This means that client should repeat
91
+ * ::BROTLI_OPERATION_FINISH operation until @p available_in becomes @c 0, and
92
+ * ::BrotliEncoderHasMoreOutput returns ::BROTLI_FALSE. If output is acquired
93
+ * via ::BrotliEncoderTakeOutput, then operation should be repeated after
94
+ * output buffer is drained.
95
+ *
96
+ * @warning Until finalization is complete, client @b SHOULD @b NOT swap,
97
+ * reduce or extend input stream.
98
+ *
99
+ * Helper function ::BrotliEncoderIsFinished checks if stream is finalized and
100
+ * output fully dumped.
101
+ *
102
+ * Adding more input data to finalized stream is impossible.
103
+ */
104
+ BROTLI_OPERATION_FINISH = 2,
105
+ /**
106
+ * Emit metadata block to stream.
107
+ *
108
+ * Metadata is opaque to Brotli: neither encoder, nor decoder processes this
109
+ * data or relies on it. It may be used to pass some extra information from
110
+ * encoder client to decoder client without interfering with main data stream.
111
+ *
112
+ * @note Encoder may emit empty metadata blocks internally, to pad encoded
113
+ * stream to byte boundary.
114
+ *
115
+ * @warning Until emitting metadata is complete client @b SHOULD @b NOT swap,
116
+ * reduce or extend input stream.
117
+ *
118
+ * @warning The whole content of input buffer is considered to be the content
119
+ * of metadata block. Do @b NOT @e append metadata to input stream,
120
+ * before it is depleted with other operations.
121
+ *
122
+ * Stream is soft-flushed before metadata block is emitted. Metadata block
123
+ * @b MUST be no longer than than 16MiB.
124
+ */
125
+ BROTLI_OPERATION_EMIT_METADATA = 3
126
+ } BrotliEncoderOperation;
127
+
128
+ /** Options to be used with ::BrotliEncoderSetParameter. */
129
+ typedef enum BrotliEncoderParameter {
130
+ /**
131
+ * Tune encoder for specific input.
132
+ *
133
+ * ::BrotliEncoderMode enumerates all available values.
134
+ */
135
+ BROTLI_PARAM_MODE = 0,
136
+ /**
137
+ * The main compression speed-density lever.
138
+ *
139
+ * The higher the quality, the slower the compression. Range is
140
+ * from ::BROTLI_MIN_QUALITY to ::BROTLI_MAX_QUALITY.
141
+ */
142
+ BROTLI_PARAM_QUALITY = 1,
143
+ /**
144
+ * Recommended sliding LZ77 window size.
145
+ *
146
+ * Encoder may reduce this value, e.g. if input is much smaller than
147
+ * window size.
148
+ *
149
+ * Window size is `(1 << value) - 16`.
150
+ *
151
+ * Range is from ::BROTLI_MIN_WINDOW_BITS to ::BROTLI_MAX_WINDOW_BITS.
152
+ */
153
+ BROTLI_PARAM_LGWIN = 2,
154
+ /**
155
+ * Recommended input block size.
156
+ *
157
+ * Encoder may reduce this value, e.g. if input is much smaller than input
158
+ * block size.
159
+ *
160
+ * Range is from ::BROTLI_MIN_INPUT_BLOCK_BITS to
161
+ * ::BROTLI_MAX_INPUT_BLOCK_BITS.
162
+ *
163
+ * @note Bigger input block size allows better compression, but consumes more
164
+ * memory. \n The rough formula of memory used for temporary input
165
+ * storage is `3 << lgBlock`.
166
+ */
167
+ BROTLI_PARAM_LGBLOCK = 3,
168
+ /**
169
+ * Flag that affects usage of "literal context modeling" format feature.
170
+ *
171
+ * This flag is a "decoding-speed vs compression ratio" trade-off.
172
+ */
173
+ BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING = 4,
174
+ /**
175
+ * Estimated total input size for all ::BrotliEncoderCompressStream calls.
176
+ *
177
+ * The default value is 0, which means that the total input size is unknown.
178
+ */
179
+ BROTLI_PARAM_SIZE_HINT = 5
180
+ } BrotliEncoderParameter;
181
+
182
+ /**
183
+ * Opaque structure that holds encoder state.
184
+ *
185
+ * Allocated and initialized with ::BrotliEncoderCreateInstance.
186
+ * Cleaned up and deallocated with ::BrotliEncoderDestroyInstance.
187
+ */
188
+ typedef struct BrotliEncoderStateStruct BrotliEncoderState;
189
+
190
+ /**
191
+ * Sets the specified parameter to the given encoder instance.
192
+ *
193
+ * @param state encoder instance
194
+ * @param param parameter to set
195
+ * @param value new parameter value
196
+ * @returns ::BROTLI_FALSE if parameter is unrecognized, or value is invalid
197
+ * @returns ::BROTLI_FALSE if value of parameter can not be changed at current
198
+ * encoder state (e.g. when encoding is started, window size might be
199
+ * already encoded and therefore it is impossible to change it)
200
+ * @returns ::BROTLI_TRUE if value is accepted
201
+ * @warning invalid values might be accepted in case they would not break
202
+ * encoding process.
203
+ */
204
+ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderSetParameter(
205
+ BrotliEncoderState* state, BrotliEncoderParameter param, uint32_t value);
206
+
207
+ /**
208
+ * Creates an instance of ::BrotliEncoderState and initializes it.
209
+ *
210
+ * @p alloc_func and @p free_func @b MUST be both zero or both non-zero. In the
211
+ * case they are both zero, default memory allocators are used. @p opaque is
212
+ * passed to @p alloc_func and @p free_func when they are called.
213
+ *
214
+ * @param alloc_func custom memory allocation function
215
+ * @param free_func custom memory fee function
216
+ * @param opaque custom memory manager handle
217
+ * @returns @c 0 if instance can not be allocated or initialized
218
+ * @returns pointer to initialized ::BrotliEncoderState otherwise
219
+ */
220
+ BROTLI_ENC_API BrotliEncoderState* BrotliEncoderCreateInstance(
221
+ brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
222
+
223
+ /**
224
+ * Deinitializes and frees ::BrotliEncoderState instance.
225
+ *
226
+ * @param state decoder instance to be cleaned up and deallocated
227
+ */
228
+ BROTLI_ENC_API void BrotliEncoderDestroyInstance(BrotliEncoderState* state);
229
+
230
+ /**
231
+ * Calculates the output size bound for the given @p input_size.
232
+ *
233
+ * @warning Result is not applicable to ::BrotliEncoderCompressStream output,
234
+ * because every "flush" adds extra overhead bytes, and some encoder
235
+ * settings (e.g. quality @c 0 and @c 1) might imply a "soft flush"
236
+ * after every chunk of input.
237
+ *
238
+ * @param input_size size of projected input
239
+ * @returns @c 0 if result does not fit @c size_t
240
+ */
241
+ BROTLI_ENC_API size_t BrotliEncoderMaxCompressedSize(size_t input_size);
242
+
243
+ /**
244
+ * Performs one-shot memory-to-memory compression.
245
+ *
246
+ * Compresses the data in @p input_buffer into @p encoded_buffer, and sets
247
+ * @p *encoded_size to the compressed length.
248
+ *
249
+ * @note If ::BrotliEncoderMaxCompressedSize(@p input_size) returns non-zero
250
+ * value, then output is guaranteed to be no longer than that.
251
+ *
252
+ * @param quality quality parameter value, e.g. ::BROTLI_DEFAULT_QUALITY
253
+ * @param lgwin lgwin parameter value, e.g. ::BROTLI_DEFAULT_WINDOW
254
+ * @param mode mode parameter value, e.g. ::BROTLI_DEFAULT_MODE
255
+ * @param input_size size of @p input_buffer
256
+ * @param input_buffer input data buffer with at least @p input_size
257
+ * addressable bytes
258
+ * @param[in, out] encoded_size @b in: size of @p encoded_buffer; \n
259
+ * @b out: length of compressed data written to
260
+ * @p encoded_buffer, or @c 0 if compression fails
261
+ * @param encoded_buffer compressed data destination buffer
262
+ * @returns ::BROTLI_FALSE in case of compression error
263
+ * @returns ::BROTLI_FALSE if output buffer is too small
264
+ * @returns ::BROTLI_TRUE otherwise
265
+ */
266
+ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompress(
267
+ int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
268
+ const uint8_t input_buffer[BROTLI_ARRAY_PARAM(input_size)],
269
+ size_t* encoded_size,
270
+ uint8_t encoded_buffer[BROTLI_ARRAY_PARAM(*encoded_size)]);
271
+
272
+ /**
273
+ * Compresses input stream to output stream.
274
+ *
275
+ * The values @p *available_in and @p *available_out must specify the number of
276
+ * bytes addressable at @p *next_in and @p *next_out respectively.
277
+ * When @p *available_out is @c 0, @p next_out is allowed to be @c NULL.
278
+ *
279
+ * After each call, @p *available_in will be decremented by the amount of input
280
+ * bytes consumed, and the @p *next_in pointer will be incremented by that
281
+ * amount. Similarly, @p *available_out will be decremented by the amount of
282
+ * output bytes written, and the @p *next_out pointer will be incremented by
283
+ * that amount.
284
+ *
285
+ * @p total_out, if it is not a null-pointer, will be set to the number
286
+ * of bytes decompressed since the last @p state initialization.
287
+ *
288
+ *
289
+ *
290
+ * Internally workflow consists of 3 tasks:
291
+ * -# (optionally) copy input data to internal buffer
292
+ * -# actually compress data and (optionally) store it to internal buffer
293
+ * -# (optionally) copy compressed bytes from internal buffer to output stream
294
+ *
295
+ * Whenever all 3 tasks can't move forward anymore, or error occurs, this
296
+ * method returns the control flow to caller.
297
+ *
298
+ * @p op is used to perform flush, finish the stream, or inject metadata block.
299
+ * See ::BrotliEncoderOperation for more information.
300
+ *
301
+ * Flushing the stream means forcing encoding of all input passed to encoder and
302
+ * completing the current output block, so it could be fully decoded by stream
303
+ * decoder. To perform flush set @p op to ::BROTLI_OPERATION_FLUSH.
304
+ * Under some circumstances (e.g. lack of output stream capacity) this operation
305
+ * would require several calls to ::BrotliEncoderCompressStream. The method must
306
+ * be called again until both input stream is depleted and encoder has no more
307
+ * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
308
+ *
309
+ * Finishing the stream means encoding of all input passed to encoder and
310
+ * adding specific "final" marks, so stream decoder could determine that stream
311
+ * is complete. To perform finish set @p op to ::BROTLI_OPERATION_FINISH.
312
+ * Under some circumstances (e.g. lack of output stream capacity) this operation
313
+ * would require several calls to ::BrotliEncoderCompressStream. The method must
314
+ * be called again until both input stream is depleted and encoder has no more
315
+ * output (see ::BrotliEncoderHasMoreOutput) after the method is called.
316
+ *
317
+ * @warning When flushing and finishing, @p op should not change until operation
318
+ * is complete; input stream should not be swapped, reduced or
319
+ * extended as well.
320
+ *
321
+ * @param state encoder instance
322
+ * @param op requested operation
323
+ * @param[in, out] available_in @b in: amount of available input; \n
324
+ * @b out: amount of unused input
325
+ * @param[in, out] next_in pointer to the next input byte
326
+ * @param[in, out] available_out @b in: length of output buffer; \n
327
+ * @b out: remaining size of output buffer
328
+ * @param[in, out] next_out compressed output buffer cursor;
329
+ * can be @c NULL if @p available_out is @c 0
330
+ * @param[out] total_out number of bytes produced so far; can be @c NULL
331
+ * @returns ::BROTLI_FALSE if there was an error
332
+ * @returns ::BROTLI_TRUE otherwise
333
+ */
334
+ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderCompressStream(
335
+ BrotliEncoderState* state, BrotliEncoderOperation op, size_t* available_in,
336
+ const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
337
+ size_t* total_out);
338
+
339
+ /**
340
+ * Checks if encoder instance reached the final state.
341
+ *
342
+ * @param state encoder instance
343
+ * @returns ::BROTLI_TRUE if encoder is in a state where it reached the end of
344
+ * the input and produced all of the output
345
+ * @returns ::BROTLI_FALSE otherwise
346
+ */
347
+ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* state);
348
+
349
+ /**
350
+ * Checks if encoder has more output.
351
+ *
352
+ * @param state encoder instance
353
+ * @returns ::BROTLI_TRUE, if encoder has some unconsumed output
354
+ * @returns ::BROTLI_FALSE otherwise
355
+ */
356
+ BROTLI_ENC_API BROTLI_BOOL BrotliEncoderHasMoreOutput(
357
+ BrotliEncoderState* state);
358
+
359
+ /**
360
+ * Acquires pointer to internal output buffer.
361
+ *
362
+ * This method is used to make language bindings easier and more efficient:
363
+ * -# push data to ::BrotliEncoderCompressStream,
364
+ * until ::BrotliEncoderHasMoreOutput returns BROTL_TRUE
365
+ * -# use ::BrotliEncoderTakeOutput to peek bytes and copy to language-specific
366
+ * entity
367
+ *
368
+ * Also this could be useful if there is an output stream that is able to
369
+ * consume all the provided data (e.g. when data is saved to file system).
370
+ *
371
+ * @attention After every call to ::BrotliEncoderTakeOutput @p *size bytes of
372
+ * output are considered consumed for all consecutive calls to the
373
+ * instance methods; returned pointer becomes invalidated as well.
374
+ *
375
+ * @note Encoder output is not guaranteed to be contiguous. This means that
376
+ * after the size-unrestricted call to ::BrotliEncoderTakeOutput,
377
+ * immediate next call to ::BrotliEncoderTakeOutput may return more data.
378
+ *
379
+ * @param state encoder instance
380
+ * @param[in, out] size @b in: number of bytes caller is ready to take, @c 0 if
381
+ * any amount could be handled; \n
382
+ * @b out: amount of data pointed by returned pointer and
383
+ * considered consumed; \n
384
+ * out value is never greater than in value, unless it is @c 0
385
+ * @returns pointer to output data
386
+ */
387
+ BROTLI_ENC_API const uint8_t* BrotliEncoderTakeOutput(
388
+ BrotliEncoderState* state, size_t* size);
389
+
390
+
391
+ /**
392
+ * Gets an encoder library version.
393
+ *
394
+ * Look at BROTLI_VERSION for more information.
395
+ */
396
+ BROTLI_ENC_API uint32_t BrotliEncoderVersion(void);
397
+
398
+ #if defined(__cplusplus) || defined(c_plusplus)
399
+ } /* extern "C" */
400
+ #endif
401
+
402
+ #endif /* BROTLI_ENC_ENCODE_H_ */