ruby-brs 1.1.1 → 1.1.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.
- checksums.yaml +4 -4
- data/README.md +14 -12
- data/ext/brs_ext/common.h +3 -0
- data/ext/brs_ext/error.c +0 -1
- data/ext/brs_ext/io.c +48 -50
- data/ext/brs_ext/main.c +0 -1
- data/ext/brs_ext/option.c +0 -3
- data/ext/brs_ext/option.h +2 -3
- data/ext/brs_ext/stream/compressor.c +17 -20
- data/ext/brs_ext/stream/compressor.h +3 -3
- data/ext/brs_ext/stream/decompressor.c +13 -16
- data/ext/brs_ext/stream/decompressor.h +3 -3
- data/ext/brs_ext/string.c +8 -10
- data/lib/brs/stream/abstract.rb +8 -6
- data/lib/brs/stream/reader.rb +1 -1
- data/lib/brs/stream/writer.rb +1 -1
- data/lib/brs/stream/writer_helpers.rb +3 -3
- data/lib/brs/validation.rb +14 -1
- data/lib/brs/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f79255ffdaf743bde0edc13c314d1cf7f12402a9993389d6a86dea3d449b4e21
|
4
|
+
data.tar.gz: 8ca40ddca9e58f2876c33bd17bc353bdb739cf79d344ff5bf3c217c52a9cd2ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 801a6df0e461a1df124647362c10a6af08d0e5621ef5682987d6d0615b548e3fa51d5c184afd804be33cf4da74ce0ef810f8209851f5b5efe31037ac6912f205
|
7
|
+
data.tar.gz: ef62bbc6958e7de16d59eb0a56f84cc9c7ae7f88738219d642562455d7d7815df29cd94fa32da7f23dbf7065310c46e0081e3d0ce0443af8aa5d0257d1a765ba
|
data/README.md
CHANGED
@@ -190,7 +190,7 @@ File maintains both source and destination buffers, it accepts both `source_buff
|
|
190
190
|
|
191
191
|
## Stream::Writer
|
192
192
|
|
193
|
-
Its behaviour is similar to builtin [`Zlib::GzipWriter`](https://ruby-doc.org/stdlib-2.
|
193
|
+
Its behaviour is similar to builtin [`Zlib::GzipWriter`](https://ruby-doc.org/stdlib-2.7.0/libdoc/zlib/rdoc/Zlib/GzipWriter.html).
|
194
194
|
|
195
195
|
Writer maintains destination buffer only, so it accepts `destination_buffer_length` option only.
|
196
196
|
|
@@ -228,7 +228,7 @@ Set another encodings, `nil` is just for compatibility with `IO`.
|
|
228
228
|
#tell
|
229
229
|
```
|
230
230
|
|
231
|
-
See [`IO`](https://ruby-doc.org/core-2.
|
231
|
+
See [`IO`](https://ruby-doc.org/core-2.7.0/IO.html) docs.
|
232
232
|
|
233
233
|
```
|
234
234
|
#write(*objects)
|
@@ -238,7 +238,7 @@ See [`IO`](https://ruby-doc.org/core-2.6.1/IO.html) docs.
|
|
238
238
|
#closed?
|
239
239
|
```
|
240
240
|
|
241
|
-
See [`Zlib::GzipWriter`](https://ruby-doc.org/stdlib-2.
|
241
|
+
See [`Zlib::GzipWriter`](https://ruby-doc.org/stdlib-2.7.0/libdoc/zlib/rdoc/Zlib/GzipWriter.html) docs.
|
242
242
|
|
243
243
|
```
|
244
244
|
#write_nonblock(object, *options)
|
@@ -260,11 +260,11 @@ Behaviour is the same as `IO#write_nonblock` method.
|
|
260
260
|
#puts(*objects)
|
261
261
|
```
|
262
262
|
|
263
|
-
Typical helpers, see [`Zlib::GzipWriter`](https://ruby-doc.org/stdlib-2.
|
263
|
+
Typical helpers, see [`Zlib::GzipWriter`](https://ruby-doc.org/stdlib-2.7.0/libdoc/zlib/rdoc/Zlib/GzipWriter.html) docs.
|
264
264
|
|
265
265
|
## Stream::Reader
|
266
266
|
|
267
|
-
Its behaviour is similar to builtin [`Zlib::GzipReader`](https://ruby-doc.org/stdlib-2.
|
267
|
+
Its behaviour is similar to builtin [`Zlib::GzipReader`](https://ruby-doc.org/stdlib-2.7.0/libdoc/zlib/rdoc/Zlib/GzipReader.html).
|
268
268
|
|
269
269
|
Reader maintains both source and destination buffers, it accepts both `source_buffer_length` and `destination_buffer_length` options.
|
270
270
|
|
@@ -299,7 +299,7 @@ Set another encodings.
|
|
299
299
|
#tell
|
300
300
|
```
|
301
301
|
|
302
|
-
See [`IO`](https://ruby-doc.org/core-2.
|
302
|
+
See [`IO`](https://ruby-doc.org/core-2.7.0/IO.html) docs.
|
303
303
|
|
304
304
|
```
|
305
305
|
#read(bytes_to_read = nil, out_buffer = nil)
|
@@ -309,14 +309,14 @@ See [`IO`](https://ruby-doc.org/core-2.6.1/IO.html) docs.
|
|
309
309
|
#closed?
|
310
310
|
```
|
311
311
|
|
312
|
-
See [`Zlib::GzipReader`](https://ruby-doc.org/stdlib-2.
|
312
|
+
See [`Zlib::GzipReader`](https://ruby-doc.org/stdlib-2.7.0/libdoc/zlib/rdoc/Zlib/GzipReader.html) docs.
|
313
313
|
|
314
314
|
```
|
315
315
|
#readpartial(bytes_to_read = nil, out_buffer = nil)
|
316
316
|
#read_nonblock(bytes_to_read, out_buffer = nil, *options)
|
317
317
|
```
|
318
318
|
|
319
|
-
See [`IO`](https://ruby-doc.org/core-2.
|
319
|
+
See [`IO`](https://ruby-doc.org/core-2.7.0/IO.html) docs.
|
320
320
|
|
321
321
|
```
|
322
322
|
#getbyte
|
@@ -339,13 +339,15 @@ See [`IO`](https://ruby-doc.org/core-2.6.1/IO.html) docs.
|
|
339
339
|
#ungetline(line)
|
340
340
|
```
|
341
341
|
|
342
|
-
Typical helpers, see [`Zlib::GzipReader`](https://ruby-doc.org/stdlib-2.
|
342
|
+
Typical helpers, see [`Zlib::GzipReader`](https://ruby-doc.org/stdlib-2.7.0/libdoc/zlib/rdoc/Zlib/GzipReader.html) docs.
|
343
343
|
|
344
344
|
## CI
|
345
345
|
|
346
|
-
|
347
|
-
|
348
|
-
|
346
|
+
See universal test script [scripts/ci_test.sh](scripts/ci_test.sh) for CI.
|
347
|
+
Please visit [scripts/test-images](scripts/test-images).
|
348
|
+
You can run this test script using many native and cross images.
|
349
|
+
|
350
|
+
Cirrus CI uses `x86_64-pc-linux-gnu` image, Circle CI - `x86_64-gentoo-linux-musl` image.
|
349
351
|
|
350
352
|
## License
|
351
353
|
|
data/ext/brs_ext/common.h
CHANGED
data/ext/brs_ext/error.c
CHANGED
data/ext/brs_ext/io.c
CHANGED
@@ -6,13 +6,11 @@
|
|
6
6
|
#include <brotli/decode.h>
|
7
7
|
#include <brotli/encode.h>
|
8
8
|
#include <brotli/types.h>
|
9
|
-
#include <stdint.h>
|
10
9
|
#include <stdio.h>
|
11
10
|
#include <stdlib.h>
|
12
11
|
#include <string.h>
|
13
12
|
|
14
13
|
#include "brs_ext/buffer.h"
|
15
|
-
#include "brs_ext/common.h"
|
16
14
|
#include "brs_ext/error.h"
|
17
15
|
#include "brs_ext/io.h"
|
18
16
|
#include "brs_ext/macro.h"
|
@@ -26,7 +24,7 @@ enum {
|
|
26
24
|
|
27
25
|
// -- file --
|
28
26
|
|
29
|
-
static inline brs_ext_result_t read_file(FILE* source_file,
|
27
|
+
static inline brs_ext_result_t read_file(FILE* source_file, brs_ext_byte_t* source_buffer, size_t* source_length_ptr, size_t source_buffer_length)
|
30
28
|
{
|
31
29
|
size_t read_length = fread(source_buffer, 1, source_buffer_length, source_file);
|
32
30
|
if (read_length == 0 && feof(source_file)) {
|
@@ -42,7 +40,7 @@ static inline brs_ext_result_t read_file(FILE* source_file, uint8_t* source_buff
|
|
42
40
|
return 0;
|
43
41
|
}
|
44
42
|
|
45
|
-
static inline brs_ext_result_t write_file(FILE* destination_file,
|
43
|
+
static inline brs_ext_result_t write_file(FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t destination_length)
|
46
44
|
{
|
47
45
|
size_t written_length = fwrite(destination_buffer, 1, destination_length, destination_file);
|
48
46
|
if (written_length != destination_length) {
|
@@ -55,15 +53,15 @@ static inline brs_ext_result_t write_file(FILE* destination_file, uint8_t* desti
|
|
55
53
|
// -- buffer --
|
56
54
|
|
57
55
|
static inline brs_ext_result_t create_buffers(
|
58
|
-
|
59
|
-
|
56
|
+
brs_ext_byte_t** source_buffer_ptr, size_t source_buffer_length,
|
57
|
+
brs_ext_byte_t** destination_buffer_ptr, size_t destination_buffer_length)
|
60
58
|
{
|
61
|
-
|
59
|
+
brs_ext_byte_t* source_buffer = malloc(source_buffer_length);
|
62
60
|
if (source_buffer == NULL) {
|
63
61
|
return BRS_EXT_ERROR_ALLOCATE_FAILED;
|
64
62
|
}
|
65
63
|
|
66
|
-
|
64
|
+
brs_ext_byte_t* destination_buffer = malloc(destination_buffer_length);
|
67
65
|
if (destination_buffer == NULL) {
|
68
66
|
free(source_buffer);
|
69
67
|
return BRS_EXT_ERROR_ALLOCATE_FAILED;
|
@@ -82,12 +80,12 @@ static inline brs_ext_result_t create_buffers(
|
|
82
80
|
// Algorithm can use same buffer again.
|
83
81
|
|
84
82
|
static inline brs_ext_result_t read_more_source(
|
85
|
-
FILE*
|
86
|
-
const
|
87
|
-
|
83
|
+
FILE* source_file,
|
84
|
+
const brs_ext_byte_t** source_ptr, size_t* source_length_ptr,
|
85
|
+
brs_ext_byte_t* source_buffer, size_t source_buffer_length)
|
88
86
|
{
|
89
|
-
const
|
90
|
-
size_t
|
87
|
+
const brs_ext_byte_t* source = *source_ptr;
|
88
|
+
size_t source_length = *source_length_ptr;
|
91
89
|
|
92
90
|
if (source != source_buffer) {
|
93
91
|
if (source_length != 0) {
|
@@ -104,8 +102,8 @@ static inline brs_ext_result_t read_more_source(
|
|
104
102
|
return BRS_EXT_ERROR_NOT_ENOUGH_SOURCE_BUFFER;
|
105
103
|
}
|
106
104
|
|
107
|
-
|
108
|
-
size_t
|
105
|
+
brs_ext_byte_t* remaining_source_buffer = source_buffer + source_length;
|
106
|
+
size_t new_source_length;
|
109
107
|
|
110
108
|
brs_ext_result_t ext_result = read_file(source_file, remaining_source_buffer, &new_source_length, remaining_source_buffer_length);
|
111
109
|
if (ext_result != 0) {
|
@@ -160,8 +158,8 @@ static inline brs_ext_result_t read_more_source(
|
|
160
158
|
// Than algorithm can use same buffer again.
|
161
159
|
|
162
160
|
static inline brs_ext_result_t flush_destination_buffer(
|
163
|
-
FILE*
|
164
|
-
|
161
|
+
FILE* destination_file,
|
162
|
+
brs_ext_byte_t* destination_buffer, size_t* destination_length_ptr, size_t destination_buffer_length)
|
165
163
|
{
|
166
164
|
if (*destination_length_ptr == 0) {
|
167
165
|
// We want to write more data at once, than buffer has.
|
@@ -178,7 +176,7 @@ static inline brs_ext_result_t flush_destination_buffer(
|
|
178
176
|
return 0;
|
179
177
|
}
|
180
178
|
|
181
|
-
static inline brs_ext_result_t write_remaining_destination(FILE* destination_file,
|
179
|
+
static inline brs_ext_result_t write_remaining_destination(FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t destination_length)
|
182
180
|
{
|
183
181
|
if (destination_length == 0) {
|
184
182
|
return 0;
|
@@ -203,17 +201,17 @@ static inline brs_ext_result_t write_remaining_destination(FILE* destination_fil
|
|
203
201
|
// -- compress --
|
204
202
|
|
205
203
|
static inline brs_ext_result_t buffered_compress(
|
206
|
-
BrotliEncoderState*
|
207
|
-
const
|
208
|
-
FILE* destination_file,
|
204
|
+
BrotliEncoderState* state_ptr,
|
205
|
+
const brs_ext_byte_t** source_ptr, size_t* source_length_ptr,
|
206
|
+
FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t* destination_length_ptr, size_t destination_buffer_length)
|
209
207
|
{
|
210
208
|
BROTLI_BOOL result;
|
211
209
|
brs_ext_result_t ext_result;
|
212
210
|
|
213
211
|
while (true) {
|
214
|
-
|
215
|
-
size_t
|
216
|
-
size_t
|
212
|
+
brs_ext_byte_t* remaining_destination_buffer = destination_buffer + *destination_length_ptr;
|
213
|
+
size_t remaining_destination_buffer_length = destination_buffer_length - *destination_length_ptr;
|
214
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
|
217
215
|
|
218
216
|
result = BrotliEncoderCompressStream(
|
219
217
|
state_ptr,
|
@@ -248,18 +246,18 @@ static inline brs_ext_result_t buffered_compress(
|
|
248
246
|
|
249
247
|
static inline brs_ext_result_t buffered_compressor_finish(
|
250
248
|
BrotliEncoderState* state_ptr,
|
251
|
-
FILE* destination_file,
|
249
|
+
FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t* destination_length_ptr, size_t destination_buffer_length)
|
252
250
|
{
|
253
251
|
BROTLI_BOOL result;
|
254
252
|
brs_ext_result_t ext_result;
|
255
253
|
|
256
|
-
const
|
257
|
-
size_t
|
254
|
+
const brs_ext_byte_t* source = NULL;
|
255
|
+
size_t source_length = 0;
|
258
256
|
|
259
257
|
while (true) {
|
260
|
-
|
261
|
-
size_t
|
262
|
-
size_t
|
258
|
+
brs_ext_byte_t* remaining_destination_buffer = destination_buffer + *destination_length_ptr;
|
259
|
+
size_t remaining_destination_buffer_length = destination_buffer_length - *destination_length_ptr;
|
260
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
|
263
261
|
|
264
262
|
result = BrotliEncoderCompressStream(
|
265
263
|
state_ptr,
|
@@ -294,14 +292,14 @@ static inline brs_ext_result_t buffered_compressor_finish(
|
|
294
292
|
|
295
293
|
static inline brs_ext_result_t compress(
|
296
294
|
BrotliEncoderState* state_ptr,
|
297
|
-
FILE* source_file,
|
298
|
-
FILE* destination_file,
|
295
|
+
FILE* source_file, brs_ext_byte_t* source_buffer, size_t source_buffer_length,
|
296
|
+
FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t destination_buffer_length)
|
299
297
|
{
|
300
298
|
brs_ext_result_t ext_result;
|
301
299
|
|
302
|
-
const
|
303
|
-
size_t
|
304
|
-
size_t
|
300
|
+
const brs_ext_byte_t* source = source_buffer;
|
301
|
+
size_t source_length = 0;
|
302
|
+
size_t destination_length = 0;
|
305
303
|
|
306
304
|
BUFFERED_READ_SOURCE(
|
307
305
|
buffered_compress,
|
@@ -347,8 +345,8 @@ VALUE brs_ext_compress_io(VALUE BRS_EXT_UNUSED(self), VALUE source, VALUE destin
|
|
347
345
|
destination_buffer_length = BRS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_COMPRESSOR;
|
348
346
|
}
|
349
347
|
|
350
|
-
|
351
|
-
|
348
|
+
brs_ext_byte_t* source_buffer;
|
349
|
+
brs_ext_byte_t* destination_buffer;
|
352
350
|
|
353
351
|
ext_result = create_buffers(
|
354
352
|
&source_buffer, source_buffer_length,
|
@@ -381,17 +379,17 @@ VALUE brs_ext_compress_io(VALUE BRS_EXT_UNUSED(self), VALUE source, VALUE destin
|
|
381
379
|
// -- decompress --
|
382
380
|
|
383
381
|
static inline brs_ext_result_t buffered_decompress(
|
384
|
-
BrotliDecoderState*
|
385
|
-
const
|
386
|
-
FILE* destination_file,
|
382
|
+
BrotliDecoderState* state_ptr,
|
383
|
+
const brs_ext_byte_t** source_ptr, size_t* source_length_ptr,
|
384
|
+
FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t* destination_length_ptr, size_t destination_buffer_length)
|
387
385
|
{
|
388
386
|
BrotliDecoderResult result;
|
389
387
|
brs_ext_result_t ext_result;
|
390
388
|
|
391
389
|
while (true) {
|
392
|
-
|
393
|
-
size_t
|
394
|
-
size_t
|
390
|
+
brs_ext_byte_t* remaining_destination_buffer = destination_buffer + *destination_length_ptr;
|
391
|
+
size_t remaining_destination_buffer_length = destination_buffer_length - *destination_length_ptr;
|
392
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
|
395
393
|
|
396
394
|
result = BrotliDecoderDecompressStream(
|
397
395
|
state_ptr,
|
@@ -429,14 +427,14 @@ static inline brs_ext_result_t buffered_decompress(
|
|
429
427
|
|
430
428
|
static inline brs_ext_result_t decompress(
|
431
429
|
BrotliDecoderState* state_ptr,
|
432
|
-
FILE* source_file,
|
433
|
-
FILE* destination_file,
|
430
|
+
FILE* source_file, brs_ext_byte_t* source_buffer, size_t source_buffer_length,
|
431
|
+
FILE* destination_file, brs_ext_byte_t* destination_buffer, size_t destination_buffer_length)
|
434
432
|
{
|
435
433
|
brs_ext_result_t ext_result;
|
436
434
|
|
437
|
-
const
|
438
|
-
size_t
|
439
|
-
size_t
|
435
|
+
const brs_ext_byte_t* source = source_buffer;
|
436
|
+
size_t source_length = 0;
|
437
|
+
size_t destination_length = 0;
|
440
438
|
|
441
439
|
BUFFERED_READ_SOURCE(
|
442
440
|
buffered_decompress,
|
@@ -474,8 +472,8 @@ VALUE brs_ext_decompress_io(VALUE BRS_EXT_UNUSED(self), VALUE source, VALUE dest
|
|
474
472
|
destination_buffer_length = BRS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_DECOMPRESSOR;
|
475
473
|
}
|
476
474
|
|
477
|
-
|
478
|
-
|
475
|
+
brs_ext_byte_t* source_buffer;
|
476
|
+
brs_ext_byte_t* destination_buffer;
|
479
477
|
|
480
478
|
ext_result = create_buffers(
|
481
479
|
&source_buffer, source_buffer_length,
|
data/ext/brs_ext/main.c
CHANGED
data/ext/brs_ext/option.c
CHANGED
data/ext/brs_ext/option.h
CHANGED
@@ -7,7 +7,6 @@
|
|
7
7
|
#include <brotli/decode.h>
|
8
8
|
#include <brotli/encode.h>
|
9
9
|
#include <stdbool.h>
|
10
|
-
#include <stdint.h>
|
11
10
|
#include <stdlib.h>
|
12
11
|
|
13
12
|
#include "brs_ext/common.h"
|
@@ -22,8 +21,8 @@ enum {
|
|
22
21
|
BRS_EXT_OPTION_TYPE_MODE
|
23
22
|
};
|
24
23
|
|
25
|
-
typedef
|
26
|
-
typedef uint32_t
|
24
|
+
typedef brs_ext_byte_fast_t brs_ext_option_type_t;
|
25
|
+
typedef uint32_t brs_ext_option_value_t;
|
27
26
|
|
28
27
|
typedef struct {
|
29
28
|
bool has_value;
|
@@ -5,11 +5,8 @@
|
|
5
5
|
|
6
6
|
#include <brotli/encode.h>
|
7
7
|
#include <brotli/types.h>
|
8
|
-
#include <stdint.h>
|
9
|
-
#include <stdlib.h>
|
10
8
|
|
11
9
|
#include "brs_ext/buffer.h"
|
12
|
-
#include "brs_ext/common.h"
|
13
10
|
#include "brs_ext/error.h"
|
14
11
|
#include "brs_ext/option.h"
|
15
12
|
#include "ruby.h"
|
@@ -21,7 +18,7 @@ static void free_compressor(brs_ext_compressor_t* compressor_ptr)
|
|
21
18
|
BrotliEncoderDestroyInstance(state_ptr);
|
22
19
|
}
|
23
20
|
|
24
|
-
|
21
|
+
brs_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
25
22
|
if (destination_buffer != NULL) {
|
26
23
|
free(destination_buffer);
|
27
24
|
}
|
@@ -70,7 +67,7 @@ VALUE brs_ext_initialize_compressor(VALUE self, VALUE options)
|
|
70
67
|
destination_buffer_length = BRS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_COMPRESSOR;
|
71
68
|
}
|
72
69
|
|
73
|
-
|
70
|
+
brs_ext_byte_t* destination_buffer = malloc(destination_buffer_length);
|
74
71
|
if (destination_buffer == NULL) {
|
75
72
|
BrotliEncoderDestroyInstance(state_ptr);
|
76
73
|
brs_ext_raise_error(BRS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -90,13 +87,13 @@ VALUE brs_ext_initialize_compressor(VALUE self, VALUE options)
|
|
90
87
|
brs_ext_raise_error(BRS_EXT_ERROR_USED_AFTER_CLOSE); \
|
91
88
|
}
|
92
89
|
|
93
|
-
#define GET_SOURCE_DATA(source_value)
|
94
|
-
Check_Type(source_value, T_STRING);
|
95
|
-
|
96
|
-
const char*
|
97
|
-
size_t
|
98
|
-
const
|
99
|
-
size_t
|
90
|
+
#define GET_SOURCE_DATA(source_value) \
|
91
|
+
Check_Type(source_value, T_STRING); \
|
92
|
+
\
|
93
|
+
const char* source = RSTRING_PTR(source_value); \
|
94
|
+
size_t source_length = RSTRING_LEN(source_value); \
|
95
|
+
const brs_ext_byte_t* remaining_source = (const brs_ext_byte_t*)source; \
|
96
|
+
size_t remaining_source_length = source_length;
|
100
97
|
|
101
98
|
VALUE brs_ext_compress(VALUE self, VALUE source_value)
|
102
99
|
{
|
@@ -130,8 +127,8 @@ VALUE brs_ext_flush_compressor(VALUE self)
|
|
130
127
|
|
131
128
|
BrotliEncoderState* state_ptr = compressor_ptr->state_ptr;
|
132
129
|
|
133
|
-
const
|
134
|
-
size_t
|
130
|
+
const brs_ext_byte_t* remaining_source = NULL;
|
131
|
+
size_t remaining_source_length = 0;
|
135
132
|
|
136
133
|
BROTLI_BOOL result = BrotliEncoderCompressStream(
|
137
134
|
state_ptr,
|
@@ -156,8 +153,8 @@ VALUE brs_ext_finish_compressor(VALUE self)
|
|
156
153
|
|
157
154
|
BrotliEncoderState* state_ptr = compressor_ptr->state_ptr;
|
158
155
|
|
159
|
-
const
|
160
|
-
size_t
|
156
|
+
const brs_ext_byte_t* remaining_source = NULL;
|
157
|
+
size_t remaining_source_length = 0;
|
161
158
|
|
162
159
|
BROTLI_BOOL result = BrotliEncoderCompressStream(
|
163
160
|
state_ptr,
|
@@ -180,9 +177,9 @@ VALUE brs_ext_compressor_read_result(VALUE self)
|
|
180
177
|
GET_COMPRESSOR(self);
|
181
178
|
DO_NOT_USE_AFTER_CLOSE(compressor_ptr);
|
182
179
|
|
183
|
-
|
184
|
-
size_t
|
185
|
-
size_t
|
180
|
+
brs_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
181
|
+
size_t destination_buffer_length = compressor_ptr->destination_buffer_length;
|
182
|
+
size_t remaining_destination_buffer_length = compressor_ptr->remaining_destination_buffer_length;
|
186
183
|
|
187
184
|
const char* result = (const char*)destination_buffer;
|
188
185
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
@@ -207,7 +204,7 @@ VALUE brs_ext_compressor_close(VALUE self)
|
|
207
204
|
compressor_ptr->state_ptr = NULL;
|
208
205
|
}
|
209
206
|
|
210
|
-
|
207
|
+
brs_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
211
208
|
if (destination_buffer != NULL) {
|
212
209
|
free(destination_buffer);
|
213
210
|
|
@@ -5,16 +5,16 @@
|
|
5
5
|
#define BRS_EXT_STREAM_COMPRESSOR_H
|
6
6
|
|
7
7
|
#include <brotli/encode.h>
|
8
|
-
#include <stdint.h>
|
9
8
|
#include <stdlib.h>
|
10
9
|
|
10
|
+
#include "brs_ext/common.h"
|
11
11
|
#include "ruby.h"
|
12
12
|
|
13
13
|
typedef struct {
|
14
14
|
BrotliEncoderState* state_ptr;
|
15
|
-
|
15
|
+
brs_ext_byte_t* destination_buffer;
|
16
16
|
size_t destination_buffer_length;
|
17
|
-
|
17
|
+
brs_ext_byte_t* remaining_destination_buffer;
|
18
18
|
size_t remaining_destination_buffer_length;
|
19
19
|
} brs_ext_compressor_t;
|
20
20
|
|
@@ -4,11 +4,8 @@
|
|
4
4
|
#include "brs_ext/stream/decompressor.h"
|
5
5
|
|
6
6
|
#include <brotli/decode.h>
|
7
|
-
#include <stdint.h>
|
8
|
-
#include <stdlib.h>
|
9
7
|
|
10
8
|
#include "brs_ext/buffer.h"
|
11
|
-
#include "brs_ext/common.h"
|
12
9
|
#include "brs_ext/error.h"
|
13
10
|
#include "brs_ext/option.h"
|
14
11
|
#include "ruby.h"
|
@@ -20,7 +17,7 @@ static void free_decompressor(brs_ext_decompressor_t* decompressor_ptr)
|
|
20
17
|
BrotliDecoderDestroyInstance(state_ptr);
|
21
18
|
}
|
22
19
|
|
23
|
-
|
20
|
+
brs_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
|
24
21
|
if (destination_buffer != NULL) {
|
25
22
|
free(destination_buffer);
|
26
23
|
}
|
@@ -69,7 +66,7 @@ VALUE brs_ext_initialize_decompressor(VALUE self, VALUE options)
|
|
69
66
|
destination_buffer_length = BRS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_DECOMPRESSOR;
|
70
67
|
}
|
71
68
|
|
72
|
-
|
69
|
+
brs_ext_byte_t* destination_buffer = malloc(destination_buffer_length);
|
73
70
|
if (destination_buffer == NULL) {
|
74
71
|
BrotliDecoderDestroyInstance(state_ptr);
|
75
72
|
brs_ext_raise_error(BRS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -89,13 +86,13 @@ VALUE brs_ext_initialize_decompressor(VALUE self, VALUE options)
|
|
89
86
|
brs_ext_raise_error(BRS_EXT_ERROR_USED_AFTER_CLOSE); \
|
90
87
|
}
|
91
88
|
|
92
|
-
#define GET_SOURCE_DATA(source_value)
|
93
|
-
Check_Type(source_value, T_STRING);
|
94
|
-
|
95
|
-
const char*
|
96
|
-
size_t
|
97
|
-
const
|
98
|
-
size_t
|
89
|
+
#define GET_SOURCE_DATA(source_value) \
|
90
|
+
Check_Type(source_value, T_STRING); \
|
91
|
+
\
|
92
|
+
const char* source = RSTRING_PTR(source_value); \
|
93
|
+
size_t source_length = RSTRING_LEN(source_value); \
|
94
|
+
const brs_ext_byte_t* remaining_source = (const brs_ext_byte_t*)source; \
|
95
|
+
size_t remaining_source_length = source_length;
|
99
96
|
|
100
97
|
VALUE brs_ext_decompress(VALUE self, VALUE source_value)
|
101
98
|
{
|
@@ -128,9 +125,9 @@ VALUE brs_ext_decompressor_read_result(VALUE self)
|
|
128
125
|
GET_DECOMPRESSOR(self);
|
129
126
|
DO_NOT_USE_AFTER_CLOSE(decompressor_ptr);
|
130
127
|
|
131
|
-
|
132
|
-
size_t
|
133
|
-
size_t
|
128
|
+
brs_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
|
129
|
+
size_t destination_buffer_length = decompressor_ptr->destination_buffer_length;
|
130
|
+
size_t remaining_destination_buffer_length = decompressor_ptr->remaining_destination_buffer_length;
|
134
131
|
|
135
132
|
const char* result = (const char*)destination_buffer;
|
136
133
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
@@ -155,7 +152,7 @@ VALUE brs_ext_decompressor_close(VALUE self)
|
|
155
152
|
decompressor_ptr->state_ptr = NULL;
|
156
153
|
}
|
157
154
|
|
158
|
-
|
155
|
+
brs_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
|
159
156
|
if (destination_buffer != NULL) {
|
160
157
|
free(destination_buffer);
|
161
158
|
|
@@ -5,16 +5,16 @@
|
|
5
5
|
#define BRS_EXT_STREAM_DECOMPRESSOR_H
|
6
6
|
|
7
7
|
#include <brotli/decode.h>
|
8
|
-
#include <stdint.h>
|
9
8
|
#include <stdlib.h>
|
10
9
|
|
10
|
+
#include "brs_ext/common.h"
|
11
11
|
#include "ruby.h"
|
12
12
|
|
13
13
|
typedef struct {
|
14
14
|
BrotliDecoderState* state_ptr;
|
15
|
-
|
15
|
+
brs_ext_byte_t* destination_buffer;
|
16
16
|
size_t destination_buffer_length;
|
17
|
-
|
17
|
+
brs_ext_byte_t* remaining_destination_buffer;
|
18
18
|
size_t remaining_destination_buffer_length;
|
19
19
|
} brs_ext_decompressor_t;
|
20
20
|
|
data/ext/brs_ext/string.c
CHANGED
@@ -6,11 +6,9 @@
|
|
6
6
|
#include <brotli/decode.h>
|
7
7
|
#include <brotli/encode.h>
|
8
8
|
#include <brotli/types.h>
|
9
|
-
#include <stdint.h>
|
10
9
|
#include <stdlib.h>
|
11
10
|
|
12
11
|
#include "brs_ext/buffer.h"
|
13
|
-
#include "brs_ext/common.h"
|
14
12
|
#include "brs_ext/error.h"
|
15
13
|
#include "brs_ext/macro.h"
|
16
14
|
#include "brs_ext/option.h"
|
@@ -57,15 +55,15 @@ static inline brs_ext_result_t compress(
|
|
57
55
|
BROTLI_BOOL result;
|
58
56
|
brs_ext_result_t ext_result;
|
59
57
|
|
60
|
-
const
|
61
|
-
size_t
|
58
|
+
const brs_ext_byte_t* remaining_source = (const brs_ext_byte_t*)source;
|
59
|
+
size_t remaining_source_length = source_length;
|
62
60
|
|
63
61
|
size_t destination_length = 0;
|
64
62
|
size_t remaining_destination_buffer_length = destination_buffer_length;
|
65
63
|
|
66
64
|
while (true) {
|
67
|
-
|
68
|
-
size_t
|
65
|
+
brs_ext_byte_t* remaining_destination_buffer = (brs_ext_byte_t*)RSTRING_PTR(destination_value) + destination_length;
|
66
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
|
69
67
|
|
70
68
|
result = BrotliEncoderCompressStream(
|
71
69
|
state_ptr,
|
@@ -159,15 +157,15 @@ static inline brs_ext_result_t decompress(
|
|
159
157
|
BrotliDecoderResult result;
|
160
158
|
brs_ext_result_t ext_result;
|
161
159
|
|
162
|
-
const
|
163
|
-
size_t
|
160
|
+
const brs_ext_byte_t* remaining_source = (const brs_ext_byte_t*)source;
|
161
|
+
size_t remaining_source_length = source_length;
|
164
162
|
|
165
163
|
size_t destination_length = 0;
|
166
164
|
size_t remaining_destination_buffer_length = destination_buffer_length;
|
167
165
|
|
168
166
|
while (true) {
|
169
|
-
|
170
|
-
size_t
|
167
|
+
brs_ext_byte_t* remaining_destination_buffer = (brs_ext_byte_t*)RSTRING_PTR(destination_value) + destination_length;
|
168
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
|
171
169
|
|
172
170
|
result = BrotliDecoderDecompressStream(
|
173
171
|
state_ptr,
|
data/lib/brs/stream/abstract.rb
CHANGED
@@ -23,17 +23,18 @@ module BRS
|
|
23
23
|
attr_reader :internal_encoding
|
24
24
|
attr_reader :transcode_options
|
25
25
|
attr_reader :pos
|
26
|
+
|
26
27
|
alias tell pos
|
27
28
|
|
28
|
-
def initialize(io,
|
29
|
+
def initialize(io, options = {})
|
29
30
|
@raw_stream = create_raw_stream
|
30
31
|
|
31
32
|
Validation.validate_io io
|
32
33
|
@io = io
|
33
34
|
|
34
|
-
@stat = Stat.new @io.stat
|
35
|
+
@stat = Stat.new @io.stat if @io.respond_to? :stat
|
35
36
|
|
36
|
-
set_encoding external_encoding, internal_encoding, transcode_options
|
37
|
+
set_encoding options[:external_encoding], options[:internal_encoding], options[:transcode_options]
|
37
38
|
reset_buffer
|
38
39
|
reset_io_advise
|
39
40
|
|
@@ -50,8 +51,8 @@ module BRS
|
|
50
51
|
|
51
52
|
protected def reset_io_advise
|
52
53
|
# Both compressor and decompressor need sequential io access.
|
53
|
-
@io.advise :sequential
|
54
|
-
rescue ::Errno::ESPIPE
|
54
|
+
@io.advise :sequential if @io.respond_to? :advise
|
55
|
+
rescue ::Errno::ESPIPE
|
55
56
|
# ok
|
56
57
|
end
|
57
58
|
|
@@ -126,7 +127,8 @@ module BRS
|
|
126
127
|
def rewind
|
127
128
|
@raw_stream = create_raw_stream
|
128
129
|
|
129
|
-
@io.rewind
|
130
|
+
@io.rewind if @io.respond_to? :rewind
|
131
|
+
|
130
132
|
reset_buffer
|
131
133
|
reset_io_advise
|
132
134
|
|
data/lib/brs/stream/reader.rb
CHANGED
@@ -147,7 +147,7 @@ module BRS
|
|
147
147
|
end
|
148
148
|
|
149
149
|
protected def transcode_to_internal(data)
|
150
|
-
data = data.encode @internal_encoding,
|
150
|
+
data = data.encode @internal_encoding, **@transcode_options unless @internal_encoding.nil?
|
151
151
|
data
|
152
152
|
end
|
153
153
|
|
data/lib/brs/stream/writer.rb
CHANGED
@@ -137,7 +137,7 @@ module BRS
|
|
137
137
|
# -- common --
|
138
138
|
|
139
139
|
protected def transcode(data)
|
140
|
-
data = data.encode @external_encoding,
|
140
|
+
data = data.encode @external_encoding, **@transcode_options unless @external_encoding.nil?
|
141
141
|
data
|
142
142
|
end
|
143
143
|
end
|
@@ -13,13 +13,13 @@ module BRS
|
|
13
13
|
write object
|
14
14
|
end
|
15
15
|
|
16
|
-
def print(*objects)
|
16
|
+
def print(*objects, field_separator: $OUTPUT_FIELD_SEPARATOR, record_separator: $OUTPUT_RECORD_SEPARATOR)
|
17
17
|
objects.each do |object|
|
18
18
|
write object
|
19
|
-
write
|
19
|
+
write field_separator unless field_separator.nil?
|
20
20
|
end
|
21
21
|
|
22
|
-
write
|
22
|
+
write record_separator unless record_separator.nil?
|
23
23
|
|
24
24
|
nil
|
25
25
|
end
|
data/lib/brs/validation.rb
CHANGED
@@ -5,6 +5,19 @@ require_relative "error"
|
|
5
5
|
|
6
6
|
module BRS
|
7
7
|
module Validation
|
8
|
+
IO_METHODS = %i[
|
9
|
+
read
|
10
|
+
write
|
11
|
+
readpartial
|
12
|
+
read_nonblock
|
13
|
+
write_nonblock
|
14
|
+
eof?
|
15
|
+
flush
|
16
|
+
close
|
17
|
+
closed?
|
18
|
+
]
|
19
|
+
.freeze
|
20
|
+
|
8
21
|
def self.validate_bool(value)
|
9
22
|
raise ValidateError, "invalid bool" unless value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
|
10
23
|
end
|
@@ -26,7 +39,7 @@ module BRS
|
|
26
39
|
end
|
27
40
|
|
28
41
|
def self.validate_io(value)
|
29
|
-
raise ValidateError, "invalid io" unless value.
|
42
|
+
raise ValidateError, "invalid io" unless IO_METHODS.all? { |method| value.respond_to? method }
|
30
43
|
end
|
31
44
|
|
32
45
|
def self.validate_hash(value)
|
data/lib/brs/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-brs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Aladjev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-06-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
214
214
|
- !ruby/object:Gem::Version
|
215
215
|
version: '0'
|
216
216
|
requirements: []
|
217
|
-
rubygems_version: 3.
|
217
|
+
rubygems_version: 3.1.2
|
218
218
|
signing_key:
|
219
219
|
specification_version: 4
|
220
220
|
summary: Ruby bindings for brotli library.
|