ruby-brs 1.0.1 → 1.1.4
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 +73 -71
- data/ext/brs_ext/buffer.c +17 -5
- data/ext/brs_ext/buffer.h +12 -0
- data/ext/brs_ext/common.h +3 -0
- data/ext/brs_ext/error.c +2 -3
- data/ext/brs_ext/error.h +1 -1
- data/ext/brs_ext/io.c +93 -77
- data/ext/brs_ext/main.c +0 -1
- data/ext/brs_ext/option.c +23 -19
- data/ext/brs_ext/option.h +9 -10
- data/ext/brs_ext/stream/compressor.c +22 -23
- data/ext/brs_ext/stream/compressor.h +3 -3
- data/ext/brs_ext/stream/decompressor.c +18 -20
- data/ext/brs_ext/stream/decompressor.h +3 -3
- data/ext/brs_ext/string.c +30 -45
- data/ext/extconf.rb +45 -30
- data/lib/brs.rb +1 -0
- data/lib/brs/file.rb +6 -0
- data/lib/brs/option.rb +14 -7
- data/lib/brs/stream/abstract.rb +9 -12
- data/lib/brs/stream/raw/abstract.rb +6 -2
- data/lib/brs/stream/raw/compressor.rb +10 -8
- data/lib/brs/stream/raw/decompressor.rb +1 -5
- data/lib/brs/stream/reader.rb +71 -52
- data/lib/brs/stream/reader_helpers.rb +2 -0
- data/lib/brs/stream/writer.rb +10 -5
- data/lib/brs/stream/writer_helpers.rb +8 -10
- data/lib/brs/string.rb +2 -0
- data/lib/brs/validation.rb +15 -2
- data/lib/brs/version.rb +1 -1
- metadata +63 -21
data/ext/brs_ext/main.c
CHANGED
data/ext/brs_ext/option.c
CHANGED
@@ -5,13 +5,12 @@
|
|
5
5
|
|
6
6
|
#include <brotli/decode.h>
|
7
7
|
#include <brotli/encode.h>
|
8
|
-
#include <stdbool.h>
|
9
|
-
#include <stdint.h>
|
10
8
|
|
11
|
-
#include "brs_ext/common.h"
|
12
9
|
#include "brs_ext/error.h"
|
13
10
|
#include "ruby.h"
|
14
11
|
|
12
|
+
// -- values --
|
13
|
+
|
15
14
|
static inline VALUE get_raw_option_value(VALUE options, const char* name)
|
16
15
|
{
|
17
16
|
return rb_funcall(options, rb_intern("[]"), 1, ID2SYM(rb_intern(name)));
|
@@ -27,14 +26,14 @@ static inline bool get_bool_option_value(VALUE raw_value)
|
|
27
26
|
return raw_type == T_TRUE;
|
28
27
|
}
|
29
28
|
|
30
|
-
static inline unsigned
|
29
|
+
static inline unsigned int get_uint_option_value(VALUE raw_value)
|
31
30
|
{
|
32
31
|
Check_Type(raw_value, T_FIXNUM);
|
33
32
|
|
34
33
|
return NUM2UINT(raw_value);
|
35
34
|
}
|
36
35
|
|
37
|
-
static inline
|
36
|
+
static inline BrotliEncoderMode get_mode_option_value(VALUE raw_value)
|
38
37
|
{
|
39
38
|
Check_Type(raw_value, T_SYMBOL);
|
40
39
|
|
@@ -68,8 +67,8 @@ void brs_ext_get_option(VALUE options, brs_ext_option_t* option, brs_ext_option_
|
|
68
67
|
case BRS_EXT_OPTION_TYPE_BOOL:
|
69
68
|
value = get_bool_option_value(raw_value) ? 1 : 0;
|
70
69
|
break;
|
71
|
-
case
|
72
|
-
value = (brs_ext_option_value_t)
|
70
|
+
case BRS_EXT_OPTION_TYPE_UINT:
|
71
|
+
value = (brs_ext_option_value_t)get_uint_option_value(raw_value);
|
73
72
|
break;
|
74
73
|
case BRS_EXT_OPTION_TYPE_MODE:
|
75
74
|
value = (brs_ext_option_value_t)get_mode_option_value(raw_value);
|
@@ -81,13 +80,17 @@ void brs_ext_get_option(VALUE options, brs_ext_option_t* option, brs_ext_option_
|
|
81
80
|
option->value = value;
|
82
81
|
}
|
83
82
|
|
84
|
-
|
83
|
+
size_t brs_ext_get_size_option_value(VALUE options, const char* name)
|
85
84
|
{
|
86
85
|
VALUE raw_value = get_raw_option_value(options, name);
|
87
86
|
|
88
|
-
|
87
|
+
Check_Type(raw_value, T_FIXNUM);
|
88
|
+
|
89
|
+
return NUM2SIZET(raw_value);
|
89
90
|
}
|
90
91
|
|
92
|
+
// -- set params --
|
93
|
+
|
91
94
|
#define SET_OPTION_VALUE(function, state_ptr, param, option) \
|
92
95
|
if (option.has_value && !function(state_ptr, param, option.value)) { \
|
93
96
|
return BRS_EXT_ERROR_VALIDATE_FAILED; \
|
@@ -120,24 +123,25 @@ brs_ext_result_t brs_ext_set_decompressor_options(BrotliDecoderState* state_ptr,
|
|
120
123
|
return 0;
|
121
124
|
}
|
122
125
|
|
126
|
+
// -- exports --
|
127
|
+
|
128
|
+
#define EXPORT_PARAM_BOUNDS(module, param, name) \
|
129
|
+
rb_define_const(module, "MIN_" name, UINT2NUM(BROTLI_MIN_##param)); \
|
130
|
+
rb_define_const(module, "MAX_" name, UINT2NUM(BROTLI_MAX_##param));
|
131
|
+
|
123
132
|
void brs_ext_option_exports(VALUE root_module)
|
124
133
|
{
|
125
|
-
VALUE
|
134
|
+
VALUE module = rb_define_module_under(root_module, "Option");
|
126
135
|
|
127
136
|
VALUE modes = rb_ary_new_from_args(
|
128
137
|
3,
|
129
138
|
ID2SYM(rb_intern("text")),
|
130
139
|
ID2SYM(rb_intern("font")),
|
131
140
|
ID2SYM(rb_intern("generic")));
|
132
|
-
rb_define_const(
|
141
|
+
rb_define_const(module, "MODES", modes);
|
133
142
|
RB_GC_GUARD(modes);
|
134
143
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
rb_define_const(option, "MIN_LGWIN", UINT2NUM(BROTLI_MIN_WINDOW_BITS));
|
139
|
-
rb_define_const(option, "MAX_LGWIN", UINT2NUM(BROTLI_MAX_WINDOW_BITS));
|
140
|
-
|
141
|
-
rb_define_const(option, "MIN_LGBLOCK", UINT2NUM(BROTLI_MIN_INPUT_BLOCK_BITS));
|
142
|
-
rb_define_const(option, "MAX_LGBLOCK", UINT2NUM(BROTLI_MAX_INPUT_BLOCK_BITS));
|
144
|
+
EXPORT_PARAM_BOUNDS(module, QUALITY, "QUALITY");
|
145
|
+
EXPORT_PARAM_BOUNDS(module, WINDOW_BITS, "LGWIN");
|
146
|
+
EXPORT_PARAM_BOUNDS(module, INPUT_BLOCK_BITS, "LGBLOCK");
|
143
147
|
}
|
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"
|
@@ -18,12 +17,12 @@
|
|
18
17
|
|
19
18
|
enum {
|
20
19
|
BRS_EXT_OPTION_TYPE_BOOL = 1,
|
21
|
-
|
20
|
+
BRS_EXT_OPTION_TYPE_UINT,
|
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;
|
@@ -54,11 +53,11 @@ void brs_ext_get_option(VALUE options, brs_ext_option_t* option, brs_ext_option_
|
|
54
53
|
brs_ext_compressor_options_t compressor_options; \
|
55
54
|
\
|
56
55
|
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_MODE, mode); \
|
57
|
-
BRS_EXT_GET_OPTION(options, compressor_options,
|
58
|
-
BRS_EXT_GET_OPTION(options, compressor_options,
|
59
|
-
BRS_EXT_GET_OPTION(options, compressor_options,
|
56
|
+
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_UINT, quality); \
|
57
|
+
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_UINT, lgwin); \
|
58
|
+
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_UINT, lgblock); \
|
60
59
|
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_BOOL, disable_literal_context_modeling); \
|
61
|
-
BRS_EXT_GET_OPTION(options, compressor_options,
|
60
|
+
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_UINT, size_hint); \
|
62
61
|
BRS_EXT_GET_OPTION(options, compressor_options, BRS_EXT_OPTION_TYPE_BOOL, large_window);
|
63
62
|
|
64
63
|
#define BRS_EXT_GET_DECOMPRESSOR_OPTIONS(options) \
|
@@ -67,10 +66,10 @@ void brs_ext_get_option(VALUE options, brs_ext_option_t* option, brs_ext_option_
|
|
67
66
|
BRS_EXT_GET_OPTION(options, decompressor_options, BRS_EXT_OPTION_TYPE_BOOL, disable_ring_buffer_reallocation); \
|
68
67
|
BRS_EXT_GET_OPTION(options, decompressor_options, BRS_EXT_OPTION_TYPE_BOOL, large_window);
|
69
68
|
|
70
|
-
|
69
|
+
size_t brs_ext_get_size_option_value(VALUE options, const char* name);
|
71
70
|
|
72
71
|
#define BRS_EXT_GET_BUFFER_LENGTH_OPTION(options, name) \
|
73
|
-
size_t name =
|
72
|
+
size_t name = brs_ext_get_size_option_value(options, #name);
|
74
73
|
|
75
74
|
brs_ext_result_t brs_ext_set_compressor_options(BrotliEncoderState* state_ptr, brs_ext_compressor_options_t* options);
|
76
75
|
brs_ext_result_t brs_ext_set_decompressor_options(BrotliDecoderState* state_ptr, brs_ext_decompressor_options_t* options);
|
@@ -4,11 +4,9 @@
|
|
4
4
|
#include "brs_ext/stream/compressor.h"
|
5
5
|
|
6
6
|
#include <brotli/encode.h>
|
7
|
-
#include <
|
8
|
-
#include <stdlib.h>
|
7
|
+
#include <brotli/types.h>
|
9
8
|
|
10
9
|
#include "brs_ext/buffer.h"
|
11
|
-
#include "brs_ext/common.h"
|
12
10
|
#include "brs_ext/error.h"
|
13
11
|
#include "brs_ext/option.h"
|
14
12
|
#include "ruby.h"
|
@@ -20,7 +18,7 @@ static void free_compressor(brs_ext_compressor_t* compressor_ptr)
|
|
20
18
|
BrotliEncoderDestroyInstance(state_ptr);
|
21
19
|
}
|
22
20
|
|
23
|
-
|
21
|
+
brs_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
24
22
|
if (destination_buffer != NULL) {
|
25
23
|
free(destination_buffer);
|
26
24
|
}
|
@@ -69,7 +67,7 @@ VALUE brs_ext_initialize_compressor(VALUE self, VALUE options)
|
|
69
67
|
destination_buffer_length = BRS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_COMPRESSOR;
|
70
68
|
}
|
71
69
|
|
72
|
-
|
70
|
+
brs_ext_byte_t* destination_buffer = malloc(destination_buffer_length);
|
73
71
|
if (destination_buffer == NULL) {
|
74
72
|
BrotliEncoderDestroyInstance(state_ptr);
|
75
73
|
brs_ext_raise_error(BRS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -89,13 +87,13 @@ VALUE brs_ext_initialize_compressor(VALUE self, VALUE options)
|
|
89
87
|
brs_ext_raise_error(BRS_EXT_ERROR_USED_AFTER_CLOSE); \
|
90
88
|
}
|
91
89
|
|
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
|
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;
|
99
97
|
|
100
98
|
VALUE brs_ext_compress(VALUE self, VALUE source_value)
|
101
99
|
{
|
@@ -116,7 +114,7 @@ VALUE brs_ext_compress(VALUE self, VALUE source_value)
|
|
116
114
|
brs_ext_raise_error(BRS_EXT_ERROR_UNEXPECTED);
|
117
115
|
}
|
118
116
|
|
119
|
-
VALUE bytes_written =
|
117
|
+
VALUE bytes_written = SIZET2NUM(source_length - remaining_source_length);
|
120
118
|
VALUE needs_more_destination = BrotliEncoderHasMoreOutput(state_ptr) ? Qtrue : Qfalse;
|
121
119
|
|
122
120
|
return rb_ary_new_from_args(2, bytes_written, needs_more_destination);
|
@@ -129,8 +127,8 @@ VALUE brs_ext_flush_compressor(VALUE self)
|
|
129
127
|
|
130
128
|
BrotliEncoderState* state_ptr = compressor_ptr->state_ptr;
|
131
129
|
|
132
|
-
const
|
133
|
-
size_t
|
130
|
+
const brs_ext_byte_t* remaining_source = NULL;
|
131
|
+
size_t remaining_source_length = 0;
|
134
132
|
|
135
133
|
BROTLI_BOOL result = BrotliEncoderCompressStream(
|
136
134
|
state_ptr,
|
@@ -155,8 +153,8 @@ VALUE brs_ext_finish_compressor(VALUE self)
|
|
155
153
|
|
156
154
|
BrotliEncoderState* state_ptr = compressor_ptr->state_ptr;
|
157
155
|
|
158
|
-
const
|
159
|
-
size_t
|
156
|
+
const brs_ext_byte_t* remaining_source = NULL;
|
157
|
+
size_t remaining_source_length = 0;
|
160
158
|
|
161
159
|
BROTLI_BOOL result = BrotliEncoderCompressStream(
|
162
160
|
state_ptr,
|
@@ -179,9 +177,9 @@ VALUE brs_ext_compressor_read_result(VALUE self)
|
|
179
177
|
GET_COMPRESSOR(self);
|
180
178
|
DO_NOT_USE_AFTER_CLOSE(compressor_ptr);
|
181
179
|
|
182
|
-
|
183
|
-
size_t
|
184
|
-
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;
|
185
183
|
|
186
184
|
const char* result = (const char*)destination_buffer;
|
187
185
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
@@ -206,7 +204,7 @@ VALUE brs_ext_compressor_close(VALUE self)
|
|
206
204
|
compressor_ptr->state_ptr = NULL;
|
207
205
|
}
|
208
206
|
|
209
|
-
|
207
|
+
brs_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
|
210
208
|
if (destination_buffer != NULL) {
|
211
209
|
free(destination_buffer);
|
212
210
|
|
@@ -221,9 +219,10 @@ VALUE brs_ext_compressor_close(VALUE self)
|
|
221
219
|
|
222
220
|
void brs_ext_compressor_exports(VALUE root_module)
|
223
221
|
{
|
224
|
-
VALUE
|
222
|
+
VALUE module = rb_define_module_under(root_module, "Stream");
|
223
|
+
|
224
|
+
VALUE compressor = rb_define_class_under(module, "NativeCompressor", rb_cObject);
|
225
225
|
|
226
|
-
VALUE compressor = rb_define_class_under(stream, "NativeCompressor", rb_cObject);
|
227
226
|
rb_define_alloc_func(compressor, brs_ext_allocate_compressor);
|
228
227
|
rb_define_method(compressor, "initialize", brs_ext_initialize_compressor, 1);
|
229
228
|
rb_define_method(compressor, "write", brs_ext_compress, 1);
|
@@ -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
|
{
|
@@ -117,10 +114,10 @@ VALUE brs_ext_decompress(VALUE self, VALUE source_value)
|
|
117
114
|
brs_ext_raise_error(brs_ext_get_decompressor_error(error_code));
|
118
115
|
}
|
119
116
|
|
120
|
-
VALUE
|
117
|
+
VALUE bytes_read = SIZET2NUM(source_length - remaining_source_length);
|
121
118
|
VALUE needs_more_destination = result == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT ? Qtrue : Qfalse;
|
122
119
|
|
123
|
-
return rb_ary_new_from_args(2,
|
120
|
+
return rb_ary_new_from_args(2, bytes_read, needs_more_destination);
|
124
121
|
}
|
125
122
|
|
126
123
|
VALUE brs_ext_decompressor_read_result(VALUE self)
|
@@ -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
|
|
@@ -170,9 +167,10 @@ VALUE brs_ext_decompressor_close(VALUE self)
|
|
170
167
|
|
171
168
|
void brs_ext_decompressor_exports(VALUE root_module)
|
172
169
|
{
|
173
|
-
VALUE
|
170
|
+
VALUE module = rb_define_module_under(root_module, "Stream");
|
171
|
+
|
172
|
+
VALUE decompressor = rb_define_class_under(module, "NativeDecompressor", rb_cObject);
|
174
173
|
|
175
|
-
VALUE decompressor = rb_define_class_under(stream, "NativeDecompressor", rb_cObject);
|
176
174
|
rb_define_alloc_func(decompressor, brs_ext_allocate_decompressor);
|
177
175
|
rb_define_method(decompressor, "initialize", brs_ext_initialize_decompressor, 1);
|
178
176
|
rb_define_method(decompressor, "read", brs_ext_decompress, 1);
|
@@ -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
@@ -5,11 +5,10 @@
|
|
5
5
|
|
6
6
|
#include <brotli/decode.h>
|
7
7
|
#include <brotli/encode.h>
|
8
|
-
#include <
|
8
|
+
#include <brotli/types.h>
|
9
9
|
#include <stdlib.h>
|
10
10
|
|
11
11
|
#include "brs_ext/buffer.h"
|
12
|
-
#include "brs_ext/common.h"
|
13
12
|
#include "brs_ext/error.h"
|
14
13
|
#include "brs_ext/macro.h"
|
15
14
|
#include "brs_ext/option.h"
|
@@ -17,26 +16,6 @@
|
|
17
16
|
|
18
17
|
// -- buffer --
|
19
18
|
|
20
|
-
static inline VALUE create_buffer(VALUE length)
|
21
|
-
{
|
22
|
-
return rb_str_new(NULL, NUM2UINT(length));
|
23
|
-
}
|
24
|
-
|
25
|
-
#define CREATE_BUFFER(buffer, length, exception) \
|
26
|
-
VALUE buffer = rb_protect(create_buffer, UINT2NUM(length), &exception);
|
27
|
-
|
28
|
-
static inline VALUE resize_buffer(VALUE args)
|
29
|
-
{
|
30
|
-
VALUE buffer = rb_ary_entry(args, 0);
|
31
|
-
VALUE length = rb_ary_entry(args, 1);
|
32
|
-
return rb_str_resize(buffer, NUM2UINT(length));
|
33
|
-
}
|
34
|
-
|
35
|
-
#define RESIZE_BUFFER(buffer, length, exception) \
|
36
|
-
VALUE resize_buffer_args = rb_ary_new_from_args(2, buffer, UINT2NUM(length)); \
|
37
|
-
buffer = rb_protect(resize_buffer, resize_buffer_args, &exception); \
|
38
|
-
RB_GC_GUARD(resize_buffer_args);
|
39
|
-
|
40
19
|
static inline brs_ext_result_t increase_destination_buffer(
|
41
20
|
VALUE destination_value, size_t destination_length,
|
42
21
|
size_t* remaining_destination_buffer_length_ptr, size_t destination_buffer_length)
|
@@ -48,7 +27,7 @@ static inline brs_ext_result_t increase_destination_buffer(
|
|
48
27
|
|
49
28
|
int exception;
|
50
29
|
|
51
|
-
|
30
|
+
BRS_EXT_RESIZE_STRING_BUFFER(destination_value, destination_length + destination_buffer_length, exception);
|
52
31
|
if (exception != 0) {
|
53
32
|
return BRS_EXT_ERROR_ALLOCATE_FAILED;
|
54
33
|
}
|
@@ -60,31 +39,33 @@ static inline brs_ext_result_t increase_destination_buffer(
|
|
60
39
|
|
61
40
|
// -- utils --
|
62
41
|
|
63
|
-
#define GET_SOURCE_DATA(source_value)
|
64
|
-
Check_Type(source_value, T_STRING);
|
65
|
-
|
66
|
-
const char*
|
67
|
-
size_t
|
68
|
-
const uint8_t* remaining_source = (const uint8_t*)source; \
|
69
|
-
size_t remaining_source_length = source_length;
|
42
|
+
#define GET_SOURCE_DATA(source_value) \
|
43
|
+
Check_Type(source_value, T_STRING); \
|
44
|
+
\
|
45
|
+
const char* source = RSTRING_PTR(source_value); \
|
46
|
+
size_t source_length = RSTRING_LEN(source_value);
|
70
47
|
|
71
48
|
// -- compress --
|
72
49
|
|
73
50
|
static inline brs_ext_result_t compress(
|
74
51
|
BrotliEncoderState* state_ptr,
|
75
|
-
const
|
52
|
+
const char* source, size_t source_length,
|
76
53
|
VALUE destination_value, size_t destination_buffer_length)
|
77
54
|
{
|
55
|
+
BROTLI_BOOL result;
|
78
56
|
brs_ext_result_t ext_result;
|
79
57
|
|
58
|
+
const brs_ext_byte_t* remaining_source = (const brs_ext_byte_t*)source;
|
59
|
+
size_t remaining_source_length = source_length;
|
60
|
+
|
80
61
|
size_t destination_length = 0;
|
81
62
|
size_t remaining_destination_buffer_length = destination_buffer_length;
|
82
63
|
|
83
64
|
while (true) {
|
84
|
-
|
85
|
-
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;
|
86
67
|
|
87
|
-
|
68
|
+
result = BrotliEncoderCompressStream(
|
88
69
|
state_ptr,
|
89
70
|
BROTLI_OPERATION_FINISH,
|
90
71
|
&remaining_source_length, &remaining_source,
|
@@ -114,7 +95,7 @@ static inline brs_ext_result_t compress(
|
|
114
95
|
|
115
96
|
int exception;
|
116
97
|
|
117
|
-
|
98
|
+
BRS_EXT_RESIZE_STRING_BUFFER(destination_value, destination_length, exception);
|
118
99
|
if (exception != 0) {
|
119
100
|
return BRS_EXT_ERROR_ALLOCATE_FAILED;
|
120
101
|
}
|
@@ -146,7 +127,7 @@ VALUE brs_ext_compress_string(VALUE BRS_EXT_UNUSED(self), VALUE source_value, VA
|
|
146
127
|
|
147
128
|
int exception;
|
148
129
|
|
149
|
-
|
130
|
+
BRS_EXT_CREATE_STRING_BUFFER(destination_value, destination_buffer_length, exception);
|
150
131
|
if (exception != 0) {
|
151
132
|
BrotliEncoderDestroyInstance(state_ptr);
|
152
133
|
brs_ext_raise_error(BRS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -154,7 +135,7 @@ VALUE brs_ext_compress_string(VALUE BRS_EXT_UNUSED(self), VALUE source_value, VA
|
|
154
135
|
|
155
136
|
ext_result = compress(
|
156
137
|
state_ptr,
|
157
|
-
|
138
|
+
source, source_length,
|
158
139
|
destination_value, destination_buffer_length);
|
159
140
|
|
160
141
|
BrotliEncoderDestroyInstance(state_ptr);
|
@@ -170,19 +151,23 @@ VALUE brs_ext_compress_string(VALUE BRS_EXT_UNUSED(self), VALUE source_value, VA
|
|
170
151
|
|
171
152
|
static inline brs_ext_result_t decompress(
|
172
153
|
BrotliDecoderState* state_ptr,
|
173
|
-
const
|
154
|
+
const char* source, size_t source_length,
|
174
155
|
VALUE destination_value, size_t destination_buffer_length)
|
175
156
|
{
|
176
|
-
|
157
|
+
BrotliDecoderResult result;
|
158
|
+
brs_ext_result_t ext_result;
|
159
|
+
|
160
|
+
const brs_ext_byte_t* remaining_source = (const brs_ext_byte_t*)source;
|
161
|
+
size_t remaining_source_length = source_length;
|
177
162
|
|
178
163
|
size_t destination_length = 0;
|
179
164
|
size_t remaining_destination_buffer_length = destination_buffer_length;
|
180
165
|
|
181
166
|
while (true) {
|
182
|
-
|
183
|
-
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;
|
184
169
|
|
185
|
-
|
170
|
+
result = BrotliDecoderDecompressStream(
|
186
171
|
state_ptr,
|
187
172
|
&remaining_source_length, &remaining_source,
|
188
173
|
&remaining_destination_buffer_length, &remaining_destination_buffer,
|
@@ -215,7 +200,7 @@ static inline brs_ext_result_t decompress(
|
|
215
200
|
|
216
201
|
int exception;
|
217
202
|
|
218
|
-
|
203
|
+
BRS_EXT_RESIZE_STRING_BUFFER(destination_value, destination_length, exception);
|
219
204
|
if (exception != 0) {
|
220
205
|
brs_ext_raise_error(BRS_EXT_ERROR_ALLOCATE_FAILED);
|
221
206
|
}
|
@@ -247,7 +232,7 @@ VALUE brs_ext_decompress_string(VALUE BRS_EXT_UNUSED(self), VALUE source_value,
|
|
247
232
|
|
248
233
|
int exception;
|
249
234
|
|
250
|
-
|
235
|
+
BRS_EXT_CREATE_STRING_BUFFER(destination_value, destination_buffer_length, exception);
|
251
236
|
if (exception != 0) {
|
252
237
|
BrotliDecoderDestroyInstance(state_ptr);
|
253
238
|
brs_ext_raise_error(BRS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -255,7 +240,7 @@ VALUE brs_ext_decompress_string(VALUE BRS_EXT_UNUSED(self), VALUE source_value,
|
|
255
240
|
|
256
241
|
ext_result = decompress(
|
257
242
|
state_ptr,
|
258
|
-
|
243
|
+
source, source_length,
|
259
244
|
destination_value, destination_buffer_length);
|
260
245
|
|
261
246
|
BrotliDecoderDestroyInstance(state_ptr);
|