ruby-lzws 1.1.0 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,11 +4,10 @@
4
4
  #include "lzws_ext/stream/compressor.h"
5
5
 
6
6
  #include <lzws/buffer.h>
7
+ #include <lzws/common.h>
7
8
  #include <lzws/compressor/common.h>
8
9
  #include <lzws/compressor/main.h>
9
10
  #include <lzws/compressor/state.h>
10
- #include <stdint.h>
11
- #include <stdlib.h>
12
11
 
13
12
  #include "lzws_ext/error.h"
14
13
  #include "lzws_ext/option.h"
@@ -21,7 +20,7 @@ static void free_compressor(lzws_ext_compressor_t* compressor_ptr)
21
20
  lzws_compressor_free_state(state_ptr);
22
21
  }
23
22
 
24
- uint8_t* destination_buffer = compressor_ptr->destination_buffer;
23
+ lzws_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
25
24
  if (destination_buffer != NULL) {
26
25
  free(destination_buffer);
27
26
  }
@@ -72,7 +71,7 @@ VALUE lzws_ext_initialize_compressor(VALUE self, VALUE options)
72
71
  }
73
72
  }
74
73
 
75
- uint8_t* destination_buffer;
74
+ lzws_ext_byte_t* destination_buffer;
76
75
 
77
76
  result = lzws_create_destination_buffer_for_compressor(&destination_buffer, &destination_buffer_length, quiet);
78
77
  if (result != 0) {
@@ -94,13 +93,13 @@ VALUE lzws_ext_initialize_compressor(VALUE self, VALUE options)
94
93
  lzws_ext_raise_error(LZWS_EXT_ERROR_USED_AFTER_CLOSE); \
95
94
  }
96
95
 
97
- #define GET_SOURCE_DATA(source_value) \
98
- Check_Type(source_value, T_STRING); \
99
- \
100
- const char* source = RSTRING_PTR(source_value); \
101
- size_t source_length = RSTRING_LEN(source_value); \
102
- uint8_t* remaining_source = (uint8_t*)source; \
103
- size_t remaining_source_length = source_length;
96
+ #define GET_SOURCE_DATA(source_value) \
97
+ Check_Type(source_value, T_STRING); \
98
+ \
99
+ const char* source = RSTRING_PTR(source_value); \
100
+ size_t source_length = RSTRING_LEN(source_value); \
101
+ lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*)source; \
102
+ size_t remaining_source_length = source_length;
104
103
 
105
104
  VALUE lzws_ext_compress(VALUE self, VALUE source_value)
106
105
  {
@@ -119,7 +118,7 @@ VALUE lzws_ext_compress(VALUE self, VALUE source_value)
119
118
  lzws_ext_raise_error(LZWS_EXT_ERROR_UNEXPECTED);
120
119
  }
121
120
 
122
- VALUE bytes_written = UINT2NUM(source_length - remaining_source_length);
121
+ VALUE bytes_written = SIZET2NUM(source_length - remaining_source_length);
123
122
  VALUE needs_more_destination = result == LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION ? Qtrue : Qfalse;
124
123
 
125
124
  return rb_ary_new_from_args(2, bytes_written, needs_more_destination);
@@ -150,9 +149,9 @@ VALUE lzws_ext_compressor_read_result(VALUE self)
150
149
  GET_COMPRESSOR(self);
151
150
  DO_NOT_USE_AFTER_CLOSE(compressor_ptr);
152
151
 
153
- uint8_t* destination_buffer = compressor_ptr->destination_buffer;
154
- size_t destination_buffer_length = compressor_ptr->destination_buffer_length;
155
- size_t remaining_destination_buffer_length = compressor_ptr->remaining_destination_buffer_length;
152
+ lzws_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
153
+ size_t destination_buffer_length = compressor_ptr->destination_buffer_length;
154
+ size_t remaining_destination_buffer_length = compressor_ptr->remaining_destination_buffer_length;
156
155
 
157
156
  const char* result = (const char*)destination_buffer;
158
157
  size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
@@ -177,7 +176,7 @@ VALUE lzws_ext_compressor_close(VALUE self)
177
176
  compressor_ptr->state_ptr = NULL;
178
177
  }
179
178
 
