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:
341 rb_sprintf(
"unknown zlib error %d: %s", err, msg));
353 fprintf(stderr,
"zlib(finalizer): %s\n", msg);
374 #if SIZEOF_LONG > SIZEOF_INT
376 checksum_long(uLong (*
func)(uLong,
const Bytef*, uInt), uLong sum,
const Bytef *ptr,
long len)
378 if (len > UINT_MAX) {
380 sum =
func(sum, ptr, UINT_MAX);
383 }
while (len >= UINT_MAX);
385 if (len > 0) sum =
func(sum, ptr, (uInt)len);
389 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
396 uLong (*
func)(uLong,
const Bytef*, uInt);
406 else if (
NIL_P(str)) {
410 sum =
func(0, Z_NULL, 0);
414 sum =
func(sum, Z_NULL, 0);
440 #ifdef HAVE_ADLER32_COMBINE
458 #define rb_zlib_adler32_combine rb_f_notimplement
478 #ifdef HAVE_CRC32_COMBINE
496 #define rb_zlib_crc32_combine rb_f_notimplement
507 #if !defined(HAVE_TYPE_Z_CRC_T)
509 typedef unsigned long z_crc_t;
511 const z_crc_t *crctbl;
515 crctbl = get_crc_table();
518 for (i = 0; i < 256; i++) {
537 int (*
run)(z_streamp, int);
541 #define ZSTREAM_FLAG_READY 0x1
542 #define ZSTREAM_FLAG_IN_STREAM 0x2
543 #define ZSTREAM_FLAG_FINISHED 0x4
544 #define ZSTREAM_FLAG_CLOSING 0x8
545 #define ZSTREAM_FLAG_GZFILE 0x10
547 #define ZSTREAM_FLAG_UNUSED 0x20
549 #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
550 #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
551 #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
552 #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING)
553 #define ZSTREAM_IS_GZFILE(z) ((z)->flags & ZSTREAM_FLAG_GZFILE)
555 #define ZSTREAM_EXPAND_BUFFER_OK 0
559 #define ZSTREAM_INITIAL_BUFSIZE 1024
561 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
562 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
565 deflateReset, deflateEnd, deflate,
569 inflateReset, inflateEnd, inflate,
583 voidpf p =
xmalloc(items * size);
607 z->
stream.opaque = Z_NULL;
609 z->
stream.next_in = Z_NULL;
611 z->
stream.next_out = Z_NULL;
616 #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
617 #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
680 else if (z->
stream.avail_out != size) {
695 return (
void *)(
VALUE)state;
749 if (z->
stream.avail_out >= (uInt)len) {
750 z->
stream.avail_out -= (uInt)len;
761 #define zstream_append_buffer2(z,v) \
762 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
820 z->
stream.avail_out = (uInt)buflen;
835 if (z->
stream.avail_out > 0) {
836 if (len > z->
stream.avail_out) len = z->
stream.avail_out;
838 z->
stream.avail_out-=(uInt)len;
852 if (z->
stream.avail_out > 0) {
861 if (len <= 0)
return;
873 #define zstream_append_input2(z,v)\
875 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
945 rb_warning(
"attempt to close uninitialized zstream; ignored.");
949 rb_warning(
"attempt to close unfinished zstream; reset forced.");
976 if (err == Z_STREAM_END) {
982 if (err != Z_OK && err != Z_BUF_ERROR)
985 if (z->
stream.avail_out > 0) {
990 if (z->
stream.avail_in == 0 && z->
func == &inflate_funcs) {
1012 return (
void *)(
VALUE)err;
1040 z->
stream.next_in = (Bytef*)
"";
1053 if (z->
stream.avail_out == 0) {
1061 if (flush != Z_FINISH && err == Z_BUF_ERROR
1062 && z->
stream.avail_out > 0) {
1068 if (err != Z_OK && err != Z_STREAM_END) {
1069 if (z->
stream.avail_in > 0) {
1072 if (err == Z_NEED_DICT) {
1086 if (z->
stream.avail_in > 0) {
1104 err = inflateSync(&z->
stream);
1112 if (err != Z_DATA_ERROR) {
1118 if (len <= 0)
return Qfalse;
1122 err = inflateSync(&z->
stream);
1127 if (err != Z_DATA_ERROR) {
1145 if (err == Z_STREAM_ERROR)
1147 if (err == Z_DATA_ERROR)
1169 z->
stream.opaque = (voidpf)obj;
1173 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
1174 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
1183 rb_raise(cZError,
"stream is not ready");
1436 #define FIXNUMARG(val, ifnil) \
1437 (NIL_P((val)) ? (ifnil) \
1438 : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val))))
1440 #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1441 #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1442 #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1443 #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1444 #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1531 rb_scan_args(argc, argv,
"04", &level, &wbits, &memlevel, &strategy);
1559 if (z1 == z2)
return self;
1617 err = deflateInit(&z.
stream, lev);
1623 args[0] = (
VALUE)&z;
1639 if (flush != Z_NO_FLUSH ||
RSTRING_LEN(src) > 0) {
1723 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1724 if (flush != Z_NO_FLUSH) {
1747 int level, strategy;
1755 err = deflateParams(&z->
stream, level, strategy);
1757 while (err == Z_BUF_ERROR) {
1758 rb_warning(
"deflateParams() returned Z_BUF_ERROR");
1761 err = deflateParams(&z->
stream, level, strategy);
1794 err = deflateSetDictionary(&z->
stream,
1929 err = inflateInit(&z.
stream);
1935 args[0] = (
VALUE)&z;
2103 err = inflateSyncPoint(&z->
stream);
2129 err = inflateSetDictionary(&z->
stream,
2149 #define GZ_MAGIC1 0x1f
2150 #define GZ_MAGIC2 0x8b
2151 #define GZ_METHOD_DEFLATE 8
2152 #define GZ_FLAG_MULTIPART 0x2
2153 #define GZ_FLAG_EXTRA 0x4
2154 #define GZ_FLAG_ORIG_NAME 0x8
2155 #define GZ_FLAG_COMMENT 0x10
2156 #define GZ_FLAG_ENCRYPT 0x20
2157 #define GZ_FLAG_UNKNOWN_MASK 0xc0
2159 #define GZ_EXTRAFLAG_FAST 0x4
2160 #define GZ_EXTRAFLAG_SLOW 0x2
2163 #define OS_MSDOS 0x00
2164 #define OS_AMIGA 0x01
2166 #define OS_UNIX 0x03
2167 #define OS_ATARI 0x05
2169 #define OS_MACOS 0x07
2170 #define OS_TOPS20 0x0a
2171 #define OS_WIN32 0x0b
2173 #define OS_VMCMS 0x04
2174 #define OS_ZSYSTEM 0x08
2176 #define OS_QDOS 0x0c
2177 #define OS_RISCOS 0x0d
2178 #define OS_UNKNOWN 0xff
2181 #define OS_CODE OS_UNIX
2211 #define GZFILE_CBUF_CAPA 10
2213 #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
2214 #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
2215 #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
2217 #define GZFILE_IS_FINISHED(gz) \
2218 (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
2220 #define GZFILE_READ_SIZE 2048
2240 if (z->
func == &deflate_funcs) {
2241 finalizer_warn(
"Zlib::GzipWriter object must be closed explicitly.");
2254 const struct zstream_funcs *funcs;
2255 void (*endfunc)(struct
gzfile *);
2269 gz->
crc = crc32(0, Z_NULL, 0);
2284 #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2285 #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2292 gz->
crc = crc32(0, Z_NULL, 0);
2371 if (
NIL_P(str))
return 0;
2389 rb_raise(cGzError,
"unexpected end of file");
2401 n = *(src++) & 0xff;
2402 n |= (*(src++) & 0xff) << 8;
2406 static unsigned long
2410 n = *(src++) & 0xff;
2411 n |= (*(src++) & 0xff) << 8;
2412 n |= (*(src++) & 0xff) << 16;
2413 n |= (*(src++) & 0xffU) << 24;
2420 *(dst++) = n & 0xff;
2421 *(dst++) = (n >> 8) & 0xff;
2422 *(dst++) = (n >> 16) & 0xff;
2423 *dst = (n >> 24) & 0xff;
2447 if (!
NIL_P(input)) {
2460 unsigned char flags = 0, extraflags = 0;
2468 if (gz->
mtime == 0) {
2469 gz->
mtime = time(0);
2472 if (gz->
level == Z_BEST_SPEED) {
2475 else if (gz->
level == Z_BEST_COMPRESSION) {
2484 buf[8] = extraflags;
2514 const unsigned char *
head;
2528 rb_raise(cGzError,
"unsupported compression method %d", head[2]);
2533 rb_raise(cGzError,
"multi-part gzip file is not supported");
2536 rb_raise(cGzError,
"encrypted gzip file is not supported");
2539 rb_raise(cGzError,
"unknown flags 0x%02x", flags);
2543 gz->
level = Z_BEST_SPEED;
2546 gz->
level = Z_BEST_COMPRESSION;
2549 gz->
level = Z_DEFAULT_COMPRESSION;
2558 rb_raise(cGzError,
"unexpected end of file");
2562 rb_raise(cGzError,
"unexpected end of file");
2568 rb_raise(cGzError,
"unexpected end of file");
2578 rb_raise(cGzError,
"unexpected end of file");
2595 unsigned long crc, length;
2609 if (gz->
crc != crc) {
2610 rb_raise(cCRCError,
"invalid compressed data -- crc error");
2613 rb_raise(cLengthError,
"invalid compressed data -- length error");
2627 ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2641 rb_raise(cGzError,
"unexpected end of file");
2711 if (len < 0)
return Qnil;
2751 if (!
NIL_P(outbuf)) {
2776 if (
NIL_P(dst))
return dst;
2800 const unsigned char *ss, *sp, *se;
2801 unsigned char *ds, *
dp, *de;
2808 ds = dp = (
unsigned char *)gz->
cbuf;
2823 if (
NIL_P(dst))
return dst;
2927 rb_raise(cGzError,
"closed gzip stream");
3003 if (close_io_on_error) {
3193 rb_raise(cGzError,
"header is already written");
3215 rb_raise(cGzError,
"header is already written");
3239 rb_raise(cGzError,
"header is already written");
3475 if (!
NIL_P(opt)) argc--;
3478 rb_scan_args(argc, argv,
"12", &io, &level, &strategy);
3483 err = deflateInit2(&gz->
z.
stream, gz->
level, Z_DEFLATED,
3516 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3517 if (flush != Z_NO_FLUSH) {
3564 #define rb_gzwriter_addstr rb_io_addstr
3569 #define rb_gzwriter_printf rb_io_printf
3574 #define rb_gzwriter_print rb_io_print
3579 #define rb_gzwriter_puts rb_io_puts
3685 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
3891 rb_warn(
"Zlib::GzipReader#bytes is deprecated; use #each_byte instead");
3945 while (n++, *(p++) ==
'\n') {
3998 long rslen, n, limit = -1;
4013 else if (!
NIL_P(rs)) {
4045 if (
NIL_P(dst))
return dst;
4084 if (!rspara)
rscheck(rsptr, rslen, rs);
4086 if (limit > 0 && filled >= limit) {
4089 res = memchr(p, rsptr[0], (filled - n + 1));
4092 if (limit > 0 && filled >= limit)
break;
4095 n += (long)(res - p);
4097 if (rslen == 1 ||
memcmp(p, rsptr, rslen) == 0)
break;
4107 if (
NIL_P(dst))
return dst;
4173 rb_warn(
"Zlib::GzipReader#lines is deprecated; use #each_line instead");
4200 VALUE mZlib, cZStream, cDeflate, cInflate;
4202 VALUE cGzipFile, cGzipWriter, cGzipReader;
4311 INT2FIX(Z_DEFAULT_COMPRESSION));
4382 id_readpartial =
rb_intern(
"readpartial");