brotli 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/main.yml +34 -0
  3. data/.github/workflows/publish.yml +34 -0
  4. data/Gemfile +6 -2
  5. data/Rakefile +18 -6
  6. data/bin/before_install.sh +9 -0
  7. data/brotli.gemspec +7 -13
  8. data/ext/brotli/brotli.c +209 -11
  9. data/ext/brotli/buffer.c +1 -7
  10. data/ext/brotli/buffer.h +1 -1
  11. data/ext/brotli/extconf.rb +45 -26
  12. data/lib/brotli/version.rb +1 -1
  13. data/smoke.sh +1 -1
  14. data/test/brotli_test.rb +104 -0
  15. data/test/brotli_writer_test.rb +36 -0
  16. data/test/test_helper.rb +8 -0
  17. data/vendor/brotli/c/common/constants.c +15 -0
  18. data/vendor/brotli/c/common/constants.h +149 -6
  19. data/vendor/brotli/c/{dec/context.h → common/context.c} +91 -186
  20. data/vendor/brotli/c/common/context.h +113 -0
  21. data/vendor/brotli/c/common/dictionary.bin +0 -0
  22. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
  23. data/vendor/brotli/c/common/dictionary.c +11 -2
  24. data/vendor/brotli/c/common/dictionary.h +4 -4
  25. data/vendor/brotli/c/common/platform.c +22 -0
  26. data/vendor/brotli/c/common/platform.h +594 -0
  27. data/vendor/brotli/c/common/transform.c +291 -0
  28. data/vendor/brotli/c/common/transform.h +85 -0
  29. data/vendor/brotli/c/common/version.h +8 -1
  30. data/vendor/brotli/c/dec/bit_reader.c +29 -1
  31. data/vendor/brotli/c/dec/bit_reader.h +91 -100
  32. data/vendor/brotli/c/dec/decode.c +665 -437
  33. data/vendor/brotli/c/dec/huffman.c +65 -84
  34. data/vendor/brotli/c/dec/huffman.h +67 -14
  35. data/vendor/brotli/c/dec/prefix.h +1 -20
  36. data/vendor/brotli/c/dec/state.c +32 -45
  37. data/vendor/brotli/c/dec/state.h +173 -55
  38. data/vendor/brotli/c/enc/backward_references.c +27 -16
  39. data/vendor/brotli/c/enc/backward_references.h +7 -7
  40. data/vendor/brotli/c/enc/backward_references_hq.c +155 -116
  41. data/vendor/brotli/c/enc/backward_references_hq.h +22 -23
  42. data/vendor/brotli/c/enc/backward_references_inc.h +32 -22
  43. data/vendor/brotli/c/enc/bit_cost.c +1 -1
  44. data/vendor/brotli/c/enc/bit_cost.h +5 -5
  45. data/vendor/brotli/c/enc/block_encoder_inc.h +7 -6
  46. data/vendor/brotli/c/enc/block_splitter.c +5 -6
  47. data/vendor/brotli/c/enc/block_splitter.h +1 -1
  48. data/vendor/brotli/c/enc/block_splitter_inc.h +26 -17
  49. data/vendor/brotli/c/enc/brotli_bit_stream.c +107 -123
  50. data/vendor/brotli/c/enc/brotli_bit_stream.h +19 -38
  51. data/vendor/brotli/c/enc/cluster.c +1 -1
  52. data/vendor/brotli/c/enc/cluster.h +1 -1
  53. data/vendor/brotli/c/enc/cluster_inc.h +6 -3
  54. data/vendor/brotli/c/enc/command.c +28 -0
  55. data/vendor/brotli/c/enc/command.h +52 -42
  56. data/vendor/brotli/c/enc/compress_fragment.c +21 -22
  57. data/vendor/brotli/c/enc/compress_fragment.h +1 -1
  58. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +102 -69
  59. data/vendor/brotli/c/enc/compress_fragment_two_pass.h +1 -1
  60. data/vendor/brotli/c/enc/dictionary_hash.c +1827 -1101
  61. data/vendor/brotli/c/enc/dictionary_hash.h +2 -1
  62. data/vendor/brotli/c/enc/encode.c +358 -195
  63. data/vendor/brotli/c/enc/encoder_dict.c +33 -0
  64. data/vendor/brotli/c/enc/encoder_dict.h +43 -0
  65. data/vendor/brotli/c/enc/entropy_encode.c +16 -14
  66. data/vendor/brotli/c/enc/entropy_encode.h +7 -7
  67. data/vendor/brotli/c/enc/entropy_encode_static.h +3 -3
  68. data/vendor/brotli/c/enc/fast_log.c +105 -0
  69. data/vendor/brotli/c/enc/fast_log.h +20 -99
  70. data/vendor/brotli/c/enc/find_match_length.h +5 -6
  71. data/vendor/brotli/c/enc/hash.h +145 -103
  72. data/vendor/brotli/c/enc/hash_composite_inc.h +125 -0
  73. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +93 -53
  74. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +54 -53
  75. data/vendor/brotli/c/enc/hash_longest_match_inc.h +58 -54
  76. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +95 -63
  77. data/vendor/brotli/c/enc/hash_rolling_inc.h +212 -0
  78. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +46 -43
  79. data/vendor/brotli/c/enc/histogram.c +9 -6
  80. data/vendor/brotli/c/enc/histogram.h +6 -3
  81. data/vendor/brotli/c/enc/histogram_inc.h +1 -1
  82. data/vendor/brotli/c/enc/literal_cost.c +5 -5
  83. data/vendor/brotli/c/enc/literal_cost.h +2 -2
  84. data/vendor/brotli/c/enc/memory.c +5 -16
  85. data/vendor/brotli/c/enc/memory.h +52 -1
  86. data/vendor/brotli/c/enc/metablock.c +171 -36
  87. data/vendor/brotli/c/enc/metablock.h +13 -8
  88. data/vendor/brotli/c/enc/metablock_inc.h +2 -2
  89. data/vendor/brotli/c/enc/params.h +46 -0
  90. data/vendor/brotli/c/enc/prefix.h +3 -4
  91. data/vendor/brotli/c/enc/quality.h +29 -24
  92. data/vendor/brotli/c/enc/ringbuffer.h +19 -12
  93. data/vendor/brotli/c/enc/static_dict.c +49 -45
  94. data/vendor/brotli/c/enc/static_dict.h +4 -3
  95. data/vendor/brotli/c/enc/static_dict_lut.h +1 -1
  96. data/vendor/brotli/c/enc/utf8_util.c +21 -21
  97. data/vendor/brotli/c/enc/utf8_util.h +1 -1
  98. data/vendor/brotli/c/enc/write_bits.h +35 -38
  99. data/vendor/brotli/c/include/brotli/decode.h +13 -8
  100. data/vendor/brotli/c/include/brotli/encode.h +54 -8
  101. data/vendor/brotli/c/include/brotli/port.h +225 -83
  102. data/vendor/brotli/c/include/brotli/types.h +0 -7
  103. metadata +28 -87
  104. data/.travis.yml +0 -30
  105. data/spec/brotli_spec.rb +0 -88
  106. data/spec/inflate_spec.rb +0 -75
  107. data/spec/spec_helper.rb +0 -4
  108. data/vendor/brotli/c/dec/port.h +0 -168
  109. data/vendor/brotli/c/dec/transform.h +0 -300
  110. data/vendor/brotli/c/enc/context.h +0 -184
  111. data/vendor/brotli/c/enc/port.h +0 -184
