zstdlib 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.md +9 -0
  3. data/Gemfile +3 -0
  4. data/README.md +96 -0
  5. data/Rakefile +29 -0
  6. data/ext/zstdlib/extconf.rb +53 -0
  7. data/ext/zstdlib/ruby/zlib-2.2/zlib.c +4659 -0
  8. data/ext/zstdlib/ruby/zlib-2.3/zlib.c +4686 -0
  9. data/ext/zstdlib/ruby/zlib-2.4/zlib.c +4843 -0
  10. data/ext/zstdlib/ruby/zlib-2.5/zlib.c +4848 -0
  11. data/ext/zstdlib/ruby/zlib-2.6/zlib.c +4890 -0
  12. data/ext/zstdlib/zlib-1.2.11/adler32.c +186 -0
  13. data/ext/zstdlib/zlib-1.2.11/compress.c +86 -0
  14. data/ext/zstdlib/zlib-1.2.11/crc32.c +442 -0
  15. data/ext/zstdlib/zlib-1.2.11/crc32.h +441 -0
  16. data/ext/zstdlib/zlib-1.2.11/deflate.c +2163 -0
  17. data/ext/zstdlib/zlib-1.2.11/deflate.h +349 -0
  18. data/ext/zstdlib/zlib-1.2.11/gzclose.c +25 -0
  19. data/ext/zstdlib/zlib-1.2.11/gzguts.h +218 -0
  20. data/ext/zstdlib/zlib-1.2.11/gzlib.c +637 -0
  21. data/ext/zstdlib/zlib-1.2.11/gzread.c +654 -0
  22. data/ext/zstdlib/zlib-1.2.11/gzwrite.c +665 -0
  23. data/ext/zstdlib/zlib-1.2.11/infback.c +640 -0
  24. data/ext/zstdlib/zlib-1.2.11/inffast.c +323 -0
  25. data/ext/zstdlib/zlib-1.2.11/inffast.h +11 -0
  26. data/ext/zstdlib/zlib-1.2.11/inffixed.h +94 -0
  27. data/ext/zstdlib/zlib-1.2.11/inflate.c +1561 -0
  28. data/ext/zstdlib/zlib-1.2.11/inflate.h +125 -0
  29. data/ext/zstdlib/zlib-1.2.11/inftrees.c +304 -0
  30. data/ext/zstdlib/zlib-1.2.11/inftrees.h +62 -0
  31. data/ext/zstdlib/zlib-1.2.11/trees.c +1203 -0
  32. data/ext/zstdlib/zlib-1.2.11/trees.h +128 -0
  33. data/ext/zstdlib/zlib-1.2.11/uncompr.c +93 -0
  34. data/ext/zstdlib/zlib-1.2.11/zconf.h +534 -0
  35. data/ext/zstdlib/zlib-1.2.11/zlib.h +1912 -0
  36. data/ext/zstdlib/zlib-1.2.11/zutil.c +325 -0
  37. data/ext/zstdlib/zlib-1.2.11/zutil.h +271 -0
  38. data/ext/zstdlib/zlib.mk +14 -0
  39. data/ext/zstdlib/zlibwrapper/zlibwrapper.c +14 -0
  40. data/ext/zstdlib/zlibwrapper.mk +14 -0
  41. data/ext/zstdlib/zstd-1.3.8/lib/common/bitstream.h +455 -0
  42. data/ext/zstdlib/zstd-1.3.8/lib/common/compiler.h +140 -0
  43. data/ext/zstdlib/zstd-1.3.8/lib/common/cpu.h +215 -0
  44. data/ext/zstdlib/zstd-1.3.8/lib/common/debug.c +44 -0
  45. data/ext/zstdlib/zstd-1.3.8/lib/common/debug.h +134 -0
  46. data/ext/zstdlib/zstd-1.3.8/lib/common/entropy_common.c +236 -0
  47. data/ext/zstdlib/zstd-1.3.8/lib/common/error_private.c +54 -0
  48. data/ext/zstdlib/zstd-1.3.8/lib/common/error_private.h +76 -0
  49. data/ext/zstdlib/zstd-1.3.8/lib/common/fse.h +708 -0
  50. data/ext/zstdlib/zstd-1.3.8/lib/common/fse_decompress.c +309 -0
  51. data/ext/zstdlib/zstd-1.3.8/lib/common/huf.h +358 -0
  52. data/ext/zstdlib/zstd-1.3.8/lib/common/mem.h +380 -0
  53. data/ext/zstdlib/zstd-1.3.8/lib/common/pool.c +340 -0
  54. data/ext/zstdlib/zstd-1.3.8/lib/common/pool.h +84 -0
  55. data/ext/zstdlib/zstd-1.3.8/lib/common/threading.c +75 -0
  56. data/ext/zstdlib/zstd-1.3.8/lib/common/threading.h +123 -0
  57. data/ext/zstdlib/zstd-1.3.8/lib/common/xxhash.c +876 -0
  58. data/ext/zstdlib/zstd-1.3.8/lib/common/xxhash.h +305 -0
  59. data/ext/zstdlib/zstd-1.3.8/lib/common/zstd_common.c +83 -0
  60. data/ext/zstdlib/zstd-1.3.8/lib/common/zstd_errors.h +93 -0
  61. data/ext/zstdlib/zstd-1.3.8/lib/common/zstd_internal.h +266 -0
  62. data/ext/zstdlib/zstd-1.3.8/lib/compress/fse_compress.c +721 -0
  63. data/ext/zstdlib/zstd-1.3.8/lib/compress/hist.c +203 -0
  64. data/ext/zstdlib/zstd-1.3.8/lib/compress/hist.h +95 -0
  65. data/ext/zstdlib/zstd-1.3.8/lib/compress/huf_compress.c +798 -0
  66. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_compress.c +4290 -0
  67. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_compress_internal.h +860 -0
  68. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_double_fast.c +499 -0
  69. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_double_fast.h +38 -0
  70. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_fast.c +391 -0
  71. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_fast.h +37 -0
  72. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_lazy.c +1106 -0
  73. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_lazy.h +67 -0
  74. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_ldm.c +597 -0
  75. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_ldm.h +105 -0
  76. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_opt.c +1217 -0
  77. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstd_opt.h +56 -0
  78. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstdmt_compress.c +2107 -0
  79. data/ext/zstdlib/zstd-1.3.8/lib/compress/zstdmt_compress.h +174 -0
  80. data/ext/zstdlib/zstd-1.3.8/lib/decompress/huf_decompress.c +1232 -0
  81. data/ext/zstdlib/zstd-1.3.8/lib/decompress/zstd_ddict.c +240 -0
  82. data/ext/zstdlib/zstd-1.3.8/lib/decompress/zstd_ddict.h +44 -0
  83. data/ext/zstdlib/zstd-1.3.8/lib/decompress/zstd_decompress.c +1672 -0
  84. data/ext/zstdlib/zstd-1.3.8/lib/decompress/zstd_decompress_block.c +1307 -0
  85. data/ext/zstdlib/zstd-1.3.8/lib/decompress/zstd_decompress_block.h +59 -0
  86. data/ext/zstdlib/zstd-1.3.8/lib/decompress/zstd_decompress_internal.h +168 -0
  87. data/ext/zstdlib/zstd-1.3.8/lib/zstd.h +1766 -0
  88. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/gzclose.c +28 -0
  89. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/gzcompatibility.h +68 -0
  90. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/gzguts.h +227 -0
  91. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/gzlib.c +640 -0
  92. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/gzread.c +670 -0
  93. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/gzwrite.c +671 -0
  94. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/zstd_zlibwrapper.c +1105 -0
  95. data/ext/zstdlib/zstd-1.3.8/zlibWrapper/zstd_zlibwrapper.h +88 -0
  96. data/ext/zstdlib/zstd.mk +14 -0
  97. data/lib/zstdlib.rb +6 -0
  98. data/test/zstdlib_test.rb +21 -0
  99. metadata +198 -0
