zstdlib 0.9.0-x86_64-darwin

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