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
@@ -4,37 +4,48 @@
4
4
  See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
5
  */
6
6
 
7
- // Function for fast encoding of an input fragment, independently from the input
8
- // history. This function uses two-pass processing: in the first pass we save
9
- // the found backward matches and literal bytes into a buffer, and in the
10
- // second pass we emit them into the bit stream using prefix codes built based
11
- // on the actual command and literal byte histograms.
7
+ /* Function for fast encoding of an input fragment, independently from the input
8
+ history. This function uses two-pass processing: in the first pass we save
9
+ the found backward matches and literal bytes into a buffer, and in the
10
+ second pass we emit them into the bit stream using prefix codes built based
11
+ on the actual command and literal byte histograms. */
12
12
 
13
13
  #ifndef BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
14
14
  #define BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
15
15
 
16
- #include "./types.h"
16
+ #include "../common/types.h"
17
+ #include "./memory.h"
18
+ #include "./port.h"
17
19
 
18
- namespace brotli {
20
+ #if defined(__cplusplus) || defined(c_plusplus)
21
+ extern "C" {
22
+ #endif
19
23
 
20
24
  static const size_t kCompressFragmentTwoPassBlockSize = 1 << 17;
21
25
 
22
- // Compresses "input" string to the "*storage" buffer as one or more complete
23
- // meta-blocks, and updates the "*storage_ix" bit position.
24
- //
25
- // If "is_last" is true, emits an additional empty last meta-block.
26
- //
27
- // REQUIRES: "input_size" is greater than zero, or "is_last" is true.
28
- // REQUIRES: "command_buf" and "literal_buf" point to at least
29
- // kCompressFragmentTwoPassBlockSize long arrays.
30
- // REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
31
- // REQUIRES: "table_size" is a power of two
32
- void BrotliCompressFragmentTwoPass(const uint8_t* input, size_t input_size,
33
- bool is_last,
34
- uint32_t* command_buf, uint8_t* literal_buf,
35
- int* table, size_t table_size,
36
- size_t* storage_ix, uint8_t* storage);
37
-
38
- } // namespace brotli
39
-
40
- #endif // BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_
26
+ /* Compresses "input" string to the "*storage" buffer as one or more complete
27
+ meta-blocks, and updates the "*storage_ix" bit position.
28
+
29
+ If "is_last" is 1, emits an additional empty last meta-block.
30
+
31
+ REQUIRES: "input_size" is greater than zero, or "is_last" is 1.
32
+ REQUIRES: "command_buf" and "literal_buf" point to at least
33
+ kCompressFragmentTwoPassBlockSize long arrays.
34
+ REQUIRES: All elements in "table[0..table_size-1]" are initialized to zero.
35
+ REQUIRES: "table_size" is a power of two */
36
+ BROTLI_INTERNAL void BrotliCompressFragmentTwoPass(MemoryManager* m,
37
+ const uint8_t* input,
38
+ size_t input_size,
39
+ BROTLI_BOOL is_last,
40
+ uint32_t* command_buf,
41
+ uint8_t* literal_buf,
42
+ int* table,
43
+ size_t table_size,
44
+ size_t* storage_ix,
45
+ uint8_t* storage);
46
+
47
+ #if defined(__cplusplus) || defined(c_plusplus)
48
+ } /* extern "C" */
49
+ #endif
50
+
51
+ #endif /* BROTLI_ENC_COMPRESS_FRAGMENT_TWO_PASS_H_ */
@@ -0,0 +1,139 @@
1
+ /* Copyright 2016 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
+ /* Brotli compressor API C++ wrapper and utilities. */
8
+
9
+ #include "./compressor.h"
10
+
11
+ #include <cstdlib> /* exit */
12
+
13
+ namespace brotli {
14
+
15
+ static void SetParams(const BrotliParams* from, BrotliEncoderState* to) {
16
+ BrotliEncoderMode mode = BROTLI_MODE_GENERIC;
17
+ if (from->mode == BrotliParams::MODE_TEXT) {
18
+ mode = BROTLI_MODE_TEXT;
19
+ } else if (from->mode == BrotliParams::MODE_FONT) {
20
+ mode = BROTLI_MODE_FONT;
21
+ }
22
+ BrotliEncoderSetParameter(to, BROTLI_PARAM_MODE, (uint32_t)mode);
23
+ BrotliEncoderSetParameter(to, BROTLI_PARAM_QUALITY, (uint32_t)from->quality);
24
+ BrotliEncoderSetParameter(to, BROTLI_PARAM_LGWIN, (uint32_t)from->lgwin);
25
+ BrotliEncoderSetParameter(to, BROTLI_PARAM_LGBLOCK, (uint32_t)from->lgblock);
26
+ }
27
+
28
+ BrotliCompressor::BrotliCompressor(BrotliParams params) {
29
+ state_ = BrotliEncoderCreateInstance(0, 0, 0);
30
+ if (state_ == 0) std::exit(EXIT_FAILURE); /* OOM */
31
+ SetParams(&params, state_);
32
+ }
33
+
34
+ BrotliCompressor::~BrotliCompressor(void) {
35
+ BrotliEncoderDestroyInstance(state_);
36
+ }
37
+
38
+ bool BrotliCompressor::WriteMetaBlock(const size_t input_size,
39
+ const uint8_t* input_buffer,
40
+ const bool is_last, size_t* encoded_size,
41
+ uint8_t* encoded_buffer) {
42
+ return !!BrotliEncoderWriteMetaBlock(state_, input_size, input_buffer,
43
+ TO_BROTLI_BOOL(is_last), encoded_size,
44
+ encoded_buffer);
45
+ }
46
+
47
+ bool BrotliCompressor::WriteMetadata(const size_t input_size,
48
+ const uint8_t* input_buffer,
49
+ const bool is_last, size_t* encoded_size,
50
+ uint8_t* encoded_buffer) {
51
+ return !!BrotliEncoderWriteMetadata(state_, input_size, input_buffer,
52
+ TO_BROTLI_BOOL(is_last), encoded_size,
53
+ encoded_buffer);
54
+ }
55
+
56
+ bool BrotliCompressor::FinishStream(size_t* encoded_size,
57
+ uint8_t* encoded_buffer) {
58
+ return !!BrotliEncoderFinishStream(state_, encoded_size, encoded_buffer);
59
+ }
60
+
61
+ void BrotliCompressor::CopyInputToRingBuffer(const size_t input_size,
62
+ const uint8_t* input_buffer) {
63
+ BrotliEncoderCopyInputToRingBuffer(state_, input_size, input_buffer);
64
+ }
65
+
66
+ bool BrotliCompressor::WriteBrotliData(const bool is_last,
67
+ const bool force_flush, size_t* out_size,
68
+ uint8_t** output) {
69
+ return !!BrotliEncoderWriteData(state_, TO_BROTLI_BOOL(is_last),
70
+ TO_BROTLI_BOOL(force_flush), out_size, output);
71
+ }
72
+
73
+ void BrotliCompressor::BrotliSetCustomDictionary(size_t size,
74
+ const uint8_t* dict) {
75
+ BrotliEncoderSetCustomDictionary(state_, size, dict);
76
+ }
77
+
78
+ int BrotliCompressBuffer(BrotliParams params, size_t input_size,
79
+ const uint8_t* input_buffer, size_t* encoded_size,
80
+ uint8_t* encoded_buffer) {
81
+ return BrotliEncoderCompress(params.quality, params.lgwin,
82
+ (BrotliEncoderMode)params.mode, input_size, input_buffer,
83
+ encoded_size, encoded_buffer);
84
+ }
85
+
86
+ int BrotliCompress(BrotliParams params, BrotliIn* in, BrotliOut* out) {
87
+ return BrotliCompressWithCustomDictionary(0, 0, params, in, out);
88
+ }
89
+
90
+ int BrotliCompressWithCustomDictionary(size_t dictsize, const uint8_t* dict,
91
+ BrotliParams params, BrotliIn* in,
92
+ BrotliOut* out) {
93
+ const size_t kOutputBufferSize = 65536;
94
+ uint8_t* output_buffer;
95
+ bool result = true;
96
+ size_t available_in = 0;
97
+ const uint8_t* next_in = NULL;
98
+ size_t total_out = 0;
99
+ bool end_of_input = false;
100
+ BrotliEncoderState* s;
101
+
102
+ s = BrotliEncoderCreateInstance(0, 0, 0);
103
+ if (!s) return 0;
104
+ SetParams(&params, s);
105
+ BrotliEncoderSetCustomDictionary(s, dictsize, dict);
106
+ output_buffer = new uint8_t[kOutputBufferSize];
107
+
108
+ while (true) {
109
+ if (available_in == 0 && !end_of_input) {
110
+ next_in = reinterpret_cast<const uint8_t*>(
111
+ in->Read(BrotliEncoderInputBlockSize(s), &available_in));
112
+ if (!next_in) {
113
+ end_of_input = true;
114
+ available_in = 0;
115
+ } else if (available_in == 0) {
116
+ continue;
117
+ }
118
+ }
119
+ size_t available_out = kOutputBufferSize;
120
+ uint8_t* next_out = output_buffer;
121
+ result = !!BrotliEncoderCompressStream(
122
+ s, end_of_input ? BROTLI_OPERATION_FINISH : BROTLI_OPERATION_PROCESS,
123
+ &available_in, &next_in, &available_out, &next_out, &total_out);
124
+ if (!result) break;
125
+ size_t used_output = kOutputBufferSize - available_out;
126
+ if (used_output != 0) {
127
+ result = out->Write(output_buffer, used_output);
128
+ if (!result) break;
129
+ }
130
+ if (BrotliEncoderIsFinished(s)) break;
131
+ }
132
+
133
+ delete[] output_buffer;
134
+ BrotliEncoderDestroyInstance(s);
135
+ return result ? 1 : 0;
136
+ }
137
+
138
+
139
+ } /* namespace brotli */
@@ -12,4 +12,150 @@
12
12
  #include "./encode.h"
