brotli 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.travis.yml +11 -3
  4. data/Gemfile +2 -0
  5. data/ext/brotli/brotli.c +279 -0
  6. data/ext/brotli/brotli.h +2 -0
  7. data/ext/brotli/buffer.c +95 -0
  8. data/ext/brotli/buffer.h +19 -0
  9. data/ext/brotli/extconf.rb +21 -81
  10. data/lib/brotli/version.rb +1 -1
  11. data/vendor/brotli/dec/bit_reader.c +5 -5
  12. data/vendor/brotli/dec/bit_reader.h +15 -15
  13. data/vendor/brotli/dec/context.h +1 -1
  14. data/vendor/brotli/dec/decode.c +433 -348
  15. data/vendor/brotli/dec/decode.h +74 -48
  16. data/vendor/brotli/dec/huffman.c +5 -4
  17. data/vendor/brotli/dec/huffman.h +4 -4
  18. data/vendor/brotli/dec/port.h +2 -95
  19. data/vendor/brotli/dec/prefix.h +5 -3
  20. data/vendor/brotli/dec/state.c +15 -27
  21. data/vendor/brotli/dec/state.h +21 -17
  22. data/vendor/brotli/dec/transform.h +1 -1
  23. data/vendor/brotli/enc/backward_references.c +892 -0
  24. data/vendor/brotli/enc/backward_references.h +85 -102
  25. data/vendor/brotli/enc/backward_references_inc.h +147 -0
  26. data/vendor/brotli/enc/bit_cost.c +35 -0
  27. data/vendor/brotli/enc/bit_cost.h +23 -121
  28. data/vendor/brotli/enc/bit_cost_inc.h +127 -0
  29. data/vendor/brotli/enc/block_encoder_inc.h +33 -0
  30. data/vendor/brotli/enc/block_splitter.c +197 -0
  31. data/vendor/brotli/enc/block_splitter.h +40 -50
  32. data/vendor/brotli/enc/block_splitter_inc.h +432 -0
  33. data/vendor/brotli/enc/brotli_bit_stream.c +1334 -0
  34. data/vendor/brotli/enc/brotli_bit_stream.h +95 -167
  35. data/vendor/brotli/enc/cluster.c +56 -0
  36. data/vendor/brotli/enc/cluster.h +23 -305
  37. data/vendor/brotli/enc/cluster_inc.h +315 -0
  38. data/vendor/brotli/enc/command.h +83 -76
  39. data/vendor/brotli/enc/compress_fragment.c +747 -0
  40. data/vendor/brotli/enc/compress_fragment.h +48 -37
  41. data/vendor/brotli/enc/compress_fragment_two_pass.c +557 -0
  42. data/vendor/brotli/enc/compress_fragment_two_pass.h +37 -26
  43. data/vendor/brotli/enc/compressor.cc +139 -0
  44. data/vendor/brotli/enc/compressor.h +146 -0
  45. data/vendor/brotli/enc/context.h +102 -96
  46. data/vendor/brotli/enc/dictionary_hash.h +9 -5
  47. data/vendor/brotli/enc/encode.c +1562 -0
  48. data/vendor/brotli/enc/encode.h +211 -199
  49. data/vendor/brotli/enc/encode_parallel.cc +161 -151
  50. data/vendor/brotli/enc/encode_parallel.h +7 -8
  51. data/vendor/brotli/enc/entropy_encode.c +501 -0
  52. data/vendor/brotli/enc/entropy_encode.h +107 -89
  53. data/vendor/brotli/enc/entropy_encode_static.h +29 -62
  54. data/vendor/brotli/enc/fast_log.h +26 -20
  55. data/vendor/brotli/enc/find_match_length.h +23 -20
  56. data/vendor/brotli/enc/hash.h +614 -871
  57. data/vendor/brotli/enc/hash_forgetful_chain_inc.h +249 -0
  58. data/vendor/brotli/enc/hash_longest_match_inc.h +241 -0
  59. data/vendor/brotli/enc/hash_longest_match_quickly_inc.h +230 -0
  60. data/vendor/brotli/enc/histogram.c +95 -0
  61. data/vendor/brotli/enc/histogram.h +49 -83
  62. data/vendor/brotli/enc/histogram_inc.h +51 -0
  63. data/vendor/brotli/enc/literal_cost.c +178 -0
  64. data/vendor/brotli/enc/literal_cost.h +16 -10
  65. data/vendor/brotli/enc/memory.c +181 -0
  66. data/vendor/brotli/enc/memory.h +62 -0
  67. data/vendor/brotli/enc/metablock.c +515 -0
  68. data/vendor/brotli/enc/metablock.h +87 -57
  69. data/vendor/brotli/enc/metablock_inc.h +183 -0
  70. data/vendor/brotli/enc/port.h +73 -47
  71. data/vendor/brotli/enc/prefix.h +34 -61
  72. data/vendor/brotli/enc/quality.h +130 -0
  73. data/vendor/brotli/enc/ringbuffer.h +137 -122
  74. data/vendor/brotli/enc/{static_dict.cc → static_dict.c} +162 -139
  75. data/vendor/brotli/enc/static_dict.h +23 -18
  76. data/vendor/brotli/enc/static_dict_lut.h +11223 -12037
  77. data/vendor/brotli/enc/streams.cc +7 -7
  78. data/vendor/brotli/enc/streams.h +32 -32
  79. data/vendor/brotli/enc/{utf8_util.cc → utf8_util.c} +22 -20
  80. data/vendor/brotli/enc/utf8_util.h +16 -9
  81. data/vendor/brotli/enc/write_bits.h +49 -43
  82. metadata +34 -25
  83. data/ext/brotli/brotli.cc +0 -181
  84. data/vendor/brotli/dec/Makefile +0 -12
  85. data/vendor/brotli/dec/dictionary.c +0 -9466
  86. data/vendor/brotli/dec/dictionary.h +0 -38
  87. data/vendor/brotli/dec/types.h +0 -38
  88. data/vendor/brotli/enc/Makefile +0 -14
  89. data/vendor/brotli/enc/backward_references.cc +0 -858
  90. data/vendor/brotli/enc/block_splitter.cc +0 -505
  91. data/vendor/brotli/enc/brotli_bit_stream.cc +0 -1181
  92. data/vendor/brotli/enc/compress_fragment.cc +0 -701
  93. data/vendor/brotli/enc/compress_fragment_two_pass.cc +0 -524
  94. data/vendor/brotli/enc/dictionary.cc +0 -9466
  95. data/vendor/brotli/enc/dictionary.h +0 -41
  96. data/vendor/brotli/enc/encode.cc +0 -1180
  97. data/vendor/brotli/enc/entropy_encode.cc +0 -480
  98. data/vendor/brotli/enc/histogram.cc +0 -67
  99. data/vendor/brotli/enc/literal_cost.cc +0 -165
  100. data/vendor/brotli/enc/metablock.cc +0 -539
  101. data/vendor/brotli/enc/transform.h +0 -248
  102. data/vendor/brotli/enc/types.h +0 -29
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd4d6db73c2704ff5b79264456b8ae9f371beb36
4
- data.tar.gz: 33b074ac4545d43ed2aeb00713573754e24cb4c0
3
+ metadata.gz: f05525cdd6344037598d1f2ec2c1305e03d6bc72
4
+ data.tar.gz: 25c48ffa9319a43a73da655c11c5756caee19875
5
5
  SHA512:
