diff options
Diffstat (limited to 'ext/zlib')
| -rw-r--r-- | ext/zlib/.cvsignore | 3 | ||||
| -rw-r--r-- | ext/zlib/.gitignore | 1 | ||||
| -rw-r--r-- | ext/zlib/MANIFEST | 4 | ||||
| -rw-r--r-- | ext/zlib/depend | 177 | ||||
| -rw-r--r-- | ext/zlib/doc/zlib.rd | 911 | ||||
| -rw-r--r-- | ext/zlib/extconf.rb | 108 | ||||
| -rw-r--r-- | ext/zlib/zlib.c | 4253 | ||||
| -rw-r--r-- | ext/zlib/zlib.gemspec | 31 |
8 files changed, 3620 insertions, 1868 deletions
diff --git a/ext/zlib/.cvsignore b/ext/zlib/.cvsignore deleted file mode 100644 index 4088712231..0000000000 --- a/ext/zlib/.cvsignore +++ /dev/null @@ -1,3 +0,0 @@ -Makefile -mkmf.log -*.def 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/MANIFEST b/ext/zlib/MANIFEST deleted file mode 100644 index 746275d5c8..0000000000 --- a/ext/zlib/MANIFEST +++ /dev/null @@ -1,4 +0,0 @@ -MANIFEST -extconf.rb -zlib.c -doc/zlib.rd 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 6adff7f89c..0000000000 --- a/ext/zlib/doc/zlib.rd +++ /dev/null @@ -1,911 +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 abstruct 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::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 bf3349a65d..2b2dbb1a5b 100644 --- a/ext/zlib/extconf.rb +++ b/ext/zlib/extconf.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true # # extconf.rb # @@ -9,20 +10,81 @@ 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 zlib'.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 'mswin32', 'mingw32', 'bccwin32' then + when /mswin|mingw|bccwin/ then # NOTE: cygwin should be regarded as Unix. os_code = 'WIN32' else @@ -36,7 +98,6 @@ if %w'z libz zlib'.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,19 +109,36 @@ if %w'z libz zlib'.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}" - defines = defines.collect{|d|' -D'+d}.join - if $CPPFLAGS then - $CPPFLAGS += defines + $defs.concat(defines.collect{|d|' -D'+d}) + + 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 - $CFLAGS += defines + have_func('crc32_combine', 'zlib.h') + have_func('adler32_combine', 'zlib.h') + have_type('z_crc_t', 'zlib.h') + if (have_type('z_size_t', 'zlib.h') && + have_func('crc32_z', 'zlib.h') && + have_func('adler32_z', 'zlib.h')) + $defs << "-DHAVE_ZLIB_SIZE_T_FUNCS" + end end - create_makefile('zlib') + create_makefile('zlib') {|conf| + if zsrc + conf.concat addconf if addconf + end + conf + } end diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c index 8e320b46a4..481d74b2b6 100644 --- a/ext/zlib/zlib.c +++ b/ext/zlib/zlib.c @@ -9,11 +9,27 @@ #include <ruby.h> #include <zlib.h> #include <time.h> +#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 "0.6.0" - +#define RUBY_ZLIB_VERSION "3.2.3" -#define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0) +#ifndef RB_PASS_CALLED_KEYWORDS +# define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass) +#endif #ifndef GZIP_SUPPORT #define GZIP_SUPPORT 1 @@ -28,171 +44,279 @@ #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 rb_zlib_adler32 _((int, VALUE*, VALUE)); -static VALUE rb_zlib_crc32 _((int, VALUE*, VALUE)); -static VALUE rb_zlib_crc_table _((VALUE)); -static voidpf zlib_mem_alloc _((voidpf, uInt, uInt)); -static void zlib_mem_free _((voidpf, voidpf)); +static NORETURN(void raise_zlib_error(int, const char*)); +static VALUE rb_zlib_version(VALUE); +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); +static voidpf zlib_mem_alloc(voidpf, uInt, uInt); +static void zlib_mem_free(voidpf, voidpf); +static void finalizer_warn(const char*); struct zstream; struct zstream_funcs; -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 char*, int)); -static VALUE zstream_detach_buffer _((struct zstream*)); -static VALUE zstream_shift_buffer _((struct zstream*, int)); -static void zstream_buffer_ungetc _((struct zstream*, int)); -static void zstream_append_input _((struct zstream*, const char*, unsigned int)); -static void zstream_discard_input _((struct zstream*, unsigned int)); -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 void 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_new _((VALUE, const struct zstream_funcs*)); -static struct zstream *get_zstream _((VALUE)); - -static VALUE rb_zstream_end _((VALUE)); -static VALUE rb_zstream_reset _((VALUE)); -static VALUE rb_zstream_finish _((VALUE)); -static VALUE rb_zstream_flush_next_in _((VALUE)); -static VALUE rb_zstream_flush_next_out _((VALUE)); -static VALUE rb_zstream_avail_out _((VALUE)); -static VALUE rb_zstream_set_avail_out _((VALUE, VALUE)); -static VALUE rb_zstream_avail_in _((VALUE)); -static VALUE rb_zstream_total_in _((VALUE)); -static VALUE rb_zstream_total_out _((VALUE)); -static VALUE rb_zstream_data_type _((VALUE)); -static VALUE rb_zstream_adler _((VALUE)); -static VALUE rb_zstream_finished_p _((VALUE)); -static VALUE rb_zstream_closed_p _((VALUE)); - -static VALUE rb_deflate_s_allocate _((VALUE)); -static VALUE rb_deflate_initialize _((int, VALUE*, VALUE)); -static VALUE rb_deflate_clone _((VALUE)); -static VALUE rb_deflate_s_deflate _((int, VALUE*, VALUE)); -static void do_deflate _((struct zstream*, VALUE, int)); -static VALUE rb_deflate_deflate _((int, VALUE*, VALUE)); -static VALUE rb_deflate_addstr _((VALUE, VALUE)); -static VALUE rb_deflate_flush _((int, VALUE*, VALUE)); -static VALUE rb_deflate_params _((VALUE, VALUE, VALUE)); -static VALUE rb_deflate_set_dictionary _((VALUE, VALUE)); - -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_addstr _((VALUE, VALUE)); -static VALUE rb_inflate_sync _((VALUE, VALUE)); -static VALUE rb_inflate_sync_point_p _((VALUE)); -static VALUE rb_inflate_set_dictionary _((VALUE, VALUE)); +struct zstream_run_args; +static void zstream_init(struct zstream*, const struct zstream_funcs*); +static void zstream_expand_buffer(struct zstream*); +static void zstream_expand_buffer_into(struct zstream*, unsigned long); +static int zstream_expand_buffer_non_stream(struct zstream *z); +static void zstream_append_buffer(struct zstream*, const Bytef*, long); +static VALUE zstream_detach_buffer(struct zstream*); +static VALUE zstream_shift_buffer(struct zstream*, long, VALUE); +static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long); +static void zstream_buffer_ungetbyte(struct zstream*, int); +static void zstream_append_input(struct zstream*, const Bytef*, long); +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 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*); + +static VALUE rb_zstream_end(VALUE); +static VALUE rb_zstream_reset(VALUE); +static VALUE rb_zstream_finish(VALUE); +static VALUE rb_zstream_flush_next_in(VALUE); +static VALUE rb_zstream_flush_next_out(VALUE); +static VALUE rb_zstream_avail_out(VALUE); +static VALUE rb_zstream_set_avail_out(VALUE, VALUE); +static VALUE rb_zstream_avail_in(VALUE); +static VALUE rb_zstream_total_in(VALUE); +static VALUE rb_zstream_total_out(VALUE); +static VALUE rb_zstream_data_type(VALUE); +static VALUE rb_zstream_adler(VALUE); +static VALUE rb_zstream_finished_p(VALUE); +static VALUE rb_zstream_closed_p(VALUE); + +static VALUE rb_deflate_s_allocate(VALUE); +static VALUE rb_deflate_initialize(int, VALUE*, VALUE); +static VALUE rb_deflate_init_copy(VALUE, VALUE); +static VALUE deflate_run(VALUE); +static VALUE rb_deflate_s_deflate(int, VALUE*, VALUE); +static void do_deflate(struct zstream*, VALUE, int); +static VALUE rb_deflate_deflate(int, VALUE*, VALUE); +static VALUE rb_deflate_addstr(VALUE, VALUE); +static VALUE rb_deflate_flush(int, VALUE*, VALUE); +static VALUE rb_deflate_params(VALUE, VALUE, VALUE); +static VALUE rb_deflate_set_dictionary(VALUE, VALUE); + +static VALUE inflate_run(VALUE); +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(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); +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 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 _((struct gzfile*)); -static int gzfile_read_raw_ensure _((struct gzfile*, int)); -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 VALUE gzfile_read _((struct gzfile*, int)); -static VALUE gzfile_read_all _((struct gzfile*)); -static void gzfile_ungetc _((struct gzfile*, int)); -static VALUE gzfile_finalize _((VALUE)); -static void gzfile_writer_end _((struct gzfile*)); -static void gzfile_reader_end _((struct gzfile*)); -static void gzfile_reader_rewind _((struct gzfile*)); -static VALUE gzfile_reader_get_unused _((struct gzfile*)); -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*)); - -static VALUE rb_gzfile_to_io _((VALUE)); -static VALUE rb_gzfile_crc _((VALUE)); -static VALUE rb_gzfile_mtime _((VALUE)); -static VALUE rb_gzfile_level _((VALUE)); -static VALUE rb_gzfile_os_code _((VALUE)); -static VALUE rb_gzfile_orig_name _((VALUE)); -static VALUE rb_gzfile_comment _((VALUE)); -static VALUE rb_gzfile_lineno _((VALUE)); -static VALUE rb_gzfile_set_lineno _((VALUE, VALUE)); -static VALUE rb_gzfile_set_mtime _((VALUE, VALUE)); -static VALUE rb_gzfile_set_orig_name _((VALUE, VALUE)); -static VALUE rb_gzfile_set_comment _((VALUE, VALUE)); -static VALUE rb_gzfile_close _((VALUE)); -static VALUE rb_gzfile_finish _((VALUE)); -static VALUE rb_gzfile_closed_p _((VALUE)); -static VALUE rb_gzfile_eof_p _((VALUE)); -static VALUE rb_gzfile_sync _((VALUE)); -static VALUE rb_gzfile_set_sync _((VALUE, VALUE)); -static VALUE rb_gzfile_total_in _((VALUE)); -static VALUE rb_gzfile_total_out _((VALUE)); - -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_putc _((VALUE, VALUE)); - -static VALUE rb_gzreader_s_allocate _((VALUE)); -static VALUE rb_gzreader_s_open _((int, VALUE*, VALUE)); -static VALUE rb_gzreader_initialize _((VALUE, VALUE)); -static VALUE rb_gzreader_rewind _((VALUE)); -static VALUE rb_gzreader_unused _((VALUE)); -static VALUE rb_gzreader_read _((int, VALUE*, VALUE)); -static VALUE rb_gzreader_getc _((VALUE)); -static VALUE rb_gzreader_readchar _((VALUE)); -static VALUE rb_gzreader_each_byte _((VALUE)); -static VALUE rb_gzreader_ungetc _((VALUE, VALUE)); -static void gzreader_skip_linebreaks _((struct gzfile*)); -static VALUE gzreader_gets _((int, VALUE*, VALUE)); -static VALUE rb_gzreader_gets _((int, VALUE*, VALUE)); -static VALUE rb_gzreader_readline _((int, VALUE*, VALUE)); -static VALUE rb_gzreader_each _((int, VALUE*, VALUE)); -static VALUE rb_gzreader_readlines _((int, VALUE*, VALUE)); +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,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*, 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*, 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*); +static VALUE gzfile_reader_end_run(VALUE); +static void gzfile_reader_end(struct gzfile*); +static void gzfile_reader_rewind(struct gzfile*); +static VALUE gzfile_reader_get_unused(struct gzfile*); +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); +static VALUE rb_gzfile_mtime(VALUE); +static VALUE rb_gzfile_level(VALUE); +static VALUE rb_gzfile_os_code(VALUE); +static VALUE rb_gzfile_orig_name(VALUE); +static VALUE rb_gzfile_comment(VALUE); +static VALUE rb_gzfile_lineno(VALUE); +static VALUE rb_gzfile_set_lineno(VALUE, VALUE); +static VALUE rb_gzfile_set_mtime(VALUE, VALUE); +static VALUE rb_gzfile_set_orig_name(VALUE, VALUE); +static VALUE rb_gzfile_set_comment(VALUE, VALUE); +static VALUE rb_gzfile_close(VALUE); +static VALUE rb_gzfile_finish(VALUE); +static VALUE rb_gzfile_closed_p(VALUE); +static VALUE rb_gzfile_eof_p(VALUE); +static VALUE rb_gzfile_sync(VALUE); +static VALUE rb_gzfile_set_sync(VALUE, VALUE); +static VALUE rb_gzfile_total_in(VALUE); +static VALUE rb_gzfile_total_out(VALUE); +static VALUE rb_gzfile_path(VALUE); + +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(int, VALUE*, VALUE); +static VALUE rb_gzwriter_putc(VALUE, VALUE); + +static VALUE rb_gzreader_s_allocate(VALUE); +static VALUE rb_gzreader_s_open(int, VALUE*, VALUE); +static VALUE rb_gzreader_initialize(int, VALUE*, VALUE); +static VALUE rb_gzreader_rewind(VALUE); +static VALUE rb_gzreader_unused(VALUE); +static VALUE rb_gzreader_read(int, VALUE*, VALUE); +static VALUE rb_gzreader_getc(VALUE); +static VALUE rb_gzreader_readchar(VALUE); +static VALUE rb_gzreader_each_byte(VALUE); +static VALUE rb_gzreader_ungetc(VALUE, VALUE); +static VALUE rb_gzreader_ungetbyte(VALUE, VALUE); +static void gzreader_skip_linebreaks(struct gzfile*); +static VALUE gzreader_gets(int, VALUE*, VALUE); +static VALUE rb_gzreader_gets(int, VALUE*, VALUE); +static VALUE rb_gzreader_readline(int, VALUE*, VALUE); +static VALUE rb_gzreader_each(int, VALUE*, VALUE); +static VALUE rb_gzreader_readlines(int, VALUE*, VALUE); #endif /* GZIP_SUPPORT */ - -void Init_zlib _((void)); - - +/* + * 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); /*--------- 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(err, msg) - int err; - const char *msg; +raise_zlib_error(int err, const char *msg) { VALUE exc; @@ -226,36 +350,64 @@ raise_zlib_error(err, 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); } +/*--- Warning (in finalizer) ---*/ + +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(klass) - VALUE klass; +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; @@ -273,38 +425,124 @@ 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, RSTRING(str)->ptr, RSTRING(str)->len); + 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 + * + */ static VALUE -rb_zlib_adler32(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; +rb_zlib_adler32(int argc, VALUE *argv, VALUE klass) { return do_checksum(argc, argv, adler32); } +#ifdef HAVE_ADLER32_COMBINE +/* + * 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_crc32(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; +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+. 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. + */ +static VALUE +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_crc_table(obj) - VALUE obj; +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; @@ -324,30 +562,41 @@ rb_zlib_crc_table(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)); - int (*end) _((z_streamp)); - int (*run) _((z_streamp, int)); + int (*reset)(z_streamp); + int (*end)(z_streamp); + int (*run)(z_streamp, int); } *func; }; -#define ZSTREAM_FLAG_READY 0x1 -#define ZSTREAM_FLAG_IN_STREAM 0x2 -#define ZSTREAM_FLAG_FINISHED 0x4 -#define ZSTREAM_FLAG_FINALIZE 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_FINALIZE(z) ((z)->flags & ZSTREAM_FLAG_FINALIZE) +#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 @@ -359,31 +608,41 @@ 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(opaque, items, size) - voidpf opaque; - uInt items, size; -{ - return xmalloc(items * size); +zlib_mem_alloc(voidpf opaque, uInt items, uInt 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 -zlib_mem_free(opaque, address) - voidpf opaque, address; +zlib_mem_free(voidpf opaque, voidpf address) { - free(address); + xfree(address); } static void -zstream_init(z, func) - struct zstream *z; - const struct zstream_funcs *func; +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; @@ -399,200 +658,297 @@ zstream_init(z, func) #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs) static void -zstream_expand_buffer(z) - struct zstream *z; +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 = RSTRING(z->buf)->ptr; - z->stream.avail_out = ZSTREAM_INITIAL_BUFSIZE; + zstream_expand_buffer_into(z, ZSTREAM_INITIAL_BUFSIZE); return; } - if (RSTRING(z->buf)->len - 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 = RSTRING(z->buf)->ptr + z->buf_filled; } static void -zstream_expand_buffer_into(z, size) - 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->stream.next_out = RSTRING(z->buf)->ptr; - z->stream.avail_out = size; + z->buf = rb_str_buf_new(size); + z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); + 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 = RSTRING(z->buf)->ptr + 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(z, src, len) - struct zstream *z; - const char *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, src, len); - z->buf_filled = len; - z->stream.next_out = RSTRING(z->buf)->ptr; + rb_str_buf_cat(z->buf, (const char*)src, len); + z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); z->stream.avail_out = 0; + rb_obj_hide(z->buf); return; } - if (RSTRING(z->buf)->len < 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 >= len) { - z->stream.avail_out -= len; + if (z->stream.avail_out >= (uInt)len) { + z->stream.avail_out -= (uInt)len; } else { z->stream.avail_out = 0; } } - memcpy(RSTRING(z->buf)->ptr + z->buf_filled, src, len); - z->buf_filled += len; - z->stream.next_out = RSTRING(z->buf)->ptr + 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) \ - zstream_append_buffer((z),RSTRING(v)->ptr,RSTRING(v)->len) + zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v)) static VALUE -zstream_detach_buffer(z) - struct zstream *z; +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); + 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(z, len) - struct zstream *z; - int len; -{ - VALUE dst; - - if (z->buf_filled <= len) { - return zstream_detach_buffer(z); - } - - dst = rb_str_substr(z->buf, 0, len); - z->buf_filled -= len; - memmove(RSTRING(z->buf)->ptr, RSTRING(z->buf)->ptr + len, - z->buf_filled); - z->stream.next_out = RSTRING(z->buf)->ptr + z->buf_filled; - z->stream.avail_out = RSTRING(z->buf)->len - z->buf_filled; - if (z->stream.avail_out > ZSTREAM_AVAIL_OUT_STEP_MAX) { - z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; - } +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; + } + 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_ungetc(z, c) - struct zstream *z; - int c; +zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len) { - if (NIL_P(z->buf) || RSTRING(z->buf)->len - z->buf_filled == 0) { - zstream_expand_buffer(z); - } + char *bufptr; + long filled; + + zstream_expand_buffer_into(z, len); - memmove(RSTRING(z->buf)->ptr + 1, RSTRING(z->buf)->ptr, z->buf_filled); - RSTRING(z->buf)->ptr[0] = (char)c; - z->buf_filled++; + 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) { - z->stream.next_out++; - z->stream.avail_out--; + if (len > z->stream.avail_out) len = z->stream.avail_out; + z->stream.next_out+=len; + z->stream.avail_out-=(uInt)len; } } static void -zstream_append_input(z, src, len) - struct zstream *z; - const char *src; - unsigned int len; +zstream_buffer_ungetbyte(struct zstream *z, int c) +{ + Bytef cc = (Bytef)c; + zstream_buffer_ungets(z, &cc, 1); +} + +static void +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, src, len); + rb_str_buf_cat(z->input, (const char*)src, len); + rb_obj_hide(z->input); } else { - rb_str_buf_cat(z->input, src, len); + rb_str_buf_cat(z->input, (const char*)src, len); } } #define zstream_append_input2(z,v)\ - zstream_append_input((z), RSTRING(v)->ptr, RSTRING(v)->len) + RB_GC_GUARD(v),\ + zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v)) static void -zstream_discard_input(z, len) - struct zstream *z; - unsigned int len; +zstream_discard_input(struct zstream *z, long len) { - if (NIL_P(z->input) || RSTRING(z->input)->len <= len) { - z->input = Qnil; + if (NIL_P(z->input)) { } - else { - memmove(RSTRING(z->input)->ptr, RSTRING(z->input)->ptr + len, - RSTRING(z->input)->len - len); - rb_str_resize(z->input, RSTRING(z->input)->len - 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(z) - struct zstream *z; +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 -zstream_passthrough_input(z) - struct zstream *z; +zstream_passthrough_input(struct zstream *z) { if (!NIL_P(z->input)) { zstream_append_buffer2(z, z->input); @@ -601,143 +957,310 @@ zstream_passthrough_input(z) } static VALUE -zstream_detach_input(z) - struct zstream *z; +zstream_detach_input(struct zstream *z) { VALUE dst; - dst = NIL_P(z->input) ? rb_str_new(0, 0) : z->input; + if (NIL_P(z->input)) { + dst = rb_str_new(0, 0); + } + else { + dst = z->input; + rb_obj_reveal(dst, rb_cString); + } z->input = Qnil; return dst; } static void -zstream_reset(z) - struct zstream *z; +zstream_reset(struct zstream *z) { int err; err = z->func->reset(&z->stream); - if (err != Z_OK && !ZSTREAM_IS_FINALIZE(z)) { + if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } 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); } -static void -zstream_end(z) - struct zstream *z; +static VALUE +zstream_end(struct zstream *z) { int err; - if (!ZSTREAM_IS_READY(z) && !ZSTREAM_IS_FINALIZE(z)) { - if (RTEST(ruby_debug)) { - rb_warning("attempt to close uninitialized zstream; ignored."); - } - return; + if (!ZSTREAM_IS_READY(z)) { + rb_warning("attempt to close uninitialized zstream; ignored."); + return Qnil; } if (z->flags & ZSTREAM_FLAG_IN_STREAM) { - if (RTEST(ruby_debug)) { - rb_warning("attempt to close unfinished zstream; reset forced."); - } + rb_warning("attempt to close unfinished zstream; reset forced."); zstream_reset(z); } zstream_reset_input(z); err = z->func->end(&z->stream); - if (err != Z_OK && !ZSTREAM_IS_FINALIZE(z)) { + if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } z->flags = 0; + 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(z, src, len, flush) - 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; + VALUE old_input = Qnil; - if (NIL_P(z->input)) { - z->stream.next_in = src; - z->stream.avail_in = len; + /* 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*)""; + z->stream.avail_in = 0; } else { zstream_append_input(z, src, len); - z->stream.next_in = RSTRING(z->input)->ptr; - z->stream.avail_in = RSTRING(z->input)->len; + /* keep reference to `z->input' so as not to be garbage collected + after zstream_reset_input() and prevent `z->stream.next_in' + from dangling. */ + 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 (;;) { - 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); } + if (!NIL_P(old_input)) { + rb_str_resize(old_input, 0); + } + + return Qnil; } static VALUE -zstream_sync(z, src, len) - struct zstream *z; - Bytef *src; - uInt len; +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) { - VALUE rest; + struct zstream_run_args args = { + .z = z, + .src = src, + .len = len, + .flush = flush, + .interrupt = 0, + .jump_state = 0, + .stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p(), + }; + + rb_ensure(zstream_run_try, (VALUE)&args, zstream_run_ensure, (VALUE)&args); + if (args.jump_state) + rb_jump_tag(args.jump_state); +} + +static VALUE +zstream_sync(struct zstream *z, Bytef *src, long len) +{ + /* VALUE rest; */ int err; if (!NIL_P(z->input)) { - z->stream.next_in = RSTRING(z->input)->ptr; - z->stream.avail_in = RSTRING(z->input)->len; + z->stream.next_in = (Bytef*)RSTRING_PTR(z->input); + z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input)); err = inflateSync(&z->stream); if (err == Z_OK) { zstream_discard_input(z, - RSTRING(z->input)->len - z->stream.avail_in); + RSTRING_LEN(z->input) - z->stream.avail_in); zstream_append_input(z, src, len); return Qtrue; } zstream_reset_input(z); if (err != Z_DATA_ERROR) { - rest = rb_str_new(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); } } @@ -745,47 +1268,71 @@ zstream_sync(z, src, 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(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(z) - 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 -zstream_free(z) - struct zstream *z; +zstream_finalize(struct zstream *z) { - z->flags |= ZSTREAM_FLAG_FINALIZE; - zstream_end(z); - free(z); + int err = z->func->end(&z->stream); + if (err == Z_STREAM_ERROR) + finalizer_warn("the stream state was inconsistent."); + if (err == Z_DATA_ERROR) + finalizer_warn("the stream was freed prematurely."); } +static void +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(klass, funcs) - VALUE klass; - const struct zstream_funcs *funcs; +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; } @@ -793,12 +1340,11 @@ zstream_new(klass, funcs) #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs) static struct zstream * -get_zstream(obj) - VALUE obj; +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"); } @@ -806,145 +1352,255 @@ get_zstream(obj) } +/* ------------------------------------------------------------------------- */ -/*-------- class Zlib::ZStream ---------*/ +/* + * Document-class: Zlib::ZStream + * + * Zlib::ZStream is 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 in the source) + * 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 input 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 occurs during processing input buffer, an exception which is a + * subclass of Zlib::Error is raised. At that time, both input and output + * buffer keep their conditions at the time when the error occurs. + * + * == Method Catalogue + * + * Many of the methods in this class are fairly low-level and unlikely to be + * of interest to users. In fact, users are unlikely to use this class + * directly; rather they will be interested in Zlib::Inflate and + * Zlib::Deflate. + * + * The higher level methods are listed below. + * + * - #total_in + * - #total_out + * - #data_type + * - #adler + * - #reset + * - #finish + * - #finished? + * - #close + * - #closed? + */ +/* + * Closes the stream. All operations on the closed stream will raise an + * exception. + */ static VALUE -rb_zstream_end(obj) - VALUE obj; +rb_zstream_end(VALUE obj) { zstream_end(get_zstream(obj)); return Qnil; } +/* + * Resets and initializes the stream. All data in both input and output buffer + * are discarded. + */ static VALUE -rb_zstream_reset(obj) - VALUE obj; +rb_zstream_reset(VALUE obj) { zstream_reset(get_zstream(obj)); return Qnil; } +/* + * 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(obj) - VALUE obj; +rb_zstream_finish(VALUE obj) { struct zstream *z = get_zstream(obj); - VALUE dst; - zstream_run(z, "", 0, Z_FINISH); - dst = zstream_detach_buffer(z); + zstream_run(z, (Bytef*)"", 0, Z_FINISH); - 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 -rb_zstream_flush_next_in(obj) - VALUE obj; +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; } +/* + * 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(obj) - VALUE obj; +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); } +/* + * Returns number of bytes of free spaces in output buffer. Because the free + * space is allocated automatically, this method returns 0 normally. + */ static VALUE -rb_zstream_avail_out(obj) - VALUE obj; +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 + * free space is allocated automatically, you usually don't need to use this + * method. + */ static VALUE -rb_zstream_set_avail_out(obj, size) - VALUE obj, size; +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; } +/* + * Returns bytes of data in the input buffer. Normally, returns 0. + */ static VALUE -rb_zstream_avail_in(obj) - VALUE obj; +rb_zstream_avail_in(VALUE obj) { struct zstream *z; - Data_Get_Struct(obj, struct zstream, z); - return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING(z->input)->len)); + TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z); + return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input))); } +/* + * Returns the total bytes of the input data to the stream. FIXME + */ static VALUE -rb_zstream_total_in(obj) - VALUE obj; +rb_zstream_total_in(VALUE obj) { return rb_uint2inum(get_zstream(obj)->stream.total_in); } +/* + * Returns the total bytes of the output data from the stream. FIXME + */ static VALUE -rb_zstream_total_out(obj) - VALUE obj; +rb_zstream_total_out(VALUE obj) { return rb_uint2inum(get_zstream(obj)->stream.total_out); } +/* + * 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(obj) - VALUE obj; +rb_zstream_data_type(VALUE obj) { return INT2FIX(get_zstream(obj)->stream.data_type); } +/* + * Returns the adler-32 checksum. + */ static VALUE -rb_zstream_adler(obj) - VALUE obj; +rb_zstream_adler(VALUE obj) { - return rb_uint2inum(get_zstream(obj)->stream.adler); + return rb_uint2inum(get_zstream(obj)->stream.adler); } +/* + * Returns true if the stream is finished. + */ static VALUE -rb_zstream_finished_p(obj) - VALUE obj; +rb_zstream_finished_p(VALUE obj) { return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse; } +/* + * Returns true if the stream is closed. + */ static VALUE -rb_zstream_closed_p(obj) - VALUE obj; +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; } +/* ------------------------------------------------------------------------- */ -/*-------- class Zlib::Deflate --------*/ +/* + * Document-class: Zlib::Deflate + * + * 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) @@ -954,24 +1610,93 @@ rb_zstream_closed_p(obj) static VALUE -rb_deflate_s_allocate(klass) - VALUE klass; +rb_deflate_s_allocate(VALUE klass) { - return zstream_deflate_new(klass); + return zstream_deflate_new(klass); } +/* + * 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. If a given argument is nil, + * the default value of that argument is used. + * + * 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(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_deflate_initialize(int argc, VALUE *argv, VALUE obj) { struct zstream *z; VALUE level, wbits, memlevel, strategy; 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), @@ -984,159 +1709,270 @@ rb_deflate_initialize(argc, argv, obj) return obj; } +/* + * Document-method: Zlib::Deflate#initialize_copy + * + * Duplicates the deflate stream. + */ static VALUE -rb_deflate_clone(obj) - VALUE obj; +rb_deflate_init_copy(VALUE self, VALUE orig) { - struct zstream *z = get_zstream(obj); - struct zstream *z2; - VALUE clone; + struct zstream *z1, *z2; int err; - clone = zstream_deflate_new(rb_class_of(obj)); - Data_Get_Struct(clone, struct zstream, z2); + TypedData_Get_Struct(self, struct zstream, &zstream_data_type, z1); + z2 = get_zstream(orig); - err = deflateCopy(&z2->stream, &z->stream); + 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->flags = z2->flags; - z2->flags = z->flags; - CLONESETUP(clone, obj); - OBJ_INFECT(clone, obj); - return clone; + return self; } static VALUE -rb_deflate_s_deflate(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; +deflate_run(VALUE args) +{ + struct zstream *z = (struct zstream*)((VALUE*)args)[0]; + VALUE src = ((VALUE*)args)[1]; + + zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_FINISH); + return zstream_detach_buffer(z); +} + +/* + * 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 + * Zlib::NO_COMPRESSION, Zlib::BEST_SPEED, Zlib::BEST_COMPRESSION, + * Zlib::DEFAULT_COMPRESSION, or an integer from 0 to 9. + * + * This method is almost equivalent to the following code: + * + * def deflate(string, level) + * z = Zlib::Deflate.new(level) + * dst = z.deflate(string, Zlib::FINISH) + * z.close + * dst + * end + * + * See also Zlib.inflate + * + */ +static VALUE +rb_deflate_s_deflate(int argc, VALUE *argv, VALUE klass) { struct zstream z; - VALUE src, level, dst; - int err; + VALUE src, level, dst, args[2]; + int err, lev; rb_scan_args(argc, argv, "11", &src, &level); + lev = ARG_LEVEL(level); + StringValue(src); zstream_init_deflate(&z); - err = deflateInit(&z.stream, ARG_LEVEL(level)); + err = deflateInit(&z.stream, lev); if (err != Z_OK) { raise_zlib_error(err, z.stream.msg); } ZSTREAM_READY(&z); - StringValue(src); - zstream_run(&z, RSTRING(src)->ptr, RSTRING(src)->len, Z_FINISH); - dst = zstream_detach_buffer(&z); - zstream_end(&z); + args[0] = (VALUE)&z; + args[1] = src; + dst = rb_ensure(deflate_run, (VALUE)args, zstream_ensure_end, (VALUE)&z); - OBJ_INFECT(dst, src); return dst; } static void -do_deflate(z, src, flush) - struct zstream *z; - VALUE src; - int flush; +do_deflate(struct zstream *z, VALUE src, int flush) { if (NIL_P(src)) { - zstream_run(z, "", 0, Z_FINISH); + zstream_run(z, (Bytef*)"", 0, Z_FINISH); return; } StringValue(src); - if (flush != Z_NO_FLUSH || RSTRING(src)->len > 0) { /* prevent BUF_ERROR */ - zstream_run(z, RSTRING(src)->ptr, RSTRING(src)->len, flush); + if (flush != Z_NO_FLUSH || RSTRING_LEN(src) > 0) { /* prevent BUF_ERROR */ + zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), flush); } } +struct rb_zlib_deflate_arguments { + struct zstream *z; + VALUE src; + int flush; +}; + static VALUE -rb_deflate_deflate(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_deflate_deflate_body(VALUE args) { - struct zstream *z = get_zstream(obj); - VALUE src, flush, dst; + struct rb_zlib_deflate_arguments *arguments = (struct rb_zlib_deflate_arguments *)args; + + do_deflate(arguments->z, arguments->src, arguments->flush); + return zstream_detach_buffer(arguments->z); +} + +/* + * Document-method: Zlib::Deflate#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 + * stream, just like Zlib::ZStream#finish. + * + * 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. + * + */ +static VALUE +rb_deflate_deflate(int argc, VALUE *argv, VALUE obj) +{ + struct zstream *z = get_zstream(obj); + 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 + * returns the Zlib::Deflate object itself. The output from the stream is + * preserved in output buffer. + */ static VALUE -rb_deflate_addstr(obj, src) - VALUE obj, src; +rb_deflate_addstr(VALUE obj, VALUE src) { - OBJ_INFECT(obj, src); do_deflate(get_zstream(obj), src, Z_NO_FLUSH); return obj; } +/* + * 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>. This method is + * just provided to improve the readability of your Ruby program. If a block + * is given chunks of deflate output are yielded to the block until the buffer + * is flushed. + * + * See Zlib::Deflate#deflate for detail on the +flush+ constants NO_FLUSH, + * SYNC_FLUSH, FULL_FLUSH and FINISH. + */ static VALUE -rb_deflate_flush(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +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); flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH); if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */ - zstream_run(z, "", 0, flush); + 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 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(obj, v_level, v_strategy) - VALUE obj, v_level, v_strategy; +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); + n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); + filled = n - z->stream.avail_out; while (err == Z_BUF_ERROR) { - if (RTEST(ruby_debug)) { - rb_warning("deflateParams() returned 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. + * + * 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(obj, dic) - VALUE obj, dic; +rb_deflate_set_dictionary(VALUE obj, VALUE dic) { struct zstream *z = get_zstream(obj); VALUE src = dic; int err; - OBJ_INFECT(obj, dic); StringValue(src); err = deflateSetDictionary(&z->stream, - RSTRING(src)->ptr, RSTRING(src)->len); + (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } @@ -1145,28 +1981,78 @@ rb_deflate_set_dictionary(obj, dic) } +/* ------------------------------------------------------------------------- */ -/*-------- class Zlib::Inflate --------*/ +/* + * Document-class: Zlib::Inflate + * + * Zlib:Inflate is the class for decompressing compressed data. Unlike + * Zlib::Deflate, an instance of this class is not able to duplicate (clone, + * dup) itself. + */ static VALUE -rb_inflate_s_allocate(klass) - VALUE klass; +rb_inflate_s_allocate(VALUE klass) { - return zstream_inflate_new(klass); + VALUE inflate = zstream_inflate_new(klass); + rb_ivar_set(inflate, id_dictionaries, rb_hash_new()); + return inflate; } +/* + * Document-method: Zlib::Inflate.new + * + * call-seq: + * Zlib::Inflate.new(window_bits = Zlib::MAX_WBITS) + * + * 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 + * + */ static VALUE -rb_inflate_initialize(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_inflate_initialize(int argc, VALUE *argv, VALUE obj) { struct zstream *z; VALUE wbits; 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) { @@ -1178,13 +2064,47 @@ rb_inflate_initialize(argc, argv, obj) } static VALUE -rb_inflate_s_inflate(obj, src) - VALUE obj, src; +inflate_run(VALUE args) +{ + struct zstream *z = (struct zstream*)((VALUE*)args)[0]; + VALUE src = ((VALUE*)args)[1]; + + zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH); + zstream_run(z, (Bytef*)"", 0, Z_FINISH); /* for checking errors */ + return zstream_detach_buffer(z); +} + +/* + * 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. + * + * 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 + * + * See also Zlib.deflate + * + */ +static VALUE +rb_inflate_s_inflate(VALUE obj, VALUE src) { struct zstream z; - VALUE dst; + VALUE dst, args[2]; int err; + StringValue(src); zstream_init_inflate(&z); err = inflateInit(&z.stream); if (err != Z_OK) { @@ -1192,39 +2112,87 @@ rb_inflate_s_inflate(obj, src) } ZSTREAM_READY(&z); - StringValue(src); - zstream_run(&z, RSTRING(src)->ptr, RSTRING(src)->len, Z_SYNC_FLUSH); - zstream_run(&z, "", 0, Z_FINISH); /* for checking errors */ - dst = zstream_detach_buffer(&z); - zstream_end(&z); + args[0] = (VALUE)&z; + args[1] = src; + dst = rb_ensure(inflate_run, (VALUE)args, zstream_ensure_end, (VALUE)&z); - OBJ_INFECT(dst, src); return dst; } static void -do_inflate(z, src) - struct zstream *z; - VALUE src; +do_inflate(struct zstream *z, VALUE src) { if (NIL_P(src)) { - zstream_run(z, "", 0, Z_FINISH); + zstream_run(z, (Bytef*)"", 0, Z_FINISH); return; } StringValue(src); - if (RSTRING(src)->len > 0) { /* prevent Z_BUF_ERROR */ - zstream_run(z, RSTRING(src)->ptr, RSTRING(src)->len, Z_SYNC_FLUSH); + if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */ + zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH); } } +/* Document-method: Zlib::Inflate#add_dictionary + * + * call-seq: add_dictionary(string) + * + * 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(obj, src) - VALUE obj, src; +rb_inflate_add_dictionary(VALUE obj, VALUE dictionary) { - struct zstream *z = get_zstream(obj); - VALUE dst; + VALUE dictionaries = rb_ivar_get(obj, id_dictionaries); + VALUE checksum = do_checksum(1, &dictionary, adler32); + + rb_hash_aset(dictionaries, checksum, dictionary); - OBJ_INFECT(obj, src); + return obj; +} + +struct rb_zlib_inflate_arguments { + struct zstream *z; + int argc; + VALUE *argv; +}; + +static VALUE +rb_inflate_inflate_body(VALUE _arguments) +{ + struct rb_zlib_inflate_arguments *arguments = (struct rb_zlib_inflate_arguments*)_arguments; + struct zstream *z = arguments->z; + int argc = arguments->argc; + VALUE *argv = arguments->argv; + VALUE dst, src, opts, buffer = Qnil; + + if (OPTHASH_GIVEN_P(opts)) { + VALUE buf; + rb_get_kwargs(opts, &id_buffer, 0, 1, &buf); + if (buf != Qundef && buf != Qnil) { + buffer = StringValue(buf); + } + } + if (buffer != Qnil) { + if (!(ZSTREAM_REUSE_BUFFER_P(z) && z->buf == buffer)) { + long len = RSTRING_LEN(buffer); + if (len >= ZSTREAM_AVAIL_OUT_STEP_MAX) { + rb_str_modify(buffer); + } + else { + len = ZSTREAM_AVAIL_OUT_STEP_MAX - len; + rb_str_modify_expand(buffer, len); + } + rb_str_set_len(buffer, 0); + z->flags |= ZSTREAM_REUSE_BUFFER; + z->buf = buffer; + } + } else if (ZSTREAM_REUSE_BUFFER_P(z)) { + z->flags &= ~ZSTREAM_REUSE_BUFFER; + z->buf = Qnil; + } + rb_scan_args(argc, argv, "10", &src); if (ZSTREAM_IS_FINISHED(z)) { if (NIL_P(src)) { @@ -1233,7 +2201,11 @@ rb_inflate_inflate(obj, 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 { @@ -1244,17 +2216,74 @@ rb_inflate_inflate(obj, 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_addstr(obj, src) - VALUE obj, src; +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); +} - OBJ_INFECT(obj, src); +/* + * call-seq: << string + * + * Inputs +string+ into the inflate stream just like Zlib::Inflate#inflate, but + * returns the Zlib::Inflate object itself. The output from the stream is + * preserved in output buffer. + */ +static VALUE +rb_inflate_addstr(VALUE obj, VALUE src) +{ + struct zstream *z = get_zstream(obj); if (ZSTREAM_IS_FINISHED(z)) { if (!NIL_P(src)) { @@ -1272,20 +2301,32 @@ rb_inflate_addstr(obj, src) return obj; } +/* + * call-seq: 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. + */ static VALUE -rb_inflate_sync(obj, src) - VALUE obj, src; +rb_inflate_sync(VALUE obj, VALUE src) { struct zstream *z = get_zstream(obj); - OBJ_INFECT(obj, src); StringValue(src); - return zstream_sync(z, RSTRING(src)->ptr, RSTRING(src)->len); + return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src)); } +/* + * Quoted verbatim from original documentation: + * + * What is this? + * + * <tt>:)</tt> + */ static VALUE -rb_inflate_sync_point_p(obj) - VALUE obj; +rb_inflate_sync_point_p(VALUE obj) { struct zstream *z = get_zstream(obj); int err; @@ -1300,18 +2341,23 @@ rb_inflate_sync_point_p(obj) return Qfalse; } +/* + * 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. + * + */ static VALUE -rb_inflate_set_dictionary(obj, dic) - VALUE obj, dic; +rb_inflate_set_dictionary(VALUE obj, VALUE dic) { struct zstream *z = get_zstream(obj); VALUE src = dic; int err; - OBJ_INFECT(obj, dic); StringValue(src); err = inflateSetDictionary(&z->stream, - RSTRING(src)->ptr, RSTRING(src)->len); + (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } @@ -1365,7 +2411,7 @@ rb_inflate_set_dictionary(obj, dic) #define OS_CODE OS_UNIX #endif -static ID id_write, id_read, id_flush, id_seek, id_close; +static ID id_write, id_readpartial, id_flush, id_seek, id_close, id_path, id_input; static VALUE cGzError, cNoFooter, cCRCError, cLengthError; @@ -1376,58 +2422,88 @@ 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; + VALUE ecopts; + VALUE path; }; +#define GZFILE_CBUF_CAPA 10 #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(gz) - 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); zstream_mark(&gz->z); + rb_gc_mark(gz->ecopts); + rb_gc_mark(gz->path); } static void -gzfile_free(gz) - struct gzfile *gz; +gzfile_free(void *p) { - gz->z.flags |= ZSTREAM_FLAG_FINALIZE; - if (ZSTREAM_IS_READY(&gz->z)) { - gz->end(gz); + struct gzfile *gz = p; + struct zstream *z = &gz->z; + + if (ZSTREAM_IS_READY(z)) { + if (z->func == &deflate_funcs) { + finalizer_warn("Zlib::GzipWriter object must be closed explicitly."); + } + zstream_finalize(z); } - free(gz); + 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; @@ -1438,7 +2514,22 @@ gzfile_new(klass, funcs, endfunc) gz->lineno = 0; gz->ungetc = 0; gz->end = endfunc; + gz->enc = rb_default_external_encoding(); + gz->enc2 = 0; + gz->ec = NULL; + gz->ecflags = 0; + gz->ecopts = Qnil; + 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; } @@ -1446,21 +2537,25 @@ gzfile_new(klass, funcs, endfunc) #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end) static void -gzfile_reset(gz) - struct gzfile *gz; +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; + if (gz->ec) { + rb_econv_close(gz->ec); + gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name, + gz->ecflags, gz->ecopts); + } } static void -gzfile_close(gz, closeflag) - struct gzfile *gz; - int closeflag; +gzfile_close(struct gzfile *gz, int closeflag) { VALUE io = gz->io; + gz->end(gz); gz->io = Qnil; gz->orig_name = Qnil; @@ -1471,14 +2566,12 @@ gzfile_close(gz, closeflag) } static void -gzfile_write_raw(gz) - struct gzfile *gz; +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)) @@ -1487,58 +2580,82 @@ gzfile_write_raw(gz) } static VALUE -gzfile_read_raw(gz) - struct gzfile *gz; +gzfile_read_raw_partial(VALUE 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_read, 1, INT2FIX(GZFILE_READ_SIZE)); - if (!NIL_P(str)) { - Check_Type(str, T_STRING); - } + 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, VALUE _) +{ + struct read_raw_arg *ra = (struct read_raw_arg *)arg; + VALUE str = Qnil; + if (rb_obj_is_kind_of(rb_errinfo(), rb_eNoMethodError)) { + 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); + } + } + return str; /* return nil when EOFError */ +} + +static VALUE +gzfile_read_raw(struct gzfile *gz, VALUE outbuf) +{ + 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(gz, size) - struct gzfile *gz; - int size; +gzfile_read_raw_ensure(struct gzfile *gz, long size, VALUE outbuf) { VALUE str; - while (NIL_P(gz->z.input) || RSTRING(gz->z.input)->len < size) { - str = gzfile_read_raw(gz); - if (NIL_P(str)) return Qfalse; + 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, outbuf); + if (NIL_P(str)) return 0; zstream_append_input2(&gz->z, str); } - return Qtrue; + return 1; } static char * -gzfile_read_raw_until_zero(gz, offset) - struct gzfile *gz; - long offset; +gzfile_read_raw_until_zero(struct gzfile *gz, long offset) { VALUE str; char *p; for (;;) { - p = memchr(RSTRING(gz->z.input)->ptr + offset, '\0', - RSTRING(gz->z.input)->len - 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"); } - offset = RSTRING(gz->z.input)->len; + offset = RSTRING_LEN(gz->z.input); zstream_append_input2(&gz->z, str); } return p; } static unsigned int -gzfile_get16(src) - const unsigned char *src; +gzfile_get16(const unsigned char *src) { unsigned int n; n = *(src++) & 0xff; @@ -1547,21 +2664,18 @@ gzfile_get16(src) } static unsigned long -gzfile_get32(src) - const unsigned char *src; +gzfile_get32(const unsigned char *src) { unsigned long n; n = *(src++) & 0xff; n |= (*(src++) & 0xff) << 8; n |= (*(src++) & 0xff) << 16; - n |= (*(src++) & 0xff) << 24; + n |= (*(src++) & 0xffU) << 24; return n; } static void -gzfile_set32(n, dst) - unsigned long n; - unsigned char *dst; +gzfile_set32(unsigned long n, unsigned char *dst) { *(dst++) = n & 0xff; *(dst++) = (n >> 8) & 0xff; @@ -1570,10 +2684,39 @@ gzfile_set32(n, dst) } static void -gzfile_make_header(gz) - struct gzfile *gz; +gzfile_raise(struct gzfile *gz, VALUE klass, const char *message) { - unsigned char buf[10]; /* the size of gzip header */ + 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 */ unsigned char flags = 0, extraflags = 0; if (!NIL_P(gz->orig_name)) { @@ -1582,7 +2725,7 @@ gzfile_make_header(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); } @@ -1597,51 +2740,50 @@ gzfile_make_header(gz) buf[1] = GZ_MAGIC2; buf[2] = GZ_METHOD_DEFLATE; buf[3] = flags; - gzfile_set32(gz->mtime, &buf[4]); + 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); - zstream_append_buffer(&gz->z, "\0", 1); + zstream_append_buffer(&gz->z, (Bytef*)"\0", 1); } if (!NIL_P(gz->comment)) { zstream_append_buffer2(&gz->z, gz->comment); - zstream_append_buffer(&gz->z, "\0", 1); + zstream_append_buffer(&gz->z, (Bytef*)"\0", 1); } gz->z.flags |= GZFILE_FLAG_HEADER_FINISHED; } static void -gzfile_make_footer(gz) - struct gzfile *gz; +gzfile_make_footer(struct gzfile *gz) { - unsigned char buf[8]; /* 8 is the size of gzip footer */ + Bytef buf[8]; /* 8 is the size of gzip footer */ 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(gz) - 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 = RSTRING(gz->z.input)->ptr; + 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]); @@ -1673,49 +2815,53 @@ gzfile_read_header(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(RSTRING(gz->z.input)->ptr); - if (!gzfile_read_raw_ensure(gz, 2 + len)) { + len = gzfile_get16((Bytef*)RSTRING_PTR(gz->z.input)); + 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(gz->z.input)->ptr; - gz->orig_name = rb_str_new(RSTRING(gz->z.input)->ptr, len); - OBJ_TAINT(gz->orig_name); /* for safe */ + len = p - RSTRING_PTR(gz->z.input); + gz->orig_name = rb_str_new(RSTRING_PTR(gz->z.input), len); 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(gz->z.input)->ptr; - gz->comment = rb_str_new(RSTRING(gz->z.input)->ptr, len); - OBJ_TAINT(gz->comment); /* for safe */ + len = p - RSTRING_PTR(gz->z.input); + gz->comment = rb_str_new(RSTRING_PTR(gz->z.input), len); zstream_discard_input(&gz->z, len + 1); } - if (gz->z.input != Qnil && RSTRING(gz->z.input)->len > 0) { + if (gz->z.input != Qnil && RSTRING_LEN(gz->z.input) > 0) { zstream_run(&gz->z, 0, 0, Z_SYNC_FLUSH); } } static void -gzfile_check_footer(gz) - 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(RSTRING(gz->z.input)->ptr); - length = gzfile_get32(RSTRING(gz->z.input)->ptr + 4); + crc = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input)); + length = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input) + 4); gz->z.stream.total_in += 8; /* to rewind correctly */ zstream_discard_input(&gz->z, 8); @@ -1723,23 +2869,20 @@ gzfile_check_footer(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(gz, str, len) - 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); } @@ -1747,159 +2890,270 @@ gzfile_write(gz, str, len) } static long -gzfile_read_more(gz) - struct gzfile *gz; +gzfile_read_more(struct gzfile *gz, VALUE outbuf) { 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"); } break; } - if (RSTRING(str)->len > 0) { /* prevent Z_BUF_ERROR */ - zstream_run(&gz->z, RSTRING(str)->ptr, RSTRING(str)->len, + 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 ZSTREAM_BUF_FILLED(&gz->z); +} + +static void +gzfile_calc_crc(struct gzfile *gz, VALUE str) +{ + if (RSTRING_LEN(str) <= gz->ungetc) { + gz->ungetc -= RSTRING_LEN(str); + } + else { + gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc, + RSTRING_LEN(str) - gz->ungetc); + gz->ungetc = 0; } - return gz->z.buf_filled; } static VALUE -gzfile_read(gz, len) - struct gzfile *gz; - int len; +gzfile_newstr(struct gzfile *gz, VALUE str) { - VALUE dst; + if (!gz->enc2) { + rb_enc_associate(str, gz->enc); + 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); + return str; + } + return rb_str_conv_enc_opts(str, gz->enc2, gz->enc, + gz->ecflags, gz->ecopts); +} - if (len <= 0) return Qnil; - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { - gzfile_read_more(gz); +static long +gzfile_fill(struct gzfile *gz, long len, VALUE outbuf) +{ + if (len < 0) + rb_raise(rb_eArgError, "negative length %ld given", len); + if (len == 0) + return 0; + while (!ZSTREAM_IS_FINISHED(&gz->z) && 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); +} + +static VALUE +gzfile_read(struct gzfile *gz, long len, VALUE outbuf) +{ + VALUE dst; + + len = gzfile_fill(gz, len, outbuf); - dst = zstream_shift_buffer(&gz->z, len); - if (RSTRING(dst)->len <= gz->ungetc) { - gz->ungetc -= RSTRING(dst)->len; + if (len < 0) { + if (!NIL_P(outbuf)) + rb_str_resize(outbuf, 0); + return Qnil; } - else { - gz->crc = crc32(gz->crc, RSTRING(dst)->ptr + gz->ungetc, - RSTRING(dst)->len - gz->ungetc); + if (len == 0) { + if (NIL_P(outbuf)) + return rb_str_new(0, 0); + else { + rb_str_resize(outbuf, 0); + return outbuf; + } } - OBJ_TAINT(dst); /* for safe */ + dst = zstream_shift_buffer(&gz->z, len, outbuf); + if (!NIL_P(dst)) gzfile_calc_crc(gz, dst); return dst; } static VALUE -gzfile_read_all(gz) - struct gzfile *gz; +gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf) { VALUE dst; + if (len < 0) + rb_raise(rb_eArgError, "negative length %ld given", len); + + if (len == 0) { + if (NIL_P(outbuf)) + return rb_str_new(0, 0); + else { + rb_str_resize(outbuf, 0); + return outbuf; + } + } + 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, 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, outbuf); + gzfile_calc_crc(gz, dst); + + return dst; +} + +static VALUE +gzfile_read_all(struct gzfile *gz, 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); } - return Qnil; + if (!NIL_P(dst)) { + rb_str_resize(dst, 0); + return dst; + } + return rb_str_new(0, 0); } dst = zstream_detach_buffer(&gz->z); - if (RSTRING(dst)->len <= gz->ungetc) { - gz->ungetc -= RSTRING(dst)->len; + if (NIL_P(dst)) return dst; + gzfile_calc_crc(gz, dst); + return gzfile_newstr(gz, dst); +} + +static VALUE +gzfile_getc(struct gzfile *gz) +{ + VALUE buf, dst = 0; + int len; + + len = rb_enc_mbmaxlen(gz->enc); + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) { + gzfile_read_more(gz, Qnil); } - else { - gz->crc = crc32(gz->crc, RSTRING(dst)->ptr + gz->ungetc, - RSTRING(dst)->len - gz->ungetc); + if (GZFILE_IS_FINISHED(gz)) { + if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { + gzfile_check_footer(gz, Qnil); + } + return Qnil; } - OBJ_TAINT(dst); /* for safe */ - return dst; + if (gz->ec && rb_enc_dummy_p(gz->enc2)) { + const unsigned char *ss, *sp, *se; + unsigned char *ds, *dp, *de; + VALUE cbuf = rb_enc_str_new(0, GZFILE_CBUF_CAPA, gz->enc); + + ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf); + se = sp + ZSTREAM_BUF_FILLED(&gz->z); + ds = dp = (unsigned char *)RSTRING_PTR(cbuf); + de = (unsigned char *)ds + GZFILE_CBUF_CAPA; + (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, Qnil); + gzfile_calc_crc(gz, 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, Qnil); + if (NIL_P(dst)) return dst; + return gzfile_newstr(gz, dst); + } } static void -gzfile_ungetc(gz, c) - struct gzfile *gz; - int c; +gzfile_ungets(struct gzfile *gz, const Bytef *b, long len) { - zstream_buffer_ungetc(&gz->z, c); - gz->ungetc++; + zstream_buffer_ungets(&gz->z, b, len); + gz->ungetc+=len; } -static VALUE -gzfile_finalize(obj) - VALUE obj; +static void +gzfile_ungetbyte(struct gzfile *gz, int c) { - struct gzfile *gz = (struct gzfile *)obj; - gzfile_write_raw(gz); - return Qnil; + zstream_buffer_ungetbyte(&gz->z, c); + gz->ungetc++; } -static void -gzfile_writer_end(gz) - struct gzfile *gz; +static VALUE +gzfile_writer_end_run(VALUE arg) { - int aborted; + struct gzfile *gz = (struct gzfile *)arg; if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) { gzfile_make_header(gz); } - zstream_run(&gz->z, "", 0, Z_FINISH); + zstream_run(&gz->z, (Bytef*)"", 0, Z_FINISH); gzfile_make_footer(gz); - - if (ZSTREAM_IS_FINALIZE(&gz->z)) { - rb_warn("Zlib::GzipWriter object must be closed explicitly."); - if (OBJ_IS_FREED(gz->io)) { - aborted = 1; - } - else { - rb_protect(gzfile_finalize, (VALUE)gz, &aborted); - } - if (aborted) { - rb_warn("gzip footer is not written; broken gzip file"); - } - zstream_end(&gz->z); - return; - } gzfile_write_raw(gz); - zstream_end(&gz->z); + + return Qnil; } static void -gzfile_reader_end(gz) - struct gzfile *gz; +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_ensure_end, (VALUE)&gz->z); +} + +static VALUE +gzfile_reader_end_run(VALUE arg) +{ + struct gzfile *gz = (struct gzfile *)arg; + if (GZFILE_IS_FINISHED(gz) - && !ZSTREAM_IS_FINALIZE(&gz->z) && !(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { - gzfile_check_footer(gz); + gzfile_check_footer(gz, Qnil); } - zstream_end(&gz->z); + return Qnil; } static void -gzfile_reader_rewind(gz) - struct gzfile *gz; +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_ensure_end, (VALUE)&gz->z); +} + +static void +gzfile_reader_rewind(struct gzfile *gz) { long n; n = gz->z.stream.total_in; if (!NIL_P(gz->z.input)) { - n += RSTRING(gz->z.input)->len; + n += RSTRING_LEN(gz->z.input); } rb_funcall(gz->io, id_seek, 2, rb_int2inum(-n), INT2FIX(1)); @@ -1907,30 +3161,27 @@ gzfile_reader_rewind(gz) } static VALUE -gzfile_reader_get_unused(gz) - struct gzfile *gz; +gzfile_reader_get_unused(struct gzfile *gz) { VALUE str; 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; } static struct gzfile * -get_gzfile(obj) - VALUE obj; +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"); } @@ -1938,16 +3189,65 @@ get_gzfile(obj) } +/* ------------------------------------------------------------------------- */ -/*-------- class Zlib::GzipFile --------*/ +/* + * Document-class: Zlib::GzipFile + * + * Zlib::GzipFile is an 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 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 -gzfile_ensure_close(obj) - VALUE obj; +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); } @@ -1955,12 +3255,25 @@ gzfile_ensure_close(obj) } static VALUE -rb_gzfile_s_wrap(argc, argv, klass) - 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); @@ -1970,104 +3283,176 @@ rb_gzfile_s_wrap(argc, argv, 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 -gzfile_s_open(argc, argv, klass, mode) - int argc; - VALUE *argv; - VALUE klass; - const char *mode; +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 +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]; - SafeStringValue(filename); - io = rb_file_open(RSTRING(filename)->ptr, mode); - + 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 -rb_gzfile_to_io(obj) - VALUE obj; +rb_gzfile_to_io(VALUE obj) { return get_gzfile(obj)->io; } +/* + * Document-method: Zlib::GzipFile#crc + * + * Returns CRC value of the uncompressed data. + */ static VALUE -rb_gzfile_crc(obj) - VALUE obj; +rb_gzfile_crc(VALUE obj) { return rb_uint2inum(get_gzfile(obj)->crc); } +/* + * Document-method: Zlib::GzipFile#mtime + * + * Returns last modification time recorded in the gzip file header. + */ static VALUE -rb_gzfile_mtime(obj) - VALUE obj; +rb_gzfile_mtime(VALUE obj) { return rb_time_new(get_gzfile(obj)->mtime, (time_t)0); } +/* + * Document-method: Zlib::GzipFile#level + * + * Returns compression level. + */ static VALUE -rb_gzfile_level(obj) - VALUE obj; +rb_gzfile_level(VALUE obj) { return INT2FIX(get_gzfile(obj)->level); } +/* + * Document-method: Zlib::GzipFile#os_code + * + * Returns OS code number recorded in the gzip file header. + */ static VALUE -rb_gzfile_os_code(obj) - VALUE obj; +rb_gzfile_os_code(VALUE obj) { return INT2FIX(get_gzfile(obj)->os_code); } +/* + * Document-method: Zlib::GzipFile#orig_name + * + * Returns original filename recorded in the gzip file header, or +nil+ if + * original filename is not present. + */ static VALUE -rb_gzfile_orig_name(obj) - VALUE obj; +rb_gzfile_orig_name(VALUE obj) { VALUE str = get_gzfile(obj)->orig_name; 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. + */ static VALUE -rb_gzfile_comment(obj) - VALUE obj; +rb_gzfile_comment(VALUE obj) { VALUE str = get_gzfile(obj)->comment; 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(obj) - VALUE obj; +rb_gzfile_lineno(VALUE obj) { return INT2NUM(get_gzfile(obj)->lineno); } +/* + * Document-method: Zlib::GzipReader#lineno= + * + * Specify line number of the last row read from this file. + */ static VALUE -rb_gzfile_set_lineno(obj, lineno) - VALUE obj, lineno; +rb_gzfile_set_lineno(VALUE obj, VALUE lineno) { struct gzfile *gz = get_gzfile(obj); gz->lineno = NUM2INT(lineno); return 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(obj, mtime) - VALUE obj, mtime; +rb_gzfile_set_mtime(VALUE obj, VALUE mtime) { struct gzfile *gz = get_gzfile(obj); VALUE val; @@ -2076,19 +3461,20 @@ rb_gzfile_set_mtime(obj, mtime) rb_raise(cGzError, "header is already written"); } - if (FIXNUM_P(time)) { - 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(obj, str) - VALUE obj, str; +rb_gzfile_set_orig_name(VALUE obj, VALUE str) { struct gzfile *gz = get_gzfile(obj); VALUE s; @@ -2098,17 +3484,21 @@ rb_gzfile_set_orig_name(obj, str) rb_raise(cGzError, "header is already written"); } s = rb_str_dup(rb_str_to_str(str)); - p = memchr(RSTRING(s)->ptr, '\0', RSTRING(s)->len); + p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s)); if (p) { - rb_str_resize(s, p - RSTRING(s)->ptr); + rb_str_resize(s, p - RSTRING_PTR(s)); } gz->orig_name = s; return str; } +/* + * Document-method: Zlib::GzipFile#comment= + * + * Specify the comment (+str+) in the gzip header. + */ static VALUE -rb_gzfile_set_comment(obj, str) - VALUE obj, str; +rb_gzfile_set_comment(VALUE obj, VALUE str) { struct gzfile *gz = get_gzfile(obj); VALUE s; @@ -2118,29 +3508,44 @@ rb_gzfile_set_comment(obj, str) rb_raise(cGzError, "header is already written"); } s = rb_str_dup(rb_str_to_str(str)); - p = memchr(RSTRING(s)->ptr, '\0', RSTRING(s)->len); + p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s)); if (p) { - rb_str_resize(s, p - RSTRING(s)->ptr); + rb_str_resize(s, p - RSTRING_PTR(s)); } gz->comment = s; return 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(obj) - VALUE obj; +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. + */ static VALUE -rb_gzfile_finish(obj) - VALUE obj; +rb_gzfile_finish(VALUE obj) { struct gzfile *gz = get_gzfile(obj); VALUE io; @@ -2150,33 +3555,58 @@ rb_gzfile_finish(obj) return io; } +/* + * Document-method: Zlib::GzipFile#closed? + * + * Same as IO#closed? + * + */ static VALUE -rb_gzfile_closed_p(obj) - VALUE obj; +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(obj) - VALUE obj; +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; } +/* + * Document-method: Zlib::GzipFile#sync + * + * Same as IO#sync + * + */ static VALUE -rb_gzfile_sync(obj) - VALUE obj; +rb_gzfile_sync(VALUE obj) { return (get_gzfile(obj)->z.flags & GZFILE_FLAG_SYNC) ? Qtrue : Qfalse; } +/* + * Document-method: Zlib::GzipFile#sync= + * + * call-seq: sync = flag + * + * Same as IO. If flag is +true+, the associated IO object must respond to the + * +flush+ method. While +sync+ mode is +true+, the compression ratio + * decreases sharply. + */ static VALUE -rb_gzfile_set_sync(obj, mode) - VALUE obj, mode; +rb_gzfile_set_sync(VALUE obj, VALUE mode) { struct gzfile *gz = get_gzfile(obj); @@ -2189,53 +3619,157 @@ rb_gzfile_set_sync(obj, mode) return mode; } +/* + * Document-method: Zlib::GzipFile#total_in + * + * Total number of input bytes read so far. + */ static VALUE -rb_gzfile_total_in(obj) - VALUE obj; +rb_gzfile_total_in(VALUE obj) { return rb_uint2inum(get_gzfile(obj)->z.stream.total_in); } +/* + * Document-method: Zlib::GzipFile#total_out + * + * Total number of output bytes output so far. + */ static VALUE -rb_gzfile_total_out(obj) - VALUE obj; +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: Zlib::GzipFile#path + * + * call-seq: path + * + * Returns the path string of the associated IO-like object. This + * method is only defined when the IO-like object responds to #path(). + */ +static VALUE +rb_gzfile_path(VALUE 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; +} -/*-------- class Zlib::GzipWriter --------*/ +static void +rb_gzfile_ecopts(struct gzfile *gz, VALUE opts) +{ + if (!NIL_P(opts)) { + rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2, NULL); + } + if (gz->enc2) { + gz->ecflags = rb_econv_prepare_opts(opts, &opts); + gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name, + gz->ecflags, opts); + gz->ecopts = 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. + * + * Following two example generate the same result. + * + * Zlib::GzipWriter.open('hoge.gz') do |gz| + * gz.write 'jugemu jugemu gokou no surikire...' + * end + * + * File.open('hoge.gz', 'w') do |f| + * gz = Zlib::GzipWriter.new(f) + * gz.write 'jugemu jugemu gokou no surikire...' + * gz.close + * end + * + * 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 + * will be not able to write the gzip footer and will generate a broken gzip + * file. + */ static VALUE -rb_gzwriter_s_allocate(klass) - VALUE klass; +rb_gzwriter_s_allocate(VALUE klass) { return gzfile_writer_new(klass); } +/* + * call-seq: Zlib::GzipWriter.open(filename, level=nil, strategy=nil) { |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 found in Zlib::GzipWriter.new and Zlib::GzipFile.wrap. + */ static VALUE -rb_gzwriter_s_open(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; +rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass) { return gzfile_s_open(argc, argv, klass, "wb"); } +/* + * 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+. +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(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz; - VALUE io, level, strategy; + VALUE io, level, strategy, opt = Qnil; int err; + 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, "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); @@ -2246,15 +3780,25 @@ rb_gzwriter_initialize(argc, argv, obj) } gz->io = io; ZSTREAM_READY(&gz->z); + rb_gzfile_ecopts(gz, opt); + + if (rb_respond_to(io, id_path)) { + /* 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; } +/* + * call-seq: flush(flush=nil) + * + * Flushes all the internal buffers of the GzipWriter object. The meaning of + * +flush+ is same as in Zlib::Deflate#deflate. <tt>Zlib::SYNC_FLUSH</tt> is used if + * +flush+ is omitted. It is no use giving flush <tt>Zlib::NO_FLUSH</tt>. + */ static VALUE -rb_gzwriter_flush(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_gzwriter_flush(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); VALUE v_flush; @@ -2264,7 +3808,7 @@ rb_gzwriter_flush(argc, argv, obj) flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH); if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */ - zstream_run(&gz->z, "", 0, flush); + zstream_run(&gz->z, (Bytef*)"", 0, flush); } gzfile_write_raw(gz); @@ -2274,62 +3818,221 @@ rb_gzwriter_flush(argc, argv, obj) return obj; } +/* + * Same as IO. + */ static VALUE -rb_gzwriter_write(obj, str) - VALUE obj, 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); + 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, RSTRING(str)->ptr, RSTRING(str)->len); - return INT2FIX(RSTRING(str)->len); + return SIZET2NUM(total); } +/* + * Same as IO. + */ static VALUE -rb_gzwriter_putc(obj, ch) - VALUE obj, ch; +rb_gzwriter_putc(VALUE obj, VALUE ch) { struct gzfile *gz = get_gzfile(obj); char c = NUM2CHR(ch); - gzfile_write(gz, &c, 1); + gzfile_write(gz, (Bytef*)&c, 1); return ch; } + + +/* + * Document-method: << + * Same as IO. + */ #define rb_gzwriter_addstr rb_io_addstr +/* + * Document-method: printf + * Same as IO. + */ #define rb_gzwriter_printf rb_io_printf +/* + * Document-method: print + * Same as IO. + */ #define rb_gzwriter_print rb_io_print +/* + * Document-method: puts + * Same as IO. + */ #define rb_gzwriter_puts rb_io_puts -/*-------- class Zlib::GzipReader --------*/ +/* ------------------------------------------------------------------------- */ + +/* + * Document-class: Zlib::GzipReader + * + * Zlib::GzipReader is the class for reading a gzipped file. GzipReader should + * be used as an IO, or -IO-like, object. + * + * Zlib::GzipReader.open('hoge.gz') {|gz| + * print gz.read + * } + * + * File.open('hoge.gz') do |f| + * gz = Zlib::GzipReader.new(f) + * print gz.read + * gz.close + * end + * + * == Method Catalogue + * + * The following methods in Zlib::GzipReader are just like their counterparts + * in IO, but they raise Zlib::Error or Zlib::GzipFile::Error exception if an + * error was found in the gzip file. + * - #each + * - #each_line + * - #each_byte + * - #gets + * - #getc + * - #lineno + * - #lineno= + * - #read + * - #readchar + * - #readline + * - #readlines + * - #ungetc + * + * Be careful of the footer of the 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 it fails, raises + * <tt>Zlib::GzipFile::NoFooter</tt>, <tt>Zlib::GzipFile::CRCError</tt>, or + * <tt>Zlib::GzipFile::LengthError</tt> 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. + * + * The rest of the methods are adequately described in their own + * documentation. + */ static VALUE -rb_gzreader_s_allocate(klass) - VALUE klass; +rb_gzreader_s_allocate(VALUE klass) { return gzfile_reader_new(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::GzipFile.wrap. + */ static VALUE -rb_gzreader_s_open(argc, argv, klass) - int argc; - VALUE *argv; - VALUE klass; +rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass) { return gzfile_s_open(argc, argv, klass, "rb"); } +/* + * 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_initialize(obj, io) - VALUE obj, io; +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 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. + */ +static VALUE +rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj) { + VALUE io, opt = Qnil; struct gzfile *gz; int err; - Data_Get_Struct(obj, struct gzfile, gz); + TypedData_Get_Struct(obj, struct gzfile, &gzfile_data_type, gz); + rb_scan_args(argc, argv, "1:", &io, &opt); /* this is undocumented feature of zlib */ err = inflateInit2(&gz->z.stream, -MAX_WBITS); @@ -2338,165 +4041,364 @@ rb_gzreader_initialize(obj, io) } 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)) { + /* 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. + */ static VALUE -rb_gzreader_rewind(obj) - VALUE obj; +rb_gzreader_rewind(VALUE obj) { struct gzfile *gz = get_gzfile(obj); gzfile_reader_rewind(gz); return INT2FIX(0); } +/* + * 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. + */ static VALUE -rb_gzreader_unused(obj) - VALUE obj; +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(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +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 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. + * It raises <code>EOFError</code> on end of file. + */ static VALUE -rb_gzreader_getc(obj) - VALUE obj; +rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj) +{ + struct gzfile *gz = get_gzfile(obj); + VALUE vlen, outbuf; + long len; + + rb_scan_args(argc, argv, "11", &vlen, &outbuf); + + len = NUM2INT(vlen); + if (len < 0) { + rb_raise(rb_eArgError, "negative length %ld given", len); + } + if (!NIL_P(outbuf)) + Check_Type(outbuf, T_STRING); + return gzfile_readpartial(gz, len, outbuf); +} + +/* + * Document-method: Zlib::GzipReader#getc + * + * See Zlib::GzipReader documentation for a description. + */ +static VALUE +rb_gzreader_getc(VALUE obj) +{ + struct gzfile *gz = get_gzfile(obj); + + return gzfile_getc(gz); +} + +/* + * Document-method: Zlib::GzipReader#readchar + * + * See Zlib::GzipReader documentation for a description. + */ +static VALUE +rb_gzreader_readchar(VALUE obj) +{ + VALUE dst; + dst = rb_gzreader_getc(obj); + if (NIL_P(dst)) { + rb_raise(rb_eEOFError, "end of file reached"); + } + return dst; +} + +/* + * Document-method: Zlib::GzipReader#getbyte + * + * See Zlib::GzipReader documentation for a description. + */ +static VALUE +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(dst)->ptr[0]) & 0xff); + dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff); } return dst; } +/* + * Document-method: Zlib::GzipReader#readbyte + * + * See Zlib::GzipReader documentation for a description. + */ static VALUE -rb_gzreader_readchar(obj) - VALUE obj; +rb_gzreader_readbyte(VALUE obj) { VALUE dst; - dst = rb_gzreader_getc(obj); + dst = rb_gzreader_getbyte(obj); if (NIL_P(dst)) { - rb_raise(rb_eEOFError, "End of file reached"); + rb_raise(rb_eEOFError, "end of file reached"); } return dst; } +/* + * Document-method: Zlib::GzipReader#each_char + * + * See Zlib::GzipReader documentation for a description. + */ static VALUE -rb_gzreader_each_byte(obj) - VALUE obj; +rb_gzreader_each_char(VALUE obj) { VALUE c; + + RETURN_ENUMERATOR(obj, 0, 0); + while (!NIL_P(c = rb_gzreader_getc(obj))) { rb_yield(c); } return Qnil; } +/* + * Document-method: Zlib::GzipReader#each_byte + * + * See Zlib::GzipReader documentation for a description. + */ +static VALUE +rb_gzreader_each_byte(VALUE obj) +{ + VALUE c; + + RETURN_ENUMERATOR(obj, 0, 0); + + while (!NIL_P(c = rb_gzreader_getbyte(obj))) { + rb_yield(c); + } + return Qnil; +} + +/* + * Document-method: Zlib::GzipReader#ungetc + * + * See Zlib::GzipReader documentation for a description. + */ static VALUE -rb_gzreader_ungetc(obj, ch) - VALUE obj, ch; +rb_gzreader_ungetc(VALUE obj, VALUE s) +{ + struct gzfile *gz; + + if (FIXNUM_P(s)) + return rb_gzreader_ungetbyte(obj, s); + gz = get_gzfile(obj); + StringValue(s); + if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) { + 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 +rb_gzreader_ungetbyte(VALUE obj, VALUE ch) { struct gzfile *gz = get_gzfile(obj); - gzfile_ungetc(gz, NUM2CHR(ch)); + gzfile_ungetbyte(gz, NUM2CHR(ch)); return Qnil; } static void -gzreader_skip_linebreaks(gz) - struct gzfile *gz; +gzreader_skip_linebreaks(struct gzfile *gz) { VALUE str; 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(gz->z.buf)->ptr; + 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); - gz->crc = crc32(gz->crc, RSTRING(str)->ptr, - RSTRING(str)->len); - while (gz->z.buf_filled == 0) { + ASSUME(!NIL_P(str)); + gzfile_calc_crc(gz, str); + 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(gz->z.buf)->ptr; + p = RSTRING_PTR(gz->z.buf); } } - str = zstream_shift_buffer(&gz->z, n - 1); - gz->crc = crc32(gz->crc, RSTRING(str)->ptr, RSTRING(str)->len); + str = zstream_shift_buffer(&gz->z, n - 1, Qnil); + gzfile_calc_crc(gz, str); +} + +static void +rscheck(const char *rsptr, long rslen, VALUE rs) +{ + if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen) + 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(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +gzreader_gets(int argc, VALUE *argv, VALUE obj) { struct gzfile *gz = get_gzfile(obj); - VALUE rs, dst; - char *rsptr, *p; - long rslen, n; + VALUE rs; + VALUE dst; + const char *rsptr; + char *p, *res; + 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 (!NIL_P(dst)) gz->lineno++; + 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; } - if (RSTRING(rs)->len == 0) { + if (RSTRING_LEN(rs) == 0) { rsptr = "\n\n"; rslen = 2; rspara = 1; - } else { - rsptr = RSTRING(rs)->ptr; - rslen = RSTRING(rs)->len; + } + else { + rsptr = RSTRING_PTR(rs); + rslen = RSTRING_LEN(rs); rspara = 0; } @@ -2504,40 +4406,65 @@ gzreader_gets(argc, argv, 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; - p = RSTRING(gz->z.buf)->ptr; 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); - p = RSTRING(gz->z.buf)->ptr + n - rslen; + gzfile_read_more(gz, Qnil); + p = RSTRING_PTR(gz->z.buf) + n - rslen; } - if (memcmp(p, rsptr, rslen) == 0) break; - p++, n++; + if (!rspara) rscheck(rsptr, rslen, rs); + filled = ZSTREAM_BUF_FILLED(&gz->z); + if (limit > 0 && filled >= limit) { + filled = limit; + } + res = memchr(p, rsptr[0], (filled - n + 1)); + if (!res) { + 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 dst; + 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(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_gzreader_gets(int argc, VALUE *argv, VALUE obj) { VALUE dst; dst = gzreader_gets(argc, argv, obj); @@ -2547,38 +4474,47 @@ rb_gzreader_gets(argc, argv, obj) return dst; } +/* + * Document-method: Zlib::GzipReader#readline + * + * See Zlib::GzipReader documentation for a description. + */ static VALUE -rb_gzreader_readline(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_gzreader_readline(int argc, VALUE *argv, VALUE obj) { VALUE dst; dst = rb_gzreader_gets(argc, argv, obj); if (NIL_P(dst)) { - rb_raise(rb_eEOFError, "End of file reached"); + rb_raise(rb_eEOFError, "end of file reached"); } return dst; } +/* + * Document-method: Zlib::GzipReader#each + * + * See Zlib::GzipReader documentation for a description. + */ static VALUE -rb_gzreader_each(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_gzreader_each(int argc, VALUE *argv, VALUE obj) { VALUE str; + + RETURN_ENUMERATOR(obj, 0, 0); + while (!NIL_P(str = gzreader_gets(argc, argv, obj))) { rb_yield(str); } return obj; } +/* + * Document-method: Zlib::GzipReader#readlines + * + * See Zlib::GzipReader documentation for a description. + */ static VALUE -rb_gzreader_readlines(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj) { VALUE str, dst; dst = rb_ary_new(); @@ -2588,11 +4524,198 @@ rb_gzreader_readlines(argc, argv, 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); +} -void Init_zlib() +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); + +/* + * 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(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; @@ -2600,6 +4723,8 @@ void 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); @@ -2608,19 +4733,24 @@ void 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); rb_undef_alloc_func(cZStream); rb_define_method(cZStream, "avail_out", rb_zstream_avail_out, 0); - rb_define_method(cZStream, "avail_out=", rb_zstream_set_avail_out, 0); + rb_define_method(cZStream, "avail_out=", rb_zstream_set_avail_out, 1); rb_define_method(cZStream, "avail_in", rb_zstream_avail_in, 0); rb_define_method(cZStream, "total_in", rb_zstream_total_in, 0); rb_define_method(cZStream, "total_out", rb_zstream_total_out, 0); @@ -2637,15 +4767,37 @@ void 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, "clone", rb_deflate_clone, 0); + rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1); rb_define_method(cDeflate, "deflate", rb_deflate_deflate, -1); rb_define_method(cDeflate, "<<", rb_deflate_addstr, 1); rb_define_method(cDeflate, "flush", rb_deflate_flush, -1); @@ -2654,43 +4806,113 @@ void 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 id_write = rb_intern("write"); id_read = rb_intern("read"); + id_readpartial = rb_intern("readpartial"); id_flush = rb_intern("flush"); 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); @@ -2729,7 +4951,7 @@ void 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); @@ -2737,38 +4959,199 @@ void 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, "initialize", rb_gzreader_initialize, -1); rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0); rb_define_method(cGzipReader, "unused", rb_gzreader_unused, 0); rb_define_method(cGzipReader, "read", rb_gzreader_read, -1); + rb_define_method(cGzipReader, "readpartial", rb_gzreader_readpartial, -1); rb_define_method(cGzipReader, "getc", rb_gzreader_getc, 0); + rb_define_method(cGzipReader, "getbyte", rb_gzreader_getbyte, 0); rb_define_method(cGzipReader, "readchar", rb_gzreader_readchar, 0); + 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, "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, "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 */ } + +/* Document error classes. */ + +/* + * Document-class: 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 + * - 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. + */ + +/* + * Document-class: Zlib::GzipFile::NoFooter + * + * 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. + */ + +/* + * 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. + */ diff --git a/ext/zlib/zlib.gemspec b/ext/zlib/zlib.gemspec new file mode 100644 index 0000000000..345dc5f225 --- /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.5.0" +end |
