brotli 0.1.0 → 0.1.1

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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +1 -1
  3. data/.travis.yml +2 -1
  4. data/README.md +1 -1
  5. data/Rakefile +1 -1
  6. data/ext/brotli/brotli.cc +1 -1
  7. data/ext/brotli/extconf.rb +72 -14
  8. data/lib/brotli/version.rb +1 -1
  9. data/vendor/brotli/LICENSE +19 -202
  10. data/vendor/brotli/dec/Makefile +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +23 -30
  12. data/vendor/brotli/dec/bit_reader.h +270 -141
  13. data/vendor/brotli/dec/context.h +3 -12
  14. data/vendor/brotli/dec/decode.c +1813 -1048
  15. data/vendor/brotli/dec/decode.h +22 -16
  16. data/vendor/brotli/dec/dictionary.c +9466 -0
  17. data/vendor/brotli/dec/dictionary.h +6 -9461
  18. data/vendor/brotli/dec/huffman.c +104 -71
  19. data/vendor/brotli/dec/huffman.h +19 -28
  20. data/vendor/brotli/dec/port.h +124 -32
  21. data/vendor/brotli/dec/prefix.h +4 -13
  22. data/vendor/brotli/dec/state.c +93 -56
  23. data/vendor/brotli/dec/state.h +124 -53
  24. data/vendor/brotli/dec/streams.c +14 -11
  25. data/vendor/brotli/dec/streams.h +6 -11
  26. data/vendor/brotli/dec/transform.h +2 -11
  27. data/vendor/brotli/dec/types.h +21 -19
  28. data/vendor/brotli/enc/Makefile +4 -1
  29. data/vendor/brotli/enc/backward_references.cc +87 -94
  30. data/vendor/brotli/enc/backward_references.h +8 -18
  31. data/vendor/brotli/enc/bit_cost.h +11 -19
  32. data/vendor/brotli/enc/block_splitter.cc +43 -48
  33. data/vendor/brotli/enc/block_splitter.h +7 -16
  34. data/vendor/brotli/enc/brotli_bit_stream.cc +48 -50
  35. data/vendor/brotli/enc/brotli_bit_stream.h +7 -16
  36. data/vendor/brotli/enc/cluster.h +24 -25
  37. data/vendor/brotli/enc/command.h +34 -41
  38. data/vendor/brotli/enc/context.h +11 -18
  39. data/vendor/brotli/enc/dictionary.cc +9466 -0
  40. data/vendor/brotli/enc/dictionary.h +20 -9464
  41. data/vendor/brotli/enc/dictionary_hash.h +7 -15
  42. data/vendor/brotli/enc/encode.cc +80 -148
  43. data/vendor/brotli/enc/encode.h +19 -29
  44. data/vendor/brotli/enc/encode_parallel.cc +35 -108
  45. data/vendor/brotli/enc/encode_parallel.h +7 -16
  46. data/vendor/brotli/enc/entropy_encode.cc +33 -42
  47. data/vendor/brotli/enc/entropy_encode.h +8 -16
  48. data/vendor/brotli/enc/fast_log.h +8 -15
  49. data/vendor/brotli/enc/find_match_length.h +7 -17
  50. data/vendor/brotli/enc/hash.h +130 -150
  51. data/vendor/brotli/enc/histogram.cc +7 -16
  52. data/vendor/brotli/enc/histogram.h +11 -17
  53. data/vendor/brotli/enc/literal_cost.cc +28 -35
  54. data/vendor/brotli/enc/literal_cost.h +9 -23
  55. data/vendor/brotli/enc/metablock.cc +18 -26
  56. data/vendor/brotli/enc/metablock.h +6 -14
  57. data/vendor/brotli/enc/port.h +14 -14
  58. data/vendor/brotli/enc/prefix.h +11 -18
  59. data/vendor/brotli/enc/ringbuffer.h +18 -27
  60. data/vendor/brotli/enc/static_dict.cc +7 -1
  61. data/vendor/brotli/enc/static_dict.h +7 -15
  62. data/vendor/brotli/enc/static_dict_lut.h +7 -15
  63. data/vendor/brotli/enc/streams.cc +15 -28
  64. data/vendor/brotli/enc/streams.h +27 -35
  65. data/vendor/brotli/enc/transform.h +9 -16
  66. data/vendor/brotli/enc/types.h +27 -0
  67. data/vendor/brotli/enc/utf8_util.cc +82 -0
  68. data/vendor/brotli/enc/utf8_util.h +25 -0
  69. data/vendor/brotli/enc/write_bits.h +11 -18
  70. metadata +7 -2
