brotli 0.6.0 → 0.9.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 (127) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +163 -2
  3. data/ext/brotli/brotli.c +971 -218
  4. data/ext/brotli/brotli.h +1 -3
  5. data/ext/brotli/buffer.c +63 -60
  6. data/ext/brotli/buffer.h +8 -9
  7. data/ext/brotli/extconf.rb +130 -40
  8. data/lib/brotli/reader.rb +167 -0
  9. data/lib/brotli/version.rb +1 -1
  10. data/lib/brotli/writer.rb +81 -0
  11. data/lib/brotli.rb +2 -0
  12. data/vendor/brotli/c/common/constants.c +7 -7
  13. data/vendor/brotli/c/common/constants.h +2 -5
  14. data/vendor/brotli/c/common/context.c +2 -2
  15. data/vendor/brotli/c/common/context.h +1 -2
  16. data/vendor/brotli/c/common/dictionary.c +4 -5856
  17. data/vendor/brotli/c/common/dictionary.h +1 -2
  18. data/vendor/brotli/c/common/dictionary_inc.h +5847 -0
  19. data/vendor/brotli/c/common/platform.c +0 -4
  20. data/vendor/brotli/c/common/platform.h +182 -43
  21. data/vendor/brotli/c/common/shared_dictionary.c +3 -7
  22. data/vendor/brotli/c/common/shared_dictionary_internal.h +1 -1
  23. data/vendor/brotli/c/common/static_init.h +56 -0
  24. data/vendor/brotli/c/common/transform.c +6 -4
  25. data/vendor/brotli/c/common/transform.h +1 -2
  26. data/vendor/brotli/c/common/version.h +5 -5
  27. data/vendor/brotli/c/dec/bit_reader.c +2 -3
  28. data/vendor/brotli/c/dec/bit_reader.h +0 -4
  29. data/vendor/brotli/c/dec/decode.c +187 -69
  30. data/vendor/brotli/c/dec/huffman.c +2 -5
  31. data/vendor/brotli/c/dec/huffman.h +0 -2
  32. data/vendor/brotli/c/dec/prefix.c +67 -0
  33. data/vendor/brotli/c/dec/prefix.h +18 -708
  34. data/vendor/brotli/c/dec/prefix_inc.h +707 -0
  35. data/vendor/brotli/c/dec/state.c +18 -15
  36. data/vendor/brotli/c/dec/state.h +2 -6
  37. data/vendor/brotli/c/dec/static_init.c +53 -0
  38. data/vendor/brotli/c/dec/static_init.h +30 -0
  39. data/vendor/brotli/c/enc/backward_references.c +32 -8
  40. data/vendor/brotli/c/enc/backward_references.h +1 -5
  41. data/vendor/brotli/c/enc/backward_references_hq.c +15 -15
  42. data/vendor/brotli/c/enc/backward_references_hq.h +1 -5
  43. data/vendor/brotli/c/enc/bit_cost.c +28 -4
  44. data/vendor/brotli/c/enc/bit_cost.h +8 -40
  45. data/vendor/brotli/c/enc/bit_cost_inc.h +1 -1
  46. data/vendor/brotli/c/enc/block_splitter.c +9 -12
  47. data/vendor/brotli/c/enc/block_splitter.h +0 -3
  48. data/vendor/brotli/c/enc/block_splitter_inc.h +14 -8
  49. data/vendor/brotli/c/enc/brotli_bit_stream.c +10 -9
  50. data/vendor/brotli/c/enc/brotli_bit_stream.h +0 -6
  51. data/vendor/brotli/c/enc/cluster.c +0 -2
  52. data/vendor/brotli/c/enc/cluster.h +0 -2
  53. data/vendor/brotli/c/enc/command.c +1 -1
  54. data/vendor/brotli/c/enc/command.h +8 -10
  55. data/vendor/brotli/c/enc/compound_dictionary.c +3 -5
  56. data/vendor/brotli/c/enc/compound_dictionary.h +1 -4
  57. data/vendor/brotli/c/enc/compress_fragment.c +3 -13
  58. data/vendor/brotli/c/enc/compress_fragment.h +0 -2
  59. data/vendor/brotli/c/enc/compress_fragment_two_pass.c +5 -15
  60. data/vendor/brotli/c/enc/compress_fragment_two_pass.h +0 -2
  61. data/vendor/brotli/c/enc/dictionary_hash.c +127 -1830
  62. data/vendor/brotli/c/enc/dictionary_hash.h +23 -3
  63. data/vendor/brotli/c/enc/dictionary_hash_inc.h +1829 -0
  64. data/vendor/brotli/c/enc/encode.c +77 -52
  65. data/vendor/brotli/c/enc/encoder_dict.c +9 -7
  66. data/vendor/brotli/c/enc/encoder_dict.h +2 -4
  67. data/vendor/brotli/c/enc/entropy_encode.c +3 -6
  68. data/vendor/brotli/c/enc/entropy_encode.h +2 -4
  69. data/vendor/brotli/c/enc/entropy_encode_static.h +18 -12
  70. data/vendor/brotli/c/enc/fast_log.c +1 -1
  71. data/vendor/brotli/c/enc/fast_log.h +2 -3
  72. data/vendor/brotli/c/enc/find_match_length.h +0 -2
  73. data/vendor/brotli/c/enc/hash.h +38 -31
  74. data/vendor/brotli/c/enc/hash_base.h +38 -0
  75. data/vendor/brotli/c/enc/hash_forgetful_chain_inc.h +11 -1
  76. data/vendor/brotli/c/enc/hash_longest_match64_inc.h +24 -7
  77. data/vendor/brotli/c/enc/hash_longest_match64_simd_inc.h +304 -0
  78. data/vendor/brotli/c/enc/hash_longest_match_inc.h +30 -11
  79. data/vendor/brotli/c/enc/hash_longest_match_quickly_inc.h +4 -0
  80. data/vendor/brotli/c/enc/hash_longest_match_simd_inc.h +278 -0
  81. data/vendor/brotli/c/enc/histogram.c +1 -0
  82. data/vendor/brotli/c/enc/histogram.h +0 -4
  83. data/vendor/brotli/c/enc/literal_cost.c +4 -6
  84. data/vendor/brotli/c/enc/literal_cost.h +0 -2
  85. data/vendor/brotli/c/enc/matching_tag_mask.h +69 -0
  86. data/vendor/brotli/c/enc/memory.c +0 -5
  87. data/vendor/brotli/c/enc/memory.h +0 -4
  88. data/vendor/brotli/c/enc/metablock.c +7 -9
  89. data/vendor/brotli/c/enc/metablock.h +3 -3
  90. data/vendor/brotli/c/enc/metablock_inc.h +4 -4
  91. data/vendor/brotli/c/enc/params.h +0 -1
  92. data/vendor/brotli/c/enc/prefix.h +0 -2
  93. data/vendor/brotli/c/enc/quality.h +17 -10
  94. data/vendor/brotli/c/enc/ringbuffer.h +1 -4
  95. data/vendor/brotli/c/enc/state.h +2 -2
  96. data/vendor/brotli/c/enc/static_dict.c +5 -11
  97. data/vendor/brotli/c/enc/static_dict.h +1 -3
  98. data/vendor/brotli/c/enc/static_dict_lut.c +224 -0
  99. data/vendor/brotli/c/enc/static_dict_lut.h +20 -5837
  100. data/vendor/brotli/c/enc/static_dict_lut_inc.h +5830 -0
  101. data/vendor/brotli/c/enc/static_init.c +59 -0
  102. data/vendor/brotli/c/enc/static_init.h +30 -0
  103. data/vendor/brotli/c/enc/utf8_util.c +1 -1
  104. data/vendor/brotli/c/enc/utf8_util.h +0 -2
  105. data/vendor/brotli/c/enc/write_bits.h +0 -2
  106. data/vendor/brotli/c/include/brotli/decode.h +2 -4
  107. data/vendor/brotli/c/include/brotli/encode.h +5 -1
  108. data/vendor/brotli/c/include/brotli/port.h +4 -7
  109. data/vendor/brotli/c/include/brotli/types.h +2 -2
  110. metadata +25 -31
  111. data/.github/workflows/main.yml +0 -37
  112. data/.github/workflows/publish.yml +0 -24
  113. data/.gitignore +0 -18
  114. data/.gitmodules +0 -3
  115. data/.rspec +0 -2
  116. data/Gemfile +0 -10
  117. data/Rakefile +0 -25
  118. data/bin/before_install.sh +0 -9
  119. data/bin/console +0 -14
  120. data/bin/setup +0 -8
  121. data/brotli.gemspec +0 -27
  122. data/smoke.sh +0 -8
  123. data/test/brotli_test.rb +0 -117
  124. data/test/brotli_writer_test.rb +0 -36
  125. data/test/test_helper.rb +0 -9
  126. data/vendor/brotli/c/common/dictionary.bin +0 -432
  127. data/vendor/brotli/c/common/dictionary.bin.br +0 -0