6
- metadata.gz: 536ba6e984a19db3e78f434f9f1b9b91cb90c5d7d1e0bbf84ee436382c05afee7e4912ae3ee03c37bbf732d9d59bc6be80f55dbf0fc245256c14cae264b87fad
7
- data.tar.gz: 553ae97d40b5df932133af25c3075194a0754b56e0e2fe233302918b94c2cc0aeaf5373f18e74e3bb798bca68e3bbf30d34c35f04a21319880c6043e1052efe2
6
+ metadata.gz: e5e83a7e936a57db7949a2015517f3c8030b3051d8a7b4051f35e1c91d62e9889c982b6026817b9cf75c678f0881ea3fbeacfafe44fc8d841b69771a66da9874
7
+ data.tar.gz: 51f70a985a8684338a2c96465bac6147921c7531649b2d75517d739782fc28485735c6cd3e22cbf400bf2bf9b6346a8f54aede3291ae2d2bb4e02a4b974e9823
data/.gitignore CHANGED
@@ -12,3 +12,6 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ /ext/brotli/common
16
+ /ext/brotli/dec
17
+ /ext/brotli/enc
data/.travis.yml CHANGED
@@ -3,14 +3,22 @@ sudo: false
3
3
  cache:
4
4
  bundler: true
5
5
  directories:
