brotli 0.2.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/main.yml +34 -0
  3. data/.github/workflows/publish.yml +34 -0
  4. data/Gemfile +6 -3
  5. data/Rakefile +16 -9
  6. data/brotli.gemspec +7 -13
  7. data/ext/brotli/brotli.c +209 -31
  8. data/ext/brotli/buffer.c +1 -7
  9. data/ext/brotli/buffer.h +1 -1
  10. data/ext/brotli/extconf.rb +20 -18
  11. data/lib/brotli/version.rb +1 -1
  12. data/test/brotli_test.rb +104 -0
  13. data/test/brotli_writer_test.rb +36 -0
  14. data/test/test_helper.rb +8 -0
  15. data/vendor/brotli/c/common/constants.c +15 -0
  16. data/vendor/brotli/c/common/constants.h +136 -0
  17. data/vendor/brotli/c/common/context.c +156 -0
  18. data/vendor/brotli/c/common/context.h +4 -152
  19. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
  20. data/vendor/brotli/c/common/dictionary.c +10 -1
  21. data/vendor/brotli/c/common/platform.c +22 -0
  22. data/vendor/brotli/c/common/platform.h +43 -17
  23. data/vendor/brotli/c/common/transform.c +59 -3
  24. data/vendor/brotli/c/common/transform.h +5 -0
  25. data/vendor/brotli/c/common/version.h +2 -2
  26. data/vendor/brotli/c/dec/bit_reader.c +28 -0
  27. data/vendor/brotli/c/dec/bit_reader.h +58 -16
  28. data/vendor/brotli/c/dec/decode.c +353 -251
  29. data/vendor/brotli/c/dec/huffman.h +6 -12
  30. data/vendor/brotli/c/dec/prefix.h +0 -18
  31. data/vendor/brotli/c/dec/state.c +9 -14
  32. data/vendor/brotli/c/dec/state.h +144 -37
  33. data/vendor/brotli/c/enc/backward_references.c +8 -7
  34. data/vendor/brotli/c/enc/backward_references.h +5 -4
  35. data/vendor/brotli/c/enc/backward_references_hq.c +51 -33
  36. data/vendor/brotli/c/enc/backward_references_hq.h +11 -8
  37. data/vendor/brotli/c/enc/backward_references_inc.h +24 -14
  38. data/vendor/brotli/c/enc/block_splitter.c +3 -3
  39. data/vendor/brotli/c/enc/block_splitter_inc.h +15 -6
  40. data/vendor/brotli/c/enc/brotli_bit_stream.c +13 -30
  41. data/vendor/brotli/c/enc/cluster_inc.h +6 -3
  42. data/vendor/brotli/c/enc/command.c +28 -0
  43. data/vendor/brotli/c/enc/command.h +12 -12
  44. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +1 -1
  45. data/vendor/brotli/c/enc/dictionary_hash.c +1826 -1100
  46. data/vendor/brotli/c/enc/dictionary_hash.h +2 -1
  47. data/vendor/brotli/c/enc/encode.c +104 -39
  48. data/vendor/brotli/c/enc/encoder_dict.c +3 -2
  49. data/vendor/brotli/c/enc/encoder_dict.h +3 -1
  50. data/vendor/brotli/c/enc/entropy_encode.c +2 -0
  51. data/vendor/brotli/c/enc/entropy_encode.h +2 -2
  52. data/vendor/brotli/c/enc/fast_log.c +105 -0
  53. data/vendor/brotli/c/enc/fast_log.h +19 -100
  54. data/vendor/brotli/c/enc/find_match_length.h +2 -3
  55. data/vendor/brotli/c/enc/hash.h +80 -90
  56. data/vendor/brotli/c/enc/hash_composite_inc.h +52 -63
  57. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +88 -49
  58. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +50 -50
  59. data/vendor/brotli/c/enc/hash_longest_match_inc.h +53 -50
  60. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +91 -60
  61. data/vendor/brotli/c/enc/hash_rolling_inc.h +23 -27
  62. data/vendor/brotli/c/enc/hash_to_binary_tree_inc.h +39 -38
  63. data/vendor/brotli/c/enc/memory.h +24 -12
  64. data/vendor/brotli/c/enc/metablock.c +23 -27
  65. data/vendor/brotli/c/enc/metablock_inc.h +1 -1
  66. data/vendor/brotli/c/enc/params.h +3 -1
  67. data/vendor/brotli/c/enc/ringbuffer.h +4 -1
  68. data/vendor/brotli/c/enc/utf8_util.c +1 -1
  69. data/vendor/brotli/c/enc/write_bits.h +27 -25
  70. data/vendor/brotli/c/include/brotli/encode.h +22 -1
  71. data/vendor/brotli/c/include/brotli/port.h +14 -0
  72. metadata +17 -97
  73. data/.travis.yml +0 -31
  74. data/docs/Brotli.html +0 -485
  75. data/docs/Brotli/Error.html +0 -124
  76. data/docs/_index.html +0 -122
  77. data/docs/class_list.html +0 -51
  78. data/docs/css/common.css +0 -1
  79. data/docs/css/full_list.css +0 -58
  80. data/docs/css/style.css +0 -496
  81. data/docs/file.README.html +0 -127
  82. data/docs/file_list.html +0 -56
  83. data/docs/frames.html +0 -17
  84. data/docs/index.html +0 -127
  85. data/docs/js/app.js +0 -292
  86. data/docs/js/full_list.js +0 -216
  87. data/docs/js/jquery.js +0 -4
  88. data/docs/method_list.html +0 -67
  89. data/docs/top-level-namespace.html +0 -110
  90. data/spec/brotli_spec.rb +0 -88
  91. data/spec/inflate_spec.rb +0 -75
  92. data/spec/spec_helper.rb +0 -4
