brotli 0.1.1 → 0.1.2

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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/ext/brotli/brotli.cc +114 -24
  3. data/ext/brotli/brotli.h +0 -1
  4. data/ext/brotli/extconf.rb +30 -23
  5. data/lib/brotli/version.rb +1 -1
  6. data/vendor/brotli/LICENSE +1 -1
  7. data/vendor/brotli/dec/Makefile +1 -1
  8. data/vendor/brotli/dec/bit_reader.c +3 -3
  9. data/vendor/brotli/dec/bit_reader.h +25 -27
  10. data/vendor/brotli/dec/context.h +4 -4
  11. data/vendor/brotli/dec/decode.c +410 -486
  12. data/vendor/brotli/dec/decode.h +101 -105
  13. data/vendor/brotli/dec/dictionary.c +1 -1
  14. data/vendor/brotli/dec/dictionary.h +7 -8
  15. data/vendor/brotli/dec/huffman.c +103 -105
  16. data/vendor/brotli/dec/huffman.h +18 -18
  17. data/vendor/brotli/dec/port.h +52 -40
  18. data/vendor/brotli/dec/prefix.h +2 -0
  19. data/vendor/brotli/dec/state.c +13 -19
  20. data/vendor/brotli/dec/state.h +25 -39
  21. data/vendor/brotli/dec/transform.h +38 -44
  22. data/vendor/brotli/dec/types.h +2 -2
  23. data/vendor/brotli/enc/Makefile +1 -1
  24. data/vendor/brotli/enc/backward_references.cc +455 -359
  25. data/vendor/brotli/enc/backward_references.h +79 -3
  26. data/vendor/brotli/enc/bit_cost.h +54 -32
  27. data/vendor/brotli/enc/block_splitter.cc +285 -193
  28. data/vendor/brotli/enc/block_splitter.h +4 -12
  29. data/vendor/brotli/enc/brotli_bit_stream.cc +623 -324
  30. data/vendor/brotli/enc/brotli_bit_stream.h +76 -37
  31. data/vendor/brotli/enc/cluster.h +161 -120
  32. data/vendor/brotli/enc/command.h +60 -37
  33. data/vendor/brotli/enc/compress_fragment.cc +701 -0
  34. data/vendor/brotli/enc/compress_fragment.h +47 -0
  35. data/vendor/brotli/enc/compress_fragment_two_pass.cc +524 -0
  36. data/vendor/brotli/enc/compress_fragment_two_pass.h +40 -0
  37. data/vendor/brotli/enc/compressor.h +15 -0
  38. data/vendor/brotli/enc/context.h +1 -1
  39. data/vendor/brotli/enc/dictionary.h +2 -2
  40. data/vendor/brotli/enc/encode.cc +819 -286
  41. data/vendor/brotli/enc/encode.h +38 -15
  42. data/vendor/brotli/enc/encode_parallel.cc +40 -42
  43. data/vendor/brotli/enc/entropy_encode.cc +144 -147
  44. data/vendor/brotli/enc/entropy_encode.h +32 -8
  45. data/vendor/brotli/enc/entropy_encode_static.h +572 -0
  46. data/vendor/brotli/enc/fast_log.h +7 -40
  47. data/vendor/brotli/enc/find_match_length.h +9 -9
  48. data/vendor/brotli/enc/hash.h +462 -154
  49. data/vendor/brotli/enc/histogram.cc +6 -6
  50. data/vendor/brotli/enc/histogram.h +13 -13
  51. data/vendor/brotli/enc/literal_cost.cc +45 -45
  52. data/vendor/brotli/enc/metablock.cc +92 -89
  53. data/vendor/brotli/enc/metablock.h +12 -12
  54. data/vendor/brotli/enc/port.h +7 -16
  55. data/vendor/brotli/enc/prefix.h +23 -22
  56. data/vendor/brotli/enc/ringbuffer.h +75 -29
  57. data/vendor/brotli/enc/static_dict.cc +56 -48
  58. data/vendor/brotli/enc/static_dict.h +5 -5
  59. data/vendor/brotli/enc/streams.cc +1 -1
  60. data/vendor/brotli/enc/streams.h +5 -5
  61. data/vendor/brotli/enc/transform.h +40 -35
  62. data/vendor/brotli/enc/types.h +2 -0
  63. data/vendor/brotli/enc/utf8_util.cc +3 -2
  64. data/vendor/brotli/enc/write_bits.h +6 -6
  65. metadata +9 -5
  66. data/vendor/brotli/dec/streams.c +0 -102
  67. data/vendor/brotli/dec/streams.h +0 -95