6
- - $HOME/.ccache
7
-
6
+ - "$HOME/.ccache"
8
7
  rvm:
9
8
  - 2.2.0
10
- - 2.3.2
9
+ - 2.3.3
10
+ - 2.4.0
11
11
  env:
12
12
  global:
13
13
  - RANTLY_COUNT=10000
14
14
  - PATH="/usr/lib/ccache:$PATH"
15
15
  before_install:
16
16
  - gem install bundler
17
+ deploy:
18
+ provider: rubygems
19
+ api_key:
20
+ secure: pHMMdI6cthdpQ8XHK4LPMRJK9yndRNRprSpttbK+M2Cq6O8ABIx9YsaiR4U6+kkS4RXwWnnJ6qkglQVLsCd1x+elJA0V8MxhOdzKyQgNC5wbIhtIdEZ9cN0c/hG8e9pKa35K9q9ETJ4M8QWNPxkJTRKw1yM8rIMbzf0oy0Btrcz+pJG8C9cMQwfV3QGJFPdO4Hcb5v2gK9rMDU5v3OD9CgbQqaZN3tbR2G1WTiAkFk86RlwPTyaurIRiUV/AQedPSPHHJ5SYbThjcu5iy1OtlFHkVTy/5QNm8ooJkX3pAVhnexTL/iufP0YI9xmYPq4k4RxK1ywSATbvz13JKyD94Sdlhpblz4kJaA4Fg3td8qQtZnFEICBuiW1yelWwVXbyb+zjeL8K4EizoqZV2WwKq2GR3fmyAgFHWiVXRbu0aNjJi23rpNkAGif41vAbsskgBgOiKYsW5/ZOE2opL7lH5l8GGjiYQ8cOKbiNKpUSFaCfwOwgZt4QBTNsN+GuasLNZkm3Dt4Wj7KhFJ+HtAfTnSpomsSQ5/wGwMUchQKo9mfSE0lmKglFyvRV23+WnEX0ZC2jUIJmaxGn1PajDL5tjnZOd5mZAehrc74r84o5olM39y7DynU6FP9QaVBaXF/vue4kIbL0ePSxxwMIYErOh0pOWbWx7ipMfDn+1jfsmvA=
21
+ gem: brotli
22
+ on:
23
+ tags: true
24
+ repo: miyucy/brotli
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in brotli.gemspec
4
4
  gemspec
