brotli 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +11 -3
  4. data/Gemfile +2 -0
  5. data/ext/brotli/brotli.c +279 -0
  6. data/ext/brotli/brotli.h +2 -0
  7. data/ext/brotli/buffer.c +95 -0
  8. data/ext/brotli/buffer.h +19 -0
  9. data/ext/brotli/extconf.rb +21 -81
  10. data/lib/brotli/version.rb +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +5 -5
  12. data/vendor/brotli/dec/bit_reader.h +15 -15
  13. data/vendor/brotli/dec/context.h +1 -1
  14. data/vendor/brotli/dec/decode.c +433 -348
  15. data/vendor/brotli/dec/decode.h +74 -48
  16. data/vendor/brotli/dec/huffman.c +5 -4
  17. data/vendor/brotli/dec/huffman.h +4 -4
  18. data/vendor/brotli/dec/port.h +2 -95
  19. data/vendor/brotli/dec/prefix.h +5 -3
  20. data/vendor/brotli/dec/state.c +15 -27
  21. data/vendor/brotli/dec/state.h +21 -17
  22. data/vendor/brotli/dec/transform.h +1 -1
  23. data/vendor/brotli/enc/backward_references.c +892 -0
  24. data/vendor/brotli/enc/backward_references.h +85 -102
  25. data/vendor/brotli/enc/backward_references_inc.h +147 -0
  26. data/vendor/brotli/enc/bit_cost.c +35 -0
  27. data/vendor/brotli/enc/bit_cost.h +23 -121
  28. data/vendor/brotli/enc/bit_cost_inc.h +127 -0
  29. data/vendor/brotli/enc/block_encoder_inc.h +33 -0
  30. data/vendor/brotli/enc/block_splitter.c +197 -0
  31. data/vendor/brotli/enc/block_splitter.h +40 -50
  32. data/vendor/brotli/enc/block_splitter_inc.h +432 -0
  33. data/vendor/brotli/enc/brotli_bit_stream.c +1334 -0
  34. data/vendor/brotli/enc/brotli_bit_stream.h +95 -167
  35. data/vendor/brotli/enc/cluster.c +56 -0
  36. data/vendor/brotli/enc/cluster.h +23 -305
  37. data/vendor/brotli/enc/cluster_inc.h +315 -0
  38. data/vendor/brotli/enc/command.h +83 -76
  39. data/vendor/brotli/enc/compress_fragment.c +747 -0
  40. data/vendor/brotli/enc/compress_fragment.h +48 -37
  41. data/vendor/brotli/enc/compress_fragment_two_pass.c +557 -0
  42. data/vendor/brotli/enc/compress_fragment_two_pass.h +37 -26
  43. data/vendor/brotli/enc/compressor.cc +139 -0
  44. data/vendor/brotli/enc/compressor.h +146 -0
  45. data/vendor/brotli/enc/context.h +102 -96
  46. data/vendor/brotli/enc/dictionary_hash.h +9 -5
  47. data/vendor/brotli/enc/encode.c +1562 -0
  48. data/vendor/brotli/enc/encode.h +211 -199
  49. data/vendor/brotli/enc/encode_parallel.cc +161 -151
  50. data/vendor/brotli/enc/encode_parallel.h +7 -8
  51. data/vendor/brotli/enc/entropy_encode.c +501 -0
  52. data/vendor/brotli/enc/entropy_encode.h +107 -89
  53. data/vendor/brotli/enc/entropy_encode_static.h +29 -62
  54. data/vendor/brotli/enc/fast_log.h +26 -20
  55. data/vendor/brotli/enc/find_match_length.h +23 -20
  56. data/vendor/brotli/enc/hash.h +614 -871
  57. data/vendor/brotli/enc/hash_forgetful_chain_inc.h +249 -0
  58. data/vendor/brotli/enc/hash_longest_match_inc.h +241 -0
  59. data/vendor/brotli/enc/hash_longest_match_quickly_inc.h +230 -0
  60. data/vendor/brotli/enc/histogram.c +95 -0
  61. data/vendor/brotli/enc/histogram.h +49 -83
  62. data/vendor/brotli/enc/histogram_inc.h +51 -0
  63. data/vendor/brotli/enc/literal_cost.c +178 -0
  64. data/vendor/brotli/enc/literal_cost.h +16 -10
  65. data/vendor/brotli/enc/memory.c +181 -0
  66. data/vendor/brotli/enc/memory.h +62 -0
  67. data/vendor/brotli/enc/metablock.c +515 -0
  68. data/vendor/brotli/enc/metablock.h +87 -57
  69. data/vendor/brotli/enc/metablock_inc.h +183 -0
  70. data/vendor/brotli/enc/port.h +73 -47
  71. data/vendor/brotli/enc/prefix.h +34 -61
  72. data/vendor/brotli/enc/quality.h +130 -0
  73. data/vendor/brotli/enc/ringbuffer.h +137 -122
  74. data/vendor/brotli/enc/{static_dict.cc → static_dict.c} +162 -139
  75. data/vendor/brotli/enc/static_dict.h +23 -18
  76. data/vendor/brotli/enc/static_dict_lut.h +11223 -12037
  77. data/vendor/brotli/enc/streams.cc +7 -7
  78. data/vendor/brotli/enc/streams.h +32 -32
  79. data/vendor/brotli/enc/{utf8_util.cc → utf8_util.c} +22 -20
  80. data/vendor/brotli/enc/utf8_util.h +16 -9
  81. data/vendor/brotli/enc/write_bits.h +49 -43
  82. metadata +34 -25
  83. data/ext/brotli/brotli.cc +0 -181
  84. data/vendor/brotli/dec/Makefile +0 -12
  85. data/vendor/brotli/dec/dictionary.c +0 -9466
  86. data/vendor/brotli/dec/dictionary.h +0 -38
  87. data/vendor/brotli/dec/types.h +0 -38
  88. data/vendor/brotli/enc/Makefile +0 -14
  89. data/vendor/brotli/enc/backward_references.cc +0 -858
  90. data/vendor/brotli/enc/block_splitter.cc +0 -505
  91. data/vendor/brotli/enc/brotli_bit_stream.cc +0 -1181
  92. data/vendor/brotli/enc/compress_fragment.cc +0 -701
  93. data/vendor/brotli/enc/compress_fragment_two_pass.cc +0 -524
  94. data/vendor/brotli/enc/dictionary.cc +0 -9466
  95. data/vendor/brotli/enc/dictionary.h +0 -41
  96. data/vendor/brotli/enc/encode.cc +0 -1180
  97. data/vendor/brotli/enc/entropy_encode.cc +0 -480
  98. data/vendor/brotli/enc/histogram.cc +0 -67
  99. data/vendor/brotli/enc/literal_cost.cc +0 -165
  100. data/vendor/brotli/enc/metablock.cc +0 -539
  101. data/vendor/brotli/enc/transform.h +0 -248
  102. data/vendor/brotli/enc/types.h +0 -29
