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
@@ -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,32 +1,51 @@
1
- require 'mkmf'
2
- require 'fileutils'
3
- require 'rbconfig'
1
+ require "mkmf"
2
+ require "fileutils"
3
+ require "rbconfig"
4
4
 
5
- $CPPFLAGS << ' -DOS_MACOSX' if RbConfig::CONFIG['host_os'] =~ /darwin|mac os/
6
- $INCFLAGS << ' -I$(srcdir)/enc -I$(srcdir)/dec -I$(srcdir)/common -I$(srcdir)/include'
7
- create_makefile('brotli/brotli')
5
+ dir_config("brotli")
8
6
 
9
- __DIR__ = File.expand_path(File.dirname __FILE__)
7
+ # libbrotli-dev
8
+ have_dev_pkg = [
9
+ have_header("brotli/decode.h"),
10
+ have_header("brotli/encode.h"),
11
+ pkg_config("libbrotlicommon"),
12
+ pkg_config("libbrotlidec"),
13
+ pkg_config("libbrotlienc")
14
+ ].all? { |e| e }
10
15
 
11
- %w[enc dec common include].each do |dirname|
12
- FileUtils.mkdir_p dirname
13
- FileUtils.cp_r File.expand_path(File.join(__DIR__, '..', '..', 'vendor', 'brotli', 'c', dirname), __DIR__), __DIR__, verbose: true
14
- 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
15
18
 
16
- srcs = []
17
- objs = []
18
- Dir[File.expand_path(File.join('{enc,dec,common,include}', '**', '*.c'), __DIR__)].sort.each do |file|
19
- file[__DIR__ + File::SEPARATOR] = ''
20
- srcs << file
21
- objs << file.sub(/\.c\z/, '.' + RbConfig::CONFIG['OBJEXT'])
22
- end
19
+ create_makefile("brotli/brotli")
20
+
21
+ unless have_dev_pkg
22
+ __DIR__ = File.expand_path(File.dirname(__FILE__))
23
+
24
+ %w[enc dec common include].each do |dirname|
25
+ FileUtils.mkdir_p dirname
26
+ FileUtils.cp_r(
27
+ File.expand_path(File.join(__DIR__, "..", "..", "vendor", "brotli", "c", dirname), __DIR__),
28
+ __DIR__,
29
+ verbose: true
30
+ )
31
+ end
32
+
33
+ srcs = []
34
+ objs = []
35
+ Dir[File.expand_path(File.join("{enc,dec,common,include}", "**", "*.c"), __DIR__)].sort.each do |file|
36
+ file[__DIR__ + File::SEPARATOR] = ""
37
+ srcs << file
38
+ objs << file.sub(/\.c\z/, "." + RbConfig::CONFIG["OBJEXT"])
39
+ end
23
40
 
24
- File.open('Makefile', 'r+') do |f|
25
- src = 'ORIG_SRCS = brotli.c buffer.c'
26
- obj = 'OBJS = brotli.o buffer.o'
27
- txt = f.read
28
- .sub(/^ORIG_SRCS = .*$/, src + ' ' + srcs.join(' '))
29
- .sub(/^OBJS = .*$/, obj + ' ' + objs.join(' '))
30
- f.rewind
31
- f.write txt
41
+ File.open("Makefile", "r+") do |f|
42
+ obj_ext = RbConfig::CONFIG["OBJEXT"]
43
+ src = "ORIG_SRCS = brotli.c buffer.c"
44
+ obj = "OBJS = brotli.#{obj_ext} buffer.#{obj_ext}"
45
+ txt = f.read
46
+ .sub(/^ORIG_SRCS = .*$/, "#{src} #{srcs.join(" ")}")
47
+ .sub(/^OBJS = .*$/, "#{obj} #{objs.join(" ")}")
48
+ f.rewind
49
+ f.write txt
50
+ end
32
51
  end
@@ -1,3 +1,3 @@
1
1
  module Brotli
2
- VERSION = '0.2.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/smoke.sh CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/bin/bash
2
2
  gem list | grep brotli && gem uninstall --force brotli
3
3
  bundle exec rake clobber build
4
- gem install --force --local --no-ri --no-rdoc "$(ls pkg/brotli-*.gem)"
4
+ gem install --force --local "$(ls pkg/brotli-*.gem)"
5
5
  cat <<EOF | ruby
6
6
  require 'brotli'
7
7
  abort if Brotli.inflate(Brotli.deflate(File.read('smoke.sh'))) != File.read('smoke.sh')
@@ -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
 
@@ -28,19 +37,58 @@
28
37
  /* "code length of 8 is repeated" */
29
38
  #define BROTLI_INITIAL_REPEATED_CODE_LENGTH 8
30
39
 
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
+ */
50
+ #define BROTLI_LARGE_MAX_DISTANCE_BITS 62U
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
+ */
57
+ #define BROTLI_LARGE_MAX_WBITS 30
58
+
31
59
  /* Specification: 4. Encoding of distances */
32
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
+ */
33
66
  #define BROTLI_MAX_NPOSTFIX 3
34
67
  #define BROTLI_MAX_NDIRECT 120
35
68
  #define BROTLI_MAX_DISTANCE_BITS 24U
36
- /* BROTLI_NUM_DISTANCE_SYMBOLS == 520 */
37
- #define BROTLI_NUM_DISTANCE_SYMBOLS (BROTLI_NUM_DISTANCE_SHORT_CODES + \
38
- BROTLI_MAX_NDIRECT + \
39
- (BROTLI_MAX_DISTANCE_BITS << \
40
- (BROTLI_MAX_NPOSTFIX + 1)))
41
- /* Distance that is guaranteed to be representable in any stream. */
69
+ #define BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX, NDIRECT, MAXNBITS) ( \
70
+ BROTLI_NUM_DISTANCE_SHORT_CODES + (NDIRECT) + \
71
+ ((MAXNBITS) << ((NPOSTFIX) + 1)))
72
+ /* BROTLI_NUM_DISTANCE_SYMBOLS == 1128 */
73
+ #define BROTLI_NUM_DISTANCE_SYMBOLS \
74
+ BROTLI_DISTANCE_ALPHABET_SIZE( \
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. */
42
80
  #define BROTLI_MAX_DISTANCE 0x3FFFFFC
43
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). */
86
+ #define BROTLI_MAX_ALLOWED_DISTANCE 0x7FFFFFFC
87
+
88
+
89
+ /* Specification: 4. Encoding of Literal Insertion Lengths and Copy Lengths */
90
+ #define BROTLI_NUM_INS_COPY_CODES 24
91
+
44
92
  /* 7.1. Context modes and context ID lookup for literals */
45
93
  /* "context IDs for literals are in the range of 0..63" */
46
94
  #define BROTLI_LITERAL_CONTEXT_BITS 6
@@ -54,4 +102,99 @@
54
102
  #define BROTLI_WINDOW_GAP 16
55
103
  #define BROTLI_MAX_BACKWARD_LIMIT(W) (((size_t)1 << (W)) - BROTLI_WINDOW_GAP)
56
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
+
57
200
  #endif /* BROTLI_COMMON_CONSTANTS_H_ */