rubysl-zlib 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39b27bbc346365ead0ddf563ea56a9372313cc9c
4
- data.tar.gz: 7bf396dc0987da9d8382a8d66f34ac5a2c78e48e
3
+ metadata.gz: 5c8834ce05136d4bccc980b8340f08ffdc55b2b8
4
+ data.tar.gz: 89e1a54194e94a79c962cf8e51d97375cc09fcce
5
5
  SHA512:
6
- metadata.gz: 7fb7a56fb838a7df3e2224f9991c560adbff1d53029cf2f7f040453fcb0e26ef8d581164e71f0ce403c2bbd5c3b98cb74fbd4ae299013b3580c2b87c7fc22cbc
7
- data.tar.gz: 46eb5d53032843d1ef59bed5c29bc225151bc871cd88c88ad9c0c73de48ab0e0246fd8e5918d2cfef75429039c29bde0b6d9cbe18a1e0553e3ad2ebcf875e578
6
+ metadata.gz: 89d136000290cdc485aba19fa7608df7ca633502d397c53bf5f1d21dfd4958e0d5cb89d40a746d72b0b9ce45673d866c33c993d44754f7245bc66dbc0c4ad222
7
+ data.tar.gz: 9cfc9b259bc9d8ad304457cda99e27c628dc78bf79ceb7268828c6f6adb3741f2f5c97b354dff6d7d755a0929808b6615684184f5e28b0524a55f70518fb17b9
data/.travis.yml CHANGED
@@ -1,8 +1,7 @@
1
1
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem --version
5
- - gem install rubysl-bundler
6
- script: bundle exec mspec spec
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
7
5
  rvm:
8
- - rbx-nightly-18mode
6
+ - 1.9.3
7
+ - rbx-nightly-19mode
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # extconf.rb
3
3
  #
4
- # $Id: extconf.rb 16679 2008-05-29 11:23:36Z knu $
4
+ # $Id: extconf.rb 26353 2010-01-19 05:14:29Z usa $
5
5
  #
6
6
 
7
7
  require 'mkmf'
@@ -58,6 +58,8 @@ if %w'z libz zlib1 zlib zdll'.find {|z| have_library(z, 'deflateReset')} and
58
58
 
59
59
  $defs.concat(defines.collect{|d|' -D'+d})
60
60
 
61
+ have_func('crc32_combine', 'zlib.h')
62
+ have_func('adler32_combine', 'zlib.h')
61
63
  have_type('z_crc_t', 'zlib.h')
62
64
 
63
65
  create_makefile('zlib/zlib')
@@ -3,7 +3,7 @@
3
3
  *
4
4
  * Copyright (C) UENO Katsuhiro 2000-2003
5
5
  *
6
- * $Id: zlib.c 31714 2011-05-23 04:49:42Z shyouhei $
6
+ * $Id: zlib.c 34544 2012-02-10 18:37:45Z naruse $
7
7
  */
8
8
 
9
9
  #define RSTRING_NOT_MODIFIED 1
@@ -11,6 +11,20 @@
11
11
  #include <ruby.h>
12
12
  #include <zlib.h>
13
13
  #include <time.h>
14
+ #include <ruby/io.h>
15
+
16
+ #ifdef HAVE_VALGRIND_MEMCHECK_H
17
+ # include <valgrind/memcheck.h>
18
+ # ifndef VALGRIND_MAKE_MEM_DEFINED
19
+ # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
20
+ # endif
21
+ # ifndef VALGRIND_MAKE_MEM_UNDEFINED
22
+ # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
23
+ # endif
24
+ #else
25
+ # define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */
26
+ # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */
27
+ #endif
14
28
 
15
29
  #define RUBY_ZLIB_VERSION "0.6.0"
16
30
 
@@ -30,166 +44,217 @@
30
44
  #endif
31
45
  #endif
32
46
 
47
+ #if SIZEOF_LONG > SIZEOF_INT
48
+ static inline uInt
49
+ max_uint(long n)
50
+ {
51
+ if (n > UINT_MAX) n = UINT_MAX;
52
+ return (uInt)n;
53
+ }
54
+ #define MAX_UINT(n) max_uint(n)
55
+ #else
56
+ #define MAX_UINT(n) (uInt)(n)
57
+ #endif
58
+
59
+ #define sizeof(x) ((int)sizeof(x))
33
60
 
34
61
  /*--------- Prototypes --------*/
35
62
 
36
- static NORETURN(void raise_zlib_error _((int, const char *)));
37
- static VALUE rb_zlib_version _((VALUE));
38
- static VALUE do_checksum _((int, VALUE*, uLong (*) _((uLong, const Bytef*, uInt))));
39
- static VALUE rb_zlib_adler32 _((int, VALUE*, VALUE));
40
- static VALUE rb_zlib_crc32 _((int, VALUE*, VALUE));
41
- static VALUE rb_zlib_crc_table _((VALUE));
42
- static voidpf zlib_mem_alloc _((voidpf, uInt, uInt));
43
- static void zlib_mem_free _((voidpf, voidpf));
44
- static void finalizer_warn _((const char*));
63
+ static NORETURN(void raise_zlib_error(int, const char*));
64
+ static VALUE rb_zlib_version(VALUE);
65
+ static VALUE do_checksum(int, VALUE*, uLong (*)(uLong, const Bytef*, uInt));
66
+ static VALUE rb_zlib_adler32(int, VALUE*, VALUE);
67
+ static VALUE rb_zlib_crc32(int, VALUE*, VALUE);
68
+ static VALUE rb_zlib_crc_table(VALUE);
69
+ static voidpf zlib_mem_alloc(voidpf, uInt, uInt);
70
+ static void zlib_mem_free(voidpf, voidpf);
71
+ static void finalizer_warn(const char*);
45
72
 
46
73
  struct zstream;
47
74
  struct zstream_funcs;
48
- static void zstream_init _((struct zstream*, const struct zstream_funcs *));
49
- static void zstream_expand_buffer _((struct zstream*));
50
- static void zstream_expand_buffer_into _((struct zstream*, int));
51
- static void zstream_append_buffer _((struct zstream*, const char*, int));
52
- static VALUE zstream_detach_buffer _((struct zstream*));
53
- static VALUE zstream_shift_buffer _((struct zstream*, int));
54
- static void zstream_buffer_ungetc _((struct zstream*, int));
55
- static void zstream_append_input _((struct zstream*, const char*, unsigned int));
56
- static void zstream_discard_input _((struct zstream*, unsigned int));
57
- static void zstream_reset_input _((struct zstream*));
58
- static void zstream_passthrough_input _((struct zstream*));
59
- static VALUE zstream_detach_input _((struct zstream*));
60
- static void zstream_reset _((struct zstream*));
61
- static VALUE zstream_end _((struct zstream*));
62
- static void zstream_run _((struct zstream*, Bytef*, uInt, int));
63
- static VALUE zstream_sync _((struct zstream*, Bytef*, uInt));
64
- static void zstream_mark _((struct zstream*));
65
- static void zstream_free _((struct zstream*));
66
- static VALUE zstream_new _((VALUE, const struct zstream_funcs*));
67
- static struct zstream *get_zstream _((VALUE));
68
- static void zstream_finalize _((struct zstream*));
69
-
70
- static VALUE rb_zstream_end _((VALUE));
71
- static VALUE rb_zstream_reset _((VALUE));
72
- static VALUE rb_zstream_finish _((VALUE));
73
- static VALUE rb_zstream_flush_next_in _((VALUE));
74
- static VALUE rb_zstream_flush_next_out _((VALUE));
75
- static VALUE rb_zstream_avail_out _((VALUE));
76
- static VALUE rb_zstream_set_avail_out _((VALUE, VALUE));
77
- static VALUE rb_zstream_avail_in _((VALUE));
78
- static VALUE rb_zstream_total_in _((VALUE));
79
- static VALUE rb_zstream_total_out _((VALUE));
80
- static VALUE rb_zstream_data_type _((VALUE));
81
- static VALUE rb_zstream_adler _((VALUE));
82
- static VALUE rb_zstream_finished_p _((VALUE));
83
- static VALUE rb_zstream_closed_p _((VALUE));
84
-
85
- static VALUE rb_deflate_s_allocate _((VALUE));
86
- static VALUE rb_deflate_initialize _((int, VALUE*, VALUE));
87
- static VALUE rb_deflate_init_copy _((VALUE, VALUE));
88
- static VALUE deflate_run _((VALUE));
89
- static VALUE rb_deflate_s_deflate _((int, VALUE*, VALUE));
90
- static void do_deflate _((struct zstream*, VALUE, int));
91
- static VALUE rb_deflate_deflate _((int, VALUE*, VALUE));
92
- static VALUE rb_deflate_addstr _((VALUE, VALUE));
93
- static VALUE rb_deflate_flush _((int, VALUE*, VALUE));
94
- static VALUE rb_deflate_params _((VALUE, VALUE, VALUE));
95
- static VALUE rb_deflate_set_dictionary _((VALUE, VALUE));
96
-
97
- static VALUE inflate_run _((VALUE));
98
- static VALUE rb_inflate_s_allocate _((VALUE));
99
- static VALUE rb_inflate_initialize _((int, VALUE*, VALUE));
100
- static VALUE rb_inflate_s_inflate _((VALUE, VALUE));
101
- static void do_inflate _((struct zstream*, VALUE));
102
- static VALUE rb_inflate_inflate _((VALUE, VALUE));
103
- static VALUE rb_inflate_addstr _((VALUE, VALUE));
104
- static VALUE rb_inflate_sync _((VALUE, VALUE));
105
- static VALUE rb_inflate_sync_point_p _((VALUE));
106
- static VALUE rb_inflate_set_dictionary _((VALUE, VALUE));
75
+ static void zstream_init(struct zstream*, const struct zstream_funcs*);
76
+ static void zstream_expand_buffer(struct zstream*);
77
+ static void zstream_expand_buffer_into(struct zstream*, unsigned long);
78
+ static void zstream_append_buffer(struct zstream*, const Bytef*, long);
79
+ static VALUE zstream_detach_buffer(struct zstream*);
80
+ static VALUE zstream_shift_buffer(struct zstream*, long);
81
+ static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long);
82
+ static void zstream_buffer_ungetbyte(struct zstream*, int);
83
+ static void zstream_append_input(struct zstream*, const Bytef*, long);
84
+ static void zstream_discard_input(struct zstream*, long);
85
+ static void zstream_reset_input(struct zstream*);
86
+ static void zstream_passthrough_input(struct zstream*);
87
+ static VALUE zstream_detach_input(struct zstream*);
88
+ static void zstream_reset(struct zstream*);
89
+ static VALUE zstream_end(struct zstream*);
90
+ static void zstream_run(struct zstream*, Bytef*, long, int);
91
+ static VALUE zstream_sync(struct zstream*, Bytef*, long);
92
+ static void zstream_mark(struct zstream*);
93
+ static void zstream_free(struct zstream*);
94
+ static VALUE zstream_new(VALUE, const struct zstream_funcs*);
95
+ static struct zstream *get_zstream(VALUE);
96
+ static void zstream_finalize(struct zstream*);
97
+
98
+ static VALUE rb_zstream_end(VALUE);
99
+ static VALUE rb_zstream_reset(VALUE);
100
+ static VALUE rb_zstream_finish(VALUE);
101
+ static VALUE rb_zstream_flush_next_in(VALUE);
102
+ static VALUE rb_zstream_flush_next_out(VALUE);
103
+ static VALUE rb_zstream_avail_out(VALUE);
104
+ static VALUE rb_zstream_set_avail_out(VALUE, VALUE);
105
+ static VALUE rb_zstream_avail_in(VALUE);
106
+ static VALUE rb_zstream_total_in(VALUE);
107
+ static VALUE rb_zstream_total_out(VALUE);
108
+ static VALUE rb_zstream_data_type(VALUE);
109
+ static VALUE rb_zstream_adler(VALUE);
110
+ static VALUE rb_zstream_finished_p(VALUE);
111
+ static VALUE rb_zstream_closed_p(VALUE);
112
+
113
+ static VALUE rb_deflate_s_allocate(VALUE);
114
+ static VALUE rb_deflate_initialize(int, VALUE*, VALUE);
115
+ static VALUE rb_deflate_init_copy(VALUE, VALUE);
116
+ static VALUE deflate_run(VALUE);
117
+ static VALUE rb_deflate_s_deflate(int, VALUE*, VALUE);
118
+ static void do_deflate(struct zstream*, VALUE, int);
119
+ static VALUE rb_deflate_deflate(int, VALUE*, VALUE);
120
+ static VALUE rb_deflate_addstr(VALUE, VALUE);
121
+ static VALUE rb_deflate_flush(int, VALUE*, VALUE);
122
+ static VALUE rb_deflate_params(VALUE, VALUE, VALUE);
123
+ static VALUE rb_deflate_set_dictionary(VALUE, VALUE);
124
+
125
+ static VALUE inflate_run(VALUE);
126
+ static VALUE rb_inflate_s_allocate(VALUE);
127
+ static VALUE rb_inflate_initialize(int, VALUE*, VALUE);
128
+ static VALUE rb_inflate_s_inflate(VALUE, VALUE);
129
+ static void do_inflate(struct zstream*, VALUE);
130
+ static VALUE rb_inflate_inflate(VALUE, VALUE);
131
+ static VALUE rb_inflate_addstr(VALUE, VALUE);
132
+ static VALUE rb_inflate_sync(VALUE, VALUE);
133
+ static VALUE rb_inflate_sync_point_p(VALUE);
134
+ static VALUE rb_inflate_set_dictionary(VALUE, VALUE);
107
135
 
108
136
  #if GZIP_SUPPORT
109
137
  struct gzfile;
110
- static void gzfile_mark _((struct gzfile*));
111
- static void gzfile_free _((struct gzfile*));
112
- static VALUE gzfile_new _((VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*))));
113
- static void gzfile_reset _((struct gzfile*));
114
- static void gzfile_close _((struct gzfile*, int));
115
- static void gzfile_write_raw _((struct gzfile*));
116
- static VALUE gzfile_read_raw _((struct gzfile*));
117
- static int gzfile_read_raw_ensure _((struct gzfile*, int));
118
- static char *gzfile_read_raw_until_zero _((struct gzfile*, long));
119
- static unsigned int gzfile_get16 _((const unsigned char*));
120
- static unsigned long gzfile_get32 _((const unsigned char*));
121
- static void gzfile_set32 _((unsigned long n, unsigned char*));
122
- static void gzfile_make_header _((struct gzfile*));
123
- static void gzfile_make_footer _((struct gzfile*));
124
- static void gzfile_read_header _((struct gzfile*));
125
- static void gzfile_check_footer _((struct gzfile*));
126
- static void gzfile_write _((struct gzfile*, Bytef*, uInt));
127
- static long gzfile_read_more _((struct gzfile*));
128
- static void gzfile_calc_crc _((struct gzfile*, VALUE));
129
- static VALUE gzfile_read _((struct gzfile*, int));
130
- static VALUE gzfile_read_all _((struct gzfile*));
131
- static void gzfile_ungetc _((struct gzfile*, int));
132
- static VALUE gzfile_writer_end_run _((VALUE));
133
- static void gzfile_writer_end _((struct gzfile*));
134
- static VALUE gzfile_reader_end_run _((VALUE));
135
- static void gzfile_reader_end _((struct gzfile*));
136
- static void gzfile_reader_rewind _((struct gzfile*));
137
- static VALUE gzfile_reader_get_unused _((struct gzfile*));
138
- static struct gzfile *get_gzfile _((VALUE));
139
- static VALUE gzfile_ensure_close _((VALUE));
140
- static VALUE rb_gzfile_s_wrap _((int, VALUE*, VALUE));
141
- static VALUE gzfile_s_open _((int, VALUE*, VALUE, const char*));
142
-
143
- static VALUE rb_gzfile_to_io _((VALUE));
144
- static VALUE rb_gzfile_crc _((VALUE));
145
- static VALUE rb_gzfile_mtime _((VALUE));
146
- static VALUE rb_gzfile_level _((VALUE));
147
- static VALUE rb_gzfile_os_code _((VALUE));
148
- static VALUE rb_gzfile_orig_name _((VALUE));
149
- static VALUE rb_gzfile_comment _((VALUE));
150
- static VALUE rb_gzfile_lineno _((VALUE));
151
- static VALUE rb_gzfile_set_lineno _((VALUE, VALUE));
152
- static VALUE rb_gzfile_set_mtime _((VALUE, VALUE));
153
- static VALUE rb_gzfile_set_orig_name _((VALUE, VALUE));
154
- static VALUE rb_gzfile_set_comment _((VALUE, VALUE));
155
- static VALUE rb_gzfile_close _((VALUE));
156
- static VALUE rb_gzfile_finish _((VALUE));
157
- static VALUE rb_gzfile_closed_p _((VALUE));
158
- static VALUE rb_gzfile_eof_p _((VALUE));
159
- static VALUE rb_gzfile_sync _((VALUE));
160
- static VALUE rb_gzfile_set_sync _((VALUE, VALUE));
161
- static VALUE rb_gzfile_total_in _((VALUE));
162
- static VALUE rb_gzfile_total_out _((VALUE));
163
-
164
- static VALUE rb_gzwriter_s_allocate _((VALUE));
165
- static VALUE rb_gzwriter_s_open _((int, VALUE*, VALUE));
166
- static VALUE rb_gzwriter_initialize _((int, VALUE*, VALUE));
167
- static VALUE rb_gzwriter_flush _((int, VALUE*, VALUE));
168
- static VALUE rb_gzwriter_write _((VALUE, VALUE));
169
- static VALUE rb_gzwriter_putc _((VALUE, VALUE));
170
-
171
- static VALUE rb_gzreader_s_allocate _((VALUE));
172
- static VALUE rb_gzreader_s_open _((int, VALUE*, VALUE));
173
- static VALUE rb_gzreader_initialize _((VALUE, VALUE));
174
- static VALUE rb_gzreader_rewind _((VALUE));
175
- static VALUE rb_gzreader_unused _((VALUE));
176
- static VALUE rb_gzreader_read _((int, VALUE*, VALUE));
177
- static VALUE rb_gzreader_getc _((VALUE));
178
- static VALUE rb_gzreader_readchar _((VALUE));
179
- static VALUE rb_gzreader_each_byte _((VALUE));
180
- static VALUE rb_gzreader_ungetc _((VALUE, VALUE));
181
- static void gzreader_skip_linebreaks _((struct gzfile*));
182
- static VALUE gzreader_gets _((int, VALUE*, VALUE));
183
- static VALUE rb_gzreader_gets _((int, VALUE*, VALUE));
184
- static VALUE rb_gzreader_readline _((int, VALUE*, VALUE));
185
- static VALUE rb_gzreader_each _((int, VALUE*, VALUE));
186
- static VALUE rb_gzreader_readlines _((int, VALUE*, VALUE));
138
+ static void gzfile_mark(struct gzfile*);
139
+ static void gzfile_free(struct gzfile*);
140
+ static VALUE gzfile_new(VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*)));
141
+ static void gzfile_reset(struct gzfile*);
142
+ static void gzfile_close(struct gzfile*, int);
143
+ static void gzfile_write_raw(struct gzfile*);
144
+ static VALUE gzfile_read_raw_partial(VALUE);
145
+ static VALUE gzfile_read_raw_rescue(VALUE);
146
+ static VALUE gzfile_read_raw(struct gzfile*);
147
+ static int gzfile_read_raw_ensure(struct gzfile*, long);
148
+ static char *gzfile_read_raw_until_zero(struct gzfile*, long);
149
+ static unsigned int gzfile_get16(const unsigned char*);
150
+ static unsigned long gzfile_get32(const unsigned char*);
151
+ static void gzfile_set32(unsigned long n, unsigned char*);
152
+ static void gzfile_make_header(struct gzfile*);
153
+ static void gzfile_make_footer(struct gzfile*);
154
+ static void gzfile_read_header(struct gzfile*);
155
+ static void gzfile_check_footer(struct gzfile*);
156
+ static void gzfile_write(struct gzfile*, Bytef*, long);
157
+ static long gzfile_read_more(struct gzfile*);
158
+ static void gzfile_calc_crc(struct gzfile*, VALUE);
159
+ static VALUE gzfile_read(struct gzfile*, long);
160
+ static VALUE gzfile_read_all(struct gzfile*);
161
+ static void gzfile_ungets(struct gzfile*, const Bytef*, long);
162
+ static void gzfile_ungetbyte(struct gzfile*, int);
163
+ static VALUE gzfile_writer_end_run(VALUE);
164
+ static void gzfile_writer_end(struct gzfile*);
165
+ static VALUE gzfile_reader_end_run(VALUE);
166
+ static void gzfile_reader_end(struct gzfile*);
167
+ static void gzfile_reader_rewind(struct gzfile*);
168
+ static VALUE gzfile_reader_get_unused(struct gzfile*);
169
+ static struct gzfile *get_gzfile(VALUE);
170
+ static VALUE gzfile_ensure_close(VALUE);
171
+ static VALUE rb_gzfile_s_wrap(int, VALUE*, VALUE);
172
+ static VALUE gzfile_s_open(int, VALUE*, VALUE, const char*);
173
+ NORETURN(static void gzfile_raise(struct gzfile *, VALUE, const char *));
174
+ static VALUE gzfile_error_inspect(VALUE);
175
+
176
+ static VALUE rb_gzfile_to_io(VALUE);
177
+ static VALUE rb_gzfile_crc(VALUE);
178
+ static VALUE rb_gzfile_mtime(VALUE);
179
+ static VALUE rb_gzfile_level(VALUE);
180
+ static VALUE rb_gzfile_os_code(VALUE);
181
+ static VALUE rb_gzfile_orig_name(VALUE);
182
+ static VALUE rb_gzfile_comment(VALUE);
183
+ static VALUE rb_gzfile_lineno(VALUE);
184
+ static VALUE rb_gzfile_set_lineno(VALUE, VALUE);
185
+ static VALUE rb_gzfile_set_mtime(VALUE, VALUE);
186
+ static VALUE rb_gzfile_set_orig_name(VALUE, VALUE);
187
+ static VALUE rb_gzfile_set_comment(VALUE, VALUE);
188
+ static VALUE rb_gzfile_close(VALUE);
189
+ static VALUE rb_gzfile_finish(VALUE);
190
+ static VALUE rb_gzfile_closed_p(VALUE);
191
+ static VALUE rb_gzfile_eof_p(VALUE);
192
+ static VALUE rb_gzfile_sync(VALUE);
193
+ static VALUE rb_gzfile_set_sync(VALUE, VALUE);
194
+ static VALUE rb_gzfile_total_in(VALUE);
195
+ static VALUE rb_gzfile_total_out(VALUE);
196
+ static VALUE rb_gzfile_path(VALUE);
197
+
198
+ static VALUE rb_gzwriter_s_allocate(VALUE);
199
+ static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE);
200
+ static VALUE rb_gzwriter_initialize(int, VALUE*, VALUE);
201
+ static VALUE rb_gzwriter_flush(int, VALUE*, VALUE);
202
+ static VALUE rb_gzwriter_write(VALUE, VALUE);
203
+ static VALUE rb_gzwriter_putc(VALUE, VALUE);
204
+
205
+ static VALUE rb_gzreader_s_allocate(VALUE);
206
+ static VALUE rb_gzreader_s_open(int, VALUE*, VALUE);
207
+ static VALUE rb_gzreader_initialize(int, VALUE*, VALUE);
208
+ static VALUE rb_gzreader_rewind(VALUE);
209
+ static VALUE rb_gzreader_unused(VALUE);
210
+ static VALUE rb_gzreader_read(int, VALUE*, VALUE);
211
+ static VALUE rb_gzreader_getc(VALUE);
212
+ static VALUE rb_gzreader_readchar(VALUE);
213
+ static VALUE rb_gzreader_each_byte(VALUE);
214
+ static VALUE rb_gzreader_ungetc(VALUE, VALUE);
215
+ static VALUE rb_gzreader_ungetbyte(VALUE, VALUE);
216
+ static void gzreader_skip_linebreaks(struct gzfile*);
217
+ static VALUE gzreader_gets(int, VALUE*, VALUE);
218
+ static VALUE rb_gzreader_gets(int, VALUE*, VALUE);
219
+ static VALUE rb_gzreader_readline(int, VALUE*, VALUE);
220
+ static VALUE rb_gzreader_each(int, VALUE*, VALUE);
221
+ static VALUE rb_gzreader_readlines(int, VALUE*, VALUE);
187
222
  #endif /* GZIP_SUPPORT */