13
13
  #include "./streams.h"
14
14
 
15
+ namespace brotli {
16
+
17
+ static const int kMinWindowBits = kBrotliMinWindowBits;
18
+ static const int kMaxWindowBits = kBrotliMaxWindowBits;
19
+ static const int kMinInputBlockBits = kBrotliMinInputBlockBits;
20
+ static const int kMaxInputBlockBits = kBrotliMaxInputBlockBits;
21
+
22
+ struct BrotliParams {
23
+ BrotliParams(void)
24
+ : mode(MODE_GENERIC),
25
+ quality(11),
26
+ lgwin(22),
27
+ lgblock(0),
28
+ enable_dictionary(true),
29
+ enable_transforms(false),
30
+ greedy_block_split(false),
31
+ enable_context_modeling(true) {}
32
+
33
+ enum Mode {
34
+ /* Default compression mode. The compressor does not know anything in
35
+ advance about the properties of the input. */
36
+ MODE_GENERIC = 0,
37
+ /* Compression mode for UTF-8 format text input. */
38
+ MODE_TEXT = 1,
39
+ /* Compression mode used in WOFF 2.0. */
40
+ MODE_FONT = 2
41
+ };
42
+ Mode mode;
43
+
44
+ /* Controls the compression-speed vs compression-density tradeoffs. The higher
45
+ the |quality|, the slower the compression. Range is 0 to 11. */
46
+ int quality;
47
+ /* Base 2 logarithm of the sliding window size. Range is 10 to 24. */
48
+ int lgwin;
49
+ /* Base 2 logarithm of the maximum input block size. Range is 16 to 24.
50
+ If set to 0, the value will be set based on the quality. */
51
+ int lgblock;
52
+
53
+ /* These settings are deprecated and will be ignored.
54
+ All speed vs. size compromises are controlled by the |quality| param. */
55
+ bool enable_dictionary;
56
+ bool enable_transforms;
57
+ bool greedy_block_split;
58
+ bool enable_context_modeling;
59
+ };
60
+
61
+ /* An instance can not be reused for multiple brotli streams. */
62
+ class BrotliCompressor {
63
+ public:
64
+ explicit BrotliCompressor(BrotliParams params);
65
+ ~BrotliCompressor(void);
66
+
67
+ /* The maximum input size that can be processed at once. */
68
+ size_t input_block_size(void) const {
69
+ return BrotliEncoderInputBlockSize(state_);
70
+ }
71
+
72
+ /* Encodes the data in |input_buffer| as a meta-block and writes it to
73
+ |encoded_buffer| (|*encoded_size should| be set to the size of
74
+ |encoded_buffer|) and sets |*encoded_size| to the number of bytes that
75
+ was written. The |input_size| must not be greater than input_block_size().
76
+ Returns false if there was an error and true otherwise. */
77
+ bool WriteMetaBlock(const size_t input_size,
78
+ const uint8_t* input_buffer,
79
+ const bool is_last,
80
+ size_t* encoded_size,
81
+ uint8_t* encoded_buffer);
82
+
83
+ /* Writes a metadata meta-block containing the given input to encoded_buffer.
84
+ |*encoded_size| should be set to the size of the encoded_buffer.
85
+ Sets |*encoded_size| to the number of bytes that was written.
86
+ Note that the given input data will not be part of the sliding window and
87
+ thus no backward references can be made to this data from subsequent
88
+ metablocks. |input_size| must not be greater than 2^24 and provided
89
+ |*encoded_size| must not be less than |input_size| + 6.
90
+ Returns false if there was an error and true otherwise. */
91
+ bool WriteMetadata(const size_t input_size,
92
+ const uint8_t* input_buffer,
93
+ const bool is_last,
94
+ size_t* encoded_size,
95
+ uint8_t* encoded_buffer);
96
+
97
+ /* Writes a zero-length meta-block with end-of-input bit set to the
98
+ internal output buffer and copies the output buffer to |encoded_buffer|
99
+ (|*encoded_size| should be set to the size of |encoded_buffer|) and sets
100
+ |*encoded_size| to the number of bytes written.
101
+ Returns false if there was an error and true otherwise. */
102
+ bool FinishStream(size_t* encoded_size, uint8_t* encoded_buffer);
103
+
104
+ /* Copies the given input data to the internal ring buffer of the compressor.
105
+ No processing of the data occurs at this time and this function can be
106
+ called multiple times before calling WriteBrotliData() to process the
107
+ accumulated input. At most input_block_size() bytes of input data can be
108
+ copied to the ring buffer, otherwise the next WriteBrotliData() will fail.
109
+ */
110
+ void CopyInputToRingBuffer(const size_t input_size,
111
+ const uint8_t* input_buffer);
112
+
113
+ /* Processes the accumulated input data and sets |*out_size| to the length of
114
+ the new output meta-block, or to zero if no new output meta-block has been
115
+ created (in this case the processed input data is buffered internally).
116
+ If |*out_size| is positive, |*output| points to the start of the output
117
+ data. If |is_last| or |force_flush| is true, an output meta-block is always
118
+ created. However, until |is_last| is true encoder may retain up to 7 bits
119
+ of the last byte of output. To force encoder to dump the remaining bits
120
+ use WriteMetadata() to append an empty meta-data block.
121
+ Returns false if the size of the input data is larger than
122
+ input_block_size(). */
123
+ bool WriteBrotliData(const bool is_last, const bool force_flush,
124
+ size_t* out_size, uint8_t** output);
125
+
126
+ /* Fills the new state with a dictionary for LZ77, warming up the ringbuffer,
127
+ e.g. for custom static dictionaries for data formats.
128
+ Not to be confused with the built-in transformable dictionary of Brotli.
129
+ To decode, use BrotliSetCustomDictionary() of the decoder with the same
130
+ dictionary. */
131
+ void BrotliSetCustomDictionary(size_t size, const uint8_t* dict);
132
+
133
+ /* No-op, but we keep it here for API backward-compatibility. */
134
+ void WriteStreamHeader(void) {}
135
+
136
+ private:
137
+ BrotliEncoderState* state_;
138
+ };
139
+
140
+ /* Compresses the data in |input_buffer| into |encoded_buffer|, and sets
141
+ |*encoded_size| to the compressed length.
142
+ Returns 0 if there was an error and 1 otherwise. */
143
+ int BrotliCompressBuffer(BrotliParams params,
144
+ size_t input_size,
145
+ const uint8_t* input_buffer,
146
+ size_t* encoded_size,
147
+ uint8_t* encoded_buffer);
148
+
149
+ /* Same as above, but uses the specified input and output classes instead
150
+ of reading from and writing to pre-allocated memory buffers. */
151
+ int BrotliCompress(BrotliParams params, BrotliIn* in, BrotliOut* out);
152
+
153
+ /* Before compressing the data, sets a custom LZ77 dictionary with
154
+ BrotliCompressor::BrotliSetCustomDictionary. */
155
+ int BrotliCompressWithCustomDictionary(size_t dictsize, const uint8_t* dict,
156
+ BrotliParams params,
157
+ BrotliIn* in, BrotliOut* out);
158
+
159
+ } /* namespace brotli */
160
+
15
161
  #endif /* BROTLI_ENC_COMPRESSOR_H_ */
