ruby-lzws 1.1.7 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/ext/extconf.rb +2 -2
- data/ext/lzws_ext/buffer.c +18 -4
- data/ext/lzws_ext/error.h +2 -1
- data/ext/lzws_ext/io.c +4 -9
- data/ext/lzws_ext/main.c +2 -0
- data/ext/lzws_ext/option.c +22 -4
- data/ext/lzws_ext/option.h +28 -24
- data/ext/lzws_ext/stream/compressor.c +17 -25
- data/ext/lzws_ext/stream/compressor.h +2 -1
- data/ext/lzws_ext/stream/decompressor.c +12 -17
- data/ext/lzws_ext/stream/decompressor.h +2 -1
- data/ext/lzws_ext/string.c +60 -74
- data/lib/lzws/option.rb +32 -33
- data/lib/lzws/version.rb +1 -1
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62d885a3a75f291ea4b808da4ef4e47a68574e74dae8fe39aad61d228a62643c
|
4
|
+
data.tar.gz: 90e5fc29ea554392dcf93bb391a38361707c14306099b6654765e399d111ea9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66c07c08d7094be0e9f2d802c01796529e0c14bcde067752aa629d7e3337218e0d6a08e7cfe08725659097fb0e3be90e7565a872a3c79dc96813afa6b235e4ab
|
7
|
+
data.tar.gz: a1d74a3408881c23f816818931685ec084bd2a0e9c114be212f8a059e37af8da882e9235f3780524e5e67557b3c9945f33b3ecab8535768e4737e972fc724d87
|
data/README.md
CHANGED
data/ext/extconf.rb
CHANGED
@@ -13,10 +13,10 @@ end
|
|
13
13
|
|
14
14
|
require_header "lzws/buffer.h"
|
15
15
|
require_header "lzws/common.h", %w[lzws_result_t]
|
16
|
-
require_header "lzws/compressor/common.h"
|
16
|
+
require_header "lzws/compressor/common.h", %w[lzws_compressor_options_t]
|
17
17
|
require_header "lzws/compressor/main.h"
|
18
18
|
require_header "lzws/compressor/state.h", %w[lzws_compressor_state_t]
|
19
|
-
require_header "lzws/decompressor/common.h"
|
19
|
+
require_header "lzws/decompressor/common.h", %w[lzws_decompressor_options_t]
|
20
20
|
require_header "lzws/decompressor/main.h"
|
21
21
|
require_header "lzws/decompressor/state.h", %w[lzws_decompressor_state_t]
|
22
22
|
require_header "lzws/file.h"
|
data/ext/lzws_ext/buffer.c
CHANGED
@@ -16,6 +16,7 @@ VALUE lzws_ext_resize_string_buffer(VALUE args)
|
|
16
16
|
{
|
17
17
|
VALUE buffer = rb_ary_entry(args, 0);
|
18
18
|
VALUE length = rb_ary_entry(args, 1);
|
19
|
+
|
19
20
|
return rb_str_resize(buffer, NUM2SIZET(length));
|
20
21
|
}
|
21
22
|
|
@@ -23,8 +24,21 @@ void lzws_ext_buffer_exports(VALUE root_module)
|
|
23
24
|
{
|
24
25
|
VALUE module = rb_define_module_under(root_module, "Buffer");
|
25
26
|
|
26
|
-
rb_define_const(
|
27
|
-
|
28
|
-
|
29
|
-
rb_define_const(
|
27
|
+
rb_define_const(
|
28
|
+
module, "DEFAULT_SOURCE_BUFFER_LENGTH_FOR_COMPRESSOR", SIZET2NUM(LZWS_DEFAULT_SOURCE_BUFFER_LENGTH_FOR_COMPRESSOR));
|
29
|
+
|
30
|
+
rb_define_const(
|
31
|
+
module,
|
32
|
+
"DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_COMPRESSOR",
|
33
|
+
SIZET2NUM(LZWS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_COMPRESSOR));
|
34
|
+
|
35
|
+
rb_define_const(
|
36
|
+
module,
|
37
|
+
"DEFAULT_SOURCE_BUFFER_LENGTH_FOR_DECOMPRESSOR",
|
38
|
+
SIZET2NUM(LZWS_DEFAULT_SOURCE_BUFFER_LENGTH_FOR_DECOMPRESSOR));
|
39
|
+
|
40
|
+
rb_define_const(
|
41
|
+
module,
|
42
|
+
"DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_DECOMPRESSOR",
|
43
|
+
SIZET2NUM(LZWS_DEFAULT_DESTINATION_BUFFER_LENGTH_FOR_DECOMPRESSOR));
|
30
44
|
}
|
data/ext/lzws_ext/error.h
CHANGED
data/ext/lzws_ext/io.c
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
// Ruby bindings for lzws library.
|
2
2
|
// Copyright (c) 2019 AUTHORS, MIT License.
|
3
3
|
|
4
|
-
#include "
|
4
|
+
#include "lzws_ext/io.h"
|
5
5
|
|
6
|
-
#include <lzws/common.h>
|
7
6
|
#include <lzws/file.h>
|
8
7
|
|
9
8
|
#include "lzws_ext/error.h"
|
10
|
-
#include "lzws_ext/io.h"
|
11
9
|
#include "lzws_ext/macro.h"
|
12
10
|
#include "lzws_ext/option.h"
|
13
11
|
#include "ruby.h"
|
12
|
+
#include "ruby/io.h"
|
14
13
|
|
15
14
|
#define GET_FILE(target) \
|
16
15
|
Check_Type(target, T_FILE); \
|
@@ -58,9 +57,7 @@ VALUE lzws_ext_compress_io(VALUE LZWS_EXT_UNUSED(self), VALUE source, VALUE dest
|
|
58
57
|
LZWS_EXT_GET_BUFFER_LENGTH_OPTION(options, destination_buffer_length);
|
59
58
|
|
60
59
|
lzws_result_t result = lzws_compress_file(
|
61
|
-
source_file, source_buffer_length,
|
62
|
-
destination_file, destination_buffer_length,
|
63
|
-
without_magic_header, max_code_bit_length, block_mode, msb, unaligned_bit_groups, quiet);
|
60
|
+
source_file, source_buffer_length, destination_file, destination_buffer_length, &compressor_options);
|
64
61
|
|
65
62
|
if (result != 0) {
|
66
63
|
lzws_ext_raise_error(get_file_error(result));
|
@@ -82,9 +79,7 @@ VALUE lzws_ext_decompress_io(VALUE LZWS_EXT_UNUSED(self), VALUE source, VALUE de
|
|
82
79
|
LZWS_EXT_GET_BUFFER_LENGTH_OPTION(options, destination_buffer_length);
|
83
80
|
|
84
81
|
lzws_result_t result = lzws_decompress_file(
|
85
|
-
source_file, source_buffer_length,
|
86
|
-
destination_file, destination_buffer_length,
|
87
|
-
without_magic_header, msb, unaligned_bit_groups, quiet);
|
82
|
+
source_file, source_buffer_length, destination_file, destination_buffer_length, &decompressor_options);
|
88
83
|
|
89
84
|
if (result != 0) {
|
90
85
|
lzws_ext_raise_error(get_file_error(result));
|
data/ext/lzws_ext/main.c
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
#include "lzws_ext/buffer.h"
|
5
5
|
#include "lzws_ext/io.h"
|
6
|
+
#include "lzws_ext/option.h"
|
6
7
|
#include "lzws_ext/stream/compressor.h"
|
7
8
|
#include "lzws_ext/stream/decompressor.h"
|
8
9
|
#include "lzws_ext/string.h"
|
@@ -14,6 +15,7 @@ void Init_lzws_ext()
|
|
14
15
|
|
15
16
|
lzws_ext_buffer_exports(root_module);
|
16
17
|
lzws_ext_io_exports(root_module);
|
18
|
+
lzws_ext_option_exports(root_module);
|
17
19
|
lzws_ext_compressor_exports(root_module);
|
18
20
|
lzws_ext_decompressor_exports(root_module);
|
19
21
|
lzws_ext_string_exports(root_module);
|
data/ext/lzws_ext/option.c
CHANGED
@@ -6,30 +6,38 @@
|
|
6
6
|
#include "lzws_ext/error.h"
|
7
7
|
#include "ruby.h"
|
8
8
|
|
9
|
+
// -- values --
|
10
|
+
|
9
11
|
static inline VALUE get_raw_option_value(VALUE options, const char* name)
|
10
12
|
{
|
11
13
|
return rb_funcall(options, rb_intern("[]"), 1, ID2SYM(rb_intern(name)));
|
12
14
|
}
|
13
15
|
|
14
|
-
|
16
|
+
void lzws_ext_get_bool_option(VALUE options, bool* option, const char* name)
|
15
17
|
{
|
16
18
|
VALUE raw_value = get_raw_option_value(options, name);
|
19
|
+
if (raw_value == Qnil) {
|
20
|
+
return;
|
21
|
+
}
|
17
22
|
|
18
23
|
int raw_type = TYPE(raw_value);
|
19
24
|
if (raw_type != T_TRUE && raw_type != T_FALSE) {
|
20
25
|
lzws_ext_raise_error(LZWS_EXT_ERROR_VALIDATE_FAILED);
|
21
26
|
}
|
22
27
|
|
23
|
-
|
28
|
+
*option = raw_type == T_TRUE;
|
24
29
|
}
|
25
30
|
|
26
|
-
|
31
|
+
void lzws_ext_get_max_code_bit_length_option(VALUE options, lzws_byte_fast_t* option, const char* name)
|
27
32
|
{
|
28
33
|
VALUE raw_value = get_raw_option_value(options, name);
|
34
|
+
if (raw_value == Qnil) {
|
35
|
+
return;
|
36
|
+
}
|
29
37
|
|
30
38
|
Check_Type(raw_value, T_FIXNUM);
|
31
39
|
|
32
|
-
|
40
|
+
*option = NUM2UINT(raw_value);
|
33
41
|
}
|
34
42
|
|
35
43
|
size_t lzws_ext_get_size_option_value(VALUE options, const char* name)
|
@@ -40,3 +48,13 @@ size_t lzws_ext_get_size_option_value(VALUE options, const char* name)
|
|
40
48
|
|
41
49
|
return NUM2SIZET(raw_value);
|
42
50
|
}
|
51
|
+
|
52
|
+
// -- exports --
|
53
|
+
|
54
|
+
void lzws_ext_option_exports(VALUE root_module)
|
55
|
+
{
|
56
|
+
VALUE module = rb_define_module_under(root_module, "Option");
|
57
|
+
|
58
|
+
rb_define_const(module, "LOWEST_MAX_CODE_BIT_LENGTH", UINT2NUM(LZWS_LOWEST_MAX_CODE_BIT_LENGTH));
|
59
|
+
rb_define_const(module, "BIGGEST_MAX_CODE_BIT_LENGTH", UINT2NUM(LZWS_BIGGEST_MAX_CODE_BIT_LENGTH));
|
60
|
+
}
|
data/ext/lzws_ext/option.h
CHANGED
@@ -4,40 +4,44 @@
|
|
4
4
|
#if !defined(LZWS_EXT_OPTIONS_H)
|
5
5
|
#define LZWS_EXT_OPTIONS_H
|
6
6
|
|
7
|
+
#include <lzws/compressor/common.h>
|
8
|
+
#include <lzws/decompressor/common.h>
|
7
9
|
#include <stdbool.h>
|
8
10
|
#include <stdlib.h>
|
9
11
|
|
10
|
-
#include "lzws_ext/common.h"
|
11
12
|
#include "ruby.h"
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
size_t lzws_ext_get_size_option_value(VALUE options, const char* name);
|
14
|
+
void lzws_ext_get_bool_option(VALUE options, bool* option, const char* name);
|
15
|
+
void lzws_ext_get_max_code_bit_length_option(VALUE options, lzws_byte_fast_t* option, const char* name);
|
16
16
|
|
17
|
-
#define LZWS_EXT_GET_BOOL_OPTION(options, name) \
|
18
|
-
|
17
|
+
#define LZWS_EXT_GET_BOOL_OPTION(options, target_options, name) \
|
18
|
+
lzws_ext_get_bool_option(options, &target_options.name, #name);
|
19
19
|
|
20
|
-
#define
|
21
|
-
|
20
|
+
#define LZWS_EXT_GET_MAX_CODE_BIT_LENGTH_OPTION(options, target_options, name) \
|
21
|
+
lzws_ext_get_max_code_bit_length_option(options, &target_options.name, #name);
|
22
22
|
|
23
|
-
#define
|
24
|
-
|
23
|
+
#define LZWS_EXT_GET_COMPRESSOR_OPTIONS(options) \
|
24
|
+
lzws_compressor_options_t compressor_options = LZWS_COMPRESSOR_DEFAULT_OPTIONS; \
|
25
|
+
\
|
26
|
+
LZWS_EXT_GET_BOOL_OPTION(options, compressor_options, without_magic_header); \
|
27
|
+
LZWS_EXT_GET_MAX_CODE_BIT_LENGTH_OPTION(options, compressor_options, max_code_bit_length); \
|
28
|
+
LZWS_EXT_GET_BOOL_OPTION(options, compressor_options, block_mode); \
|
29
|
+
LZWS_EXT_GET_BOOL_OPTION(options, compressor_options, msb); \
|
30
|
+
LZWS_EXT_GET_BOOL_OPTION(options, compressor_options, unaligned_bit_groups); \
|
31
|
+
LZWS_EXT_GET_BOOL_OPTION(options, compressor_options, quiet);
|
25
32
|
|
26
|
-
#define
|
27
|
-
|
28
|
-
|
29
|
-
LZWS_EXT_GET_BOOL_OPTION(options,
|
30
|
-
LZWS_EXT_GET_BOOL_OPTION(options, msb);
|
31
|
-
LZWS_EXT_GET_BOOL_OPTION(options, unaligned_bit_groups);
|
32
|
-
LZWS_EXT_GET_BOOL_OPTION(options, quiet);
|
33
|
+
#define LZWS_EXT_GET_DECOMPRESSOR_OPTIONS(options) \
|
34
|
+
lzws_decompressor_options_t decompressor_options = LZWS_DECOMPRESSOR_DEFAULT_OPTIONS; \
|
35
|
+
\
|
36
|
+
LZWS_EXT_GET_BOOL_OPTION(options, decompressor_options, without_magic_header); \
|
37
|
+
LZWS_EXT_GET_BOOL_OPTION(options, decompressor_options, msb); \
|
38
|
+
LZWS_EXT_GET_BOOL_OPTION(options, decompressor_options, unaligned_bit_groups); \
|
39
|
+
LZWS_EXT_GET_BOOL_OPTION(options, decompressor_options, quiet);
|
33
40
|
|
34
|
-
|
35
|
-
LZWS_EXT_GET_BOOL_OPTION(options, without_magic_header); \
|
36
|
-
LZWS_EXT_GET_BOOL_OPTION(options, msb); \
|
37
|
-
LZWS_EXT_GET_BOOL_OPTION(options, unaligned_bit_groups); \
|
38
|
-
LZWS_EXT_GET_BOOL_OPTION(options, quiet);
|
41
|
+
size_t lzws_ext_get_size_option_value(VALUE options, const char* name);
|
39
42
|
|
40
|
-
#define LZWS_EXT_GET_BUFFER_LENGTH_OPTION(options, name)
|
41
|
-
|
43
|
+
#define LZWS_EXT_GET_BUFFER_LENGTH_OPTION(options, name) size_t name = lzws_ext_get_size_option_value(options, #name);
|
44
|
+
|
45
|
+
void lzws_ext_option_exports(VALUE root_module);
|
42
46
|
|
43
47
|
#endif // LZWS_EXT_OPTIONS_H
|
@@ -4,8 +4,6 @@
|
|
4
4
|
#include "lzws_ext/stream/compressor.h"
|
5
5
|
|
6
6
|
#include <lzws/buffer.h>
|
7
|
-
#include <lzws/common.h>
|
8
|
-
#include <lzws/compressor/common.h>
|
9
7
|
#include <lzws/compressor/main.h>
|
10
8
|
#include <lzws/compressor/state.h>
|
11
9
|
|
@@ -31,8 +29,7 @@ static void free_compressor(lzws_ext_compressor_t* compressor_ptr)
|
|
31
29
|
VALUE lzws_ext_allocate_compressor(VALUE klass)
|
32
30
|
{
|
33
31
|
lzws_ext_compressor_t* compressor_ptr;
|
34
|
-
|
35
|
-
VALUE self = Data_Make_Struct(klass, lzws_ext_compressor_t, NULL, free_compressor, compressor_ptr);
|
32
|
+
VALUE self = Data_Make_Struct(klass, lzws_ext_compressor_t, NULL, free_compressor, compressor_ptr);
|
36
33
|
|
37
34
|
compressor_ptr->state_ptr = NULL;
|
38
35
|
compressor_ptr->destination_buffer = NULL;
|
@@ -56,10 +53,7 @@ VALUE lzws_ext_initialize_compressor(VALUE self, VALUE options)
|
|
56
53
|
|
57
54
|
lzws_compressor_state_t* state_ptr;
|
58
55
|
|
59
|
-
lzws_result_t result = lzws_compressor_get_initial_state(
|
60
|
-
&state_ptr,
|
61
|
-
without_magic_header, max_code_bit_length, block_mode, msb, unaligned_bit_groups, quiet);
|
62
|
-
|
56
|
+
lzws_result_t result = lzws_compressor_get_initial_state(&state_ptr, &compressor_options);
|
63
57
|
if (result != 0) {
|
64
58
|
switch (result) {
|
65
59
|
case LZWS_COMPRESSOR_ALLOCATE_FAILED:
|
@@ -73,7 +67,9 @@ VALUE lzws_ext_initialize_compressor(VALUE self, VALUE options)
|
|
73
67
|
|
74
68
|
lzws_ext_byte_t* destination_buffer;
|
75
69
|
|
76
|
-
result = lzws_create_destination_buffer_for_compressor(
|
70
|
+
result = lzws_create_destination_buffer_for_compressor(
|
71
|
+
&destination_buffer, &destination_buffer_length, compressor_options.quiet);
|
72
|
+
|
77
73
|
if (result != 0) {
|
78
74
|
lzws_compressor_free_state(state_ptr);
|
79
75
|
lzws_ext_raise_error(LZWS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -98,7 +94,7 @@ VALUE lzws_ext_initialize_compressor(VALUE self, VALUE options)
|
|
98
94
|
\
|
99
95
|
const char* source = RSTRING_PTR(source_value); \
|
100
96
|
size_t source_length = RSTRING_LEN(source_value); \
|
101
|
-
lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*)source;
|
97
|
+
lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*) source; \
|
102
98
|
size_t remaining_source_length = source_length;
|
103
99
|
|
104
100
|
VALUE lzws_ext_compress(VALUE self, VALUE source_value)
|
@@ -109,12 +105,12 @@ VALUE lzws_ext_compress(VALUE self, VALUE source_value)
|
|
109
105
|
|
110
106
|
lzws_result_t result = lzws_compress(
|
111
107
|
compressor_ptr->state_ptr,
|
112
|
-
&remaining_source,
|
113
|
-
&
|
108
|
+
&remaining_source,
|
109
|
+
&remaining_source_length,
|
110
|
+
&compressor_ptr->remaining_destination_buffer,
|
111
|
+
&compressor_ptr->remaining_destination_buffer_length);
|
114
112
|
|
115
|
-
if (
|
116
|
-
result != 0 &&
|
117
|
-
result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) {
|
113
|
+
if (result != 0 && result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) {
|
118
114
|
lzws_ext_raise_error(LZWS_EXT_ERROR_UNEXPECTED);
|
119
115
|
}
|
120
116
|
|
@@ -131,17 +127,14 @@ VALUE lzws_ext_compressor_finish(VALUE self)
|
|
131
127
|
|
132
128
|
lzws_result_t result = lzws_compressor_finish(
|
133
129
|
compressor_ptr->state_ptr,
|
134
|
-
&compressor_ptr->remaining_destination_buffer,
|
130
|
+
&compressor_ptr->remaining_destination_buffer,
|
131
|
+
&compressor_ptr->remaining_destination_buffer_length);
|
135
132
|
|
136
|
-
if (
|
137
|
-
result != 0 &&
|
138
|
-
result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) {
|
133
|
+
if (result != 0 && result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) {
|
139
134
|
lzws_ext_raise_error(LZWS_EXT_ERROR_UNEXPECTED);
|
140
135
|
}
|
141
136
|
|
142
|
-
|
143
|
-
|
144
|
-
return needs_more_destination;
|
137
|
+
return result == LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION ? Qtrue : Qfalse;
|
145
138
|
}
|
146
139
|
|
147
140
|
VALUE lzws_ext_compressor_read_result(VALUE self)
|
@@ -153,10 +146,9 @@ VALUE lzws_ext_compressor_read_result(VALUE self)
|
|
153
146
|
size_t destination_buffer_length = compressor_ptr->destination_buffer_length;
|
154
147
|
size_t remaining_destination_buffer_length = compressor_ptr->remaining_destination_buffer_length;
|
155
148
|
|
156
|
-
const char* result = (const char*)destination_buffer;
|
149
|
+
const char* result = (const char*) destination_buffer;
|
157
150
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
158
|
-
|
159
|
-
VALUE result_value = rb_str_new(result, result_length);
|
151
|
+
VALUE result_value = rb_str_new(result, result_length);
|
160
152
|
|
161
153
|
compressor_ptr->remaining_destination_buffer = destination_buffer;
|
162
154
|
compressor_ptr->remaining_destination_buffer_length = destination_buffer_length;
|
@@ -4,8 +4,6 @@
|
|
4
4
|
#include "lzws_ext/stream/decompressor.h"
|
5
5
|
|
6
6
|
#include <lzws/buffer.h>
|
7
|
-
#include <lzws/common.h>
|
8
|
-
#include <lzws/decompressor/common.h>
|
9
7
|
#include <lzws/decompressor/main.h>
|
10
8
|
#include <lzws/decompressor/state.h>
|
11
9
|
|
@@ -31,7 +29,6 @@ static void free_decompressor(lzws_ext_decompressor_t* decompressor_ptr)
|
|
31
29
|
VALUE lzws_ext_allocate_decompressor(VALUE klass)
|
32
30
|
{
|
33
31
|
lzws_ext_decompressor_t* decompressor_ptr;
|
34
|
-
|
35
32
|
VALUE self = Data_Make_Struct(klass, lzws_ext_decompressor_t, NULL, free_decompressor, decompressor_ptr);
|
36
33
|
|
37
34
|
decompressor_ptr->state_ptr = NULL;
|
@@ -56,10 +53,7 @@ VALUE lzws_ext_initialize_decompressor(VALUE self, VALUE options)
|
|
56
53
|
|
57
54
|
lzws_decompressor_state_t* state_ptr;
|
58
55
|
|
59
|
-
lzws_result_t result = lzws_decompressor_get_initial_state(
|
60
|
-
&state_ptr,
|
61
|
-
without_magic_header, msb, unaligned_bit_groups, quiet);
|
62
|
-
|
56
|
+
lzws_result_t result = lzws_decompressor_get_initial_state(&state_ptr, &decompressor_options);
|
63
57
|
if (result != 0) {
|
64
58
|
switch (result) {
|
65
59
|
case LZWS_DECOMPRESSOR_ALLOCATE_FAILED:
|
@@ -71,7 +65,9 @@ VALUE lzws_ext_initialize_decompressor(VALUE self, VALUE options)
|
|
71
65
|
|
72
66
|
lzws_ext_byte_t* destination_buffer;
|
73
67
|
|
74
|
-
result = lzws_create_destination_buffer_for_decompressor(
|
68
|
+
result = lzws_create_destination_buffer_for_decompressor(
|
69
|
+
&destination_buffer, &destination_buffer_length, decompressor_options.quiet);
|
70
|
+
|
75
71
|
if (result != 0) {
|
76
72
|
lzws_decompressor_free_state(state_ptr);
|
77
73
|
lzws_ext_raise_error(LZWS_EXT_ERROR_ALLOCATE_FAILED);
|
@@ -96,7 +92,7 @@ VALUE lzws_ext_initialize_decompressor(VALUE self, VALUE options)
|
|
96
92
|
\
|
97
93
|
const char* source = RSTRING_PTR(source_value); \
|
98
94
|
size_t source_length = RSTRING_LEN(source_value); \
|
99
|
-
lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*)source;
|
95
|
+
lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*) source; \
|
100
96
|
size_t remaining_source_length = source_length;
|
101
97
|
|
102
98
|
VALUE lzws_ext_decompress(VALUE self, VALUE source_value)
|
@@ -107,12 +103,12 @@ VALUE lzws_ext_decompress(VALUE self, VALUE source_value)
|
|
107
103
|
|
108
104
|
lzws_result_t result = lzws_decompress(
|
109
105
|
decompressor_ptr->state_ptr,
|
110
|
-
&remaining_source,
|
111
|
-
&
|
106
|
+
&remaining_source,
|
107
|
+
&remaining_source_length,
|
108
|
+
&decompressor_ptr->remaining_destination_buffer,
|
109
|
+
&decompressor_ptr->remaining_destination_buffer_length);
|
112
110
|
|
113
|
-
if (
|
114
|
-
result != 0 &&
|
115
|
-
result != LZWS_DECOMPRESSOR_NEEDS_MORE_DESTINATION) {
|
111
|
+
if (result != 0 && result != LZWS_DECOMPRESSOR_NEEDS_MORE_DESTINATION) {
|
116
112
|
switch (result) {
|
117
113
|
case LZWS_DECOMPRESSOR_INVALID_MAGIC_HEADER:
|
118
114
|
case LZWS_DECOMPRESSOR_INVALID_MAX_CODE_BIT_LENGTH:
|
@@ -139,10 +135,9 @@ VALUE lzws_ext_decompressor_read_result(VALUE self)
|
|
139
135
|
size_t destination_buffer_length = decompressor_ptr->destination_buffer_length;
|
140
136
|
size_t remaining_destination_buffer_length = decompressor_ptr->remaining_destination_buffer_length;
|
141
137
|
|
142
|
-
const char* result = (const char*)destination_buffer;
|
138
|
+
const char* result = (const char*) destination_buffer;
|
143
139
|
size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
|
144
|
-
|
145
|
-
VALUE result_value = rb_str_new(result, result_length);
|
140
|
+
VALUE result_value = rb_str_new(result, result_length);
|
146
141
|
|
147
142
|
decompressor_ptr->remaining_destination_buffer = destination_buffer;
|
148
143
|
decompressor_ptr->remaining_destination_buffer_length = destination_buffer_length;
|
data/ext/lzws_ext/string.c
CHANGED
@@ -4,11 +4,8 @@
|
|
4
4
|
#include "lzws_ext/string.h"
|
5
5
|
|
6
6
|
#include <lzws/buffer.h>
|
7
|
-
#include <lzws/common.h>
|
8
|
-
#include <lzws/compressor/common.h>
|
9
7
|
#include <lzws/compressor/main.h>
|
10
8
|
#include <lzws/compressor/state.h>
|
11
|
-
#include <lzws/decompressor/common.h>
|
12
9
|
#include <lzws/decompressor/main.h>
|
13
10
|
#include <lzws/decompressor/state.h>
|
14
11
|
#include <stdlib.h>
|
@@ -22,8 +19,10 @@
|
|
22
19
|
// -- buffer --
|
23
20
|
|
24
21
|
static inline lzws_ext_result_t increase_destination_buffer(
|
25
|
-
VALUE
|
26
|
-
size_t
|
22
|
+
VALUE destination_value,
|
23
|
+
size_t destination_length,
|
24
|
+
size_t* remaining_destination_buffer_length_ptr,
|
25
|
+
size_t destination_buffer_length)
|
27
26
|
{
|
28
27
|
if (*remaining_destination_buffer_length_ptr == destination_buffer_length) {
|
29
28
|
// We want to write more data at once, than buffer has.
|
@@ -52,49 +51,46 @@ static inline lzws_ext_result_t increase_destination_buffer(
|
|
52
51
|
|
53
52
|
// -- compress --
|
54
53
|
|
55
|
-
#define BUFFERED_COMPRESS(function, ...)
|
56
|
-
while (true) {
|
57
|
-
lzws_ext_byte_t* remaining_destination_buffer
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
if (
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
} \
|
81
|
-
\
|
82
|
-
break; \
|
54
|
+
#define BUFFERED_COMPRESS(function, ...) \
|
55
|
+
while (true) { \
|
56
|
+
lzws_ext_byte_t* remaining_destination_buffer = \
|
57
|
+
(lzws_ext_byte_t*) RSTRING_PTR(destination_value) + destination_length; \
|
58
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length; \
|
59
|
+
\
|
60
|
+
result = function(__VA_ARGS__, &remaining_destination_buffer, &remaining_destination_buffer_length); \
|
61
|
+
if (result != 0 && result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) { \
|
62
|
+
return LZWS_EXT_ERROR_UNEXPECTED; \
|
63
|
+
} \
|
64
|
+
\
|
65
|
+
destination_length += prev_remaining_destination_buffer_length - remaining_destination_buffer_length; \
|
66
|
+
\
|
67
|
+
if (result == LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) { \
|
68
|
+
ext_result = increase_destination_buffer( \
|
69
|
+
destination_value, destination_length, &remaining_destination_buffer_length, destination_buffer_length); \
|
70
|
+
\
|
71
|
+
if (ext_result != 0) { \
|
72
|
+
return ext_result; \
|
73
|
+
} \
|
74
|
+
\
|
75
|
+
continue; \
|
76
|
+
} \
|
77
|
+
\
|
78
|
+
break; \
|
83
79
|
}
|
84
80
|
|
85
81
|
static inline lzws_ext_result_t compress(
|
86
82
|
lzws_compressor_state_t* state_ptr,
|
87
|
-
const char*
|
88
|
-
|
83
|
+
const char* source,
|
84
|
+
size_t source_length,
|
85
|
+
VALUE destination_value,
|
86
|
+
size_t destination_buffer_length)
|
89
87
|
{
|
90
88
|
lzws_result_t result;
|
91
89
|
lzws_ext_result_t ext_result;
|
92
|
-
|
93
|
-
|
94
|
-
size_t
|
95
|
-
|
96
|
-
size_t destination_length = 0;
|
97
|
-
size_t remaining_destination_buffer_length = destination_buffer_length;
|
90
|
+
lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*) source;
|
91
|
+
size_t remaining_source_length = source_length;
|
92
|
+
size_t destination_length = 0;
|
93
|
+
size_t remaining_destination_buffer_length = destination_buffer_length;
|
98
94
|
|
99
95
|
BUFFERED_COMPRESS(lzws_compress, state_ptr, &remaining_source, &remaining_source_length);
|
100
96
|
BUFFERED_COMPRESS(lzws_compressor_finish, state_ptr);
|
@@ -118,10 +114,7 @@ VALUE lzws_ext_compress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value,
|
|
118
114
|
|
119
115
|
lzws_compressor_state_t* state_ptr;
|
120
116
|
|
121
|
-
lzws_result_t result = lzws_compressor_get_initial_state(
|
122
|
-
&state_ptr,
|
123
|
-
without_magic_header, max_code_bit_length, block_mode, msb, unaligned_bit_groups, quiet);
|
124
|
-
|
117
|
+
lzws_result_t result = lzws_compressor_get_initial_state(&state_ptr, &compressor_options);
|
125
118
|
if (result != 0) {
|
126
119
|
switch (result) {
|
127
120
|
case LZWS_COMPRESSOR_ALLOCATE_FAILED:
|
@@ -145,10 +138,8 @@ VALUE lzws_ext_compress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value,
|
|
145
138
|
lzws_ext_raise_error(LZWS_EXT_ERROR_ALLOCATE_FAILED);
|
146
139
|
}
|
147
140
|
|
148
|
-
lzws_ext_result_t ext_result =
|
149
|
-
state_ptr,
|
150
|
-
source, source_length,
|
151
|
-
destination_value, destination_buffer_length);
|
141
|
+
lzws_ext_result_t ext_result =
|
142
|
+
compress(state_ptr, source, source_length, destination_value, destination_buffer_length);
|
152
143
|
|
153
144
|
lzws_compressor_free_state(state_ptr);
|
154
145
|
|
@@ -163,30 +154,31 @@ VALUE lzws_ext_compress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value,
|
|
163
154
|
|
164
155
|
static inline lzws_ext_result_t decompress(
|
165
156
|
lzws_decompressor_state_t* state_ptr,
|
166
|
-
const char*
|
167
|
-
|
157
|
+
const char* source,
|
158
|
+
size_t source_length,
|
159
|
+
VALUE destination_value,
|
160
|
+
size_t destination_buffer_length)
|
168
161
|
{
|
169
162
|
lzws_result_t result;
|
170
163
|
lzws_ext_result_t ext_result;
|
171
|
-
|
172
|
-
|
173
|
-
size_t
|
174
|
-
|
175
|
-
size_t destination_length = 0;
|
176
|
-
size_t remaining_destination_buffer_length = destination_buffer_length;
|
164
|
+
lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*) source;
|
165
|
+
size_t remaining_source_length = source_length;
|
166
|
+
size_t destination_length = 0;
|
167
|
+
size_t remaining_destination_buffer_length = destination_buffer_length;
|
177
168
|
|
178
169
|
while (true) {
|
179
|
-
lzws_ext_byte_t* remaining_destination_buffer
|
180
|
-
|
170
|
+
lzws_ext_byte_t* remaining_destination_buffer =
|
171
|
+
(lzws_ext_byte_t*) RSTRING_PTR(destination_value) + destination_length;
|
172
|
+
size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
|
181
173
|
|
182
174
|
result = lzws_decompress(
|
183
175
|
state_ptr,
|
184
|
-
&remaining_source,
|
185
|
-
&
|
176
|
+
&remaining_source,
|
177
|
+
&remaining_source_length,
|
178
|
+
&remaining_destination_buffer,
|
179
|
+
&remaining_destination_buffer_length);
|
186
180
|
|
187
|
-
if (
|
188
|
-
result != 0 &&
|
189
|
-
result != LZWS_DECOMPRESSOR_NEEDS_MORE_DESTINATION) {
|
181
|
+
if (result != 0 && result != LZWS_DECOMPRESSOR_NEEDS_MORE_DESTINATION) {
|
190
182
|
switch (result) {
|
191
183
|
case LZWS_DECOMPRESSOR_INVALID_MAGIC_HEADER:
|
192
184
|
case LZWS_DECOMPRESSOR_INVALID_MAX_CODE_BIT_LENGTH:
|
@@ -202,8 +194,7 @@ static inline lzws_ext_result_t decompress(
|
|
202
194
|
|
203
195
|
if (result == LZWS_DECOMPRESSOR_NEEDS_MORE_DESTINATION) {
|
204
196
|
ext_result = increase_destination_buffer(
|
205
|
-
destination_value, destination_length,
|
206
|
-
&remaining_destination_buffer_length, destination_buffer_length);
|
197
|
+
destination_value, destination_length, &remaining_destination_buffer_length, destination_buffer_length);
|
207
198
|
|
208
199
|
if (ext_result != 0) {
|
209
200
|
return ext_result;
|
@@ -234,10 +225,7 @@ VALUE lzws_ext_decompress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value
|
|
234
225
|
|
235
226
|
lzws_decompressor_state_t* state_ptr;
|
236
227
|
|
237
|
-
lzws_result_t result = lzws_decompressor_get_initial_state(
|
238
|
-
&state_ptr,
|
239
|
-
without_magic_header, msb, unaligned_bit_groups, quiet);
|
240
|
-
|
228
|
+
lzws_result_t result = lzws_decompressor_get_initial_state(&state_ptr, &decompressor_options);
|
241
229
|
if (result != 0) {
|
242
230
|
switch (result) {
|
243
231
|
case LZWS_DECOMPRESSOR_ALLOCATE_FAILED:
|
@@ -259,10 +247,8 @@ VALUE lzws_ext_decompress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value
|
|
259
247
|
lzws_ext_raise_error(LZWS_EXT_ERROR_ALLOCATE_FAILED);
|
260
248
|
}
|
261
249
|
|
262
|
-
lzws_ext_result_t ext_result =
|
263
|
-
state_ptr,
|
264
|
-
source, source_length,
|
265
|
-
destination_value, destination_buffer_length);
|
250
|
+
lzws_ext_result_t ext_result =
|
251
|
+
decompress(state_ptr, source, source_length, destination_value, destination_buffer_length);
|
266
252
|
|
267
253
|
lzws_decompressor_free_state(state_ptr);
|
268
254
|
|
data/lib/lzws/option.rb
CHANGED
@@ -8,45 +8,37 @@ require_relative "validation"
|
|
8
8
|
|
9
9
|
module LZWS
|
10
10
|
module Option
|
11
|
-
|
12
|
-
|
13
|
-
DEFAULT_BUFFER_LENGTH = 0
|
14
|
-
LOWEST_MAX_CODE_BIT_LENGTH = 9
|
15
|
-
BIGGEST_MAX_CODE_BIT_LENGTH = 16
|
16
|
-
|
17
|
-
DECOMPRESSOR_DEFAULTS = {
|
18
|
-
:without_magic_header => false,
|
19
|
-
:msb => false,
|
20
|
-
:unaligned_bit_groups => false,
|
21
|
-
:quiet => false
|
22
|
-
}
|
23
|
-
.freeze
|
24
|
-
|
25
|
-
COMPRESSOR_DEFAULTS = DECOMPRESSOR_DEFAULTS.merge(
|
26
|
-
:max_code_bit_length => BIGGEST_MAX_CODE_BIT_LENGTH,
|
27
|
-
:block_mode => true
|
28
|
-
)
|
29
|
-
.freeze
|
11
|
+
DEFAULT_BUFFER_LENGTH = 0
|
30
12
|
|
31
13
|
def self.get_compressor_options(options, buffer_length_names)
|
32
14
|
Validation.validate_hash options
|
33
15
|
|
34
16
|
buffer_length_defaults = buffer_length_names.each_with_object({}) { |name, defaults| defaults[name] = DEFAULT_BUFFER_LENGTH }
|
35
|
-
options =
|
17
|
+
options = buffer_length_defaults.merge options
|
36
18
|
|
37
19
|
buffer_length_names.each { |name| Validation.validate_not_negative_integer options[name] }
|
38
20
|
|
39
21
|
max_code_bit_length = options[:max_code_bit_length]
|
40
|
-
|
22
|
+
unless max_code_bit_length.nil?
|
23
|
+
Validation.validate_positive_integer max_code_bit_length
|
24
|
+
raise ValidateError, "invalid max code bit length" if
|
25
|
+
max_code_bit_length < LOWEST_MAX_CODE_BIT_LENGTH || max_code_bit_length > BIGGEST_MAX_CODE_BIT_LENGTH
|
26
|
+
end
|
41
27
|
|
42
|
-
|
43
|
-
|
28
|
+
without_magic_header = options[:without_magic_header]
|
29
|
+
Validation.validate_bool without_magic_header unless without_magic_header.nil?
|
44
30
|
|
45
|
-
|
46
|
-
Validation.validate_bool
|
47
|
-
|
48
|
-
|
49
|
-
Validation.validate_bool
|
31
|
+
block_mode = options[:block_mode]
|
32
|
+
Validation.validate_bool block_mode unless block_mode.nil?
|
33
|
+
|
34
|
+
msb = options[:msb]
|
35
|
+
Validation.validate_bool msb unless msb.nil?
|
36
|
+
|
37
|
+
unaligned_bit_groups = options[:unaligned_bit_groups]
|
38
|
+
Validation.validate_bool unaligned_bit_groups unless unaligned_bit_groups.nil?
|
39
|
+
|
40
|
+
quiet = options[:quiet]
|
41
|
+
Validation.validate_bool quiet unless quiet.nil?
|
50
42
|
|
51
43
|
options
|
52
44
|
end
|
@@ -55,14 +47,21 @@ module LZWS
|
|
55
47
|
Validation.validate_hash options
|
56
48
|
|
57
49
|
buffer_length_defaults = buffer_length_names.each_with_object({}) { |name, defaults| defaults[name] = DEFAULT_BUFFER_LENGTH }
|
58
|
-
options =
|
50
|
+
options = buffer_length_defaults.merge options
|
59
51
|
|
60
52
|
buffer_length_names.each { |name| Validation.validate_not_negative_integer options[name] }
|
61
53
|
|
62
|
-
|
63
|
-
Validation.validate_bool
|
64
|
-
|
65
|
-
|
54
|
+
without_magic_header = options[:without_magic_header]
|
55
|
+
Validation.validate_bool without_magic_header unless without_magic_header.nil?
|
56
|
+
|
57
|
+
msb = options[:msb]
|
58
|
+
Validation.validate_bool msb unless msb.nil?
|
59
|
+
|
60
|
+
unaligned_bit_groups = options[:unaligned_bit_groups]
|
61
|
+
Validation.validate_bool unaligned_bit_groups unless unaligned_bit_groups.nil?
|
62
|
+
|
63
|
+
quiet = options[:quiet]
|
64
|
+
Validation.validate_bool quiet unless quiet.nil?
|
66
65
|
|
67
66
|
options
|
68
67
|
end
|
data/lib/lzws/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-lzws
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
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-10-
|
11
|
+
date: 2020-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|
@@ -44,14 +44,14 @@ dependencies:
|
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '5.
|
47
|
+
version: '5.14'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '5.
|
54
|
+
version: '5.14'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: ocg
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,28 +100,28 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '0.
|
103
|
+
version: '0.93'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '0.
|
110
|
+
version: '0.93'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rubocop-performance
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
115
|
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '1.
|
117
|
+
version: '1.8'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '1.
|
124
|
+
version: '1.8'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: simplecov
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|