188
223
 
189
-
190
- void Init_zlib _((void));
191
-
192
-
224
+ /*
225
+ * Document-module: Zlib
226
+ *
227
+ * == Overview
228
+ *
229
+ * Access to the zlib library.
230
+ *
231
+ * == Class tree
232
+ *
233
+ * - Zlib::Deflate
234
+ * - Zlib::Inflate
235
+ * - Zlib::ZStream
236
+ * - Zlib::Error
237
+ * - Zlib::StreamEnd
238
+ * - Zlib::NeedDict
239
+ * - Zlib::DataError
240
+ * - Zlib::StreamError
241
+ * - Zlib::MemError
242
+ * - Zlib::BufError
243
+ * - Zlib::VersionError
244
+ *
245
+ * (if you have GZIP_SUPPORT)
246
+ * - Zlib::GzipReader
247
+ * - Zlib::GzipWriter
248
+ * - Zlib::GzipFile
249
+ * - Zlib::GzipFile::Error
250
+ * - Zlib::GzipFile::LengthError
251
+ * - Zlib::GzipFile::CRCError
252
+ * - Zlib::GzipFile::NoFooter
253
+ *
254
+ * see also zlib.h
255
+ *
256
+ */
257
+ void Init_zlib(void);
193
258
 
194
259
  /*--------- Exceptions --------*/
195
260
 
@@ -197,9 +262,7 @@ static VALUE cZError, cStreamEnd, cNeedDict;
197
262
  static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError;
198
263
 
199
264
  static void
200
- raise_zlib_error(err, msg)
201
- int err;
202
- const char *msg;
265
+ raise_zlib_error(int err, const char *msg)
203
266
  {
204
267
  VALUE exc;
205
268
 
@@ -247,8 +310,7 @@ raise_zlib_error(err, msg)
247
310
  /*--- Warning (in finalizer) ---*/
248
311
 
249
312
  static void
250
- finalizer_warn(msg)
251
- const char *msg;
313
+ finalizer_warn(const char *msg)
252
314
  {
253
315
  fprintf(stderr, "zlib(finalizer): %s\n", msg);
254
316
  }
@@ -257,11 +319,12 @@ finalizer_warn(msg)
257
319
  /*-------- module Zlib --------*/
258
320
 
259
321
  /*
322
+ * Document-method: Zlib.zlib_version
323
+ *
260
324
  * Returns the string which represents the version of zlib library.
261
325
  */
262
326
  static VALUE
263
- rb_zlib_version(klass)
264
- VALUE klass;
327
+ rb_zlib_version(VALUE klass)
265
328
  {
266
329
  VALUE str;
267
330
 
@@ -270,11 +333,29 @@ rb_zlib_version(klass)
270
333
  return str;
271
334
  }
272
335
 
336
+ #if SIZEOF_LONG > SIZEOF_INT
337
+ static uLong
338
+ checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len)
339
+ {
340
+ if (len > UINT_MAX) {
341
+ do {
342
+ sum = func(sum, ptr, UINT_MAX);
343
+ ptr += UINT_MAX;
344
+ len -= UINT_MAX;
345
+ } while (len >= UINT_MAX);
346
+ }
347
+ if (len > 0) sum = func(sum, ptr, (uInt)len);
348
+ return sum;
349
+ }
350
+ #else
351
+ #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
352
+ #endif
353
+
273
354
  static VALUE
274
355
  do_checksum(argc, argv, func)
275
356
  int argc;
276
357
  VALUE *argv;
277
- uLong (*func) _((uLong, const Bytef *, uInt));
358
+ uLong (*func)(uLong, const Bytef*, uInt);
278
359
  {
279
360
  VALUE str, vsum;
280
361
  unsigned long sum;
@@ -296,12 +377,14 @@ do_checksum(argc, argv, func)
296
377
  }
297
378
  else {
298
379
  StringValue(str);
299
- sum = func(sum, RSTRING(str)->ptr, RSTRING(str)->len);
380
+ sum = checksum_long(func, sum, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
300
381
  }
301
382
  return rb_uint2inum(sum);
302
383
  }
303
384
 
304
385
  /*
386
+ * Document-method: Zlib.adler32
387
+ *
305
388
  * call-seq: Zlib.adler32(string, adler)
306
389
  *
307
390
  * Calculates Adler-32 checksum for +string+, and returns updated value of
@@ -311,15 +394,35 @@ do_checksum(argc, argv, func)
311
394
  * FIXME: expression.
312
395
  */
313
396
  static VALUE
314
- rb_zlib_adler32(argc, argv, klass)
315
- int argc;
316
- VALUE *argv;
317
- VALUE klass;
397
+ rb_zlib_adler32(int argc, VALUE *argv, VALUE klass)
318
398
  {
319
399
  return do_checksum(argc, argv, adler32);
320
400
  }
321
401
 
402
+ #ifdef HAVE_ADLER32_COMBINE
322
403
  /*
404
+ * Document-method: Zlib.adler32_combine
405
+ *
406
+ * call-seq: Zlib.adler32_combine(adler1, adler2, len2)
407
+ *
408
+ * Combine two Adler-32 check values in to one. +alder1+ is the first Adler-32
409
+ * value, +adler2+ is the second Adler-32 value. +len2+ is the length of the
410
+ * string used to generate +adler2+.
411
+ *
412
+ */
413
+ static VALUE
414
+ rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2)
415
+ {
416
+ return ULONG2NUM(
417
+ adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2)));
418
+ }
419
+ #else
420
+ #define rb_zlib_adler32_combine rb_f_notimplement
421
+ #endif
422
+
423
+ /*
424
+ * Document-method: Zlib.crc32
425
+ *
323
426
  * call-seq: Zlib.crc32(string, adler)
324
427
  *
325
428
  * Calculates CRC checksum for +string+, and returns updated value of +crc+. If
@@ -329,20 +432,39 @@ rb_zlib_adler32(argc, argv, klass)
329
432
  * FIXME: expression.
330
433
  */
331
434
  static VALUE
332
- rb_zlib_crc32(argc, argv, klass)
333
- int argc;
334
- VALUE *argv;
335
- VALUE klass;
435
+ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
336
436
  {
337
437
  return do_checksum(argc, argv, crc32);
338
438
  }
339
439
 
440
+ #ifdef HAVE_CRC32_COMBINE
441
+ /*
442
+ * Document-method: Zlib.crc32_combine
443
+ *
444
+ * call-seq: Zlib.crc32_combine(crc1, crc2, len2)
445
+ *
446
+ * Combine two CRC-32 check values in to one. +crc1+ is the first CRC-32
447
+ * value, +crc2+ is the second CRC-32 value. +len2+ is the length of the
448
+ * string used to generate +crc2+.
449
+ *
450
+ */
451
+ static VALUE
452
+ rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
453
+ {
454
+ return ULONG2NUM(
455
+ crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2)));
456
+ }
457
+ #else
458
+ #define rb_zlib_crc32_combine rb_f_notimplement
459
+ #endif
460
+
340
461
  /*
462
+ * Document-method: Zlib.crc_table
463
+ *
341
464
  * Returns the table for calculating CRC checksum as an array.
342
465
  */
343
466
  static VALUE