@@ -1,16 +1,7 @@
1
1
  /* Copyright 2013 Google Inc. All Rights Reserved.
2
2
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
14
5
  */
15
6
 
16
7
  /* Bit reading helpers */
@@ -18,126 +9,179 @@
18
9
  #ifndef BROTLI_DEC_BIT_READER_H_
19
10
  #define BROTLI_DEC_BIT_READER_H_
20
11
 
12
+ #include <stdio.h>
21
13
  #include <string.h>
22
14
  #include "./port.h"
23
- #include "./streams.h"
24
15
  #include "./types.h"
25
16
 
26
17
  #if defined(__cplusplus) || defined(c_plusplus)
27
18
  extern "C" {
28
19
  #endif
29
20
 
30
- #define BROTLI_MAX_NUM_BIT_READ 25
31
- #define BROTLI_READ_SIZE 1024
32
- #define BROTLI_IMPLICIT_ZEROES 128
33
- #define BROTLI_IBUF_SIZE (BROTLI_READ_SIZE + BROTLI_IMPLICIT_ZEROES)
34
- #define BROTLI_IBUF_MASK (BROTLI_READ_SIZE - 1)
21
+ #if (BROTLI_64_BITS)
22
+ #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 4
23
+ typedef uint64_t reg_t;
24
+ #else
25
+ #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 2
26
+ typedef uint32_t reg_t;
27
+ #endif
28
+
29
+ static const uint32_t kBitMask[33] = { 0x0000,
30
+ 0x00000001, 0x00000003, 0x00000007, 0x0000000F,
31
+ 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
32
+ 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
33
+ 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
34
+ 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
35
+ 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
36
+ 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
37
+ 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
38
+ };
35
39
 
36
- /* Masking with this expression turns to a single "Unsigned Bit Field Extract"
37
- UBFX instruction on ARM. */
38
- static BROTLI_INLINE uint32_t BitMask(int n) { return ~((0xffffffff) << n); }
40
+ static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
41
+ if (IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
42
+ /* Masking with this expression turns to a single
43
+ "Unsigned Bit Field Extract" UBFX instruction on ARM. */
44
+ return ~((0xffffffffU) << n);
45
+ } else {
46
+ return kBitMask[n];
47
+ }
48
+ }
39
49
 
40
50
  typedef struct {
41
- #if (BROTLI_64_BITS_LITTLE_ENDIAN)
42
- uint64_t val_; /* pre-fetched bits */
43
- #else
44
- uint32_t val_; /* pre-fetched bits */
45
- #endif
51
+ reg_t val_; /* pre-fetched bits */
46
52
  uint32_t bit_pos_; /* current bit-reading position in val_ */
47
- uint8_t* next_in; /* the byte we're reading from */
48
- uint32_t avail_in;
49
- int eos_; /* input stream is finished */
50
- BrotliInput input_; /* input callback */
51
-
52
- /* Input byte buffer, consist of a ringbuffer and a "slack" region where */
53
- /* bytes from the start of the ringbuffer are copied. */
54
- uint8_t buf_[BROTLI_IBUF_SIZE];
53
+ const uint8_t* next_in; /* the byte we're reading from */
54
+ size_t avail_in;
55
55
  } BrotliBitReader;
56
56
 
57
- /* Initializes the bitreader fields. After this, BrotliReadInput then
58
- BrotliWarmupBitReader must be used. */
59
- void BrotliInitBitReader(BrotliBitReader* const br, BrotliInput input);
57
+ typedef struct {
58
+ reg_t val_;
59
+ uint32_t bit_pos_;
60
+ const uint8_t* next_in;
61
+ size_t avail_in;
62
+ } BrotliBitReaderState;
60
63
 
61
- /* Initializes bit reading and bit position with the first input data available.
62
- Requires that there is enough input available (BrotliCheckInputAmount). */
63
- void BrotliWarmupBitReader(BrotliBitReader* const br);
64
+ /* Initializes the bitreader fields. */
65
+ void BrotliInitBitReader(BrotliBitReader* const br);
64
66
 
