diff options
Diffstat (limited to 'ext/zlib')
| -rw-r--r-- | ext/zlib/.cvsignore | 4 | ||||
| -rw-r--r-- | ext/zlib/.gitignore | 1 | ||||
| -rw-r--r-- | ext/zlib/depend | 177 | ||||
| -rw-r--r-- | ext/zlib/doc/zlib.rd | 917 | ||||
| -rw-r--r-- | ext/zlib/extconf.rb | 103 | ||||
| -rw-r--r-- | ext/zlib/zlib.c | 2700 | ||||
| -rw-r--r-- | ext/zlib/zlib.gemspec | 31 |
7 files changed, 2386 insertions, 1547 deletions
diff --git a/ext/zlib/.cvsignore b/ext/zlib/.cvsignore deleted file mode 100644 index 814345ece8..0000000000 --- a/ext/zlib/.cvsignore +++ /dev/null @@ -1,4 +0,0 @@ -Makefile -mkmf.log -*.def -extconf.h 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/doc/zlib.rd b/ext/zlib/doc/zlib.rd deleted file mode 100644 index 0a5e2d0abf..0000000000 --- a/ext/zlib/doc/zlib.rd +++ /dev/null @@ -1,917 +0,0 @@ -=begin -# -# zlib.rd.src -# -# Copyright (C) UENO Katsuhiro 2000-2003 -# -# $Id$ -# - -= Ruby/zlib version 0.6.0 - -Ruby/zlib is an extension library to use zlib from Ruby. -Ruby/zlib also provides the features for accessing gzipped files. - -You can modify or redistribute Ruby/zlib in the same manner of -Ruby interpreter. The latest version of Ruby/zlib would be found -at ((<URL:http://www.blue.sky.or.jp/>)). - -Any comments and suggestions are always welcome. Please send -them to ruby-list ML, ruby-ext ML, ruby-talk ML, or the author's -mail address ((<URL:mailto:katsu@blue.sky.or.jp>)). - -This document is experimental and broken English version. -If you find some mistakes or strange expressions (including -kidding or unnatural ones) in this document, please let me know -for my study. - -* ((<Zlib>)) - - * ((<Zlib::Error>)) - * ((<Zlib::ZStream>)) - * ((<Zlib::Deflate>)) - * ((<Zlib::Inflate>)) - * ((<Zlib::GzipFile>)) - * ((<Zlib::GzipFile::Error>)) - * ((<Zlib::GzipWriter>)) - * ((<Zlib::GzipReader>)) - -* ((<Changes from 0.5 to 0.6>)) -* ((<Changes from 0.4 to 0.5>)) - -== Zlib - -Zlib is the module which provides the other features in zlib C -library. See zlib.h for detail of each module function. - -=== Module Functions: - ---- Zlib.zlib_version - - Returns the string which represents the version of zlib - library. - ---- Zlib.adler32([string[, adler]]) - - Calculates Alder-32 checksum for ((|string|)), - and returns updated value of ((|alder|)). - If ((|string|)) is omitted, it returns the Adler-32 initial - value. If ((|alder|)) is omitted, it assumes that the initial - value is given to ((|alder|)). - ---- 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. ((|crc|)) is omitted, - it assumes that the initial value is given to ((|crc|)). - ---- Zlib.crc_table - - Returns the table for calculating CRC checksum as an array. - -=== Constants: - ---- 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. - - -== Zlib::Error - -The superclass for all exceptions raised by Ruby/zlib. - -The following exceptions are defined as subclasses of Zlib::Error. -These exceptions are raised when zlib library functions return -with an error status. - - * Zlib::StreamEnd - * Zlib::NeedDict - * Zlib::DataError - * Zlib::StreamError - * Zlib::MemError - * Zlib::BufError - * Zlib::VersionError - -=== SuperClass: - -* StandardError - - -== Zlib::ZStream - -The abstract class for the stream which handles the compressed -data. The operations are defined in the subclasses, -((<Zlib::Deflate>)) for compression, and ((<Zlib::Inflate>)) -for decompression. - -An instance of Zlib::ZStream has one stream (struct zstream) and -two variable-length buffers which associated to the input -(next_in) of the stream and the output (next_out) of the stream. -In this document, "input buffer" means the buffer for input, and -"output buffer" means the buffer for output. - -Data inputed into an instance of Zlib::ZStream are temporally -stored into the end of input buffer, and then data in input buffer -are processed from the beginning of the buffer until no more -output from the stream is produced (i.e. until avail_out > 0 -after processing). During processing, output buffer is allocated -and expanded automatically to hold all output data. - -Some particular instance methods consume the data in output buffer -and return them as a String. - -Here is an ascii art for describing above: - - +================ an instance of Zlib::ZStream ================+ - || || - || +--------+ +-------+ +--------+ || - || +--| output |<---------|zstream|<---------| input |<--+ || - || | | buffer | next_out+-------+next_in | buffer | | || - || | +--------+ +--------+ | || - || | | || - +===|======================================================|===+ - | | - v | - "output data" "input data" - -If an error is occurred during processing input buffer, -an exception which is a subclass of ((<Zlib::Error>)) is raised. -At that time, both input and output buffer keeps their conditions -at the time when the error is occurred. - -=== SuperClass: - -* Object - -=== Class Methods: - ---- Zlib::ZStream.new - - See ((<Zlib::Deflate.new>)) and ((<Zlib::Inflate.new>)). - -=== Methods: - ---- Zlib::ZStream#avail_in - - Returns bytes of data in input buffer. - Normally, returns 0. - ---- Zlib::ZStream#avail_out - - Returns bytes of free spaces in output buffer. - Because the free spaces are allocated automatically, - this method returns 0 normally. - ---- Zlib::ZStream#avail_out = size - - Allocates free spaces of ((|size|)) bytes in output buffer. - If there are more than ((|size|)) bytes spaces in the buffer, - the buffer is truncated. - Because the free spaces are allocated automatically, - you usually need not to use this method. - ---- Zlib::ZStream#flush_next_in - - Flushes input buffer and returns all data in that buffer. - ---- Zlib::ZStream#flush_next_out - - Flushes output buffer and returns all data in that buffer. - ---- Zlib::ZStream#total_in - - Returns the total bytes of the input data to the stream. - ---- Zlib::ZStream#total_out - - Returns the total bytes of the output data from the stream. - ---- Zlib::ZStream#data_type - - Guesses the type of the data which have been inputed into - the stream. The returned value is either ((<Zlib::BINARY>)), - ((<Zlib::ASCII>)), or ((<Zlib::UNKNOWN>)). - ---- Zlib::ZStream#adler - - Returns the alder-32 checksum. - ---- Zlib::ZStream#reset - - Resets and initializes the stream. All data in both - input and output buffer are discarded. - ---- Zlib::ZStream#finish - - Finishes the stream and flushes output buffer. - See ((<Zlib::Deflate#finish>)) and ((<Zlib::Inflate#finish>)) - for detail of the behavior. - ---- Zlib::ZStream#finished? ---- Zlib::ZStream#stream_end? - - Returns true if the stream is finished. - ---- Zlib::ZStream#close ---- Zlib::ZStream#end - - Closes the stream. All operations on the closed stream - will raise an exception. - ---- Zlib::ZStream#closed? ---- Zlib::ZStream#ended? - - Returns true if the stream closed. - - -== Zlib::Deflate - -The class for compressing string data. - -=== SuperClass: - -* ((<Zlib::ZStream>)) - -=== Class Methods: - ---- Zlib::Deflate.deflate(string[, level]) - - Compresses ((|string|)). The avail values of ((|level|)) are - ((<Zlib::NO_COMPRESSION>)), ((<Zlib::BEST_SPEED>)), - ((<Zlib::BEST_COMPRESSION>)), ((<Zlib::DEFAULT_COMPRESSION>)), - and the 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::FINISH) - z.close - dst - end - ---- Zlib::Deflate.new([level[, windowBits[, memlevel[, strategy]]]]) - - 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. - -=== Methods: - ---- Zlib::Deflate#clone - - Duplicates the deflate stream. - ---- Zlib::Deflate#deflate(string[, flush]) - - Inputs ((|string|)) into the deflate stream and returns - the output from the stream. Calling this method, - both input and output buffer of the stream are flushed. - If ((|string|)) is nil, this method finishes the stream, - just like ((<Zlib::ZStream#finish>)). - The value of ((|flush|)) should be either ((<Zlib::NO_FLUSH>)), - ((<Zlib::SYNC_FLUSH>)), ((<Zlib::FULL_FLUSH>)), or - ((<Zlib::FINISH>)). - See zlib.h for details. - ---- Zlib::Deflate#<< string - - Inputs ((|string|)) into the deflate stream just like - ((<Zlib::Deflate#deflate>)), but returns Zlib::Deflate object - itself. The output from the stream is preserved in output - buffer. - ---- Zlib::Deflate#flush([flush]) - - This method is equivalent to (({deflate('', ((|flush|)))})). - If ((|flush|)) is omitted, ((<Zlib::SYNC_FLUSH>)) is used - as ((|flush|)). This method is just provided for - readability of your Ruby script. - ---- Zlib::Deflate#finish - - Finishes the stream. This method is equivalent to - (({deflate('', Zlib::FINISH)})). - ---- Zlib::Deflate#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. - ---- Zlib::Deflate#set_dictionary(string) - - Sets the preset dictionary and returns ((|string|)). - This method is available just only after - ((<Zlib::Deflate.new>)) or ((<Zlib::ZStream#reset>)) method - was called. See zlib.h for details. - - -== Zlib::Inflate - -The class for decompressing compressed data. -Unlike ((<Zlib::Deflate>)), an instance of this class is not able -to duplicate (clone, dup) itself. - -=== SuperClass: - -* ((<Zlib::ZStream>)) - -=== Class Methods: - ---- Zlib::Inflate.inflate(string) - - Decompresses ((|string|)). Raises a ((<Zlib::NeedDict>)) - exception if a preset dictionary is needed for decompression. - - This method is almost equivalent to the following code: - - def inflate(string) - zstream = Zlib::Inflate.new - buf = zstream.inflate(string) - zstream.finish - zstream.close - buf - end - ---- Zlib::Inflate.new([windowBits]) - - Creates a new inflate stream for decompression. - See zlib.h for details of the argument. - If ((|windowBits|)) is nil, the default value is used. - -=== Methods: - ---- Zlib::Inflate#inflate(string) - - Inputs ((|string|)) into the inflate stream and returns - the output from the stream. Calling this method, - both input and 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. - ---- Zlib::Inflate#<< string - - Inputs ((|string|)) into the inflate stream just like - ((<Zlib::Inflate#inflate>)), but returns Zlib::Inflate object - itself. The output from the stream is preserved in output - buffer. - ---- Zlib::Inflate#finish - - Finishes the inflate stream and returns the garbage - following the compressed data. Raises an exception - if the stream is not finished - (i.e. ((<Zlib::ZStream#finished?>)) doesn't returns true). - - The inflate stream finishes itself as soon as it meets - the end code of the compressed data, you need not to call - this method explicitly. However, this method is useful - for checking whether the data is correctly ended or not. - ---- Zlib::Inflate#set_dictionary(string) - - Sets the preset dictionary and returns ((|string|)) - This method is available just only after a ((<Zlib::NeedDict>)) - exception was raised. See zlib.h for details. - ---- Zlib::Inflate#sync(string) - - Inputs ((|string|)) into the end of input buffer and - skips data until a full flush point can be found. - If the point is found in the buffer, this method flushes - the buffer and returns false. Otherwise it returns true - and the following data of full flush point is preserved - in the buffer. - ---- Zlib::Inflate#sync_point? - - What is this? - - -== Zlib::GzipFile - -The abstract class for handling a gzip formatted compressed file. -The operations are defined in the subclasses, -((<Zlib::GzipReader>)) for reading, and ((<Zlib::GzipWriter>)) -for writing. - -GzipReader should be used with associating an instance of IO class -(or an object which has the same methods as IO has). - -=== SuperClass: - -* Object - -=== Class Methods: - ---- Zlib::GzipFile.new(args...) - - See ((<Zlib::GzipReader.new>)) and ((<Zlib::GzipWriter.new>)). - ---- Zlib::GzipFile.wrap(args...) {|gz| ... } - - See ((<Zlib::GzipReader.wrap>)) and ((<Zlib::GzipWriter.wrap>)). - ---- Zlib::GzipFile.open(args...) {|gz| ... } - - See ((<Zlib::GzipReader.open>)) and ((<Zlib::GzipWriter.open>)). - -=== Methods: - ---- Zlib::GzipFile#closed? ---- Zlib::GzipFile#to_io - - Same as IO. - ---- Zlib::GzipFile#close - - Closes the GzipFile object. This method calls close method - of the associated IO object. Returns the associated IO object. - ---- Zlib::GzipFile#finish - - Closes the GzipFile object. Unlike ((<Zlib::GzipFile#close>)), - this method ((*never*)) calls close method of the associated IO - object. Returns the associated IO object. - ---- Zlib::GzipFile#crc - - Returns CRC value of the uncompressed data. - ---- Zlib::GzipFile#level - - Returns compression level. - ---- Zlib::GzipFile#mtime - - Returns last modification time recorded in the gzip - file header. - ---- Zlib::GzipFile#os_code - - Returns OS code number recorded in the gzip file header. - ---- Zlib::GzipFile#orig_name - - Returns original filename recorded in the gzip file header, - or nil if original filename is not present. - ---- Zlib::GzipFile#comment - - Returns comments recorded in the gzip file header, or - nil if the comments is not present. - ---- Zlib::GzipFile#sync ---- Zlib::GzipFile#sync= flag - - Same as IO. If ((|flag|)) is true, the associated IO object - must respond to flush method. While `sync' mode is true, - the compression ratio decreases sharply. - ---- Zlib::GzipFile#path - - Returns the path string of the associated IO-like object. This - method is only defined when the IO-like object responds to - #path(). - - -== Zlib::GzipFile::Error - -The superclass for all exceptions raised during processing a gzip -file. - -The following exceptions are defined as subclasses of -Zlib::GzipFile::Error. - -: Zlib::GzipFile::NoFooter - - Raised when gzip file footer has not found. - -: Zlib::GzipFile::CRCError - - Raised when the CRC checksum recorded in gzip file footer - is not equivalent to CRC checksum of the actually - uncompressed data. - -: Zlib::GzipFile::LengthError - - Raised when the data length recorded in gzip file footer - is not equivalent to length of the actually uncompressed data. - -=== SuperClass: - -* ((<Zlib::Error>)) - - -== Zlib::GzipReader - -The class for reading a gzipped file. GzipReader should be used -with associating an instance of IO class (or an object which has -the same methods as IO has). - - Zlib::GzipReader.open('hoge.gz') {|gz| - print gz.read - } - - f = File.open('hoge.gz') - gz = Zlib::GzipReader.new(f) - print gz.read - gz.close - -=== SuperClass: - -* ((<Zlib::GzipFile>)) - -=== Included Modules: - -* Enumerable - -=== Class Methods: - ---- Zlib::GzipReader.new(io) - - Creates a GzipReader object associated with ((|io|)). - The GzipReader object reads gzipped data from ((|io|)), - and parses/decompresses them. At least, ((|io|)) must have - read method that behaves same as read method in IO class. - - If the gzip file header is incorrect, raises an - ((<Zlib::GzipFile::Error>)) exception. - ---- Zlib::GzipReader.wrap(io) {|gz| ... } - - Creates a GzipReader object associated with ((|io|)), and - executes the block with the newly created GzipReader object, - just like File::open. The GzipReader 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. - ---- Zlib::GzipReader.open(filename) ---- Zlib::GzipReader.open(filename) {|gz| ... } - - Opens a file specified by ((|filename|)) as a gzipped file, - and returns a GzipReader object associated with that file. - Further details of this method are same as - ((<Zlib::GzipReader.new>)) and ((<ZLib::GzipReader.wrap>)). - -=== ¥á¥½¥Ã¥É: - ---- Zlib::GzipReader#eof ---- Zlib::GzipReader#eof? - - Returns true if the object reaches the end of compressed data. - Note that eof? does ((*not*)) return true when reaches the - end of ((*file*)). - ---- Zlib::GzipReader#pos ---- Zlib::GzipReader#tell - - Returns the total bytes of data decompressed until now. - Not that it does ((*not*)) the position of file pointer. - ---- Zlib::GzipReader#each([rs]) ---- Zlib::GzipReader#each_line([rs]) ---- Zlib::GzipReader#each_byte([rs]) ---- Zlib::GzipReader#gets([rs]) ---- Zlib::GzipReader#getc ---- Zlib::GzipReader#lineno ---- Zlib::GzipReader#lineno= ---- Zlib::GzipReader#read([length]) ---- Zlib::GzipReader#readchar ---- Zlib::GzipReader#readline([rs]) ---- Zlib::GzipReader#readlines([rs]) ---- Zlib::GzipReader#ungetc(char) - - Same as IO, but raises ((<Zlib::Error>)) or - ((<Zlib::GzipFile::Error>)) exception if an error was found - in the gzip file. - - Be careful of the footer of gzip file. A gzip file has - the checksum of pre-compressed data in its footer. - GzipReader checks all uncompressed data against that checksum - at the following cases, and if failed, raises - ((<Zlib::GzipFile::NoFooter>)), ((<Zlib::GzipFile::CRCError>)), - or ((<Zlib::GzipFile::LengthError>)) exception. - - * When an reading request is received beyond the end of file - (the end of compressed data). - That is, when ((<Zlib::GzipReader#read>)), - ((<Zlib::GzipReader#gets>)), or some other methods for reading - returns nil. - - * When ((<Zlib::GzipFile#close>)) method is called after - the object reaches the end of file. - - * When ((<Zlib::GzipReader#unused>)) method is called after - the object reaches the end of file. - ---- Zlib::GzipReader#rewind - - Resets the position of the file pointer to the point - created the GzipReader object. - The associated IO object need to respond to seek method. - ---- Zlib::GzipReader#unused - - Returns the rest of the data which had read for parsing gzip - format, or nil if the whole gzip file is not parsed yet. - - -== Zlib::GzipWriter - -The class for writing a gzipped file. GzipWriter should be used -with associate with an instance of IO class (or an object which -has the same methods as IO has). - - Zlib::GzipWriter.open('hoge.gz') {|gz| - gz.write 'jugemu jugemu gokou no surikire...' - } - - f = File.open('hoge.gz', 'w') - gz = Zlib::GzipWriter.new(f) - gz.write 'jugemu jugemu gokou no surikire...' - gz.close - -NOTE: Due to the limitation in finalizer of Ruby, you must close -explicitly GzipWriter object by ((<Zlib::GzipWriter#close>)) etc. -Otherwise, GzipWriter should be not able to write gzip footer and -generate broken gzip file. - -=== SuperClass: - -* ((<Zlib::GzipFile>)) - -=== Class Methods: - ---- Zlib::GzipWriter.new(io[, level[, strategy]]) - - Creates a GzipWriter object associated with ((|io|)). - ((|level|)) and ((|strategy|)) should be same as the - arguments of ((<Zlib::Deflate.new>)). The GzipWriter object - writes gzipped data to ((|io|)). At least, ((|io|)) must - respond to write method that behaves same as write method - in IO class. - ---- Zlib::GzipWriter.wrap(io[, level[, strategy]]) {|gz| ... } - - Creates a GzipWriter object associated with ((|io|)), and - executes the block with the newly created GzipWriter object, - just like File::open. The GzipWriter 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. - ---- Zlib::GzipWriter.open(filename[, level[, strategy]]) ---- Zlib::GzipWriter.open(filename[, level[, strategy]]) {|gz| ... } - - Opens a file specified by ((|filename|)) for writing - gzip compressed data, and returns a GzipWriter object - associated with that file. Further details of this method - are same as ((<Zlib::GzipWriter.new>)) and - ((<Zlib::GzipWriter#wrap>)). - - -=== Methods: - ---- Zlib::GzipWriter#close ---- Zlib::GzipWriter#finish - - Closes the GzipFile object. This method calls close method - of the associated IO object. Returns the associated IO object. - See ((<Zlib::GzipFile#close>)) and ((<Zlib::GzipFile#finish>)) - for the difference between close and finish. - - ((*NOTE: Due to the limitation in finalizer of Ruby, you must - close GzipWriter object explicitly. Otherwise, GzipWriter - should be not able to write gzip footer and generate broken - gzip file.*)) - ---- Zlib::GzipWriter#pos ---- Zlib::GzipWriter#tell - - Returns the total bytes of data compressed until now. - Note that it does ((*not*)) the position of file pointer. - ---- Zlib::GzipWriter#<< str ---- Zlib::GzipWriter#putc(ch) ---- Zlib::GzipWriter#puts(obj...) ---- Zlib::GzipWriter#print(arg...) ---- Zlib::GzipWriter#printf(format, arg...) ---- Zlib::GzipWriter#write(str) - - Same as IO. - ---- Zlib::GzipWriter#flush([flush]) - - Flushes all the internal buffers of the GzipWriter object. - The meaning of ((|flush|)) is same as one of the argument of - ((<Zlib::Deflate#deflate>)). - ((<Zlib::SYNC_FLUSH>)) is used if ((|flush|)) is omitted. - It is no use giving ((|flush|)) ((<Zlib::NO_FLUSH>)). - ---- Zlib::GzipWriter#mtime= time - - Sets last modification time to be stored in the gzip file - header. ((<Zlib::GzipFile::Error>)) exception will be raised - if this method is called after writing method (like - ((<Zlib::GzipWriter#write>))) was called. - ---- Zlib::GzipWriter#orig_name= filename - - Sets original filename to be stored in the gzip file header. - ((<Zlib::GzipFile::Error>)) exception will be raised - if this method is called after writing method (like - ((<Zlib::GzipWriter#write>))) was called. - ---- Zlib::GzipWriter#comment= string - - Sets comments to be stored in the gzip file header. - ((<Zlib::GzipFile::Error>)) exception will be raised - if this method is called after writing method (like - ((<Zlib::GzipWriter#write>))) was called. - - -== Changes from 0.5 to 0.6 - -* New methods: - - * ((<Zlib::GzipFile.wrap>)) - * ((<Zlib::GzipFile#finish>)) - -* New constants: - - * ((<Zlib::ZLIB_VERSION>)) - * ((<Zlib::OS_VMCMS>)) - * ((<Zlib::OS_ZSYSTEM>)) - * ((<Zlib::OS_CPM>)) - * ((<Zlib::OS_QDOS>)) - * ((<Zlib::OS_RISCOS>)) - * ((<Zlib::OS_UNKNOWN>)) - -* Changed methods: - - * ((<Zlib::GzipFile.new>)) now takes no block. Use - ((<Zlib::GzipFile.wrap>)) instead. - - * ((<Zlib::GzipFile#close>)) now takes no argument. Use - ((<Zlib::GzipFile#finish>)) instead. - -* Renamed methods: - - * Zlib.version is renamed to ((<Zlib.zlib_version>)). - -* Changed constants: - - * ((<Zlib::VERSION>)) indicates the version of Ruby/zlib. - The zlib.h version is now in ((<Zlib::ZLIB_VERSION>)). - -* Backward compatibility: - - * For backward compatibility for 0.5, the obsoleted methods and - arguments are still available. - - * Obsoleted classes, methods, and constants for backward - compatibility for 0.4 or earlier are removed. - -== Changes from 0.4 to 0.5 - -Almost all the code are rewritten. -I hope all changes are enumerated below :-) - -* The names of almost classes and some methods are changed. - All classes and constants are now defined under module - ((<Zlib>)). The obsoleted names are also available for backward - compatibility. - - * Classes - - * Deflate -> ((<Zlib::Deflate>)) - * Inflate -> ((<Zlib::Inflate>)) - * Zlib::Gzip -> ((<Zlib::GzipFile>)) - * GzipReader -> ((<Zlib::GzipReader>)) - * GzipWriter -> ((<Zlib::GzipWriter>)) - * Zlib::Gzip::Error -> ((<Zlib::GzipFile::Error>)) - * Zlib::GzipReader::NoFooter -> ((<Zlib::GzipFile::NoFooter>)) - * Zlib::GzipReader::CRCError -> ((<Zlib::GzipFile::CRCError>)) - * Zlib::GzipReader::LengthError -> ((<Zlib::GzipFile::LengthError>)) - - * Constants - - * Zlib::ZStream::BINARY -> ((<Zlib::BINARY>)) - * Zlib::ZStream::ASCII -> ((<Zlib::ASCII>)) - * Zlib::ZStream::UNKNOWN -> ((<Zlib::UNKNOWN>)) - * Zlib::Deflate::NO_COMPRESSION -> ((<Zlib::NO_COMPRESSION>)) - * Zlib::Deflate::BEST_SPEED -> ((<Zlib::BEST_SPEED>)) - * Zlib::Deflate::BEST_COMPRESSION -> ((<Zlib::BEST_COMPRESSION>)) - * Zlib::Deflate::DEFAULT_COMPRESSION -> ((<Zlib::DEFAULT_COMPRESSION>)) - * Zlib::Deflate::FILTERED -> ((<Zlib::FILTERED>)) - * Zlib::Deflate::HUFFMAN_ONLY -> ((<Zlib::HUFFMAN_ONLY>)) - * Zlib::Deflate::DEFAULT_STRATEGY -> ((<Zlib::DEFAULT_STRATEGY>)) - * Zlib::Deflate::MAX_WBITS -> ((<Zlib::MAX_WBITS>)) - * Zlib::Deflate::DEF_MEM_LEVEL -> ((<Zlib::DEF_MEM_LEVEL>)) - * Zlib::Deflate::MAX_MEM_LEVEL -> ((<Zlib::MAX_MEM_LEVEL>)) - * Zlib::Deflate::NO_FLUSH -> ((<Zlib::NO_FLUSH>)) - * Zlib::Deflate::SYNC_FLUSH -> ((<Zlib::SYNC_FLUSH>)) - * Zlib::Deflate::FULL_FLUSH -> ((<Zlib::FULL_FLUSH>)) - * Zlib::Inflate::MAX_WBITS -> ((<Zlib::MAX_WBITS>)) - * Zlib::GzipReader::OS_* -> ((<Zlib::OS_*|Zlib::OS_CODE>)) - - * Methods - - * Zlib::ZStream#flush_out -> ((<Zlib::ZStream#flush_next_out>)) - -* Made buffer for input (next_in). - -* ((<Zlib::GzipReader#unused>)) returns nil after closing. - -* Now you are up to call ((<Zlib::GzipWriter#close>)) explicitly -to avoid segv in finalizer. -((<[ruby-dev:11915]|URL:http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-dev/11915>)) - -* divided initialize from new. - -* remove sanity checks for arguments for deflateInit2 and - inflateInit2. - -* adapted the behavior of ((<Zlib::GzipWriter#puts>)) to Ruby-1.7. - -* Made all functions static. - - -=end diff --git a/ext/zlib/extconf.rb b/ext/zlib/extconf.rb index 53b971b189..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,14 +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}) - create_makefile('zlib') + 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') {|conf| + if zsrc + conf.concat addconf if addconf + end + conf + } end diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index a865cf384f..481d74b2b6 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -9,12 +9,27 @@ #include <ruby.h> #include <zlib.h> #include <time.h> -#include <ruby/encoding.h> - -#define RUBY_ZLIB_VERSION "0.6.0" +#include <ruby/io.h> +#include <ruby/thread.h> + +#ifdef HAVE_VALGRIND_MEMCHECK_H +# include <valgrind/memcheck.h> +# ifndef VALGRIND_MAKE_MEM_DEFINED +# define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n)) +# endif +# ifndef VALGRIND_MAKE_MEM_UNDEFINED +# define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n)) +# endif +#else +# define VALGRIND_MAKE_MEM_DEFINED(p, n) 0 +# define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0 +#endif +#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 @@ -29,12 +44,36 @@ #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) +{ + if (n > UINT_MAX) n = UINT_MAX; + return (uInt)n; +} +#define MAX_UINT(n) max_uint(n) +#else +#define MAX_UINT(n) (uInt)(n) +#endif + +#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); @@ -44,25 +83,28 @@ 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*, int); -static void zstream_append_buffer(struct zstream*, const Bytef*, int); +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*, int); -static void zstream_buffer_ungets(struct zstream*, const Bytef*, int); +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*, unsigned int); -static void zstream_discard_input(struct zstream*, unsigned int); +static void zstream_append_input(struct zstream*, const Bytef*, long); +static void zstream_discard_input(struct zstream*, long); static void zstream_reset_input(struct zstream*); 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 void zstream_run(struct zstream*, Bytef*, uInt, int); -static VALUE zstream_sync(struct zstream*, Bytef*, uInt); -static void zstream_mark(struct zstream*); -static void zstream_free(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(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*); @@ -99,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); @@ -107,30 +149,30 @@ 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*, int); +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_write(struct gzfile*, Bytef*, uInt); -static long gzfile_read_more(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*, VALUE outbuf); static void gzfile_calc_crc(struct gzfile*, VALUE); -static VALUE gzfile_read(struct gzfile*, int); -static VALUE gzfile_read_all(struct gzfile*); -static void gzfile_ungets(struct gzfile*, const Bytef*, int); +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); static void gzfile_writer_end(struct gzfile*); @@ -142,6 +184,8 @@ static struct gzfile *get_gzfile(VALUE); static VALUE gzfile_ensure_close(VALUE); static VALUE rb_gzfile_s_wrap(int, VALUE*, VALUE); static VALUE gzfile_s_open(int, VALUE*, VALUE, const char*); +NORETURN(static void gzfile_raise(struct gzfile *, VALUE, const char *)); +static VALUE gzfile_error_inspect(VALUE); static VALUE rb_gzfile_to_io(VALUE); static VALUE rb_gzfile_crc(VALUE); @@ -169,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); @@ -191,16 +235,85 @@ static VALUE rb_gzreader_each(int, VALUE*, VALUE); static VALUE rb_gzreader_readlines(int, VALUE*, VALUE); #endif /* GZIP_SUPPORT */ - +/* + * Document-module: Zlib + * + * 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) + * + * 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 + * + * - Zlib::Deflate + * - Zlib::Inflate + * - Zlib::ZStream + * - Zlib::Error + * - Zlib::StreamEnd + * - Zlib::NeedDict + * - Zlib::DataError + * - Zlib::StreamError + * - Zlib::MemError + * - Zlib::BufError + * - Zlib::VersionError + * - Zlib::InProgressError + * + * (if you have GZIP_SUPPORT) + * - Zlib::GzipReader + * - Zlib::GzipWriter + * - Zlib::GzipFile + * - Zlib::GzipFile::Error + * - Zlib::GzipFile::LengthError + * - Zlib::GzipFile::CRCError + * - Zlib::GzipFile::NoFooter + * + */ void Init_zlib(void); -int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p); -VALUE rb_str_conv_enc_opts(VALUE, rb_encoding*, rb_encoding*, int, VALUE); - /*--------- 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) @@ -237,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); @@ -253,30 +363,51 @@ 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 } /*-------- module Zlib --------*/ /* + * Document-method: Zlib.zlib_version + * * Returns the string which represents the version of zlib library. */ static VALUE rb_zlib_version(VALUE klass) { - VALUE str; + return rb_str_new2(zlibVersion()); +} - str = rb_str_new2(zlibVersion()); - OBJ_TAINT(str); /* for safe */ - return str; +#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(mask32(sum), ptr, UINT_MAX); + ptr += UINT_MAX; + len -= UINT_MAX; + } while (len >= UINT_MAX); + } + if (len > 0) sum = func(mask32(sum), ptr, (uInt)len); + return sum; } +#else +#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; @@ -294,23 +425,43 @@ 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); - sum = func(sum, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str)); + sum = checksum_long(func, sum, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str)); } return rb_uint2inum(sum); } /* + * Document-method: Zlib.adler32 + * * call-seq: Zlib.adler32(string, adler) * * 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) @@ -318,12 +469,37 @@ rb_zlib_adler32(int argc, VALUE *argv, VALUE klass) return do_checksum(argc, argv, adler32); } +#ifdef HAVE_ADLER32_COMBINE /* - * call-seq: Zlib.crc32(string, adler) + * Document-method: Zlib.adler32_combine + * + * call-seq: Zlib.adler32_combine(adler1, adler2, len2) + * + * 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+. + * + */ +static VALUE +rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2) +{ + return ULONG2NUM( + adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2))); +} +#else +#define rb_zlib_adler32_combine rb_f_notimplement +#endif + +/* + * Document-method: Zlib.crc32 + * + * 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. */ @@ -333,13 +509,40 @@ rb_zlib_crc32(int argc, VALUE *argv, VALUE klass) return do_checksum(argc, argv, crc32); } +#ifdef HAVE_CRC32_COMBINE +/* + * Document-method: Zlib.crc32_combine + * + * call-seq: Zlib.crc32_combine(crc1, crc2, len2) + * + * Combine two CRC-32 check values in to one. +crc1+ is the first CRC-32 + * value, +crc2+ is the second CRC-32 value. +len2+ is the length of the + * string used to generate +crc2+. + * + */ +static VALUE +rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2) +{ + return ULONG2NUM( + crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2))); +} +#else +#define rb_zlib_crc32_combine rb_f_notimplement +#endif + /* + * Document-method: Zlib.crc_table + * * Returns the table for calculating CRC checksum as an array. */ 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; @@ -359,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); @@ -369,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 @@ -394,11 +608,26 @@ 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) { - return 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 */ + (void)VALGRIND_MAKE_MEM_DEFINED(p, items * size); + return p; } static void @@ -412,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; @@ -431,82 +660,123 @@ 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) ? - 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 -zstream_expand_buffer_into(struct zstream *z, int size) +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 = size; - RBASIC(z->buf)->klass = 0; + z->stream.avail_out = MAX_UINT(size); + 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; - z->stream.avail_out = size; + 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, int len) +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 { if (z->stream.avail_out >= (uInt)len) { - z->stream.avail_out -= len; + z->stream.avail_out -= (uInt)len; } else { 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) \ @@ -517,86 +787,108 @@ 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, int len) -{ - VALUE dst; - - 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; - z->stream.avail_out = RSTRING_LEN(z->buf) - z->buf_filled; - if (z->stream.avail_out > ZSTREAM_AVAIL_OUT_STEP_MAX) { - z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; + 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; } + z->stream.avail_out = (uInt)buflen; return dst; } static void -zstream_buffer_ungets(struct zstream *z, const Bytef *b, int len) +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; - z->stream.avail_out-=len; + z->stream.avail_out-=(uInt)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 -zstream_append_input(struct zstream *z, const Bytef *src, unsigned int len) +zstream_append_input(struct zstream *z, const Bytef *src, long len) { if (len <= 0) return; 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); @@ -604,25 +896,55 @@ zstream_append_input(struct zstream *z, const Bytef *src, unsigned int len) } #define zstream_append_input2(z,v)\ + RB_GC_GUARD(v),\ zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v)) static void -zstream_discard_input(struct zstream *z, unsigned int len) +zstream_discard_input(struct zstream *z, long len) { - if (NIL_P(z->input) || (unsigned int)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 @@ -644,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; } @@ -662,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); @@ -691,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, uInt 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*)""; @@ -704,67 +1150,107 @@ zstream_run(struct zstream *z, Bytef *src, uInt len, int flush) } else { zstream_append_input(z, src, len); - z->stream.next_in = (Bytef*)RSTRING_PTR(z->input); - z->stream.avail_in = 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, uInt len) +zstream_sync(struct zstream *z, Bytef *src, long len) { - VALUE rest; + /* VALUE rest; */ int err; if (!NIL_P(z->input)) { z->stream.next_in = (Bytef*)RSTRING_PTR(z->input); - z->stream.avail_in = RSTRING_LEN(z->input); + z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input)); err = inflateSync(&z->stream); if (err == Z_OK) { zstream_discard_input(z, @@ -774,7 +1260,7 @@ zstream_sync(struct zstream *z, Bytef *src, uInt 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); } } @@ -782,24 +1268,26 @@ zstream_sync(struct zstream *z, Bytef *src, uInt len) if (len <= 0) return Qfalse; z->stream.next_in = src; - z->stream.avail_in = len; + z->stream.avail_in = MAX_UINT(len); err = inflateSync(&z->stream); if (err == Z_OK) { zstream_append_input(z, z->stream.next_in, z->stream.avail_in); 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 @@ -813,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; } @@ -841,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"); } @@ -935,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 @@ -960,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); } /* @@ -989,13 +1500,13 @@ 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); } /* * Allocates +size+ bytes of free space in the output buffer. If there are more - * than +size+ bytes already in the buffer, the buffer is truncated. Because + * than +size+ bytes already in the buffer, the buffer is truncated. Because * free space is allocated automatically, you usually don't need to use this * method. */ @@ -1004,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; } @@ -1016,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))); } @@ -1039,9 +1549,9 @@ rb_zstream_total_out(VALUE obj) } /* - * Guesses the type of the data which have been inputed into the stream. The - * returned value is either <tt>Zlib::BINARY</tt>, <tt>Zlib::ASCII</tt>, or - * <tt>Zlib::UNKNOWN</tt>. + * 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>. */ static VALUE rb_zstream_data_type(VALUE obj) @@ -1055,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); } /* @@ -1074,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; } @@ -1084,13 +1594,13 @@ rb_zstream_closed_p(VALUE obj) /* * Document-class: Zlib::Deflate * - * Zlib::Deflate is the class for compressing data. See Zlib::Stream for more + * Zlib::Deflate is the class for compressing data. See Zlib::ZStream for more * information. */ #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) @@ -1106,13 +1616,77 @@ rb_deflate_s_allocate(VALUE klass) } /* - * call-seq: Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil) + * Document-method: Zlib::Deflate.new + * + * call-seq: + * Zlib::Deflate.new(level=DEFAULT_COMPRESSION, window_bits=MAX_WBITS, mem_level=DEF_MEM_LEVEL, strategy=DEFAULT_STRATEGY) * - * 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. + * Creates a new deflate stream for compression. If a given argument is nil, + * the default value of that argument is used. * - * TODO: document better! + * 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: + * + * * Zlib::DEFAULT_COMPRESSION + * * Zlib::NO_COMPRESSION + * * Zlib::BEST_SPEED + * * Zlib::BEST_COMPRESSION + * + * 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. + * + * 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: + * + * * Zlib::DEF_MEM_LEVEL + * * Zlib::MAX_MEM_LEVEL + * + * The +strategy+ sets the deflate compression strategy. The following + * strategies are available: + * + * 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 + * + * See the constants for further description. + * + * == 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) @@ -1122,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), @@ -1136,6 +1710,8 @@ rb_deflate_initialize(int argc, VALUE *argv, VALUE obj) } /* + * Document-method: Zlib::Deflate#initialize_copy + * * Duplicates the deflate stream. */ static VALUE @@ -1144,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; @@ -1170,12 +1746,15 @@ deflate_run(VALUE args) } /* - * call-seq: Zlib::Deflate.deflate(string[, level]) + * Document-method: Zlib::Deflate.deflate + * + * call-seq: + * Zlib.deflate(string[, level]) + * Zlib::Deflate.deflate(string[, level]) * * Compresses the given +string+. Valid values of level are - * <tt>Zlib::NO_COMPRESSION</tt>, <tt>Zlib::BEST_SPEED</tt>, - * <tt>Zlib::BEST_COMPRESSION</tt>, <tt>Zlib::DEFAULT_COMPRESSION</tt>, and an - * integer from 0 to 9. + * 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: * @@ -1186,7 +1765,7 @@ deflate_run(VALUE args) * dst * end * - * TODO: what's default value of +level+? + * See also Zlib.inflate * */ static VALUE @@ -1209,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; } @@ -1228,36 +1806,62 @@ 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); +} + /* - * call-seq: deflate(string[, flush]) + * Document-method: Zlib::Deflate#deflate + * + * 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. * - * The value of +flush+ should be either <tt>Zlib::NO_FLUSH</tt>, - * <tt>Zlib::SYNC_FLUSH</tt>, <tt>Zlib::FULL_FLUSH</tt>, or - * <tt>Zlib::FINISH</tt>. See zlib.h for details. + * 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: + * + * 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. * - * TODO: document better! */ 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#<< + * * call-seq: << string * * Inputs +string+ into the deflate stream just like Zlib::Deflate#deflate, but @@ -1267,25 +1871,30 @@ 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; } /* - * call-seq: flush(flush) + * Document-method: Zlib::Deflate#flush + * + * call-seq: + * flush(flush = Zlib::SYNC_FLUSH) -> String + * flush(flush = Zlib::SYNC_FLUSH) { |chunk| ... } -> nil * - * This method is equivalent to <tt>deflate('', flush)</tt>. If flush is omitted, - * <tt>Zlib::SYNC_FLUSH</tt> is used as flush. This method is just provided - * to improve the readability of your Ruby program. + * 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. * - * TODO: document better! + * 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); @@ -1293,20 +1902,21 @@ 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); } /* + * Document-method: Zlib::Deflate.params + * * 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. * - * TODO: document better! + * 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. + * + * See Zlib::Deflate.new for a description of +level+ and +strategy+. + * */ static VALUE rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) @@ -1314,32 +1924,44 @@ rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) struct zstream *z = get_zstream(obj); int level, strategy; int err; + uInt n; + long filled; level = ARG_LEVEL(v_level); strategy = ARG_STRATEGY(v_strategy); - zstream_run(z, (Bytef*)"", 0, Z_SYNC_FLUSH); + n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); + 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); + 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; } /* + * Document-method: Zlib::Deflate.set_dictionary + * * call-seq: set_dictionary(string) * * Sets the preset dictionary and returns +string+. This method is available * just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called. * See zlib.h for details. * - * TODO: document better! + * Can raise errors of Z_STREAM_ERROR if a parameter is invalid (such as + * NULL dictionary) or the stream state is inconsistent, Z_DATA_ERROR if + * the given dictionary doesn't match the expected one (incorrect adler32 value) + * */ static VALUE rb_deflate_set_dictionary(VALUE obj, VALUE dic) @@ -1348,10 +1970,9 @@ 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_LEN(src)); + (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } @@ -1370,21 +1991,58 @@ 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; } /* - * call-seq: Zlib::Inflate.new(window_bits) + * Document-method: Zlib::Inflate.new + * + * call-seq: + * Zlib::Inflate.new(window_bits = Zlib::MAX_WBITS) * - * 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. + * Creates a new inflate stream for decompression. +window_bits+ sets the + * size of the history buffer and can have the following values: + * + * 0:: + * Have inflate use the window size from the zlib header of the compressed + * stream. + * + * (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. + * + * 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). + * + * (-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. + * + * This is for use with other formats that use the deflate compressed data + * format such as zip which provide their own check values. + * + * == Example + * + * open "compressed.file" do |compressed_io| + * zi = Zlib::Inflate.new(Zlib::MAX_WBITS + 32) + * + * begin + * open "uncompressed.file", "w+" do |uncompressed_io| + * uncompressed_io << zi.inflate(compressed_io.read) + * end + * ensure + * zi.close + * end + * end * - * TODO: document better! */ static VALUE rb_inflate_initialize(int argc, VALUE *argv, VALUE obj) @@ -1394,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) { @@ -1417,7 +2075,11 @@ inflate_run(VALUE args) } /* - * call-seq: Zlib::Inflate.inflate(string) + * Document-method: Zlib::inflate + * + * call-seq: + * Zlib.inflate(string) + * Zlib::Inflate.inflate(string) * * Decompresses +string+. Raises a Zlib::NeedDict exception if a preset * dictionary is needed for decompression. @@ -1432,6 +2094,8 @@ inflate_run(VALUE args) * buf * end * + * See also Zlib.deflate + * */ static VALUE rb_inflate_s_inflate(VALUE obj, VALUE src) @@ -1450,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; } @@ -1464,32 +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); } } -/* - * call-seq: inflate(string) +/* Document-method: Zlib::Inflate#add_dictionary * - * 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. + * call-seq: add_dictionary(string) * - * 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>) - * - * TODO: document better! + * 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); + + return obj; +} + +struct rb_zlib_inflate_arguments { + struct zstream *z; + int argc; + VALUE *argv; +}; - OBJ_INFECT(obj, src); +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)) { @@ -1498,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 { @@ -1509,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 @@ -1525,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); @@ -1556,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)); } @@ -1585,10 +2342,11 @@ rb_inflate_sync_point_p(VALUE obj) } /* + * Document-method: Zlib::Inflate#set_dictionary + * * Sets the preset dictionary and returns +string+. This method is available just * only after a Zlib::NeedDict exception was raised. See zlib.h for details. * - * TODO: document better! */ static VALUE rb_inflate_set_dictionary(VALUE obj, VALUE dic) @@ -1597,10 +2355,9 @@ 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_LEN(src)); + (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } @@ -1654,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; +static ID id_write, id_readpartial, id_flush, id_seek, id_close, id_path, id_input; static VALUE cGzError, cNoFooter, cCRCError, cLengthError; @@ -1665,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; - int ungetc; + 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 @@ -1686,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); @@ -1705,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)) { @@ -1715,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); +} + +static const rb_data_type_t gzfile_data_type = { + "gzfile", + { gzfile_mark, gzfile_free, gzfile_memsize, }, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY +}; - obj = Data_Make_Struct(klass, struct gzfile, gzfile_mark, gzfile_free, gz); +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; @@ -1747,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; } @@ -1760,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; @@ -1789,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)) @@ -1802,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); } @@ -1825,24 +2607,31 @@ 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, int 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); - if (NIL_P(str)) return Qfalse; + str = gzfile_read_raw(gz, outbuf); + if (NIL_P(str)) return 0; zstream_append_input2(&gz->z, str); } - return Qtrue; + return 1; } static char * @@ -1855,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"); } @@ -1895,6 +2684,36 @@ gzfile_set32(unsigned long n, unsigned char *dst) } static void +gzfile_raise(struct gzfile *gz, VALUE klass, const char *message) +{ + VALUE exc = rb_exc_new2(klass, message); + if (!NIL_P(gz->z.input)) { + rb_ivar_set(exc, id_input, rb_str_resurrect(gz->z.input)); + } + rb_exc_raise(exc); +} + +/* + * Document-method: Zlib::GzipFile::Error#inspect + * + * Constructs a String of the GzipFile Error + */ +static VALUE +gzfile_error_inspect(VALUE error) +{ + VALUE str = rb_call_super(0, 0); + VALUE input = rb_attr_get(error, id_input); + + if (!NIL_P(input)) { + rb_str_resize(str, RSTRING_LEN(str)-1); + rb_str_cat2(str, ", input="); + rb_str_append(str, rb_str_inspect(input)); + rb_str_cat2(str, ">"); + } + return str; +} + +static void gzfile_make_header(struct gzfile *gz) { Bytef buf[10]; /* the size of gzip header */ @@ -1906,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); } @@ -1924,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); @@ -1945,25 +2764,26 @@ 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 */ - rb_raise(cGzError, "not in gzip format"); + /* 10 is the size of gzip header */ + if (!gzfile_read_raw_ensure(gz, 10, outbuf)) { + gzfile_raise(gz, cGzError, "not in gzip format"); } head = (unsigned char*)RSTRING_PTR(gz->z.input); if (head[0] != GZ_MAGIC1 || head[1] != GZ_MAGIC2) { - rb_raise(cGzError, "not in gzip format"); + gzfile_raise(gz, cGzError, "not in gzip format"); } if (head[2] != GZ_METHOD_DEFLATE) { rb_raise(cGzError, "unsupported compression method %d", head[2]); @@ -1995,27 +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, 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, 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); } @@ -2025,14 +2849,15 @@ 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 */ - rb_raise(cNoFooter, "footer is not found"); + /* 8 is the size of gzip footer */ + if (!gzfile_read_raw_ensure(gz, 8, outbuf)) { + gzfile_raise(gz, cNoFooter, "footer is not found"); } crc = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input)); @@ -2044,20 +2869,20 @@ gzfile_check_footer(struct gzfile *gz) if (gz->crc != crc) { rb_raise(cCRCError, "invalid compressed data -- crc error"); } - if (gz->z.stream.total_out != length) { + if ((uint32_t)gz->z.stream.total_out != length) { rb_raise(cLengthError, "invalid compressed data -- length error"); } } static void -gzfile_write(struct gzfile *gz, Bytef *str, uInt len) +gzfile_write(struct gzfile *gz, Bytef *str, long len) { if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) { gzfile_make_header(gz); } if (len > 0 || (gz->z.flags & GZFILE_FLAG_SYNC)) { - gz->crc = crc32(gz->crc, str, len); + gz->crc = checksum_long(crc32, gz->crc, str, len); zstream_run(&gz->z, str, len, (gz->z.flags & GZFILE_FLAG_SYNC) ? Z_SYNC_FLUSH : Z_NO_FLUSH); } @@ -2065,12 +2890,12 @@ gzfile_write(struct gzfile *gz, Bytef *str, uInt 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"); @@ -2080,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 @@ -2093,8 +2919,8 @@ gzfile_calc_crc(struct gzfile *gz, VALUE str) gz->ungetc -= RSTRING_LEN(str); } else { - gz->crc = crc32(gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc, - RSTRING_LEN(str) - gz->ungetc); + gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc, + RSTRING_LEN(str) - gz->ungetc); gz->ungetc = 0; } } @@ -2104,53 +2930,69 @@ 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, gz->ecflags, gz->ecopts); } -static VALUE -gzfile_read(struct gzfile *gz, int len) +static long +gzfile_fill(struct gzfile *gz, long len, VALUE outbuf) { - VALUE dst; - if (len < 0) - rb_raise(rb_eArgError, "negative length %d given", len); + rb_raise(rb_eArgError, "negative length %ld given", len); if (len == 0) - return rb_str_new(0, 0); - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { - gzfile_read_more(gz); + return 0; + 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 Qnil; + return -1; } + return len < ZSTREAM_BUF_FILLED(&gz->z) ? len : ZSTREAM_BUF_FILLED(&gz->z); +} - dst = zstream_shift_buffer(&gz->z, len); - gzfile_calc_crc(gz, dst); +static VALUE +gzfile_read(struct gzfile *gz, long len, VALUE outbuf) +{ + VALUE 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; } static VALUE -gzfile_readpartial(struct gzfile *gz, int len, VALUE outbuf) +gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf) { VALUE dst; if (len < 0) - rb_raise(rb_eArgError, "negative length %d given", len); - - if (!NIL_P(outbuf)) - OBJ_TAINT(outbuf); + rb_raise(rb_eArgError, "negative length %ld given", len); if (len == 0) { if (NIL_P(outbuf)) @@ -2160,48 +3002,44 @@ gzfile_readpartial(struct gzfile *gz, int 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); } @@ -2212,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; } @@ -2225,34 +3063,30 @@ 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); } } static void -gzfile_ungets(struct gzfile *gz, const Bytef *b, int len) +gzfile_ungets(struct gzfile *gz, const Bytef *b, long len) { zstream_buffer_ungets(&gz->z, b, len); gz->ungetc+=len; @@ -2287,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 @@ -2297,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; @@ -2309,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 @@ -2334,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_dup(gz->z.input); - OBJ_TAINT(str); /* for safe */ + str = rb_str_resurrect(gz->z.input); return str; } @@ -2348,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"); } @@ -2366,28 +3199,81 @@ get_gzfile(VALUE obj) * Zlib::GzipReader for reading, and Zlib::GzipWriter for writing. * * GzipReader should be used by associating an IO, or IO-like, object. + * + * == Method Catalogue + * + * - ::wrap + * - ::open (Zlib::GzipReader::open and Zlib::GzipWriter::open) + * - #close + * - #closed? + * - #comment + * - comment= (Zlib::GzipWriter#comment=) + * - #crc + * - eof? (Zlib::GzipReader#eof?) + * - #finish + * - #level + * - lineno (Zlib::GzipReader#lineno) + * - lineno= (Zlib::GzipReader#lineno=) + * - #mtime + * - mtime= (Zlib::GzipWriter#mtime=) + * - #orig_name + * - orig_name (Zlib::GzipWriter#orig_name=) + * - #os_code + * - path (when the underlying IO supports #path) + * - #sync + * - #sync= + * - #to_io + * + * (due to internal structure, documentation may appear under Zlib::GzipReader + * or Zlib::GzipWriter) */ +typedef struct { + int argc; + VALUE *argv; + VALUE klass; +} new_wrap_arg_t; + +static VALUE +new_wrap(VALUE tmp) +{ + new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp; + return rb_class_new_instance_kw(arg->argc, arg->argv, arg->klass, RB_PASS_CALLED_KEYWORDS); +} + static VALUE 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); } return Qnil; } -/* - * See Zlib::GzipReader#wrap and Zlib::GzipWriter#wrap. - */ static VALUE -rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass) +gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error) { - VALUE obj = rb_class_new_instance(argc, argv, klass); + VALUE obj; + + if (close_io_on_error) { + int state = 0; + new_wrap_arg_t arg; + arg.argc = argc; + arg.argv = argv; + arg.klass = klass; + obj = rb_protect(new_wrap, (VALUE)&arg, &state); + if (state) { + rb_io_close(argv[0]); + rb_jump_tag(state); + } + } + else { + obj = rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS); + } if (rb_block_given_p()) { return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj); @@ -2398,6 +3284,29 @@ rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass) } /* + * Document-method: Zlib::GzipFile.wrap + * + * 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. + * + * 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) +{ + return gzfile_wrap(argc, argv, klass, 0); +} + +/* + * Document-method: Zlib::GzipFile.open + * * See Zlib::GzipReader#open and Zlib::GzipWriter#open. */ static VALUE @@ -2405,16 +3314,16 @@ 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; - return rb_gzfile_s_wrap(argc, argv, klass); + return gzfile_wrap(argc, argv, klass, 1); } /* + * Document-method: Zlib::GzipFile#to_io + * * Same as IO. */ static VALUE @@ -2424,6 +3333,8 @@ rb_gzfile_to_io(VALUE obj) } /* + * Document-method: Zlib::GzipFile#crc + * * Returns CRC value of the uncompressed data. */ static VALUE @@ -2433,6 +3344,8 @@ rb_gzfile_crc(VALUE obj) } /* + * Document-method: Zlib::GzipFile#mtime + * * Returns last modification time recorded in the gzip file header. */ static VALUE @@ -2442,6 +3355,8 @@ rb_gzfile_mtime(VALUE obj) } /* + * Document-method: Zlib::GzipFile#level + * * Returns compression level. */ static VALUE @@ -2451,6 +3366,8 @@ rb_gzfile_level(VALUE obj) } /* + * Document-method: Zlib::GzipFile#os_code + * * Returns OS code number recorded in the gzip file header. */ static VALUE @@ -2460,6 +3377,8 @@ rb_gzfile_os_code(VALUE obj) } /* + * Document-method: Zlib::GzipFile#orig_name + * * Returns original filename recorded in the gzip file header, or +nil+ if * original filename is not present. */ @@ -2470,11 +3389,12 @@ rb_gzfile_orig_name(VALUE obj) if (!NIL_P(str)) { str = rb_str_dup(str); } - OBJ_TAINT(str); /* for safe */ return str; } /* + * Document-method: Zlib::GzipFile#comment + * * Returns comments recorded in the gzip file header, or nil if the comments * is not present. */ @@ -2485,12 +3405,13 @@ rb_gzfile_comment(VALUE obj) if (!NIL_P(str)) { str = rb_str_dup(str); } - OBJ_TAINT(str); /* for safe */ return str; } /* - * ??? + * Document-method: Zlib::GzipFile#lineno + * + * The line number of the last row read from this file. */ static VALUE rb_gzfile_lineno(VALUE obj) @@ -2499,7 +3420,9 @@ rb_gzfile_lineno(VALUE obj) } /* - * ??? + * Document-method: Zlib::GzipReader#lineno= + * + * Specify line number of the last row read from this file. */ static VALUE rb_gzfile_set_lineno(VALUE obj, VALUE lineno) @@ -2510,7 +3433,23 @@ rb_gzfile_set_lineno(VALUE obj, VALUE lineno) } /* - * ??? + * Document-method: Zlib::GzipWriter#mtime= + * + * Specify the modification time (+mtime+) in the gzip header. + * 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) @@ -2522,18 +3461,17 @@ 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) ? FIX2INT(val) : rb_big2ulong(val); - } + val = rb_Integer(mtime); + gz->mtime = NUM2UINT(val); + gz->z.flags |= GZFILE_FLAG_MTIME_IS_SET; + return mtime; } /* - * ??? + * Document-method: Zlib::GzipFile#orig_name= + * + * Specify the original name (+str+) in the gzip header. */ static VALUE rb_gzfile_set_orig_name(VALUE obj, VALUE str) @@ -2555,7 +3493,9 @@ rb_gzfile_set_orig_name(VALUE obj, VALUE str) } /* - * ??? + * Document-method: Zlib::GzipFile#comment= + * + * Specify the comment (+str+) in the gzip header. */ static VALUE rb_gzfile_set_comment(VALUE obj, VALUE str) @@ -2577,21 +3517,29 @@ rb_gzfile_set_comment(VALUE obj, VALUE str) } /* + * Document-method: Zlib::GzipFile#close + * * Closes the GzipFile object. This method calls close method of the * associated IO object. Returns the associated IO object. */ 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; } /* + * Document-method: Zlib::GzipFile#finish + * * Closes the GzipFile object. Unlike Zlib::GzipFile#close, this method never * calls the close method of the associated IO object. Returns the associated IO * object. @@ -2608,28 +3556,39 @@ rb_gzfile_finish(VALUE obj) } /* - * Same as IO. + * Document-method: Zlib::GzipFile#closed? + * + * Same as IO#closed? + * */ 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; } /* - * ??? + * Document-method: Zlib::GzipFile#eof? + * + * Returns +true+ or +false+ whether the stream has reached the end. */ 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; } /* - * Same as IO. + * Document-method: Zlib::GzipFile#sync + * + * Same as IO#sync + * */ static VALUE rb_gzfile_sync(VALUE obj) @@ -2638,6 +3597,8 @@ rb_gzfile_sync(VALUE obj) } /* + * Document-method: Zlib::GzipFile#sync= + * * call-seq: sync = flag * * Same as IO. If flag is +true+, the associated IO object must respond to the @@ -2659,7 +3620,9 @@ rb_gzfile_set_sync(VALUE obj, VALUE mode) } /* - * ??? + * Document-method: Zlib::GzipFile#total_in + * + * Total number of input bytes read so far. */ static VALUE rb_gzfile_total_in(VALUE obj) @@ -2668,17 +3631,26 @@ rb_gzfile_total_in(VALUE obj) } /* - * ??? + * Document-method: Zlib::GzipFile#total_out + * + * Total number of output bytes output so far. */ 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)); + } } /* - * Document-method: path + * Document-method: Zlib::GzipFile#path * * call-seq: path * @@ -2689,15 +3661,25 @@ 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) { if (!NIL_P(opts)) { - rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2); + rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2, NULL); } if (gz->enc2) { gz->ecflags = rb_econv_prepare_opts(opts, &opts); @@ -2713,9 +3695,9 @@ rb_gzfile_ecopts(struct gzfile *gz, VALUE opts) * Document-class: Zlib::GzipWriter * * Zlib::GzipWriter is a class for writing gzipped files. GzipWriter should - * be used with an instance of IO, or IO-like, object. + * be used with an instance of IO, or IO-like, object. * - * For example: + * Following two example generate the same result. * * Zlib::GzipWriter.open('hoge.gz') do |gz| * gz.write 'jugemu jugemu gokou no surikire...' @@ -2727,8 +3709,14 @@ rb_gzfile_ecopts(struct gzfile *gz, VALUE opts) * gz.close * end * - * # TODO: test these. Are they equivalent? Can GzipWriter.new take a - * # block? + * To make like gzip(1) does, run following: + * + * orig = 'hoge.txt' + * Zlib::GzipWriter.open('hoge.gz') do |gz| + * gz.mtime = File.mtime(orig) + * gz.orig_name = orig + * gz.write IO.binread(orig) + * end * * NOTE: Due to the limitation of Ruby's finalizer, you must explicitly close * GzipWriter objects by Zlib::GzipWriter#close etc. Otherwise, GzipWriter @@ -2747,7 +3735,7 @@ rb_gzwriter_s_allocate(VALUE klass) * * Opens a file specified by +filename+ for writing gzip compressed data, and * returns a GzipWriter object associated with that file. Further details of - * this method are found in Zlib::GzipWriter.new and Zlib::GzipWriter#wrap. + * this method are found in Zlib::GzipWriter.new and Zlib::GzipFile.wrap. */ static VALUE rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass) @@ -2756,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) @@ -2774,9 +3767,9 @@ rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj) opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash"); if (!NIL_P(opt)) argc--; } - + 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); @@ -2790,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; @@ -2829,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); } /* @@ -2885,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 @@ -2897,9 +3896,6 @@ rb_gzwriter_putc(VALUE obj, VALUE ch) * gz.close * end * - * # TODO: test these. Are they equivalent? Can GzipReader.new take a - * # block? - * * == Method Catalogue * * The following methods in Zlib::GzipReader are just like their counterparts @@ -2943,11 +3939,13 @@ rb_gzreader_s_allocate(VALUE klass) } /* + * Document-method: Zlib::GzipReader.open + * * call-seq: Zlib::GzipReader.open(filename) {|gz| ... } * * Opens a file specified by +filename+ as a gzipped file, and returns a * GzipReader object associated with that file. Further details of this method - * are in Zlib::GzipReader.new and ZLib::GzipReader.wrap. + * are in Zlib::GzipReader.new and ZLib::GzipFile.wrap. */ static VALUE rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass) @@ -2956,11 +3954,72 @@ rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass) } /* - * call-seq: Zlib::GzipReader.new(io) + * 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, 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. @@ -2972,12 +4031,8 @@ rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj) struct gzfile *gz; int err; - Data_Get_Struct(obj, struct gzfile, gz); - if (argc > 1) { - opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash"); - if (!NIL_P(opt)) argc--; - } - rb_scan_args(argc, argv, "1", &io); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); + rb_scan_args(argc, argv, "1:", &io, &opt); /* this is undocumented feature of zlib */ err = inflateInit2(&gz->z.stream, -MAX_WBITS); @@ -2986,18 +4041,20 @@ 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; } /* + * Document-method: Zlib::GzipReader#rewind + * * Resets the position of the file pointer to the point created the GzipReader * object. The associated IO object needs to respond to the +seek+ method. */ @@ -3010,6 +4067,8 @@ rb_gzreader_rewind(VALUE obj) } /* + * Document-method: Zlib::GzipReader#unused + * * Returns the rest of the data which had read for parsing gzip format, or * +nil+ if the whole gzip file is not parsed yet. */ @@ -3017,37 +4076,41 @@ 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); } /* + * Document-method: Zlib::GzipReader#read + * * See Zlib::GzipReader documentation for a description. */ static VALUE rb_gzreader_read(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); - VALUE vlen; - int len; + 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 %d given", len); + rb_raise(rb_eArgError, "negative length %ld given", len); } - return gzfile_read(gz, len); + return gzfile_read(gz, len, outbuf); } /* + * Document-method: Zlib::GzipReader#readpartial + * * 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. @@ -3058,13 +4121,13 @@ rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); VALUE vlen, outbuf; - int len; + long len; rb_scan_args(argc, argv, "11", &vlen, &outbuf); len = NUM2INT(vlen); if (len < 0) { - rb_raise(rb_eArgError, "negative length %d given", len); + rb_raise(rb_eArgError, "negative length %ld given", len); } if (!NIL_P(outbuf)) Check_Type(outbuf, T_STRING); @@ -3072,6 +4135,8 @@ rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj) } /* + * Document-method: Zlib::GzipReader#getc + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3083,6 +4148,8 @@ rb_gzreader_getc(VALUE obj) } /* + * Document-method: Zlib::GzipReader#readchar + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3097,6 +4164,8 @@ rb_gzreader_readchar(VALUE obj) } /* + * Document-method: Zlib::GzipReader#getbyte + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3105,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); } @@ -3113,6 +4182,8 @@ rb_gzreader_getbyte(VALUE obj) } /* + * Document-method: Zlib::GzipReader#readbyte + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3127,6 +4198,8 @@ rb_gzreader_readbyte(VALUE obj) } /* + * Document-method: Zlib::GzipReader#each_char + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3143,6 +4216,8 @@ rb_gzreader_each_char(VALUE obj) } /* + * Document-method: Zlib::GzipReader#each_byte + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3159,6 +4234,8 @@ rb_gzreader_each_byte(VALUE obj) } /* + * Document-method: Zlib::GzipReader#ungetc + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3174,10 +4251,13 @@ 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; } /* + * Document-method: Zlib::GzipReader#ungetbyte + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3195,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); } @@ -3226,32 +4307,87 @@ rscheck(const char *rsptr, long rslen, VALUE rs) rb_raise(rb_eRuntimeError, "rs modified"); } +static long +gzreader_charboundary(struct gzfile *gz, long n) +{ + char *s = RSTRING_PTR(gz->z.buf); + 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) { + 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_bytes)) { + return l + MBCLEN_CHARFOUND_LEN(n_bytes); + } + } + return n; +} + 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; - long rslen, n; + long rslen, n, limit = -1; int rspara; + rb_encoding *enc = gz->enc; + int maxlen = rb_enc_mbmaxlen(enc); if (argc == 0) { rs = rb_rs; } else { - rb_scan_args(argc, argv, "1", &rs); - if (!NIL_P(rs)) { - Check_Type(rs, T_STRING); + VALUE lim, tmp; + + rb_scan_args(argc, argv, "11", &rs, &lim); + if (!NIL_P(lim)) { + if (!NIL_P(rs)) StringValue(rs); + } + else if (!NIL_P(rs)) { + tmp = rb_check_string_type(rs); + if (NIL_P(tmp)) { + lim = rs; + rs = rb_rs; + } + else { + rs = tmp; + } + } + if (!NIL_P(lim)) { + limit = NUM2LONG(lim); + if (limit == 0) return rb_str_new(0,0); } } if (NIL_P(rs)) { - dst = gzfile_read_all(gz); - if (RSTRING_LEN(dst) != 0) gz->lineno++; - else + if (limit < 0) { + dst = gzfile_read_all(gz, Qnil); + if (RSTRING_LEN(dst) == 0) return Qnil; + } + else if ((n = gzfile_fill(gz, limit, Qnil)) <= 0) { return Qnil; + } + else { + if (maxlen > 1 && n >= limit && !GZFILE_IS_FINISHED(gz)) { + n = gzreader_charboundary(gz, n); + } + else { + n = limit; + } + dst = zstream_shift_buffer(&gz->z, n, Qnil); + if (NIL_P(dst)) return dst; + gzfile_calc_crc(gz, dst); + dst = gzfile_newstr(gz, dst); + } + gz->lineno++; return dst; } @@ -3259,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; @@ -3269,45 +4406,62 @@ 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 (;;) { - if (n > gz->z.buf_filled) { + long 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); - res = memchr(p, rsptr[0], (gz->z.buf_filled - n + 1)); + filled = ZSTREAM_BUF_FILLED(&gz->z); + if (limit > 0 && filled >= limit) { + filled = limit; + } + res = memchr(p, rsptr[0], (filled - n + 1)); if (!res) { - n = gz->z.buf_filled + 1; - } else { + n = filled; + if (limit > 0 && filled >= limit) break; + n++; + } + else { n += (long)(res - p); p = res; if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break; p++, n++; } } + 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); } /* + * 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) @@ -3321,6 +4475,8 @@ rb_gzreader_gets(int argc, VALUE *argv, VALUE obj) } /* + * Document-method: Zlib::GzipReader#readline + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3335,6 +4491,8 @@ rb_gzreader_readline(int argc, VALUE *argv, VALUE obj) } /* + * Document-method: Zlib::GzipReader#each + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3351,6 +4509,8 @@ rb_gzreader_each(int argc, VALUE *argv, VALUE obj) } /* + * Document-method: Zlib::GzipReader#readlines + * * See Zlib::GzipReader documentation for a description. */ static VALUE @@ -3364,92 +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); + +/* + * 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); /* - * 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. + * call-seq: + * Zlib.gunzip(src) -> String + * + * Decode the given gzipped +string+. + * + * 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; @@ -3457,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); @@ -3465,13 +4733,18 @@ 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); + rb_define_module_function(mZlib, "adler32_combine", rb_zlib_adler32_combine, 3); rb_define_module_function(mZlib, "crc32", rb_zlib_crc32, -1); + rb_define_module_function(mZlib, "crc32_combine", rb_zlib_crc32_combine, 3); rb_define_module_function(mZlib, "crc_table", rb_zlib_crc_table, 0); + /* The Ruby/zlib version string. */ rb_define_const(mZlib, "VERSION", rb_str_new2(RUBY_ZLIB_VERSION)); + /* The string which represents the version of zlib.h */ rb_define_const(mZlib, "ZLIB_VERSION", rb_str_new2(ZLIB_VERSION)); cZStream = rb_define_class_under(mZlib, "ZStream", rb_cObject); @@ -3494,12 +4767,34 @@ 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); + /* Represents binary data as guessed by deflate. + * + * See Zlib::Deflate#data_type. */ rb_define_const(mZlib, "BINARY", INT2FIX(Z_BINARY)); + + /* 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)); + +#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); rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1); + rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1); rb_define_alloc_func(cDeflate, rb_deflate_s_allocate); rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1); rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1); @@ -3511,31 +4806,94 @@ Init_zlib() cInflate = rb_define_class_under(mZlib, "Inflate", cZStream); rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1); + 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); + /* 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)); + /* Fastest compression level, but with the lowest space savings. */ rb_define_const(mZlib, "BEST_SPEED", INT2FIX(Z_BEST_SPEED)); + /* Slowest compression level, but with the best space savings. */ rb_define_const(mZlib, "BEST_COMPRESSION", INT2FIX(Z_BEST_COMPRESSION)); + /* Default compression level which is a good trade-off between space and + * time + */ rb_define_const(mZlib, "DEFAULT_COMPRESSION", INT2FIX(Z_DEFAULT_COMPRESSION)); + /* 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)); + + /* Deflate strategy which uses Huffman codes only (no string matching). */ rb_define_const(mZlib, "HUFFMAN_ONLY", INT2FIX(Z_HUFFMAN_ONLY)); + +#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 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)); + + /* The default memory level for allocating zlib deflate compression state. + */ rb_define_const(mZlib, "DEF_MEM_LEVEL", INT2FIX(DEF_MEM_LEVEL)); + + /* The maximum memory level for allocating zlib deflate compression state. + */ rb_define_const(mZlib, "MAX_MEM_LEVEL", INT2FIX(MAX_MEM_LEVEL)); + /* 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)); + + /* 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)); + + /* 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)); + + /* Processes all pending input and flushes pending output. */ rb_define_const(mZlib, "FINISH", INT2FIX(Z_FINISH)); #if GZIP_SUPPORT @@ -3546,10 +4904,15 @@ Init_zlib() id_seek = rb_intern("seek"); id_close = rb_intern("close"); id_path = rb_intern("path"); + id_input = rb_intern("@input"); cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject); cGzError = rb_define_class_under(cGzipFile, "Error", cZError); + /* input gzipped string */ + rb_define_attr(cGzError, "input", 1, 0); + rb_define_method(cGzError, "inspect", gzfile_error_inspect, 0); + cNoFooter = rb_define_class_under(cGzipFile, "NoFooter", cGzError); cCRCError = rb_define_class_under(cGzipFile, "CRCError", cGzError); cLengthError = rb_define_class_under(cGzipFile,"LengthError",cGzError); @@ -3588,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); @@ -3596,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); @@ -3608,34 +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); + /* The OS code of current host */ rb_define_const(mZlib, "OS_CODE", INT2FIX(OS_CODE)); + /* OS code for MSDOS hosts */ rb_define_const(mZlib, "OS_MSDOS", INT2FIX(OS_MSDOS)); + /* OS code for Amiga hosts */ rb_define_const(mZlib, "OS_AMIGA", INT2FIX(OS_AMIGA)); + /* OS code for VMS hosts */ rb_define_const(mZlib, "OS_VMS", INT2FIX(OS_VMS)); + /* OS code for UNIX hosts */ rb_define_const(mZlib, "OS_UNIX", INT2FIX(OS_UNIX)); + /* OS code for Atari hosts */ rb_define_const(mZlib, "OS_ATARI", INT2FIX(OS_ATARI)); + /* OS code for OS2 hosts */ rb_define_const(mZlib, "OS_OS2", INT2FIX(OS_OS2)); + /* OS code for Mac OS hosts */ rb_define_const(mZlib, "OS_MACOS", INT2FIX(OS_MACOS)); + /* OS code for TOPS-20 hosts */ rb_define_const(mZlib, "OS_TOPS20", INT2FIX(OS_TOPS20)); + /* OS code for Win32 hosts */ rb_define_const(mZlib, "OS_WIN32", INT2FIX(OS_WIN32)); - + /* OS code for VM OS hosts */ rb_define_const(mZlib, "OS_VMCMS", INT2FIX(OS_VMCMS)); + /* OS code for Z-System hosts */ rb_define_const(mZlib, "OS_ZSYSTEM", INT2FIX(OS_ZSYSTEM)); + /* OS code for CP/M hosts */ rb_define_const(mZlib, "OS_CPM", INT2FIX(OS_CPM)); + /* OS code for QDOS hosts */ rb_define_const(mZlib, "OS_QDOS", INT2FIX(OS_QDOS)); + /* OS code for RISC OS hosts */ rb_define_const(mZlib, "OS_RISCOS", INT2FIX(OS_RISCOS)); + /* 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 */ } @@ -3657,10 +5041,96 @@ Init_zlib() * - Zlib::MemError * - Zlib::BufError * - Zlib::VersionError + * - Zlib::InProgressError * */ /* + * Document-class: Zlib::StreamEnd + * + * Subclass of Zlib::Error + * + * When zlib returns a Z_STREAM_END + * is return if the end of the compressed data has been reached + * and all uncompressed out put has been produced. + * + */ + +/* + * Document-class: Zlib::NeedDict + * + * Subclass of Zlib::Error + * + * When zlib returns a Z_NEED_DICT + * if a preset dictionary is needed at this point. + * + * Used by Zlib::Inflate.inflate and <tt>Zlib.inflate</tt> + */ + +/* + * Document-class: Zlib::VersionError + * + * Subclass of Zlib::Error + * + * When zlib returns a Z_VERSION_ERROR, + * usually if the zlib library version is incompatible with the + * version assumed by the caller. + * + */ + +/* + * Document-class: Zlib::MemError + * + * Subclass of Zlib::Error + * + * When zlib returns a Z_MEM_ERROR, + * usually if there was not enough memory. + * + */ + +/* + * Document-class: Zlib::StreamError + * + * Subclass of Zlib::Error + * + * When zlib returns a Z_STREAM_ERROR, + * usually if the stream state was inconsistent. + * + */ + +/* + * Document-class: Zlib::BufError + * + * Subclass of Zlib::Error when zlib returns a Z_BUF_ERROR. + * + * Usually if no progress is possible. + * + */ + +/* + * Document-class: Zlib::DataError + * + * Subclass of Zlib::Error when zlib returns a Z_DATA_ERROR. + * + * Usually if a stream was prematurely freed. + * + */ + +/* + * 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. @@ -3669,21 +5139,19 @@ Init_zlib() /* * Document-class: Zlib::GzipFile::NoFooter * - * Raised when gzip file footer is not found. + * Raised when gzip file footer is not found. */ /* * Document-class: Zlib::GzipFile::CRCError * * Raised when the CRC checksum recorded in gzip file footer is not equivalent - * to the CRC checksum of the actual uncompressed data. + * to the CRC checksum of the actual uncompressed data. */ /* * Document-class: Zlib::GzipFile::LengthError * * Raised when the data length recorded in the gzip file footer is not equivalent - * to the length of the actual uncompressed data. + * 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 |