@@ -9,135 +9,117 @@
9
9
  #ifndef BROTLI_DEC_DECODE_H_
10
10
  #define BROTLI_DEC_DECODE_H_
11
11
 
12
- #include "./state.h"
13
- #include "./streams.h"
14
12
  #include "./types.h"
15
13
 
16
14
  #if defined(__cplusplus) || defined(c_plusplus)
17
15
  extern "C" {
18
16
  #endif
19
17
 
18
+ typedef struct BrotliStateStruct BrotliState;
19
+
20
20
  typedef enum {
21
- /* Decoding error, e.g. corrupt input or no memory */
21
+ /* Decoding error, e.g. corrupt input or memory allocation problem */
22
22
  BROTLI_RESULT_ERROR = 0,
23
- /* Successfully completely done */
23
+ /* Decoding successfully completed */
24
24
  BROTLI_RESULT_SUCCESS = 1,
25
- /* Partially done, but must be called again with more input */
25
+ /* Partially done; should be called again with more input */
26
26
  BROTLI_RESULT_NEEDS_MORE_INPUT = 2,
27
- /* Partially done, but must be called again with more output */
27
+ /* Partially done; should be called again with more output */
28
28
  BROTLI_RESULT_NEEDS_MORE_OUTPUT = 3
29
29
  } BrotliResult;
30
30
 
31
- /* BROTLI_FAILURE macro unwraps to BROTLI_RESULT_ERROR in non-debug build. */
32
- /* In debug build it dumps file name, line and pretty function name. */
33
- #if defined(_MSC_VER) || !defined(BROTLI_DEBUG)
34
- #define BROTLI_FAILURE() BROTLI_RESULT_ERROR
35
- #else
36
- #define BROTLI_FAILURE() \
37
- BrotliFailure(__FILE__, __LINE__, __PRETTY_FUNCTION__)
38
- static inline BrotliResult BrotliFailure(const char *f, int l, const char *fn) {
39
- fprintf(stderr, "ERROR at %s:%d (%s)\n", f, l, fn);
40
- fflush(stderr);
41
- return BROTLI_RESULT_ERROR;
42
- }
43
- #endif
31
+ #define BROTLI_ERROR_CODES_LIST(BROTLI_ERROR_CODE, SEPARATOR) \
32
+ BROTLI_ERROR_CODE(_, NO_ERROR, 0) SEPARATOR \
33
+ /* Same as BrotliResult values */ \
34
+ BROTLI_ERROR_CODE(_, SUCCESS, 1) SEPARATOR \
35
+ BROTLI_ERROR_CODE(_, NEEDS_MORE_INPUT, 2) SEPARATOR \
36
+ BROTLI_ERROR_CODE(_, NEEDS_MORE_OUTPUT, 3) SEPARATOR \
37
+ \
38
+ /* Errors caused by invalid input */ \
39
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, EXUBERANT_NIBBLE, -1) SEPARATOR \
40
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, RESERVED, -2) SEPARATOR \
41
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, EXUBERANT_META_NIBBLE, -3) SEPARATOR \
42
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, SIMPLE_HUFFMAN_ALPHABET, -4) SEPARATOR \
43
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, SIMPLE_HUFFMAN_SAME, -5) SEPARATOR \
44
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, CL_SPACE, -6) SEPARATOR \
45
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, HUFFMAN_SPACE, -7) SEPARATOR \
46
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, CONTEXT_MAP_REPEAT, -8) SEPARATOR \
47
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_LENGTH_1, -9) SEPARATOR \
48
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, BLOCK_LENGTH_2, -10) SEPARATOR \
49
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, TRANSFORM, -11) SEPARATOR \
50
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, DICTIONARY, -12) SEPARATOR \
51
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, WINDOW_BITS, -13) SEPARATOR \
52
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_1, -14) SEPARATOR \
53
+ BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_2, -15) SEPARATOR \
54
+ \
55
+ /* -16..-20 codes are reserved */ \
56
+ \
57
+ /* Memory allocation problems */ \
58
+ BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MODES, -21) SEPARATOR \
59
+ /* Literal, insert and distance trees together */ \
60
+ BROTLI_ERROR_CODE(_ERROR_ALLOC_, TREE_GROUPS, -22) SEPARATOR \
61
+ /* -23..-24 codes are reserved for distinct tree groups */ \
62
+ BROTLI_ERROR_CODE(_ERROR_ALLOC_, CONTEXT_MAP, -25) SEPARATOR \
63
+ BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_1, -26) SEPARATOR \
64
+ BROTLI_ERROR_CODE(_ERROR_ALLOC_, RING_BUFFER_2, -27) SEPARATOR \
65
+ /* -28..-29 codes are reserved for dynamic ringbuffer allocation */ \
66
+ BROTLI_ERROR_CODE(_ERROR_ALLOC_, BLOCK_TYPE_TREES, -30) SEPARATOR \
67
+ \
68
+ /* "Impossible" states */ \
69
+ BROTLI_ERROR_CODE(_ERROR_, UNREACHABLE, -31)
44
70
 
