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:
299 snprintf(buf, BUFSIZ,
"unknown zlib error %d: %s", err, msg);
313 fprintf(stderr,
"zlib(finalizer): %s\n", msg);
334 #if SIZEOF_LONG > SIZEOF_INT
338 if (len > UINT_MAX) {
340 sum =
func(sum, ptr, UINT_MAX);
343 }
while (len >= UINT_MAX);
345 if (len > 0) sum =
func(sum, ptr, (uInt)len);
349 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
356 uLong (*
func)(uLong,
const Bytef*, uInt);
366 else if (
NIL_P(str)) {
370 sum =
func(0, Z_NULL, 0);
374 sum =
func(sum, Z_NULL, 0);
400 #ifdef HAVE_ADLER32_COMBINE
418 #define rb_zlib_adler32_combine rb_f_notimplement
438 #ifdef HAVE_CRC32_COMBINE
456 #define rb_zlib_crc32_combine rb_f_notimplement
467 #if !defined(HAVE_TYPE_Z_CRC_T)
469 typedef unsigned long z_crc_t;
471 const z_crc_t *crctbl;
475 crctbl = get_crc_table();
478 for (i = 0; i < 256; i++) {
497 int (*
run)(z_streamp, int);
501 #define ZSTREAM_FLAG_READY 0x1
502 #define ZSTREAM_FLAG_IN_STREAM 0x2
503 #define ZSTREAM_FLAG_FINISHED 0x4
504 #define ZSTREAM_FLAG_CLOSING 0x8
505 #define ZSTREAM_FLAG_UNUSED 0x10
507 #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY)
508 #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY)
509 #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED)
510 #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING)
514 #define ZSTREAM_INITIAL_BUFSIZE 1024
515 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
516 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
519 deflateReset, deflateEnd, deflate,
523 inflateReset, inflateEnd, inflate,
554 z->
stream.opaque = Z_NULL;
556 z->
stream.next_in = Z_NULL;
558 z->
stream.next_out = Z_NULL;
563 #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
564 #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
610 else if (z->
stream.avail_out != size) {
635 if (z->
stream.avail_out >= (uInt)len) {
636 z->
stream.avail_out -= (uInt)len;
647 #define zstream_append_buffer2(z,v) \
648 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
691 z->
stream.avail_out = (uInt)buflen;
706 if (z->
stream.avail_out > 0) {
707 if (len > z->
stream.avail_out) len = z->
stream.avail_out;
709 z->
stream.avail_out-=(uInt)len;
723 if (z->
stream.avail_out > 0) {
732 if (len <= 0)
return;
744 #define zstream_append_input2(z,v)\
746 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
816 rb_warning(
"attempt to close uninitialized zstream; ignored.");
820 rb_warning(
"attempt to close unfinished zstream; reset forced.");
841 z->
stream.next_in = (Bytef*)
"";
854 if (z->
stream.avail_out == 0) {
867 if (err == Z_STREAM_END) {
873 if (flush != Z_FINISH && err == Z_BUF_ERROR
874 && z->
stream.avail_out > 0) {
879 if (z->
stream.avail_in > 0) {
884 if (z->
stream.avail_out > 0) {
892 if (z->
stream.avail_in > 0) {
907 err = inflateSync(&z->
stream);
915 if (err != Z_DATA_ERROR) {
921 if (len <= 0)
return Qfalse;
925 err = inflateSync(&z->
stream);
930 if (err != Z_DATA_ERROR) {
948 if (err == Z_STREAM_ERROR)
950 if (err == Z_DATA_ERROR)
975 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
976 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
985 rb_raise(cZError,
"stream is not ready");
1230 #define FIXNUMARG(val, ifnil) \
1231 (NIL_P((val)) ? (ifnil) \
1232 : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val))))
1234 #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1235 #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1236 #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1237 #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1238 #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1309 VALUE level, wbits, memlevel, strategy;
1312 rb_scan_args(argc, argv,
"04", &level, &wbits, &memlevel, &strategy);
1397 err = deflateInit(&z.
stream, lev);
1403 args[0] = (
VALUE)&z;
1419 if (flush != Z_NO_FLUSH ||
RSTRING_LEN(src) > 0) {
1461 VALUE src, flush, dst;
1509 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1510 if (flush != Z_NO_FLUSH) {
1542 int level, strategy;
1550 err = deflateParams(&z->
stream, level, strategy);
1552 while (err == Z_BUF_ERROR) {
1553 rb_warning(
"deflateParams() returned Z_BUF_ERROR");
1556 err = deflateParams(&z->
stream, level, strategy);
1589 err = deflateSetDictionary(&z->
stream,
1717 err = inflateInit(&z.
stream);
1723 args[0] = (
VALUE)&z;
1867 err = inflateSyncPoint(&z->
stream);
1893 err = inflateSetDictionary(&z->
stream,
1913 #define GZ_MAGIC1 0x1f
1914 #define GZ_MAGIC2 0x8b
1915 #define GZ_METHOD_DEFLATE 8
1916 #define GZ_FLAG_MULTIPART 0x2
1917 #define GZ_FLAG_EXTRA 0x4
1918 #define GZ_FLAG_ORIG_NAME 0x8
1919 #define GZ_FLAG_COMMENT 0x10
1920 #define GZ_FLAG_ENCRYPT 0x20
1921 #define GZ_FLAG_UNKNOWN_MASK 0xc0
1923 #define GZ_EXTRAFLAG_FAST 0x4
1924 #define GZ_EXTRAFLAG_SLOW 0x2
1927 #define OS_MSDOS 0x00
1928 #define OS_AMIGA 0x01
1930 #define OS_UNIX 0x03
1931 #define OS_ATARI 0x05
1933 #define OS_MACOS 0x07
1934 #define OS_TOPS20 0x0a
1935 #define OS_WIN32 0x0b
1937 #define OS_VMCMS 0x04
1938 #define OS_ZSYSTEM 0x08
1940 #define OS_QDOS 0x0c
1941 #define OS_RISCOS 0x0d
1942 #define OS_UNKNOWN 0xff
1945 #define OS_CODE OS_UNIX
1975 #define GZFILE_CBUF_CAPA 10
1977 #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
1978 #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
1979 #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
1981 #define GZFILE_IS_FINISHED(gz) \
1982 (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
1984 #define GZFILE_READ_SIZE 2048
2004 if (z->
func == &deflate_funcs) {
2005 finalizer_warn(
"Zlib::GzipWriter object must be closed explicitly.");
2018 const struct zstream_funcs *funcs;
2019 void (*endfunc)(struct
gzfile *);
2032 gz->
crc = crc32(0, Z_NULL, 0);
2047 #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2048 #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2054 gz->
crc = crc32(0, Z_NULL, 0);
2133 if (
NIL_P(str))
return 0;
2151 rb_raise(cGzError,
"unexpected end of file");
2163 n = *(src++) & 0xff;
2164 n |= (*(src++) & 0xff) << 8;
2168 static unsigned long
2172 n = *(src++) & 0xff;
2173 n |= (*(src++) & 0xff) << 8;
2174 n |= (*(src++) & 0xff) << 16;
2175 n |= (*(src++) & 0xffU) << 24;
2182 *(dst++) = n & 0xff;
2183 *(dst++) = (n >> 8) & 0xff;
2184 *(dst++) = (n >> 16) & 0xff;
2185 *dst = (n >> 24) & 0xff;
2209 if (!
NIL_P(input)) {
2222 unsigned char flags = 0, extraflags = 0;
2230 if (gz->
mtime == 0) {
2231 gz->
mtime = time(0);
2234 if (gz->
level == Z_BEST_SPEED) {
2237 else if (gz->
level == Z_BEST_COMPRESSION) {
2246 buf[8] = extraflags;
2276 const unsigned char *head;
2290 rb_raise(cGzError,
"unsupported compression method %d", head[2]);
2295 rb_raise(cGzError,
"multi-part gzip file is not supported");
2298 rb_raise(cGzError,
"encrypted gzip file is not supported");
2301 rb_raise(cGzError,
"unknown flags 0x%02x", flags);
2305 gz->
level = Z_BEST_SPEED;
2308 gz->
level = Z_BEST_COMPRESSION;
2311 gz->
level = Z_DEFAULT_COMPRESSION;
2320 rb_raise(cGzError,
"unexpected end of file");
2324 rb_raise(cGzError,
"unexpected end of file");
2330 rb_raise(cGzError,
"unexpected end of file");
2340 rb_raise(cGzError,
"unexpected end of file");
2357 unsigned long crc, length;
2371 if (gz->
crc != crc) {
2372 rb_raise(cCRCError,
"invalid compressed data -- crc error");
2375 rb_raise(cLengthError,
"invalid compressed data -- length error");
2389 ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2403 rb_raise(cGzError,
"unexpected end of file");
2473 if (len < 0)
return Qnil;
2513 if (!
NIL_P(outbuf)) {
2561 const unsigned char *ss, *sp, *se;
2562 unsigned char *ds, *
dp, *de;
2570 ds = dp = (
unsigned char *)gz->
cbuf;
2688 rb_raise(cGzError,
"closed gzip stream");
2764 if (close_io_on_error) {
2951 rb_raise(cGzError,
"header is already written");
2977 rb_raise(cGzError,
"header is already written");
3001 rb_raise(cGzError,
"header is already written");
3232 if (!
NIL_P(opt)) argc--;
3235 rb_scan_args(argc, argv,
"12", &io, &level, &strategy);
3240 err = deflateInit2(&gz->
z.
stream, gz->
level, Z_DEFLATED,
3273 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3274 if (flush != Z_NO_FLUSH) {
3321 #define rb_gzwriter_addstr rb_io_addstr
3326 #define rb_gzwriter_printf rb_io_printf
3331 #define rb_gzwriter_print rb_io_print
3336 #define rb_gzwriter_puts rb_io_puts
3437 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
3683 while (n++, *(p++) ==
'\n') {
3736 long rslen, n, limit = -1;
3751 else if (!
NIL_P(rs)) {
3821 if (!rspara)
rscheck(rsptr, rslen, rs);
3823 if (limit > 0 && filled >= limit) {
3826 res = memchr(p, rsptr[0], (filled - n + 1));
3829 if (limit > 0 && filled >= limit)
break;
3832 n += (
long)(res - p);
3834 if (rslen == 1 ||
memcmp(p, rsptr, rslen) == 0)
break;
4005 VALUE mZlib, cZStream, cDeflate, cInflate;
4007 VALUE cGzipFile, cGzipWriter, cGzipReader;
4102 INT2FIX(Z_DEFAULT_COMPRESSION));
4156 id_readpartial =
rb_intern(
"readpartial");