data/ext/brotli/brotli.c CHANGED
@@ -1,21 +1,65 @@
1
1
  #include "brotli.h"
2
2
 
3
- #define CSTR2SYM(x) ID2SYM(rb_intern(x))
4
-
5
3
  static VALUE rb_eBrotli;
4
+ static ID id_cmp;
5
+ static ID id_dictionary;
6
+ static ID id_font;
7
+ static ID id_generic;
8
+ static ID id_lgblock;
9
+ static ID id_lgwin;
10
+ static ID id_mode;
11
+ static ID id_output_buffer_limit;
12
+ static ID id_quality;
13
+ static ID id_read;
14
+ static ID id_text;
6
15
 
7
16
  static inline void*
8
17
  brotli_alloc(void* opaque, size_t size)
9
18
  {
19
+ (void)opaque;
10
20
  return ruby_xmalloc(size);
11
21
  }
12
22
 
13
23
  static inline void
14
24
  brotli_free(void* opaque, void* address)
15
25
  {
16
- if (address) {
17
- ruby_xfree(address);
26
+ (void)opaque;
27
+ ruby_xfree(address);
28
+ }
29
+
30
+ static VALUE
31
+ brotli_hash_lookup(VALUE hash, ID key)
32
+ {
33
+ if (NIL_P(hash)) {
34
+ return Qnil;
18
35
  }
36
+
37
+ Check_Type(hash, T_HASH);
38
+ return rb_hash_lookup(hash, ID2SYM(key));
39
+ }
40
+
41
+ static BrotliEncoderState*
42
+ brotli_encoder_state_create(void)
43
+ {
44
+ BrotliEncoderState* state = BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL);
45
+
46
+ if (!state) {
47
+ rb_raise(rb_eNoMemError, "BrotliEncoderCreateInstance failed");
48
+ }
49
+
50
+ return state;
51
+ }
52
+
53
+ static BrotliDecoderState*
54
+ brotli_decoder_state_create(void)
55
+ {
56
+ BrotliDecoderState* state = BrotliDecoderCreateInstance(brotli_alloc, brotli_free, NULL);
57
+
58
+ if (!state) {
59
+ rb_raise(rb_eNoMemError, "BrotliDecoderCreateInstance failed");
60
+ }
61
+
62
+ return state;
19
63
  }
20
64
 
21
65
  /*******************************************************************************
@@ -28,8 +72,22 @@ typedef struct {
28
72
  BrotliDecoderState* s;
29
73
  buffer_t* buffer;
30
74
  BrotliDecoderResult r;
75
+ uint8_t* dict;
76
+ size_t dict_len;
77
+ int oom;
31
78
  } brotli_inflate_args_t;
32
79
 
80
+ static void
81
+ brotli_inflate_cleanup(brotli_inflate_args_t* args)
82
+ {
83
+ delete_buffer(args->buffer);
84
+ args->buffer = NULL;
85
+ if (args->s) {
86
+ BrotliDecoderDestroyInstance(args->s);
87
+ args->s = NULL;
88
+ }
89
+ }
90
+
33
91
  static void*
34
92
  brotli_inflate_no_gvl(void *arg)
35
93
  {
@@ -44,6 +102,12 @@ brotli_inflate_no_gvl(void *arg)
44
102
  buffer_t* buffer = args->buffer;
45
103
  BrotliDecoderState* s = args->s;
46
104
 
105
+ /* Attach dictionary if provided */
106
+ if (args->dict && args->dict_len > 0) {
107
+ BrotliDecoderAttachDictionary(s, BROTLI_SHARED_DICTIONARY_RAW,
108
+ args->dict_len, args->dict);
109
+ }
110
+
47
111
  for (;;) {
48
112
  r = BrotliDecoderDecompressStream(s,
49
113
  &available_in, &next_in,
@@ -53,14 +117,19 @@ brotli_inflate_no_gvl(void *arg)
53
117
  if (r != BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
54
118
  break;
55
119
  }
56
- append_buffer(buffer, output, BUFSIZ);
120
+ if (append_buffer(buffer, output, BUFSIZ) != 0) {
121
+ args->oom = 1;
122
+ break;
123
+ }
57
124
  available_out = BUFSIZ;
58
125
  next_out = output;
59
126
  }
60
127
 
61
128
  if (r == BROTLI_DECODER_RESULT_SUCCESS) {
62
129
  if (next_out != output) {
63
- append_buffer(buffer, output, next_out - output);
130
+ if (append_buffer(buffer, output, next_out - output) != 0) {
131
+ args->oom = 1;
132
+ }
64
133
  }
65
134
  }
66
135
  args->r = r;
@@ -68,51 +137,84 @@ brotli_inflate_no_gvl(void *arg)
68
137
  return arg;
69
138
  }
70
139
 
71
- static ID id_read;
140
+ static VALUE
141
+ brotli_inflate_take_result(VALUE arg)
142
+ {
143
+ brotli_inflate_args_t *args = (brotli_inflate_args_t *)arg;
144
+ return rb_str_new(args->buffer->ptr, args->buffer->used);
145
+ }
72
146
 
73
147
  static VALUE
74
- brotli_inflate(VALUE self, VALUE str)
148
+ brotli_inflate(int argc, VALUE *argv, VALUE self)
75
149
  {
150
+ VALUE str = Qnil;
151
+ VALUE opts = Qnil;
76
152
  VALUE value = Qnil;
77
- brotli_inflate_args_t args;
153
+ VALUE dict = Qnil;
154
+ const char *error = NULL;
155
+ brotli_inflate_args_t args = {0};
156
+
157
+ (void)self;
158
+
159
+ rb_scan_args(argc, argv, "11", &str, &opts);
78
160
 
79
161
  if (rb_respond_to(str, id_read)) {
80
- str = rb_funcall(str, id_read, 0, 0);
162
+ str = rb_funcall(str, id_read, 0);
81
163
  }
82
164
 
83
165
  StringValue(str);
166
+ dict = brotli_hash_lookup(opts, id_dictionary);
167
+ if (!NIL_P(dict)) {
168
+ StringValue(dict);
169
+ }
84
170
 
85
171
  args.str = (uint8_t*)RSTRING_PTR(str);
86
172
  args.len = (size_t)RSTRING_LEN(str);
173
+ args.s = brotli_decoder_state_create();
87
174
  args.buffer = create_buffer(BUFSIZ);
88
- args.s = BrotliDecoderCreateInstance(brotli_alloc,
89
- brotli_free,
90
- NULL);
175
+ if (!args.buffer) {
176
+ brotli_inflate_cleanup(&args);
177
+ rb_raise(rb_eNoMemError, "failed to allocate decompression buffer");
178
+ }
91
179
  args.r = BROTLI_DECODER_RESULT_ERROR;
92
180
 
93
- #ifdef HAVE_RUBY_THREAD_H
94
- rb_thread_call_without_gvl(brotli_inflate_no_gvl, (void *)&args, NULL, NULL);
95
- #else
96
- brotli_inflate_no_gvl((void *)&args);
97
- #endif
181
+ if (!NIL_P(dict)) {
182
+ args.dict = (uint8_t*)RSTRING_PTR(dict);
183
+ args.dict_len = (size_t)RSTRING_LEN(dict);
184
+ } else {
185
+ args.dict = NULL;
186
+ args.dict_len = 0;
187
+ }
188
+
189
+ rb_thread_call_without_gvl(brotli_inflate_no_gvl, &args, NULL, NULL);
190
+ RB_GC_GUARD(str);
191
+ RB_GC_GUARD(dict);
192
+
193
+ /* Check before args.r: a failed final append leaves r at SUCCESS. */
194
+ if (args.oom) {
195
+ brotli_inflate_cleanup(&args);
196
+ rb_raise(rb_eNoMemError, "failed to allocate decompression buffer");
197
+ }
198
+
98
199
  if (args.r == BROTLI_DECODER_RESULT_SUCCESS) {
99
- value = rb_str_new(args.buffer->ptr, args.buffer->used);
100
- delete_buffer(args.buffer);
101
- BrotliDecoderDestroyInstance(args.s);
200
+ int state = 0;
201
+ value = rb_protect(brotli_inflate_take_result, (VALUE)&args, &state);
202
+ if (state) {
203
+ brotli_inflate_cleanup(&args);
204
+ rb_jump_tag(state);
205
+ }
102
206
  } else if (args.r == BROTLI_DECODER_RESULT_ERROR) {
103
- const char * error = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(args.s));
104
- delete_buffer(args.buffer);
105
- BrotliDecoderDestroyInstance(args.s);
106
- rb_raise(rb_eBrotli, "%s", error);
207
+ error = BrotliDecoderErrorString(BrotliDecoderGetErrorCode(args.s));
107
208
  } else if (args.r == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
108
- delete_buffer(args.buffer);
109
- BrotliDecoderDestroyInstance(args.s);
110
- rb_raise(rb_eBrotli, "Needs more input");
111
- } else if (args.r == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
112
- /* never reach to this block */
113
- delete_buffer(args.buffer);
114
- BrotliDecoderDestroyInstance(args.s);
115
- rb_raise(rb_eBrotli, "Needs more output");
209
+ error = "Needs more input";
210
+ } else {
211
+ error = "Needs more output";
212
+ }
213
+
214
+ brotli_inflate_cleanup(&args);
215
+
216
+ if (error) {
217
+ rb_raise(rb_eBrotli, "%s", error);
116
218
  }