344
- rb_zlib_crc_table(obj)
345
- VALUE obj;
467
+ rb_zlib_crc_table(VALUE obj)
346
468
  {
347
469
  #if !defined(HAVE_TYPE_Z_CRC_T)
348
470
  /* z_crc_t is defined since zlib-1.2.7. */
@@ -372,9 +494,9 @@ struct zstream {
372
494
  VALUE input;
373
495
  z_stream stream;
374
496
  const struct zstream_funcs {
375
- int (*reset) _((z_streamp));
376
- int (*end) _((z_streamp));
377
- int (*run) _((z_streamp, int));
497
+ int (*reset)(z_streamp);
498
+ int (*end)(z_streamp);
499
+ int (*run)(z_streamp, int);
378
500
  } *func;
379
501
  };
380
502
 
@@ -405,24 +527,25 @@ static const struct zstream_funcs inflate_funcs = {
405
527
 
406
528
 
407
529
  static voidpf
408
- zlib_mem_alloc(opaque, items, size)
409
- voidpf opaque;
410
- uInt items, size;
411
- {
412
- return xmalloc(items * size);
530
+ zlib_mem_alloc(voidpf opaque, uInt items, uInt size)
531
+ {
532
+ voidpf p = xmalloc(items * size);
533
+ /* zlib FAQ: Valgrind (or some similar memory access checker) says that
534
+ deflate is performing a conditional jump that depends on an
535
+ uninitialized value. Isn't that a bug?
536
+ http://www.zlib.net/zlib_faq.html#faq36 */
537
+ VALGRIND_MAKE_MEM_DEFINED(p, items * size);
538
+ return p;
413
539
  }
414
540
 
415
541
  static void
416
- zlib_mem_free(opaque, address)
417
- voidpf opaque, address;
542
+ zlib_mem_free(voidpf opaque, voidpf address)
418
543
  {
419
- free(address);
544
+ xfree(address);
420
545
  }
421
546
 
422
547
  static void
423
- zstream_init(z, func)
424
- struct zstream *z;
425
- const struct zstream_funcs *func;
548
+ zstream_init(struct zstream *z, const struct zstream_funcs *func)
426
549
  {
427
550
  z->flags = 0;
428
551
  z->buf = Qnil;
@@ -443,8 +566,7 @@ zstream_init(z, func)
443
566
  #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
444
567
 
445
568
  static void
446
- zstream_expand_buffer(z)
447
- struct zstream *z;
569
+ zstream_expand_buffer(struct zstream *z)
448
570
  {
449
571
  long inc;
450
572
 
@@ -453,12 +575,12 @@ zstream_expand_buffer(z)
453
575
  rb_str_buf_new makes a zero-length string. */
454
576
  z->buf = rb_str_new(0, ZSTREAM_INITIAL_BUFSIZE);
455
577
  z->buf_filled = 0;
456
- z->stream.next_out = RSTRING(z->buf)->ptr;
578
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
457
579
  z->stream.avail_out = ZSTREAM_INITIAL_BUFSIZE;
458
580
  return;
459
581
  }
460
582
 
461
- if (RSTRING(z->buf)->len - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
583
+ if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
462
584
  /* to keep other threads from freezing */
463
585
  z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
464
586
  }
@@ -469,69 +591,63 @@ zstream_expand_buffer(z)
469
591
  }
470
592
  rb_str_resize(z->buf, z->buf_filled + inc);
471
593
  z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
472
- inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
594
+ (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
473
595
  }
474
- z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
596
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
475
597
  }
476
598
 
477
599
  static void
478
- zstream_expand_buffer_into(z, size)
479
- struct zstream *z;
480
- int size;
600
+ zstream_expand_buffer_into(struct zstream *z, unsigned long size)
481
601
  {
482
602
  if (NIL_P(z->buf)) {
483
603
  /* I uses rb_str_new here not rb_str_buf_new because
484
604
  rb_str_buf_new makes a zero-length string. */
485
605
  z->buf = rb_str_new(0, size);
486
606
  z->buf_filled = 0;
487
- z->stream.next_out = RSTRING(z->buf)->ptr;
488
- z->stream.avail_out = size;
607
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
608
+ z->stream.avail_out = MAX_UINT(size);
489
609
  }
490
610
  else if (z->stream.avail_out != size) {
491
611
  rb_str_resize(z->buf, z->buf_filled + size);
492
- z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
493
- z->stream.avail_out = size;
612
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
613
+ z->stream.avail_out = MAX_UINT(size);
494
614
  }
495
615
  }
496
616
 
497
617
  static void
498
- zstream_append_buffer(z, src, len)
499
- struct zstream *z;
500
- const char *src;
501
- int len;
618
+ zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
502
619
  {
503
620
  if (NIL_P(z->buf)) {
504
621
  z->buf = rb_str_buf_new(len);
505
- rb_str_buf_cat(z->buf, src, len);
622
+ rb_str_buf_cat(z->buf, (const char*)src, len);
506
623
  z->buf_filled = len;
507
- z->stream.next_out = RSTRING(z->buf)->ptr;
624
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
508
625
  z->stream.avail_out = 0;
509
626
  return;
510
627
  }
511
628
 
512
- if (RSTRING(z->buf)->len < z->buf_filled + len) {
629
+ if (RSTRING_LEN(z->buf) < z->buf_filled + len) {
513
630
  rb_str_resize(z->buf, z->buf_filled + len);
514
631
  z->stream.avail_out = 0;
515
632
  }
516
633
  else {
517
- if (z->stream.avail_out >= len) {
518
- z->stream.avail_out -= len;
634
+ if (z->stream.avail_out >= (uInt)len) {
635
+ z->stream.avail_out -= (uInt)len;
519
636
  }
520
637
  else {
521
638
  z->stream.avail_out = 0;
522
639
  }
523
640
  }
524
- memcpy(RSTRING(z->buf)->ptr + z->buf_filled, src, len);
641
+ memcpy(RSTRING_PTR(z->buf) + z->buf_filled, src, len);
525
642
  z->buf_filled += len;
526
- z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
643
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
527
644
  }
528
645
 
529
646
  #define zstream_append_buffer2(z,v) \
530
- zstream_append_buffer((z),RSTRING(v)->ptr,RSTRING(v)->len)
647
+ zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
531
648
 
532
649
  static VALUE
533
- zstream_detach_buffer(z)
534
- struct zstream *z;
650
+ zstream_detach_buffer(struct zstream *z)
535
651
  {
536
652
  VALUE dst;
537
653
 
@@ -551,40 +667,55 @@ zstream_detach_buffer(z)
551
667
  }
552
668
 
553
669
  static VALUE
554
- zstream_shift_buffer(z, len)
555
- struct zstream *z;
556
- int len;
670
+ zstream_shift_buffer(struct zstream *z, long len)
557
671
  {
558
672
  VALUE dst;
673
+ long buflen;
559
674
 
560
675
  if (z->buf_filled <= len) {
561
676
  return zstream_detach_buffer(z);
562
677
  }
563
678
 
564
- dst = rb_str_substr(z->buf, 0, len);
679
+ dst = rb_str_subseq(z->buf, 0, len);
565
680
  z->buf_filled -= len;
566
- memmove(RSTRING(z->buf)->ptr, RSTRING(z->buf)->ptr + len,
681
+ memmove(RSTRING_PTR(z->buf), RSTRING_PTR(z->buf) + len,
567
682
  z->buf_filled);
568
- z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled;
569
- z->stream.avail_out = RSTRING(z->buf)->len - z->buf_filled;
570
- if (z->stream.avail_out > ZSTREAM_AVAIL_OUT_STEP_MAX) {
571
- z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
683
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
684
+ buflen = RSTRING_LEN(z->buf) - z->buf_filled;
685
+ if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) {
686
+ buflen = ZSTREAM_AVAIL_OUT_STEP_MAX;
572
687
  }
688
+ z->stream.avail_out = (uInt)buflen;
573
689
 
574
690
  return dst;
575
691
  }
576
692
 
577
693
  static void
578
- zstream_buffer_ungetc(z, c)
579
- struct zstream *z;
580
- int c;
694
+ zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len)
695
+ {
696
+ if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) {
697
+ zstream_expand_buffer_into(z, len);
698
+ }
699
+
700
+ memmove(RSTRING_PTR(z->buf) + len, RSTRING_PTR(z->buf), z->buf_filled);
701
+ memmove(RSTRING_PTR(z->buf), b, len);
702
+ z->buf_filled+=len;
703
+ if (z->stream.avail_out > 0) {
704
+ if (len > z->stream.avail_out) len = z->stream.avail_out;
705
+ z->stream.next_out+=len;
706
+ z->stream.avail_out-=(uInt)len;
707
+ }
708
+ }
709
+
710
+ static void
711
+ zstream_buffer_ungetbyte(struct zstream *z, int c)
581
712
  {
582
- if (NIL_P(z->buf) || RSTRING(z->buf)->len - z->buf_filled == 0) {
713
+ if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) {
583
714
  zstream_expand_buffer(z);
584
715
  }
585
716
 
586
- memmove(RSTRING(z->buf)->ptr + 1, RSTRING(z->buf)->ptr, z->buf_filled);
587
- RSTRING(z->buf)->ptr[0] = (char)c;
717
+ memmove(RSTRING_PTR(z->buf) + 1, RSTRING_PTR(z->buf), z->buf_filled);
718
+ RSTRING_PTR(z->buf)[0] = (char)c;
588
719
  z->buf_filled++;
589
720
  if (z->stream.avail_out > 0) {
590
721
  z->stream.next_out++;
@@ -593,51 +724,44 @@ zstream_buffer_ungetc(z, c)
593
724
  }
594
725
 
595
726
  static void
596
- zstream_append_input(z, src, len)
597
- struct zstream *z;
598
- const char *src;
599
- unsigned int len;
727
+ zstream_append_input(struct zstream *z, const Bytef *src, long len)
600
728
  {
601
729
  if (len <= 0) return;
602
730
 
603
731
  if (NIL_P(z->input)) {
604
732
  z->input = rb_str_buf_new(len);
605
- rb_str_buf_cat(z->input, src, len);
733
+ rb_str_buf_cat(z->input, (const char*)src, len);
606
734
  }
607
735
  else {
608
- rb_str_buf_cat(z->input, src, len);
736
+ rb_str_buf_cat(z->input, (const char*)src, len);
609
737
  }
610
738
  }
611
739
 
612
740
  #define zstream_append_input2(z,v)\
613
- RB_GC_GUARD(v),\
741
+ RB_GC_GUARD(v) = v,\
614
742
  zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
615
743
 
616
744
  static void
617
- zstream_discard_input(z, len)
618
- struct zstream *z;
619
- unsigned int len;
745
+ zstream_discard_input(struct zstream *z, long len)
620
746
  {
621
- if (NIL_P(z->input) || RSTRING(z->input)->len <= len) {
747
+ if (NIL_P(z->input) || RSTRING_LEN(z->input) <= len) {
622
748
  z->input = Qnil;
623
749
  }
624
750
  else {
625
- memmove(RSTRING(z->input)->ptr, RSTRING(z->input)->ptr + len,
626
- RSTRING(z->input)->len - len);
627
- rb_str_resize(z->input, RSTRING(z->input)->len - len);
751
+ memmove(RSTRING_PTR(z->input), RSTRING_PTR(z->input) + len,
752
+ RSTRING_LEN(z->input) - len);
753
+ rb_str_resize(z->input, RSTRING_LEN(z->input) - len);
628
754
  }
629
755
  }
630
756
 
631
757
  static void
632
- zstream_reset_input(z)
633
- struct zstream *z;
758
+ zstream_reset_input(struct zstream *z)
634
759
  {
635
760
  z->input = Qnil;
636
761
  }
637
762
 
638
763
  static void
639
- zstream_passthrough_input(z)
640
- struct zstream *z;
764
+ zstream_passthrough_input(struct zstream *z)
641
765
  {
642
766
  if (!NIL_P(z->input)) {
643
767
  zstream_append_buffer2(z, z->input);
@@ -646,8 +770,7 @@ zstream_passthrough_input(z)
646
770
  }
647
771
 
648
772
  static VALUE
649
- zstream_detach_input(z)
650
- struct zstream *z;
773
+ zstream_detach_input(struct zstream *z)
651
774
  {
652
775
  VALUE dst;
653
776
 
@@ -662,8 +785,7 @@ zstream_detach_input(z)
662
785
  }
663
786
 
664
787
  static void
665
- zstream_reset(z)
666
- struct zstream *z;
788
+ zstream_reset(struct zstream *z)
667
789
  {
668
790
  int err;
669
791
 
@@ -680,8 +802,7 @@ zstream_reset(z)
680
802
  }
681
803
 
682
804
  static VALUE
683
- zstream_end(z)
684
- struct zstream *z;
805
+ zstream_end(struct zstream *z)
685
806
  {
686
807
  int err;
687
808
 
@@ -704,24 +825,20 @@ zstream_end(z)
704
825
  }
705
826
 
706
827
  static void
707
- zstream_run(z, src, len, flush)
708
- struct zstream *z;
709
- Bytef *src;
710
- uInt len;
711
- int flush;
828
+ zstream_run(struct zstream *z, Bytef *src, long len, int flush)
712
829
  {
713
830
  uInt n;
714
831
  int err;
715
- volatile VALUE guard;
832
+ volatile VALUE guard = Qnil;
716
833
 
717
834
  if (NIL_P(z->input) && len == 0) {
718
- z->stream.next_in = "";
835
+ z->stream.next_in = (Bytef*)"";
719
836
  z->stream.avail_in = 0;
720
837
  }
721
838
  else {
722
839
  zstream_append_input(z, src, len);
723
- z->stream.next_in = RSTRING(z->input)->ptr;
724
- z->stream.avail_in = RSTRING(z->input)->len;
840
+ z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
841
+ z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
725
842
  /* keep reference to `z->input' so as not to be garbage collected
726
843
  after zstream_reset_input() and prevent `z->stream.next_in'
727
844
  from dangling. */
@@ -735,7 +852,7 @@ zstream_run(z, src, len, flush)
735
852
  for (;;) {
736
853
  /* VC allocates err and guard to same address. accessing err and guard
737
854
  in same scope prevents it. */
738
- RB_GC_GUARD(guard);
855
+ RB_GC_GUARD(guard) = guard;
739
856
  n = z->stream.avail_out;
740
857
  err = z->func->run(&z->stream, flush);
741
858
  z->buf_filled += n - z->stream.avail_out;
@@ -773,27 +890,24 @@ zstream_run(z, src, len, flush)
773
890
  }
774
891
 
775
892
  static VALUE
776
- zstream_sync(z, src, len)
777
- struct zstream *z;
778
- Bytef *src;
779
- uInt len;
893
+ zstream_sync(struct zstream *z, Bytef *src, long len)
780
894
  {
781
895
  VALUE rest;
782
896
  int err;
783
897
 
784
898
  if (!NIL_P(z->input)) {
785
- z->stream.next_in = RSTRING(z->input)->ptr;
786
- z->stream.avail_in = RSTRING(z->input)->len;
899
+ z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
900
+ z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
787
901
  err = inflateSync(&z->stream);
788
902
  if (err == Z_OK) {
789
903
  zstream_discard_input(z,
790
- RSTRING(z->input)->len - z->stream.avail_in);
904
+ RSTRING_LEN(z->input) - z->stream.avail_in);
791
905
  zstream_append_input(z, src, len);
792
906
  return Qtrue;
793
907
  }
794
908
  zstream_reset_input(z);
795
909
  if (err != Z_DATA_ERROR) {
796
- rest = rb_str_new(z->stream.next_in, z->stream.avail_in);
910
+ rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in);
797
911
  raise_zlib_error(err, z->stream.msg);
798
912
  }
799
913
  }
@@ -801,30 +915,28 @@ zstream_sync(z, src, len)
801
915
  if (len <= 0) return Qfalse;
802
916
 
803
917
  z->stream.next_in = src;
804
- z->stream.avail_in = len;
918
+ z->stream.avail_in = MAX_UINT(len);
805
919
  err = inflateSync(&z->stream);
806
920
  if (err == Z_OK) {
807
921
  zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
808
922
  return Qtrue;
809
923
  }
810
924
  if (err != Z_DATA_ERROR) {
811
- rest = rb_str_new(z->stream.next_in, z->stream.avail_in);
925
+ rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in);
812
926
  raise_zlib_error(err, z->stream.msg);
813
927
  }
814
928
  return Qfalse;
815
929
  }
816
930
 
817
931
  static void
818
- zstream_mark(z)
819
- struct zstream *z;
932
+ zstream_mark(struct zstream *z)
820
933
  {
821
934
  rb_gc_mark(z->buf);
822
935
  rb_gc_mark(z->input);
823
936
  }
824
937
 
825
938
  static void
826
- zstream_finalize(z)
827
- struct zstream *z;
939
+ zstream_finalize(struct zstream *z)
828
940
  {
829
941
  int err = z->func->end(&z->stream);
830
942
  if (err == Z_STREAM_ERROR)
@@ -834,19 +946,16 @@ zstream_finalize(z)
834
946
  }
835
947
 
836
948
  static void
837
- zstream_free(z)
838
- struct zstream *z;
949
+ zstream_free(struct zstream *z)
839
950
  {
840
951
  if (ZSTREAM_IS_READY(z)) {
841
952
  zstream_finalize(z);
842
953
  }
843
- free(z);
954
+ xfree(z);
844
955
  }
845
956
 
846
957
  static VALUE
847
- zstream_new(klass, funcs)
848
- VALUE klass;
849
- const struct zstream_funcs *funcs;
958
+ zstream_new(VALUE klass, const struct zstream_funcs *funcs)
850
959
  {
851
960
  VALUE obj;
852
961
  struct zstream *z;
@@ -861,8 +970,7 @@ zstream_new(klass, funcs)
861
970
  #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
862
971
 
863
972
  static struct zstream *
864
- get_zstream(obj)
865
- VALUE obj;
973
+ get_zstream(VALUE obj)
866
974
  {
867
975
  struct zstream *z;
868
976
 
@@ -942,8 +1050,7 @@ get_zstream(obj)
942
1050
  * exception.
943
1051
  */
944
1052
  static VALUE
945
- rb_zstream_end(obj)
946
- VALUE obj;
1053
+ rb_zstream_end(VALUE obj)
947
1054
  {
948
1055
  zstream_end(get_zstream(obj));
949
1056
  return Qnil;
@@ -954,8 +1061,7 @@ rb_zstream_end(obj)
954
1061
  * are discarded.
955
1062
  */
956
1063
  static VALUE
957
- rb_zstream_reset(obj)
958
- VALUE obj;
1064
+ rb_zstream_reset(VALUE obj)
959
1065
  {
960
1066
  zstream_reset(get_zstream(obj));
961
1067
  return Qnil;
@@ -966,13 +1072,12 @@ rb_zstream_reset(obj)
966
1072
  * Zlib::Inflate#finish for details of this behavior.
967
1073
  */
968
1074
  static VALUE
969
- rb_zstream_finish(obj)
970
- VALUE obj;
1075
+ rb_zstream_finish(VALUE obj)
971
1076
  {
972
1077
  struct zstream *z = get_zstream(obj);
973
1078
  VALUE dst;
974
1079
 
975
- zstream_run(z, "", 0, Z_FINISH);
1080
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH);
976
1081
  dst = zstream_detach_buffer(z);
977
1082
 
978
1083
  OBJ_INFECT(dst, obj);
@@ -983,8 +1088,7 @@ rb_zstream_finish(obj)
983
1088
  * Flushes input buffer and returns all data in that buffer.
984
1089
  */
985
1090
  static VALUE
986
- rb_zstream_flush_next_in(obj)
987
- VALUE obj;
1091
+ rb_zstream_flush_next_in(VALUE obj)
988
1092
  {
989
1093
  struct zstream *z;
990
1094
  VALUE dst;
@@ -999,8 +1103,7 @@ rb_zstream_flush_next_in(obj)
999
1103
  * Flushes output buffer and returns all data in that buffer.
1000
1104
  */
1001
1105
  static VALUE
1002
- rb_zstream_flush_next_out(obj)
1003
- VALUE obj;
1106
+ rb_zstream_flush_next_out(VALUE obj)
1004
1107
  {
1005
1108
  struct zstream *z;
1006
1109
  VALUE dst;
@@ -1016,8 +1119,7 @@ rb_zstream_flush_next_out(obj)
1016
1119
  * space is allocated automatically, this method returns 0 normally.
1017
1120
  */
1018
1121
  static VALUE
1019
- rb_zstream_avail_out(obj)
1020
- VALUE obj;
1122
+ rb_zstream_avail_out(VALUE obj)
1021
1123
  {
1022
1124
  struct zstream *z;
1023
1125
  Data_Get_Struct(obj, struct zstream, z);
@@ -1026,13 +1128,12 @@ rb_zstream_avail_out(obj)
1026
1128
 
1027
1129
  /*
1028
1130
  * Allocates +size+ bytes of free space in the output buffer. If there are more
1029
- * than +size+ bytes already in the buffer, the buffer is truncated. Because
1131
+ * than +size+ bytes already in the buffer, the buffer is truncated. Because
1030
1132
  * free space is allocated automatically, you usually don't need to use this
1031
1133
  * method.
1032
1134
  */
1033
1135
  static VALUE
1034
- rb_zstream_set_avail_out(obj, size)
1035
- VALUE obj, size;
1136
+ rb_zstream_set_avail_out(VALUE obj, VALUE size)
1036
1137
  {
1037
1138
  struct zstream *z = get_zstream(obj);
1038
1139
 
@@ -1045,20 +1146,18 @@ rb_zstream_set_avail_out(obj, size)
1045
1146
  * Returns bytes of data in the input buffer. Normally, returns 0.
1046
1147
  */
1047
1148
  static VALUE
1048
- rb_zstream_avail_in(obj)
1049
- VALUE obj;
1149
+ rb_zstream_avail_in(VALUE obj)
1050
1150
  {
1051
1151
  struct zstream *z;
1052
1152
  Data_Get_Struct(obj, struct zstream, z);
1053
- return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING(z->input)->len));
1153
+ return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input)));
1054
1154
  }
1055
1155
 
1056
1156
  /*
1057
1157
  * Returns the total bytes of the input data to the stream. FIXME
1058
1158
  */
1059
1159
  static VALUE
1060
- rb_zstream_total_in(obj)
1061
- VALUE obj;
1160
+ rb_zstream_total_in(VALUE obj)
1062
1161
  {
1063
1162
  return rb_uint2inum(get_zstream(obj)->stream.total_in);
1064
1163
  }
@@ -1067,20 +1166,18 @@ rb_zstream_total_in(obj)
1067
1166
  * Returns the total bytes of the output data from the stream. FIXME
1068
1167
  */
1069
1168
  static VALUE
1070
- rb_zstream_total_out(obj)
1071
- VALUE obj;
1169
+ rb_zstream_total_out(VALUE obj)
1072
1170
  {
1073
1171
  return rb_uint2inum(get_zstream(obj)->stream.total_out);
1074
1172
  }
1075
1173
 
1076
1174
  /*
1077
1175
  * Guesses the type of the data which have been inputed into the stream. The
1078
- * returned value is either <tt>Zlib::BINARY</tt>, <tt>Zlib::ASCII</tt>, or
1079
- * <tt>Zlib::UNKNOWN</tt>.
1176
+ * returned value is either <tt>BINARY</tt>, <tt>ASCII</tt>, or
1177
+ * <tt>UNKNOWN</tt>.
1080
1178
  */
1081
1179
  static VALUE
1082
- rb_zstream_data_type(obj)
1083
- VALUE obj;
1180
+ rb_zstream_data_type(VALUE obj)
1084
1181
  {
1085
1182
  return INT2FIX(get_zstream(obj)->stream.data_type);
1086
1183
  }
@@ -1089,8 +1186,7 @@ rb_zstream_data_type(obj)
1089
1186
  * Returns the adler-32 checksum.
1090
1187
  */
1091
1188
  static VALUE
1092
- rb_zstream_adler(obj)
1093
- VALUE obj;
1189
+ rb_zstream_adler(VALUE obj)
1094
1190
  {
1095
1191
  return rb_uint2inum(get_zstream(obj)->stream.adler);
1096
1192
  }
@@ -1099,8 +1195,7 @@ rb_zstream_adler(obj)
1099
1195
  * Returns true if the stream is finished.
1100
1196
  */
1101
1197
  static VALUE
1102
- rb_zstream_finished_p(obj)
1103
- VALUE obj;
1198
+ rb_zstream_finished_p(VALUE obj)
1104
1199
  {
1105
1200
  return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse;
1106
1201
  }
@@ -1109,8 +1204,7 @@ rb_zstream_finished_p(obj)
1109
1204
  * Returns true if the stream is closed.
1110
1205
  */
1111
1206
  static VALUE
1112
- rb_zstream_closed_p(obj)
1113
- VALUE obj;
1207
+ rb_zstream_closed_p(VALUE obj)
1114
1208
  {
1115
1209
  struct zstream *z;
1116
1210
  Data_Get_Struct(obj, struct zstream, z);
@@ -1123,7 +1217,7 @@ rb_zstream_closed_p(obj)
1123
1217
  /*
1124
1218
  * Document-class: Zlib::Deflate
1125
1219
  *
1126
- * Zlib::Deflate is the class for compressing data. See Zlib::Stream for more
1220
+ * Zlib::Deflate is the class for compressing data. See Zlib::ZStream for more
1127
1221
  * information.
1128
1222
  */
1129
1223
 
@@ -1139,26 +1233,71 @@ rb_zstream_closed_p(obj)
1139
1233
 
1140
1234
 
1141
1235
  static VALUE
1142
- rb_deflate_s_allocate(klass)
1143
- VALUE klass;
1236
+ rb_deflate_s_allocate(VALUE klass)
1144
1237
  {
1145
1238
  return zstream_deflate_new(klass);
1146
1239
  }
1147
1240
 
1148
1241
  /*
1242
+ * Document-method: Zlib::Deflate.new
1243
+ *
1149
1244
  * call-seq: Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil)
1150
1245
  *
1246
+ * == Arguments
1247
+ *
1248
+ * +level+::
1249
+ * An Integer compression level between
1250
+ * BEST_SPEED and BEST_COMPRESSION
1251
+ * +windowBits+::
1252
+ * An Integer for the windowBits size. Should be
1253
+ * in the range 8..15, larger values of this parameter
1254
+ * result in better at the expense of memory usage.
1255
+ * +memlevel+::
1256
+ * Specifies how much memory should be allocated for
1257
+ * the internal compression state.
1258
+ * Between DEF_MEM_LEVEL and MAX_MEM_LEVEL
1259
+ * +strategy+::
1260
+ * A parameter to tune the compression algorithm. Use the
1261
+ * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a
1262
+ * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no
1263
+ * string match).
1264
+ *
1265
+ * == Description
1266
+ *
1151
1267
  * Creates a new deflate stream for compression. See zlib.h for details of
1152
1268
  * each argument. If an argument is nil, the default value of that argument is
1153
1269
  * used.
1154
1270
  *
1155
- * TODO: document better!
1271
+ *
1272
+ * == examples
1273
+ *
1274
+ * === basic
1275
+ *
1276
+ * f = File.new("compressed.file","w+")
1277
+ * #=> #<File:compressed.file>
1278
+ * f << Zlib::Deflate.new().deflate(File.read("big.file"))
1279
+ * #=> #<File:compressed.file>
1280
+ * f.close
1281
+ * #=> nil
1282
+ *
1283
+ * === a little more robust
1284
+ *
1285
+ * compressed_file = File.open("compressed.file", "w+")
1286
+ * #=> #<File:compressed.file>
1287
+ * zd = Zlib::Deflate.new(Zlib::BEST_COMPRESSION, 15, Zlib::MAX_MEM_LEVEL, Zlib::HUFFMAN_ONLY)
1288
+ * #=> #<Zlib::Deflate:0x000000008610a0>
1289
+ * compressed_file << zd.deflate(File.read("big.file"))
1290
+ * #=> "\xD4z\xC6\xDE\b\xA1K\x1Ej\x8A ..."
1291
+ * compressed_file.close
1292
+ * #=> nil
1293
+ * zd.close
1294
+ * #=> nil
1295
+ *
1296
+ * (while this example will work, for best optimization the flags need to be reviewed for your specific function)
1297
+ *
1156
1298
  */
1157
1299
  static VALUE
1158
- rb_deflate_initialize(argc, argv, obj)
1159
- int argc;
1160
- VALUE *argv;
1161
- VALUE obj;
1300
+ rb_deflate_initialize(int argc, VALUE *argv, VALUE obj)
1162
1301
  {
1163
1302
  struct zstream *z;
1164
1303
  VALUE level, wbits, memlevel, strategy;
@@ -1179,11 +1318,12 @@ rb_deflate_initialize(argc, argv, obj)
1179
1318
  }
1180
1319
 
1181
1320
  /*
1321
+ * Document-method: Zlib::Deflate#initialize_copy
1322
+ *
1182
1323
  * Duplicates the deflate stream.
1183
1324
  */
1184
1325
  static VALUE
1185
- rb_deflate_init_copy(self, orig)
1186
- VALUE self, orig;
1326
+ rb_deflate_init_copy(VALUE self, VALUE orig)
1187
1327
  {
1188
1328
  struct zstream *z1, *z2;
1189
1329
  int err;
@@ -1204,41 +1344,40 @@ rb_deflate_init_copy(self, orig)
1204
1344
  }
1205
1345
 
1206
1346
  static VALUE
1207
- deflate_run(args)
1208
- VALUE args;
1347
+ deflate_run(VALUE args)
1209
1348
  {
1210
- struct zstream *z = (struct zstream *)((VALUE *)args)[0];
1211
- VALUE src = ((VALUE *)args)[1];
1349
+ struct zstream *z = (struct zstream*)((VALUE*)args)[0];
1350
+ VALUE src = ((VALUE*)args)[1];
1212
1351
 
1213
- zstream_run(z, RSTRING(src)->ptr, RSTRING(src)->len, Z_FINISH);
1352
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_FINISH);
1214
1353
  return zstream_detach_buffer(z);
1215
1354
  }
1216
1355
 
1217
1356
  /*
1218
- * call-seq: Zlib::Deflate.deflate(string[, level])
1357
+ * Document-method: Zlib::Deflate.deflate
1358
+ *
1359
+ * call-seq: Zlib.deflate(string[, level])
1360
+ * Zlib::Deflate.deflate(string[, level])
1219
1361
  *
1220
1362
  * Compresses the given +string+. Valid values of level are
1221
- * <tt>Zlib::NO_COMPRESSION</tt>, <tt>Zlib::BEST_SPEED</tt>,
1222
- * <tt>Zlib::BEST_COMPRESSION</tt>, <tt>Zlib::DEFAULT_COMPRESSION</tt>, and an
1223
- * integer from 0 to 9.
1363
+ * <tt>NO_COMPRESSION</tt>, <tt>BEST_SPEED</tt>,
1364
+ * <tt>BEST_COMPRESSION</tt>, <tt>DEFAULT_COMPRESSION</tt>, and an
1365
+ * integer from 0 to 9 (the default is 6).
1224
1366
  *
1225
1367
  * This method is almost equivalent to the following code:
1226
1368
  *
1227
1369
  * def deflate(string, level)
1228
1370
  * z = Zlib::Deflate.new(level)
1229
- * dst = z.deflate(string, Zlib::FINISH)
1371
+ * dst = z.deflate(string, Zlib::NO_FLUSH)
1230
1372
  * z.close
1231
1373
  * dst
1232
1374
  * end
1233
1375
  *
1234
- * TODO: what's default value of +level+?
1376
+ * See also Zlib.inflate
1235
1377
  *
1236
1378
  */
1237
1379
  static VALUE
1238
- rb_deflate_s_deflate(argc, argv, klass)
1239
- int argc;
1240
- VALUE *argv;
1241
- VALUE klass;
1380
+ rb_deflate_s_deflate(int argc, VALUE *argv, VALUE klass)
1242
1381
  {
1243
1382
  struct zstream z;
1244
1383
  VALUE src, level, dst, args[2];
@@ -1264,40 +1403,53 @@ rb_deflate_s_deflate(argc, argv, klass)
1264
1403
  }
1265
1404
 
1266
1405
  static void
1267
- do_deflate(z, src, flush)
1268
- struct zstream *z;
1269
- VALUE src;
1270
- int flush;
1406
+ do_deflate(struct zstream *z, VALUE src, int flush)
1271
1407
  {
1272
1408
  if (NIL_P(src)) {
1273
- zstream_run(z, "", 0, Z_FINISH);
1409
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1274
1410
  return;
1275
1411
  }
1276
1412
  StringValue(src);
1277
- if (flush != Z_NO_FLUSH || RSTRING(src)->len > 0) { /* prevent BUF_ERROR */
1278
- zstream_run(z, RSTRING(src)->ptr, RSTRING(src)->len, flush);
1413
+ if (flush != Z_NO_FLUSH || RSTRING_LEN(src) > 0) { /* prevent BUF_ERROR */
1414
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), flush);
1279
1415
  }
1280
1416
  }
1281
1417
 
1282
1418
  /*
1419
+ * Document-method: Zlib.deflate
1420
+ *
1283
1421
  * call-seq: deflate(string[, flush])
1284
1422
  *
1423
+ * == Arguments
1424
+ *
1425
+ * +string+::
1426
+ * String
1427
+ *
1428
+ * +flush+::
1429
+ * Integer representing a flush code. Either NO_FLUSH,
1430
+ * SYNC_FLUSH, FULL_FLUSH, or FINISH. See zlib.h for details.
1431
+ * Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
1432
+ * decide how much data to accumulate before producing output, in order to
1433
+ * maximize compression.
1434
+ *
1435
+ * == Description
1436
+ *
1285
1437
  * Inputs +string+ into the deflate stream and returns the output from the
1286
1438
  * stream. On calling this method, both the input and the output buffers of
1287
- * the stream are flushed. If +string+ is nil, this method finishes the
1439
+ * the stream are flushed.
1440
+ *
1441
+ * If +string+ is nil, this method finishes the
1288
1442
  * stream, just like Zlib::ZStream#finish.
1289
1443
  *
1290
- * The value of +flush+ should be either <tt>Zlib::NO_FLUSH</tt>,
1291
- * <tt>Zlib::SYNC_FLUSH</tt>, <tt>Zlib::FULL_FLUSH</tt>, or
1292
- * <tt>Zlib::FINISH</tt>. See zlib.h for details.
1444
+ * == Usage
1445
+ *
1446
+ * comp = Zlib.deflate(File.read("big.file"))
1447
+ * or
1448
+ * comp = Zlib.deflate(File.read("big.file"), Zlib::FULL_FLUSH)
1293
1449
  *
1294
- * TODO: document better!
1295
1450
  */
1296
1451
  static VALUE
1297
- rb_deflate_deflate(argc, argv, obj)
1298
- int argc;
1299
- VALUE *argv;
1300
- VALUE obj;
1452
+ rb_deflate_deflate(int argc, VALUE *argv, VALUE obj)
1301
1453
  {
1302
1454
  struct zstream *z = get_zstream(obj);
1303
1455
  VALUE src, flush, dst;
@@ -1312,6 +1464,8 @@ rb_deflate_deflate(argc, argv, obj)
1312
1464
  }
1313
1465
 
1314
1466
  /*
1467
+ * Document-method: Zlib::Deflate.<<
1468
+ *
1315
1469
  * call-seq: << string
1316
1470
  *
1317
1471
  * Inputs +string+ into the deflate stream just like Zlib::Deflate#deflate, but
@@ -1319,8 +1473,7 @@ rb_deflate_deflate(argc, argv, obj)
1319
1473
  * preserved in output buffer.
1320
1474
  */
1321
1475
  static VALUE
1322
- rb_deflate_addstr(obj, src)
1323
- VALUE obj, src;
1476
+ rb_deflate_addstr(VALUE obj, VALUE src)
1324
1477
  {
1325
1478
  OBJ_INFECT(obj, src);
1326
1479
  do_deflate(get_zstream(obj), src, Z_NO_FLUSH);
@@ -1328,19 +1481,19 @@ rb_deflate_addstr(obj, src)
1328
1481
  }
1329
1482
 
1330
1483
  /*
1484
+ * Document-method: Zlib::Deflate#flush
1485
+ *
1331
1486
  * call-seq: flush(flush)
1332
1487
  *
1333
1488
  * This method is equivalent to <tt>deflate('', flush)</tt>. If flush is omitted,
1334
- * <tt>Zlib::SYNC_FLUSH</tt> is used as flush. This method is just provided
1489
+ * <tt>SYNC_FLUSH</tt> is used as flush. This method is just provided
1335
1490
  * to improve the readability of your Ruby program.
1336
1491
  *
1337
- * TODO: document better!
1492
+ * Please visit your zlib.h for a deeper detail on NO_FLUSH, SYNC_FLUSH, FULL_FLUSH, and FINISH
1493
+ *
1338
1494
  */
1339
1495
  static VALUE
1340
- rb_deflate_flush(argc, argv, obj)
1341
- int argc;
1342
- VALUE *argv;
1343
- VALUE obj;
1496
+ rb_deflate_flush(int argc, VALUE *argv, VALUE obj)
1344
1497
  {
1345
1498
  struct zstream *z = get_zstream(obj);
1346
1499
  VALUE v_flush, dst;
@@ -1349,7 +1502,7 @@ rb_deflate_flush(argc, argv, obj)
1349
1502
  rb_scan_args(argc, argv, "01", &v_flush);
1350
1503
  flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1351
1504
  if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */
1352
- zstream_run(z, "", 0, flush);
1505
+ zstream_run(z, (Bytef*)"", 0, flush);
1353
1506
  }
1354
1507
  dst = zstream_detach_buffer(z);
1355
1508
 
@@ -1358,17 +1511,26 @@ rb_deflate_flush(argc, argv, obj)
1358
1511
  }
1359
1512
 
1360
1513
  /*
1514
+ * Document-method: Zlib::Deflate.params
1515
+ *
1361
1516
  * call-seq: params(level, strategy)
1362
- *
1517
+ *
1363
1518
  * Changes the parameters of the deflate stream. See zlib.h for details. The
1364
1519
  * output from the stream by changing the params is preserved in output
1365
1520
  * buffer.
1366
1521
  *
1367
- * TODO: document better!
1522
+ * +level+::
1523
+ * An Integer compression level between
1524
+ * BEST_SPEED and BEST_COMPRESSION
1525
+ * +strategy+::
1526
+ * A parameter to tune the compression algorithm. Use the
1527
+ * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a
1528
+ * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no
1529
+ * string match).
1530
+ *
1368
1531
  */
1369
1532
  static VALUE
1370
- rb_deflate_params(obj, v_level, v_strategy)
1371
- VALUE obj, v_level, v_strategy;
1533
+ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
1372
1534
  {
1373
1535
  struct zstream *z = get_zstream(obj);
1374
1536
  int level, strategy;
@@ -1396,17 +1558,21 @@ rb_deflate_params(obj, v_level, v_strategy)
1396
1558
  }
1397
1559
 
1398
1560
  /*
1561
+ * Document-method: Zlib::Deflate.set_dictionary
1562
+ *
1399
1563
  * call-seq: set_dictionary(string)
1400
1564
  *
1401
1565
  * Sets the preset dictionary and returns +string+. This method is available
1402
1566
  * just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called.
1403
1567
  * See zlib.h for details.
1404
1568
  *
1405
- * TODO: document better!
1569
+ * Can raise errors of Z_STREAM_ERROR if a parameter is invalid (such as
1570
+ * NULL dictionary) or the stream state is inconsistent, Z_DATA_ERROR if
1571
+ * the given dictionary doesn't match the expected one (incorrect adler32 value)
1572
+ *
1406
1573
  */
1407
1574
  static VALUE
1408
- rb_deflate_set_dictionary(obj, dic)
1409
- VALUE obj, dic;
1575
+ rb_deflate_set_dictionary(VALUE obj, VALUE dic)
1410
1576
  {
1411
1577
  struct zstream *z = get_zstream(obj);
1412
1578
  VALUE src = dic;
@@ -1415,7 +1581,7 @@ rb_deflate_set_dictionary(obj, dic)
1415
1581
  OBJ_INFECT(obj, dic);
1416
1582
  StringValue(src);
1417
1583
  err = deflateSetDictionary(&z->stream,
1418
- RSTRING(src)->ptr, RSTRING(src)->len);
1584
+ (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
1419
1585
  if (err != Z_OK) {
1420
1586
  raise_zlib_error(err, z->stream.msg);
1421
1587
  }
@@ -1437,25 +1603,53 @@ rb_deflate_set_dictionary(obj, dic)
1437
1603
 
1438
1604
 
1439
1605
  static VALUE
1440
- rb_inflate_s_allocate(klass)
1441
- VALUE klass;
1606
+ rb_inflate_s_allocate(VALUE klass)
1442
1607
  {
1443
1608
  return zstream_inflate_new(klass);
1444
1609
  }
1445
1610
 
1446
1611
  /*
1612
+ * Document-method: Zlib::Inflate.new
1613
+ *
1447
1614
  * call-seq: Zlib::Inflate.new(window_bits)
1448
1615
  *
1616
+ * == Arguments
1617
+ *
1618
+ * +windowBits+::
1619
+ * An Integer for the windowBits size. Should be
1620
+ * in the range 8..15, larger values of this parameter
1621
+ * result in better at the expense of memory usage.
1622
+ *
1623
+ * == Description
1624
+ *
1449
1625
  * Creates a new inflate stream for decompression. See zlib.h for details
1450
1626
  * of the argument. If +window_bits+ is +nil+, the default value is used.
1451
1627
  *
1452
- * TODO: document better!
1628
+ * == Example
1629
+ *
1630
+ * cf = File.open("compressed.file")
1631
+ * ucf = File.open("uncompressed.file", "w+")
1632
+ * zi = Zlib::Inflate.new(Zlib::MAX_WBITS)
1633
+ *
1634
+ * ucf << zi.inflate(cf.read)
1635
+ *
1636
+ * ucf.close
1637
+ * zi.close
1638
+ * cf.close
1639
+ *
1640
+ * or
1641
+ *
1642
+ * File.open("compressed.file") {|cf|
1643
+ * zi = Zlib::Inflate.new
1644
+ * File.open("uncompressed.file", "w+") {|ucf|
1645
+ * ucf << zi.inflate(cf.read)
1646
+ * }
1647
+ * zi.close
1648
+ * }
1649
+ *
1453
1650
  */
1454
1651
  static VALUE
1455
- rb_inflate_initialize(argc, argv, obj)
1456
- int argc;
1457
- VALUE *argv;
1458
- VALUE obj;
1652
+ rb_inflate_initialize(int argc, VALUE *argv, VALUE obj)
1459
1653
  {
1460
1654
  struct zstream *z;
1461
1655
  VALUE wbits;
@@ -1474,18 +1668,19 @@ rb_inflate_initialize(argc, argv, obj)
1474
1668
  }
1475
1669
 
1476
1670
  static VALUE
1477
- inflate_run(args)
1478
- VALUE args;
1671
+ inflate_run(VALUE args)
1479
1672
  {
1480
- struct zstream *z = (struct zstream *)((VALUE *)args)[0];
1481
- VALUE src = ((VALUE *)args)[1];
1673
+ struct zstream *z = (struct zstream*)((VALUE*)args)[0];
1674
+ VALUE src = ((VALUE*)args)[1];
1482
1675
 
1483
- zstream_run(z, RSTRING(src)->ptr, RSTRING(src)->len, Z_SYNC_FLUSH);
1484
- zstream_run(z, "", 0, Z_FINISH); /* for checking errors */
1676
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1677
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH); /* for checking errors */
1485
1678
  return zstream_detach_buffer(z);
1486
1679
  }
1487
1680
 
1488
1681
  /*
1682
+ * Document-method: Zlib::Inflate.inflate
1683
+ *
1489
1684
  * call-seq: Zlib::Inflate.inflate(string)
1490
1685
  *
1491
1686
  * Decompresses +string+. Raises a Zlib::NeedDict exception if a preset
@@ -1501,10 +1696,11 @@ inflate_run(args)
1501
1696
  * buf
1502
1697
  * end
1503
1698
  *
1699
+ * See also Zlib.deflate
1700
+ *
1504
1701
  */
1505
1702
  static VALUE
1506
- rb_inflate_s_inflate(obj, src)
1507
- VALUE obj, src;
1703
+ rb_inflate_s_inflate(VALUE obj, VALUE src)
1508
1704
  {
1509
1705
  struct zstream z;
1510
1706
  VALUE dst, args[2];
@@ -1527,21 +1723,21 @@ rb_inflate_s_inflate(obj, src)
1527
1723
  }
1528
1724
 
1529
1725
  static void
1530
- do_inflate(z, src)
1531
- struct zstream *z;
1532
- VALUE src;
1726
+ do_inflate(struct zstream *z, VALUE src)
1533
1727
  {
1534
1728
  if (NIL_P(src)) {
1535
- zstream_run(z, "", 0, Z_FINISH);
1729
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1536
1730
  return;
1537
1731
  }
1538
1732
  StringValue(src);
1539
- if (RSTRING(src)->len > 0) { /* prevent Z_BUF_ERROR */
1540
- zstream_run(z, RSTRING(src)->ptr, RSTRING(src)->len, Z_SYNC_FLUSH);
1733
+ if (RSTRING_LEN(src) > 0) { /* prevent Z_BUF_ERROR */
1734
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1541
1735
  }
1542
1736
  }
1543
1737
 
1544
1738
  /*
1739
+ * Document-method: Zlib::Inflate#inflate
1740
+ *
1545
1741
  * call-seq: inflate(string)
1546
1742
  *
1547
1743
  * Inputs +string+ into the inflate stream and returns the output from the
@@ -1553,11 +1749,10 @@ do_inflate(z, src)
1553
1749
  * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then
1554
1750
  * call this method again with an empty string. (<i>???</i>)
1555
1751
  *
1556
- * TODO: document better!
1752
+ * See also Zlib::Inflate.new
1557
1753
  */
1558
1754
  static VALUE
1559
- rb_inflate_inflate(obj, src)
1560
- VALUE obj, src;
1755
+ rb_inflate_inflate(VALUE obj, VALUE src)
1561
1756
  {
1562
1757
  struct zstream *z = get_zstream(obj);
1563
1758
  VALUE dst;
@@ -1594,8 +1789,7 @@ rb_inflate_inflate(obj, src)
1594
1789
  * preserved in output buffer.
1595
1790
  */
1596
1791
  static VALUE
1597
- rb_inflate_addstr(obj, src)
1598
- VALUE obj, src;
1792
+ rb_inflate_addstr(VALUE obj, VALUE src)
1599
1793
  {
1600
1794
  struct zstream *z = get_zstream(obj);
1601
1795
 
@@ -1626,14 +1820,13 @@ rb_inflate_addstr(obj, src)
1626
1820
  * following data of full flush point is preserved in the buffer.
1627
1821
  */
1628
1822
  static VALUE
1629
- rb_inflate_sync(obj, src)
1630
- VALUE obj, src;
1823
+ rb_inflate_sync(VALUE obj, VALUE src)
1631
1824
  {
1632
1825
  struct zstream *z = get_zstream(obj);
1633
1826
 
1634
1827
  OBJ_INFECT(obj, src);
1635
1828
  StringValue(src);
1636
- return zstream_sync(z, RSTRING(src)->ptr, RSTRING(src)->len);
1829
+ return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src));
1637
1830
  }
1638
1831
 
1639
1832
  /*
@@ -1644,8 +1837,7 @@ rb_inflate_sync(obj, src)
1644
1837
  * <tt>:)</tt>
1645
1838
  */
1646
1839
  static VALUE
1647
- rb_inflate_sync_point_p(obj)
1648
- VALUE obj;
1840
+ rb_inflate_sync_point_p(VALUE obj)
1649
1841
  {
1650
1842
  struct zstream *z = get_zstream(obj);
1651
1843
  int err;
@@ -1661,14 +1853,14 @@ rb_inflate_sync_point_p(obj)
1661
1853
  }
1662
1854
 
1663
1855
  /*
1856
+ * Document-method: Zlib::Inflate#set_dictionary
1857
+ *
1664
1858
  * Sets the preset dictionary and returns +string+. This method is available just
1665
1859
  * only after a Zlib::NeedDict exception was raised. See zlib.h for details.
1666
1860
  *
1667
- * TODO: document better!
1668
1861
  */
1669
1862
  static VALUE
1670
- rb_inflate_set_dictionary(obj, dic)
1671
- VALUE obj, dic;
1863
+ rb_inflate_set_dictionary(VALUE obj, VALUE dic)
1672
1864
  {
1673
1865
  struct zstream *z = get_zstream(obj);
1674
1866
  VALUE src = dic;
@@ -1677,7 +1869,7 @@ rb_inflate_set_dictionary(obj, dic)
1677
1869
  OBJ_INFECT(obj, dic);
1678
1870
  StringValue(src);
1679
1871
  err = inflateSetDictionary(&z->stream,
1680
- RSTRING(src)->ptr, RSTRING(src)->len);
1872
+ (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
1681
1873
  if (err != Z_OK) {
1682
1874
  raise_zlib_error(err, z->stream.msg);
1683
1875
  }
@@ -1731,7 +1923,7 @@ rb_inflate_set_dictionary(obj, dic)
1731
1923
  #define OS_CODE OS_UNIX
1732
1924
  #endif
1733
1925
 
1734
- static ID id_write, id_read, id_flush, id_seek, id_close;
1926
+ static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path, id_input;
1735
1927
  static VALUE cGzError, cNoFooter, cCRCError, cLengthError;
1736
1928
 
1737
1929
 
@@ -1748,33 +1940,41 @@ struct gzfile {
1748
1940
  VALUE comment; /* for header; must be a String */
1749
1941
  unsigned long crc;
1750
1942
  int lineno;
1751
- int ungetc;
1943
+ long ungetc;
1752
1944
  void (*end)(struct gzfile *);
1945
+ rb_encoding *enc;
1946
+ rb_encoding *enc2;
1947
+ rb_econv_t *ec;
1948
+ int ecflags;
1949
+ VALUE ecopts;
1950
+ char *cbuf;
1951
+ VALUE path;
1753
1952
  };
1953
+ #define GZFILE_CBUF_CAPA 10
1754
1954
 
1755
1955
  #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
1756
1956
  #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
1757
1957
  #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
1758
1958
 
1759
1959
  #define GZFILE_IS_FINISHED(gz) \
1760
- (ZSTREAM_IS_FINISHED(&gz->z) && (gz)->z.buf_filled == 0)
1960
+ (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
1761
1961
 
1762
1962
  #define GZFILE_READ_SIZE 2048
1763
1963
 
1764
1964
 
1765
1965
  static void
1766
- gzfile_mark(gz)
1767
- struct gzfile *gz;
1966
+ gzfile_mark(struct gzfile *gz)
1768
1967
  {
1769
1968
  rb_gc_mark(gz->io);
1770
1969
  rb_gc_mark(gz->orig_name);
1771
1970
  rb_gc_mark(gz->comment);
1772
1971
  zstream_mark(&gz->z);
1972
+ rb_gc_mark(gz->ecopts);
1973
+ rb_gc_mark(gz->path);
1773
1974
  }
1774
1975
 
1775
1976
  static void
1776
- gzfile_free(gz)
1777
- struct gzfile *gz;
1977
+ gzfile_free(struct gzfile *gz)
1778
1978
  {
1779
1979
  struct zstream *z = &gz->z;
1780
1980
 
@@ -1784,14 +1984,17 @@ gzfile_free(gz)
1784
1984
  }
1785
1985
  zstream_finalize(z);
1786
1986
  }
1787
- free(gz);
1987
+ if (gz->cbuf) {
1988
+ xfree(gz->cbuf);
1989
+ }
1990
+ xfree(gz);
1788
1991
  }
1789
1992
 
1790
1993
  static VALUE
1791
1994
  gzfile_new(klass, funcs, endfunc)
1792
1995
  VALUE klass;
1793
1996
  const struct zstream_funcs *funcs;
1794
- void (*endfunc) _((struct gzfile *));
1997
+ void (*endfunc)(struct gzfile *);
1795
1998
  {
1796
1999
  VALUE obj;
1797
2000
  struct gzfile *gz;
@@ -1808,6 +2011,13 @@ gzfile_new(klass, funcs, endfunc)
1808
2011
  gz->lineno = 0;
1809
2012
  gz->ungetc = 0;
1810
2013
  gz->end = endfunc;
2014
+ gz->enc = rb_default_external_encoding();
2015
+ gz->enc2 = 0;
2016
+ gz->ec = NULL;
2017
+ gz->ecflags = 0;
2018
+ gz->ecopts = Qnil;
2019
+ gz->cbuf = 0;
2020
+ gz->path = Qnil;
1811
2021
 
1812
2022
  return obj;
1813
2023
  }
@@ -1816,19 +2026,24 @@ gzfile_new(klass, funcs, endfunc)
1816
2026
  #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
1817
2027
 
1818
2028
  static void
1819
- gzfile_reset(gz)
1820
- struct gzfile *gz;
2029
+ gzfile_reset(struct gzfile *gz)
1821
2030
  {
1822
2031
  zstream_reset(&gz->z);
1823
2032
  gz->crc = crc32(0, Z_NULL, 0);
1824
2033
  gz->lineno = 0;
1825
2034
  gz->ungetc = 0;
2035
+ /* TODO: Encodings
2036
+ *
2037
+ if (gz->ec) {
2038
+ rb_econv_close(gz->ec);
2039
+ gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name,
2040
+ gz->ecflags, gz->ecopts);
2041
+ }
2042
+ */
1826
2043
  }
1827
2044
 
1828
2045
  static void
1829
- gzfile_close(gz, closeflag)
1830
- struct gzfile *gz;
1831
- int closeflag;
2046
+ gzfile_close(struct gzfile *gz, int closeflag)
1832
2047
  {
1833
2048
  VALUE io = gz->io;
1834
2049
 
@@ -1842,8 +2057,7 @@ gzfile_close(gz, closeflag)
1842
2057
  }
1843
2058
 
1844
2059
  static void
1845
- gzfile_write_raw(gz)
1846
- struct gzfile *gz;
2060
+ gzfile_write_raw(struct gzfile *gz)
1847
2061
  {
1848
2062
  VALUE str;
1849
2063
 
@@ -1858,58 +2072,73 @@ gzfile_write_raw(gz)
1858
2072
  }
1859
2073
 
1860
2074
  static VALUE
1861
- gzfile_read_raw(gz)
1862
- struct gzfile *gz;
2075
+ gzfile_read_raw_partial(VALUE arg)
1863
2076
  {
2077
+ struct gzfile *gz = (struct gzfile*)arg;
1864
2078
  VALUE str;
1865
2079
 
1866
- str = rb_funcall(gz->io, id_read, 1, INT2FIX(GZFILE_READ_SIZE));
1867
- if (!NIL_P(str)) {
1868
- Check_Type(str, T_STRING);
1869
- }
2080
+ str = rb_funcall(gz->io, id_readpartial, 1, INT2FIX(GZFILE_READ_SIZE));
2081
+ Check_Type(str, T_STRING);
1870
2082
  return str;
1871
2083
  }
1872
2084
 
2085
+ static VALUE
2086
+ gzfile_read_raw_rescue(VALUE arg)
2087
+ {
2088
+ struct gzfile *gz = (struct gzfile*)arg;
2089
+ VALUE str = Qnil;
2090
+ if (rb_obj_is_kind_of(rb_errinfo(), rb_eNoMethodError)) {
2091
+ str = rb_funcall(gz->io, id_read, 1, INT2FIX(GZFILE_READ_SIZE));
2092
+ if (!NIL_P(str)) {
2093
+ Check_Type(str, T_STRING);
2094
+ }
2095
+ }
2096
+ return str; /* return nil when EOFError */
2097
+ }
2098
+
2099
+ static VALUE
2100
+ gzfile_read_raw(struct gzfile *gz)
2101
+ {
2102
+ return rb_rescue2(gzfile_read_raw_partial, (VALUE)gz,
2103
+ gzfile_read_raw_rescue, (VALUE)gz,
2104
+ rb_eEOFError, rb_eNoMethodError, (VALUE)0);
2105
+ }
2106
+
1873
2107
  static int
1874
- gzfile_read_raw_ensure(gz, size)
1875
- struct gzfile *gz;
1876
- int size;
2108
+ gzfile_read_raw_ensure(struct gzfile *gz, long size)
1877
2109
  {
1878
2110
  VALUE str;
1879
2111
 
1880
- while (NIL_P(gz->z.input) || RSTRING(gz->z.input)->len < size) {
2112
+ while (NIL_P(gz->z.input) || RSTRING_LEN(gz->z.input) < size) {
1881
2113
  str = gzfile_read_raw(gz);
1882
- if (NIL_P(str)) return Qfalse;
2114
+ if (NIL_P(str)) return 0;
1883
2115
  zstream_append_input2(&gz->z, str);
1884
2116
  }
1885
- return Qtrue;
2117
+ return 1;
1886
2118
  }
1887
2119
 
1888
2120
  static char *
1889
- gzfile_read_raw_until_zero(gz, offset)
1890
- struct gzfile *gz;
1891
- long offset;
2121
+ gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
1892
2122
  {
1893
2123
  VALUE str;
1894
2124
  char *p;
1895
2125
 
1896
2126
  for (;;) {
1897
- p = memchr(RSTRING(gz->z.input)->ptr + offset, '\0',
1898
- RSTRING(gz->z.input)->len - offset);
2127
+ p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0',
2128
+ RSTRING_LEN(gz->z.input) - offset);
1899
2129
  if (p) break;
1900
2130
  str = gzfile_read_raw(gz);
1901
2131
  if (NIL_P(str)) {
1902
2132
  rb_raise(cGzError, "unexpected end of file");
1903
2133
  }
1904
- offset = RSTRING(gz->z.input)->len;
2134
+ offset = RSTRING_LEN(gz->z.input);
1905
2135
  zstream_append_input2(&gz->z, str);
1906
2136
  }
1907
2137
  return p;
1908
2138
  }
1909
2139
 
1910
2140
  static unsigned int
1911
- gzfile_get16(src)
1912
- const unsigned char *src;
2141
+ gzfile_get16(const unsigned char *src)
1913
2142
  {
1914
2143
  unsigned int n;
1915
2144
  n = *(src++) & 0xff;
@@ -1918,8 +2147,7 @@ gzfile_get16(src)
1918
2147
  }
1919
2148
 
1920
2149
  static unsigned long
1921
- gzfile_get32(src)
1922
- const unsigned char *src;
2150
+ gzfile_get32(const unsigned char *src)
1923
2151
  {
1924
2152
  unsigned long n;
1925
2153
  n = *(src++) & 0xff;
@@ -1930,9 +2158,7 @@ gzfile_get32(src)
1930
2158
  }
1931
2159
 
1932
2160
  static void
1933
- gzfile_set32(n, dst)
1934
- unsigned long n;
1935
- unsigned char *dst;
2161
+ gzfile_set32(unsigned long n, unsigned char *dst)
1936
2162
  {
1937
2163
  *(dst++) = n & 0xff;
1938
2164
  *(dst++) = (n >> 8) & 0xff;
@@ -1941,10 +2167,39 @@ gzfile_set32(n, dst)
1941
2167
  }
1942
2168
 
1943
2169
  static void
1944
- gzfile_make_header(gz)
1945
- struct gzfile *gz;
2170
+ gzfile_raise(struct gzfile *gz, VALUE klass, const char *message)
2171
+ {
2172
+ VALUE exc = rb_exc_new2(klass, message);
2173
+ if (!NIL_P(gz->z.input)) {
2174
+ rb_ivar_set(exc, id_input, rb_str_dup(gz->z.input));
2175
+ }
2176
+ rb_exc_raise(exc);
2177
+ }
2178
+
2179
+ /*
2180
+ * Document-method: Zlib::GzipFile::Error#inspect
2181
+ *
2182
+ * Constructs a String of the GzipFile Error
2183
+ */
2184
+ static VALUE
2185
+ gzfile_error_inspect(VALUE error)
2186
+ {
2187
+ VALUE str = rb_call_super(0, 0);
2188
+ VALUE input = rb_attr_get(error, id_input);
2189
+
2190
+ if (!NIL_P(input)) {
2191
+ rb_str_resize(str, RSTRING_LEN(str)-1);
2192
+ rb_str_cat2(str, ", input=");
2193
+ rb_str_append(str, rb_str_inspect(input));
2194
+ rb_str_cat2(str, ">");
2195
+ }
2196
+ return str;
2197
+ }
2198
+
2199
+ static void
2200
+ gzfile_make_header(struct gzfile *gz)
1946
2201
  {
1947
- unsigned char buf[10]; /* the size of gzip header */
2202
+ Bytef buf[10]; /* the size of gzip header */
1948
2203
  unsigned char flags = 0, extraflags = 0;
1949
2204
 
1950
2205
  if (!NIL_P(gz->orig_name)) {
@@ -1968,28 +2223,27 @@ gzfile_make_header(gz)
1968
2223
  buf[1] = GZ_MAGIC2;
1969
2224
  buf[2] = GZ_METHOD_DEFLATE;
1970
2225
  buf[3] = flags;
1971
- gzfile_set32(gz->mtime, &buf[4]);
2226
+ gzfile_set32((unsigned long)gz->mtime, &buf[4]);
1972
2227
  buf[8] = extraflags;
1973
2228
  buf[9] = gz->os_code;
1974
2229
  zstream_append_buffer(&gz->z, buf, sizeof(buf));
1975
2230
 
1976
2231
  if (!NIL_P(gz->orig_name)) {
1977
2232
  zstream_append_buffer2(&gz->z, gz->orig_name);
1978
- zstream_append_buffer(&gz->z, "\0", 1);
2233
+ zstream_append_buffer(&gz->z, (Bytef*)"\0", 1);
1979
2234
  }
1980
2235
  if (!NIL_P(gz->comment)) {
1981
2236
  zstream_append_buffer2(&gz->z, gz->comment);
1982
- zstream_append_buffer(&gz->z, "\0", 1);
2237
+ zstream_append_buffer(&gz->z, (Bytef*)"\0", 1);
1983
2238
  }
1984
2239
 
1985
2240
  gz->z.flags |= GZFILE_FLAG_HEADER_FINISHED;
1986
2241
  }
1987
2242
 
1988
2243
  static void
1989
- gzfile_make_footer(gz)
1990
- struct gzfile *gz;
2244
+ gzfile_make_footer(struct gzfile *gz)
1991
2245
  {
1992
- unsigned char buf[8]; /* 8 is the size of gzip footer */
2246
+ Bytef buf[8]; /* 8 is the size of gzip footer */
1993
2247
 
1994
2248
  gzfile_set32(gz->crc, buf);
1995
2249
  gzfile_set32(gz->z.stream.total_in, &buf[4]);
@@ -1998,21 +2252,20 @@ gzfile_make_footer(gz)
1998
2252
  }
1999
2253
 
2000
2254
  static void
2001
- gzfile_read_header(gz)
2002
- struct gzfile *gz;
2255
+ gzfile_read_header(struct gzfile *gz)
2003
2256
  {
2004
2257
  const unsigned char *head;
2005
2258
  long len;
2006
2259
  char flags, *p;
2007
2260
 
2008
2261
  if (!gzfile_read_raw_ensure(gz, 10)) { /* 10 is the size of gzip header */
2009
- rb_raise(cGzError, "not in gzip format");
2262
+ gzfile_raise(gz, cGzError, "not in gzip format");
2010
2263
  }
2011
2264
 
2012
- head = RSTRING(gz->z.input)->ptr;
2265
+ head = (unsigned char*)RSTRING_PTR(gz->z.input);
2013
2266
 
2014
2267
  if (head[0] != GZ_MAGIC1 || head[1] != GZ_MAGIC2) {
2015
- rb_raise(cGzError, "not in gzip format");
2268
+ gzfile_raise(gz, cGzError, "not in gzip format");
2016
2269
  }
2017
2270
  if (head[2] != GZ_METHOD_DEFLATE) {
2018
2271
  rb_raise(cGzError, "unsupported compression method %d", head[2]);
@@ -2047,46 +2300,51 @@ gzfile_read_header(gz)
2047
2300
  if (!gzfile_read_raw_ensure(gz, 2)) {
2048
2301
  rb_raise(cGzError, "unexpected end of file");
2049
2302
  }
2050
- len = gzfile_get16(RSTRING(gz->z.input)->ptr);
2303
+ len = gzfile_get16((Bytef*)RSTRING_PTR(gz->z.input));
2051
2304
  if (!gzfile_read_raw_ensure(gz, 2 + len)) {
2052
2305
  rb_raise(cGzError, "unexpected end of file");
2053
2306
  }
2054
2307
  zstream_discard_input(&gz->z, 2 + len);
2055
2308
  }
2056
2309
  if (flags & GZ_FLAG_ORIG_NAME) {
2310
+ if (!gzfile_read_raw_ensure(gz, 1)) {
2311
+ rb_raise(cGzError, "unexpected end of file");
2312
+ }
2057
2313
  p = gzfile_read_raw_until_zero(gz, 0);
2058
- len = p - RSTRING(gz->z.input)->ptr;
2059
- gz->orig_name = rb_str_new(RSTRING(gz->z.input)->ptr, len);
2314
+ len = p - RSTRING_PTR(gz->z.input);
2315
+ gz->orig_name = rb_str_new(RSTRING_PTR(gz->z.input), len);
2060
2316
  OBJ_TAINT(gz->orig_name); /* for safe */
2061
2317
  zstream_discard_input(&gz->z, len + 1);
2062
2318
  }
2063
2319
  if (flags & GZ_FLAG_COMMENT) {
2320
+ if (!gzfile_read_raw_ensure(gz, 1)) {
2321
+ rb_raise(cGzError, "unexpected end of file");
2322
+ }
2064
2323
  p = gzfile_read_raw_until_zero(gz, 0);
2065
- len = p - RSTRING(gz->z.input)->ptr;
2066
- gz->comment = rb_str_new(RSTRING(gz->z.input)->ptr, len);
2324
+ len = p - RSTRING_PTR(gz->z.input);
2325
+ gz->comment = rb_str_new(RSTRING_PTR(gz->z.input), len);
2067
2326
  OBJ_TAINT(gz->comment); /* for safe */
2068
2327
  zstream_discard_input(&gz->z, len + 1);
2069
2328
  }
2070
2329
 
2071
- if (gz->z.input != Qnil && RSTRING(gz->z.input)->len > 0) {
2330
+ if (gz->z.input != Qnil && RSTRING_LEN(gz->z.input) > 0) {
2072
2331
  zstream_run(&gz->z, 0, 0, Z_SYNC_FLUSH);
2073
2332
  }
2074
2333
  }
2075
2334
 
2076
2335
  static void
2077
- gzfile_check_footer(gz)
2078
- struct gzfile *gz;
2336
+ gzfile_check_footer(struct gzfile *gz)
2079
2337
  {
2080
2338
  unsigned long crc, length;
2081
2339
 
2082
2340
  gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED;
2083
2341
 
2084
2342
  if (!gzfile_read_raw_ensure(gz, 8)) { /* 8 is the size of gzip footer */
2085
- rb_raise(cNoFooter, "footer is not found");
2343
+ gzfile_raise(gz, cNoFooter, "footer is not found");
2086
2344
  }
2087
2345
 
2088
- crc = gzfile_get32(RSTRING(gz->z.input)->ptr);
2089
- length = gzfile_get32(RSTRING(gz->z.input)->ptr + 4);
2346
+ crc = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input));
2347
+ length = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input) + 4);
2090
2348
 
2091
2349
  gz->z.stream.total_in += 8; /* to rewind correctly */
2092
2350
  zstream_discard_input(&gz->z, 8);
@@ -2094,23 +2352,20 @@ gzfile_check_footer(gz)
2094
2352
  if (gz->crc != crc) {
2095
2353
  rb_raise(cCRCError, "invalid compressed data -- crc error");
2096
2354
  }
2097
- if ((gz->z.stream.total_out & 0xFFFFFFFFUL) != length) {
2355
+ if ((uint32_t)gz->z.stream.total_out != length) {
2098
2356
  rb_raise(cLengthError, "invalid compressed data -- length error");
2099
2357
  }
2100
2358
  }
2101
2359
 
2102
2360
  static void
2103
- gzfile_write(gz, str, len)
2104
- struct gzfile *gz;
2105
- Bytef *str;
2106
- uInt len;
2361
+ gzfile_write(struct gzfile *gz, Bytef *str, long len)
2107
2362
  {
2108
2363
  if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2109
2364
  gzfile_make_header(gz);
2110
2365
  }
2111
2366
 
2112
2367
  if (len > 0 || (gz->z.flags & GZFILE_FLAG_SYNC)) {
2113
- gz->crc = crc32(gz->crc, str, len);
2368
+ gz->crc = checksum_long(crc32, gz->crc, str, len);
2114
2369
  zstream_run(&gz->z, str, len, (gz->z.flags & GZFILE_FLAG_SYNC)
2115
2370
  ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2116
2371
  }
@@ -2118,8 +2373,7 @@ gzfile_write(gz, str, len)
2118
2373
  }
2119
2374
 
2120
2375
  static long
2121
- gzfile_read_more(gz)
2122
- struct gzfile *gz;
2376
+ gzfile_read_more(struct gzfile *gz)
2123
2377
  {
2124
2378
  volatile VALUE str;
2125
2379
 
@@ -2131,8 +2385,8 @@ gzfile_read_more(gz)
2131
2385
  }
2132
2386
  break;
2133
2387
  }
2134
- if (RSTRING(str)->len > 0) { /* prevent Z_BUF_ERROR */
2135
- zstream_run(&gz->z, RSTRING(str)->ptr, RSTRING(str)->len,
2388
+ if (RSTRING_LEN(str) > 0) { /* prevent Z_BUF_ERROR */
2389
+ zstream_run(&gz->z, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str),
2136
2390
  Z_SYNC_FLUSH);
2137
2391
  }
2138
2392
  if (gz->z.buf_filled > 0) break;
@@ -2141,31 +2395,47 @@ gzfile_read_more(gz)
2141
2395
  }
2142
2396
 
2143
2397
  static void
2144
- gzfile_calc_crc(gz, str)
2145
- struct gzfile *gz;
2146
- VALUE str;
2398
+ gzfile_calc_crc(struct gzfile *gz, VALUE str)
2147
2399
  {
2148
- if (RSTRING(str)->len <= gz->ungetc) {
2149
- gz->ungetc -= RSTRING(str)->len;
2400
+ if (RSTRING_LEN(str) <= gz->ungetc) {
2401
+ gz->ungetc -= RSTRING_LEN(str);
2150
2402
  }
2151
2403
  else {
2152
- gz->crc = crc32(gz->crc, RSTRING(str)->ptr + gz->ungetc,
2153
- RSTRING(str)->len - gz->ungetc);
2404
+ gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc,
2405
+ RSTRING_LEN(str) - gz->ungetc);
2154
2406
  gz->ungetc = 0;
2155
2407
  }
2156
2408
  }
2157
2409
 
2158
2410
  static VALUE
2159
- gzfile_read(gz, len)
2160
- struct gzfile *gz;
2161
- int len;
2411
+ gzfile_newstr(struct gzfile *gz, VALUE str)
2162
2412
  {
2163
- VALUE dst;
2413
+ if (!gz->enc2) {
2414
+ rb_enc_associate(str, gz->enc);
2415
+ OBJ_TAINT(str); /* for safe */
2416
+ return str;
2417
+ }
2418
+ /* TODO: Encodings
2419
+ *
2420
+ if (gz->ec && rb_enc_dummy_p(gz->enc2)) {
2421
+ str = rb_econv_str_convert(gz->ec, str, ECONV_PARTIAL_INPUT);
2422
+ rb_enc_associate(str, gz->enc);
2423
+ OBJ_TAINT(str);
2424
+ return str;
2425
+ }
2426
+ return rb_str_conv_enc_opts(str, gz->enc2, gz->enc,
2427
+ gz->ecflags, gz->ecopts);
2428
+ */
2429
+ return str;
2430
+ }
2164
2431
 
2432
+ static long
2433
+ gzfile_fill(struct gzfile *gz, long len)
2434
+ {
2165
2435
  if (len < 0)
2166
- rb_raise(rb_eArgError, "negative length %d given", len);
2436
+ rb_raise(rb_eArgError, "negative length %ld given", len);
2167
2437
  if (len == 0)
2168
- return rb_str_new(0, 0);
2438
+ return 0;
2169
2439
  while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) {
2170
2440
  gzfile_read_more(gz);
2171
2441
  }
@@ -2173,51 +2443,155 @@ gzfile_read(gz, len)
2173
2443
  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2174
2444
  gzfile_check_footer(gz);
2175
2445
  }
2176
- return Qnil;
2446
+ return -1;
2177
2447
  }
2448
+ return len < gz->z.buf_filled ? len : gz->z.buf_filled;
2449
+ }
2450
+
2451
+ static VALUE
2452
+ gzfile_read(struct gzfile *gz, long len)
2453
+ {
2454
+ VALUE dst;
2178
2455
 
2456
+ len = gzfile_fill(gz, len);
2457
+ if (len == 0) return rb_str_new(0, 0);
2458
+ if (len < 0) return Qnil;
2179
2459
  dst = zstream_shift_buffer(&gz->z, len);
2180
2460
  gzfile_calc_crc(gz, dst);
2181
-
2182
- OBJ_TAINT(dst); /* for safe */
2183
2461
  return dst;
2184
2462
  }
2185
2463
 
2186
2464
  static VALUE
2187
- gzfile_read_all(gz)
2188
- struct gzfile *gz;
2465
+ gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
2189
2466
  {
2190
2467
  VALUE dst;
2191
2468
 
2192
- while (!ZSTREAM_IS_FINISHED(&gz->z)) {
2469
+ if (len < 0)
2470
+ rb_raise(rb_eArgError, "negative length %ld given", len);
2471
+
2472
+ if (!NIL_P(outbuf))
2473
+ OBJ_TAINT(outbuf);
2474
+
2475
+ if (len == 0) {
2476
+ if (NIL_P(outbuf))
2477
+ return rb_str_new(0, 0);
2478
+ else {
2479
+ rb_str_resize(outbuf, 0);
2480
+ return outbuf;
2481
+ }
2482
+ }
2483
+ while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled == 0) {
2193
2484
  gzfile_read_more(gz);
2194
2485
  }
2195
2486
  if (GZFILE_IS_FINISHED(gz)) {
2196
2487
  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2197
2488
  gzfile_check_footer(gz);
2198
2489
  }
2199
- return rb_str_new(0, 0);
2490
+ if (!NIL_P(outbuf))
2491
+ rb_str_resize(outbuf, 0);
2492
+ rb_raise(rb_eEOFError, "end of file reached");
2200
2493
  }
2201
2494
 
2202
- dst = zstream_detach_buffer(&gz->z);
2495
+ dst = zstream_shift_buffer(&gz->z, len);
2203
2496
  gzfile_calc_crc(gz, dst);
2204
2497
 
2498
+ if (!NIL_P(outbuf)) {
2499
+ rb_str_resize(outbuf, RSTRING_LEN(dst));
2500
+ memcpy(RSTRING_PTR(outbuf), RSTRING_PTR(dst), RSTRING_LEN(dst));
2501
+ dst = outbuf;
2502
+ }
2205
2503
  OBJ_TAINT(dst); /* for safe */
2206
2504
  return dst;
2207
2505
  }
2208
2506
 
2209
- static void
2210
- gzfile_ungetc(gz, c)
2211
- struct gzfile *gz;
2212
- int c;
2507
+ static VALUE
2508
+ gzfile_read_all(struct gzfile *gz)
2509
+ {
2510
+ VALUE dst;
2511
+
2512
+ while (!ZSTREAM_IS_FINISHED(&gz->z)) {
2513
+ gzfile_read_more(gz);
2514
+ }
2515
+ if (GZFILE_IS_FINISHED(gz)) {
2516
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2517
+ gzfile_check_footer(gz);
2518
+ }
2519
+ return rb_str_new(0, 0);
2520
+ }
2521
+
2522
+ dst = zstream_detach_buffer(&gz->z);
2523
+ gzfile_calc_crc(gz, dst);
2524
+ OBJ_TAINT(dst);
2525
+ return gzfile_newstr(gz, dst);
2526
+ }
2527
+
2528
+ static VALUE
2529
+ gzfile_getc(struct gzfile *gz)
2530
+ {
2531
+ VALUE buf, dst = 0;
2532
+ int len;
2533
+
2534
+ len = rb_enc_mbmaxlen(gz->enc);
2535
+ while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) {
2536
+ gzfile_read_more(gz);
2537
+ }
2538
+ if (GZFILE_IS_FINISHED(gz)) {
2539
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2540
+ gzfile_check_footer(gz);
2541
+ }
2542
+ return Qnil;
2543
+ }
2544
+
2545
+ /* TODO: Encodings
2546
+ *
2547
+ if (gz->ec && rb_enc_dummy_p(gz->enc2)) {
2548
+ const unsigned char *ss, *sp, *se;
2549
+ unsigned char *ds, *dp, *de;
2550
+ rb_econv_result_t res;
2551
+
2552
+ if (!gz->cbuf) {
2553
+ gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA);
2554
+ }
2555
+ ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf);
2556
+ se = sp + gz->z.buf_filled;
2557
+ ds = dp = (unsigned char *)gz->cbuf;
2558
+ de = (unsigned char *)ds + GZFILE_CBUF_CAPA;
2559
+ res = rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT);
2560
+ rb_econv_check_error(gz->ec);
2561
+ dst = zstream_shift_buffer(&gz->z, sp - ss);
2562
+ gzfile_calc_crc(gz, dst);
2563
+ dst = rb_str_new(gz->cbuf, dp - ds);
2564
+ rb_enc_associate(dst, gz->enc);
2565
+ OBJ_TAINT(dst);
2566
+ return dst;
2567
+ }
2568
+ else {
2569
+ */
2570
+ buf = gz->z.buf;
2571
+ len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), gz->enc);
2572
+ dst = gzfile_read(gz, len);
2573
+ return gzfile_newstr(gz, dst);
2574
+ /* TODO: Encodings
2575
+ }
2576
+ */
2577
+ }
2578
+
2579
+ static void
2580
+ gzfile_ungets(struct gzfile *gz, const Bytef *b, long len)
2581
+ {
2582
+ zstream_buffer_ungets(&gz->z, b, len);
2583
+ gz->ungetc+=len;
2584
+ }
2585
+
2586
+ static void
2587
+ gzfile_ungetbyte(struct gzfile *gz, int c)
2213
2588
  {
2214
- zstream_buffer_ungetc(&gz->z, c);
2589
+ zstream_buffer_ungetbyte(&gz->z, c);
2215
2590
  gz->ungetc++;
2216
2591
  }
