brotli 0.1.8 → 0.2.0

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 +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
@@ -1,221 +0,0 @@
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
- /* API for Brotli compression. */
8
-
9
- #ifndef BROTLI_ENC_ENCODE_H_
10
- #define BROTLI_ENC_ENCODE_H_
11
-
12
- #include "../common/types.h"
13
-
14
- #if defined(__cplusplus) || defined(c_plusplus)
15
- extern "C" {
16
- #endif
17
-
18
- static const int kBrotliMaxWindowBits = 24;
19
- static const int kBrotliMinWindowBits = 10;
20
- static const int kBrotliMinInputBlockBits = 16;
21
- static const int kBrotliMaxInputBlockBits = 24;
22
-
23
- #define BROTLI_MIN_QUALITY 0
24
- #define BROTLI_MAX_QUALITY 11
25
-
26
- typedef enum BrotliEncoderMode {
27
- /* Default compression mode. The compressor does not know anything in
28
- advance about the properties of the input. */
29
- BROTLI_MODE_GENERIC = 0,
30
- /* Compression mode for UTF-8 format text input. */
31
- BROTLI_MODE_TEXT = 1,
32
- /* Compression mode used in WOFF 2.0. */
33
- BROTLI_MODE_FONT = 2
34
- } BrotliEncoderMode;
35
-
36
- #define BROTLI_DEFAULT_QUALITY 11
37
- #define BROTLI_DEFAULT_WINDOW 22
38
- #define BROTLI_DEFAULT_MODE BROTLI_MODE_GENERIC
39
-
40
- typedef enum BrotliEncoderOperation {
41
- BROTLI_OPERATION_PROCESS = 0,
42
- /* Request output stream to flush. Performed when input stream is depleted
43
- and there is enough space in output stream. */
44
- BROTLI_OPERATION_FLUSH = 1,
45
- /* Request output stream to finish. Performed when input stream is depleted
46
- and there is enough space in output stream. */
47
- BROTLI_OPERATION_FINISH = 2
48
- } BrotliEncoderOperation;
49
-
50
- typedef enum BrotliEncoderParameter {
51
- BROTLI_PARAM_MODE = 0,
52
- /* Controls the compression-speed vs compression-density tradeoffs. The higher
53
- the quality, the slower the compression. Range is 0 to 11. */
54
- BROTLI_PARAM_QUALITY = 1,
55
- /* Base 2 logarithm of the sliding window size. Range is 10 to 24. */
56
- BROTLI_PARAM_LGWIN = 2,
57
- /* Base 2 logarithm of the maximum input block size. Range is 16 to 24.
58
- If set to 0, the value will be set based on the quality. */
59
- BROTLI_PARAM_LGBLOCK = 3
60
- } BrotliEncoderParameter;
61
-
62
- /* A state can not be reused for multiple brotli streams. */
63
- typedef struct BrotliEncoderStateStruct BrotliEncoderState;
64
-
65
- BROTLI_BOOL BrotliEncoderSetParameter(
66
- BrotliEncoderState* state, BrotliEncoderParameter p, uint32_t value);
67
-
68
- /* Creates the instance of BrotliEncoderState and initializes it.
69
- |alloc_func| and |free_func| MUST be both zero or both non-zero. In the case
70
- they are both zero, default memory allocators are used. |opaque| is passed to
71
- |alloc_func| and |free_func| when they are called. */
72
- BrotliEncoderState* BrotliEncoderCreateInstance(brotli_alloc_func alloc_func,
73
- brotli_free_func free_func,
74
- void* opaque);
75
-
76
- /* Deinitializes and frees BrotliEncoderState instance. */
77
- void BrotliEncoderDestroyInstance(BrotliEncoderState* state);
78
- /* The maximum input size that can be processed at once. */
79
- size_t BrotliEncoderInputBlockSize(BrotliEncoderState* state);
80
-
81
- /* Encodes the data in |input_buffer| as a meta-block and writes it to
82
- |encoded_buffer| (|*encoded_size should| be set to the size of
83
- |encoded_buffer|) and sets |*encoded_size| to the number of bytes that
84
- was written. The |input_size| must not be greater than input_block_size().
85
- Returns false if there was an error and true otherwise. */
86
- BROTLI_BOOL BrotliEncoderWriteMetaBlock(
87
- BrotliEncoderState* state, const size_t input_size,
88
- const uint8_t* input_buffer, const BROTLI_BOOL is_last,
89
- size_t* encoded_size, uint8_t* encoded_buffer);
90
-
91
- /* Writes a metadata meta-block containing the given input to encoded_buffer.
92
- |*encoded_size| should be set to the size of the encoded_buffer.
93
- Sets |*encoded_size| to the number of bytes that was written.
94
- Note that the given input data will not be part of the sliding window and
95
- thus no backward references can be made to this data from subsequent
96
- metablocks. |input_size| must not be greater than 2^24 and provided
97
- |*encoded_size| must not be less than |input_size| + 6.
98
- Returns false if there was an error and true otherwise. */
99
- BROTLI_BOOL BrotliEncoderWriteMetadata(
100
- BrotliEncoderState* state, const size_t input_size,
101
- const uint8_t* input_buffer, const BROTLI_BOOL is_last,
102
- size_t* encoded_size, uint8_t* encoded_buffer);
103
-
104
- /* Writes a zero-length meta-block with end-of-input bit set to the
105
- internal output buffer and copies the output buffer to |encoded_buffer|
106
- (|*encoded_size| should be set to the size of |encoded_buffer|) and sets
107
- |*encoded_size| to the number of bytes written.
108
- Returns false if there was an error and true otherwise. */
109
- BROTLI_BOOL BrotliEncoderFinishStream(
110
- BrotliEncoderState* state, size_t* encoded_size, uint8_t* encoded_buffer);
111
-
112
- /* Copies the given input data to the internal ring buffer of the compressor.
113
- No processing of the data occurs at this time and this function can be
114
- called multiple times before calling WriteBrotliData() to process the
115
- accumulated input. At most input_block_size() bytes of input data can be
116
- copied to the ring buffer, otherwise the next WriteBrotliData() will fail.
117
- */
118
- void BrotliEncoderCopyInputToRingBuffer(BrotliEncoderState* state,
119
- const size_t input_size,
120
- const uint8_t* input_buffer);
121
-
122
- /* Processes the accumulated input data and sets |*out_size| to the length of
123
- the new output meta-block, or to zero if no new output meta-block has been
124
- created (in this case the processed input data is buffered internally).
125
- If |*out_size| is positive, |*output| points to the start of the output
126
- data. If |is_last| or |force_flush| is 1, an output meta-block is always
127
- created. However, until |is_last| is 1 encoder may retain up to 7 bits
128
- of the last byte of output. To force encoder to dump the remaining bits
129
- use WriteMetadata() to append an empty meta-data block.
130
- Returns false if the size of the input data is larger than
131
- input_block_size(). */
132
- BROTLI_BOOL BrotliEncoderWriteData(
133
- BrotliEncoderState* state, const BROTLI_BOOL is_last,
134
- const BROTLI_BOOL force_flush, size_t* out_size, uint8_t** output);
135
-
136
- /* Fills the new state with a dictionary for LZ77, warming up the ringbuffer,
137
- e.g. for custom static dictionaries for data formats.
138
- Not to be confused with the built-in transformable dictionary of Brotli.
139
- To decode, use BrotliSetCustomDictionary() of the decoder with the same
140
- dictionary. */
141
- void BrotliEncoderSetCustomDictionary(BrotliEncoderState* state, size_t size,
142
- const uint8_t* dict);
143
-
144
- /* Returns buffer size that is large enough to contain BrotliEncoderCompress
145
- output for any input.
146
- Returns 0 if result does not fit size_t. */
147
- size_t BrotliEncoderMaxCompressedSize(size_t input_size);
148
-
149
- /* Compresses the data in |input_buffer| into |encoded_buffer|, and sets
150
- |*encoded_size| to the compressed length.
151
- BROTLI_DEFAULT_QUALITY, BROTLI_DEFAULT_WINDOW and BROTLI_DEFAULT_MODE should
152
- be used as |quality|, |lgwin| and |mode| if there are no specific
153
- requirements to encoder speed and compression ratio.
154
- If compression fails, |*encoded_size| is set to 0.
155
- If BrotliEncoderMaxCompressedSize(|input_size|) is not zero, then
156
- |*encoded_size| is never set to the bigger value.
157
- Returns false if there was an error and true otherwise. */
158
- BROTLI_BOOL BrotliEncoderCompress(
159
- int quality, int lgwin, BrotliEncoderMode mode, size_t input_size,
160
- const uint8_t* input_buffer, size_t* encoded_size, uint8_t* encoded_buffer);
161
-
162
- /* Progressively compress input stream and push produced bytes to output stream.
163
- Internally workflow consists of 3 tasks:
164
- * (optional) copy input data to internal buffer
165
- * actually compress data and (optionally) store it to internal buffer
166
- * (optional) copy compressed bytes from internal buffer to output stream
167
- Whenever all 3 tasks can't move forward anymore, or error occurs, this
168
- method returns.
169
-
170
- |available_in| and |next_in| represent input stream; when X bytes of input
171
- are consumed, X is subtracted from |available_in| and added to |next_in|.
172
- |available_out| and |next_out| represent output stream; when Y bytes are
173
- pushed to output, Y is subtracted from |available_out| and added to
174
- |next_out|. |total_out|, if it is not a null-pointer, is assigned to the
175
- total amount of bytes pushed by the instance of encoder to output.
176
-
177
- |op| is used to perform flush or finish the stream.
178
-
179
- Flushing the stream means forcing encoding of all input passed to encoder and
180
- completing the current output block, so it could be fully decoded by stream
181
- decoder. To perform flush |op| must be set to BROTLI_OPERATION_FLUSH. Under
182
- some circumstances (e.g. lack of output stream capacity) this operation would
183
- require several calls to BrotliEncoderCompressStream. The method must be
184
- called again until both input stream is depleted and encoder has no more
185
- output (see BrotliEncoderHasMoreOutput) after the method is called.
186
-
187
- Finishing the stream means encoding of all input passed to encoder and
188
- adding specific "final" marks, so stream decoder could determine that stream
189
- is complete. To perform finish |op| must be set to BROTLI_OPERATION_FINISH.
190
- Under some circumstances (e.g. lack of output stream capacity) this operation
191
- would require several calls to BrotliEncoderCompressStream. The method must
192
- be called again until both input stream is depleted and encoder has no more
193
- output (see BrotliEncoderHasMoreOutput) after the method is called.
194
-
195
- WARNING: when flushing and finishing, |op| should not change until operation
196
- is complete; input stream should not be refilled as well.
197
-
198
- Returns false if there was an error and true otherwise.
199
- */
200
- BROTLI_BOOL BrotliEncoderCompressStream(
201
- BrotliEncoderState* s, BrotliEncoderOperation op, size_t* available_in,
202
- const uint8_t** next_in, size_t* available_out, uint8_t** next_out,
203
- size_t* total_out);
204
-
205
- /* Check if encoder is in "finished" state, i.e. no more input is acceptable and
206
- no more output will be produced.
207
- Works only with BrotliEncoderCompressStream workflow.
208
- Returns 1 if stream is finished and 0 otherwise. */
209
- BROTLI_BOOL BrotliEncoderIsFinished(BrotliEncoderState* s);
210
-
211
- /* Check if encoder has more output bytes in internal buffer.
212
- Works only with BrotliEncoderCompressStream workflow.
213
- Returns 1 if has more output (in internal buffer) and 0 otherwise. */
214
- BROTLI_BOOL BrotliEncoderHasMoreOutput(BrotliEncoderState* s);
215
-
216
-
217
- #if defined(__cplusplus) || defined(c_plusplus)
218
- } /* extern "C" */
219
- #endif
220
-
221
- #endif /* BROTLI_ENC_ENCODE_H_ */
@@ -1,289 +0,0 @@
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 parallel Brotli compressor. */
8
-
9
- #include "./encode_parallel.h"
10
-
11
- #include <vector>
12
-
13
- #include "./backward_references.h"
14
- #include "./brotli_bit_stream.h"
15
- #include "./context.h"
16
- #include "./entropy_encode.h"
17
- #include "./fast_log.h"
18
- #include "./hash.h"
19
- #include "./metablock.h"
20
- #include "./port.h"
21
- #include "./prefix.h"
22
- #include "./quality.h"
23
- #include "./utf8_util.h"
24
-
25
- namespace brotli {
26
-
27
- namespace {
28
-
29
- static void RecomputeDistancePrefixes(Command* cmds, size_t num_commands,
30
- uint32_t num_direct_distance_codes,
31
- uint32_t distance_postfix_bits) {
32
- if (num_direct_distance_codes == 0 &&
33
- distance_postfix_bits == 0) {
34
- return;
35
- }
36
- for (size_t i = 0; i < num_commands; ++i) {
37
- Command* cmd = &cmds[i];
38
- if (CommandCopyLen(cmd) && cmd->cmd_prefix_ >= 128) {
39
- PrefixEncodeCopyDistance(CommandDistanceCode(cmd),
40
- num_direct_distance_codes,
41
- distance_postfix_bits,
42
- &cmd->dist_prefix_,
43
- &cmd->dist_extra_);
44
- }
45
- }
46
- }
47
-
48
- /* Returns 1 on success, otherwise 0. */
49
- int WriteMetaBlockParallel(const BrotliEncoderParams* params,
50
- const uint32_t input_size,
51
- const uint8_t* input_buffer,
52
- const uint32_t prefix_size,
53
- const uint8_t* prefix_buffer,
54
- const int is_first,
55
- const int is_last,
56
- size_t* encoded_size,
57
- uint8_t* encoded_buffer) {
58
- if (input_size == 0) {
59
- return 0;
60
- }
61
-
62
- MemoryManager memory_manager;
63
- MemoryManager* m = &memory_manager;
64
- BrotliInitMemoryManager(m, 0, 0, 0);
65
-
66
- uint8_t* storage;
67
- size_t storage_ix;
68
- uint8_t first_byte;
69
- size_t first_byte_bits;
70
- size_t output_size;
71
- uint32_t num_direct_distance_codes;
72
- uint32_t distance_postfix_bits;
73
- ContextType literal_context_mode;
74
- size_t last_insert_len = 0;
75
- size_t num_commands = 0;
76
- size_t num_literals = 0;
77
- int dist_cache[4] = { -4, -4, -4, -4 };
78
- Command* commands;
79
- Hashers* hashers;
80
- int use_utf8_mode;
81
- uint8_t prev_byte;
82
- uint8_t prev_byte2;
83
- const uint32_t mask = BROTLI_UINT32_MAX >> 1;
84
-
85
- /* Copy prefix + next input block into a continuous area. */
86
- uint32_t input_pos = prefix_size;
87
- /* CreateBackwardReferences reads up to 3 bytes past the end of input if the
88
- mask points past the end of input.
89
- FindMatchLengthWithLimit could do another 8 bytes look-forward. */
90
- uint8_t* input = BROTLI_ALLOC(m, uint8_t, prefix_size + input_size + 4 + 8);
91
- if (BROTLI_IS_OOM(m)) goto oom;
92
- memcpy(input, prefix_buffer, prefix_size);
93
- memcpy(input + input_pos, input_buffer, input_size);
94
- /* Since we don't have a ringbuffer, masking is a no-op.
95
- We use one less bit than the full range because some of the code uses
96
- mask + 1 as the size of the ringbuffer. */
97
-
98
- prev_byte = input_pos > 0 ? input[(input_pos - 1) & mask] : 0;
99
- prev_byte2 = input_pos > 1 ? input[(input_pos - 2) & mask] : 0;
100
-
101
- /* Decide about UTF8 mode. */
102
- static const double kMinUTF8Ratio = 0.75;
103
- use_utf8_mode = BrotliIsMostlyUTF8(
104
- input, input_pos, mask, input_size, kMinUTF8Ratio);
105
-
106
- /* Initialize hashers. */
107
- hashers = BROTLI_ALLOC(m, Hashers, 1);
108
- if (BROTLI_IS_OOM(m)) goto oom;
109
- InitHashers(hashers);
110
- HashersSetup(m, hashers, ChooseHasher(params));
111
- if (BROTLI_IS_OOM(m)) goto oom;
112
-
113
- /* Compute backward references. */
114
- commands = BROTLI_ALLOC(m, Command, ((input_size + 1) >> 1));
115
- if (BROTLI_IS_OOM(m)) goto oom;
116
- BrotliCreateBackwardReferences(m, input_size, input_pos,
117
- TO_BROTLI_BOOL(is_last), input, mask, params,
118
- hashers, dist_cache, &last_insert_len, commands, &num_commands,
119
- &num_literals);
120
- if (BROTLI_IS_OOM(m)) goto oom;
121
- DestroyHashers(m, hashers);
122
- BROTLI_FREE(m, hashers);
123
- if (last_insert_len > 0) {
124
- InitInsertCommand(&commands[num_commands++], last_insert_len);
125
- num_literals += last_insert_len;
126
- }
127
- assert(num_commands != 0);
128
-
129
- /* Build the meta-block. */
130
- MetaBlockSplit mb;
131
- InitMetaBlockSplit(&mb);
132
- num_direct_distance_codes = params->mode == BROTLI_MODE_FONT ? 12 : 0;
133
- distance_postfix_bits = params->mode == BROTLI_MODE_FONT ? 1 : 0;
134
- literal_context_mode = use_utf8_mode ? CONTEXT_UTF8 : CONTEXT_SIGNED;
135
- RecomputeDistancePrefixes(commands, num_commands,
136
- num_direct_distance_codes,
137
- distance_postfix_bits);
138
- if (params->quality < MIN_QUALITY_FOR_HQ_BLOCK_SPLITTING) {
139
- BrotliBuildMetaBlockGreedy(m, input, input_pos, mask,
140
- commands, num_commands,
141
- &mb);
142
- if (BROTLI_IS_OOM(m)) goto oom;
143
- } else {
144
- BrotliBuildMetaBlock(m, input, input_pos, mask, params,
145
- prev_byte, prev_byte2,
146
- commands, num_commands,
147
- literal_context_mode,
148
- &mb);
149
- if (BROTLI_IS_OOM(m)) goto oom;
150
- }
151
-
152
- /* Set up the temporary output storage. */
153
- storage = BROTLI_ALLOC(m, uint8_t, 2 * input_size + 500);
154
- if (BROTLI_IS_OOM(m)) goto oom;
155
- first_byte = 0;
156
- first_byte_bits = 0;
157
- if (is_first) {
158
- if (params->lgwin == 16) {
159
- first_byte = 0;
160
- first_byte_bits = 1;
161
- } else if (params->lgwin == 17) {
162
- first_byte = 1;
163
- first_byte_bits = 7;
164
- } else {
165
- first_byte = static_cast<uint8_t>(((params->lgwin - 17) << 1) | 1);
166
- first_byte_bits = 4;
167
- }
168
- }
169
- storage[0] = static_cast<uint8_t>(first_byte);
170
- storage_ix = first_byte_bits;
171
-
172
- /* Store the meta-block to the temporary output. */
173
- BrotliStoreMetaBlock(m, input, input_pos, input_size, mask,
174
- prev_byte, prev_byte2,
175
- TO_BROTLI_BOOL(is_last),
176
- num_direct_distance_codes,
177
- distance_postfix_bits,
178
- literal_context_mode,
179
- commands, num_commands,
180
- &mb,
181
- &storage_ix, storage);
182
- if (BROTLI_IS_OOM(m)) goto oom;
183
- DestroyMetaBlockSplit(m, &mb);
184
- BROTLI_FREE(m, commands);
185
-
186
- /* If this is not the last meta-block, store an empty metadata
187
- meta-block so that the meta-block will end at a byte boundary. */
188
- if (!is_last) {
189
- BrotliStoreSyncMetaBlock(&storage_ix, storage);
190
- }
191
-
192
- /* If the compressed data is too large, fall back to an uncompressed
193
- meta-block. */
194
- output_size = storage_ix >> 3;
195
- if (input_size + 4 < output_size) {
196
- storage[0] = static_cast<uint8_t>(first_byte);
197
- storage_ix = first_byte_bits;
198
- BrotliStoreUncompressedMetaBlock(
199
- TO_BROTLI_BOOL(is_last), input, input_pos, mask, input_size,
200
- &storage_ix, storage);
201
- output_size = storage_ix >> 3;
202
- }
203
-
204
- /* Copy the temporary output with size-check to the output. */
205
- if (output_size > *encoded_size) {
206
- BROTLI_FREE(m, storage);
207
- BROTLI_FREE(m, input);
208
- return 0;
209
- }
210
- memcpy(encoded_buffer, storage, output_size);
211
- *encoded_size = output_size;
212
- BROTLI_FREE(m, storage);
213
- BROTLI_FREE(m, input);
214
- return 1;
215
-
216
- oom:
217
- BrotliWipeOutMemoryManager(m);
218
- return 0;
219
- }
220
-
221
- } /* namespace */
222
-
223
- int BrotliCompressBufferParallel(BrotliParams compressor_params,
224
- size_t input_size,
225
- const uint8_t* input_buffer,
226
- size_t* encoded_size,
227
- uint8_t* encoded_buffer) {
228
- if (*encoded_size == 0) {
229
- /* Output buffer needs at least one byte. */
230
- return 0;
231
- } else if (input_size == 0) {
232
- encoded_buffer[0] = 6;
233
- *encoded_size = 1;
234
- return 1;
235
- }
236
-
237
- BrotliEncoderParams params;
238
- params.mode = (BrotliEncoderMode)compressor_params.mode;
239
- params.quality = compressor_params.quality;
240
- params.lgwin = compressor_params.lgwin;
241
- params.lgblock = compressor_params.lgblock;
242
-
243
- SanitizeParams(&params);
244
- params.lgblock = ComputeLgBlock(&params);
245
- size_t max_input_block_size = 1 << params.lgblock;
246
- size_t max_prefix_size = 1u << params.lgwin;
247
-
248
- std::vector<std::vector<uint8_t> > compressed_pieces;
249
-
250
- /* Compress block-by-block independently. */
251
- for (size_t pos = 0; pos < input_size; ) {
252
- uint32_t input_block_size = static_cast<uint32_t>(
253
- BROTLI_MIN(size_t, max_input_block_size, input_size - pos));
254
- uint32_t prefix_size =
255
- static_cast<uint32_t>(BROTLI_MIN(size_t, max_prefix_size, pos));
256
- size_t out_size = input_block_size + (input_block_size >> 3) + 1024;
257
- std::vector<uint8_t> out(out_size);
258
- if (!WriteMetaBlockParallel(&params,
259
- input_block_size,
260
- &input_buffer[pos],
261
- prefix_size,
262
- &input_buffer[pos - prefix_size],
263
- (pos == 0) ? 1 : 0,
264
- (pos + input_block_size == input_size) ? 1 : 0,
265
- &out_size,
266
- &out[0])) {
267
- return 0;
268
- }
269
- out.resize(out_size);
270
- compressed_pieces.push_back(out);
271
- pos += input_block_size;
272
- }
273
-
274
- /* Piece together the output. */
275
- size_t out_pos = 0;
276
- for (size_t i = 0; i < compressed_pieces.size(); ++i) {
277
- const std::vector<uint8_t>& out = compressed_pieces[i];
278
- if (out_pos + out.size() > *encoded_size) {
279
- return 0;
280
- }
281
- memcpy(&encoded_buffer[out_pos], &out[0], out.size());
282
- out_pos += out.size();
283
- }
284
- *encoded_size = out_pos;
285
-
286
- return 1;
287
- }
288
-
289
- } /* namespace brotli */