117
219
 
118
220
  return value;
@@ -122,81 +224,113 @@ brotli_inflate(VALUE self, VALUE str)
122
224
  * deflate
123
225
  ******************************************************************************/
124
226
 
125
- static void
227
+ static const char *
126
228
  brotli_deflate_set_mode(BrotliEncoderState* s, VALUE value)
127
229
  {
128
230
  if (NIL_P(value)) {
129
- return;
231
+ return NULL;
232
+ }
233
+
234
+ if (value == ID2SYM(id_generic)) {
235
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_GENERIC);
236
+ } else if (value == ID2SYM(id_text)) {
237
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_TEXT);
238
+ } else if (value == ID2SYM(id_font)) {
239
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_FONT);
130
240
  } else {
131
- if (value == CSTR2SYM("generic")) {
132
- BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_GENERIC);
133
- } else if (value == CSTR2SYM("text")) {
134
- BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_TEXT);
135
- } else if (value == CSTR2SYM("font")) {
136
- BrotliEncoderSetParameter(s, BROTLI_PARAM_MODE, BROTLI_MODE_FONT);
137
- } else {
138
- rb_raise(rb_eArgError, "invalid mode");
139
- }
241
+ return "invalid mode";
140
242
  }
243
+
244
+ return NULL;
141
245
  }
142
246
 
143
- static void
247
+ static const char *
144
248
  brotli_deflate_set_quality(BrotliEncoderState* s, VALUE value)
145
249
  {
250
+ long param;
251
+
146
252
  if (NIL_P(value)) {
147
- return;
253
+ return NULL;
254
+ }
255
+
256
+ if (!FIXNUM_P(value)) {
257
+ return "invalid quality value. Should be 0 to 11.";
258
+ }
259
+
260
+ param = FIX2LONG(value);
261
+ if (0 <= param && param <= 11) {
262
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, (uint32_t)param);
148
263
  } else {
149
- int32_t param = NUM2INT(value);
150
- if (0 <= param && param <= 11) {
151
- BrotliEncoderSetParameter(s, BROTLI_PARAM_QUALITY, param);
152
- } else {
153
- rb_raise(rb_eArgError, "invalid quality value. Should be 0 to 11.");
154
- }
264
+ return "invalid quality value. Should be 0 to 11.";
155
265
  }
266
+
267
+ return NULL;
156
268
  }
157
269
 
158
- static void
270
+ static const char *
159
271
  brotli_deflate_set_lgwin(BrotliEncoderState* s, VALUE value)
160
272
  {
273
+ long param;
274
+
161
275
  if (NIL_P(value)) {
162
- return;
276
+ return NULL;
277
+ }
278
+
279
+ if (!FIXNUM_P(value)) {
280
+ return "invalid lgwin value. Should be 10 to 24.";
281
+ }
282
+
283
+ param = FIX2LONG(value);
284
+ if (10 <= param && param <= 24) {
285
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, (uint32_t)param);
163
286
  } else {
164
- int32_t param = NUM2INT(value);
165
- if (10 <= param && param <= 24) {
166
- BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, param);
167
- } else {
168
- rb_raise(rb_eArgError, "invalid lgwin value. Should be 10 to 24.");
169
- }
287
+ return "invalid lgwin value. Should be 10 to 24.";
170
288
  }
289
+
290
+ return NULL;
171
291
  }
172
292
 
173
- static void
293
+ static const char *
174
294
  brotli_deflate_set_lgblock(BrotliEncoderState* s, VALUE value)
175
295
  {
296
+ long param;
297
+
176
298
  if (NIL_P(value)) {
177
- return;
299
+ return NULL;
300
+ }
301
+
302
+ if (!FIXNUM_P(value)) {
303
+ return "invalid lgblock value. Should be 0 or 16 to 24.";
304
+ }
305
+
306
+ param = FIX2LONG(value);
307
+ if (param == 0 || (16 <= param && param <= 24)) {
308
+ BrotliEncoderSetParameter(s, BROTLI_PARAM_LGBLOCK, (uint32_t)param);
178
309
  } else {
179
- int32_t param = NUM2INT(value);
180
- if ((param == 0) || (16 <= param && param <= 24)) {
181
- BrotliEncoderSetParameter(s, BROTLI_PARAM_LGBLOCK, param);
182
- } else {
183
- rb_raise(rb_eArgError, "invalid lgblock value. Should be 0 or 16 to 24.");
184
- }
310
+ return "invalid lgblock value. Should be 0 or 16 to 24.";
185
311
  }
312
+
313
+ return NULL;
186
314
  }
187
315
 
188
- static BrotliEncoderState*
316
+ static const char *
189
317
  brotli_deflate_parse_options(BrotliEncoderState* s, VALUE opts)
190
318
  {
191
- if (!NIL_P(opts)) {
192
- Check_Type(opts, T_HASH);
193
- brotli_deflate_set_mode(s, rb_hash_aref(opts, CSTR2SYM("mode")));
194
- brotli_deflate_set_quality(s, rb_hash_aref(opts, CSTR2SYM("quality")));
195
- brotli_deflate_set_lgwin(s, rb_hash_aref(opts, CSTR2SYM("lgwin")));
196
- brotli_deflate_set_lgblock(s, rb_hash_aref(opts, CSTR2SYM("lgblock")));
197
- }
319
+ const char *err;
198
320
 
199
- return s;
321
+ err = brotli_deflate_set_mode(s, brotli_hash_lookup(opts, id_mode));
322
+ if (err) {
323
+ return err;
324
+ }
325
+ err = brotli_deflate_set_quality(s, brotli_hash_lookup(opts, id_quality));
326
+ if (err) {
327
+ return err;
328
+ }
329
+ err = brotli_deflate_set_lgwin(s, brotli_hash_lookup(opts, id_lgwin));
330
+ if (err) {
331
+ return err;
332
+ }
333
+ return brotli_deflate_set_lgblock(s, brotli_hash_lookup(opts, id_lgblock));
200
334
  }
201
335
 
