bzip2-ruby 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +21 -0
- data/README.rdoc +2 -8
- data/Rakefile +1 -1
- data/VERSION.yml +1 -1
- data/bzip2-ruby.gemspec +22 -21
- data/ext/{bz2.c → bzip2.c} +534 -509
- data/ext/extconf.rb +6 -1
- data/lib/bzip2.rb +5 -0
- data/spec/reader_spec.rb +357 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/writer_spec.rb +139 -0
- metadata +13 -10
- data/History.txt +0 -5
- data/test/reader.rb +0 -370
- data/test/runit_.rb +0 -32
- data/test/writer.rb +0 -152
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
== 0.2.5 ???
|
2
|
+
|
3
|
+
* initial conversion of original tests over to rspec
|
4
|
+
|
5
|
+
== 0.2.4 2009-05-02
|
6
|
+
|
7
|
+
* renamed BZ2 module/namespace to Bzip2
|
8
|
+
|
9
|
+
== 0.2.3 2009-05-02
|
10
|
+
|
11
|
+
* renamed gem to bzip2-ruby from bz2
|
12
|
+
* initial conversion to jeweler
|
13
|
+
* bundling gemspec
|
14
|
+
* README and file structure organization updates
|
15
|
+
* updated Init_*() ruby initializer function to match new gem name
|
16
|
+
|
17
|
+
== 0.2.2 2008-12-22
|
18
|
+
|
19
|
+
* 1 major enhancement:
|
20
|
+
* Gemify bz2 library from http://moulon.inra.fr/ruby/bz2.html
|
21
|
+
* All credit goes to Guy Decoux <ts@moulon.inra.fr>
|
data/README.rdoc
CHANGED
@@ -8,14 +8,6 @@ You may need to specify:
|
|
8
8
|
|
9
9
|
--with-bz2-dir=<include file directory for libbzip2>
|
10
10
|
|
11
|
-
== Documentation:
|
12
|
-
|
13
|
-
coming soon...
|
14
|
-
|
15
|
-
== Spec Tests:
|
16
|
-
|
17
|
-
coming soon...
|
18
|
-
|
19
11
|
== Copying
|
20
12
|
|
21
13
|
This extension module is copyrighted free software by Guy Decoux
|
@@ -28,3 +20,5 @@ You may need to specify:
|
|
28
20
|
* Renamed BZ2 module/namespace to Bzip2
|
29
21
|
* Renamed compiled binary from "bz2" to "bzip2"
|
30
22
|
* Renamed gem from "bz2" to "bzip2-ruby"
|
23
|
+
* Converted original tests to rspec
|
24
|
+
* 1.9 compatibility
|
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ begin
|
|
7
7
|
gem.email = "seniorlopez@gmail.com"
|
8
8
|
gem.homepage = "http://github.com/brianmario/bzip2-ruby"
|
9
9
|
gem.authors = ["Guy Decoux", "Brian Lopez"]
|
10
|
-
gem.require_paths = ["ext"]
|
10
|
+
gem.require_paths = ["lib", "ext"]
|
11
11
|
gem.extra_rdoc_files = `git ls-files *.rdoc`.split("\n")
|
12
12
|
gem.files = `git ls-files`.split("\n")
|
13
13
|
gem.extensions = ["ext/extconf.rb"]
|
data/VERSION.yml
CHANGED
data/bzip2-ruby.gemspec
CHANGED
@@ -2,40 +2,41 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{bzip2-ruby}
|
5
|
-
s.version = "0.2.
|
5
|
+
s.version = "0.2.5"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Guy Decoux", "Brian Lopez"]
|
9
|
-
s.date = %q{2009-05-
|
9
|
+
s.date = %q{2009-05-18}
|
10
10
|
s.email = %q{seniorlopez@gmail.com}
|
11
11
|
s.extensions = ["ext/extconf.rb"]
|
12
12
|
s.extra_rdoc_files = [
|
13
|
-
"
|
13
|
+
"CHANGELOG.rdoc",
|
14
|
+
"README.rdoc"
|
14
15
|
]
|
15
16
|
s.files = [
|
16
|
-
"
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
17
|
+
"CHANGELOG.rdoc",
|
18
|
+
"README.rdoc",
|
19
|
+
"Rakefile",
|
20
|
+
"VERSION.yml",
|
21
|
+
"bzip2-ruby.gemspec",
|
22
|
+
"ext/bzip2.c",
|
23
|
+
"ext/extconf.rb",
|
24
|
+
"lib/bzip2.rb",
|
25
|
+
"spec/reader_spec.rb",
|
26
|
+
"spec/spec_helper.rb",
|
27
|
+
"spec/writer_spec.rb",
|
28
|
+
"tasks/extconf.rake",
|
29
|
+
"tasks/extconf/bz2.rake"
|
28
30
|
]
|
29
|
-
s.has_rdoc = true
|
30
31
|
s.homepage = %q{http://github.com/brianmario/bzip2-ruby}
|
31
32
|
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
-
s.require_paths = ["ext"]
|
33
|
-
s.rubygems_version = %q{1.3.
|
33
|
+
s.require_paths = ["lib", "ext"]
|
34
|
+
s.rubygems_version = %q{1.3.3}
|
34
35
|
s.summary = %q{Ruby C bindings to libbzip2.}
|
35
36
|
s.test_files = [
|
36
|
-
"
|
37
|
-
|
38
|
-
|
37
|
+
"spec/reader_spec.rb",
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"spec/writer_spec.rb"
|
39
40
|
]
|
40
41
|
|
41
42
|
if s.respond_to? :specification_version then
|
data/ext/{bz2.c → bzip2.c}
RENAMED
@@ -1,7 +1,11 @@
|
|
1
1
|
#include <ruby.h>
|
2
|
-
#include <rubyio.h>
|
3
2
|
#include <bzlib.h>
|
3
|
+
#ifndef RUBY_19_COMPATIBILITY
|
4
|
+
#include <rubyio.h>
|
4
5
|
#include <version.h>
|
6
|
+
#else
|
7
|
+
#include <ruby/io.h>
|
8
|
+
#endif
|
5
9
|
|
6
10
|
static VALUE bz_cWriter, bz_cReader, bz_cInternal;
|
7
11
|
static VALUE bz_eError, bz_eConfigError, bz_eEOZError;
|
@@ -33,11 +37,11 @@ struct bz_iv {
|
|
33
37
|
void (*finalize)();
|
34
38
|
};
|
35
39
|
|
36
|
-
#define Get_BZ2(obj, bzf)
|
37
|
-
rb_io_taint_check(obj);
|
38
|
-
Data_Get_Struct(obj, struct bz_file, bzf);
|
39
|
-
if (!RTEST(bzf->io)) {
|
40
|
-
|
40
|
+
#define Get_BZ2(obj, bzf) \
|
41
|
+
rb_io_taint_check(obj); \
|
42
|
+
Data_Get_Struct(obj, struct bz_file, bzf); \
|
43
|
+
if (!RTEST(bzf->io)) { \
|
44
|
+
rb_raise(rb_eIOError, "closed IO"); \
|
41
45
|
}
|
42
46
|
|
43
47
|
static VALUE
|
@@ -50,37 +54,37 @@ bz_raise(error)
|
|
50
54
|
exc = bz_eError;
|
51
55
|
switch (error) {
|
52
56
|
case BZ_SEQUENCE_ERROR:
|
53
|
-
|
54
|
-
|
57
|
+
msg = "incorrect sequence";
|
58
|
+
break;
|
55
59
|
case BZ_PARAM_ERROR:
|
56
|
-
|
57
|
-
|
60
|
+
msg = "parameter out of range";
|
61
|
+
break;
|
58
62
|
case BZ_MEM_ERROR:
|
59
|
-
|
60
|
-
|
63
|
+
msg = "not enough memory is available";
|
64
|
+
break;
|
61
65
|
case BZ_DATA_ERROR:
|
62
|
-
|
63
|
-
|
66
|
+
msg = "data integrity error is detected";
|
67
|
+
break;
|
64
68
|
case BZ_DATA_ERROR_MAGIC:
|
65
|
-
|
66
|
-
|
69
|
+
msg = "compressed stream does not start with the correct magic bytes";
|
70
|
+
break;
|
67
71
|
case BZ_IO_ERROR:
|
68
|
-
|
69
|
-
|
72
|
+
msg = "error reading or writing";
|
73
|
+
break;
|
70
74
|
case BZ_UNEXPECTED_EOF:
|
71
|
-
|
72
|
-
|
73
|
-
|
75
|
+
exc = bz_eEOZError;
|
76
|
+
msg = "compressed file finishes before the logical end of stream is detected";
|
77
|
+
break;
|
74
78
|
case BZ_OUTBUFF_FULL:
|
75
|
-
|
76
|
-
|
79
|
+
msg = "output buffer full";
|
80
|
+
break;
|
77
81
|
case BZ_CONFIG_ERROR:
|
78
|
-
|
79
|
-
|
80
|
-
|
82
|
+
exc = bz_eConfigError;
|
83
|
+
msg = "library has been improperly compiled on your platform";
|
84
|
+
break;
|
81
85
|
default:
|
82
|
-
|
83
|
-
|
86
|
+
msg = "unknown error";
|
87
|
+
exc = bz_eError;
|
84
88
|
}
|
85
89
|
rb_raise(exc, msg);
|
86
90
|
}
|
@@ -109,24 +113,26 @@ bz_find_struct(obj, ptr, posp)
|
|
109
113
|
struct bz_iv *bziv;
|
110
114
|
int i;
|
111
115
|
|
112
|
-
for (i = 0; i <
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
116
|
+
for (i = 0; i < RARRAY_LEN(bz_internal_ary); i++) {
|
117
|
+
Data_Get_Struct(RARRAY_PTR(bz_internal_ary)[i], struct bz_iv, bziv);
|
118
|
+
if (ptr) {
|
119
|
+
#ifndef RUBY_19_COMPATIBILITY
|
120
|
+
if (TYPE(bziv->io) == T_FILE && RFILE(bziv->io)->fptr == (OpenFile *)ptr) {
|
121
|
+
#else
|
122
|
+
if (TYPE(bziv->io) == T_FILE && RFILE(bziv->io)->fptr == (rb_io_t *)ptr) {
|
123
|
+
#endif
|
124
|
+
if (posp) *posp = i;
|
125
|
+
return bziv;
|
126
|
+
}
|
127
|
+
else if (TYPE(bziv->io) == T_DATA && DATA_PTR(bziv->io) == ptr) {
|
128
|
+
if (posp) *posp = i;
|
129
|
+
return bziv;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
else if (bziv->io == obj) {
|
133
|
+
if (posp) *posp = i;
|
134
|
+
return bziv;
|
135
|
+
}
|
130
136
|
}
|
131
137
|
if (posp) *posp = -1;
|
132
138
|
return 0;
|
@@ -139,34 +145,34 @@ bz_writer_internal_flush(bzf)
|
|
139
145
|
int closed = 1;
|
140
146
|
|
141
147
|
if (rb_respond_to(bzf->io, id_closed)) {
|
142
|
-
|
148
|
+
closed = RTEST(rb_funcall2(bzf->io, id_closed, 0, 0));
|
143
149
|
}
|
144
150
|
if (bzf->buf) {
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
151
|
+
if (!closed && bzf->state == BZ_OK) {
|
152
|
+
bzf->bzs.next_in = NULL;
|
153
|
+
bzf->bzs.avail_in = 0;
|
154
|
+
do {
|
155
|
+
bzf->bzs.next_out = bzf->buf;
|
156
|
+
bzf->bzs.avail_out = bzf->buflen;
|
157
|
+
bzf->state = BZ2_bzCompress(&(bzf->bzs), BZ_FINISH);
|
158
|
+
if (bzf->state != BZ_FINISH_OK &&
|
159
|
+
bzf->state != BZ_STREAM_END) {
|
160
|
+
break;
|
161
|
+
}
|
162
|
+
if (bzf->bzs.avail_out < bzf->buflen) {
|
163
|
+
rb_funcall(bzf->io, id_write, 1,
|
164
|
+
rb_str_new(bzf->buf,
|
165
|
+
bzf->buflen - bzf->bzs.avail_out));
|
166
|
+
}
|
167
|
+
} while (bzf->state != BZ_STREAM_END);
|
168
|
+
}
|
169
|
+
free(bzf->buf);
|
170
|
+
bzf->buf = 0;
|
171
|
+
BZ2_bzCompressEnd(&(bzf->bzs));
|
172
|
+
bzf->state = BZ_OK;
|
173
|
+
if (!closed && rb_respond_to(bzf->io, id_flush)) {
|
174
|
+
rb_funcall2(bzf->io, id_flush, 0, 0);
|
175
|
+
}
|
170
176
|
}
|
171
177
|
return closed;
|
172
178
|
}
|
@@ -182,25 +188,25 @@ bz_writer_internal_close(bzf)
|
|
182
188
|
closed = bz_writer_internal_flush(bzf);
|
183
189
|
bziv = bz_find_struct(bzf->io, 0, &pos);
|
184
190
|
if (bziv) {
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
191
|
+
if (TYPE(bzf->io) == T_FILE) {
|
192
|
+
RFILE(bzf->io)->fptr->finalize = bziv->finalize;
|
193
|
+
}
|
194
|
+
else if (TYPE(bziv->io) == T_DATA) {
|
195
|
+
RDATA(bziv->io)->dfree = bziv->finalize;
|
196
|
+
}
|
197
|
+
RDATA(bziv->bz2)->dfree = ruby_xfree;
|
198
|
+
bziv->bz2 = 0;
|
199
|
+
rb_ary_delete_at(bz_internal_ary, pos);
|
194
200
|
}
|
195
201
|
if (bzf->flags & BZ2_RB_CLOSE) {
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
202
|
+
bzf->flags &= ~BZ2_RB_CLOSE;
|
203
|
+
if (!closed && rb_respond_to(bzf->io, id_close)) {
|
204
|
+
rb_funcall2(bzf->io, id_close, 0, 0);
|
205
|
+
}
|
206
|
+
res = Qnil;
|
201
207
|
}
|
202
208
|
else {
|
203
|
-
|
209
|
+
res = bzf->io;
|
204
210
|
}
|
205
211
|
bzf->io = Qnil;
|
206
212
|
return res;
|
@@ -215,26 +221,26 @@ bz_internal_finalize(ary, obj)
|
|
215
221
|
struct bz_iv *bziv;
|
216
222
|
struct bz_file *bzf;
|
217
223
|
|
218
|
-
for (i = 0; i <
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
224
|
+
for (i = 0; i < RARRAY_LEN(bz_internal_ary); i++) {
|
225
|
+
elem = RARRAY_PTR(bz_internal_ary)[i];
|
226
|
+
Data_Get_Struct(elem, struct bz_iv, bziv);
|
227
|
+
if (bziv->bz2) {
|
228
|
+
RDATA(bziv->bz2)->dfree = ruby_xfree;
|
229
|
+
if (TYPE(bziv->io) == T_FILE) {
|
230
|
+
RFILE(bziv->io)->fptr->finalize = bziv->finalize;
|
231
|
+
}
|
232
|
+
else if (TYPE(bziv->io) == T_DATA) {
|
233
|
+
RDATA(bziv->io)->dfree = bziv->finalize;
|
234
|
+
}
|
235
|
+
Data_Get_Struct(bziv->bz2, struct bz_file, bzf);
|
236
|
+
closed = bz_writer_internal_flush(bzf);
|
237
|
+
if (bzf->flags & BZ2_RB_CLOSE) {
|
238
|
+
bzf->flags &= ~BZ2_RB_CLOSE;
|
239
|
+
if (!closed && rb_respond_to(bzf->io, id_close)) {
|
240
|
+
rb_funcall2(bzf->io, id_close, 0, 0);
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
238
244
|
}
|
239
245
|
return Qnil;
|
240
246
|
}
|
@@ -249,7 +255,7 @@ bz_writer_close(obj)
|
|
249
255
|
Get_BZ2(obj, bzf);
|
250
256
|
res = bz_writer_internal_close(bzf);
|
251
257
|
if (!NIL_P(res) && (bzf->flags & BZ2_RB_INTERNAL)) {
|
252
|
-
|
258
|
+
RBASIC(res)->klass = rb_cString;
|
253
259
|
}
|
254
260
|
return res;
|
255
261
|
}
|
@@ -265,12 +271,12 @@ bz_writer_close_bang(obj)
|
|
265
271
|
closed = bzf->flags & (BZ2_RB_INTERNAL|BZ2_RB_CLOSE);
|
266
272
|
bz_writer_close(obj);
|
267
273
|
if (!closed && rb_respond_to(bzf->io, id_close)) {
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
+
if (rb_respond_to(bzf->io, id_closed)) {
|
275
|
+
closed = RTEST(rb_funcall2(bzf->io, id_closed, 0, 0));
|
276
|
+
}
|
277
|
+
if (!closed) {
|
278
|
+
rb_funcall2(bzf->io, id_close, 0, 0);
|
279
|
+
}
|
274
280
|
}
|
275
281
|
return Qnil;
|
276
282
|
}
|
@@ -293,24 +299,35 @@ bz_io_data_finalize(ptr)
|
|
293
299
|
|
294
300
|
bziv = bz_find_struct(0, ptr, &pos);
|
295
301
|
if (bziv) {
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
302
|
+
rb_ary_delete_at(bz_internal_ary, pos);
|
303
|
+
Data_Get_Struct(bziv->bz2, struct bz_file, bzf);
|
304
|
+
rb_protect(bz_writer_internal_flush, (VALUE)bzf, 0);
|
305
|
+
RDATA(bziv->bz2)->dfree = ruby_xfree;
|
306
|
+
if (bziv->finalize) {
|
307
|
+
(*bziv->finalize)(ptr);
|
308
|
+
} else if (TYPE(bzf->io) == T_FILE) {
|
309
|
+
#ifndef RUBY_19_COMPATIBILITY
|
310
|
+
OpenFile *file = (OpenFile *)ptr;
|
311
|
+
if (file->f) {
|
312
|
+
fclose(file->f);
|
313
|
+
file->f = 0;
|
314
|
+
}
|
315
|
+
if (file->f2) {
|
316
|
+
fclose(file->f2);
|
317
|
+
file->f2 = 0;
|
318
|
+
}
|
319
|
+
#else
|
320
|
+
rb_io_t *file = (rb_io_t *)ptr;
|
321
|
+
if (file->fd) {
|
322
|
+
fclose(file->fd);
|
323
|
+
file->fd = 0;
|
324
|
+
}
|
325
|
+
if (file->stdio_file) {
|
326
|
+
fclose(file->stdio_file);
|
327
|
+
file->stdio_file = 0;
|
328
|
+
}
|
329
|
+
#endif
|
330
|
+
}
|
314
331
|
}
|
315
332
|
}
|
316
333
|
|
@@ -338,7 +355,7 @@ bz_writer_s_alloc(obj)
|
|
338
355
|
struct bz_file *bzf;
|
339
356
|
VALUE res;
|
340
357
|
res = Data_Make_Struct(obj, struct bz_file, bz_file_mark,
|
341
|
-
|
358
|
+
bz_writer_free, bzf);
|
342
359
|
bzf->bzs.bzalloc = bz_malloc;
|
343
360
|
bzf->bzs.bzfree = bz_free;
|
344
361
|
bzf->blocks = DEFAULT_BLOCKS;
|
@@ -354,7 +371,7 @@ bz_writer_flush(obj)
|
|
354
371
|
|
355
372
|
Get_BZ2(obj, bzf);
|
356
373
|
if (bzf->flags & BZ2_RB_INTERNAL) {
|
357
|
-
|
374
|
+
return bz_writer_close(obj);
|
358
375
|
}
|
359
376
|
bz_writer_internal_flush(bzf);
|
360
377
|
return Qnil;
|
@@ -369,22 +386,22 @@ bz_writer_s_open(argc, argv, obj)
|
|
369
386
|
struct bz_file *bzf;
|
370
387
|
|
371
388
|
if (argc < 1) {
|
372
|
-
|
389
|
+
rb_raise(rb_eArgError, "invalid number of arguments");
|
373
390
|
}
|
374
391
|
if (argc == 1) {
|
375
|
-
|
376
|
-
|
392
|
+
argv[0] = rb_funcall(rb_mKernel, id_open, 2, argv[0],
|
393
|
+
rb_str_new2("wb"));
|
377
394
|
}
|
378
395
|
else {
|
379
|
-
|
380
|
-
|
381
|
-
|
396
|
+
argv[1] = rb_funcall2(rb_mKernel, id_open, 2, argv);
|
397
|
+
argv += 1;
|
398
|
+
argc -= 1;
|
382
399
|
}
|
383
400
|
res = rb_funcall2(obj, id_new, argc, argv);
|
384
401
|
Data_Get_Struct(res, struct bz_file, bzf);
|
385
402
|
bzf->flags |= BZ2_RB_CLOSE;
|
386
403
|
if (rb_block_given_p()) {
|
387
|
-
|
404
|
+
return rb_ensure(rb_yield, res, bz_writer_close, res);
|
388
405
|
}
|
389
406
|
return res;
|
390
407
|
}
|
@@ -394,10 +411,10 @@ bz_str_write(obj, str)
|
|
394
411
|
VALUE obj, str;
|
395
412
|
{
|
396
413
|
if (TYPE(str) != T_STRING) {
|
397
|
-
|
414
|
+
rb_raise(rb_eArgError, "expected a String");
|
398
415
|
}
|
399
|
-
if (
|
400
|
-
|
416
|
+
if (RSTRING_LEN(str)) {
|
417
|
+
rb_str_cat(obj, RSTRING_PTR(str), RSTRING_LEN(str));
|
401
418
|
}
|
402
419
|
return str;
|
403
420
|
}
|
@@ -421,60 +438,64 @@ bz_writer_init(argc, argv, obj)
|
|
421
438
|
|
422
439
|
switch(rb_scan_args(argc, argv, "03", &a, &b, &c)) {
|
423
440
|
case 3:
|
424
|
-
|
425
|
-
|
441
|
+
work = NUM2INT(c);
|
442
|
+
/* ... */
|
426
443
|
case 2:
|
427
|
-
|
444
|
+
blocks = NUM2INT(b);
|
428
445
|
}
|
429
446
|
Data_Get_Struct(obj, struct bz_file, bzf);
|
430
447
|
if (NIL_P(a)) {
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
448
|
+
a = rb_str_new(0, 0);
|
449
|
+
rb_define_method(rb_singleton_class(a), "write", bz_str_write, 1);
|
450
|
+
rb_define_method(rb_singleton_class(a), "closed?", bz_str_closed, 0);
|
451
|
+
bzf->flags |= BZ2_RB_INTERNAL;
|
435
452
|
}
|
436
453
|
else {
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
454
|
+
VALUE iv;
|
455
|
+
struct bz_iv *bziv;
|
456
|
+
#ifndef RUBY_19_COMPATIBILITY
|
457
|
+
OpenFile *fptr;
|
458
|
+
#else
|
459
|
+
rb_io_t *fptr;
|
460
|
+
#endif
|
461
|
+
|
462
|
+
rb_io_taint_check(a);
|
463
|
+
if (!rb_respond_to(a, id_write)) {
|
464
|
+
rb_raise(rb_eArgError, "first argument must respond to #write");
|
465
|
+
}
|
466
|
+
if (TYPE(a) == T_FILE) {
|
467
|
+
GetOpenFile(a, fptr);
|
468
|
+
rb_io_check_writable(fptr);
|
469
|
+
}
|
470
|
+
else if (rb_respond_to(a, id_closed)) {
|
471
|
+
iv = rb_funcall2(a, id_closed, 0, 0);
|
472
|
+
if (RTEST(iv)) {
|
473
|
+
rb_raise(rb_eArgError, "closed object");
|
474
|
+
}
|
475
|
+
}
|
476
|
+
bziv = bz_find_struct(a, 0, 0);
|
477
|
+
if (bziv) {
|
478
|
+
if (RTEST(bziv->bz2)) {
|
479
|
+
rb_raise(rb_eArgError, "invalid data type");
|
480
|
+
}
|
481
|
+
bziv->bz2 = obj;
|
482
|
+
}
|
483
|
+
else {
|
484
|
+
iv = Data_Make_Struct(rb_cData, struct bz_iv, 0, free, bziv);
|
485
|
+
bziv->io = a;
|
486
|
+
bziv->bz2 = obj;
|
487
|
+
rb_ary_push(bz_internal_ary, iv);
|
488
|
+
}
|
489
|
+
switch (TYPE(a)) {
|
490
|
+
case T_FILE:
|
491
|
+
bziv->finalize = RFILE(a)->fptr->finalize;
|
492
|
+
RFILE(a)->fptr->finalize = bz_io_data_finalize;
|
493
|
+
break;
|
494
|
+
case T_DATA:
|
495
|
+
bziv->finalize = RDATA(a)->dfree;
|
496
|
+
RDATA(a)->dfree = bz_io_data_finalize;
|
497
|
+
break;
|
498
|
+
}
|
478
499
|
}
|
479
500
|
bzf->io = a;
|
480
501
|
bzf->blocks = blocks;
|
@@ -494,36 +515,36 @@ bz_writer_write(obj, a)
|
|
494
515
|
a = rb_obj_as_string(a);
|
495
516
|
Get_BZ2(obj, bzf);
|
496
517
|
if (!bzf->buf) {
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
}
|
510
|
-
bzf->bzs.next_in =
|
511
|
-
bzf->bzs.avail_in =
|
518
|
+
if (bzf->state != BZ_OK) {
|
519
|
+
bz_raise(bzf->state);
|
520
|
+
}
|
521
|
+
bzf->state = BZ2_bzCompressInit(&(bzf->bzs), bzf->blocks,
|
522
|
+
0, bzf->work);
|
523
|
+
if (bzf->state != BZ_OK) {
|
524
|
+
bz_writer_internal_flush(bzf);
|
525
|
+
bz_raise(bzf->state);
|
526
|
+
}
|
527
|
+
bzf->buf = ALLOC_N(char, BZ_RB_BLOCKSIZE + 1);
|
528
|
+
bzf->buflen = BZ_RB_BLOCKSIZE;
|
529
|
+
bzf->buf[0] = bzf->buf[bzf->buflen] = '\0';
|
530
|
+
}
|
531
|
+
bzf->bzs.next_in = RSTRING_PTR(a);
|
532
|
+
bzf->bzs.avail_in = RSTRING_LEN(a);
|
512
533
|
while (bzf->bzs.avail_in) {
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
}
|
526
|
-
return INT2NUM(
|
534
|
+
bzf->bzs.next_out = bzf->buf;
|
535
|
+
bzf->bzs.avail_out = bzf->buflen;
|
536
|
+
bzf->state = BZ2_bzCompress(&(bzf->bzs), BZ_RUN);
|
537
|
+
if (bzf->state == BZ_SEQUENCE_ERROR || bzf->state == BZ_PARAM_ERROR) {
|
538
|
+
bz_writer_internal_flush(bzf);
|
539
|
+
bz_raise(bzf->state);
|
540
|
+
}
|
541
|
+
bzf->state = BZ_OK;
|
542
|
+
if (bzf->bzs.avail_out < bzf->buflen) {
|
543
|
+
n = bzf->buflen - bzf->bzs.avail_out;
|
544
|
+
rb_funcall(bzf->io, id_write, 1, rb_str_new(bzf->buf, n));
|
545
|
+
}
|
546
|
+
}
|
547
|
+
return INT2NUM(RSTRING_LEN(a));
|
527
548
|
}
|
528
549
|
|
529
550
|
static VALUE
|
@@ -542,15 +563,15 @@ bz_compress(argc, argv, obj)
|
|
542
563
|
VALUE bz2, str;
|
543
564
|
|
544
565
|
if (!argc) {
|
545
|
-
|
566
|
+
rb_raise(rb_eArgError, "need a String to compress");
|
546
567
|
}
|
547
568
|
str = rb_str_to_str(argv[0]);
|
548
569
|
argv[0] = Qnil;
|
549
570
|
bz2 = rb_funcall2(bz_cWriter, id_new, argc, argv);
|
550
571
|
if (OBJ_TAINTED(str)) {
|
551
|
-
|
552
|
-
|
553
|
-
|
572
|
+
struct bz_file *bzf;
|
573
|
+
Data_Get_Struct(bz2, struct bz_file, bzf);
|
574
|
+
OBJ_TAINT(bzf->io);
|
554
575
|
}
|
555
576
|
bz_writer_write(bz2, str);
|
556
577
|
return bz_writer_close(bz2);
|
@@ -563,7 +584,7 @@ bz_reader_s_alloc(obj)
|
|
563
584
|
struct bz_file *bzf;
|
564
585
|
VALUE res;
|
565
586
|
res = Data_Make_Struct(obj, struct bz_file, bz_file_mark,
|
566
|
-
|
587
|
+
ruby_xfree, bzf);
|
567
588
|
bzf->bzs.bzalloc = bz_malloc;
|
568
589
|
bzf->bzs.bzfree = bz_free;
|
569
590
|
bzf->blocks = DEFAULT_BLOCKS;
|
@@ -582,7 +603,7 @@ bz_reader_s_open(argc, argv, obj)
|
|
582
603
|
struct bz_file *bzf;
|
583
604
|
|
584
605
|
if (argc < 1) {
|
585
|
-
|
606
|
+
rb_raise(rb_eArgError, "invalid number of arguments");
|
586
607
|
}
|
587
608
|
argv[0] = rb_funcall2(rb_mKernel, id_open, 1, argv);
|
588
609
|
if (NIL_P(argv[0])) return Qnil;
|
@@ -590,7 +611,7 @@ bz_reader_s_open(argc, argv, obj)
|
|
590
611
|
Data_Get_Struct(res, struct bz_file, bzf);
|
591
612
|
bzf->flags |= BZ2_RB_CLOSE;
|
592
613
|
if (rb_block_given_p()) {
|
593
|
-
|
614
|
+
return rb_ensure(rb_yield, res, bz_reader_close, res);
|
594
615
|
}
|
595
616
|
return res;
|
596
617
|
}
|
@@ -606,42 +627,46 @@ bz_reader_init(argc, argv, obj)
|
|
606
627
|
int internal = 0;
|
607
628
|
|
608
629
|
if (rb_scan_args(argc, argv, "11", &a, &b) == 2) {
|
609
|
-
|
630
|
+
small = RTEST(b);
|
610
631
|
}
|
611
632
|
rb_io_taint_check(a);
|
612
633
|
if (OBJ_TAINTED(a)) {
|
613
|
-
|
634
|
+
OBJ_TAINT(obj);
|
614
635
|
}
|
615
636
|
if (rb_respond_to(a, id_read)) {
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
637
|
+
if (TYPE(a) == T_FILE) {
|
638
|
+
#ifndef RUBY_19_COMPATIBILITY
|
639
|
+
OpenFile *fptr;
|
640
|
+
#else
|
641
|
+
rb_io_t *fptr;
|
642
|
+
#endif
|
643
|
+
|
644
|
+
GetOpenFile(a, fptr);
|
645
|
+
rb_io_check_readable(fptr);
|
646
|
+
}
|
647
|
+
else if (rb_respond_to(a, id_closed)) {
|
648
|
+
VALUE iv = rb_funcall2(a, id_closed, 0, 0);
|
649
|
+
if (RTEST(iv)) {
|
650
|
+
rb_raise(rb_eArgError, "closed object");
|
651
|
+
}
|
652
|
+
}
|
628
653
|
}
|
629
654
|
else {
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
655
|
+
struct bz_str *bzs;
|
656
|
+
VALUE res;
|
657
|
+
|
658
|
+
if (!rb_respond_to(a, id_str)) {
|
659
|
+
rb_raise(rb_eArgError, "first argument must respond to #read");
|
660
|
+
}
|
661
|
+
a = rb_funcall2(a, id_str, 0, 0);
|
662
|
+
if (TYPE(a) != T_STRING) {
|
663
|
+
rb_raise(rb_eArgError, "#to_str must return a String");
|
664
|
+
}
|
665
|
+
res = Data_Make_Struct(bz_cInternal, struct bz_str,
|
666
|
+
bz_str_mark, ruby_xfree, bzs);
|
667
|
+
bzs->str = a;
|
668
|
+
a = res;
|
669
|
+
internal = BZ2_RB_INTERNAL;
|
645
670
|
}
|
646
671
|
Data_Get_Struct(obj, struct bz_file, bzf);
|
647
672
|
bzf->io = a;
|
@@ -658,23 +683,23 @@ bz_get_bzf(obj)
|
|
658
683
|
|
659
684
|
Get_BZ2(obj, bzf);
|
660
685
|
if (!bzf->buf) {
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
686
|
+
if (bzf->state != BZ_OK) {
|
687
|
+
bz_raise(bzf->state);
|
688
|
+
}
|
689
|
+
bzf->state = BZ2_bzDecompressInit(&(bzf->bzs), 0, bzf->small);
|
690
|
+
if (bzf->state != BZ_OK) {
|
691
|
+
BZ2_bzDecompressEnd(&(bzf->bzs));
|
692
|
+
bz_raise(bzf->state);
|
693
|
+
}
|
694
|
+
bzf->buf = ALLOC_N(char, BZ_RB_BLOCKSIZE + 1);
|
695
|
+
bzf->buflen = BZ_RB_BLOCKSIZE;
|
696
|
+
bzf->buf[0] = bzf->buf[bzf->buflen] = '\0';
|
697
|
+
bzf->bzs.total_out_hi32 = bzf->bzs.total_out_lo32 = 0;
|
698
|
+
bzf->bzs.next_out = bzf->buf;
|
699
|
+
bzf->bzs.avail_out = 0;
|
675
700
|
}
|
676
701
|
if (bzf->state == BZ_STREAM_END && !bzf->bzs.avail_out) {
|
677
|
-
|
702
|
+
return 0;
|
678
703
|
}
|
679
704
|
return bzf;
|
680
705
|
}
|
@@ -687,33 +712,33 @@ bz_next_available(bzf, in)
|
|
687
712
|
bzf->bzs.next_out = bzf->buf;
|
688
713
|
bzf->bzs.avail_out = 0;
|
689
714
|
if (bzf->state == BZ_STREAM_END) {
|
690
|
-
|
715
|
+
return BZ_STREAM_END;
|
691
716
|
}
|
692
717
|
if (!bzf->bzs.avail_in) {
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
718
|
+
bzf->in = rb_funcall(bzf->io, id_read, 1, INT2FIX(1024));
|
719
|
+
if (TYPE(bzf->in) != T_STRING || RSTRING_LEN(bzf->in) == 0) {
|
720
|
+
BZ2_bzDecompressEnd(&(bzf->bzs));
|
721
|
+
bzf->bzs.avail_out = 0;
|
722
|
+
bzf->state = BZ_UNEXPECTED_EOF;
|
723
|
+
bz_raise(bzf->state);
|
724
|
+
}
|
725
|
+
bzf->bzs.next_in = RSTRING_PTR(bzf->in);
|
726
|
+
bzf->bzs.avail_in = RSTRING_LEN(bzf->in);
|
702
727
|
}
|
703
728
|
if ((bzf->buflen - in) < (BZ_RB_BLOCKSIZE / 2)) {
|
704
|
-
|
705
|
-
|
706
|
-
|
729
|
+
bzf->buf = REALLOC_N(bzf->buf, char, bzf->buflen+BZ_RB_BLOCKSIZE+1);
|
730
|
+
bzf->buflen += BZ_RB_BLOCKSIZE;
|
731
|
+
bzf->buf[bzf->buflen] = '\0';
|
707
732
|
}
|
708
733
|
bzf->bzs.avail_out = bzf->buflen - in;
|
709
734
|
bzf->bzs.next_out = bzf->buf + in;
|
710
735
|
bzf->state = BZ2_bzDecompress(&(bzf->bzs));
|
711
736
|
if (bzf->state != BZ_OK) {
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
737
|
+
BZ2_bzDecompressEnd(&(bzf->bzs));
|
738
|
+
if (bzf->state != BZ_STREAM_END) {
|
739
|
+
bzf->bzs.avail_out = 0;
|
740
|
+
bz_raise(bzf->state);
|
741
|
+
}
|
717
742
|
}
|
718
743
|
bzf->bzs.avail_out = bzf->buflen - bzf->bzs.avail_out;
|
719
744
|
bzf->bzs.next_out = bzf->buf;
|
@@ -735,56 +760,56 @@ bz_read_until(bzf, str, len, td1)
|
|
735
760
|
|
736
761
|
res = rb_str_new(0, 0);
|
737
762
|
while (1) {
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
763
|
+
total = bzf->bzs.avail_out;
|
764
|
+
if (len == 1) {
|
765
|
+
tx = memchr(bzf->bzs.next_out, *str, bzf->bzs.avail_out);
|
766
|
+
if (tx) {
|
767
|
+
i = tx - bzf->bzs.next_out + len;
|
768
|
+
res = rb_str_cat(res, bzf->bzs.next_out, i);
|
769
|
+
bzf->bzs.next_out += i;
|
770
|
+
bzf->bzs.avail_out -= i;
|
771
|
+
return res;
|
772
|
+
}
|
773
|
+
}
|
774
|
+
else {
|
775
|
+
tx = bzf->bzs.next_out;
|
776
|
+
end = bzf->bzs.next_out + bzf->bzs.avail_out;
|
777
|
+
while (tx + len <= end) {
|
778
|
+
for (p = str, t = tx; p != pend; ++p, ++t) {
|
779
|
+
if (*p != *t) break;
|
780
|
+
}
|
781
|
+
if (p == pend) {
|
782
|
+
i = tx - bzf->bzs.next_out + len;
|
783
|
+
res = rb_str_cat(res, bzf->bzs.next_out, i);
|
784
|
+
bzf->bzs.next_out += i;
|
785
|
+
bzf->bzs.avail_out -= i;
|
786
|
+
return res;
|
787
|
+
}
|
788
|
+
if (td1) {
|
789
|
+
tx += td1[(int)*(tx + len)];
|
790
|
+
}
|
791
|
+
else {
|
792
|
+
tx += 1;
|
793
|
+
}
|
794
|
+
}
|
795
|
+
}
|
796
|
+
nex = 0;
|
797
|
+
if (total) {
|
798
|
+
nex = len - 1;
|
799
|
+
res = rb_str_cat(res, bzf->bzs.next_out, total - nex);
|
800
|
+
if (nex) {
|
801
|
+
MEMMOVE(bzf->buf, bzf->bzs.next_out + total - nex, char, nex);
|
802
|
+
}
|
803
|
+
}
|
804
|
+
if (bz_next_available(bzf, nex) == BZ_STREAM_END) {
|
805
|
+
if (nex) {
|
806
|
+
res = rb_str_cat(res, bzf->buf, nex);
|
807
|
+
}
|
808
|
+
if (RSTRING_LEN(res)) {
|
809
|
+
return res;
|
810
|
+
}
|
811
|
+
return Qnil;
|
812
|
+
}
|
788
813
|
}
|
789
814
|
return Qnil;
|
790
815
|
}
|
@@ -797,17 +822,17 @@ bz_read_while(bzf, c)
|
|
797
822
|
char *end;
|
798
823
|
|
799
824
|
while (1) {
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
825
|
+
end = bzf->bzs.next_out + bzf->bzs.avail_out;
|
826
|
+
while (bzf->bzs.next_out < end) {
|
827
|
+
if (c != *bzf->bzs.next_out) {
|
828
|
+
bzf->bzs.avail_out = end - bzf->bzs.next_out;
|
829
|
+
return *bzf->bzs.next_out;
|
830
|
+
}
|
831
|
+
++bzf->bzs.next_out;
|
832
|
+
}
|
833
|
+
if (bz_next_available(bzf, 0) == BZ_STREAM_END) {
|
834
|
+
return EOF;
|
835
|
+
}
|
811
836
|
}
|
812
837
|
return EOF;
|
813
838
|
}
|
@@ -824,40 +849,40 @@ bz_reader_read(argc, argv, obj)
|
|
824
849
|
|
825
850
|
rb_scan_args(argc, argv, "01", &length);
|
826
851
|
if (NIL_P(length)) {
|
827
|
-
|
852
|
+
n = -1;
|
828
853
|
}
|
829
854
|
else {
|
830
|
-
|
831
|
-
|
832
|
-
|
833
|
-
|
855
|
+
n = NUM2INT(length);
|
856
|
+
if (n < 0) {
|
857
|
+
rb_raise(rb_eArgError, "negative length %d given", n);
|
858
|
+
}
|
834
859
|
}
|
835
860
|
bzf = bz_get_bzf(obj);
|
836
861
|
if (!bzf) {
|
837
|
-
|
862
|
+
return Qnil;
|
838
863
|
}
|
839
864
|
res = rb_str_new(0, 0);
|
840
865
|
if (OBJ_TAINTED(obj)) {
|
841
|
-
|
866
|
+
OBJ_TAINT(res);
|
842
867
|
}
|
843
868
|
if (n == 0) {
|
844
|
-
|
869
|
+
return res;
|
845
870
|
}
|
846
871
|
while (1) {
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
872
|
+
total = bzf->bzs.avail_out;
|
873
|
+
if (n != -1 && (RSTRING_LEN(res) + total) >= n) {
|
874
|
+
n -= RSTRING_LEN(res);
|
875
|
+
res = rb_str_cat(res, bzf->bzs.next_out, n);
|
876
|
+
bzf->bzs.next_out += n;
|
877
|
+
bzf->bzs.avail_out -= n;
|
878
|
+
return res;
|
879
|
+
}
|
880
|
+
if (total) {
|
881
|
+
res = rb_str_cat(res, bzf->bzs.next_out, total);
|
882
|
+
}
|
883
|
+
if (bz_next_available(bzf, 0) == BZ_STREAM_END) {
|
884
|
+
return res;
|
885
|
+
}
|
861
886
|
}
|
862
887
|
return Qnil;
|
863
888
|
}
|
@@ -868,10 +893,10 @@ bz_getc(obj)
|
|
868
893
|
{
|
869
894
|
VALUE length = INT2FIX(1);
|
870
895
|
VALUE res = bz_reader_read(1, &length, obj);
|
871
|
-
if (NIL_P(res) ||
|
872
|
-
|
896
|
+
if (NIL_P(res) || RSTRING_LEN(res) == 0) {
|
897
|
+
return EOF;
|
873
898
|
}
|
874
|
-
return
|
899
|
+
return RSTRING_PTR(res)[0];
|
875
900
|
}
|
876
901
|
|
877
902
|
static VALUE
|
@@ -883,19 +908,19 @@ bz_reader_ungetc(obj, a)
|
|
883
908
|
|
884
909
|
Get_BZ2(obj, bzf);
|
885
910
|
if (!bzf->buf) {
|
886
|
-
|
911
|
+
bz_raise(BZ_SEQUENCE_ERROR);
|
887
912
|
}
|
888
913
|
if (bzf->bzs.avail_out < bzf->buflen) {
|
889
|
-
|
890
|
-
|
891
|
-
|
914
|
+
bzf->bzs.next_out -= 1;
|
915
|
+
bzf->bzs.next_out[0] = c;
|
916
|
+
bzf->bzs.avail_out += 1;
|
892
917
|
}
|
893
918
|
else {
|
894
|
-
|
895
|
-
|
896
|
-
|
897
|
-
|
898
|
-
|
919
|
+
bzf->buf = REALLOC_N(bzf->buf, char, bzf->buflen + 2);
|
920
|
+
bzf->buf[bzf->buflen++] = c;
|
921
|
+
bzf->buf[bzf->buflen] = '\0';
|
922
|
+
bzf->bzs.next_out = bzf->buf;
|
923
|
+
bzf->bzs.avail_out = bzf->buflen;
|
899
924
|
}
|
900
925
|
return Qnil;
|
901
926
|
}
|
@@ -909,20 +934,20 @@ bz_reader_ungets(obj, a)
|
|
909
934
|
Check_Type(a, T_STRING);
|
910
935
|
Get_BZ2(obj, bzf);
|
911
936
|
if (!bzf->buf) {
|
912
|
-
|
937
|
+
bz_raise(BZ_SEQUENCE_ERROR);
|
913
938
|
}
|
914
|
-
if ((bzf->bzs.avail_out +
|
915
|
-
|
916
|
-
|
917
|
-
|
939
|
+
if ((bzf->bzs.avail_out + RSTRING_LEN(a)) < bzf->buflen) {
|
940
|
+
bzf->bzs.next_out -= RSTRING_LEN(a);
|
941
|
+
MEMCPY(bzf->bzs.next_out, RSTRING_PTR(a), char, RSTRING_LEN(a));
|
942
|
+
bzf->bzs.avail_out += RSTRING_LEN(a);
|
918
943
|
}
|
919
944
|
else {
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
945
|
+
bzf->buf = REALLOC_N(bzf->buf, char, bzf->buflen + RSTRING_LEN(a) + 1);
|
946
|
+
MEMCPY(bzf->buf + bzf->buflen, RSTRING_PTR(a), char,RSTRING_LEN(a));
|
947
|
+
bzf->buflen += RSTRING_LEN(a);
|
948
|
+
bzf->buf[bzf->buflen] = '\0';
|
949
|
+
bzf->bzs.next_out = bzf->buf;
|
950
|
+
bzf->bzs.avail_out = bzf->buflen;
|
926
951
|
}
|
927
952
|
return Qnil;
|
928
953
|
}
|
@@ -936,11 +961,11 @@ bz_reader_gets(obj)
|
|
936
961
|
|
937
962
|
bzf = bz_get_bzf(obj);
|
938
963
|
if (bzf) {
|
939
|
-
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
964
|
+
str = bz_read_until(bzf, "\n", 1, 0);
|
965
|
+
if (!NIL_P(str)) {
|
966
|
+
bzf->lineno++;
|
967
|
+
OBJ_TAINT(str);
|
968
|
+
}
|
944
969
|
}
|
945
970
|
return str;
|
946
971
|
}
|
@@ -958,59 +983,59 @@ bz_reader_gets_internal(argc, argv, obj, td, init)
|
|
958
983
|
|
959
984
|
rs = rb_rs;
|
960
985
|
if (argc) {
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
986
|
+
rb_scan_args(argc, argv, "1", &rs);
|
987
|
+
if (!NIL_P(rs)) {
|
988
|
+
Check_Type(rs, T_STRING);
|
989
|
+
}
|
965
990
|
}
|
966
991
|
if (NIL_P(rs)) {
|
967
|
-
|
992
|
+
return bz_reader_read(1, &rs, obj);
|
968
993
|
}
|
969
|
-
rslen =
|
970
|
-
if (rs == rb_default_rs || (rslen == 1 &&
|
971
|
-
|
994
|
+
rslen = RSTRING_LEN(rs);
|
995
|
+
if (rs == rb_default_rs || (rslen == 1 && RSTRING_PTR(rs)[0] == '\n')) {
|
996
|
+
return bz_reader_gets(obj);
|
972
997
|
}
|
973
998
|
|
974
999
|
if (rslen == 0) {
|
975
|
-
|
976
|
-
|
977
|
-
|
1000
|
+
rsptr = "\n\n";
|
1001
|
+
rslen = 2;
|
1002
|
+
rspara = 1;
|
978
1003
|
}
|
979
1004
|
else {
|
980
|
-
|
981
|
-
|
1005
|
+
rsptr = RSTRING_PTR(rs);
|
1006
|
+
rspara = 0;
|
982
1007
|
}
|
983
1008
|
|
984
1009
|
bzf = bz_get_bzf(obj);
|
985
1010
|
if (!bzf) {
|
986
|
-
|
1011
|
+
return Qnil;
|
987
1012
|
}
|
988
1013
|
if (rspara) {
|
989
|
-
|
1014
|
+
bz_read_while(bzf, '\n');
|
990
1015
|
}
|
991
1016
|
td1 = 0;
|
992
1017
|
if (rslen != 1) {
|
993
|
-
|
994
|
-
|
1018
|
+
if (init) {
|
1019
|
+
int i;
|
995
1020
|
|
996
|
-
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
1001
|
-
|
1002
|
-
|
1003
|
-
|
1021
|
+
for (i = 0; i < ASIZE; i++) {
|
1022
|
+
td[i] = rslen + 1;
|
1023
|
+
}
|
1024
|
+
for (i = 0; i < rslen; i++) {
|
1025
|
+
td[(int)*(rsptr + i)] = rslen - i;
|
1026
|
+
}
|
1027
|
+
}
|
1028
|
+
td1 = td;
|
1004
1029
|
}
|
1005
1030
|
|
1006
1031
|
res = bz_read_until(bzf, rsptr, rslen, td1);
|
1007
1032
|
if (rspara) {
|
1008
|
-
|
1033
|
+
bz_read_while(bzf, '\n');
|
1009
1034
|
}
|
1010
1035
|
|
1011
1036
|
if (!NIL_P(res)) {
|
1012
|
-
|
1013
|
-
|
1037
|
+
bzf->lineno++;
|
1038
|
+
OBJ_TAINT(res);
|
1014
1039
|
}
|
1015
1040
|
return res;
|
1016
1041
|
}
|
@@ -1024,13 +1049,13 @@ bz_reader_set_unused(obj, a)
|
|
1024
1049
|
Check_Type(a, T_STRING);
|
1025
1050
|
Get_BZ2(obj, bzf);
|
1026
1051
|
if (!bzf->in) {
|
1027
|
-
|
1052
|
+
bzf->in = rb_str_new(RSTRING_PTR(a), RSTRING_LEN(a));
|
1028
1053
|
}
|
1029
1054
|
else {
|
1030
|
-
|
1055
|
+
bzf->in = rb_str_cat(bzf->in, RSTRING_PTR(a), RSTRING_LEN(a));
|
1031
1056
|
}
|
1032
|
-
bzf->bzs.next_in =
|
1033
|
-
bzf->bzs.avail_in =
|
1057
|
+
bzf->bzs.next_in = RSTRING_PTR(bzf->in);
|
1058
|
+
bzf->bzs.avail_in = RSTRING_LEN(bzf->in);
|
1034
1059
|
return Qnil;
|
1035
1060
|
}
|
1036
1061
|
|
@@ -1042,10 +1067,10 @@ bz_reader_getc(obj)
|
|
1042
1067
|
VALUE len = INT2FIX(1);
|
1043
1068
|
|
1044
1069
|
str = bz_reader_read(1, &len, obj);
|
1045
|
-
if (NIL_P(str) ||
|
1046
|
-
|
1070
|
+
if (NIL_P(str) || RSTRING_LEN(str) == 0) {
|
1071
|
+
return Qnil;
|
1047
1072
|
}
|
1048
|
-
return INT2FIX(
|
1073
|
+
return INT2FIX(RSTRING_PTR(str)[0] & 0xff);
|
1049
1074
|
}
|
1050
1075
|
|
1051
1076
|
static void
|
@@ -1061,7 +1086,7 @@ bz_reader_readchar(obj)
|
|
1061
1086
|
VALUE res = bz_reader_getc(obj);
|
1062
1087
|
|
1063
1088
|
if (NIL_P(res)) {
|
1064
|
-
|
1089
|
+
bz_eoz_error();
|
1065
1090
|
}
|
1066
1091
|
return res;
|
1067
1092
|
}
|
@@ -1075,7 +1100,7 @@ bz_reader_gets_m(argc, argv, obj)
|
|
1075
1100
|
VALUE str = bz_reader_gets_internal(argc, argv, obj, td, Qtrue);
|
1076
1101
|
|
1077
1102
|
if (!NIL_P(str)) {
|
1078
|
-
|
1103
|
+
rb_lastline_set(str);
|
1079
1104
|
}
|
1080
1105
|
return str;
|
1081
1106
|
}
|
@@ -1088,7 +1113,7 @@ bz_reader_readline(argc, argv, obj)
|
|
1088
1113
|
VALUE res = bz_reader_gets_m(argc, argv, obj);
|
1089
1114
|
|
1090
1115
|
if (NIL_P(res)) {
|
1091
|
-
|
1116
|
+
bz_eoz_error();
|
1092
1117
|
}
|
1093
1118
|
return res;
|
1094
1119
|
}
|
@@ -1104,8 +1129,8 @@ bz_reader_readlines(argc, argv, obj)
|
|
1104
1129
|
in = Qtrue;
|
1105
1130
|
ary = rb_ary_new();
|
1106
1131
|
while (!NIL_P(line = bz_reader_gets_internal(argc, argv, obj, td, in))) {
|
1107
|
-
|
1108
|
-
|
1132
|
+
in = Qfalse;
|
1133
|
+
rb_ary_push(ary, line);
|
1109
1134
|
}
|
1110
1135
|
return ary;
|
1111
1136
|
}
|
@@ -1120,8 +1145,8 @@ bz_reader_each_line(argc, argv, obj)
|
|
1120
1145
|
|
1121
1146
|
in = Qtrue;
|
1122
1147
|
while (!NIL_P(line = bz_reader_gets_internal(argc, argv, obj, td, in))) {
|
1123
|
-
|
1124
|
-
|
1148
|
+
in = Qfalse;
|
1149
|
+
rb_yield(line);
|
1125
1150
|
}
|
1126
1151
|
return obj;
|
1127
1152
|
}
|
@@ -1133,7 +1158,7 @@ bz_reader_each_byte(obj)
|
|
1133
1158
|
int c;
|
1134
1159
|
|
1135
1160
|
while ((c = bz_getc(obj)) != EOF) {
|
1136
|
-
|
1161
|
+
rb_yield(INT2FIX(c & 0xff));
|
1137
1162
|
}
|
1138
1163
|
return obj;
|
1139
1164
|
}
|
@@ -1147,14 +1172,14 @@ bz_reader_unused(obj)
|
|
1147
1172
|
|
1148
1173
|
Get_BZ2(obj, bzf);
|
1149
1174
|
if (!bzf->in || bzf->state != BZ_STREAM_END) {
|
1150
|
-
|
1175
|
+
return Qnil;
|
1151
1176
|
}
|
1152
1177
|
if (bzf->bzs.avail_in) {
|
1153
|
-
|
1154
|
-
|
1178
|
+
res = rb_tainted_str_new(bzf->bzs.next_in, bzf->bzs.avail_in);
|
1179
|
+
bzf->bzs.avail_in = 0;
|
1155
1180
|
}
|
1156
1181
|
else {
|
1157
|
-
|
1182
|
+
res = rb_tainted_str_new(0, 0);
|
1158
1183
|
}
|
1159
1184
|
return res;
|
1160
1185
|
}
|
@@ -1167,10 +1192,10 @@ bz_reader_eoz(obj)
|
|
1167
1192
|
|
1168
1193
|
Get_BZ2(obj, bzf);
|
1169
1194
|
if (!bzf->in || !bzf->buf) {
|
1170
|
-
|
1195
|
+
return Qnil;
|
1171
1196
|
}
|
1172
1197
|
if (bzf->state == BZ_STREAM_END && !bzf->bzs.avail_out) {
|
1173
|
-
|
1198
|
+
return Qtrue;
|
1174
1199
|
}
|
1175
1200
|
return Qfalse;
|
1176
1201
|
}
|
@@ -1184,20 +1209,20 @@ bz_reader_eof(obj)
|
|
1184
1209
|
|
1185
1210
|
res = bz_reader_eoz(obj);
|
1186
1211
|
if (RTEST(res)) {
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1212
|
+
Get_BZ2(obj, bzf);
|
1213
|
+
if (bzf->bzs.avail_in) {
|
1214
|
+
res = Qfalse;
|
1215
|
+
}
|
1216
|
+
else {
|
1217
|
+
res = bz_reader_getc(obj);
|
1218
|
+
if (NIL_P(res)) {
|
1219
|
+
res = Qtrue;
|
1220
|
+
}
|
1221
|
+
else {
|
1222
|
+
bz_reader_ungetc(res);
|
1223
|
+
res = Qfalse;
|
1224
|
+
}
|
1225
|
+
}
|
1201
1226
|
}
|
1202
1227
|
return res;
|
1203
1228
|
}
|
@@ -1221,27 +1246,27 @@ bz_reader_close(obj)
|
|
1221
1246
|
|
1222
1247
|
Get_BZ2(obj, bzf);
|
1223
1248
|
if (bzf->buf) {
|
1224
|
-
|
1225
|
-
|
1249
|
+
free(bzf->buf);
|
1250
|
+
bzf->buf = 0;
|
1226
1251
|
}
|
1227
1252
|
if (bzf->state == BZ_OK) {
|
1228
|
-
|
1253
|
+
BZ2_bzDecompressEnd(&(bzf->bzs));
|
1229
1254
|
}
|
1230
1255
|
if (bzf->flags & BZ2_RB_CLOSE) {
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1256
|
+
int closed = 0;
|
1257
|
+
if (rb_respond_to(bzf->io, id_closed)) {
|
1258
|
+
VALUE iv = rb_funcall2(bzf->io, id_closed, 0, 0);
|
1259
|
+
closed = RTEST(iv);
|
1260
|
+
}
|
1261
|
+
if (!closed && rb_respond_to(bzf->io, id_close)) {
|
1262
|
+
rb_funcall2(bzf->io, id_close, 0, 0);
|
1263
|
+
}
|
1239
1264
|
}
|
1240
1265
|
if (bzf->flags & (BZ2_RB_CLOSE|BZ2_RB_INTERNAL)) {
|
1241
|
-
|
1266
|
+
res = Qnil;
|
1242
1267
|
}
|
1243
1268
|
else {
|
1244
|
-
|
1269
|
+
res = bzf->io;
|
1245
1270
|
}
|
1246
1271
|
bzf->io = 0;
|
1247
1272
|
return res;
|
@@ -1255,8 +1280,8 @@ bz_reader_finish(obj)
|
|
1255
1280
|
|
1256
1281
|
Get_BZ2(obj, bzf);
|
1257
1282
|
if (bzf->buf) {
|
1258
|
-
|
1259
|
-
|
1283
|
+
rb_funcall2(obj, id_read, 0, 0);
|
1284
|
+
free(bzf->buf);
|
1260
1285
|
}
|
1261
1286
|
bzf->buf = 0;
|
1262
1287
|
bzf->state = BZ_OK;
|
@@ -1274,12 +1299,12 @@ bz_reader_close_bang(obj)
|
|
1274
1299
|
closed = bzf->flags & (BZ2_RB_CLOSE|BZ2_RB_INTERNAL);
|
1275
1300
|
bz_reader_close(obj);
|
1276
1301
|
if (!closed && rb_respond_to(bzf->io, id_close)) {
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1302
|
+
if (rb_respond_to(bzf->io, id_closed)) {
|
1303
|
+
closed = RTEST(rb_funcall2(bzf->io, id_closed, 0, 0));
|
1304
|
+
}
|
1305
|
+
if (!closed) {
|
1306
|
+
rb_funcall2(bzf->io, id_close, 0, 0);
|
1307
|
+
}
|
1283
1308
|
}
|
1284
1309
|
return Qnil;
|
1285
1310
|
}
|
@@ -1299,9 +1324,9 @@ bz_reader_foreach_line(arg)
|
|
1299
1324
|
|
1300
1325
|
in = Qtrue;
|
1301
1326
|
while (!NIL_P(str = bz_reader_gets_internal(arg->argc, &arg->sep,
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1327
|
+
arg->obj, td, in))) {
|
1328
|
+
in = Qfalse;
|
1329
|
+
rb_yield(str);
|
1305
1330
|
}
|
1306
1331
|
return Qnil;
|
1307
1332
|
}
|
@@ -1316,7 +1341,7 @@ bz_reader_s_foreach(argc, argv, obj)
|
|
1316
1341
|
struct bz_file *bzf;
|
1317
1342
|
|
1318
1343
|
if (!rb_block_given_p()) {
|
1319
|
-
|
1344
|
+
rb_raise(rb_eArgError, "call out of a block");
|
1320
1345
|
}
|
1321
1346
|
rb_scan_args(argc, argv, "11", &fname, &sep);
|
1322
1347
|
Check_SafeStr(fname);
|
@@ -1340,9 +1365,9 @@ bz_reader_i_readlines(arg)
|
|
1340
1365
|
in = Qtrue;
|
1341
1366
|
res = rb_ary_new();
|
1342
1367
|
while (!NIL_P(str = bz_reader_gets_internal(arg->argc, &arg->sep,
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1368
|
+
arg->obj, td, in))) {
|
1369
|
+
in = Qfalse;
|
1370
|
+
rb_ary_push(res, str);
|
1346
1371
|
}
|
1347
1372
|
return res;
|
1348
1373
|
}
|
@@ -1411,25 +1436,25 @@ bz_str_read(argc, argv, obj)
|
|
1411
1436
|
Data_Get_Struct(obj, struct bz_str, bzs);
|
1412
1437
|
rb_scan_args(argc, argv, "01", &len);
|
1413
1438
|
if (NIL_P(len)) {
|
1414
|
-
|
1439
|
+
count = RSTRING_LEN(bzs->str);
|
1415
1440
|
}
|
1416
1441
|
else {
|
1417
|
-
|
1418
|
-
|
1419
|
-
|
1420
|
-
|
1442
|
+
count = NUM2INT(len);
|
1443
|
+
if (count < 0) {
|
1444
|
+
rb_raise(rb_eArgError, "negative length %d given", count);
|
1445
|
+
}
|
1421
1446
|
}
|
1422
1447
|
if (!count || bzs->pos == -1) {
|
1423
|
-
|
1448
|
+
return Qnil;
|
1424
1449
|
}
|
1425
|
-
if ((bzs->pos + count) >=
|
1426
|
-
|
1427
|
-
|
1428
|
-
|
1450
|
+
if ((bzs->pos + count) >= RSTRING_LEN(bzs->str)) {
|
1451
|
+
res = rb_str_new(RSTRING_PTR(bzs->str) + bzs->pos,
|
1452
|
+
RSTRING_LEN(bzs->str) - bzs->pos);
|
1453
|
+
bzs->pos = -1;
|
1429
1454
|
}
|
1430
1455
|
else {
|
1431
|
-
|
1432
|
-
|
1456
|
+
res = rb_str_new(RSTRING_PTR(bzs->str) + bzs->pos, count);
|
1457
|
+
bzs->pos += count;
|
1433
1458
|
}
|
1434
1459
|
return res;
|
1435
1460
|
}
|
@@ -1442,7 +1467,7 @@ bz_uncompress(argc, argv, obj)
|
|
1442
1467
|
VALUE bz2, nilv = Qnil;
|
1443
1468
|
|
1444
1469
|
if (!argc) {
|
1445
|
-
|
1470
|
+
rb_raise(rb_eArgError, "need a String to Uncompress");
|
1446
1471
|
}
|
1447
1472
|
argv[0] = rb_str_to_str(argv[0]);
|
1448
1473
|
bz2 = rb_funcall2(bz_cReader, id_new, argc, argv);
|
@@ -1471,19 +1496,19 @@ bz_proc_new(func, val)
|
|
1471
1496
|
rb_intern("to_proc"), 0, 0);
|
1472
1497
|
}
|
1473
1498
|
|
1474
|
-
void
|
1499
|
+
void Init_bzip2_ext()
|
1475
1500
|
{
|
1476
1501
|
VALUE bz_mBzip2;
|
1477
1502
|
|
1478
1503
|
if (rb_const_defined_at(rb_cObject, rb_intern("Bzip2"))) {
|
1479
|
-
|
1504
|
+
rb_raise(rb_eNameError, "module already defined");
|
1480
1505
|
}
|
1481
1506
|
|
1482
1507
|
bz_internal_ary = rb_ary_new();
|
1483
1508
|
rb_global_variable(&bz_internal_ary);
|
1484
1509
|
rb_funcall(rb_const_get(rb_cObject, rb_intern("ObjectSpace")),
|
1485
|
-
|
1486
|
-
|
1510
|
+
rb_intern("define_finalizer"), 2, bz_internal_ary,
|
1511
|
+
bz_proc_new(bz_internal_finalize, 0));
|
1487
1512
|
|
1488
1513
|
id_new = rb_intern("new");
|
1489
1514
|
id_write = rb_intern("write");
|