5
+
6
+ gem 'travis'
@@ -0,0 +1,279 @@
1
+ #include "brotli.h"
2
+
3
+ #define CSTR2SYM(x) ID2SYM(rb_intern(x))
4
+
5
+ static VALUE rb_mBrotli;
6
+ static VALUE rb_eBrotli;
7
+
8
+ static void*
9
+ brotli_alloc(void* opaque, size_t size)
10
+ {
11
+ return malloc(size);
12
+ }
13
+
14
+ static void
15
+ brotli_free(void* opaque, void* address)
16
+ {
17
+ if (address) {
18
+ free(address);
19
+ }
20
+ }
21
+
22
+ /*******************************************************************************
23
+ * inflate
24
+ ******************************************************************************/
25
+
26
+ typedef struct {
27
+ uint8_t* str;
28
+ size_t len;
29
+ BrotliDecoderState* s;
30
+ buffer_t* buffer;
31
+ BrotliDecoderResult r;
32
+ } brotli_inflate_args_t;
33
+
34
+ static void*
35
+ brotli_inflate_no_gvl(void *arg)
36
+ {
37
+ brotli_inflate_args_t *args = (brotli_inflate_args_t*)arg;
38
+ uint8_t output[BUFSIZ];
39
+ BrotliDecoderResult r = BROTLI_RESULT_ERROR;
40
+ size_t available_in = args->len;
41
+ const uint8_t* next_in = args->str;
42
+ size_t available_out = BUFSIZ;
43
+ uint8_t* next_out = output;
44
+ size_t total_out = 0;
45
+ buffer_t* buffer = args->buffer;
46
+ BrotliDecoderState* s = args->s;
47
+
48
+ for (;;) {
49
+ r = BrotliDecoderDecompressStream(s,
50
+ &available_in, &next_in,
51
+ &available_out, &next_out,
52
+ &total_out);
53
+ /* success, error or needs more input */
54
+ if (r != BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
55
+ break;
56
+ }
57
+ append_buffer(buffer, output, BUFSIZ);
58
+ available_out = BUFSIZ;
59
+ next_out = output;
60
+ }
61
+
62
+ if (r == BROTLI_DECODER_RESULT_SUCCESS) {
63
+ if (next_out != output) {
64
+ append_buffer(buffer, output, next_out - output);
65
+ }
66
+ }
67
+ args->r = r;
68
+
69
+ return arg;
70
+ }
71
+
72
+ static VALUE
73
+ brotli_inflate(VALUE self, VALUE str)
74
+ {
75
+ VALUE value = Qnil;
76
+ brotli_inflate_args_t args;
77
+
78
+ StringValue(str);
79
+
80
+ args.str = (uint8_t*)RSTRING_PTR(str);
81
+ args.len = (size_t)RSTRING_LEN(str);
82
+ args.buffer = create_buffer(BUFSIZ);
83
+ args.s = BrotliDecoderCreateInstance(brotli_alloc,
84
+ brotli_free,
85
+ NULL);
86
+ args.r = BROTLI_RESULT_ERROR;
87
+
88
+ rb_thread_call_without_gvl(brotli_inflate_no_gvl, (void *)&args, NULL, NULL);
89
+ if (args.r == BROTLI_DECODER_RESULT_SUCCESS) {
90
+ value = rb_str_new(args.buffer->ptr, args.buffer->used);
91
+ delete_buffer(args.buffer);
92
+ BrotliDecoderDestroyInstance(args.s);
93
+ } else if (args.r == BROTLI_DECODER_RESULT_ERROR) {
94
+ const char * error = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(args.s));
95
+ delete_buffer(args.buffer);
96
+ BrotliDecoderDestroyInstance(args.s);
97
+ rb_raise(rb_eBrotli, "%s", error);
98
+ } else if (args.r == BROTLI_RESULT_NEEDS_MORE_INPUT) {
99
+ delete_buffer(args.buffer);
100
+ BrotliDecoderDestroyInstance(args.s);
101
+ rb_raise(rb_eBrotli, "Needs more input");
102
+ } else if (args.r == BROTLI_RESULT_NEEDS_MORE_OUTPUT) {
103
+ /* never reach to this block */
104
+ delete_buffer(args.buffer);
105
+ BrotliDecoderDestroyInstance(args.s);
106
+ rb_raise(rb_eBrotli, "Needs more output");
107
+ }
108
+
109
+ return value;
110
+ }
111
+
112
+ /*******************************************************************************
113
+ * deflate
114
+ ******************************************************************************/
115
+
116
+ static void
117
+ brotli_deflate_set_mode(BrotliEncoderState* s, VALUE value)
118
+ {
119
+ if (NIL_P(value)) {
120
+ return;
121
+ } else {
122
+ if (value == CSTR2SYM("generic")) {
123
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_GENERIC);
124
+ } else if (value == CSTR2SYM("text")) {
125
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_TEXT);
126
+ } else if (value == CSTR2SYM("font")) {
127
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_FONT);
128
+ } else {
129
+ rb_raise(rb_eArgError, "invalid mode");
130
+ }
131
+ }
132
+ }
133
+
134
+ static void
135
+ brotli_deflate_set_quality(BrotliEncoderState* s, VALUE value)
136
+ {
137
+ uint32_t param;
138
+ if (NIL_P(value)) {
139
+ return;
140
+ } else {
141
+ param = NUM2INT(value);
142
+ if (0 <= param && param <= 11) {
143
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, param);
144
+ } else {
145
+ rb_raise(rb_eArgError, "invalid quality value. Should be 0 to 11.");
146
+ }
147
+ }
148
+ }
149
+
150
+ static void
151
+ brotli_deflate_set_lgwin(BrotliEncoderState* s, VALUE value)
152
+ {
153
+ uint32_t param;
154
+ if (NIL_P(value)) {
155
+ return;
156
+ } else {
157
+ param = NUM2INT(value);
158
+ if (10 <= param && param <= 24) {
159
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, param);
160
+ } else {
161
+ rb_raise(rb_eArgError, "invalid lgwin value. Should be 10 to 24.");
162
+ }
163
+ }
164
+ }
165
+
166
+ static void
167
+ brotli_deflate_set_lgblock(BrotliEncoderState* s, VALUE value)
168
+ {
169
+ uint32_t param;
170
+ if (NIL_P(value)) {
171
+ return;
172
+ } else {
173
+ param = NUM2INT(value);
174
+ if ((param == 0) || (16 <= param && param <= 24)) {
175
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGBLOCK, param);
176
+ } else {
177
+ rb_raise(rb_eArgError, "invalid lgblock value. Should be 0 or 16 to 24.");
178
+ }
179
+ }
180
+ }
181
+
182
+ static BrotliEncoderState*
183
+ brotli_deflate_parse_options(BrotliEncoderState* s, VALUE opts)
184
+ {
185
+ if (!NIL_P(opts)) {
186
+ brotli_deflate_set_mode(s, rb_hash_aref(opts, CSTR2SYM("mode")));
187
+ brotli_deflate_set_quality(s, rb_hash_aref(opts, CSTR2SYM("quality")));
188
+ brotli_deflate_set_lgwin(s, rb_hash_aref(opts, CSTR2SYM("lgwin")));
189
+ brotli_deflate_set_lgblock(s, rb_hash_aref(opts, CSTR2SYM("lgblock")));
190
+ }
191
+
192
+ return s;
193
+ }
194
+
195
+ typedef struct {
196
+ uint8_t *str;
197
+ size_t len;
198
+ BrotliEncoderState* s;
199
+ buffer_t* buffer;
200
+ BROTLI_BOOL finished;
201
+ } brotli_deflate_args_t;
202
+
203
+ static void*
204
+ brotli_deflate_no_gvl(void *arg)
205
+ {
206
+ brotli_deflate_args_t *args = (brotli_deflate_args_t *)arg;
207
+ uint8_t output[BUFSIZ];
208
+ BROTLI_BOOL r = BROTLI_FALSE;
209
+ size_t available_in = args->len;
210
+ const uint8_t* next_in = args->str;
211
+ size_t available_out = BUFSIZ;
212
+ uint8_t* next_out = output;
213
+ size_t total_out = 0;
214
+ buffer_t* buffer = args->buffer;
215
+ BrotliEncoderState* s = args->s;
216
+
217
+ for (;;) {
218
+ r = BrotliEncoderCompressStream(s,
219
+ BROTLI_OPERATION_FINISH,
220
+ &available_in, &next_in,
221
+ &available_out, &next_out, &total_out);
222
+ if (r == BROTLI_FALSE) {
223
+ args->finished = BROTLI_FALSE;
224
+ break;
225
+ } else {
226
+ append_buffer(buffer, output, next_out - output);
227
+ available_out = BUFSIZ;
228
+ next_out = output;
229
+
230
+ if (BrotliEncoderIsFinished(args->s)) {
231
+ args->finished = BROTLI_TRUE;
232
+ break;
233
+ }
234
+ }
235
+ }
236
+
237
+ return arg;
238
+ }
239
+
240
+ static VALUE
241
+ brotli_deflate(int argc, VALUE *argv, VALUE self)
242
+ {
243
+ VALUE str = Qnil, opts = Qnil, value = Qnil;
244
+ brotli_deflate_args_t args;
245
+
246
+ rb_scan_args(argc, argv, "11", &str, &opts);
247
+ StringValue(str);
248
+
249
+ args.str = (uint8_t*)RSTRING_PTR(str);
250
+ args.len = (size_t)RSTRING_LEN(str);
251
+ args.s = brotli_deflate_parse_options(
252
+ BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL),
253
+ opts);
254
+ args.buffer = create_buffer(BUFSIZ);
255
+ args.finished = BROTLI_FALSE;
256
+
257
+ rb_thread_call_without_gvl(brotli_deflate_no_gvl, (void *)&args, NULL, NULL);
258
+ if (args.finished == BROTLI_TRUE) {
259
+ value = rb_str_new(args.buffer->ptr, args.buffer->used);
260
+ }
261
+
262
+ delete_buffer(args.buffer);
263
+ BrotliEncoderDestroyInstance(args.s);
264
+
265
+ return value;
266
+ }
267
+
268
+ /*******************************************************************************
269
+ * entry
270
+ ******************************************************************************/
271
+
272
+ void
273
+ Init_brotli(void)
274
+ {
275
+ rb_mBrotli = rb_define_module("Brotli");
276
+ rb_eBrotli = rb_define_class_under(rb_mBrotli, "Error", rb_eStandardError);
277
+ rb_define_singleton_method(rb_mBrotli, "deflate", RUBY_METHOD_FUNC(brotli_deflate), -1);
278
+ rb_define_singleton_method(rb_mBrotli, "inflate", RUBY_METHOD_FUNC(brotli_inflate), 1);
279
+ }
data/ext/brotli/brotli.h CHANGED
@@ -2,7 +2,9 @@
2
2
  #define BROTLI_H 1