202
336
  typedef struct {
@@ -205,8 +339,25 @@ typedef struct {
205
339
  BrotliEncoderState* s;
206
340
  buffer_t* buffer;
207
341
  BROTLI_BOOL finished;
342
+ BrotliEncoderPreparedDictionary* prepared_dict;
343
+ int oom;
208
344
  } brotli_deflate_args_t;
209
345
 
346
+ static void
347
+ brotli_deflate_cleanup(brotli_deflate_args_t* args)
348
+ {
349
+ delete_buffer(args->buffer);
350
+ args->buffer = NULL;
351
+ if (args->s) {
352
+ BrotliEncoderDestroyInstance(args->s);
353
+ args->s = NULL;
354
+ }
355
+ if (args->prepared_dict) {
356
+ BrotliEncoderDestroyPreparedDictionary(args->prepared_dict);
357
+ args->prepared_dict = NULL;
358
+ }
359
+ }
360
+
210
361
  static void*
211
362
  brotli_deflate_no_gvl(void *arg)
212
363
  {
@@ -230,7 +381,11 @@ brotli_deflate_no_gvl(void *arg)
230
381
  args->finished = BROTLI_FALSE;
231
382
  break;
232
383
  } else {
233
- append_buffer(buffer, output, next_out - output);
384
+ if (append_buffer(buffer, output, next_out - output) != 0) {
385
+ args->oom = 1;
386
+ args->finished = BROTLI_FALSE;
387
+ break;
388
+ }
234
389
  available_out = BUFSIZ;
235
390
  next_out = output;
236
391
 
@@ -244,38 +399,93 @@ brotli_deflate_no_gvl(void *arg)
244
399
  return arg;
245
400
  }
246
401
 
402
+ static VALUE
403
+ brotli_deflate_take_result(VALUE arg)
404
+ {
405
+ brotli_deflate_args_t *args = (brotli_deflate_args_t *)arg;
406
+ return rb_str_new(args->buffer->ptr, args->buffer->used);
407
+ }
408
+
247
409
  static VALUE
248
410
  brotli_deflate(int argc, VALUE *argv, VALUE self)
249
411
  {
250
- VALUE str = Qnil, opts = Qnil, value = Qnil;
251
- brotli_deflate_args_t args;
412
+ VALUE str = Qnil;
413
+ VALUE opts = Qnil;
414
+ VALUE value = Qnil;
415
+ VALUE dict = Qnil;
416
+ brotli_deflate_args_t args = {0};
417
+ size_t max_compressed_size;
418
+ const char *err;
419
+
420
+ (void)self;
252
421
 
253
422
  rb_scan_args(argc, argv, "11", &str, &opts);
254
423
  if (NIL_P(str)) {
255
424
  rb_raise(rb_eArgError, "input should not be nil");
256
- return Qnil;
257
425
  }
258
426
  StringValue(str);
427
+ dict = brotli_hash_lookup(opts, id_dictionary);
428
+ if (!NIL_P(dict)) {
429
+ StringValue(dict);
430
+ }
259
431
 
260
432
  args.str = (uint8_t*)RSTRING_PTR(str);
261
433
  args.len = (size_t)RSTRING_LEN(str);
262
- args.s = brotli_deflate_parse_options(
263
- BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL),
264
- opts);
265
- args.buffer = create_buffer(BUFSIZ);
434
+ args.s = brotli_encoder_state_create();
435
+ err = brotli_deflate_parse_options(args.s, opts);
436
+ if (err) {
437
+ brotli_deflate_cleanup(&args);
438
+ rb_raise(rb_eArgError, "%s", err);
439
+ }
440
+ max_compressed_size = BrotliEncoderMaxCompressedSize(args.len);
441
+ args.buffer = create_buffer(max_compressed_size);
442
+ if (!args.buffer) {
443
+ brotli_deflate_cleanup(&args);
444
+ rb_raise(rb_eNoMemError, "failed to allocate compression buffer");
445
+ }
266
446
  args.finished = BROTLI_FALSE;
267
447
 
268
- #ifdef HAVE_RUBY_THREAD_H
269
- rb_thread_call_without_gvl(brotli_deflate_no_gvl, (void *)&args, NULL, NULL);
270
- #else
271
- brotli_deflate_no_gvl((void *)&args);
272
- #endif
448
+ args.prepared_dict = NULL;
449
+ if (!NIL_P(dict)) {
450
+ args.prepared_dict = BrotliEncoderPrepareDictionary(
451
+ BROTLI_SHARED_DICTIONARY_RAW,
452
+ (size_t)RSTRING_LEN(dict),
453
+ (const uint8_t*)RSTRING_PTR(dict),
454
+ BROTLI_MAX_QUALITY,
455
+ brotli_alloc,
456
+ brotli_free,
457
+ NULL);
458
+ if (!args.prepared_dict) {
459
+ brotli_deflate_cleanup(&args);
460
+ rb_raise(rb_eBrotli, "Failed to prepare dictionary for compression");
461
+ }
462
+ if (!BrotliEncoderAttachPreparedDictionary(args.s, args.prepared_dict)) {
463
+ brotli_deflate_cleanup(&args);
464
+ rb_raise(rb_eBrotli, "Failed to attach dictionary for compression");
465
+ }
466
+ }
467
+
468
+ rb_thread_call_without_gvl(brotli_deflate_no_gvl, &args, NULL, NULL);
469
+ RB_GC_GUARD(str);
470
+ RB_GC_GUARD(dict);
471
+ if (args.oom) {
472
+ brotli_deflate_cleanup(&args);
473
+ rb_raise(rb_eNoMemError, "failed to allocate compression buffer");
474
+ }
273
475
  if (args.finished == BROTLI_TRUE) {
274
- value = rb_str_new(args.buffer->ptr, args.buffer->used);
476
+ int state = 0;
477
+ value = rb_protect(brotli_deflate_take_result, (VALUE)&args, &state);
478
+ if (state) {
479
+ brotli_deflate_cleanup(&args);
480
+ rb_jump_tag(state);
481
+ }
275
482
  }
276
483
 
277
- delete_buffer(args.buffer);
278
- BrotliEncoderDestroyInstance(args.s);
484
+ brotli_deflate_cleanup(&args);
485
+
486
+ if (args.finished != BROTLI_TRUE) {
487
+ rb_raise(rb_eBrotli, "Failed to compress");
488
+ }
279
489
 
280
490
  return value;
281
491
  }
@@ -287,176 +497,706 @@ brotli_deflate(int argc, VALUE *argv, VALUE self)
287
497
  static VALUE brotli_version(VALUE klass) {
288
498
  uint32_t ver = BrotliEncoderVersion();
289
499
  char version[255];
500
+
501
+ (void)klass;
290
502
  snprintf(version, sizeof(version), "%u.%u.%u", ver >> 24, (ver >> 12) & 0xFFF, ver & 0xFFF);
291
- return rb_str_new2(version);
503
+ return rb_str_new_cstr(version);
292
504
  }
293
505
 
294
506
  /*******************************************************************************
295
- * Writer
507
+ * Streaming APIs
296
508
  ******************************************************************************/
297
509
 
298
- static ID id_write, id_flush, id_close;
299
-
300
- struct brotli {
301
- VALUE io;
302
- BrotliEncoderState* state;
303
- };
510
+ static VALUE rb_cBrotliCompressor;
511
+ static VALUE rb_cBrotliDecompressor;
304
512
 
305
- static void br_mark(void *p)
513
+ static uint8_t*
514
+ brotli_copy_string_data(VALUE string, size_t* len)
306
515
  {
307
- struct brotli *br = p;
308
- rb_gc_mark(br->io);
516
+ size_t copy_len;
517
+ size_t alloc_len;
518
+ uint8_t* copy;
519
+
520
+ StringValue(string);
521
+ copy_len = (size_t)RSTRING_LEN(string);
522
+ alloc_len = copy_len > 0 ? copy_len : 1;
523
+ copy = brotli_alloc(NULL, alloc_len);
524
+ if (copy_len > 0) {
525
+ memcpy(copy, RSTRING_PTR(string), copy_len);
526
+ }
527
+ *len = copy_len;
528
+ return copy;
309
529
  }
310
530
 
311
- static void br_free(void *p)
312
- {
313
- struct brotli* br = p;
314
- BrotliEncoderDestroyInstance(br->state);
315
- br->state = NULL;
316
- br->io = Qnil;
317
- ruby_xfree(br);
318
- }
531
+ typedef struct {
532
+ BrotliEncoderState* state;
533
+ BrotliEncoderPreparedDictionary* prepared_dict;
534
+ uint8_t* dict_data;
535
+ size_t dict_len;
536
+ BROTLI_BOOL finished;
537
+ } brotli_encoder_t;
319
538
 
320
- static size_t br_memsize(const void *p)
321
- {
322
- return sizeof(struct brotli);
323
- }
539
+ typedef struct {
540
+ brotli_encoder_t encoder;
541
+ } brotli_compressor_t;
324
542
 
325
- static const rb_data_type_t brotli_data_type = {
326
- "brotli",
327
- { br_mark, br_free, br_memsize },
328
- 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
329
- };
543
+ typedef struct {
544
+ BrotliDecoderState* state;
545
+ uint8_t* dict_data;
546
+ size_t dict_len;
547
+ VALUE pending_input;
548
+ BROTLI_BOOL needs_more_output;
549
+ BROTLI_BOOL finished;
550
+ } brotli_decompressor_t;
330
551
 
331
552
  typedef struct {
332
553
  BrotliEncoderState* state;
333
554
  BrotliEncoderOperation op;
334
555
  size_t available_in;
335
556
  const uint8_t* next_in;
557
+ BROTLI_BOOL ok;
336
558
  } brotli_encoder_args_t;
337
559
 