2217
2592
 
2218
2593
  static VALUE
2219
- gzfile_writer_end_run(arg)
2220
- VALUE arg;
2594
+ gzfile_writer_end_run(VALUE arg)
2221
2595
  {
2222
2596
  struct gzfile *gz = (struct gzfile *)arg;
2223
2597
 
@@ -2225,7 +2599,7 @@ gzfile_writer_end_run(arg)
2225
2599
  gzfile_make_header(gz);
2226
2600
  }
2227
2601
 
2228
- zstream_run(&gz->z, "", 0, Z_FINISH);
2602
+ zstream_run(&gz->z, (Bytef*)"", 0, Z_FINISH);
2229
2603
  gzfile_make_footer(gz);
2230
2604
  gzfile_write_raw(gz);
2231
2605
 
@@ -2233,8 +2607,7 @@ gzfile_writer_end_run(arg)
2233
2607
  }
2234
2608
 
2235
2609
  static void
2236
- gzfile_writer_end(gz)
2237
- struct gzfile *gz;
2610
+ gzfile_writer_end(struct gzfile *gz)
2238
2611
  {
2239
2612
  if (ZSTREAM_IS_CLOSING(&gz->z)) return;
2240
2613
  gz->z.flags |= ZSTREAM_FLAG_CLOSING;
@@ -2243,8 +2616,7 @@ gzfile_writer_end(gz)
2243
2616
  }