65
- /* Pulls data from the input to the the read buffer.
67
+ /* Ensures that accumulator is not empty. May consume one byte of input.
68
+ Returns 0 if data is required but there is no input available.
69
+ For BROTLI_BUILD_PORTABLE this function also prepares bit reader for aligned
70
+ reading. */
71
+ int BrotliWarmupBitReader(BrotliBitReader* const br);
66
72
 
67
- Returns 0 if one of:
68
- - the input callback returned an error, or
69
- - there is no more input and the position is past the end of the stream.
70
- - finish is false and less than BROTLI_READ_SIZE are available - a next call
71
- when more data is available makes it continue including the partially read
72
- data
73
+ static BROTLI_INLINE void BrotliBitReaderSaveState(
74
+ BrotliBitReader* const from, BrotliBitReaderState* to) {
75
+ to->val_ = from->val_;
76
+ to->bit_pos_ = from->bit_pos_;
77
+ to->next_in = from->next_in;
78
+ to->avail_in = from->avail_in;
79
+ }
73
80
 
74
- If finish is true and the end of the stream is reached,
75
- BROTLI_IMPLICIT_ZEROES additional zero bytes are copied to the ringbuffer.
76
- */
77
- static BROTLI_INLINE int BrotliReadInput(
78
- BrotliBitReader* const br, int finish) {
79
- if (PREDICT_FALSE(br->eos_)) {
80
- return 0;
81
- } else {
82
- size_t i;
83
- int bytes_read;
84
- if (br->next_in != br->buf_) {
85
- for (i = 0; i < br->avail_in; i++) {
86
- br->buf_[i] = br->next_in[i];
87
- }
88
- br->next_in = br->buf_;
89
- }
90
- bytes_read = BrotliRead(br->input_, br->next_in + br->avail_in,
91
- (size_t)(BROTLI_READ_SIZE - br->avail_in));
92
- if (bytes_read < 0) {
93
- return 0;
94
- }
95
- br->avail_in += (uint32_t)bytes_read;
96
- if (br->avail_in < BROTLI_READ_SIZE) {
97
- if (!finish) {
98
- return 0;
99
- }
100
- br->eos_ = 1;
101
- /* Store BROTLI_IMPLICIT_ZEROES bytes of zero after the stream end. */
102
- memset(br->next_in + br->avail_in, 0, BROTLI_IMPLICIT_ZEROES);
103
- br->avail_in += BROTLI_IMPLICIT_ZEROES;
104
- }
105
- return 1;
106
- }
81
+ static BROTLI_INLINE void BrotliBitReaderRestoreState(
82
+ BrotliBitReader* const to, BrotliBitReaderState* from) {
83
+ to->val_ = from->val_;
84
+ to->bit_pos_ = from->bit_pos_;
85
+ to->next_in = from->next_in;
86
+ to->avail_in = from->avail_in;
87
+ }
88
+
89
+ static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
90
+ const BrotliBitReader* br) {
91
+ return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
107
92
  }
108
93
 
109
94
  /* Returns amount of unread bytes the bit reader still has buffered from the
110
95
  BrotliInput, including whole bytes in br->val_. */
111
96
  static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
112
- return br->avail_in + sizeof(br->val_) - (br->bit_pos_ >> 3);
97
+ return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
113
98
  }
114
99
 
115
100
  /* Checks if there is at least num bytes left in the input ringbuffer (excluding
116
- the bits remaining in br->val_). The maximum value for num is
117
- BROTLI_IMPLICIT_ZEROES bytes. */
101
+ the bits remaining in br->val_). */
118
102
  static BROTLI_INLINE int BrotliCheckInputAmount(
119
103
  BrotliBitReader* const br, size_t num) {
120
104
  return br->avail_in >= num;
121
105
  }
122
106
 