560
+ typedef struct {
561
+ BrotliDecoderState* state;
562
+ size_t available_in;
563
+ const uint8_t* next_in;
564
+ size_t available_out;
565
+ uint8_t* next_out;
566
+ BrotliDecoderResult result;
567
+ } brotli_decoder_args_t;
568
+
338
569
  static void* compress_no_gvl(void *ptr) {
339
570
  brotli_encoder_args_t *args = ptr;
340
571
  size_t zero = 0;
341
- if (!BrotliEncoderCompressStream(args->state, args->op,
342
- &args->available_in, &args->next_in,
343
- &zero, NULL, NULL)) {
344
- rb_raise(rb_eBrotli, "BrotliEncoderCompressStream failed");
345
- }
572
+ args->ok = BrotliEncoderCompressStream(args->state, args->op,
573
+ &args->available_in, &args->next_in,
574
+ &zero, NULL, NULL);
575
+ return NULL;
576
+ }
577
+
578
+ static void* decompress_no_gvl(void *ptr) {
579
+ brotli_decoder_args_t *args = ptr;
580
+ args->result = BrotliDecoderDecompressStream(args->state,
581
+ &args->available_in,
582
+ &args->next_in,
583
+ &args->available_out,
584
+ &args->next_out,
585
+ NULL);
346
586
  return NULL;
347
587
  }
348
588
 