2244
2617
 
2245
2618
  static VALUE
2246
- gzfile_reader_end_run(arg)
2247
- VALUE arg;
2619
+ gzfile_reader_end_run(VALUE arg)
2248
2620
  {
2249
2621
  struct gzfile *gz = (struct gzfile *)arg;
2250
2622
 
@@ -2257,8 +2629,7 @@ gzfile_reader_end_run(arg)
2257
2629
  }
2258
2630
 
2259
2631
  static void
2260
- gzfile_reader_end(gz)
2261
- struct gzfile *gz;
2632
+ gzfile_reader_end(struct gzfile *gz)
2262
2633
  {
2263
2634
  if (ZSTREAM_IS_CLOSING(&gz->z)) return;
2264
2635
  gz->z.flags |= ZSTREAM_FLAG_CLOSING;
@@ -2267,14 +2638,13 @@ gzfile_reader_end(gz)
2267
2638
  }
2268
2639
 
2269
2640
  static void
2270
- gzfile_reader_rewind(gz)
2271
- struct gzfile *gz;
2641
+ gzfile_reader_rewind(struct gzfile *gz)
2272
2642
  {
2273
2643
  long n;
2274
2644
 
2275
2645
  n = gz->z.stream.total_in;
2276
2646
  if (!NIL_P(gz->z.input)) {
2277
- n += RSTRING(gz->z.input)->len;
2647
+ n += RSTRING_LEN(gz->z.input);
2278
2648
  }
2279
2649
 
2280
2650
  rb_funcall(gz->io, id_seek, 2, rb_int2inum(-n), INT2FIX(1));
@@ -2282,8 +2652,7 @@ gzfile_reader_rewind(gz)
2282
2652
  }
2283
2653
 
2284
2654
  static VALUE
2285
- gzfile_reader_get_unused(gz)
2286
- struct gzfile *gz;
2655
+ gzfile_reader_get_unused(struct gzfile *gz)
2287
2656
  {
2288
2657
  VALUE str;
2289
2658
 
@@ -2300,8 +2669,7 @@ gzfile_reader_get_unused(gz)
2300
2669
  }
2301
2670
 
2302
2671
  static struct gzfile *