3
3
 
4
4
  #include "ruby.h"
5
+ #include "ruby/thread.h"
5
6
  #include "enc/encode.h"
6
7
  #include "dec/decode.h"
8
+ #include "buffer.h"
7
9
 
8
10
  #endif /* BROTLI_H */
@@ -0,0 +1,95 @@
1
+ #include "buffer.h"
2
+ #define INITIAL (1024)
3
+
4
+ buffer_t*
5
+ create_buffer(const size_t initial) {
6
+ buffer_t *buffer = malloc(sizeof(buffer_t));
7
+ if (buffer == NULL) {
8
+ return NULL;
9
+ }
10
+
11
+ buffer->used = 0;
12
+ buffer->expand_count = 0;
13
+ buffer->expand_ratio = 130;
14
+ buffer->size = (size_t) (initial > 0 ? initial : INITIAL);
15
+ buffer->ptr = malloc(buffer->size);
16
+ if (buffer->ptr == NULL) {
17
+ delete_buffer(buffer);
18
+ return NULL;
19
+ }
20
+
21
+ return buffer;
22
+ }
23
+
24
+ void
25
+ delete_buffer(buffer_t* buffer) {
26
+ if (buffer->ptr != NULL) {
27
+ free(buffer->ptr);
28
+ buffer->ptr = NULL;
29
+ }
30
+ free(buffer);
31
+ }
32
+
33
+ static
34
+ buffer_t*
35
+ expand_buffer(buffer_t* const buffer, const size_t need) {
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;
44
+ buffer->size = size;
45
+ buffer->expand_count += 1;
46
+ return buffer;
47
+ }
48
+
49
+ buffer_t*
50
+ append_buffer(buffer_t* buffer, const void* ptr, const size_t size) {
51
+ if (buffer->used + size > buffer->size) {
52
+ if (expand_buffer(buffer, buffer->used + size) == NULL) {
53
+ return NULL;
54
+ }
55
+ }
56
+ memcpy(buffer->ptr + buffer->used, ptr, size);
57
+ buffer->used += size;
58
+ return buffer;
59
+ }
60
+
61
+ #if 0
62
+ #include <stdio.h>
63
+
64
+ void
65
+ inspect_buffer(buffer_t* buffer) {
66
+ printf("ptr=%p size=%d used=%d expc=%d\n",
67
+ buffer->ptr,
68
+ buffer->size,
69
+ buffer->used,
70
+ buffer->expand_count);
71
+ }
72
+
73
+ int
74
+ main(int argc, char *argv[])
75
+ {
76
+ buffer_t* b = create_buffer(8);
77
+
78
+ inspect_buffer(b);
79
+ append_buffer(b, "1111", 4);
80
+ inspect_buffer(b);
81
+ append_buffer(b, "2222", 4);
82
+ inspect_buffer(b);
83
+ append_buffer(b, "3333", 4);
84
+ inspect_buffer(b);
85
+ append_buffer(b, "4444", 4);
86
+ inspect_buffer(b);
87
+ append_buffer(b, "5555", 4);
88
+ inspect_buffer(b);
89
+ append_buffer(b, "66", 2);
90
+ inspect_buffer(b);
91
+
92
+ delete_buffer(b);
93
+ return 0;
94
+ }
95
+ #endif
@@ -0,0 +1,19 @@
1
+ #ifndef __BUFFER_HEADER__
2
+ #include <stdint.h>
3
+ #include <stdlib.h>
4
+ #include <stddef.h>
5
+ #include <string.h>
6
+
7
+ typedef struct {
8
+ uint8_t* ptr;
9
+ size_t size;
10
+ size_t used;
11
+ size_t expand_ratio;
12
+ size_t expand_count;
13
+ } buffer_t;
14
+
15
+ buffer_t* create_buffer(const size_t initial);
16
+ void delete_buffer(buffer_t* buffer);
17
+ buffer_t* append_buffer(buffer_t* buffer, const void* ptr, const size_t size);
18
+
19
+ #endif // __BUFFER_HEADER__
@@ -3,94 +3,34 @@ require 'fileutils'
3
3
  require 'rbconfig'
