diff options
Diffstat (limited to 'ext/zlib')
| -rw-r--r-- | ext/zlib/.gitignore | 1 | ||||
| -rw-r--r-- | ext/zlib/depend | 177 | ||||
| -rw-r--r-- | ext/zlib/extconf.rb | 104 | ||||
| -rw-r--r-- | ext/zlib/zlib.c | 2216 | ||||
| -rw-r--r-- | ext/zlib/zlib.gemspec | 31 |
5 files changed, 1801 insertions, 728 deletions
diff --git a/ext/zlib/.gitignore b/ext/zlib/.gitignore new file mode 100644 index 0000000000..069491b4b4 --- /dev/null +++ b/ext/zlib/.gitignore @@ -0,0 +1 @@ +/zlib-[1-9]*.*.* diff --git a/ext/zlib/depend b/ext/zlib/depend new file mode 100644 index 0000000000..22e9ca867a --- /dev/null +++ b/ext/zlib/depend @@ -0,0 +1,177 @@ +# AUTOGENERATED DEPENDENCIES START +zlib.o: $(RUBY_EXTCONF_H) +zlib.o: $(arch_hdrdir)/ruby/config.h +zlib.o: $(hdrdir)/ruby.h +zlib.o: $(hdrdir)/ruby/assert.h +zlib.o: $(hdrdir)/ruby/backward.h +zlib.o: $(hdrdir)/ruby/backward/2/assume.h +zlib.o: $(hdrdir)/ruby/backward/2/attributes.h +zlib.o: $(hdrdir)/ruby/backward/2/bool.h +zlib.o: $(hdrdir)/ruby/backward/2/inttypes.h +zlib.o: $(hdrdir)/ruby/backward/2/limits.h +zlib.o: $(hdrdir)/ruby/backward/2/long_long.h +zlib.o: $(hdrdir)/ruby/backward/2/stdalign.h +zlib.o: $(hdrdir)/ruby/backward/2/stdarg.h +zlib.o: $(hdrdir)/ruby/defines.h +zlib.o: $(hdrdir)/ruby/encoding.h +zlib.o: $(hdrdir)/ruby/intern.h +zlib.o: $(hdrdir)/ruby/internal/abi.h +zlib.o: $(hdrdir)/ruby/internal/anyargs.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/char.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/double.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/fixnum.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/gid_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/int.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/intptr_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/long.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/long_long.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/mode_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/off_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/pid_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/short.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/size_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/st_data_t.h +zlib.o: $(hdrdir)/ruby/internal/arithmetic/uid_t.h +zlib.o: $(hdrdir)/ruby/internal/assume.h +zlib.o: $(hdrdir)/ruby/internal/attr/alloc_size.h +zlib.o: $(hdrdir)/ruby/internal/attr/artificial.h +zlib.o: $(hdrdir)/ruby/internal/attr/cold.h +zlib.o: $(hdrdir)/ruby/internal/attr/const.h +zlib.o: $(hdrdir)/ruby/internal/attr/constexpr.h +zlib.o: $(hdrdir)/ruby/internal/attr/deprecated.h +zlib.o: $(hdrdir)/ruby/internal/attr/diagnose_if.h +zlib.o: $(hdrdir)/ruby/internal/attr/enum_extensibility.h +zlib.o: $(hdrdir)/ruby/internal/attr/error.h +zlib.o: $(hdrdir)/ruby/internal/attr/flag_enum.h +zlib.o: $(hdrdir)/ruby/internal/attr/forceinline.h +zlib.o: $(hdrdir)/ruby/internal/attr/format.h +zlib.o: $(hdrdir)/ruby/internal/attr/maybe_unused.h +zlib.o: $(hdrdir)/ruby/internal/attr/noalias.h +zlib.o: $(hdrdir)/ruby/internal/attr/nodiscard.h +zlib.o: $(hdrdir)/ruby/internal/attr/noexcept.h +zlib.o: $(hdrdir)/ruby/internal/attr/noinline.h +zlib.o: $(hdrdir)/ruby/internal/attr/nonnull.h +zlib.o: $(hdrdir)/ruby/internal/attr/noreturn.h +zlib.o: $(hdrdir)/ruby/internal/attr/packed_struct.h +zlib.o: $(hdrdir)/ruby/internal/attr/pure.h +zlib.o: $(hdrdir)/ruby/internal/attr/restrict.h +zlib.o: $(hdrdir)/ruby/internal/attr/returns_nonnull.h +zlib.o: $(hdrdir)/ruby/internal/attr/warning.h +zlib.o: $(hdrdir)/ruby/internal/attr/weakref.h +zlib.o: $(hdrdir)/ruby/internal/cast.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is/apple.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is/clang.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is/gcc.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is/intel.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is/msvc.h +zlib.o: $(hdrdir)/ruby/internal/compiler_is/sunpro.h +zlib.o: $(hdrdir)/ruby/internal/compiler_since.h +zlib.o: $(hdrdir)/ruby/internal/config.h +zlib.o: $(hdrdir)/ruby/internal/constant_p.h +zlib.o: $(hdrdir)/ruby/internal/core.h +zlib.o: $(hdrdir)/ruby/internal/core/rarray.h +zlib.o: $(hdrdir)/ruby/internal/core/rbasic.h +zlib.o: $(hdrdir)/ruby/internal/core/rbignum.h +zlib.o: $(hdrdir)/ruby/internal/core/rclass.h +zlib.o: $(hdrdir)/ruby/internal/core/rdata.h +zlib.o: $(hdrdir)/ruby/internal/core/rfile.h +zlib.o: $(hdrdir)/ruby/internal/core/rhash.h +zlib.o: $(hdrdir)/ruby/internal/core/robject.h +zlib.o: $(hdrdir)/ruby/internal/core/rregexp.h +zlib.o: $(hdrdir)/ruby/internal/core/rstring.h +zlib.o: $(hdrdir)/ruby/internal/core/rstruct.h +zlib.o: $(hdrdir)/ruby/internal/core/rtypeddata.h +zlib.o: $(hdrdir)/ruby/internal/ctype.h +zlib.o: $(hdrdir)/ruby/internal/dllexport.h +zlib.o: $(hdrdir)/ruby/internal/dosish.h +zlib.o: $(hdrdir)/ruby/internal/encoding/coderange.h +zlib.o: $(hdrdir)/ruby/internal/encoding/ctype.h +zlib.o: $(hdrdir)/ruby/internal/encoding/encoding.h +zlib.o: $(hdrdir)/ruby/internal/encoding/pathname.h +zlib.o: $(hdrdir)/ruby/internal/encoding/re.h +zlib.o: $(hdrdir)/ruby/internal/encoding/sprintf.h +zlib.o: $(hdrdir)/ruby/internal/encoding/string.h +zlib.o: $(hdrdir)/ruby/internal/encoding/symbol.h +zlib.o: $(hdrdir)/ruby/internal/encoding/transcode.h +zlib.o: $(hdrdir)/ruby/internal/error.h +zlib.o: $(hdrdir)/ruby/internal/eval.h +zlib.o: $(hdrdir)/ruby/internal/event.h +zlib.o: $(hdrdir)/ruby/internal/fl_type.h +zlib.o: $(hdrdir)/ruby/internal/gc.h +zlib.o: $(hdrdir)/ruby/internal/glob.h +zlib.o: $(hdrdir)/ruby/internal/globals.h +zlib.o: $(hdrdir)/ruby/internal/has/attribute.h +zlib.o: $(hdrdir)/ruby/internal/has/builtin.h +zlib.o: $(hdrdir)/ruby/internal/has/c_attribute.h +zlib.o: $(hdrdir)/ruby/internal/has/cpp_attribute.h +zlib.o: $(hdrdir)/ruby/internal/has/declspec_attribute.h +zlib.o: $(hdrdir)/ruby/internal/has/extension.h +zlib.o: $(hdrdir)/ruby/internal/has/feature.h +zlib.o: $(hdrdir)/ruby/internal/has/warning.h +zlib.o: $(hdrdir)/ruby/internal/intern/array.h +zlib.o: $(hdrdir)/ruby/internal/intern/bignum.h +zlib.o: $(hdrdir)/ruby/internal/intern/class.h +zlib.o: $(hdrdir)/ruby/internal/intern/compar.h +zlib.o: $(hdrdir)/ruby/internal/intern/complex.h +zlib.o: $(hdrdir)/ruby/internal/intern/cont.h +zlib.o: $(hdrdir)/ruby/internal/intern/dir.h +zlib.o: $(hdrdir)/ruby/internal/intern/enum.h +zlib.o: $(hdrdir)/ruby/internal/intern/enumerator.h +zlib.o: $(hdrdir)/ruby/internal/intern/error.h +zlib.o: $(hdrdir)/ruby/internal/intern/eval.h +zlib.o: $(hdrdir)/ruby/internal/intern/file.h +zlib.o: $(hdrdir)/ruby/internal/intern/hash.h +zlib.o: $(hdrdir)/ruby/internal/intern/io.h +zlib.o: $(hdrdir)/ruby/internal/intern/load.h +zlib.o: $(hdrdir)/ruby/internal/intern/marshal.h +zlib.o: $(hdrdir)/ruby/internal/intern/numeric.h +zlib.o: $(hdrdir)/ruby/internal/intern/object.h +zlib.o: $(hdrdir)/ruby/internal/intern/parse.h +zlib.o: $(hdrdir)/ruby/internal/intern/proc.h +zlib.o: $(hdrdir)/ruby/internal/intern/process.h +zlib.o: $(hdrdir)/ruby/internal/intern/random.h +zlib.o: $(hdrdir)/ruby/internal/intern/range.h +zlib.o: $(hdrdir)/ruby/internal/intern/rational.h +zlib.o: $(hdrdir)/ruby/internal/intern/re.h +zlib.o: $(hdrdir)/ruby/internal/intern/ruby.h +zlib.o: $(hdrdir)/ruby/internal/intern/select.h +zlib.o: $(hdrdir)/ruby/internal/intern/select/largesize.h +zlib.o: $(hdrdir)/ruby/internal/intern/set.h +zlib.o: $(hdrdir)/ruby/internal/intern/signal.h +zlib.o: $(hdrdir)/ruby/internal/intern/sprintf.h +zlib.o: $(hdrdir)/ruby/internal/intern/string.h +zlib.o: $(hdrdir)/ruby/internal/intern/struct.h +zlib.o: $(hdrdir)/ruby/internal/intern/thread.h +zlib.o: $(hdrdir)/ruby/internal/intern/time.h +zlib.o: $(hdrdir)/ruby/internal/intern/variable.h +zlib.o: $(hdrdir)/ruby/internal/intern/vm.h +zlib.o: $(hdrdir)/ruby/internal/interpreter.h +zlib.o: $(hdrdir)/ruby/internal/iterator.h +zlib.o: $(hdrdir)/ruby/internal/memory.h +zlib.o: $(hdrdir)/ruby/internal/method.h +zlib.o: $(hdrdir)/ruby/internal/module.h +zlib.o: $(hdrdir)/ruby/internal/newobj.h +zlib.o: $(hdrdir)/ruby/internal/scan_args.h +zlib.o: $(hdrdir)/ruby/internal/special_consts.h +zlib.o: $(hdrdir)/ruby/internal/static_assert.h +zlib.o: $(hdrdir)/ruby/internal/stdalign.h +zlib.o: $(hdrdir)/ruby/internal/stdbool.h +zlib.o: $(hdrdir)/ruby/internal/stdckdint.h +zlib.o: $(hdrdir)/ruby/internal/symbol.h +zlib.o: $(hdrdir)/ruby/internal/value.h +zlib.o: $(hdrdir)/ruby/internal/value_type.h +zlib.o: $(hdrdir)/ruby/internal/variable.h +zlib.o: $(hdrdir)/ruby/internal/warning_push.h +zlib.o: $(hdrdir)/ruby/internal/xmalloc.h +zlib.o: $(hdrdir)/ruby/io.h +zlib.o: $(hdrdir)/ruby/missing.h +zlib.o: $(hdrdir)/ruby/onigmo.h +zlib.o: $(hdrdir)/ruby/oniguruma.h +zlib.o: $(hdrdir)/ruby/ruby.h +zlib.o: $(hdrdir)/ruby/st.h +zlib.o: $(hdrdir)/ruby/subst.h +zlib.o: $(hdrdir)/ruby/thread.h +zlib.o: zlib.c +# AUTOGENERATED DEPENDENCIES END diff --git a/ext/zlib/extconf.rb b/ext/zlib/extconf.rb index 499f55a046..2b2dbb1a5b 100644 --- a/ext/zlib/extconf.rb +++ b/ext/zlib/extconf.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # # extconf.rb # @@ -9,19 +10,80 @@ require 'rbconfig' dir_config 'zlib' +libs = $libs +have_zlib = %w'z libz zlib1 zlib zdll zlibwapi'.any? {|z| have_library(z, 'deflateReset(NULL)', 'zlib.h')} -if %w'z libz zlib1 zlib zdll'.find {|z| have_library(z, 'deflateReset')} and - have_header('zlib.h') then +unless have_zlib + $libs = libs + unless File.directory?(zsrc = "#{$srcdir}/zlib") + dirs = Dir.open($srcdir) {|z| z.grep(/\Azlib-\d+[.\d]*\z/) {|x|"#{$srcdir}/#{x}"}} + dirs.delete_if {|x| !File.directory?(x)} + zsrc = dirs.max_by {|x| x.scan(/\d+/).map(&:to_i)} + end + if zsrc + addconf = [ + "ZSRC = $(srcdir)/#{File.basename(zsrc)}\n", + "all:\n", + ] + $INCFLAGS << " -I$(ZSRC)" + if $mswin or $mingw + dll = "zlib1.dll" + $extso << dll + $cleanfiles << "$(topdir)/#{dll}" << "$(ZIMPLIB)" + zmk = "\t$(MAKE) -f $(ZMKFILE) TOP=$(ZSRC)" + zopts = [] + if $nmake + zmkfile = "$(ZSRC)/win32/Makefile.msc" + m = "#{zsrc}/win32/Makefile.msc" + # zopts << "USE_ASM=1" + zopts << "ARCH=#{RbConfig::CONFIG['target_cpu']}" + else + zmkfile = "$(ZSRC)/win32/Makefile.gcc" + m = "#{zsrc}/win32/Makefile.gcc" + zmk += " PREFIX=" + zmk << CONFIG['CC'][/(.*-)gcc([^\/]*)\z/, 1] + zmk << " CC=$(CC)" if $2 + end + m = File.read(m) + zimplib = m[/^IMPLIB[ \t]*=[ \t]*(\S+)/, 1] + ($LOCAL_LIBS << " ./" << zimplib).strip! + unless $nmake or /^TOP[ \t]/ =~ m + m.gsub!(/win32\/zlib\.def/, '$(TOP)/\&') + m.gsub!(/^(\t.*[ \t])(\S+\.rc)/, '\1-I$(<D) $<') + m = "TOP = .\n""VPATH=$(TOP)\n" + m + zmkfile = File.basename(zmkfile) + File.rename(zmkfile, zmkfile+".orig") if File.exist?(zmkfile) + File.write(zmkfile, m) + end + addconf.push( + "ZMKFILE = #{zmkfile}\n", + "ZIMPLIB = #{zimplib}\n", + "ZOPTS = #{zopts.join(' ')}\n", + "$(TARGET_SO): $(ZIMPLIB)\n", + "$(ZIMPLIB):\n", + "#{zmk} $(ZOPTS) $@\n", + "install-so static: $(topdir)/#{dll}", + "$(topdir)/#{dll}: $(ZIMPLIB)\n", + "\t$(Q) $(COPY) #{dll} $(@D)\n", + "clean: clean-zsrc\n", + "clean-zsrc:\n", + "#{zmk} clean\n", + ) + end + Logging.message "using zlib in #{zsrc}\n" + $defs << "-DHAVE_ZLIB_H" + have_zlib = true + end +end +if have_zlib defines = [] - message 'checking for kind of operating system... ' + Logging::message 'checking for kind of operating system... ' os_code = with_config('os-code') || case RUBY_PLATFORM.split('-',2)[1] when 'amigaos' then os_code = 'AMIGA' - when /\Aos2[\-_]emx\z/ then - os_code = 'OS2' when /mswin|mingw|bccwin/ then # NOTE: cygwin should be regarded as Unix. os_code = 'WIN32' @@ -36,7 +98,6 @@ if %w'z libz zlib1 zlib zdll'.find {|z| have_library(z, 'deflateReset')} and 'OS_VMS' => 'VMS', 'OS_UNIX' => 'Unix', 'OS_ATARI' => 'Atari', - 'OS_OS2' => 'OS/2', 'OS_MACOS' => 'MacOS', 'OS_TOPS20' => 'TOPS20', 'OS_WIN32' => 'Win32', @@ -48,17 +109,36 @@ if %w'z libz zlib1 zlib zdll'.find {|z| have_library(z, 'deflateReset')} and 'OS_UNKNOWN' => 'Unknown', } unless OS_NAMES.key? os_code then - puts "invalid OS_CODE `#{os_code}'" - exit + raise "invalid OS_CODE `#{os_code}'" end - message "#{OS_NAMES[os_code]}\n" + Logging::message "#{OS_NAMES[os_code]}\n" defines << "OS_CODE=#{os_code}" $defs.concat(defines.collect{|d|' -D'+d}) - have_func('crc32_combine', 'zlib.h') - have_func('adler32_combine', 'zlib.h') + if zsrc + $defs << "-DHAVE_CRC32_COMBINE" + $defs << "-DHAVE_ADLER32_COMBINE" + $defs << "-DHAVE_TYPE_Z_CRC_T" + $defs << "-DHAVE_CRC32_Z" + $defs << "-DHAVE_ADLER32_Z" + $defs << "-DHAVE_ZLIB_SIZE_T_FUNCS" + else + have_func('crc32_combine', 'zlib.h') + have_func('adler32_combine', 'zlib.h') + have_type('z_crc_t', 'zlib.h') + if (have_type('z_size_t', 'zlib.h') && + have_func('crc32_z', 'zlib.h') && + have_func('adler32_z', 'zlib.h')) + $defs << "-DHAVE_ZLIB_SIZE_T_FUNCS" + end + end - create_makefile('zlib') + create_makefile('zlib') {|conf| + if zsrc + conf.concat addconf if addconf + end + conf + } end diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 42c2c75f92..481d74b2b6 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -10,6 +10,7 @@ #include <zlib.h> #include <time.h> #include <ruby/io.h> +#include <ruby/thread.h> #ifdef HAVE_VALGRIND_MEMCHECK_H # include <valgrind/memcheck.h> @@ -20,14 +21,15 @@ # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n)) # endif #else -# define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */ -# define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */ +# define VALGRIND_MAKE_MEM_DEFINED(p, n) 0 +# define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0 #endif -#define RUBY_ZLIB_VERSION "0.6.0" +#define RUBY_ZLIB_VERSION "3.2.3" - -#define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0) +#ifndef RB_PASS_CALLED_KEYWORDS +# define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass) +#endif #ifndef GZIP_SUPPORT #define GZIP_SUPPORT 1 @@ -42,6 +44,14 @@ #endif #endif +#if defined(HAVE_ZLIB_SIZE_T_FUNCS) +typedef uLong (*checksum_func)(uLong, const Bytef*, z_size_t); +# define crc32 crc32_z +# define adler32 adler32_z +#else +typedef uLong (*checksum_func)(uLong, const Bytef*, uInt); +#endif + #if SIZEOF_LONG > SIZEOF_INT static inline uInt max_uint(long n) @@ -54,13 +64,16 @@ max_uint(long n) #define MAX_UINT(n) (uInt)(n) #endif -#define sizeof(x) ((int)sizeof(x)) +#define OPTHASH_GIVEN_P(opts) \ + (argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1)) + +static ID id_dictionaries, id_read, id_buffer; /*--------- Prototypes --------*/ static NORETURN(void raise_zlib_error(int, const char*)); static VALUE rb_zlib_version(VALUE); -static VALUE do_checksum(int, VALUE*, uLong (*)(uLong, const Bytef*, uInt)); +static VALUE do_checksum(int, VALUE*, checksum_func); static VALUE rb_zlib_adler32(int, VALUE*, VALUE); static VALUE rb_zlib_crc32(int, VALUE*, VALUE); static VALUE rb_zlib_crc_table(VALUE); @@ -70,12 +83,14 @@ static void finalizer_warn(const char*); struct zstream; struct zstream_funcs; +struct zstream_run_args; static void zstream_init(struct zstream*, const struct zstream_funcs*); static void zstream_expand_buffer(struct zstream*); static void zstream_expand_buffer_into(struct zstream*, unsigned long); +static int zstream_expand_buffer_non_stream(struct zstream *z); static void zstream_append_buffer(struct zstream*, const Bytef*, long); static VALUE zstream_detach_buffer(struct zstream*); -static VALUE zstream_shift_buffer(struct zstream*, long); +static VALUE zstream_shift_buffer(struct zstream*, long, VALUE); static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long); static void zstream_buffer_ungetbyte(struct zstream*, int); static void zstream_append_input(struct zstream*, const Bytef*, long); @@ -85,10 +100,11 @@ static void zstream_passthrough_input(struct zstream*); static VALUE zstream_detach_input(struct zstream*); static void zstream_reset(struct zstream*); static VALUE zstream_end(struct zstream*); +static VALUE zstream_ensure_end(VALUE v); static void zstream_run(struct zstream*, Bytef*, long, int); static VALUE zstream_sync(struct zstream*, Bytef*, long); -static void zstream_mark(struct zstream*); -static void zstream_free(struct zstream*); +static void zstream_mark(void*); +static void zstream_free(void*); static VALUE zstream_new(VALUE, const struct zstream_funcs*); static struct zstream *get_zstream(VALUE); static void zstream_finalize(struct zstream*); @@ -125,7 +141,7 @@ static VALUE rb_inflate_s_allocate(VALUE); static VALUE rb_inflate_initialize(int, VALUE*, VALUE); static VALUE rb_inflate_s_inflate(VALUE, VALUE); static void do_inflate(struct zstream*, VALUE); -static VALUE rb_inflate_inflate(VALUE, VALUE); +static VALUE rb_inflate_inflate(int, VALUE*, VALUE); static VALUE rb_inflate_addstr(VALUE, VALUE); static VALUE rb_inflate_sync(VALUE, VALUE); static VALUE rb_inflate_sync_point_p(VALUE); @@ -133,29 +149,29 @@ static VALUE rb_inflate_set_dictionary(VALUE, VALUE); #if GZIP_SUPPORT struct gzfile; -static void gzfile_mark(struct gzfile*); -static void gzfile_free(struct gzfile*); +static void gzfile_mark(void*); +static void gzfile_free(void*); static VALUE gzfile_new(VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*))); static void gzfile_reset(struct gzfile*); static void gzfile_close(struct gzfile*, int); static void gzfile_write_raw(struct gzfile*); static VALUE gzfile_read_raw_partial(VALUE); -static VALUE gzfile_read_raw_rescue(VALUE); -static VALUE gzfile_read_raw(struct gzfile*); -static int gzfile_read_raw_ensure(struct gzfile*, long); +static VALUE gzfile_read_raw_rescue(VALUE,VALUE); +static VALUE gzfile_read_raw(struct gzfile*, VALUE outbuf); +static int gzfile_read_raw_ensure(struct gzfile*, long, VALUE outbuf); static char *gzfile_read_raw_until_zero(struct gzfile*, long); static unsigned int gzfile_get16(const unsigned char*); static unsigned long gzfile_get32(const unsigned char*); static void gzfile_set32(unsigned long n, unsigned char*); static void gzfile_make_header(struct gzfile*); static void gzfile_make_footer(struct gzfile*); -static void gzfile_read_header(struct gzfile*); -static void gzfile_check_footer(struct gzfile*); +static void gzfile_read_header(struct gzfile*, VALUE outbuf); +static void gzfile_check_footer(struct gzfile*, VALUE outbuf); static void gzfile_write(struct gzfile*, Bytef*, long); -static long gzfile_read_more(struct gzfile*); +static long gzfile_read_more(struct gzfile*, VALUE outbuf); static void gzfile_calc_crc(struct gzfile*, VALUE); -static VALUE gzfile_read(struct gzfile*, long); -static VALUE gzfile_read_all(struct gzfile*); +static VALUE gzfile_read(struct gzfile*, long, VALUE); +static VALUE gzfile_read_all(struct gzfile*, VALUE); static void gzfile_ungets(struct gzfile*, const Bytef*, long); static void gzfile_ungetbyte(struct gzfile*, int); static VALUE gzfile_writer_end_run(VALUE); @@ -197,7 +213,7 @@ static VALUE rb_gzwriter_s_allocate(VALUE); static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE); static VALUE rb_gzwriter_initialize(int, VALUE*, VALUE); static VALUE rb_gzwriter_flush(int, VALUE*, VALUE); -static VALUE rb_gzwriter_write(VALUE, VALUE); +static VALUE rb_gzwriter_write(int, VALUE*, VALUE); static VALUE rb_gzwriter_putc(VALUE, VALUE); static VALUE rb_gzreader_s_allocate(VALUE); @@ -222,9 +238,50 @@ static VALUE rb_gzreader_readlines(int, VALUE*, VALUE); /* * Document-module: Zlib * - * == Overview + * This module provides access to the {zlib library}[http://zlib.net]. Zlib is + * designed to be a portable, free, general-purpose, legally unencumbered -- + * that is, not covered by any patents -- lossless data-compression library + * for use on virtually any computer hardware and operating system. + * + * The zlib compression library provides in-memory compression and + * decompression functions, including integrity checks of the uncompressed + * data. + * + * The zlib compressed data format is described in RFC 1950, which is a + * wrapper around a deflate stream which is described in RFC 1951. + * + * The library also supports reading and writing files in gzip (.gz) format + * with an interface similar to that of IO. The gzip format is described in + * RFC 1952 which is also a wrapper around a deflate stream. + * + * The zlib format was designed to be compact and fast for use in memory and on + * communications channels. The gzip format was designed for single-file + * compression on file systems, has a larger header than zlib to maintain + * directory information, and uses a different, slower check method than zlib. + * + * See your system's zlib.h for further information about zlib + * + * == Sample usage + * + * Using the wrapper to compress strings with default parameters is quite + * simple: + * + * require "zlib" + * + * data_to_compress = File.read("don_quixote.txt") + * + * puts "Input size: #{data_to_compress.size}" + * #=> Input size: 2347740 + * + * data_compressed = Zlib::Deflate.deflate(data_to_compress) * - * Access to the zlib library. + * puts "Compressed size: #{data_compressed.size}" + * #=> Compressed size: 887238 + * + * uncompressed_data = Zlib::Inflate.inflate(data_compressed) + * + * puts "Uncompressed data is: #{uncompressed_data}" + * #=> Uncompressed data is: The Project Gutenberg EBook of Don Quixote... * * == Class tree * @@ -239,6 +296,7 @@ static VALUE rb_gzreader_readlines(int, VALUE*, VALUE); * - Zlib::MemError * - Zlib::BufError * - Zlib::VersionError + * - Zlib::InProgressError * * (if you have GZIP_SUPPORT) * - Zlib::GzipReader @@ -249,15 +307,13 @@ static VALUE rb_gzreader_readlines(int, VALUE*, VALUE); * - Zlib::GzipFile::CRCError * - Zlib::GzipFile::NoFooter * - * see also zlib.h - * */ void Init_zlib(void); /*--------- Exceptions --------*/ static VALUE cZError, cStreamEnd, cNeedDict; -static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError; +static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError, cInProgressError; static void raise_zlib_error(int err, const char *msg) @@ -294,11 +350,8 @@ raise_zlib_error(int err, const char *msg) rb_sys_fail(msg); /* no return */ default: - { - char buf[BUFSIZ]; - snprintf(buf, BUFSIZ, "unknown zlib error %d: %s", err, msg); - exc = rb_exc_new2(cZError, buf); - } + exc = rb_exc_new_str(cZError, + rb_sprintf("unknown zlib error %d: %s", err, msg)); } rb_exc_raise(exc); @@ -310,7 +363,9 @@ raise_zlib_error(int err, const char *msg) static void finalizer_warn(const char *msg) { +#if 0 fprintf(stderr, "zlib(finalizer): %s\n", msg); +#endif } @@ -324,36 +379,35 @@ finalizer_warn(const char *msg) static VALUE rb_zlib_version(VALUE klass) { - VALUE str; - - str = rb_str_new2(zlibVersion()); - OBJ_TAINT(str); /* for safe */ - return str; + return rb_str_new2(zlibVersion()); } -#if SIZEOF_LONG > SIZEOF_INT +#if SIZEOF_LONG * CHAR_BIT > 32 +# define mask32(x) ((x) & 0xffffffff) +#else +# define mask32(x) (x) +#endif + +#if SIZEOF_LONG > SIZEOF_INT && !defined(HAVE_ZLIB_SIZE_T_FUNCS) static uLong checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len) { if (len > UINT_MAX) { do { - sum = func(sum, ptr, UINT_MAX); + sum = func(mask32(sum), ptr, UINT_MAX); ptr += UINT_MAX; len -= UINT_MAX; } while (len >= UINT_MAX); } - if (len > 0) sum = func(sum, ptr, (uInt)len); + if (len > 0) sum = func(mask32(sum), ptr, (uInt)len); return sum; } #else -#define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len)) +#define checksum_long(func, sum, ptr, len) (func)(mask32(sum), (ptr), (len)) #endif static VALUE -do_checksum(argc, argv, func) - int argc; - VALUE *argv; - uLong (*func)(uLong, const Bytef*, uInt); +do_checksum(int argc, VALUE *argv, checksum_func func) { VALUE str, vsum; unsigned long sum; @@ -371,7 +425,16 @@ do_checksum(argc, argv, func) } if (NIL_P(str)) { - sum = func(sum, Z_NULL, 0); + sum = func(mask32(sum), Z_NULL, 0); + } + else if (rb_obj_is_kind_of(str, rb_cIO)) { + VALUE buf; + VALUE buflen = INT2NUM(8192); + + while (!NIL_P(buf = rb_funcall(str, id_read, 1, buflen))) { + StringValue(buf); + sum = checksum_long(func, sum, (Bytef*)RSTRING_PTR(buf), RSTRING_LEN(buf)); + } } else { StringValue(str); @@ -388,8 +451,17 @@ do_checksum(argc, argv, func) * Calculates Adler-32 checksum for +string+, and returns updated value of * +adler+. If +string+ is omitted, it returns the Adler-32 initial value. If * +adler+ is omitted, it assumes that the initial value is given to +adler+. + * If +string+ is an IO instance, reads from the IO until the IO returns nil + * and returns Adler-32 of all read data. + * + * Example usage: + * + * require "zlib" + * + * data = "foo" + * puts "Adler32 checksum: #{Zlib.adler32(data).to_s(16)}" + * #=> Adler32 checksum: 2820145 * - * FIXME: expression. */ static VALUE rb_zlib_adler32(int argc, VALUE *argv, VALUE klass) @@ -403,7 +475,7 @@ rb_zlib_adler32(int argc, VALUE *argv, VALUE klass) * * call-seq: Zlib.adler32_combine(adler1, adler2, len2) * - * Combine two Adler-32 check values in to one. +alder1+ is the first Adler-32 + * Combine two Adler-32 check values in to one. +adler1+ is the first Adler-32 * value, +adler2+ is the second Adler-32 value. +len2+ is the length of the * string used to generate +adler2+. * @@ -411,7 +483,7 @@ rb_zlib_adler32(int argc, VALUE *argv, VALUE klass) static VALUE rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2) { - return ULONG2NUM( + return ULONG2NUM( adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2))); } #else @@ -421,11 +493,13 @@ rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2) /* * Document-method: Zlib.crc32 * - * call-seq: Zlib.crc32(string, adler) + * call-seq: Zlib.crc32(string, crc) * * Calculates CRC checksum for +string+, and returns updated value of +crc+. If * +string+ is omitted, it returns the CRC initial value. If +crc+ is omitted, it - * assumes that the initial value is given to +crc+. + * assumes that the initial value is given to +crc+. If +string+ is an IO instance, + * reads from the IO until the IO returns nil and returns CRC checksum of all read + * data. * * FIXME: expression. */ @@ -449,7 +523,7 @@ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass) static VALUE rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2) { - return ULONG2NUM( + return ULONG2NUM( crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2))); } #else @@ -464,7 +538,11 @@ rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2) static VALUE rb_zlib_crc_table(VALUE obj) { - const unsigned long *crctbl; +#if !defined(HAVE_TYPE_Z_CRC_T) + /* z_crc_t is defined since zlib-1.2.7. */ + typedef unsigned long z_crc_t; +#endif + const z_crc_t *crctbl; VALUE dst; int i; @@ -484,8 +562,8 @@ rb_zlib_crc_table(VALUE obj) struct zstream { unsigned long flags; VALUE buf; - long buf_filled; VALUE input; + VALUE mutex; z_stream stream; const struct zstream_funcs { int (*reset)(z_streamp); @@ -494,20 +572,31 @@ struct zstream { } *func; }; -#define ZSTREAM_FLAG_READY 0x1 -#define ZSTREAM_FLAG_IN_STREAM 0x2 -#define ZSTREAM_FLAG_FINISHED 0x4 -#define ZSTREAM_FLAG_CLOSING 0x8 -#define ZSTREAM_FLAG_UNUSED 0x10 +#define ZSTREAM_FLAG_READY (1 << 0) +#define ZSTREAM_FLAG_IN_STREAM (1 << 1) +#define ZSTREAM_FLAG_FINISHED (1 << 2) +#define ZSTREAM_FLAG_CLOSING (1 << 3) +#define ZSTREAM_FLAG_GZFILE (1 << 4) /* disallows yield from expand_buffer for + gzip*/ +#define ZSTREAM_REUSE_BUFFER (1 << 5) +#define ZSTREAM_IN_PROGRESS (1 << 6) +#define ZSTREAM_FLAG_UNUSED (1 << 7) #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY) #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY) #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED) #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING) +#define ZSTREAM_IS_GZFILE(z) ((z)->flags & ZSTREAM_FLAG_GZFILE) +#define ZSTREAM_BUF_FILLED(z) (NIL_P((z)->buf) ? 0 : RSTRING_LEN((z)->buf)) + +#define ZSTREAM_REUSE_BUFFER_P(z) ((z)->flags & ZSTREAM_REUSE_BUFFER) + +#define ZSTREAM_EXPAND_BUFFER_OK 0 /* I think that more better value should be found, but I gave up finding it. B) */ #define ZSTREAM_INITIAL_BUFSIZE 1024 +/* Allow a quick return when the thread is interrupted */ #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048 @@ -519,16 +608,25 @@ static const struct zstream_funcs inflate_funcs = { inflateReset, inflateEnd, inflate, }; +struct zstream_run_args { + struct zstream *const z; + Bytef *src; + long len; + int flush; /* stream flush value for inflate() or deflate() */ + int interrupt; /* stop processing the stream and return to ruby */ + int jump_state; /* for buffer expansion block break or exception */ + int stream_output; /* for streaming zlib processing */ +}; static voidpf zlib_mem_alloc(voidpf opaque, uInt items, uInt size) { - voidpf p = xmalloc(items * size); + voidpf p = xmalloc2(items, size); /* zlib FAQ: Valgrind (or some similar memory access checker) says that deflate is performing a conditional jump that depends on an uninitialized value. Isn't that a bug? http://www.zlib.net/zlib_faq.html#faq36 */ - VALGRIND_MAKE_MEM_DEFINED(p, items * size); + (void)VALGRIND_MAKE_MEM_DEFINED(p, items * size); return p; } @@ -543,8 +641,8 @@ zstream_init(struct zstream *z, const struct zstream_funcs *func) { z->flags = 0; z->buf = Qnil; - z->buf_filled = 0; z->input = Qnil; + z->mutex = rb_mutex_new(); z->stream.zalloc = zlib_mem_alloc; z->stream.zfree = zlib_mem_free; z->stream.opaque = Z_NULL; @@ -562,33 +660,44 @@ zstream_init(struct zstream *z, const struct zstream_funcs *func) static void zstream_expand_buffer(struct zstream *z) { - long inc; - if (NIL_P(z->buf)) { - /* I uses rb_str_new here not rb_str_buf_new because - rb_str_buf_new makes a zero-length string. */ - z->buf = rb_str_new(0, ZSTREAM_INITIAL_BUFSIZE); - z->buf_filled = 0; - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); - z->stream.avail_out = ZSTREAM_INITIAL_BUFSIZE; - RBASIC(z->buf)->klass = 0; + zstream_expand_buffer_into(z, ZSTREAM_INITIAL_BUFSIZE); return; } - if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { - /* to keep other threads from freezing */ - z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; + if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) { + long buf_filled = ZSTREAM_BUF_FILLED(z); + if (buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { + int state = 0; + + if (!ZSTREAM_REUSE_BUFFER_P(z)) { + rb_obj_reveal(z->buf, rb_cString); + } + + rb_protect(rb_yield, z->buf, &state); + + if (ZSTREAM_REUSE_BUFFER_P(z)) { + rb_str_modify(z->buf); + rb_str_set_len(z->buf, 0); + } + else { + z->buf = Qnil; + } + zstream_expand_buffer_into(z, ZSTREAM_AVAIL_OUT_STEP_MAX); + + if (state) + rb_jump_tag(state); + + return; + } + else { + zstream_expand_buffer_into(z, + ZSTREAM_AVAIL_OUT_STEP_MAX - buf_filled); + } } else { - inc = z->buf_filled / 2; - if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) { - inc = ZSTREAM_AVAIL_OUT_STEP_MIN; - } - rb_str_resize(z->buf, z->buf_filled + inc); - z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ? - (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX; + zstream_expand_buffer_non_stream(z); } - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; } static void @@ -597,34 +706,65 @@ zstream_expand_buffer_into(struct zstream *z, unsigned long size) if (NIL_P(z->buf)) { /* I uses rb_str_new here not rb_str_buf_new because rb_str_buf_new makes a zero-length string. */ - z->buf = rb_str_new(0, size); - z->buf_filled = 0; + z->buf = rb_str_buf_new(size); z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); z->stream.avail_out = MAX_UINT(size); - RBASIC(z->buf)->klass = 0; + rb_obj_hide(z->buf); } else if (z->stream.avail_out != size) { - rb_str_resize(z->buf, z->buf_filled + size); - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; + rb_str_modify_expand(z->buf, size); + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); z->stream.avail_out = MAX_UINT(size); } } +static int +zstream_expand_buffer_protect(struct zstream *z) +{ + int state = 0; + + rb_protect((VALUE (*)(VALUE))zstream_expand_buffer, (VALUE)z, &state); + + return state; +} + +static int +zstream_expand_buffer_non_stream(struct zstream *z) +{ + long inc, len = ZSTREAM_BUF_FILLED(z); + + if (rb_str_capacity(z->buf) - len >= ZSTREAM_AVAIL_OUT_STEP_MAX) { + z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; + } + else { + inc = len / 2; + if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) { + inc = ZSTREAM_AVAIL_OUT_STEP_MIN; + } + + rb_str_modify_expand(z->buf, inc); + z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ? + (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX; + } + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); + + return ZSTREAM_EXPAND_BUFFER_OK; +} + static void zstream_append_buffer(struct zstream *z, const Bytef *src, long len) { if (NIL_P(z->buf)) { z->buf = rb_str_buf_new(len); rb_str_buf_cat(z->buf, (const char*)src, len); - z->buf_filled = len; z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); z->stream.avail_out = 0; - RBASIC(z->buf)->klass = 0; + rb_obj_hide(z->buf); return; } - if (RSTRING_LEN(z->buf) < z->buf_filled + len) { - rb_str_resize(z->buf, z->buf_filled + len); + if ((long)rb_str_capacity(z->buf) < ZSTREAM_BUF_FILLED(z) + len) { + rb_str_modify_expand(z->buf, len); z->stream.avail_out = 0; } else { @@ -635,9 +775,8 @@ zstream_append_buffer(struct zstream *z, const Bytef *src, long len) z->stream.avail_out = 0; } } - memcpy(RSTRING_PTR(z->buf) + z->buf_filled, src, len); - z->buf_filled += len; - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; + rb_str_cat(z->buf, (const char *)src, len); + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); } #define zstream_append_buffer2(z,v) \ @@ -648,39 +787,65 @@ zstream_detach_buffer(struct zstream *z) { VALUE dst; + if (!ZSTREAM_IS_FINISHED(z) && !ZSTREAM_IS_GZFILE(z) && + rb_block_given_p()) { + /* prevent tiny yields mid-stream, save for next + * zstream_expand_buffer() or stream end */ + return Qnil; + } + if (NIL_P(z->buf)) { dst = rb_str_new(0, 0); } else { dst = z->buf; - rb_str_resize(dst, z->buf_filled); - RBASIC(dst)->klass = rb_cString; + if (!ZSTREAM_REUSE_BUFFER_P(z)) { + rb_obj_reveal(dst, rb_cString); + } } z->buf = Qnil; - z->buf_filled = 0; z->stream.next_out = 0; z->stream.avail_out = 0; + + if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) { + rb_yield(dst); + dst = Qnil; + } + return dst; } static VALUE -zstream_shift_buffer(struct zstream *z, long len) -{ - VALUE dst; - long buflen; - - if (z->buf_filled <= len) { - return zstream_detach_buffer(z); +zstream_shift_buffer(struct zstream *z, long len, VALUE dst) +{ + char *bufptr; + long buflen = ZSTREAM_BUF_FILLED(z); + + if (buflen <= len) { + if (NIL_P(dst) || (!ZSTREAM_IS_FINISHED(z) && !ZSTREAM_IS_GZFILE(z) && + rb_block_given_p())) { + return zstream_detach_buffer(z); + } else { + bufptr = RSTRING_PTR(z->buf); + rb_str_resize(dst, buflen); + memcpy(RSTRING_PTR(dst), bufptr, buflen); + } + buflen = 0; + } else { + bufptr = RSTRING_PTR(z->buf); + if (NIL_P(dst)) { + dst = rb_str_new(bufptr, len); + } else { + rb_str_resize(dst, len); + memcpy(RSTRING_PTR(dst), bufptr, len); + } + buflen -= len; } - - dst = rb_str_subseq(z->buf, 0, len); - RBASIC(dst)->klass = rb_cString; - z->buf_filled -= len; - memmove(RSTRING_PTR(z->buf), RSTRING_PTR(z->buf) + len, - z->buf_filled); - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; - buflen = RSTRING_LEN(z->buf) - z->buf_filled; + memmove(bufptr, bufptr + len, buflen); + rb_str_set_len(z->buf, buflen); + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); + buflen = (long)rb_str_capacity(z->buf) - ZSTREAM_BUF_FILLED(z); if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) { buflen = ZSTREAM_AVAIL_OUT_STEP_MAX; } @@ -692,13 +857,15 @@ zstream_shift_buffer(struct zstream *z, long len) static void zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len) { - if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) { - zstream_expand_buffer_into(z, len); - } + char *bufptr; + long filled; + + zstream_expand_buffer_into(z, len); - memmove(RSTRING_PTR(z->buf) + len, RSTRING_PTR(z->buf), z->buf_filled); - memmove(RSTRING_PTR(z->buf), b, len); - z->buf_filled+=len; + RSTRING_GETMEM(z->buf, bufptr, filled); + memmove(bufptr + len, bufptr, filled); + memmove(bufptr, b, len); + rb_str_set_len(z->buf, filled + len); if (z->stream.avail_out > 0) { if (len > z->stream.avail_out) len = z->stream.avail_out; z->stream.next_out+=len; @@ -709,17 +876,8 @@ zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len) static void zstream_buffer_ungetbyte(struct zstream *z, int c) { - if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) { - zstream_expand_buffer(z); - } - - memmove(RSTRING_PTR(z->buf) + 1, RSTRING_PTR(z->buf), z->buf_filled); - RSTRING_PTR(z->buf)[0] = (char)c; - z->buf_filled++; - if (z->stream.avail_out > 0) { - z->stream.next_out++; - z->stream.avail_out--; - } + Bytef cc = (Bytef)c; + zstream_buffer_ungets(z, &cc, 1); } static void @@ -730,7 +888,7 @@ zstream_append_input(struct zstream *z, const Bytef *src, long len) if (NIL_P(z->input)) { z->input = rb_str_buf_new(len); rb_str_buf_cat(z->input, (const char*)src, len); - RBASIC(z->input)->klass = 0; + rb_obj_hide(z->input); } else { rb_str_buf_cat(z->input, (const char*)src, len); @@ -744,20 +902,49 @@ zstream_append_input(struct zstream *z, const Bytef *src, long len) static void zstream_discard_input(struct zstream *z, long len) { - if (NIL_P(z->input) || RSTRING_LEN(z->input) <= len) { - z->input = Qnil; + if (NIL_P(z->input)) { } - else { - memmove(RSTRING_PTR(z->input), RSTRING_PTR(z->input) + len, - RSTRING_LEN(z->input) - len); - rb_str_resize(z->input, RSTRING_LEN(z->input) - len); + else if (RBASIC_CLASS(z->input) == 0) { + /* hidden, we created z->input and have complete control */ + char *ptr; + long oldlen, newlen; + + RSTRING_GETMEM(z->input, ptr, oldlen); + newlen = oldlen - len; + if (newlen > 0) { + memmove(ptr, ptr + len, newlen); + } + if (newlen < 0) { + newlen = 0; + } + rb_str_resize(z->input, newlen); + if (newlen == 0) { + z->input = Qnil; + } + else { + rb_str_set_len(z->input, newlen); + } + } + else { /* do not mangle user-provided data */ + if (RSTRING_LEN(z->input) <= len) { + z->input = Qnil; + } + else { + z->input = rb_str_subseq(z->input, len, + RSTRING_LEN(z->input) - len); + } } } static void zstream_reset_input(struct zstream *z) { - z->input = Qnil; + if (!NIL_P(z->input) && RBASIC_CLASS(z->input) == 0) { + rb_str_resize(z->input, 0); + } + else { + z->input = Qnil; + } } static void @@ -779,10 +966,9 @@ zstream_detach_input(struct zstream *z) } else { dst = z->input; - RBASIC(dst)->klass = rb_cString; + rb_obj_reveal(dst, rb_cString); } z->input = Qnil; - RBASIC(dst)->klass = rb_cString; return dst; } @@ -797,7 +983,6 @@ zstream_reset(struct zstream *z) } z->flags = ZSTREAM_FLAG_READY; z->buf = Qnil; - z->buf_filled = 0; z->stream.next_out = 0; z->stream.avail_out = 0; zstream_reset_input(z); @@ -826,12 +1011,138 @@ zstream_end(struct zstream *z) return Qnil; } +static VALUE +zstream_ensure_end(VALUE v) +{ + return zstream_end((struct zstream *)v); +} + +static void * +zstream_run_once(void *_arguments) +{ + struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments; + struct zstream *z = arguments->z; + + uintptr_t error = z->func->run(&z->stream, arguments->flush); + + return (void*)error; +} + +/* + * There is no safe way to interrupt z->run->func(). + * async-signal-safe + */ static void -zstream_run(struct zstream *z, Bytef *src, long len, int flush) +zstream_unblock_func(void *ptr) { + struct zstream_run_args *args = (struct zstream_run_args *)ptr; + + args->interrupt = 1; +} + +#ifndef RB_NOGVL_OFFLOAD_SAFE +// Default to no-op if it's not defined: +#define RB_NOGVL_OFFLOAD_SAFE 0 +#endif + +static VALUE +zstream_run_once_begin(VALUE _arguments) +{ + struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments; + struct zstream *z = arguments->z; + + rb_str_locktmp(z->buf); + +#ifndef RB_NOGVL_UBF_ASYNC_SAFE + return (VALUE)rb_thread_call_without_gvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments); +#else + return (VALUE)rb_nogvl(zstream_run_once, (void *)arguments, zstream_unblock_func, (void *)arguments, RB_NOGVL_UBF_ASYNC_SAFE | RB_NOGVL_OFFLOAD_SAFE); +#endif +} + +static VALUE +zstream_run_once_ensure(VALUE _arguments) +{ + struct zstream_run_args *arguments = (struct zstream_run_args *)_arguments; + struct zstream *z = arguments->z; + + rb_str_unlocktmp(z->buf); + + return Qnil; +} + +static int +zstream_run_func(struct zstream_run_args *args) +{ + struct zstream *z = args->z; + int state; uInt n; + + int err = Z_OK; + while (!args->interrupt) { + n = z->stream.avail_out; + + err = (int)(VALUE)rb_ensure(zstream_run_once_begin, (VALUE)args, zstream_run_once_ensure, (VALUE)args); + + rb_str_set_len(z->buf, ZSTREAM_BUF_FILLED(z) + (n - z->stream.avail_out)); + + if (err == Z_STREAM_END) { + z->flags &= ~ZSTREAM_FLAG_IN_STREAM; + z->flags |= ZSTREAM_FLAG_FINISHED; + break; + } + + if (err != Z_OK && err != Z_BUF_ERROR) { + break; + } + + if (z->stream.avail_out > 0) { + z->flags |= ZSTREAM_FLAG_IN_STREAM; + break; + } + + if (z->stream.avail_in == 0 && z->func == &inflate_funcs) { + /* break here because inflate() return Z_BUF_ERROR when avail_in == 0. */ + /* but deflate() could be called with avail_in == 0 (there's hidden buffer + in zstream->state) */ + z->flags |= ZSTREAM_FLAG_IN_STREAM; + break; + } + + if (args->stream_output) { + state = zstream_expand_buffer_protect(z); + } + else { + state = zstream_expand_buffer_non_stream(z); + } + + if (state) { + err = Z_OK; /* buffer expanded but stream processing was stopped */ + args->jump_state = state; + break; + } + } + + return err; +} + +static VALUE +zstream_run_try(VALUE value_arg) +{ + struct zstream_run_args *args = (struct zstream_run_args *)value_arg; + struct zstream *z = args->z; + Bytef *src = args->src; + long len = args->len; + int flush = args->flush; + int err; - volatile VALUE guard = Qnil; + VALUE old_input = Qnil; + + /* Cannot start zstream while it is in progress. */ + if (z->flags & ZSTREAM_IN_PROGRESS) { + rb_raise(cInProgressError, "zlib stream is in progress"); + } + z->flags |= ZSTREAM_IN_PROGRESS; if (NIL_P(z->input) && len == 0) { z->stream.next_in = (Bytef*)""; @@ -839,62 +1150,102 @@ zstream_run(struct zstream *z, Bytef *src, long len, int flush) } else { zstream_append_input(z, src, len); - z->stream.next_in = (Bytef*)RSTRING_PTR(z->input); - z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input)); /* keep reference to `z->input' so as not to be garbage collected after zstream_reset_input() and prevent `z->stream.next_in' from dangling. */ - guard = z->input; + old_input = zstream_detach_input(z); + rb_obj_hide(old_input); /* for GVL release and later recycle */ + z->stream.next_in = (Bytef*)RSTRING_PTR(old_input); + z->stream.avail_in = MAX_UINT(RSTRING_LEN(old_input)); } if (z->stream.avail_out == 0) { zstream_expand_buffer(z); } - for (;;) { - /* VC allocates err and guard to same address. accessing err and guard - in same scope prevents it. */ - RB_GC_GUARD(guard); - n = z->stream.avail_out; - err = z->func->run(&z->stream, flush); - z->buf_filled += n - z->stream.avail_out; - rb_thread_schedule(); +loop: + err = zstream_run_func(args); - if (err == Z_STREAM_END) { - z->flags &= ~ZSTREAM_FLAG_IN_STREAM; - z->flags |= ZSTREAM_FLAG_FINISHED; - break; + /* retry if no exception is thrown */ + if (err == Z_OK && args->interrupt) { + args->interrupt = 0; + + /* Retry only if both avail_in > 0 (more input to process) and avail_out > 0 + * (output buffer has space). If avail_out == 0, the buffer is full and should + * be consumed by the caller first. If avail_in == 0, there's nothing more to process. */ + if (z->stream.avail_in > 0 && z->stream.avail_out > 0) { + goto loop; + } + } + + if (flush != Z_FINISH && err == Z_BUF_ERROR && z->stream.avail_out > 0) { + z->flags |= ZSTREAM_FLAG_IN_STREAM; + } + + zstream_reset_input(z); + + if (err != Z_OK && err != Z_STREAM_END) { + if (z->stream.avail_in > 0) { + zstream_append_input(z, z->stream.next_in, z->stream.avail_in); } - if (err != Z_OK) { - if (flush != Z_FINISH && err == Z_BUF_ERROR - && z->stream.avail_out > 0) { - z->flags |= ZSTREAM_FLAG_IN_STREAM; - break; - } - zstream_reset_input(z); - if (z->stream.avail_in > 0) { - zstream_append_input(z, z->stream.next_in, z->stream.avail_in); + if (err == Z_NEED_DICT) { + VALUE self = (VALUE)z->stream.opaque; + if (self) { + VALUE dicts = rb_ivar_get(self, id_dictionaries); + VALUE dict = rb_hash_aref(dicts, rb_uint2inum(z->stream.adler)); + if (!NIL_P(dict)) { + rb_inflate_set_dictionary(self, dict); + goto loop; + } } - raise_zlib_error(err, z->stream.msg); - } - if (z->stream.avail_out > 0) { - z->flags |= ZSTREAM_FLAG_IN_STREAM; - break; } - zstream_expand_buffer(z); + raise_zlib_error(err, z->stream.msg); } - zstream_reset_input(z); if (z->stream.avail_in > 0) { zstream_append_input(z, z->stream.next_in, z->stream.avail_in); - guard = Qnil; /* prevent tail call to make guard effective */ } + if (!NIL_P(old_input)) { + rb_str_resize(old_input, 0); + } + + return Qnil; +} + +static VALUE +zstream_run_ensure(VALUE value_arg) +{ + struct zstream_run_args *args = (struct zstream_run_args *)value_arg; + struct zstream *z = args->z; + + /* Remove ZSTREAM_IN_PROGRESS flag to signal that this zstream is not in use. */ + z->flags &= ~ZSTREAM_IN_PROGRESS; + + return Qnil; +} + +static void +zstream_run(struct zstream *z, Bytef *src, long len, int flush) +{ + struct zstream_run_args args = { + .z = z, + .src = src, + .len = len, + .flush = flush, + .interrupt = 0, + .jump_state = 0, + .stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(), + }; + + rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args); + if (args.jump_state) + rb_jump_tag(args.jump_state); } static VALUE zstream_sync(struct zstream *z, Bytef *src, long len) { - VALUE rest; + /* VALUE rest; */ int err; if (!NIL_P(z->input)) { @@ -909,7 +1260,7 @@ zstream_sync(struct zstream *z, Bytef *src, long len) } zstream_reset_input(z); if (err != Z_DATA_ERROR) { - rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); + /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */ raise_zlib_error(err, z->stream.msg); } } @@ -924,17 +1275,19 @@ zstream_sync(struct zstream *z, Bytef *src, long len) return Qtrue; } if (err != Z_DATA_ERROR) { - rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); + /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */ raise_zlib_error(err, z->stream.msg); } return Qfalse; } static void -zstream_mark(struct zstream *z) +zstream_mark(void *p) { + struct zstream *z = p; rb_gc_mark(z->buf); rb_gc_mark(z->input); + rb_gc_mark(z->mutex); } static void @@ -948,23 +1301,38 @@ zstream_finalize(struct zstream *z) } static void -zstream_free(struct zstream *z) +zstream_free(void *p) { + struct zstream *z = p; + if (ZSTREAM_IS_READY(z)) { zstream_finalize(z); } xfree(z); } +static size_t +zstream_memsize(const void *p) +{ + /* n.b. this does not track memory managed via zalloc/zfree callbacks */ + return sizeof(struct zstream); +} + +static const rb_data_type_t zstream_data_type = { + "zstream", + { zstream_mark, zstream_free, zstream_memsize, }, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY +}; + static VALUE zstream_new(VALUE klass, const struct zstream_funcs *funcs) { VALUE obj; struct zstream *z; - obj = Data_Make_Struct(klass, struct zstream, - zstream_mark, zstream_free, z); + obj = TypedData_Make_Struct(klass, struct zstream, &zstream_data_type, z); zstream_init(z, funcs); + z->stream.opaque = (voidpf)obj; return obj; } @@ -976,7 +1344,7 @@ get_zstream(VALUE obj) { struct zstream *z; - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); if (!ZSTREAM_IS_READY(z)) { rb_raise(cZError, "stream is not ready"); } @@ -1070,23 +1438,28 @@ rb_zstream_reset(VALUE obj) } /* - * Finishes the stream and flushes output buffer. See Zlib::Deflate#finish and - * Zlib::Inflate#finish for details of this behavior. + * call-seq: + * finish -> String + * finish { |chunk| ... } -> nil + * + * Finishes the stream and flushes output buffer. If a block is given each + * chunk is yielded to the block until the input buffer has been flushed to + * the output buffer. */ static VALUE rb_zstream_finish(VALUE obj) { struct zstream *z = get_zstream(obj); - VALUE dst; zstream_run(z, (Bytef*)"", 0, Z_FINISH); - dst = zstream_detach_buffer(z); - OBJ_INFECT(dst, obj); - return dst; + return zstream_detach_buffer(z); } /* + * call-seq: + * flush_next_in -> input + * * Flushes input buffer and returns all data in that buffer. */ static VALUE @@ -1095,25 +1468,28 @@ rb_zstream_flush_next_in(VALUE obj) struct zstream *z; VALUE dst; - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); dst = zstream_detach_input(z); - OBJ_INFECT(dst, obj); return dst; } /* - * Flushes output buffer and returns all data in that buffer. + * call-seq: + * flush_next_out -> String + * flush_next_out { |chunk| ... } -> nil + * + * Flushes output buffer and returns all data in that buffer. If a block is + * given each chunk is yielded to the block until the current output buffer + * has been flushed. */ static VALUE rb_zstream_flush_next_out(VALUE obj) { struct zstream *z; - VALUE dst; - Data_Get_Struct(obj, struct zstream, z); - dst = zstream_detach_buffer(z); - OBJ_INFECT(dst, obj); - return dst; + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); + + return zstream_detach_buffer(z); } /* @@ -1124,7 +1500,7 @@ static VALUE rb_zstream_avail_out(VALUE obj) { struct zstream *z; - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); return rb_uint2inum(z->stream.avail_out); } @@ -1139,7 +1515,6 @@ rb_zstream_set_avail_out(VALUE obj, VALUE size) { struct zstream *z = get_zstream(obj); - Check_Type(size, T_FIXNUM); zstream_expand_buffer_into(z, FIX2INT(size)); return size; } @@ -1151,7 +1526,7 @@ static VALUE rb_zstream_avail_in(VALUE obj) { struct zstream *z; - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input))); } @@ -1174,7 +1549,7 @@ rb_zstream_total_out(VALUE obj) } /* - * Guesses the type of the data which have been inputed into the stream. The + * Guesses the type of the data which have been inputted into the stream. The * returned value is either <tt>BINARY</tt>, <tt>ASCII</tt>, or * <tt>UNKNOWN</tt>. */ @@ -1190,7 +1565,7 @@ rb_zstream_data_type(VALUE obj) static VALUE rb_zstream_adler(VALUE obj) { - return rb_uint2inum(get_zstream(obj)->stream.adler); + return rb_uint2inum(get_zstream(obj)->stream.adler); } /* @@ -1209,7 +1584,7 @@ static VALUE rb_zstream_closed_p(VALUE obj) { struct zstream *z; - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue; } @@ -1225,7 +1600,7 @@ rb_zstream_closed_p(VALUE obj) #define FIXNUMARG(val, ifnil) \ (NIL_P((val)) ? (ifnil) \ - : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val)))) + : (FIX2INT((val)))) #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION) #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS) @@ -1243,60 +1618,75 @@ rb_deflate_s_allocate(VALUE klass) /* * Document-method: Zlib::Deflate.new * - * call-seq: Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil) + * call-seq: + * Zlib::Deflate.new(level=DEFAULT_COMPRESSION, window_bits=MAX_WBITS, mem_level=DEF_MEM_LEVEL, strategy=DEFAULT_STRATEGY) * - * == Arguments + * Creates a new deflate stream for compression. If a given argument is nil, + * the default value of that argument is used. * - * +level+:: - * An Integer compression level between - * BEST_SPEED and BEST_COMPRESSION - * +windowBits+:: - * An Integer for the windowBits size. Should be - * in the range 8..15, larger values of this parameter - * result in better at the expense of memory usage. - * +memlevel+:: - * Specifies how much memory should be allocated for - * the internal compression state. - * Between DEF_MEM_LEVEL and MAX_MEM_LEVEL - * +strategy+:: - * A parameter to tune the compression algorithm. Use the - * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a - * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no - * string match). + * The +level+ sets the compression level for the deflate stream between 0 (no + * compression) and 9 (best compression). The following constants have been + * defined to make code more readable: * - * == Description + * * Zlib::DEFAULT_COMPRESSION + * * Zlib::NO_COMPRESSION + * * Zlib::BEST_SPEED + * * Zlib::BEST_COMPRESSION * - * Creates a new deflate stream for compression. See zlib.h for details of - * each argument. If an argument is nil, the default value of that argument is - * used. + * See http://www.zlib.net/manual.html#Constants for further information. * + * The +window_bits+ sets the size of the history buffer and should be between + * 8 and 15. Larger values of this parameter result in better compression at + * the expense of memory usage. * - * == examples + * The +mem_level+ specifies how much memory should be allocated for the + * internal compression state. 1 uses minimum memory but is slow and reduces + * compression ratio while 9 uses maximum memory for optimal speed. The + * default value is 8. Two constants are defined: * - * === basic + * * Zlib::DEF_MEM_LEVEL + * * Zlib::MAX_MEM_LEVEL * - * f = File.new("compressed.file","w+") - * #=> #<File:compressed.file> - * f << Zlib::Deflate.new().deflate(File.read("big.file")) - * #=> #<File:compressed.file> - * f.close - * #=> nil + * The +strategy+ sets the deflate compression strategy. The following + * strategies are available: * - * === a little more robust + * Zlib::DEFAULT_STRATEGY:: For normal data + * Zlib::FILTERED:: For data produced by a filter or predictor + * Zlib::FIXED:: Prevents dynamic Huffman codes + * Zlib::HUFFMAN_ONLY:: Prevents string matching + * Zlib::RLE:: Designed for better compression of PNG image data * - * compressed_file = File.open("compressed.file", "w+") - * #=> #<File:compressed.file> - * zd = Zlib::Deflate.new(Zlib::BEST_COMPRESSION, 15, Zlib::MAX_MEM_LEVEL, Zlib::HUFFMAN_ONLY) - * #=> #<Zlib::Deflate:0x000000008610a0> - * compressed_file << zd.deflate(File.read("big.file")) - * #=> "\xD4z\xC6\xDE\b\xA1K\x1Ej\x8A ..." - * compressed_file.close - * #=> nil - * zd.close - * #=> nil + * See the constants for further description. * - * (while this example will work, for best optimization the flags need to be reviewed for your specific function) + * == Examples * + * === Basic + * + * open "compressed.file", "w+" do |io| + * io << Zlib::Deflate.new.deflate(File.read("big.file")) + * end + * + * === Custom compression + * + * open "compressed.file", "w+" do |compressed_io| + * deflate = Zlib::Deflate.new(Zlib::BEST_COMPRESSION, + * Zlib::MAX_WBITS, + * Zlib::MAX_MEM_LEVEL, + * Zlib::HUFFMAN_ONLY) + * + * begin + * open "big.file" do |big_io| + * until big_io.eof? do + * compressed_io << zd.deflate(big_io.read(16384)) + * end + * end + * ensure + * deflate.close + * end + * end + * + * While this example will work, for best optimization review the flags for + * your specific time, memory usage and output space requirements. */ static VALUE rb_deflate_initialize(int argc, VALUE *argv, VALUE obj) @@ -1306,7 +1696,7 @@ rb_deflate_initialize(int argc, VALUE *argv, VALUE obj) int err; rb_scan_args(argc, argv, "04", &level, &wbits, &memlevel, &strategy); - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); err = deflateInit2(&z->stream, ARG_LEVEL(level), Z_DEFLATED, ARG_WBITS(wbits), ARG_MEMLEVEL(memlevel), @@ -1330,16 +1720,16 @@ rb_deflate_init_copy(VALUE self, VALUE orig) struct zstream *z1, *z2; int err; - Data_Get_Struct(self, struct zstream, z1); + TypedData_Get_Struct(self, struct zstream, &zstream_data_type, z1); z2 = get_zstream(orig); + if (z1 == z2) return self; err = deflateCopy(&z1->stream, &z2->stream); if (err != Z_OK) { raise_zlib_error(err, 0); } z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input); z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf); - z1->buf_filled = z2->buf_filled; z1->flags = z2->flags; return self; @@ -1358,19 +1748,19 @@ deflate_run(VALUE args) /* * Document-method: Zlib::Deflate.deflate * - * call-seq: Zlib.deflate(string[, level]) - * Zlib::Deflate.deflate(string[, level]) + * call-seq: + * Zlib.deflate(string[, level]) + * Zlib::Deflate.deflate(string[, level]) * * Compresses the given +string+. Valid values of level are - * <tt>NO_COMPRESSION</tt>, <tt>BEST_SPEED</tt>, - * <tt>BEST_COMPRESSION</tt>, <tt>DEFAULT_COMPRESSION</tt>, and an - * integer from 0 to 9 (the default is 6). + * Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION, + * Zlib::DEFAULT_COMPRESSION, or an integer from 0 to 9. * * This method is almost equivalent to the following code: * * def deflate(string, level) * z = Zlib::Deflate.new(level) - * dst = z.deflate(string, Zlib::NO_FLUSH) + * dst = z.deflate(string, Zlib::FINISH) * z.close * dst * end @@ -1398,9 +1788,8 @@ rb_deflate_s_deflate(int argc, VALUE *argv, VALUE klass) args[0] = (VALUE)&z; args[1] = src; - dst = rb_ensure(deflate_run, (VALUE)args, zstream_end, (VALUE)&z); + dst = rb_ensure(deflate_run, (VALUE)args, zstream_ensure_end, (VALUE)&z); - OBJ_INFECT(dst, src); return dst; } @@ -1417,56 +1806,61 @@ do_deflate(struct zstream *z, VALUE src, int flush) } } +struct rb_zlib_deflate_arguments { + struct zstream *z; + VALUE src; + int flush; +}; + +static VALUE +rb_deflate_deflate_body(VALUE args) +{ + struct rb_zlib_deflate_arguments *arguments = (struct rb_zlib_deflate_arguments *)args; + + do_deflate(arguments->z, arguments->src, arguments->flush); + + return zstream_detach_buffer(arguments->z); +} + /* - * Document-method: Zlib.deflate - * - * call-seq: deflate(string[, flush]) - * - * == Arguments - * - * +string+:: - * String - * - * +flush+:: - * Integer representing a flush code. Either NO_FLUSH, - * SYNC_FLUSH, FULL_FLUSH, or FINISH. See zlib.h for details. - * Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to - * decide how much data to accumulate before producing output, in order to - * maximize compression. + * Document-method: Zlib::Deflate#deflate * - * == Description + * call-seq: + * z.deflate(string, flush = Zlib::NO_FLUSH) -> String + * z.deflate(string, flush = Zlib::NO_FLUSH) { |chunk| ... } -> nil * * Inputs +string+ into the deflate stream and returns the output from the * stream. On calling this method, both the input and the output buffers of - * the stream are flushed. - * - * If +string+ is nil, this method finishes the + * the stream are flushed. If +string+ is nil, this method finishes the * stream, just like Zlib::ZStream#finish. * - * == Usage + * If a block is given consecutive deflated chunks from the +string+ are + * yielded to the block and +nil+ is returned. + * + * The +flush+ parameter specifies the flush mode. The following constants + * may be used: * - * comp = Zlib.deflate(File.read("big.file")) - * or - * comp = Zlib.deflate(File.read("big.file"), Zlib::FULL_FLUSH) + * Zlib::NO_FLUSH:: The default + * Zlib::SYNC_FLUSH:: Flushes the output to a byte boundary + * Zlib::FULL_FLUSH:: SYNC_FLUSH + resets the compression state + * Zlib::FINISH:: Pending input is processed, pending output is flushed. + * + * See the constants for further description. * */ static VALUE rb_deflate_deflate(int argc, VALUE *argv, VALUE obj) { struct zstream *z = get_zstream(obj); - VALUE src, flush, dst; - + VALUE src, flush; rb_scan_args(argc, argv, "11", &src, &flush); - OBJ_INFECT(obj, src); - do_deflate(z, src, ARG_FLUSH(flush)); - dst = zstream_detach_buffer(z); + struct rb_zlib_deflate_arguments arguments = {z, src, ARG_FLUSH(flush)}; - OBJ_INFECT(dst, obj); - return dst; + return rb_mutex_synchronize(z->mutex, rb_deflate_deflate_body, (VALUE)&arguments); } /* - * Document-method: Zlib::Deflate.<< + * Document-method: Zlib::Deflate#<< * * call-seq: << string * @@ -1477,7 +1871,6 @@ rb_deflate_deflate(int argc, VALUE *argv, VALUE obj) static VALUE rb_deflate_addstr(VALUE obj, VALUE src) { - OBJ_INFECT(obj, src); do_deflate(get_zstream(obj), src, Z_NO_FLUSH); return obj; } @@ -1485,20 +1878,23 @@ rb_deflate_addstr(VALUE obj, VALUE src) /* * Document-method: Zlib::Deflate#flush * - * call-seq: flush(flush) - * - * This method is equivalent to <tt>deflate('', flush)</tt>. If flush is omitted, - * <tt>SYNC_FLUSH</tt> is used as flush. This method is just provided - * to improve the readability of your Ruby program. + * call-seq: + * flush(flush = Zlib::SYNC_FLUSH) -> String + * flush(flush = Zlib::SYNC_FLUSH) { |chunk| ... } -> nil * - * Please visit your zlib.h for a deeper detail on NO_FLUSH, SYNC_FLUSH, FULL_FLUSH, and FINISH + * This method is equivalent to <tt>deflate('', flush)</tt>. This method is + * just provided to improve the readability of your Ruby program. If a block + * is given chunks of deflate output are yielded to the block until the buffer + * is flushed. * + * See Zlib::Deflate#deflate for detail on the +flush+ constants NO_FLUSH, + * SYNC_FLUSH, FULL_FLUSH and FINISH. */ static VALUE rb_deflate_flush(int argc, VALUE *argv, VALUE obj) { struct zstream *z = get_zstream(obj); - VALUE v_flush, dst; + VALUE v_flush; int flush; rb_scan_args(argc, argv, "01", &v_flush); @@ -1506,10 +1902,8 @@ rb_deflate_flush(int argc, VALUE *argv, VALUE obj) if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */ zstream_run(z, (Bytef*)"", 0, flush); } - dst = zstream_detach_buffer(z); - OBJ_INFECT(dst, obj); - return dst; + return zstream_detach_buffer(z); } /* @@ -1517,18 +1911,11 @@ rb_deflate_flush(int argc, VALUE *argv, VALUE obj) * * call-seq: params(level, strategy) * - * Changes the parameters of the deflate stream. See zlib.h for details. The - * output from the stream by changing the params is preserved in output - * buffer. + * Changes the parameters of the deflate stream to allow changes between + * different types of data that require different types of compression. Any + * unprocessed data is flushed before changing the params. * - * +level+:: - * An Integer compression level between - * BEST_SPEED and BEST_COMPRESSION - * +strategy+:: - * A parameter to tune the compression algorithm. Use the - * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a - * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no - * string match). + * See Zlib::Deflate.new for a description of +level+ and +strategy+. * */ static VALUE @@ -1538,23 +1925,26 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) int level, strategy; int err; uInt n; + long filled; level = ARG_LEVEL(v_level); strategy = ARG_STRATEGY(v_strategy); n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); - z->buf_filled += n - z->stream.avail_out; + filled = n - z->stream.avail_out; while (err == Z_BUF_ERROR) { rb_warning("deflateParams() returned Z_BUF_ERROR"); zstream_expand_buffer(z); + rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled); n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); - z->buf_filled += n - z->stream.avail_out; + filled = n - z->stream.avail_out; } if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } + rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled); return Qnil; } @@ -1580,7 +1970,6 @@ rb_deflate_set_dictionary(VALUE obj, VALUE dic) VALUE src = dic; int err; - OBJ_INFECT(obj, dic); StringValue(src); err = deflateSetDictionary(&z->stream, (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); @@ -1602,52 +1991,57 @@ rb_deflate_set_dictionary(VALUE obj, VALUE dic) * dup) itself. */ - - static VALUE rb_inflate_s_allocate(VALUE klass) { - return zstream_inflate_new(klass); + VALUE inflate = zstream_inflate_new(klass); + rb_ivar_set(inflate, id_dictionaries, rb_hash_new()); + return inflate; } /* * Document-method: Zlib::Inflate.new * - * call-seq: Zlib::Inflate.new(window_bits) + * call-seq: + * Zlib::Inflate.new(window_bits = Zlib::MAX_WBITS) * - * == Arguments + * Creates a new inflate stream for decompression. +window_bits+ sets the + * size of the history buffer and can have the following values: * - * +windowBits+:: - * An Integer for the windowBits size. Should be - * in the range 8..15, larger values of this parameter - * result in better at the expense of memory usage. + * 0:: + * Have inflate use the window size from the zlib header of the compressed + * stream. * - * == Description + * (8..15):: + * Overrides the window size of the inflate header in the compressed stream. + * The window size must be greater than or equal to the window size of the + * compressed stream. * - * Creates a new inflate stream for decompression. See zlib.h for details - * of the argument. If +window_bits+ is +nil+, the default value is used. - * - * == Example + * Greater than 15:: + * Add 32 to window_bits to enable zlib and gzip decoding with automatic + * header detection, or add 16 to decode only the gzip format (a + * Zlib::DataError will be raised for a non-gzip stream). * - * cf = File.open("compressed.file") - * ucf = File.open("uncompressed.file", "w+") - * zi = Zlib::Inflate.new(Zlib::MAX_WBITS) + * (-8..-15):: + * Enables raw deflate mode which will not generate a check value, and will + * not look for any check values for comparison at the end of the stream. * - * ucf << zi.inflate(cf.read) + * This is for use with other formats that use the deflate compressed data + * format such as zip which provide their own check values. * - * ucf.close - * zi.close - * cf.close + * == Example * - * or + * open "compressed.file" do |compressed_io| + * zi = Zlib::Inflate.new(Zlib::MAX_WBITS + 32) * - * File.open("compressed.file") {|cf| - * zi = Zlib::Inflate.new - * File.open("uncompressed.file", "w+") {|ucf| - * ucf << zi.inflate(cf.read) - * } - * zi.close - * } + * begin + * open "uncompressed.file", "w+" do |uncompressed_io| + * uncompressed_io << zi.inflate(compressed_io.read) + * end + * ensure + * zi.close + * end + * end * */ static VALUE @@ -1658,7 +2052,7 @@ rb_inflate_initialize(int argc, VALUE *argv, VALUE obj) int err; rb_scan_args(argc, argv, "01", &wbits); - Data_Get_Struct(obj, struct zstream, z); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); err = inflateInit2(&z->stream, ARG_WBITS(wbits)); if (err != Z_OK) { @@ -1681,9 +2075,11 @@ inflate_run(VALUE args) } /* - * Document-method: Zlib::Inflate.inflate + * Document-method: Zlib::inflate * - * call-seq: Zlib::Inflate.inflate(string) + * call-seq: + * Zlib.inflate(string) + * Zlib::Inflate.inflate(string) * * Decompresses +string+. Raises a Zlib::NeedDict exception if a preset * dictionary is needed for decompression. @@ -1718,9 +2114,8 @@ rb_inflate_s_inflate(VALUE obj, VALUE src) args[0] = (VALUE)&z; args[1] = src; - dst = rb_ensure(inflate_run, (VALUE)args, zstream_end, (VALUE)&z); + dst = rb_ensure(inflate_run, (VALUE)args, zstream_ensure_end, (VALUE)&z); - OBJ_INFECT(dst, src); return dst; } @@ -1732,34 +2127,72 @@ do_inflate(struct zstream *z, VALUE src) return; } StringValue(src); - if (RSTRING_LEN(src) > 0) { /* prevent Z_BUF_ERROR */ + if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */ zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH); } } -/* - * Document-method: Zlib::Inflate#inflate +/* Document-method: Zlib::Inflate#add_dictionary * - * call-seq: inflate(string) - * - * Inputs +string+ into the inflate stream and returns the output from the - * stream. Calling this method, both the input and the output buffer of the - * stream are flushed. If string is +nil+, this method finishes the stream, - * just like Zlib::ZStream#finish. - * - * Raises a Zlib::NeedDict exception if a preset dictionary is needed to - * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then - * call this method again with an empty string. (<i>???</i>) + * call-seq: add_dictionary(string) * - * See also Zlib::Inflate.new + * Provide the inflate stream with a dictionary that may be required in the + * future. Multiple dictionaries may be provided. The inflate stream will + * automatically choose the correct user-provided dictionary based on the + * stream's required dictionary. */ static VALUE -rb_inflate_inflate(VALUE obj, VALUE src) +rb_inflate_add_dictionary(VALUE obj, VALUE dictionary) { - struct zstream *z = get_zstream(obj); - VALUE dst; + VALUE dictionaries = rb_ivar_get(obj, id_dictionaries); + VALUE checksum = do_checksum(1, &dictionary, adler32); + + rb_hash_aset(dictionaries, checksum, dictionary); - OBJ_INFECT(obj, src); + return obj; +} + +struct rb_zlib_inflate_arguments { + struct zstream *z; + int argc; + VALUE *argv; +}; + +static VALUE +rb_inflate_inflate_body(VALUE _arguments) +{ + struct rb_zlib_inflate_arguments *arguments = (struct rb_zlib_inflate_arguments*)_arguments; + struct zstream *z = arguments->z; + int argc = arguments->argc; + VALUE *argv = arguments->argv; + VALUE dst, src, opts, buffer = Qnil; + + if (OPTHASH_GIVEN_P(opts)) { + VALUE buf; + rb_get_kwargs(opts, &id_buffer, 0, 1, &buf); + if (buf != Qundef && buf != Qnil) { + buffer = StringValue(buf); + } + } + if (buffer != Qnil) { + if (!(ZSTREAM_REUSE_BUFFER_P(z) && z->buf == buffer)) { + long len = RSTRING_LEN(buffer); + if (len >= ZSTREAM_AVAIL_OUT_STEP_MAX) { + rb_str_modify(buffer); + } + else { + len = ZSTREAM_AVAIL_OUT_STEP_MAX - len; + rb_str_modify_expand(buffer, len); + } + rb_str_set_len(buffer, 0); + z->flags |= ZSTREAM_REUSE_BUFFER; + z->buf = buffer; + } + } else if (ZSTREAM_REUSE_BUFFER_P(z)) { + z->flags &= ~ZSTREAM_REUSE_BUFFER; + z->buf = Qnil; + } + rb_scan_args(argc, argv, "10", &src); if (ZSTREAM_IS_FINISHED(z)) { if (NIL_P(src)) { @@ -1768,7 +2201,11 @@ rb_inflate_inflate(VALUE obj, VALUE src) else { StringValue(src); zstream_append_buffer2(z, src); - dst = rb_str_new(0, 0); + if (ZSTREAM_REUSE_BUFFER_P(z)) { + dst = rb_str_resize(buffer, 0); + } else { + dst = rb_str_new(0, 0); + } } } else { @@ -1779,11 +2216,64 @@ rb_inflate_inflate(VALUE obj, VALUE src) } } - OBJ_INFECT(dst, obj); return dst; } /* + * Document-method: Zlib::Inflate#inflate + * + * call-seq: + * inflate(deflate_string, buffer: nil) -> String + * inflate(deflate_string, buffer: nil) { |chunk| ... } -> nil + * + * Inputs +deflate_string+ into the inflate stream and returns the output from + * the stream. Calling this method, both the input and the output buffer of + * the stream are flushed. If string is +nil+, this method finishes the + * stream, just like Zlib::ZStream#finish. + * + * If a block is given consecutive inflated chunks from the +deflate_string+ + * are yielded to the block and +nil+ is returned. + * + * If a :buffer keyword argument is given and not nil: + * + * * The :buffer keyword should be a String, and will used as the output buffer. + * Using this option can reuse the memory required during inflation. + * * When not passing a block, the return value will be the same object as the + * :buffer keyword argument. + * * When passing a block, the yielded chunks will be the same value as the + * :buffer keyword argument. + * + * Raises a Zlib::NeedDict exception if a preset dictionary is needed to + * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then + * call this method again with an empty string to flush the stream: + * + * inflater = Zlib::Inflate.new + * + * begin + * out = inflater.inflate compressed + * rescue Zlib::NeedDict + * # ensure the dictionary matches the stream's required dictionary + * raise unless inflater.adler == Zlib.adler32(dictionary) + * + * inflater.set_dictionary dictionary + * inflater.inflate '' + * end + * + * # ... + * + * inflater.close + * + * See also Zlib::Inflate.new + */ +static VALUE +rb_inflate_inflate(int argc, VALUE* argv, VALUE obj) +{ + struct zstream *z = get_zstream(obj); + struct rb_zlib_inflate_arguments arguments = {z, argc, argv}; + return rb_mutex_synchronize(z->mutex, rb_inflate_inflate_body, (VALUE)&arguments); +} + +/* * call-seq: << string * * Inputs +string+ into the inflate stream just like Zlib::Inflate#inflate, but @@ -1795,8 +2285,6 @@ rb_inflate_addstr(VALUE obj, VALUE src) { struct zstream *z = get_zstream(obj); - OBJ_INFECT(obj, src); - if (ZSTREAM_IS_FINISHED(z)) { if (!NIL_P(src)) { StringValue(src); @@ -1826,7 +2314,6 @@ rb_inflate_sync(VALUE obj, VALUE src) { struct zstream *z = get_zstream(obj); - OBJ_INFECT(obj, src); StringValue(src); return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src)); } @@ -1868,7 +2355,6 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic) VALUE src = dic; int err; - OBJ_INFECT(obj, dic); StringValue(src); err = inflateSetDictionary(&z->stream, (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); @@ -1925,7 +2411,7 @@ rb_inflate_set_dictionary(VALUE obj, VALUE dic) #define OS_CODE OS_UNIX #endif -static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path, id_input; +static ID id_write, id_readpartial, id_flush, id_seek, id_close, id_path, id_input; static VALUE cGzError, cNoFooter, cCRCError, cLengthError; @@ -1936,20 +2422,19 @@ struct gzfile { struct zstream z; VALUE io; int level; - time_t mtime; /* for header */ int os_code; /* for header */ + time_t mtime; /* for header */ VALUE orig_name; /* for header; must be a String */ VALUE comment; /* for header; must be a String */ unsigned long crc; + int ecflags; int lineno; long ungetc; void (*end)(struct gzfile *); rb_encoding *enc; rb_encoding *enc2; rb_econv_t *ec; - int ecflags; VALUE ecopts; - char *cbuf; VALUE path; }; #define GZFILE_CBUF_CAPA 10 @@ -1957,16 +2442,28 @@ struct gzfile { #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1) #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2) +#define GZFILE_FLAG_MTIME_IS_SET (ZSTREAM_FLAG_UNUSED << 3) #define GZFILE_IS_FINISHED(gz) \ - (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0) + (ZSTREAM_IS_FINISHED(&(gz)->z) && ZSTREAM_BUF_FILLED(&(gz)->z) == 0) #define GZFILE_READ_SIZE 2048 +enum { read_raw_arg_len, read_raw_arg_buf, read_raw_arg__count}; +struct read_raw_arg { + VALUE io; + const VALUE argv[read_raw_arg__count]; /* for rb_funcallv */ +}; + +#define read_raw_arg_argc(ra) \ + ((int)read_raw_arg__count - NIL_P((ra)->argv[read_raw_arg__count - 1])) +#define read_raw_arg_init(io, len, buf) { io, { len, buf } } static void -gzfile_mark(struct gzfile *gz) +gzfile_mark(void *p) { + struct gzfile *gz = p; + rb_gc_mark(gz->io); rb_gc_mark(gz->orig_name); rb_gc_mark(gz->comment); @@ -1976,8 +2473,9 @@ gzfile_mark(struct gzfile *gz) } static void -gzfile_free(struct gzfile *gz) +gzfile_free(void *p) { + struct gzfile *gz = p; struct zstream *z = &gz->z; if (ZSTREAM_IS_READY(z)) { @@ -1986,23 +2484,26 @@ gzfile_free(struct gzfile *gz) } zstream_finalize(z); } - if (gz->cbuf) { - xfree(gz->cbuf); - } xfree(gz); } -static VALUE -gzfile_new(klass, funcs, endfunc) - VALUE klass; - const struct zstream_funcs *funcs; - void (*endfunc)(struct gzfile *); +static size_t +gzfile_memsize(const void *p) { - VALUE obj; - struct gzfile *gz; + return sizeof(struct gzfile); +} - obj = Data_Make_Struct(klass, struct gzfile, gzfile_mark, gzfile_free, gz); +static const rb_data_type_t gzfile_data_type = { + "gzfile", + { gzfile_mark, gzfile_free, gzfile_memsize, }, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY +}; + +static void +gzfile_init(struct gzfile *gz, const struct zstream_funcs *funcs, void (*endfunc)(struct gzfile *)) +{ zstream_init(&gz->z, funcs); + gz->z.flags |= ZSTREAM_FLAG_GZFILE; gz->io = Qnil; gz->level = 0; gz->mtime = 0; @@ -2018,9 +2519,17 @@ gzfile_new(klass, funcs, endfunc) gz->ec = NULL; gz->ecflags = 0; gz->ecopts = Qnil; - gz->cbuf = 0; gz->path = Qnil; +} +static VALUE +gzfile_new(VALUE klass, const struct zstream_funcs *funcs, void (*endfunc)(struct gzfile *)) +{ + VALUE obj; + struct gzfile *gz; + + obj = TypedData_Make_Struct(klass, struct gzfile, &gzfile_data_type, gz); + gzfile_init(gz, funcs, endfunc); return obj; } @@ -2031,6 +2540,7 @@ static void gzfile_reset(struct gzfile *gz) { zstream_reset(&gz->z); + gz->z.flags |= ZSTREAM_FLAG_GZFILE; gz->crc = crc32(0, Z_NULL, 0); gz->lineno = 0; gz->ungetc = 0; @@ -2060,9 +2570,8 @@ gzfile_write_raw(struct gzfile *gz) { VALUE str; - if (gz->z.buf_filled > 0) { + if (ZSTREAM_BUF_FILLED(&gz->z) > 0) { str = zstream_detach_buffer(&gz->z); - OBJ_TAINT(str); /* for safe */ rb_funcall(gz->io, id_write, 1, str); if ((gz->z.flags & GZFILE_FLAG_SYNC) && rb_respond_to(gz->io, id_flush)) @@ -2073,21 +2582,23 @@ gzfile_write_raw(struct gzfile *gz) static VALUE gzfile_read_raw_partial(VALUE arg) { - struct gzfile *gz = (struct gzfile*)arg; + struct read_raw_arg *ra = (struct read_raw_arg *)arg; VALUE str; + int argc = read_raw_arg_argc(ra); - str = rb_funcall(gz->io, id_readpartial, 1, INT2FIX(GZFILE_READ_SIZE)); + str = rb_funcallv(ra->io, id_readpartial, argc, ra->argv); Check_Type(str, T_STRING); return str; } static VALUE -gzfile_read_raw_rescue(VALUE arg) +gzfile_read_raw_rescue(VALUE arg, VALUE _) { - struct gzfile *gz = (struct gzfile*)arg; + struct read_raw_arg *ra = (struct read_raw_arg *)arg; VALUE str = Qnil; if (rb_obj_is_kind_of(rb_errinfo(), rb_eNoMethodError)) { - str = rb_funcall(gz->io, id_read, 1, INT2FIX(GZFILE_READ_SIZE)); + int argc = read_raw_arg_argc(ra); + str = rb_funcallv(ra->io, id_read, argc, ra->argv); if (!NIL_P(str)) { Check_Type(str, T_STRING); } @@ -2096,20 +2607,27 @@ gzfile_read_raw_rescue(VALUE arg) } static VALUE -gzfile_read_raw(struct gzfile *gz) +gzfile_read_raw(struct gzfile *gz, VALUE outbuf) { - return rb_rescue2(gzfile_read_raw_partial, (VALUE)gz, - gzfile_read_raw_rescue, (VALUE)gz, + struct read_raw_arg ra = + read_raw_arg_init(gz->io, INT2FIX(GZFILE_READ_SIZE), outbuf); + + return rb_rescue2(gzfile_read_raw_partial, (VALUE)&ra, + gzfile_read_raw_rescue, (VALUE)&ra, rb_eEOFError, rb_eNoMethodError, (VALUE)0); } static int -gzfile_read_raw_ensure(struct gzfile *gz, long size) +gzfile_read_raw_ensure(struct gzfile *gz, long size, VALUE outbuf) { VALUE str; + if (gz->io == Qundef) { /* Zlib.gunzip */ + if (NIL_P(gz->z.input) || RSTRING_LEN(gz->z.input) < size) + rb_raise(cGzError, "unexpected end of string"); + } while (NIL_P(gz->z.input) || RSTRING_LEN(gz->z.input) < size) { - str = gzfile_read_raw(gz); + str = gzfile_read_raw(gz, outbuf); if (NIL_P(str)) return 0; zstream_append_input2(&gz->z, str); } @@ -2126,7 +2644,7 @@ gzfile_read_raw_until_zero(struct gzfile *gz, long offset) p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0', RSTRING_LEN(gz->z.input) - offset); if (p) break; - str = gzfile_read_raw(gz); + str = gzfile_read_raw(gz, Qnil); if (NIL_P(str)) { rb_raise(cGzError, "unexpected end of file"); } @@ -2207,7 +2725,7 @@ gzfile_make_header(struct gzfile *gz) if (!NIL_P(gz->comment)) { flags |= GZ_FLAG_COMMENT; } - if (gz->mtime == 0) { + if (!(gz->z.flags & GZFILE_FLAG_MTIME_IS_SET)) { gz->mtime = time(0); } @@ -2225,7 +2743,7 @@ gzfile_make_header(struct gzfile *gz) gzfile_set32((unsigned long)gz->mtime, &buf[4]); buf[8] = extraflags; buf[9] = gz->os_code; - zstream_append_buffer(&gz->z, buf, sizeof(buf)); + zstream_append_buffer(&gz->z, buf, (long)sizeof(buf)); if (!NIL_P(gz->orig_name)) { zstream_append_buffer2(&gz->z, gz->orig_name); @@ -2246,18 +2764,19 @@ gzfile_make_footer(struct gzfile *gz) gzfile_set32(gz->crc, buf); gzfile_set32(gz->z.stream.total_in, &buf[4]); - zstream_append_buffer(&gz->z, buf, sizeof(buf)); + zstream_append_buffer(&gz->z, buf, (long)sizeof(buf)); gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED; } static void -gzfile_read_header(struct gzfile *gz) +gzfile_read_header(struct gzfile *gz, VALUE outbuf) { const unsigned char *head; long len; char flags, *p; - if (!gzfile_read_raw_ensure(gz, 10)) { /* 10 is the size of gzip header */ + /* 10 is the size of gzip header */ + if (!gzfile_read_raw_ensure(gz, 10, outbuf)) { gzfile_raise(gz, cGzError, "not in gzip format"); } @@ -2296,33 +2815,31 @@ gzfile_read_header(struct gzfile *gz) zstream_discard_input(&gz->z, 10); if (flags & GZ_FLAG_EXTRA) { - if (!gzfile_read_raw_ensure(gz, 2)) { + if (!gzfile_read_raw_ensure(gz, 2, outbuf)) { rb_raise(cGzError, "unexpected end of file"); } len = gzfile_get16((Bytef*)RSTRING_PTR(gz->z.input)); - if (!gzfile_read_raw_ensure(gz, 2 + len)) { + if (!gzfile_read_raw_ensure(gz, 2 + len, outbuf)) { rb_raise(cGzError, "unexpected end of file"); } zstream_discard_input(&gz->z, 2 + len); } if (flags & GZ_FLAG_ORIG_NAME) { - if (!gzfile_read_raw_ensure(gz, 1)) { + if (!gzfile_read_raw_ensure(gz, 1, outbuf)) { rb_raise(cGzError, "unexpected end of file"); } p = gzfile_read_raw_until_zero(gz, 0); len = p - RSTRING_PTR(gz->z.input); gz->orig_name = rb_str_new(RSTRING_PTR(gz->z.input), len); - OBJ_TAINT(gz->orig_name); /* for safe */ zstream_discard_input(&gz->z, len + 1); } if (flags & GZ_FLAG_COMMENT) { - if (!gzfile_read_raw_ensure(gz, 1)) { + if (!gzfile_read_raw_ensure(gz, 1, outbuf)) { rb_raise(cGzError, "unexpected end of file"); } p = gzfile_read_raw_until_zero(gz, 0); len = p - RSTRING_PTR(gz->z.input); gz->comment = rb_str_new(RSTRING_PTR(gz->z.input), len); - OBJ_TAINT(gz->comment); /* for safe */ zstream_discard_input(&gz->z, len + 1); } @@ -2332,13 +2849,14 @@ gzfile_read_header(struct gzfile *gz) } static void -gzfile_check_footer(struct gzfile *gz) +gzfile_check_footer(struct gzfile *gz, VALUE outbuf) { unsigned long crc, length; gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED; - if (!gzfile_read_raw_ensure(gz, 8)) { /* 8 is the size of gzip footer */ + /* 8 is the size of gzip footer */ + if (!gzfile_read_raw_ensure(gz, 8, outbuf)) { gzfile_raise(gz, cNoFooter, "footer is not found"); } @@ -2372,12 +2890,12 @@ gzfile_write(struct gzfile *gz, Bytef *str, long len) } static long -gzfile_read_more(struct gzfile *gz) +gzfile_read_more(struct gzfile *gz, VALUE outbuf) { - volatile VALUE str; + VALUE str; while (!ZSTREAM_IS_FINISHED(&gz->z)) { - str = gzfile_read_raw(gz); + str = gzfile_read_raw(gz, outbuf); if (NIL_P(str)) { if (!ZSTREAM_IS_FINISHED(&gz->z)) { rb_raise(cGzError, "unexpected end of file"); @@ -2387,10 +2905,11 @@ gzfile_read_more(struct gzfile *gz) if (RSTRING_LEN(str) > 0) { /* prevent Z_BUF_ERROR */ zstream_run(&gz->z, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str), Z_SYNC_FLUSH); + RB_GC_GUARD(str); } - if (gz->z.buf_filled > 0) break; + if (ZSTREAM_BUF_FILLED(&gz->z) > 0) break; } - return gz->z.buf_filled; + return ZSTREAM_BUF_FILLED(&gz->z); } static void @@ -2401,7 +2920,7 @@ gzfile_calc_crc(struct gzfile *gz, VALUE str) } else { gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc, - RSTRING_LEN(str) - gz->ungetc); + RSTRING_LEN(str) - gz->ungetc); gz->ungetc = 0; } } @@ -2411,13 +2930,11 @@ gzfile_newstr(struct gzfile *gz, VALUE str) { if (!gz->enc2) { rb_enc_associate(str, gz->enc); - OBJ_TAINT(str); /* for safe */ return str; } if (gz->ec && rb_enc_dummy_p(gz->enc2)) { str = rb_econv_str_convert(gz->ec, str, ECONV_PARTIAL_INPUT); rb_enc_associate(str, gz->enc); - OBJ_TAINT(str); return str; } return rb_str_conv_enc_opts(str, gz->enc2, gz->enc, @@ -2425,34 +2942,47 @@ gzfile_newstr(struct gzfile *gz, VALUE str) } static long -gzfile_fill(struct gzfile *gz, long len) +gzfile_fill(struct gzfile *gz, long len, VALUE outbuf) { if (len < 0) rb_raise(rb_eArgError, "negative length %ld given", len); if (len == 0) return 0; - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { - gzfile_read_more(gz); + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) { + gzfile_read_more(gz, outbuf); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, outbuf); } return -1; } - return len < gz->z.buf_filled ? len : gz->z.buf_filled; + return len < ZSTREAM_BUF_FILLED(&gz->z) ? len : ZSTREAM_BUF_FILLED(&gz->z); } static VALUE -gzfile_read(struct gzfile *gz, long len) +gzfile_read(struct gzfile *gz, long len, VALUE outbuf) { VALUE dst; - len = gzfile_fill(gz, len); - if (len == 0) return rb_str_new(0, 0); - if (len < 0) return Qnil; - dst = zstream_shift_buffer(&gz->z, len); - gzfile_calc_crc(gz, dst); + len = gzfile_fill(gz, len, outbuf); + + if (len < 0) { + if (!NIL_P(outbuf)) + rb_str_resize(outbuf, 0); + return Qnil; + } + if (len == 0) { + if (NIL_P(outbuf)) + return rb_str_new(0, 0); + else { + rb_str_resize(outbuf, 0); + return outbuf; + } + } + + dst = zstream_shift_buffer(&gz->z, len, outbuf); + if (!NIL_P(dst)) gzfile_calc_crc(gz, dst); return dst; } @@ -2464,9 +2994,6 @@ gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf) if (len < 0) rb_raise(rb_eArgError, "negative length %ld given", len); - if (!NIL_P(outbuf)) - OBJ_TAINT(outbuf); - if (len == 0) { if (NIL_P(outbuf)) return rb_str_new(0, 0); @@ -2475,48 +3002,44 @@ gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf) return outbuf; } } - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled == 0) { - gzfile_read_more(gz); + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) == 0) { + gzfile_read_more(gz, outbuf); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, outbuf); } if (!NIL_P(outbuf)) rb_str_resize(outbuf, 0); rb_raise(rb_eEOFError, "end of file reached"); } - dst = zstream_shift_buffer(&gz->z, len); + dst = zstream_shift_buffer(&gz->z, len, outbuf); gzfile_calc_crc(gz, dst); - if (!NIL_P(outbuf)) { - rb_str_resize(outbuf, RSTRING_LEN(dst)); - memcpy(RSTRING_PTR(outbuf), RSTRING_PTR(dst), RSTRING_LEN(dst)); - dst = outbuf; - } - OBJ_TAINT(dst); /* for safe */ return dst; } static VALUE -gzfile_read_all(struct gzfile *gz) +gzfile_read_all(struct gzfile *gz, VALUE dst) { - VALUE dst; - while (!ZSTREAM_IS_FINISHED(&gz->z)) { - gzfile_read_more(gz); + gzfile_read_more(gz, dst); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, dst); } + if (!NIL_P(dst)) { + rb_str_resize(dst, 0); + return dst; + } return rb_str_new(0, 0); } dst = zstream_detach_buffer(&gz->z); + if (NIL_P(dst)) return dst; gzfile_calc_crc(gz, dst); - OBJ_TAINT(dst); return gzfile_newstr(gz, dst); } @@ -2527,12 +3050,12 @@ gzfile_getc(struct gzfile *gz) int len; len = rb_enc_mbmaxlen(gz->enc); - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { - gzfile_read_more(gz); + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) { + gzfile_read_more(gz, Qnil); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, Qnil); } return Qnil; } @@ -2540,28 +3063,24 @@ gzfile_getc(struct gzfile *gz) if (gz->ec && rb_enc_dummy_p(gz->enc2)) { const unsigned char *ss, *sp, *se; unsigned char *ds, *dp, *de; - rb_econv_result_t res; + VALUE cbuf = rb_enc_str_new(0, GZFILE_CBUF_CAPA, gz->enc); - if (!gz->cbuf) { - gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA); - } ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf); - se = sp + gz->z.buf_filled; - ds = dp = (unsigned char *)gz->cbuf; + se = sp + ZSTREAM_BUF_FILLED(&gz->z); + ds = dp = (unsigned char *)RSTRING_PTR(cbuf); de = (unsigned char *)ds + GZFILE_CBUF_CAPA; - res = rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT); + (void)rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT); rb_econv_check_error(gz->ec); - dst = zstream_shift_buffer(&gz->z, sp - ss); + dst = zstream_shift_buffer(&gz->z, sp - ss, Qnil); gzfile_calc_crc(gz, dst); - dst = rb_str_new(gz->cbuf, dp - ds); - rb_enc_associate(dst, gz->enc); - OBJ_TAINT(dst); - return dst; + rb_str_resize(cbuf, dp - ds); + return cbuf; } else { buf = gz->z.buf; len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), gz->enc); - dst = gzfile_read(gz, len); + dst = gzfile_read(gz, len, Qnil); + if (NIL_P(dst)) return dst; return gzfile_newstr(gz, dst); } } @@ -2602,7 +3121,7 @@ gzfile_writer_end(struct gzfile *gz) if (ZSTREAM_IS_CLOSING(&gz->z)) return; gz->z.flags |= ZSTREAM_FLAG_CLOSING; - rb_ensure(gzfile_writer_end_run, (VALUE)gz, zstream_end, (VALUE)&gz->z); + rb_ensure(gzfile_writer_end_run, (VALUE)gz, zstream_ensure_end, (VALUE)&gz->z); } static VALUE @@ -2612,7 +3131,7 @@ gzfile_reader_end_run(VALUE arg) if (GZFILE_IS_FINISHED(gz) && !(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, Qnil); } return Qnil; @@ -2624,7 +3143,7 @@ gzfile_reader_end(struct gzfile *gz) if (ZSTREAM_IS_CLOSING(&gz->z)) return; gz->z.flags |= ZSTREAM_FLAG_CLOSING; - rb_ensure(gzfile_reader_end_run, (VALUE)gz, zstream_end, (VALUE)&gz->z); + rb_ensure(gzfile_reader_end_run, (VALUE)gz, zstream_ensure_end, (VALUE)&gz->z); } static void @@ -2649,12 +3168,11 @@ gzfile_reader_get_unused(struct gzfile *gz) if (!ZSTREAM_IS_READY(&gz->z)) return Qnil; if (!GZFILE_IS_FINISHED(gz)) return Qnil; if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, Qnil); } if (NIL_P(gz->z.input)) return Qnil; str = rb_str_resurrect(gz->z.input); - OBJ_TAINT(str); /* for safe */ return str; } @@ -2663,7 +3181,7 @@ get_gzfile(VALUE obj) { struct gzfile *gz; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); if (!ZSTREAM_IS_READY(&gz->z)) { rb_raise(cGzError, "closed gzip stream"); } @@ -2721,7 +3239,7 @@ static VALUE new_wrap(VALUE tmp) { new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp; - return rb_class_new_instance(arg->argc, arg->argv, arg->klass); + return rb_class_new_instance_kw(arg->argc, arg->argv, arg->klass, RB_PASS_CALLED_KEYWORDS); } static VALUE @@ -2729,7 +3247,7 @@ gzfile_ensure_close(VALUE obj) { struct gzfile *gz; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); if (ZSTREAM_IS_READY(&gz->z)) { gzfile_close(gz, 1); } @@ -2754,7 +3272,7 @@ gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error) } } else { - obj = rb_class_new_instance(argc, argv, klass); + obj = rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS); } if (rb_block_given_p()) { @@ -2768,14 +3286,17 @@ gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error) /* * Document-method: Zlib::GzipFile.wrap * - * call-seq: Zlib::GzipFile.wrap(io) { |gz| ... } + * call-seq: + * Zlib::GzipReader.wrap(io, ...) { |gz| ... } + * Zlib::GzipWriter.wrap(io, ...) { |gz| ... } + * + * Creates a GzipReader or GzipWriter associated with +io+, passing in any + * necessary extra options, and executes the block with the newly created + * object just like File.open. * - * Creates a GzipFile object associated with +io+, and - * executes the block with the newly created GzipFile object, - * just like File.open. The GzipFile object will be closed - * automatically after executing the block. If you want to keep - * the associated IO object opening, you may call - * +Zlib::GzipFile#finish+ method in the block. + * The GzipFile object will be closed automatically after executing the block. + * If you want to keep the associated IO object open, you may call + * Zlib::GzipFile#finish method in the block. */ static VALUE rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass) @@ -2793,9 +3314,7 @@ gzfile_s_open(int argc, VALUE *argv, VALUE klass, const char *mode) { VALUE io, filename; - if (argc < 1) { - rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)"); - } + rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); filename = argv[0]; io = rb_file_open_str(filename, mode); argv[0] = io; @@ -2870,7 +3389,6 @@ rb_gzfile_orig_name(VALUE obj) if (!NIL_P(str)) { str = rb_str_dup(str); } - OBJ_TAINT(str); /* for safe */ return str; } @@ -2887,7 +3405,6 @@ rb_gzfile_comment(VALUE obj) if (!NIL_P(str)) { str = rb_str_dup(str); } - OBJ_TAINT(str); /* for safe */ return str; } @@ -2919,7 +3436,20 @@ rb_gzfile_set_lineno(VALUE obj, VALUE lineno) * Document-method: Zlib::GzipWriter#mtime= * * Specify the modification time (+mtime+) in the gzip header. - * Using a Fixnum or Integer + * Using an Integer. + * + * Setting the mtime in the gzip header does not effect the + * mtime of the file generated. Different utilities that + * expand the gzipped files may use the mtime + * header. For example the gunzip utility can use the `-N` + * flag which will set the resultant file's mtime to the + * value in the header. By default many tools will set + * the mtime of the expanded file to the mtime of the + * gzipped file, not the mtime in the header. + * + * If you do not set an mtime, the default value will be the time + * when compression started. Setting a value of 0 indicates + * no time stamp is available. */ static VALUE rb_gzfile_set_mtime(VALUE obj, VALUE mtime) @@ -2931,13 +3461,10 @@ rb_gzfile_set_mtime(VALUE obj, VALUE mtime) rb_raise(cGzError, "header is already written"); } - if (FIXNUM_P(mtime)) { - gz->mtime = FIX2INT(mtime); - } - else { - val = rb_Integer(mtime); - gz->mtime = FIXNUM_P(val) ? FIX2UINT(val) : rb_big2ulong(val); - } + val = rb_Integer(mtime); + gz->mtime = NUM2UINT(val); + gz->z.flags |= GZFILE_FLAG_MTIME_IS_SET; + return mtime; } @@ -2998,9 +3525,13 @@ rb_gzfile_set_comment(VALUE obj, VALUE str) static VALUE rb_gzfile_close(VALUE obj) { - struct gzfile *gz = get_gzfile(obj); + struct gzfile *gz; VALUE io; + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); + if (!ZSTREAM_IS_READY(&gz->z)) { + return Qnil; + } io = gz->io; gzfile_close(gz, 1); return io; @@ -3034,7 +3565,7 @@ static VALUE rb_gzfile_closed_p(VALUE obj) { struct gzfile *gz; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); return NIL_P(gz->io) ? Qtrue : Qfalse; } @@ -3047,6 +3578,9 @@ static VALUE rb_gzfile_eof_p(VALUE obj) { struct gzfile *gz = get_gzfile(obj); + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) == 0) { + gzfile_read_more(gz, Qnil); + } return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse; } @@ -3105,7 +3639,14 @@ static VALUE rb_gzfile_total_out(VALUE obj) { struct gzfile *gz = get_gzfile(obj); - return rb_uint2inum(gz->z.stream.total_out - gz->z.buf_filled); + uLong total_out = gz->z.stream.total_out; + long buf_filled = ZSTREAM_BUF_FILLED(&gz->z); + + if (total_out >= (uLong)buf_filled) { + return rb_uint2inum(total_out - buf_filled); + } else { + return LONG2FIX(-(buf_filled - (long)total_out)); + } } /* @@ -3120,10 +3661,20 @@ static VALUE rb_gzfile_path(VALUE obj) { struct gzfile *gz; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); return gz->path; } +static VALUE +gzfile_initialize_path_partial(VALUE obj) +{ + struct gzfile* gz; + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); + gz->path = rb_funcall(gz->io, id_path, 0); + rb_define_singleton_method(obj, "path", rb_gzfile_path, 0); + return Qnil; +} + static void rb_gzfile_ecopts(struct gzfile *gz, VALUE opts) { @@ -3193,12 +3744,17 @@ rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass) } /* - * call-seq: Zlib::GzipWriter.new(io, level, strategy) + * call-seq: + * Zlib::GzipWriter.new(io, level = nil, strategy = nil, options = {}) * * Creates a GzipWriter object associated with +io+. +level+ and +strategy+ * should be the same as the arguments of Zlib::Deflate.new. The GzipWriter - * object writes gzipped data to +io+. At least, +io+ must respond to the - * +write+ method that behaves same as write method in IO class. + * object writes gzipped data to +io+. +io+ must respond to the + * +write+ method that behaves the same as IO#write. + * + * The +options+ hash may be used to set the encoding of the data. + * +:external_encoding+, +:internal_encoding+ and +:encoding+ may be set as in + * IO::new. */ static VALUE rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj) @@ -3213,7 +3769,7 @@ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj) } rb_scan_args(argc, argv, "12", &io, &level, &strategy); - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); /* this is undocumented feature of zlib */ gz->level = ARG_LEVEL(level); @@ -3227,8 +3783,8 @@ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj) rb_gzfile_ecopts(gz, opt); if (rb_respond_to(io, id_path)) { - gz->path = rb_funcall(gz->io, id_path, 0); - rb_define_singleton_method(obj, "path", rb_gzfile_path, 0); + /* File#path may raise IOError in case when a path is unavailable */ + rb_rescue2(gzfile_initialize_path_partial, obj, NULL, Qnil, rb_eIOError, (VALUE)0); } return obj; @@ -3266,17 +3822,23 @@ rb_gzwriter_flush(int argc, VALUE *argv, VALUE obj) * Same as IO. */ static VALUE -rb_gzwriter_write(VALUE obj, VALUE str) +rb_gzwriter_write(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); - - if (TYPE(str) != T_STRING) - str = rb_obj_as_string(str); - if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) { - str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2); + size_t total = 0; + + while (argc-- > 0) { + VALUE str = *argv++; + if (!RB_TYPE_P(str, T_STRING)) + str = rb_obj_as_string(str); + if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) { + str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2); + } + gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str)); + total += RSTRING_LEN(str); + RB_GC_GUARD(str); } - gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str)); - return INT2FIX(RSTRING_LEN(str)); + return SIZET2NUM(total); } /* @@ -3322,7 +3884,7 @@ rb_gzwriter_putc(VALUE obj, VALUE ch) * Document-class: Zlib::GzipReader * * Zlib::GzipReader is the class for reading a gzipped file. GzipReader should - * be used an IO, or -IO-lie, object. + * be used as an IO, or -IO-like, object. * * Zlib::GzipReader.open('hoge.gz') {|gz| * print gz.read @@ -3392,13 +3954,72 @@ rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass) } /* + * Document-method: Zlib::GzipReader.zcat + * + * call-seq: + * Zlib::GzipReader.zcat(io, options = {}, &block) => nil + * Zlib::GzipReader.zcat(io, options = {}) => string + * + * Decompresses all gzip data in the +io+, handling multiple gzip + * streams until the end of the +io+. There should not be any non-gzip + * data after the gzip streams. + * + * If a block is given, it is yielded strings of uncompressed data, + * and the method returns +nil+. + * If a block is not given, the method returns the concatenation of + * all uncompressed data in all gzip streams. + */ +static VALUE +rb_gzreader_s_zcat(int argc, VALUE *argv, VALUE klass) +{ + VALUE io, unused, obj, buf=0, tmpbuf; + long pos; + + rb_check_arity(argc, 1, 2); + io = argv[0]; + + do { + obj = rb_funcallv(klass, rb_intern("new"), argc, argv); + if (rb_block_given_p()) { + rb_gzreader_each(0, 0, obj); + } + else { + if (!buf) { + buf = rb_str_new(0, 0); + } + tmpbuf = gzfile_read_all(get_gzfile(obj), Qnil); + rb_str_cat(buf, RSTRING_PTR(tmpbuf), RSTRING_LEN(tmpbuf)); + } + + rb_gzreader_read(0, 0, obj); + pos = NUM2LONG(rb_funcall(io, rb_intern("pos"), 0)); + unused = rb_gzreader_unused(obj); + rb_gzfile_finish(obj); + if (!NIL_P(unused)) { + pos -= NUM2LONG(rb_funcall(unused, rb_intern("length"), 0)); + rb_funcall(io, rb_intern("pos="), 1, LONG2NUM(pos)); + } + } while (pos < NUM2LONG(rb_funcall(io, rb_intern("size"), 0))); + + if (rb_block_given_p()) { + return Qnil; + } + return buf; +} + +/* * Document-method: Zlib::GzipReader.new * - * call-seq: Zlib::GzipReader.new(io) + * call-seq: + * Zlib::GzipReader.new(io, options = {}) * * Creates a GzipReader object associated with +io+. The GzipReader object reads - * gzipped data from +io+, and parses/decompresses them. At least, +io+ must have - * a +read+ method that behaves same as the +read+ method in IO class. + * gzipped data from +io+, and parses/decompresses it. The +io+ must + * have a +read+ method that behaves same as the IO#read. + * + * The +options+ hash may be used to set the encoding of the data. + * +:external_encoding+, +:internal_encoding+ and +:encoding+ may be set as in + * IO::new. * * If the gzip file header is incorrect, raises an Zlib::GzipFile::Error * exception. @@ -3410,7 +4031,7 @@ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj) struct gzfile *gz; int err; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); rb_scan_args(argc, argv, "1:", &io, &opt); /* this is undocumented feature of zlib */ @@ -3420,12 +4041,12 @@ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj) } gz->io = io; ZSTREAM_READY(&gz->z); - gzfile_read_header(gz); + gzfile_read_header(gz, Qnil); rb_gzfile_ecopts(gz, opt); if (rb_respond_to(io, id_path)) { - gz->path = rb_funcall(gz->io, id_path, 0); - rb_define_singleton_method(obj, "path", rb_gzfile_path, 0); + /* File#path may raise IOError in case when a path is unavailable */ + rb_rescue2(gzfile_initialize_path_partial, obj, NULL, Qnil, rb_eIOError, (VALUE)0); } return obj; @@ -3455,7 +4076,7 @@ static VALUE rb_gzreader_unused(VALUE obj) { struct gzfile *gz; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); return gzfile_reader_get_unused(gz); } @@ -3468,19 +4089,19 @@ static VALUE rb_gzreader_read(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); - VALUE vlen; + VALUE vlen, outbuf; long len; - rb_scan_args(argc, argv, "01", &vlen); + rb_scan_args(argc, argv, "02", &vlen, &outbuf); if (NIL_P(vlen)) { - return gzfile_read_all(gz); + return gzfile_read_all(gz, outbuf); } len = NUM2INT(vlen); if (len < 0) { rb_raise(rb_eArgError, "negative length %ld given", len); } - return gzfile_read(gz, len); + return gzfile_read(gz, len, outbuf); } /* @@ -3489,7 +4110,7 @@ rb_gzreader_read(int argc, VALUE *argv, VALUE obj) * call-seq: * gzipreader.readpartial(maxlen [, outbuf]) => string, outbuf * - * Reads at most <i>maxlen</i> bytes from the gziped stream but + * Reads at most <i>maxlen</i> bytes from the gzipped stream but * it blocks only if <em>gzipreader</em> has no data immediately available. * If the optional <i>outbuf</i> argument is present, * it must reference a String, which will receive the data. @@ -3553,7 +4174,7 @@ rb_gzreader_getbyte(VALUE obj) struct gzfile *gz = get_gzfile(obj); VALUE dst; - dst = gzfile_read(gz, 1); + dst = gzfile_read(gz, 1, Qnil); if (!NIL_P(dst)) { dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff); } @@ -3630,6 +4251,7 @@ rb_gzreader_ungetc(VALUE obj, VALUE s) s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2); } gzfile_ungets(gz, (const Bytef*)RSTRING_PTR(s), RSTRING_LEN(s)); + RB_GC_GUARD(s); return Qnil; } @@ -3653,27 +4275,28 @@ gzreader_skip_linebreaks(struct gzfile *gz) char *p; int n; - while (gz->z.buf_filled == 0) { + while (ZSTREAM_BUF_FILLED(&gz->z) == 0) { if (GZFILE_IS_FINISHED(gz)) return; - gzfile_read_more(gz); + gzfile_read_more(gz, Qnil); } n = 0; p = RSTRING_PTR(gz->z.buf); while (n++, *(p++) == '\n') { - if (n >= gz->z.buf_filled) { + if (n >= ZSTREAM_BUF_FILLED(&gz->z)) { str = zstream_detach_buffer(&gz->z); + ASSUME(!NIL_P(str)); gzfile_calc_crc(gz, str); - while (gz->z.buf_filled == 0) { + while (ZSTREAM_BUF_FILLED(&gz->z) == 0) { if (GZFILE_IS_FINISHED(gz)) return; - gzfile_read_more(gz); + gzfile_read_more(gz, Qnil); } n = 0; p = RSTRING_PTR(gz->z.buf); } } - str = zstream_shift_buffer(&gz->z, n - 1); + str = zstream_shift_buffer(&gz->z, n - 1, Qnil); gzfile_calc_crc(gz, str); } @@ -3688,18 +4311,18 @@ static long gzreader_charboundary(struct gzfile *gz, long n) { char *s = RSTRING_PTR(gz->z.buf); - char *e = s + gz->z.buf_filled; - char *p = rb_enc_left_char_head(s, s + n, e, gz->enc); + char *e = s + ZSTREAM_BUF_FILLED(&gz->z); + char *p = rb_enc_left_char_head(s, s + n - 1, e, gz->enc); long l = p - s; if (l < n) { - n = rb_enc_precise_mbclen(p, e, gz->enc); - if (MBCLEN_NEEDMORE_P(n)) { - if ((l = gzfile_fill(gz, l + MBCLEN_NEEDMORE_LEN(n))) > 0) { + int n_bytes = rb_enc_precise_mbclen(p, e, gz->enc); + if (MBCLEN_NEEDMORE_P(n_bytes)) { + if ((l = gzfile_fill(gz, n + MBCLEN_NEEDMORE_LEN(n_bytes), Qnil)) > 0) { return l; } } - else if (MBCLEN_CHARFOUND_P(n)) { - return l + MBCLEN_CHARFOUND_LEN(n); + else if (MBCLEN_CHARFOUND_P(n_bytes)) { + return l + MBCLEN_CHARFOUND_LEN(n_bytes); } } return n; @@ -3709,7 +4332,7 @@ static VALUE gzreader_gets(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); - volatile VALUE rs; + VALUE rs; VALUE dst; const char *rsptr; char *p, *res; @@ -3746,10 +4369,10 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) if (NIL_P(rs)) { if (limit < 0) { - dst = gzfile_read_all(gz); + dst = gzfile_read_all(gz, Qnil); if (RSTRING_LEN(dst) == 0) return Qnil; } - else if ((n = gzfile_fill(gz, limit)) <= 0) { + else if ((n = gzfile_fill(gz, limit, Qnil)) <= 0) { return Qnil; } else { @@ -3759,7 +4382,8 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) else { n = limit; } - dst = zstream_shift_buffer(&gz->z, n); + dst = zstream_shift_buffer(&gz->z, n, Qnil); + if (NIL_P(dst)) return dst; gzfile_calc_crc(gz, dst); dst = gzfile_newstr(gz, dst); } @@ -3771,7 +4395,8 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) rsptr = "\n\n"; rslen = 2; rspara = 1; - } else { + } + else { rsptr = RSTRING_PTR(rs); rslen = RSTRING_LEN(rs); rspara = 0; @@ -3781,25 +4406,25 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) gzreader_skip_linebreaks(gz); } - while (gz->z.buf_filled < rslen) { + while (ZSTREAM_BUF_FILLED(&gz->z) < rslen) { if (ZSTREAM_IS_FINISHED(&gz->z)) { - if (gz->z.buf_filled > 0) gz->lineno++; - return gzfile_read(gz, rslen); + if (ZSTREAM_BUF_FILLED(&gz->z) > 0) gz->lineno++; + return gzfile_read(gz, rslen, Qnil); } - gzfile_read_more(gz); + gzfile_read_more(gz, Qnil); } p = RSTRING_PTR(gz->z.buf); n = rslen; for (;;) { long filled; - if (n > gz->z.buf_filled) { + if (n > ZSTREAM_BUF_FILLED(&gz->z)) { if (ZSTREAM_IS_FINISHED(&gz->z)) break; - gzfile_read_more(gz); + gzfile_read_more(gz, Qnil); p = RSTRING_PTR(gz->z.buf) + n - rslen; } if (!rspara) rscheck(rsptr, rslen, rs); - filled = gz->z.buf_filled; + filled = ZSTREAM_BUF_FILLED(&gz->z); if (limit > 0 && filled >= limit) { filled = limit; } @@ -3808,22 +4433,25 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) n = filled; if (limit > 0 && filled >= limit) break; n++; - } else { + } + else { n += (long)(res - p); p = res; if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break; p++, n++; } } - if (maxlen > 1 && n == limit && (gz->z.buf_filled > n || !ZSTREAM_IS_FINISHED(&gz->z))) { + if (maxlen > 1 && n == limit && (ZSTREAM_BUF_FILLED(&gz->z) > n || !ZSTREAM_IS_FINISHED(&gz->z))) { n = gzreader_charboundary(gz, n); } gz->lineno++; - dst = gzfile_read(gz, n); + dst = gzfile_read(gz, n, Qnil); + if (NIL_P(dst)) return dst; if (rspara) { gzreader_skip_linebreaks(gz); } + RB_GC_GUARD(rs); return gzfile_newstr(gz, dst); } @@ -3832,6 +4460,8 @@ gzreader_gets(int argc, VALUE *argv, VALUE obj) * Document-method: Zlib::GzipReader#gets * * See Zlib::GzipReader documentation for a description. + * However, note that this method can return +nil+ even if + * #eof? returns false, unlike the behavior of File#gets. */ static VALUE rb_gzreader_gets(int argc, VALUE *argv, VALUE obj) @@ -3894,94 +4524,198 @@ rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj) return dst; } -#endif /* GZIP_SUPPORT */ +/* + * Document-method: Zlib::GzipReader#external_encoding + * + * See Zlib::GzipReader documentation for a description. + */ +static VALUE +rb_gzreader_external_encoding(VALUE self) +{ + return rb_enc_from_encoding(get_gzfile(self)->enc); +} + +static VALUE +zlib_gzip_end_rescue(VALUE arg) +{ + struct gzfile *gz = (struct gzfile *)arg; + gz->end(gz); + return Qnil; +} +static VALUE +zlib_gzip_ensure(VALUE arg) +{ + return rb_rescue(zlib_gzip_end_rescue, arg, NULL, Qnil); +} +static void +zlib_gzip_end(struct gzfile *gz) +{ + gz->z.flags |= ZSTREAM_FLAG_CLOSING; + zstream_run(&gz->z, (Bytef*)"", 0, Z_FINISH); + gzfile_make_footer(gz); + zstream_end(&gz->z); +} + +static ID id_level, id_strategy; +static VALUE zlib_gzip_run(VALUE arg); /* - * Document-module: Zlib + * call-seq: + * Zlib.gzip(src, level: nil, strategy: nil) -> String + * + * Gzip the given +string+. Valid values of level are + * Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION, + * Zlib::DEFAULT_COMPRESSION (default), or an integer from 0 to 9. + * + * This method is almost equivalent to the following code: + * + * def gzip(string, level: nil, strategy: nil) + * sio = StringIO.new + * sio.binmode + * gz = Zlib::GzipWriter.new(sio, level, strategy) + * gz.write(string) + * gz.close + * sio.string + * end + * + * See also Zlib.gunzip + * + */ +static VALUE +zlib_s_gzip(int argc, VALUE *argv, VALUE klass) +{ + struct gzfile gz0; + struct gzfile *gz = &gz0; + int err; + VALUE src, opts, level=Qnil, strategy=Qnil, args[2]; + + if (OPTHASH_GIVEN_P(opts)) { + ID keyword_ids[2]; + VALUE kwargs[2]; + keyword_ids[0] = id_level; + keyword_ids[1] = id_strategy; + rb_get_kwargs(opts, keyword_ids, 0, 2, kwargs); + if (kwargs[0] != Qundef) { + level = kwargs[0]; + } + if (kwargs[1] != Qundef) { + strategy = kwargs[1]; + } + } + rb_scan_args(argc, argv, "10", &src); + StringValue(src); + gzfile_init(gz, &deflate_funcs, zlib_gzip_end); + gz->level = ARG_LEVEL(level); + err = deflateInit2(&gz->z.stream, gz->level, Z_DEFLATED, + -MAX_WBITS, DEF_MEM_LEVEL, ARG_STRATEGY(strategy)); + if (err != Z_OK) { + zlib_gzip_end(gz); + raise_zlib_error(err, gz->z.stream.msg); + } + ZSTREAM_READY(&gz->z); + args[0] = (VALUE)gz; + args[1] = src; + return rb_ensure(zlib_gzip_run, (VALUE)args, zlib_gzip_ensure, (VALUE)gz); +} + +static VALUE +zlib_gzip_run(VALUE arg) +{ + VALUE *args = (VALUE *)arg; + struct gzfile *gz = (struct gzfile *)args[0]; + VALUE src = args[1]; + long len; + + gzfile_make_header(gz); + len = RSTRING_LEN(src); + if (len > 0) { + Bytef *ptr = (Bytef *)RSTRING_PTR(src); + gz->crc = checksum_long(crc32, gz->crc, ptr, len); + zstream_run(&gz->z, ptr, len, Z_NO_FLUSH); + } + gzfile_close(gz, 0); + return zstream_detach_buffer(&gz->z); +} + +static void +zlib_gunzip_end(struct gzfile *gz) +{ + gz->z.flags |= ZSTREAM_FLAG_CLOSING; + zstream_end(&gz->z); +} + +static VALUE zlib_gunzip_run(VALUE arg); + +/* + * call-seq: + * Zlib.gunzip(src) -> String + * + * Decode the given gzipped +string+. * - * The Zlib module contains several classes for compressing and decompressing - * streams, and for working with "gzip" files. - * - * == Classes - * - * Following are the classes that are most likely to be of interest to the - * user: - * Zlib::Inflate - * Zlib::Deflate - * Zlib::GzipReader - * Zlib::GzipWriter - * - * There are two important base classes for the classes above: Zlib::ZStream - * and Zlib::GzipFile. Everything else is an error class. - * - * == Constants - * - * Here's a list. - * - * Zlib::VERSION - * The Ruby/zlib version string. - * - * Zlib::ZLIB_VERSION - * The string which represents the version of zlib.h. - * - * Zlib::BINARY - * Zlib::ASCII - * Zlib::UNKNOWN - * The integers representing data types which Zlib::ZStream#data_type - * method returns. - * - * Zlib::NO_COMPRESSION - * Zlib::BEST_SPEED - * Zlib::BEST_COMPRESSION - * Zlib::DEFAULT_COMPRESSION - * The integers representing compression levels which are an argument - * for Zlib::Deflate.new, Zlib::Deflate#deflate, and so on. - * - * Zlib::FILTERED - * Zlib::HUFFMAN_ONLY - * Zlib::DEFAULT_STRATEGY - * The integers representing compression methods which are an argument - * for Zlib::Deflate.new and Zlib::Deflate#params. - * - * Zlib::DEF_MEM_LEVEL - * Zlib::MAX_MEM_LEVEL - * The integers representing memory levels which are an argument for - * Zlib::Deflate.new, Zlib::Deflate#params, and so on. - * - * Zlib::MAX_WBITS - * The default value of windowBits which is an argument for - * Zlib::Deflate.new and Zlib::Inflate.new. - * - * Zlib::NO_FLUSH - * Zlib::SYNC_FLUSH - * Zlib::FULL_FLUSH - * Zlib::FINISH - * The integers to control the output of the deflate stream, which are - * an argument for Zlib::Deflate#deflate and so on. - * - * Zlib::OS_CODE - * Zlib::OS_MSDOS - * Zlib::OS_AMIGA - * Zlib::OS_VMS - * Zlib::OS_UNIX - * Zlib::OS_VMCMS - * Zlib::OS_ATARI - * Zlib::OS_OS2 - * Zlib::OS_MACOS - * Zlib::OS_ZSYSTEM - * Zlib::OS_CPM - * Zlib::OS_TOPS20 - * Zlib::OS_WIN32 - * Zlib::OS_QDOS - * Zlib::OS_RISCOS - * Zlib::OS_UNKNOWN - * The return values of Zlib::GzipFile#os_code method. + * This method is almost equivalent to the following code: + * + * def gunzip(string) + * sio = StringIO.new(string) + * gz = Zlib::GzipReader.new(sio, encoding: Encoding::ASCII_8BIT) + * gz.read + * ensure + * gz&.close + * end + * + * See also Zlib.gzip */ +static VALUE +zlib_gunzip(VALUE klass, VALUE src) +{ + struct gzfile gz0; + struct gzfile *gz = &gz0; + int err; + + StringValue(src); + + gzfile_init(gz, &inflate_funcs, zlib_gunzip_end); + err = inflateInit2(&gz->z.stream, -MAX_WBITS); + if (err != Z_OK) { + raise_zlib_error(err, gz->z.stream.msg); + } + gz->io = Qundef; + gz->z.input = src; + ZSTREAM_READY(&gz->z); + return rb_ensure(zlib_gunzip_run, (VALUE)gz, zlib_gzip_ensure, (VALUE)gz); +} + +static VALUE +zlib_gunzip_run(VALUE arg) +{ + struct gzfile *gz = (struct gzfile *)arg; + VALUE dst; + + gzfile_read_header(gz, Qnil); + dst = zstream_detach_buffer(&gz->z); + ASSUME(!NIL_P(dst)); + gzfile_calc_crc(gz, dst); + if (!ZSTREAM_IS_FINISHED(&gz->z)) { + rb_raise(cGzError, "unexpected end of file"); + } + if (NIL_P(gz->z.input)) { + rb_raise(cNoFooter, "footer is not found"); + } + gzfile_check_footer(gz, Qnil); + return dst; +} + +#endif /* GZIP_SUPPORT */ + void -Init_zlib() +Init_zlib(void) { +#ifdef HAVE_RB_EXT_RACTOR_SAFE + rb_ext_ractor_safe(true); +#endif + +#undef rb_intern VALUE mZlib, cZStream, cDeflate, cInflate; #if GZIP_SUPPORT VALUE cGzipFile, cGzipWriter, cGzipReader; @@ -3989,6 +4723,8 @@ Init_zlib() mZlib = rb_define_module("Zlib"); + id_dictionaries = rb_intern("@dictionaries"); + cZError = rb_define_class_under(mZlib, "Error", rb_eStandardError); cStreamEnd = rb_define_class_under(mZlib, "StreamEnd", cZError); cNeedDict = rb_define_class_under(mZlib, "NeedDict", cZError); @@ -3997,6 +4733,7 @@ Init_zlib() cMemError = rb_define_class_under(mZlib, "MemError", cZError); cBufError = rb_define_class_under(mZlib, "BufError", cZError); cVersionError = rb_define_class_under(mZlib, "VersionError", cZError); + cInProgressError = rb_define_class_under(mZlib, "InProgressError", cZError); rb_define_module_function(mZlib, "zlib_version", rb_zlib_version, 0); rb_define_module_function(mZlib, "adler32", rb_zlib_adler32, -1); @@ -4030,14 +4767,29 @@ Init_zlib() rb_define_method(cZStream, "flush_next_in", rb_zstream_flush_next_in, 0); rb_define_method(cZStream, "flush_next_out", rb_zstream_flush_next_out, 0); - /* Integer representing date types which - * ZStream#data_type method returns */ + /* Represents binary data as guessed by deflate. + * + * See Zlib::Deflate#data_type. */ rb_define_const(mZlib, "BINARY", INT2FIX(Z_BINARY)); - /* Integer representing date types which - * ZStream#data_type method returns */ + + /* Represents text data as guessed by deflate. + * + * NOTE: The underlying constant Z_ASCII was deprecated in favor of Z_TEXT + * in zlib 1.2.2. New applications should not use this constant. + * + * See Zlib::Deflate#data_type. */ rb_define_const(mZlib, "ASCII", INT2FIX(Z_ASCII)); - /* Integer representing date types which - * ZStream#data_type method returns */ + +#ifdef Z_TEXT + /* Represents text data as guessed by deflate. + * + * See Zlib::Deflate#data_type. */ + rb_define_const(mZlib, "TEXT", INT2FIX(Z_TEXT)); +#endif + + /* Represents an unknown data type as guessed by deflate. + * + * See Zlib::Deflate#data_type. */ rb_define_const(mZlib, "UNKNOWN", INT2FIX(Z_UNKNOWN)); cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream); @@ -4057,77 +4809,91 @@ Init_zlib() rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1); rb_define_alloc_func(cInflate, rb_inflate_s_allocate); rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1); - rb_define_method(cInflate, "inflate", rb_inflate_inflate, 1); + rb_define_method(cInflate, "add_dictionary", rb_inflate_add_dictionary, 1); + rb_define_method(cInflate, "inflate", rb_inflate_inflate, -1); rb_define_method(cInflate, "<<", rb_inflate_addstr, 1); rb_define_method(cInflate, "sync", rb_inflate_sync, 1); rb_define_method(cInflate, "sync_point?", rb_inflate_sync_point_p, 0); rb_define_method(cInflate, "set_dictionary", rb_inflate_set_dictionary, 1); - /* compression level 0 - * - * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ + /* No compression, passes through data untouched. Use this for appending + * pre-compressed data to a deflate stream. + */ rb_define_const(mZlib, "NO_COMPRESSION", INT2FIX(Z_NO_COMPRESSION)); - /* compression level 1 - * - * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ + /* Fastest compression level, but with the lowest space savings. */ rb_define_const(mZlib, "BEST_SPEED", INT2FIX(Z_BEST_SPEED)); - /* compression level 9 - * - * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ + /* Slowest compression level, but with the best space savings. */ rb_define_const(mZlib, "BEST_COMPRESSION", INT2FIX(Z_BEST_COMPRESSION)); - /* compression level -1 - * - * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ + /* Default compression level which is a good trade-off between space and + * time + */ rb_define_const(mZlib, "DEFAULT_COMPRESSION", INT2FIX(Z_DEFAULT_COMPRESSION)); - /* compression method 1 - * - * Which is an argument for Deflate.new and Deflate#params. */ + /* Deflate strategy for data produced by a filter (or predictor). The + * effect of FILTERED is to force more Huffman codes and less string + * matching; it is somewhat intermediate between DEFAULT_STRATEGY and + * HUFFMAN_ONLY. Filtered data consists mostly of small values with a + * somewhat random distribution. + */ rb_define_const(mZlib, "FILTERED", INT2FIX(Z_FILTERED)); - /* compression method 2 - * - * Which is an argument for Deflate.new and Deflate#params. */ + + /* Deflate strategy which uses Huffman codes only (no string matching). */ rb_define_const(mZlib, "HUFFMAN_ONLY", INT2FIX(Z_HUFFMAN_ONLY)); - /* compression method 0 - * - * Which is an argument for Deflate.new and Deflate#params. */ + +#ifdef Z_RLE + /* Deflate compression strategy designed to be almost as fast as + * HUFFMAN_ONLY, but give better compression for PNG image data. + */ + rb_define_const(mZlib, "RLE", INT2FIX(Z_RLE)); +#endif + +#ifdef Z_FIXED + /* Deflate strategy which prevents the use of dynamic Huffman codes, + * allowing for a simpler decoder for specialized applications. + */ + rb_define_const(mZlib, "FIXED", INT2FIX(Z_FIXED)); +#endif + + /* Default deflate strategy which is used for normal data. */ rb_define_const(mZlib, "DEFAULT_STRATEGY", INT2FIX(Z_DEFAULT_STRATEGY)); - /* The default value of windowBits which is an argument for - * Deflate.new and Inflate.new. - */ + /* The maximum size of the zlib history buffer. Note that zlib allows + * larger values to enable different inflate modes. See Zlib::Inflate.new + * for details. + */ rb_define_const(mZlib, "MAX_WBITS", INT2FIX(MAX_WBITS)); - /* Default value is 8 - * - * The integer representing memory levels. - * Which are an argument for Deflate.new, Deflate#params, and so on. */ + + /* The default memory level for allocating zlib deflate compression state. + */ rb_define_const(mZlib, "DEF_MEM_LEVEL", INT2FIX(DEF_MEM_LEVEL)); - /* Maximum level is 9 - * - * The integers representing memory levels which are an argument for - * Deflate.new, Deflate#params, and so on. */ + + /* The maximum memory level for allocating zlib deflate compression state. + */ rb_define_const(mZlib, "MAX_MEM_LEVEL", INT2FIX(MAX_MEM_LEVEL)); - /* Output control - 0 - * - * The integers to control the output of the deflate stream, which are - * an argument for Deflate#deflate and so on. */ + /* NO_FLUSH is the default flush method and allows deflate to decide how + * much data to accumulate before producing output in order to maximize + * compression. + */ rb_define_const(mZlib, "NO_FLUSH", INT2FIX(Z_NO_FLUSH)); - /* Output control - 2 - * - * The integers to control the output of the deflate stream, which are - * an argument for Deflate#deflate and so on. */ + + /* The SYNC_FLUSH method flushes all pending output to the output buffer + * and the output is aligned on a byte boundary. Flushing may degrade + * compression so it should be used only when necessary, such as at a + * request or response boundary for a network stream. + */ rb_define_const(mZlib, "SYNC_FLUSH", INT2FIX(Z_SYNC_FLUSH)); - /* Output control - 3 - * - * The integers to control the output of the deflate stream, which are - * an argument for Deflate#deflate and so on. */ + + /* Flushes all output as with SYNC_FLUSH, and the compression state is + * reset so that decompression can restart from this point if previous + * compressed data has been damaged or if random access is desired. Like + * SYNC_FLUSH, using FULL_FLUSH too often can seriously degrade + * compression. + */ rb_define_const(mZlib, "FULL_FLUSH", INT2FIX(Z_FULL_FLUSH)); - /* Oputput control - 4 - * - * The integers to control the output of the deflate stream, which are - * an argument for Deflate#deflate and so on. */ + + /* Processes all pending input and flushes pending output. */ rb_define_const(mZlib, "FINISH", INT2FIX(Z_FINISH)); #if GZIP_SUPPORT @@ -4185,7 +4951,7 @@ Init_zlib() rb_define_alloc_func(cGzipWriter, rb_gzwriter_s_allocate); rb_define_method(cGzipWriter, "initialize", rb_gzwriter_initialize,-1); rb_define_method(cGzipWriter, "flush", rb_gzwriter_flush, -1); - rb_define_method(cGzipWriter, "write", rb_gzwriter_write, 1); + rb_define_method(cGzipWriter, "write", rb_gzwriter_write, -1); rb_define_method(cGzipWriter, "putc", rb_gzwriter_putc, 1); rb_define_method(cGzipWriter, "<<", rb_gzwriter_addstr, 1); rb_define_method(cGzipWriter, "printf", rb_gzwriter_printf, -1); @@ -4193,6 +4959,7 @@ Init_zlib() rb_define_method(cGzipWriter, "puts", rb_gzwriter_puts, -1); rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1); + rb_define_singleton_method(cGzipReader, "zcat", rb_gzreader_s_zcat, -1); rb_define_alloc_func(cGzipReader, rb_gzreader_s_allocate); rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, -1); rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0); @@ -4205,50 +4972,54 @@ Init_zlib() rb_define_method(cGzipReader, "readbyte", rb_gzreader_readbyte, 0); rb_define_method(cGzipReader, "each_byte", rb_gzreader_each_byte, 0); rb_define_method(cGzipReader, "each_char", rb_gzreader_each_char, 0); - rb_define_method(cGzipReader, "bytes", rb_gzreader_each_byte, 0); rb_define_method(cGzipReader, "ungetc", rb_gzreader_ungetc, 1); rb_define_method(cGzipReader, "ungetbyte", rb_gzreader_ungetbyte, 1); rb_define_method(cGzipReader, "gets", rb_gzreader_gets, -1); rb_define_method(cGzipReader, "readline", rb_gzreader_readline, -1); rb_define_method(cGzipReader, "each", rb_gzreader_each, -1); rb_define_method(cGzipReader, "each_line", rb_gzreader_each, -1); - rb_define_method(cGzipReader, "lines", rb_gzreader_each, -1); rb_define_method(cGzipReader, "readlines", rb_gzreader_readlines, -1); + rb_define_method(cGzipReader, "external_encoding", rb_gzreader_external_encoding, 0); + + rb_define_singleton_method(mZlib, "gzip", zlib_s_gzip, -1); + rb_define_singleton_method(mZlib, "gunzip", zlib_gunzip, 1); - /* From GzipFile#os_code - code of current host */ + /* The OS code of current host */ rb_define_const(mZlib, "OS_CODE", INT2FIX(OS_CODE)); - /* From GzipFile#os_code - 0x00 */ + /* OS code for MSDOS hosts */ rb_define_const(mZlib, "OS_MSDOS", INT2FIX(OS_MSDOS)); - /* From GzipFile#os_code - 0x01 */ + /* OS code for Amiga hosts */ rb_define_const(mZlib, "OS_AMIGA", INT2FIX(OS_AMIGA)); - /* From GzipFile#os_code - 0x02 */ + /* OS code for VMS hosts */ rb_define_const(mZlib, "OS_VMS", INT2FIX(OS_VMS)); - /* From GzipFile#os_code - 0x03 */ + /* OS code for UNIX hosts */ rb_define_const(mZlib, "OS_UNIX", INT2FIX(OS_UNIX)); - /* From GzipFile#os_code - 0x05 */ + /* OS code for Atari hosts */ rb_define_const(mZlib, "OS_ATARI", INT2FIX(OS_ATARI)); - /* From GzipFile#os_code - 0x06 */ + /* OS code for OS2 hosts */ rb_define_const(mZlib, "OS_OS2", INT2FIX(OS_OS2)); - /* From GzipFile#os_code - 0x07 */ + /* OS code for Mac OS hosts */ rb_define_const(mZlib, "OS_MACOS", INT2FIX(OS_MACOS)); - /* From GzipFile#os_code - 0x0a */ + /* OS code for TOPS-20 hosts */ rb_define_const(mZlib, "OS_TOPS20", INT2FIX(OS_TOPS20)); - /* From GzipFile#os_code - 0x0b */ + /* OS code for Win32 hosts */ rb_define_const(mZlib, "OS_WIN32", INT2FIX(OS_WIN32)); - - /* From GzipFile#os_code - 0x04 */ + /* OS code for VM OS hosts */ rb_define_const(mZlib, "OS_VMCMS", INT2FIX(OS_VMCMS)); - /* From GzipFile#os_code - 0x08 */ + /* OS code for Z-System hosts */ rb_define_const(mZlib, "OS_ZSYSTEM", INT2FIX(OS_ZSYSTEM)); - /* From GzipFile#os_code - 0x09 */ + /* OS code for CP/M hosts */ rb_define_const(mZlib, "OS_CPM", INT2FIX(OS_CPM)); - /* From GzipFile#os_code - 0x0c */ + /* OS code for QDOS hosts */ rb_define_const(mZlib, "OS_QDOS", INT2FIX(OS_QDOS)); - /* From GzipFile#os_code - 0x0d */ + /* OS code for RISC OS hosts */ rb_define_const(mZlib, "OS_RISCOS", INT2FIX(OS_RISCOS)); - /* From GzipFile#os_code - 0xff */ + /* OS code for unknown hosts */ rb_define_const(mZlib, "OS_UNKNOWN", INT2FIX(OS_UNKNOWN)); + id_level = rb_intern("level"); + id_strategy = rb_intern("strategy"); + id_buffer = rb_intern("buffer"); #endif /* GZIP_SUPPORT */ } @@ -4270,6 +5041,7 @@ Init_zlib() * - Zlib::MemError * - Zlib::BufError * - Zlib::VersionError + * - Zlib::InProgressError * */ @@ -4345,6 +5117,20 @@ Init_zlib() */ /* + * Document-class: Zlib::InProgressError + * + * Subclass of Zlib::Error. This error is raised when the zlib + * stream is currently in progress. + * + * For example: + * + * inflater = Zlib::Inflate.new + * inflater.inflate(compressed) do + * inflater.inflate(compressed) # Raises Zlib::InProgressError + * end + */ + +/* * Document-class: Zlib::GzipFile::Error * * Base class of errors that occur when processing GZIP files. @@ -4369,5 +5155,3 @@ Init_zlib() * Raised when the data length recorded in the gzip file footer is not equivalent * to the length of the actual uncompressed data. */ - - diff --git a/ext/zlib/zlib.gemspec b/ext/zlib/zlib.gemspec new file mode 100644 index 0000000000..ba7114476f --- /dev/null +++ b/ext/zlib/zlib.gemspec @@ -0,0 +1,31 @@ +# coding: utf-8 +# frozen_string_literal: true + +source_version = ["", "ext/zlib/"].find do |dir| + begin + break File.open(File.join(__dir__, "#{dir}zlib.c")) {|f| + f.gets("\n#define RUBY_ZLIB_VERSION ") + f.gets[/\s*"(.+)"/, 1] + } + rescue Errno::ENOENT + end +end + +Gem::Specification.new do |spec| + spec.name = "zlib" + spec.version = source_version + spec.authors = ["Yukihiro Matsumoto", "UENO Katsuhiro"] + spec.email = ["matz@ruby-lang.org", nil] + + spec.summary = %q{Ruby interface for the zlib compression/decompression library} + spec.description = %q{Ruby interface for the zlib compression/decompression library} + spec.homepage = "https://github.com/ruby/zlib" + spec.licenses = ["Ruby", "BSD-2-Clause"] + + spec.files = ["COPYING", "BSDL", "README.md", "ext/zlib/extconf.rb", "ext/zlib/zlib.c", "zlib.gemspec"] + spec.bindir = "exe" + spec.executables = [] + spec.require_paths = ["lib"] + spec.extensions = "ext/zlib/extconf.rb" + spec.required_ruby_version = ">= 2.7.0" +end |
