ruby-brs 1.1.0 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +63 -74
- 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/ext/extconf.rb +13 -0
- data/lib/brs/file.rb +4 -0
- data/lib/brs/stream/abstract.rb +9 -12
- data/lib/brs/stream/raw/abstract.rb +6 -2
- data/lib/brs/stream/raw/compressor.rb +5 -7
- 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/validation.rb +15 -2
- data/lib/brs/version.rb +1 -1
- metadata +41 -13
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/ext/extconf.rb
CHANGED
@@ -61,7 +61,20 @@ $srcs = %w[
|
|
61
61
|
.map { |name| "src/#{extension_name}/#{name}.c" }
|
62
62
|
.freeze
|
63
63
|
|
64
|
+
# Removing library duplicates.
|
65
|
+
$libs = $libs.split(%r{\s})
|
66
|
+
.reject(&:empty?)
|
67
|
+
.sort
|
68
|
+
.uniq
|
69
|
+
.join " "
|
70
|
+
|
71
|
+
if ENV["CI"] || ENV["COVERAGE"]
|
72
|
+
$CFLAGS << " --coverage"
|
73
|
+
$LDFLAGS << " --coverage"
|
74
|
+
end
|
75
|
+
|
64
76
|
$CFLAGS << " -Wno-declaration-after-statement"
|
77
|
+
|
65
78
|
$VPATH << "$(srcdir)/#{extension_name}:$(srcdir)/#{extension_name}/stream"
|
66
79
|
# rubocop:enable Style/GlobalVars
|
67
80
|
|
data/lib/brs/file.rb
CHANGED
@@ -22,6 +22,8 @@ module BRS
|
|
22
22
|
open_files(source, destination) do |source_io, destination_io|
|
23
23
|
BRS._native_compress_io source_io, destination_io, options
|
24
24
|
end
|
25
|
+
|
26
|
+
nil
|
25
27
|
end
|
26
28
|
|
27
29
|
def self.decompress(source, destination, options = {})
|
@@ -33,6 +35,8 @@ module BRS
|
|
33
35
|
open_files(source, destination) do |source_io, destination_io|
|
34
36
|
BRS._native_decompress_io source_io, destination_io, options
|
35
37
|
end
|
38
|
+
|
39
|
+
nil
|
36
40
|
end
|
37
41
|
|
38
42
|
private_class_method def self.open_files(source, destination, &_block)
|
data/lib/brs/stream/abstract.rb
CHANGED
@@ -17,23 +17,19 @@ module BRS
|
|
17
17
|
|
18
18
|
include Delegates
|
19
19
|
|
20
|
-
attr_reader :io
|
21
|
-
|
22
|
-
attr_reader :external_encoding
|
23
|
-
attr_reader :internal_encoding
|
24
|
-
attr_reader :transcode_options
|
25
|
-
attr_reader :pos
|
20
|
+
attr_reader :io, :stat, :external_encoding, :internal_encoding, :transcode_options, :pos
|
21
|
+
|
26
22
|
alias tell pos
|
27
23
|
|
28
|
-
def initialize(io,
|
24
|
+
def initialize(io, options = {})
|
29
25
|
@raw_stream = create_raw_stream
|
30
26
|
|
31
27
|
Validation.validate_io io
|
32
28
|
@io = io
|
33
29
|
|
34
|
-
@stat = Stat.new @io.stat
|
30
|
+
@stat = Stat.new @io.stat if @io.respond_to? :stat
|
35
31
|
|
36
|
-
set_encoding external_encoding, internal_encoding, transcode_options
|
32
|
+
set_encoding options[:external_encoding], options[:internal_encoding], options[:transcode_options]
|
37
33
|
reset_buffer
|
38
34
|
reset_io_advise
|
39
35
|
|
@@ -50,8 +46,8 @@ module BRS
|
|
50
46
|
|
51
47
|
protected def reset_io_advise
|
52
48
|
# Both compressor and decompressor need sequential io access.
|
53
|
-
@io.advise :sequential
|
54
|
-
rescue ::Errno::ESPIPE
|
49
|
+
@io.advise :sequential if @io.respond_to? :advise
|
50
|
+
rescue ::Errno::ESPIPE
|
55
51
|
# ok
|
56
52
|
end
|
57
53
|
|
@@ -126,7 +122,8 @@ module BRS
|
|
126
122
|
def rewind
|
127
123
|
@raw_stream = create_raw_stream
|
128
124
|
|
129
|
-
@io.rewind
|
125
|
+
@io.rewind if @io.respond_to? :rewind
|
126
|
+
|
130
127
|
reset_buffer
|
131
128
|
reset_io_advise
|
132
129
|
|
@@ -19,11 +19,13 @@ module BRS
|
|
19
19
|
|
20
20
|
def flush(&writer)
|
21
21
|
write_result(&writer)
|
22
|
+
|
23
|
+
nil
|
22
24
|
end
|
23
25
|
|
24
|
-
protected def
|
26
|
+
protected def more_destination(&writer)
|
25
27
|
result_bytesize = write_result(&writer)
|
26
|
-
raise NotEnoughDestinationError, "not enough destination" if result_bytesize
|
28
|
+
raise NotEnoughDestinationError, "not enough destination" if result_bytesize.zero?
|
27
29
|
end
|
28
30
|
|
29
31
|
protected def write_result(&_writer)
|
@@ -44,6 +46,8 @@ module BRS
|
|
44
46
|
|
45
47
|
@native_stream.close
|
46
48
|
@is_closed = true
|
49
|
+
|
50
|
+
nil
|
47
51
|
end
|
48
52
|
|
49
53
|
def closed?
|
@@ -39,13 +39,15 @@ module BRS
|
|
39
39
|
|
40
40
|
if need_more_destination
|
41
41
|
source = source.byteslice bytes_written, source.bytesize - bytes_written
|
42
|
-
|
42
|
+
more_destination(&writer)
|
43
43
|
next
|
44
44
|
end
|
45
45
|
|
46
46
|
unless bytes_written == source.bytesize
|
47
|
+
# :nocov:
|
47
48
|
# Compressor write should eat all provided "source" without remainder.
|
48
49
|
raise UnexpectedError, "unexpected error"
|
50
|
+
# :nocov:
|
49
51
|
end
|
50
52
|
|
51
53
|
break
|
@@ -63,7 +65,7 @@ module BRS
|
|
63
65
|
need_more_destination = @native_stream.flush
|
64
66
|
|
65
67
|
if need_more_destination
|
66
|
-
|
68
|
+
more_destination(&writer)
|
67
69
|
next
|
68
70
|
end
|
69
71
|
|
@@ -71,8 +73,6 @@ module BRS
|
|
71
73
|
end
|
72
74
|
|
73
75
|
super
|
74
|
-
|
75
|
-
nil
|
76
76
|
end
|
77
77
|
|
78
78
|
def close(&writer)
|
@@ -84,7 +84,7 @@ module BRS
|
|
84
84
|
need_more_destination = @native_stream.finish
|
85
85
|
|
86
86
|
if need_more_destination
|
87
|
-
|
87
|
+
more_destination(&writer)
|
88
88
|
next
|
89
89
|
end
|
90
90
|
|
@@ -92,8 +92,6 @@ module BRS
|
|
92
92
|
end
|
93
93
|
|
94
94
|
super
|
95
|
-
|
96
|
-
nil
|
97
95
|
end
|
98
96
|
end
|
99
97
|
end
|