15 #ifdef HAVE_VALGRIND_MEMCHECK_H
16 # include <valgrind/memcheck.h>
17 # ifndef VALGRIND_MAKE_MEM_DEFINED
18 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
20 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
21 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
24 # define VALGRIND_MAKE_MEM_DEFINED(p, n) 0
25 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) 0
28 #define RUBY_ZLIB_VERSION "0.6.0"
31 #define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0)
34 #define GZIP_SUPPORT 1
39 #if MAX_MEM_LEVEL >= 8
40 #define DEF_MEM_LEVEL 8
42 #define DEF_MEM_LEVEL MAX_MEM_LEVEL
46 #if SIZEOF_LONG > SIZEOF_INT
50 if (n > UINT_MAX) n = UINT_MAX;
53 #define MAX_UINT(n) max_uint(n)
55 #define MAX_UINT(n) (uInt)(n)
58 #define sizeof(x) ((int)sizeof(x))
152 static unsigned long gzfile_get32(
const unsigned char*);
153 static void gzfile_set32(
unsigned long n,
unsigned char*);
330 case Z_VERSION_ERROR:
342 snprintf(buf, BUFSIZ,
"unknown zlib error %d: %s", err, msg);
356 fprintf(stderr,
"zlib(finalizer): %s\n", msg);
377 #if SIZEOF_LONG > SIZEOF_INT
379 checksum_long(uLong (*
func)(uLong,
const Bytef*, uInt), uLong sum,
const Bytef *ptr,
long len)
381 if (len > UINT_MAX) {
383 sum =
func(sum, ptr, UINT_MAX);
386 }
while (len >= UINT_MAX);
388 if (len > 0) sum =
func(sum, ptr, (uInt)len);
392 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
399 uLong (*
func)(uLong,
const Bytef*, uInt);
409 else if (
NIL_P(str)) {
413 sum =
func(0, Z_NULL, 0);
417 sum =
func(sum, Z_NULL, 0);
443 #ifdef HAVE_ADLER32_COMBINE
461 #define rb_zlib_adler32_combine rb_f_notimplement
481 #ifdef HAVE_CRC32_COMBINE
499 #define rb_zlib_crc32_combine rb_f_notimplement
510 #if !defined(HAVE_TYPE_Z_CRC_T)
512 typedef unsigned long z_crc_t;
514 const z_crc_t *crctbl;
518 crctbl = get_crc_table();
521 for (i = 0; i < 256; i++) {
540 int (*
run)(z_streamp, int);
544 #define ZSTREAM_FLAG_READY 0x1
545 #define ZSTREAM_FLAG_IN_STREAM 0x2
546 #define ZSTREAM_FLAG_FINISHED 0x4
547 #define ZSTREAM_FLAG_CLOSING 0x8
548 #define ZSTREAM_FLAG_GZFILE 0x10
550 #define ZSTREAM_FLAG_UNUSED 0x20
552 #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
553 #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
554 #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
555 #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING)
556 #define ZSTREAM_IS_GZFILE(z) ((z)->flags & ZSTREAM_FLAG_GZFILE)
558 #define ZSTREAM_EXPAND_BUFFER_OK 0
562 #define ZSTREAM_INITIAL_BUFSIZE 1024
564 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
565 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
568 deflateReset, deflateEnd, deflate,
572 inflateReset, inflateEnd, inflate,
610 z->
stream.opaque = Z_NULL;
612 z->
stream.next_in = Z_NULL;
614 z->
stream.next_out = Z_NULL;
619 #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
620 #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
683 else if (z->
stream.avail_out != size) {
698 return (
void *)(
VALUE)state;
752 if (z->
stream.avail_out >= (uInt)len) {
753 z->
stream.avail_out -= (uInt)len;
764 #define zstream_append_buffer2(z,v) \
765 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
823 z->
stream.avail_out = (uInt)buflen;
838 if (z->
stream.avail_out > 0) {
839 if (len > z->
stream.avail_out) len = z->
stream.avail_out;
841 z->
stream.avail_out-=(uInt)len;
855 if (z->
stream.avail_out > 0) {
864 if (len <= 0)
return;
876 #define zstream_append_input2(z,v)\
878 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
948 rb_warning(
"attempt to close uninitialized zstream; ignored.");
952 rb_warning(
"attempt to close unfinished zstream; reset forced.");
979 if (err == Z_STREAM_END) {
985 if (err != Z_OK && err != Z_BUF_ERROR)
988 if (z->
stream.avail_out > 0) {
993 if (z->
stream.avail_in == 0 && z->
func == &inflate_funcs) {
1015 return (
void *)(
VALUE)err;
1043 z->
stream.next_in = (Bytef*)
"";
1056 if (z->
stream.avail_out == 0) {
1064 if (flush != Z_FINISH && err == Z_BUF_ERROR
1065 && z->
stream.avail_out > 0) {
1071 if (err != Z_OK && err != Z_STREAM_END) {
1072 if (z->
stream.avail_in > 0) {
1075 if (err == Z_NEED_DICT) {
1087 if (z->
stream.avail_in > 0) {
1105 err = inflateSync(&z->
stream);
1113 if (err != Z_DATA_ERROR) {
1119 if (len <= 0)
return Qfalse;
1123 err = inflateSync(&z->
stream);
1128 if (err != Z_DATA_ERROR) {
1146 if (err == Z_STREAM_ERROR)
1148 if (err == Z_DATA_ERROR)
1170 z->
stream.opaque = (voidpf)obj;
1174 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
1175 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
1184 rb_raise(cZError,
"stream is not ready");
1435 #define FIXNUMARG(val, ifnil) \
1436 (NIL_P((val)) ? (ifnil) \
1437 : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val))))
1439 #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1440 #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1441 #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1442 #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1443 #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1530 rb_scan_args(argc, argv,
"04", &level, &wbits, &memlevel, &strategy);
1615 err = deflateInit(&z.
stream, lev);
1621 args[0] = (
VALUE)&z;
1637 if (flush != Z_NO_FLUSH ||
RSTRING_LEN(src) > 0) {
1721 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1722 if (flush != Z_NO_FLUSH) {
1745 int level, strategy;
1753 err = deflateParams(&z->
stream, level, strategy);
1755 while (err == Z_BUF_ERROR) {
1756 rb_warning(
"deflateParams() returned Z_BUF_ERROR");
1759 err = deflateParams(&z->
stream, level, strategy);
1792 err = deflateSetDictionary(&z->
stream,
1927 err = inflateInit(&z.
stream);
1933 args[0] = (
VALUE)&z;
2101 err = inflateSyncPoint(&z->
stream);
2127 err = inflateSetDictionary(&z->
stream,
2147 #define GZ_MAGIC1 0x1f
2148 #define GZ_MAGIC2 0x8b
2149 #define GZ_METHOD_DEFLATE 8
2150 #define GZ_FLAG_MULTIPART 0x2
2151 #define GZ_FLAG_EXTRA 0x4
2152 #define GZ_FLAG_ORIG_NAME 0x8
2153 #define GZ_FLAG_COMMENT 0x10
2154 #define GZ_FLAG_ENCRYPT 0x20
2155 #define GZ_FLAG_UNKNOWN_MASK 0xc0
2157 #define GZ_EXTRAFLAG_FAST 0x4
2158 #define GZ_EXTRAFLAG_SLOW 0x2
2161 #define OS_MSDOS 0x00
2162 #define OS_AMIGA 0x01
2164 #define OS_UNIX 0x03
2165 #define OS_ATARI 0x05
2167 #define OS_MACOS 0x07
2168 #define OS_TOPS20 0x0a
2169 #define OS_WIN32 0x0b
2171 #define OS_VMCMS 0x04
2172 #define OS_ZSYSTEM 0x08
2174 #define OS_QDOS 0x0c
2175 #define OS_RISCOS 0x0d
2176 #define OS_UNKNOWN 0xff
2179 #define OS_CODE OS_UNIX
2209 #define GZFILE_CBUF_CAPA 10
2211 #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
2212 #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
2213 #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
2215 #define GZFILE_IS_FINISHED(gz) \
2216 (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
2218 #define GZFILE_READ_SIZE 2048
2238 if (z->
func == &deflate_funcs) {
2239 finalizer_warn(
"Zlib::GzipWriter object must be closed explicitly.");
2252 const struct zstream_funcs *funcs;
2253 void (*endfunc)(struct
gzfile *);
2267 gz->
crc = crc32(0, Z_NULL, 0);
2282 #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2283 #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2289 gz->
crc = crc32(0, Z_NULL, 0);
2368 if (
NIL_P(str))
return 0;
2386 rb_raise(cGzError,
"unexpected end of file");
2398 n = *(src++) & 0xff;
2399 n |= (*(src++) & 0xff) << 8;
2403 static unsigned long
2407 n = *(src++) & 0xff;
2408 n |= (*(src++) & 0xff) << 8;
2409 n |= (*(src++) & 0xff) << 16;
2410 n |= (*(src++) & 0xffU) << 24;
2417 *(dst++) = n & 0xff;
2418 *(dst++) = (n >> 8) & 0xff;
2419 *(dst++) = (n >> 16) & 0xff;
2420 *dst = (n >> 24) & 0xff;
2444 if (!
NIL_P(input)) {
2457 unsigned char flags = 0, extraflags = 0;
2465 if (gz->
mtime == 0) {
2466 gz->
mtime = time(0);
2469 if (gz->
level == Z_BEST_SPEED) {
2472 else if (gz->
level == Z_BEST_COMPRESSION) {
2481 buf[8] = extraflags;
2511 const unsigned char *
head;
2525 rb_raise(cGzError,
"unsupported compression method %d", head[2]);
2530 rb_raise(cGzError,
"multi-part gzip file is not supported");
2533 rb_raise(cGzError,
"encrypted gzip file is not supported");
2536 rb_raise(cGzError,
"unknown flags 0x%02x", flags);
2540 gz->
level = Z_BEST_SPEED;
2543 gz->
level = Z_BEST_COMPRESSION;
2546 gz->
level = Z_DEFAULT_COMPRESSION;
2555 rb_raise(cGzError,
"unexpected end of file");
2559 rb_raise(cGzError,
"unexpected end of file");
2565 rb_raise(cGzError,
"unexpected end of file");
2575 rb_raise(cGzError,
"unexpected end of file");
2592 unsigned long crc, length;
2606 if (gz->
crc != crc) {
2607 rb_raise(cCRCError,
"invalid compressed data -- crc error");
2610 rb_raise(cLengthError,
"invalid compressed data -- length error");
2624 ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2638 rb_raise(cGzError,
"unexpected end of file");
2708 if (len < 0)
return Qnil;
2748 if (!
NIL_P(outbuf)) {
2796 const unsigned char *ss, *sp, *se;
2797 unsigned char *ds, *
dp, *de;
2804 ds = dp = (
unsigned char *)gz->
cbuf;
2922 rb_raise(cGzError,
"closed gzip stream");
2998 if (close_io_on_error) {
3188 rb_raise(cGzError,
"header is already written");
3214 rb_raise(cGzError,
"header is already written");
3238 rb_raise(cGzError,
"header is already written");
3474 if (!
NIL_P(opt)) argc--;
3477 rb_scan_args(argc, argv,
"12", &io, &level, &strategy);
3482 err = deflateInit2(&gz->
z.
stream, gz->
level, Z_DEFLATED,
3515 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3516 if (flush != Z_NO_FLUSH) {
3563 #define rb_gzwriter_addstr rb_io_addstr
3568 #define rb_gzwriter_printf rb_io_printf
3573 #define rb_gzwriter_print rb_io_print
3578 #define rb_gzwriter_puts rb_io_puts
3684 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
3890 rb_warn(
"Zlib::GzipReader#bytes is deprecated; use #each_byte instead");
3944 while (n++, *(p++) ==
'\n') {
3997 long rslen, n, limit = -1;
4012 else if (!
NIL_P(rs)) {
4082 if (!rspara)
rscheck(rsptr, rslen, rs);
4084 if (limit > 0 && filled >= limit) {
4087 res = memchr(p, rsptr[0], (filled - n + 1));
4090 if (limit > 0 && filled >= limit)
break;
4093 n += (long)(res - p);
4095 if (rslen == 1 ||
memcmp(p, rsptr, rslen) == 0)
break;
4170 rb_warn(
"Zlib::GzipReader#lines is deprecated; use #each_line instead");
4197 VALUE mZlib, cZStream, cDeflate, cInflate;
4199 VALUE cGzipFile, cGzipWriter, cGzipReader;
4308 INT2FIX(Z_DEFAULT_COMPRESSION));
4379 id_readpartial =
rb_intern(
"readpartial");