@@ -0,0 +1,291 @@
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
+ #include "./transform.h"
8
+
9
+ #if defined(__cplusplus) || defined(c_plusplus)
10
+ extern "C" {
11
+ #endif
12
+
13
+ /* RFC 7932 transforms string data */
14
+ static const char kPrefixSuffix[217] =
15
+ "\1 \2, \10 of the \4 of \2s \1.\5 and \4 "
16
+ /* 0x _0 _2 __5 _E _3 _6 _8 _E */
17
+ "in \1\"\4 to \2\">\1\n\2. \1]\5 for \3 a \6 "
18
+ /* 2x _3_ _5 _A_ _D_ _F _2 _4 _A _E */
19
+ "that \1\'\6 with \6 from \4 by \1(\6. T"
20
+ /* 4x _5_ _7 _E _5 _A _C */
21
+ "he \4 on \4 as \4 is \4ing \2\n\t\1:\3ed "
22
+ /* 6x _3 _8 _D _2 _7_ _ _A _C */
23
+ "\2=\"\4 at \3ly \1,\2=\'\5.com/\7. This \5"
24
+ /* 8x _0 _ _3 _8 _C _E _ _1 _7 _F */
25
+ " not \3er \3al \4ful \4ive \5less \4es"
26
+ /* Ax _5 _9 _D _2 _7 _D */
27
+ "t \4ize \2\xc2\xa0\4ous \5 the \2e "; /* \0 - implicit trailing zero. */
28
+ /* Cx _2 _7___ ___ _A _F _5 _8 */
29
+
30
+ static const uint16_t kPrefixSuffixMap[50] = {
31
+ 0x00, 0x02, 0x05, 0x0E, 0x13, 0x16, 0x18, 0x1E, 0x23, 0x25,
32
+ 0x2A, 0x2D, 0x2F, 0x32, 0x34, 0x3A, 0x3E, 0x45, 0x47, 0x4E,
33
+ 0x55, 0x5A, 0x5C, 0x63, 0x68, 0x6D, 0x72, 0x77, 0x7A, 0x7C,
34
+ 0x80, 0x83, 0x88, 0x8C, 0x8E, 0x91, 0x97, 0x9F, 0xA5, 0xA9,
35
+ 0xAD, 0xB2, 0xB7, 0xBD, 0xC2, 0xC7, 0xCA, 0xCF, 0xD5, 0xD8
36
+ };
37
+
38
+ /* RFC 7932 transforms */
39
+ static const uint8_t kTransformsData[] = {
40
+ 49, BROTLI_TRANSFORM_IDENTITY, 49,
41
+ 49, BROTLI_TRANSFORM_IDENTITY, 0,
42
+ 0, BROTLI_TRANSFORM_IDENTITY, 0,
43
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_1, 49,
44
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0,
45
+ 49, BROTLI_TRANSFORM_IDENTITY, 47,
46
+ 0, BROTLI_TRANSFORM_IDENTITY, 49,
47
+ 4, BROTLI_TRANSFORM_IDENTITY, 0,
48
+ 49, BROTLI_TRANSFORM_IDENTITY, 3,
49
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49,
50
+ 49, BROTLI_TRANSFORM_IDENTITY, 6,
51
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_2, 49,
52
+ 49, BROTLI_TRANSFORM_OMIT_LAST_1, 49,
53
+ 1, BROTLI_TRANSFORM_IDENTITY, 0,
54
+ 49, BROTLI_TRANSFORM_IDENTITY, 1,
55
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 0,
56
+ 49, BROTLI_TRANSFORM_IDENTITY, 7,
57
+ 49, BROTLI_TRANSFORM_IDENTITY, 9,
58
+ 48, BROTLI_TRANSFORM_IDENTITY, 0,
59
+ 49, BROTLI_TRANSFORM_IDENTITY, 8,
60
+ 49, BROTLI_TRANSFORM_IDENTITY, 5,
61
+ 49, BROTLI_TRANSFORM_IDENTITY, 10,
62
+ 49, BROTLI_TRANSFORM_IDENTITY, 11,
63
+ 49, BROTLI_TRANSFORM_OMIT_LAST_3, 49,
64
+ 49, BROTLI_TRANSFORM_IDENTITY, 13,
65
+ 49, BROTLI_TRANSFORM_IDENTITY, 14,
66
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_3, 49,
67
+ 49, BROTLI_TRANSFORM_OMIT_LAST_2, 49,
68
+ 49, BROTLI_TRANSFORM_IDENTITY, 15,
69
+ 49, BROTLI_TRANSFORM_IDENTITY, 16,
70
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 49,
71
+ 49, BROTLI_TRANSFORM_IDENTITY, 12,
72
+ 5, BROTLI_TRANSFORM_IDENTITY, 49,
73
+ 0, BROTLI_TRANSFORM_IDENTITY, 1,
74
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_4, 49,
75
+ 49, BROTLI_TRANSFORM_IDENTITY, 18,
76
+ 49, BROTLI_TRANSFORM_IDENTITY, 17,
77
+ 49, BROTLI_TRANSFORM_IDENTITY, 19,
78
+ 49, BROTLI_TRANSFORM_IDENTITY, 20,
79
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_5, 49,
80
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_6, 49,
81
+ 47, BROTLI_TRANSFORM_IDENTITY, 49,
82
+ 49, BROTLI_TRANSFORM_OMIT_LAST_4, 49,
83
+ 49, BROTLI_TRANSFORM_IDENTITY, 22,
84
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 49,
85
+ 49, BROTLI_TRANSFORM_IDENTITY, 23,
86
+ 49, BROTLI_TRANSFORM_IDENTITY, 24,
87
+ 49, BROTLI_TRANSFORM_IDENTITY, 25,
88
+ 49, BROTLI_TRANSFORM_OMIT_LAST_7, 49,
89
+ 49, BROTLI_TRANSFORM_OMIT_LAST_1, 26,
90
+ 49, BROTLI_TRANSFORM_IDENTITY, 27,
91
+ 49, BROTLI_TRANSFORM_IDENTITY, 28,
92
+ 0, BROTLI_TRANSFORM_IDENTITY, 12,
93
+ 49, BROTLI_TRANSFORM_IDENTITY, 29,
94
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_9, 49,
95
+ 49, BROTLI_TRANSFORM_OMIT_FIRST_7, 49,
96
+ 49, BROTLI_TRANSFORM_OMIT_LAST_6, 49,
97
+ 49, BROTLI_TRANSFORM_IDENTITY, 21,
98
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1,
99
+ 49, BROTLI_TRANSFORM_OMIT_LAST_8, 49,
100
+ 49, BROTLI_TRANSFORM_IDENTITY, 31,
101
+ 49, BROTLI_TRANSFORM_IDENTITY, 32,
102
+ 47, BROTLI_TRANSFORM_IDENTITY, 3,
103
+ 49, BROTLI_TRANSFORM_OMIT_LAST_5, 49,
104
+ 49, BROTLI_TRANSFORM_OMIT_LAST_9, 49,
105
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 1,
106
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 8,
107
+ 5, BROTLI_TRANSFORM_IDENTITY, 21,
108
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 0,
109
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 10,
110
+ 49, BROTLI_TRANSFORM_IDENTITY, 30,
111
+ 0, BROTLI_TRANSFORM_IDENTITY, 5,
112
+ 35, BROTLI_TRANSFORM_IDENTITY, 49,
113
+ 47, BROTLI_TRANSFORM_IDENTITY, 2,
114
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 17,
115
+ 49, BROTLI_TRANSFORM_IDENTITY, 36,
116
+ 49, BROTLI_TRANSFORM_IDENTITY, 33,
117
+ 5, BROTLI_TRANSFORM_IDENTITY, 0,
118
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 21,
119
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5,
120
+ 49, BROTLI_TRANSFORM_IDENTITY, 37,
121
+ 0, BROTLI_TRANSFORM_IDENTITY, 30,
122
+ 49, BROTLI_TRANSFORM_IDENTITY, 38,
123
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 0,
124
+ 49, BROTLI_TRANSFORM_IDENTITY, 39,
125
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 49,
126
+ 49, BROTLI_TRANSFORM_IDENTITY, 34,
127
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 8,
128
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12,
129
+ 0, BROTLI_TRANSFORM_IDENTITY, 21,
130
+ 49, BROTLI_TRANSFORM_IDENTITY, 40,
131
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 12,
132
+ 49, BROTLI_TRANSFORM_IDENTITY, 41,
133
+ 49, BROTLI_TRANSFORM_IDENTITY, 42,
134
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 17,
135
+ 49, BROTLI_TRANSFORM_IDENTITY, 43,
136
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 5,
137
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 10,
138
+ 0, BROTLI_TRANSFORM_IDENTITY, 34,
139
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33,
140
+ 49, BROTLI_TRANSFORM_IDENTITY, 44,
141
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 5,
142
+ 45, BROTLI_TRANSFORM_IDENTITY, 49,
143
+ 0, BROTLI_TRANSFORM_IDENTITY, 33,
144
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30,
145
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 30,
146
+ 49, BROTLI_TRANSFORM_IDENTITY, 46,
147
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 1,
148
+ 49, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34,
149
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 33,
150
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 30,
151
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 1,
152
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 33,
153
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 21,
154
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 12,
155
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 5,
156
+ 49, BROTLI_TRANSFORM_UPPERCASE_ALL, 34,
157
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 12,
158
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 30,
159
+ 0, BROTLI_TRANSFORM_UPPERCASE_ALL, 34,
160
+ 0, BROTLI_TRANSFORM_UPPERCASE_FIRST, 34,
161
+ };
162
+
163
+ static const BrotliTransforms kBrotliTransforms = {
164
+ sizeof(kPrefixSuffix),
165
+ (const uint8_t*)kPrefixSuffix,
166
+ kPrefixSuffixMap,
167
+ sizeof(kTransformsData) / (3 * sizeof(kTransformsData[0])),
168
+ kTransformsData,
169
+ NULL, /* no extra parameters */
170
+ {0, 12, 27, 23, 42, 63, 56, 48, 59, 64}
171
+ };
172
+
173
+ const BrotliTransforms* BrotliGetTransforms(void) {
174
+ return &kBrotliTransforms;
175
+ }
176
+
177
+ static int ToUpperCase(uint8_t* p) {
178
+ if (p[0] < 0xC0) {
179
+ if (p[0] >= 'a' && p[0] <= 'z') {
180
+ p[0] ^= 32;
181
+ }
182
+ return 1;
183
+ }
184
+ /* An overly simplified uppercasing model for UTF-8. */
185
+ if (p[0] < 0xE0) {
186
+ p[1] ^= 32;
187
+ return 2;
188
+ }
189
+ /* An arbitrary transform for three byte characters. */
190
+ p[2] ^= 5;
191
+ return 3;
192
+ }
193
+
194
+ static int Shift(uint8_t* word, int word_len, uint16_t parameter) {
195
+ /* Limited sign extension: scalar < (1 << 24). */
196
+ uint32_t scalar =
197
+ (parameter & 0x7FFFu) + (0x1000000u - (parameter & 0x8000u));
198
+ if (word[0] < 0x80) {
199
+ /* 1-byte rune / 0sssssss / 7 bit scalar (ASCII). */
200
+ scalar += (uint32_t)word[0];
201
+ word[0] = (uint8_t)(scalar & 0x7Fu);
202
+ return 1;
203
+ } else if (word[0] < 0xC0) {
204
+ /* Continuation / 10AAAAAA. */
205
+ return 1;
206
+ } else if (word[0] < 0xE0) {
207
+ /* 2-byte rune / 110sssss AAssssss / 11 bit scalar. */
208
+ if (word_len < 2) return 1;
209
+ scalar += (uint32_t)((word[1] & 0x3Fu) | ((word[0] & 0x1Fu) << 6u));
210
+ word[0] = (uint8_t)(0xC0 | ((scalar >> 6u) & 0x1F));
211
+ word[1] = (uint8_t)((word[1] & 0xC0) | (scalar & 0x3F));
212
+ return 2;
213
+ } else if (word[0] < 0xF0) {
214
+ /* 3-byte rune / 1110ssss AAssssss BBssssss / 16 bit scalar. */
215
+ if (word_len < 3) return word_len;
216
+ scalar += (uint32_t)((word[2] & 0x3Fu) | ((word[1] & 0x3Fu) << 6u) |
217
+ ((word[0] & 0x0Fu) << 12u));
218
+ word[0] = (uint8_t)(0xE0 | ((scalar >> 12u) & 0x0F));
219
+ word[1] = (uint8_t)((word[1] & 0xC0) | ((scalar >> 6u) & 0x3F));
220
+ word[2] = (uint8_t)((word[2] & 0xC0) | (scalar & 0x3F));
221
+ return 3;
222
+ } else if (word[0] < 0xF8) {
223
+ /* 4-byte rune / 11110sss AAssssss BBssssss CCssssss / 21 bit scalar. */
224
+ if (word_len < 4) return word_len;
225
+ scalar += (uint32_t)((word[3] & 0x3Fu) | ((word[2] & 0x3Fu) << 6u) |
226
+ ((word[1] & 0x3Fu) << 12u) | ((word[0] & 0x07u) << 18u));
227
+ word[0] = (uint8_t)(0xF0 | ((scalar >> 18u) & 0x07));
228
+ word[1] = (uint8_t)((word[1] & 0xC0) | ((scalar >> 12u) & 0x3F));
229
+ word[2] = (uint8_t)((word[2] & 0xC0) | ((scalar >> 6u) & 0x3F));
230
+ word[3] = (uint8_t)((word[3] & 0xC0) | (scalar & 0x3F));
231
+ return 4;
232
+ }
233
+ return 1;
234
+ }
235
+
236
+ int BrotliTransformDictionaryWord(uint8_t* dst, const uint8_t* word, int len,
237
+ const BrotliTransforms* transforms, int transform_idx) {
238
+ int idx = 0;
239
+ const uint8_t* prefix = BROTLI_TRANSFORM_PREFIX(transforms, transform_idx);
240
+ uint8_t type = BROTLI_TRANSFORM_TYPE(transforms, transform_idx);
241
+ const uint8_t* suffix = BROTLI_TRANSFORM_SUFFIX(transforms, transform_idx);
242
+ {
243
+ int prefix_len = *prefix++;
244
+ while (prefix_len--) { dst[idx++] = *prefix++; }
245
+ }
246
+ {
247
+ const int t = type;
248
+ int i = 0;
249
+ if (t <= BROTLI_TRANSFORM_OMIT_LAST_9) {
250
+ len -= t;
251
+ } else if (t >= BROTLI_TRANSFORM_OMIT_FIRST_1
252
+ && t <= BROTLI_TRANSFORM_OMIT_FIRST_9) {
253
+ int skip = t - (BROTLI_TRANSFORM_OMIT_FIRST_1 - 1);
254
+ word += skip;
255
+ len -= skip;
256
+ }
257
+ while (i < len) { dst[idx++] = word[i++]; }
258
+ if (t == BROTLI_TRANSFORM_UPPERCASE_FIRST) {
259
+ ToUpperCase(&dst[idx - len]);
260
+ } else if (t == BROTLI_TRANSFORM_UPPERCASE_ALL) {
261
+ uint8_t* uppercase = &dst[idx - len];
262
+ while (len > 0) {
263
+ int step = ToUpperCase(uppercase);
264
+ uppercase += step;
265
+ len -= step;
266
+ }
267
+ } else if (t == BROTLI_TRANSFORM_SHIFT_FIRST) {
268
+ uint16_t param = (uint16_t)(transforms->params[transform_idx * 2]
269
+ + (transforms->params[transform_idx * 2 + 1] << 8u));
270
+ Shift(&dst[idx - len], len, param);
271
+ } else if (t == BROTLI_TRANSFORM_SHIFT_ALL) {
272
+ uint16_t param = (uint16_t)(transforms->params[transform_idx * 2]
273
+ + (transforms->params[transform_idx * 2 + 1] << 8u));
274
+ uint8_t* shift = &dst[idx - len];
275
+ while (len > 0) {
276
+ int step = Shift(shift, len, param);
277
+ shift += step;
278
+ len -= step;
279
+ }
280
+ }
281
+ }
282
+ {
283
+ int suffix_len = *suffix++;
284
+ while (suffix_len--) { dst[idx++] = *suffix++; }
285
+ return idx;
286
+ }
287
+ }
288
+
289
+ #if defined(__cplusplus) || defined(c_plusplus)
290
+ } /* extern "C" */
291
+ #endif
@@ -0,0 +1,85 @@
1
+ /* transforms is a part of ABI, but not API.
2
+
3
+ It means that there are some functions that are supposed to be in "common"
4
+ library, but header itself is not placed into include/brotli. This way,
5
+ aforementioned functions will be available only to brotli internals.
6
+ */
7
+
8
+ #ifndef BROTLI_COMMON_TRANSFORM_H_
9
+ #define BROTLI_COMMON_TRANSFORM_H_
10
+
11
+ #include <brotli/port.h>
12
+ #include <brotli/types.h>
13
+
14
+ #if defined(__cplusplus) || defined(c_plusplus)
15
+ extern "C" {
16
+ #endif
17
+
18
+ enum BrotliWordTransformType {
19
+ BROTLI_TRANSFORM_IDENTITY = 0,
20
+ BROTLI_TRANSFORM_OMIT_LAST_1 = 1,
21
+ BROTLI_TRANSFORM_OMIT_LAST_2 = 2,
22
+ BROTLI_TRANSFORM_OMIT_LAST_3 = 3,
23
+ BROTLI_TRANSFORM_OMIT_LAST_4 = 4,
24
+ BROTLI_TRANSFORM_OMIT_LAST_5 = 5,
25
+ BROTLI_TRANSFORM_OMIT_LAST_6 = 6,
26
+ BROTLI_TRANSFORM_OMIT_LAST_7 = 7,
27
+ BROTLI_TRANSFORM_OMIT_LAST_8 = 8,
28
+ BROTLI_TRANSFORM_OMIT_LAST_9 = 9,
29
+ BROTLI_TRANSFORM_UPPERCASE_FIRST = 10,
30
+ BROTLI_TRANSFORM_UPPERCASE_ALL = 11,
31
+ BROTLI_TRANSFORM_OMIT_FIRST_1 = 12,
32
+ BROTLI_TRANSFORM_OMIT_FIRST_2 = 13,
33
+ BROTLI_TRANSFORM_OMIT_FIRST_3 = 14,
34
+ BROTLI_TRANSFORM_OMIT_FIRST_4 = 15,
35
+ BROTLI_TRANSFORM_OMIT_FIRST_5 = 16,
36
+ BROTLI_TRANSFORM_OMIT_FIRST_6 = 17,
37
+ BROTLI_TRANSFORM_OMIT_FIRST_7 = 18,
38
+ BROTLI_TRANSFORM_OMIT_FIRST_8 = 19,
39
+ BROTLI_TRANSFORM_OMIT_FIRST_9 = 20,
40
+ BROTLI_TRANSFORM_SHIFT_FIRST = 21,
41
+ BROTLI_TRANSFORM_SHIFT_ALL = 22,
42
+ BROTLI_NUM_TRANSFORM_TYPES /* Counts transforms, not a transform itself. */
43
+ };
44
+
45
+ #define BROTLI_TRANSFORMS_MAX_CUT_OFF BROTLI_TRANSFORM_OMIT_LAST_9
46
+
47
+ typedef struct BrotliTransforms {
48
+ uint16_t prefix_suffix_size;
49
+ /* Last character must be null, so prefix_suffix_size must be at least 1. */
50
+ const uint8_t* prefix_suffix;
51
+ const uint16_t* prefix_suffix_map;
52
+ uint32_t num_transforms;
53
+ /* Each entry is a [prefix_id, transform, suffix_id] triplet. */
54
+ const uint8_t* transforms;
55
+ /* Shift for BROTLI_TRANSFORM_SHIFT_FIRST and BROTLI_TRANSFORM_SHIFT_ALL,
56
+ must be NULL if and only if no such transforms are present. */
57
+ const uint8_t* params;
58
+ /* Indices of transforms like ["", BROTLI_TRANSFORM_OMIT_LAST_#, ""].
59
+ 0-th element corresponds to ["", BROTLI_TRANSFORM_IDENTITY, ""].
60
+ -1, if cut-off transform does not exist. */
61
+ int16_t cutOffTransforms[BROTLI_TRANSFORMS_MAX_CUT_OFF + 1];
62
+ } BrotliTransforms;
63
+
64
+ /* T is BrotliTransforms*; result is uint8_t. */
65
+ #define BROTLI_TRANSFORM_PREFIX_ID(T, I) ((T)->transforms[((I) * 3) + 0])
66
+ #define BROTLI_TRANSFORM_TYPE(T, I) ((T)->transforms[((I) * 3) + 1])
67
+ #define BROTLI_TRANSFORM_SUFFIX_ID(T, I) ((T)->transforms[((I) * 3) + 2])
68
+
69
+ /* T is BrotliTransforms*; result is const uint8_t*. */
70
+ #define BROTLI_TRANSFORM_PREFIX(T, I) (&(T)->prefix_suffix[ \
71
+ (T)->prefix_suffix_map[BROTLI_TRANSFORM_PREFIX_ID(T, I)]])
72
+ #define BROTLI_TRANSFORM_SUFFIX(T, I) (&(T)->prefix_suffix[ \
73
+ (T)->prefix_suffix_map[BROTLI_TRANSFORM_SUFFIX_ID(T, I)]])
74
+
75
+ BROTLI_COMMON_API const BrotliTransforms* BrotliGetTransforms(void);
76
+
77
+ BROTLI_COMMON_API int BrotliTransformDictionaryWord(
78
+ uint8_t* dst, const uint8_t* word, int len,
79
+ const BrotliTransforms* transforms, int transform_idx);
80
+
81
+ #if defined(__cplusplus) || defined(c_plusplus)
82
+ } /* extern "C" */
83
+ #endif
84
+
85
+ #endif /* BROTLI_COMMON_TRANSFORM_H_ */
@@ -14,6 +14,13 @@
14
14
  BrotliEncoderVersion methods. */