2303
- get_gzfile(obj)
2304
- VALUE obj;
2672
+ get_gzfile(VALUE obj)
2305
2673
  {
2306
2674
  struct gzfile *gz;
2307
2675
 
@@ -2323,12 +2691,51 @@ get_gzfile(obj)
2323
2691
  * Zlib::GzipReader for reading, and Zlib::GzipWriter for writing.
2324
2692
  *
2325
2693
  * GzipReader should be used by associating an IO, or IO-like, object.
2694
+ *
2695
+ * == Method Catalogue
2696
+ *
2697
+ * - ::wrap
2698
+ * - ::open (Zlib::GzipReader::open and Zlib::GzipWriter::open)
2699
+ * - #close
2700
+ * - #closed?
2701
+ * - #comment
2702
+ * - comment= (Zlib::GzipWriter#comment=)
2703
+ * - #crc
2704
+ * - eof? (Zlib::GzipReader#eof?)
2705
+ * - #finish
2706
+ * - #level
2707
+ * - lineno (Zlib::GzipReader#lineno)
2708
+ * - lineno= (Zlib::GzipReader#lineno=)
2709
+ * - #mtime
2710
+ * - mtime= (Zlib::GzipWriter#mtime=)
2711
+ * - #orig_name
2712
+ * - orig_name (Zlib::GzipWriter#orig_name=)
2713
+ * - #os_code
2714
+ * - path (when the underlying IO supports #path)
2715
+ * - #sync
2716
+ * - #sync=
2717
+ * - #to_io
2718
+ *
2719
+ * (due to internal structure, documentation may appear under Zlib::GzipReader
2720
+ * or Zlib::GzipWriter)
2326
2721
  */
2327
2722
 
2328
2723
 
2724
+ typedef struct {
2725
+ int argc;
2726
+ VALUE *argv;
2727
+ VALUE klass;
2728
+ } new_wrap_arg_t;
2729
+
2329
2730
  static VALUE
2330
- gzfile_ensure_close(obj)
2331
- VALUE obj;
2731
+ new_wrap(VALUE tmp)
2732
+ {
2733
+ new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp;
2734
+ return rb_class_new_instance(arg->argc, arg->argv, arg->klass);
2735
+ }
2736
+
2737
+ static VALUE
2738
+ gzfile_ensure_close(VALUE obj)
2332
2739
  {
2333
2740
  struct gzfile *gz;
2334
2741
 
@@ -2339,16 +2746,26 @@ gzfile_ensure_close(obj)
2339
2746
  return Qnil;
2340
2747
  }
2341
2748
 
2342
- /*
2343
- * See Zlib::GzipReader#wrap and Zlib::GzipWriter#wrap.
2344
- */
2345
2749
  static VALUE
2346
- rb_gzfile_s_wrap(argc, argv, klass)
2347
- int argc;
2348
- VALUE *argv;
2349
- VALUE klass;
2750
+ gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error)
2350
2751
  {
2351
- VALUE obj = rb_class_new_instance(argc, argv, klass);
2752
+ VALUE obj;
2753
+
2754
+ if (close_io_on_error) {
2755
+ int state = 0;
2756
+ new_wrap_arg_t arg;
2757
+ arg.argc = argc;
2758
+ arg.argv = argv;
2759
+ arg.klass = klass;
2760
+ obj = rb_protect(new_wrap, (VALUE)&arg, &state);
2761
+ if (state) {
2762
+ rb_io_close(argv[0]);
2763
+ rb_jump_tag(state);
2764
+ }
2765
+ }
2766
+ else {
2767
+ obj = rb_class_new_instance(argc, argv, klass);
2768
+ }
2352
2769
 
2353
2770
  if (rb_block_given_p()) {
2354
2771
  return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj);
@@ -2359,14 +2776,30 @@ rb_gzfile_s_wrap(argc, argv, klass)
2359
2776
  }
2360
2777
 
2361
2778
  /*
2779
+ * Document-method: Zlib::GzipFile.wrap
2780
+ *
2781
+ * call-seq: Zlib::GzipFile.wrap(io) { |gz| ... }
2782
+ *
2783
+ * Creates a GzipFile object associated with +io+, and
2784
+ * executes the block with the newly created GzipFile object,
2785
+ * just like File.open. The GzipFile object will be closed
2786
+ * automatically after executing the block. If you want to keep
2787
+ * the associated IO object opening, you may call
2788
+ * +Zlib::GzipFile#finish+ method in the block.
2789
+ */
2790
+ static VALUE
2791
+ rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass)
2792
+ {
2793
+ return gzfile_wrap(argc, argv, klass, 0);
2794
+ }
2795
+
2796
+ /*
2797
+ * Document-method: Zlib::GzipFile.open
2798
+ *
2362
2799
  * See Zlib::GzipReader#open and Zlib::GzipWriter#open.
2363
2800
  */
2364
2801
  static VALUE
2365
- gzfile_s_open(argc, argv, klass, mode)
2366
- int argc;
2367
- VALUE *argv;
2368
- VALUE klass;
2369
- const char *mode;
2802
+ gzfile_s_open(int argc, VALUE *argv, VALUE klass, const char *mode)
2370
2803
  {
2371
2804
  VALUE io, filename;
2372
2805
 
@@ -2374,70 +2807,74 @@ gzfile_s_open(argc, argv, klass, mode)
2374
2807
  rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
2375
2808
  }
2376
2809
  filename = argv[0];
2377
- SafeStringValue(filename);
2378
- io = rb_file_open(RSTRING(filename)->ptr, mode);
2379
-
2810
+ io = rb_file_open_str(filename, mode);
2380
2811
  argv[0] = io;
2381
- return rb_gzfile_s_wrap(argc, argv, klass);
2812
+ return gzfile_wrap(argc, argv, klass, 1);
2382
2813
  }
2383
2814
 
2384
2815
  /*
2816
+ * Document-method: Zlib::GzipFile#to_io
2817
+ *
2385
2818
  * Same as IO.
2386
2819
  */
2387
2820
  static VALUE
2388
- rb_gzfile_to_io(obj)
2389
- VALUE obj;
2821
+ rb_gzfile_to_io(VALUE obj)
2390
2822
  {
2391
2823
  return get_gzfile(obj)->io;
2392
2824
  }
2393
2825
 
2394
2826
  /*
2827
+ * Document-method: Zlib::GzipFile#crc
2828
+ *
2395
2829
  * Returns CRC value of the uncompressed data.
2396
2830
  */
2397
2831
  static VALUE
2398
- rb_gzfile_crc(obj)
2399
- VALUE obj;
2832
+ rb_gzfile_crc(VALUE obj)
2400
2833
  {
2401
2834
  return rb_uint2inum(get_gzfile(obj)->crc);
2402
2835
  }
2403
2836
 
2404
2837
  /*
2838
+ * Document-method: Zlib::GzipFile#mtime
2839
+ *
2405
2840
  * Returns last modification time recorded in the gzip file header.
2406
2841
  */
2407
2842
  static VALUE
2408
- rb_gzfile_mtime(obj)
2409
- VALUE obj;
2843
+ rb_gzfile_mtime(VALUE obj)
2410
2844
  {
2411
2845
  return rb_time_new(get_gzfile(obj)->mtime, (time_t)0);
2412
2846
  }
2413
2847
 
2414
2848
  /*
2849
+ * Document-method: Zlib::GzipFile#level
2850
+ *
2415
2851
  * Returns compression level.
2416
2852
  */
2417
2853
  static VALUE
2418
- rb_gzfile_level(obj)
2419
- VALUE obj;
2854
+ rb_gzfile_level(VALUE obj)
2420
2855
  {
2421
2856
  return INT2FIX(get_gzfile(obj)->level);
2422
2857
  }
2423
2858
 
2424
2859
  /*
2860
+ * Document-method: Zlib::GzipFile#os_code
2861
+ *
2425
2862
  * Returns OS code number recorded in the gzip file header.
2426
2863
  */
2427
2864
  static VALUE
2428
- rb_gzfile_os_code(obj)
2429
- VALUE obj;
2865
+ rb_gzfile_os_code(VALUE obj)
2430
2866
  {
2431
2867
  return INT2FIX(get_gzfile(obj)->os_code);
2432
2868
  }
2433
2869
 
2434
2870
  /*
2871
+ * Document-method: Zlib::GzipFile#orig_name
2872
+ *
2435
2873
  * Returns original filename recorded in the gzip file header, or +nil+ if
2436
2874
  * original filename is not present.
2437
2875
  */
2438
2876
  static VALUE
2439
- rb_gzfile_orig_name(obj)
2440
- VALUE obj;
2877
+ rb_gzfile_orig_name(VALUE obj)
2441
2878
  {
2442
2879
  VALUE str = get_gzfile(obj)->orig_name;
2443
2880
  if (!NIL_P(str)) {
@@ -2448,12 +2885,13 @@ rb_gzfile_orig_name(obj)
2448
2885
  }
2449
2886
 
2450
2887
  /*
2888
+ * Document-method: Zlib::GzipFile#comment
2889
+ *
2451
2890
  * Returns comments recorded in the gzip file header, or nil if the comments
2452
2891
  * is not present.
2453
2892
  */
2454
2893
  static VALUE
2455
- rb_gzfile_comment(obj)
2456
- VALUE obj;
2894
+ rb_gzfile_comment(VALUE obj)
2457
2895
  {
2458
2896
  VALUE str = get_gzfile(obj)->comment;
2459
2897
  if (!NIL_P(str)) {
@@ -2464,21 +2902,23 @@ rb_gzfile_comment(obj)
2464
2902
  }
2465
2903
 
2466
2904
  /*
2467
- * ???
2905
+ * Document-method: Zlib::GzipFile#lineno
2906
+ *
2907
+ * The line number of the last row read from this file.
2468
2908
  */
2469
2909
  static VALUE
2470
- rb_gzfile_lineno(obj)
2471
- VALUE obj;
2910
+ rb_gzfile_lineno(VALUE obj)
2472
2911
  {
2473
2912
  return INT2NUM(get_gzfile(obj)->lineno);
2474
2913
  }
2475
2914
 
2476
2915
  /*
2477
- * ???
2916
+ * Document-method: Zlib::GzipReader#lineno=
2917
+ *
2918
+ * Specify line number of the last row read from this file.
2478
2919
  */
2479
2920
  static VALUE
2480
- rb_gzfile_set_lineno(obj, lineno)
2481
- VALUE obj, lineno;
2921
+ rb_gzfile_set_lineno(VALUE obj, VALUE lineno)
2482
2922
  {
2483
2923
  struct gzfile *gz = get_gzfile(obj);
2484
2924
  gz->lineno = NUM2INT(lineno);
@@ -2486,11 +2926,13 @@ rb_gzfile_set_lineno(obj, lineno)
2486
2926
  }
2487
2927
 
2488
2928
  /*
2489
- * ???
2929
+ * Document-method: Zlib::GzipWriter#mtime=
2930
+ *
2931
+ * Specify the modification time (+mtime+) in the gzip header.
2932
+ * Using a Fixnum or Integer
2490
2933
  */
2491
2934
  static VALUE
2492
- rb_gzfile_set_mtime(obj, mtime)
2493
- VALUE obj, mtime;
2935
+ rb_gzfile_set_mtime(VALUE obj, VALUE mtime)
2494
2936
  {
2495
2937
  struct gzfile *gz = get_gzfile(obj);
2496
2938
  VALUE val;
@@ -2504,17 +2946,18 @@ rb_gzfile_set_mtime(obj, mtime)
2504
2946
  }
2505
2947
  else {
2506
2948
  val = rb_Integer(mtime);
2507
- gz->mtime = FIXNUM_P(val) ? FIX2INT(val) : rb_big2ulong(val);
2949
+ gz->mtime = FIXNUM_P(val) ? FIX2UINT(val) : rb_big2ulong(val);
2508
2950
  }
2509
2951
  return mtime;
2510
2952
  }
2511
2953
 
2512
2954
  /*
2513
- * ???
2955
+ * Document-method: Zlib::GzipFile#orig_name=
2956
+ *
2957
+ * Specify the original name (+str+) in the gzip header.
2514
2958
  */
2515
2959
  static VALUE
2516
- rb_gzfile_set_orig_name(obj, str)
2517
- VALUE obj, str;
2960
+ rb_gzfile_set_orig_name(VALUE obj, VALUE str)
2518
2961
  {
2519
2962
  struct gzfile *gz = get_gzfile(obj);
2520
2963
  VALUE s;
@@ -2524,20 +2967,21 @@ rb_gzfile_set_orig_name(obj, str)
2524
2967
  rb_raise(cGzError, "header is already written");
2525
2968
  }
2526
2969
  s = rb_str_dup(rb_str_to_str(str));
2527
- p = memchr(RSTRING(s)->ptr, '\0', RSTRING(s)->len);
2970
+ p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s));
2528
2971
  if (p) {
2529
- rb_str_resize(s, p - RSTRING(s)->ptr);
2972
+ rb_str_resize(s, p - RSTRING_PTR(s));
2530
2973
  }
2531
2974
  gz->orig_name = s;
2532
2975
  return str;
2533
2976
  }
2534
2977
 
2535
2978
  /*
2536
- * ???
2979
+ * Document-method: Zlib::GzipFile#comment=
2980
+ *
2981
+ * Specify the comment (+str+) in the gzip header.
2537
2982
  */
2538
2983
  static VALUE
2539
- rb_gzfile_set_comment(obj, str)
2540
- VALUE obj, str;
2984
+ rb_gzfile_set_comment(VALUE obj, VALUE str)
2541
2985
  {
2542
2986
  struct gzfile *gz = get_gzfile(obj);
2543
2987
  VALUE s;
@@ -2547,21 +2991,22 @@ rb_gzfile_set_comment(obj, str)
2547
2991
  rb_raise(cGzError, "header is already written");
2548
2992
  }
2549
2993
  s = rb_str_dup(rb_str_to_str(str));
2550
- p = memchr(RSTRING(s)->ptr, '\0', RSTRING(s)->len);
2994
+ p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s));
2551
2995
  if (p) {
2552
- rb_str_resize(s, p - RSTRING(s)->ptr);
2996
+ rb_str_resize(s, p - RSTRING_PTR(s));
2553
2997
  }
2554
2998
  gz->comment = s;
2555
2999
  return str;
2556
3000
  }
2557
3001
 
2558
3002
  /*
3003
+ * Document-method: Zlib::GzipFile#close
3004
+ *
2559
3005
  * Closes the GzipFile object. This method calls close method of the
2560
3006
  * associated IO object. Returns the associated IO object.
2561
3007
  */
2562
3008
  static VALUE
2563
- rb_gzfile_close(obj)
2564
- VALUE obj;
3009
+ rb_gzfile_close(VALUE obj)
2565
3010
  {
2566
3011
  struct gzfile *gz = get_gzfile(obj);
2567
3012
  VALUE io;
@@ -2572,13 +3017,14 @@ rb_gzfile_close(obj)
2572
3017
  }
2573
3018
 
2574
3019
  /*
3020
+ * Document-method: Zlib::GzipFile#finish
3021
+ *
2575
3022
  * Closes the GzipFile object. Unlike Zlib::GzipFile#close, this method never
2576
3023
  * calls the close method of the associated IO object. Returns the associated IO
2577
3024
  * object.
2578
3025
  */
2579
3026
  static VALUE
2580
- rb_gzfile_finish(obj)
2581
- VALUE obj;
3027
+ rb_gzfile_finish(VALUE obj)
2582
3028
  {
2583
3029
  struct gzfile *gz = get_gzfile(obj);
2584
3030
  VALUE io;
@@ -2589,11 +3035,13 @@ rb_gzfile_finish(obj)
2589
3035
  }
2590
3036
 
2591
3037
  /*
2592
- * Same as IO.
3038
+ * Document-method: Zlib::GzipFile#closed?
3039
+ *
3040
+ * Same as IO#closed?
3041
+ *
2593
3042
  */
2594
3043
  static VALUE
2595
- rb_gzfile_closed_p(obj)
2596
- VALUE obj;
3044
+ rb_gzfile_closed_p(VALUE obj)
2597
3045
  {
2598
3046
  struct gzfile *gz;
2599
3047
  Data_Get_Struct(obj, struct gzfile, gz);
@@ -2601,27 +3049,32 @@ rb_gzfile_closed_p(obj)
2601
3049
  }
2602
3050
 
2603
3051
  /*
2604
- * ???
3052
+ * Document-method: Zlib::GzipFile#eof?
3053
+ *
3054
+ * Returns +true+ or +false+ whether the stream has reached the end.
2605
3055
  */
2606
3056
  static VALUE
2607
- rb_gzfile_eof_p(obj)
2608
- VALUE obj;
3057
+ rb_gzfile_eof_p(VALUE obj)
2609
3058
  {
2610
3059
  struct gzfile *gz = get_gzfile(obj);
2611
3060
  return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse;
2612
3061
  }
2613
3062
 
2614
3063
  /*
2615
- * Same as IO.
3064
+ * Document-method: Zlib::GzipFile#sync
3065
+ *
3066
+ * Same as IO#sync
3067
+ *
2616
3068
  */
2617
3069
  static VALUE
2618
- rb_gzfile_sync(obj)
2619
- VALUE obj;
3070
+ rb_gzfile_sync(VALUE obj)
2620
3071
  {
2621
3072
  return (get_gzfile(obj)->z.flags & GZFILE_FLAG_SYNC) ? Qtrue : Qfalse;
2622
3073
  }
2623
3074
 
2624
3075
  /*
3076
+ * Document-method: Zlib::GzipFile#sync=
3077
+ *
2625
3078
  * call-seq: sync = flag
2626
3079
  *
2627
3080
  * Same as IO. If flag is +true+, the associated IO object must respond to the
@@ -2629,8 +3082,7 @@ rb_gzfile_sync(obj)
2629
3082
  * decreases sharply.
2630
3083
  */
2631
3084
  static VALUE
2632
- rb_gzfile_set_sync(obj, mode)
2633
- VALUE obj, mode;
3085
+ rb_gzfile_set_sync(VALUE obj, VALUE mode)
2634
3086
  {
2635
3087
  struct gzfile *gz = get_gzfile(obj);
2636
3088
 
@@ -2644,26 +3096,60 @@ rb_gzfile_set_sync(obj, mode)
2644
3096
  }
2645
3097
 
2646
3098
  /*
2647
- * ???
3099
+ * Document-method: Zlib::GzipFile#total_in
3100
+ *
3101
+ * Total number of input bytes read so far.
2648
3102
  */
2649
3103
  static VALUE
2650
- rb_gzfile_total_in(obj)
2651
- VALUE obj;
3104
+ rb_gzfile_total_in(VALUE obj)
2652
3105
  {
2653
3106
  return rb_uint2inum(get_gzfile(obj)->z.stream.total_in);
2654
3107
  }
2655
3108
 
2656
3109
  /*
2657
- * ???
3110
+ * Document-method: Zlib::GzipFile#total_out
3111
+ *
3112
+ * Total number of output bytes output so far.
2658
3113
  */
2659
3114
  static VALUE
2660
- rb_gzfile_total_out(obj)
2661
- VALUE obj;
3115
+ rb_gzfile_total_out(VALUE obj)
2662
3116
  {
2663
3117
  struct gzfile *gz = get_gzfile(obj);
2664
3118
  return rb_uint2inum(gz->z.stream.total_out - gz->z.buf_filled);
2665
3119
  }
2666
3120
 
3121
+ /*
3122
+ * Document-method: Zlib::GzipFile#path
3123
+ *
3124
+ * call-seq: path
3125
+ *
3126
+ * Returns the path string of the associated IO-like object. This
3127
+ * method is only defined when the IO-like object responds to #path().
3128
+ */
3129
+ static VALUE
3130
+ rb_gzfile_path(VALUE obj)
3131
+ {
3132
+ struct gzfile *gz;
3133
+ Data_Get_Struct(obj, struct gzfile, gz);
3134
+ return gz->path;
3135
+ }
3136
+
3137
+ static void
3138
+ rb_gzfile_ecopts(struct gzfile *gz, VALUE opts)
3139
+ {
3140
+ /* TODO: Encodings
3141
+ *
3142
+ if (!NIL_P(opts)) {
3143
+ rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2, NULL);
3144
+ }
3145
+ if (gz->enc2) {
3146
+ gz->ecflags = rb_econv_prepare_opts(opts, &opts);
3147
+ gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name,
3148
+ gz->ecflags, opts);
3149
+ gz->ecopts = opts;
3150
+ }
3151
+ */
3152
+ }
2667
3153
 
2668
3154
  /* ------------------------------------------------------------------------- */
2669
3155
 
@@ -2671,9 +3157,9 @@ rb_gzfile_total_out(obj)
2671
3157
  * Document-class: Zlib::GzipWriter
2672
3158
  *
2673
3159
  * Zlib::GzipWriter is a class for writing gzipped files. GzipWriter should
2674
- * be used with an instance of IO, or IO-like, object.
3160
+ * be used with an instance of IO, or IO-like, object.
2675
3161
  *
2676
- * For example:
3162
+ * Following two example generate the same result.
2677
3163
  *
2678
3164
  * Zlib::GzipWriter.open('hoge.gz') do |gz|
2679
3165
  * gz.write 'jugemu jugemu gokou no surikire...'
@@ -2685,8 +3171,14 @@ rb_gzfile_total_out(obj)
2685
3171
  * gz.close
2686
3172
  * end
2687
3173
  *
2688
- * # TODO: test these. Are they equivalent? Can GzipWriter.new take a
2689
- * # block?
3174
+ * To make like gzip(1) does, run following:
3175
+ *
3176
+ * orig = 'hoge.txt'
3177
+ * Zlib::GzipWriter.open('hoge.gz') do |gz|
3178
+ * gz.mtime = File.mtime(orig)
3179
+ * gz.orig_name = orig
3180
+ * gz.write IO.binread(orig)
3181
+ * end
2690
3182
  *
2691
3183
  * NOTE: Due to the limitation of Ruby's finalizer, you must explicitly close
2692
3184
  * GzipWriter objects by Zlib::GzipWriter#close etc. Otherwise, GzipWriter
@@ -2695,8 +3187,7 @@ rb_gzfile_total_out(obj)
2695
3187
  */
2696
3188
 
2697
3189
  static VALUE
2698
- rb_gzwriter_s_allocate(klass)
2699
- VALUE klass;
3190
+ rb_gzwriter_s_allocate(VALUE klass)
2700
3191
  {
2701
3192
  return gzfile_writer_new(klass);
2702
3193
  }
@@ -2706,13 +3197,10 @@ rb_gzwriter_s_allocate(klass)
2706
3197
  *
2707
3198
  * Opens a file specified by +filename+ for writing gzip compressed data, and
2708
3199
  * returns a GzipWriter object associated with that file. Further details of
2709
- * this method are found in Zlib::GzipWriter.new and Zlib::GzipWriter#wrap.
3200
+ * this method are found in Zlib::GzipWriter.new and Zlib::GzipFile.wrap.
2710
3201
  */