180
- uint8_t* destination_buffer = compressor_ptr->destination_buffer;
179
+ lzws_ext_byte_t* destination_buffer = compressor_ptr->destination_buffer;
181
180
  if (destination_buffer != NULL) {
182
181
  free(destination_buffer);
183
182
 
@@ -192,9 +191,10 @@ VALUE lzws_ext_compressor_close(VALUE self)
192
191
 
193
192
  void lzws_ext_compressor_exports(VALUE root_module)
194
193
  {
195
- VALUE stream = rb_define_module_under(root_module, "Stream");
194
+ VALUE module = rb_define_module_under(root_module, "Stream");
195
+
196
+ VALUE compressor = rb_define_class_under(module, "NativeCompressor", rb_cObject);
196
197
 
197
- VALUE compressor = rb_define_class_under(stream, "NativeCompressor", rb_cObject);
198
198
  rb_define_alloc_func(compressor, lzws_ext_allocate_compressor);
199
199
  rb_define_method(compressor, "initialize", lzws_ext_initialize_compressor, 1);
200
200
  rb_define_method(compressor, "write", lzws_ext_compress, 1);
@@ -5,16 +5,16 @@
5
5
  #define LZWS_EXT_STREAM_COMPRESSOR_H
6
6
 
7
7
  #include <lzws/compressor/state.h>
8
- #include <stdint.h>
9
8
  #include <stdlib.h>
10
9
 
10
+ #include "lzws_ext/common.h"
11
11
  #include "ruby.h"
12
12
 
13
13
  typedef struct {
14
14
  lzws_compressor_state_t* state_ptr;
15
- uint8_t* destination_buffer;
15
+ lzws_ext_byte_t* destination_buffer;
16
16
  size_t destination_buffer_length;
17
- uint8_t* remaining_destination_buffer;
17
+ lzws_ext_byte_t* remaining_destination_buffer;
18
18
  size_t remaining_destination_buffer_length;
19
19
  } lzws_ext_compressor_t;
20
20
 
@@ -4,11 +4,10 @@
4
4
  #include "lzws_ext/stream/decompressor.h"
5
5
 
6
6
  #include <lzws/buffer.h>
7
+ #include <lzws/common.h>
7
8
  #include <lzws/decompressor/common.h>
8
9
  #include <lzws/decompressor/main.h>
9
10
  #include <lzws/decompressor/state.h>
10
- #include <stdint.h>
11
- #include <stdlib.h>
12
11
 
13
12
  #include "lzws_ext/error.h"
14
13
  #include "lzws_ext/option.h"
@@ -21,7 +20,7 @@ static void free_decompressor(lzws_ext_decompressor_t* decompressor_ptr)
21
20
  lzws_decompressor_free_state(state_ptr);
22
21
  }
23
22
 
24
- uint8_t* destination_buffer = decompressor_ptr->destination_buffer;
23
+ lzws_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
25
24
  if (destination_buffer != NULL) {
26
25
  free(destination_buffer);
27
26
  }
@@ -70,7 +69,7 @@ VALUE lzws_ext_initialize_decompressor(VALUE self, VALUE options)
70
69
  }
71
70
  }
72
71
 
73
- uint8_t* destination_buffer;
72
+ lzws_ext_byte_t* destination_buffer;
74
73
 
75
74
  result = lzws_create_destination_buffer_for_decompressor(&destination_buffer, &destination_buffer_length, quiet);
76
75
  if (result != 0) {
@@ -92,13 +91,13 @@ VALUE lzws_ext_initialize_decompressor(VALUE self, VALUE options)
92
91
  lzws_ext_raise_error(LZWS_EXT_ERROR_USED_AFTER_CLOSE); \
93
92
  }
94
93
 
95
- #define GET_SOURCE_DATA(source_value) \
96
- Check_Type(source_value, T_STRING); \
97
- \
98
- const char* source = RSTRING_PTR(source_value); \
99
- size_t source_length = RSTRING_LEN(source_value); \
100
- uint8_t* remaining_source = (uint8_t*)source; \
101
- size_t remaining_source_length = source_length;
94
+ #define GET_SOURCE_DATA(source_value) \
95
+ Check_Type(source_value, T_STRING); \
96
+ \
97
+ const char* source = RSTRING_PTR(source_value); \
98
+ size_t source_length = RSTRING_LEN(source_value); \
99
+ lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*)source; \
100
+ size_t remaining_source_length = source_length;
102
101
 
103
102
  VALUE lzws_ext_decompress(VALUE self, VALUE source_value)
104
103
  {
@@ -125,7 +124,7 @@ VALUE lzws_ext_decompress(VALUE self, VALUE source_value)
125
124
  }
126
125
  }
127
126
 
128
- VALUE bytes_read = UINT2NUM(source_length - remaining_source_length);
127
+ VALUE bytes_read = SIZET2NUM(source_length - remaining_source_length);
129
128
  VALUE needs_more_destination = result == LZWS_DECOMPRESSOR_NEEDS_MORE_DESTINATION ? Qtrue : Qfalse;
