zstdlib 0.9.0-x64-mingw-ucrt

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