4
4
 
5
5
  $CPPFLAGS << ' -DOS_MACOSX' if RbConfig::CONFIG['host_os'] =~ /darwin|mac os/
6
- $INCFLAGS << ' -I./enc -I./dec'
6
+ $INCFLAGS << ' -I./enc -I./dec -I./common'
7
+ create_makefile('brotli/brotli')
8
+
9
+ def acopy(dir)
10
+ # source dir
11
+ FileUtils.mkdir_p File.expand_path(File.join(__dir__, dir))
12
+ # object dir
13
+ FileUtils.mkdir_p dir
14
+
15
+ files = Dir[File.expand_path(File.join(__dir__, '..', '..', 'vendor', 'brotli', dir, '*.[ch]'))]
16
+ FileUtils.cp files, dir, verbose: true
7
17
 
8
- FileUtils.mkdir_p 'enc'
9
- FileUtils.mkdir_p 'dec'
10
- FileUtils.mkdir_p File.join(__dir__, 'enc')
11
- FileUtils.mkdir_p File.join(__dir__, 'dec')
18
+ srcs = files.map { |e| File.basename e }.select { |e| e.end_with? '.c' }.map { |e| File.join(dir, e) }
19
+ objs = srcs.map { |e| e.sub(/\.c\z/, '.' + $OBJEXT) }
20
+ [srcs, objs]
21
+ end
12
22
 