@@ -34,13 +34,7 @@ static
34
34
  buffer_t*
35
35
  expand_buffer(buffer_t* const buffer, const size_t need) {
36
36
  size_t size = need * buffer->expand_ratio / 100;
37
- uint8_t* ptr = malloc(size);
38
- if (ptr == NULL) {
39
- return NULL;
40
- }
41
- memcpy(ptr, buffer->ptr, buffer->size);
42
- free(buffer->ptr);
43
- buffer->ptr = ptr;
37
+ buffer->ptr = realloc(buffer->ptr, size);
44
38
  buffer->size = size;
45
39
  buffer->expand_count += 1;
46
40
  return buffer;
@@ -5,7 +5,7 @@
5
5
  #include <string.h>
6
6
 
7
7
  typedef struct {
8
- uint8_t* ptr;
8
+ char* ptr;
9
9
  size_t size;
10
10
  size_t used;
11
11
  size_t expand_ratio;
@@ -1,6 +1,6 @@
1
- require 'mkmf'
2
- require 'fileutils'
3
- require 'rbconfig'
1
+ require "mkmf"
2
+ require "fileutils"
3
+ require "rbconfig"
4
4
 
5
5
  dir_config("brotli")
6
6
 
@@ -13,36 +13,38 @@ have_dev_pkg = [
13
13
  pkg_config("libbrotlienc")
14
14
  ].all? { |e| e }
15
15
 
16
- $CPPFLAGS << ' -DOS_MACOSX' if RbConfig::CONFIG['host_os'] =~ /darwin|mac os/
17
- unless have_dev_pkg
18
- $INCFLAGS << ' -I$(srcdir)/enc -I$(srcdir)/dec -I$(srcdir)/common -I$(srcdir)/include'
19
- end
16
+ $CPPFLAGS << " -DOS_MACOSX" if RbConfig::CONFIG["host_os"] =~ /darwin|mac os/
17
+ $INCFLAGS << " -I$(srcdir)/enc -I$(srcdir)/dec -I$(srcdir)/common -I$(srcdir)/include" unless have_dev_pkg
20
18
 
21
- create_makefile('brotli/brotli')
19
+ create_makefile("brotli/brotli")
22
20
 
23
21
  unless have_dev_pkg
24
- __DIR__ = File.expand_path(File.dirname __FILE__)
22
+ __DIR__ = File.expand_path(File.dirname(__FILE__))
25
23
 
26
24
  %w[enc dec common include].each do |dirname|
27
25
  FileUtils.mkdir_p dirname
28
- FileUtils.cp_r File.expand_path(File.join(__DIR__, '..', '..', 'vendor', 'brotli', 'c', dirname), __DIR__), __DIR__, verbose: true
26
+ FileUtils.cp_r(
27
+ File.expand_path(File.join(__DIR__, "..", "..", "vendor", "brotli", "c", dirname), __DIR__),
28
+ __DIR__,
29
+ verbose: true
30
+ )
29
31
  end
30
32
 
31
33
  srcs = []
32
34
  objs = []
33
- Dir[File.expand_path(File.join('{enc,dec,common,include}', '**', '*.c'), __DIR__)].sort.each do |file|
34
- file[__DIR__ + File::SEPARATOR] = ''
35
+ Dir[File.expand_path(File.join("{enc,dec,common,include}", "**", "*.c"), __DIR__)].sort.each do |file|
36
+ file[__DIR__ + File::SEPARATOR] = ""
35
37
  srcs << file
36
- objs << file.sub(/\.c\z/, '.' + RbConfig::CONFIG['OBJEXT'])
38
+ objs << file.sub(/\.c\z/, "." + RbConfig::CONFIG["OBJEXT"])
37
39
  end
38
40
 
39
- File.open('Makefile', 'r+') do |f|
40
- obj_ext = RbConfig::CONFIG['OBJEXT']
41
- src = 'ORIG_SRCS = brotli.c buffer.c'
41
+ File.open("Makefile", "r+") do |f|
42
+ obj_ext = RbConfig::CONFIG["OBJEXT"]
43
+ src = "ORIG_SRCS = brotli.c buffer.c"
42
44
  obj = "OBJS = brotli.#{obj_ext} buffer.#{obj_ext}"
43
45
  txt = f.read
44
- .sub(/^ORIG_SRCS = .*$/, src + ' ' + srcs.join(' '))
45
- .sub(/^OBJS = .*$/, obj + ' ' + objs.join(' '))
46
+ .sub(/^ORIG_SRCS = .*$/, "#{src} #{srcs.join(" ")}")
47
+ .sub(/^OBJS = .*$/, "#{obj} #{objs.join(" ")}")
46
48
  f.rewind
47
49
  f.write txt
48
50
  end
@@ -1,3 +1,3 @@
1
1
  module Brotli
2
- VERSION = '0.2.3'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class BrotliTest < Test::Unit::TestCase
6
+ def testdata
7
+ @testdata ||= File.binread(
8
+ File.expand_path(File.join("..", "vendor", "brotli", "tests", "testdata", "alice29.txt"), __dir__)
9
+ )
10
+ end
11
+
12
+ test "VERSION" do
13
+ assert do
14
+ ::Brotli.const_defined?(:VERSION)
15
+ end
16
+ end
17
+
18
+ test "well done" do
19
+ property_of { string }.check do |string|
20
+ assert_equal string, Brotli.inflate(Brotli.deflate(string.dup))
21
+ end
22
+ end
23
+
24
+ sub_test_case ".version" do
25
+ test "returns string" do
26
+ assert_equal "1.0.9", Brotli.version
27
+ end
28
+ end
29
+
30
+ sub_test_case ".deflate" do
31
+ test "works" do
32
+ property_of {
33
+ [choose(nil, :generic, :text, :font), range(0, 11), range(10, 24), range(16, 24)]
34
+ }.check do |(mode, quality, lgwin, lgblock)|
35
+ assert_nothing_raised ArgumentError do
36
+ Brotli.deflate(
37
+ testdata,
38
+ mode: mode, quality: quality, lgwin: lgwin, lgblock: lgblock
39
+ )
40
+ end
41
+ end
42
+ end
43
+
44
+ test "raise ArgumentError if given invalid options" do
45
+ assert_raise ArgumentError do
46
+ Brotli.deflate(testdata, mode: "generic")
47
+ end
48
+ assert_raise ArgumentError do
49
+ Brotli.deflate(testdata, quality: 12)
50
+ end
51
+ assert_raise ArgumentError do
52
+ Brotli.deflate(testdata, lgwin: 9)
53
+ end
54
+ assert_raise ArgumentError do
55
+ Brotli.deflate(testdata, lgwin: 25)
56
+ end
57
+ assert_raise ArgumentError do
58
+ Brotli.deflate(testdata, lgblock: 15)
59
+ end
60
+ assert_raise ArgumentError do
61
+ Brotli.deflate(testdata, lgblock: 25)
62
+ end
63
+ end
64
+ end
65
+
66
+ sub_test_case ".inflate" do
67
+ def testdata2
68
+ @testdata2 ||= File.binread(
69
+ File.expand_path(File.join("..", "vendor", "brotli", "tests", "testdata", "alice29.txt.compressed"), __dir__)
70
+ )
71
+ end
72
+
73
+ test "works" do
74
+ assert_equal testdata, Brotli.inflate(testdata2)
75
+ end
76
+
77
+ test "raise error when pass insufficient data" do
78
+ assert_raise Brotli::Error do
79
+ Brotli.inflate(testdata2[0, 64])
80
+ end
81
+ end
82
+
83
+ test "raise error when pass invalid data" do
84
+ assert_raise Brotli::Error do
85
+ Brotli.inflate(testdata2.reverse)
86
+ end
87
+ end
88
+ end
89
+
90
+ sub_test_case "Ractor safe" do
91
+ test "able to invoke non-main ractor" do
92
+ unless defined? ::Ractor
93
+ notify "Ractor not defined"
94
+ omit
95
+ end
96
+ ractors = Array.new(2) do
97
+ Ractor.new(testdata) do |testdata|
98
+ Brotli.inflate(Brotli.deflate(testdata)) == testdata
99
+ end
100
+ end
101
+ assert_equal [true, true], ractors.map(&:take)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+ require "tempfile"
5
+
6
+ class BrotliWriterTest < Test::Unit::TestCase
7
+ def setup
8
+ @tempfile = Tempfile.new
9
+ end
10
+
11
+ def cleanup
12
+ @tempfile.close!
13
+ end
14
+
15
+ def testdata
16
+ @testdata ||= File.binread(
17
+ File.expand_path(File.join("..", "vendor", "brotli", "tests", "testdata", "alice29.txt"), __dir__)
18
+ )
19
+ end
20
+
21
+ test "works" do
22
+ writer = Brotli::Writer.new @tempfile
23
+ assert_equal testdata.bytesize, writer.write(testdata)
24
+ assert_equal @tempfile, writer.close
25
+ assert_equal true, @tempfile.closed?
26
+
27
+ @tempfile.open
28
+ assert_equal testdata, Brotli.inflate(@tempfile.read)
29
+ end
30
+
31
+ test "raise" do
32
+ assert_raise do
33
+ Brotli::Writer.new nil
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+ require "brotli"
5
+
6
+ require "test-unit"
7
+ require "test/unit/rr"
8
+ require "rantly/testunit_extensions"
@@ -0,0 +1,15 @@
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 "./constants.h"
8
+
9
+ const BrotliPrefixCodeRange
10
+ _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS] = {
11
+ {1, 2}, {5, 2}, {9, 2}, {13, 2}, {17, 3}, {25, 3},
12
+ {33, 3}, {41, 3}, {49, 4}, {65, 4}, {81, 4}, {97, 4},
13
+ {113, 5}, {145, 5}, {177, 5}, {209, 5}, {241, 6}, {305, 6},
14
+ {369, 7}, {497, 8}, {753, 9}, {1265, 10}, {2289, 11}, {4337, 12},
15
+ {8433, 13}, {16625, 24}};
@@ -4,9 +4,18 @@
4
4
  See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
5
  */
6
6
 
7
+ /**
8
+ * @file
9
+ * Common constants used in decoder and encoder API.
10
+ */
11
+
7
12
  #ifndef BROTLI_COMMON_CONSTANTS_H_
8
13
  #define BROTLI_COMMON_CONSTANTS_H_
9
14
 
15
+ #include "./platform.h"
16
+ #include <brotli/port.h>
17
+ #include <brotli/types.h>
18
+
10
19
  /* Specification: 7.3. Encoding of the context map */
11
20
  #define BROTLI_CONTEXT_MAP_MAX_RLE 16
12
21
 
@@ -29,12 +38,31 @@
29
38
  #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8
30
39
 
31
40
  /* "Large Window Brotli" */
41
+
42
+ /**
43
+ * The theoretical maximum number of distance bits specified for large window
44
+ * brotli, for 64-bit encoders and decoders. Even when in practice 32-bit
45
+ * encoders and decoders only support up to 30 max distance bits, the value is
46
+ * set to 62 because it affects the large window brotli file format.
47
+ * Specifically, it affects the encoding of simple huffman tree for distances,
48
+ * see Specification RFC 7932 chapter 3.4.
49
+ */
32
50
  #define BROTLI_LARGE_MAX_DISTANCE_BITS 62U
33
51
  #define BROTLI_LARGE_MIN_WBITS 10
52
+ /**
53
+ * The maximum supported large brotli window bits by the encoder and decoder.
54
+ * Large window brotli allows up to 62 bits, however the current encoder and
55
+ * decoder, designed for 32-bit integers, only support up to 30 bits maximum.
56
+ */
34
57
  #define BROTLI_LARGE_MAX_WBITS 30
35
58
 
36
59
  /* Specification: 4. Encoding of distances */
37
60
  #define BROTLI_NUM_DISTANCE_SHORT_CODES 16
61
+ /**
62
+ * Maximal number of "postfix" bits.
63
+ *
64
+ * Number of "postfix" bits is stored as 2 bits in meta-block header.
65
+ */
38
66
  #define BROTLI_MAX_NPOSTFIX 3
39
67
  #define BROTLI_MAX_NDIRECT 120
40
68
  #define BROTLI_MAX_DISTANCE_BITS 24U
@@ -45,9 +73,22 @@
45
73
  #define BROTLI_NUM_DISTANCE_SYMBOLS \
46
74
  BROTLI_DISTANCE_ALPHABET_SIZE( \
47
75
  BROTLI_MAX_NDIRECT, BROTLI_MAX_NPOSTFIX, BROTLI_LARGE_MAX_DISTANCE_BITS)
76
+
77
+ /* ((1 << 26) - 4) is the maximal distance that can be expressed in RFC 7932
78
+ brotli stream using NPOSTFIX = 0 and NDIRECT = 0. With other NPOSTFIX and
79
+ NDIRECT values distances up to ((1 << 29) + 88) could be expressed. */
48
80
  #define BROTLI_MAX_DISTANCE 0x3FFFFFC
81
+
82
+ /* ((1 << 31) - 4) is the safe distance limit. Using this number as a limit
83
+ allows safe distance calculation without overflows, given the distance
84
+ alphabet size is limited to corresponding size
85
+ (see kLargeWindowDistanceCodeLimits). */
49
86
  #define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC
50
87
 
88
+
89
+ /* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */
90
+ #define BROTLI_NUM_INS_COPY_CODES 24
91
+
51
92
  /* 7.1. Context modes and context ID lookup for literals */
52
93
  /* "context IDs for literals are in the range of 0..63" */
53
94
  #define BROTLI_LITERAL_CONTEXT_BITS 6
@@ -61,4 +102,99 @@
61
102
  #define BROTLI_WINDOW_GAP 16
62
103
  #define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)
63
104
 
105
+ typedef struct BrotliDistanceCodeLimit {
106
+ uint32_t max_alphabet_size;
107
+ uint32_t max_distance;
108
+ } BrotliDistanceCodeLimit;
109
+
110
+ /* This function calculates maximal size of distance alphabet, such that the
111
+ distances greater than the given values can not be represented.
112
+
113
+ This limits are designed to support fast and safe 32-bit decoders.
114
+ "32-bit" means that signed integer values up to ((1 << 31) - 1) could be
115
+ safely expressed.
116
+
117
+ Brotli distance alphabet symbols do not represent consecutive distance
118
+ ranges. Each distance alphabet symbol (excluding direct distances and short
119
+ codes), represent interleaved (for NPOSTFIX > 0) range of distances.
120
+ A "group" of consecutive (1 << NPOSTFIX) symbols represent non-interleaved
121
+ range. Two consecutive groups require the same amount of "extra bits".
122
+
123
+ It is important that distance alphabet represents complete "groups".
124
+ To avoid complex logic on encoder side about interleaved ranges
125
+ it was decided to restrict both sides to complete distance code "groups".
126
+ */
127
+ BROTLI_UNUSED_FUNCTION BrotliDistanceCodeLimit BrotliCalculateDistanceCodeLimit(
128
+ uint32_t max_distance, uint32_t npostfix, uint32_t ndirect) {
129
+ BrotliDistanceCodeLimit result;
130
+ /* Marking this function as unused, because not all files
131
+ including "constants.h" use it -> compiler warns about that. */
132
+ BROTLI_UNUSED(&BrotliCalculateDistanceCodeLimit);
133
+ if (max_distance <= ndirect) {
134
+ /* This case never happens / exists only for the sake of completeness. */
135
+ result.max_alphabet_size = max_distance + BROTLI_NUM_DISTANCE_SHORT_CODES;
136
+ result.max_distance = max_distance;
137
+ return result;
138
+ } else {
139
+ /* The first prohibited value. */
140
+ uint32_t forbidden_distance = max_distance + 1;
141
+ /* Subtract "directly" encoded region. */
142
+ uint32_t offset = forbidden_distance - ndirect - 1;
143
+ uint32_t ndistbits = 0;
144
+ uint32_t tmp;
145
+ uint32_t half;
146
+ uint32_t group;
147
+ /* Postfix for the last dcode in the group. */
148
+ uint32_t postfix = (1u << npostfix) - 1;
149
+ uint32_t extra;
150
+ uint32_t start;
151
+ /* Remove postfix and "head-start". */
152
+ offset = (offset >> npostfix) + 4;
153
+ /* Calculate the number of distance bits. */
154
+ tmp = offset / 2;
155
+ /* Poor-man's log2floor, to avoid extra dependencies. */
156
+ while (tmp != 0) {ndistbits++; tmp = tmp >> 1;}
157
+ /* One bit is covered with subrange addressing ("half"). */
158
+ ndistbits--;
159
+ /* Find subrange. */
160
+ half = (offset >> ndistbits) & 1;
161
+ /* Calculate the "group" part of dcode. */
162
+ group = ((ndistbits - 1) << 1) | half;
163
+ /* Calculated "group" covers the prohibited distance value. */
164
+ if (group == 0) {
165
+ /* This case is added for correctness; does not occur for limit > 128. */
166
+ result.max_alphabet_size = ndirect + BROTLI_NUM_DISTANCE_SHORT_CODES;
167
+ result.max_distance = ndirect;
168
+ return result;
169
+ }
170
+ /* Decrement "group", so it is the last permitted "group". */
171
+ group--;
172
+ /* After group was decremented, ndistbits and half must be recalculated. */
173
+ ndistbits = (group >> 1) + 1;
174
+ /* The last available distance in the subrange has all extra bits set. */
175
+ extra = (1u << ndistbits) - 1;
176
+ /* Calculate region start. NB: ndistbits >= 1. */
177
+ start = (1u << (ndistbits + 1)) - 4;
178
+ /* Move to subregion. */
179
+ start += (group & 1) << ndistbits;
180
+ /* Calculate the alphabet size. */
181
+ result.max_alphabet_size = ((group << npostfix) | postfix) + ndirect +
182
+ BROTLI_NUM_DISTANCE_SHORT_CODES + 1;
183
+ /* Calculate the maximal distance representable by alphabet. */
184
+ result.max_distance = ((start + extra) << npostfix) + postfix + ndirect + 1;
185
+ return result;
186
+ }
187
+ }
188
+
189
+ /* Represents the range of values belonging to a prefix code:
190
+ [offset, offset + 2^nbits) */
191
+ typedef struct {
192
+ uint16_t offset;
193
+ uint8_t nbits;
194
+ } BrotliPrefixCodeRange;
195
+
196
+ /* "Soft-private", it is exported, but not "advertised" as API. */
197
+ BROTLI_COMMON_API extern const BrotliPrefixCodeRange
198
+ _kBrotliPrefixCodeRanges[BROTLI_NUM_BLOCK_LEN_SYMBOLS];
199
+
64
200
  #endif /* BROTLI_COMMON_CONSTANTS_H_ */
@@ -0,0 +1,156 @@
1
+ #include "./context.h"
2
+
3
+ #include <brotli/types.h>
4
+
5
+ /* Common context lookup table for all context modes. */
6
+ const uint8_t _kBrotliContextLookupTable[2048] = {
7
+ /* CONTEXT_LSB6, last byte. */
8
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
9
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
10
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
11
+ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
12
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
13
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
14
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
15
+ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
16
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
17
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
18
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
19
+ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
20
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
21
+ 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
22
+ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
23
+ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
24
+
25
+ /* CONTEXT_LSB6, second last byte, */
26
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42
+
43
+ /* CONTEXT_MSB6, last byte. */
44
+ 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
45
+ 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
46
+ 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11,
47
+ 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
48
+ 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, 19, 19, 19, 19,
49
+ 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23,
50
+ 24, 24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27,
51
+ 28, 28, 28, 28, 29, 29, 29, 29, 30, 30, 30, 30, 31, 31, 31, 31,
52
+ 32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 34, 35, 35, 35, 35,
53
+ 36, 36, 36, 36, 37, 37, 37, 37, 38, 38, 38, 38, 39, 39, 39, 39,
54
+ 40, 40, 40, 40, 41, 41, 41, 41, 42, 42, 42, 42, 43, 43, 43, 43,
55
+ 44, 44, 44, 44, 45, 45, 45, 45, 46, 46, 46, 46, 47, 47, 47, 47,
56
+ 48, 48, 48, 48, 49, 49, 49, 49, 50, 50, 50, 50, 51, 51, 51, 51,
57
+ 52, 52, 52, 52, 53, 53, 53, 53, 54, 54, 54, 54, 55, 55, 55, 55,
58
+ 56, 56, 56, 56, 57, 57, 57, 57, 58, 58, 58, 58, 59, 59, 59, 59,
59
+ 60, 60, 60, 60, 61, 61, 61, 61, 62, 62, 62, 62, 63, 63, 63, 63,
60
+
61
+ /* CONTEXT_MSB6, second last byte, */
62
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78
+
79
+ /* CONTEXT_UTF8, last byte. */
80
+ /* ASCII range. */
81
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 0, 4, 0, 0,
82
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83
+ 8, 12, 16, 12, 12, 20, 12, 16, 24, 28, 12, 12, 32, 12, 36, 12,
84
+ 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 32, 32, 24, 40, 28, 12,
85
+ 12, 48, 52, 52, 52, 48, 52, 52, 52, 48, 52, 52, 52, 52, 52, 48,
86
+ 52, 52, 52, 52, 52, 48, 52, 52, 52, 52, 52, 24, 12, 28, 12, 12,
87
+ 12, 56, 60, 60, 60, 56, 60, 60, 60, 56, 60, 60, 60, 60, 60, 56,
88
+ 60, 60, 60, 60, 60, 56, 60, 60, 60, 60, 60, 24, 12, 28, 12, 0,
89
+ /* UTF8 continuation byte range. */
90
+ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
91
+ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
92
+ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
93
+ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
94
+ /* UTF8 lead byte range. */
95
+ 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
96
+ 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
97
+ 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
98
+ 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
99
+
100
+ /* CONTEXT_UTF8 second last byte. */
101
+ /* ASCII range. */
102
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104
+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
105
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
106
+ 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
107
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1,
108
+ 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
109
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 0,
110
+ /* UTF8 continuation byte range. */
111
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116
+ /* UTF8 lead byte range. */
117
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
119
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
120
+
121
+ /* CONTEXT_SIGNED, last byte, same as the above values shifted by 3 bits. */
122
+ 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
123
+ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
124
+ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
125
+ 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
126
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
127
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
128
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
129
+ 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
130
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
131
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
132
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
133
+ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
134
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
135
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
136
+ 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
137
+ 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 56,
138
+
139
+ /* CONTEXT_SIGNED, second last byte. */
140
+ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
141
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
142
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
143
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
144
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
145
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
146
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
147
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
148
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
149
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
150
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
151
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
152
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
153
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
154
+ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
155
+ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7,
156
+ };