zstdlib 0.11.0-x86_64-darwin → 0.13.0-x86_64-darwin

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