zstdlib 0.13.0-x86_64-linux

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