brotli 0.9.1 → 0.9.3
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.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/ext/brotli/brotli.c +76 -7
- data/ext/brotli/buffer.c +35 -11
- data/ext/brotli/buffer.h +3 -1
- data/ext/brotli/extconf.rb +14 -0
- data/lib/brotli/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 69c5177d348df73fc4a46b6885e0432228c9050a0c003daa70ac19ec8a161c33
|
|
4
|
+
data.tar.gz: 80b2f49422f8e77dec0c297f2c74e5d3ee8f326f06f958c86d215f4391150527
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4574d6aaeecfcd073d3bb8bdfa21a766f71843f52d9e7e8446549fb93748a79775a7f296dcf2756e28b796daaef229d696c900d68e159c0dc3cee74357c1b1da
|
|
7
|
+
data.tar.gz: 86002231379c75386cce7da64fadae167c04afb5d635fcc46d1bad0b6137d4921e65736463c8585d6c7c8f6b3818db1a453bf1c7091ab59d482c9d7940f270ae
|
data/README.md
CHANGED
|
@@ -192,6 +192,28 @@ You usually do not need `output_buffer_limit:` when:
|
|
|
192
192
|
- you are using `Brotli::Reader`, which already handles incremental buffering for IO-style reads
|
|
193
193
|
- you are happy for each `#process` call to return all currently available output
|
|
194
194
|
|
|
195
|
+
### Large-window Brotli
|
|
196
|
+
|
|
197
|
+
RFC 9841 large-window streams are rejected by default. Applications that require
|
|
198
|
+
this extension, such as readers of the PDF Association Brotli extension, can opt
|
|
199
|
+
in for a single decoder:
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
Brotli.inflate(compressed, large_window: true)
|
|
203
|
+
Brotli::Reader.new(io, large_window: true)
|
|
204
|
+
Brotli::Decompressor.new(large_window: true)
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The option does not enable shared dictionaries or a framing format and does not
|
|
208
|
+
change the default for other decoders.
|
|
209
|
+
|
|
210
|
+
**Memory warning:** with `large_window: true`, the stream header may request a
|
|
211
|
+
window of up to 1 GiB (2^30 bytes), and the decoder allocates its ring buffer
|
|
212
|
+
to match. A small untrusted input can therefore make a single decoder reserve
|
|
213
|
+
close to 1 GiB. `output_buffer_limit:` does not bound this allocation. Enable
|
|
214
|
+
the option only for trusted input, or enforce a memory limit at the process or
|
|
215
|
+
container level.
|
|
216
|
+
|
|
195
217
|
See test/brotli_test.rb for more examples.
|
|
196
218
|
|
|
197
219
|
## Development
|
data/ext/brotli/brotli.c
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
static VALUE rb_eBrotli;
|
|
4
4
|
static ID id_cmp;
|
|
5
5
|
static ID id_dictionary;
|
|
6
|
+
static ID id_large_window;
|
|
6
7
|
static ID id_font;
|
|
7
8
|
static ID id_generic;
|
|
8
9
|
static ID id_lgblock;
|
|
@@ -74,6 +75,7 @@ typedef struct {
|
|
|
74
75
|
BrotliDecoderResult r;
|
|
75
76
|
uint8_t* dict;
|
|
76
77
|
size_t dict_len;
|
|
78
|
+
int oom;
|
|
77
79
|
} brotli_inflate_args_t;
|
|
78
80
|
|
|
79
81
|
static void
|
|
@@ -116,14 +118,19 @@ brotli_inflate_no_gvl(void *arg)
|
|
|
116
118
|
if (r != BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT) {
|
|
117
119
|
break;
|
|
118
120
|
}
|
|
119
|
-
append_buffer(buffer, output, BUFSIZ)
|
|
121
|
+
if (append_buffer(buffer, output, BUFSIZ) != 0) {
|
|
122
|
+
args->oom = 1;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
120
125
|
available_out = BUFSIZ;
|
|
121
126
|
next_out = output;
|
|
122
127
|
}
|
|
123
128
|
|
|
124
129
|
if (r == BROTLI_DECODER_RESULT_SUCCESS) {
|
|
125
130
|
if (next_out != output) {
|
|
126
|
-
append_buffer(buffer, output, next_out - output)
|
|
131
|
+
if (append_buffer(buffer, output, next_out - output) != 0) {
|
|
132
|
+
args->oom = 1;
|
|
133
|
+
}
|
|
127
134
|
}
|
|
128
135
|
}
|
|
129
136
|
args->r = r;
|
|
@@ -138,6 +145,13 @@ brotli_inflate_take_result(VALUE arg)
|
|
|
138
145
|
return rb_str_new(args->buffer->ptr, args->buffer->used);
|
|
139
146
|
}
|
|
140
147
|
|
|
148
|
+
static VALUE
|
|
149
|
+
brotli_inflate_run(VALUE arg)
|
|
150
|
+
{
|
|
151
|
+
rb_thread_call_without_gvl(brotli_inflate_no_gvl, (void *)arg, NULL, NULL);
|
|
152
|
+
return Qnil;
|
|
153
|
+
}
|
|
154
|
+
|
|
141
155
|
static VALUE
|
|
142
156
|
brotli_inflate(int argc, VALUE *argv, VALUE self)
|
|
143
157
|
{
|
|
@@ -147,6 +161,7 @@ brotli_inflate(int argc, VALUE *argv, VALUE self)
|
|
|
147
161
|
VALUE dict = Qnil;
|
|
148
162
|
const char *error = NULL;
|
|
149
163
|
brotli_inflate_args_t args = {0};
|
|
164
|
+
int state = 0;
|
|
150
165
|
|
|
151
166
|
(void)self;
|
|
152
167
|
|
|
@@ -166,8 +181,18 @@ brotli_inflate(int argc, VALUE *argv, VALUE self)
|
|
|
166
181
|
args.len = (size_t)RSTRING_LEN(str);
|
|
167
182
|
args.s = brotli_decoder_state_create();
|
|
168
183
|
args.buffer = create_buffer(BUFSIZ);
|
|
184
|
+
if (!args.buffer) {
|
|
185
|
+
brotli_inflate_cleanup(&args);
|
|
186
|
+
rb_raise(rb_eNoMemError, "failed to allocate decompression buffer");
|
|
187
|
+
}
|
|
169
188
|
args.r = BROTLI_DECODER_RESULT_ERROR;
|
|
170
189
|
|
|
190
|
+
if (RTEST(brotli_hash_lookup(opts, id_large_window)) &&
|
|
191
|
+
!BrotliDecoderSetParameter(args.s, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1)) {
|
|
192
|
+
brotli_inflate_cleanup(&args);
|
|
193
|
+
rb_raise(rb_eBrotli, "Failed to enable large-window decompression");
|
|
194
|
+
}
|
|
195
|
+
|
|
171
196
|
if (!NIL_P(dict)) {
|
|
172
197
|
args.dict = (uint8_t*)RSTRING_PTR(dict);
|
|
173
198
|
args.dict_len = (size_t)RSTRING_LEN(dict);
|
|
@@ -176,12 +201,26 @@ brotli_inflate(int argc, VALUE *argv, VALUE self)
|
|
|
176
201
|
args.dict_len = 0;
|
|
177
202
|
}
|
|
178
203
|
|
|
179
|
-
|
|
204
|
+
/*
|
|
205
|
+
* rb_thread_call_without_gvl() handles pending interrupts (Thread#raise,
|
|
206
|
+
* Timeout, signals) around the worker and may raise from there. Catch it
|
|
207
|
+
* so the decoder state and output buffer are released before re-raising.
|
|
208
|
+
*/
|
|
209
|
+
rb_protect(brotli_inflate_run, (VALUE)&args, &state);
|
|
180
210
|
RB_GC_GUARD(str);
|
|
181
211
|
RB_GC_GUARD(dict);
|
|
212
|
+
if (state) {
|
|
213
|
+
brotli_inflate_cleanup(&args);
|
|
214
|
+
rb_jump_tag(state);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/* Check before args.r: a failed final append leaves r at SUCCESS. */
|
|
218
|
+
if (args.oom) {
|
|
219
|
+
brotli_inflate_cleanup(&args);
|
|
220
|
+
rb_raise(rb_eNoMemError, "failed to allocate decompression buffer");
|
|
221
|
+
}
|
|
182
222
|
|
|
183
223
|
if (args.r == BROTLI_DECODER_RESULT_SUCCESS) {
|
|
184
|
-
int state = 0;
|
|
185
224
|
value = rb_protect(brotli_inflate_take_result, (VALUE)&args, &state);
|
|
186
225
|
if (state) {
|
|
187
226
|
brotli_inflate_cleanup(&args);
|
|
@@ -324,6 +363,7 @@ typedef struct {
|
|
|
324
363
|
buffer_t* buffer;
|
|
325
364
|
BROTLI_BOOL finished;
|
|
326
365
|
BrotliEncoderPreparedDictionary* prepared_dict;
|
|
366
|
+
int oom;
|
|
327
367
|
} brotli_deflate_args_t;
|
|
328
368
|
|
|
329
369
|
static void
|
|
@@ -364,7 +404,11 @@ brotli_deflate_no_gvl(void *arg)
|
|
|
364
404
|
args->finished = BROTLI_FALSE;
|
|
365
405
|
break;
|
|
366
406
|
} else {
|
|
367
|
-
append_buffer(buffer, output, next_out - output)
|
|
407
|
+
if (append_buffer(buffer, output, next_out - output) != 0) {
|
|
408
|
+
args->oom = 1;
|
|
409
|
+
args->finished = BROTLI_FALSE;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
368
412
|
available_out = BUFSIZ;
|
|
369
413
|
next_out = output;
|
|
370
414
|
|
|
@@ -385,6 +429,13 @@ brotli_deflate_take_result(VALUE arg)
|
|
|
385
429
|
return rb_str_new(args->buffer->ptr, args->buffer->used);
|
|
386
430
|
}
|
|
387
431
|
|
|
432
|
+
static VALUE
|
|
433
|
+
brotli_deflate_run(VALUE arg)
|
|
434
|
+
{
|
|
435
|
+
rb_thread_call_without_gvl(brotli_deflate_no_gvl, (void *)arg, NULL, NULL);
|
|
436
|
+
return Qnil;
|
|
437
|
+
}
|
|
438
|
+
|
|
388
439
|
static VALUE
|
|
389
440
|
brotli_deflate(int argc, VALUE *argv, VALUE self)
|
|
390
441
|
{
|
|
@@ -395,6 +446,7 @@ brotli_deflate(int argc, VALUE *argv, VALUE self)
|
|
|
395
446
|
brotli_deflate_args_t args = {0};
|
|
396
447
|
size_t max_compressed_size;
|
|
397
448
|
const char *err;
|
|
449
|
+
int state = 0;
|
|
398
450
|
|
|
399
451
|
(void)self;
|
|
400
452
|
|
|
@@ -418,6 +470,10 @@ brotli_deflate(int argc, VALUE *argv, VALUE self)
|
|
|
418
470
|
}
|
|
419
471
|
max_compressed_size = BrotliEncoderMaxCompressedSize(args.len);
|
|
420
472
|
args.buffer = create_buffer(max_compressed_size);
|
|
473
|
+
if (!args.buffer) {
|
|
474
|
+
brotli_deflate_cleanup(&args);
|
|
475
|
+
rb_raise(rb_eNoMemError, "failed to allocate compression buffer");
|
|
476
|
+
}
|
|
421
477
|
args.finished = BROTLI_FALSE;
|
|
422
478
|
|
|
423
479
|
args.prepared_dict = NULL;
|
|
@@ -440,11 +496,19 @@ brotli_deflate(int argc, VALUE *argv, VALUE self)
|
|
|
440
496
|
}
|
|
441
497
|
}
|
|
442
498
|
|
|
443
|
-
|
|
499
|
+
/* See brotli_inflate: an interrupt may raise out of the GVL release. */
|
|
500
|
+
rb_protect(brotli_deflate_run, (VALUE)&args, &state);
|
|
444
501
|
RB_GC_GUARD(str);
|
|
445
502
|
RB_GC_GUARD(dict);
|
|
503
|
+
if (state) {
|
|
504
|
+
brotli_deflate_cleanup(&args);
|
|
505
|
+
rb_jump_tag(state);
|
|
506
|
+
}
|
|
507
|
+
if (args.oom) {
|
|
508
|
+
brotli_deflate_cleanup(&args);
|
|
509
|
+
rb_raise(rb_eNoMemError, "failed to allocate compression buffer");
|
|
510
|
+
}
|
|
446
511
|
if (args.finished == BROTLI_TRUE) {
|
|
447
|
-
int state = 0;
|
|
448
512
|
value = rb_protect(brotli_deflate_take_result, (VALUE)&args, &state);
|
|
449
513
|
if (state) {
|
|
450
514
|
brotli_deflate_cleanup(&args);
|
|
@@ -942,6 +1006,10 @@ rb_decompressor_initialize(int argc, VALUE* argv, VALUE self)
|
|
|
942
1006
|
rb_scan_args(argc, argv, "01", &opts);
|
|
943
1007
|
TypedData_Get_Struct(self, brotli_decompressor_t, &brotli_decompressor_data_type, br);
|
|
944
1008
|
brotli_decompressor_reset(self, br);
|
|
1009
|
+
if (RTEST(brotli_hash_lookup(opts, id_large_window)) &&
|
|
1010
|
+
!BrotliDecoderSetParameter(br->state, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1)) {
|
|
1011
|
+
rb_raise(rb_eBrotli, "Failed to enable large-window decompression");
|
|
1012
|
+
}
|
|
945
1013
|
brotli_decompressor_attach_dictionary(br, opts);
|
|
946
1014
|
|
|
947
1015
|
return self;
|
|
@@ -1187,6 +1255,7 @@ Init_brotli(void)
|
|
|
1187
1255
|
rb_define_singleton_method(rb_mBrotli, "version", brotli_version, 0);
|
|
1188
1256
|
id_cmp = rb_intern("<=>");
|
|
1189
1257
|
id_dictionary = rb_intern("dictionary");
|
|
1258
|
+
id_large_window = rb_intern("large_window");
|
|
1190
1259
|
id_font = rb_intern("font");
|
|
1191
1260
|
id_generic = rb_intern("generic");
|
|
1192
1261
|
id_lgblock = rb_intern("lgblock");
|
data/ext/brotli/buffer.c
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
#include "buffer.h"
|
|
2
|
-
#include "ruby/ruby.h"
|
|
3
2
|
|
|
4
3
|
#include <stdint.h>
|
|
4
|
+
#include <stdlib.h>
|
|
5
5
|
#include <string.h>
|
|
6
6
|
|
|
7
7
|
#define BUFFER_INITIAL_SIZE 1024
|
|
8
8
|
|
|
9
|
+
/*
|
|
10
|
+
* append_buffer runs inside rb_thread_call_without_gvl. A failing Ruby
|
|
11
|
+
* allocator there raises NoMemoryError past the caller's cleanup, so this
|
|
12
|
+
* buffer uses malloc/realloc/free and reports failure through return values.
|
|
13
|
+
*/
|
|
14
|
+
|
|
9
15
|
buffer_t*
|
|
10
16
|
create_buffer(size_t initial)
|
|
11
17
|
{
|
|
12
|
-
buffer_t *buffer =
|
|
18
|
+
buffer_t *buffer = malloc(sizeof(*buffer));
|
|
19
|
+
|
|
20
|
+
if (!buffer) {
|
|
21
|
+
return NULL;
|
|
22
|
+
}
|
|
13
23
|
buffer->used = 0;
|
|
14
24
|
buffer->size = initial > 0 ? initial : BUFFER_INITIAL_SIZE;
|
|
15
|
-
buffer->ptr =
|
|
25
|
+
buffer->ptr = malloc(buffer->size);
|
|
26
|
+
if (!buffer->ptr) {
|
|
27
|
+
free(buffer);
|
|
28
|
+
return NULL;
|
|
29
|
+
}
|
|
16
30
|
return buffer;
|
|
17
31
|
}
|
|
18
32
|
|
|
@@ -23,8 +37,8 @@ delete_buffer(buffer_t* buffer)
|
|
|
23
37
|
return;
|
|
24
38
|
}
|
|
25
39
|
|
|
26
|
-
|
|
27
|
-
|
|
40
|
+
free(buffer->ptr);
|
|
41
|
+
free(buffer);
|
|
28
42
|
}
|
|
29
43
|
|
|
30
44
|
static size_t
|
|
@@ -42,27 +56,37 @@ buffer_size_for(size_t current, size_t required)
|
|
|
42
56
|
return size;
|
|
43
57
|
}
|
|
44
58
|
|
|
45
|
-
static
|
|
59
|
+
static int
|
|
46
60
|
expand_buffer(buffer_t* buffer, size_t required)
|
|
47
61
|
{
|
|
48
|
-
|
|
49
|
-
|
|
62
|
+
size_t size = buffer_size_for(buffer->size, required);
|
|
63
|
+
char* ptr = realloc(buffer->ptr, size);
|
|
64
|
+
|
|
65
|
+
if (!ptr) {
|
|
66
|
+
return -1;
|
|
67
|
+
}
|
|
68
|
+
buffer->ptr = ptr;
|
|
69
|
+
buffer->size = size;
|
|
70
|
+
return 0;
|
|
50
71
|
}
|
|
51
72
|
|
|
52
|
-
|
|
73
|
+
int
|
|
53
74
|
append_buffer(buffer_t* buffer, const void* ptr, size_t size)
|
|
54
75
|
{
|
|
55
76
|
size_t required;
|
|
56
77
|
|
|
57
78
|
if (size == 0) {
|
|
58
|
-
return;
|
|
79
|
+
return 0;
|
|
59
80
|
}
|
|
60
81
|
|
|
61
82
|
required = buffer->used + size;
|
|
62
83
|
if (required > buffer->size) {
|
|
63
|
-
expand_buffer(buffer, required)
|
|
84
|
+
if (expand_buffer(buffer, required) != 0) {
|
|
85
|
+
return -1;
|
|
86
|
+
}
|
|
64
87
|
}
|
|
65
88
|
|
|
66
89
|
memcpy(buffer->ptr + buffer->used, ptr, size);
|
|
67
90
|
buffer->used += size;
|
|
91
|
+
return 0;
|
|
68
92
|
}
|
data/ext/brotli/buffer.h
CHANGED
|
@@ -9,8 +9,10 @@ typedef struct {
|
|
|
9
9
|
size_t used;
|
|
10
10
|
} buffer_t;
|
|
11
11
|
|
|
12
|
+
/* Never raise, so they are safe without the GVL. create_buffer returns NULL
|
|
13
|
+
* and append_buffer returns -1 when allocation fails. */
|
|
12
14
|
buffer_t* create_buffer(size_t initial);
|
|
13
15
|
void delete_buffer(buffer_t* buffer);
|
|
14
|
-
|
|
16
|
+
int append_buffer(buffer_t* buffer, const void* ptr, size_t size);
|
|
15
17
|
|
|
16
18
|
#endif /* BUFFER_H */
|
data/ext/brotli/extconf.rb
CHANGED
|
@@ -129,5 +129,19 @@ if use_vendored_brotli
|
|
|
129
129
|
$INCFLAGS << " -Ienc -Idec -Icommon -Iinclude"
|
|
130
130
|
end
|
|
131
131
|
|
|
132
|
+
# Leak-harness build hooks (inert unless the env var is set).
|
|
133
|
+
# One append reaches brotli.c, buffer.c, and the vendored sources, because
|
|
134
|
+
# add_vendored_sources_to_makefile rewrites ORIG_SRCS/OBJS after create_makefile.
|
|
135
|
+
if ENV["BROTLI_DEBUG"] || ENV["BROTLI_ASAN"]
|
|
136
|
+
# Append -O0 last so it overrides any earlier optimization flag already in
|
|
137
|
+
# $CFLAGS (-O2/-O3/-Os/-Ofast/bare -O) -- the last -O on the command line wins
|
|
138
|
+
# for both gcc and clang. A gsub(/-O\d/) would miss -Os/-Ofast/-O.
|
|
139
|
+
$CFLAGS << " -O0 -g3 -fno-omit-frame-pointer"
|
|
140
|
+
end
|
|
141
|
+
if ENV["BROTLI_ASAN"]
|
|
142
|
+
$CFLAGS << " -fsanitize=address"
|
|
143
|
+
$LDFLAGS << " -fsanitize=address"
|
|
144
|
+
end
|
|
145
|
+
|
|
132
146
|
create_makefile("brotli/brotli")
|
|
133
147
|
add_vendored_sources_to_makefile if use_vendored_brotli
|
data/lib/brotli/version.rb
CHANGED