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)
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)
59static ID id_dictionaries;
63static NORETURN(
void raise_zlib_error(
int,
const char*));
65static VALUE do_checksum(
int,
VALUE*, uLong (*)(uLong,
const Bytef*, uInt));
69static voidpf zlib_mem_alloc(voidpf, uInt, uInt);
70static void zlib_mem_free(voidpf, voidpf);
71static void finalizer_warn(
const char*);
76static void zstream_init(
struct zstream*,
const struct zstream_funcs*);
77static void zstream_expand_buffer(
struct zstream*);
78static void zstream_expand_buffer_into(
struct zstream*,
unsigned long);
79static int zstream_expand_buffer_non_stream(
struct zstream *
z);
80static void zstream_append_buffer(
struct zstream*,
const Bytef*,
long);
83static void zstream_buffer_ungets(
struct zstream*,
const Bytef*,
unsigned long);
84static void zstream_buffer_ungetbyte(
struct zstream*,
int);
85static void zstream_append_input(
struct zstream*,
const Bytef*,
long);
86static void zstream_discard_input(
struct zstream*,
long);
87static void zstream_reset_input(
struct zstream*);
88static void zstream_passthrough_input(
struct zstream*);
90static void zstream_reset(
struct zstream*);
93static void zstream_run(
struct zstream*, Bytef*,
long,
int);
95static void zstream_mark(
void*);
96static void zstream_free(
void*);
97static VALUE zstream_new(
VALUE,
const struct zstream_funcs*);
99static void zstream_finalize(
struct zstream*);
141static void gzfile_mark(
void*);
142static void gzfile_free(
void*);
143static VALUE gzfile_new(
VALUE,
const struct zstream_funcs*,
void (*)
_((
struct gzfile*)));
144static void gzfile_reset(
struct gzfile*);
145static void gzfile_close(
struct gzfile*,
int);
146static void gzfile_write_raw(
struct gzfile*);
150static int gzfile_read_raw_ensure(
struct gzfile*,
long,
VALUE outbuf);
151static char *gzfile_read_raw_until_zero(
struct gzfile*,
long);
152static unsigned int gzfile_get16(
const unsigned char*);
153static unsigned long gzfile_get32(
const unsigned char*);
154static void gzfile_set32(
unsigned long n,
unsigned char*);
155static void gzfile_make_header(
struct gzfile*);
156static void gzfile_make_footer(
struct gzfile*);
157static void gzfile_read_header(
struct gzfile*,
VALUE outbuf);
158static void gzfile_check_footer(
struct gzfile*,
VALUE outbuf);
159static void gzfile_write(
struct gzfile*, Bytef*,
long);
160static long gzfile_read_more(
struct gzfile*,
VALUE outbuf);
164static void gzfile_ungets(
struct gzfile*,
const Bytef*,
long);
165static void gzfile_ungetbyte(
struct gzfile*,
int);
167static void gzfile_writer_end(
struct gzfile*);
169static void gzfile_reader_end(
struct gzfile*);
170static void gzfile_reader_rewind(
struct gzfile*);
171static VALUE gzfile_reader_get_unused(
struct gzfile*);
219static void gzreader_skip_linebreaks(
struct gzfile*);
304static VALUE cZError, cStreamEnd, cNeedDict;
305static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError, cInProgressError;
308raise_zlib_error(
int err,
const char *msg)
332 case Z_VERSION_ERROR:
353finalizer_warn(
const char *msg)
372#if SIZEOF_LONG > SIZEOF_INT
374checksum_long(uLong (*func)(uLong,
const Bytef*, uInt), uLong sum,
const Bytef *
ptr,
long len)
383 if (
len > 0) sum = func(sum,
ptr, (uInt)
len);
387#define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
391do_checksum(
int argc,
VALUE *
argv, uLong (*func)(uLong,
const Bytef*, uInt))
405 sum = func(0, Z_NULL, 0);
409 sum = func(sum, Z_NULL, 0);
439 return do_checksum(
argc,
argv, adler32);
442#ifdef HAVE_ADLER32_COMBINE
460#define rb_zlib_adler32_combine rb_f_notimplement
477 return do_checksum(
argc,
argv, crc32);
480#ifdef HAVE_CRC32_COMBINE
498#define rb_zlib_crc32_combine rb_f_notimplement
509#if !defined(HAVE_TYPE_Z_CRC_T)
511 typedef unsigned long z_crc_t;
513 const z_crc_t *crctbl;
517 crctbl = get_crc_table();
520 for (
i = 0;
i < 256;
i++) {
543#define ZSTREAM_FLAG_READY (1 << 0)
544#define ZSTREAM_FLAG_IN_STREAM (1 << 1)
545#define ZSTREAM_FLAG_FINISHED (1 << 2)
546#define ZSTREAM_FLAG_CLOSING (1 << 3)
547#define ZSTREAM_FLAG_GZFILE (1 << 4)
549#define ZSTREAM_IN_PROGRESS (1 << 5)
550#define ZSTREAM_FLAG_UNUSED (1 << 6)
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)
557#define ZSTREAM_BUF_FILLED(z) (NIL_P((z)->buf) ? 0 : RSTRING_LEN((z)->buf))
559#define ZSTREAM_EXPAND_BUFFER_OK 0
563#define ZSTREAM_INITIAL_BUFSIZE 1024
565#define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
566#define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
568static const struct zstream_funcs deflate_funcs = {
569 deflateReset, deflateEnd, deflate,
572static const struct zstream_funcs inflate_funcs = {
573 inflateReset, inflateEnd, inflate,
587zlib_mem_alloc(voidpf opaque, uInt items, uInt
size)
599zlib_mem_free(voidpf opaque, voidpf address)
605zstream_init(
struct zstream *z,
const struct zstream_funcs *
func)
611 z->
stream.zalloc = zlib_mem_alloc;
612 z->
stream.zfree = zlib_mem_free;
613 z->
stream.opaque = Z_NULL;
615 z->
stream.next_in = Z_NULL;
617 z->
stream.next_out = Z_NULL;
622#define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
623#define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
626zstream_expand_buffer(
struct zstream *z)
653 zstream_expand_buffer_into(z,
658 zstream_expand_buffer_non_stream(z);
663zstream_expand_buffer_into(
struct zstream *z,
unsigned long size)
681zstream_expand_buffer_protect(
void *
ptr)
688 return (
void *)(
VALUE)state;
692zstream_expand_buffer_non_stream(
struct zstream *z)
715zstream_append_buffer(
struct zstream *z,
const Bytef *
src,
long len)
742#define zstream_append_buffer2(z,v) \
743 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
746zstream_detach_buffer(
struct zstream *z)
778zstream_shift_buffer(
struct zstream *z,
long len)
785 return zstream_detach_buffer(z);
798 z->
stream.avail_out = (uInt)buflen;
804zstream_buffer_ungets(
struct zstream *z,
const Bytef *b,
unsigned long len)
810 zstream_expand_buffer_into(z,
len);
817 if (z->
stream.avail_out > 0) {
825zstream_buffer_ungetbyte(
struct zstream *z,
int c)
828 zstream_buffer_ungets(z, &
cc, 1);
832zstream_append_input(
struct zstream *z,
const Bytef *
src,
long len)
834 if (
len <= 0)
return;
846#define zstream_append_input2(z,v)\
848 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
851zstream_discard_input(
struct zstream *z,
long len)
861 newlen = oldlen -
len;
889zstream_reset_input(
struct zstream *z)
900zstream_passthrough_input(
struct zstream *z)
909zstream_detach_input(
struct zstream *z)
925zstream_reset(
struct zstream *z)
937 zstream_reset_input(z);
946 rb_warning(
"attempt to close uninitialized zstream; ignored.");
950 rb_warning(
"attempt to close unfinished zstream; reset forced.");
954 zstream_reset_input(z);
964zstream_ensure_end(
VALUE v)
966 return zstream_end((
struct zstream *)
v);
970zstream_run_func(
void *
ptr)
983 if (
err == Z_STREAM_END) {
984 z->
flags &= ~ZSTREAM_FLAG_IN_STREAM;
989 if (
err != Z_OK &&
err != Z_BUF_ERROR)
992 if (z->
stream.avail_out > 0) {
997 if (z->
stream.avail_in == 0 && z->
func == &inflate_funcs) {
1010 state = zstream_expand_buffer_non_stream(z);
1028zstream_unblock_func(
void *
ptr)
1036zstream_run_try(
VALUE value_arg)
1042 int flush = args->
flush;
1048 z->
stream.next_in = (Bytef*)
"";
1052 zstream_append_input(z,
src,
len);
1056 old_input = zstream_detach_input(z);
1062 if (z->
stream.avail_out == 0) {
1063 zstream_expand_buffer(z);
1067#ifndef RB_NOGVL_UBF_ASYNC_SAFE
1069 zstream_unblock_func, (
void *)args);
1072 zstream_unblock_func, (
void *)args,
1077 if (
err == Z_OK && args->interrupt) {
1078 args->interrupt = 0;
1082 if (flush != Z_FINISH &&
err == Z_BUF_ERROR
1083 && z->
stream.avail_out > 0) {
1087 zstream_reset_input(z);
1089 if (
err != Z_OK &&
err != Z_STREAM_END) {
1090 if (z->
stream.avail_in > 0) {
1091 zstream_append_input(z, z->
stream.next_in, z->
stream.avail_in);
1093 if (
err == Z_NEED_DICT) {
1099 rb_inflate_set_dictionary(
self, dict);
1107 if (z->
stream.avail_in > 0) {
1108 zstream_append_input(z, z->
stream.next_in, z->
stream.avail_in);
1110 if (!
NIL_P(old_input)) {
1115 if (args->jump_state)
1121zstream_run_ensure(
VALUE value_arg)
1126 args->
z->
flags &= ~ZSTREAM_IN_PROGRESS;
1132zstream_run_synchronized(
VALUE value_arg)
1138 rb_raise(cInProgressError,
"zlib stream is in progress");
1142 rb_ensure(zstream_run_try, value_arg, zstream_run_ensure, value_arg);
1172 zstream_discard_input(
z,
1174 zstream_append_input(
z,
src,
len);
1177 zstream_reset_input(
z);
1178 if (
err != Z_DATA_ERROR) {
1193 if (
err != Z_DATA_ERROR) {
1201zstream_mark(
void *p)
1210zstream_finalize(
struct zstream *z)
1213 if (
err == Z_STREAM_ERROR)
1214 finalizer_warn(
"the stream state was inconsistent.");
1215 if (
err == Z_DATA_ERROR)
1216 finalizer_warn(
"the stream was freed prematurely.");
1220zstream_free(
void *p)
1225 zstream_finalize(z);
1231zstream_memsize(
const void *p)
1234 return sizeof(
struct zstream);
1239 { zstream_mark, zstream_free, zstream_memsize, },
1244zstream_new(
VALUE klass,
const struct zstream_funcs *funcs)
1250 zstream_init(z, funcs);
1255#define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
1256#define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
1265 rb_raise(cZError,
"stream is not ready");
1341 zstream_end(get_zstream(
obj));
1352 zstream_reset(get_zstream(
obj));
1370 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1372 return zstream_detach_buffer(z);
1387 dst = zstream_detach_input(z);
1401rb_zstream_flush_next_out(
VALUE obj)
1407 return zstream_detach_buffer(z);
1516#define FIXNUMARG(val, ifnil) \
1517 (NIL_P((val)) ? (ifnil) \
1520#define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION)
1521#define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS)
1522#define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL)
1523#define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY)
1524#define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH)
1610 VALUE level, wbits, memlevel, strategy;
1639 z2 = get_zstream(orig);
1641 if (z1 == z2)
return self;
1644 raise_zlib_error(
err, 0);
1654deflate_run(
VALUE args)
1660 return zstream_detach_buffer(z);
1704 args[0] = (
VALUE)&z;
1715 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1759 return zstream_detach_buffer(z);
1774 do_deflate(get_zstream(
obj),
src, Z_NO_FLUSH);
1801 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1802 if (flush != Z_NO_FLUSH) {
1803 zstream_run(z, (Bytef*)
"", 0, flush);
1806 return zstream_detach_buffer(z);
1825 int level, strategy;
1834 err = deflateParams(&z->
stream, level, strategy);
1835 filled =
n - z->
stream.avail_out;
1836 while (
err == Z_BUF_ERROR) {
1837 rb_warning(
"deflateParams() returned Z_BUF_ERROR");
1838 zstream_expand_buffer(z);
1841 err = deflateParams(&z->
stream, level, strategy);
1842 filled =
n - z->
stream.avail_out;
1967inflate_run(
VALUE args)
1973 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
1974 return zstream_detach_buffer(z);
2015 args[0] = (
VALUE)&z;
2026 zstream_run(z, (Bytef*)
"", 0, Z_FINISH);
2048 VALUE checksum = do_checksum(1, &dictionary, adler32);
2100 dst = zstream_detach_buffer(z);
2110 dst = zstream_detach_buffer(z);
2112 zstream_passthrough_input(z);
2140 zstream_passthrough_input(z);
2222#define GZ_MAGIC1 0x1f
2223#define GZ_MAGIC2 0x8b
2224#define GZ_METHOD_DEFLATE 8
2225#define GZ_FLAG_MULTIPART 0x2
2226#define GZ_FLAG_EXTRA 0x4
2227#define GZ_FLAG_ORIG_NAME 0x8
2228#define GZ_FLAG_COMMENT 0x10
2229#define GZ_FLAG_ENCRYPT 0x20
2230#define GZ_FLAG_UNKNOWN_MASK 0xc0
2232#define GZ_EXTRAFLAG_FAST 0x4
2233#define GZ_EXTRAFLAG_SLOW 0x2
2236#define OS_MSDOS 0x00
2237#define OS_AMIGA 0x01
2240#define OS_ATARI 0x05
2242#define OS_MACOS 0x07
2243#define OS_TOPS20 0x0a
2244#define OS_WIN32 0x0b
2246#define OS_VMCMS 0x04
2247#define OS_ZSYSTEM 0x08
2250#define OS_RISCOS 0x0d
2251#define OS_UNKNOWN 0xff
2254#define OS_CODE OS_UNIX
2257static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path, id_input;
2258static VALUE cGzError, cNoFooter, cCRCError, cLengthError;
2283#define GZFILE_CBUF_CAPA 10
2285#define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
2286#define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
2287#define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
2288#define GZFILE_FLAG_MTIME_IS_SET (ZSTREAM_FLAG_UNUSED << 3)
2290#define GZFILE_IS_FINISHED(gz) \
2291 (ZSTREAM_IS_FINISHED(&(gz)->z) && ZSTREAM_BUF_FILLED(&(gz)->z) == 0)
2293#define GZFILE_READ_SIZE 2048
2314 zstream_mark(&gz->
z);
2326 if (z->
func == &deflate_funcs) {
2327 finalizer_warn(
"Zlib::GzipWriter object must be closed explicitly.");
2329 zstream_finalize(z);
2335gzfile_memsize(
const void *p)
2337 return sizeof(
struct gzfile);
2342 { gzfile_mark, gzfile_free, gzfile_memsize, },
2347gzfile_init(
struct gzfile *gz,
const struct zstream_funcs *funcs,
void (*endfunc)(
struct gzfile *))
2349 zstream_init(&gz->
z, funcs);
2357 gz->
crc = crc32(0, Z_NULL, 0);
2370gzfile_new(
VALUE klass,
const struct zstream_funcs *funcs,
void (*endfunc)(
struct gzfile *))
2376 gzfile_init(gz, funcs, endfunc);
2380#define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end)
2381#define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end)
2384gzfile_reset(
struct gzfile *gz)
2386 zstream_reset(&gz->
z);
2388 gz->
crc = crc32(0, Z_NULL, 0);
2399gzfile_close(
struct gzfile *gz,
int closeflag)
2413gzfile_write_raw(
struct gzfile *gz)
2418 str = zstream_detach_buffer(&gz->
z);
2463 gzfile_read_raw_rescue, (
VALUE)&ra,
2474 rb_raise(cGzError,
"unexpected end of string");
2477 str = gzfile_read_raw(gz, outbuf);
2485gzfile_read_raw_until_zero(
struct gzfile *gz,
long offset)
2494 str = gzfile_read_raw(gz,
Qnil);
2496 rb_raise(cGzError,
"unexpected end of file");
2505gzfile_get16(
const unsigned char *
src)
2508 n = *(
src++) & 0xff;
2509 n |= (*(
src++) & 0xff) << 8;
2514gzfile_get32(
const unsigned char *
src)
2517 n = *(
src++) & 0xff;
2518 n |= (*(
src++) & 0xff) << 8;
2519 n |= (*(
src++) & 0xff) << 16;
2520 n |= (*(
src++) & 0xffU) << 24;
2525gzfile_set32(
unsigned long n,
unsigned char *dst)
2527 *(dst++) =
n & 0xff;
2528 *(dst++) = (
n >> 8) & 0xff;
2529 *(dst++) = (
n >> 16) & 0xff;
2530 *dst = (
n >> 24) & 0xff;
2564gzfile_make_header(
struct gzfile *gz)
2567 unsigned char flags = 0, extraflags = 0;
2579 if (gz->
level == Z_BEST_SPEED) {
2582 else if (gz->
level == Z_BEST_COMPRESSION) {
2590 gzfile_set32((
unsigned long)gz->
mtime, &
buf[4]);
2591 buf[8] = extraflags;
2593 zstream_append_buffer(&gz->
z,
buf, (
long)
sizeof(
buf));
2597 zstream_append_buffer(&gz->
z, (Bytef*)
"\0", 1);
2601 zstream_append_buffer(&gz->
z, (Bytef*)
"\0", 1);
2608gzfile_make_footer(
struct gzfile *gz)
2612 gzfile_set32(gz->
crc,
buf);
2613 gzfile_set32(gz->
z.
stream.total_in, &
buf[4]);
2614 zstream_append_buffer(&gz->
z,
buf, (
long)
sizeof(
buf));
2619gzfile_read_header(
struct gzfile *gz,
VALUE outbuf)
2621 const unsigned char *head;
2626 if (!gzfile_read_raw_ensure(gz, 10, outbuf)) {
2627 gzfile_raise(gz, cGzError,
"not in gzip format");
2633 gzfile_raise(gz, cGzError,
"not in gzip format");
2636 rb_raise(cGzError,
"unsupported compression method %d", head[2]);
2641 rb_raise(cGzError,
"multi-part gzip file is not supported");
2644 rb_raise(cGzError,
"encrypted gzip file is not supported");
2647 rb_raise(cGzError,
"unknown flags 0x%02x", flags);
2651 gz->
level = Z_BEST_SPEED;
2654 gz->
level = Z_BEST_COMPRESSION;
2657 gz->
level = Z_DEFAULT_COMPRESSION;
2660 gz->
mtime = gzfile_get32(&head[4]);
2662 zstream_discard_input(&gz->
z, 10);
2665 if (!gzfile_read_raw_ensure(gz, 2, outbuf)) {
2666 rb_raise(cGzError,
"unexpected end of file");
2669 if (!gzfile_read_raw_ensure(gz, 2 +
len, outbuf)) {
2670 rb_raise(cGzError,
"unexpected end of file");
2672 zstream_discard_input(&gz->
z, 2 +
len);
2675 if (!gzfile_read_raw_ensure(gz, 1, outbuf)) {
2676 rb_raise(cGzError,
"unexpected end of file");
2678 p = gzfile_read_raw_until_zero(gz, 0);
2681 zstream_discard_input(&gz->
z,
len + 1);
2684 if (!gzfile_read_raw_ensure(gz, 1, outbuf)) {
2685 rb_raise(cGzError,
"unexpected end of file");
2687 p = gzfile_read_raw_until_zero(gz, 0);
2690 zstream_discard_input(&gz->
z,
len + 1);
2694 zstream_run(&gz->
z, 0, 0, Z_SYNC_FLUSH);
2699gzfile_check_footer(
struct gzfile *gz,
VALUE outbuf)
2701 unsigned long crc, length;
2706 if (!gzfile_read_raw_ensure(gz, 8, outbuf)) {
2707 gzfile_raise(gz, cNoFooter,
"footer is not found");
2714 zstream_discard_input(&gz->
z, 8);
2716 if (gz->
crc != crc) {
2717 rb_raise(cCRCError,
"invalid compressed data -- crc error");
2720 rb_raise(cLengthError,
"invalid compressed data -- length error");
2728 gzfile_make_header(gz);
2734 ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2736 gzfile_write_raw(gz);
2745 str = gzfile_read_raw(gz, outbuf);
2748 rb_raise(cGzError,
"unexpected end of file");
2799 gzfile_read_more(gz,
Qnil);
2803 gzfile_check_footer(gz,
Qnil);
2815 len = gzfile_fill(gz,
len);
2818 dst = zstream_shift_buffer(&gz->
z,
len);
2819 if (!
NIL_P(dst)) gzfile_calc_crc(gz, dst);
2840 gzfile_read_more(gz, outbuf);
2844 gzfile_check_footer(gz, outbuf);
2851 dst = zstream_shift_buffer(&gz->
z,
len);
2852 gzfile_calc_crc(gz, dst);
2854 if (!
NIL_P(outbuf)) {
2865gzfile_read_all(
struct gzfile *gz)
2870 gzfile_read_more(gz,
Qnil);
2874 gzfile_check_footer(gz,
Qnil);
2879 dst = zstream_detach_buffer(&gz->
z);
2880 if (
NIL_P(dst))
return dst;
2881 gzfile_calc_crc(gz, dst);
2882 return gzfile_newstr(gz, dst);
2886gzfile_getc(
struct gzfile *gz)
2893 gzfile_read_more(gz,
Qnil);
2897 gzfile_check_footer(gz,
Qnil);
2903 const unsigned char *ss, *sp, *se;
2904 unsigned char *ds, *
dp, *de;
2913 dst = zstream_shift_buffer(&gz->
z, sp - ss);
2914 gzfile_calc_crc(gz, dst);
2921 dst = gzfile_read(gz,
len);
2922 if (
NIL_P(dst))
return dst;
2923 return gzfile_newstr(gz, dst);
2928gzfile_ungets(
struct gzfile *gz,
const Bytef *b,
long len)
2930 zstream_buffer_ungets(&gz->
z, b,
len);
2935gzfile_ungetbyte(
struct gzfile *gz,
int c)
2937 zstream_buffer_ungetbyte(&gz->
z, c);
2947 gzfile_make_header(gz);
2950 zstream_run(&gz->
z, (Bytef*)
"", 0, Z_FINISH);
2951 gzfile_make_footer(gz);
2952 gzfile_write_raw(gz);
2958gzfile_writer_end(
struct gzfile *gz)
2973 gzfile_check_footer(gz,
Qnil);
2980gzfile_reader_end(
struct gzfile *gz)
2989gzfile_reader_rewind(
struct gzfile *gz)
3003gzfile_reader_get_unused(
struct gzfile *gz)
3010 gzfile_check_footer(gz,
Qnil);
3025 rb_raise(cGzError,
"closed gzip stream");
3091 gzfile_close(gz, 1);
3101 if (close_io_on_error) {
3171 return get_gzfile(
obj)->
io;
3300 rb_raise(cGzError,
"header is already written");
3323 rb_raise(cGzError,
"header is already written");
3347 rb_raise(cGzError,
"header is already written");
3375 gzfile_close(gz, 1);
3393 gzfile_close(gz, 0);
3453 gz->
z.
flags &= ~GZFILE_FLAG_SYNC;
3478 uLong total_out = gz->
z.
stream.total_out;
3481 if (total_out >= (uLong)buf_filled) {
3484 return LONG2FIX(-(buf_filled - (
long)total_out));
3609 rb_gzfile_ecopts(gz, opt);
3635 flush =
FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3636 if (flush != Z_NO_FLUSH) {
3637 zstream_run(&gz->
z, (Bytef*)
"", 0, flush);
3640 gzfile_write_raw(gz);
3656 while (
argc-- > 0) {
3679 gzfile_write(gz, (Bytef*)&c, 1);
3689#define rb_gzwriter_addstr rb_io_addstr
3694#define rb_gzwriter_printf rb_io_printf
3699#define rb_gzwriter_print rb_io_print
3704#define rb_gzwriter_puts rb_io_puts
3810 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
3816 gzfile_read_header(gz,
Qnil);
3817 rb_gzfile_ecopts(gz, opt);
3837 gzfile_reader_rewind(gz);
3852 return gzfile_reader_get_unused(gz);
3869 return gzfile_read_all(gz);
3876 return gzfile_read(gz,
len);
3906 return gzfile_readpartial(gz,
len, outbuf);
3919 return gzfile_getc(gz);
3931 dst = rb_gzreader_getc(
obj);
3949 dst = gzfile_read(gz, 1);
3965 dst = rb_gzreader_getbyte(
obj);
3984 while (!
NIL_P(c = rb_gzreader_getc(
obj))) {
4002 while (!
NIL_P(c = rb_gzreader_getbyte(
obj))) {
4016 rb_warn(
"Zlib::GzipReader#bytes is deprecated; use #each_byte instead");
4019 return rb_gzreader_each_byte(
obj);
4033 return rb_gzreader_ungetbyte(
obj, s);
4034 gz = get_gzfile(
obj);
4053 gzfile_ungetbyte(gz,
NUM2CHR(ch));
4058gzreader_skip_linebreaks(
struct gzfile *gz)
4066 gzfile_read_more(gz,
Qnil);
4071 while (
n++, *(p++) ==
'\n') {
4073 str = zstream_detach_buffer(&gz->
z);
4074 gzfile_calc_crc(gz,
str);
4077 gzfile_read_more(gz,
Qnil);
4084 str = zstream_shift_buffer(&gz->
z,
n - 1);
4085 gzfile_calc_crc(gz,
str);
4089rscheck(
const char *rsptr,
long rslen,
VALUE rs)
4096gzreader_charboundary(
struct gzfile *gz,
long n)
4124 long rslen,
n, limit = -1;
4139 else if (!
NIL_P(rs)) {
4157 dst = gzfile_read_all(gz);
4160 else if ((
n = gzfile_fill(gz, limit)) <= 0) {
4165 n = gzreader_charboundary(gz,
n);
4170 dst = zstream_shift_buffer(&gz->
z,
n);
4171 if (
NIL_P(dst))
return dst;
4172 gzfile_calc_crc(gz, dst);
4173 dst = gzfile_newstr(gz, dst);
4191 gzreader_skip_linebreaks(gz);
4197 return gzfile_read(gz, rslen);
4199 gzfile_read_more(gz,
Qnil);
4208 gzfile_read_more(gz,
Qnil);
4211 if (!rspara) rscheck(rsptr, rslen, rs);
4213 if (limit > 0 && filled >= limit) {
4216 res =
memchr(p, rsptr[0], (filled -
n + 1));
4219 if (limit > 0 && filled >= limit)
break;
4223 n += (
long)(res - p);
4225 if (rslen == 1 ||
memcmp(p, rsptr, rslen) == 0)
break;
4230 n = gzreader_charboundary(gz,
n);
4234 dst = gzfile_read(gz,
n);
4235 if (
NIL_P(dst))
return dst;
4237 gzreader_skip_linebreaks(gz);
4241 return gzfile_newstr(gz, dst);
4302 rb_warn(
"Zlib::GzipReader#lines is deprecated; use #each_line instead");
4330rb_gzreader_external_encoding(
VALUE self)
4350zlib_gzip_end(
struct gzfile *gz)
4353 zstream_run(&gz->
z, (Bytef*)
"", 0, Z_FINISH);
4354 gzfile_make_footer(gz);
4355 zstream_end(&gz->
z);
4358#define OPTHASH_GIVEN_P(opts) \
4359 (argc > 0 && !NIL_P((opts) = rb_check_hash_type(argv[argc-1])) && (--argc, 1))
4360static ID id_level, id_strategy;
4389 struct gzfile *gz = &gz0;
4396 keyword_ids[0] = id_level;
4397 keyword_ids[1] = id_strategy;
4399 if (kwargs[0] !=
Qundef) {
4402 if (kwargs[1] !=
Qundef) {
4403 strategy = kwargs[1];
4408 gzfile_init(gz, &deflate_funcs, zlib_gzip_end);
4417 args[0] = (
VALUE)gz;
4430 gzfile_make_header(gz);
4435 zstream_run(&gz->
z,
ptr,
len, Z_NO_FLUSH);
4437 gzfile_close(gz, 0);
4438 return zstream_detach_buffer(&gz->
z);
4442zlib_gunzip_end(
struct gzfile *gz)
4445 zstream_end(&gz->
z);
4472 struct gzfile *gz = &gz0;
4477 gzfile_init(gz, &inflate_funcs, zlib_gunzip_end);
4478 err = inflateInit2(&gz->
z.
stream, -MAX_WBITS);
4494 gzfile_read_header(gz,
Qnil);
4495 dst = zstream_detach_buffer(&gz->
z);
4496 gzfile_calc_crc(gz, dst);
4498 rb_raise(cGzError,
"unexpected end of file");
4501 rb_raise(cNoFooter,
"footer is not found");
4503 gzfile_check_footer(gz,
Qnil);
4513 VALUE mZlib, cZStream, cDeflate, cInflate;
4515 VALUE cGzipFile, cGzipWriter, cGzipReader;
4520 id_dictionaries =
rb_intern(
"@dictionaries");
4562 rb_define_method(cZStream,
"flush_next_out", rb_zstream_flush_next_out, 0);
4599 rb_define_method(cDeflate,
"set_dictionary", rb_deflate_set_dictionary, 1);
4606 rb_define_method(cInflate,
"add_dictionary", rb_inflate_add_dictionary, 1);
4611 rb_define_method(cInflate,
"set_dictionary", rb_inflate_set_dictionary, 1);
4625 INT2FIX(Z_DEFAULT_COMPRESSION));
4696 id_readpartial =
rb_intern(
"readpartial");
4777 rb_define_method(cGzipReader,
"external_encoding", rb_gzreader_external_encoding, 0);
int rb_enc_dummy_p(rb_encoding *enc)
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
rb_encoding * rb_ascii8bit_encoding(void)
rb_encoding * rb_enc_get(VALUE obj)
rb_encoding * rb_default_external_encoding(void)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
#define ECONV_AFTER_OUTPUT
int rb_econv_prepare_opts(VALUE opthash, VALUE *ecopts)
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
#define rb_enc_left_char_head(s, p, e, enc)
rb_econv_result_t rb_econv_convert(rb_econv_t *ec, const unsigned char **source_buffer_ptr, const unsigned char *source_buffer_end, unsigned char **destination_buffer_ptr, unsigned char *destination_buffer_end, int flags)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
#define MBCLEN_NEEDMORE_LEN(ret)
#define rb_enc_mbmaxlen(enc)
VALUE rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags)
rb_econv_t * rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE ecopts)
#define MBCLEN_CHARFOUND_LEN(ret)
void rb_econv_check_error(rb_econv_t *ec)
#define MBCLEN_NEEDMORE_P(ret)
#define ECONV_PARTIAL_INPUT
#define MBCLEN_CHARFOUND_P(ret)
void rb_econv_close(rb_econv_t *ec)
char str[HTML_ESCAPE_MAX_LEN+1]
void rb_include_module(VALUE, VALUE)
VALUE rb_define_class_under(VALUE, const char *, VALUE)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *)
void rb_define_attr(VALUE, const char *, int, int)
Defines (a) public accessor method(s) for an attribute.
int rb_block_given_p(void)
Determines if the current method is given a block.
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *)
VALUE rb_cObject
Object class.
void rb_raise(VALUE exc, const char *fmt,...)
VALUE rb_rescue2(VALUE(*)(VALUE), VALUE, VALUE(*)(VALUE, VALUE), VALUE,...)
An equivalent of rescue clause.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_protect(VALUE(*)(VALUE), VALUE, int *)
Protects a function call from potential global escapes from the function.
void rb_warn(const char *fmt,...)
VALUE rb_exc_new_str(VALUE, VALUE)
VALUE rb_rescue(VALUE(*)(VALUE), VALUE, VALUE(*)(VALUE, VALUE), VALUE)
An equivalent of rescue clause.
VALUE rb_ensure(VALUE(*)(VALUE), VALUE, VALUE(*)(VALUE), VALUE)
An equivalent to ensure clause.
VALUE rb_errinfo(void)
The current exception in the current thread.
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
void rb_sys_fail(const char *mesg)
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Tries to convert an object into another type.
VALUE rb_class_new_instance_kw(int, const VALUE *, VALUE, int)
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_Integer(VALUE)
Equivalent to Kernel#Integer in Ruby.
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
size_t rb_str_capacity(VALUE str)
void(* end)(struct gzfile *)
union read_raw_arg::@134 as
struct read_raw_arg::@134::@135 in
int(* run)(z_streamp, int)
const struct zstream::zstream_funcs * func
RUBY_SYMBOL_EXPORT_BEGIN void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
#define RB_NOGVL_UBF_ASYNC_SAFE
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
#define RUBY_ZLIB_VERSION
#define rb_zlib_crc32_combine
#define ZSTREAM_IS_READY(z)
#define zstream_append_input2(z, v)
#define VALGRIND_MAKE_MEM_DEFINED(p, n)
#define ZSTREAM_BUF_FILLED(z)
#define GZ_METHOD_DEFLATE
#define rb_zlib_adler32_combine
#define zstream_append_buffer2(z, v)
#define GZ_FLAG_ORIG_NAME
NORETURN(static void gzfile_raise(struct gzfile *, VALUE, const char *))
#define ZSTREAM_FLAG_IN_STREAM
#define GZ_EXTRAFLAG_FAST
#define GZFILE_IS_FINISHED(gz)
#define zstream_init_deflate(z)
#define rb_gzwriter_addstr
#define zstream_init_inflate(z)
#define rb_gzwriter_printf
#define gzfile_writer_new(gz)
#define zstream_deflate_new(klass)
#define GZ_FLAG_MULTIPART
#define ZSTREAM_FLAG_READY
#define ZSTREAM_INITIAL_BUFSIZE
#define GZFILE_FLAG_FOOTER_FINISHED
#define ZSTREAM_IS_GZFILE(z)
#define GZ_EXTRAFLAG_SLOW
#define checksum_long(func, sum, ptr, len)
#define ARG_MEMLEVEL(val)
#define rb_gzwriter_print
#define ZSTREAM_IN_PROGRESS
#define ZSTREAM_FLAG_FINISHED
#define gzfile_reader_new(gz)
#define GZ_FLAG_UNKNOWN_MASK
#define GZFILE_FLAG_MTIME_IS_SET
#define ZSTREAM_AVAIL_OUT_STEP_MAX
#define ZSTREAM_FLAG_GZFILE
#define ZSTREAM_IS_FINISHED(z)
#define OPTHASH_GIVEN_P(opts)
#define GZFILE_FLAG_HEADER_FINISHED
#define ZSTREAM_AVAIL_OUT_STEP_MIN
#define ZSTREAM_FLAG_CLOSING
#define FIXNUMARG(val, ifnil)
#define zstream_inflate_new(klass)
#define ZSTREAM_IS_CLOSING(z)
#define ZSTREAM_EXPAND_BUFFER_OK
#define ARG_STRATEGY(val)