14 #ifdef HAVE_VALGRIND_MEMCHECK_H
15 # include <valgrind/memcheck.h>
16 # ifndef VALGRIND_MAKE_MEM_DEFINED
17 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n))
19 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
20 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
23 # define VALGRIND_MAKE_MEM_DEFINED(p, n)
24 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n)
27 #define RUBY_ZLIB_VERSION "0.6.0"
30 #define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0)
33 #define GZIP_SUPPORT 1
38 #if MAX_MEM_LEVEL >= 8
39 #define DEF_MEM_LEVEL 8
41 #define DEF_MEM_LEVEL MAX_MEM_LEVEL
45 #if SIZEOF_LONG > SIZEOF_INT
49 if (n > UINT_MAX) n = UINT_MAX;
52 #define MAX_UINT(n) max_uint(n)
54 #define MAX_UINT(n) (uInt)(n)
57 #define sizeof(x) ((int)sizeof(x))
148 static unsigned long gzfile_get32(
const unsigned char*);
149 static void gzfile_set32(
unsigned long n,
unsigned char*);
287 case Z_VERSION_ERROR:
298 rb_sprintf(
"unknown zlib error %d: %s", err, msg));
310 fprintf(stderr,
"zlib(finalizer): %s\n", msg);
331 #if SIZEOF_LONG > SIZEOF_INT
335 if (len > UINT_MAX) {
337 sum =
func(sum, ptr, UINT_MAX);
340 }
while (len >= UINT_MAX);
342 if (len > 0) sum =
func(sum, ptr, (uInt)len);
346 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
353 uLong (*
func)(uLong,
const Bytef*, uInt);
363 else if (
NIL_P(str)) {
367 sum =
func(0, Z_NULL, 0);
371 sum =
func(sum, Z_NULL, 0);
397 #ifdef HAVE_ADLER32_COMBINE
415 #define rb_zlib_adler32_combine rb_f_notimplement
435 #ifdef HAVE_CRC32_COMBINE
453 #define rb_zlib_crc32_combine rb_f_notimplement
464 #if !defined(HAVE_TYPE_Z_CRC_T)
466 typedef unsigned long z_crc_t;
468 const z_crc_t *crctbl;
472 crctbl = get_crc_table();
475 for (i = 0; i < 256; i++) {
494 int (*
run)(z_streamp, int);
498 #define ZSTREAM_FLAG_READY 0x1
499 #define ZSTREAM_FLAG_IN_STREAM 0x2
500 #define ZSTREAM_FLAG_FINISHED 0x4
501 #define ZSTREAM_FLAG_CLOSING 0x8
502 #define ZSTREAM_FLAG_UNUSED 0x10
504 #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
505 #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
506 #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
507 #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING)
511 #define ZSTREAM_INITIAL_BUFSIZE 1024
512 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
513 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
516 deflateReset, deflateEnd, deflate,
520 inflateReset, inflateEnd, inflate,
551 z->
stream.opaque = Z_NULL;
553 z->
stream.next_in = Z_NULL;
555 z->
stream.next_out = Z_NULL;
560 #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
561 #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
607 else if (z->
stream.avail_out != size) {
632 if (z->
stream.avail_out >= (uInt)len) {
633 z->
stream.avail_out -= (uInt)len;
644 #define zstream_append_buffer2(z,v) \
645 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
688 z->
stream.avail_out = (uInt)buflen;
703 if (z->
stream.avail_out > 0) {
704 if (len > z->
stream.avail_out) len = z->
stream.avail_out;
706 z->
stream.avail_out-=(uInt)len;
720 if (z->
stream.avail_out > 0) {
729 if (len <= 0)
return;
741 #define zstream_append_input2(z,v)\
743 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
813 rb_warning(
"attempt to close uninitialized zstream; ignored.");
817 rb_warning(
"attempt to close unfinished zstream; reset forced.");
838 z->
stream.next_in = (Bytef*)
"";
851 if (z->
stream.avail_out == 0) {
864 if (err == Z_STREAM_END) {
870 if (flush != Z_FINISH && err == Z_BUF_ERROR
871 && z->
stream.avail_out > 0) {
876 if (z->
stream.avail_in > 0) {
881 if (z->
stream.avail_out > 0) {
889 if (z->
stream.avail_in > 0) {
904 err = inflateSync(&z->
stream);
912 if (err != Z_DATA_ERROR) {
918 if (len <= 0)
return Qfalse;
922 err = inflateSync(&z->
stream);
927 if (err != Z_DATA_ERROR) {
945 if (err == Z_STREAM_ERROR)
947 if (err == Z_DATA_ERROR)
972 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
973 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
982 rb_raise(cZError,
"stream is not ready");
1227 #define FIXNUMARG(val, ifnil) \
1228 (NIL_P((val)) ? (ifnil) \
1229 : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val))))
1231 #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1232 #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1233 #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1234 #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1235 #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1306 VALUE level, wbits, memlevel, strategy;
1309 rb_scan_args(argc, argv,
"04", &level, &wbits, &memlevel, &strategy);
1337 if (z1 == z2)
return self;
1395 err = deflateInit(&z.
stream, lev);
1401 args[0] = (
VALUE)&z;
1417 if (flush != Z_NO_FLUSH ||
RSTRING_LEN(src) > 0) {
1459 VALUE src, flush, dst;
1507 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1508 if (flush != Z_NO_FLUSH) {
1540 int level, strategy;
1548 err = deflateParams(&z->
stream, level, strategy);
1550 while (err == Z_BUF_ERROR) {
1551 rb_warning(
"deflateParams() returned Z_BUF_ERROR");
1554 err = deflateParams(&z->
stream, level, strategy);
1587 err = deflateSetDictionary(&z->
stream,
1715 err = inflateInit(&z.
stream);
1721 args[0] = (
VALUE)&z;
1865 err = inflateSyncPoint(&z->
stream);
1891 err = inflateSetDictionary(&z->
stream,
1911 #define GZ_MAGIC1 0x1f
1912 #define GZ_MAGIC2 0x8b
1913 #define GZ_METHOD_DEFLATE 8
1914 #define GZ_FLAG_MULTIPART 0x2
1915 #define GZ_FLAG_EXTRA 0x4
1916 #define GZ_FLAG_ORIG_NAME 0x8
1917 #define GZ_FLAG_COMMENT 0x10
1918 #define GZ_FLAG_ENCRYPT 0x20
1919 #define GZ_FLAG_UNKNOWN_MASK 0xc0
1921 #define GZ_EXTRAFLAG_FAST 0x4
1922 #define GZ_EXTRAFLAG_SLOW 0x2
1925 #define OS_MSDOS 0x00
1926 #define OS_AMIGA 0x01
1928 #define OS_UNIX 0x03
1929 #define OS_ATARI 0x05
1931 #define OS_MACOS 0x07
1932 #define OS_TOPS20 0x0a
1933 #define OS_WIN32 0x0b
1935 #define OS_VMCMS 0x04
1936 #define OS_ZSYSTEM 0x08
1938 #define OS_QDOS 0x0c
1939 #define OS_RISCOS 0x0d
1940 #define OS_UNKNOWN 0xff
1943 #define OS_CODE OS_UNIX
1973 #define GZFILE_CBUF_CAPA 10
1975 #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
1976 #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
1977 #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
1979 #define GZFILE_IS_FINISHED(gz) \
1980 (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
1982 #define GZFILE_READ_SIZE 2048
2002 if (z->
func == &deflate_funcs) {
2003 finalizer_warn(
"Zlib::GzipWriter object must be closed explicitly.");
2016 const struct zstream_funcs *funcs;
2017 void (*endfunc)(struct
gzfile *);
2030 gz->
crc = crc32(0, Z_NULL, 0);
2045 #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2046 #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2052 gz->
crc = crc32(0, Z_NULL, 0);
2131 if (
NIL_P(str))
return 0;
2149 rb_raise(cGzError,
"unexpected end of file");
2161 n = *(src++) & 0xff;
2162 n |= (*(src++) & 0xff) << 8;
2166 static unsigned long
2170 n = *(src++) & 0xff;
2171 n |= (*(src++) & 0xff) << 8;
2172 n |= (*(src++) & 0xff) << 16;
2173 n |= (*(src++) & 0xffU) << 24;
2180 *(dst++) = n & 0xff;
2181 *(dst++) = (n >> 8) & 0xff;
2182 *(dst++) = (n >> 16) & 0xff;
2183 *dst = (n >> 24) & 0xff;
2207 if (!
NIL_P(input)) {
2220 unsigned char flags = 0, extraflags = 0;
2228 if (gz->
mtime == 0) {
2229 gz->
mtime = time(0);
2232 if (gz->
level == Z_BEST_SPEED) {
2235 else if (gz->
level == Z_BEST_COMPRESSION) {
2244 buf[8] = extraflags;
2274 const unsigned char *head;
2288 rb_raise(cGzError,
"unsupported compression method %d", head[2]);
2293 rb_raise(cGzError,
"multi-part gzip file is not supported");
2296 rb_raise(cGzError,
"encrypted gzip file is not supported");
2299 rb_raise(cGzError,
"unknown flags 0x%02x", flags);
2303 gz->
level = Z_BEST_SPEED;
2306 gz->
level = Z_BEST_COMPRESSION;
2309 gz->
level = Z_DEFAULT_COMPRESSION;
2318 rb_raise(cGzError,
"unexpected end of file");
2322 rb_raise(cGzError,
"unexpected end of file");
2328 rb_raise(cGzError,
"unexpected end of file");
2338 rb_raise(cGzError,
"unexpected end of file");
2355 unsigned long crc, length;
2369 if (gz->
crc != crc) {
2370 rb_raise(cCRCError,
"invalid compressed data -- crc error");
2373 rb_raise(cLengthError,
"invalid compressed data -- length error");
2387 ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2401 rb_raise(cGzError,
"unexpected end of file");
2471 if (len < 0)
return Qnil;
2511 if (!
NIL_P(outbuf)) {
2559 const unsigned char *ss, *sp, *se;
2560 unsigned char *ds, *
dp, *de;
2568 ds = dp = (
unsigned char *)gz->
cbuf;
2686 rb_raise(cGzError,
"closed gzip stream");
2762 if (close_io_on_error) {
2949 rb_raise(cGzError,
"header is already written");
2975 rb_raise(cGzError,
"header is already written");
2999 rb_raise(cGzError,
"header is already written");
3230 if (!
NIL_P(opt)) argc--;
3233 rb_scan_args(argc, argv,
"12", &io, &level, &strategy);
3238 err = deflateInit2(&gz->
z.
stream, gz->
level, Z_DEFLATED,
3271 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3272 if (flush != Z_NO_FLUSH) {
3319 #define rb_gzwriter_addstr rb_io_addstr
3324 #define rb_gzwriter_printf rb_io_printf
3329 #define rb_gzwriter_print rb_io_print
3334 #define rb_gzwriter_puts rb_io_puts
3435 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
3681 while (n++, *(p++) ==
'\n') {
3734 long rslen, n, limit = -1;
3749 else if (!
NIL_P(rs)) {
3819 if (!rspara)
rscheck(rsptr, rslen, rs);
3821 if (limit > 0 && filled >= limit) {
3824 res = memchr(p, rsptr[0], (filled - n + 1));
3827 if (limit > 0 && filled >= limit)
break;
3830 n += (
long)(res - p);
3832 if (rslen == 1 ||
memcmp(p, rsptr, rslen) == 0)
break;
4003 VALUE mZlib, cZStream, cDeflate, cInflate;
4005 VALUE cGzipFile, cGzipWriter, cGzipReader;
4100 INT2FIX(Z_DEFAULT_COMPRESSION));
4154 id_readpartial =
rb_intern(
"readpartial");