zstdlib 0.9.0-arm64-darwin

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