130
129
 
131
130
  return rb_ary_new_from_args(2, bytes_read, needs_more_destination);
@@ -136,9 +135,9 @@ VALUE lzws_ext_decompressor_read_result(VALUE self)
136
135
  GET_DECOMPRESSOR(self);
137
136
  DO_NOT_USE_AFTER_CLOSE(decompressor_ptr);
138
137
 
139
- uint8_t* destination_buffer = decompressor_ptr->destination_buffer;
140
- size_t destination_buffer_length = decompressor_ptr->destination_buffer_length;
141
- size_t remaining_destination_buffer_length = decompressor_ptr->remaining_destination_buffer_length;
138
+ lzws_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
139
+ size_t destination_buffer_length = decompressor_ptr->destination_buffer_length;
140
+ size_t remaining_destination_buffer_length = decompressor_ptr->remaining_destination_buffer_length;
142
141
 
143
142
  const char* result = (const char*)destination_buffer;
144
143
  size_t result_length = destination_buffer_length - remaining_destination_buffer_length;
@@ -163,7 +162,7 @@ VALUE lzws_ext_decompressor_close(VALUE self)
163
162
  decompressor_ptr->state_ptr = NULL;
164
163
  }
165
164
 
166
- uint8_t* destination_buffer = decompressor_ptr->destination_buffer;
165
+ lzws_ext_byte_t* destination_buffer = decompressor_ptr->destination_buffer;
167
166
  if (destination_buffer != NULL) {
168
167
  free(destination_buffer);
169
168
 
@@ -178,9 +177,10 @@ VALUE lzws_ext_decompressor_close(VALUE self)
178
177
 
179
178
  void lzws_ext_decompressor_exports(VALUE root_module)
180
179
  {
181
- VALUE stream = rb_define_module_under(root_module, "Stream");
180
+ VALUE module = rb_define_module_under(root_module, "Stream");
181
+
182
+ VALUE decompressor = rb_define_class_under(module, "NativeDecompressor", rb_cObject);
182
183
 
183
- VALUE decompressor = rb_define_class_under(stream, "NativeDecompressor", rb_cObject);
184
184
  rb_define_alloc_func(decompressor, lzws_ext_allocate_decompressor);
185
185
  rb_define_method(decompressor, "initialize", lzws_ext_initialize_decompressor, 1);
186
186
  rb_define_method(decompressor, "read", lzws_ext_decompress, 1);
@@ -5,16 +5,16 @@
5
5
  #define LZWS_EXT_STREAM_DECOMPRESSOR_H
6
6
 
7
7
  #include <lzws/decompressor/state.h>
8
- #include <stdint.h>
9
8
  #include <stdlib.h>
10
9
 
10
+ #include "lzws_ext/common.h"
11
11
  #include "ruby.h"
12
12
 
13
13
  typedef struct {
14
14
  lzws_decompressor_state_t* state_ptr;
15
- uint8_t* destination_buffer;
15
+ lzws_ext_byte_t* destination_buffer;
16
16
  size_t destination_buffer_length;
17
- uint8_t* remaining_destination_buffer;
17
+ lzws_ext_byte_t* remaining_destination_buffer;
18
18
  size_t remaining_destination_buffer_length;
19
19
  } lzws_ext_decompressor_t;
20
20
 
@@ -4,15 +4,16 @@
4
4
  #include "lzws_ext/string.h"
5
5
 
6
6
  #include <lzws/buffer.h>
7
+ #include <lzws/common.h>
7
8
  #include <lzws/compressor/common.h>
8
9
  #include <lzws/compressor/main.h>
9
10
  #include <lzws/compressor/state.h>
10
11
  #include <lzws/decompressor/common.h>
11
12
  #include <lzws/decompressor/main.h>
12
13
  #include <lzws/decompressor/state.h>
13
- #include <stdint.h>
14
14
  #include <stdlib.h>
15
15
 
16
+ #include "lzws_ext/buffer.h"
16
17
  #include "lzws_ext/error.h"
17
18
  #include "lzws_ext/macro.h"
18
19
  #include "lzws_ext/option.h"
@@ -20,26 +21,6 @@
20
21
 
21
22
  // -- buffer --
22
23
 
23
- static inline VALUE create_buffer(VALUE length)
24
- {
25
- return rb_str_new(NULL, NUM2UINT(length));
26
- }
27
-
28
- #define CREATE_BUFFER(buffer, length, exception) \
29
- VALUE buffer = rb_protect(create_buffer, UINT2NUM(length), &exception);
30
-
31
- static inline VALUE resize_buffer(VALUE args)
32
- {
33
- VALUE buffer = rb_ary_entry(args, 0);
34
- VALUE length = rb_ary_entry(args, 1);
35
- return rb_str_resize(buffer, NUM2UINT(length));
36
- }
37
-
38
- #define RESIZE_BUFFER(buffer, length, exception) \
39
- VALUE resize_buffer_args = rb_ary_new_from_args(2, buffer, UINT2NUM(length)); \
40
- buffer = rb_protect(resize_buffer, resize_buffer_args, &exception); \
41
- RB_GC_GUARD(resize_buffer_args);
42
-
43
24
  static inline lzws_ext_result_t increase_destination_buffer(
44
25
  VALUE destination_value, size_t destination_length,
45
26
  size_t* remaining_destination_buffer_length_ptr, size_t destination_buffer_length)
@@ -51,7 +32,7 @@ static inline lzws_ext_result_t increase_destination_buffer(
51
32
 
52
33
  int exception;
53
34
 
54
- RESIZE_BUFFER(destination_value, destination_length + destination_buffer_length, exception);
35
+ LZWS_EXT_RESIZE_STRING_BUFFER(destination_value, destination_length + destination_buffer_length, exception);
55
36
  if (exception != 0) {
56
37
  return LZWS_EXT_ERROR_ALLOCATE_FAILED;
57
38
  }
@@ -63,54 +44,55 @@ static inline lzws_ext_result_t increase_destination_buffer(
63
44
 
64
45
  // -- utils --
65
46
 
66
- #define GET_SOURCE_DATA(source_value) \
67
- Check_Type(source_value, T_STRING); \
68
- \
69
- const char* source = RSTRING_PTR(source_value); \
70
- size_t source_length = RSTRING_LEN(source_value); \
71
- uint8_t* remaining_source = (uint8_t*)source; \
72
- size_t remaining_source_length = source_length;
47
+ #define GET_SOURCE_DATA(source_value) \
48
+ Check_Type(source_value, T_STRING); \
49
+ \
50
+ const char* source = RSTRING_PTR(source_value); \
51
+ size_t source_length = RSTRING_LEN(source_value);
73
52
 
74
53
  // -- compress --
75
54
 
76
- #define BUFFERED_COMPRESS(function, ...) \
77
- while (true) { \
78
- uint8_t* remaining_destination_buffer = (uint8_t*)RSTRING_PTR(destination_value) + destination_length; \
79
- size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length; \
80
- \
81
- result = function(__VA_ARGS__, &remaining_destination_buffer, &remaining_destination_buffer_length); \
82
- \
83
- if ( \
84
- result != 0 && \
85
- result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) { \
86
- return LZWS_EXT_ERROR_UNEXPECTED; \
87
- } \
88
- \
89
- destination_length += prev_remaining_destination_buffer_length - remaining_destination_buffer_length; \
90
- \
91
- if (result == LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) { \
92
- ext_result = increase_destination_buffer( \
93
- destination_value, destination_length, \
94
- &remaining_destination_buffer_length, destination_buffer_length); \
95
- \
96
- if (ext_result != 0) { \
97
- return ext_result; \
98
- } \
99
- \
100
- continue; \
101
- } \
102
- \
103
- break; \
55
+ #define BUFFERED_COMPRESS(function, ...) \
56
+ while (true) { \
57
+ lzws_ext_byte_t* remaining_destination_buffer = (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
+ \
62
+ if ( \
63
+ result != 0 && \
64
+ result != LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) { \
65
+ return LZWS_EXT_ERROR_UNEXPECTED; \
66
+ } \
67
+ \
68
+ destination_length += prev_remaining_destination_buffer_length - remaining_destination_buffer_length; \
69
+ \
70
+ if (result == LZWS_COMPRESSOR_NEEDS_MORE_DESTINATION) { \
71
+ ext_result = increase_destination_buffer( \
72
+ destination_value, destination_length, \
73
+ &remaining_destination_buffer_length, destination_buffer_length); \
74
+ \
75
+ if (ext_result != 0) { \
76
+ return ext_result; \
77
+ } \
78
+ \
79
+ continue; \
80
+ } \
81
+ \
82
+ break; \
104
83
  }
105
84
 
106
85
  static inline lzws_ext_result_t compress(
107
86
  lzws_compressor_state_t* state_ptr,
108
- uint8_t* remaining_source, size_t remaining_source_length,
87
+ const char* source, size_t source_length,
109
88
  VALUE destination_value, size_t destination_buffer_length)
110
89
  {
111
90
  lzws_result_t result;
112
91
  lzws_ext_result_t ext_result;
113
92
 
93
+ lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*)source;
94
+ size_t remaining_source_length = source_length;
95
+
114
96
  size_t destination_length = 0;
115
97
  size_t remaining_destination_buffer_length = destination_buffer_length;
116
98
 
@@ -119,7 +101,7 @@ static inline lzws_ext_result_t compress(
119
101
 
120
102
  int exception;
121
103
 
122
- RESIZE_BUFFER(destination_value, destination_length, exception);
104
+ LZWS_EXT_RESIZE_STRING_BUFFER(destination_value, destination_length, exception);
123
105
  if (exception != 0) {
124
106
  return LZWS_EXT_ERROR_ALLOCATE_FAILED;
125
107
  }
@@ -157,7 +139,7 @@ VALUE lzws_ext_compress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value,
157
139
 
158
140
  int exception;
159
141
 
160
- CREATE_BUFFER(destination_value, destination_buffer_length, exception);
142
+ LZWS_EXT_CREATE_STRING_BUFFER(destination_value, destination_buffer_length, exception);
161
143
  if (exception != 0) {
162
144
  lzws_compressor_free_state(state_ptr);
163
145
  lzws_ext_raise_error(LZWS_EXT_ERROR_ALLOCATE_FAILED);
@@ -165,7 +147,7 @@ VALUE lzws_ext_compress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value,
165
147
 
166
148
  lzws_ext_result_t ext_result = compress(
167
149
  state_ptr,
168
- remaining_source, remaining_source_length,
150
+ source, source_length,
169
151
  destination_value, destination_buffer_length);
170
152
 
171
153
  lzws_compressor_free_state(state_ptr);
@@ -181,18 +163,21 @@ VALUE lzws_ext_compress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value,
181
163
 
182
164
  static inline lzws_ext_result_t decompress(
183
165
  lzws_decompressor_state_t* state_ptr,
184
- uint8_t* remaining_source, size_t remaining_source_length,
166
+ const char* source, size_t source_length,
185
167
  VALUE destination_value, size_t destination_buffer_length)
186
168
  {
187
169
  lzws_result_t result;
188
170
  lzws_ext_result_t ext_result;
189
171
 
172
+ lzws_ext_byte_t* remaining_source = (lzws_ext_byte_t*)source;
173
+ size_t remaining_source_length = source_length;
174
+
190
175
  size_t destination_length = 0;
191
176
  size_t remaining_destination_buffer_length = destination_buffer_length;
192
177
 
193
178
  while (true) {
194
- uint8_t* remaining_destination_buffer = (uint8_t*)RSTRING_PTR(destination_value) + destination_length;
195
- size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
179
+ lzws_ext_byte_t* remaining_destination_buffer = (lzws_ext_byte_t*)RSTRING_PTR(destination_value) + destination_length;
180
+ size_t prev_remaining_destination_buffer_length = remaining_destination_buffer_length;
196
181
 
197
182
  result = lzws_decompress(
198
183
  state_ptr,
@@ -232,7 +217,7 @@ static inline lzws_ext_result_t decompress(
232
217
 
233
218
  int exception;
234
219
 
235
- RESIZE_BUFFER(destination_value, destination_length, exception);
220
+ LZWS_EXT_RESIZE_STRING_BUFFER(destination_value, destination_length, exception);
236
221
  if (exception != 0) {
237
222
  return LZWS_EXT_ERROR_ALLOCATE_FAILED;
238
223
  }
@@ -268,7 +253,7 @@ VALUE lzws_ext_decompress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value
268
253
 
269
254
  int exception;
270
255
 
271
- CREATE_BUFFER(destination_value, destination_buffer_length, exception);
256
+ LZWS_EXT_CREATE_STRING_BUFFER(destination_value, destination_buffer_length, exception);
272
257
  if (exception != 0) {
273
258
  lzws_decompressor_free_state(state_ptr);
274
259
  lzws_ext_raise_error(LZWS_EXT_ERROR_ALLOCATE_FAILED);
@@ -276,7 +261,7 @@ VALUE lzws_ext_decompress_string(VALUE LZWS_EXT_UNUSED(self), VALUE source_value
276
261
 
277
262
  lzws_ext_result_t ext_result = decompress(
278
263
  state_ptr,
279
- remaining_source, remaining_source_length,
264
+ source, source_length,
280
265
  destination_value, destination_buffer_length);
281
266
 
282
267
  lzws_decompressor_free_state(state_ptr);
@@ -5,3 +5,4 @@ require_relative "lzws/stream/reader"
5
5
  require_relative "lzws/stream/writer"
6
6
  require_relative "lzws/file"
7
7
  require_relative "lzws/string"
8
+ require_relative "lzws/version"