45
- /* Creates the instance of BrotliState and initializes it. alloc_func and
46
- free_func MUST be both zero or both non-zero. In the case they are both zero,
47
- default memory allocators are used. opaque parameter is passed to alloc_func
48
- and free_func when they are called. */
71
+ typedef enum {
72
+ #define _BROTLI_COMMA ,
73
+ #define _BROTLI_ERROR_CODE_ENUM_ITEM(PREFIX, NAME, CODE) \
74
+ BROTLI ## PREFIX ## NAME = CODE
75
+ BROTLI_ERROR_CODES_LIST(_BROTLI_ERROR_CODE_ENUM_ITEM, _BROTLI_COMMA)
76
+ #undef _BROTLI_ERROR_CODE_ENUM_ITEM
77
+ #undef _BROTLI_COMMA
78
+ } BrotliErrorCode;
79
+
80
+ #define BROTLI_LAST_ERROR_CODE BROTLI_ERROR_UNREACHABLE
81
+
82
+ /* Creates the instance of BrotliState and initializes it. |alloc_func| and
83
+ |free_func| MUST be both zero or both non-zero. In the case they are both
84
+ zero, default memory allocators are used. |opaque| is passed to |alloc_func|
85
+ and |free_func| when they are called. */
49
86
  BrotliState* BrotliCreateState(
50
87
  brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque);
51
88
 
52
89
  /* Deinitializes and frees BrotliState instance. */
53
90
  void BrotliDestroyState(BrotliState* state);
54
91
 
55
- /* Sets *decoded_size to the decompressed size of the given encoded stream. */
56
- /* This function only works if the encoded buffer has a single meta block, */
57
- /* or if it has two meta-blocks, where the first is uncompressed and the */
58
- /* second is empty. */
59
- /* Returns 1 on success, 0 on failure. */
92
+ /* Sets |*decoded_size| to the decompressed size of the given encoded stream.
93
+ This function only works if the encoded buffer has a single meta block,
94
+ or if it has two meta-blocks, where the first is uncompressed and the
95
+ second is empty.
96
+ Returns 1 on success, 0 on failure. */
60
97
  int BrotliDecompressedSize(size_t encoded_size,
61
98
  const uint8_t* encoded_buffer,
62
99
  size_t* decoded_size);
63
100
 
64
- /* Decompresses the data in encoded_buffer into decoded_buffer, and sets */
65
- /* *decoded_size to the decompressed length. */
101
+ /* Decompresses the data in |encoded_buffer| into |decoded_buffer|, and sets
102
+ |*decoded_size| to the decompressed length. */
66
103
  BrotliResult BrotliDecompressBuffer(size_t encoded_size,
67
104
  const uint8_t* encoded_buffer,
68
105
  size_t* decoded_size,
69
106
  uint8_t* decoded_buffer);
70
107
 