@@ -4,91 +4,95 @@
4
4
  See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
5
  */
6
6
 
7
- // Functions to map previous bytes into a context id.
7
+ /* Functions to map previous bytes into a context id. */
8
8
 
9
9
  #ifndef BROTLI_ENC_CONTEXT_H_
10
10
  #define BROTLI_ENC_CONTEXT_H_
11
11
 
12
- #include "./types.h"
13
-
14
- namespace brotli {
15
-
16
- // Second-order context lookup table for UTF8 byte streams.
17
- //
18
- // If p1 and p2 are the previous two bytes, we calculate the context as
19
- //
20
- // context = kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256].
21
- //
22
- // If the previous two bytes are ASCII characters (i.e. < 128), this will be
23
- // equivalent to
24
- //
25
- // context = 4 * context1(p1) + context2(p2),
26
- //
27
- // where context1 is based on the previous byte in the following way:
28
- //
29
- // 0 : non-ASCII control
30
- // 1 : \t, \n, \r
31
- // 2 : space
32
- // 3 : other punctuation
33
- // 4 : " '
34
- // 5 : %
35
- // 6 : ( < [ {
36
- // 7 : ) > ] }
37
- // 8 : , ; :
38
- // 9 : .
39
- // 10 : =
40
- // 11 : number
41
- // 12 : upper-case vowel
42
- // 13 : upper-case consonant
43
- // 14 : lower-case vowel
44
- // 15 : lower-case consonant
45
- //
46
- // and context2 is based on the second last byte:
47
- //
48
- // 0 : control, space
49
- // 1 : punctuation
50
- // 2 : upper-case letter, number
51
- // 3 : lower-case letter
52
- //
53
- // If the last byte is ASCII, and the second last byte is not (in a valid UTF8
54
- // stream it will be a continuation byte, value between 128 and 191), the
55
- // context is the same as if the second last byte was an ASCII control or space.
56
- //
57
- // If the last byte is a UTF8 lead byte (value >= 192), then the next byte will
58
- // be a continuation byte and the context id is 2 or 3 depending on the LSB of
59
- // the last byte and to a lesser extent on the second last byte if it is ASCII.
60
- //
61
- // If the last byte is a UTF8 continuation byte, the second last byte can be:
62
- // - continuation byte: the next byte is probably ASCII or lead byte (assuming
63
- // 4-byte UTF8 characters are rare) and the context id is 0 or 1.
64
- // - lead byte (192 - 207): next byte is ASCII or lead byte, context is 0 or 1
65
- // - lead byte (208 - 255): next byte is continuation byte, context is 2 or 3
66
- //
67
- // The possible value combinations of the previous two bytes, the range of
68
- // context ids and the type of the next byte is summarized in the table below:
69
- //
70
- // |--------\-----------------------------------------------------------------|
71
- // | \ Last byte |
72
- // | Second \---------------------------------------------------------------|
73
- // | last byte \ ASCII | cont. byte | lead byte |
74
- // | \ (0-127) | (128-191) | (192-) |
75
- // |=============|===================|=====================|==================|
76
- // | ASCII | next: ASCII/lead | not valid | next: cont. |
77
- // | (0-127) | context: 4 - 63 | | context: 2 - 3 |
78
- // |-------------|-------------------|---------------------|------------------|
79
- // | cont. byte | next: ASCII/lead | next: ASCII/lead | next: cont. |
80
- // | (128-191) | context: 4 - 63 | context: 0 - 1 | context: 2 - 3 |
81
- // |-------------|-------------------|---------------------|------------------|
82
- // | lead byte | not valid | next: ASCII/lead | not valid |
83
- // | (192-207) | | context: 0 - 1 | |
84
- // |-------------|-------------------|---------------------|------------------|
85
- // | lead byte | not valid | next: cont. | not valid |
86
- // | (208-) | | context: 2 - 3 | |
87
- // |-------------|-------------------|---------------------|------------------|
12
+ #include "../common/types.h"
13
+ #include "../common/port.h"
14
+
15
+ #if defined(__cplusplus) || defined(c_plusplus)
16
+ extern "C" {
17
+ #endif
18
+
19
+ /* Second-order context lookup table for UTF8 byte streams.
20
+
21
+ If p1 and p2 are the previous two bytes, we calculate the context as
22
+
23
+ context = kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256].
24
+
25
+ If the previous two bytes are ASCII characters (i.e. < 128), this will be
26
+ equivalent to
27
+
28
+ context = 4 * context1(p1) + context2(p2),
29
+
30
+ where context1 is based on the previous byte in the following way:
31
+
32
+ 0 : non-ASCII control
33
+ 1 : \t, \n, \r
34
+ 2 : space
35
+ 3 : other punctuation
36
+ 4 : " '
37
+ 5 : %
38
+ 6 : ( < [ {
39
+ 7 : ) > ] }
40
+ 8 : , ; :
41
+ 9 : .
42
+ 10 : =
43
+ 11 : number
44
+ 12 : upper-case vowel
45
+ 13 : upper-case consonant
46
+ 14 : lower-case vowel
47
+ 15 : lower-case consonant
48
+
49
+ and context2 is based on the second last byte:
50
+
51
+ 0 : control, space
52
+ 1 : punctuation
53
+ 2 : upper-case letter, number
54
+ 3 : lower-case letter
55
+
56
+ If the last byte is ASCII, and the second last byte is not (in a valid UTF8
57
+ stream it will be a continuation byte, value between 128 and 191), the
58
+ context is the same as if the second last byte was an ASCII control or space.
59
+
60
+ If the last byte is a UTF8 lead byte (value >= 192), then the next byte will
61
+ be a continuation byte and the context id is 2 or 3 depending on the LSB of
62
+ the last byte and to a lesser extent on the second last byte if it is ASCII.
63
+
64
+ If the last byte is a UTF8 continuation byte, the second last byte can be:
65
+ - continuation byte: the next byte is probably ASCII or lead byte (assuming
66
+ 4-byte UTF8 characters are rare) and the context id is 0 or 1.
67
+ - lead byte (192 - 207): next byte is ASCII or lead byte, context is 0 or 1
68
+ - lead byte (208 - 255): next byte is continuation byte, context is 2 or 3
69
+
70
+ The possible value combinations of the previous two bytes, the range of
71
+ context ids and the type of the next byte is summarized in the table below:
72
+
73
+ |--------\-----------------------------------------------------------------|
74
+ | \ Last byte |
75
+ | Second \---------------------------------------------------------------|
76
+ | last byte \ ASCII | cont. byte | lead byte |
77
+ | \ (0-127) | (128-191) | (192-) |
78
+ |=============|===================|=====================|==================|
79
+ | ASCII | next: ASCII/lead | not valid | next: cont. |
80
+ | (0-127) | context: 4 - 63 | | context: 2 - 3 |
81
+ |-------------|-------------------|---------------------|------------------|
82
+ | cont. byte | next: ASCII/lead | next: ASCII/lead | next: cont. |
83
+ | (128-191) | context: 4 - 63 | context: 0 - 1 | context: 2 - 3 |
84
+ |-------------|-------------------|---------------------|------------------|
85
+ | lead byte | not valid | next: ASCII/lead | not valid |
86
+ | (192-207) | | context: 0 - 1 | |
87
+ |-------------|-------------------|---------------------|------------------|
88
+ | lead byte | not valid | next: cont. | not valid |
89
+ | (208-) | | context: 2 - 3 | |
90
+ |-------------|-------------------|---------------------|------------------|
91
+ */
88
92
  static const uint8_t kUTF8ContextLookup[512] = {
89
- // Last byte.
90
- //
91
- // ASCII range.
93
+ /* Last byte. */
94
+ /* */
95
+ /* ASCII range. */
92
96
  0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 0, 0,
93
97
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94
98
  8, 12, 16, 12, 12, 20, 12, 16, 24, 28, 12, 12, 32, 12, 36, 12,
@@ -97,19 +101,19 @@ static const uint8_t kUTF8ContextLookup[512] = {
97
101
  52, 52, 52, 52, 52, 48, 52, 52, 52, 52, 52, 24, 12, 28, 12, 12,
98
102
  12, 56, 60, 60, 60, 56, 60, 60, 60, 56, 60, 60, 60, 60, 60, 56,
99
103
  60, 60, 60, 60, 60, 56, 60, 60, 60, 60, 60, 24, 12, 28, 12, 0,
100
- // UTF8 continuation byte range.
104
+ /* UTF8 continuation byte range. */
101
105
  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
102
106
  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
103
107
  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
104
108
  0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
105
- // UTF8 lead byte range.
109
+ /* UTF8 lead byte range. */
106
110
  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
107
111
  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
108
112
  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
109
113
  2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
110
- // Second last byte.
111
- //
112
- // ASCII range.
114
+ /* Second last byte. */
115
+ /* */
116
+ /* ASCII range. */
113
117
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114
118
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115
119
  0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -118,19 +122,19 @@ static const uint8_t kUTF8ContextLookup[512] = {
118
122
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
119
123
  1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
120
124
  3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 0,
121
- // UTF8 continuation byte range.
125
+ /* UTF8 continuation byte range. */
122
126
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123
127
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124
128
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125
129
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126
130
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
127
- // UTF8 lead byte range.
131
+ /* UTF8 lead byte range. */
128
132
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129
133
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
130
134
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131
135
  };
132
136
 
133
- // Context lookup table for small signed integers.
137
+ /* Context lookup table for small signed integers. */
134
138
  static const uint8_t kSigned3BitContextLookup[] = {
135
139
  0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
136
140
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
@@ -150,29 +154,31 @@ static const uint8_t kSigned3BitContextLookup[] = {
150
154
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
151
155
  };
152
156
 
153
- enum ContextType {
157
+ typedef enum ContextType {
154
158
  CONTEXT_LSB6 = 0,
155
159
  CONTEXT_MSB6 = 1,
156
160
  CONTEXT_UTF8 = 2,
157
161
  CONTEXT_SIGNED = 3
158
- };
162
+ } ContextType;
159
163
 
160
- static inline uint8_t Context(uint8_t p1, uint8_t p2, ContextType mode) {
164
+ static BROTLI_INLINE uint8_t Context(uint8_t p1, uint8_t p2, ContextType mode) {
161
165
  switch (mode) {
162
166
  case CONTEXT_LSB6:
163
167
  return p1 & 0x3f;
164
168
  case CONTEXT_MSB6:
165
- return static_cast<uint8_t>(p1 >> 2);
169
+ return (uint8_t)(p1 >> 2);
166
170
  case CONTEXT_UTF8:
167
171
  return kUTF8ContextLookup[p1] | kUTF8ContextLookup[p2 + 256];
168
172
  case CONTEXT_SIGNED:
169
- return static_cast<uint8_t>((kSigned3BitContextLookup[p1] << 3) +
170
- kSigned3BitContextLookup[p2]);
173
+ return (uint8_t)((kSigned3BitContextLookup[p1] << 3) +
174
+ kSigned3BitContextLookup[p2]);
171
175
  default:
172
176
  return 0;
173
177
  }
174
178
  }
175
179
 
176
- } // namespace brotli
180
+ #if defined(__cplusplus) || defined(c_plusplus)
181
+ } /* extern "C" */
182
+ #endif
177
183
 
178
- #endif // BROTLI_ENC_CONTEXT_H_
184
+ #endif /* BROTLI_ENC_CONTEXT_H_ */
@@ -4,14 +4,16 @@
4
4
  See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
5
  */
6
6
 
7
- // Hash table on the 4-byte prefixes of static dictionary words.
7
+ /* Hash table on the 4-byte prefixes of static dictionary words. */
8
8
 
9
9
  #ifndef BROTLI_ENC_DICTIONARY_HASH_H_
10
10
  #define BROTLI_ENC_DICTIONARY_HASH_H_
11
11
 
12
- #include "./types.h"
12
+ #include "../common/types.h"
13
13
 
14
- namespace brotli {
14
+ #if defined(__cplusplus) || defined(c_plusplus)
15
+ extern "C" {
16
+ #endif
15
17
 
16
18
  static const uint16_t kStaticDictionaryHash[] = {
17
19
  0x7d48, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
@@ -4112,6 +4114,8 @@ static const uint16_t kStaticDictionaryHash[] = {
4112
4114
  0x0000, 0x0000, 0x0d88, 0x4ac5, 0x0000, 0x0000, 0x0000, 0x0000,
4113
4115
  };
4114
4116
 
4115
- } // namespace brotli
4117
+ #if defined(__cplusplus) || defined(c_plusplus)
4118
+ } /* extern "C" */
4119
+ #endif
4116
4120
 
4117
- #endif // BROTLI_ENC_DICTIONARY_HASH_H_
4121
+ #endif /* BROTLI_ENC_DICTIONARY_HASH_H_ */