2711
3202
  static VALUE
2712
- rb_gzwriter_s_open(argc, argv, klass)
2713
- int argc;
2714
- VALUE *argv;
2715
- VALUE klass;
3203
+ rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass)
2716
3204
  {
2717
3205
  return gzfile_s_open(argc, argv, klass, "wb");
2718
3206
  }
@@ -2726,15 +3214,17 @@ rb_gzwriter_s_open(argc, argv, klass)
2726
3214
  * +write+ method that behaves same as write method in IO class.
2727
3215
  */
2728
3216
  static VALUE
2729
- rb_gzwriter_initialize(argc, argv, obj)
2730
- int argc;
2731
- VALUE *argv;
2732
- VALUE obj;
3217
+ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj)
2733
3218
  {
2734
3219
  struct gzfile *gz;
2735
- VALUE io, level, strategy;
3220
+ VALUE io, level, strategy, opt = Qnil;
2736
3221
  int err;
2737
3222
 
3223
+ if (argc > 1) {
3224
+ opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
3225
+ if (!NIL_P(opt)) argc--;
3226
+ }
3227
+
2738
3228
  rb_scan_args(argc, argv, "12", &io, &level, &strategy);
2739
3229
  Data_Get_Struct(obj, struct gzfile, gz);
2740
3230
 
@@ -2747,6 +3237,12 @@ rb_gzwriter_initialize(argc, argv, obj)
2747
3237
  }
2748
3238
  gz->io = io;
2749
3239
  ZSTREAM_READY(&gz->z);
3240
+ rb_gzfile_ecopts(gz, opt);
3241
+
3242
+ if (rb_respond_to(io, id_path)) {
3243
+ gz->path = rb_funcall(gz->io, id_path, 0);
3244
+ rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
3245
+ }
2750
3246
 
2751
3247
  return obj;
2752
3248
  }
@@ -2759,10 +3255,7 @@ rb_gzwriter_initialize(argc, argv, obj)
2759
3255
  * +flush+ is omitted. It is no use giving flush <tt>Zlib::NO_FLUSH</tt>.
2760
3256
  */
2761
3257
  static VALUE
2762
- rb_gzwriter_flush(argc, argv, obj)
2763
- int argc;
2764
- VALUE *argv;
2765
- VALUE obj;
3258
+ rb_gzwriter_flush(int argc, VALUE *argv, VALUE obj)
2766
3259
  {
2767
3260
  struct gzfile *gz = get_gzfile(obj);
2768
3261
  VALUE v_flush;
@@ -2772,7 +3265,7 @@ rb_gzwriter_flush(argc, argv, obj)
2772
3265
 
2773
3266
  flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
2774
3267
  if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */
2775
- zstream_run(&gz->z, "", 0, flush);
3268
+ zstream_run(&gz->z, (Bytef*)"", 0, flush);
2776
3269
  }
2777
3270
 
2778
3271
  gzfile_write_raw(gz);
@@ -2786,29 +3279,29 @@ rb_gzwriter_flush(argc, argv, obj)
2786
3279
  * Same as IO.
2787
3280
  */
2788
3281
  static VALUE
2789
- rb_gzwriter_write(obj, str)
2790
- VALUE obj, str;
3282
+ rb_gzwriter_write(VALUE obj, VALUE str)
2791
3283
  {
2792
3284
  struct gzfile *gz = get_gzfile(obj);
2793
3285
 
2794
- if (TYPE(str) != T_STRING) {
3286
+ if (TYPE(str) != T_STRING)
2795
3287
  str = rb_obj_as_string(str);
3288
+ if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3289
+ str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2);
2796
3290
  }
2797
- gzfile_write(gz, RSTRING(str)->ptr, RSTRING(str)->len);
2798
- return INT2FIX(RSTRING(str)->len);
3291
+ gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
3292
+ return INT2FIX(RSTRING_LEN(str));
2799
3293
  }
2800
3294
 
2801
3295
  /*
2802
3296
  * Same as IO.
2803
3297
  */
2804
3298
  static VALUE
2805
- rb_gzwriter_putc(obj, ch)
2806
- VALUE obj, ch;
3299
+ rb_gzwriter_putc(VALUE obj, VALUE ch)
2807
3300
  {
2808
3301
  struct gzfile *gz = get_gzfile(obj);
2809
3302
  char c = NUM2CHR(ch);
2810
3303
 
2811
- gzfile_write(gz, &c, 1);
3304
+ gzfile_write(gz, (Bytef*)&c, 1);
2812
3305
  return ch;
2813
3306
  }
2814
3307
 
@@ -2854,9 +3347,6 @@ rb_gzwriter_putc(obj, ch)
2854
3347
  * gz.close
2855
3348
  * end
2856
3349
  *
2857
- * # TODO: test these. Are they equivalent? Can GzipReader.new take a
2858
- * # block?
2859
- *
2860
3350
  * == Method Catalogue
2861
3351
  *
2862
3352
  * The following methods in Zlib::GzipReader are just like their counterparts
@@ -2894,29 +3384,29 @@ rb_gzwriter_putc(obj, ch)
2894
3384
  */
2895
3385
 
2896
3386
  static VALUE
2897
- rb_gzreader_s_allocate(klass)
2898
- VALUE klass;
3387
+ rb_gzreader_s_allocate(VALUE klass)
2899
3388
  {
2900
3389
  return gzfile_reader_new(klass);
2901
3390
  }
2902
3391
 
2903
3392
  /*
3393
+ * Document-method: Zlib::GzipReader.open
3394
+ *
2904
3395
  * call-seq: Zlib::GzipReader.open(filename) {|gz| ... }
2905
3396
  *
2906
3397
  * Opens a file specified by +filename+ as a gzipped file, and returns a
2907
3398
  * GzipReader object associated with that file. Further details of this method
2908
- * are in Zlib::GzipReader.new and ZLib::GzipReader.wrap.
3399
+ * are in Zlib::GzipReader.new and ZLib::GzipFile.wrap.
2909
3400
  */
2910
3401
  static VALUE
2911
- rb_gzreader_s_open(argc, argv, klass)
2912
- int argc;
2913
- VALUE *argv;
2914
- VALUE klass;
3402
+ rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass)
2915
3403
  {
2916
3404
  return gzfile_s_open(argc, argv, klass, "rb");
2917
3405
  }
2918
3406
 
2919
3407
  /*
3408
+ * Document-method: Zlib::GzipReader.new
3409
+ *
2920
3410
  * call-seq: Zlib::GzipReader.new(io)
2921
3411
  *
2922
3412
  * Creates a GzipReader object associated with +io+. The GzipReader object reads
@@ -2927,13 +3417,14 @@ rb_gzreader_s_open(argc, argv, klass)
2927
3417
  * exception.
2928
3418
  */
2929
3419
  static VALUE
2930
- rb_gzreader_initialize(obj, io)
2931
- VALUE obj, io;
3420
+ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj)
2932
3421
  {
3422
+ VALUE io, opt = Qnil;
2933
3423
  struct gzfile *gz;
2934
3424
  int err;
2935
3425
 
2936
3426
  Data_Get_Struct(obj, struct gzfile, gz);
3427
+ rb_scan_args(argc, argv, "1:", &io, &opt);
2937
3428
 
2938
3429
  /* this is undocumented feature of zlib */
2939
3430
  err = inflateInit2(&gz->z.stream, -MAX_WBITS);
@@ -2943,17 +3434,24 @@ rb_gzreader_initialize(obj, io)
2943
3434
  gz->io = io;
2944
3435
  ZSTREAM_READY(&gz->z);
2945
3436
  gzfile_read_header(gz);
3437
+ rb_gzfile_ecopts(gz, opt);
3438
+
3439
+ if (rb_respond_to(io, id_path)) {
3440
+ gz->path = rb_funcall(gz->io, id_path, 0);
3441
+ rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
3442
+ }
2946
3443
 
2947
3444
  return obj;
2948
3445
  }
2949
3446
 
2950
3447
  /*
3448
+ * Document-method: Zlib::GzipReader#rewind
3449
+ *
2951
3450
  * Resets the position of the file pointer to the point created the GzipReader
2952
3451
  * object. The associated IO object needs to respond to the +seek+ method.
2953
3452
  */
2954
3453
  static VALUE
2955
- rb_gzreader_rewind(obj)
2956
- VALUE obj;
3454
+ rb_gzreader_rewind(VALUE obj)
2957
3455
  {
2958
3456
  struct gzfile *gz = get_gzfile(obj);
2959
3457
  gzfile_reader_rewind(gz);
@@ -2961,12 +3459,13 @@ rb_gzreader_rewind(obj)
2961
3459
  }
2962
3460
 
2963
3461
  /*
3462
+ * Document-method: Zlib::GzipReader#unused
3463
+ *
2964
3464
  * Returns the rest of the data which had read for parsing gzip format, or
2965
3465
  * +nil+ if the whole gzip file is not parsed yet.
2966
3466
  */
2967
3467
  static VALUE
2968
- rb_gzreader_unused(obj)
2969
- VALUE obj;
3468
+ rb_gzreader_unused(VALUE obj)
2970
3469
  {
2971
3470
  struct gzfile *gz;
2972
3471
  Data_Get_Struct(obj, struct gzfile, gz);
@@ -2974,17 +3473,16 @@ rb_gzreader_unused(obj)
2974
3473
  }
2975
3474
 
2976
3475
  /*
3476
+ * Document-method: Zlib::GzipReader#read
3477
+ *
2977
3478
  * See Zlib::GzipReader documentation for a description.
2978
3479
  */
2979
3480
  static VALUE
2980
- rb_gzreader_read(argc, argv, obj)
2981
- int argc;
2982
- VALUE *argv;
2983
- VALUE obj;
3481
+ rb_gzreader_read(int argc, VALUE *argv, VALUE obj)
2984
3482
  {
2985
3483
  struct gzfile *gz = get_gzfile(obj);
2986
3484
  VALUE vlen;
2987
- int len;
3485
+ long len;
2988
3486
 
2989
3487
  rb_scan_args(argc, argv, "01", &vlen);
2990
3488
  if (NIL_P(vlen)) {
@@ -2993,37 +3491,98 @@ rb_gzreader_read(argc, argv, obj)
2993
3491
 
2994
3492
  len = NUM2INT(vlen);
2995
3493
  if (len < 0) {
2996
- rb_raise(rb_eArgError, "negative length %d given", len);
3494
+ rb_raise(rb_eArgError, "negative length %ld given", len);
2997
3495
  }
2998
3496
  return gzfile_read(gz, len);
2999
3497
  }
3000
3498
 
3001
3499
  /*
3500
+ * Document-method: Zlib::GzipReader#readpartial
3501
+ *
3502
+ * call-seq:
3503
+ * gzipreader.readpartial(maxlen [, outbuf]) => string, outbuf
3504
+ *
3505
+ * Reads at most <i>maxlen</i> bytes from the gziped stream but
3506
+ * it blocks only if <em>gzipreader</em> has no data immediately available.
3507
+ * If the optional <i>outbuf</i> argument is present,
3508
+ * it must reference a String, which will receive the data.
3509
+ * It raises <code>EOFError</code> on end of file.
3510
+ */
3511
+ static VALUE
3512
+ rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj)
3513
+ {
3514
+ struct gzfile *gz = get_gzfile(obj);
3515
+ VALUE vlen, outbuf;
3516
+ long len;
3517
+
3518
+ rb_scan_args(argc, argv, "11", &vlen, &outbuf);
3519
+
3520
+ len = NUM2INT(vlen);
3521
+ if (len < 0) {
3522
+ rb_raise(rb_eArgError, "negative length %ld given", len);
3523
+ }
3524
+ if (!NIL_P(outbuf))
3525
+ Check_Type(outbuf, T_STRING);
3526
+ return gzfile_readpartial(gz, len, outbuf);
3527
+ }
3528
+
3529
+ /*
3530
+ * Document-method: Zlib::GzipReader#getc
3531
+ *
3002
3532
  * See Zlib::GzipReader documentation for a description.
3003
3533
  */
3004
3534
  static VALUE
3005
- rb_gzreader_getc(obj)
3006
- VALUE obj;
3535
+ rb_gzreader_getc(VALUE obj)
3536
+ {
3537
+ struct gzfile *gz = get_gzfile(obj);
3538
+
3539
+ return gzfile_getc(gz);
3540
+ }
3541
+
3542
+ /*
3543
+ * Document-method: Zlib::GzipReader#readchar
3544
+ *
3545
+ * See Zlib::GzipReader documentation for a description.
3546
+ */
3547
+ static VALUE
3548
+ rb_gzreader_readchar(VALUE obj)
3549
+ {
3550
+ VALUE dst;
3551
+ dst = rb_gzreader_getc(obj);
3552
+ if (NIL_P(dst)) {
3553
+ rb_raise(rb_eEOFError, "end of file reached");
3554
+ }
3555
+ return dst;
3556
+ }
3557
+
3558
+ /*
3559
+ * Document-method: Zlib::GzipReader#getbyte
3560
+ *
3561
+ * See Zlib::GzipReader documentation for a description.
3562
+ */
3563
+ static VALUE
3564
+ rb_gzreader_getbyte(VALUE obj)
3007
3565
  {
3008
3566
  struct gzfile *gz = get_gzfile(obj);
3009
3567
  VALUE dst;
3010
3568
 
3011
3569
  dst = gzfile_read(gz, 1);
3012
3570
  if (!NIL_P(dst)) {
3013
- dst = INT2FIX((unsigned int)(RSTRING(dst)->ptr[0]) & 0xff);
3571
+ dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff);
3014
3572
  }
3015
3573
  return dst;
3016
3574
  }
3017
3575
 
3018
3576
  /*
3577
+ * Document-method: Zlib::GzipReader#readbyte
3578
+ *
3019
3579
  * See Zlib::GzipReader documentation for a description.
3020
3580
  */
3021
3581
  static VALUE
3022
- rb_gzreader_readchar(obj)
3023
- VALUE obj;
3582
+ rb_gzreader_readbyte(VALUE obj)
3024
3583
  {
3025
3584
  VALUE dst;
3026
- dst = rb_gzreader_getc(obj);
3585
+ dst = rb_gzreader_getbyte(obj);
3027
3586
  if (NIL_P(dst)) {
3028
3587
  rb_raise(rb_eEOFError, "end of file reached");
3029
3588
  }
@@ -3031,13 +3590,17 @@ rb_gzreader_readchar(obj)
3031
3590
  }
3032
3591
 
3033
3592
  /*
3593
+ * Document-method: Zlib::GzipReader#each_char
3594
+ *
3034
3595
  * See Zlib::GzipReader documentation for a description.
3035
3596
  */
3036
3597
  static VALUE
3037
- rb_gzreader_each_byte(obj)
3038
- VALUE obj;
3598
+ rb_gzreader_each_char(VALUE obj)
3039
3599
  {
3040
3600
  VALUE c;
3601
+
3602
+ RETURN_ENUMERATOR(obj, 0, 0);
3603
+
3041
3604
  while (!NIL_P(c = rb_gzreader_getc(obj))) {
3042
3605
  rb_yield(c);
3043
3606
  }
@@ -3045,20 +3608,59 @@ rb_gzreader_each_byte(obj)
3045
3608
  }
3046
3609
 
3047
3610
  /*
3611
+ * Document-method: Zlib::GzipReader#each_byte
3612
+ *
3048
3613
  * See Zlib::GzipReader documentation for a description.
3049
3614
  */
3050
3615
  static VALUE
3051
- rb_gzreader_ungetc(obj, ch)
3052
- VALUE obj, ch;
3616
+ rb_gzreader_each_byte(VALUE obj)
3617
+ {
3618
+ VALUE c;
3619
+
3620
+ RETURN_ENUMERATOR(obj, 0, 0);
3621
+
3622
+ while (!NIL_P(c = rb_gzreader_getbyte(obj))) {
3623
+ rb_yield(c);
3624
+ }
3625
+ return Qnil;
3626
+ }
3627
+
3628
+ /*
3629
+ * Document-method: Zlib::GzipReader#ungetc
3630
+ *
3631
+ * See Zlib::GzipReader documentation for a description.
3632
+ */
3633
+ static VALUE
3634
+ rb_gzreader_ungetc(VALUE obj, VALUE s)
3635
+ {
3636
+ struct gzfile *gz;
3637
+
3638
+ if (FIXNUM_P(s))
3639
+ return rb_gzreader_ungetbyte(obj, s);
3640
+ gz = get_gzfile(obj);
3641
+ StringValue(s);
3642
+ if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3643
+ s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2);
3644
+ }
3645
+ gzfile_ungets(gz, (const Bytef*)RSTRING_PTR(s), RSTRING_LEN(s));
3646
+ return Qnil;
3647
+ }
3648
+
3649
+ /*
3650
+ * Document-method: Zlib::GzipReader#ungetbyte
3651
+ *
3652
+ * See Zlib::GzipReader documentation for a description.
3653
+ */
3654
+ static VALUE
3655
+ rb_gzreader_ungetbyte(VALUE obj, VALUE ch)
3053
3656
  {
3054
3657
  struct gzfile *gz = get_gzfile(obj);
3055
- gzfile_ungetc(gz, NUM2CHR(ch));
3658
+ gzfile_ungetbyte(gz, NUM2CHR(ch));
3056
3659
  return Qnil;
3057
3660
  }
3058
3661
 
3059
3662
  static void
3060
- gzreader_skip_linebreaks(gz)
3061
- struct gzfile *gz;
3663
+ gzreader_skip_linebreaks(struct gzfile *gz)
3062
3664
  {
3063
3665
  VALUE str;
3064
3666
  char *p;
@@ -3069,7 +3671,7 @@ gzreader_skip_linebreaks(gz)
3069
3671
  gzfile_read_more(gz);
3070
3672
  }
3071
3673
  n = 0;
3072
- p = RSTRING(gz->z.buf)->ptr;
3674
+ p = RSTRING_PTR(gz->z.buf);
3073
3675
 
3074
3676
  while (n++, *(p++) == '\n') {
3075
3677
  if (n >= gz->z.buf_filled) {
@@ -3080,7 +3682,7 @@ gzreader_skip_linebreaks(gz)
3080
3682
  gzfile_read_more(gz);
3081
3683
  }
3082
3684
  n = 0;
3083
- p = RSTRING(gz->z.buf)->ptr;
3685
+ p = RSTRING_PTR(gz->z.buf);
3084
3686
  }
3085
3687
  }
3086
3688
 
@@ -3089,53 +3691,102 @@ gzreader_skip_linebreaks(gz)
3089
3691
  }
3090
3692
 
3091
3693
  static void
3092
- rscheck(rsptr, rslen, rs)
3093
- char *rsptr;
3094
- long rslen;
3095
- VALUE rs;
3694
+ rscheck(const char *rsptr, long rslen, VALUE rs)
3096
3695
  {
3097
- if (RSTRING(rs)->ptr != rsptr && RSTRING(rs)->len != rslen)
3696
+ if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
3098
3697
  rb_raise(rb_eRuntimeError, "rs modified");
3099
3698
  }
3100
3699
 
3700
+ static long
3701
+ gzreader_charboundary(struct gzfile *gz, long n)
3702
+ {
3703
+ char *s = RSTRING_PTR(gz->z.buf);
3704
+ char *e = s + gz->z.buf_filled;
3705
+ char *p = rb_enc_left_char_head(s, s + n, e, gz->enc);
3706
+ long l = p - s;
3707
+ if (l < n) {
3708
+ n = rb_enc_precise_mbclen(p, e, gz->enc);
3709
+ if (ONIGENC_MBCLEN_NEEDMORE_P(n)) {
3710
+ if ((l = gzfile_fill(gz, l + ONIGENC_MBCLEN_NEEDMORE_LEN(n))) > 0) {
3711
+ return l;
3712
+ }
3713
+ }
3714
+ else if (ONIGENC_MBCLEN_CHARFOUND_P(n)) {
3715
+ return l + ONIGENC_MBCLEN_CHARFOUND_LEN(n);
3716
+ }
3717
+ }
3718
+ return n;
3719
+ }
3720
+
3101
3721
  static VALUE