13
23
  srcs = []
14
24
  objs = []
15
- [
16
- 'dec/bit_reader.c',
17
- 'dec/bit_reader.h',
18
- 'dec/context.h',
19
- 'dec/decode.c',
20
- 'dec/decode.h',
21
- 'dec/dictionary.c',
22
- 'dec/dictionary.h',
23
- 'dec/huffman.c',
24
- 'dec/huffman.h',
25
- 'dec/port.h',
26
- 'dec/prefix.h',
27
- 'dec/state.c',
28
- 'dec/state.h',
29
- 'dec/transform.h',
30
- 'dec/types.h',
31
- 'enc/backward_references.cc',
32
- 'enc/backward_references.h',
33
- 'enc/bit_cost.h',
34
- 'enc/block_splitter.cc',
35
- 'enc/block_splitter.h',
36
- 'enc/brotli_bit_stream.cc',
37
- 'enc/brotli_bit_stream.h',
38
- 'enc/cluster.h',
39
- 'enc/command.h',
40
- 'enc/compress_fragment.cc',
41
- 'enc/compress_fragment.h',
42
- 'enc/compress_fragment_two_pass.cc',
43
- 'enc/compress_fragment_two_pass.h',
44
- 'enc/compressor.h',
45
- 'enc/context.h',
46
- #'enc/dictionary.cc',
47
- 'enc/dictionary.h',
48
- 'enc/dictionary_hash.h',
49
- 'enc/encode.cc',
50
- 'enc/encode.h',
51
- 'enc/encode_parallel.cc',
52
- 'enc/encode_parallel.h',
53
- 'enc/entropy_encode.cc',
54
- 'enc/entropy_encode.h',
55
- 'enc/entropy_encode_static.h',
56
- 'enc/fast_log.h',
57
- 'enc/find_match_length.h',
58
- 'enc/hash.h',
59
- 'enc/histogram.cc',
60
- 'enc/histogram.h',
61
- 'enc/literal_cost.cc',
62
- 'enc/literal_cost.h',
63
- 'enc/metablock.cc',
64
- 'enc/metablock.h',
65
- 'enc/port.h',
66
- 'enc/prefix.h',
67
- 'enc/ringbuffer.h',
68
- 'enc/static_dict.cc',
69
- 'enc/static_dict.h',
70
- 'enc/static_dict_lut.h',
71
- 'enc/streams.cc',
72
- 'enc/streams.h',
73
- 'enc/transform.h',
74
- 'enc/types.h',
75
- 'enc/utf8_util.cc',
76
- 'enc/utf8_util.h',
77
- 'enc/write_bits.h',
78
- ].each do |file|
79
- FileUtils.cp(File.expand_path(File.join(__dir__, '..', '..', 'vendor', 'brotli', file)),
80
- File.expand_path(File.join(__dir__, file)),
81
- verbose: true)
82
- if file.end_with?('.c') or file.end_with?('.cc')
83
- srcs.push file
84
- objs.push file.sub(/\.cc?\z/, '.' + $OBJEXT)
85
- end
25
+ %w(enc dec common).each do |dir|
26
+ a, b = acopy dir
27
+ srcs.concat a
28
+ objs.concat b
86
29
  end