71
- /* Same as above, but uses the specified input and output callbacks instead */
72
- /* of reading from and writing to pre-allocated memory buffers. */
73
- /* DEPRECATED */
74
- BrotliResult BrotliDecompress(BrotliInput input, BrotliOutput output);
75
-
76
- /* Same as above, but supports the caller to call the decoder repeatedly with
77
- partial data to support streaming. The state must be initialized with
78
- BrotliStateInit and reused with every call for the same stream.
79
- Return values:
80
- 0: failure.
81
- 1: success, and done.
82
- 2: success so far, end not reached so should call again with more input.
83
- The finish parameter is used as follows, for a series of calls with the
84
- same state:
85
- 0: Every call except the last one must be called with finish set to 0. The
86
- last call may have finish set to either 0 or 1. Only if finish is 0, can
87
- the function return 2. It may also return 0 or 1, in that case no more
88
- calls (even with finish 1) may be made.
89
- 1: Only the last call may have finish set to 1. It's ok to give empty input
90
- if all input was already given to previous calls. It is also ok to have
91
- only one single call in total, with finish 1, and with all input
92
- available immediately. That matches the non-streaming case. If finish is
93
- 1, the function can only return 0 or 1, never 2. After a finish, no more
94
- calls may be done.
95
- After everything is done, the state must be cleaned with BrotliStateCleanup
96
- to free allocated resources.
97
- The given BrotliOutput must always accept all output and make enough space,
98
- it returning a smaller value than the amount of bytes to write always results
99
- in an error.
100
- */
101
- /* DEPRECATED */
102
- BrotliResult BrotliDecompressStreaming(BrotliInput input, BrotliOutput output,
103
- int finish, BrotliState* s);
104
-
105
- /* Same as above, but with memory buffers.
106
- Must be called with an allocated input buffer in *next_in and an allocated
107
- output buffer in *next_out. The values *available_in and *available_out
108
- must specify the allocated size in *next_in and *next_out respectively.
109
- The value *total_out must be 0 initially, and will be summed with the
110
- amount of output bytes written after each call, so that at the end it
111
- gives the complete decoded size.
112
- After each call, *available_in will be decremented by the amount of input
113
- bytes consumed, and the *next_in pointer will be incremented by that amount.
114
- Similarly, *available_out will be decremented by the amount of output
115
- bytes written, and the *next_out pointer will be incremented by that
116
- amount.
117
-
118
- The input may be partial. With each next function call, *next_in and
119
- *available_in must be updated to point to a next part of the compressed
120
- input. The current implementation will always consume all input unless
121
- an error occurs, so normally *available_in will always be 0 after
122
- calling this function and the next adjacent part of input is desired.
123
-
124
- In the current implementation, the function requires that there is enough
125
- output buffer size to write all currently processed input, so
126
- *available_out must be large enough. Since the function updates *next_out
127
- each time, as long as the output buffer is large enough you can keep
128
- reusing this variable. It is also possible to update *next_out and
129
- *available_out yourself before a next call, e.g. to point to a new larger
130
- buffer.
131
- */
132
- /* DEPRECATED */
133
- BrotliResult BrotliDecompressBufferStreaming(size_t* available_in,
134
- const uint8_t** next_in,
135
- int finish,
136
- size_t* available_out,
137
- uint8_t** next_out,
138
- size_t* total_out,
139
- BrotliState* s);
108
+ /* Decompresses the data. Supports partial input and output.
140
109
 
110
+ Must be called with an allocated input buffer in |*next_in| and an allocated
111
+ output buffer in |*next_out|. The values |*available_in| and |*available_out|
112
+ must specify the allocated size in |*next_in| and |*next_out| respectively.
113
+
114
+ After each call, |*available_in| will be decremented by the amount of input
115
+ bytes consumed, and the |*next_in| pointer will be incremented by that
116
+ amount. Similarly, |*available_out| will be decremented by the amount of
117
+ output bytes written, and the |*next_out| pointer will be incremented by that
118
+ amount. |total_out|, if it is not a null-pointer, will be set to the number
119
+ of bytes decompressed since the last state initialization.
120
+
121
+ Input is never overconsumed, so |next_in| and |available_in| could be passed
122
+ to the next consumer after decoding is complete. */
141
123
  BrotliResult BrotliDecompressStream(size_t* available_in,
142
124
  const uint8_t** next_in,
143
125
  size_t* available_out,
@@ -148,16 +130,30 @@ BrotliResult BrotliDecompressStream(size_t* available_in,
148
130
  /* Fills the new state with a dictionary for LZ77, warming up the ringbuffer,
149
131
  e.g. for custom static dictionaries for data formats.
150
132
  Not to be confused with the built-in transformable dictionary of Brotli.
151
- The dictionary must exist in memory until decoding is done and is owned by
152
- the caller. To use:
153
- -initialize state with BrotliStateInit
154
- -use BrotliSetCustomDictionary
155
- -use BrotliDecompressBufferStreaming
156
- -clean up with BrotliStateCleanup
133
+ |size| should be less or equal to 2^24 (16MiB), otherwise the dictionary will
134
+ be ignored. The dictionary must exist in memory until decoding is done and
135
+ is owned by the caller. To use:
136
+ 1) Allocate and initialize state with BrotliCreateState
137
+ 2) Use BrotliSetCustomDictionary
138
+ 3) Use BrotliDecompressStream
139
+ 4) Clean up and free state with BrotliDestroyState
157
140
  */
158
141
  void BrotliSetCustomDictionary(
159
142
  size_t size, const uint8_t* dict, BrotliState* s);
160
143
 
144
+ /* Returns 1, if s is in a state where we have not read any input bytes yet,
145
+ and 0 otherwise */
146
+ int BrotliStateIsStreamStart(const BrotliState* s);
147
+
148
+ /* Returns 1, if s is in a state where we reached the end of the input and
149
+ produced all of the output, and 0 otherwise. */
150
+ int BrotliStateIsStreamEnd(const BrotliState* s);
151
+
152
+ /* Returns detailed error code after BrotliDecompressStream returns
153
+ BROTLI_RESULT_ERROR. */
154
+ BrotliErrorCode BrotliGetErrorCode(const BrotliState* s);
155
+
156
+ const char* BrotliErrorString(BrotliErrorCode c);
161
157
 
162
158
  #if defined(__cplusplus) || defined(c_plusplus)
163
159
  } /* extern "C" */
@@ -9462,5 +9462,5 @@ const uint8_t kBrotliDictionary[122784] = {
9462
9462
  };
9463
9463
 
9464
9464
  #if defined(__cplusplus) || defined(c_plusplus)
9465
- } /* extern "C" */
9465
+ } /* extern "C" */
9466
9466
  #endif
