zstdlib 0.8.0-x86-mingw32 → 0.9.0-x86-mingw32

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