87
30
 
88
- have_library('stdc++')
89
- create_makefile('brotli/brotli')
90
-
91
31
  File.open('Makefile', 'r+') do |f|
92
- src = 'ORIG_SRCS = brotli.cc'
93
- obj = 'OBJS = brotli.o'
32
+ src = 'ORIG_SRCS = brotli.c buffer.c'
33
+ obj = 'OBJS = brotli.o buffer.o'
94
34
  txt = f.read
95
35
  .sub(/^#{src}$/, src + ' ' + srcs.join(' '))
96
36
  .sub(/^#{obj}$/, obj + ' ' + objs.join(' '))
@@ -1,3 +1,3 @@
1
1
  module Brotli
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
@@ -8,8 +8,8 @@
8
8
 
9
9
  #include "./bit_reader.h"
10
10
 
11
+ #include "../common/types.h"
11
12
  #include "./port.h"
12
- #include "./types.h"
13
13
 
14
14
  #if defined(__cplusplus) || defined(c_plusplus)
15
15
  extern "C" {
@@ -20,7 +20,7 @@ void BrotliInitBitReader(BrotliBitReader* const br) {
20
20
  br->bit_pos_ = sizeof(br->val_) << 3;
21
21
  }
22
22
 
23
- int BrotliWarmupBitReader(BrotliBitReader* const br) {
23
+ BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {
24
24
  size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1;
25
25
  /* Fixing alignment after unaligned BrotliFillWindow would result accumulator
26
26
  overflow. If unalignment is caused by BrotliSafeReadBits, then there is
@@ -30,17 +30,17 @@ int BrotliWarmupBitReader(BrotliBitReader* const br) {
30
30
  }
31
31
  if (BrotliGetAvailableBits(br) == 0) {
32
32
  if (!BrotliPullByte(br)) {
33
- return 0;
33
+ return BROTLI_FALSE;
34
34
  }
35
35
  }
36
36
 
37
37
  while ((((size_t)br->next_in) & aligned_read_mask) != 0) {
38
38
  if (!BrotliPullByte(br)) {
39
39
  /* If we consumed all the input, we don't care about the alignment. */
40
- return 1;
40
+ return BROTLI_TRUE;
41
41
  }
42
42
  }
43
- return 1;
43
+ return BROTLI_TRUE;
44
44
  }
45
45
 
46
46
  #if defined(__cplusplus) || defined(c_plusplus)