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 "1.1.0"
30 #ifndef RB_PASS_CALLED_KEYWORDS
31 # define rb_class_new_instance_kw(argc, argv, klass, kw_splat) rb_class_new_instance(argc, argv, klass)
35 #define GZIP_SUPPORT 1
40 #if MAX_MEM_LEVEL >= 8
41 #define DEF_MEM_LEVEL 8
43 #define DEF_MEM_LEVEL MAX_MEM_LEVEL
47 #if SIZEOF_LONG > SIZEOF_INT
54 #define MAX_UINT(n) max_uint(n)
56 #define MAX_UINT(n) (uInt)(n)
59 static ID id_dictionaries;
63 static NORETURN(
void raise_zlib_error(
int,
const char*));
65 static VALUE do_checksum(
int,
VALUE*, uLong (*)(uLong,
const Bytef*, uInt));
69 static voidpf zlib_mem_alloc(voidpf, uInt, uInt);
70 static void zlib_mem_free(voidpf, voidpf);
71 static void finalizer_warn(
const char*);
76 static void zstream_init(
struct zstream*,
const struct zstream_funcs*);
77 static void zstream_expand_buffer(
struct zstream*);
78 static void zstream_expand_buffer_into(
struct zstream*,
unsigned long);
79 static int zstream_expand_buffer_non_stream(
struct zstream *
z);
80 static void zstream_append_buffer(
struct zstream*,
const Bytef*,
long);
82 static VALUE zstream_shift_buffer(
struct zstream*,
long);
83 static void zstream_buffer_ungets(
struct zstream*,
const Bytef*,
unsigned long);
84 static void zstream_buffer_ungetbyte(
struct zstream*,
int);
85 static void zstream_append_input(
struct zstream*,
const Bytef*,
long);
86 static void zstream_discard_input(
struct zstream*,
long);
87 static void zstream_reset_input(
struct zstream*);
88 static void zstream_passthrough_input(
struct zstream*);
90 static void zstream_reset(
struct zstream*);
93 static void zstream_run(
struct zstream*, Bytef*,
long,
int);
94 static VALUE zstream_sync(
struct zstream*, Bytef*,
long);
95 static void zstream_mark(
void*);
96 static void zstream_free(
void*);
97 static VALUE zstream_new(
VALUE,
const struct zstream_funcs*);
99 static void zstream_finalize(
struct zstream*);
105 static VALUE rb_zstream_flush_next_out(
VALUE);
141 static void gzfile_mark(
void*);
142 static void gzfile_free(
void*);
143 static VALUE gzfile_new(
VALUE,
const struct zstream_funcs*,
void (*)
_((
struct gzfile*)));
144 static void gzfile_reset(
struct gzfile*);
145 static void gzfile_close(
struct gzfile*,
int);
146 static void gzfile_write_raw(
struct gzfile*);
150 static int gzfile_read_raw_ensure(
struct gzfile*,
long,
VALUE outbuf);
151 static char *gzfile_read_raw_until_zero(
struct gzfile*,
long);
152 static unsigned int gzfile_get16(
const unsigned char*);
153 static unsigned long gzfile_get32(
const unsigned char*);
154 static void gzfile_set32(
unsigned long n,
unsigned char*);
155 static void gzfile_make_header(
struct gzfile*);
156 static void gzfile_make_footer(
struct gzfile*);
157 static void gzfile_read_header(
struct gzfile*,
VALUE outbuf);
158 static void gzfile_check_footer(
struct gzfile*,
VALUE outbuf);
159 static void gzfile_write(
struct gzfile*, Bytef*,
long);
160 static long gzfile_read_more(
struct gzfile*,
VALUE outbuf);
161 static void gzfile_calc_crc(
struct gzfile*,
VALUE);
164 static void gzfile_ungets(
struct gzfile*,
const Bytef*,
long);
165 static void gzfile_ungetbyte(
struct gzfile*,
int);
167 static void gzfile_writer_end(
struct gzfile*);
169 static void gzfile_reader_end(
struct gzfile*);
170 static void gzfile_reader_rewind(
struct gzfile*);
171 static VALUE gzfile_reader_get_unused(
struct gzfile*);
219 static void gzreader_skip_linebreaks(
struct gzfile*);
303 static VALUE cZError, cStreamEnd, cNeedDict;
304 static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError;
307 raise_zlib_error(
int err,
const char *msg)
331 case Z_VERSION_ERROR:
352 finalizer_warn(
const char *msg)
371 #if SIZEOF_LONG > SIZEOF_INT
373 checksum_long(uLong (*func)(uLong,
const Bytef*, uInt), uLong sum,
const Bytef *
ptr,
long len)
382 if (
len > 0) sum = func(sum,
ptr, (uInt)
len);
386 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
390 do_checksum(
int argc,
VALUE *
argv, uLong (*func)(uLong,
const Bytef*, uInt))
404 sum = func(0, Z_NULL, 0);
408 sum = func(sum, Z_NULL, 0);
438 return do_checksum(
argc,
argv, adler32);
441 #ifdef HAVE_ADLER32_COMBINE
459 #define rb_zlib_adler32_combine rb_f_notimplement
476 return do_checksum(
argc,
argv, crc32);
479 #ifdef HAVE_CRC32_COMBINE
497 #define rb_zlib_crc32_combine rb_f_notimplement
508 #if !defined(HAVE_TYPE_Z_CRC_T)
510 typedef unsigned long z_crc_t;
512 const z_crc_t *crctbl;
516 crctbl = get_crc_table();
519 for (
i = 0;
i < 256;
i++) {
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)
554 #define ZSTREAM_BUF_FILLED(z) (NIL_P((z)->buf) ? 0 : RSTRING_LEN((z)->buf))
556 #define ZSTREAM_EXPAND_BUFFER_OK 0
560 #define ZSTREAM_INITIAL_BUFSIZE 1024
562 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
563 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
565 static const struct zstream_funcs deflate_funcs = {
566 deflateReset, deflateEnd, deflate,
569 static const struct zstream_funcs inflate_funcs = {
570 inflateReset, inflateEnd, inflate,
582 zlib_mem_alloc(voidpf opaque, uInt items, uInt
size)
594 zlib_mem_free(voidpf opaque, voidpf address)
600 zstream_init(
struct zstream *z,
const struct zstream_funcs *
func)
605 z->
stream.zalloc = zlib_mem_alloc;
606 z->
stream.zfree = zlib_mem_free;
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)
620 zstream_expand_buffer(
struct zstream *z)
645 zstream_expand_buffer_into(z,
650 zstream_expand_buffer_non_stream(z);
655 zstream_expand_buffer_into(
struct zstream *z,
unsigned long size)
673 zstream_expand_buffer_protect(
void *
ptr)
680 return (
void *)(
VALUE)state;
684 zstream_expand_buffer_non_stream(
struct zstream *z)
707 zstream_append_buffer(
struct zstream *z,
const Bytef *
src,
long len)
734 #define zstream_append_buffer2(z,v) \
735 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
738 zstream_detach_buffer(
struct zstream *z)
770 zstream_shift_buffer(
struct zstream *z,
long len)
777 return zstream_detach_buffer(z);
790 z->
stream.avail_out = (uInt)buflen;
796 zstream_buffer_ungets(
struct zstream *z,
const Bytef *b,
unsigned long len)
802 zstream_expand_buffer_into(z,
len);
809 if (z->
stream.avail_out > 0) {
817 zstream_buffer_ungetbyte(
struct zstream *z,
int c)
820 zstream_buffer_ungets(z, &
cc, 1);
824 zstream_append_input(
struct zstream *z,
const Bytef *
src,
long len)
826 if (
len <= 0)
return;
838 #define zstream_append_input2(z,v)\
840 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
843 zstream_discard_input(
struct zstream *z,
long len)
853 newlen = oldlen -
len;
881 zstream_reset_input(
struct zstream *z)
892 zstream_passthrough_input(
struct zstream *z)
901 zstream_detach_input(
struct zstream *z)
917 zstream_reset(
struct zstream *z)
929 zstream_reset_input(z);
938 rb_warning(
"attempt to close uninitialized zstream; ignored.");
942 rb_warning(
"attempt to close unfinished zstream; reset forced.");
946 zstream_reset_input(z);
956 zstream_ensure_end(
VALUE v)
958 return zstream_end((
struct zstream *)
v);
962 zstream_run_func(
void *
ptr)
975 if (
err == Z_STREAM_END) {
981 if (
err != Z_OK &&
err != Z_BUF_ERROR)
984 if (z->
stream.avail_out > 0) {
989 if (z->
stream.avail_in == 0 && z->
func == &inflate_funcs) {
1002 state = zstream_expand_buffer_non_stream(z);
1020 zstream_unblock_func(
void *
ptr)
1041 z->
stream.next_in = (Bytef*)
"";
1045 zstream_append_input(
z,
src,
len);
1049 old_input = zstream_detach_input(
z);
1055 if (
z->
stream.avail_out == 0) {
1056 zstream_expand_buffer(
z);
1060 #ifndef RB_NOGVL_UBF_ASYNC_SAFE
1062 zstream_unblock_func, (
void *)&args);
1065 zstream_unblock_func, (
void *)&args,
1069 if (
flush != Z_FINISH &&
err == Z_BUF_ERROR
1074 zstream_reset_input(
z);
1076 if (
err != Z_OK &&
err != Z_STREAM_END) {
1080 if (
err == Z_NEED_DICT) {
1086 rb_inflate_set_dictionary(
self, dict);
1097 if (!
NIL_P(old_input)) {
1102 if (args.jump_state)
1117 zstream_discard_input(
z,
1119 zstream_append_input(
z,
src,
len);
1122 zstream_reset_input(
z);
1123 if (
err != Z_DATA_ERROR) {
1138 if (
err != Z_DATA_ERROR) {
1146 zstream_mark(
void *p)
1154 zstream_finalize(
struct zstream *z)
1157 if (
err == Z_STREAM_ERROR)
1158 finalizer_warn(
"the stream state was inconsistent.");
1159 if (
err == Z_DATA_ERROR)
1160 finalizer_warn(
"the stream was freed prematurely.");
1164 zstream_free(
void *p)
1169 zstream_finalize(z);
1175 zstream_memsize(
const void *p)
1178 return sizeof(
struct zstream);
1183 { zstream_mark, zstream_free, zstream_memsize, },
1188 zstream_new(
VALUE klass,
const struct zstream_funcs *funcs)
1194 zstream_init(z, funcs);
1199 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
1200 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
1209 rb_raise(cZError,
"stream is not ready");
1285 zstream_end(get_zstream(
obj));
1296 zstream_reset(get_zstream(
obj));
1314 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1316 return zstream_detach_buffer(z);
1325 rb_zstream_flush_next_in(
VALUE obj)
1331 dst = zstream_detach_input(z);
1345 rb_zstream_flush_next_out(
VALUE obj)
1351 return zstream_detach_buffer(z);
1460 #define FIXNUMARG(val, ifnil) \
1461 (NIL_P((val)) ? (ifnil) \
1464 #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1465 #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1466 #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1467 #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1468 #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1554 VALUE level, wbits, memlevel, strategy;
1583 z2 = get_zstream(orig);
1585 if (z1 == z2)
return self;
1588 raise_zlib_error(
err, 0);
1598 deflate_run(
VALUE args)
1604 return zstream_detach_buffer(z);
1648 args[0] = (
VALUE)&z;
1659 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1703 return zstream_detach_buffer(z);
1718 do_deflate(get_zstream(
obj),
src, Z_NO_FLUSH);
1745 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1746 if (flush != Z_NO_FLUSH) {
1747 zstream_run(z, (Bytef*)
"", 0, flush);
1750 return zstream_detach_buffer(z);
1769 int level, strategy;
1778 err = deflateParams(&z->
stream, level, strategy);
1779 filled =
n - z->
stream.avail_out;
1780 while (
err == Z_BUF_ERROR) {
1781 rb_warning(
"deflateParams() returned Z_BUF_ERROR");
1782 zstream_expand_buffer(z);
1785 err = deflateParams(&z->
stream, level, strategy);
1786 filled =
n - z->
stream.avail_out;
1911 inflate_run(
VALUE args)
1917 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1918 return zstream_detach_buffer(z);
1959 args[0] = (
VALUE)&z;
1970 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1992 VALUE checksum = do_checksum(1, &dictionary, adler32);
2044 dst = zstream_detach_buffer(z);
2054 dst = zstream_detach_buffer(z);
2056 zstream_passthrough_input(z);
2084 zstream_passthrough_input(z);
2166 #define GZ_MAGIC1 0x1f
2167 #define GZ_MAGIC2 0x8b
2168 #define GZ_METHOD_DEFLATE 8
2169 #define GZ_FLAG_MULTIPART 0x2
2170 #define GZ_FLAG_EXTRA 0x4
2171 #define GZ_FLAG_ORIG_NAME 0x8
2172 #define GZ_FLAG_COMMENT 0x10
2173 #define GZ_FLAG_ENCRYPT 0x20
2174 #define GZ_FLAG_UNKNOWN_MASK 0xc0
2176 #define GZ_EXTRAFLAG_FAST 0x4
2177 #define GZ_EXTRAFLAG_SLOW 0x2
2180 #define OS_MSDOS 0x00
2181 #define OS_AMIGA 0x01
2183 #define OS_UNIX 0x03
2184 #define OS_ATARI 0x05
2186 #define OS_MACOS 0x07
2187 #define OS_TOPS20 0x0a
2188 #define OS_WIN32 0x0b
2190 #define OS_VMCMS 0x04
2191 #define OS_ZSYSTEM 0x08
2193 #define OS_QDOS 0x0c
2194 #define OS_RISCOS 0x0d
2195 #define OS_UNKNOWN 0xff
2198 #define OS_CODE OS_UNIX
2201 static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path, id_input;
2202 static VALUE cGzError, cNoFooter, cCRCError, cLengthError;
2227 #define GZFILE_CBUF_CAPA 10
2229 #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
2230 #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
2231 #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
2232 #define GZFILE_FLAG_MTIME_IS_SET (ZSTREAM_FLAG_UNUSED << 3)
2234 #define GZFILE_IS_FINISHED(gz) \
2235 (ZSTREAM_IS_FINISHED(&(gz)->z) && ZSTREAM_BUF_FILLED(&(gz)->z) == 0)
2237 #define GZFILE_READ_SIZE 2048
2251 gzfile_mark(
void *p)
2258 zstream_mark(&gz->
z);
2264 gzfile_free(
void *p)
2270 if (z->
func == &deflate_funcs) {
2271 finalizer_warn(
"Zlib::GzipWriter object must be closed explicitly.");
2273 zstream_finalize(z);
2279 gzfile_memsize(
const void *p)
2281 return sizeof(
struct gzfile);
2286 { gzfile_mark, gzfile_free, gzfile_memsize, },
2291 gzfile_init(
struct gzfile *gz,
const struct zstream_funcs *funcs,
void (*endfunc)(
struct gzfile *))
2293 zstream_init(&gz->
z, funcs);
2301 gz->
crc = crc32(0, Z_NULL, 0);
2314 gzfile_new(
VALUE klass,
const struct zstream_funcs *funcs,
void (*endfunc)(
struct gzfile *))
2320 gzfile_init(gz, funcs, endfunc);
2324 #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2325 #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2328 gzfile_reset(
struct gzfile *gz)
2330 zstream_reset(&gz->
z);
2332 gz->
crc = crc32(0, Z_NULL, 0);
2343 gzfile_close(
struct gzfile *gz,
int closeflag)
2357 gzfile_write_raw(
struct gzfile *gz)
2362 str = zstream_detach_buffer(&gz->
z);
2404 ra.
as.
in.buf = outbuf;
2407 gzfile_read_raw_rescue, (
VALUE)&ra,
2418 rb_raise(cGzError,
"unexpected end of string");
2421 str = gzfile_read_raw(gz, outbuf);
2429 gzfile_read_raw_until_zero(
struct gzfile *gz,
long offset)
2438 str = gzfile_read_raw(gz,
Qnil);
2440 rb_raise(cGzError,
"unexpected end of file");
2449 gzfile_get16(
const unsigned char *
src)
2452 n = *(
src++) & 0xff;
2453 n |= (*(
src++) & 0xff) << 8;
2457 static unsigned long
2458 gzfile_get32(
const unsigned char *
src)
2461 n = *(
src++) & 0xff;
2462 n |= (*(
src++) & 0xff) << 8;
2463 n |= (*(
src++) & 0xff) << 16;
2464 n |= (*(
src++) & 0xffU) << 24;
2469 gzfile_set32(
unsigned long n,
unsigned char *dst)
2471 *(dst++) =
n & 0xff;
2472 *(dst++) = (
n >> 8) & 0xff;
2473 *(dst++) = (
n >> 16) & 0xff;
2474 *dst = (
n >> 24) & 0xff;
2508 gzfile_make_header(
struct gzfile *gz)
2511 unsigned char flags = 0, extraflags = 0;
2523 if (gz->
level == Z_BEST_SPEED) {
2526 else if (gz->
level == Z_BEST_COMPRESSION) {
2534 gzfile_set32((
unsigned long)gz->
mtime, &
buf[4]);
2535 buf[8] = extraflags;
2537 zstream_append_buffer(&gz->
z,
buf, (
long)
sizeof(
buf));
2541 zstream_append_buffer(&gz->
z, (Bytef*)
"\0", 1);
2545 zstream_append_buffer(&gz->
z, (Bytef*)
"\0", 1);
2552 gzfile_make_footer(
struct gzfile *gz)
2556 gzfile_set32(gz->
crc,
buf);
2557 gzfile_set32(gz->
z.
stream.total_in, &
buf[4]);
2558 zstream_append_buffer(&gz->
z,
buf, (
long)
sizeof(
buf));
2563 gzfile_read_header(
struct gzfile *gz,
VALUE outbuf)
2565 const unsigned char *head;
2570 if (!gzfile_read_raw_ensure(gz, 10, outbuf)) {
2571 gzfile_raise(gz, cGzError,
"not in gzip format");
2577 gzfile_raise(gz, cGzError,
"not in gzip format");
2580 rb_raise(cGzError,
"unsupported compression method %d", head[2]);
2585 rb_raise(cGzError,
"multi-part gzip file is not supported");
2588 rb_raise(cGzError,
"encrypted gzip file is not supported");
2591 rb_raise(cGzError,
"unknown flags 0x%02x", flags);
2595 gz->
level = Z_BEST_SPEED;
2598 gz->
level = Z_BEST_COMPRESSION;
2601 gz->
level = Z_DEFAULT_COMPRESSION;
2604 gz->
mtime = gzfile_get32(&head[4]);
2606 zstream_discard_input(&gz->
z, 10);
2609 if (!gzfile_read_raw_ensure(gz, 2, outbuf)) {
2610 rb_raise(cGzError,
"unexpected end of file");
2613 if (!gzfile_read_raw_ensure(gz, 2 +
len, outbuf)) {
2614 rb_raise(cGzError,
"unexpected end of file");
2616 zstream_discard_input(&gz->
z, 2 +
len);
2619 if (!gzfile_read_raw_ensure(gz, 1, outbuf)) {
2620 rb_raise(cGzError,
"unexpected end of file");
2622 p = gzfile_read_raw_until_zero(gz, 0);
2625 zstream_discard_input(&gz->
z,
len + 1);
2628 if (!gzfile_read_raw_ensure(gz, 1, outbuf)) {
2629 rb_raise(cGzError,
"unexpected end of file");
2631 p = gzfile_read_raw_until_zero(gz, 0);
2634 zstream_discard_input(&gz->
z,
len + 1);
2638 zstream_run(&gz->
z, 0, 0, Z_SYNC_FLUSH);
2643 gzfile_check_footer(
struct gzfile *gz,
VALUE outbuf)
2645 unsigned long crc, length;
2650 if (!gzfile_read_raw_ensure(gz, 8, outbuf)) {
2651 gzfile_raise(gz, cNoFooter,
"footer is not found");
2658 zstream_discard_input(&gz->
z, 8);
2660 if (gz->
crc != crc) {
2661 rb_raise(cCRCError,
"invalid compressed data -- crc error");
2664 rb_raise(cLengthError,
"invalid compressed data -- length error");
2672 gzfile_make_header(gz);
2678 ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2680 gzfile_write_raw(gz);
2689 str = gzfile_read_raw(gz, outbuf);
2692 rb_raise(cGzError,
"unexpected end of file");
2736 gzfile_fill(
struct gzfile *gz,
long len)
2743 gzfile_read_more(gz,
Qnil);
2747 gzfile_check_footer(gz,
Qnil);
2755 gzfile_read(
struct gzfile *gz,
long len)
2759 len = gzfile_fill(gz,
len);
2762 dst = zstream_shift_buffer(&gz->
z,
len);
2763 if (!
NIL_P(dst)) gzfile_calc_crc(gz, dst);
2784 gzfile_read_more(gz, outbuf);
2788 gzfile_check_footer(gz, outbuf);
2795 dst = zstream_shift_buffer(&gz->
z,
len);
2796 gzfile_calc_crc(gz, dst);
2798 if (!
NIL_P(outbuf)) {
2809 gzfile_read_all(
struct gzfile *gz)
2814 gzfile_read_more(gz,
Qnil);
2818 gzfile_check_footer(gz,
Qnil);
2823 dst = zstream_detach_buffer(&gz->
z);
2824 if (
NIL_P(dst))
return dst;
2825 gzfile_calc_crc(gz, dst);
2826 return gzfile_newstr(gz, dst);
2830 gzfile_getc(
struct gzfile *gz)
2837 gzfile_read_more(gz,
Qnil);
2841 gzfile_check_footer(gz,
Qnil);
2847 const unsigned char *ss, *sp, *se;
2848 unsigned char *ds, *
dp, *de;
2857 dst = zstream_shift_buffer(&gz->
z, sp - ss);
2858 gzfile_calc_crc(gz, dst);
2865 dst = gzfile_read(gz,
len);
2866 if (
NIL_P(dst))
return dst;
2867 return gzfile_newstr(gz, dst);
2872 gzfile_ungets(
struct gzfile *gz,
const Bytef *b,
long len)
2874 zstream_buffer_ungets(&gz->
z, b,
len);
2879 gzfile_ungetbyte(
struct gzfile *gz,
int c)
2881 zstream_buffer_ungetbyte(&gz->
z, c);
2891 gzfile_make_header(gz);
2894 zstream_run(&gz->
z, (Bytef*)
"", 0, Z_FINISH);
2895 gzfile_make_footer(gz);
2896 gzfile_write_raw(gz);
2902 gzfile_writer_end(
struct gzfile *gz)
2917 gzfile_check_footer(gz,
Qnil);
2924 gzfile_reader_end(
struct gzfile *gz)
2933 gzfile_reader_rewind(
struct gzfile *gz)
2947 gzfile_reader_get_unused(
struct gzfile *gz)
2954 gzfile_check_footer(gz,
Qnil);
2969 rb_raise(cGzError,
"closed gzip stream");
3035 gzfile_close(gz, 1);
3045 if (close_io_on_error) {
3115 return get_gzfile(
obj)->
io;
3244 rb_raise(cGzError,
"header is already written");
3267 rb_raise(cGzError,
"header is already written");
3291 rb_raise(cGzError,
"header is already written");
3319 gzfile_close(gz, 1);
3337 gzfile_close(gz, 0);
3422 uLong total_out = gz->
z.
stream.total_out;
3425 if (total_out >= (uLong)buf_filled) {
3428 return LONG2FIX(-(buf_filled - (
long)total_out));
3553 rb_gzfile_ecopts(gz, opt);
3579 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3580 if (flush != Z_NO_FLUSH) {
3581 zstream_run(&gz->
z, (Bytef*)
"", 0, flush);
3584 gzfile_write_raw(gz);
3600 while (
argc-- > 0) {
3623 gzfile_write(gz, (Bytef*)&c, 1);
3633 #define rb_gzwriter_addstr rb_io_addstr
3638 #define rb_gzwriter_printf rb_io_printf
3643 #define rb_gzwriter_print rb_io_print
3648 #define rb_gzwriter_puts rb_io_puts
3754 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
3760 gzfile_read_header(gz,
Qnil);
3761 rb_gzfile_ecopts(gz, opt);
3781 gzfile_reader_rewind(gz);
3796 return gzfile_reader_get_unused(gz);
3813 return gzfile_read_all(gz);
3820 return gzfile_read(gz,
len);
3850 return gzfile_readpartial(gz,
len, outbuf);
3863 return gzfile_getc(gz);
3875 dst = rb_gzreader_getc(
obj);
3893 dst = gzfile_read(gz, 1);
3909 dst = rb_gzreader_getbyte(
obj);
3928 while (!
NIL_P(c = rb_gzreader_getc(
obj))) {
3946 while (!
NIL_P(c = rb_gzreader_getbyte(
obj))) {
3960 rb_warn(
"Zlib::GzipReader#bytes is deprecated; use #each_byte instead");
3963 return rb_gzreader_each_byte(
obj);
3977 return rb_gzreader_ungetbyte(
obj, s);
3978 gz = get_gzfile(
obj);
3997 gzfile_ungetbyte(gz,
NUM2CHR(ch));
4002 gzreader_skip_linebreaks(
struct gzfile *gz)
4010 gzfile_read_more(gz,
Qnil);
4015 while (
n++, *(p++) ==
'\n') {
4017 str = zstream_detach_buffer(&gz->
z);
4018 gzfile_calc_crc(gz,
str);
4021 gzfile_read_more(gz,
Qnil);
4028 str = zstream_shift_buffer(&gz->
z,
n - 1);
4029 gzfile_calc_crc(gz,
str);
4033 rscheck(
const char *rsptr,
long rslen,
VALUE rs)
4040 gzreader_charboundary(
struct gzfile *gz,
long n)
4068 long rslen,
n, limit = -1;
4083 else if (!
NIL_P(rs)) {
4101 dst = gzfile_read_all(gz);
4104 else if ((
n = gzfile_fill(gz, limit)) <= 0) {
4109 n = gzreader_charboundary(gz,
n);
4114 dst = zstream_shift_buffer(&gz->
z,
n);
4115 if (
NIL_P(dst))
return dst;
4116 gzfile_calc_crc(gz, dst);
4117 dst = gzfile_newstr(gz, dst);
4135 gzreader_skip_linebreaks(gz);
4141 return gzfile_read(gz, rslen);
4143 gzfile_read_more(gz,
Qnil);
4152 gzfile_read_more(gz,
Qnil);
4155 if (!rspara) rscheck(rsptr, rslen, rs);
4157 if (limit > 0 && filled >= limit) {
4160 res =
memchr(p, rsptr[0], (filled -
n + 1));
4163 if (limit > 0 && filled >= limit)
break;
4167 n += (
long)(res - p);
4169 if (rslen == 1 ||
memcmp(p, rsptr, rslen) == 0)
break;
4174 n = gzreader_charboundary(gz,
n);
4178 dst = gzfile_read(gz,
n);
4179 if (
NIL_P(dst))
return dst;
4181 gzreader_skip_linebreaks(gz);
4185 return gzfile_newstr(gz, dst);
4246 rb_warn(
"Zlib::GzipReader#lines is deprecated; use #each_line instead");
4274 rb_gzreader_external_encoding(
VALUE self)
4294 zlib_gzip_end(
struct gzfile *gz)
4297 zstream_run(&gz->
z, (Bytef*)
"", 0, Z_FINISH);
4298 gzfile_make_footer(gz);
4299 zstream_end(&gz->
z);
4302 #define OPTHASH_GIVEN_P(opts) \
4303 (argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
4304 static ID id_level, id_strategy;
4333 struct gzfile *gz = &gz0;
4340 keyword_ids[0] = id_level;
4341 keyword_ids[1] = id_strategy;
4343 if (kwargs[0] !=
Qundef) {
4346 if (kwargs[1] !=
Qundef) {
4347 strategy = kwargs[1];
4352 gzfile_init(gz, &deflate_funcs, zlib_gzip_end);
4361 args[0] = (
VALUE)gz;
4374 gzfile_make_header(gz);
4379 zstream_run(&gz->
z,
ptr,
len, Z_NO_FLUSH);
4381 gzfile_close(gz, 0);
4382 return zstream_detach_buffer(&gz->
z);
4386 zlib_gunzip_end(
struct gzfile *gz)
4389 zstream_end(&gz->
z);
4416 struct gzfile *gz = &gz0;
4421 gzfile_init(gz, &inflate_funcs, zlib_gunzip_end);
4422 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
4438 gzfile_read_header(gz,
Qnil);
4439 dst = zstream_detach_buffer(&gz->
z);
4440 gzfile_calc_crc(gz, dst);
4442 rb_raise(cGzError,
"unexpected end of file");
4445 rb_raise(cNoFooter,
"footer is not found");
4447 gzfile_check_footer(gz,
Qnil);
4457 VALUE mZlib, cZStream, cDeflate, cInflate;
4459 VALUE cGzipFile, cGzipWriter, cGzipReader;
4464 id_dictionaries =
rb_intern(
"@dictionaries");
4505 rb_define_method(cZStream,
"flush_next_out", rb_zstream_flush_next_out, 0);
4542 rb_define_method(cDeflate,
"set_dictionary", rb_deflate_set_dictionary, 1);
4549 rb_define_method(cInflate,
"add_dictionary", rb_inflate_add_dictionary, 1);
4554 rb_define_method(cInflate,
"set_dictionary", rb_inflate_set_dictionary, 1);
4568 INT2FIX(Z_DEFAULT_COMPRESSION));
4639 id_readpartial =
rb_intern(
"readpartial");
4720 rb_define_method(cGzipReader,
"external_encoding", rb_gzreader_external_encoding, 0);