@@ -0,0 +1,4686 @@
1
+ /*
2
+ * zlib.c - An interface for zlib.
3
+ *
4
+ * Copyright (C) UENO Katsuhiro 2000-2003
5
+ *
6
+ * $Id: zlib.c 59532 2017-08-09 08:08:01Z usa $
7
+ */
8
+
9
+ #include <ruby.h>
10
+ #include <zlib.h>
11
+ #include <time.h>
12
+ #include <ruby/io.h>
13
+ #include <ruby/thread.h>
14
+
15
+ #ifdef HAVE_VALGRIND_MEMCHECK_H
16
+ # include <valgrind/memcheck.h>
17
+ # ifndef VALGRIND_MAKE_MEM_DEFINED
18
+ # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
19
+ # endif
20
+ # ifndef VALGRIND_MAKE_MEM_UNDEFINED
21
+ # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
22
+ # endif
23
+ #else
24
+ # define VALGRIND_MAKE_MEM_DEFINED(p, n) 0
25
+ # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0
26
+ #endif
27
+
28
+ #define RUBY_ZLIB_VERSION "0.6.0"
29
+
30
+ #ifndef GZIP_SUPPORT
31
+ #define GZIP_SUPPORT 1
32
+ #endif
33
+
34
+ /* from zutil.h */
35
+ #ifndef DEF_MEM_LEVEL
36
+ #if MAX_MEM_LEVEL >= 8
37
+ #define DEF_MEM_LEVEL 8
38
+ #else
39
+ #define DEF_MEM_LEVEL MAX_MEM_LEVEL
40
+ #endif
41
+ #endif
42
+
43
+ #if SIZEOF_LONG > SIZEOF_INT
44
+ static inline uInt
45
+ max_uint(long n)
46
+ {
47
+ if (n > UINT_MAX) n = UINT_MAX;
48
+ return (uInt)n;
49
+ }
50
+ #define MAX_UINT(n) max_uint(n)
51
+ #else
52
+ #define MAX_UINT(n) (uInt)(n)
53
+ #endif
54
+
55
+ #define sizeof(x) ((int)sizeof(x))
56
+
57
+ static ID id_dictionaries;
58
+
59
+ /*--------- Prototypes --------*/
60
+
61
+ static NORETURN(void raise_zlib_error(int, const char*));
62
+ static VALUE rb_zlib_version(VALUE);
63
+ static VALUE do_checksum(int, VALUE*, uLong (*)(uLong, const Bytef*, uInt));
64
+ static VALUE rb_zlib_adler32(int, VALUE*, VALUE);
65
+ static VALUE rb_zlib_crc32(int, VALUE*, VALUE);
66
+ static VALUE rb_zlib_crc_table(VALUE);
67
+ static voidpf zlib_mem_alloc(voidpf, uInt, uInt);
68
+ static void zlib_mem_free(voidpf, voidpf);
69
+ static void finalizer_warn(const char*);
70
+
71
+ struct zstream;
72
+ struct zstream_funcs;
73
+ struct zstream_run_args;
74
+ static void zstream_init(struct zstream*, const struct zstream_funcs*);
75
+ static void zstream_expand_buffer(struct zstream*);
76
+ static void zstream_expand_buffer_into(struct zstream*, unsigned long);
77
+ static void zstream_append_buffer(struct zstream*, const Bytef*, long);
78
+ static VALUE zstream_detach_buffer(struct zstream*);
79
+ static VALUE zstream_shift_buffer(struct zstream*, long);
80
+ static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long);
81
+ static void zstream_buffer_ungetbyte(struct zstream*, int);
82
+ static void zstream_append_input(struct zstream*, const Bytef*, long);
83
+ static void zstream_discard_input(struct zstream*, long);
84
+ static void zstream_reset_input(struct zstream*);
85
+ static void zstream_passthrough_input(struct zstream*);
86
+ static VALUE zstream_detach_input(struct zstream*);
87
+ static void zstream_reset(struct zstream*);
88
+ static VALUE zstream_end(struct zstream*);
89
+ static void zstream_run(struct zstream*, Bytef*, long, int);
90
+ static VALUE zstream_sync(struct zstream*, Bytef*, long);
91
+ static void zstream_mark(void*);
92
+ static void zstream_free(void*);
93
+ static VALUE zstream_new(VALUE, const struct zstream_funcs*);
94
+ static struct zstream *get_zstream(VALUE);
95
+ static void zstream_finalize(struct zstream*);
96
+
97
+ static VALUE rb_zstream_end(VALUE);
98
+ static VALUE rb_zstream_reset(VALUE);
99
+ static VALUE rb_zstream_finish(VALUE);
100
+ static VALUE rb_zstream_flush_next_in(VALUE);
101
+ static VALUE rb_zstream_flush_next_out(VALUE);
102
+ static VALUE rb_zstream_avail_out(VALUE);
103
+ static VALUE rb_zstream_set_avail_out(VALUE, VALUE);
104
+ static VALUE rb_zstream_avail_in(VALUE);
105
+ static VALUE rb_zstream_total_in(VALUE);
106
+ static VALUE rb_zstream_total_out(VALUE);
107
+ static VALUE rb_zstream_data_type(VALUE);
108
+ static VALUE rb_zstream_adler(VALUE);
109
+ static VALUE rb_zstream_finished_p(VALUE);
110
+ static VALUE rb_zstream_closed_p(VALUE);
111
+
112
+ static VALUE rb_deflate_s_allocate(VALUE);
113
+ static VALUE rb_deflate_initialize(int, VALUE*, VALUE);
114
+ static VALUE rb_deflate_init_copy(VALUE, VALUE);
115
+ static VALUE deflate_run(VALUE);
116
+ static VALUE rb_deflate_s_deflate(int, VALUE*, VALUE);
117
+ static void do_deflate(struct zstream*, VALUE, int);
118
+ static VALUE rb_deflate_deflate(int, VALUE*, VALUE);
119
+ static VALUE rb_deflate_addstr(VALUE, VALUE);
120
+ static VALUE rb_deflate_flush(int, VALUE*, VALUE);
121
+ static VALUE rb_deflate_params(VALUE, VALUE, VALUE);
122
+ static VALUE rb_deflate_set_dictionary(VALUE, VALUE);
123
+
124
+ static VALUE inflate_run(VALUE);
125
+ static VALUE rb_inflate_s_allocate(VALUE);
126
+ static VALUE rb_inflate_initialize(int, VALUE*, VALUE);
127
+ static VALUE rb_inflate_s_inflate(VALUE, VALUE);
128
+ static void do_inflate(struct zstream*, VALUE);
129
+ static VALUE rb_inflate_inflate(VALUE, VALUE);
130
+ static VALUE rb_inflate_addstr(VALUE, VALUE);
131
+ static VALUE rb_inflate_sync(VALUE, VALUE);
132
+ static VALUE rb_inflate_sync_point_p(VALUE);
133
+ static VALUE rb_inflate_set_dictionary(VALUE, VALUE);
134
+
135
+ #if GZIP_SUPPORT
136
+ struct gzfile;
137
+ static void gzfile_mark(void*);
138
+ static void gzfile_free(void*);
139
+ static VALUE gzfile_new(VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*)));
140
+ static void gzfile_reset(struct gzfile*);
141
+ static void gzfile_close(struct gzfile*, int);
142
+ static void gzfile_write_raw(struct gzfile*);
143
+ static VALUE gzfile_read_raw_partial(VALUE);
144
+ static VALUE gzfile_read_raw_rescue(VALUE);
145
+ static VALUE gzfile_read_raw(struct gzfile*);
146
+ static int gzfile_read_raw_ensure(struct gzfile*, long);
147
+ static char *gzfile_read_raw_until_zero(struct gzfile*, long);
148
+ static unsigned int gzfile_get16(const unsigned char*);
149
+ static unsigned long gzfile_get32(const unsigned char*);
150
+ static void gzfile_set32(unsigned long n, unsigned char*);
151
+ static void gzfile_make_header(struct gzfile*);
152
+ static void gzfile_make_footer(struct gzfile*);
153
+ static void gzfile_read_header(struct gzfile*);
154
+ static void gzfile_check_footer(struct gzfile*);
155
+ static void gzfile_write(struct gzfile*, Bytef*, long);
156
+ static long gzfile_read_more(struct gzfile*);
157
+ static void gzfile_calc_crc(struct gzfile*, VALUE);
158
+ static VALUE gzfile_read(struct gzfile*, long);
159
+ static VALUE gzfile_read_all(struct gzfile*);
160
+ static void gzfile_ungets(struct gzfile*, const Bytef*, long);
161
+ static void gzfile_ungetbyte(struct gzfile*, int);
162
+ static VALUE gzfile_writer_end_run(VALUE);
163
+ static void gzfile_writer_end(struct gzfile*);
164
+ static VALUE gzfile_reader_end_run(VALUE);
165
+ static void gzfile_reader_end(struct gzfile*);
166
+ static void gzfile_reader_rewind(struct gzfile*);
167
+ static VALUE gzfile_reader_get_unused(struct gzfile*);
168
+ static struct gzfile *get_gzfile(VALUE);
169
+ static VALUE gzfile_ensure_close(VALUE);
170
+ static VALUE rb_gzfile_s_wrap(int, VALUE*, VALUE);
171
+ static VALUE gzfile_s_open(int, VALUE*, VALUE, const char*);
172
+ NORETURN(static void gzfile_raise(struct gzfile *, VALUE, const char *));
173
+ static VALUE gzfile_error_inspect(VALUE);
174
+
175
+ static VALUE rb_gzfile_to_io(VALUE);
176
+ static VALUE rb_gzfile_crc(VALUE);
177
+ static VALUE rb_gzfile_mtime(VALUE);
178
+ static VALUE rb_gzfile_level(VALUE);
179
+ static VALUE rb_gzfile_os_code(VALUE);
180
+ static VALUE rb_gzfile_orig_name(VALUE);
181
+ static VALUE rb_gzfile_comment(VALUE);
182
+ static VALUE rb_gzfile_lineno(VALUE);
183
+ static VALUE rb_gzfile_set_lineno(VALUE, VALUE);
184
+ static VALUE rb_gzfile_set_mtime(VALUE, VALUE);
185
+ static VALUE rb_gzfile_set_orig_name(VALUE, VALUE);
186
+ static VALUE rb_gzfile_set_comment(VALUE, VALUE);
187
+ static VALUE rb_gzfile_close(VALUE);
188
+ static VALUE rb_gzfile_finish(VALUE);
189
+ static VALUE rb_gzfile_closed_p(VALUE);
190
+ static VALUE rb_gzfile_eof_p(VALUE);
191
+ static VALUE rb_gzfile_sync(VALUE);
192
+ static VALUE rb_gzfile_set_sync(VALUE, VALUE);
193
+ static VALUE rb_gzfile_total_in(VALUE);
194
+ static VALUE rb_gzfile_total_out(VALUE);
195
+ static VALUE rb_gzfile_path(VALUE);
196
+
197
+ static VALUE rb_gzwriter_s_allocate(VALUE);
198
+ static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE);
199
+ static VALUE rb_gzwriter_initialize(int, VALUE*, VALUE);
200
+ static VALUE rb_gzwriter_flush(int, VALUE*, VALUE);
201
+ static VALUE rb_gzwriter_write(VALUE, VALUE);
202
+ static VALUE rb_gzwriter_putc(VALUE, VALUE);
203
+
204
+ static VALUE rb_gzreader_s_allocate(VALUE);
205
+ static VALUE rb_gzreader_s_open(int, VALUE*, VALUE);
206
+ static VALUE rb_gzreader_initialize(int, VALUE*, VALUE);
207
+ static VALUE rb_gzreader_rewind(VALUE);
208
+ static VALUE rb_gzreader_unused(VALUE);
209
+ static VALUE rb_gzreader_read(int, VALUE*, VALUE);
210
+ static VALUE rb_gzreader_getc(VALUE);
211
+ static VALUE rb_gzreader_readchar(VALUE);
212
+ static VALUE rb_gzreader_each_byte(VALUE);
213
+ static VALUE rb_gzreader_ungetc(VALUE, VALUE);
214
+ static VALUE rb_gzreader_ungetbyte(VALUE, VALUE);
215
+ static void gzreader_skip_linebreaks(struct gzfile*);
216
+ static VALUE gzreader_gets(int, VALUE*, VALUE);
217
+ static VALUE rb_gzreader_gets(int, VALUE*, VALUE);
218
+ static VALUE rb_gzreader_readline(int, VALUE*, VALUE);
219
+ static VALUE rb_gzreader_each(int, VALUE*, VALUE);
220
+ static VALUE rb_gzreader_readlines(int, VALUE*, VALUE);
221
+ #endif /* GZIP_SUPPORT */
222
+
223
+ /*
224
+ * Document-module: Zlib
225
+ *
226
+ * This module provides access to the {zlib library}[http://zlib.net]. Zlib is
227
+ * designed to be a portable, free, general-purpose, legally unencumbered --
228
+ * that is, not covered by any patents -- lossless data-compression library
229
+ * for use on virtually any computer hardware and operating system.
230
+ *
231
+ * The zlib compression library provides in-memory compression and
232
+ * decompression functions, including integrity checks of the uncompressed
233
+ * data.
234
+ *
235
+ * The zlib compressed data format is described in RFC 1950, which is a
236
+ * wrapper around a deflate stream which is described in RFC 1951.
237
+ *
238
+ * The library also supports reading and writing files in gzip (.gz) format
239
+ * with an interface similar to that of IO. The gzip format is described in
240
+ * RFC 1952 which is also a wrapper around a deflate stream.
241
+ *
242
+ * The zlib format was designed to be compact and fast for use in memory and on
243
+ * communications channels. The gzip format was designed for single-file
244
+ * compression on file systems, has a larger header than zlib to maintain
245
+ * directory information, and uses a different, slower check method than zlib.
246
+ *
247
+ * See your system's zlib.h for further information about zlib
248
+ *
249
+ * == Sample usage
250
+ *
251
+ * Using the wrapper to compress strings with default parameters is quite
252
+ * simple:
253
+ *
254
+ * require "zlib"
255
+ *
256
+ * data_to_compress = File.read("don_quixote.txt")
257
+ *
258
+ * puts "Input size: #{data_to_compress.size}"
259
+ * #=> Input size: 2347740
260
+ *
261
+ * data_compressed = Zlib::Deflate.deflate(data_to_compress)
262
+ *
263
+ * puts "Compressed size: #{data_compressed.size}"
264
+ * #=> Compressed size: 887238
265
+ *
266
+ * uncompressed_data = Zlib::Inflate.inflate(data_compressed)
267
+ *
268
+ * puts "Uncompressed data is: #{uncompressed_data}"
269
+ * #=> Uncompressed data is: The Project Gutenberg EBook of Don Quixote...
270
+ *
271
+ * == Class tree
272
+ *
273
+ * - Zlib::Deflate
274
+ * - Zlib::Inflate
275
+ * - Zlib::ZStream
276
+ * - Zlib::Error
277
+ * - Zlib::StreamEnd
278
+ * - Zlib::NeedDict
279
+ * - Zlib::DataError
280
+ * - Zlib::StreamError
281
+ * - Zlib::MemError
282
+ * - Zlib::BufError
283
+ * - Zlib::VersionError
284
+ *
285
+ * (if you have GZIP_SUPPORT)
286
+ * - Zlib::GzipReader
287
+ * - Zlib::GzipWriter
288
+ * - Zlib::GzipFile
289
+ * - Zlib::GzipFile::Error
290
+ * - Zlib::GzipFile::LengthError
291
+ * - Zlib::GzipFile::CRCError
292
+ * - Zlib::GzipFile::NoFooter
293
+ *
294
+ */
295
+ void Init_zlib(void);
296
+
297
+ /*--------- Exceptions --------*/
298
+
299
+ static VALUE cZError, cStreamEnd, cNeedDict;
300
+ static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError;
301
+
302
+ static void
303
+ raise_zlib_error(int err, const char *msg)
304
+ {
305
+ VALUE exc;
306
+
307
+ if (!msg) {
308
+ msg = zError(err);
309
+ }
310
+
311
+ switch(err) {
312
+ case Z_STREAM_END:
313
+ exc = rb_exc_new2(cStreamEnd, msg);
314
+ break;
315
+ case Z_NEED_DICT:
316
+ exc = rb_exc_new2(cNeedDict, msg);
317
+ break;
318
+ case Z_STREAM_ERROR:
319
+ exc = rb_exc_new2(cStreamError, msg);
320
+ break;
321
+ case Z_DATA_ERROR:
322
+ exc = rb_exc_new2(cDataError, msg);
323
+ break;
324
+ case Z_BUF_ERROR:
325
+ exc = rb_exc_new2(cBufError, msg);
326
+ break;
327
+ case Z_VERSION_ERROR:
328
+ exc = rb_exc_new2(cVersionError, msg);
329
+ break;
330
+ case Z_MEM_ERROR:
331
+ exc = rb_exc_new2(cMemError, msg);
332
+ break;
333
+ case Z_ERRNO:
334
+ rb_sys_fail(msg);
335
+ /* no return */
336
+ default:
337
+ exc = rb_exc_new_str(cZError,
338
+ rb_sprintf("unknown zlib error %d: %s", err, msg));
339
+ }
340
+
341
+ rb_exc_raise(exc);
342
+ }
343
+
344
+
345
+ /*--- Warning (in finalizer) ---*/
346
+
347
+ static void
348
+ finalizer_warn(const char *msg)
349
+ {
350
+ fprintf(stderr, "zlib(finalizer): %s\n", msg);
351
+ }
352
+
353
+
354
+ /*-------- module Zlib --------*/
355
+
356
+ /*
357
+ * Document-method: Zlib.zlib_version
358
+ *
359
+ * Returns the string which represents the version of zlib library.
360
+ */
361
+ static VALUE
362
+ rb_zlib_version(VALUE klass)
363
+ {
364
+ VALUE str;
365
+
366
+ str = rb_str_new2(zlibVersion());
367
+ OBJ_TAINT(str); /* for safe */
368
+ return str;
369
+ }
370
+
371
+ #if SIZEOF_LONG > SIZEOF_INT
372
+ static uLong
373
+ checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len)
374
+ {
375
+ if (len > UINT_MAX) {
376
+ do {
377
+ sum = func(sum, ptr, UINT_MAX);
378
+ ptr += UINT_MAX;
379
+ len -= UINT_MAX;
380
+ } while (len >= UINT_MAX);
381
+ }
382
+ if (len > 0) sum = func(sum, ptr, (uInt)len);
383
+ return sum;
384
+ }
385
+ #else
386
+ #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
387
+ #endif
388
+
389
+ static VALUE
390
+ do_checksum(int argc, VALUE *argv, uLong (*func)(uLong, const Bytef*, uInt))
391
+ {
392
+ VALUE str, vsum;
393
+ unsigned long sum;
394
+
395
+ rb_scan_args(argc, argv, "02", &str, &vsum);
396
+
397
+ if (!NIL_P(vsum)) {
398
+ sum = NUM2ULONG(vsum);
399
+ }
400
+ else if (NIL_P(str)) {
401
+ sum = 0;
402
+ }
403
+ else {
404
+ sum = func(0, Z_NULL, 0);
405
+ }
406
+
407
+ if (NIL_P(str)) {
408
+ sum = func(sum, Z_NULL, 0);
409
+ }
410
+ else {
411
+ StringValue(str);
412
+ sum = checksum_long(func, sum, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
413
+ }
414
+ return rb_uint2inum(sum);
415
+ }
416
+
417
+ /*
418
+ * Document-method: Zlib.adler32
419
+ *
420
+ * call-seq: Zlib.adler32(string, adler)
421
+ *
422
+ * Calculates Adler-32 checksum for +string+, and returns updated value of
423
+ * +adler+. If +string+ is omitted, it returns the Adler-32 initial value. If
424
+ * +adler+ is omitted, it assumes that the initial value is given to +adler+.
425
+ *
426
+ * Example usage:
427
+ *
428
+ * require "zlib"
429
+ *
430
+ * data = "foo"
431
+ * puts "Adler32 checksum: #{Zlib.adler32(data).to_s(16)}"
432
+ * #=> Adler32 checksum: 2820145
433
+ *
434
+ */
435
+ static VALUE
436
+ rb_zlib_adler32(int argc, VALUE *argv, VALUE klass)
437
+ {
438
+ return do_checksum(argc, argv, adler32);
439
+ }
440
+
441
+ #ifdef HAVE_ADLER32_COMBINE
442
+ /*
443
+ * Document-method: Zlib.adler32_combine
444
+ *
445
+ * call-seq: Zlib.adler32_combine(adler1, adler2, len2)
446
+ *
447
+ * Combine two Adler-32 check values in to one. +alder1+ is the first Adler-32
448
+ * value, +adler2+ is the second Adler-32 value. +len2+ is the length of the
449
+ * string used to generate +adler2+.
450
+ *
451
+ */
452
+ static VALUE
453
+ rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2)
454
+ {
455
+ return ULONG2NUM(
456
+ adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2)));
457
+ }
458
+ #else
459
+ #define rb_zlib_adler32_combine rb_f_notimplement
460
+ #endif
461
+
462
+ /*
463
+ * Document-method: Zlib.crc32
464
+ *
465
+ * call-seq: Zlib.crc32(string, crc)
466
+ *
467
+ * Calculates CRC checksum for +string+, and returns updated value of +crc+. If
468
+ * +string+ is omitted, it returns the CRC initial value. If +crc+ is omitted, it
469
+ * assumes that the initial value is given to +crc+.
470
+ *
471
+ * FIXME: expression.
472
+ */
473
+ static VALUE
474
+ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
475
+ {
476
+ return do_checksum(argc, argv, crc32);
477
+ }
478
+
479
+ #ifdef HAVE_CRC32_COMBINE
480
+ /*
481
+ * Document-method: Zlib.crc32_combine
482
+ *
483
+ * call-seq: Zlib.crc32_combine(crc1, crc2, len2)
484
+ *
485
+ * Combine two CRC-32 check values in to one. +crc1+ is the first CRC-32
486
+ * value, +crc2+ is the second CRC-32 value. +len2+ is the length of the
487
+ * string used to generate +crc2+.
488
+ *
489
+ */
490
+ static VALUE
491
+ rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
492
+ {
493
+ return ULONG2NUM(
494
+ crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2)));
495
+ }
496
+ #else
497
+ #define rb_zlib_crc32_combine rb_f_notimplement
498
+ #endif
499
+
500
+ /*
501
+ * Document-method: Zlib.crc_table
502
+ *
503
+ * Returns the table for calculating CRC checksum as an array.
504
+ */
505
+ static VALUE
506
+ rb_zlib_crc_table(VALUE obj)
507
+ {
508
+ #if !defined(HAVE_TYPE_Z_CRC_T)
509
+ /* z_crc_t is defined since zlib-1.2.7. */
510
+ typedef unsigned long z_crc_t;
511
+ #endif
512
+ const z_crc_t *crctbl;
513
+ VALUE dst;
514
+ int i;
515
+
516
+ crctbl = get_crc_table();
517
+ dst = rb_ary_new2(256);
518
+
519
+ for (i = 0; i < 256; i++) {
520
+ rb_ary_push(dst, rb_uint2inum(crctbl[i]));
521
+ }
522
+ return dst;
523
+ }
524
+
525
+
526
+
527
+ /*-------- zstream - internal APIs --------*/
528
+
529
+ struct zstream {
530
+ unsigned long flags;
531
+ VALUE buf;
532
+ long buf_filled;
533
+ VALUE input;
534
+ z_stream stream;
535
+ const struct zstream_funcs {
536
+ int (*reset)(z_streamp);
537
+ int (*end)(z_streamp);
538
+ int (*run)(z_streamp, int);
539
+ } *func;
540
+ };
541
+
542
+ #define ZSTREAM_FLAG_READY 0x1
543
+ #define ZSTREAM_FLAG_IN_STREAM 0x2
544
+ #define ZSTREAM_FLAG_FINISHED 0x4
545
+ #define ZSTREAM_FLAG_CLOSING 0x8
546
+ #define ZSTREAM_FLAG_GZFILE 0x10 /* disallows yield from expand_buffer for
547
+ gzip*/
548
+ #define ZSTREAM_FLAG_UNUSED 0x20
549
+
550
+ #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
551
+ #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
552
+ #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
553
+ #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING)
554
+ #define ZSTREAM_IS_GZFILE(z) ((z)->flags & ZSTREAM_FLAG_GZFILE)
555
+
556
+ #define ZSTREAM_EXPAND_BUFFER_OK 0
557
+
558
+ /* I think that more better value should be found,
559
+ but I gave up finding it. B) */
560
+ #define ZSTREAM_INITIAL_BUFSIZE 1024
561
+ /* Allow a quick return when the thread is interrupted */
562
+ #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
563
+ #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
564
+
565
+ static const struct zstream_funcs deflate_funcs = {
566
+ deflateReset, deflateEnd, deflate,
567
+ };
568
+
569
+ static const struct zstream_funcs inflate_funcs = {
570
+ inflateReset, inflateEnd, inflate,
571
+ };
572
+
573
+ struct zstream_run_args {
574
+ struct zstream * z;
575
+ int flush; /* stream flush value for inflate() or deflate() */
576
+ int interrupt; /* stop processing the stream and return to ruby */
577
+ int jump_state; /* for buffer expansion block break or exception */
578
+ int stream_output; /* for streaming zlib processing */
579
+ };
580
+
581
+ static voidpf
582
+ zlib_mem_alloc(voidpf opaque, uInt items, uInt size)
583
+ {
584
+ voidpf p = xmalloc2(items, size);
585
+ /* zlib FAQ: Valgrind (or some similar memory access checker) says that
586
+ deflate is performing a conditional jump that depends on an
587
+ uninitialized value. Isn't that a bug?
588
+ http://www.zlib.net/zlib_faq.html#faq36 */
589
+ (void)VALGRIND_MAKE_MEM_DEFINED(p, items * size);
590
+ return p;
591
+ }
592
+
593
+ static void
594
+ zlib_mem_free(voidpf opaque, voidpf address)
595
+ {
596
+ xfree(address);
597
+ }
598
+
599
+ static void
600
+ zstream_init(struct zstream *z, const struct zstream_funcs *func)
601
+ {
602
+ z->flags = 0;
603
+ z->buf = Qnil;
604
+ z->buf_filled = 0;
605
+ z->input = Qnil;
606
+ z->stream.zalloc = zlib_mem_alloc;
607
+ z->stream.zfree = zlib_mem_free;
608
+ z->stream.opaque = Z_NULL;
609
+ z->stream.msg = Z_NULL;
610
+ z->stream.next_in = Z_NULL;
611
+ z->stream.avail_in = 0;
612
+ z->stream.next_out = Z_NULL;
613
+ z->stream.avail_out = 0;
614
+ z->func = func;
615
+ }
616
+
617
+ #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
618
+ #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
619
+
620
+ static void
621
+ zstream_expand_buffer(struct zstream *z)
622
+ {
623
+ if (NIL_P(z->buf)) {
624
+ zstream_expand_buffer_into(z, ZSTREAM_INITIAL_BUFSIZE);
625
+ return;
626
+ }
627
+
628
+ if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) {
629
+ if (z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
630
+ int state = 0;
631
+ VALUE self = (VALUE)z->stream.opaque;
632
+
633
+ rb_str_resize(z->buf, z->buf_filled);
634
+ rb_obj_reveal(z->buf, rb_cString);
635
+ OBJ_INFECT(z->buf, self);
636
+
637
+ rb_protect(rb_yield, z->buf, &state);
638
+
639
+ z->buf = Qnil;
640
+ zstream_expand_buffer_into(z, ZSTREAM_AVAIL_OUT_STEP_MAX);
641
+
642
+ if (state)
643
+ rb_jump_tag(state);
644
+
645
+ return;
646
+ }
647
+ else {
648
+ zstream_expand_buffer_into(z,
649
+ ZSTREAM_AVAIL_OUT_STEP_MAX - z->buf_filled);
650
+ }
651
+ }
652
+ else {
653
+ if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
654
+ z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
655
+ }
656
+ else {
657
+ long inc = z->buf_filled / 2;
658
+ if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) {
659
+ inc = ZSTREAM_AVAIL_OUT_STEP_MIN;
660
+ }
661
+ rb_str_resize(z->buf, z->buf_filled + inc);
662
+ z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
663
+ (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
664
+ }
665
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
666
+ }
667
+ }
668
+
669
+ static void
670
+ zstream_expand_buffer_into(struct zstream *z, unsigned long size)
671
+ {
672
+ if (NIL_P(z->buf)) {
673
+ /* I uses rb_str_new here not rb_str_buf_new because
674
+ rb_str_buf_new makes a zero-length string. */
675
+ z->buf = rb_str_new(0, size);
676
+ z->buf_filled = 0;
677
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
678
+ z->stream.avail_out = MAX_UINT(size);
679
+ rb_obj_hide(z->buf);
680
+ }
681
+ else if (z->stream.avail_out != size) {
682
+ rb_str_resize(z->buf, z->buf_filled + size);
683
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
684
+ z->stream.avail_out = MAX_UINT(size);
685
+ }
686
+ }
687
+
688
+ static void *
689
+ zstream_expand_buffer_protect(void *ptr)
690
+ {
691
+ struct zstream *z = (struct zstream *)ptr;
692
+ int state = 0;
693
+
694
+ rb_protect((VALUE (*)(VALUE))zstream_expand_buffer, (VALUE)z, &state);
695
+
696
+ return (void *)(VALUE)state;
697
+ }
698
+
699
+ static int
700
+ zstream_expand_buffer_without_gvl(struct zstream *z)
701
+ {
702
+ char * new_str;
703
+ long inc, len;
704
+
705
+ if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
706
+ z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
707
+ }
708
+ else {
709
+ inc = z->buf_filled / 2;
710
+ if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) {
711
+ inc = ZSTREAM_AVAIL_OUT_STEP_MIN;
712
+ }
713
+
714
+ len = z->buf_filled + inc;
715
+
716
+ new_str = ruby_xrealloc(RSTRING(z->buf)->as.heap.ptr, len + 1);
717
+
718
+ /* from rb_str_resize */
719
+ RSTRING(z->buf)->as.heap.ptr = new_str;
720
+ RSTRING(z->buf)->as.heap.ptr[len] = '\0'; /* sentinel */
721
+ RSTRING(z->buf)->as.heap.len =
722
+ RSTRING(z->buf)->as.heap.aux.capa = len;
723
+
724
+ z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
725
+ (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
726
+ }
727
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
728
+
729
+ return ZSTREAM_EXPAND_BUFFER_OK;
730
+ }
731
+
732
+ static void
733
+ zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
734
+ {
735
+ if (NIL_P(z->buf)) {
736
+ z->buf = rb_str_buf_new(len);
737
+ rb_str_buf_cat(z->buf, (const char*)src, len);
738
+ z->buf_filled = len;
739
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
740
+ z->stream.avail_out = 0;
741
+ rb_obj_hide(z->buf);
742
+ return;
743
+ }
744
+
745
+ if (RSTRING_LEN(z->buf) < z->buf_filled + len) {
746
+ rb_str_resize(z->buf, z->buf_filled + len);
747
+ z->stream.avail_out = 0;
748
+ }
749
+ else {
750
+ if (z->stream.avail_out >= (uInt)len) {
751
+ z->stream.avail_out -= (uInt)len;
752
+ }
753
+ else {
754
+ z->stream.avail_out = 0;
755
+ }
756
+ }
757
+ memcpy(RSTRING_PTR(z->buf) + z->buf_filled, src, len);
758
+ z->buf_filled += len;
759
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
760
+ }
761
+
762
+ #define zstream_append_buffer2(z,v) \
763
+ zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
764
+
765
+ static VALUE
766
+ zstream_detach_buffer(struct zstream *z)
767
+ {
768
+ VALUE dst, self = (VALUE)z->stream.opaque;
769
+
770
+ if (!ZSTREAM_IS_FINISHED(z) && !ZSTREAM_IS_GZFILE(z) &&
771
+ rb_block_given_p()) {
772
+ /* prevent tiny yields mid-stream, save for next
773
+ * zstream_expand_buffer() or stream end */
774
+ return Qnil;
775
+ }
776
+
777
+ if (NIL_P(z->buf)) {
778
+ dst = rb_str_new(0, 0);
779
+ }
780
+ else {
781
+ dst = z->buf;
782
+ rb_str_resize(dst, z->buf_filled);
783
+ rb_obj_reveal(dst, rb_cString);
784
+ }
785
+
786
+ OBJ_INFECT(dst, self);
787
+
788
+ z->buf = Qnil;
789
+ z->buf_filled = 0;
790
+ z->stream.next_out = 0;
791
+ z->stream.avail_out = 0;
792
+
793
+ if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) {
794
+ rb_yield(dst);
795
+ dst = Qnil;
796
+ }
797
+
798
+ return dst;
799
+ }
800
+
801
+ static VALUE
802
+ zstream_shift_buffer(struct zstream *z, long len)
803
+ {
804
+ VALUE dst;
805
+ long buflen;
806
+
807
+ if (z->buf_filled <= len) {
808
+ return zstream_detach_buffer(z);
809
+ }
810
+
811
+ dst = rb_str_new(RSTRING_PTR(z->buf), len);
812
+ z->buf_filled -= len;
813
+ memmove(RSTRING_PTR(z->buf), RSTRING_PTR(z->buf) + len,
814
+ z->buf_filled);
815
+ z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
816
+ buflen = RSTRING_LEN(z->buf) - z->buf_filled;
817
+ if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) {
818
+ buflen = ZSTREAM_AVAIL_OUT_STEP_MAX;
819
+ }
820
+ z->stream.avail_out = (uInt)buflen;
821
+
822
+ return dst;
823
+ }
824
+
825
+ static void
826
+ zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len)
827
+ {
828
+ if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) {
829
+ zstream_expand_buffer_into(z, len);
830
+ }
831
+
832
+ memmove(RSTRING_PTR(z->buf) + len, RSTRING_PTR(z->buf), z->buf_filled);
833
+ memmove(RSTRING_PTR(z->buf), b, len);
834
+ z->buf_filled+=len;
835
+ if (z->stream.avail_out > 0) {
836
+ if (len > z->stream.avail_out) len = z->stream.avail_out;
837
+ z->stream.next_out+=len;
838
+ z->stream.avail_out-=(uInt)len;
839
+ }
840
+ }
841
+
842
+ static void
843
+ zstream_buffer_ungetbyte(struct zstream *z, int c)
844
+ {
845
+ if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) {
846
+ zstream_expand_buffer(z);
847
+ }
848
+
849
+ memmove(RSTRING_PTR(z->buf) + 1, RSTRING_PTR(z->buf), z->buf_filled);
850
+ RSTRING_PTR(z->buf)[0] = (char)c;
851
+ z->buf_filled++;
852
+ if (z->stream.avail_out > 0) {
853
+ z->stream.next_out++;
854
+ z->stream.avail_out--;
855
+ }
856
+ }
857
+
858
+ static void
859
+ zstream_append_input(struct zstream *z, const Bytef *src, long len)
860
+ {
861
+ if (len <= 0) return;
862
+
863
+ if (NIL_P(z->input)) {
864
+ z->input = rb_str_buf_new(len);
865
+ rb_str_buf_cat(z->input, (const char*)src, len);
866
+ rb_obj_hide(z->input);
867
+ }
868
+ else {
869
+ rb_str_buf_cat(z->input, (const char*)src, len);
870
+ }
871
+ }
872
+
873
+ #define zstream_append_input2(z,v)\
874
+ RB_GC_GUARD(v),\
875
+ zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
876
+
877
+ static void
878
+ zstream_discard_input(struct zstream *z, long len)
879
+ {
880
+ if (NIL_P(z->input) || RSTRING_LEN(z->input) <= len) {
881
+ z->input = Qnil;
882
+ }
883
+ else {
884
+ memmove(RSTRING_PTR(z->input), RSTRING_PTR(z->input) + len,
885
+ RSTRING_LEN(z->input) - len);
886
+ rb_str_resize(z->input, RSTRING_LEN(z->input) - len);
887
+ }
888
+ }
889
+
890
+ static void
891
+ zstream_reset_input(struct zstream *z)
892
+ {
893
+ z->input = Qnil;
894
+ }
895
+
896
+ static void
897
+ zstream_passthrough_input(struct zstream *z)
898
+ {
899
+ if (!NIL_P(z->input)) {
900
+ zstream_append_buffer2(z, z->input);
901
+ z->input = Qnil;
902
+ }
903
+ }
904
+
905
+ static VALUE
906
+ zstream_detach_input(struct zstream *z)
907
+ {
908
+ VALUE dst;
909
+
910
+ if (NIL_P(z->input)) {
911
+ dst = rb_str_new(0, 0);
912
+ }
913
+ else {
914
+ dst = z->input;
915
+ rb_obj_reveal(dst, rb_cString);
916
+ }
917
+ z->input = Qnil;
918
+ rb_obj_reveal(dst, rb_cString);
919
+ return dst;
920
+ }
921
+
922
+ static void
923
+ zstream_reset(struct zstream *z)
924
+ {
925
+ int err;
926
+
927
+ err = z->func->reset(&z->stream);
928
+ if (err != Z_OK) {
929
+ raise_zlib_error(err, z->stream.msg);
930
+ }
931
+ z->flags = ZSTREAM_FLAG_READY;
932
+ z->buf = Qnil;
933
+ z->buf_filled = 0;
934
+ z->stream.next_out = 0;
935
+ z->stream.avail_out = 0;
936
+ zstream_reset_input(z);
937
+ }
938
+
939
+ static VALUE
940
+ zstream_end(struct zstream *z)
941
+ {
942
+ int err;
943
+
944
+ if (!ZSTREAM_IS_READY(z)) {
945
+ rb_warning("attempt to close uninitialized zstream; ignored.");
946
+ return Qnil;
947
+ }
948
+ if (z->flags & ZSTREAM_FLAG_IN_STREAM) {
949
+ rb_warning("attempt to close unfinished zstream; reset forced.");
950
+ zstream_reset(z);
951
+ }
952
+
953
+ zstream_reset_input(z);
954
+ err = z->func->end(&z->stream);
955
+ if (err != Z_OK) {
956
+ raise_zlib_error(err, z->stream.msg);
957
+ }
958
+ z->flags = 0;
959
+ return Qnil;
960
+ }
961
+
962
+ static void *
963
+ zstream_run_func(void *ptr)
964
+ {
965
+ struct zstream_run_args *args = (struct zstream_run_args *)ptr;
966
+ int err, state, flush = args->flush;
967
+ struct zstream *z = args->z;
968
+ uInt n;
969
+
970
+ err = Z_OK;
971
+ while (!args->interrupt) {
972
+ n = z->stream.avail_out;
973
+ err = z->func->run(&z->stream, flush);
974
+ z->buf_filled += n - z->stream.avail_out;
975
+
976
+ if (err == Z_STREAM_END) {
977
+ z->flags &= ~ZSTREAM_FLAG_IN_STREAM;
978
+ z->flags |= ZSTREAM_FLAG_FINISHED;
979
+ break;
980
+ }
981
+
982
+ if (err != Z_OK && err != Z_BUF_ERROR)
983
+ break;
984
+
985
+ if (z->stream.avail_out > 0) {
986
+ z->flags |= ZSTREAM_FLAG_IN_STREAM;
987
+ break;
988
+ }
989
+
990
+ if (z->stream.avail_in == 0 && z->func == &inflate_funcs) {
991
+ /* break here because inflate() return Z_BUF_ERROR when avail_in == 0. */
992
+ /* but deflate() could be called with avail_in == 0 (there's hidden buffer
993
+ in zstream->state) */
994
+ z->flags |= ZSTREAM_FLAG_IN_STREAM;
995
+ break;
996
+ }
997
+
998
+ if (args->stream_output) {
999
+ state = (int)(VALUE)rb_thread_call_with_gvl(zstream_expand_buffer_protect,
1000
+ (void *)z);
1001
+ }
1002
+ else {
1003
+ state = zstream_expand_buffer_without_gvl(z);
1004
+ }
1005
+
1006
+ if (state) {
1007
+ err = Z_OK; /* buffer expanded but stream processing was stopped */
1008
+ args->jump_state = state;
1009
+ break;
1010
+ }
1011
+ }
1012
+
1013
+ return (void *)(VALUE)err;
1014
+ }
1015
+
1016
+ /*
1017
+ * There is no safe way to interrupt z->run->func().
1018
+ */
1019
+ static void
1020
+ zstream_unblock_func(void *ptr)
1021
+ {
1022
+ struct zstream_run_args *args = (struct zstream_run_args *)ptr;
1023
+
1024
+ args->interrupt = 1;
1025
+ }
1026
+
1027
+ static void
1028
+ zstream_run(struct zstream *z, Bytef *src, long len, int flush)
1029
+ {
1030
+ struct zstream_run_args args;
1031
+ int err;
1032
+ VALUE guard = Qnil;
1033
+
1034
+ args.z = z;
1035
+ args.flush = flush;
1036
+ args.interrupt = 0;
1037
+ args.jump_state = 0;
1038
+ args.stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p();
1039
+
1040
+ if (NIL_P(z->input) && len == 0) {
1041
+ z->stream.next_in = (Bytef*)"";
1042
+ z->stream.avail_in = 0;
1043
+ }
1044
+ else {
1045
+ zstream_append_input(z, src, len);
1046
+ z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
1047
+ z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
1048
+ /* keep reference to `z->input' so as not to be garbage collected
1049
+ after zstream_reset_input() and prevent `z->stream.next_in'
1050
+ from dangling. */
1051
+ guard = z->input;
1052
+ }
1053
+
1054
+ if (z->stream.avail_out == 0) {
1055
+ zstream_expand_buffer(z);
1056
+ }
1057
+
1058
+ loop:
1059
+ err = (int)(VALUE)rb_thread_call_without_gvl(zstream_run_func, (void *)&args,
1060
+ zstream_unblock_func, (void *)&args);
1061
+
1062
+ if (flush != Z_FINISH && err == Z_BUF_ERROR
1063
+ && z->stream.avail_out > 0) {
1064
+ z->flags |= ZSTREAM_FLAG_IN_STREAM;
1065
+ }
1066
+
1067
+ zstream_reset_input(z);
1068
+
1069
+ if (err != Z_OK && err != Z_STREAM_END) {
1070
+ if (z->stream.avail_in > 0) {
1071
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1072
+ }
1073
+ if (err == Z_NEED_DICT) {
1074
+ VALUE self = (VALUE)z->stream.opaque;
1075
+ if (self) {
1076
+ VALUE dicts = rb_ivar_get(self, id_dictionaries);
1077
+ VALUE dict = rb_hash_aref(dicts, rb_uint2inum(z->stream.adler));
1078
+ if (!NIL_P(dict)) {
1079
+ rb_inflate_set_dictionary(self, dict);
1080
+ goto loop;
1081
+ }
1082
+ }
1083
+ }
1084
+ raise_zlib_error(err, z->stream.msg);
1085
+ }
1086
+
1087
+ if (z->stream.avail_in > 0) {
1088
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1089
+ RB_GC_GUARD(guard); /* prevent tail call to make guard effective */
1090
+ }
1091
+
1092
+ if (args.jump_state)
1093
+ rb_jump_tag(args.jump_state);
1094
+ }
1095
+
1096
+ static VALUE
1097
+ zstream_sync(struct zstream *z, Bytef *src, long len)
1098
+ {
1099
+ /* VALUE rest; */
1100
+ int err;
1101
+
1102
+ if (!NIL_P(z->input)) {
1103
+ z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
1104
+ z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
1105
+ err = inflateSync(&z->stream);
1106
+ if (err == Z_OK) {
1107
+ zstream_discard_input(z,
1108
+ RSTRING_LEN(z->input) - z->stream.avail_in);
1109
+ zstream_append_input(z, src, len);
1110
+ return Qtrue;
1111
+ }
1112
+ zstream_reset_input(z);
1113
+ if (err != Z_DATA_ERROR) {
1114
+ /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */
1115
+ raise_zlib_error(err, z->stream.msg);
1116
+ }
1117
+ }
1118
+
1119
+ if (len <= 0) return Qfalse;
1120
+
1121
+ z->stream.next_in = src;
1122
+ z->stream.avail_in = MAX_UINT(len);
1123
+ err = inflateSync(&z->stream);
1124
+ if (err == Z_OK) {
1125
+ zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
1126
+ return Qtrue;
1127
+ }
1128
+ if (err != Z_DATA_ERROR) {
1129
+ /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */
1130
+ raise_zlib_error(err, z->stream.msg);
1131
+ }
1132
+ return Qfalse;
1133
+ }
1134
+
1135
+ static void
1136
+ zstream_mark(void *p)
1137
+ {
1138
+ struct zstream *z = p;
1139
+ rb_gc_mark(z->buf);
1140
+ rb_gc_mark(z->input);
1141
+ }
1142
+
1143
+ static void
1144
+ zstream_finalize(struct zstream *z)
1145
+ {
1146
+ int err = z->func->end(&z->stream);
1147
+ if (err == Z_STREAM_ERROR)
1148
+ finalizer_warn("the stream state was inconsistent.");
1149
+ if (err == Z_DATA_ERROR)
1150
+ finalizer_warn("the stream was freed prematurely.");
1151
+ }
1152
+
1153
+ static void
1154
+ zstream_free(void *p)
1155
+ {
1156
+ struct zstream *z = p;
1157
+
1158
+ if (ZSTREAM_IS_READY(z)) {
1159
+ zstream_finalize(z);
1160
+ }
1161
+ xfree(z);
1162
+ }
1163
+
1164
+ static size_t
1165
+ zstream_memsize(const void *p)
1166
+ {
1167
+ /* n.b. this does not track memory managed via zalloc/zfree callbacks */
1168
+ return sizeof(struct zstream);
1169
+ }
1170
+
1171
+ static const rb_data_type_t zstream_data_type = {
1172
+ "zstream",
1173
+ { zstream_mark, zstream_free, zstream_memsize, },
1174
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
1175
+ };
1176
+
1177
+ static VALUE
1178
+ zstream_new(VALUE klass, const struct zstream_funcs *funcs)
1179
+ {
1180
+ VALUE obj;
1181
+ struct zstream *z;
1182
+
1183
+ obj = TypedData_Make_Struct(klass, struct zstream, &zstream_data_type, z);
1184
+ zstream_init(z, funcs);
1185
+ z->stream.opaque = (voidpf)obj;
1186
+ return obj;
1187
+ }
1188
+
1189
+ #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
1190
+ #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
1191
+
1192
+ static struct zstream *
1193
+ get_zstream(VALUE obj)
1194
+ {
1195
+ struct zstream *z;
1196
+
1197
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1198
+ if (!ZSTREAM_IS_READY(z)) {
1199
+ rb_raise(cZError, "stream is not ready");
1200
+ }
1201
+ return z;
1202
+ }
1203
+
1204
+
1205
+ /* ------------------------------------------------------------------------- */
1206
+
1207
+ /*
1208
+ * Document-class: Zlib::ZStream
1209
+ *
1210
+ * Zlib::ZStream is the abstract class for the stream which handles the
1211
+ * compressed data. The operations are defined in the subclasses:
1212
+ * Zlib::Deflate for compression, and Zlib::Inflate for decompression.
1213
+ *
1214
+ * An instance of Zlib::ZStream has one stream (struct zstream in the source)
1215
+ * and two variable-length buffers which associated to the input (next_in) of
1216
+ * the stream and the output (next_out) of the stream. In this document,
1217
+ * "input buffer" means the buffer for input, and "output buffer" means the
1218
+ * buffer for output.
1219
+ *
1220
+ * Data input into an instance of Zlib::ZStream are temporally stored into
1221
+ * the end of input buffer, and then data in input buffer are processed from
1222
+ * the beginning of the buffer until no more output from the stream is
1223
+ * produced (i.e. until avail_out > 0 after processing). During processing,
1224
+ * output buffer is allocated and expanded automatically to hold all output
1225
+ * data.
1226
+ *
1227
+ * Some particular instance methods consume the data in output buffer and
1228
+ * return them as a String.
1229
+ *
1230
+ * Here is an ascii art for describing above:
1231
+ *
1232
+ * +================ an instance of Zlib::ZStream ================+
1233
+ * || ||
1234
+ * || +--------+ +-------+ +--------+ ||
1235
+ * || +--| output |<---------|zstream|<---------| input |<--+ ||
1236
+ * || | | buffer | next_out+-------+next_in | buffer | | ||
1237
+ * || | +--------+ +--------+ | ||
1238
+ * || | | ||
1239
+ * +===|======================================================|===+
1240
+ * | |
1241
+ * v |
1242
+ * "output data" "input data"
1243
+ *
1244
+ * If an error occurs during processing input buffer, an exception which is a
1245
+ * subclass of Zlib::Error is raised. At that time, both input and output
1246
+ * buffer keep their conditions at the time when the error occurs.
1247
+ *
1248
+ * == Method Catalogue
1249
+ *
1250
+ * Many of the methods in this class are fairly low-level and unlikely to be
1251
+ * of interest to users. In fact, users are unlikely to use this class
1252
+ * directly; rather they will be interested in Zlib::Inflate and
1253
+ * Zlib::Deflate.
1254
+ *
1255
+ * The higher level methods are listed below.
1256
+ *
1257
+ * - #total_in
1258
+ * - #total_out
1259
+ * - #data_type
1260
+ * - #adler
1261
+ * - #reset
1262
+ * - #finish
1263
+ * - #finished?
1264
+ * - #close
1265
+ * - #closed?
1266
+ */
1267
+
1268
+ /*
1269
+ * Closes the stream. All operations on the closed stream will raise an
1270
+ * exception.
1271
+ */
1272
+ static VALUE
1273
+ rb_zstream_end(VALUE obj)
1274
+ {
1275
+ zstream_end(get_zstream(obj));
1276
+ return Qnil;
1277
+ }
1278
+
1279
+ /*
1280
+ * Resets and initializes the stream. All data in both input and output buffer
1281
+ * are discarded.
1282
+ */
1283
+ static VALUE
1284
+ rb_zstream_reset(VALUE obj)
1285
+ {
1286
+ zstream_reset(get_zstream(obj));
1287
+ return Qnil;
1288
+ }
1289
+
1290
+ /*
1291
+ * call-seq:
1292
+ * finish -> String
1293
+ * finish { |chunk| ... } -> nil
1294
+ *
1295
+ * Finishes the stream and flushes output buffer. If a block is given each
1296
+ * chunk is yielded to the block until the input buffer has been flushed to
1297
+ * the output buffer.
1298
+ */
1299
+ static VALUE
1300
+ rb_zstream_finish(VALUE obj)
1301
+ {
1302
+ struct zstream *z = get_zstream(obj);
1303
+
1304
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1305
+
1306
+ return zstream_detach_buffer(z);
1307
+ }
1308
+
1309
+ /*
1310
+ * call-seq:
1311
+ * flush_next_in -> input
1312
+ *
1313
+ */
1314
+ static VALUE
1315
+ rb_zstream_flush_next_in(VALUE obj)
1316
+ {
1317
+ struct zstream *z;
1318
+ VALUE dst;
1319
+
1320
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1321
+ dst = zstream_detach_input(z);
1322
+ OBJ_INFECT(dst, obj);
1323
+ return dst;
1324
+ }
1325
+
1326
+ /*
1327
+ * call-seq:
1328
+ * flush_next_out -> String
1329
+ * flush_next_out { |chunk| ... } -> nil
1330
+ *
1331
+ * Flushes output buffer and returns all data in that buffer. If a block is
1332
+ * given each chunk is yielded to the block until the current output buffer
1333
+ * has been flushed.
1334
+ */
1335
+ static VALUE
1336
+ rb_zstream_flush_next_out(VALUE obj)
1337
+ {
1338
+ struct zstream *z;
1339
+
1340
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1341
+
1342
+ return zstream_detach_buffer(z);
1343
+ }
1344
+
1345
+ /*
1346
+ * Returns number of bytes of free spaces in output buffer. Because the free
1347
+ * space is allocated automatically, this method returns 0 normally.
1348
+ */
1349
+ static VALUE
1350
+ rb_zstream_avail_out(VALUE obj)
1351
+ {
1352
+ struct zstream *z;
1353
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1354
+ return rb_uint2inum(z->stream.avail_out);
1355
+ }
1356
+
1357
+ /*
1358
+ * Allocates +size+ bytes of free space in the output buffer. If there are more
1359
+ * than +size+ bytes already in the buffer, the buffer is truncated. Because
1360
+ * free space is allocated automatically, you usually don't need to use this
1361
+ * method.
1362
+ */
1363
+ static VALUE
1364
+ rb_zstream_set_avail_out(VALUE obj, VALUE size)
1365
+ {
1366
+ struct zstream *z = get_zstream(obj);
1367
+
1368
+ Check_Type(size, T_FIXNUM);
1369
+ zstream_expand_buffer_into(z, FIX2INT(size));
1370
+ return size;
1371
+ }
1372
+
1373
+ /*
1374
+ * Returns bytes of data in the input buffer. Normally, returns 0.
1375
+ */
1376
+ static VALUE
1377
+ rb_zstream_avail_in(VALUE obj)
1378
+ {
1379
+ struct zstream *z;
1380
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1381
+ return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input)));
1382
+ }
1383
+
1384
+ /*
1385
+ * Returns the total bytes of the input data to the stream. FIXME
1386
+ */
1387
+ static VALUE
1388
+ rb_zstream_total_in(VALUE obj)
1389
+ {
1390
+ return rb_uint2inum(get_zstream(obj)->stream.total_in);
1391
+ }
1392
+
1393
+ /*
1394
+ * Returns the total bytes of the output data from the stream. FIXME
1395
+ */
1396
+ static VALUE
1397
+ rb_zstream_total_out(VALUE obj)
1398
+ {
1399
+ return rb_uint2inum(get_zstream(obj)->stream.total_out);
1400
+ }
1401
+
1402
+ /*
1403
+ * Guesses the type of the data which have been inputed into the stream. The
1404
+ * returned value is either <tt>BINARY</tt>, <tt>ASCII</tt>, or
1405
+ * <tt>UNKNOWN</tt>.
1406
+ */
1407
+ static VALUE
1408
+ rb_zstream_data_type(VALUE obj)
1409
+ {
1410
+ return INT2FIX(get_zstream(obj)->stream.data_type);
1411
+ }
1412
+
1413
+ /*
1414
+ * Returns the adler-32 checksum.
1415
+ */
1416
+ static VALUE
1417
+ rb_zstream_adler(VALUE obj)
1418
+ {
1419
+ return rb_uint2inum(get_zstream(obj)->stream.adler);
1420
+ }
1421
+
1422
+ /*
1423
+ * Returns true if the stream is finished.
1424
+ */
1425
+ static VALUE
1426
+ rb_zstream_finished_p(VALUE obj)
1427
+ {
1428
+ return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse;
1429
+ }
1430
+
1431
+ /*
1432
+ * Returns true if the stream is closed.
1433
+ */
1434
+ static VALUE
1435
+ rb_zstream_closed_p(VALUE obj)
1436
+ {
1437
+ struct zstream *z;
1438
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1439
+ return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue;
1440
+ }
1441
+
1442
+
1443
+ /* ------------------------------------------------------------------------- */
1444
+
1445
+ /*
1446
+ * Document-class: Zlib::Deflate
1447
+ *
1448
+ * Zlib::Deflate is the class for compressing data. See Zlib::ZStream for more
1449
+ * information.
1450
+ */
1451
+
1452
+ #define FIXNUMARG(val, ifnil) \
1453
+ (NIL_P((val)) ? (ifnil) \
1454
+ : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val))))
1455
+
1456
+ #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1457
+ #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1458
+ #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1459
+ #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1460
+ #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1461
+
1462
+
1463
+ static VALUE
1464
+ rb_deflate_s_allocate(VALUE klass)
1465
+ {
1466
+ return zstream_deflate_new(klass);
1467
+ }
1468
+
1469
+ /*
1470
+ * Document-method: Zlib::Deflate.new
1471
+ *
1472
+ * call-seq:
1473
+ * Zlib::Deflate.new(level=DEFAULT_COMPRESSION, window_bits=MAX_WBITS, mem_level=DEF_MEM_LEVEL, strategy=DEFAULT_STRATEGY)
1474
+ *
1475
+ * Creates a new deflate stream for compression. If a given argument is nil,
1476
+ * the default value of that argument is used.
1477
+ *
1478
+ * The +level+ sets the compression level for the deflate stream between 0 (no
1479
+ * compression) and 9 (best compression). The following constants have been
1480
+ * defined to make code more readable:
1481
+ *
1482
+ * * Zlib::DEFAULT_COMPRESSION
1483
+ * * Zlib::NO_COMPRESSION
1484
+ * * Zlib::BEST_SPEED
1485
+ * * Zlib::BEST_COMPRESSION
1486
+ *
1487
+ * See http://www.zlib.net/manual.html#Constants for further information.
1488
+ *
1489
+ * The +window_bits+ sets the size of the history buffer and should be between
1490
+ * 8 and 15. Larger values of this parameter result in better compression at
1491
+ * the expense of memory usage.
1492
+ *
1493
+ * The +mem_level+ specifies how much memory should be allocated for the
1494
+ * internal compression state. 1 uses minimum memory but is slow and reduces
1495
+ * compression ratio while 9 uses maximum memory for optimal speed. The
1496
+ * default value is 8. Two constants are defined:
1497
+ *
1498
+ * * Zlib::DEF_MEM_LEVEL
1499
+ * * Zlib::MAX_MEM_LEVEL
1500
+ *
1501
+ * The +strategy+ sets the deflate compression strategy. The following
1502
+ * strategies are available:
1503
+ *
1504
+ * Zlib::DEFAULT_STRATEGY:: For normal data
1505
+ * Zlib::FILTERED:: For data produced by a filter or predictor
1506
+ * Zlib::FIXED:: Prevents dynamic Huffman codes
1507
+ * Zlib::HUFFMAN_ONLY:: Prevents string matching
1508
+ * Zlib::RLE:: Designed for better compression of PNG image data
1509
+ *
1510
+ * See the constants for further description.
1511
+ *
1512
+ * == Examples
1513
+ *
1514
+ * === Basic
1515
+ *
1516
+ * open "compressed.file", "w+" do |io|
1517
+ * io << Zlib::Deflate.new.deflate(File.read("big.file"))
1518
+ * end
1519
+ *
1520
+ * === Custom compression
1521
+ *
1522
+ * open "compressed.file", "w+" do |compressed_io|
1523
+ * deflate = Zlib::Deflate.new(Zlib::BEST_COMPRESSION,
1524
+ * Zlib::MAX_WBITS,
1525
+ * Zlib::MAX_MEM_LEVEL,
1526
+ * Zlib::HUFFMAN_ONLY)
1527
+ *
1528
+ * begin
1529
+ * open "big.file" do |big_io|
1530
+ * until big_io.eof? do
1531
+ * compressed_io << zd.deflate(big_io.read(16384))
1532
+ * end
1533
+ * end
1534
+ * ensure
1535
+ * deflate.close
1536
+ * end
1537
+ * end
1538
+ *
1539
+ * While this example will work, for best optimization review the flags for
1540
+ * your specific time, memory usage and output space requirements.
1541
+ */
1542
+ static VALUE
1543
+ rb_deflate_initialize(int argc, VALUE *argv, VALUE obj)
1544
+ {
1545
+ struct zstream *z;
1546
+ VALUE level, wbits, memlevel, strategy;
1547
+ int err;
1548
+
1549
+ rb_scan_args(argc, argv, "04", &level, &wbits, &memlevel, &strategy);
1550
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1551
+
1552
+ err = deflateInit2(&z->stream, ARG_LEVEL(level), Z_DEFLATED,
1553
+ ARG_WBITS(wbits), ARG_MEMLEVEL(memlevel),
1554
+ ARG_STRATEGY(strategy));
1555
+ if (err != Z_OK) {
1556
+ raise_zlib_error(err, z->stream.msg);
1557
+ }
1558
+ ZSTREAM_READY(z);
1559
+
1560
+ return obj;
1561
+ }
1562
+
1563
+ /*
1564
+ * Document-method: Zlib::Deflate#initialize_copy
1565
+ *
1566
+ * Duplicates the deflate stream.
1567
+ */
1568
+ static VALUE
1569
+ rb_deflate_init_copy(VALUE self, VALUE orig)
1570
+ {
1571
+ struct zstream *z1, *z2;
1572
+ int err;
1573
+
1574
+ TypedData_Get_Struct(self, struct zstream, &zstream_data_type, z1);
1575
+ z2 = get_zstream(orig);
1576
+
1577
+ if (z1 == z2) return self;
1578
+ err = deflateCopy(&z1->stream, &z2->stream);
1579
+ if (err != Z_OK) {
1580
+ raise_zlib_error(err, 0);
1581
+ }
1582
+ z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input);
1583
+ z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf);
1584
+ z1->buf_filled = z2->buf_filled;
1585
+ z1->flags = z2->flags;
1586
+
1587
+ return self;
1588
+ }
1589
+
1590
+ static VALUE
1591
+ deflate_run(VALUE args)
1592
+ {
1593
+ struct zstream *z = (struct zstream*)((VALUE*)args)[0];
1594
+ VALUE src = ((VALUE*)args)[1];
1595
+
1596
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_FINISH);
1597
+ return zstream_detach_buffer(z);
1598
+ }
1599
+
1600
+ /*
1601
+ * Document-method: Zlib::Deflate.deflate
1602
+ *
1603
+ * call-seq:
1604
+ * Zlib.deflate(string[, level])
1605
+ * Zlib::Deflate.deflate(string[, level])
1606
+ *
1607
+ * Compresses the given +string+. Valid values of level are
1608
+ * Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION,
1609
+ * Zlib::DEFAULT_COMPRESSION, or an integer from 0 to 9.
1610
+ *
1611
+ * This method is almost equivalent to the following code:
1612
+ *
1613
+ * def deflate(string, level)
1614
+ * z = Zlib::Deflate.new(level)
1615
+ * dst = z.deflate(string, Zlib::FINISH)
1616
+ * z.close
1617
+ * dst
1618
+ * end
1619
+ *
1620
+ * See also Zlib.inflate
1621
+ *
1622
+ */
1623
+ static VALUE
1624
+ rb_deflate_s_deflate(int argc, VALUE *argv, VALUE klass)
1625
+ {
1626
+ struct zstream z;
1627
+ VALUE src, level, dst, args[2];
1628
+ int err, lev;
1629
+
1630
+ rb_scan_args(argc, argv, "11", &src, &level);
1631
+
1632
+ lev = ARG_LEVEL(level);
1633
+ StringValue(src);
1634
+ zstream_init_deflate(&z);
1635
+ err = deflateInit(&z.stream, lev);
1636
+ if (err != Z_OK) {
1637
+ raise_zlib_error(err, z.stream.msg);
1638
+ }
1639
+ ZSTREAM_READY(&z);
1640
+
1641
+ args[0] = (VALUE)&z;
1642
+ args[1] = src;
1643
+ dst = rb_ensure(deflate_run, (VALUE)args, zstream_end, (VALUE)&z);
1644
+
1645
+ OBJ_INFECT(dst, src);
1646
+ return dst;
1647
+ }
1648
+
1649
+ static void
1650
+ do_deflate(struct zstream *z, VALUE src, int flush)
1651
+ {
1652
+ if (NIL_P(src)) {
1653
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1654
+ return;
1655
+ }
1656
+ StringValue(src);
1657
+ if (flush != Z_NO_FLUSH || RSTRING_LEN(src) > 0) { /* prevent BUF_ERROR */
1658
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), flush);
1659
+ }
1660
+ }
1661
+
1662
+ /*
1663
+ * Document-method: Zlib::Deflate#deflate
1664
+ *
1665
+ * call-seq:
1666
+ * z.deflate(string, flush = Zlib::NO_FLUSH) -> String
1667
+ * z.deflate(string, flush = Zlib::NO_FLUSH) { |chunk| ... } -> nil
1668
+ *
1669
+ * Inputs +string+ into the deflate stream and returns the output from the
1670
+ * stream. On calling this method, both the input and the output buffers of
1671
+ * the stream are flushed. If +string+ is nil, this method finishes the
1672
+ * stream, just like Zlib::ZStream#finish.
1673
+ *
1674
+ * If a block is given consecutive deflated chunks from the +string+ are
1675
+ * yielded to the block and +nil+ is returned.
1676
+ *
1677
+ * The +flush+ parameter specifies the flush mode. The following constants
1678
+ * may be used:
1679
+ *
1680
+ * Zlib::NO_FLUSH:: The default
1681
+ * Zlib::SYNC_FLUSH:: Flushes the output to a byte boundary
1682
+ * Zlib::FULL_FLUSH:: SYNC_FLUSH + resets the compression state
1683
+ * Zlib::FINISH:: Pending input is processed, pending output is flushed.
1684
+ *
1685
+ * See the constants for further description.
1686
+ *
1687
+ */
1688
+ static VALUE
1689
+ rb_deflate_deflate(int argc, VALUE *argv, VALUE obj)
1690
+ {
1691
+ struct zstream *z = get_zstream(obj);
1692
+ VALUE src, flush;
1693
+
1694
+ rb_scan_args(argc, argv, "11", &src, &flush);
1695
+ OBJ_INFECT(obj, src);
1696
+ do_deflate(z, src, ARG_FLUSH(flush));
1697
+
1698
+ return zstream_detach_buffer(z);
1699
+ }
1700
+
1701
+ /*
1702
+ * Document-method: Zlib::Deflate#<<
1703
+ *
1704
+ * call-seq: << string
1705
+ *
1706
+ * Inputs +string+ into the deflate stream just like Zlib::Deflate#deflate, but
1707
+ * returns the Zlib::Deflate object itself. The output from the stream is
1708
+ * preserved in output buffer.
1709
+ */
1710
+ static VALUE
1711
+ rb_deflate_addstr(VALUE obj, VALUE src)
1712
+ {
1713
+ OBJ_INFECT(obj, src);
1714
+ do_deflate(get_zstream(obj), src, Z_NO_FLUSH);
1715
+ return obj;
1716
+ }
1717
+
1718
+ /*
1719
+ * Document-method: Zlib::Deflate#flush
1720
+ *
1721
+ * call-seq:
1722
+ * flush(flush = Zlib::SYNC_FLUSH) -> String
1723
+ * flush(flush = Zlib::SYNC_FLUSH) { |chunk| ... } -> nil
1724
+ *
1725
+ * This method is equivalent to <tt>deflate('', flush)</tt>. This method is
1726
+ * just provided to improve the readability of your Ruby program. If a block
1727
+ * is given chunks of deflate output are yielded to the block until the buffer
1728
+ * is flushed.
1729
+ *
1730
+ * See Zlib::Deflate#deflate for detail on the +flush+ constants NO_FLUSH,
1731
+ * SYNC_FLUSH, FULL_FLUSH and FINISH.
1732
+ */
1733
+ static VALUE
1734
+ rb_deflate_flush(int argc, VALUE *argv, VALUE obj)
1735
+ {
1736
+ struct zstream *z = get_zstream(obj);
1737
+ VALUE v_flush;
1738
+ int flush;
1739
+
1740
+ rb_scan_args(argc, argv, "01", &v_flush);
1741
+ flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1742
+ if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */
1743
+ zstream_run(z, (Bytef*)"", 0, flush);
1744
+ }
1745
+
1746
+ return zstream_detach_buffer(z);
1747
+ }
1748
+
1749
+ /*
1750
+ * Document-method: Zlib::Deflate.params
1751
+ *
1752
+ * call-seq: params(level, strategy)
1753
+ *
1754
+ * Changes the parameters of the deflate stream to allow changes between
1755
+ * different types of data that require different types of compression. Any
1756
+ * unprocessed data is flushed before changing the params.
1757
+ *
1758
+ * See Zlib::Deflate.new for a description of +level+ and +strategy+.
1759
+ *
1760
+ */
1761
+ static VALUE
1762
+ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
1763
+ {
1764
+ struct zstream *z = get_zstream(obj);
1765
+ int level, strategy;
1766
+ int err;
1767
+ uInt n;
1768
+
1769
+ level = ARG_LEVEL(v_level);
1770
+ strategy = ARG_STRATEGY(v_strategy);
1771
+
1772
+ n = z->stream.avail_out;
1773
+ err = deflateParams(&z->stream, level, strategy);
1774
+ z->buf_filled += n - z->stream.avail_out;
1775
+ while (err == Z_BUF_ERROR) {
1776
+ rb_warning("deflateParams() returned Z_BUF_ERROR");
1777
+ zstream_expand_buffer(z);
1778
+ n = z->stream.avail_out;
1779
+ err = deflateParams(&z->stream, level, strategy);
1780
+ z->buf_filled += n - z->stream.avail_out;
1781
+ }
1782
+ if (err != Z_OK) {
1783
+ raise_zlib_error(err, z->stream.msg);
1784
+ }
1785
+
1786
+ return Qnil;
1787
+ }
1788
+
1789
+ /*
1790
+ * Document-method: Zlib::Deflate.set_dictionary
1791
+ *
1792
+ * call-seq: set_dictionary(string)
1793
+ *
1794
+ * Sets the preset dictionary and returns +string+. This method is available
1795
+ * just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called.
1796
+ * See zlib.h for details.
1797
+ *
1798
+ * Can raise errors of Z_STREAM_ERROR if a parameter is invalid (such as
1799
+ * NULL dictionary) or the stream state is inconsistent, Z_DATA_ERROR if
1800
+ * the given dictionary doesn't match the expected one (incorrect adler32 value)
1801
+ *
1802
+ */
1803
+ static VALUE
1804
+ rb_deflate_set_dictionary(VALUE obj, VALUE dic)
1805
+ {
1806
+ struct zstream *z = get_zstream(obj);
1807
+ VALUE src = dic;
1808
+ int err;
1809
+
1810
+ OBJ_INFECT(obj, dic);
1811
+ StringValue(src);
1812
+ err = deflateSetDictionary(&z->stream,
1813
+ (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
1814
+ if (err != Z_OK) {
1815
+ raise_zlib_error(err, z->stream.msg);
1816
+ }
1817
+
1818
+ return dic;
1819
+ }
1820
+
1821
+
1822
+ /* ------------------------------------------------------------------------- */
1823
+
1824
+ /*
1825
+ * Document-class: Zlib::Inflate
1826
+ *
1827
+ * Zlib:Inflate is the class for decompressing compressed data. Unlike
1828
+ * Zlib::Deflate, an instance of this class is not able to duplicate (clone,
1829
+ * dup) itself.
1830
+ */
1831
+
1832
+ static VALUE
1833
+ rb_inflate_s_allocate(VALUE klass)
1834
+ {
1835
+ VALUE inflate = zstream_inflate_new(klass);
1836
+ rb_ivar_set(inflate, id_dictionaries, rb_hash_new());
1837
+ return inflate;
1838
+ }
1839
+
1840
+ /*
1841
+ * Document-method: Zlib::Inflate.new
1842
+ *
1843
+ * call-seq:
1844
+ * Zlib::Inflate.new(window_bits = Zlib::MAX_WBITS)
1845
+ *
1846
+ * Creates a new inflate stream for decompression. +window_bits+ sets the
1847
+ * size of the history buffer and can have the following values:
1848
+ *
1849
+ * 0::
1850
+ * Have inflate use the window size from the zlib header of the compressed
1851
+ * stream.
1852
+ *
1853
+ * (8..15)::
1854
+ * Overrides the window size of the inflate header in the compressed stream.
1855
+ * The window size must be greater than or equal to the window size of the
1856
+ * compressed stream.
1857
+ *
1858
+ * Greater than 15::
1859
+ * Add 32 to window_bits to enable zlib and gzip decoding with automatic
1860
+ * header detection, or add 16 to decode only the gzip format (a
1861
+ * Zlib::DataError will be raised for a non-gzip stream).
1862
+ *
1863
+ * (-8..-15)::
1864
+ * Enables raw deflate mode which will not generate a check value, and will
1865
+ * not look for any check values for comparison at the end of the stream.
1866
+ *
1867
+ * This is for use with other formats that use the deflate compressed data
1868
+ * format such as zip which provide their own check values.
1869
+ *
1870
+ * == Example
1871
+ *
1872
+ * open "compressed.file" do |compressed_io|
1873
+ * zi = Zlib::Inflate.new(Zlib::MAX_WBITS + 32)
1874
+ *
1875
+ * begin
1876
+ * open "uncompressed.file", "w+" do |uncompressed_io|
1877
+ * uncompressed_io << zi.inflate(compressed_io.read)
1878
+ * end
1879
+ * ensure
1880
+ * zi.close
1881
+ * end
1882
+ * end
1883
+ *
1884
+ */
1885
+ static VALUE
1886
+ rb_inflate_initialize(int argc, VALUE *argv, VALUE obj)
1887
+ {
1888
+ struct zstream *z;
1889
+ VALUE wbits;
1890
+ int err;
1891
+
1892
+ rb_scan_args(argc, argv, "01", &wbits);
1893
+ TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
1894
+
1895
+ err = inflateInit2(&z->stream, ARG_WBITS(wbits));
1896
+ if (err != Z_OK) {
1897
+ raise_zlib_error(err, z->stream.msg);
1898
+ }
1899
+ ZSTREAM_READY(z);
1900
+
1901
+ return obj;
1902
+ }
1903
+
1904
+ static VALUE
1905
+ inflate_run(VALUE args)
1906
+ {
1907
+ struct zstream *z = (struct zstream*)((VALUE*)args)[0];
1908
+ VALUE src = ((VALUE*)args)[1];
1909
+
1910
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1911
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH); /* for checking errors */
1912
+ return zstream_detach_buffer(z);
1913
+ }
1914
+
1915
+ /*
1916
+ * Document-method: Zlib::inflate
1917
+ *
1918
+ * call-seq:
1919
+ * Zlib.inflate(string)
1920
+ * Zlib::Inflate.inflate(string)
1921
+ *
1922
+ * Decompresses +string+. Raises a Zlib::NeedDict exception if a preset
1923
+ * dictionary is needed for decompression.
1924
+ *
1925
+ * This method is almost equivalent to the following code:
1926
+ *
1927
+ * def inflate(string)
1928
+ * zstream = Zlib::Inflate.new
1929
+ * buf = zstream.inflate(string)
1930
+ * zstream.finish
1931
+ * zstream.close
1932
+ * buf
1933
+ * end
1934
+ *
1935
+ * See also Zlib.deflate
1936
+ *
1937
+ */
1938
+ static VALUE
1939
+ rb_inflate_s_inflate(VALUE obj, VALUE src)
1940
+ {
1941
+ struct zstream z;
1942
+ VALUE dst, args[2];
1943
+ int err;
1944
+
1945
+ StringValue(src);
1946
+ zstream_init_inflate(&z);
1947
+ err = inflateInit(&z.stream);
1948
+ if (err != Z_OK) {
1949
+ raise_zlib_error(err, z.stream.msg);
1950
+ }
1951
+ ZSTREAM_READY(&z);
1952
+
1953
+ args[0] = (VALUE)&z;
1954
+ args[1] = src;
1955
+ dst = rb_ensure(inflate_run, (VALUE)args, zstream_end, (VALUE)&z);
1956
+
1957
+ OBJ_INFECT(dst, src);
1958
+ return dst;
1959
+ }
1960
+
1961
+ static void
1962
+ do_inflate(struct zstream *z, VALUE src)
1963
+ {
1964
+ if (NIL_P(src)) {
1965
+ zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1966
+ return;
1967
+ }
1968
+ StringValue(src);
1969
+ if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */
1970
+ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1971
+ }
1972
+ }
1973
+
1974
+ /* Document-method: Zlib::Inflate#add_dictionary
1975
+ *
1976
+ * call-seq: add_dictionary(string)
1977
+ *
1978
+ * Provide the inflate stream with a dictionary that may be required in the
1979
+ * future. Multiple dictionaries may be provided. The inflate stream will
1980
+ * automatically choose the correct user-provided dictionary based on the
1981
+ * stream's required dictionary.
1982
+ */
1983
+ static VALUE
1984
+ rb_inflate_add_dictionary(VALUE obj, VALUE dictionary)
1985
+ {
1986
+ VALUE dictionaries = rb_ivar_get(obj, id_dictionaries);
1987
+ VALUE checksum = do_checksum(1, &dictionary, adler32);
1988
+
1989
+ rb_hash_aset(dictionaries, checksum, dictionary);
1990
+
1991
+ return obj;
1992
+ }
1993
+
1994
+ /*
1995
+ * Document-method: Zlib::Inflate#inflate
1996
+ *
1997
+ * call-seq:
1998
+ * inflate(deflate_string) -> String
1999
+ * inflate(deflate_string) { |chunk| ... } -> nil
2000
+ *
2001
+ * Inputs +deflate_string+ into the inflate stream and returns the output from
2002
+ * the stream. Calling this method, both the input and the output buffer of
2003
+ * the stream are flushed. If string is +nil+, this method finishes the
2004
+ * stream, just like Zlib::ZStream#finish.
2005
+ *
2006
+ * If a block is given consecutive inflated chunks from the +deflate_string+
2007
+ * are yielded to the block and +nil+ is returned.
2008
+ *
2009
+ * Raises a Zlib::NeedDict exception if a preset dictionary is needed to
2010
+ * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then
2011
+ * call this method again with an empty string to flush the stream:
2012
+ *
2013
+ * inflater = Zlib::Inflate.new
2014
+ *
2015
+ * begin
2016
+ * out = inflater.inflate compressed
2017
+ * rescue Zlib::NeedDict
2018
+ * # ensure the dictionary matches the stream's required dictionary
2019
+ * raise unless inflater.adler == Zlib.adler32(dictionary)
2020
+ *
2021
+ * inflater.set_dictionary dictionary
2022
+ * inflater.inflate ''
2023
+ * end
2024
+ *
2025
+ * # ...
2026
+ *
2027
+ * inflater.close
2028
+ *
2029
+ * See also Zlib::Inflate.new
2030
+ */
2031
+ static VALUE
2032
+ rb_inflate_inflate(VALUE obj, VALUE src)
2033
+ {
2034
+ struct zstream *z = get_zstream(obj);
2035
+ VALUE dst;
2036
+
2037
+ OBJ_INFECT(obj, src);
2038
+
2039
+ if (ZSTREAM_IS_FINISHED(z)) {
2040
+ if (NIL_P(src)) {
2041
+ dst = zstream_detach_buffer(z);
2042
+ }
2043
+ else {
2044
+ StringValue(src);
2045
+ zstream_append_buffer2(z, src);
2046
+ dst = rb_str_new(0, 0);
2047
+ OBJ_INFECT(dst, obj);
2048
+ }
2049
+ }
2050
+ else {
2051
+ do_inflate(z, src);
2052
+ dst = zstream_detach_buffer(z);
2053
+ if (ZSTREAM_IS_FINISHED(z)) {
2054
+ zstream_passthrough_input(z);
2055
+ }
2056
+ }
2057
+
2058
+ return dst;
2059
+ }
2060
+
2061
+ /*
2062
+ * call-seq: << string
2063
+ *
2064
+ * Inputs +string+ into the inflate stream just like Zlib::Inflate#inflate, but
2065
+ * returns the Zlib::Inflate object itself. The output from the stream is
2066
+ * preserved in output buffer.
2067
+ */
2068
+ static VALUE
2069
+ rb_inflate_addstr(VALUE obj, VALUE src)
2070
+ {
2071
+ struct zstream *z = get_zstream(obj);
2072
+
2073
+ OBJ_INFECT(obj, src);
2074
+
2075
+ if (ZSTREAM_IS_FINISHED(z)) {
2076
+ if (!NIL_P(src)) {
2077
+ StringValue(src);
2078
+ zstream_append_buffer2(z, src);
2079
+ }
2080
+ }
2081
+ else {
2082
+ do_inflate(z, src);
2083
+ if (ZSTREAM_IS_FINISHED(z)) {
2084
+ zstream_passthrough_input(z);
2085
+ }
2086
+ }
2087
+
2088
+ return obj;
2089
+ }
2090
+
2091
+ /*
2092
+ * call-seq: sync(string)
2093
+ *
2094
+ * Inputs +string+ into the end of input buffer and skips data until a full
2095
+ * flush point can be found. If the point is found in the buffer, this method
2096
+ * flushes the buffer and returns false. Otherwise it returns +true+ and the
2097
+ * following data of full flush point is preserved in the buffer.
2098
+ */
2099
+ static VALUE
2100
+ rb_inflate_sync(VALUE obj, VALUE src)
2101
+ {
2102
+ struct zstream *z = get_zstream(obj);
2103
+
2104
+ OBJ_INFECT(obj, src);
2105
+ StringValue(src);
2106
+ return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src));
2107
+ }
2108
+
2109
+ /*
2110
+ * Quoted verbatim from original documentation:
2111
+ *
2112
+ * What is this?
2113
+ *
2114
+ * <tt>:)</tt>
2115
+ */
2116
+ static VALUE
2117
+ rb_inflate_sync_point_p(VALUE obj)
2118
+ {
2119
+ struct zstream *z = get_zstream(obj);
2120
+ int err;
2121
+
2122
+ err = inflateSyncPoint(&z->stream);
2123
+ if (err == 1) {
2124
+ return Qtrue;
2125
+ }
2126
+ if (err != Z_OK) {
2127
+ raise_zlib_error(err, z->stream.msg);
2128
+ }
2129
+ return Qfalse;
2130
+ }
2131
+
2132
+ /*
2133
+ * Document-method: Zlib::Inflate#set_dictionary
2134
+ *
2135
+ * Sets the preset dictionary and returns +string+. This method is available just
2136
+ * only after a Zlib::NeedDict exception was raised. See zlib.h for details.
2137
+ *
2138
+ */
2139
+ static VALUE
2140
+ rb_inflate_set_dictionary(VALUE obj, VALUE dic)
2141
+ {
2142
+ struct zstream *z = get_zstream(obj);
2143
+ VALUE src = dic;
2144
+ int err;
2145
+
2146
+ OBJ_INFECT(obj, dic);
2147
+ StringValue(src);
2148
+ err = inflateSetDictionary(&z->stream,
2149
+ (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
2150
+ if (err != Z_OK) {
2151
+ raise_zlib_error(err, z->stream.msg);
2152
+ }
2153
+
2154
+ return dic;
2155
+ }
2156
+
2157
+
2158
+
2159
+ #if GZIP_SUPPORT
2160
+
2161
+ /* NOTE: Features for gzip files of Ruby/zlib are written from scratch
2162
+ * and using undocumented feature of zlib, negative wbits.
2163
+ * I don't think gzFile APIs of zlib are good for Ruby.
2164
+ */
2165
+
2166
+ /*------- .gz file header --------*/
2167
+
2168
+ #define GZ_MAGIC1 0x1f
2169
+ #define GZ_MAGIC2 0x8b
2170
+ #define GZ_METHOD_DEFLATE 8
2171
+ #define GZ_FLAG_MULTIPART 0x2
2172
+ #define GZ_FLAG_EXTRA 0x4
2173
+ #define GZ_FLAG_ORIG_NAME 0x8
2174
+ #define GZ_FLAG_COMMENT 0x10
2175
+ #define GZ_FLAG_ENCRYPT 0x20
2176
+ #define GZ_FLAG_UNKNOWN_MASK 0xc0
2177
+
2178
+ #define GZ_EXTRAFLAG_FAST 0x4
2179
+ #define GZ_EXTRAFLAG_SLOW 0x2
2180
+
2181
+ /* from zutil.h */
2182
+ #define OS_MSDOS 0x00
2183
+ #define OS_AMIGA 0x01
2184
+ #define OS_VMS 0x02
2185
+ #define OS_UNIX 0x03
2186
+ #define OS_ATARI 0x05
2187
+ #define OS_OS2 0x06
2188
+ #define OS_MACOS 0x07
2189
+ #define OS_TOPS20 0x0a
2190
+ #define OS_WIN32 0x0b
2191
+
2192
+ #define OS_VMCMS 0x04
2193
+ #define OS_ZSYSTEM 0x08
2194
+ #define OS_CPM 0x09
2195
+ #define OS_QDOS 0x0c
2196
+ #define OS_RISCOS 0x0d
2197
+ #define OS_UNKNOWN 0xff
2198
+
2199
+ #ifndef OS_CODE
2200
+ #define OS_CODE OS_UNIX
2201
+ #endif
2202
+
2203
+ static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path, id_input;
2204
+ static VALUE cGzError, cNoFooter, cCRCError, cLengthError;
2205
+
2206
+
2207
+
2208
+ /*-------- gzfile internal APIs --------*/
2209
+
2210
+ struct gzfile {
2211
+ struct zstream z;
2212
+ VALUE io;
2213
+ int level;
2214
+ int os_code; /* for header */
2215
+ time_t mtime; /* for header */
2216
+ VALUE orig_name; /* for header; must be a String */
2217
+ VALUE comment; /* for header; must be a String */
2218
+ unsigned long crc;
2219
+ int ecflags;
2220
+ int lineno;
2221
+ long ungetc;
2222
+ void (*end)(struct gzfile *);
2223
+ rb_encoding *enc;
2224
+ rb_encoding *enc2;
2225
+ rb_econv_t *ec;
2226
+ VALUE ecopts;
2227
+ char *cbuf;
2228
+ VALUE path;
2229
+ };
2230
+ #define GZFILE_CBUF_CAPA 10
2231
+
2232
+ #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
2233
+ #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
2234
+ #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
2235
+
2236
+ #define GZFILE_IS_FINISHED(gz) \
2237
+ (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
2238
+
2239
+ #define GZFILE_READ_SIZE 2048
2240
+
2241
+
2242
+ static void
2243
+ gzfile_mark(void *p)
2244
+ {
2245
+ struct gzfile *gz = p;
2246
+
2247
+ rb_gc_mark(gz->io);
2248
+ rb_gc_mark(gz->orig_name);
2249
+ rb_gc_mark(gz->comment);
2250
+ zstream_mark(&gz->z);
2251
+ rb_gc_mark(gz->ecopts);
2252
+ rb_gc_mark(gz->path);
2253
+ }
2254
+
2255
+ static void
2256
+ gzfile_free(void *p)
2257
+ {
2258
+ struct gzfile *gz = p;
2259
+ struct zstream *z = &gz->z;
2260
+
2261
+ if (ZSTREAM_IS_READY(z)) {
2262
+ if (z->func == &deflate_funcs) {
2263
+ finalizer_warn("Zlib::GzipWriter object must be closed explicitly.");
2264
+ }
2265
+ zstream_finalize(z);
2266
+ }
2267
+ if (gz->cbuf) {
2268
+ xfree(gz->cbuf);
2269
+ }
2270
+ xfree(gz);
2271
+ }
2272
+
2273
+ static size_t
2274
+ gzfile_memsize(const void *p)
2275
+ {
2276
+ const struct gzfile *gz = p;
2277
+ size_t size = sizeof(struct gzfile);
2278
+
2279
+ if (gz->cbuf)
2280
+ size += GZFILE_CBUF_CAPA;
2281
+
2282
+ return size;
2283
+ }
2284
+
2285
+ static const rb_data_type_t gzfile_data_type = {
2286
+ "gzfile",
2287
+ { gzfile_mark, gzfile_free, gzfile_memsize, },
2288
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2289
+ };
2290
+
2291
+ static VALUE
2292
+ gzfile_new(klass, funcs, endfunc)
2293
+ VALUE klass;
2294
+ const struct zstream_funcs *funcs;
2295
+ void (*endfunc)(struct gzfile *);
2296
+ {
2297
+ VALUE obj;
2298
+ struct gzfile *gz;
2299
+
2300
+ obj = TypedData_Make_Struct(klass, struct gzfile, &gzfile_data_type, gz);
2301
+ zstream_init(&gz->z, funcs);
2302
+ gz->z.flags |= ZSTREAM_FLAG_GZFILE;
2303
+ gz->io = Qnil;
2304
+ gz->level = 0;
2305
+ gz->mtime = 0;
2306
+ gz->os_code = OS_CODE;
2307
+ gz->orig_name = Qnil;
2308
+ gz->comment = Qnil;
2309
+ gz->crc = crc32(0, Z_NULL, 0);
2310
+ gz->lineno = 0;
2311
+ gz->ungetc = 0;
2312
+ gz->end = endfunc;
2313
+ gz->enc = rb_default_external_encoding();
2314
+ gz->enc2 = 0;
2315
+ gz->ec = NULL;
2316
+ gz->ecflags = 0;
2317
+ gz->ecopts = Qnil;
2318
+ gz->cbuf = 0;
2319
+ gz->path = Qnil;
2320
+
2321
+ return obj;
2322
+ }
2323
+
2324
+ #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2325
+ #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2326
+
2327
+ static void
2328
+ gzfile_reset(struct gzfile *gz)
2329
+ {
2330
+ zstream_reset(&gz->z);
2331
+ gz->z.flags |= ZSTREAM_FLAG_GZFILE;
2332
+ gz->crc = crc32(0, Z_NULL, 0);
2333
+ gz->lineno = 0;
2334
+ gz->ungetc = 0;
2335
+ if (gz->ec) {
2336
+ rb_econv_close(gz->ec);
2337
+ gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name,
2338
+ gz->ecflags, gz->ecopts);
2339
+ }
2340
+ }
2341
+
2342
+ static void
2343
+ gzfile_close(struct gzfile *gz, int closeflag)
2344
+ {
2345
+ VALUE io = gz->io;
2346
+
2347
+ gz->end(gz);
2348
+ gz->io = Qnil;
2349
+ gz->orig_name = Qnil;
2350
+ gz->comment = Qnil;
2351
+ if (closeflag && rb_respond_to(io, id_close)) {
2352
+ rb_funcall(io, id_close, 0);
2353
+ }
2354
+ }
2355
+
2356
+ static void
2357
+ gzfile_write_raw(struct gzfile *gz)
2358
+ {
2359
+ VALUE str;
2360
+
2361
+ if (gz->z.buf_filled > 0) {
2362
+ str = zstream_detach_buffer(&gz->z);
2363
+ OBJ_TAINT(str); /* for safe */
2364
+ rb_funcall(gz->io, id_write, 1, str);
2365
+ if ((gz->z.flags & GZFILE_FLAG_SYNC)
2366
+ && rb_respond_to(gz->io, id_flush))
2367
+ rb_funcall(gz->io, id_flush, 0);
2368
+ }
2369
+ }
2370
+
2371
+ static VALUE
2372
+ gzfile_read_raw_partial(VALUE arg)
2373
+ {
2374
+ struct gzfile *gz = (struct gzfile*)arg;
2375
+ VALUE str;
2376
+
2377
+ str = rb_funcall(gz->io, id_readpartial, 1, INT2FIX(GZFILE_READ_SIZE));
2378
+ Check_Type(str, T_STRING);
2379
+ return str;
2380
+ }
2381
+
2382
+ static VALUE
2383
+ gzfile_read_raw_rescue(VALUE arg)
2384
+ {
2385
+ struct gzfile *gz = (struct gzfile*)arg;
2386
+ VALUE str = Qnil;
2387
+ if (rb_obj_is_kind_of(rb_errinfo(), rb_eNoMethodError)) {
2388
+ str = rb_funcall(gz->io, id_read, 1, INT2FIX(GZFILE_READ_SIZE));
2389
+ if (!NIL_P(str)) {
2390
+ Check_Type(str, T_STRING);
2391
+ }
2392
+ }
2393
+ return str; /* return nil when EOFError */
2394
+ }
2395
+
2396
+ static VALUE
2397
+ gzfile_read_raw(struct gzfile *gz)
2398
+ {
2399
+ return rb_rescue2(gzfile_read_raw_partial, (VALUE)gz,
2400
+ gzfile_read_raw_rescue, (VALUE)gz,
2401
+ rb_eEOFError, rb_eNoMethodError, (VALUE)0);
2402
+ }
2403
+
2404
+ static int
2405
+ gzfile_read_raw_ensure(struct gzfile *gz, long size)
2406
+ {
2407
+ VALUE str;
2408
+
2409
+ while (NIL_P(gz->z.input) || RSTRING_LEN(gz->z.input) < size) {
2410
+ str = gzfile_read_raw(gz);
2411
+ if (NIL_P(str)) return 0;
2412
+ zstream_append_input2(&gz->z, str);
2413
+ }
2414
+ return 1;
2415
+ }
2416
+
2417
+ static char *
2418
+ gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
2419
+ {
2420
+ VALUE str;
2421
+ char *p;
2422
+
2423
+ for (;;) {
2424
+ p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0',
2425
+ RSTRING_LEN(gz->z.input) - offset);
2426
+ if (p) break;
2427
+ str = gzfile_read_raw(gz);
2428
+ if (NIL_P(str)) {
2429
+ rb_raise(cGzError, "unexpected end of file");
2430
+ }
2431
+ offset = RSTRING_LEN(gz->z.input);
2432
+ zstream_append_input2(&gz->z, str);
2433
+ }
2434
+ return p;
2435
+ }
2436
+
2437
+ static unsigned int
2438
+ gzfile_get16(const unsigned char *src)
2439
+ {
2440
+ unsigned int n;
2441
+ n = *(src++) & 0xff;
2442
+ n |= (*(src++) & 0xff) << 8;
2443
+ return n;
2444
+ }
2445
+
2446
+ static unsigned long
2447
+ gzfile_get32(const unsigned char *src)
2448
+ {
2449
+ unsigned long n;
2450
+ n = *(src++) & 0xff;
2451
+ n |= (*(src++) & 0xff) << 8;
2452
+ n |= (*(src++) & 0xff) << 16;
2453
+ n |= (*(src++) & 0xffU) << 24;
2454
+ return n;
2455
+ }
2456
+
2457
+ static void
2458
+ gzfile_set32(unsigned long n, unsigned char *dst)
2459
+ {
2460
+ *(dst++) = n & 0xff;
2461
+ *(dst++) = (n >> 8) & 0xff;
2462
+ *(dst++) = (n >> 16) & 0xff;
2463
+ *dst = (n >> 24) & 0xff;
2464
+ }
2465
+
2466
+ static void
2467
+ gzfile_raise(struct gzfile *gz, VALUE klass, const char *message)
2468
+ {
2469
+ VALUE exc = rb_exc_new2(klass, message);
2470
+ if (!NIL_P(gz->z.input)) {
2471
+ rb_ivar_set(exc, id_input, rb_str_resurrect(gz->z.input));
2472
+ }
2473
+ rb_exc_raise(exc);
2474
+ }
2475
+
2476
+ /*
2477
+ * Document-method: Zlib::GzipFile::Error#inspect
2478
+ *
2479
+ * Constructs a String of the GzipFile Error
2480
+ */
2481
+ static VALUE
2482
+ gzfile_error_inspect(VALUE error)
2483
+ {
2484
+ VALUE str = rb_call_super(0, 0);
2485
+ VALUE input = rb_attr_get(error, id_input);
2486
+
2487
+ if (!NIL_P(input)) {
2488
+ rb_str_resize(str, RSTRING_LEN(str)-1);
2489
+ rb_str_cat2(str, ", input=");
2490
+ rb_str_append(str, rb_str_inspect(input));
2491
+ rb_str_cat2(str, ">");
2492
+ }
2493
+ return str;
2494
+ }
2495
+
2496
+ static void
2497
+ gzfile_make_header(struct gzfile *gz)
2498
+ {
2499
+ Bytef buf[10]; /* the size of gzip header */
2500
+ unsigned char flags = 0, extraflags = 0;
2501
+
2502
+ if (!NIL_P(gz->orig_name)) {
2503
+ flags |= GZ_FLAG_ORIG_NAME;
2504
+ }
2505
+ if (!NIL_P(gz->comment)) {
2506
+ flags |= GZ_FLAG_COMMENT;
2507
+ }
2508
+ if (gz->mtime == 0) {
2509
+ gz->mtime = time(0);
2510
+ }
2511
+
2512
+ if (gz->level == Z_BEST_SPEED) {
2513
+ extraflags |= GZ_EXTRAFLAG_FAST;
2514
+ }
2515
+ else if (gz->level == Z_BEST_COMPRESSION) {
2516
+ extraflags |= GZ_EXTRAFLAG_SLOW;
2517
+ }
2518
+
2519
+ buf[0] = GZ_MAGIC1;
2520
+ buf[1] = GZ_MAGIC2;
2521
+ buf[2] = GZ_METHOD_DEFLATE;
2522
+ buf[3] = flags;
2523
+ gzfile_set32((unsigned long)gz->mtime, &buf[4]);
2524
+ buf[8] = extraflags;
2525
+ buf[9] = gz->os_code;
2526
+ zstream_append_buffer(&gz->z, buf, sizeof(buf));
2527
+
2528
+ if (!NIL_P(gz->orig_name)) {
2529
+ zstream_append_buffer2(&gz->z, gz->orig_name);
2530
+ zstream_append_buffer(&gz->z, (Bytef*)"\0", 1);
2531
+ }
2532
+ if (!NIL_P(gz->comment)) {
2533
+ zstream_append_buffer2(&gz->z, gz->comment);
2534
+ zstream_append_buffer(&gz->z, (Bytef*)"\0", 1);
2535
+ }
2536
+
2537
+ gz->z.flags |= GZFILE_FLAG_HEADER_FINISHED;
2538
+ }
2539
+
2540
+ static void
2541
+ gzfile_make_footer(struct gzfile *gz)
2542
+ {
2543
+ Bytef buf[8]; /* 8 is the size of gzip footer */
2544
+
2545
+ gzfile_set32(gz->crc, buf);
2546
+ gzfile_set32(gz->z.stream.total_in, &buf[4]);
2547
+ zstream_append_buffer(&gz->z, buf, sizeof(buf));
2548
+ gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED;
2549
+ }
2550
+
2551
+ static void
2552
+ gzfile_read_header(struct gzfile *gz)
2553
+ {
2554
+ const unsigned char *head;
2555
+ long len;
2556
+ char flags, *p;
2557
+
2558
+ if (!gzfile_read_raw_ensure(gz, 10)) { /* 10 is the size of gzip header */
2559
+ gzfile_raise(gz, cGzError, "not in gzip format");
2560
+ }
2561
+
2562
+ head = (unsigned char*)RSTRING_PTR(gz->z.input);
2563
+
2564
+ if (head[0] != GZ_MAGIC1 || head[1] != GZ_MAGIC2) {
2565
+ gzfile_raise(gz, cGzError, "not in gzip format");
2566
+ }
2567
+ if (head[2] != GZ_METHOD_DEFLATE) {
2568
+ rb_raise(cGzError, "unsupported compression method %d", head[2]);
2569
+ }
2570
+
2571
+ flags = head[3];
2572
+ if (flags & GZ_FLAG_MULTIPART) {
2573
+ rb_raise(cGzError, "multi-part gzip file is not supported");
2574
+ }
2575
+ else if (flags & GZ_FLAG_ENCRYPT) {
2576
+ rb_raise(cGzError, "encrypted gzip file is not supported");
2577
+ }
2578
+ else if (flags & GZ_FLAG_UNKNOWN_MASK) {
2579
+ rb_raise(cGzError, "unknown flags 0x%02x", flags);
2580
+ }
2581
+
2582
+ if (head[8] & GZ_EXTRAFLAG_FAST) {
2583
+ gz->level = Z_BEST_SPEED;
2584
+ }
2585
+ else if (head[8] & GZ_EXTRAFLAG_SLOW) {
2586
+ gz->level = Z_BEST_COMPRESSION;
2587
+ }
2588
+ else {
2589
+ gz->level = Z_DEFAULT_COMPRESSION;
2590
+ }
2591
+
2592
+ gz->mtime = gzfile_get32(&head[4]);
2593
+ gz->os_code = head[9];
2594
+ zstream_discard_input(&gz->z, 10);
2595
+
2596
+ if (flags & GZ_FLAG_EXTRA) {
2597
+ if (!gzfile_read_raw_ensure(gz, 2)) {
2598
+ rb_raise(cGzError, "unexpected end of file");
2599
+ }
2600
+ len = gzfile_get16((Bytef*)RSTRING_PTR(gz->z.input));
2601
+ if (!gzfile_read_raw_ensure(gz, 2 + len)) {
2602
+ rb_raise(cGzError, "unexpected end of file");
2603
+ }
2604
+ zstream_discard_input(&gz->z, 2 + len);
2605
+ }
2606
+ if (flags & GZ_FLAG_ORIG_NAME) {
2607
+ if (!gzfile_read_raw_ensure(gz, 1)) {
2608
+ rb_raise(cGzError, "unexpected end of file");
2609
+ }
2610
+ p = gzfile_read_raw_until_zero(gz, 0);
2611
+ len = p - RSTRING_PTR(gz->z.input);
2612
+ gz->orig_name = rb_str_new(RSTRING_PTR(gz->z.input), len);
2613
+ OBJ_TAINT(gz->orig_name); /* for safe */
2614
+ zstream_discard_input(&gz->z, len + 1);
2615
+ }
2616
+ if (flags & GZ_FLAG_COMMENT) {
2617
+ if (!gzfile_read_raw_ensure(gz, 1)) {
2618
+ rb_raise(cGzError, "unexpected end of file");
2619
+ }
2620
+ p = gzfile_read_raw_until_zero(gz, 0);
2621
+ len = p - RSTRING_PTR(gz->z.input);
2622
+ gz->comment = rb_str_new(RSTRING_PTR(gz->z.input), len);
2623
+ OBJ_TAINT(gz->comment); /* for safe */
2624
+ zstream_discard_input(&gz->z, len + 1);
2625
+ }
2626
+
2627
+ if (gz->z.input != Qnil && RSTRING_LEN(gz->z.input) > 0) {
2628
+ zstream_run(&gz->z, 0, 0, Z_SYNC_FLUSH);
2629
+ }
2630
+ }
2631
+
2632
+ static void
2633
+ gzfile_check_footer(struct gzfile *gz)
2634
+ {
2635
+ unsigned long crc, length;
2636
+
2637
+ gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED;
2638
+
2639
+ if (!gzfile_read_raw_ensure(gz, 8)) { /* 8 is the size of gzip footer */
2640
+ gzfile_raise(gz, cNoFooter, "footer is not found");
2641
+ }
2642
+
2643
+ crc = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input));
2644
+ length = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input) + 4);
2645
+
2646
+ gz->z.stream.total_in += 8; /* to rewind correctly */
2647
+ zstream_discard_input(&gz->z, 8);
2648
+
2649
+ if (gz->crc != crc) {
2650
+ rb_raise(cCRCError, "invalid compressed data -- crc error");
2651
+ }
2652
+ if ((uint32_t)gz->z.stream.total_out != length) {
2653
+ rb_raise(cLengthError, "invalid compressed data -- length error");
2654
+ }
2655
+ }
2656
+
2657
+ static void
2658
+ gzfile_write(struct gzfile *gz, Bytef *str, long len)
2659
+ {
2660
+ if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2661
+ gzfile_make_header(gz);
2662
+ }
2663
+
2664
+ if (len > 0 || (gz->z.flags & GZFILE_FLAG_SYNC)) {
2665
+ gz->crc = checksum_long(crc32, gz->crc, str, len);
2666
+ zstream_run(&gz->z, str, len, (gz->z.flags & GZFILE_FLAG_SYNC)
2667
+ ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2668
+ }
2669
+ gzfile_write_raw(gz);
2670
+ }
2671
+
2672
+ static long
2673
+ gzfile_read_more(struct gzfile *gz)
2674
+ {
2675
+ VALUE str;
2676
+
2677
+ while (!ZSTREAM_IS_FINISHED(&gz->z)) {
2678
+ str = gzfile_read_raw(gz);
2679
+ if (NIL_P(str)) {
2680
+ if (!ZSTREAM_IS_FINISHED(&gz->z)) {
2681
+ rb_raise(cGzError, "unexpected end of file");
2682
+ }
2683
+ break;
2684
+ }
2685
+ if (RSTRING_LEN(str) > 0) { /* prevent Z_BUF_ERROR */
2686
+ zstream_run(&gz->z, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str),
2687
+ Z_SYNC_FLUSH);
2688
+ RB_GC_GUARD(str);
2689
+ }
2690
+ if (gz->z.buf_filled > 0) break;
2691
+ }
2692
+ return gz->z.buf_filled;
2693
+ }
2694
+
2695
+ static void
2696
+ gzfile_calc_crc(struct gzfile *gz, VALUE str)
2697
+ {
2698
+ if (RSTRING_LEN(str) <= gz->ungetc) {
2699
+ gz->ungetc -= RSTRING_LEN(str);
2700
+ }
2701
+ else {
2702
+ gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc,
2703
+ RSTRING_LEN(str) - gz->ungetc);
2704
+ gz->ungetc = 0;
2705
+ }
2706
+ }
2707
+
2708
+ static VALUE
2709
+ gzfile_newstr(struct gzfile *gz, VALUE str)
2710
+ {
2711
+ if (!gz->enc2) {
2712
+ rb_enc_associate(str, gz->enc);
2713
+ OBJ_TAINT(str); /* for safe */
2714
+ return str;
2715
+ }
2716
+ if (gz->ec && rb_enc_dummy_p(gz->enc2)) {
2717
+ str = rb_econv_str_convert(gz->ec, str, ECONV_PARTIAL_INPUT);
2718
+ rb_enc_associate(str, gz->enc);
2719
+ OBJ_TAINT(str);
2720
+ return str;
2721
+ }
2722
+ return rb_str_conv_enc_opts(str, gz->enc2, gz->enc,
2723
+ gz->ecflags, gz->ecopts);
2724
+ }
2725
+
2726
+ static long
2727
+ gzfile_fill(struct gzfile *gz, long len)
2728
+ {
2729
+ if (len < 0)
2730
+ rb_raise(rb_eArgError, "negative length %ld given", len);
2731
+ if (len == 0)
2732
+ return 0;
2733
+ while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) {
2734
+ gzfile_read_more(gz);
2735
+ }
2736
+ if (GZFILE_IS_FINISHED(gz)) {
2737
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2738
+ gzfile_check_footer(gz);
2739
+ }
2740
+ return -1;
2741
+ }
2742
+ return len < gz->z.buf_filled ? len : gz->z.buf_filled;
2743
+ }
2744
+
2745
+ static VALUE
2746
+ gzfile_read(struct gzfile *gz, long len)
2747
+ {
2748
+ VALUE dst;
2749
+
2750
+ len = gzfile_fill(gz, len);
2751
+ if (len == 0) return rb_str_new(0, 0);
2752
+ if (len < 0) return Qnil;
2753
+ dst = zstream_shift_buffer(&gz->z, len);
2754
+ if (!NIL_P(dst)) gzfile_calc_crc(gz, dst);
2755
+ return dst;
2756
+ }
2757
+
2758
+ static VALUE
2759
+ gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
2760
+ {
2761
+ VALUE dst;
2762
+
2763
+ if (len < 0)
2764
+ rb_raise(rb_eArgError, "negative length %ld given", len);
2765
+
2766
+ if (!NIL_P(outbuf))
2767
+ OBJ_TAINT(outbuf);
2768
+
2769
+ if (len == 0) {
2770
+ if (NIL_P(outbuf))
2771
+ return rb_str_new(0, 0);
2772
+ else {
2773
+ rb_str_resize(outbuf, 0);
2774
+ return outbuf;
2775
+ }
2776
+ }
2777
+ while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled == 0) {
2778
+ gzfile_read_more(gz);
2779
+ }
2780
+ if (GZFILE_IS_FINISHED(gz)) {
2781
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2782
+ gzfile_check_footer(gz);
2783
+ }
2784
+ if (!NIL_P(outbuf))
2785
+ rb_str_resize(outbuf, 0);
2786
+ rb_raise(rb_eEOFError, "end of file reached");
2787
+ }
2788
+
2789
+ dst = zstream_shift_buffer(&gz->z, len);
2790
+ gzfile_calc_crc(gz, dst);
2791
+
2792
+ if (!NIL_P(outbuf)) {
2793
+ rb_str_resize(outbuf, RSTRING_LEN(dst));
2794
+ memcpy(RSTRING_PTR(outbuf), RSTRING_PTR(dst), RSTRING_LEN(dst));
2795
+ RB_GC_GUARD(dst);
2796
+ dst = outbuf;
2797
+ }
2798
+ OBJ_TAINT(dst); /* for safe */
2799
+ return dst;
2800
+ }
2801
+
2802
+ static VALUE
2803
+ gzfile_read_all(struct gzfile *gz)
2804
+ {
2805
+ VALUE dst;
2806
+
2807
+ while (!ZSTREAM_IS_FINISHED(&gz->z)) {
2808
+ gzfile_read_more(gz);
2809
+ }
2810
+ if (GZFILE_IS_FINISHED(gz)) {
2811
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2812
+ gzfile_check_footer(gz);
2813
+ }
2814
+ return rb_str_new(0, 0);
2815
+ }
2816
+
2817
+ dst = zstream_detach_buffer(&gz->z);
2818
+ if (NIL_P(dst)) return dst;
2819
+ gzfile_calc_crc(gz, dst);
2820
+ OBJ_TAINT(dst);
2821
+ return gzfile_newstr(gz, dst);
2822
+ }
2823
+
2824
+ static VALUE
2825
+ gzfile_getc(struct gzfile *gz)
2826
+ {
2827
+ VALUE buf, dst = 0;
2828
+ int len;
2829
+
2830
+ len = rb_enc_mbmaxlen(gz->enc);
2831
+ while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) {
2832
+ gzfile_read_more(gz);
2833
+ }
2834
+ if (GZFILE_IS_FINISHED(gz)) {
2835
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2836
+ gzfile_check_footer(gz);
2837
+ }
2838
+ return Qnil;
2839
+ }
2840
+
2841
+ if (gz->ec && rb_enc_dummy_p(gz->enc2)) {
2842
+ const unsigned char *ss, *sp, *se;
2843
+ unsigned char *ds, *dp, *de;
2844
+
2845
+ if (!gz->cbuf) {
2846
+ gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA);
2847
+ }
2848
+ ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf);
2849
+ se = sp + gz->z.buf_filled;
2850
+ ds = dp = (unsigned char *)gz->cbuf;
2851
+ de = (unsigned char *)ds + GZFILE_CBUF_CAPA;
2852
+ (void)rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT);
2853
+ rb_econv_check_error(gz->ec);
2854
+ dst = zstream_shift_buffer(&gz->z, sp - ss);
2855
+ gzfile_calc_crc(gz, dst);
2856
+ dst = rb_str_new(gz->cbuf, dp - ds);
2857
+ rb_enc_associate(dst, gz->enc);
2858
+ OBJ_TAINT(dst);
2859
+ return dst;
2860
+ }
2861
+ else {
2862
+ buf = gz->z.buf;
2863
+ len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), gz->enc);
2864
+ dst = gzfile_read(gz, len);
2865
+ if (NIL_P(dst)) return dst;
2866
+ return gzfile_newstr(gz, dst);
2867
+ }
2868
+ }
2869
+
2870
+ static void
2871
+ gzfile_ungets(struct gzfile *gz, const Bytef *b, long len)
2872
+ {
2873
+ zstream_buffer_ungets(&gz->z, b, len);
2874
+ gz->ungetc+=len;
2875
+ }
2876
+
2877
+ static void
2878
+ gzfile_ungetbyte(struct gzfile *gz, int c)
2879
+ {
2880
+ zstream_buffer_ungetbyte(&gz->z, c);
2881
+ gz->ungetc++;
2882
+ }
2883
+
2884
+ static VALUE
2885
+ gzfile_writer_end_run(VALUE arg)
2886
+ {
2887
+ struct gzfile *gz = (struct gzfile *)arg;
2888
+
2889
+ if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2890
+ gzfile_make_header(gz);
2891
+ }
2892
+
2893
+ zstream_run(&gz->z, (Bytef*)"", 0, Z_FINISH);
2894
+ gzfile_make_footer(gz);
2895
+ gzfile_write_raw(gz);
2896
+
2897
+ return Qnil;
2898
+ }
2899
+
2900
+ static void
2901
+ gzfile_writer_end(struct gzfile *gz)
2902
+ {
2903
+ if (ZSTREAM_IS_CLOSING(&gz->z)) return;
2904
+ gz->z.flags |= ZSTREAM_FLAG_CLOSING;
2905
+
2906
+ rb_ensure(gzfile_writer_end_run, (VALUE)gz, zstream_end, (VALUE)&gz->z);
2907
+ }
2908
+
2909
+ static VALUE
2910
+ gzfile_reader_end_run(VALUE arg)
2911
+ {
2912
+ struct gzfile *gz = (struct gzfile *)arg;
2913
+
2914
+ if (GZFILE_IS_FINISHED(gz)
2915
+ && !(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2916
+ gzfile_check_footer(gz);
2917
+ }
2918
+
2919
+ return Qnil;
2920
+ }
2921
+
2922
+ static void
2923
+ gzfile_reader_end(struct gzfile *gz)
2924
+ {
2925
+ if (ZSTREAM_IS_CLOSING(&gz->z)) return;
2926
+ gz->z.flags |= ZSTREAM_FLAG_CLOSING;
2927
+
2928
+ rb_ensure(gzfile_reader_end_run, (VALUE)gz, zstream_end, (VALUE)&gz->z);
2929
+ }
2930
+
2931
+ static void
2932
+ gzfile_reader_rewind(struct gzfile *gz)
2933
+ {
2934
+ long n;
2935
+
2936
+ n = gz->z.stream.total_in;
2937
+ if (!NIL_P(gz->z.input)) {
2938
+ n += RSTRING_LEN(gz->z.input);
2939
+ }
2940
+
2941
+ rb_funcall(gz->io, id_seek, 2, rb_int2inum(-n), INT2FIX(1));
2942
+ gzfile_reset(gz);
2943
+ }
2944
+
2945
+ static VALUE
2946
+ gzfile_reader_get_unused(struct gzfile *gz)
2947
+ {
2948
+ VALUE str;
2949
+
2950
+ if (!ZSTREAM_IS_READY(&gz->z)) return Qnil;
2951
+ if (!GZFILE_IS_FINISHED(gz)) return Qnil;
2952
+ if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2953
+ gzfile_check_footer(gz);
2954
+ }
2955
+ if (NIL_P(gz->z.input)) return Qnil;
2956
+
2957
+ str = rb_str_resurrect(gz->z.input);
2958
+ OBJ_TAINT(str); /* for safe */
2959
+ return str;
2960
+ }
2961
+
2962
+ static struct gzfile *
2963
+ get_gzfile(VALUE obj)
2964
+ {
2965
+ struct gzfile *gz;
2966
+
2967
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
2968
+ if (!ZSTREAM_IS_READY(&gz->z)) {
2969
+ rb_raise(cGzError, "closed gzip stream");
2970
+ }
2971
+ return gz;
2972
+ }
2973
+
2974
+
2975
+ /* ------------------------------------------------------------------------- */
2976
+
2977
+ /*
2978
+ * Document-class: Zlib::GzipFile
2979
+ *
2980
+ * Zlib::GzipFile is an abstract class for handling a gzip formatted
2981
+ * compressed file. The operations are defined in the subclasses,
2982
+ * Zlib::GzipReader for reading, and Zlib::GzipWriter for writing.
2983
+ *
2984
+ * GzipReader should be used by associating an IO, or IO-like, object.
2985
+ *
2986
+ * == Method Catalogue
2987
+ *
2988
+ * - ::wrap
2989
+ * - ::open (Zlib::GzipReader::open and Zlib::GzipWriter::open)
2990
+ * - #close
2991
+ * - #closed?
2992
+ * - #comment
2993
+ * - comment= (Zlib::GzipWriter#comment=)
2994
+ * - #crc
2995
+ * - eof? (Zlib::GzipReader#eof?)
2996
+ * - #finish
2997
+ * - #level
2998
+ * - lineno (Zlib::GzipReader#lineno)
2999
+ * - lineno= (Zlib::GzipReader#lineno=)
3000
+ * - #mtime
3001
+ * - mtime= (Zlib::GzipWriter#mtime=)
3002
+ * - #orig_name
3003
+ * - orig_name (Zlib::GzipWriter#orig_name=)
3004
+ * - #os_code
3005
+ * - path (when the underlying IO supports #path)
3006
+ * - #sync
3007
+ * - #sync=
3008
+ * - #to_io
3009
+ *
3010
+ * (due to internal structure, documentation may appear under Zlib::GzipReader
3011
+ * or Zlib::GzipWriter)
3012
+ */
3013
+
3014
+
3015
+ typedef struct {
3016
+ int argc;
3017
+ VALUE *argv;
3018
+ VALUE klass;
3019
+ } new_wrap_arg_t;
3020
+
3021
+ static VALUE
3022
+ new_wrap(VALUE tmp)
3023
+ {
3024
+ new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp;
3025
+ return rb_class_new_instance(arg->argc, arg->argv, arg->klass);
3026
+ }
3027
+
3028
+ static VALUE
3029
+ gzfile_ensure_close(VALUE obj)
3030
+ {
3031
+ struct gzfile *gz;
3032
+
3033
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3034
+ if (ZSTREAM_IS_READY(&gz->z)) {
3035
+ gzfile_close(gz, 1);
3036
+ }
3037
+ return Qnil;
3038
+ }
3039
+
3040
+ static VALUE
3041
+ gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error)
3042
+ {
3043
+ VALUE obj;
3044
+
3045
+ if (close_io_on_error) {
3046
+ int state = 0;
3047
+ new_wrap_arg_t arg;
3048
+ arg.argc = argc;
3049
+ arg.argv = argv;
3050
+ arg.klass = klass;
3051
+ obj = rb_protect(new_wrap, (VALUE)&arg, &state);
3052
+ if (state) {
3053
+ rb_io_close(argv[0]);
3054
+ rb_jump_tag(state);
3055
+ }
3056
+ }
3057
+ else {
3058
+ obj = rb_class_new_instance(argc, argv, klass);
3059
+ }
3060
+
3061
+ if (rb_block_given_p()) {
3062
+ return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj);
3063
+ }
3064
+ else {
3065
+ return obj;
3066
+ }
3067
+ }
3068
+
3069
+ /*
3070
+ * Document-method: Zlib::GzipFile.wrap
3071
+ *
3072
+ * call-seq:
3073
+ * Zlib::GzipReader.wrap(io, ...) { |gz| ... }
3074
+ * Zlib::GzipWriter.wrap(io, ...) { |gz| ... }
3075
+ *
3076
+ * Creates a GzipReader or GzipWriter associated with +io+, passing in any
3077
+ * necessary extra options, and executes the block with the newly created
3078
+ * object just like File.open.
3079
+ *
3080
+ * The GzipFile object will be closed automatically after executing the block.
3081
+ * If you want to keep the associated IO object open, you may call
3082
+ * Zlib::GzipFile#finish method in the block.
3083
+ */
3084
+ static VALUE
3085
+ rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass)
3086
+ {
3087
+ return gzfile_wrap(argc, argv, klass, 0);
3088
+ }
3089
+
3090
+ /*
3091
+ * Document-method: Zlib::GzipFile.open
3092
+ *
3093
+ * See Zlib::GzipReader#open and Zlib::GzipWriter#open.
3094
+ */
3095
+ static VALUE
3096
+ gzfile_s_open(int argc, VALUE *argv, VALUE klass, const char *mode)
3097
+ {
3098
+ VALUE io, filename;
3099
+
3100
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
3101
+ filename = argv[0];
3102
+ io = rb_file_open_str(filename, mode);
3103
+ argv[0] = io;
3104
+ return gzfile_wrap(argc, argv, klass, 1);
3105
+ }
3106
+
3107
+ /*
3108
+ * Document-method: Zlib::GzipFile#to_io
3109
+ *
3110
+ * Same as IO.
3111
+ */
3112
+ static VALUE
3113
+ rb_gzfile_to_io(VALUE obj)
3114
+ {
3115
+ return get_gzfile(obj)->io;
3116
+ }
3117
+
3118
+ /*
3119
+ * Document-method: Zlib::GzipFile#crc
3120
+ *
3121
+ * Returns CRC value of the uncompressed data.
3122
+ */
3123
+ static VALUE
3124
+ rb_gzfile_crc(VALUE obj)
3125
+ {
3126
+ return rb_uint2inum(get_gzfile(obj)->crc);
3127
+ }
3128
+
3129
+ /*
3130
+ * Document-method: Zlib::GzipFile#mtime
3131
+ *
3132
+ * Returns last modification time recorded in the gzip file header.
3133
+ */
3134
+ static VALUE
3135
+ rb_gzfile_mtime(VALUE obj)
3136
+ {
3137
+ return rb_time_new(get_gzfile(obj)->mtime, (time_t)0);
3138
+ }
3139
+
3140
+ /*
3141
+ * Document-method: Zlib::GzipFile#level
3142
+ *
3143
+ * Returns compression level.
3144
+ */
3145
+ static VALUE
3146
+ rb_gzfile_level(VALUE obj)
3147
+ {
3148
+ return INT2FIX(get_gzfile(obj)->level);
3149
+ }
3150
+
3151
+ /*
3152
+ * Document-method: Zlib::GzipFile#os_code
3153
+ *
3154
+ * Returns OS code number recorded in the gzip file header.
3155
+ */
3156
+ static VALUE
3157
+ rb_gzfile_os_code(VALUE obj)
3158
+ {
3159
+ return INT2FIX(get_gzfile(obj)->os_code);
3160
+ }
3161
+
3162
+ /*
3163
+ * Document-method: Zlib::GzipFile#orig_name
3164
+ *
3165
+ * Returns original filename recorded in the gzip file header, or +nil+ if
3166
+ * original filename is not present.
3167
+ */
3168
+ static VALUE
3169
+ rb_gzfile_orig_name(VALUE obj)
3170
+ {
3171
+ VALUE str = get_gzfile(obj)->orig_name;
3172
+ if (!NIL_P(str)) {
3173
+ str = rb_str_dup(str);
3174
+ }
3175
+ OBJ_TAINT(str); /* for safe */
3176
+ return str;
3177
+ }
3178
+
3179
+ /*
3180
+ * Document-method: Zlib::GzipFile#comment
3181
+ *
3182
+ * Returns comments recorded in the gzip file header, or nil if the comments
3183
+ * is not present.
3184
+ */
3185
+ static VALUE
3186
+ rb_gzfile_comment(VALUE obj)
3187
+ {
3188
+ VALUE str = get_gzfile(obj)->comment;
3189
+ if (!NIL_P(str)) {
3190
+ str = rb_str_dup(str);
3191
+ }
3192
+ OBJ_TAINT(str); /* for safe */
3193
+ return str;
3194
+ }
3195
+
3196
+ /*
3197
+ * Document-method: Zlib::GzipFile#lineno
3198
+ *
3199
+ * The line number of the last row read from this file.
3200
+ */
3201
+ static VALUE
3202
+ rb_gzfile_lineno(VALUE obj)
3203
+ {
3204
+ return INT2NUM(get_gzfile(obj)->lineno);
3205
+ }
3206
+
3207
+ /*
3208
+ * Document-method: Zlib::GzipReader#lineno=
3209
+ *
3210
+ * Specify line number of the last row read from this file.
3211
+ */
3212
+ static VALUE
3213
+ rb_gzfile_set_lineno(VALUE obj, VALUE lineno)
3214
+ {
3215
+ struct gzfile *gz = get_gzfile(obj);
3216
+ gz->lineno = NUM2INT(lineno);
3217
+ return lineno;
3218
+ }
3219
+
3220
+ /*
3221
+ * Document-method: Zlib::GzipWriter#mtime=
3222
+ *
3223
+ * Specify the modification time (+mtime+) in the gzip header.
3224
+ * Using a Fixnum or Integer
3225
+ */
3226
+ static VALUE
3227
+ rb_gzfile_set_mtime(VALUE obj, VALUE mtime)
3228
+ {
3229
+ struct gzfile *gz = get_gzfile(obj);
3230
+ VALUE val;
3231
+
3232
+ if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) {
3233
+ rb_raise(cGzError, "header is already written");
3234
+ }
3235
+
3236
+ val = rb_Integer(mtime);
3237
+ gz->mtime = NUM2UINT(val);
3238
+
3239
+ return mtime;
3240
+ }
3241
+
3242
+ /*
3243
+ * Document-method: Zlib::GzipFile#orig_name=
3244
+ *
3245
+ * Specify the original name (+str+) in the gzip header.
3246
+ */
3247
+ static VALUE
3248
+ rb_gzfile_set_orig_name(VALUE obj, VALUE str)
3249
+ {
3250
+ struct gzfile *gz = get_gzfile(obj);
3251
+ VALUE s;
3252
+ char *p;
3253
+
3254
+ if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) {
3255
+ rb_raise(cGzError, "header is already written");
3256
+ }
3257
+ s = rb_str_dup(rb_str_to_str(str));
3258
+ p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s));
3259
+ if (p) {
3260
+ rb_str_resize(s, p - RSTRING_PTR(s));
3261
+ }
3262
+ gz->orig_name = s;
3263
+ return str;
3264
+ }
3265
+
3266
+ /*
3267
+ * Document-method: Zlib::GzipFile#comment=
3268
+ *
3269
+ * Specify the comment (+str+) in the gzip header.
3270
+ */
3271
+ static VALUE
3272
+ rb_gzfile_set_comment(VALUE obj, VALUE str)
3273
+ {
3274
+ struct gzfile *gz = get_gzfile(obj);
3275
+ VALUE s;
3276
+ char *p;
3277
+
3278
+ if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) {
3279
+ rb_raise(cGzError, "header is already written");
3280
+ }
3281
+ s = rb_str_dup(rb_str_to_str(str));
3282
+ p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s));
3283
+ if (p) {
3284
+ rb_str_resize(s, p - RSTRING_PTR(s));
3285
+ }
3286
+ gz->comment = s;
3287
+ return str;
3288
+ }
3289
+
3290
+ /*
3291
+ * Document-method: Zlib::GzipFile#close
3292
+ *
3293
+ * Closes the GzipFile object. This method calls close method of the
3294
+ * associated IO object. Returns the associated IO object.
3295
+ */
3296
+ static VALUE
3297
+ rb_gzfile_close(VALUE obj)
3298
+ {
3299
+ struct gzfile *gz;
3300
+ VALUE io;
3301
+
3302
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3303
+ if (!ZSTREAM_IS_READY(&gz->z)) {
3304
+ return Qnil;
3305
+ }
3306
+ io = gz->io;
3307
+ gzfile_close(gz, 1);
3308
+ return io;
3309
+ }
3310
+
3311
+ /*
3312
+ * Document-method: Zlib::GzipFile#finish
3313
+ *
3314
+ * Closes the GzipFile object. Unlike Zlib::GzipFile#close, this method never
3315
+ * calls the close method of the associated IO object. Returns the associated IO
3316
+ * object.
3317
+ */
3318
+ static VALUE
3319
+ rb_gzfile_finish(VALUE obj)
3320
+ {
3321
+ struct gzfile *gz = get_gzfile(obj);
3322
+ VALUE io;
3323
+
3324
+ io = gz->io;
3325
+ gzfile_close(gz, 0);
3326
+ return io;
3327
+ }
3328
+
3329
+ /*
3330
+ * Document-method: Zlib::GzipFile#closed?
3331
+ *
3332
+ * Same as IO#closed?
3333
+ *
3334
+ */
3335
+ static VALUE
3336
+ rb_gzfile_closed_p(VALUE obj)
3337
+ {
3338
+ struct gzfile *gz;
3339
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3340
+ return NIL_P(gz->io) ? Qtrue : Qfalse;
3341
+ }
3342
+
3343
+ /*
3344
+ * Document-method: Zlib::GzipFile#eof?
3345
+ *
3346
+ * Returns +true+ or +false+ whether the stream has reached the end.
3347
+ */
3348
+ static VALUE
3349
+ rb_gzfile_eof_p(VALUE obj)
3350
+ {
3351
+ struct gzfile *gz = get_gzfile(obj);
3352
+ return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse;
3353
+ }
3354
+
3355
+ /*
3356
+ * Document-method: Zlib::GzipFile#sync
3357
+ *
3358
+ * Same as IO#sync
3359
+ *
3360
+ */
3361
+ static VALUE
3362
+ rb_gzfile_sync(VALUE obj)
3363
+ {
3364
+ return (get_gzfile(obj)->z.flags & GZFILE_FLAG_SYNC) ? Qtrue : Qfalse;
3365
+ }
3366
+
3367
+ /*
3368
+ * Document-method: Zlib::GzipFile#sync=
3369
+ *
3370
+ * call-seq: sync = flag
3371
+ *
3372
+ * Same as IO. If flag is +true+, the associated IO object must respond to the
3373
+ * +flush+ method. While +sync+ mode is +true+, the compression ratio
3374
+ * decreases sharply.
3375
+ */
3376
+ static VALUE
3377
+ rb_gzfile_set_sync(VALUE obj, VALUE mode)
3378
+ {
3379
+ struct gzfile *gz = get_gzfile(obj);
3380
+
3381
+ if (RTEST(mode)) {
3382
+ gz->z.flags |= GZFILE_FLAG_SYNC;
3383
+ }
3384
+ else {
3385
+ gz->z.flags &= ~GZFILE_FLAG_SYNC;
3386
+ }
3387
+ return mode;
3388
+ }
3389
+
3390
+ /*
3391
+ * Document-method: Zlib::GzipFile#total_in
3392
+ *
3393
+ * Total number of input bytes read so far.
3394
+ */
3395
+ static VALUE
3396
+ rb_gzfile_total_in(VALUE obj)
3397
+ {
3398
+ return rb_uint2inum(get_gzfile(obj)->z.stream.total_in);
3399
+ }
3400
+
3401
+ /*
3402
+ * Document-method: Zlib::GzipFile#total_out
3403
+ *
3404
+ * Total number of output bytes output so far.
3405
+ */
3406
+ static VALUE
3407
+ rb_gzfile_total_out(VALUE obj)
3408
+ {
3409
+ struct gzfile *gz = get_gzfile(obj);
3410
+ uLong total_out = gz->z.stream.total_out;
3411
+ long buf_filled = gz->z.buf_filled;
3412
+
3413
+ if (total_out >= (uLong)buf_filled) {
3414
+ return rb_uint2inum(total_out - buf_filled);
3415
+ } else {
3416
+ return LONG2FIX(-(buf_filled - (long)total_out));
3417
+ }
3418
+ }
3419
+
3420
+ /*
3421
+ * Document-method: Zlib::GzipFile#path
3422
+ *
3423
+ * call-seq: path
3424
+ *
3425
+ * Returns the path string of the associated IO-like object. This
3426
+ * method is only defined when the IO-like object responds to #path().
3427
+ */
3428
+ static VALUE
3429
+ rb_gzfile_path(VALUE obj)
3430
+ {
3431
+ struct gzfile *gz;
3432
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3433
+ return gz->path;
3434
+ }
3435
+
3436
+ static void
3437
+ rb_gzfile_ecopts(struct gzfile *gz, VALUE opts)
3438
+ {
3439
+ if (!NIL_P(opts)) {
3440
+ rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2, NULL);
3441
+ }
3442
+ if (gz->enc2) {
3443
+ gz->ecflags = rb_econv_prepare_opts(opts, &opts);
3444
+ gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name,
3445
+ gz->ecflags, opts);
3446
+ gz->ecopts = opts;
3447
+ }
3448
+ }
3449
+
3450
+ /* ------------------------------------------------------------------------- */
3451
+
3452
+ /*
3453
+ * Document-class: Zlib::GzipWriter
3454
+ *
3455
+ * Zlib::GzipWriter is a class for writing gzipped files. GzipWriter should
3456
+ * be used with an instance of IO, or IO-like, object.
3457
+ *
3458
+ * Following two example generate the same result.
3459
+ *
3460
+ * Zlib::GzipWriter.open('hoge.gz') do |gz|
3461
+ * gz.write 'jugemu jugemu gokou no surikire...'
3462
+ * end
3463
+ *
3464
+ * File.open('hoge.gz', 'w') do |f|
3465
+ * gz = Zlib::GzipWriter.new(f)
3466
+ * gz.write 'jugemu jugemu gokou no surikire...'
3467
+ * gz.close
3468
+ * end
3469
+ *
3470
+ * To make like gzip(1) does, run following:
3471
+ *
3472
+ * orig = 'hoge.txt'
3473
+ * Zlib::GzipWriter.open('hoge.gz') do |gz|
3474
+ * gz.mtime = File.mtime(orig)
3475
+ * gz.orig_name = orig
3476
+ * gz.write IO.binread(orig)
3477
+ * end
3478
+ *
3479
+ * NOTE: Due to the limitation of Ruby's finalizer, you must explicitly close
3480
+ * GzipWriter objects by Zlib::GzipWriter#close etc. Otherwise, GzipWriter
3481
+ * will be not able to write the gzip footer and will generate a broken gzip
3482
+ * file.
3483
+ */
3484
+
3485
+ static VALUE
3486
+ rb_gzwriter_s_allocate(VALUE klass)
3487
+ {
3488
+ return gzfile_writer_new(klass);
3489
+ }
3490
+
3491
+ /*
3492
+ * call-seq: Zlib::GzipWriter.open(filename, level=nil, strategy=nil) { |gz| ... }
3493
+ *
3494
+ * Opens a file specified by +filename+ for writing gzip compressed data, and
3495
+ * returns a GzipWriter object associated with that file. Further details of
3496
+ * this method are found in Zlib::GzipWriter.new and Zlib::GzipFile.wrap.
3497
+ */
3498
+ static VALUE
3499
+ rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass)
3500
+ {
3501
+ return gzfile_s_open(argc, argv, klass, "wb");
3502
+ }
3503
+
3504
+ /*
3505
+ * call-seq:
3506
+ * Zlib::GzipWriter.new(io, level = nil, strategy = nil, options = {})
3507
+ *
3508
+ * Creates a GzipWriter object associated with +io+. +level+ and +strategy+
3509
+ * should be the same as the arguments of Zlib::Deflate.new. The GzipWriter
3510
+ * object writes gzipped data to +io+. +io+ must respond to the
3511
+ * +write+ method that behaves the same as IO#write.
3512
+ *
3513
+ * The +options+ hash may be used to set the encoding of the data.
3514
+ * +:external_encoding+, +:internal_encoding+ and +:encoding+ may be set as in
3515
+ * IO::new.
3516
+ */
3517
+ static VALUE
3518
+ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj)
3519
+ {
3520
+ struct gzfile *gz;
3521
+ VALUE io, level, strategy, opt = Qnil;
3522
+ int err;
3523
+
3524
+ if (argc > 1) {
3525
+ opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
3526
+ if (!NIL_P(opt)) argc--;
3527
+ }
3528
+
3529
+ rb_scan_args(argc, argv, "12", &io, &level, &strategy);
3530
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3531
+
3532
+ /* this is undocumented feature of zlib */
3533
+ gz->level = ARG_LEVEL(level);
3534
+ err = deflateInit2(&gz->z.stream, gz->level, Z_DEFLATED,
3535
+ -MAX_WBITS, DEF_MEM_LEVEL, ARG_STRATEGY(strategy));
3536
+ if (err != Z_OK) {
3537
+ raise_zlib_error(err, gz->z.stream.msg);
3538
+ }
3539
+ gz->io = io;
3540
+ ZSTREAM_READY(&gz->z);
3541
+ rb_gzfile_ecopts(gz, opt);
3542
+
3543
+ if (rb_respond_to(io, id_path)) {
3544
+ gz->path = rb_funcall(gz->io, id_path, 0);
3545
+ rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
3546
+ }
3547
+
3548
+ return obj;
3549
+ }
3550
+
3551
+ /*
3552
+ * call-seq: flush(flush=nil)
3553
+ *
3554
+ * Flushes all the internal buffers of the GzipWriter object. The meaning of
3555
+ * +flush+ is same as in Zlib::Deflate#deflate. <tt>Zlib::SYNC_FLUSH</tt> is used if
3556
+ * +flush+ is omitted. It is no use giving flush <tt>Zlib::NO_FLUSH</tt>.
3557
+ */
3558
+ static VALUE
3559
+ rb_gzwriter_flush(int argc, VALUE *argv, VALUE obj)
3560
+ {
3561
+ struct gzfile *gz = get_gzfile(obj);
3562
+ VALUE v_flush;
3563
+ int flush;
3564
+
3565
+ rb_scan_args(argc, argv, "01", &v_flush);
3566
+
3567
+ flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3568
+ if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */
3569
+ zstream_run(&gz->z, (Bytef*)"", 0, flush);
3570
+ }
3571
+
3572
+ gzfile_write_raw(gz);
3573
+ if (rb_respond_to(gz->io, id_flush)) {
3574
+ rb_funcall(gz->io, id_flush, 0);
3575
+ }
3576
+ return obj;
3577
+ }
3578
+
3579
+ /*
3580
+ * Same as IO.
3581
+ */
3582
+ static VALUE
3583
+ rb_gzwriter_write(VALUE obj, VALUE str)
3584
+ {
3585
+ struct gzfile *gz = get_gzfile(obj);
3586
+
3587
+ if (!RB_TYPE_P(str, T_STRING))
3588
+ str = rb_obj_as_string(str);
3589
+ if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3590
+ str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2);
3591
+ }
3592
+ gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
3593
+ RB_GC_GUARD(str);
3594
+ return INT2FIX(RSTRING_LEN(str));
3595
+ }
3596
+
3597
+ /*
3598
+ * Same as IO.
3599
+ */
3600
+ static VALUE
3601
+ rb_gzwriter_putc(VALUE obj, VALUE ch)
3602
+ {
3603
+ struct gzfile *gz = get_gzfile(obj);
3604
+ char c = NUM2CHR(ch);
3605
+
3606
+ gzfile_write(gz, (Bytef*)&c, 1);
3607
+ return ch;
3608
+ }
3609
+
3610
+
3611
+
3612
+ /*
3613
+ * Document-method: <<
3614
+ * Same as IO.
3615
+ */
3616
+ #define rb_gzwriter_addstr rb_io_addstr
3617
+ /*
3618
+ * Document-method: printf
3619
+ * Same as IO.
3620
+ */
3621
+ #define rb_gzwriter_printf rb_io_printf
3622
+ /*
3623
+ * Document-method: print
3624
+ * Same as IO.
3625
+ */
3626
+ #define rb_gzwriter_print rb_io_print
3627
+ /*
3628
+ * Document-method: puts
3629
+ * Same as IO.
3630
+ */
3631
+ #define rb_gzwriter_puts rb_io_puts
3632
+
3633
+
3634
+ /* ------------------------------------------------------------------------- */
3635
+
3636
+ /*
3637
+ * Document-class: Zlib::GzipReader
3638
+ *
3639
+ * Zlib::GzipReader is the class for reading a gzipped file. GzipReader should
3640
+ * be used as an IO, or -IO-like, object.
3641
+ *
3642
+ * Zlib::GzipReader.open('hoge.gz') {|gz|
3643
+ * print gz.read
3644
+ * }
3645
+ *
3646
+ * File.open('hoge.gz') do |f|
3647
+ * gz = Zlib::GzipReader.new(f)
3648
+ * print gz.read
3649
+ * gz.close
3650
+ * end
3651
+ *
3652
+ * == Method Catalogue
3653
+ *
3654
+ * The following methods in Zlib::GzipReader are just like their counterparts
3655
+ * in IO, but they raise Zlib::Error or Zlib::GzipFile::Error exception if an
3656
+ * error was found in the gzip file.
3657
+ * - #each
3658
+ * - #each_line
3659
+ * - #each_byte
3660
+ * - #gets
3661
+ * - #getc
3662
+ * - #lineno
3663
+ * - #lineno=
3664
+ * - #read
3665
+ * - #readchar
3666
+ * - #readline
3667
+ * - #readlines
3668
+ * - #ungetc
3669
+ *
3670
+ * Be careful of the footer of the gzip file. A gzip file has the checksum of
3671
+ * pre-compressed data in its footer. GzipReader checks all uncompressed data
3672
+ * against that checksum at the following cases, and if it fails, raises
3673
+ * <tt>Zlib::GzipFile::NoFooter</tt>, <tt>Zlib::GzipFile::CRCError</tt>, or
3674
+ * <tt>Zlib::GzipFile::LengthError</tt> exception.
3675
+ *
3676
+ * - When an reading request is received beyond the end of file (the end of
3677
+ * compressed data). That is, when Zlib::GzipReader#read,
3678
+ * Zlib::GzipReader#gets, or some other methods for reading returns nil.
3679
+ * - When Zlib::GzipFile#close method is called after the object reaches the
3680
+ * end of file.
3681
+ * - When Zlib::GzipReader#unused method is called after the object reaches
3682
+ * the end of file.
3683
+ *
3684
+ * The rest of the methods are adequately described in their own
3685
+ * documentation.
3686
+ */
3687
+
3688
+ static VALUE
3689
+ rb_gzreader_s_allocate(VALUE klass)
3690
+ {
3691
+ return gzfile_reader_new(klass);
3692
+ }
3693
+
3694
+ /*
3695
+ * Document-method: Zlib::GzipReader.open
3696
+ *
3697
+ * call-seq: Zlib::GzipReader.open(filename) {|gz| ... }
3698
+ *
3699
+ * Opens a file specified by +filename+ as a gzipped file, and returns a
3700
+ * GzipReader object associated with that file. Further details of this method
3701
+ * are in Zlib::GzipReader.new and ZLib::GzipFile.wrap.
3702
+ */
3703
+ static VALUE
3704
+ rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass)
3705
+ {
3706
+ return gzfile_s_open(argc, argv, klass, "rb");
3707
+ }
3708
+
3709
+ /*
3710
+ * Document-method: Zlib::GzipReader.new
3711
+ *
3712
+ * call-seq:
3713
+ * Zlib::GzipReader.new(io, options = {})
3714
+ *
3715
+ * Creates a GzipReader object associated with +io+. The GzipReader object reads
3716
+ * gzipped data from +io+, and parses/decompresses it. The +io+ must
3717
+ * have a +read+ method that behaves same as the IO#read.
3718
+ *
3719
+ * The +options+ hash may be used to set the encoding of the data.
3720
+ * +:external_encoding+, +:internal_encoding+ and +:encoding+ may be set as in
3721
+ * IO::new.
3722
+ *
3723
+ * If the gzip file header is incorrect, raises an Zlib::GzipFile::Error
3724
+ * exception.
3725
+ */
3726
+ static VALUE
3727
+ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj)
3728
+ {
3729
+ VALUE io, opt = Qnil;
3730
+ struct gzfile *gz;
3731
+ int err;
3732
+
3733
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3734
+ rb_scan_args(argc, argv, "1:", &io, &opt);
3735
+
3736
+ /* this is undocumented feature of zlib */
3737
+ err = inflateInit2(&gz->z.stream, -MAX_WBITS);
3738
+ if (err != Z_OK) {
3739
+ raise_zlib_error(err, gz->z.stream.msg);
3740
+ }
3741
+ gz->io = io;
3742
+ ZSTREAM_READY(&gz->z);
3743
+ gzfile_read_header(gz);
3744
+ rb_gzfile_ecopts(gz, opt);
3745
+
3746
+ if (rb_respond_to(io, id_path)) {
3747
+ gz->path = rb_funcall(gz->io, id_path, 0);
3748
+ rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
3749
+ }
3750
+
3751
+ return obj;
3752
+ }
3753
+
3754
+ /*
3755
+ * Document-method: Zlib::GzipReader#rewind
3756
+ *
3757
+ * Resets the position of the file pointer to the point created the GzipReader
3758
+ * object. The associated IO object needs to respond to the +seek+ method.
3759
+ */
3760
+ static VALUE
3761
+ rb_gzreader_rewind(VALUE obj)
3762
+ {
3763
+ struct gzfile *gz = get_gzfile(obj);
3764
+ gzfile_reader_rewind(gz);
3765
+ return INT2FIX(0);
3766
+ }
3767
+
3768
+ /*
3769
+ * Document-method: Zlib::GzipReader#unused
3770
+ *
3771
+ * Returns the rest of the data which had read for parsing gzip format, or
3772
+ * +nil+ if the whole gzip file is not parsed yet.
3773
+ */
3774
+ static VALUE
3775
+ rb_gzreader_unused(VALUE obj)
3776
+ {
3777
+ struct gzfile *gz;
3778
+ TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz);
3779
+ return gzfile_reader_get_unused(gz);
3780
+ }
3781
+
3782
+ /*
3783
+ * Document-method: Zlib::GzipReader#read
3784
+ *
3785
+ * See Zlib::GzipReader documentation for a description.
3786
+ */
3787
+ static VALUE
3788
+ rb_gzreader_read(int argc, VALUE *argv, VALUE obj)
3789
+ {
3790
+ struct gzfile *gz = get_gzfile(obj);
3791
+ VALUE vlen;
3792
+ long len;
3793
+
3794
+ rb_scan_args(argc, argv, "01", &vlen);
3795
+ if (NIL_P(vlen)) {
3796
+ return gzfile_read_all(gz);
3797
+ }
3798
+
3799
+ len = NUM2INT(vlen);
3800
+ if (len < 0) {
3801
+ rb_raise(rb_eArgError, "negative length %ld given", len);
3802
+ }
3803
+ return gzfile_read(gz, len);
3804
+ }
3805
+
3806
+ /*
3807
+ * Document-method: Zlib::GzipReader#readpartial
3808
+ *
3809
+ * call-seq:
3810
+ * gzipreader.readpartial(maxlen [, outbuf]) => string, outbuf
3811
+ *
3812
+ * Reads at most <i>maxlen</i> bytes from the gziped stream but
3813
+ * it blocks only if <em>gzipreader</em> has no data immediately available.
3814
+ * If the optional <i>outbuf</i> argument is present,
3815
+ * it must reference a String, which will receive the data.
3816
+ * It raises <code>EOFError</code> on end of file.
3817
+ */
3818
+ static VALUE
3819
+ rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj)
3820
+ {
3821
+ struct gzfile *gz = get_gzfile(obj);
3822
+ VALUE vlen, outbuf;
3823
+ long len;
3824
+
3825
+ rb_scan_args(argc, argv, "11", &vlen, &outbuf);
3826
+
3827
+ len = NUM2INT(vlen);
3828
+ if (len < 0) {
3829
+ rb_raise(rb_eArgError, "negative length %ld given", len);
3830
+ }
3831
+ if (!NIL_P(outbuf))
3832
+ Check_Type(outbuf, T_STRING);
3833
+ return gzfile_readpartial(gz, len, outbuf);
3834
+ }
3835
+
3836
+ /*
3837
+ * Document-method: Zlib::GzipReader#getc
3838
+ *
3839
+ * See Zlib::GzipReader documentation for a description.
3840
+ */
3841
+ static VALUE
3842
+ rb_gzreader_getc(VALUE obj)
3843
+ {
3844
+ struct gzfile *gz = get_gzfile(obj);
3845
+
3846
+ return gzfile_getc(gz);
3847
+ }
3848
+
3849
+ /*
3850
+ * Document-method: Zlib::GzipReader#readchar
3851
+ *
3852
+ * See Zlib::GzipReader documentation for a description.
3853
+ */
3854
+ static VALUE
3855
+ rb_gzreader_readchar(VALUE obj)
3856
+ {
3857
+ VALUE dst;
3858
+ dst = rb_gzreader_getc(obj);
3859
+ if (NIL_P(dst)) {
3860
+ rb_raise(rb_eEOFError, "end of file reached");
3861
+ }
3862
+ return dst;
3863
+ }
3864
+
3865
+ /*
3866
+ * Document-method: Zlib::GzipReader#getbyte
3867
+ *
3868
+ * See Zlib::GzipReader documentation for a description.
3869
+ */
3870
+ static VALUE
3871
+ rb_gzreader_getbyte(VALUE obj)
3872
+ {
3873
+ struct gzfile *gz = get_gzfile(obj);
3874
+ VALUE dst;
3875
+
3876
+ dst = gzfile_read(gz, 1);
3877
+ if (!NIL_P(dst)) {
3878
+ dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff);
3879
+ }
3880
+ return dst;
3881
+ }
3882
+
3883
+ /*
3884
+ * Document-method: Zlib::GzipReader#readbyte
3885
+ *
3886
+ * See Zlib::GzipReader documentation for a description.
3887
+ */
3888
+ static VALUE
3889
+ rb_gzreader_readbyte(VALUE obj)
3890
+ {
3891
+ VALUE dst;
3892
+ dst = rb_gzreader_getbyte(obj);
3893
+ if (NIL_P(dst)) {
3894
+ rb_raise(rb_eEOFError, "end of file reached");
3895
+ }
3896
+ return dst;
3897
+ }
3898
+
3899
+ /*
3900
+ * Document-method: Zlib::GzipReader#each_char
3901
+ *
3902
+ * See Zlib::GzipReader documentation for a description.
3903
+ */
3904
+ static VALUE
3905
+ rb_gzreader_each_char(VALUE obj)
3906
+ {
3907
+ VALUE c;
3908
+
3909
+ RETURN_ENUMERATOR(obj, 0, 0);
3910
+
3911
+ while (!NIL_P(c = rb_gzreader_getc(obj))) {
3912
+ rb_yield(c);
3913
+ }
3914
+ return Qnil;
3915
+ }
3916
+
3917
+ /*
3918
+ * Document-method: Zlib::GzipReader#each_byte
3919
+ *
3920
+ * See Zlib::GzipReader documentation for a description.
3921
+ */
3922
+ static VALUE
3923
+ rb_gzreader_each_byte(VALUE obj)
3924
+ {
3925
+ VALUE c;
3926
+
3927
+ RETURN_ENUMERATOR(obj, 0, 0);
3928
+
3929
+ while (!NIL_P(c = rb_gzreader_getbyte(obj))) {
3930
+ rb_yield(c);
3931
+ }
3932
+ return Qnil;
3933
+ }
3934
+
3935
+ /*
3936
+ * Document-method: Zlib::GzipReader#bytes
3937
+ *
3938
+ * This is a deprecated alias for <code>each_byte</code>.
3939
+ */
3940
+ static VALUE
3941
+ rb_gzreader_bytes(VALUE obj)
3942
+ {
3943
+ rb_warn("Zlib::GzipReader#bytes is deprecated; use #each_byte instead");
3944
+ if (!rb_block_given_p())
3945
+ return rb_enumeratorize(obj, ID2SYM(rb_intern("each_byte")), 0, 0);
3946
+ return rb_gzreader_each_byte(obj);
3947
+ }
3948
+
3949
+ /*
3950
+ * Document-method: Zlib::GzipReader#ungetc
3951
+ *
3952
+ * See Zlib::GzipReader documentation for a description.
3953
+ */
3954
+ static VALUE
3955
+ rb_gzreader_ungetc(VALUE obj, VALUE s)
3956
+ {
3957
+ struct gzfile *gz;
3958
+
3959
+ if (FIXNUM_P(s))
3960
+ return rb_gzreader_ungetbyte(obj, s);
3961
+ gz = get_gzfile(obj);
3962
+ StringValue(s);
3963
+ if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3964
+ s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2);
3965
+ }
3966
+ gzfile_ungets(gz, (const Bytef*)RSTRING_PTR(s), RSTRING_LEN(s));
3967
+ RB_GC_GUARD(s);
3968
+ return Qnil;
3969
+ }
3970
+
3971
+ /*
3972
+ * Document-method: Zlib::GzipReader#ungetbyte
3973
+ *
3974
+ * See Zlib::GzipReader documentation for a description.
3975
+ */
3976
+ static VALUE
3977
+ rb_gzreader_ungetbyte(VALUE obj, VALUE ch)
3978
+ {
3979
+ struct gzfile *gz = get_gzfile(obj);
3980
+ gzfile_ungetbyte(gz, NUM2CHR(ch));
3981
+ return Qnil;
3982
+ }
3983
+
3984
+ static void
3985
+ gzreader_skip_linebreaks(struct gzfile *gz)
3986
+ {
3987
+ VALUE str;
3988
+ char *p;
3989
+ int n;
3990
+
3991
+ while (gz->z.buf_filled == 0) {
3992
+ if (GZFILE_IS_FINISHED(gz)) return;
3993
+ gzfile_read_more(gz);
3994
+ }
3995
+ n = 0;
3996
+ p = RSTRING_PTR(gz->z.buf);
3997
+
3998
+ while (n++, *(p++) == '\n') {
3999
+ if (n >= gz->z.buf_filled) {
4000
+ str = zstream_detach_buffer(&gz->z);
4001
+ gzfile_calc_crc(gz, str);
4002
+ while (gz->z.buf_filled == 0) {
4003
+ if (GZFILE_IS_FINISHED(gz)) return;
4004
+ gzfile_read_more(gz);
4005
+ }
4006
+ n = 0;
4007
+ p = RSTRING_PTR(gz->z.buf);
4008
+ }
4009
+ }
4010
+
4011
+ str = zstream_shift_buffer(&gz->z, n - 1);
4012
+ gzfile_calc_crc(gz, str);
4013
+ }
4014
+
4015
+ static void
4016
+ rscheck(const char *rsptr, long rslen, VALUE rs)
4017
+ {
4018
+ if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
4019
+ rb_raise(rb_eRuntimeError, "rs modified");
4020
+ }
4021
+
4022
+ static long
4023
+ gzreader_charboundary(struct gzfile *gz, long n)
4024
+ {
4025
+ char *s = RSTRING_PTR(gz->z.buf);
4026
+ char *e = s + gz->z.buf_filled;
4027
+ char *p = rb_enc_left_char_head(s, s + n, e, gz->enc);
4028
+ long l = p - s;
4029
+ if (l < n) {
4030
+ n = rb_enc_precise_mbclen(p, e, gz->enc);
4031
+ if (MBCLEN_NEEDMORE_P(n)) {
4032
+ if ((l = gzfile_fill(gz, l + MBCLEN_NEEDMORE_LEN(n))) > 0) {
4033
+ return l;
4034
+ }
4035
+ }
4036
+ else if (MBCLEN_CHARFOUND_P(n)) {
4037
+ return l + MBCLEN_CHARFOUND_LEN(n);
4038
+ }
4039
+ }
4040
+ return n;
4041
+ }
4042
+
4043
+ static VALUE
4044
+ gzreader_gets(int argc, VALUE *argv, VALUE obj)
4045
+ {
4046
+ struct gzfile *gz = get_gzfile(obj);
4047
+ VALUE rs;
4048
+ VALUE dst;
4049
+ const char *rsptr;
4050
+ char *p, *res;
4051
+ long rslen, n, limit = -1;
4052
+ int rspara;
4053
+ rb_encoding *enc = gz->enc;
4054
+ int maxlen = rb_enc_mbmaxlen(enc);
4055
+
4056
+ if (argc == 0) {
4057
+ rs = rb_rs;
4058
+ }
4059
+ else {
4060
+ VALUE lim, tmp;
4061
+
4062
+ rb_scan_args(argc, argv, "11", &rs, &lim);
4063
+ if (!NIL_P(lim)) {
4064
+ if (!NIL_P(rs)) StringValue(rs);
4065
+ }
4066
+ else if (!NIL_P(rs)) {
4067
+ tmp = rb_check_string_type(rs);
4068
+ if (NIL_P(tmp)) {
4069
+ lim = rs;
4070
+ rs = rb_rs;
4071
+ }
4072
+ else {
4073
+ rs = tmp;
4074
+ }
4075
+ }
4076
+ if (!NIL_P(lim)) {
4077
+ limit = NUM2LONG(lim);
4078
+ if (limit == 0) return rb_str_new(0,0);
4079
+ }
4080
+ }
4081
+
4082
+ if (NIL_P(rs)) {
4083
+ if (limit < 0) {
4084
+ dst = gzfile_read_all(gz);
4085
+ if (RSTRING_LEN(dst) == 0) return Qnil;
4086
+ }
4087
+ else if ((n = gzfile_fill(gz, limit)) <= 0) {
4088
+ return Qnil;
4089
+ }
4090
+ else {
4091
+ if (maxlen > 1 && n >= limit && !GZFILE_IS_FINISHED(gz)) {
4092
+ n = gzreader_charboundary(gz, n);
4093
+ }
4094
+ else {
4095
+ n = limit;
4096
+ }
4097
+ dst = zstream_shift_buffer(&gz->z, n);
4098
+ if (NIL_P(dst)) return dst;
4099
+ gzfile_calc_crc(gz, dst);
4100
+ dst = gzfile_newstr(gz, dst);
4101
+ }
4102
+ gz->lineno++;
4103
+ return dst;
4104
+ }
4105
+
4106
+ if (RSTRING_LEN(rs) == 0) {
4107
+ rsptr = "\n\n";
4108
+ rslen = 2;
4109
+ rspara = 1;
4110
+ }
4111
+ else {
4112
+ rsptr = RSTRING_PTR(rs);
4113
+ rslen = RSTRING_LEN(rs);
4114
+ rspara = 0;
4115
+ }
4116
+
4117
+ if (rspara) {
4118
+ gzreader_skip_linebreaks(gz);
4119
+ }
4120
+
4121
+ while (gz->z.buf_filled < rslen) {
4122
+ if (ZSTREAM_IS_FINISHED(&gz->z)) {
4123
+ if (gz->z.buf_filled > 0) gz->lineno++;
4124
+ return gzfile_read(gz, rslen);
4125
+ }
4126
+ gzfile_read_more(gz);
4127
+ }
4128
+
4129
+ p = RSTRING_PTR(gz->z.buf);
4130
+ n = rslen;
4131
+ for (;;) {
4132
+ long filled;
4133
+ if (n > gz->z.buf_filled) {
4134
+ if (ZSTREAM_IS_FINISHED(&gz->z)) break;
4135
+ gzfile_read_more(gz);
4136
+ p = RSTRING_PTR(gz->z.buf) + n - rslen;
4137
+ }
4138
+ if (!rspara) rscheck(rsptr, rslen, rs);
4139
+ filled = gz->z.buf_filled;
4140
+ if (limit > 0 && filled >= limit) {
4141
+ filled = limit;
4142
+ }
4143
+ res = memchr(p, rsptr[0], (filled - n + 1));
4144
+ if (!res) {
4145
+ n = filled;
4146
+ if (limit > 0 && filled >= limit) break;
4147
+ n++;
4148
+ }
4149
+ else {
4150
+ n += (long)(res - p);
4151
+ p = res;
4152
+ if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break;
4153
+ p++, n++;
4154
+ }
4155
+ }
4156
+ if (maxlen > 1 && n == limit && (gz->z.buf_filled > n || !ZSTREAM_IS_FINISHED(&gz->z))) {
4157
+ n = gzreader_charboundary(gz, n);
4158
+ }
4159
+
4160
+ gz->lineno++;
4161
+ dst = gzfile_read(gz, n);
4162
+ if (NIL_P(dst)) return dst;
4163
+ if (rspara) {
4164
+ gzreader_skip_linebreaks(gz);
4165
+ }
4166
+ RB_GC_GUARD(rs);
4167
+
4168
+ return gzfile_newstr(gz, dst);
4169
+ }
4170
+
4171
+ /*
4172
+ * Document-method: Zlib::GzipReader#gets
4173
+ *
4174
+ * See Zlib::GzipReader documentation for a description.
4175
+ */
4176
+ static VALUE
4177
+ rb_gzreader_gets(int argc, VALUE *argv, VALUE obj)
4178
+ {
4179
+ VALUE dst;
4180
+ dst = gzreader_gets(argc, argv, obj);
4181
+ if (!NIL_P(dst)) {
4182
+ rb_lastline_set(dst);
4183
+ }
4184
+ return dst;
4185
+ }
4186
+
4187
+ /*
4188
+ * Document-method: Zlib::GzipReader#readline
4189
+ *
4190
+ * See Zlib::GzipReader documentation for a description.
4191
+ */
4192
+ static VALUE
4193
+ rb_gzreader_readline(int argc, VALUE *argv, VALUE obj)
4194
+ {
4195
+ VALUE dst;
4196
+ dst = rb_gzreader_gets(argc, argv, obj);
4197
+ if (NIL_P(dst)) {
4198
+ rb_raise(rb_eEOFError, "end of file reached");
4199
+ }
4200
+ return dst;
4201
+ }
4202
+
4203
+ /*
4204
+ * Document-method: Zlib::GzipReader#each
4205
+ *
4206
+ * See Zlib::GzipReader documentation for a description.
4207
+ */
4208
+ static VALUE
4209
+ rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
4210
+ {
4211
+ VALUE str;
4212
+
4213
+ RETURN_ENUMERATOR(obj, 0, 0);
4214
+
4215
+ while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
4216
+ rb_yield(str);
4217
+ }
4218
+ return obj;
4219
+ }
4220
+
4221
+ /*
4222
+ * Document-method: Zlib::GzipReader#lines
4223
+ *
4224
+ * This is a deprecated alias for <code>each_line</code>.
4225
+ */
4226
+ static VALUE
4227
+ rb_gzreader_lines(int argc, VALUE *argv, VALUE obj)
4228
+ {
4229
+ rb_warn("Zlib::GzipReader#lines is deprecated; use #each_line instead");
4230
+ if (!rb_block_given_p())
4231
+ return rb_enumeratorize(obj, ID2SYM(rb_intern("each_line")), argc, argv);
4232
+ return rb_gzreader_each(argc, argv, obj);
4233
+ }
4234
+
4235
+ /*
4236
+ * Document-method: Zlib::GzipReader#readlines
4237
+ *
4238
+ * See Zlib::GzipReader documentation for a description.
4239
+ */
4240
+ static VALUE
4241
+ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj)
4242
+ {
4243
+ VALUE str, dst;
4244
+ dst = rb_ary_new();
4245
+ while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
4246
+ rb_ary_push(dst, str);
4247
+ }
4248
+ return dst;
4249
+ }
4250
+
4251
+ /*
4252
+ * Document-method: Zlib::GzipReader#external_encoding
4253
+ *
4254
+ * See Zlib::GzipReader documentation for a description.
4255
+ */
4256
+ static VALUE
4257
+ rb_gzreader_external_encoding(VALUE self)
4258
+ {
4259
+ return rb_enc_from_encoding(get_gzfile(self)->enc);
4260
+ }
4261
+
4262
+ #endif /* GZIP_SUPPORT */
4263
+
4264
+ void
4265
+ Init_zlib(void)
4266
+ {
4267
+ VALUE mZlib, cZStream, cDeflate, cInflate;
4268
+ #if GZIP_SUPPORT
4269
+ VALUE cGzipFile, cGzipWriter, cGzipReader;
4270
+ #endif
4271
+
4272
+ mZlib = rb_define_module("Zlib");
4273
+
4274
+ id_dictionaries = rb_intern("@dictionaries");
4275
+
4276
+ cZError = rb_define_class_under(mZlib, "Error", rb_eStandardError);
4277
+ cStreamEnd = rb_define_class_under(mZlib, "StreamEnd", cZError);
4278
+ cNeedDict = rb_define_class_under(mZlib, "NeedDict", cZError);
4279
+ cDataError = rb_define_class_under(mZlib, "DataError", cZError);
4280
+ cStreamError = rb_define_class_under(mZlib, "StreamError", cZError);
4281
+ cMemError = rb_define_class_under(mZlib, "MemError", cZError);
4282
+ cBufError = rb_define_class_under(mZlib, "BufError", cZError);
4283
+ cVersionError = rb_define_class_under(mZlib, "VersionError", cZError);
4284
+
4285
+ rb_define_module_function(mZlib, "zlib_version", rb_zlib_version, 0);
4286
+ rb_define_module_function(mZlib, "adler32", rb_zlib_adler32, -1);
4287
+ rb_define_module_function(mZlib, "adler32_combine", rb_zlib_adler32_combine, 3);
4288
+ rb_define_module_function(mZlib, "crc32", rb_zlib_crc32, -1);
4289
+ rb_define_module_function(mZlib, "crc32_combine", rb_zlib_crc32_combine, 3);
4290
+ rb_define_module_function(mZlib, "crc_table", rb_zlib_crc_table, 0);
4291
+
4292
+ /* The Ruby/zlib version string. */
4293
+ rb_define_const(mZlib, "VERSION", rb_str_new2(RUBY_ZLIB_VERSION));
4294
+ /* The string which represents the version of zlib.h */
4295
+ rb_define_const(mZlib, "ZLIB_VERSION", rb_str_new2(ZLIB_VERSION));
4296
+
4297
+ cZStream = rb_define_class_under(mZlib, "ZStream", rb_cObject);
4298
+ rb_undef_alloc_func(cZStream);
4299
+ rb_define_method(cZStream, "avail_out", rb_zstream_avail_out, 0);
4300
+ rb_define_method(cZStream, "avail_out=", rb_zstream_set_avail_out, 1);
4301
+ rb_define_method(cZStream, "avail_in", rb_zstream_avail_in, 0);
4302
+ rb_define_method(cZStream, "total_in", rb_zstream_total_in, 0);
4303
+ rb_define_method(cZStream, "total_out", rb_zstream_total_out, 0);
4304
+ rb_define_method(cZStream, "data_type", rb_zstream_data_type, 0);
4305
+ rb_define_method(cZStream, "adler", rb_zstream_adler, 0);
4306
+ rb_define_method(cZStream, "finished?", rb_zstream_finished_p, 0);
4307
+ rb_define_method(cZStream, "stream_end?", rb_zstream_finished_p, 0);
4308
+ rb_define_method(cZStream, "closed?", rb_zstream_closed_p, 0);
4309
+ rb_define_method(cZStream, "ended?", rb_zstream_closed_p, 0);
4310
+ rb_define_method(cZStream, "close", rb_zstream_end, 0);
4311
+ rb_define_method(cZStream, "end", rb_zstream_end, 0);
4312
+ rb_define_method(cZStream, "reset", rb_zstream_reset, 0);
4313
+ rb_define_method(cZStream, "finish", rb_zstream_finish, 0);
4314
+ rb_define_method(cZStream, "flush_next_in", rb_zstream_flush_next_in, 0);
4315
+ rb_define_method(cZStream, "flush_next_out", rb_zstream_flush_next_out, 0);
4316
+
4317
+ /* Represents binary data as guessed by deflate.
4318
+ *
4319
+ * See Zlib::Deflate#data_type. */
4320
+ rb_define_const(mZlib, "BINARY", INT2FIX(Z_BINARY));
4321
+
4322
+ /* Represents text data as guessed by deflate.
4323
+ *
4324
+ * NOTE: The underlying constant Z_ASCII was deprecated in favor of Z_TEXT
4325
+ * in zlib 1.2.2. New applications should not use this constant.
4326
+ *
4327
+ * See Zlib::Deflate#data_type. */
4328
+ rb_define_const(mZlib, "ASCII", INT2FIX(Z_ASCII));
4329
+
4330
+ #ifdef Z_TEXT
4331
+ /* Represents text data as guessed by deflate.
4332
+ *
4333
+ * See Zlib::Deflate#data_type. */
4334
+ rb_define_const(mZlib, "TEXT", INT2FIX(Z_TEXT));
4335
+ #endif
4336
+
4337
+ /* Represents an unknown data type as guessed by deflate.
4338
+ *
4339
+ * See Zlib::Deflate#data_type. */
4340
+ rb_define_const(mZlib, "UNKNOWN", INT2FIX(Z_UNKNOWN));
4341
+
4342
+ cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream);
4343
+ rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1);
4344
+ rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1);
4345
+ rb_define_alloc_func(cDeflate, rb_deflate_s_allocate);
4346
+ rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1);
4347
+ rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1);
4348
+ rb_define_method(cDeflate, "deflate", rb_deflate_deflate, -1);
4349
+ rb_define_method(cDeflate, "<<", rb_deflate_addstr, 1);
4350
+ rb_define_method(cDeflate, "flush", rb_deflate_flush, -1);
4351
+ rb_define_method(cDeflate, "params", rb_deflate_params, 2);
4352
+ rb_define_method(cDeflate, "set_dictionary", rb_deflate_set_dictionary, 1);
4353
+
4354
+ cInflate = rb_define_class_under(mZlib, "Inflate", cZStream);
4355
+ rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1);
4356
+ rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1);
4357
+ rb_define_alloc_func(cInflate, rb_inflate_s_allocate);
4358
+ rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1);
4359
+ rb_define_method(cInflate, "add_dictionary", rb_inflate_add_dictionary, 1);
4360
+ rb_define_method(cInflate, "inflate", rb_inflate_inflate, 1);
4361
+ rb_define_method(cInflate, "<<", rb_inflate_addstr, 1);
4362
+ rb_define_method(cInflate, "sync", rb_inflate_sync, 1);
4363
+ rb_define_method(cInflate, "sync_point?", rb_inflate_sync_point_p, 0);
4364
+ rb_define_method(cInflate, "set_dictionary", rb_inflate_set_dictionary, 1);
4365
+
4366
+ /* No compression, passes through data untouched. Use this for appending
4367
+ * pre-compressed data to a deflate stream.
4368
+ */
4369
+ rb_define_const(mZlib, "NO_COMPRESSION", INT2FIX(Z_NO_COMPRESSION));
4370
+ /* Fastest compression level, but with the lowest space savings. */
4371
+ rb_define_const(mZlib, "BEST_SPEED", INT2FIX(Z_BEST_SPEED));
4372
+ /* Slowest compression level, but with the best space savings. */
4373
+ rb_define_const(mZlib, "BEST_COMPRESSION", INT2FIX(Z_BEST_COMPRESSION));
4374
+ /* Default compression level which is a good trade-off between space and
4375
+ * time
4376
+ */
4377
+ rb_define_const(mZlib, "DEFAULT_COMPRESSION",
4378
+ INT2FIX(Z_DEFAULT_COMPRESSION));
4379
+
4380
+ /* Deflate strategy for data produced by a filter (or predictor). The
4381
+ * effect of FILTERED is to force more Huffman codes and less string
4382
+ * matching; it is somewhat intermediate between DEFAULT_STRATEGY and
4383
+ * HUFFMAN_ONLY. Filtered data consists mostly of small values with a
4384
+ * somewhat random distribution.
4385
+ */
4386
+ rb_define_const(mZlib, "FILTERED", INT2FIX(Z_FILTERED));
4387
+
4388
+ /* Deflate strategy which uses Huffman codes only (no string matching). */
4389
+ rb_define_const(mZlib, "HUFFMAN_ONLY", INT2FIX(Z_HUFFMAN_ONLY));
4390
+
4391
+ #ifdef Z_RLE
4392
+ /* Deflate compression strategy designed to be almost as fast as
4393
+ * HUFFMAN_ONLY, but give better compression for PNG image data.
4394
+ */
4395
+ rb_define_const(mZlib, "RLE", INT2FIX(Z_RLE));
4396
+ #endif
4397
+
4398
+ #ifdef Z_FIXED
4399
+ /* Deflate strategy which prevents the use of dynamic Huffman codes,
4400
+ * allowing for a simpler decoder for specialized applications.
4401
+ */
4402
+ rb_define_const(mZlib, "FIXED", INT2FIX(Z_FIXED));
4403
+ #endif
4404
+
4405
+ /* Default deflate strategy which is used for normal data. */
4406
+ rb_define_const(mZlib, "DEFAULT_STRATEGY", INT2FIX(Z_DEFAULT_STRATEGY));
4407
+
4408
+ /* The maximum size of the zlib history buffer. Note that zlib allows
4409
+ * larger values to enable different inflate modes. See Zlib::Inflate.new
4410
+ * for details.
4411
+ */
4412
+ rb_define_const(mZlib, "MAX_WBITS", INT2FIX(MAX_WBITS));
4413
+
4414
+ /* The default memory level for allocating zlib deflate compression state.
4415
+ */
4416
+ rb_define_const(mZlib, "DEF_MEM_LEVEL", INT2FIX(DEF_MEM_LEVEL));
4417
+
4418
+ /* The maximum memory level for allocating zlib deflate compression state.
4419
+ */
4420
+ rb_define_const(mZlib, "MAX_MEM_LEVEL", INT2FIX(MAX_MEM_LEVEL));
4421
+
4422
+ /* NO_FLUSH is the default flush method and allows deflate to decide how
4423
+ * much data to accumulate before producing output in order to maximize
4424
+ * compression.
4425
+ */
4426
+ rb_define_const(mZlib, "NO_FLUSH", INT2FIX(Z_NO_FLUSH));
4427
+
4428
+ /* The SYNC_FLUSH method flushes all pending output to the output buffer
4429
+ * and the output is aligned on a byte boundary. Flushing may degrade
4430
+ * compression so it should be used only when necessary, such as at a
4431
+ * request or response boundary for a network stream.
4432
+ */
4433
+ rb_define_const(mZlib, "SYNC_FLUSH", INT2FIX(Z_SYNC_FLUSH));
4434
+
4435
+ /* Flushes all output as with SYNC_FLUSH, and the compression state is
4436
+ * reset so that decompression can restart from this point if previous
4437
+ * compressed data has been damaged or if random access is desired. Like
4438
+ * SYNC_FLUSH, using FULL_FLUSH too often can seriously degrade
4439
+ * compression.
4440
+ */
4441
+ rb_define_const(mZlib, "FULL_FLUSH", INT2FIX(Z_FULL_FLUSH));
4442
+
4443
+ /* Processes all pending input and flushes pending output. */
4444
+ rb_define_const(mZlib, "FINISH", INT2FIX(Z_FINISH));
4445
+
4446
+ #if GZIP_SUPPORT
4447
+ id_write = rb_intern("write");
4448
+ id_read = rb_intern("read");
4449
+ id_readpartial = rb_intern("readpartial");
4450
+ id_flush = rb_intern("flush");
4451
+ id_seek = rb_intern("seek");
4452
+ id_close = rb_intern("close");
4453
+ id_path = rb_intern("path");
4454
+ id_input = rb_intern("@input");
4455
+
4456
+ cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject);
4457
+ cGzError = rb_define_class_under(cGzipFile, "Error", cZError);
4458
+
4459
+ /* input gzipped string */
4460
+ rb_define_attr(cGzError, "input", 1, 0);
4461
+ rb_define_method(cGzError, "inspect", gzfile_error_inspect, 0);
4462
+
4463
+ cNoFooter = rb_define_class_under(cGzipFile, "NoFooter", cGzError);
4464
+ cCRCError = rb_define_class_under(cGzipFile, "CRCError", cGzError);
4465
+ cLengthError = rb_define_class_under(cGzipFile,"LengthError",cGzError);
4466
+
4467
+ cGzipWriter = rb_define_class_under(mZlib, "GzipWriter", cGzipFile);
4468
+ cGzipReader = rb_define_class_under(mZlib, "GzipReader", cGzipFile);
4469
+ rb_include_module(cGzipReader, rb_mEnumerable);
4470
+
4471
+ rb_define_singleton_method(cGzipFile, "wrap", rb_gzfile_s_wrap, -1);
4472
+ rb_undef_alloc_func(cGzipFile);
4473
+ rb_define_method(cGzipFile, "to_io", rb_gzfile_to_io, 0);
4474
+ rb_define_method(cGzipFile, "crc", rb_gzfile_crc, 0);
4475
+ rb_define_method(cGzipFile, "mtime", rb_gzfile_mtime, 0);
4476
+ rb_define_method(cGzipFile, "level", rb_gzfile_level, 0);
4477
+ rb_define_method(cGzipFile, "os_code", rb_gzfile_os_code, 0);
4478
+ rb_define_method(cGzipFile, "orig_name", rb_gzfile_orig_name, 0);
4479
+ rb_define_method(cGzipFile, "comment", rb_gzfile_comment, 0);
4480
+ rb_define_method(cGzipReader, "lineno", rb_gzfile_lineno, 0);
4481
+ rb_define_method(cGzipReader, "lineno=", rb_gzfile_set_lineno, 1);
4482
+ rb_define_method(cGzipWriter, "mtime=", rb_gzfile_set_mtime, 1);
4483
+ rb_define_method(cGzipWriter, "orig_name=", rb_gzfile_set_orig_name,1);
4484
+ rb_define_method(cGzipWriter, "comment=", rb_gzfile_set_comment, 1);
4485
+ rb_define_method(cGzipFile, "close", rb_gzfile_close, 0);
4486
+ rb_define_method(cGzipFile, "finish", rb_gzfile_finish, 0);
4487
+ rb_define_method(cGzipFile, "closed?", rb_gzfile_closed_p, 0);
4488
+ rb_define_method(cGzipReader, "eof", rb_gzfile_eof_p, 0);
4489
+ rb_define_method(cGzipReader, "eof?", rb_gzfile_eof_p, 0);
4490
+ rb_define_method(cGzipFile, "sync", rb_gzfile_sync, 0);
4491
+ rb_define_method(cGzipFile, "sync=", rb_gzfile_set_sync, 1);
4492
+ rb_define_method(cGzipReader, "pos", rb_gzfile_total_out, 0);
4493
+ rb_define_method(cGzipWriter, "pos", rb_gzfile_total_in, 0);
4494
+ rb_define_method(cGzipReader, "tell", rb_gzfile_total_out, 0);
4495
+ rb_define_method(cGzipWriter, "tell", rb_gzfile_total_in, 0);
4496
+
4497
+ rb_define_singleton_method(cGzipWriter, "open", rb_gzwriter_s_open,-1);
4498
+ rb_define_alloc_func(cGzipWriter, rb_gzwriter_s_allocate);
4499
+ rb_define_method(cGzipWriter, "initialize", rb_gzwriter_initialize,-1);
4500
+ rb_define_method(cGzipWriter, "flush", rb_gzwriter_flush, -1);
4501
+ rb_define_method(cGzipWriter, "write", rb_gzwriter_write, 1);
4502
+ rb_define_method(cGzipWriter, "putc", rb_gzwriter_putc, 1);
4503
+ rb_define_method(cGzipWriter, "<<", rb_gzwriter_addstr, 1);
4504
+ rb_define_method(cGzipWriter, "printf", rb_gzwriter_printf, -1);
4505
+ rb_define_method(cGzipWriter, "print", rb_gzwriter_print, -1);
4506
+ rb_define_method(cGzipWriter, "puts", rb_gzwriter_puts, -1);
4507
+
4508
+ rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1);
4509
+ rb_define_alloc_func(cGzipReader, rb_gzreader_s_allocate);
4510
+ rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, -1);
4511
+ rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0);
4512
+ rb_define_method(cGzipReader, "unused", rb_gzreader_unused, 0);
4513
+ rb_define_method(cGzipReader, "read", rb_gzreader_read, -1);
4514
+ rb_define_method(cGzipReader, "readpartial", rb_gzreader_readpartial, -1);
4515
+ rb_define_method(cGzipReader, "getc", rb_gzreader_getc, 0);
4516
+ rb_define_method(cGzipReader, "getbyte", rb_gzreader_getbyte, 0);
4517
+ rb_define_method(cGzipReader, "readchar", rb_gzreader_readchar, 0);
4518
+ rb_define_method(cGzipReader, "readbyte", rb_gzreader_readbyte, 0);
4519
+ rb_define_method(cGzipReader, "each_byte", rb_gzreader_each_byte, 0);
4520
+ rb_define_method(cGzipReader, "each_char", rb_gzreader_each_char, 0);
4521
+ rb_define_method(cGzipReader, "bytes", rb_gzreader_bytes, 0);
4522
+ rb_define_method(cGzipReader, "ungetc", rb_gzreader_ungetc, 1);
4523
+ rb_define_method(cGzipReader, "ungetbyte", rb_gzreader_ungetbyte, 1);
4524
+ rb_define_method(cGzipReader, "gets", rb_gzreader_gets, -1);
4525
+ rb_define_method(cGzipReader, "readline", rb_gzreader_readline, -1);
4526
+ rb_define_method(cGzipReader, "each", rb_gzreader_each, -1);
4527
+ rb_define_method(cGzipReader, "each_line", rb_gzreader_each, -1);
4528
+ rb_define_method(cGzipReader, "lines", rb_gzreader_lines, -1);
4529
+ rb_define_method(cGzipReader, "readlines", rb_gzreader_readlines, -1);
4530
+ rb_define_method(cGzipReader, "external_encoding", rb_gzreader_external_encoding, 0);
4531
+
4532
+ /* The OS code of current host */
4533
+ rb_define_const(mZlib, "OS_CODE", INT2FIX(OS_CODE));
4534
+ /* OS code for MSDOS hosts */
4535
+ rb_define_const(mZlib, "OS_MSDOS", INT2FIX(OS_MSDOS));
4536
+ /* OS code for Amiga hosts */
4537
+ rb_define_const(mZlib, "OS_AMIGA", INT2FIX(OS_AMIGA));
4538
+ /* OS code for VMS hosts */
4539
+ rb_define_const(mZlib, "OS_VMS", INT2FIX(OS_VMS));
4540
+ /* OS code for UNIX hosts */
4541
+ rb_define_const(mZlib, "OS_UNIX", INT2FIX(OS_UNIX));
4542
+ /* OS code for Atari hosts */
4543
+ rb_define_const(mZlib, "OS_ATARI", INT2FIX(OS_ATARI));
4544
+ /* OS code for OS2 hosts */
4545
+ rb_define_const(mZlib, "OS_OS2", INT2FIX(OS_OS2));
4546
+ /* OS code for Mac OS hosts */
4547
+ rb_define_const(mZlib, "OS_MACOS", INT2FIX(OS_MACOS));
4548
+ /* OS code for TOPS-20 hosts */
4549
+ rb_define_const(mZlib, "OS_TOPS20", INT2FIX(OS_TOPS20));
4550
+ /* OS code for Win32 hosts */
4551
+ rb_define_const(mZlib, "OS_WIN32", INT2FIX(OS_WIN32));
4552
+ /* OS code for VM OS hosts */
4553
+ rb_define_const(mZlib, "OS_VMCMS", INT2FIX(OS_VMCMS));
4554
+ /* OS code for Z-System hosts */
4555
+ rb_define_const(mZlib, "OS_ZSYSTEM", INT2FIX(OS_ZSYSTEM));
4556
+ /* OS code for CP/M hosts */
4557
+ rb_define_const(mZlib, "OS_CPM", INT2FIX(OS_CPM));
4558
+ /* OS code for QDOS hosts */
4559
+ rb_define_const(mZlib, "OS_QDOS", INT2FIX(OS_QDOS));
4560
+ /* OS code for RISC OS hosts */
4561
+ rb_define_const(mZlib, "OS_RISCOS", INT2FIX(OS_RISCOS));
4562
+ /* OS code for unknown hosts */
4563
+ rb_define_const(mZlib, "OS_UNKNOWN", INT2FIX(OS_UNKNOWN));
4564
+
4565
+ #endif /* GZIP_SUPPORT */
4566
+ }
4567
+
4568
+ /* Document error classes. */
4569
+
4570
+ /*
4571
+ * Document-class: Zlib::Error
4572
+ *
4573
+ * The superclass for all exceptions raised by Ruby/zlib.
4574
+ *
4575
+ * The following exceptions are defined as subclasses of Zlib::Error. These
4576
+ * exceptions are raised when zlib library functions return with an error
4577
+ * status.
4578
+ *
4579
+ * - Zlib::StreamEnd
4580
+ * - Zlib::NeedDict
4581
+ * - Zlib::DataError
4582
+ * - Zlib::StreamError
4583
+ * - Zlib::MemError
4584
+ * - Zlib::BufError
4585
+ * - Zlib::VersionError
4586
+ *
4587
+ */
4588
+
4589
+ /*
4590
+ * Document-class: Zlib::StreamEnd
4591
+ *
4592
+ * Subclass of Zlib::Error
4593
+ *
4594
+ * When zlib returns a Z_STREAM_END
4595
+ * is return if the end of the compressed data has been reached
4596
+ * and all uncompressed out put has been produced.
4597
+ *
4598
+ */
4599
+
4600
+ /*
4601
+ * Document-class: Zlib::NeedDict
4602
+ *
4603
+ * Subclass of Zlib::Error
4604
+ *
4605
+ * When zlib returns a Z_NEED_DICT
4606
+ * if a preset dictionary is needed at this point.
4607
+ *
4608
+ * Used by Zlib::Inflate.inflate and <tt>Zlib.inflate</tt>
4609
+ */
4610
+
4611
+ /*
4612
+ * Document-class: Zlib::VersionError
4613
+ *
4614
+ * Subclass of Zlib::Error
4615
+ *
4616
+ * When zlib returns a Z_VERSION_ERROR,
4617
+ * usually if the zlib library version is incompatible with the
4618
+ * version assumed by the caller.
4619
+ *
4620
+ */
4621
+
4622
+ /*
4623
+ * Document-class: Zlib::MemError
4624
+ *
4625
+ * Subclass of Zlib::Error
4626
+ *
4627
+ * When zlib returns a Z_MEM_ERROR,
4628
+ * usually if there was not enough memory.
4629
+ *
4630
+ */
4631
+
4632
+ /*
4633
+ * Document-class: Zlib::StreamError
4634
+ *
4635
+ * Subclass of Zlib::Error
4636
+ *
4637
+ * When zlib returns a Z_STREAM_ERROR,
4638
+ * usually if the stream state was inconsistent.
4639
+ *
4640
+ */
4641
+
4642
+ /*
4643
+ * Document-class: Zlib::BufError
4644
+ *
4645
+ * Subclass of Zlib::Error when zlib returns a Z_BUF_ERROR.
4646
+ *
4647
+ * Usually if no progress is possible.
4648
+ *
4649
+ */
4650
+
4651
+ /*
4652
+ * Document-class: Zlib::DataError
4653
+ *
4654
+ * Subclass of Zlib::Error when zlib returns a Z_DATA_ERROR.
4655
+ *
4656
+ * Usually if a stream was prematurely freed.
4657
+ *
4658
+ */
4659
+
4660
+ /*
4661
+ * Document-class: Zlib::GzipFile::Error
4662
+ *
4663
+ * Base class of errors that occur when processing GZIP files.
4664
+ */
4665
+
4666
+ /*
4667
+ * Document-class: Zlib::GzipFile::NoFooter
4668
+ *
4669
+ * Raised when gzip file footer is not found.
4670
+ */
4671
+
4672
+ /*
4673
+ * Document-class: Zlib::GzipFile::CRCError
4674
+ *
4675
+ * Raised when the CRC checksum recorded in gzip file footer is not equivalent
4676
+ * to the CRC checksum of the actual uncompressed data.
4677
+ */
4678
+
4679
+ /*
4680
+ * Document-class: Zlib::GzipFile::LengthError
4681
+ *
4682
+ * Raised when the data length recorded in the gzip file footer is not equivalent
4683
+ * to the length of the actual uncompressed data.
4684
+ */
4685
+
4686
+