@@ -0,0 +1,1562 @@
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
+ /* Implementation of Brotli compressor. */
8
+
9
+ #include "./encode.h"
10
+
11
+ #include <stdlib.h> /* free, malloc */
12
+ #include <string.h> /* memcpy, memset */
13
+
14
+ #include "./backward_references.h"
15
+ #include "./bit_cost.h"
16
+ #include "./brotli_bit_stream.h"
17
+ #include "./compress_fragment.h"
18
+ #include "./compress_fragment_two_pass.h"
19
+ #include "./context.h"
20
+ #include "./entropy_encode.h"
21
+ #include "./fast_log.h"
22
+ #include "./hash.h"
23
+ #include "./histogram.h"
24
+ #include "./memory.h"
25
+ #include "./metablock.h"
26
+ #include "./port.h"
27
+ #include "./prefix.h"
28
+ #include "./quality.h"
29
+ #include "./ringbuffer.h"
30
+ #include "./utf8_util.h"
31
+ #include "./write_bits.h"
32
+
33
+ #if defined(__cplusplus) || defined(c_plusplus)
34
+ extern "C" {
35
+ #endif
36
+
37
+ #define COPY_ARRAY(dst, src) memcpy(dst, src, sizeof(src));
38
+
39
+ typedef enum BrotliEncoderStreamState {
40
+ /* Default state. */
41
+ BROTLI_STREAM_PROCESSING = 0,
42
+ /* Intermediate state; after next block is emitted, byte-padding should be
43
+ performed before getting back to default state. */
44
+ BROTLI_STREAM_FLUSH_REQUESTED = 1,
45
+ /* Last metablock was produced; no more input is acceptable. */
46
+ BROTLI_STREAM_FINISHED = 2
47
+ } BrotliEncoderStreamState;
48
+
49
+ typedef struct BrotliEncoderStateStruct {
50
+ BrotliEncoderParams params;
51
+
52
+ MemoryManager memory_manager_;
53
+
54
+ Hashers hashers_;
55
+ uint64_t input_pos_;
56
+ RingBuffer ringbuffer_;
57
+ size_t cmd_alloc_size_;
58
+ Command* commands_;
59
+ size_t num_commands_;
60
+ size_t num_literals_;
61
+ size_t last_insert_len_;
62
+ uint64_t last_flush_pos_;
63
+ uint64_t last_processed_pos_;
64
+ int dist_cache_[4];
65
+ int saved_dist_cache_[4];
66
+ uint8_t last_byte_;
67
+ uint8_t last_byte_bits_;
68
+ uint8_t prev_byte_;
69
+ uint8_t prev_byte2_;
70
+ size_t storage_size_;
71
+ uint8_t* storage_;
72
+ /* Hash table for FAST_ONE_PASS_COMPRESSION_QUALITY mode. */
73
+ int small_table_[1 << 10]; /* 4KiB */
74
+ int* large_table_; /* Allocated only when needed */
75
+ size_t large_table_size_;
76
+ /* Command and distance prefix codes (each 64 symbols, stored back-to-back)
77
+ used for the next block in FAST_ONE_PASS_COMPRESSION_QUALITY. The command
78
+ prefix code is over a smaller alphabet with the following 64 symbols:
79
+ 0 - 15: insert length code 0, copy length code 0 - 15, same distance
80
+ 16 - 39: insert length code 0, copy length code 0 - 23
81
+ 40 - 63: insert length code 0 - 23, copy length code 0
82
+ Note that symbols 16 and 40 represent the same code in the full alphabet,
83
+ but we do not use either of them in FAST_ONE_PASS_COMPRESSION_QUALITY. */
84
+ uint8_t cmd_depths_[128];
85
+ uint16_t cmd_bits_[128];
86
+ /* The compressed form of the command and distance prefix codes for the next
87
+ block in FAST_ONE_PASS_COMPRESSION_QUALITY. */
88
+ uint8_t cmd_code_[512];
89
+ size_t cmd_code_numbits_;
90
+ /* Command and literal buffers for FAST_TWO_PASS_COMPRESSION_QUALITY. */
91
+ uint32_t* command_buf_;
92
+ uint8_t* literal_buf_;
93
+
94
+ uint8_t* next_out_;
95
+ size_t available_out_;
96
+ size_t total_out_;
97
+ uint8_t flush_buf_[2];
98
+ BrotliEncoderStreamState stream_state_;
99
+
100
+ BROTLI_BOOL is_last_block_emitted_;
101
+ BROTLI_BOOL is_initialized_;
102
+ } BrotliEncoderStateStruct;
103
+
104
+ static BROTLI_BOOL EnsureInitialized(BrotliEncoderState* s);
105
+
106
+ size_t BrotliEncoderInputBlockSize(BrotliEncoderState* s) {
107
+ if (!EnsureInitialized(s)) return 0;
108
+ return (size_t)1 << s->params.lgblock;
109
+ }
110
+
111
+ static uint64_t UnprocessedInputSize(BrotliEncoderState* s) {
112
+ return s->input_pos_ - s->last_processed_pos_;
113
+ }
114
+
115
+ static size_t RemainingInputBlockSize(BrotliEncoderState* s) {
116
+ const uint64_t delta = UnprocessedInputSize(s);
117
+ size_t block_size = BrotliEncoderInputBlockSize(s);
118
+ if (delta >= block_size) return 0;
119
+ return block_size - (size_t)delta;
120
+ }
121
+
122
+ BROTLI_BOOL BrotliEncoderSetParameter(
123
+ BrotliEncoderState* state, BrotliEncoderParameter p, uint32_t value) {
124
+ /* Changing parameters on the fly is not implemented yet. */
125
+ if (state->is_initialized_) return BROTLI_FALSE;
126
+ /* TODO: Validate/clamp params here. */
127
+ switch (p) {
128
+ case BROTLI_PARAM_MODE:
129
+ state->params.mode = (BrotliEncoderMode)value;
130
+ return BROTLI_TRUE;
131
+
132
+ case BROTLI_PARAM_QUALITY:
133
+ state->params.quality = (int)value;
134
+ return BROTLI_TRUE;
135
+
136
+ case BROTLI_PARAM_LGWIN:
137
+ state->params.lgwin = (int)value;
138
+ return BROTLI_TRUE;
139
+
140
+ case BROTLI_PARAM_LGBLOCK:
141
+ state->params.lgblock = (int)value;
142
+ return BROTLI_TRUE;
143
+
144
+ default: return BROTLI_FALSE;
145
+ }
146
+ }
147
+
148
+ static void RecomputeDistancePrefixes(Command* cmds,
149
+ size_t num_commands,
150
+ uint32_t num_direct_distance_codes,
151
+ uint32_t distance_postfix_bits) {
152
+ size_t i;
153
+ if (num_direct_distance_codes == 0 && distance_postfix_bits == 0) {
154
+ return;
155
+ }
156
+ for (i = 0; i < num_commands; ++i) {
157
+ Command* cmd = &cmds[i];
158
+ if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
159
+ PrefixEncodeCopyDistance(CommandDistanceCode(cmd),
160
+ num_direct_distance_codes,
161
+ distance_postfix_bits,
162
+ &cmd->dist_prefix_,
163
+ &cmd->dist_extra_);
164
+ }
165
+ }
166
+ }
167
+
168
+ /* Wraps 64-bit input position to 32-bit ringbuffer position preserving
169
+ "not-a-first-lap" feature. */
170
+ static uint32_t WrapPosition(uint64_t position) {
171
+ uint32_t result = (uint32_t)position;
172
+ uint64_t gb = position >> 30;
173
+ if (gb > 2) {
174
+ /* Wrap every 2GiB; The first 3GB are continous. */
175
+ result = (result & ((1u << 30) - 1)) | ((uint32_t)((gb - 1) & 1) + 1) << 30;
176
+ }
177
+ return result;
178
+ }
179
+
180
+ static uint8_t* GetBrotliStorage(BrotliEncoderState* s, size_t size) {
181
+ MemoryManager* m = &s->memory_manager_;
182
+ if (s->storage_size_ < size) {
183
+ BROTLI_FREE(m, s->storage_);
184
+ s->storage_ = BROTLI_ALLOC(m, uint8_t, size);
185
+ if (BROTLI_IS_OOM(m)) return NULL;
186
+ s->storage_size_ = size;
187
+ }
188
+ return s->storage_;
189
+ }
190
+
191
+ static size_t HashTableSize(size_t max_table_size, size_t input_size) {
192
+ size_t htsize = 256;
193
+ while (htsize < max_table_size && htsize < input_size) {
194
+ htsize <<= 1;
195
+ }
196
+ return htsize;
197
+ }
198
+
199
+ static int* GetHashTable(BrotliEncoderState* s, int quality,
200
+ size_t input_size, size_t* table_size) {
201
+ /* Use smaller hash table when input.size() is smaller, since we
202
+ fill the table, incurring O(hash table size) overhead for
203
+ compression, and if the input is short, we won't need that
204
+ many hash table entries anyway. */
205
+ MemoryManager* m = &s->memory_manager_;
206
+ const size_t max_table_size = MaxHashTableSize(quality);
207
+ size_t htsize = HashTableSize(max_table_size, input_size);
208
+ int* table;
209
+ assert(max_table_size >= 256);
210
+
211
+ if (htsize <= sizeof(s->small_table_) / sizeof(s->small_table_[0])) {
212
+ table = s->small_table_;
213
+ } else {
214
+ if (htsize > s->large_table_size_) {
215
+ s->large_table_size_ = htsize;
216
+ BROTLI_FREE(m, s->large_table_);
217
+ s->large_table_ = BROTLI_ALLOC(m, int, htsize);
218
+ if (BROTLI_IS_OOM(m)) return 0;
219
+ }
220
+ table = s->large_table_;
221
+ }
222
+
223
+ *table_size = htsize;
224
+ memset(table, 0, htsize * sizeof(*table));
225
+ return table;
226
+ }
227
+
228
+ static void EncodeWindowBits(int lgwin, uint8_t* last_byte,
229
+ uint8_t* last_byte_bits) {
230
+ if (lgwin == 16) {
231
+ *last_byte = 0;
232
+ *last_byte_bits = 1;
233
+ } else if (lgwin == 17) {
234
+ *last_byte = 1;
235
+ *last_byte_bits = 7;
236
+ } else if (lgwin > 17) {
237
+ *last_byte = (uint8_t)(((lgwin - 17) << 1) | 1);
238
+ *last_byte_bits = 4;
239
+ } else {
240
+ *last_byte = (uint8_t)(((lgwin - 8) << 4) | 1);
241
+ *last_byte_bits = 7;
242
+ }
243
+ }
244
+
245
+ /* Initializes the command and distance prefix codes for the first block. */
246
+ static void InitCommandPrefixCodes(uint8_t cmd_depths[128],
247
+ uint16_t cmd_bits[128],
248
+ uint8_t cmd_code[512],
249
+ size_t* cmd_code_numbits) {
250
+ static const uint8_t kDefaultCommandDepths[128] = {
251
+ 0, 4, 4, 5, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
252
+ 0, 0, 0, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7,
253
+ 7, 7, 10, 10, 10, 10, 10, 10, 0, 4, 4, 5, 5, 5, 6, 6,
254
+ 7, 8, 8, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
255
+ 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256
+ 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4,
257
+ 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 8, 10,
258
+ 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
259
+ };
260
+ static const uint16_t kDefaultCommandBits[128] = {
261
+ 0, 0, 8, 9, 3, 35, 7, 71,
262
+ 39, 103, 23, 47, 175, 111, 239, 31,
263
+ 0, 0, 0, 4, 12, 2, 10, 6,
264
+ 13, 29, 11, 43, 27, 59, 87, 55,
265
+ 15, 79, 319, 831, 191, 703, 447, 959,
266
+ 0, 14, 1, 25, 5, 21, 19, 51,
267
+ 119, 159, 95, 223, 479, 991, 63, 575,
268
+ 127, 639, 383, 895, 255, 767, 511, 1023,
269
+ 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
270
+ 27, 59, 7, 39, 23, 55, 30, 1, 17, 9, 25, 5, 0, 8, 4, 12,
271
+ 2, 10, 6, 21, 13, 29, 3, 19, 11, 15, 47, 31, 95, 63, 127, 255,
272
+ 767, 2815, 1791, 3839, 511, 2559, 1535, 3583, 1023, 3071, 2047, 4095,
273
+ };
274
+ static const uint8_t kDefaultCommandCode[] = {
275
+ 0xff, 0x77, 0xd5, 0xbf, 0xe7, 0xde, 0xea, 0x9e, 0x51, 0x5d, 0xde, 0xc6,
276
+ 0x70, 0x57, 0xbc, 0x58, 0x58, 0x58, 0xd8, 0xd8, 0x58, 0xd5, 0xcb, 0x8c,
277
+ 0xea, 0xe0, 0xc3, 0x87, 0x1f, 0x83, 0xc1, 0x60, 0x1c, 0x67, 0xb2, 0xaa,
278
+ 0x06, 0x83, 0xc1, 0x60, 0x30, 0x18, 0xcc, 0xa1, 0xce, 0x88, 0x54, 0x94,
279
+ 0x46, 0xe1, 0xb0, 0xd0, 0x4e, 0xb2, 0xf7, 0x04, 0x00,
280
+ };
281
+ static const size_t kDefaultCommandCodeNumBits = 448;
282
+ COPY_ARRAY(cmd_depths, kDefaultCommandDepths);
283
+ COPY_ARRAY(cmd_bits, kDefaultCommandBits);
284
+
285
+ /* Initialize the pre-compressed form of the command and distance prefix
286
+ codes. */
287
+ COPY_ARRAY(cmd_code, kDefaultCommandCode);
288
+ *cmd_code_numbits = kDefaultCommandCodeNumBits;
289
+ }
290
+
291
+ /* Decide about the context map based on the ability of the prediction
292
+ ability of the previous byte UTF8-prefix on the next byte. The
293
+ prediction ability is calculated as shannon entropy. Here we need
294
+ shannon entropy instead of 'BitsEntropy' since the prefix will be
295
+ encoded with the remaining 6 bits of the following byte, and
296
+ BitsEntropy will assume that symbol to be stored alone using Huffman
297
+ coding. */
298
+ static void ChooseContextMap(int quality,
299
+ uint32_t* bigram_histo,
300
+ size_t* num_literal_contexts,
301
+ const uint32_t** literal_context_map) {
302
+ static const uint32_t kStaticContextMapContinuation[64] = {
303
+ 1, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
304
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
305
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
306
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
307
+ };
308
+ static const uint32_t kStaticContextMapSimpleUTF8[64] = {
309
+ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
310
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
311
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
312
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
313
+ };
314
+
315
+ uint32_t monogram_histo[3] = { 0 };
316
+ uint32_t two_prefix_histo[6] = { 0 };
317
+ size_t total = 0;
318
+ size_t i;
319
+ size_t dummy;
320
+ double entropy[4];
321
+ for (i = 0; i < 9; ++i) {
322
+ size_t j = i;
323
+ total += bigram_histo[i];
324
+ monogram_histo[i % 3] += bigram_histo[i];
325
+ if (j >= 6) {
326
+ j -= 6;
327
+ }
328
+ two_prefix_histo[j] += bigram_histo[i];
329
+ }
330
+ entropy[1] = ShannonEntropy(monogram_histo, 3, &dummy);
331
+ entropy[2] = (ShannonEntropy(two_prefix_histo, 3, &dummy) +
332
+ ShannonEntropy(two_prefix_histo + 3, 3, &dummy));
333
+ entropy[3] = 0;
334
+ for (i = 0; i < 3; ++i) {
335
+ entropy[3] += ShannonEntropy(bigram_histo + 3 * i, 3, &dummy);
336
+ }
337
+
338
+ assert(total != 0);
339
+ entropy[0] = 1.0 / (double)total;
340
+ entropy[1] *= entropy[0];
341
+ entropy[2] *= entropy[0];
342
+ entropy[3] *= entropy[0];
343
+
344
+ if (quality < MIN_QUALITY_FOR_HQ_CONTEXT_MODELING) {
345
+ /* 3 context models is a bit slower, don't use it at lower qualities. */
346
+ entropy[3] = entropy[1] * 10;
347
+ }
348
+ /* If expected savings by symbol are less than 0.2 bits, skip the
349
+ context modeling -- in exchange for faster decoding speed. */
350
+ if (entropy[1] - entropy[2] < 0.2 &&
351
+ entropy[1] - entropy[3] < 0.2) {
352
+ *num_literal_contexts = 1;
353
+ } else if (entropy[2] - entropy[3] < 0.02) {
354
+ *num_literal_contexts = 2;
355
+ *literal_context_map = kStaticContextMapSimpleUTF8;
356
+ } else {
357
+ *num_literal_contexts = 3;
358
+ *literal_context_map = kStaticContextMapContinuation;
359
+ }
360
+ }
361
+
362
+ static void DecideOverLiteralContextModeling(const uint8_t* input,
363
+ size_t start_pos, size_t length, size_t mask, int quality,
364
+ ContextType* literal_context_mode, size_t* num_literal_contexts,
365
+ const uint32_t** literal_context_map) {
366
+ if (quality < MIN_QUALITY_FOR_CONTEXT_MODELING || length < 64) {
367
+ return;
368
+ } else {
369
+ /* Gather bigram data of the UTF8 byte prefixes. To make the analysis of
370
+ UTF8 data faster we only examine 64 byte long strides at every 4kB
371
+ intervals. */
372
+ const size_t end_pos = start_pos + length;
373
+ uint32_t bigram_prefix_histo[9] = { 0 };
374
+ for (; start_pos + 64 <= end_pos; start_pos += 4096) {
375
+ static const int lut[4] = { 0, 0, 1, 2 };
376
+ const size_t stride_end_pos = start_pos + 64;
377
+ int prev = lut[input[start_pos & mask] >> 6] * 3;
378
+ size_t pos;
379
+ for (pos = start_pos + 1; pos < stride_end_pos; ++pos) {
380
+ const uint8_t literal = input[pos & mask];
381
+ ++bigram_prefix_histo[prev + lut[literal >> 6]];
382
+ prev = lut[literal >> 6] * 3;
383
+ }
384
+ }
385
+ *literal_context_mode = CONTEXT_UTF8;
386
+ ChooseContextMap(quality, &bigram_prefix_histo[0], num_literal_contexts,
387
+ literal_context_map);
388
+ }
389
+ }
390
+
391
+ static BROTLI_BOOL ShouldCompress(
392
+ const uint8_t* data, const size_t mask, const uint64_t last_flush_pos,
393
+ const size_t bytes, const size_t num_literals, const size_t num_commands) {
394
+ if (num_commands < (bytes >> 8) + 2) {
395
+ if (num_literals > 0.99 * (double)bytes) {
396
+ uint32_t literal_histo[256] = { 0 };
397
+ static const uint32_t kSampleRate = 13;
398
+ static const double kMinEntropy = 7.92;
399
+ const double bit_cost_threshold =
400
+ (double)bytes * kMinEntropy / kSampleRate;
401
+ size_t t = (bytes + kSampleRate - 1) / kSampleRate;
402
+ uint32_t pos = (uint32_t)last_flush_pos;
403
+ size_t i;
404
+ for (i = 0; i < t; i++) {
405
+ ++literal_histo[data[pos & mask]];
406
+ pos += kSampleRate;
407
+ }
408
+ if (BitsEntropy(literal_histo, 256) > bit_cost_threshold) {
409
+ return BROTLI_FALSE;
410
+ }
411
+ }
412
+ }
413
+ return BROTLI_TRUE;
414
+ }
415
+
416
+ static void WriteMetaBlockInternal(MemoryManager* m,
417
+ const uint8_t* data,
418
+ const size_t mask,
419
+ const uint64_t last_flush_pos,
420
+ const size_t bytes,
421
+ const BROTLI_BOOL is_last,
422
+ const BrotliEncoderParams* params,
423
+ const uint8_t prev_byte,
424
+ const uint8_t prev_byte2,
425
+ const size_t num_literals,
426
+ const size_t num_commands,
427
+ Command* commands,
428
+ const int* saved_dist_cache,
429
+ int* dist_cache,
430
+ size_t* storage_ix,
431
+ uint8_t* storage) {
432
+ const uint32_t wrapped_last_flush_pos = WrapPosition(last_flush_pos);
433
+ uint8_t last_byte;
434
+ uint8_t last_byte_bits;
435
+ uint32_t num_direct_distance_codes = 0;
436
+ uint32_t distance_postfix_bits = 0;
437
+
438
+ if (bytes == 0) {
439
+ /* Write the ISLAST and ISEMPTY bits. */
440
+ BrotliWriteBits(2, 3, storage_ix, storage);
441
+ *storage_ix = (*storage_ix + 7u) & ~7u;
442
+ return;
443
+ }
444
+
445
+ if (!ShouldCompress(data, mask, last_flush_pos, bytes,
446
+ num_literals, num_commands)) {
447
+ /* Restore the distance cache, as its last update by
448
+ CreateBackwardReferences is now unused. */
449
+ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0]));
450
+ BrotliStoreUncompressedMetaBlock(is_last, data,
451
+ wrapped_last_flush_pos, mask, bytes,
452
+ storage_ix, storage);
453
+ return;
454
+ }
455
+
456
+ last_byte = storage[0];
457
+ last_byte_bits = (uint8_t)(*storage_ix & 0xff);
458
+ if (params->quality >= MIN_QUALITY_FOR_RECOMPUTE_DISTANCE_PREFIXES &&
459
+ params->mode == BROTLI_MODE_FONT) {
460
+ num_direct_distance_codes = 12;
461
+ distance_postfix_bits = 1;
462
+ RecomputeDistancePrefixes(commands,
463
+ num_commands,
464
+ num_direct_distance_codes,
465
+ distance_postfix_bits);
466
+ }
467
+ if (params->quality <= MAX_QUALITY_FOR_STATIC_ENRTOPY_CODES) {
468
+ BrotliStoreMetaBlockFast(m, data, wrapped_last_flush_pos,
469
+ bytes, mask, is_last,
470
+ commands, num_commands,
471
+ storage_ix, storage);
472
+ if (BROTLI_IS_OOM(m)) return;
473
+ } else if (params->quality < MIN_QUALITY_FOR_BLOCK_SPLIT) {
474
+ BrotliStoreMetaBlockTrivial(m, data, wrapped_last_flush_pos,
475
+ bytes, mask, is_last,
476
+ commands, num_commands,
477
+ storage_ix, storage);
478
+ if (BROTLI_IS_OOM(m)) return;
479
+ } else {
480
+ ContextType literal_context_mode = CONTEXT_UTF8;
481
+ MetaBlockSplit mb;
482
+ InitMetaBlockSplit(&mb);
483
+ if (params->quality < MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING) {
484
+ size_t num_literal_contexts = 1;
485
+ const uint32_t* literal_context_map = NULL;
486
+ DecideOverLiteralContextModeling(data, wrapped_last_flush_pos,
487
+ bytes, mask,
488
+ params->quality,
489
+ &literal_context_mode,
490
+ &num_literal_contexts,
491
+ &literal_context_map);
492
+ if (literal_context_map == NULL) {
493
+ BrotliBuildMetaBlockGreedy(m, data, wrapped_last_flush_pos, mask,
494
+ commands, num_commands, &mb);
495
+ if (BROTLI_IS_OOM(m)) return;
496
+ } else {
497
+ BrotliBuildMetaBlockGreedyWithContexts(m, data,
498
+ wrapped_last_flush_pos,
499
+ mask,
500
+ prev_byte, prev_byte2,
501
+ literal_context_mode,
502
+ num_literal_contexts,
503
+ literal_context_map,
504
+ commands, num_commands,
505
+ &mb);
506
+ if (BROTLI_IS_OOM(m)) return;
507
+ }
508
+ } else {
509
+ if (!BrotliIsMostlyUTF8(data, wrapped_last_flush_pos, mask, bytes,
510
+ kMinUTF8Ratio)) {
511
+ literal_context_mode = CONTEXT_SIGNED;
512
+ }
513
+ BrotliBuildMetaBlock(m, data, wrapped_last_flush_pos, mask, params,
514
+ prev_byte, prev_byte2,
515
+ commands, num_commands,
516
+ literal_context_mode,
517
+ &mb);
518
+ if (BROTLI_IS_OOM(m)) return;
519
+ }
520
+ if (params->quality >= MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS) {
521
+ BrotliOptimizeHistograms(num_direct_distance_codes,
522
+ distance_postfix_bits,
523
+ &mb);
524
+ }
525
+ BrotliStoreMetaBlock(m, data, wrapped_last_flush_pos, bytes, mask,
526
+ prev_byte, prev_byte2,
527
+ is_last,
528
+ num_direct_distance_codes,
529
+ distance_postfix_bits,
530
+ literal_context_mode,
531
+ commands, num_commands,
532
+ &mb,
533
+ storage_ix, storage);
534
+ if (BROTLI_IS_OOM(m)) return;
535
+ DestroyMetaBlockSplit(m, &mb);
536
+ }
537
+ if (bytes + 4 < (*storage_ix >> 3)) {
538
+ /* Restore the distance cache and last byte. */
539
+ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0]));
540
+ storage[0] = last_byte;
541
+ *storage_ix = last_byte_bits;
542
+ BrotliStoreUncompressedMetaBlock(is_last, data,
543
+ wrapped_last_flush_pos, mask,
544
+ bytes, storage_ix, storage);
545
+ }
546
+ }
547
+
548
+ static BROTLI_BOOL EnsureInitialized(BrotliEncoderState* s) {
549
+ if (BROTLI_IS_OOM(&s->memory_manager_)) return BROTLI_FALSE;
550
+ if (s->is_initialized_) return BROTLI_TRUE;
551
+
552
+ SanitizeParams(&s->params);
553
+ s->params.lgblock = ComputeLgBlock(&s->params);
554
+
555
+ RingBufferSetup(&s->params, &s->ringbuffer_);
556
+
557
+ /* Initialize last byte with stream header. */
558
+ EncodeWindowBits(s->params.lgwin, &s->last_byte_, &s->last_byte_bits_);
559
+
560
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
561
+ InitCommandPrefixCodes(s->cmd_depths_, s->cmd_bits_,
562
+ s->cmd_code_, &s->cmd_code_numbits_);
563
+ }
564
+
565
+ /* Initialize hashers. */
566
+ HashersSetup(&s->memory_manager_, &s->hashers_, ChooseHasher(&s->params));
567
+ if (BROTLI_IS_OOM(&s->memory_manager_)) return BROTLI_FALSE;
568
+
569
+ s->is_initialized_ = BROTLI_TRUE;
570
+ return BROTLI_TRUE;
571
+ }
572
+
573
+ static void BrotliEncoderInitState(BrotliEncoderState* s) {
574
+ s->params.mode = BROTLI_DEFAULT_MODE;
575
+ s->params.quality = BROTLI_DEFAULT_QUALITY;
576
+ s->params.lgwin = BROTLI_DEFAULT_WINDOW;
577
+ s->params.lgblock = 0;
578
+
579
+ s->input_pos_ = 0;
580
+ s->num_commands_ = 0;
581
+ s->num_literals_ = 0;
582
+ s->last_insert_len_ = 0;
583
+ s->last_flush_pos_ = 0;
584
+ s->last_processed_pos_ = 0;
585
+ s->prev_byte_ = 0;
586
+ s->prev_byte2_ = 0;
587
+ s->storage_size_ = 0;
588
+ s->storage_ = 0;
589
+ s->large_table_ = NULL;
590
+ s->large_table_size_ = 0;
591
+ s->cmd_code_numbits_ = 0;
592
+ s->command_buf_ = NULL;
593
+ s->literal_buf_ = NULL;
594
+ s->next_out_ = NULL;
595
+ s->available_out_ = 0;
596
+ s->total_out_ = 0;
597
+ s->stream_state_ = BROTLI_STREAM_PROCESSING;
598
+ s->is_last_block_emitted_ = BROTLI_FALSE;
599
+ s->is_initialized_ = BROTLI_FALSE;
600
+
601
+ InitHashers(&s->hashers_);
602
+
603
+ RingBufferInit(&s->ringbuffer_);
604
+
605
+ s->commands_ = 0;
606
+ s->cmd_alloc_size_ = 0;
607
+
608
+ /* Initialize distance cache. */
609
+ s->dist_cache_[0] = 4;
610
+ s->dist_cache_[1] = 11;
611
+ s->dist_cache_[2] = 15;
612
+ s->dist_cache_[3] = 16;
613
+ /* Save the state of the distance cache in case we need to restore it for
614
+ emitting an uncompressed block. */
615
+ memcpy(s->saved_dist_cache_, s->dist_cache_, sizeof(s->dist_cache_));
616
+ }
617
+
618
+ BrotliEncoderState* BrotliEncoderCreateInstance(brotli_alloc_func alloc_func,
619
+ brotli_free_func free_func,
620
+ void* opaque) {
621
+ BrotliEncoderState* state = 0;
622
+ if (!alloc_func && !free_func) {
623
+ state = (BrotliEncoderState*)malloc(sizeof(BrotliEncoderState));
624
+ } else if (alloc_func && free_func) {
625
+ state = (BrotliEncoderState*)alloc_func(opaque, sizeof(BrotliEncoderState));
626
+ }
627
+ if (state == 0) {
628
+ /* BROTLI_DUMP(); */
629
+ return 0;
630
+ }
631
+ BrotliInitMemoryManager(
632
+ &state->memory_manager_, alloc_func, free_func, opaque);
633
+ BrotliEncoderInitState(state);
634
+ return state;
635
+ }
636
+
637
+ static void BrotliEncoderCleanupState(BrotliEncoderState* s) {
638
+ MemoryManager* m = &s->memory_manager_;
639
+ if (BROTLI_IS_OOM(m)) {
640
+ BrotliWipeOutMemoryManager(m);
641
+ return;
642
+ }
643
+ BROTLI_FREE(m, s->storage_);
644
+ BROTLI_FREE(m, s->commands_);
645
+ RingBufferFree(m, &s->ringbuffer_);
646
+ DestroyHashers(m, &s->hashers_);
647
+ BROTLI_FREE(m, s->large_table_);
648
+ BROTLI_FREE(m, s->command_buf_);
649
+ BROTLI_FREE(m, s->literal_buf_);
650
+ }
651
+
652
+ /* Deinitializes and frees BrotliEncoderState instance. */
653
+ void BrotliEncoderDestroyInstance(BrotliEncoderState* state) {
654
+ if (!state) {
655
+ return;
656
+ } else {
657
+ MemoryManager* m = &state->memory_manager_;
658
+ brotli_free_func free_func = m->free_func;
659
+ void* opaque = m->opaque;
660
+ BrotliEncoderCleanupState(state);
661
+ free_func(opaque, state);
662
+ }
663
+ }
664
+
665
+ void BrotliEncoderCopyInputToRingBuffer(BrotliEncoderState* s,
666
+ const size_t input_size,
667
+ const uint8_t* input_buffer) {
668
+ RingBuffer* ringbuffer_ = &s->ringbuffer_;
669
+ MemoryManager* m = &s->memory_manager_;
670
+ if (!EnsureInitialized(s)) return;
671
+ RingBufferWrite(m, input_buffer, input_size, ringbuffer_);
672
+ if (BROTLI_IS_OOM(m)) return;
673
+ s->input_pos_ += input_size;
674
+
675
+ /* TL;DR: If needed, initialize 7 more bytes in the ring buffer to make the
676
+ hashing not depend on uninitialized data. This makes compression
677
+ deterministic and it prevents uninitialized memory warnings in Valgrind.
678
+ Even without erasing, the output would be valid (but nondeterministic).
679
+
680
+ Background information: The compressor stores short (at most 8 bytes)
681
+ substrings of the input already read in a hash table, and detects
682
+ repetitions by looking up such substrings in the hash table. If it
683
+ can find a substring, it checks whether the substring is really there
684
+ in the ring buffer (or it's just a hash collision). Should the hash
685
+ table become corrupt, this check makes sure that the output is
686
+ still valid, albeit the compression ratio would be bad.
687
+
688
+ The compressor populates the hash table from the ring buffer as it's
689
+ reading new bytes from the input. However, at the last few indexes of
690
+ the ring buffer, there are not enough bytes to build full-length
691
+ substrings from. Since the hash table always contains full-length
692
+ substrings, we erase with dummy 0s here to make sure that those
693
+ substrings will contain 0s at the end instead of uninitialized
694
+ data.
695
+
696
+ Please note that erasing is not necessary (because the
697
+ memory region is already initialized since he ring buffer
698
+ has a `tail' that holds a copy of the beginning,) so we
699
+ skip erasing if we have already gone around at least once in
700
+ the ring buffer.
701
+
702
+ Only clear during the first round of ringbuffer writes. On
703
+ subsequent rounds data in the ringbuffer would be affected. */
704
+ if (ringbuffer_->pos_ <= ringbuffer_->mask_) {
705
+ /* This is the first time when the ring buffer is being written.
706
+ We clear 7 bytes just after the bytes that have been copied from
707
+ the input buffer.
708
+
709
+ The ringbuffer has a "tail" that holds a copy of the beginning,
710
+ but only once the ring buffer has been fully written once, i.e.,
711
+ pos <= mask. For the first time, we need to write values
712
+ in this tail (where index may be larger than mask), so that
713
+ we have exactly defined behavior and don't read un-initialized
714
+ memory. Due to performance reasons, hashing reads data using a
715
+ LOAD64, which can go 7 bytes beyond the bytes written in the
716
+ ringbuffer. */
717
+ memset(ringbuffer_->buffer_ + ringbuffer_->pos_, 0, 7);
718
+ }
719
+ }
720
+
721
+ void BrotliEncoderSetCustomDictionary(BrotliEncoderState* s, size_t size,
722
+ const uint8_t* dict) {
723
+ size_t max_dict_size = MaxBackwardLimit(s->params.lgwin);
724
+ size_t dict_size = size;
725
+ MemoryManager* m = &s->memory_manager_;
726
+
727
+ if (!EnsureInitialized(s)) return;
728
+
729
+ if (dict_size == 0 ||
730
+ s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
731
+ s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
732
+ return;
733
+ }
734
+ if (size > max_dict_size) {
735
+ dict += size - max_dict_size;
736
+ dict_size = max_dict_size;
737
+ }
738
+ BrotliEncoderCopyInputToRingBuffer(s, dict_size, dict);
739
+ s->last_flush_pos_ = dict_size;
740
+ s->last_processed_pos_ = dict_size;
741
+ if (dict_size > 0) {
742
+ s->prev_byte_ = dict[dict_size - 1];
743
+ }
744
+ if (dict_size > 1) {
745
+ s->prev_byte2_ = dict[dict_size - 2];
746
+ }
747
+ HashersPrependCustomDictionary(m, &s->hashers_, &s->params, dict_size, dict);
748
+ if (BROTLI_IS_OOM(m)) return;
749
+ }
750
+
751
+ /* Marks all input as processed.
752
+ Returns true if position wrapping occurs. */
753
+ static BROTLI_BOOL UpdateLastProcessedPos(BrotliEncoderState* s) {
754
+ uint32_t wrapped_last_processed_pos = WrapPosition(s->last_processed_pos_);
755
+ uint32_t wrapped_input_pos = WrapPosition(s->input_pos_);
756
+ s->last_processed_pos_ = s->input_pos_;
757
+ return TO_BROTLI_BOOL(wrapped_input_pos < wrapped_last_processed_pos);
758
+ }
759
+
760
+ BROTLI_BOOL BrotliEncoderWriteData(
761
+ BrotliEncoderState* s, const BROTLI_BOOL is_last,
762
+ const BROTLI_BOOL force_flush, size_t* out_size, uint8_t** output) {
763
+ const uint64_t delta = UnprocessedInputSize(s);
764
+ const uint32_t bytes = (uint32_t)delta;
765
+ const uint32_t wrapped_last_processed_pos =
766
+ WrapPosition(s->last_processed_pos_);
767
+ uint8_t* data;
768
+ uint32_t mask;
769
+ MemoryManager* m = &s->memory_manager_;
770
+
771
+ if (!EnsureInitialized(s)) return BROTLI_FALSE;
772
+ data = s->ringbuffer_.buffer_;
773
+ mask = s->ringbuffer_.mask_;
774
+
775
+ /* Adding more blocks after "last" block is forbidden. */
776
+ if (s->is_last_block_emitted_) return BROTLI_FALSE;
777
+ if (is_last) s->is_last_block_emitted_ = BROTLI_TRUE;
778
+
779
+ if (delta > BrotliEncoderInputBlockSize(s)) {
780
+ return BROTLI_FALSE;
781
+ }
782
+ if (s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY &&
783
+ !s->command_buf_) {
784
+ s->command_buf_ =
785
+ BROTLI_ALLOC(m, uint32_t, kCompressFragmentTwoPassBlockSize);
786
+ s->literal_buf_ =
787
+ BROTLI_ALLOC(m, uint8_t, kCompressFragmentTwoPassBlockSize);
788
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
789
+ }
790
+
791
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
792
+ s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
793
+ uint8_t* storage;
794
+ size_t storage_ix = s->last_byte_bits_;
795
+ size_t table_size;
796
+ int* table;
797
+
798
+ if (delta == 0 && !is_last) {
799
+ /* We have no new input data and we don't have to finish the stream, so
800
+ nothing to do. */
801
+ *out_size = 0;
802
+ return BROTLI_TRUE;
803
+ }
804
+ storage = GetBrotliStorage(s, 2 * bytes + 500);
805
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
806
+ storage[0] = s->last_byte_;
807
+ table = GetHashTable(s, s->params.quality, bytes, &table_size);
808
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
809
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
810
+ BrotliCompressFragmentFast(
811
+ m, &data[wrapped_last_processed_pos & mask],
812
+ bytes, is_last,
813
+ table, table_size,
814
+ s->cmd_depths_, s->cmd_bits_,
815
+ &s->cmd_code_numbits_, s->cmd_code_,
816
+ &storage_ix, storage);
817
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
818
+ } else {
819
+ BrotliCompressFragmentTwoPass(
820
+ m, &data[wrapped_last_processed_pos & mask],
821
+ bytes, is_last,
822
+ s->command_buf_, s->literal_buf_,
823
+ table, table_size,
824
+ &storage_ix, storage);
825
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
826
+ }
827
+ s->last_byte_ = storage[storage_ix >> 3];
828
+ s->last_byte_bits_ = storage_ix & 7u;
829
+ UpdateLastProcessedPos(s);
830
+ *output = &storage[0];
831
+ *out_size = storage_ix >> 3;
832
+ return BROTLI_TRUE;
833
+ }
834
+
835
+ {
836
+ /* Theoretical max number of commands is 1 per 2 bytes. */
837
+ size_t newsize = s->num_commands_ + bytes / 2 + 1;
838
+ if (newsize > s->cmd_alloc_size_) {
839
+ Command* new_commands;
840
+ /* Reserve a bit more memory to allow merging with a next block
841
+ without realloc: that would impact speed. */
842
+ newsize += (bytes / 4) + 16;
843
+ s->cmd_alloc_size_ = newsize;
844
+ new_commands = BROTLI_ALLOC(m, Command, newsize);
845
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
846
+ if (s->commands_) {
847
+ memcpy(new_commands, s->commands_, sizeof(Command) * s->num_commands_);
848
+ BROTLI_FREE(m, s->commands_);
849
+ }
850
+ s->commands_ = new_commands;
851
+ }
852
+ }
853
+
854
+ BrotliCreateBackwardReferences(m, bytes, wrapped_last_processed_pos,
855
+ is_last, data, mask,
856
+ &s->params,
857
+ &s->hashers_,
858
+ s->dist_cache_,
859
+ &s->last_insert_len_,
860
+ &s->commands_[s->num_commands_],
861
+ &s->num_commands_,
862
+ &s->num_literals_);
863
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
864
+
865
+ {
866
+ const size_t max_length = MaxMetablockSize(&s->params);
867
+ const size_t max_literals = max_length / 8;
868
+ const size_t max_commands = max_length / 8;
869
+ const size_t processed_bytes = (size_t)(s->input_pos_ - s->last_flush_pos_);
870
+ /* If maximal possible additional block doesn't fit metablock, flush now. */
871
+ /* TODO: Postpone decision until next block arrives? */
872
+ const BROTLI_BOOL next_input_fits_metablock = TO_BROTLI_BOOL(
873
+ processed_bytes + BrotliEncoderInputBlockSize(s) <= max_length);
874
+ /* If block splitting is not used, then flush as soon as there is some
875
+ amount of commands / literals produced. */
876
+ const BROTLI_BOOL should_flush = TO_BROTLI_BOOL(
877
+ s->params.quality < MIN_QUALITY_FOR_BLOCK_SPLIT &&
878
+ s->num_literals_ + s->num_commands_ >= MAX_NUM_DELAYED_SYMBOLS);
879
+ if (!is_last && !force_flush && !should_flush &&
880
+ next_input_fits_metablock &&
881
+ s->num_literals_ < max_literals &&
882
+ s->num_commands_ < max_commands) {
883
+ /* Merge with next input block. Everything will happen later. */
884
+ if (UpdateLastProcessedPos(s)) {
885
+ HashersReset(&s->hashers_, ChooseHasher(&s->params));
886
+ }
887
+ *out_size = 0;
888
+ return BROTLI_TRUE;
889
+ }
890
+ }
891
+
892
+ /* Create the last insert-only command. */
893
+ if (s->last_insert_len_ > 0) {
894
+ InitInsertCommand(&s->commands_[s->num_commands_++], s->last_insert_len_);
895
+ s->num_literals_ += s->last_insert_len_;
896
+ s->last_insert_len_ = 0;
897
+ }
898
+
899
+ if (!is_last && s->input_pos_ == s->last_flush_pos_) {
900
+ /* We have no new input data and we don't have to finish the stream, so
901
+ nothing to do. */
902
+ *out_size = 0;
903
+ return BROTLI_TRUE;
904
+ }
905
+ assert(s->input_pos_ >= s->last_flush_pos_);
906
+ assert(s->input_pos_ > s->last_flush_pos_ || is_last);
907
+ assert(s->input_pos_ - s->last_flush_pos_ <= 1u << 24);
908
+ {
909
+ const uint32_t metablock_size =
910
+ (uint32_t)(s->input_pos_ - s->last_flush_pos_);
911
+ uint8_t* storage = GetBrotliStorage(s, 2 * metablock_size + 500);
912
+ size_t storage_ix = s->last_byte_bits_;
913
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
914
+ storage[0] = s->last_byte_;
915
+ WriteMetaBlockInternal(
916
+ m, data, mask, s->last_flush_pos_, metablock_size, is_last,
917
+ &s->params, s->prev_byte_, s->prev_byte2_,
918
+ s->num_literals_, s->num_commands_, s->commands_, s->saved_dist_cache_,
919
+ s->dist_cache_, &storage_ix, storage);
920
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
921
+ s->last_byte_ = storage[storage_ix >> 3];
922
+ s->last_byte_bits_ = storage_ix & 7u;
923
+ s->last_flush_pos_ = s->input_pos_;
924
+ if (UpdateLastProcessedPos(s)) {
925
+ HashersReset(&s->hashers_, ChooseHasher(&s->params));
926
+ }
927
+ if (s->last_flush_pos_ > 0) {
928
+ s->prev_byte_ = data[((uint32_t)s->last_flush_pos_ - 1) & mask];
929
+ }
930
+ if (s->last_flush_pos_ > 1) {
931
+ s->prev_byte2_ = data[(uint32_t)(s->last_flush_pos_ - 2) & mask];
932
+ }
933
+ s->num_commands_ = 0;
934
+ s->num_literals_ = 0;
935
+ /* Save the state of the distance cache in case we need to restore it for
936
+ emitting an uncompressed block. */
937
+ memcpy(s->saved_dist_cache_, s->dist_cache_, sizeof(s->dist_cache_));
938
+ *output = &storage[0];
939
+ *out_size = storage_ix >> 3;
940
+ return BROTLI_TRUE;
941
+ }
942
+ }
943
+
944
+ BROTLI_BOOL BrotliEncoderWriteMetaBlock(
945
+ BrotliEncoderState* s, const size_t input_size, const uint8_t* input_buffer,
946
+ const BROTLI_BOOL is_last, size_t* encoded_size, uint8_t* encoded_buffer) {
947
+ size_t out_size = 0;
948
+ uint8_t* output;
949
+ int result;
950
+ if (!EnsureInitialized(s)) return BROTLI_FALSE;
951
+ BrotliEncoderCopyInputToRingBuffer(s, input_size, input_buffer);
952
+ result = BrotliEncoderWriteData(
953
+ s, is_last, /* force_flush */ BROTLI_TRUE, &out_size, &output);
954
+ if (!result || out_size > *encoded_size) {
955
+ return BROTLI_FALSE;
956
+ }
957
+ if (out_size > 0) {
958
+ memcpy(encoded_buffer, output, out_size);
959
+ }
960
+ *encoded_size = out_size;
961
+ return BROTLI_TRUE;
962
+ }
963
+
964
+ BROTLI_BOOL BrotliEncoderWriteMetadata(
965
+ BrotliEncoderState* s, const size_t input_size, const uint8_t* input_buffer,
966
+ const BROTLI_BOOL is_last, size_t* encoded_size, uint8_t* encoded_buffer) {
967
+ uint64_t hdr_buffer_data[2];
968
+ uint8_t* hdr_buffer = (uint8_t*)&hdr_buffer_data[0];
969
+ size_t storage_ix;
970
+ if (!EnsureInitialized(s)) return BROTLI_FALSE;
971
+ if (input_size > (1 << 24) || input_size + 6 > *encoded_size) {
972
+ return BROTLI_FALSE;
973
+ }
974
+ storage_ix = s->last_byte_bits_;
975
+ hdr_buffer[0] = s->last_byte_;
976
+ BrotliWriteBits(1, 0, &storage_ix, hdr_buffer);
977
+ BrotliWriteBits(2, 3, &storage_ix, hdr_buffer);
978
+ BrotliWriteBits(1, 0, &storage_ix, hdr_buffer);
979
+ if (input_size == 0) {
980
+ BrotliWriteBits(2, 0, &storage_ix, hdr_buffer);
981
+ *encoded_size = (storage_ix + 7u) >> 3;
982
+ memcpy(encoded_buffer, hdr_buffer, *encoded_size);
983
+ } else {
984
+ uint32_t nbits = (input_size == 1) ? 0 :
985
+ (Log2FloorNonZero((uint32_t)input_size - 1) + 1);
986
+ uint32_t nbytes = (nbits + 7) / 8;
987
+ size_t hdr_size;
988
+ BrotliWriteBits(2, nbytes, &storage_ix, hdr_buffer);
989
+ BrotliWriteBits(8 * nbytes, input_size - 1, &storage_ix, hdr_buffer);
990
+ hdr_size = (storage_ix + 7u) >> 3;
991
+ memcpy(encoded_buffer, hdr_buffer, hdr_size);
992
+ memcpy(&encoded_buffer[hdr_size], input_buffer, input_size);
993
+ *encoded_size = hdr_size + input_size;
994
+ }
995
+ if (is_last) {
996
+ encoded_buffer[(*encoded_size)++] = 3;
997
+ }
998
+ s->last_byte_ = 0;
999
+ s->last_byte_bits_ = 0;
1000
+ return BROTLI_TRUE;
1001
+ }
1002
+
1003
+ BROTLI_BOOL BrotliEncoderFinishStream(
1004
+ BrotliEncoderState* s, size_t* encoded_size, uint8_t* encoded_buffer) {
1005
+ if (!EnsureInitialized(s)) return BROTLI_FALSE;
1006
+ return BrotliEncoderWriteMetaBlock(
1007
+ s, 0, NULL, 1, encoded_size, encoded_buffer);
1008
+ }
1009
+
1010
+ static BROTLI_BOOL BrotliCompressBufferQuality10(
1011
+ int lgwin, size_t input_size, const uint8_t* input_buffer,
1012
+ size_t* encoded_size, uint8_t* encoded_buffer) {
1013
+ MemoryManager memory_manager;
1014
+ MemoryManager* m = &memory_manager;
1015
+
1016
+ const size_t mask = BROTLI_SIZE_MAX >> 1;
1017
+ const size_t max_backward_limit = MaxBackwardLimit(lgwin);
1018
+ int dist_cache[4] = { 4, 11, 15, 16 };
1019
+ int saved_dist_cache[4] = { 4, 11, 15, 16 };
1020
+ BROTLI_BOOL ok = BROTLI_TRUE;
1021
+ const size_t max_out_size = *encoded_size;
1022
+ size_t total_out_size = 0;
1023
+ uint8_t last_byte;
1024
+ uint8_t last_byte_bits;
1025
+ H10* hasher;
1026
+
1027
+ const size_t hasher_eff_size =
1028
+ BROTLI_MIN(size_t, input_size, max_backward_limit + 16);
1029
+
1030
+ BrotliEncoderParams params;
1031
+
1032
+ const int lgmetablock = BROTLI_MIN(int, 24, lgwin + 1);
1033
+ size_t max_block_size;
1034
+ const size_t max_metablock_size = (size_t)1 << lgmetablock;
1035
+ const size_t max_literals_per_metablock = max_metablock_size / 8;
1036
+ const size_t max_commands_per_metablock = max_metablock_size / 8;
1037
+ size_t metablock_start = 0;
1038
+ uint8_t prev_byte = 0;
1039
+ uint8_t prev_byte2 = 0;
1040
+
1041
+ params.mode = BROTLI_DEFAULT_MODE;
1042
+ params.quality = 10;
1043
+ params.lgwin = lgwin;
1044
+ params.lgblock = 0;
1045
+ SanitizeParams(&params);
1046
+ params.lgblock = ComputeLgBlock(&params);
1047
+ max_block_size = (size_t)1 << params.lgblock;
1048
+
1049
+ BrotliInitMemoryManager(m, 0, 0, 0);
1050
+
1051
+ assert(input_size <= mask + 1);
1052
+ EncodeWindowBits(lgwin, &last_byte, &last_byte_bits);
1053
+ hasher = BROTLI_ALLOC(m, H10, 1);
1054
+ if (BROTLI_IS_OOM(m)) goto oom;
1055
+ InitializeH10(hasher);
1056
+ InitH10(m, hasher, input_buffer, &params, 0, hasher_eff_size, 1);
1057
+ if (BROTLI_IS_OOM(m)) goto oom;
1058
+
1059
+ while (ok && metablock_start < input_size) {
1060
+ const size_t metablock_end =
1061
+ BROTLI_MIN(size_t, input_size, metablock_start + max_metablock_size);
1062
+ const size_t expected_num_commands =
1063
+ (metablock_end - metablock_start) / 12 + 16;
1064
+ Command* commands = 0;
1065
+ size_t num_commands = 0;
1066
+ size_t last_insert_len = 0;
1067
+ size_t num_literals = 0;
1068
+ size_t metablock_size = 0;
1069
+ size_t cmd_alloc_size = 0;
1070
+ BROTLI_BOOL is_last;
1071
+ uint8_t* storage;
1072
+ size_t storage_ix;
1073
+
1074
+ size_t block_start;
1075
+ for (block_start = metablock_start; block_start < metablock_end; ) {
1076
+ size_t block_size =
1077
+ BROTLI_MIN(size_t, metablock_end - block_start, max_block_size);
1078
+ ZopfliNode* nodes = BROTLI_ALLOC(m, ZopfliNode, block_size + 1);
1079
+ size_t path_size;
1080
+ size_t new_cmd_alloc_size;
1081
+ if (BROTLI_IS_OOM(m)) goto oom;
1082
+ BrotliInitZopfliNodes(nodes, block_size + 1);
1083
+ StitchToPreviousBlockH10(hasher, block_size, block_start,
1084
+ input_buffer, mask);
1085
+ path_size = BrotliZopfliComputeShortestPath(
1086
+ m, block_size, block_start, input_buffer, mask, &params,
1087
+ max_backward_limit, dist_cache, hasher, nodes);
1088
+ if (BROTLI_IS_OOM(m)) goto oom;
1089
+ /* We allocate a command buffer in the first iteration of this loop that
1090
+ will be likely big enough for the whole metablock, so that for most
1091
+ inputs we will not have to reallocate in later iterations. We do the
1092
+ allocation here and not before the loop, because if the input is small,
1093
+ this will be allocated after the zopfli cost model is freed, so this
1094
+ will not increase peak memory usage.
1095
+ TODO: If the first allocation is too small, increase command
1096
+ buffer size exponentially. */
1097
+ new_cmd_alloc_size = BROTLI_MAX(size_t, expected_num_commands,
1098
+ num_commands + path_size + 1);
1099
+ if (cmd_alloc_size != new_cmd_alloc_size) {
1100
+ Command* new_commands = BROTLI_ALLOC(m, Command, new_cmd_alloc_size);
1101
+ if (BROTLI_IS_OOM(m)) goto oom;
1102
+ cmd_alloc_size = new_cmd_alloc_size;
1103
+ if (commands) {
1104
+ memcpy(new_commands, commands, sizeof(Command) * num_commands);
1105
+ BROTLI_FREE(m, commands);
1106
+ }
1107
+ commands = new_commands;
1108
+ }
1109
+ BrotliZopfliCreateCommands(block_size, block_start, max_backward_limit,
1110
+ &nodes[0], dist_cache, &last_insert_len,
1111
+ &commands[num_commands], &num_literals);
1112
+ num_commands += path_size;
1113
+ block_start += block_size;
1114
+ metablock_size += block_size;
1115
+ BROTLI_FREE(m, nodes);
1116
+ if (num_literals > max_literals_per_metablock ||
1117
+ num_commands > max_commands_per_metablock) {
1118
+ break;
1119
+ }
1120
+ }
1121
+
1122
+ if (last_insert_len > 0) {
1123
+ InitInsertCommand(&commands[num_commands++], last_insert_len);
1124
+ num_literals += last_insert_len;
1125
+ }
1126
+
1127
+ is_last = TO_BROTLI_BOOL(metablock_start + metablock_size == input_size);
1128
+ storage = NULL;
1129
+ storage_ix = last_byte_bits;
1130
+
1131
+ if (metablock_size == 0) {
1132
+ /* Write the ISLAST and ISEMPTY bits. */
1133
+ storage = BROTLI_ALLOC(m, uint8_t, 16);
1134
+ if (BROTLI_IS_OOM(m)) goto oom;
1135
+ storage[0] = last_byte;
1136
+ BrotliWriteBits(2, 3, &storage_ix, storage);
1137
+ storage_ix = (storage_ix + 7u) & ~7u;
1138
+ } else if (!ShouldCompress(input_buffer, mask, metablock_start,
1139
+ metablock_size, num_literals, num_commands)) {
1140
+ /* Restore the distance cache, as its last update by
1141
+ CreateBackwardReferences is now unused. */
1142
+ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0]));
1143
+ storage = BROTLI_ALLOC(m, uint8_t, metablock_size + 16);
1144
+ if (BROTLI_IS_OOM(m)) goto oom;
1145
+ storage[0] = last_byte;
1146
+ BrotliStoreUncompressedMetaBlock(is_last, input_buffer,
1147
+ metablock_start, mask, metablock_size,
1148
+ &storage_ix, storage);
1149
+ } else {
1150
+ uint32_t num_direct_distance_codes = 0;
1151
+ uint32_t distance_postfix_bits = 0;
1152
+ ContextType literal_context_mode = CONTEXT_UTF8;
1153
+ MetaBlockSplit mb;
1154
+ InitMetaBlockSplit(&mb);
1155
+ if (!BrotliIsMostlyUTF8(input_buffer, metablock_start, mask,
1156
+ metablock_size, kMinUTF8Ratio)) {
1157
+ literal_context_mode = CONTEXT_SIGNED;
1158
+ }
1159
+ BrotliBuildMetaBlock(m, input_buffer, metablock_start, mask, &params,
1160
+ prev_byte, prev_byte2,
1161
+ commands, num_commands,
1162
+ literal_context_mode,
1163
+ &mb);
1164
+ if (BROTLI_IS_OOM(m)) goto oom;
1165
+ BrotliOptimizeHistograms(num_direct_distance_codes,
1166
+ distance_postfix_bits,
1167
+ &mb);
1168
+ storage = BROTLI_ALLOC(m, uint8_t, 2 * metablock_size + 500);
1169
+ if (BROTLI_IS_OOM(m)) goto oom;
1170
+ storage[0] = last_byte;
1171
+ BrotliStoreMetaBlock(m, input_buffer, metablock_start, metablock_size,
1172
+ mask, prev_byte, prev_byte2,
1173
+ is_last,
1174
+ num_direct_distance_codes,
1175
+ distance_postfix_bits,
1176
+ literal_context_mode,
1177
+ commands, num_commands,
1178
+ &mb,
1179
+ &storage_ix, storage);
1180
+ if (BROTLI_IS_OOM(m)) goto oom;
1181
+ if (metablock_size + 4 < (storage_ix >> 3)) {
1182
+ /* Restore the distance cache and last byte. */
1183
+ memcpy(dist_cache, saved_dist_cache, 4 * sizeof(dist_cache[0]));
1184
+ storage[0] = last_byte;
1185
+ storage_ix = last_byte_bits;
1186
+ BrotliStoreUncompressedMetaBlock(is_last, input_buffer,
1187
+ metablock_start, mask,
1188
+ metablock_size, &storage_ix, storage);
1189
+ }
1190
+ DestroyMetaBlockSplit(m, &mb);
1191
+ }
1192
+ last_byte = storage[storage_ix >> 3];
1193
+ last_byte_bits = storage_ix & 7u;
1194
+ metablock_start += metablock_size;
1195
+ prev_byte = input_buffer[metablock_start - 1];
1196
+ prev_byte2 = input_buffer[metablock_start - 2];
1197
+ /* Save the state of the distance cache in case we need to restore it for
1198
+ emitting an uncompressed block. */
1199
+ memcpy(saved_dist_cache, dist_cache, 4 * sizeof(dist_cache[0]));
1200
+
1201
+ {
1202
+ const size_t out_size = storage_ix >> 3;
1203
+ total_out_size += out_size;
1204
+ if (total_out_size <= max_out_size) {
1205
+ memcpy(encoded_buffer, storage, out_size);
1206
+ encoded_buffer += out_size;
1207
+ } else {
1208
+ ok = BROTLI_FALSE;
1209
+ }
1210
+ }
1211
+ BROTLI_FREE(m, storage);
1212
+ BROTLI_FREE(m, commands);
1213
+ }
1214
+
1215
+ *encoded_size = total_out_size;
1216
+ CleanupH10(m, hasher);
1217
+ BROTLI_FREE(m, hasher);
1218
+ return ok;
1219
+
1220
+ oom:
1221
+ BrotliWipeOutMemoryManager(m);
1222
+ return BROTLI_FALSE;
1223
+ }
1224
+
1225
+ size_t BrotliEncoderMaxCompressedSize(size_t input_size) {
1226
+ /* [window bits / empty metadata] + N * [uncompressed] + [last empty] */
1227
+ size_t num_large_blocks = input_size >> 24;
1228
+ size_t tail = input_size - (num_large_blocks << 24);
1229
+ size_t tail_overhead = (tail > (1 << 20)) ? 4 : 3;
1230
+ size_t overhead = 2 + (4 * num_large_blocks) + tail_overhead + 1;
1231
+ size_t result = input_size + overhead;
1232
+ if (input_size == 0) return 1;
1233
+ return (result < input_size) ? 0 : result;
1234
+ }
1235
+
1236
+ /* Wraps data to uncompressed brotli stream with minimal window size.
1237
+ |output| should point at region with at least BrotliEncoderMaxCompressedSize
1238
+ addressable bytes.
1239
+ Returns the length of stream. */
1240
+ static size_t MakeUncompressedStream(
1241
+ const uint8_t* input, size_t input_size, uint8_t* output) {
1242
+ size_t size = input_size;
1243
+ size_t result = 0;
1244
+ size_t offset = 0;
1245
+ if (input_size == 0) {
1246
+ output[0] = 6;
1247
+ return 1;
1248
+ }
1249
+ output[result++] = 0x21; /* window bits = 10, is_last = false */
1250
+ output[result++] = 0x03; /* empty metadata, padding */
1251
+ while (size > 0) {
1252
+ uint32_t nibbles = 0;
1253
+ uint32_t chunk_size;
1254
+ uint32_t bits;
1255
+ chunk_size = (size > (1u << 24)) ? (1u << 24) : (uint32_t)size;
1256
+ if (chunk_size > (1u << 16)) nibbles = (chunk_size > (1u << 20)) ? 2 : 1;
1257
+ bits =
1258
+ (nibbles << 1) | ((chunk_size - 1) << 3) | (1u << (19 + 4 * nibbles));
1259
+ output[result++] = (uint8_t)bits;
1260
+ output[result++] = (uint8_t)(bits >> 8);
1261
+ output[result++] = (uint8_t)(bits >> 16);
1262
+ if (nibbles == 2) output[result++] = (uint8_t)(bits >> 24);
1263
+ memcpy(&output[result], &input[offset], chunk_size);
1264
+ result += chunk_size;
1265
+ offset += chunk_size;
1266
+ size -= chunk_size;
1267
+ }
1268
+ output[result++] = 3;
1269
+ return result;
1270
+ }
1271
+
1272
+ BROTLI_BOOL BrotliEncoderCompress(
1273
+ int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
1274
+ const uint8_t* input_buffer, size_t* encoded_size,
1275
+ uint8_t* encoded_buffer) {
1276
+ BrotliEncoderState* s;
1277
+ size_t out_size = *encoded_size;
1278
+ const uint8_t* input_start = input_buffer;
1279
+ uint8_t* output_start = encoded_buffer;
1280
+ size_t max_out_size = BrotliEncoderMaxCompressedSize(input_size);
1281
+ if (out_size == 0) {
1282
+ /* Output buffer needs at least one byte. */
1283
+ return BROTLI_FALSE;
1284
+ }
1285
+ if (input_size == 0) {
1286
+ /* Handle the special case of empty input. */
1287
+ *encoded_size = 1;
1288
+ *encoded_buffer = 6;
1289
+ return BROTLI_TRUE;
1290
+ }
1291
+ if (quality == 10) {
1292
+ /* TODO: Implement this direct path for all quality levels. */
1293
+ const int lg_win = BROTLI_MIN(int, 24, BROTLI_MAX(int, 16, lgwin));
1294
+ int ok = BrotliCompressBufferQuality10(lg_win, input_size, input_buffer,
1295
+ encoded_size, encoded_buffer);
1296
+ if (!ok || (max_out_size && *encoded_size > max_out_size)) {
1297
+ goto fallback;
1298
+ }
1299
+ return BROTLI_TRUE;
1300
+ }
1301
+
1302
+ s = BrotliEncoderCreateInstance(0, 0, 0);
1303
+ if (!s) {
1304
+ return BROTLI_FALSE;
1305
+ } else {
1306
+ size_t available_in = input_size;
1307
+ const uint8_t* next_in = input_buffer;
1308
+ size_t available_out = *encoded_size;
1309
+ uint8_t* next_out = encoded_buffer;
1310
+ size_t total_out = 0;
1311
+ BROTLI_BOOL result = BROTLI_FALSE;
1312
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, (uint32_t)quality);
1313
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, (uint32_t)lgwin);
1314
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, (uint32_t)mode);
1315
+ result = BrotliEncoderCompressStream(s, BROTLI_OPERATION_FINISH,
1316
+ &available_in, &next_in, &available_out, &next_out, &total_out);
1317
+ if (!BrotliEncoderIsFinished(s)) result = 0;
1318
+ *encoded_size = total_out;
1319
+ BrotliEncoderDestroyInstance(s);
1320
+ if (!result || (max_out_size && *encoded_size > max_out_size)) {
1321
+ goto fallback;
1322
+ }
1323
+ return BROTLI_TRUE;
1324
+ }
1325
+ fallback:
1326
+ *encoded_size = 0;
1327
+ if (!max_out_size) return BROTLI_FALSE;
1328
+ if (out_size >= max_out_size) {
1329
+ *encoded_size =
1330
+ MakeUncompressedStream(input_start, input_size, output_start);
1331
+ return BROTLI_TRUE;
1332
+ }
1333
+ return BROTLI_FALSE;
1334
+ }
1335
+
1336
+ static void InjectBytePaddingBlock(BrotliEncoderState* s) {
1337
+ uint32_t seal = s->last_byte_;
1338
+ size_t seal_bits = s->last_byte_bits_;
1339
+ s->last_byte_ = 0;
1340
+ s->last_byte_bits_ = 0;
1341
+ /* is_last = 0, data_nibbles = 11, reseved = 0, meta_nibbles = 00 */
1342
+ seal |= 0x6u << seal_bits;
1343
+ seal_bits += 6;
1344
+ s->flush_buf_[0] = (uint8_t)seal;
1345
+ if (seal_bits > 8) s->flush_buf_[1] = (uint8_t)(seal >> 8);
1346
+ s->next_out_ = s->flush_buf_;
1347
+ s->available_out_ = (seal_bits + 7) >> 3;
1348
+ }
1349
+
1350
+ static BROTLI_BOOL BrotliEncoderCompressStreamFast(
1351
+ BrotliEncoderState* s, BrotliEncoderOperation op, size_t* available_in,
1352
+ const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
1353
+ size_t* total_out) {
1354
+ const size_t block_size_limit = (size_t)1 << s->params.lgwin;
1355
+ const size_t buf_size = BROTLI_MIN(size_t, kCompressFragmentTwoPassBlockSize,
1356
+ BROTLI_MIN(size_t, *available_in, block_size_limit));
1357
+ uint32_t* tmp_command_buf = NULL;
1358
+ uint32_t* command_buf = NULL;
1359
+ uint8_t* tmp_literal_buf = NULL;
1360
+ uint8_t* literal_buf = NULL;
1361
+ MemoryManager* m = &s->memory_manager_;
1362
+ if (s->params.quality != FAST_ONE_PASS_COMPRESSION_QUALITY &&
1363
+ s->params.quality != FAST_TWO_PASS_COMPRESSION_QUALITY) {
1364
+ return BROTLI_FALSE;
1365
+ }
1366
+ if (s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
1367
+ if (!s->command_buf_ && buf_size == kCompressFragmentTwoPassBlockSize) {
1368
+ s->command_buf_ =
1369
+ BROTLI_ALLOC(m, uint32_t, kCompressFragmentTwoPassBlockSize);
1370
+ s->literal_buf_ =
1371
+ BROTLI_ALLOC(m, uint8_t, kCompressFragmentTwoPassBlockSize);
1372
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1373
+ }
1374
+ if (s->command_buf_) {
1375
+ command_buf = s->command_buf_;
1376
+ literal_buf = s->literal_buf_;
1377
+ } else {
1378
+ tmp_command_buf = BROTLI_ALLOC(m, uint32_t, buf_size);
1379
+ tmp_literal_buf = BROTLI_ALLOC(m, uint8_t, buf_size);
1380
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1381
+ command_buf = tmp_command_buf;
1382
+ literal_buf = tmp_literal_buf;
1383
+ }
1384
+ }
1385
+
1386
+ while (BROTLI_TRUE) {
1387
+ if (s->available_out_ == 0 &&
1388
+ s->stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED) {
1389
+ s->stream_state_ = BROTLI_STREAM_PROCESSING;
1390
+ if (s->last_byte_bits_ == 0) break;
1391
+ InjectBytePaddingBlock(s);
1392
+ continue;
1393
+ }
1394
+
1395
+ if (s->available_out_ != 0 && *available_out != 0) {
1396
+ size_t copy_output_size =
1397
+ BROTLI_MIN(size_t, s->available_out_, *available_out);
1398
+ memcpy(*next_out, s->next_out_, copy_output_size);
1399
+ *next_out += copy_output_size;
1400
+ *available_out -= copy_output_size;
1401
+ s->next_out_ += copy_output_size;
1402
+ s->available_out_ -= copy_output_size;
1403
+ s->total_out_ += copy_output_size;
1404
+ if (total_out) *total_out = s->total_out_;
1405
+ continue;
1406
+ }
1407
+
1408
+ /* Compress block only when internal output buffer is empty, stream is not
1409
+ finished, there is no pending flush request, and there is either
1410
+ additional input or pending operation. */
1411
+ if (s->available_out_ == 0 &&
1412
+ s->stream_state_ == BROTLI_STREAM_PROCESSING &&
1413
+ (*available_in != 0 || op != BROTLI_OPERATION_PROCESS)) {
1414
+ size_t block_size = BROTLI_MIN(size_t, block_size_limit, *available_in);
1415
+ BROTLI_BOOL is_last =
1416
+ (*available_in == block_size) && (op == BROTLI_OPERATION_FINISH);
1417
+ BROTLI_BOOL force_flush =
1418
+ (*available_in == block_size) && (op == BROTLI_OPERATION_FLUSH);
1419
+ size_t max_out_size = 2 * block_size + 500;
1420
+ BROTLI_BOOL inplace = BROTLI_TRUE;
1421
+ uint8_t* storage = NULL;
1422
+ size_t storage_ix = s->last_byte_bits_;
1423
+ size_t table_size;
1424
+ int* table;
1425
+
1426
+ if (force_flush && block_size == 0) {
1427
+ s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED;
1428
+ continue;
1429
+ }
1430
+ if (max_out_size <= *available_out) {
1431
+ storage = *next_out;
1432
+ } else {
1433
+ inplace = 0;
1434
+ storage = GetBrotliStorage(s, max_out_size);
1435
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1436
+ }
1437
+ storage[0] = s->last_byte_;
1438
+ table = GetHashTable(s, s->params.quality, block_size, &table_size);
1439
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1440
+
1441
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY) {
1442
+ BrotliCompressFragmentFast(m, *next_in, block_size, is_last, table,
1443
+ table_size, s->cmd_depths_, s->cmd_bits_, &s->cmd_code_numbits_,
1444
+ s->cmd_code_, &storage_ix, storage);
1445
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1446
+ } else {
1447
+ BrotliCompressFragmentTwoPass(m, *next_in, block_size, is_last,
1448
+ command_buf, literal_buf, table, table_size,
1449
+ &storage_ix, storage);
1450
+ if (BROTLI_IS_OOM(m)) return BROTLI_FALSE;
1451
+ }
1452
+ *next_in += block_size;
1453
+ *available_in -= block_size;
1454
+ if (inplace) {
1455
+ size_t out_bytes = storage_ix >> 3;
1456
+ assert(out_bytes <= *available_out);
1457
+ assert((storage_ix & 7) == 0 || out_bytes < *available_out);
1458
+ *next_out += out_bytes;
1459
+ *available_out -= out_bytes;
1460
+ s->total_out_ += out_bytes;
1461
+ if (total_out) *total_out = s->total_out_;
1462
+ } else {
1463
+ size_t out_bytes = storage_ix >> 3;
1464
+ s->next_out_ = storage;
1465
+ s->available_out_ = out_bytes;
1466
+ }
1467
+ s->last_byte_ = storage[storage_ix >> 3];
1468
+ s->last_byte_bits_ = storage_ix & 7u;
1469
+
1470
+ if (force_flush) s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED;
1471
+ if (is_last) s->stream_state_ = BROTLI_STREAM_FINISHED;
1472
+ continue;
1473
+ }
1474
+ break;
1475
+ }
1476
+ BROTLI_FREE(m, tmp_command_buf);
1477
+ BROTLI_FREE(m, tmp_literal_buf);
1478
+ return BROTLI_TRUE;
1479
+ }
1480
+
1481
+ BROTLI_BOOL BrotliEncoderCompressStream(
1482
+ BrotliEncoderState* s, BrotliEncoderOperation op, size_t* available_in,
1483
+ const uint8_t** next_in, size_t* available_out,uint8_t** next_out,
1484
+ size_t* total_out) {
1485
+ if (!EnsureInitialized(s)) return BROTLI_FALSE;
1486
+
1487
+ if (s->stream_state_ != BROTLI_STREAM_PROCESSING && *available_in != 0) {
1488
+ return BROTLI_FALSE;
1489
+ }
1490
+ if (s->params.quality == FAST_ONE_PASS_COMPRESSION_QUALITY ||
1491
+ s->params.quality == FAST_TWO_PASS_COMPRESSION_QUALITY) {
1492
+ return BrotliEncoderCompressStreamFast(s, op, available_in, next_in,
1493
+ available_out, next_out, total_out);
1494
+ }
1495
+ while (BROTLI_TRUE) {
1496
+ size_t remaining_block_size = RemainingInputBlockSize(s);
1497
+
1498
+ if (remaining_block_size != 0 && *available_in != 0) {
1499
+ size_t copy_input_size =
1500
+ BROTLI_MIN(size_t, remaining_block_size, *available_in);
1501
+ BrotliEncoderCopyInputToRingBuffer(s, copy_input_size, *next_in);
1502
+ *next_in += copy_input_size;
1503
+ *available_in -= copy_input_size;
1504
+ continue;
1505
+ }
1506
+
1507
+ if (s->available_out_ == 0 &&
1508
+ s->stream_state_ == BROTLI_STREAM_FLUSH_REQUESTED) {
1509
+ s->stream_state_ = BROTLI_STREAM_PROCESSING;
1510
+ if (s->last_byte_bits_ == 0) break;
1511
+ InjectBytePaddingBlock(s);
1512
+ continue;
1513
+ }
1514
+
1515
+ if (s->available_out_ != 0 && *available_out != 0) {
1516
+ size_t copy_output_size =
1517
+ BROTLI_MIN(size_t, s->available_out_, *available_out);
1518
+ memcpy(*next_out, s->next_out_, copy_output_size);
1519
+ *next_out += copy_output_size;
1520
+ *available_out -= copy_output_size;
1521
+ s->next_out_ += copy_output_size;
1522
+ s->available_out_ -= copy_output_size;
1523
+ s->total_out_ += copy_output_size;
1524
+ if (total_out) *total_out = s->total_out_;
1525
+ continue;
1526
+ }
1527
+
1528
+ /* Compress data only when internal outpuf buffer is empty, stream is not
1529
+ finished and there is no pending flush request. */
1530
+ if (s->available_out_ == 0 &&
1531
+ s->stream_state_ == BROTLI_STREAM_PROCESSING) {
1532
+ if (remaining_block_size == 0 || op != BROTLI_OPERATION_PROCESS) {
1533
+ BROTLI_BOOL is_last = TO_BROTLI_BOOL(
1534
+ (*available_in == 0) && op == BROTLI_OPERATION_FINISH);
1535
+ BROTLI_BOOL force_flush = TO_BROTLI_BOOL(
1536
+ (*available_in == 0) && op == BROTLI_OPERATION_FLUSH);
1537
+ BROTLI_BOOL result = BrotliEncoderWriteData(s, is_last, force_flush,
1538
+ &s->available_out_, &s->next_out_);
1539
+ if (!result) return BROTLI_FALSE;
1540
+ if (force_flush) s->stream_state_ = BROTLI_STREAM_FLUSH_REQUESTED;
1541
+ if (is_last) s->stream_state_ = BROTLI_STREAM_FINISHED;
1542
+ continue;
1543
+ }
1544
+ }
1545
+ break;
1546
+ }
1547
+ return BROTLI_TRUE;
1548
+ }
1549
+
1550
+ BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* s) {
1551
+ return TO_BROTLI_BOOL(s->stream_state_ == BROTLI_STREAM_FINISHED &&
1552
+ !BrotliEncoderHasMoreOutput(s));
1553
+ }
1554
+
1555
+ BROTLI_BOOL BrotliEncoderHasMoreOutput(BrotliEncoderState* s) {
1556
+ return TO_BROTLI_BOOL(s->available_out_ != 0);
1557
+ }
1558
+
1559
+
1560
+ #if defined(__cplusplus) || defined(c_plusplus)
1561
+ } /* extern "C" */
1562
+ #endif