3102
- gzreader_gets(argc, argv, obj)
3103
- int argc;
3104
- VALUE *argv;
3105
- VALUE obj;
3722
+ gzreader_gets(int argc, VALUE *argv, VALUE obj)
3106
3723
  {
3107
3724
  struct gzfile *gz = get_gzfile(obj);
3108
3725
  volatile VALUE rs;
3109
3726
  VALUE dst;
3110
- char *rsptr, *p, *res;
3111
- long rslen, n;
3727
+ const char *rsptr;
3728
+ char *p, *res;
3729
+ long rslen, n, limit = -1;
3112
3730
  int rspara;
3731
+ rb_encoding *enc = gz->enc;
3732
+ int maxlen = rb_enc_mbmaxlen(enc);
3113
3733
 
3114
3734
  if (argc == 0) {
3115
3735
  rs = rb_rs;
3116
3736
  }
3117
3737
  else {
3118
- rb_scan_args(argc, argv, "1", &rs);
3119
- if (!NIL_P(rs)) {
3120
- Check_Type(rs, T_STRING);
3738
+ VALUE lim, tmp;
3739
+
3740
+ rb_scan_args(argc, argv, "11", &rs, &lim);
3741
+ if (!NIL_P(lim)) {
3742
+ if (!NIL_P(rs)) StringValue(rs);
3743
+ }
3744
+ else if (!NIL_P(rs)) {
3745
+ tmp = rb_check_string_type(rs);
3746
+ if (NIL_P(tmp)) {
3747
+ lim = rs;
3748
+ rs = rb_rs;
3749
+ }
3750
+ else {
3751
+ rs = tmp;
3752
+ }
3753
+ }
3754
+ if (!NIL_P(lim)) {
3755
+ limit = NUM2LONG(lim);
3756
+ if (limit == 0) return rb_str_new(0,0);
3121
3757
  }
3122
3758
  }
3123
3759
 
3124
3760
  if (NIL_P(rs)) {
3125
- dst = gzfile_read_all(gz);
3126
- if (RSTRING(dst)->len != 0) gz->lineno++;
3127
- else
3128
- return Qnil;
3761
+ if (limit < 0) {
3762
+ dst = gzfile_read_all(gz);
3763
+ if (RSTRING_LEN(dst) == 0) return Qnil;
3764
+ }
3765
+ else if ((n = gzfile_fill(gz, limit)) <= 0) {
3766
+ return Qnil;
3767
+ }
3768
+ else {
3769
+ if (maxlen > 1 && n >= limit && !GZFILE_IS_FINISHED(gz)) {
3770
+ n = gzreader_charboundary(gz, n);
3771
+ }
3772
+ else {
3773
+ n = limit;
3774
+ }
3775
+ dst = zstream_shift_buffer(&gz->z, n);
3776
+ gzfile_calc_crc(gz, dst);
3777
+ dst = gzfile_newstr(gz, dst);
3778
+ }
3779
+ gz->lineno++;
3129
3780
  return dst;
3130
3781
  }
3131
3782
 
3132
- if (RSTRING(rs)->len == 0) {
3783
+ if (RSTRING_LEN(rs) == 0) {
3133
3784
  rsptr = "\n\n";
3134
3785
  rslen = 2;
3135
3786
  rspara = 1;
3136
3787
  } else {
3137
- rsptr = RSTRING(rs)->ptr;
3138
- rslen = RSTRING(rs)->len;
3788
+ rsptr = RSTRING_PTR(rs);
3789
+ rslen = RSTRING_LEN(rs);
3139
3790
  rspara = 0;
3140
3791
  }
3141
3792
 
@@ -3151,18 +3802,25 @@ gzreader_gets(argc, argv, obj)
3151
3802
  gzfile_read_more(gz);
3152
3803
  }
3153
3804
 
3154
- p = RSTRING(gz->z.buf)->ptr;
3805
+ p = RSTRING_PTR(gz->z.buf);
3155
3806
  n = rslen;
3156
3807
  for (;;) {
3808
+ long filled;
3157
3809
  if (n > gz->z.buf_filled) {
3158
3810
  if (ZSTREAM_IS_FINISHED(&gz->z)) break;
3159
3811
  gzfile_read_more(gz);
3160
- p = RSTRING(gz->z.buf)->ptr + n - rslen;
3812
+ p = RSTRING_PTR(gz->z.buf) + n - rslen;
3161
3813
  }
3162
3814
  if (!rspara) rscheck(rsptr, rslen, rs);
3163
- res = memchr(p, rsptr[0], (gz->z.buf_filled - n + 1));
3815
+ filled = gz->z.buf_filled;
3816
+ if (limit > 0 && filled >= limit) {
3817
+ filled = limit;
3818
+ }
3819
+ res = memchr(p, rsptr[0], (filled - n + 1));
3164
3820
  if (!res) {
3165
- n = gz->z.buf_filled + 1;
3821
+ n = filled;
3822
+ if (limit > 0 && filled >= limit) break;
3823
+ n++;
3166
3824
  } else {
3167
3825
  n += (long)(res - p);
3168
3826
  p = res;
@@ -3170,6 +3828,9 @@ gzreader_gets(argc, argv, obj)
3170
3828
  p++, n++;
3171
3829
  }
3172
3830
  }
3831
+ if (maxlen > 1 && n == limit && (gz->z.buf_filled > n || !ZSTREAM_IS_FINISHED(&gz->z))) {
3832
+ n = gzreader_charboundary(gz, n);
3833
+ }
3173
3834
 
3174
3835
  gz->lineno++;
3175
3836
  dst = gzfile_read(gz, n);
@@ -3177,17 +3838,16 @@ gzreader_gets(argc, argv, obj)
3177
3838
  gzreader_skip_linebreaks(gz);
3178
3839
  }
3179
3840
 
3180
- return dst;
3841
+ return gzfile_newstr(gz, dst);
3181
3842
  }
3182
3843
 
3183
3844
  /*
3845
+ * Document-method: Zlib::GzipReader#gets
3846
+ *
3184
3847
  * See Zlib::GzipReader documentation for a description.
3185
3848
  */
3186
3849
  static VALUE
3187
- rb_gzreader_gets(argc, argv, obj)
3188
- int argc;
3189
- VALUE *argv;
3190
- VALUE obj;
3850
+ rb_gzreader_gets(int argc, VALUE *argv, VALUE obj)
3191
3851
  {
3192
3852
  VALUE dst;
3193
3853
  dst = gzreader_gets(argc, argv, obj);
@@ -3198,13 +3858,12 @@ rb_gzreader_gets(argc, argv, obj)
3198
3858
  }
3199
3859
 
3200
3860
  /*
3861
+ * Document-method: Zlib::GzipReader#readline
3862
+ *
3201
3863
  * See Zlib::GzipReader documentation for a description.
3202
3864
  */
3203
3865
  static VALUE
3204
- rb_gzreader_readline(argc, argv, obj)
3205
- int argc;
3206
- VALUE *argv;
3207
- VALUE obj;
3866
+ rb_gzreader_readline(int argc, VALUE *argv, VALUE obj)
3208
3867
  {
3209
3868
  VALUE dst;
3210
3869
  dst = rb_gzreader_gets(argc, argv, obj);
@@ -3215,15 +3874,17 @@ rb_gzreader_readline(argc, argv, obj)
3215
3874
  }
3216
3875
 
3217
3876
  /*
3877
+ * Document-method: Zlib::GzipReader#each
3878
+ *
3218
3879
  * See Zlib::GzipReader documentation for a description.
3219
3880
  */
3220
3881
  static VALUE
3221
- rb_gzreader_each(argc, argv, obj)
3222
- int argc;
3223
- VALUE *argv;
3224
- VALUE obj;
3882
+ rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
3225
3883
  {
3226
3884
  VALUE str;
3885
+
3886
+ RETURN_ENUMERATOR(obj, 0, 0);
3887
+
3227
3888
  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
3228
3889
  rb_yield(str);
3229
3890
  }
@@ -3231,13 +3892,12 @@ rb_gzreader_each(argc, argv, obj)
3231
3892
  }
3232
3893
 
3233
3894
  /*
3895
+ * Document-method: Zlib::GzipReader#readlines
3896
+ *
3234
3897
  * See Zlib::GzipReader documentation for a description.
3235
3898
  */
3236
3899
  static VALUE
3237
- rb_gzreader_readlines(argc, argv, obj)
3238
- int argc;
3239
- VALUE *argv;
3240
- VALUE obj;
3900
+ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
3241
3901
  {
3242
3902
  VALUE str, dst;
3243
3903
  dst = rb_ary_new();
@@ -3252,6 +3912,8 @@ rb_gzreader_readlines(argc, argv, obj)
3252
3912
 
3253
3913
 
3254
3914
  /*
3915
+ * Document-module: Zlib
3916
+ *
3255
3917
  * The Zlib module contains several classes for compressing and decompressing
3256
3918
  * streams, and for working with "gzip" files.
3257
3919
  *
@@ -3330,7 +3992,8 @@ rb_gzreader_readlines(argc, argv, obj)
3330
3992
  * Zlib::OS_UNKNOWN
3331
3993
  * The return values of Zlib::GzipFile#os_code method.
3332
3994
  */
3333
- void Init_zlib()
3995
+ void
3996
+ Init_zlib()
3334
3997
  {
3335
3998
  VALUE mZlib, cZStream, cDeflate, cInflate;
3336
3999
  #if GZIP_SUPPORT
@@ -3350,10 +4013,14 @@ void Init_zlib()
3350
4013
 
3351
4014
  rb_define_module_function(mZlib, "zlib_version", rb_zlib_version, 0);
3352
4015
  rb_define_module_function(mZlib, "adler32", rb_zlib_adler32, -1);
4016
+ rb_define_module_function(mZlib, "adler32_combine", rb_zlib_adler32_combine, 3);
3353
4017
  rb_define_module_function(mZlib, "crc32", rb_zlib_crc32, -1);
4018
+ rb_define_module_function(mZlib, "crc32_combine", rb_zlib_crc32_combine, 3);
3354
4019
  rb_define_module_function(mZlib, "crc_table", rb_zlib_crc_table, 0);
3355
4020
 
4021
+ /* The Ruby/zlib version string. */
3356
4022
  rb_define_const(mZlib, "VERSION", rb_str_new2(RUBY_ZLIB_VERSION));
4023
+ /* The string which represents the version of zlib.h */
3357
4024
  rb_define_const(mZlib, "ZLIB_VERSION", rb_str_new2(ZLIB_VERSION));
3358
4025
 
3359
4026
  cZStream = rb_define_class_under(mZlib, "ZStream", rb_cObject);
@@ -3376,12 +4043,19 @@ void Init_zlib()
3376
4043
  rb_define_method(cZStream, "flush_next_in", rb_zstream_flush_next_in, 0);
3377
4044
  rb_define_method(cZStream, "flush_next_out", rb_zstream_flush_next_out, 0);
3378
4045
 
4046
+ /* Integer representing date types which
4047
+ * ZStream#data_type method returns */
3379
4048
  rb_define_const(mZlib, "BINARY", INT2FIX(Z_BINARY));
4049
+ /* Integer representing date types which
4050
+ * ZStream#data_type method returns */
3380
4051
  rb_define_const(mZlib, "ASCII", INT2FIX(Z_ASCII));
4052
+ /* Integer representing date types which
4053
+ * ZStream#data_type method returns */
3381
4054
  rb_define_const(mZlib, "UNKNOWN", INT2FIX(Z_UNKNOWN));
3382
4055
 
3383
4056
  cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream);
3384
4057
  rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1);
4058
+ rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1);
3385
4059
  rb_define_alloc_func(cDeflate, rb_deflate_s_allocate);
3386
4060
  rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1);
3387
4061
  rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1);
@@ -3393,6 +4067,7 @@ void Init_zlib()
3393
4067
 
3394
4068
  cInflate = rb_define_class_under(mZlib, "Inflate", cZStream);
3395
4069
  rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1);
4070
+ rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1);
3396
4071
  rb_define_alloc_func(cInflate, rb_inflate_s_allocate);
3397
4072
  rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1);
3398
4073
  rb_define_method(cInflate, "inflate", rb_inflate_inflate, 1);
@@ -3401,35 +4076,90 @@ void Init_zlib()
3401
4076
  rb_define_method(cInflate, "sync_point?", rb_inflate_sync_point_p, 0);
3402
4077
  rb_define_method(cInflate, "set_dictionary", rb_inflate_set_dictionary, 1);
3403
4078
 
4079
+ /* compression level 0
4080
+ *
4081
+ * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
3404
4082
  rb_define_const(mZlib, "NO_COMPRESSION", INT2FIX(Z_NO_COMPRESSION));
4083
+ /* compression level 1
4084
+ *
4085
+ * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
3405
4086
  rb_define_const(mZlib, "BEST_SPEED", INT2FIX(Z_BEST_SPEED));
4087
+ /* compression level 9
4088
+ *
4089
+ * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
3406
4090
  rb_define_const(mZlib, "BEST_COMPRESSION", INT2FIX(Z_BEST_COMPRESSION));
4091
+ /* compression level -1
4092
+ *
4093
+ * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
3407
4094
  rb_define_const(mZlib, "DEFAULT_COMPRESSION",
3408
4095
  INT2FIX(Z_DEFAULT_COMPRESSION));
3409
4096
 
4097
+ /* compression method 1
4098
+ *
4099
+ * Which is an argument for Deflate.new and Deflate#params. */
3410
4100
  rb_define_const(mZlib, "FILTERED", INT2FIX(Z_FILTERED));
4101
+ /* compression method 2
4102
+ *
4103
+ * Which is an argument for Deflate.new and Deflate#params. */
3411
4104
  rb_define_const(mZlib, "HUFFMAN_ONLY", INT2FIX(Z_HUFFMAN_ONLY));
4105
+ /* compression method 0
4106
+ *
4107
+ * Which is an argument for Deflate.new and Deflate#params. */
3412
4108
  rb_define_const(mZlib, "DEFAULT_STRATEGY", INT2FIX(Z_DEFAULT_STRATEGY));
3413
4109
 
4110
+ /* The default value of windowBits which is an argument for
4111
+ * Deflate.new and Inflate.new.
4112
+ */
3414
4113
  rb_define_const(mZlib, "MAX_WBITS", INT2FIX(MAX_WBITS));
4114
+ /* Default value is 8
4115
+ *
4116
+ * The integer representing memory levels.
4117
+ * Which are an argument for Deflate.new, Deflate#params, and so on. */
3415
4118
  rb_define_const(mZlib, "DEF_MEM_LEVEL", INT2FIX(DEF_MEM_LEVEL));
4119
+ /* Maximum level is 9
4120
+ *
4121
+ * The integers representing memory levels which are an argument for
4122
+ * Deflate.new, Deflate#params, and so on. */
3416
4123
  rb_define_const(mZlib, "MAX_MEM_LEVEL", INT2FIX(MAX_MEM_LEVEL));
3417
4124
 
4125
+ /* Output control - 0
4126
+ *
4127
+ * The integers to control the output of the deflate stream, which are
4128
+ * an argument for Deflate#deflate and so on. */
3418
4129
  rb_define_const(mZlib, "NO_FLUSH", INT2FIX(Z_NO_FLUSH));
4130
+ /* Output control - 2
4131
+ *
4132
+ * The integers to control the output of the deflate stream, which are
4133
+ * an argument for Deflate#deflate and so on. */
3419
4134
  rb_define_const(mZlib, "SYNC_FLUSH", INT2FIX(Z_SYNC_FLUSH));
4135
+ /* Output control - 3
4136
+ *
4137
+ * The integers to control the output of the deflate stream, which are
4138
+ * an argument for Deflate#deflate and so on. */
3420
4139
  rb_define_const(mZlib, "FULL_FLUSH", INT2FIX(Z_FULL_FLUSH));
4140
+ /* Oputput control - 4
4141
+ *
4142
+ * The integers to control the output of the deflate stream, which are
4143
+ * an argument for Deflate#deflate and so on. */
3421
4144
  rb_define_const(mZlib, "FINISH", INT2FIX(Z_FINISH));
3422
4145
 
3423
4146
  #if GZIP_SUPPORT
3424
4147
  id_write = rb_intern("write");
3425
4148
  id_read = rb_intern("read");
4149
+ id_readpartial = rb_intern("readpartial");
3426
4150
  id_flush = rb_intern("flush");
3427
4151
  id_seek = rb_intern("seek");
3428
4152
  id_close = rb_intern("close");
4153
+ id_path = rb_intern("path");
4154
+ id_input = rb_intern("@input");
3429
4155
 
3430
4156
  cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject);
3431
4157
  cGzError = rb_define_class_under(cGzipFile, "Error", cZError);
3432
4158
 
4159
+ /* input gzipped string */
4160
+ rb_define_attr(cGzError, "input", 1, 0);
4161
+ rb_define_method(cGzError, "inspect", gzfile_error_inspect, 0);
4162
+
3433
4163
  cNoFooter = rb_define_class_under(cGzipFile, "NoFooter", cGzError);
3434
4164
  cCRCError = rb_define_class_under(cGzipFile, "CRCError", cGzError);
3435
4165
  cLengthError = rb_define_class_under(cGzipFile,"LengthError",cGzError);
@@ -3477,36 +4207,59 @@ void Init_zlib()
3477
4207
 
3478
4208
  rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1);
3479
4209
  rb_define_alloc_func(cGzipReader, rb_gzreader_s_allocate);
3480
- rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, 1);
4210
+ rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, -1);
3481
4211
  rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0);
3482
4212
  rb_define_method(cGzipReader, "unused", rb_gzreader_unused, 0);
3483
4213
  rb_define_method(cGzipReader, "read", rb_gzreader_read, -1);
4214
+ rb_define_method(cGzipReader, "readpartial", rb_gzreader_readpartial, -1);
3484
4215
  rb_define_method(cGzipReader, "getc", rb_gzreader_getc, 0);
4216
+ rb_define_method(cGzipReader, "getbyte", rb_gzreader_getbyte, 0);
3485
4217
  rb_define_method(cGzipReader, "readchar", rb_gzreader_readchar, 0);
4218
+ rb_define_method(cGzipReader, "readbyte", rb_gzreader_readbyte, 0);
3486
4219
  rb_define_method(cGzipReader, "each_byte", rb_gzreader_each_byte, 0);
4220
+ rb_define_method(cGzipReader, "each_char", rb_gzreader_each_char, 0);
4221
+ rb_define_method(cGzipReader, "bytes", rb_gzreader_each_byte, 0);
3487
4222
  rb_define_method(cGzipReader, "ungetc", rb_gzreader_ungetc, 1);
4223
+ rb_define_method(cGzipReader, "ungetbyte", rb_gzreader_ungetbyte, 1);
3488
4224
  rb_define_method(cGzipReader, "gets", rb_gzreader_gets, -1);
3489
4225
  rb_define_method(cGzipReader, "readline", rb_gzreader_readline, -1);
3490
4226
  rb_define_method(cGzipReader, "each", rb_gzreader_each, -1);
3491
4227
  rb_define_method(cGzipReader, "each_line", rb_gzreader_each, -1);
4228
+ rb_define_method(cGzipReader, "lines", rb_gzreader_each, -1);
3492
4229
  rb_define_method(cGzipReader, "readlines", rb_gzreader_readlines, -1);
3493
4230
 
4231
+ /* From GzipFile#os_code - code of current host */
3494
4232
  rb_define_const(mZlib, "OS_CODE", INT2FIX(OS_CODE));
4233
+ /* From GzipFile#os_code - 0x00 */
3495
4234
  rb_define_const(mZlib, "OS_MSDOS", INT2FIX(OS_MSDOS));
4235
+ /* From GzipFile#os_code - 0x01 */
3496
4236
  rb_define_const(mZlib, "OS_AMIGA", INT2FIX(OS_AMIGA));
4237
+ /* From GzipFile#os_code - 0x02 */
3497
4238
  rb_define_const(mZlib, "OS_VMS", INT2FIX(OS_VMS));
4239
+ /* From GzipFile#os_code - 0x03 */
3498
4240
  rb_define_const(mZlib, "OS_UNIX", INT2FIX(OS_UNIX));
4241
+ /* From GzipFile#os_code - 0x05 */
3499
4242
  rb_define_const(mZlib, "OS_ATARI", INT2FIX(OS_ATARI));
4243
+ /* From GzipFile#os_code - 0x06 */
3500
4244
  rb_define_const(mZlib, "OS_OS2", INT2FIX(OS_OS2));
4245
+ /* From GzipFile#os_code - 0x07 */
3501
4246
  rb_define_const(mZlib, "OS_MACOS", INT2FIX(OS_MACOS));
4247
+ /* From GzipFile#os_code - 0x0a */
3502
4248
  rb_define_const(mZlib, "OS_TOPS20", INT2FIX(OS_TOPS20));
4249
+ /* From GzipFile#os_code - 0x0b */
3503
4250
  rb_define_const(mZlib, "OS_WIN32", INT2FIX(OS_WIN32));
3504
4251
 
4252
+ /* From GzipFile#os_code - 0x04 */
3505
4253
  rb_define_const(mZlib, "OS_VMCMS", INT2FIX(OS_VMCMS));
4254
+ /* From GzipFile#os_code - 0x08 */
3506
4255
  rb_define_const(mZlib, "OS_ZSYSTEM", INT2FIX(OS_ZSYSTEM));
4256
+ /* From GzipFile#os_code - 0x09 */
3507
4257
  rb_define_const(mZlib, "OS_CPM", INT2FIX(OS_CPM));
4258
+ /* From GzipFile#os_code - 0x0c */
3508
4259
  rb_define_const(mZlib, "OS_QDOS", INT2FIX(OS_QDOS));
4260
+ /* From GzipFile#os_code - 0x0d */
3509
4261
  rb_define_const(mZlib, "OS_RISCOS", INT2FIX(OS_RISCOS));
4262
+ /* From GzipFile#os_code - 0xff */
3510
4263
  rb_define_const(mZlib, "OS_UNKNOWN", INT2FIX(OS_UNKNOWN));
3511
4264
 
3512
4265
  #endif /* GZIP_SUPPORT */
@@ -3533,6 +4286,77 @@ void Init_zlib()
3533
4286
  *
3534
4287
  */
3535
4288
 
4289
+ /*
4290
+ * Document-class: Zlib::StreamEnd
4291
+ *
4292
+ * Subclass of Zlib::Error
4293
+ *
4294
+ * When zlib returns a Z_STREAM_END
4295
+ * is return if the end of the compressed data has been reached
4296
+ * and all uncompressed out put has been produced.
4297
+ *
4298
+ */
4299
+
4300
+ /*
4301
+ * Document-class: Zlib::NeedDict
4302
+ *
4303
+ * Subclass of Zlib::Error
4304
+ *
4305
+ * When zlib returns a Z_NEED_DICT
4306
+ * if a preset dictionary is needed at this point.
4307
+ *
4308
+ * Used by Zlib::Inflate.inflate and <tt>Zlib.inflate</tt>
4309
+ */
4310
+
4311
+ /*
4312
+ * Document-class: Zlib::VersionError
4313
+ *
4314
+ * Subclass of Zlib::Error
4315
+ *
4316
+ * When zlib returns a Z_VERSION_ERROR,
4317
+ * usually if the zlib library version is incompatible with the
4318
+ * version assumed by the caller.
4319
+ *
4320
+ */
4321
+
4322
+ /*
4323
+ * Document-class: Zlib::MemError
4324
+ *
4325
+ * Subclass of Zlib::Error
4326
+ *
4327
+ * When zlib returns a Z_MEM_ERROR,
4328
+ * usually if there was not enough memory.
4329
+ *
4330
+ */
4331
+
4332
+ /*
4333
+ * Document-class: Zlib::StreamError
4334
+ *
4335
+ * Subclass of Zlib::Error
4336
+ *
4337
+ * When zlib returns a Z_STREAM_ERROR,
4338
+ * usually if the stream state was inconsistent.
4339
+ *
4340
+ */
4341
+
4342
+ /*
4343
+ * Document-class: Zlib::BufError
4344
+ *
4345
+ * Subclass of Zlib::Error when zlib returns a Z_BUF_ERROR.
4346
+ *
4347
+ * Usually if no progress is possible.
4348
+ *
4349
+ */
4350
+
4351
+ /*
4352
+ * Document-class: Zlib::DataError
4353
+ *
4354
+ * Subclass of Zlib::Error when zlib returns a Z_DATA_ERROR.
4355
+ *
4356
+ * Usually if a stream was prematurely freed.
4357
+ *
4358
+ */
4359
+
3536
4360
  /*
3537
4361
  * Document-class: Zlib::GzipFile::Error
3538
4362
  *
@@ -3542,21 +4366,21 @@ void Init_zlib()
3542
4366
  /*
3543
4367
  * Document-class: Zlib::GzipFile::NoFooter
3544
4368
  *
3545
- * Raised when gzip file footer is not found.
4369
+ * Raised when gzip file footer is not found.
3546
4370
  */
3547
4371
 
3548
4372
  /*
3549
4373
  * Document-class: Zlib::GzipFile::CRCError
3550
4374
  *
3551
4375
  * Raised when the CRC checksum recorded in gzip file footer is not equivalent
3552
- * to the CRC checksum of the actual uncompressed data.
4376
+ * to the CRC checksum of the actual uncompressed data.
3553
4377
  */
3554
4378
 
3555
4379
  /*
3556
4380
  * Document-class: Zlib::GzipFile::LengthError
3557
4381
  *
3558
4382
  * Raised when the data length recorded in the gzip file footer is not equivalent
3559
- * to the length of the actual uncompressed data.
4383
+ * to the length of the actual uncompressed data.
3560
4384
  */
3561
4385
 
3562
4386