/ext/zlib/zlib.c
C | 5027 lines | 3125 code | 550 blank | 1352 comment | 462 complexity | e1a45e150b172a14dc9b2912e1df3ab7 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, 0BSD, Unlicense, GPL-2.0, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- /*
- * zlib.c - An interface for zlib.
- *
- * Copyright (C) UENO Katsuhiro 2000-2003
- *
- * $Id$
- */
- #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 "1.1.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
- #endif
- /* from zutil.h */
- #ifndef DEF_MEM_LEVEL
- #if MAX_MEM_LEVEL >= 8
- #define DEF_MEM_LEVEL 8
- #else
- #define DEF_MEM_LEVEL MAX_MEM_LEVEL
- #endif
- #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 void finalizer_warn(const char*);
- struct zstream;
- struct zstream_funcs;
- struct zstream_run_args;
- static void zstream_init(struct zstream*, const struct zstream_funcs*);
- static void zstream_expand_buffer(struct zstream*);
- static void zstream_expand_buffer_into(struct zstream*, unsigned long);
- static int zstream_expand_buffer_non_stream(struct zstream *z);
- static void zstream_append_buffer(struct zstream*, const Bytef*, long);
- static VALUE zstream_detach_buffer(struct zstream*);
- static VALUE zstream_shift_buffer(struct zstream*, long);
- static 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(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);
- static VALUE gzfile_read_all(struct gzfile*);
- 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 */
- /*
- * 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
- *
- * (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 void
- raise_zlib_error(int err, const char *msg)
- {
- VALUE exc;
- if (!msg) {
- msg = zError(err);
- }
- switch(err) {
- case Z_STREAM_END:
- exc = rb_exc_new2(cStreamEnd, msg);
- break;
- case Z_NEED_DICT:
- exc = rb_exc_new2(cNeedDict, msg);
- break;
- case Z_STREAM_ERROR:
- exc = rb_exc_new2(cStreamError, msg);
- break;
- case Z_DATA_ERROR:
- exc = rb_exc_new2(cDataError, msg);
- break;
- case Z_BUF_ERROR:
- exc = rb_exc_new2(cBufError, msg);
- break;
- case Z_VERSION_ERROR:
- exc = rb_exc_new2(cVersionError, msg);
- break;
- case Z_MEM_ERROR:
- exc = rb_exc_new2(cMemError, msg);
- break;
- case Z_ERRNO:
- rb_sys_fail(msg);
- /* no return */
- default:
- 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(VALUE klass)
- {
- return rb_str_new2(zlibVersion());
- }
- #if SIZEOF_LONG > SIZEOF_INT
- static uLong
- checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len)
- {
- if (len > UINT_MAX) {
- do {
- sum = func(sum, ptr, UINT_MAX);
- ptr += UINT_MAX;
- len -= UINT_MAX;
- } while (len >= UINT_MAX);
- }
- if (len > 0) sum = func(sum, ptr, (uInt)len);
- return sum;
- }
- #else
- #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
- #endif
- static VALUE
- do_checksum(int argc, VALUE *argv, uLong (*func)(uLong, const Bytef*, uInt))
- {
- VALUE str, vsum;
- unsigned long sum;
- rb_scan_args(argc, argv, "02", &str, &vsum);
- if (!NIL_P(vsum)) {
- sum = NUM2ULONG(vsum);
- }
- else if (NIL_P(str)) {
- sum = 0;
- }
- else {
- sum = func(0, Z_NULL, 0);
- }
- if (NIL_P(str)) {
- sum = func(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 = 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(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. +alder1+ is the first Adler-32
- * value, +adler2+ is the second Adler-32 value. +len2+ is the length of the
- * string used to generate +adler2+.
- *
- */
- static VALUE
- rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2)
- {
- return ULONG2NUM(
- adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2)));
- }
- #else
- #define rb_zlib_adler32_combine rb_f_notimplement
- #endif
- /*
- * Document-method: Zlib.crc32
- *
- * call-seq: Zlib.crc32(string, crc)
- *
- * Calculates CRC checksum for +string+, and returns updated value of +crc+. If
- * +string+ is omitted, it returns the CRC initial value. If +crc+ is omitted, it
- * assumes that the initial value is given to +crc+. 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_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)
- {
- #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;
- crctbl = get_crc_table();
- dst = rb_ary_new2(256);
- for (i = 0; i < 256; i++) {
- rb_ary_push(dst, rb_uint2inum(crctbl[i]));
- }
- return dst;
- }
- /*-------- zstream - internal APIs --------*/
- struct zstream {
- unsigned long flags;
- VALUE buf;
- VALUE input;
- VALUE mutex;
- z_stream stream;
- const struct zstream_funcs {
- 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_CLOSING 0x8
- #define ZSTREAM_FLAG_GZFILE 0x10 /* disallows yield from expand_buffer for
- gzip*/
- #define ZSTREAM_REUSE_BUFFER 0x20
- #define ZSTREAM_FLAG_UNUSED 0x40
- #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
- #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
- #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
- #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING)
- #define ZSTREAM_IS_GZFILE(z) ((z)->flags & ZSTREAM_FLAG_GZFILE)
- #define ZSTREAM_BUF_FILLED(z) (NIL_P((z)->buf) ? 0 : RSTRING_LEN((z)->buf))
- #define ZSTREAM_REUSE_BUFFER_P(z) ((z)->flags & ZSTREAM_REUSE_BUFFER)
- #define ZSTREAM_EXPAND_BUFFER_OK 0
- /* I think that more better value should be found,
- but I gave up finding it. B) */
- #define ZSTREAM_INITIAL_BUFSIZE 1024
- /* Allow a quick return when the thread is interrupted */
- #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
- #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
- static const struct zstream_funcs deflate_funcs = {
- deflateReset, deflateEnd, deflate,
- };
- static const struct zstream_funcs inflate_funcs = {
- inflateReset, inflateEnd, inflate,
- };
- struct zstream_run_args {
- struct zstream * z;
- int flush; /* stream flush value for inflate() or deflate() */
- int interrupt; /* stop processing the stream and return to ruby */
- int jump_state; /* for buffer expansion block break or exception */
- int stream_output; /* for streaming zlib processing */
- };
- static voidpf
- zlib_mem_alloc(voidpf opaque, uInt items, uInt size)
- {
- voidpf p = 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(voidpf opaque, voidpf address)
- {
- xfree(address);
- }
- static void
- zstream_init(struct zstream *z, const struct zstream_funcs *func)
- {
- z->flags = 0;
- z->buf = Qnil;
- 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;
- z->stream.msg = Z_NULL;
- z->stream.next_in = Z_NULL;
- z->stream.avail_in = 0;
- z->stream.next_out = Z_NULL;
- z->stream.avail_out = 0;
- z->func = func;
- }
- #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
- #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
- static void
- zstream_expand_buffer(struct zstream *z)
- {
- if (NIL_P(z->buf)) {
- zstream_expand_buffer_into(z, ZSTREAM_INITIAL_BUFSIZE);
- return;
- }
- 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_mutex_unlock(z->mutex);
- rb_protect(rb_yield, z->buf, &state);
- rb_mutex_lock(z->mutex);
- 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 {
- zstream_expand_buffer_non_stream(z);
- }
- }
- static void
- 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_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_modify_expand(z->buf, size);
- z->stream.next_out = (Bytef*)RSTRING_END(z->buf);
- z->stream.avail_out = MAX_UINT(size);
- }
- }
- static void *
- zstream_expand_buffer_protect(void *ptr)
- {
- struct zstream *z = (struct zstream *)ptr;
- int state = 0;
- rb_protect((VALUE (*)(VALUE))zstream_expand_buffer, (VALUE)z, &state);
- return (void *)(VALUE)state;
- }
- static int
- zstream_expand_buffer_non_stream(struct zstream *z)
- {
- long inc, len = ZSTREAM_BUF_FILLED(z);
- if (rb_str_capacity(z->buf) - len >= ZSTREAM_AVAIL_OUT_STEP_MAX) {
- z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
- }
- else {
- inc = len / 2;
- if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) {
- inc = ZSTREAM_AVAIL_OUT_STEP_MIN;
- }
- rb_str_modify_expand(z->buf, inc);
- z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
- (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
- }
- z->stream.next_out = (Bytef*)RSTRING_END(z->buf);
- return ZSTREAM_EXPAND_BUFFER_OK;
- }
- static void
- zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
- {
- if (NIL_P(z->buf)) {
- z->buf = rb_str_buf_new(len);
- rb_str_buf_cat(z->buf, (const char*)src, len);
- z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
- z->stream.avail_out = 0;
- rb_obj_hide(z->buf);
- return;
- }
- if ((long)rb_str_capacity(z->buf) < ZSTREAM_BUF_FILLED(z) + len) {
- rb_str_modify_expand(z->buf, len);
- z->stream.avail_out = 0;
- }
- else {
- if (z->stream.avail_out >= (uInt)len) {
- z->stream.avail_out -= (uInt)len;
- }
- else {
- z->stream.avail_out = 0;
- }
- }
- 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),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
- static VALUE
- 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;
- if (!ZSTREAM_REUSE_BUFFER_P(z)) {
- rb_obj_reveal(dst, rb_cString);
- }
- }
- z->buf = Qnil;
- z->stream.next_out = 0;
- z->stream.avail_out = 0;
- if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) {
- rb_yield(dst);
- dst = Qnil;
- }
- return dst;
- }
- static VALUE
- zstream_shift_buffer(struct zstream *z, long len)
- {
- VALUE dst;
- char *bufptr;
- long buflen = ZSTREAM_BUF_FILLED(z);
- if (buflen <= len) {
- return zstream_detach_buffer(z);
- }
- bufptr = RSTRING_PTR(z->buf);
- dst = rb_str_new(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_ungets(struct zstream *z, const Bytef *b, unsigned long len)
- {
- char *bufptr;
- long filled;
- if (NIL_P(z->buf) || (long)rb_str_capacity(z->buf) <= ZSTREAM_BUF_FILLED(z)) {
- zstream_expand_buffer_into(z, len);
- }
- RSTRING_GETMEM(z->buf, bufptr, filled);
- memmove(bufptr + len, bufptr, filled);
- memmove(bufptr, b, len);
- rb_str_set_len(z->buf, filled + len);
- if (z->stream.avail_out > 0) {
- if (len > z->stream.avail_out) len = z->stream.avail_out;
- z->stream.next_out+=len;
- z->stream.avail_out-=(uInt)len;
- }
- }
- static void
- 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, (const char*)src, len);
- rb_obj_hide(z->input);
- }
- else {
- rb_str_buf_cat(z->input, (const char*)src, len);
- }
- }
- #define zstream_append_input2(z,v)\
- RB_GC_GUARD(v),\
- zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
- static void
- zstream_discard_input(struct zstream *z, long len)
- {
- if (NIL_P(z->input)) {
- }
- 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) {
- rb_gc_force_recycle(z->input);
- 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_substr(z->input, len,
- RSTRING_LEN(z->input) - len);
- }
- }
- }
- static void
- zstream_reset_input(struct zstream *z)
- {
- 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(struct zstream *z)
- {
- if (!NIL_P(z->input)) {
- zstream_append_buffer2(z, z->input);
- z->input = Qnil;
- }
- }
- static VALUE
- zstream_detach_input(struct zstream *z)
- {
- VALUE dst;
- 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(struct zstream *z)
- {
- int err;
- err = z->func->reset(&z->stream);
- if (err != Z_OK) {
- raise_zlib_error(err, z->stream.msg);
- }
- z->flags = ZSTREAM_FLAG_READY;
- z->buf = Qnil;
- z->stream.next_out = 0;
- z->stream.avail_out = 0;
- zstream_reset_input(z);
- }
- static VALUE
- zstream_end(struct zstream *z)
- {
- int err;
- if (!ZSTREAM_IS_READY(z)) {
- rb_warning("attempt to close uninitialized zstream; ignored.");
- return Qnil;
- }
- if (z->flags & ZSTREAM_FLAG_IN_STREAM) {
- 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) {
- 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_func(void *ptr)
- {
- struct zstream_run_args *args = (struct zstream_run_args *)ptr;
- int err, state, flush = args->flush;
- struct zstream *z = args->z;
- uInt n;
- err = Z_OK;
- while (!args->interrupt) {
- n = z->stream.avail_out;
- err = z->func->run(&z->stream, flush);
- 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 = (int)(VALUE)rb_thread_call_with_gvl(zstream_expand_buffer_protect,
- (void *)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 (void *)(VALUE)err;
- }
- /*
- * There is no safe way to interrupt z->run->func().
- * async-signal-safe
- */
- static void
- zstream_unblock_func(void *ptr)
- {
- struct zstream_run_args *args = (struct zstream_run_args *)ptr;
- args->interrupt = 1;
- }
- static void
- zstream_run0(struct zstream *z, Bytef *src, long len, int flush)
- {
- struct zstream_run_args args;
- int err;
- VALUE old_input = Qnil;
- args.z = z;
- args.flush = flush;
- args.interrupt = 0;
- args.jump_state = 0;
- args.stream_output = !ZSTREAM_IS_GZFILE(z) && rb_block_given_p();
- if (NIL_P(z->input) && len == 0) {
- z->stream.next_in = (Bytef*)"";
- z->stream.avail_in = 0;
- }
- else {
- zstream_append_input(z, src, 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);
- }
- loop:
- #ifndef RB_NOGVL_UBF_ASYNC_SAFE
- err = (int)(VALUE)rb_thread_call_without_gvl(zstream_run_func, (void *)&args,
- zstream_unblock_func, (void *)&args);
- #else
- err = (int)(VALUE)rb_nogvl(zstream_run_func, (void *)&args,
- zstream_unblock_func, (void *)&args,
- RB_NOGVL_UBF_ASYNC_SAFE);
- #endif
- /* retry if no exception is thrown */
- if (err == Z_OK && args.interrupt) {
- args.interrupt = 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_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_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);
- rb_gc_force_recycle(old_input);
- }
- if (args.jump_state)
- rb_jump_tag(args.jump_state);
- }
- struct zstream_run_synchronized_args {
- struct zstream *z;
- Bytef *src;
- long len;
- int flush;
- };
- static VALUE
- zstream_run_synchronized(VALUE value_arg)
- {
- struct zstream_run_synchronized_args *run_args = (struct zstream_run_synchronized_args *)value_arg;
- zstream_run0(run_args->z, run_args->src, run_args->len, run_args->flush);
- return Qnil;
- }
- static void
- zstream_run(struct zstream *z, Bytef *src, long len, int flush)
- {
- struct zstream_run_synchronized_args run_args;
- run_args.z = z;
- run_args.src = src;
- run_args.len = len;
- run_args.flush = flush;
- rb_mutex_synchronize(z->mutex, zstream_run_synchronized, (VALUE)&run_args);
- }
- static VALUE
- zstream_sync(struct zstream *z, Bytef *src, long len)
- {
- /* VALUE rest; */
- int err;
- if (!NIL_P(z->input)) {
- 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_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((char*)z->stream.next_in, z->stream.avail_in); */
- raise_zlib_error(err, z->stream.msg);
- }
- }
- if (len <= 0) return Qfalse;
- z->stream.next_in = src;
- z->stream.avail_in = MAX_UINT(len);
- err = inflateSync(&z->stream);
- if (err == Z_OK) {
- zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
- return Qtrue;
- }
- if (err != Z_DATA_ERROR) {
- /* rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); */
- raise_zlib_error(err, z->stream.msg);
- }
- return Qfalse;
- }
- static void
- 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_finalize(struct zstream *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(VALUE klass, const struct zstream_funcs *funcs)
- {
- VALUE obj;
- struct zstream *z;
- obj = TypedData_Make_Struct(klass, struct zstream, &zstream_data_type, z);
- zstream_init(z, funcs);
- z->stream.opaque = (voidpf)obj;
- return obj;
- }
- #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
- #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
- static struct zstream *
- get_zstream(VALUE 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");
- }
- return z;
- }
- /* ------------------------------------------------------------------------- */
- /*
- * 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(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(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(VALUE obj)
- {
- struct zstream *z = get_zstream(obj);
- zstream_run(z, (Bytef*)"", 0, Z_FINISH);
- return zstream_detach_buffer(z);
- }
- /*
- * call-seq:
- * flush_next_in -> input
- *
- */
- static VALUE
- rb_zstream_flush_next_in(VALUE obj)
- {
- struct zstream *z;
- VALUE dst;
- TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
- dst = zstream_detach_input(z);
- 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(VALUE obj)
- {
- struct zstream *z;
- 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(VALUE 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(VALUE obj, VALUE size)
- {
- struct zstream *z = get_zstream(obj);
- 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(VALUE obj)
- {
- struct zstream *z;
- TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
- return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input)));
- }
- /*
- * Returns the total bytes of the input data to the stream. FIXME
- */
- static VALUE
- 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(VALUE obj)
- {
- return rb_uint2inum(get_zstream(obj)->stream.total_out);
- }
- /*
- * Guesses the type of the data which have been inputed into the stream. The
- * returned value is either <tt>BINARY</tt>, <tt>ASCII</tt>, or
- * <tt>UNKNOWN</tt>.
- */
- static VALUE
- rb_zstream_data_type(VALUE obj)
- {
- return INT2FIX(get_zstream(obj)->stream.data_type);
- }
- /*
- * Returns the adler-32 checksum.
- */
- static VALUE
- rb_zstream_adler(VALUE obj)
- {
- return rb_uint2inum(get_zstream(obj)->stream.adler);
- }
- /*
- * Returns true if the stream is finished.
- */
- static VALUE
- 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(VALUE obj)
- {
- struct zstream *z;
- TypedData_Get_Struct(obj, struct zstream, &zstream_data_type, z);
- return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue;
- }
- /* ------------------------------------------------------------------------- */
- /*
- * 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) \
- : (FIX2INT((val))))
- #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
- #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
- #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
- #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
- #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
- static VALUE
- rb_deflate_s_allocate(VALUE 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(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);
- 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),
- ARG_STRATEGY(strategy));
- if (err != Z_OK) {
- raise_zlib_error(err, z->stream.msg);
- }
- ZSTREAM_READY(z);
- return obj;
- }
- /*
- * Document-method: Zlib::Deflate#initialize_copy
- *
- * Duplicates the deflate stream.
- */
- static VALUE
- rb_deflate_init_copy(VALUE self, VALUE orig)
- {
- struct zstream *z1, *z2;
- int err;
- TypedData_Get_Struct(self, struct zstream, &zstream_data_type, z1);
- z2 = get_zstream(orig);
- if (z1 == z2) return self;
- err = deflateCopy(&z1->stream, &z2->stream);
- if (err != Z_OK) {
- raise_zlib_error(err, 0);
- }
- z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input);
- z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf);
- z1->flags = z2->flags;
- return self;
- }
- static VALUE
- 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, 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, lev);
- if (err != Z_OK) {
- raise_zlib_error(err, z.stream.msg);
- }
- ZSTREAM_READY(&z);
- args[0] = (VALUE)&z;
- args[1] = src;
- dst = rb_ensure(deflate_run, (VALUE)args, zstream_ensure_end, (VALUE)&z);
- return dst;
- }
- static void
- do_deflate(struct zstream *z, VALUE src, int flush)
- {
- if (NIL_P(src)) {
- zstream_run(z, (Bytef*)"", 0, Z_FINISH);
- return;
- }
- StringValue(src);
- if (flush != Z_NO_FLUSH || RSTRING_LEN(src) > 0) { /* prevent BUF_ERROR */
- zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), flush);
- }
- }
- /*
- * Document-method: Zlib::Deflate#deflate
- *
- * call-seq:
- * z.deflate(string, flush = Zlib::NO_FLUSH) -> String
- * z.deflate(string, flush = Zlib::NO_FLUSH) { |chunk| ... } -> nil
- *
- * Inputs +string+ into the deflate stream and returns the output from the
- * stream. On calling this method, both the input and the output buffers of
- * the stream are flushed. If +string+ is nil, this method finishes the
- * 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);
- do_deflate(z, src, ARG_FLUSH(flush));
- return zstream_detach_buffer(z);
- }
- /*
- * 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(VALUE obj, VALUE 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(int argc, VALUE *argv, VALUE obj)
- {
- struct zstream *z = get_zstream(obj);
- 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, (Bytef*)"", 0, flush);
- }
- return zstream_detach_buffer(z);
- }
- /*
- * Document-method: Zlib::Deflate.params
- *
- * call-seq: params(level, strategy)
- *
- * Changes the parameters of the deflate str…
Large files files are truncated, but you can click here to view the full file