@@ -17,23 +17,22 @@ extern "C" {
17
17
 
18
18
  extern const uint8_t kBrotliDictionary[122784];
19
19
 
20
- static const int kBrotliDictionaryOffsetsByLength[] = {
21
- 0, 0, 0, 0, 0, 4096, 9216, 21504, 35840, 44032,
22
- 53248, 63488, 74752, 87040, 93696, 100864, 104704, 106752, 108928, 113536,
23
- 115968, 118528, 119872, 121280, 122016,
20
+ static const uint32_t kBrotliDictionaryOffsetsByLength[] = {
21
+ 0, 0, 0, 0, 0, 4096, 9216, 21504, 35840, 44032, 53248, 63488, 74752, 87040,
22
+ 93696, 100864, 104704, 106752, 108928, 113536, 115968, 118528, 119872, 121280,
23
+ 122016
24
24
  };
25
25
 
26
26
  static const uint8_t kBrotliDictionarySizeBitsByLength[] = {
27
- 0, 0, 0, 0, 10, 10, 11, 11, 10, 10,
28
- 10, 10, 10, 9, 9, 8, 7, 7, 8, 7,
29
- 7, 6, 6, 5, 5,
27
+ 0, 0, 0, 0, 10, 10, 11, 11, 10, 10, 10, 10, 10,
28
+ 9, 9, 8, 7, 7, 8, 7, 7, 6, 6, 5, 5,
30
29
  };
31
30
 
32
31
  static const int kBrotliMinDictionaryWordLength = 4;
33
32
  static const int kBrotliMaxDictionaryWordLength = 24;
34
33
 
35
34
  #if defined(__cplusplus) || defined(c_plusplus)
36
- } /* extern "C" */
35
+ } /* extern "C" */
37
36
  #endif
38
37
 
39
38
  #endif /* BROTLI_DEC_DICTIONARY_H_ */
@@ -6,11 +6,12 @@
6
6
 
7
7
  /* Utilities for building Huffman decoding tables. */
8
8
 
9
- #include <stdlib.h>
10
- #include <stdio.h>
11
- #include <string.h>
12
9
  #include "./huffman.h"
10
+
11
+ #include <string.h> /* memcpy, memset */
12
+
13
13
  #include "./port.h"
14
+ #include "./types.h"
14
15
 
15
16
  #if defined(__cplusplus) || defined(c_plusplus)