15
15
 
16
16
  /* Semantic version, calculated as (MAJOR << 24) | (MINOR << 12) | PATCH */
17
- #define BROTLI_VERSION 0x1000001
17
+ #define BROTLI_VERSION 0x1000009
18
+
19
+ /* This macro is used by build system to produce Libtool-friendly soname. See
20
+ https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
21
+ */
22
+
23
+ /* ABI version, calculated as (CURRENT << 24) | (REVISION << 12) | AGE */
24
+ #define BROTLI_ABI_VERSION 0x1009000
18
25
 
19
26
  #endif /* BROTLI_COMMON_VERSION_H_ */
@@ -8,13 +8,24 @@
8
8
 
9
9
  #include "./bit_reader.h"
10
10
 
11
+ #include "../common/platform.h"
11
12
  #include <brotli/types.h>
12
- #include "./port.h"
13
13
 
14
14
  #if defined(__cplusplus) || defined(c_plusplus)
15
15
  extern "C" {
16
16
  #endif
17
17
 
18
+ const uint32_t kBrotliBitMask[33] = { 0x00000000,
19
+ 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
20
+ 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
21
+ 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
22
+ 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
23
+ 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
24
+ 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
25
+ 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
26
+ 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
27
+ };
28
+
18
29
  void BrotliInitBitReader(BrotliBitReader* const br) {
19
30
  br->val_ = 0;
20
31
  br->bit_pos_ = sizeof(br->val_) << 3;
@@ -43,6 +54,23 @@ BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {
43
54
  return BROTLI_TRUE;
44
55
  }
45
56
 
57
+ BROTLI_BOOL BrotliSafeReadBits32Slow(BrotliBitReader* const br,
58
+ uint32_t n_bits, uint32_t* val) {
59
+ uint32_t low_val;
60
+ uint32_t high_val;
61
+ BrotliBitReaderState memento;
62
+ BROTLI_DCHECK(n_bits <= 32);
63
+ BROTLI_DCHECK(n_bits > 24);
64
+ BrotliBitReaderSaveState(br, &memento);
65
+ if (!BrotliSafeReadBits(br, 16, &low_val) ||
66
+ !BrotliSafeReadBits(br, n_bits - 16, &high_val)) {
67
+ BrotliBitReaderRestoreState(br, &memento);
68
+ return BROTLI_FALSE;
69
+ }
70
+ *val = low_val | (high_val << 16);
71
+ return BROTLI_TRUE;
72
+ }
73
+
46
74
  #if defined(__cplusplus) || defined(c_plusplus)
47
75
  } /* extern "C" */
48
76
  #endif
@@ -11,45 +11,37 @@
11
11
 
12
12
  #include <string.h> /* memcpy */
13
13
 
14
+ #include "../common/constants.h"
15
+ #include "../common/platform.h"
14
16
  #include <brotli/types.h>
15
- #include "./port.h"
16
17
 
17
18
  #if defined(__cplusplus) || defined(c_plusplus)
18
19
  extern "C" {
19
20
  #endif
20
21
 
21
- #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(reg_t) >> 1)
22
+ #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
22
23
 
23
- static const uint32_t kBitMask[33] = { 0x0000,
24
- 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
25
- 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
26
- 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
27
- 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
28
- 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
29
- 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
30
- 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
31
- 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
32
- };
24
+ BROTLI_INTERNAL extern const uint32_t kBrotliBitMask[33];
33
25
 
34
26
  static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
35
- if (IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
27
+ if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
36
28
  /* Masking with this expression turns to a single
37
29
  "Unsigned Bit Field Extract" UBFX instruction on ARM. */
38
- return ~((0xffffffffU) << n);
30
+ return ~((0xFFFFFFFFu) << n);
39
31
  } else {
40
- return kBitMask[n];
32
+ return kBrotliBitMask[n];
41
33
  }
42
34
  }
43
35
 
44
36
  typedef struct {
45
- reg_t val_; /* pre-fetched bits */
37
+ brotli_reg_t val_; /* pre-fetched bits */
46
38
  uint32_t bit_pos_; /* current bit-reading position in val_ */
47
39
  const uint8_t* next_in; /* the byte we're reading from */
48
40
  size_t avail_in;
49
41
  } BrotliBitReader;
50
42
 
51
43
  typedef struct {
52
- reg_t val_;
44
+ brotli_reg_t val_;
53
45
  uint32_t bit_pos_;
54
46
  const uint8_t* next_in;
55
47
  size_t avail_in;
@@ -58,12 +50,19 @@ typedef struct {
58
50
  /* Initializes the BrotliBitReader fields. */
59
51
  BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br);
60
52
 
61
- /* Ensures that accumulator is not empty. May consume one byte of input.
62
- Returns 0 if data is required but there is no input available.
53
+ /* Ensures that accumulator is not empty.
54
+ May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
55
+ Returns BROTLI_FALSE if data is required but there is no input available.
63
56
  For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
64
57
  reading. */
65
58
  BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
66
59
 
60
+ /* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden
61
+ the main code-path. Never called for RFC brotli streams, required only for
62
+ "large-window" mode and other extensions. */
63
+ BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(
64
+ BrotliBitReader* const br, uint32_t n_bits, uint32_t* val);
65
+
67
66
  static BROTLI_INLINE void BrotliBitReaderSaveState(
68
67
  BrotliBitReader* const from, BrotliBitReaderState* to) {
69
68
  to->val_ = from->val_;
@@ -86,8 +85,11 @@ static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
86
85
  }
87
86
 
88
87
  /* Returns amount of unread bytes the bit reader still has buffered from the
89
- BrotliInput, including whole bytes in br->val_. */
88
+ BrotliInput, including whole bytes in br->val_. Result is capped with
89
+ maximal ring-buffer size (larger number won't be utilized anyway). */
90
90
  static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
91
+ static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
92
+ if (br->avail_in > kCap) return kCap;
91
93
  return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
92
94
  }
93
95
 
@@ -98,82 +100,27 @@ static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(
98
100
  return TO_BROTLI_BOOL(br->avail_in >= num);
99
101
  }
100
102
 
101
- static BROTLI_INLINE uint16_t BrotliLoad16LE(const uint8_t* in) {
102
- if (BROTLI_LITTLE_ENDIAN) {
103
- return *((const uint16_t*)in);
104
- } else if (BROTLI_BIG_ENDIAN) {
105
- uint16_t value = *((const uint16_t*)in);
106
- return (uint16_t)(((value & 0xFFU) << 8) | ((value & 0xFF00U) >> 8));
107
- } else {
108
- return (uint16_t)(in[0] | (in[1] << 8));
109
- }
110
- }
111
-
112
- static BROTLI_INLINE uint32_t BrotliLoad32LE(const uint8_t* in) {
113
- if (BROTLI_LITTLE_ENDIAN) {
114
- return *((const uint32_t*)in);
115
- } else if (BROTLI_BIG_ENDIAN) {
116
- uint32_t value = *((const uint32_t*)in);
117
- return ((value & 0xFFU) << 24) | ((value & 0xFF00U) << 8) |
118
- ((value & 0xFF0000U) >> 8) | ((value & 0xFF000000U) >> 24);
119
- } else {
120
- uint32_t value = (uint32_t)(*(in++));
121
- value |= (uint32_t)(*(in++)) << 8;
122
- value |= (uint32_t)(*(in++)) << 16;
123
- value |= (uint32_t)(*(in++)) << 24;
124
- return value;
125
- }
126
- }
127
-
128
- #if (BROTLI_64_BITS)
129
- static BROTLI_INLINE uint64_t BrotliLoad64LE(const uint8_t* in) {
130
- if (BROTLI_LITTLE_ENDIAN) {
131
- return *((const uint64_t*)in);
132
- } else if (BROTLI_BIG_ENDIAN) {
133
- uint64_t value = *((const uint64_t*)in);
134
- return
135
- ((value & 0xFFU) << 56) |
136
- ((value & 0xFF00U) << 40) |
137
- ((value & 0xFF0000U) << 24) |
138
- ((value & 0xFF000000U) << 8) |
139
- ((value & 0xFF00000000U) >> 8) |
140
- ((value & 0xFF0000000000U) >> 24) |
141
- ((value & 0xFF000000000000U) >> 40) |
142
- ((value & 0xFF00000000000000U) >> 56);
143
- } else {
144
- uint64_t value = (uint64_t)(*(in++));
145
- value |= (uint64_t)(*(in++)) << 8;
146
- value |= (uint64_t)(*(in++)) << 16;
147
- value |= (uint64_t)(*(in++)) << 24;
148
- value |= (uint64_t)(*(in++)) << 32;
149
- value |= (uint64_t)(*(in++)) << 40;
150
- value |= (uint64_t)(*(in++)) << 48;
151
- value |= (uint64_t)(*(in++)) << 56;
152
- return value;
153
- }
154
- }
155
- #endif
156
-
157
- /* Guarantees that there are at least n_bits + 1 bits in accumulator.
103
+ /* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
158
104
  Precondition: accumulator contains at least 1 bit.
159
- n_bits should be in the range [1..24] for regular build. For portable
105
+ |n_bits| should be in the range [1..24] for regular build. For portable
160
106
  non-64-bit little-endian build only 16 bits are safe to request. */
161
107
  static BROTLI_INLINE void BrotliFillBitWindow(
162
108
  BrotliBitReader* const br, uint32_t n_bits) {
163
109
  #if (BROTLI_64_BITS)
164
- if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
110
+ if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
165
111
  if (br->bit_pos_ >= 56) {
166
112
  br->val_ >>= 56;
167
113
  br->bit_pos_ ^= 56; /* here same as -= 56 because of the if condition */
168
- br->val_ |= BrotliLoad64LE(br->next_in) << 8;
114
+ br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8;
169
115
  br->avail_in -= 7;
170
116
  br->next_in += 7;
171
117
  }
172
- } else if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 16)) {
118
+ } else if (
119
+ !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) {
173
120
  if (br->bit_pos_ >= 48) {
174
121
  br->val_ >>= 48;
175
122
  br->bit_pos_ ^= 48; /* here same as -= 48 because of the if condition */
176
- br->val_ |= BrotliLoad64LE(br->next_in) << 16;
123
+ br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16;
177
124
  br->avail_in -= 6;
178
125
  br->next_in += 6;
179
126
  }
@@ -181,17 +128,17 @@ static BROTLI_INLINE void BrotliFillBitWindow(
181
128
  if (br->bit_pos_ >= 32) {
182
129
  br->val_ >>= 32;
183
130
  br->bit_pos_ ^= 32; /* here same as -= 32 because of the if condition */
184
- br->val_ |= ((uint64_t)BrotliLoad32LE(br->next_in)) << 32;
131
+ br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32;
185
132
  br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
186
133
  br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
187
134
  }
188
135
  }
189
136
  #else
190
- if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
137
+ if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
191
138
  if (br->bit_pos_ >= 24) {
192
139
  br->val_ >>= 24;
193
140
  br->bit_pos_ ^= 24; /* here same as -= 24 because of the if condition */
194
- br->val_ |= BrotliLoad32LE(br->next_in) << 8;
141
+ br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8;
195
142
  br->avail_in -= 3;
196
143
  br->next_in += 3;
197
144
  }
@@ -199,7 +146,7 @@ static BROTLI_INLINE void BrotliFillBitWindow(
199
146
  if (br->bit_pos_ >= 16) {
200
147
  br->val_ >>= 16;
201
148
  br->bit_pos_ ^= 16; /* here same as -= 16 because of the if condition */
202
- br->val_ |= ((uint32_t)BrotliLoad16LE(br->next_in)) << 16;
149
+ br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16;
203
150
  br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
204
151
  br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
205
152
  }
@@ -213,7 +160,8 @@ static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
213
160
  BrotliFillBitWindow(br, 17);
214
161
  }
215
162
 
216
- /* Pulls one byte of input to accumulator. */
163
+ /* Tries to pull one byte of input to accumulator.
164
+ Returns BROTLI_FALSE if there is no input available. */
217
165
  static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
218
166
  if (br->avail_in == 0) {
219
167
  return BROTLI_FALSE;
@@ -232,7 +180,8 @@ static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
232
180
 
233
181
  /* Returns currently available bits.
234
182
  The number of valid bits could be calculated by BrotliGetAvailableBits. */
235
- static BROTLI_INLINE reg_t BrotliGetBitsUnmasked(BrotliBitReader* const br) {
183
+ static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
184
+ BrotliBitReader* const br) {
236
185
  return br->val_ >> br->bit_pos_;
237
186
  }
238
187
 
@@ -244,15 +193,16 @@ static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
244
193
  return (uint32_t)BrotliGetBitsUnmasked(br);
245
194
  }
246
195
 
247
- /* Returns the specified number of bits from |br| without advancing bit pos. */
196
+ /* Returns the specified number of bits from |br| without advancing bit
197
+ position. */
248
198
  static BROTLI_INLINE uint32_t BrotliGetBits(
249
199
  BrotliBitReader* const br, uint32_t n_bits) {
250
200
  BrotliFillBitWindow(br, n_bits);
251
201
  return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
252
202
  }
253
203
 
254
- /* Tries to peek the specified amount of bits. Returns 0, if there is not
255
- enough input. */
204
+ /* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there
205
+ is not enough input. */
256
206
  static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
257
207
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
258
208
  while (BrotliGetAvailableBits(br) < n_bits) {
@@ -264,7 +214,7 @@ static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
264
214
  return BROTLI_TRUE;
265
215
  }
266
216
 
267
- /* Advances the bit pos by n_bits. */
217
+ /* Advances the bit pos by |n_bits|. */
268
218
  static BROTLI_INLINE void BrotliDropBits(
269
219
  BrotliBitReader* const br, uint32_t n_bits) {
270
220
  br->bit_pos_ += n_bits;
@@ -284,19 +234,21 @@ static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
284
234
  }
285
235
 
286
236
  /* Reads the specified number of bits from |br| and advances the bit pos.
287
- Precondition: accumulator MUST contain at least n_bits. */
237
+ Precondition: accumulator MUST contain at least |n_bits|. */
288
238
  static BROTLI_INLINE void BrotliTakeBits(
289
239
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
290
240
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
291
- BROTLI_LOG(("[BrotliReadBits] %d %d %d val: %6x\n",
292
- (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val));
241
+ BROTLI_LOG(("[BrotliTakeBits] %d %d %d val: %6x\n",
242
+ (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val));
293
243
  BrotliDropBits(br, n_bits);
294
244
  }
295
245
 
296
246
  /* Reads the specified number of bits from |br| and advances the bit pos.
297
- Assumes that there is enough input to perform BrotliFillBitWindow. */
298
- static BROTLI_INLINE uint32_t BrotliReadBits(
247
+ Assumes that there is enough input to perform BrotliFillBitWindow.
248
+ Up to 24 bits are allowed to be requested from this method. */
249
+ static BROTLI_INLINE uint32_t BrotliReadBits24(
299
250
  BrotliBitReader* const br, uint32_t n_bits) {
251
+ BROTLI_DCHECK(n_bits <= 24);
300
252
  if (BROTLI_64_BITS || (n_bits <= 16)) {
301
253
  uint32_t val;
302
254
  BrotliFillBitWindow(br, n_bits);
@@ -313,10 +265,32 @@ static BROTLI_INLINE uint32_t BrotliReadBits(
313
265
  }
314
266
  }
315
267
 
316
- /* Tries to read the specified amount of bits. Returns 0, if there is not
317
- enough input. n_bits MUST be positive. */
268
+ /* Same as BrotliReadBits24, but allows reading up to 32 bits. */
269
+ static BROTLI_INLINE uint32_t BrotliReadBits32(
270
+ BrotliBitReader* const br, uint32_t n_bits) {
271
+ BROTLI_DCHECK(n_bits <= 32);
272
+ if (BROTLI_64_BITS || (n_bits <= 16)) {
273
+ uint32_t val;
274
+ BrotliFillBitWindow(br, n_bits);
275
+ BrotliTakeBits(br, n_bits, &val);
276
+ return val;
277
+ } else {
278
+ uint32_t low_val;
279
+ uint32_t high_val;
280
+ BrotliFillBitWindow(br, 16);
281
+ BrotliTakeBits(br, 16, &low_val);
282
+ BrotliFillBitWindow(br, 16);
283
+ BrotliTakeBits(br, n_bits - 16, &high_val);
284
+ return low_val | (high_val << 16);
285
+ }
286
+ }
287
+
288
+ /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
289
+ is not enough input. |n_bits| MUST be positive.
290
+ Up to 24 bits are allowed to be requested from this method. */
318
291
  static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
319
292
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
293
+ BROTLI_DCHECK(n_bits <= 24);
320
294
  while (BrotliGetAvailableBits(br) < n_bits) {
321
295
  if (!BrotliPullByte(br)) {
322
296
  return BROTLI_FALSE;
@@ -326,6 +300,23 @@ static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
326
300
  return BROTLI_TRUE;
327
301
  }
328
302
 
303
+ /* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */
304
+ static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(
305
+ BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
306
+ BROTLI_DCHECK(n_bits <= 32);
307
+ if (BROTLI_64_BITS || (n_bits <= 24)) {
308
+ while (BrotliGetAvailableBits(br) < n_bits) {
309
+ if (!BrotliPullByte(br)) {
310
+ return BROTLI_FALSE;
311
+ }
312
+ }
313
+ BrotliTakeBits(br, n_bits, val);
314
+ return BROTLI_TRUE;
315
+ } else {
316
+ return BrotliSafeReadBits32Slow(br, n_bits, val);
317
+ }
318
+ }
319
+
329
320
  /* Advances the bit reader position to the next byte boundary and verifies
330
321
  that any skipped bits are set to zero. */
331
322
  static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
@@ -338,7 +329,7 @@ static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
338
329
  }
339
330
 
340
331
  /* Copies remaining input bytes stored in the bit reader to the output. Value
341
- num may not be larger than BrotliGetRemainingBytes. The bit reader must be
332
+ |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
342
333
  warmed up again after this. */
343
334
  static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
344
335
  BrotliBitReader* br, size_t num) {