349
- static size_t push_output(struct brotli *br) {
350
- size_t len = 0;
351
- if (BrotliEncoderHasMoreOutput(br->state)) {
352
- const uint8_t* out = BrotliEncoderTakeOutput(br->state, &len);
589
+ static void
590
+ brotli_encoder_step(brotli_encoder_args_t *args)
591
+ {
592
+ rb_thread_call_without_gvl(compress_no_gvl, (void*)args, NULL, NULL);
593
+ }
594
+
595
+ static void
596
+ brotli_decompressor_step(brotli_decoder_args_t *args)
597
+ {
598
+ rb_thread_call_without_gvl(decompress_no_gvl, (void*)args, NULL, NULL);
599
+ }
600
+
601
+ static void
602
+ brotli_encoder_destroy(brotli_encoder_t* encoder)
603
+ {
604
+ if (encoder->state) {
605
+ BrotliEncoderDestroyInstance(encoder->state);
606
+ encoder->state = NULL;
607
+ }
608
+ if (encoder->prepared_dict) {
609
+ BrotliEncoderDestroyPreparedDictionary(encoder->prepared_dict);
610
+ encoder->prepared_dict = NULL;
611
+ }
612
+ brotli_free(NULL, encoder->dict_data);
613
+ encoder->dict_data = NULL;
614
+ encoder->dict_len = 0;
615
+ encoder->finished = BROTLI_FALSE;
616
+ }
617
+
618
+ static void
619
+ brotli_encoder_reset(brotli_encoder_t* encoder)
620
+ {
621
+ brotli_encoder_destroy(encoder);
622
+ encoder->state = brotli_encoder_state_create();
623
+ }
624
+
625
+ static void
626
+ brotli_encoder_take_output_to_string(BrotliEncoderState* state, VALUE output)
627
+ {
628
+ while (BrotliEncoderHasMoreOutput(state)) {
629
+ size_t len = 0;
630
+ const uint8_t* out = BrotliEncoderTakeOutput(state, &len);
353
631
  if (len > 0) {
354
- rb_funcall(br->io, id_write, 1, rb_str_new((const char*)out, len));
632
+ rb_str_cat(output, (const char*)out, len);
355
633
  }
356
634
  }
357
- return len;
358
635
  }
359
636
 
360
- static VALUE rb_writer_alloc(VALUE klass) {
361
- struct brotli *br;
362
- VALUE obj = TypedData_Make_Struct(klass, struct brotli, &brotli_data_type, br);
363
- br->io = Qnil;
364
- br->state = BrotliEncoderCreateInstance(brotli_alloc, brotli_free, NULL);
365
- if (!br->state) {
366
- rb_raise(rb_eNoMemError, "BrotliEncoderCreateInstance failed");
367
- return Qnil;
637
+ static void
638
+ brotli_encoder_attach_dictionary(brotli_encoder_t* encoder, VALUE opts)
639
+ {
640
+ VALUE dict = brotli_hash_lookup(opts, id_dictionary);
641
+
642
+ if (NIL_P(dict)) {
643
+ return;
644
+ }
645
+
646
+ encoder->dict_data = brotli_copy_string_data(dict, &encoder->dict_len);
647
+ encoder->prepared_dict = BrotliEncoderPrepareDictionary(
648
+ BROTLI_SHARED_DICTIONARY_RAW,
649
+ encoder->dict_len,
650
+ encoder->dict_data,
651
+ BROTLI_MAX_QUALITY,
652
+ brotli_alloc,
653
+ brotli_free,
654
+ NULL);
655
+
656
+ if (!encoder->prepared_dict) {
657
+ brotli_free(NULL, encoder->dict_data);
658
+ encoder->dict_data = NULL;
659
+ encoder->dict_len = 0;
660
+ rb_raise(rb_eBrotli, "Failed to prepare dictionary for compression");
661
+ }
662
+
663
+ if (!BrotliEncoderAttachPreparedDictionary(encoder->state, encoder->prepared_dict)) {
664
+ BrotliEncoderDestroyPreparedDictionary(encoder->prepared_dict);
665
+ encoder->prepared_dict = NULL;
666
+ brotli_free(NULL, encoder->dict_data);
667
+ encoder->dict_data = NULL;
668
+ encoder->dict_len = 0;
669
+ rb_raise(rb_eBrotli, "Failed to attach dictionary for compression");
368
670
  }
671
+ }
672
+
673
+ static VALUE
674
+ brotli_encoder_stream_to_string(brotli_encoder_t* encoder,
675
+ BrotliEncoderOperation op,
676
+ const uint8_t* input,
677
+ size_t input_len)
678
+ {
679
+ VALUE output = rb_str_buf_new(BUFSIZ);
680
+ brotli_encoder_args_t args = {
681
+ .state = encoder->state,
682
+ .op = op,
683
+ .available_in = input_len,
684
+ .next_in = input,
685
+ .ok = BROTLI_FALSE
686
+ };
687
+
688
+ if (op == BROTLI_OPERATION_PROCESS && input_len == 0) {
689
+ return output;
690
+ }
691
+
692
+ for (;;) {
693
+ long output_len_before;
694
+ size_t produced;
695
+
696
+ brotli_encoder_step(&args);
697
+ if (args.ok == BROTLI_FALSE) {
698
+ rb_raise(rb_eBrotli, "BrotliEncoderCompressStream failed");
699
+ }
700
+
701
+ output_len_before = RSTRING_LEN(output);
702
+ brotli_encoder_take_output_to_string(encoder->state, output);
703
+ produced = (size_t)(RSTRING_LEN(output) - output_len_before);
704
+
705
+ if (args.available_in > 0 || BrotliEncoderHasMoreOutput(encoder->state)) {
706
+ continue;
707
+ }
708
+ if (op != BROTLI_OPERATION_PROCESS && produced > 0) {
709
+ continue;
710
+ }
711
+ break;
712
+ }
713
+
714
+ return output;
715
+ }
716
+
717
+ /*******************************************************************************
718
+ * Compressor
719
+ ******************************************************************************/
720
+
721
+ static void
722
+ brotli_compressor_free(void *p)
723
+ {
724
+ brotli_compressor_t* br = p;
725
+ brotli_encoder_destroy(&br->encoder);
726
+ ruby_xfree(br);
727
+ }
728
+
729
+ static size_t
730
+ brotli_compressor_memsize(const void *p)
731
+ {
732
+ const brotli_compressor_t *br = p;
733
+ return sizeof(*br) + br->encoder.dict_len;
734
+ }
735
+
736
+ static const rb_data_type_t brotli_compressor_data_type = {
737
+ "brotli_compressor",
738
+ { 0, brotli_compressor_free, brotli_compressor_memsize },
739
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
740
+ };
741
+
742
+ static VALUE
743
+ rb_compressor_alloc(VALUE klass)
744
+ {
745
+ brotli_compressor_t *br;
746
+ VALUE obj = TypedData_Make_Struct(klass, brotli_compressor_t, &brotli_compressor_data_type, br);
747
+ br->encoder.state = NULL;
748
+ br->encoder.prepared_dict = NULL;
749
+ br->encoder.dict_data = NULL;
750
+ br->encoder.dict_len = 0;
751
+ br->encoder.finished = BROTLI_FALSE;
369
752
  return obj;
370
753
  }
371
754
 
372
- static VALUE rb_writer_initialize(int argc, VALUE* argv, VALUE self) {
373
- VALUE io = Qnil;
755
+ static void
756
+ brotli_compressor_ensure_open(brotli_compressor_t* br)
757
+ {
758
+ if (!br->encoder.state) {
759
+ rb_raise(rb_eBrotli, "Compressor is closed");
760
+ }
761
+ }
762
+
763
+ static VALUE
764
+ rb_compressor_initialize(int argc, VALUE* argv, VALUE self)
765
+ {
374
766
  VALUE opts = Qnil;
375
- rb_scan_args(argc, argv, "11", &io, &opts);
376
- if (NIL_P(io)) {
377
- rb_raise(rb_eArgError, "io should not be nil");
378
- return Qnil;
767
+ brotli_compressor_t *br;
768
+ const char *err;
769
+
770
+ rb_scan_args(argc, argv, "01", &opts);
771
+
772
+ TypedData_Get_Struct(self, brotli_compressor_t, &brotli_compressor_data_type, br);
773
+ brotli_encoder_reset(&br->encoder);
774
+ err = brotli_deflate_parse_options(br->encoder.state, opts);
775
+ if (err) {
776
+ rb_raise(rb_eArgError, "%s", err);
379
777
  }
778
+ brotli_encoder_attach_dictionary(&br->encoder, opts);
380
779
 
381
- struct brotli *br;
382
- TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
383
- brotli_deflate_parse_options(br->state, opts);
384
- br->io = io;
385
780
  return self;
386
781
  }
387
782
 
388
- static VALUE rb_writer_write(VALUE self, VALUE buf) {
389
- struct brotli* br;
390
- TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
391
- StringValue(buf);
783
+ static VALUE
784
+ rb_compressor_process(VALUE self, VALUE input)
785
+ {
786
+ brotli_compressor_t *br;
787
+ VALUE output;
788
+ TypedData_Get_Struct(self, brotli_compressor_t, &brotli_compressor_data_type, br);
789
+ brotli_compressor_ensure_open(br);
790
+ if (br->encoder.finished) {
791
+ rb_raise(rb_eBrotli, "Compressor is finished");
792
+ }
793
+
794
+ StringValue(input);
795
+ output = brotli_encoder_stream_to_string(&br->encoder,
796
+ BROTLI_OPERATION_PROCESS,
797
+ (const uint8_t*)RSTRING_PTR(input),
798
+ (size_t)RSTRING_LEN(input));
799
+ RB_GC_GUARD(input);
800
+ return output;
801
+ }
392
802
 
393
- const size_t total = (size_t)RSTRING_LEN(buf);
803
+ static VALUE
804
+ rb_compressor_flush(VALUE self)
805
+ {
806
+ brotli_compressor_t *br;
807
+ TypedData_Get_Struct(self, brotli_compressor_t, &brotli_compressor_data_type, br);
808
+ brotli_compressor_ensure_open(br);
809
+ if (br->encoder.finished) {
810
+ rb_raise(rb_eBrotli, "Compressor is finished");
811
+ }
394
812
 
395
- brotli_encoder_args_t args = {
396
- .state = br->state,
397
- .op = BROTLI_OPERATION_PROCESS,
398
- .available_in = total,
399
- .next_in = (uint8_t*)RSTRING_PTR(buf)
400
- };
813
+ return brotli_encoder_stream_to_string(&br->encoder, BROTLI_OPERATION_FLUSH, NULL, 0);
814
+ }
815
+
816
+ static VALUE
817
+ rb_compressor_finish(VALUE self)
818
+ {
819
+ brotli_compressor_t *br;
820
+ VALUE output;
401
821
 
402
- while (args.available_in > 0) {
403
- rb_thread_call_without_gvl(compress_no_gvl, (void*)&args, NULL, NULL);
404
- push_output(br);
822
+ TypedData_Get_Struct(self, brotli_compressor_t, &brotli_compressor_data_type, br);
823
+ brotli_compressor_ensure_open(br);
824
+
825
+ if (br->encoder.finished) {
826
+ return rb_str_new("", 0);
405
827
  }
406
828
 
407
- return SIZET2NUM(total);
829
+ output = brotli_encoder_stream_to_string(&br->encoder, BROTLI_OPERATION_FINISH, NULL, 0);
830
+ br->encoder.finished = BROTLI_TRUE;
831
+ return output;
408
832
  }
409
833
 
410
- static VALUE rb_writer_finish(VALUE self) {
411
- struct brotli* br;
412
- TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
834
+ static VALUE
835
+ rb_compressor_is_finished(VALUE self)
836
+ {
837
+ brotli_compressor_t *br;
413
838
 
414
- brotli_encoder_args_t args = {
415
- .state = br->state,
416
- .op = BROTLI_OPERATION_FINISH,
417
- .available_in = 0,
418
- .next_in = NULL
419
- };
839
+ TypedData_Get_Struct(self, brotli_compressor_t, &brotli_compressor_data_type, br);
840
+ brotli_compressor_ensure_open(br);
841
+ return br->encoder.finished ? Qtrue : Qfalse;
842
+ }
843
+
844
+ /*******************************************************************************
845
+ * Decompressor
846
+ ******************************************************************************/
420
847
 
421
- while (!BrotliEncoderIsFinished(br->state)) {
422
- rb_thread_call_without_gvl(compress_no_gvl, (void*)&args, NULL, NULL);
423
- push_output(br);
848
+ static void
849
+ brotli_decompressor_mark(void *p)
850
+ {
851
+ brotli_decompressor_t *br = p;
852
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
853
+ rb_gc_mark_movable(br->pending_input);
854
+ #else
855
+ rb_gc_mark(br->pending_input);
856
+ #endif
857
+ }
858
+
859
+ static void
860
+ brotli_decompressor_free(void *p)
861
+ {
862
+ brotli_decompressor_t *br = p;
863
+ if (br->state) {
864
+ BrotliDecoderDestroyInstance(br->state);
865
+ br->state = NULL;
424
866
  }
425
- return br->io;
867
+ brotli_free(NULL, br->dict_data);
868
+ br->dict_data = NULL;
869
+ br->dict_len = 0;
870
+ br->pending_input = Qnil;
871
+ br->needs_more_output = BROTLI_FALSE;
872
+ br->finished = BROTLI_FALSE;
873
+ ruby_xfree(br);
426
874
  }
427
875
 
428
- static VALUE rb_writer_flush(VALUE self) {
429
- struct brotli *br;
430
- TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
876
+ static void
877
+ brotli_decompressor_set_pending_input(VALUE self, brotli_decompressor_t* br, VALUE pending_input)
878
+ {
879
+ RB_OBJ_WRITE(self, &br->pending_input, pending_input);
880
+ }
431
881
 
432
- brotli_encoder_args_t args = {
433
- .state = br->state,
434
- .op = BROTLI_OPERATION_FLUSH,
435
- .available_in = 0,
436
- .next_in = NULL
437
- };
882
+ static void
883
+ brotli_decompressor_reset(VALUE self, brotli_decompressor_t* br)
884
+ {
885
+ if (br->state) {
886
+ BrotliDecoderDestroyInstance(br->state);
887
+ br->state = NULL;
888
+ }
889
+
890
+ brotli_free(NULL, br->dict_data);
891
+ br->dict_data = NULL;
892
+ br->dict_len = 0;
893
+ brotli_decompressor_set_pending_input(self, br, Qnil);
894
+ br->needs_more_output = BROTLI_FALSE;
895
+ br->finished = BROTLI_FALSE;
896
+ br->state = brotli_decoder_state_create();
897
+ }
898
+
899
+ static size_t
900
+ brotli_decompressor_memsize(const void *p)
901
+ {
902
+ const brotli_decompressor_t *br = p;
903
+ return sizeof(*br) + br->dict_len;
904
+ }
905
+
906
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
907
+ static void
908
+ brotli_decompressor_compact(void *p)
909
+ {
910
+ brotli_decompressor_t *br = p;
911
+
912
+ if (!NIL_P(br->pending_input)) {
913
+ br->pending_input = rb_gc_location(br->pending_input);
914
+ }
915
+ }
916
+ #endif
917
+
918
+ static const rb_data_type_t brotli_decompressor_data_type = {
919
+ "brotli_decompressor",
920
+ {
921
+ brotli_decompressor_mark,
922
+ brotli_decompressor_free,
923
+ brotli_decompressor_memsize,
924
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
925
+ brotli_decompressor_compact,
926
+ #endif
927
+ },
928
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
929
+ };
930
+
931
+ static VALUE
932
+ rb_decompressor_alloc(VALUE klass)
933
+ {
934
+ brotli_decompressor_t *br;
935
+ VALUE obj = TypedData_Make_Struct(klass, brotli_decompressor_t, &brotli_decompressor_data_type, br);
936
+ br->state = NULL;
937
+ br->dict_data = NULL;
938
+ br->dict_len = 0;
939
+ RB_OBJ_WRITE(obj, &br->pending_input, Qnil);
940
+ br->needs_more_output = BROTLI_FALSE;
941
+ br->finished = BROTLI_FALSE;
942
+ return obj;
943
+ }
438
944
 
439
- do {
440
- rb_thread_call_without_gvl(compress_no_gvl, (void*)&args, NULL, NULL);
441
- push_output(br);
442
- } while (BrotliEncoderHasMoreOutput(br->state));
945
+ static void
946
+ brotli_decompressor_attach_dictionary(brotli_decompressor_t* br, VALUE opts)
947
+ {
948
+ VALUE dict = brotli_hash_lookup(opts, id_dictionary);
949
+ if (NIL_P(dict)) {
950
+ return;
951
+ }
443
952
 
444
- if (rb_respond_to(br->io, id_flush)) {
445
- rb_funcall(br->io, id_flush, 0);
953
+ br->dict_data = brotli_copy_string_data(dict, &br->dict_len);
954
+ if (!BrotliDecoderAttachDictionary(br->state,
955
+ BROTLI_SHARED_DICTIONARY_RAW,
956
+ br->dict_len,
957
+ br->dict_data)) {
958
+ brotli_free(NULL, br->dict_data);
959
+ br->dict_data = NULL;
960
+ br->dict_len = 0;
961
+ rb_raise(rb_eBrotli, "Failed to attach dictionary for decompression");
446
962
  }
963
+ }
964
+
965
+ static VALUE
966
+ rb_decompressor_initialize(int argc, VALUE* argv, VALUE self)
967
+ {
968
+ VALUE opts = Qnil;
969
+ brotli_decompressor_t *br;
970
+
971
+ rb_scan_args(argc, argv, "01", &opts);
972
+ TypedData_Get_Struct(self, brotli_decompressor_t, &brotli_decompressor_data_type, br);
973
+ brotli_decompressor_reset(self, br);
974
+ brotli_decompressor_attach_dictionary(br, opts);
975
+
447
976
  return self;
448
977
  }
449
978
 
450
- static VALUE rb_writer_close(VALUE self) {
451
- struct brotli* br;
452
- TypedData_Get_Struct(self, struct brotli, &brotli_data_type, br);
979
+ static BROTLI_BOOL
980
+ brotli_decompressor_has_pending_input(const brotli_decompressor_t *br)
981
+ {
982
+ return !NIL_P(br->pending_input) && RSTRING_LEN(br->pending_input) > 0;
983
+ }
984
+
985
+ static size_t
986
+ brotli_decompressor_pending_input_length(const brotli_decompressor_t *br)
987
+ {
988
+ if (!brotli_decompressor_has_pending_input(br)) {
989
+ return 0;
990
+ }
991
+ return (size_t)RSTRING_LEN(br->pending_input);
992
+ }
993
+
994
+ static void
995
+ brotli_decompressor_clear_pending_input(VALUE self, brotli_decompressor_t* br)
996
+ {
997
+ brotli_decompressor_set_pending_input(self, br, Qnil);
998
+ }
999
+
1000
+ static void
1001
+ brotli_decompressor_ensure_open(const brotli_decompressor_t* br)
1002
+ {
1003
+ if (!br->state) {
1004
+ rb_raise(rb_eBrotli, "Decompressor is closed");
1005
+ }
1006
+ }
1007
+
1008
+ static BROTLI_BOOL
1009
+ brotli_decompressor_needs_output_drain(const brotli_decompressor_t *br)
1010
+ {
1011
+ return br->needs_more_output || brotli_decompressor_has_pending_input(br) ||
1012
+ BrotliDecoderHasMoreOutput(br->state);
1013
+ }
1014
+
1015
+ static void
1016
+ brotli_decompressor_store_pending_input(VALUE self,
1017
+ brotli_decompressor_t* br,
1018
+ const uint8_t* next_in,
1019
+ size_t available_in)
1020
+ {
1021
+ if (available_in == 0) {
1022
+ brotli_decompressor_clear_pending_input(self, br);
1023
+ return;
1024
+ }
1025
+ brotli_decompressor_set_pending_input(self, br, rb_str_new((const char*)next_in, (long)available_in));
1026
+ }
1027
+
1028
+ static size_t
1029
+ brotli_output_buffer_limit(VALUE opts)
1030
+ {
1031
+ VALUE limit_value = brotli_hash_lookup(opts, id_output_buffer_limit);
1032
+
1033
+ if (NIL_P(limit_value)) {
1034
+ return 0;
1035
+ }
1036
+
1037
+ if (rb_cmpint(rb_funcall(limit_value, id_cmp, 1, INT2FIX(0)), limit_value, INT2FIX(0)) <= 0) {
1038
+ rb_raise(rb_eArgError, "output_buffer_limit must be positive");
1039
+ }
1040
+
1041
+ return NUM2SIZET(limit_value);
1042
+ }
1043
+
1044
+ static VALUE
1045
+ rb_decompressor_process(int argc, VALUE* argv, VALUE self)
1046
+ {
1047
+ brotli_decompressor_t *br;
1048
+ VALUE input = Qnil;
1049
+ VALUE opts = Qnil;
1050
+ VALUE input_source = Qnil;
1051
+ VALUE output;
1052
+ size_t available_in;
1053
+ const uint8_t* next_in;
1054
+ size_t output_buffer_limit;
1055
+ BROTLI_BOOL limit_output;
1056
+ BrotliDecoderResult result = BROTLI_DECODER_RESULT_ERROR;
1057
+ uint8_t outbuf[BUFSIZ];
1058
+
1059
+ rb_scan_args(argc, argv, "11", &input, &opts);
1060
+ TypedData_Get_Struct(self, brotli_decompressor_t, &brotli_decompressor_data_type, br);
1061
+ brotli_decompressor_ensure_open(br);
1062
+ output_buffer_limit = brotli_output_buffer_limit(opts);
1063
+ limit_output = output_buffer_limit > 0 ? BROTLI_TRUE : BROTLI_FALSE;
1064
+
1065
+ if (br->finished) {
1066
+ StringValue(input);
1067
+ available_in = (size_t)RSTRING_LEN(input);
1068
+ if (available_in == 0) {
1069
+ return rb_str_buf_new(0);
1070
+ }
1071
+ rb_raise(rb_eBrotli, "Decompressor is finished");
1072
+ }
1073
+
1074
+ StringValue(input);
1075
+ if (brotli_decompressor_needs_output_drain(br)) {
1076
+ if (RSTRING_LEN(input) > 0) {
1077
+ rb_raise(rb_eBrotli,
1078
+ "Decompressor cannot accept more data until pending output is drained");
1079
+ }
1080
+ if (brotli_decompressor_has_pending_input(br)) {
1081
+ input_source = br->pending_input;
1082
+ } else {
1083
+ input_source = input;
1084
+ }
1085
+ } else {
1086
+ input_source = input;
1087
+ }
1088
+
1089
+ if (input_source == br->pending_input) {
1090
+ available_in = brotli_decompressor_pending_input_length(br);
1091
+ next_in = (const uint8_t*)RSTRING_PTR(input_source);
1092
+ } else {
1093
+ available_in = (size_t)RSTRING_LEN(input_source);
1094
+ next_in = (const uint8_t*)RSTRING_PTR(input_source);
1095
+ brotli_decompressor_clear_pending_input(self, br);
1096
+ }
1097
+ br->needs_more_output = BROTLI_FALSE;
1098
+ output = rb_str_buf_new(BUFSIZ);
1099
+
1100
+ for (;;) {
1101
+ size_t chunk_size = BUFSIZ;
1102
+ brotli_decoder_args_t args;
1103
+ size_t produced;
1104
+
1105
+ if (limit_output) {
1106
+ size_t used = (size_t)RSTRING_LEN(output);
1107
+ if (used >= output_buffer_limit) {
1108
+ brotli_decompressor_store_pending_input(self, br, next_in, available_in);
1109
+ br->needs_more_output = BROTLI_TRUE;
1110
+ break;
1111
+ }
1112
+ if (output_buffer_limit - used < chunk_size) {
1113
+ chunk_size = output_buffer_limit - used;
1114
+ }
1115
+ }
1116
+
1117
+ args.state = br->state;
1118
+ args.available_in = available_in;
1119
+ args.next_in = next_in;
1120
+ args.available_out = chunk_size;
1121
+ args.next_out = outbuf;
1122
+ args.result = BROTLI_DECODER_RESULT_ERROR;
1123
+ brotli_decompressor_step(&args);
1124
+ available_in = args.available_in;
1125
+ next_in = args.next_in;
1126
+ result = args.result;
1127
+
1128
+ produced = chunk_size - args.available_out;
1129
+ if (produced > 0) {
1130
+ rb_str_cat(output, (const char*)outbuf, produced);
1131
+ }
1132
+
1133
+ if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
1134
+ if (limit_output && (size_t)RSTRING_LEN(output) >= output_buffer_limit) {
1135
+ brotli_decompressor_store_pending_input(self, br, next_in, available_in);
1136
+ br->needs_more_output = BROTLI_TRUE;
1137
+ break;
1138
+ }
1139
+ continue;
1140
+ }
1141
+ if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
1142
+ if (BrotliDecoderHasMoreOutput(br->state)) {
1143
+ continue;
1144
+ }
1145
+ brotli_decompressor_clear_pending_input(self, br);
1146
+ br->needs_more_output = BROTLI_FALSE;
1147
+ break;
1148
+ }
1149
+ if (result == BROTLI_DECODER_RESULT_SUCCESS) {
1150
+ br->finished = BROTLI_TRUE;
1151
+ brotli_decompressor_store_pending_input(self, br, next_in, available_in);
1152
+ br->needs_more_output = BROTLI_FALSE;
1153
+ break;
1154
+ }
1155
+
1156
+ rb_raise(rb_eBrotli, "%s",
1157
+ BrotliDecoderErrorString(BrotliDecoderGetErrorCode(br->state)));
1158
+ }
1159
+
1160
+ RB_GC_GUARD(input_source);
1161
+ return output;
1162
+ }
1163
+
1164
+ static VALUE
1165
+ rb_decompressor_is_finished(VALUE self)
1166
+ {
1167
+ brotli_decompressor_t *br;
1168
+
1169
+ TypedData_Get_Struct(self, brotli_decompressor_t, &brotli_decompressor_data_type, br);
1170
+ brotli_decompressor_ensure_open(br);
1171
+
1172
+ if (br->finished || BrotliDecoderIsFinished(br->state)) {
1173
+ br->finished = BROTLI_TRUE;
1174
+ return Qtrue;
1175
+ }
1176
+ return Qfalse;
1177
+ }
453
1178
 
454
- rb_writer_finish(self);
1179
+ static VALUE
1180
+ rb_decompressor_can_accept_more_data(VALUE self)
1181
+ {
1182
+ brotli_decompressor_t *br;
1183
+ TypedData_Get_Struct(self, brotli_decompressor_t, &brotli_decompressor_data_type, br);
1184
+ brotli_decompressor_ensure_open(br);
1185
+ return (br->finished || brotli_decompressor_needs_output_drain(br)) ? Qfalse : Qtrue;
1186
+ }
455
1187
 
456
- if (rb_respond_to(br->io, id_close)) {
457
- rb_funcall(br->io, id_close, 0);
1188
+ static VALUE
1189
+ rb_decompressor_unused_data(VALUE self)
1190
+ {
1191
+ brotli_decompressor_t *br;
1192
+ size_t available_in;
1193
+ TypedData_Get_Struct(self, brotli_decompressor_t, &brotli_decompressor_data_type, br);
1194
+ brotli_decompressor_ensure_open(br);
1195
+ available_in = brotli_decompressor_pending_input_length(br);
1196
+ if (!br->finished || available_in == 0) {
1197
+ return rb_str_buf_new(0);
458
1198
  }
459
- return br->io;
1199
+ return rb_str_dup(br->pending_input);
460
1200
  }
461
1201
 
462
1202
  /*******************************************************************************
@@ -466,28 +1206,41 @@ static VALUE rb_writer_close(VALUE self) {
466
1206
  void
467
1207
  Init_brotli(void)
468
1208
  {
469
- #if HAVE_RB_EXT_RACTOR_SAFE
470
1209
  rb_ext_ractor_safe(true);
471
- #endif
472
1210
 
473
1211
  VALUE rb_mBrotli;
474
- VALUE rb_Writer;
475
1212
  rb_mBrotli = rb_define_module("Brotli");
476
1213
  rb_eBrotli = rb_define_class_under(rb_mBrotli, "Error", rb_eStandardError);
477
- rb_global_variable(&rb_eBrotli);
478
- rb_define_singleton_method(rb_mBrotli, "deflate", RUBY_METHOD_FUNC(brotli_deflate), -1);
479
- rb_define_singleton_method(rb_mBrotli, "inflate", RUBY_METHOD_FUNC(brotli_inflate), 1);
480
- rb_define_singleton_method(rb_mBrotli, "version", RUBY_METHOD_FUNC(brotli_version), 0);
1214
+ rb_define_singleton_method(rb_mBrotli, "deflate", brotli_deflate, -1);
1215
+ rb_define_singleton_method(rb_mBrotli, "inflate", brotli_inflate, -1);
1216
+ rb_define_singleton_method(rb_mBrotli, "version", brotli_version, 0);
1217
+ id_cmp = rb_intern("<=>");
1218
+ id_dictionary = rb_intern("dictionary");
1219
+ id_font = rb_intern("font");
1220
+ id_generic = rb_intern("generic");
1221
+ id_lgblock = rb_intern("lgblock");
1222
+ id_lgwin = rb_intern("lgwin");
1223
+ id_mode = rb_intern("mode");
1224
+ id_output_buffer_limit = rb_intern("output_buffer_limit");
1225
+ id_quality = rb_intern("quality");
481
1226
  id_read = rb_intern("read");
482
- // Brotli::Writer
483
- id_write = rb_intern("write");
484
- id_flush = rb_intern("flush");
485
- id_close = rb_intern("close");
486
- rb_Writer = rb_define_class_under(rb_mBrotli, "Writer", rb_cObject);
487
- rb_define_alloc_func(rb_Writer, rb_writer_alloc);
488
- rb_define_method(rb_Writer, "initialize", RUBY_METHOD_FUNC(rb_writer_initialize), -1);
489
- rb_define_method(rb_Writer, "write", RUBY_METHOD_FUNC(rb_writer_write), 1);
490
- rb_define_method(rb_Writer, "finish", RUBY_METHOD_FUNC(rb_writer_finish), 0);
491
- rb_define_method(rb_Writer, "flush", RUBY_METHOD_FUNC(rb_writer_flush), 0);
492
- rb_define_method(rb_Writer, "close", RUBY_METHOD_FUNC(rb_writer_close), 0);
1227
+ id_text = rb_intern("text");
1228
+
1229
+ rb_cBrotliCompressor = rb_define_class_under(rb_mBrotli, "Compressor", rb_cObject);
1230
+ rb_define_alloc_func(rb_cBrotliCompressor, rb_compressor_alloc);
1231
+ rb_define_method(rb_cBrotliCompressor, "initialize", rb_compressor_initialize, -1);
1232
+ rb_define_method(rb_cBrotliCompressor, "process", rb_compressor_process, 1);
1233
+ rb_define_method(rb_cBrotliCompressor, "flush", rb_compressor_flush, 0);
1234
+ rb_define_method(rb_cBrotliCompressor, "finish", rb_compressor_finish, 0);
1235
+ rb_define_method(rb_cBrotliCompressor, "finished?", rb_compressor_is_finished, 0);
1236
+
1237
+ rb_cBrotliDecompressor = rb_define_class_under(rb_mBrotli, "Decompressor", rb_cObject);
1238
+ rb_define_alloc_func(rb_cBrotliDecompressor, rb_decompressor_alloc);
1239
+ rb_define_method(rb_cBrotliDecompressor, "initialize", rb_decompressor_initialize, -1);
1240
+ rb_define_method(rb_cBrotliDecompressor, "process", rb_decompressor_process, -1);
1241
+ rb_define_method(rb_cBrotliDecompressor, "is_finished", rb_decompressor_is_finished, 0);
1242
+ rb_define_method(rb_cBrotliDecompressor, "finished?", rb_decompressor_is_finished, 0);
1243
+ rb_define_method(rb_cBrotliDecompressor, "can_accept_more_data", rb_decompressor_can_accept_more_data, 0);
1244
+ rb_define_method(rb_cBrotliDecompressor, "can_accept_more_data?", rb_decompressor_can_accept_more_data, 0);
1245
+ rb_define_method(rb_cBrotliDecompressor, "unused_data", rb_decompressor_unused_data, 0);
493
1246
  }