16
17
  extern "C" {
@@ -23,43 +24,43 @@ extern "C" {
23
24
  #else
24
25
  #define BROTLI_REVERSE_BITS_BASE 0
25
26
  static uint8_t kReverseBits[1 << BROTLI_REVERSE_BITS_MAX] = {
26
- 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
27
- 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
28
- 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
29
- 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
30
- 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
31
- 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
32
- 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
33
- 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
34
- 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
35
- 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
36
- 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
37
- 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
38
- 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
39
- 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
40
- 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
41
- 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
42
- 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
43
- 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
44
- 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9,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 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
56
+ 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
57
+ 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
58
+ 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
58
59
  };
59
- #endif /* BROTLI_RBIT */
60
+ #endif /* BROTLI_RBIT */
60
61
 
61
62
  #define BROTLI_REVERSE_BITS_LOWEST \
62
- (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
63
+ (1U << (BROTLI_REVERSE_BITS_MAX - 1 + BROTLI_REVERSE_BITS_BASE))
63
64
 
64
65
  /* Returns reverse(num >> BROTLI_REVERSE_BITS_BASE, BROTLI_REVERSE_BITS_MAX),
65
66
  where reverse(value, len) is the bit-wise reversal of the len least
@@ -98,23 +99,22 @@ static BROTLI_INLINE int NextTableBitSize(const uint16_t* const count,
98
99
  return len - root_bits;
99
100
  }
100
101
 
101
-
102
102
  void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
103
103
  const uint8_t* const code_lengths,
104
- uint16_t *count) {
105
- HuffmanCode code; /* current table entry */
106
- int symbol; /* symbol index in original or sorted table */
107
- uint32_t key; /* prefix code */
108
- uint32_t key_step; /* prefix code addend */
109
- int step; /* step size to replicate values in current table */
110
- int table_size; /* size of current table */
111
- int sorted[18]; /* symbols sorted by code length */
104
+ uint16_t* count) {
105
+ HuffmanCode code; /* current table entry */
106
+ int symbol; /* symbol index in original or sorted table */
107
+ uint32_t key; /* prefix code */
108
+ uint32_t key_step; /* prefix code addend */
109
+ int step; /* step size to replicate values in current table */
110
+ int table_size; /* size of current table */
111
+ int sorted[18]; /* symbols sorted by code length */
112
112
  /* offsets in sorted table for each length */
113
113
  int offset[BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH + 1];
114
114
  int bits;
115
115
  int bits_count;
116
- BROTLI_DCHECK(
117
- BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <= BROTLI_REVERSE_BITS_MAX);
116
+ BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH <=
117
+ BROTLI_REVERSE_BITS_MAX);
118
118
 
119
119
  /* generate offsets into sorted symbol table by code length */
120
120
  symbol = -1;
@@ -169,26 +169,26 @@ void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* table,
169
169
  uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
170
170
  int root_bits,
171
171
  const uint16_t* const symbol_lists,
172
- uint16_t *count) {
173
- HuffmanCode code; /* current table entry */
174
- HuffmanCode* table; /* next available space in table */
175
- int len; /* current code length */
176
- int symbol; /* symbol index in original or sorted table */
177
- uint32_t key; /* prefix code */
178
- uint32_t key_step; /* prefix code addend */
179
- uint32_t sub_key; /* 2nd level table prefix code */
180
- uint32_t sub_key_step;/* 2nd level table prefix code addend */
181
- int step; /* step size to replicate values in current table */
182
- int table_bits; /* key length of current table */
183
- int table_size; /* size of current table */
184
- int total_size; /* sum of root table size and 2nd level table sizes */
172
+ uint16_t* count) {
173
+ HuffmanCode code; /* current table entry */
174
+ HuffmanCode* table; /* next available space in table */
175
+ int len; /* current code length */
176
+ int symbol; /* symbol index in original or sorted table */
177
+ uint32_t key; /* prefix code */
178
+ uint32_t key_step; /* prefix code addend */
179
+ uint32_t sub_key; /* 2nd level table prefix code */
180
+ uint32_t sub_key_step; /* 2nd level table prefix code addend */
181
+ int step; /* step size to replicate values in current table */
182
+ int table_bits; /* key length of current table */
183
+ int table_size; /* size of current table */
184
+ int total_size; /* sum of root table size and 2nd level table sizes */
185
185
  int max_length = -1;
186
186
  int bits;
187
187
  int bits_count;
188
188
 
189
189
  BROTLI_DCHECK(root_bits <= BROTLI_REVERSE_BITS_MAX);
190
- BROTLI_DCHECK(
191
- BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <= BROTLI_REVERSE_BITS_MAX);
190
+ BROTLI_DCHECK(BROTLI_HUFFMAN_MAX_CODE_LENGTH - root_bits <=
191
+ BROTLI_REVERSE_BITS_MAX);
192
192
 
193
193
  while (symbol_lists[max_length] == 0xFFFF) max_length--;
194
194
  max_length += BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
@@ -237,7 +237,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
237
237
  for (len = root_bits + 1, step = 2; len <= max_length; ++len) {
238
238
  symbol = len - (BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1);
239
239
  for (; count[len] != 0; --count[len]) {
240
- if (sub_key == (uint32_t)(BROTLI_REVERSE_BITS_LOWEST << 1)) {
240
+ if (sub_key == (BROTLI_REVERSE_BITS_LOWEST << 1U)) {
241
241
  table += table_size;
242
242
  table_bits = NextTableBitSize(count, len, root_bits);
243
243
  table_size = 1 << table_bits;
@@ -245,8 +245,8 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
245
245
  sub_key = BrotliReverseBits(key);
246
246
  key += key_step;
247
247
  root_table[sub_key].bits = (uint8_t)(table_bits + root_bits);
248
- root_table[sub_key].value = (uint16_t)(
249
- ((size_t)(table - root_table)) - sub_key);
248
+ root_table[sub_key].value =
249
+ (uint16_t)(((size_t)(table - root_table)) - sub_key);
250
250
  sub_key = 0;
251
251
  }
252
252
  code.bits = (uint8_t)(len - root_bits);
@@ -264,7 +264,7 @@ uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
264
264
 
265
265
  uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
266
266
  int root_bits,
267
- uint16_t *val,
267
+ uint16_t* val,
268
268
  uint32_t num_symbols) {
269
269
  uint32_t table_size = 1;
270
270
  const uint32_t goal_size = 1U << root_bits;
@@ -301,49 +301,47 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
301
301
  table[3].bits = 2;
302
302
  table_size = 4;
303
303
  break;
304
- case 3:
305
- {
306
- int i, k;
307
- for (i = 0; i < 3; ++i) {
308
- for (k = i + 1; k < 4; ++k) {
309
- if (val[k] < val[i]) {
310
- uint16_t t = val[k];
311
- val[k] = val[i];
312
- val[i] = t;
313
- }
304
+ case 3: {
305
+ int i, k;
306
+ for (i = 0; i < 3; ++i) {
307
+ for (k = i + 1; k < 4; ++k) {
308
+ if (val[k] < val[i]) {
309
+ uint16_t t = val[k];
310
+ val[k] = val[i];
311
+ val[i] = t;
314
312
  }
315
313
  }
316
- for (i = 0; i < 4; ++i) {
317
- table[i].bits = 2;
318
- }
319
- table[0].value = val[0];
320
- table[2].value = val[1];
321
- table[1].value = val[2];
322
- table[3].value = val[3];
323
- table_size = 4;
324
314
  }
315
+ for (i = 0; i < 4; ++i) {
316
+ table[i].bits = 2;
317
+ }
318
+ table[0].value = val[0];
319
+ table[2].value = val[1];
320
+ table[1].value = val[2];
321
+ table[3].value = val[3];
322
+ table_size = 4;
325
323
  break;
326
- case 4:
327
- {
328
- int i;
329
- if (val[3] < val[2]) {
330
- uint16_t t = val[3];
331
- val[3] = val[2];
332
- val[2] = t;
333
- }
334
- for (i = 0; i < 7; ++i) {
335
- table[i].value = val[0];
336
- table[i].bits = (uint8_t)(1 + (i & 1));
337
- }
338
- table[1].value = val[1];
339
- table[3].value = val[2];
340
- table[5].value = val[1];
341
- table[7].value = val[3];
342
- table[3].bits = 3;
343
- table[7].bits = 3;
344
- table_size = 8;
324
+ }
325
+ case 4: {
326
+ int i;
327
+ if (val[3] < val[2]) {
328
+ uint16_t t = val[3];
329
+ val[3] = val[2];
330
+ val[2] = t;
345
331
  }
332
+ for (i = 0; i < 7; ++i) {
333
+ table[i].value = val[0];
334
+ table[i].bits = (uint8_t)(1 + (i & 1));
335
+ }
336
+ table[1].value = val[1];
337
+ table[3].value = val[2];
338
+ table[5].value = val[1];
339
+ table[7].value = val[3];
340
+ table[3].bits = 3;
341
+ table[7].bits = 3;
342
+ table_size = 8;
346
343
  break;
344
+ }
347
345
  }
348
346
  while (table_size != goal_size) {
349
347
  memcpy(&table[table_size], &table[0],
@@ -354,5 +352,5 @@ uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
354
352
  }
355
353
 
356
354
  #if defined(__cplusplus) || defined(c_plusplus)
357
- } /* extern "C" */
355
+ } /* extern "C" */
358
356
  #endif
@@ -10,6 +10,7 @@
10
10
  #define BROTLI_DEC_HUFFMAN_H_
11
11
 
12
12
  #include "./types.h"
13
+ #include "./port.h"
13
14
 
14
15
  #if defined(__cplusplus) || defined(c_plusplus)
15
16
  extern "C" {
@@ -20,38 +21,37 @@ extern "C" {
20
21
  /* For current format this constant equals to kNumInsertAndCopyCodes */
21
22
  #define BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE 704
22
23
 
23
- /* Maximum possible Huffman table size for an alphabet size of 704, max code
24
- * length 15 and root table bits 8. */
25
- #define BROTLI_HUFFMAN_MAX_TABLE_SIZE 1080
24
+ /* Maximum possible Huffman table size for an alphabet size of (index * 32),
25
+ * max code length 15 and root table bits 8. */
26
+ static const uint16_t kMaxHuffmanTableSize[] = {
27
+ 256, 402, 436, 468, 500, 534, 566, 598, 630, 662, 694, 726, 758, 790, 822,
28
+ 854, 886, 920, 952, 984, 1016, 1048, 1080};
29
+ #define BROTLI_HUFFMAN_MAX_SIZE_26 396
30
+ #define BROTLI_HUFFMAN_MAX_SIZE_258 632
31
+ #define BROTLI_HUFFMAN_MAX_SIZE_272 646
26
32
 
27
33
  #define BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH 5
28
34
 
29
35
  typedef struct {
30
- uint8_t bits; /* number of bits used for this symbol */
31
- uint16_t value; /* symbol value or table offset */
36
+ uint8_t bits; /* number of bits used for this symbol */
37
+ uint16_t value; /* symbol value or table offset */
32
38
  } HuffmanCode;
33
39
 
34
-
35
40
  /* Builds Huffman lookup table assuming code lengths are in symbol order. */
36
- void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
37
- const uint8_t* const code_lengths,
38
- uint16_t *count);
41
+ BROTLI_INTERNAL void BrotliBuildCodeLengthsHuffmanTable(HuffmanCode* root_table,
42
+ const uint8_t* const code_lengths, uint16_t* count);
39
43
 
40
44
  /* Builds Huffman lookup table assuming code lengths are in symbol order. */
41
45
  /* Returns size of resulting table. */
42
- uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
43
- int root_bits,
44
- const uint16_t* const symbol_lists,
45
- uint16_t *count_arg);
46
+ BROTLI_INTERNAL uint32_t BrotliBuildHuffmanTable(HuffmanCode* root_table,
47
+ int root_bits, const uint16_t* const symbol_lists, uint16_t* count_arg);
46
48
 
47
49
  /* Builds a simple Huffman table. The num_symbols parameter is to be */
48
50
  /* interpreted as follows: 0 means 1 symbol, 1 means 2 symbols, 2 means 3 */
49
51
  /* symbols, 3 means 4 symbols with lengths 2,2,2,2, 4 means 4 symbols with */
50
52
  /* lengths 1,2,3,3. */
51
- uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
52
- int root_bits,
53
- uint16_t *symbols,
54
- uint32_t num_symbols);
53
+ BROTLI_INTERNAL uint32_t BrotliBuildSimpleHuffmanTable(HuffmanCode* table,
54
+ int root_bits, uint16_t* symbols, uint32_t num_symbols);
55
55
 
56
56
  /* Contains a collection of Huffman trees with the same alphabet size. */
57
57
  typedef struct {
@@ -62,7 +62,7 @@ typedef struct {
62
62
  } HuffmanTreeGroup;
63
63
 
64
64
  #if defined(__cplusplus) || defined(c_plusplus)
65
- } /* extern "C" */
65
+ } /* extern "C" */
66
66
  #endif
67
67
 
68
68
  #endif /* BROTLI_DEC_HUFFMAN_H_ */