123
- /* Guarantees that there are at least n_bits in the buffer.
124
- n_bits should be in the range [1..24] */
107
+ static BROTLI_INLINE uint16_t BrotliLoad16LE(const uint8_t* in) {
108
+ if (BROTLI_LITTLE_ENDIAN) {
109
+ return *((const uint16_t*)in);
110
+ } else if (BROTLI_BIG_ENDIAN) {
111
+ uint16_t value = *((const uint16_t*)in);
112
+ return (uint16_t)(
113
+ ((value & 0xFFU) << 8) |
114
+ ((value & 0xFF00U) >> 8));
115
+ } else {
116
+ return (uint16_t)(in[0] | (in[1] << 8));
117
+ }
118
+ }
119
+
120
+ static BROTLI_INLINE uint32_t BrotliLoad32LE(const uint8_t* in) {
121
+ if (BROTLI_LITTLE_ENDIAN) {
122
+ return *((const uint32_t*)in);
123
+ } else if (BROTLI_BIG_ENDIAN) {
124
+ uint32_t value = *((const uint32_t*)in);
125
+ return ((value & 0xFFU) << 24) | ((value & 0xFF00U) << 8) |
126
+ ((value & 0xFF0000U) >> 8) | ((value & 0xFF000000U) >> 24);
127
+ } else {
128
+ uint32_t value = (uint32_t)(*(in++));
129
+ value |= (uint32_t)(*(in++)) << 8;
130
+ value |= (uint32_t)(*(in++)) << 16;
131
+ value |= (uint32_t)(*(in++)) << 24;
132
+ return value;
133
+ }
134
+ }
135
+
136
+ #if (BROTLI_64_BITS)
137
+ static BROTLI_INLINE uint64_t BrotliLoad64LE(const uint8_t* in) {
138
+ if (BROTLI_LITTLE_ENDIAN) {
139
+ return *((const uint64_t*)in);
140
+ } else if (BROTLI_BIG_ENDIAN) {
141
+ uint64_t value = *((const uint64_t*)in);
142
+ return
143
+ ((value & 0xFFU) << 56) |
144
+ ((value & 0xFF00U) << 40) |
145
+ ((value & 0xFF0000U) << 24) |
146
+ ((value & 0xFF000000U) << 8) |
147
+ ((value & 0xFF00000000U) >> 8) |
148
+ ((value & 0xFF0000000000U) >> 24) |
149
+ ((value & 0xFF000000000000U) >> 40) |
150
+ ((value & 0xFF00000000000000U) >> 56);
151
+ } else {
152
+ uint64_t value = (uint64_t)(*(in++));
153
+ value |= (uint64_t)(*(in++)) << 8;
154
+ value |= (uint64_t)(*(in++)) << 16;
155
+ value |= (uint64_t)(*(in++)) << 24;
156
+ value |= (uint64_t)(*(in++)) << 32;
157
+ value |= (uint64_t)(*(in++)) << 40;
158
+ value |= (uint64_t)(*(in++)) << 48;
159
+ value |= (uint64_t)(*(in++)) << 56;
160
+ return value;
161
+ }
162
+ }
163
+ #endif
164
+
165
+ /* Guarantees that there are at least n_bits + 1 bits in accumulator.
166
+ Precondition: accumulator contains at least 1 bit.
167
+ n_bits should be in the range [1..24] for regular build. For portable
168
+ non-64-bit little endian build only 16 bits are safe to request. */
125
169
  static BROTLI_INLINE void BrotliFillBitWindow(
126
- BrotliBitReader* const br, int n_bits) {
127
- #if (BROTLI_64_BITS_LITTLE_ENDIAN)
128
- if (IS_CONSTANT(n_bits) && n_bits <= 8) {
170
+ BrotliBitReader* const br, uint32_t n_bits) {
171
+ #if (BROTLI_64_BITS)
172
+ if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
129
173
  if (br->bit_pos_ >= 56) {
130
174
  br->val_ >>= 56;
131
175
  br->bit_pos_ ^= 56; /* here same as -= 56 because of the if condition */
132
- br->val_ |= (*(const uint64_t*)(br->next_in)) << 8;
176
+ br->val_ |= BrotliLoad64LE(br->next_in) << 8;
133
177
  br->avail_in -= 7;
134
178
  br->next_in += 7;
135
179
  }
136
- } else if (IS_CONSTANT(n_bits) && n_bits <= 16) {
180
+ } else if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 16)) {
137
181
  if (br->bit_pos_ >= 48) {
138
182
  br->val_ >>= 48;
139
183
  br->bit_pos_ ^= 48; /* here same as -= 48 because of the if condition */
140
- br->val_ |= (*(const uint64_t*)(br->next_in)) << 16;
184
+ br->val_ |= BrotliLoad64LE(br->next_in) << 16;
141
185
  br->avail_in -= 6;
142
186
  br->next_in += 6;
143
187
  }
@@ -145,17 +189,17 @@ static BROTLI_INLINE void BrotliFillBitWindow(
145
189
  if (br->bit_pos_ >= 32) {
146
190
  br->val_ >>= 32;
147
191
  br->bit_pos_ ^= 32; /* here same as -= 32 because of the if condition */
148
- br->val_ |= ((uint64_t)(*(const uint32_t*)(br->next_in))) << 32;
149
- br->avail_in -= 4;
150
- br->next_in += 4;
192
+ br->val_ |= ((uint64_t)BrotliLoad32LE(br->next_in)) << 32;
193
+ br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
194
+ br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
151
195
  }
152
196
  }
153
- #elif (BROTLI_LITTLE_ENDIAN)
154
- if (IS_CONSTANT(n_bits) && n_bits <= 8) {
197
+ #else
198
+ if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
155
199
  if (br->bit_pos_ >= 24) {
156
200
  br->val_ >>= 24;
157
201
  br->bit_pos_ ^= 24; /* here same as -= 24 because of the if condition */
158
- br->val_ |= (*(const uint32_t*)(br->next_in)) << 8;
202
+ br->val_ |= BrotliLoad32LE(br->next_in) << 8;
159
203
  br->avail_in -= 3;
160
204
  br->next_in += 3;
161
205
  }
@@ -163,90 +207,175 @@ static BROTLI_INLINE void BrotliFillBitWindow(
163
207
  if (br->bit_pos_ >= 16) {
164
208
  br->val_ >>= 16;
165
209
  br->bit_pos_ ^= 16; /* here same as -= 16 because of the if condition */
166
- br->val_ |= ((uint32_t)(*(const uint16_t*)(br->next_in))) << 16;
167
- br->avail_in -= 2;
168
- br->next_in += 2;
169
- }
170
- if (!IS_CONSTANT(n_bits) || (n_bits > 16)) {
171
- if (br->bit_pos_ >= 8) {
172
- br->val_ >>= 8;
173
- br->bit_pos_ ^= 8; /* here same as -= 8 because of the if condition */
174
- br->val_ |= ((uint32_t)*br->next_in) << 24;
175
- --br->avail_in;
176
- ++br->next_in;
177
- }
210
+ br->val_ |= ((uint32_t)BrotliLoad16LE(br->next_in)) << 16;
211
+ br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
212
+ br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
178
213
  }
179
214
  }
215
+ #endif
216
+ }
217
+
218
+ /* Mosltly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
219
+ more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
220
+ static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
221
+ BrotliFillBitWindow(br, 17);
222
+ }
223
+
224
+ /* Pulls one byte of input to accumulator. */
225
+ static BROTLI_INLINE int BrotliPullByte(BrotliBitReader* const br) {
226
+ if (br->avail_in == 0) {
227
+ return 0;
228
+ }
229
+ br->val_ >>= 8;
230
+ #if (BROTLI_64_BITS)
231
+ br->val_ |= ((uint64_t)*br->next_in) << 56;
180
232
  #else
181
- while (br->bit_pos_ >= 8) {
182
- br->val_ >>= 8;
183
233
  br->val_ |= ((uint32_t)*br->next_in) << 24;
184
- br->bit_pos_ -= 8;
185
- --br->avail_in;
186
- ++br->next_in;
187
- }
188
234
  #endif
235
+ br->bit_pos_ -= 8;
236
+ --br->avail_in;
237
+ ++br->next_in;
238
+ return 1;
189
239
  }
190
240
 
191
- /* Like BrotliGetBits, but does not mask the result, it is only guaranteed
192
- that it has minimum n_bits. */
193
- static BROTLI_INLINE uint32_t BrotliGetBitsUnmasked(
194
- BrotliBitReader* const br, int n_bits) {
195
- BrotliFillBitWindow(br, n_bits);
196
- return (uint32_t)(br->val_ >> br->bit_pos_);
241
+ /* Returns currently available bits.
242
+ The number of valid bits could be calclulated by BrotliGetAvailableBits. */
243
+ static BROTLI_INLINE reg_t BrotliGetBitsUnmasked(BrotliBitReader* const br) {
244
+ return br->val_ >> br->bit_pos_;
245
+ }
246
+
247
+ /* Like BrotliGetBits, but does not mask the result.
248
+ The result contains at least 16 valid bits. */
249
+ static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
250
+ BrotliBitReader* const br) {
251
+ BrotliFillBitWindow(br, 16);
252
+ return (uint32_t)BrotliGetBitsUnmasked(br);
197
253
  }
198
254
 
199
255
  /* Returns the specified number of bits from br without advancing bit pos. */
200
256
  static BROTLI_INLINE uint32_t BrotliGetBits(
201
- BrotliBitReader* const br, int n_bits) {
257
+ BrotliBitReader* const br, uint32_t n_bits) {
202
258
  BrotliFillBitWindow(br, n_bits);
203
- return (uint32_t)(br->val_ >> br->bit_pos_) & BitMask(n_bits);
259
+ return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
260
+ }
261
+
262
+ /* Tries to peek the specified amount of bits. Returns 0, if there is not
263
+ enough input. */
264
+ static BROTLI_INLINE int BrotliSafeGetBits(
265
+ BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
266
+ while (BrotliGetAvailableBits(br) < n_bits) {
267
+ if (!BrotliPullByte(br)) {
268
+ return 0;
269
+ }
270
+ }
271
+ *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
272
+ return 1;
204
273
  }
205
274
 
206
275
  /* Advances the bit pos by n_bits. */
207
276
  static BROTLI_INLINE void BrotliDropBits(
208
- BrotliBitReader* const br, int n_bits) {
209
- br->bit_pos_ += (uint32_t)n_bits;
277
+ BrotliBitReader* const br, uint32_t n_bits) {
278
+ br->bit_pos_ += n_bits;
210
279
  }
211
280
 
212
- /* Reads the specified number of bits from br and advances the bit pos. */
213
- static BROTLI_INLINE uint32_t BrotliReadBits(
214
- BrotliBitReader* const br, int n_bits) {
215
- uint32_t val;
216
- BrotliFillBitWindow(br, n_bits);
217
- val = (uint32_t)(br->val_ >> br->bit_pos_) & BitMask(n_bits);
281
+ static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
282
+ uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
283
+ uint32_t unused_bits = unused_bytes << 3;
284
+ br->avail_in += unused_bytes;
285
+ br->next_in -= unused_bytes;
286
+ br->val_ <<= unused_bits;
287
+ br->bit_pos_ += unused_bits;
288
+ }
289
+
290
+ /* Reads the specified number of bits from br and advances the bit pos.
291
+ Precondition: accumulator MUST contain at least n_bits. */
292
+ static BROTLI_INLINE void BrotliTakeBits(
293
+ BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
294
+ *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
218
295
  #ifdef BROTLI_DECODE_DEBUG
219
296
  printf("[BrotliReadBits] %d %d %d val: %6x\n",
220
- (int)br->avail_in, (int)br->bit_pos_, n_bits, val);
297
+ (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val);
221
298
  #endif
222
- br->bit_pos_ += (uint32_t)n_bits;
223
- return val;
299
+ BrotliDropBits(br, n_bits);
300
+ }
301
+
302
+ /* Reads the specified number of bits from br and advances the bit pos.
303
+ Assumes that there is enough input to perform BrotliFillBitWindow. */
304
+ static BROTLI_INLINE uint32_t BrotliReadBits(
305
+ BrotliBitReader* const br, uint32_t n_bits) {
306
+ if (BROTLI_64_BITS || (n_bits <= 16)) {
307
+ uint32_t val;
308
+ BrotliFillBitWindow(br, n_bits);
309
+ BrotliTakeBits(br, n_bits, &val);
310
+ return val;
311
+ } else {
312
+ uint32_t low_val;
313
+ uint32_t high_val;
314
+ BrotliFillBitWindow(br, 16);
315
+ BrotliTakeBits(br, 16, &low_val);
316
+ BrotliFillBitWindow(br, 8);
317
+ BrotliTakeBits(br, n_bits - 16, &high_val);
318
+ return low_val | (high_val << 16);
319
+ }
320
+ }
321
+
322
+ /* Tries to read the specified amount of bits. Returns 0, if there is not
323
+ enough input. n_bits MUST be positive. */
324
+ static BROTLI_INLINE int BrotliSafeReadBits(
325
+ BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
326
+ while (BrotliGetAvailableBits(br) < n_bits) {
327
+ if (!BrotliPullByte(br)) {
328
+ return 0;
329
+ }
330
+ }
331
+ BrotliTakeBits(br, n_bits, val);
332
+ return 1;
224
333
  }
225
334
 
226
335
  /* Advances the bit reader position to the next byte boundary and verifies
227
336
  that any skipped bits are set to zero. */
228
337
  static BROTLI_INLINE int BrotliJumpToByteBoundary(BrotliBitReader* br) {
229
- uint32_t new_bit_pos = (br->bit_pos_ + 7) & (uint32_t)(~7UL);
230
- uint32_t pad_bits = BrotliReadBits(br, (int)(new_bit_pos - br->bit_pos_));
338
+ uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
339
+ uint32_t pad_bits = 0;
340
+ if (pad_bits_count != 0) {
341
+ BrotliTakeBits(br, pad_bits_count, &pad_bits);
342
+ }
231
343
  return pad_bits == 0;
232
344
  }
233
345
 
346
+ /* Peeks a byte at specified offset.
347
+ Precondition: bit reader is parked to a byte boundary.
348
+ Returns -1 if operation is not feasible. */
349
+ static BROTLI_INLINE int BrotliPeekByte(BrotliBitReader* br, size_t offset) {
350
+ uint32_t available_bits = BrotliGetAvailableBits(br);
351
+ size_t bytes_left = available_bits >> 3;
352
+ if (available_bits & 7) {
353
+ return -1;
354
+ }
355
+ if (offset < bytes_left) {
356
+ return (BrotliGetBitsUnmasked(br) >> (unsigned)(offset << 3)) & 0xFF;
357
+ }
358
+ offset -= bytes_left;
359
+ if (offset < br->avail_in) {
360
+ return br->next_in[offset];
361
+ }
362
+ return -1;
363
+ }
364
+
234
365
  /* Copies remaining input bytes stored in the bit reader to the output. Value
235
366
  num may not be larger than BrotliGetRemainingBytes. The bit reader must be
236
367
  warmed up again after this. */
237
368
  static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
238
369
  BrotliBitReader* br, size_t num) {
239
- while (br->bit_pos_ + 8 <= (BROTLI_64_BITS_LITTLE_ENDIAN ? 64 : 32)
240
- && num > 0) {
241
- *dest = (uint8_t)(br->val_ >> br->bit_pos_);
242
- br->bit_pos_ += 8;
370
+ while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
371
+ *dest = (uint8_t)BrotliGetBitsUnmasked(br);
372
+ BrotliDropBits(br, 8);
243
373
  ++dest;
244
374
  --num;
245
375
  }
246
376
  memcpy(dest, br->next_in, num);
247
- br->avail_in -= (uint32_t)num;
377
+ br->avail_in -= num;
248
378
  br->next_in += num;
249
- br->bit_pos_ = 0;
250
379
  }
251
380
 
252
381
  #if defined(__cplusplus) || defined(c_plusplus)
@@ -1,16 +1,7 @@
1
1
  /* Copyright 2013 Google Inc. All Rights Reserved.
2
2
 
3
- Licensed under the Apache License, Version 2.0 (the "License");
4
- you may not use this file except in compliance with the License.
5
- You may obtain a copy of the License at
6
-
7
- http://www.apache.org/licenses/LICENSE-2.0
8
-
9
- Unless required by applicable law or agreed to in writing, software
10
- distributed under the License is distributed on an "AS IS" BASIS,
11
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- See the License for the specific language governing permissions and
13
- limitations under the License.
3
+ Distributed under MIT license.
4
+ See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
14
5
  */
15
6
 
16
7
  /* Lookup table to map the previous two bytes to a context id.
@@ -22,7 +13,7 @@
22
13
  CONTEXT_SIGNED: second-order context model tuned for signed integers.
23
14
 
24
15
  The context id for the UTF8 context model is calculated as follows. If p1
25
- and p2 are the previous two bytes, we calcualte the context as
16
+ and p2 are the previous two bytes, we calculate the context as
26
17
 
27
18
  context = kContextLookup[p1] | kContextLookup[p2 + 256].
28
19