zstdlib 0.1.0-x64-mingw32

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