zstdlib 0.1.0-x64-mingw32

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