Ruby  1.9.3p551(2014-11-13revision48407)
zlib.c
Go to the documentation of this file.
1 /*
2  * zlib.c - An interface for zlib.
3  *
4  * Copyright (C) UENO Katsuhiro 2000-2003
5  *
6  * $Id: zlib.c 45091 2014-02-22 00:50:33Z usa $
7  */
8 
9 #include <ruby.h>
10 #include <zlib.h>
11 #include <time.h>
12 #include <ruby/io.h>
13 
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))
18 # endif
19 # ifndef VALGRIND_MAKE_MEM_UNDEFINED
20 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n))
21 # endif
22 #else
23 # define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */
24 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */
25 #endif
26 
27 #define RUBY_ZLIB_VERSION "0.6.0"
28 
29 
30 #define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0)
31 
32 #ifndef GZIP_SUPPORT
33 #define GZIP_SUPPORT 1
34 #endif
35 
36 /* from zutil.h */
37 #ifndef DEF_MEM_LEVEL
38 #if MAX_MEM_LEVEL >= 8
39 #define DEF_MEM_LEVEL 8
40 #else
41 #define DEF_MEM_LEVEL MAX_MEM_LEVEL
42 #endif
43 #endif
44 
45 #if SIZEOF_LONG > SIZEOF_INT
46 static inline uInt
47 max_uint(long n)
48 {
49  if (n > UINT_MAX) n = UINT_MAX;
50  return (uInt)n;
51 }
52 #define MAX_UINT(n) max_uint(n)
53 #else
54 #define MAX_UINT(n) (uInt)(n)
55 #endif
56 
57 #define sizeof(x) ((int)sizeof(x))
58 
59 /*--------- Prototypes --------*/
60 
61 static NORETURN(void raise_zlib_error(int, const char*));
63 static VALUE do_checksum(int, VALUE*, uLong (*)(uLong, const Bytef*, uInt));
64 static VALUE rb_zlib_adler32(int, VALUE*, VALUE);
65 static VALUE rb_zlib_crc32(int, VALUE*, VALUE);
67 static voidpf zlib_mem_alloc(voidpf, uInt, uInt);
68 static void zlib_mem_free(voidpf, voidpf);
69 static void finalizer_warn(const char*);
70 
71 struct zstream;
72 struct zstream_funcs;
73 static void zstream_init(struct zstream*, const struct zstream_funcs*);
74 static void zstream_expand_buffer(struct zstream*);
75 static void zstream_expand_buffer_into(struct zstream*, unsigned long);
76 static void zstream_append_buffer(struct zstream*, const Bytef*, long);
77 static VALUE zstream_detach_buffer(struct zstream*);
78 static VALUE zstream_shift_buffer(struct zstream*, long);
79 static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long);
80 static void zstream_buffer_ungetbyte(struct zstream*, int);
81 static void zstream_append_input(struct zstream*, const Bytef*, long);
82 static void zstream_discard_input(struct zstream*, long);
83 static void zstream_reset_input(struct zstream*);
84 static void zstream_passthrough_input(struct zstream*);
85 static VALUE zstream_detach_input(struct zstream*);
86 static void zstream_reset(struct zstream*);
87 static VALUE zstream_end(struct zstream*);
88 static void zstream_run(struct zstream*, Bytef*, long, int);
89 static VALUE zstream_sync(struct zstream*, Bytef*, long);
90 static void zstream_mark(struct zstream*);
91 static void zstream_free(struct zstream*);
92 static VALUE zstream_new(VALUE, const struct zstream_funcs*);
93 static struct zstream *get_zstream(VALUE);
94 static void zstream_finalize(struct zstream*);
95 
96 static VALUE rb_zstream_end(VALUE);
110 
112 static VALUE rb_deflate_initialize(int, VALUE*, VALUE);
114 static VALUE deflate_run(VALUE);
115 static VALUE rb_deflate_s_deflate(int, VALUE*, VALUE);
116 static void do_deflate(struct zstream*, VALUE, int);
117 static VALUE rb_deflate_deflate(int, VALUE*, VALUE);
119 static VALUE rb_deflate_flush(int, VALUE*, VALUE);
122 
123 static VALUE inflate_run(VALUE);
125 static VALUE rb_inflate_initialize(int, VALUE*, VALUE);
127 static void do_inflate(struct zstream*, VALUE);
133 
134 #if GZIP_SUPPORT
135 struct gzfile;
136 static void gzfile_mark(struct gzfile*);
137 static void gzfile_free(struct gzfile*);
138 static VALUE gzfile_new(VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*)));
139 static void gzfile_reset(struct gzfile*);
140 static void gzfile_close(struct gzfile*, int);
141 static void gzfile_write_raw(struct gzfile*);
144 static VALUE gzfile_read_raw(struct gzfile*);
145 static int gzfile_read_raw_ensure(struct gzfile*, long);
146 static char *gzfile_read_raw_until_zero(struct gzfile*, long);
147 static unsigned int gzfile_get16(const unsigned char*);
148 static unsigned long gzfile_get32(const unsigned char*);
149 static void gzfile_set32(unsigned long n, unsigned char*);
150 static void gzfile_make_header(struct gzfile*);
151 static void gzfile_make_footer(struct gzfile*);
152 static void gzfile_read_header(struct gzfile*);
153 static void gzfile_check_footer(struct gzfile*);
154 static void gzfile_write(struct gzfile*, Bytef*, long);
155 static long gzfile_read_more(struct gzfile*);
156 static void gzfile_calc_crc(struct gzfile*, VALUE);
157 static VALUE gzfile_read(struct gzfile*, long);
158 static VALUE gzfile_read_all(struct gzfile*);
159 static void gzfile_ungets(struct gzfile*, const Bytef*, long);
160 static void gzfile_ungetbyte(struct gzfile*, int);
162 static void gzfile_writer_end(struct gzfile*);
164 static void gzfile_reader_end(struct gzfile*);
165 static void gzfile_reader_rewind(struct gzfile*);
166 static VALUE gzfile_reader_get_unused(struct gzfile*);
167 static struct gzfile *get_gzfile(VALUE);
169 static VALUE rb_gzfile_s_wrap(int, VALUE*, VALUE);
170 static VALUE gzfile_s_open(int, VALUE*, VALUE, const char*);
171 NORETURN(static void gzfile_raise(struct gzfile *, VALUE, const char *));
173 
174 static VALUE rb_gzfile_to_io(VALUE);
175 static VALUE rb_gzfile_crc(VALUE);
176 static VALUE rb_gzfile_mtime(VALUE);
177 static VALUE rb_gzfile_level(VALUE);
186 static VALUE rb_gzfile_close(VALUE);
189 static VALUE rb_gzfile_eof_p(VALUE);
190 static VALUE rb_gzfile_sync(VALUE);
194 static VALUE rb_gzfile_path(VALUE);
195 
197 static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE);
198 static VALUE rb_gzwriter_initialize(int, VALUE*, VALUE);
199 static VALUE rb_gzwriter_flush(int, VALUE*, VALUE);
202 
204 static VALUE rb_gzreader_s_open(int, VALUE*, VALUE);
205 static VALUE rb_gzreader_initialize(int, VALUE*, VALUE);
208 static VALUE rb_gzreader_read(int, VALUE*, VALUE);
214 static void gzreader_skip_linebreaks(struct gzfile*);
215 static VALUE gzreader_gets(int, VALUE*, VALUE);
216 static VALUE rb_gzreader_gets(int, VALUE*, VALUE);
217 static VALUE rb_gzreader_readline(int, VALUE*, VALUE);
218 static VALUE rb_gzreader_each(int, VALUE*, VALUE);
219 static VALUE rb_gzreader_readlines(int, VALUE*, VALUE);
220 #endif /* GZIP_SUPPORT */
221 
222 /*
223  * Document-module: Zlib
224  *
225  * == Overview
226  *
227  * Access to the zlib library.
228  *
229  * == Class tree
230  *
231  * - Zlib::Deflate
232  * - Zlib::Inflate
233  * - Zlib::ZStream
234  * - Zlib::Error
235  * - Zlib::StreamEnd
236  * - Zlib::NeedDict
237  * - Zlib::DataError
238  * - Zlib::StreamError
239  * - Zlib::MemError
240  * - Zlib::BufError
241  * - Zlib::VersionError
242  *
243  * (if you have GZIP_SUPPORT)
244  * - Zlib::GzipReader
245  * - Zlib::GzipWriter
246  * - Zlib::GzipFile
247  * - Zlib::GzipFile::Error
248  * - Zlib::GzipFile::LengthError
249  * - Zlib::GzipFile::CRCError
250  * - Zlib::GzipFile::NoFooter
251  *
252  * see also zlib.h
253  *
254  */
255 void Init_zlib(void);
256 
257 /*--------- Exceptions --------*/
258 
261 
262 static void
263 raise_zlib_error(int err, const char *msg)
264 {
265  VALUE exc;
266 
267  if (!msg) {
268  msg = zError(err);
269  }
270 
271  switch(err) {
272  case Z_STREAM_END:
273  exc = rb_exc_new2(cStreamEnd, msg);
274  break;
275  case Z_NEED_DICT:
276  exc = rb_exc_new2(cNeedDict, msg);
277  break;
278  case Z_STREAM_ERROR:
279  exc = rb_exc_new2(cStreamError, msg);
280  break;
281  case Z_DATA_ERROR:
282  exc = rb_exc_new2(cDataError, msg);
283  break;
284  case Z_BUF_ERROR:
285  exc = rb_exc_new2(cBufError, msg);
286  break;
287  case Z_VERSION_ERROR:
288  exc = rb_exc_new2(cVersionError, msg);
289  break;
290  case Z_MEM_ERROR:
291  exc = rb_exc_new2(cMemError, msg);
292  break;
293  case Z_ERRNO:
294  rb_sys_fail(msg);
295  /* no return */
296  default:
297  exc = rb_exc_new3(cZError,
298  rb_sprintf("unknown zlib error %d: %s", err, msg));
299  }
300 
301  rb_exc_raise(exc);
302 }
303 
304 
305 /*--- Warning (in finalizer) ---*/
306 
307 static void
308 finalizer_warn(const char *msg)
309 {
310  fprintf(stderr, "zlib(finalizer): %s\n", msg);
311 }
312 
313 
314 /*-------- module Zlib --------*/
315 
316 /*
317  * Document-method: Zlib.zlib_version
318  *
319  * Returns the string which represents the version of zlib library.
320  */
321 static VALUE
323 {
324  VALUE str;
325 
326  str = rb_str_new2(zlibVersion());
327  OBJ_TAINT(str); /* for safe */
328  return str;
329 }
330 
331 #if SIZEOF_LONG > SIZEOF_INT
332 static uLong
333 checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len)
334 {
335  if (len > UINT_MAX) {
336  do {
337  sum = func(sum, ptr, UINT_MAX);
338  ptr += UINT_MAX;
339  len -= UINT_MAX;
340  } while (len >= UINT_MAX);
341  }
342  if (len > 0) sum = func(sum, ptr, (uInt)len);
343  return sum;
344 }
345 #else
346 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len))
347 #endif
348 
349 static VALUE
353  uLong (*func)(uLong, const Bytef*, uInt);
354 {
355  VALUE str, vsum;
356  unsigned long sum;
357 
358  rb_scan_args(argc, argv, "02", &str, &vsum);
359 
360  if (!NIL_P(vsum)) {
361  sum = NUM2ULONG(vsum);
362  }
363  else if (NIL_P(str)) {
364  sum = 0;
365  }
366  else {
367  sum = func(0, Z_NULL, 0);
368  }
369 
370  if (NIL_P(str)) {
371  sum = func(sum, Z_NULL, 0);
372  }
373  else {
374  StringValue(str);
375  sum = checksum_long(func, sum, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
376  }
377  return rb_uint2inum(sum);
378 }
379 
380 /*
381  * Document-method: Zlib.adler32
382  *
383  * call-seq: Zlib.adler32(string, adler)
384  *
385  * Calculates Adler-32 checksum for +string+, and returns updated value of
386  * +adler+. If +string+ is omitted, it returns the Adler-32 initial value. If
387  * +adler+ is omitted, it assumes that the initial value is given to +adler+.
388  *
389  * FIXME: expression.
390  */
391 static VALUE
392 rb_zlib_adler32(int argc, VALUE *argv, VALUE klass)
393 {
394  return do_checksum(argc, argv, adler32);
395 }
396 
397 #ifdef HAVE_ADLER32_COMBINE
398 /*
399  * Document-method: Zlib.adler32_combine
400  *
401  * call-seq: Zlib.adler32_combine(adler1, adler2, len2)
402  *
403  * Combine two Adler-32 check values in to one. +alder1+ is the first Adler-32
404  * value, +adler2+ is the second Adler-32 value. +len2+ is the length of the
405  * string used to generate +adler2+.
406  *
407  */
408 static VALUE
409 rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2)
410 {
411  return ULONG2NUM(
412  adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2)));
413 }
414 #else
415 #define rb_zlib_adler32_combine rb_f_notimplement
416 #endif
417 
418 /*
419  * Document-method: Zlib.crc32
420  *
421  * call-seq: Zlib.crc32(string, adler)
422  *
423  * Calculates CRC checksum for +string+, and returns updated value of +crc+. If
424  * +string+ is omitted, it returns the CRC initial value. If +crc+ is omitted, it
425  * assumes that the initial value is given to +crc+.
426  *
427  * FIXME: expression.
428  */
429 static VALUE
430 rb_zlib_crc32(int argc, VALUE *argv, VALUE klass)
431 {
432  return do_checksum(argc, argv, crc32);
433 }
434 
435 #ifdef HAVE_CRC32_COMBINE
436 /*
437  * Document-method: Zlib.crc32_combine
438  *
439  * call-seq: Zlib.crc32_combine(crc1, crc2, len2)
440  *
441  * Combine two CRC-32 check values in to one. +crc1+ is the first CRC-32
442  * value, +crc2+ is the second CRC-32 value. +len2+ is the length of the
443  * string used to generate +crc2+.
444  *
445  */
446 static VALUE
447 rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2)
448 {
449  return ULONG2NUM(
450  crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2)));
451 }
452 #else
453 #define rb_zlib_crc32_combine rb_f_notimplement
454 #endif
455 
456 /*
457  * Document-method: Zlib.crc_table
458  *
459  * Returns the table for calculating CRC checksum as an array.
460  */
461 static VALUE
463 {
464 #if !defined(HAVE_TYPE_Z_CRC_T)
465  /* z_crc_t is defined since zlib-1.2.7. */
466  typedef unsigned long z_crc_t;
467 #endif
468  const z_crc_t *crctbl;
469  VALUE dst;
470  int i;
471 
472  crctbl = get_crc_table();
473  dst = rb_ary_new2(256);
474 
475  for (i = 0; i < 256; i++) {
476  rb_ary_push(dst, rb_uint2inum(crctbl[i]));
477  }
478  return dst;
479 }
480 
481 
482 
483 /*-------- zstream - internal APIs --------*/
484 
485 struct zstream {
486  unsigned long flags;
490  z_stream stream;
491  const struct zstream_funcs {
492  int (*reset)(z_streamp);
493  int (*end)(z_streamp);
494  int (*run)(z_streamp, int);
495  } *func;
496 };
497 
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
503 
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)
508 
509 /* I think that more better value should be found,
510  but I gave up finding it. B) */
511 #define ZSTREAM_INITIAL_BUFSIZE 1024
512 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384
513 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048
514 
515 static const struct zstream_funcs deflate_funcs = {
516  deflateReset, deflateEnd, deflate,
517 };
518 
519 static const struct zstream_funcs inflate_funcs = {
520  inflateReset, inflateEnd, inflate,
521 };
522 
523 
524 static voidpf
525 zlib_mem_alloc(voidpf opaque, uInt items, uInt size)
526 {
527  voidpf p = xmalloc(items * size);
528  /* zlib FAQ: Valgrind (or some similar memory access checker) says that
529  deflate is performing a conditional jump that depends on an
530  uninitialized value. Isn't that a bug?
531  http://www.zlib.net/zlib_faq.html#faq36 */
532  VALGRIND_MAKE_MEM_DEFINED(p, items * size);
533  return p;
534 }
535 
536 static void
537 zlib_mem_free(voidpf opaque, voidpf address)
538 {
539  xfree(address);
540 }
541 
542 static void
543 zstream_init(struct zstream *z, const struct zstream_funcs *func)
544 {
545  z->flags = 0;
546  z->buf = Qnil;
547  z->buf_filled = 0;
548  z->input = Qnil;
549  z->stream.zalloc = zlib_mem_alloc;
550  z->stream.zfree = zlib_mem_free;
551  z->stream.opaque = Z_NULL;
552  z->stream.msg = Z_NULL;
553  z->stream.next_in = Z_NULL;
554  z->stream.avail_in = 0;
555  z->stream.next_out = Z_NULL;
556  z->stream.avail_out = 0;
557  z->func = func;
558 }
559 
560 #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs)
561 #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs)
562 
563 static void
565 {
566  long inc;
567 
568  if (NIL_P(z->buf)) {
569  /* I uses rb_str_new here not rb_str_buf_new because
570  rb_str_buf_new makes a zero-length string. */
572  z->buf_filled = 0;
573  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
574  z->stream.avail_out = ZSTREAM_INITIAL_BUFSIZE;
575  RBASIC(z->buf)->klass = 0;
576  return;
577  }
578 
580  /* to keep other threads from freezing */
581  z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX;
582  }
583  else {
584  inc = z->buf_filled / 2;
585  if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) {
587  }
588  rb_str_resize(z->buf, z->buf_filled + inc);
589  z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ?
590  (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX;
591  }
592  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
593 }
594 
595 static void
596 zstream_expand_buffer_into(struct zstream *z, unsigned long size)
597 {
598  if (NIL_P(z->buf)) {
599  /* I uses rb_str_new here not rb_str_buf_new because
600  rb_str_buf_new makes a zero-length string. */
601  z->buf = rb_str_new(0, size);
602  z->buf_filled = 0;
603  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
604  z->stream.avail_out = MAX_UINT(size);
605  RBASIC(z->buf)->klass = 0;
606  }
607  else if (z->stream.avail_out != size) {
608  rb_str_resize(z->buf, z->buf_filled + size);
609  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
610  z->stream.avail_out = MAX_UINT(size);
611  }
612 }
613 
614 static void
615 zstream_append_buffer(struct zstream *z, const Bytef *src, long len)
616 {
617  if (NIL_P(z->buf)) {
618  z->buf = rb_str_buf_new(len);
619  rb_str_buf_cat(z->buf, (const char*)src, len);
620  z->buf_filled = len;
621  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf);
622  z->stream.avail_out = 0;
623  RBASIC(z->buf)->klass = 0;
624  return;
625  }
626 
627  if (RSTRING_LEN(z->buf) < z->buf_filled + len) {
628  rb_str_resize(z->buf, z->buf_filled + len);
629  z->stream.avail_out = 0;
630  }
631  else {
632  if (z->stream.avail_out >= (uInt)len) {
633  z->stream.avail_out -= (uInt)len;
634  }
635  else {
636  z->stream.avail_out = 0;
637  }
638  }
639  memcpy(RSTRING_PTR(z->buf) + z->buf_filled, src, len);
640  z->buf_filled += len;
641  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
642 }
643 
644 #define zstream_append_buffer2(z,v) \
645  zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v))
646 
647 static VALUE
649 {
650  VALUE dst;
651 
652  if (NIL_P(z->buf)) {
653  dst = rb_str_new(0, 0);
654  }
655  else {
656  dst = z->buf;
657  rb_str_resize(dst, z->buf_filled);
658  RBASIC(dst)->klass = rb_cString;
659  }
660 
661  z->buf = Qnil;
662  z->buf_filled = 0;
663  z->stream.next_out = 0;
664  z->stream.avail_out = 0;
665  return dst;
666 }
667 
668 static VALUE
669 zstream_shift_buffer(struct zstream *z, long len)
670 {
671  VALUE dst;
672  long buflen;
673 
674  if (z->buf_filled <= len) {
675  return zstream_detach_buffer(z);
676  }
677 
678  dst = rb_str_subseq(z->buf, 0, len);
679  RBASIC(dst)->klass = rb_cString;
680  z->buf_filled -= len;
681  memmove(RSTRING_PTR(z->buf), RSTRING_PTR(z->buf) + len,
682  z->buf_filled);
683  z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled;
684  buflen = RSTRING_LEN(z->buf) - z->buf_filled;
685  if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) {
687  }
688  z->stream.avail_out = (uInt)buflen;
689 
690  return dst;
691 }
692 
693 static void
694 zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len)
695 {
696  if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) {
698  }
699 
700  memmove(RSTRING_PTR(z->buf) + len, RSTRING_PTR(z->buf), z->buf_filled);
701  memmove(RSTRING_PTR(z->buf), b, len);
702  z->buf_filled+=len;
703  if (z->stream.avail_out > 0) {
704  if (len > z->stream.avail_out) len = z->stream.avail_out;
705  z->stream.next_out+=len;
706  z->stream.avail_out-=(uInt)len;
707  }
708 }
709 
710 static void
712 {
713  if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) {
715  }
716 
717  memmove(RSTRING_PTR(z->buf) + 1, RSTRING_PTR(z->buf), z->buf_filled);
718  RSTRING_PTR(z->buf)[0] = (char)c;
719  z->buf_filled++;
720  if (z->stream.avail_out > 0) {
721  z->stream.next_out++;
722  z->stream.avail_out--;
723  }
724 }
725 
726 static void
727 zstream_append_input(struct zstream *z, const Bytef *src, long len)
728 {
729  if (len <= 0) return;
730 
731  if (NIL_P(z->input)) {
732  z->input = rb_str_buf_new(len);
733  rb_str_buf_cat(z->input, (const char*)src, len);
734  RBASIC(z->input)->klass = 0;
735  }
736  else {
737  rb_str_buf_cat(z->input, (const char*)src, len);
738  }
739 }
740 
741 #define zstream_append_input2(z,v)\
742  RB_GC_GUARD(v),\
743  zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v))
744 
745 static void
746 zstream_discard_input(struct zstream *z, long len)
747 {
748  if (NIL_P(z->input) || RSTRING_LEN(z->input) <= len) {
749  z->input = Qnil;
750  }
751  else {
752  memmove(RSTRING_PTR(z->input), RSTRING_PTR(z->input) + len,
753  RSTRING_LEN(z->input) - len);
754  rb_str_resize(z->input, RSTRING_LEN(z->input) - len);
755  }
756 }
757 
758 static void
760 {
761  z->input = Qnil;
762 }
763 
764 static void
766 {
767  if (!NIL_P(z->input)) {
769  z->input = Qnil;
770  }
771 }
772 
773 static VALUE
775 {
776  VALUE dst;
777 
778  if (NIL_P(z->input)) {
779  dst = rb_str_new(0, 0);
780  }
781  else {
782  dst = z->input;
783  RBASIC(dst)->klass = rb_cString;
784  }
785  z->input = Qnil;
786  RBASIC(dst)->klass = rb_cString;
787  return dst;
788 }
789 
790 static void
792 {
793  int err;
794 
795  err = z->func->reset(&z->stream);
796  if (err != Z_OK) {
797  raise_zlib_error(err, z->stream.msg);
798  }
800  z->buf = Qnil;
801  z->buf_filled = 0;
802  z->stream.next_out = 0;
803  z->stream.avail_out = 0;
805 }
806 
807 static VALUE
809 {
810  int err;
811 
812  if (!ZSTREAM_IS_READY(z)) {
813  rb_warning("attempt to close uninitialized zstream; ignored.");
814  return Qnil;
815  }
816  if (z->flags & ZSTREAM_FLAG_IN_STREAM) {
817  rb_warning("attempt to close unfinished zstream; reset forced.");
818  zstream_reset(z);
819  }
820 
822  err = z->func->end(&z->stream);
823  if (err != Z_OK) {
824  raise_zlib_error(err, z->stream.msg);
825  }
826  z->flags = 0;
827  return Qnil;
828 }
829 
830 static void
831 zstream_run(struct zstream *z, Bytef *src, long len, int flush)
832 {
833  uInt n;
834  int err;
835  volatile VALUE guard = Qnil;
836 
837  if (NIL_P(z->input) && len == 0) {
838  z->stream.next_in = (Bytef*)"";
839  z->stream.avail_in = 0;
840  }
841  else {
842  zstream_append_input(z, src, len);
843  z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
844  z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
845  /* keep reference to `z->input' so as not to be garbage collected
846  after zstream_reset_input() and prevent `z->stream.next_in'
847  from dangling. */
848  guard = z->input;
849  }
850 
851  if (z->stream.avail_out == 0) {
853  }
854 
855  for (;;) {
856  /* VC allocates err and guard to same address. accessing err and guard
857  in same scope prevents it. */
858  RB_GC_GUARD(guard);
859  n = z->stream.avail_out;
860  err = z->func->run(&z->stream, flush);
861  z->buf_filled += n - z->stream.avail_out;
863 
864  if (err == Z_STREAM_END) {
867  break;
868  }
869  if (err != Z_OK) {
870  if (flush != Z_FINISH && err == Z_BUF_ERROR
871  && z->stream.avail_out > 0) {
873  break;
874  }
876  if (z->stream.avail_in > 0) {
877  zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
878  }
879  raise_zlib_error(err, z->stream.msg);
880  }
881  if (z->stream.avail_out > 0) {
883  break;
884  }
886  }
887 
889  if (z->stream.avail_in > 0) {
890  zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
891  guard = Qnil; /* prevent tail call to make guard effective */
892  }
893 }
894 
895 static VALUE
896 zstream_sync(struct zstream *z, Bytef *src, long len)
897 {
898  VALUE rest;
899  int err;
900 
901  if (!NIL_P(z->input)) {
902  z->stream.next_in = (Bytef*)RSTRING_PTR(z->input);
903  z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input));
904  err = inflateSync(&z->stream);
905  if (err == Z_OK) {
907  RSTRING_LEN(z->input) - z->stream.avail_in);
908  zstream_append_input(z, src, len);
909  return Qtrue;
910  }
912  if (err != Z_DATA_ERROR) {
913  rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in);
914  raise_zlib_error(err, z->stream.msg);
915  }
916  }
917 
918  if (len <= 0) return Qfalse;
919 
920  z->stream.next_in = src;
921  z->stream.avail_in = MAX_UINT(len);
922  err = inflateSync(&z->stream);
923  if (err == Z_OK) {
924  zstream_append_input(z, z->stream.next_in, z->stream.avail_in);
925  return Qtrue;
926  }
927  if (err != Z_DATA_ERROR) {
928  rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in);
929  raise_zlib_error(err, z->stream.msg);
930  }
931  return Qfalse;
932 }
933 
934 static void
936 {
937  rb_gc_mark(z->buf);
938  rb_gc_mark(z->input);
939 }
940 
941 static void
943 {
944  int err = z->func->end(&z->stream);
945  if (err == Z_STREAM_ERROR)
946  finalizer_warn("the stream state was inconsistent.");
947  if (err == Z_DATA_ERROR)
948  finalizer_warn("the stream was freed prematurely.");
949 }
950 
951 static void
953 {
954  if (ZSTREAM_IS_READY(z)) {
955  zstream_finalize(z);
956  }
957  xfree(z);
958 }
959 
960 static VALUE
961 zstream_new(VALUE klass, const struct zstream_funcs *funcs)
962 {
963  VALUE obj;
964  struct zstream *z;
965 
966  obj = Data_Make_Struct(klass, struct zstream,
968  zstream_init(z, funcs);
969  return obj;
970 }
971 
972 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs)
973 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs)
974 
975 static struct zstream *
977 {
978  struct zstream *z;
979 
980  Data_Get_Struct(obj, struct zstream, z);
981  if (!ZSTREAM_IS_READY(z)) {
982  rb_raise(cZError, "stream is not ready");
983  }
984  return z;
985 }
986 
987 
988 /* ------------------------------------------------------------------------- */
989 
990 /*
991  * Document-class: Zlib::ZStream
992  *
993  * Zlib::ZStream is the abstract class for the stream which handles the
994  * compressed data. The operations are defined in the subclasses:
995  * Zlib::Deflate for compression, and Zlib::Inflate for decompression.
996  *
997  * An instance of Zlib::ZStream has one stream (struct zstream in the source)
998  * and two variable-length buffers which associated to the input (next_in) of
999  * the stream and the output (next_out) of the stream. In this document,
1000  * "input buffer" means the buffer for input, and "output buffer" means the
1001  * buffer for output.
1002  *
1003  * Data input into an instance of Zlib::ZStream are temporally stored into
1004  * the end of input buffer, and then data in input buffer are processed from
1005  * the beginning of the buffer until no more output from the stream is
1006  * produced (i.e. until avail_out > 0 after processing). During processing,
1007  * output buffer is allocated and expanded automatically to hold all output
1008  * data.
1009  *
1010  * Some particular instance methods consume the data in output buffer and
1011  * return them as a String.
1012  *
1013  * Here is an ascii art for describing above:
1014  *
1015  * +================ an instance of Zlib::ZStream ================+
1016  * || ||
1017  * || +--------+ +-------+ +--------+ ||
1018  * || +--| output |<---------|zstream|<---------| input |<--+ ||
1019  * || | | buffer | next_out+-------+next_in | buffer | | ||
1020  * || | +--------+ +--------+ | ||
1021  * || | | ||
1022  * +===|======================================================|===+
1023  * | |
1024  * v |
1025  * "output data" "input data"
1026  *
1027  * If an error occurs during processing input buffer, an exception which is a
1028  * subclass of Zlib::Error is raised. At that time, both input and output
1029  * buffer keep their conditions at the time when the error occurs.
1030  *
1031  * == Method Catalogue
1032  *
1033  * Many of the methods in this class are fairly low-level and unlikely to be
1034  * of interest to users. In fact, users are unlikely to use this class
1035  * directly; rather they will be interested in Zlib::Inflate and
1036  * Zlib::Deflate.
1037  *
1038  * The higher level methods are listed below.
1039  *
1040  * - #total_in
1041  * - #total_out
1042  * - #data_type
1043  * - #adler
1044  * - #reset
1045  * - #finish
1046  * - #finished?
1047  * - #close
1048  * - #closed?
1049  */
1050 
1051 /*
1052  * Closes the stream. All operations on the closed stream will raise an
1053  * exception.
1054  */
1055 static VALUE
1057 {
1058  zstream_end(get_zstream(obj));
1059  return Qnil;
1060 }
1061 
1062 /*
1063  * Resets and initializes the stream. All data in both input and output buffer
1064  * are discarded.
1065  */
1066 static VALUE
1068 {
1069  zstream_reset(get_zstream(obj));
1070  return Qnil;
1071 }
1072 
1073 /*
1074  * Finishes the stream and flushes output buffer. See Zlib::Deflate#finish and
1075  * Zlib::Inflate#finish for details of this behavior.
1076  */
1077 static VALUE
1079 {
1080  struct zstream *z = get_zstream(obj);
1081  VALUE dst;
1082 
1083  zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1084  dst = zstream_detach_buffer(z);
1085 
1086  OBJ_INFECT(dst, obj);
1087  return dst;
1088 }
1089 
1090 /*
1091  * Flushes input buffer and returns all data in that buffer.
1092  */
1093 static VALUE
1095 {
1096  struct zstream *z;
1097  VALUE dst;
1098 
1099  Data_Get_Struct(obj, struct zstream, z);
1100  dst = zstream_detach_input(z);
1101  OBJ_INFECT(dst, obj);
1102  return dst;
1103 }
1104 
1105 /*
1106  * Flushes output buffer and returns all data in that buffer.
1107  */
1108 static VALUE
1110 {
1111  struct zstream *z;
1112  VALUE dst;
1113 
1114  Data_Get_Struct(obj, struct zstream, z);
1115  dst = zstream_detach_buffer(z);
1116  OBJ_INFECT(dst, obj);
1117  return dst;
1118 }
1119 
1120 /*
1121  * Returns number of bytes of free spaces in output buffer. Because the free
1122  * space is allocated automatically, this method returns 0 normally.
1123  */
1124 static VALUE
1126 {
1127  struct zstream *z;
1128  Data_Get_Struct(obj, struct zstream, z);
1129  return rb_uint2inum(z->stream.avail_out);
1130 }
1131 
1132 /*
1133  * Allocates +size+ bytes of free space in the output buffer. If there are more
1134  * than +size+ bytes already in the buffer, the buffer is truncated. Because
1135  * free space is allocated automatically, you usually don't need to use this
1136  * method.
1137  */
1138 static VALUE
1140 {
1141  struct zstream *z = get_zstream(obj);
1142 
1143  Check_Type(size, T_FIXNUM);
1145  return size;
1146 }
1147 
1148 /*
1149  * Returns bytes of data in the input buffer. Normally, returns 0.
1150  */
1151 static VALUE
1153 {
1154  struct zstream *z;
1155  Data_Get_Struct(obj, struct zstream, z);
1156  return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input)));
1157 }
1158 
1159 /*
1160  * Returns the total bytes of the input data to the stream. FIXME
1161  */
1162 static VALUE
1164 {
1165  return rb_uint2inum(get_zstream(obj)->stream.total_in);
1166 }
1167 
1168 /*
1169  * Returns the total bytes of the output data from the stream. FIXME
1170  */
1171 static VALUE
1173 {
1174  return rb_uint2inum(get_zstream(obj)->stream.total_out);
1175 }
1176 
1177 /*
1178  * Guesses the type of the data which have been inputed into the stream. The
1179  * returned value is either <tt>BINARY</tt>, <tt>ASCII</tt>, or
1180  * <tt>UNKNOWN</tt>.
1181  */
1182 static VALUE
1184 {
1185  return INT2FIX(get_zstream(obj)->stream.data_type);
1186 }
1187 
1188 /*
1189  * Returns the adler-32 checksum.
1190  */
1191 static VALUE
1193 {
1194  return rb_uint2inum(get_zstream(obj)->stream.adler);
1195 }
1196 
1197 /*
1198  * Returns true if the stream is finished.
1199  */
1200 static VALUE
1202 {
1203  return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse;
1204 }
1205 
1206 /*
1207  * Returns true if the stream is closed.
1208  */
1209 static VALUE
1211 {
1212  struct zstream *z;
1213  Data_Get_Struct(obj, struct zstream, z);
1214  return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue;
1215 }
1216 
1217 
1218 /* ------------------------------------------------------------------------- */
1219 
1220 /*
1221  * Document-class: Zlib::Deflate
1222  *
1223  * Zlib::Deflate is the class for compressing data. See Zlib::ZStream for more
1224  * information.
1225  */
1226 
1227 #define FIXNUMARG(val, ifnil) \
1228  (NIL_P((val)) ? (ifnil) \
1229  : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val))))
1230 
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)
1236 
1237 
1238 static VALUE
1240 {
1241  return zstream_deflate_new(klass);
1242 }
1243 
1244 /*
1245  * Document-method: Zlib::Deflate.new
1246  *
1247  * call-seq: Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil)
1248  *
1249  * == Arguments
1250  *
1251  * +level+::
1252  * An Integer compression level between
1253  * BEST_SPEED and BEST_COMPRESSION
1254  * +windowBits+::
1255  * An Integer for the windowBits size. Should be
1256  * in the range 8..15, larger values of this parameter
1257  * result in better at the expense of memory usage.
1258  * +memlevel+::
1259  * Specifies how much memory should be allocated for
1260  * the internal compression state.
1261  * Between DEF_MEM_LEVEL and MAX_MEM_LEVEL
1262  * +strategy+::
1263  * A parameter to tune the compression algorithm. Use the
1264  * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a
1265  * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no
1266  * string match).
1267  *
1268  * == Description
1269  *
1270  * Creates a new deflate stream for compression. See zlib.h for details of
1271  * each argument. If an argument is nil, the default value of that argument is
1272  * used.
1273  *
1274  *
1275  * == examples
1276  *
1277  * === basic
1278  *
1279  * f = File.new("compressed.file","w+")
1280  * #=> #<File:compressed.file>
1281  * f << Zlib::Deflate.new().deflate(File.read("big.file"))
1282  * #=> #<File:compressed.file>
1283  * f.close
1284  * #=> nil
1285  *
1286  * === a little more robust
1287  *
1288  * compressed_file = File.open("compressed.file", "w+")
1289  * #=> #<File:compressed.file>
1290  * zd = Zlib::Deflate.new(Zlib::BEST_COMPRESSION, 15, Zlib::MAX_MEM_LEVEL, Zlib::HUFFMAN_ONLY)
1291  * #=> #<Zlib::Deflate:0x000000008610a0>
1292  * compressed_file << zd.deflate(File.read("big.file"))
1293  * #=> "\xD4z\xC6\xDE\b\xA1K\x1Ej\x8A ..."
1294  * compressed_file.close
1295  * #=> nil
1296  * zd.close
1297  * #=> nil
1298  *
1299  * (while this example will work, for best optimization the flags need to be reviewed for your specific function)
1300  *
1301  */
1302 static VALUE
1304 {
1305  struct zstream *z;
1306  VALUE level, wbits, memlevel, strategy;
1307  int err;
1308 
1309  rb_scan_args(argc, argv, "04", &level, &wbits, &memlevel, &strategy);
1310  Data_Get_Struct(obj, struct zstream, z);
1311 
1312  err = deflateInit2(&z->stream, ARG_LEVEL(level), Z_DEFLATED,
1313  ARG_WBITS(wbits), ARG_MEMLEVEL(memlevel),
1314  ARG_STRATEGY(strategy));
1315  if (err != Z_OK) {
1316  raise_zlib_error(err, z->stream.msg);
1317  }
1318  ZSTREAM_READY(z);
1319 
1320  return obj;
1321 }
1322 
1323 /*
1324  * Document-method: Zlib::Deflate#initialize_copy
1325  *
1326  * Duplicates the deflate stream.
1327  */
1328 static VALUE
1330 {
1331  struct zstream *z1, *z2;
1332  int err;
1333 
1334  Data_Get_Struct(self, struct zstream, z1);
1335  z2 = get_zstream(orig);
1336 
1337  if (z1 == z2) return self;
1338  err = deflateCopy(&z1->stream, &z2->stream);
1339  if (err != Z_OK) {
1340  raise_zlib_error(err, 0);
1341  }
1342  z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input);
1343  z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf);
1344  z1->buf_filled = z2->buf_filled;
1345  z1->flags = z2->flags;
1346 
1347  return self;
1348 }
1349 
1350 static VALUE
1352 {
1353  struct zstream *z = (struct zstream*)((VALUE*)args)[0];
1354  VALUE src = ((VALUE*)args)[1];
1355 
1356  zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_FINISH);
1357  return zstream_detach_buffer(z);
1358 }
1359 
1360 /*
1361  * Document-method: Zlib::Deflate.deflate
1362  *
1363  * call-seq: Zlib.deflate(string[, level])
1364  * Zlib::Deflate.deflate(string[, level])
1365  *
1366  * Compresses the given +string+. Valid values of level are
1367  * <tt>NO_COMPRESSION</tt>, <tt>BEST_SPEED</tt>,
1368  * <tt>BEST_COMPRESSION</tt>, <tt>DEFAULT_COMPRESSION</tt>, and an
1369  * integer from 0 to 9 (the default is 6).
1370  *
1371  * This method is almost equivalent to the following code:
1372  *
1373  * def deflate(string, level)
1374  * z = Zlib::Deflate.new(level)
1375  * dst = z.deflate(string, Zlib::NO_FLUSH)
1376  * z.close
1377  * dst
1378  * end
1379  *
1380  * See also Zlib.inflate
1381  *
1382  */
1383 static VALUE
1384 rb_deflate_s_deflate(int argc, VALUE *argv, VALUE klass)
1385 {
1386  struct zstream z;
1387  VALUE src, level, dst, args[2];
1388  int err, lev;
1389 
1390  rb_scan_args(argc, argv, "11", &src, &level);
1391 
1392  lev = ARG_LEVEL(level);
1393  StringValue(src);
1395  err = deflateInit(&z.stream, lev);
1396  if (err != Z_OK) {
1397  raise_zlib_error(err, z.stream.msg);
1398  }
1399  ZSTREAM_READY(&z);
1400 
1401  args[0] = (VALUE)&z;
1402  args[1] = src;
1403  dst = rb_ensure(deflate_run, (VALUE)args, zstream_end, (VALUE)&z);
1404 
1405  OBJ_INFECT(dst, src);
1406  return dst;
1407 }
1408 
1409 static void
1410 do_deflate(struct zstream *z, VALUE src, int flush)
1411 {
1412  if (NIL_P(src)) {
1413  zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1414  return;
1415  }
1416  StringValue(src);
1417  if (flush != Z_NO_FLUSH || RSTRING_LEN(src) > 0) { /* prevent BUF_ERROR */
1418  zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), flush);
1419  }
1420 }
1421 
1422 /*
1423  * Document-method: Zlib.deflate
1424  *
1425  * call-seq: deflate(string[, flush])
1426  *
1427  * == Arguments
1428  *
1429  * +string+::
1430  * String
1431  *
1432  * +flush+::
1433  * Integer representing a flush code. Either NO_FLUSH,
1434  * SYNC_FLUSH, FULL_FLUSH, or FINISH. See zlib.h for details.
1435  * Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
1436  * decide how much data to accumulate before producing output, in order to
1437  * maximize compression.
1438  *
1439  * == Description
1440  *
1441  * Inputs +string+ into the deflate stream and returns the output from the
1442  * stream. On calling this method, both the input and the output buffers of
1443  * the stream are flushed.
1444  *
1445  * If +string+ is nil, this method finishes the
1446  * stream, just like Zlib::ZStream#finish.
1447  *
1448  * == Usage
1449  *
1450  * comp = Zlib.deflate(File.read("big.file"))
1451  * or
1452  * comp = Zlib.deflate(File.read("big.file"), Zlib::FULL_FLUSH)
1453  *
1454  */
1455 static VALUE
1457 {
1458  struct zstream *z = get_zstream(obj);
1459  VALUE src, flush, dst;
1460 
1461  rb_scan_args(argc, argv, "11", &src, &flush);
1462  OBJ_INFECT(obj, src);
1463  do_deflate(z, src, ARG_FLUSH(flush));
1464  dst = zstream_detach_buffer(z);
1465 
1466  OBJ_INFECT(dst, obj);
1467  return dst;
1468 }
1469 
1470 /*
1471  * Document-method: Zlib::Deflate.<<
1472  *
1473  * call-seq: << string
1474  *
1475  * Inputs +string+ into the deflate stream just like Zlib::Deflate#deflate, but
1476  * returns the Zlib::Deflate object itself. The output from the stream is
1477  * preserved in output buffer.
1478  */
1479 static VALUE
1481 {
1482  OBJ_INFECT(obj, src);
1483  do_deflate(get_zstream(obj), src, Z_NO_FLUSH);
1484  return obj;
1485 }
1486 
1487 /*
1488  * Document-method: Zlib::Deflate#flush
1489  *
1490  * call-seq: flush(flush)
1491  *
1492  * This method is equivalent to <tt>deflate('', flush)</tt>. If flush is omitted,
1493  * <tt>SYNC_FLUSH</tt> is used as flush. This method is just provided
1494  * to improve the readability of your Ruby program.
1495  *
1496  * Please visit your zlib.h for a deeper detail on NO_FLUSH, SYNC_FLUSH, FULL_FLUSH, and FINISH
1497  *
1498  */
1499 static VALUE
1500 rb_deflate_flush(int argc, VALUE *argv, VALUE obj)
1501 {
1502  struct zstream *z = get_zstream(obj);
1503  VALUE v_flush, dst;
1504  int flush;
1505 
1506  rb_scan_args(argc, argv, "01", &v_flush);
1507  flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
1508  if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */
1509  zstream_run(z, (Bytef*)"", 0, flush);
1510  }
1511  dst = zstream_detach_buffer(z);
1512 
1513  OBJ_INFECT(dst, obj);
1514  return dst;
1515 }
1516 
1517 /*
1518  * Document-method: Zlib::Deflate.params
1519  *
1520  * call-seq: params(level, strategy)
1521  *
1522  * Changes the parameters of the deflate stream. See zlib.h for details. The
1523  * output from the stream by changing the params is preserved in output
1524  * buffer.
1525  *
1526  * +level+::
1527  * An Integer compression level between
1528  * BEST_SPEED and BEST_COMPRESSION
1529  * +strategy+::
1530  * A parameter to tune the compression algorithm. Use the
1531  * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a
1532  * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no
1533  * string match).
1534  *
1535  */
1536 static VALUE
1537 rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy)
1538 {
1539  struct zstream *z = get_zstream(obj);
1540  int level, strategy;
1541  int err;
1542  uInt n;
1543 
1544  level = ARG_LEVEL(v_level);
1545  strategy = ARG_STRATEGY(v_strategy);
1546 
1547  n = z->stream.avail_out;
1548  err = deflateParams(&z->stream, level, strategy);
1549  z->buf_filled += n - z->stream.avail_out;
1550  while (err == Z_BUF_ERROR) {
1551  rb_warning("deflateParams() returned Z_BUF_ERROR");
1553  n = z->stream.avail_out;
1554  err = deflateParams(&z->stream, level, strategy);
1555  z->buf_filled += n - z->stream.avail_out;
1556  }
1557  if (err != Z_OK) {
1558  raise_zlib_error(err, z->stream.msg);
1559  }
1560 
1561  return Qnil;
1562 }
1563 
1564 /*
1565  * Document-method: Zlib::Deflate.set_dictionary
1566  *
1567  * call-seq: set_dictionary(string)
1568  *
1569  * Sets the preset dictionary and returns +string+. This method is available
1570  * just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called.
1571  * See zlib.h for details.
1572  *
1573  * Can raise errors of Z_STREAM_ERROR if a parameter is invalid (such as
1574  * NULL dictionary) or the stream state is inconsistent, Z_DATA_ERROR if
1575  * the given dictionary doesn't match the expected one (incorrect adler32 value)
1576  *
1577  */
1578 static VALUE
1580 {
1581  struct zstream *z = get_zstream(obj);
1582  VALUE src = dic;
1583  int err;
1584 
1585  OBJ_INFECT(obj, dic);
1586  StringValue(src);
1587  err = deflateSetDictionary(&z->stream,
1588  (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
1589  if (err != Z_OK) {
1590  raise_zlib_error(err, z->stream.msg);
1591  }
1592 
1593  return dic;
1594 }
1595 
1596 
1597 /* ------------------------------------------------------------------------- */
1598 
1599 /*
1600  * Document-class: Zlib::Inflate
1601  *
1602  * Zlib:Inflate is the class for decompressing compressed data. Unlike
1603  * Zlib::Deflate, an instance of this class is not able to duplicate (clone,
1604  * dup) itself.
1605  */
1606 
1607 
1608 
1609 static VALUE
1611 {
1612  return zstream_inflate_new(klass);
1613 }
1614 
1615 /*
1616  * Document-method: Zlib::Inflate.new
1617  *
1618  * call-seq: Zlib::Inflate.new(window_bits)
1619  *
1620  * == Arguments
1621  *
1622  * +windowBits+::
1623  * An Integer for the windowBits size. Should be
1624  * in the range 8..15, larger values of this parameter
1625  * result in better at the expense of memory usage.
1626  *
1627  * == Description
1628  *
1629  * Creates a new inflate stream for decompression. See zlib.h for details
1630  * of the argument. If +window_bits+ is +nil+, the default value is used.
1631  *
1632  * == Example
1633  *
1634  * cf = File.open("compressed.file")
1635  * ucf = File.open("uncompressed.file", "w+")
1636  * zi = Zlib::Inflate.new(Zlib::MAX_WBITS)
1637  *
1638  * ucf << zi.inflate(cf.read)
1639  *
1640  * ucf.close
1641  * zi.close
1642  * cf.close
1643  *
1644  * or
1645  *
1646  * File.open("compressed.file") {|cf|
1647  * zi = Zlib::Inflate.new
1648  * File.open("uncompressed.file", "w+") {|ucf|
1649  * ucf << zi.inflate(cf.read)
1650  * }
1651  * zi.close
1652  * }
1653  *
1654  */
1655 static VALUE
1657 {
1658  struct zstream *z;
1659  VALUE wbits;
1660  int err;
1661 
1662  rb_scan_args(argc, argv, "01", &wbits);
1663  Data_Get_Struct(obj, struct zstream, z);
1664 
1665  err = inflateInit2(&z->stream, ARG_WBITS(wbits));
1666  if (err != Z_OK) {
1667  raise_zlib_error(err, z->stream.msg);
1668  }
1669  ZSTREAM_READY(z);
1670 
1671  return obj;
1672 }
1673 
1674 static VALUE
1676 {
1677  struct zstream *z = (struct zstream*)((VALUE*)args)[0];
1678  VALUE src = ((VALUE*)args)[1];
1679 
1680  zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1681  zstream_run(z, (Bytef*)"", 0, Z_FINISH); /* for checking errors */
1682  return zstream_detach_buffer(z);
1683 }
1684 
1685 /*
1686  * Document-method: Zlib::Inflate.inflate
1687  *
1688  * call-seq: Zlib::Inflate.inflate(string)
1689  *
1690  * Decompresses +string+. Raises a Zlib::NeedDict exception if a preset
1691  * dictionary is needed for decompression.
1692  *
1693  * This method is almost equivalent to the following code:
1694  *
1695  * def inflate(string)
1696  * zstream = Zlib::Inflate.new
1697  * buf = zstream.inflate(string)
1698  * zstream.finish
1699  * zstream.close
1700  * buf
1701  * end
1702  *
1703  * See also Zlib.deflate
1704  *
1705  */
1706 static VALUE
1708 {
1709  struct zstream z;
1710  VALUE dst, args[2];
1711  int err;
1712 
1713  StringValue(src);
1715  err = inflateInit(&z.stream);
1716  if (err != Z_OK) {
1717  raise_zlib_error(err, z.stream.msg);
1718  }
1719  ZSTREAM_READY(&z);
1720 
1721  args[0] = (VALUE)&z;
1722  args[1] = src;
1723  dst = rb_ensure(inflate_run, (VALUE)args, zstream_end, (VALUE)&z);
1724 
1725  OBJ_INFECT(dst, src);
1726  return dst;
1727 }
1728 
1729 static void
1731 {
1732  if (NIL_P(src)) {
1733  zstream_run(z, (Bytef*)"", 0, Z_FINISH);
1734  return;
1735  }
1736  StringValue(src);
1737  if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */
1738  zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH);
1739  }
1740 }
1741 
1742 /*
1743  * Document-method: Zlib::Inflate#inflate
1744  *
1745  * call-seq: inflate(string)
1746  *
1747  * Inputs +string+ into the inflate stream and returns the output from the
1748  * stream. Calling this method, both the input and the output buffer of the
1749  * stream are flushed. If string is +nil+, this method finishes the stream,
1750  * just like Zlib::ZStream#finish.
1751  *
1752  * Raises a Zlib::NeedDict exception if a preset dictionary is needed to
1753  * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then
1754  * call this method again with an empty string to flush the stream:
1755  *
1756  * inflater = Zlib::Inflate.new
1757  *
1758  * begin
1759  * out = inflater.inflate compressed
1760  * rescue Zlib::NeedDict
1761  * # ensure the dictionary matches the stream's required dictionary
1762  * raise unless inflater.adler == Zlib.adler32(dictionary)
1763  *
1764  * inflater.set_dictionary dictionary
1765  * inflater.inflate ''
1766  * end
1767  *
1768  * # ...
1769  *
1770  * inflater.close
1771  *
1772  * See also Zlib::Inflate.new
1773  */
1774 static VALUE
1776 {
1777  struct zstream *z = get_zstream(obj);
1778  VALUE dst;
1779 
1780  OBJ_INFECT(obj, src);
1781 
1782  if (ZSTREAM_IS_FINISHED(z)) {
1783  if (NIL_P(src)) {
1784  dst = zstream_detach_buffer(z);
1785  }
1786  else {
1787  StringValue(src);
1788  zstream_append_buffer2(z, src);
1789  dst = rb_str_new(0, 0);
1790  }
1791  }
1792  else {
1793  do_inflate(z, src);
1794  dst = zstream_detach_buffer(z);
1795  if (ZSTREAM_IS_FINISHED(z)) {
1797  }
1798  }
1799 
1800  OBJ_INFECT(dst, obj);
1801  return dst;
1802 }
1803 
1804 /*
1805  * call-seq: << string
1806  *
1807  * Inputs +string+ into the inflate stream just like Zlib::Inflate#inflate, but
1808  * returns the Zlib::Inflate object itself. The output from the stream is
1809  * preserved in output buffer.
1810  */
1811 static VALUE
1813 {
1814  struct zstream *z = get_zstream(obj);
1815 
1816  OBJ_INFECT(obj, src);
1817 
1818  if (ZSTREAM_IS_FINISHED(z)) {
1819  if (!NIL_P(src)) {
1820  StringValue(src);
1821  zstream_append_buffer2(z, src);
1822  }
1823  }
1824  else {
1825  do_inflate(z, src);
1826  if (ZSTREAM_IS_FINISHED(z)) {
1828  }
1829  }
1830 
1831  return obj;
1832 }
1833 
1834 /*
1835  * call-seq: sync(string)
1836  *
1837  * Inputs +string+ into the end of input buffer and skips data until a full
1838  * flush point can be found. If the point is found in the buffer, this method
1839  * flushes the buffer and returns false. Otherwise it returns +true+ and the
1840  * following data of full flush point is preserved in the buffer.
1841  */
1842 static VALUE
1844 {
1845  struct zstream *z = get_zstream(obj);
1846 
1847  OBJ_INFECT(obj, src);
1848  StringValue(src);
1849  return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src));
1850 }
1851 
1852 /*
1853  * Quoted verbatim from original documentation:
1854  *
1855  * What is this?
1856  *
1857  * <tt>:)</tt>
1858  */
1859 static VALUE
1861 {
1862  struct zstream *z = get_zstream(obj);
1863  int err;
1864 
1865  err = inflateSyncPoint(&z->stream);
1866  if (err == 1) {
1867  return Qtrue;
1868  }
1869  if (err != Z_OK) {
1870  raise_zlib_error(err, z->stream.msg);
1871  }
1872  return Qfalse;
1873 }
1874 
1875 /*
1876  * Document-method: Zlib::Inflate#set_dictionary
1877  *
1878  * Sets the preset dictionary and returns +string+. This method is available just
1879  * only after a Zlib::NeedDict exception was raised. See zlib.h for details.
1880  *
1881  */
1882 static VALUE
1884 {
1885  struct zstream *z = get_zstream(obj);
1886  VALUE src = dic;
1887  int err;
1888 
1889  OBJ_INFECT(obj, dic);
1890  StringValue(src);
1891  err = inflateSetDictionary(&z->stream,
1892  (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src));
1893  if (err != Z_OK) {
1894  raise_zlib_error(err, z->stream.msg);
1895  }
1896 
1897  return dic;
1898 }
1899 
1900 
1901 
1902 #if GZIP_SUPPORT
1903 
1904 /* NOTE: Features for gzip files of Ruby/zlib are written from scratch
1905  * and using undocumented feature of zlib, negative wbits.
1906  * I don't think gzFile APIs of zlib are good for Ruby.
1907  */
1908 
1909 /*------- .gz file header --------*/
1910 
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
1920 
1921 #define GZ_EXTRAFLAG_FAST 0x4
1922 #define GZ_EXTRAFLAG_SLOW 0x2
1923 
1924 /* from zutil.h */
1925 #define OS_MSDOS 0x00
1926 #define OS_AMIGA 0x01
1927 #define OS_VMS 0x02
1928 #define OS_UNIX 0x03
1929 #define OS_ATARI 0x05
1930 #define OS_OS2 0x06
1931 #define OS_MACOS 0x07
1932 #define OS_TOPS20 0x0a
1933 #define OS_WIN32 0x0b
1934 
1935 #define OS_VMCMS 0x04
1936 #define OS_ZSYSTEM 0x08
1937 #define OS_CPM 0x09
1938 #define OS_QDOS 0x0c
1939 #define OS_RISCOS 0x0d
1940 #define OS_UNKNOWN 0xff
1941 
1942 #ifndef OS_CODE
1943 #define OS_CODE OS_UNIX
1944 #endif
1945 
1948 
1949 
1950 
1951 /*-------- gzfile internal APIs --------*/
1952 
1953 struct gzfile {
1954  struct zstream z;
1956  int level;
1957  time_t mtime; /* for header */
1958  int os_code; /* for header */
1959  VALUE orig_name; /* for header; must be a String */
1960  VALUE comment; /* for header; must be a String */
1961  unsigned long crc;
1962  int lineno;
1963  long ungetc;
1964  void (*end)(struct gzfile *);
1968  int ecflags;
1970  char *cbuf;
1972 };
1973 #define GZFILE_CBUF_CAPA 10
1974 
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)
1978 
1979 #define GZFILE_IS_FINISHED(gz) \
1980  (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0)
1981 
1982 #define GZFILE_READ_SIZE 2048
1983 
1984 
1985 static void
1987 {
1988  rb_gc_mark(gz->io);
1989  rb_gc_mark(gz->orig_name);
1990  rb_gc_mark(gz->comment);
1991  zstream_mark(&gz->z);
1992  rb_gc_mark(gz->ecopts);
1993  rb_gc_mark(gz->path);
1994 }
1995 
1996 static void
1998 {
1999  struct zstream *z = &gz->z;
2000 
2001  if (ZSTREAM_IS_READY(z)) {
2002  if (z->func == &deflate_funcs) {
2003  finalizer_warn("Zlib::GzipWriter object must be closed explicitly.");
2004  }
2005  zstream_finalize(z);
2006  }
2007  if (gz->cbuf) {
2008  xfree(gz->cbuf);
2009  }
2010  xfree(gz);
2011 }
2012 
2013 static VALUE
2014 gzfile_new(klass, funcs, endfunc)
2015  VALUE klass;
2016  const struct zstream_funcs *funcs;
2017  void (*endfunc)(struct gzfile *);
2018 {
2019  VALUE obj;
2020  struct gzfile *gz;
2021 
2022  obj = Data_Make_Struct(klass, struct gzfile, gzfile_mark, gzfile_free, gz);
2023  zstream_init(&gz->z, funcs);
2024  gz->io = Qnil;
2025  gz->level = 0;
2026  gz->mtime = 0;
2029  gz->comment = Qnil;
2030  gz->crc = crc32(0, Z_NULL, 0);
2031  gz->lineno = 0;
2032  gz->ungetc = 0;
2033  gz->end = endfunc;
2035  gz->enc2 = 0;
2036  gz->ec = NULL;
2037  gz->ecflags = 0;
2038  gz->ecopts = Qnil;
2039  gz->cbuf = 0;
2040  gz->path = Qnil;
2041 
2042  return obj;
2043 }
2044 
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)
2047 
2048 static void
2050 {
2051  zstream_reset(&gz->z);
2052  gz->crc = crc32(0, Z_NULL, 0);
2053  gz->lineno = 0;
2054  gz->ungetc = 0;
2055  if (gz->ec) {
2056  rb_econv_close(gz->ec);
2057  gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name,
2058  gz->ecflags, gz->ecopts);
2059  }
2060 }
2061 
2062 static void
2063 gzfile_close(struct gzfile *gz, int closeflag)
2064 {
2065  VALUE io = gz->io;
2066 
2067  gz->end(gz);
2068  gz->io = Qnil;
2069  gz->orig_name = Qnil;
2070  gz->comment = Qnil;
2071  if (closeflag && rb_respond_to(io, id_close)) {
2072  rb_funcall(io, id_close, 0);
2073  }
2074 }
2075 
2076 static void
2078 {
2079  VALUE str;
2080 
2081  if (gz->z.buf_filled > 0) {
2082  str = zstream_detach_buffer(&gz->z);
2083  OBJ_TAINT(str); /* for safe */
2084  rb_funcall(gz->io, id_write, 1, str);
2085  if ((gz->z.flags & GZFILE_FLAG_SYNC)
2086  && rb_respond_to(gz->io, id_flush))
2087  rb_funcall(gz->io, id_flush, 0);
2088  }
2089 }
2090 
2091 static VALUE
2093 {
2094  struct gzfile *gz = (struct gzfile*)arg;
2095  VALUE str;
2096 
2097  str = rb_funcall(gz->io, id_readpartial, 1, INT2FIX(GZFILE_READ_SIZE));
2098  Check_Type(str, T_STRING);
2099  return str;
2100 }
2101 
2102 static VALUE
2104 {
2105  struct gzfile *gz = (struct gzfile*)arg;
2106  VALUE str = Qnil;
2108  str = rb_funcall(gz->io, id_read, 1, INT2FIX(GZFILE_READ_SIZE));
2109  if (!NIL_P(str)) {
2110  Check_Type(str, T_STRING);
2111  }
2112  }
2113  return str; /* return nil when EOFError */
2114 }
2115 
2116 static VALUE
2118 {
2122 }
2123 
2124 static int
2126 {
2127  VALUE str;
2128 
2129  while (NIL_P(gz->z.input) || RSTRING_LEN(gz->z.input) < size) {
2130  str = gzfile_read_raw(gz);
2131  if (NIL_P(str)) return 0;
2132  zstream_append_input2(&gz->z, str);
2133  }
2134  return 1;
2135 }
2136 
2137 static char *
2138 gzfile_read_raw_until_zero(struct gzfile *gz, long offset)
2139 {
2140  VALUE str;
2141  char *p;
2142 
2143  for (;;) {
2144  p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0',
2145  RSTRING_LEN(gz->z.input) - offset);
2146  if (p) break;
2147  str = gzfile_read_raw(gz);
2148  if (NIL_P(str)) {
2149  rb_raise(cGzError, "unexpected end of file");
2150  }
2151  offset = RSTRING_LEN(gz->z.input);
2152  zstream_append_input2(&gz->z, str);
2153  }
2154  return p;
2155 }
2156 
2157 static unsigned int
2158 gzfile_get16(const unsigned char *src)
2159 {
2160  unsigned int n;
2161  n = *(src++) & 0xff;
2162  n |= (*(src++) & 0xff) << 8;
2163  return n;
2164 }
2165 
2166 static unsigned long
2167 gzfile_get32(const unsigned char *src)
2168 {
2169  unsigned long n;
2170  n = *(src++) & 0xff;
2171  n |= (*(src++) & 0xff) << 8;
2172  n |= (*(src++) & 0xff) << 16;
2173  n |= (*(src++) & 0xffU) << 24;
2174  return n;
2175 }
2176 
2177 static void
2178 gzfile_set32(unsigned long n, unsigned char *dst)
2179 {
2180  *(dst++) = n & 0xff;
2181  *(dst++) = (n >> 8) & 0xff;
2182  *(dst++) = (n >> 16) & 0xff;
2183  *dst = (n >> 24) & 0xff;
2184 }
2185 
2186 static void
2187 gzfile_raise(struct gzfile *gz, VALUE klass, const char *message)
2188 {
2189  VALUE exc = rb_exc_new2(klass, message);
2190  if (!NIL_P(gz->z.input)) {
2191  rb_ivar_set(exc, id_input, rb_str_resurrect(gz->z.input));
2192  }
2193  rb_exc_raise(exc);
2194 }
2195 
2196 /*
2197  * Document-method: Zlib::GzipFile::Error#inspect
2198  *
2199  * Constructs a String of the GzipFile Error
2200  */
2201 static VALUE
2203 {
2204  VALUE str = rb_call_super(0, 0);
2205  VALUE input = rb_attr_get(error, id_input);
2206 
2207  if (!NIL_P(input)) {
2208  rb_str_resize(str, RSTRING_LEN(str)-1);
2209  rb_str_cat2(str, ", input=");
2210  rb_str_append(str, rb_str_inspect(input));
2211  rb_str_cat2(str, ">");
2212  }
2213  return str;
2214 }
2215 
2216 static void
2218 {
2219  Bytef buf[10]; /* the size of gzip header */
2220  unsigned char flags = 0, extraflags = 0;
2221 
2222  if (!NIL_P(gz->orig_name)) {
2223  flags |= GZ_FLAG_ORIG_NAME;
2224  }
2225  if (!NIL_P(gz->comment)) {
2226  flags |= GZ_FLAG_COMMENT;
2227  }
2228  if (gz->mtime == 0) {
2229  gz->mtime = time(0);
2230  }
2231 
2232  if (gz->level == Z_BEST_SPEED) {
2233  extraflags |= GZ_EXTRAFLAG_FAST;
2234  }
2235  else if (gz->level == Z_BEST_COMPRESSION) {
2236  extraflags |= GZ_EXTRAFLAG_SLOW;
2237  }
2238 
2239  buf[0] = GZ_MAGIC1;
2240  buf[1] = GZ_MAGIC2;
2241  buf[2] = GZ_METHOD_DEFLATE;
2242  buf[3] = flags;
2243  gzfile_set32((unsigned long)gz->mtime, &buf[4]);
2244  buf[8] = extraflags;
2245  buf[9] = gz->os_code;
2246  zstream_append_buffer(&gz->z, buf, sizeof(buf));
2247 
2248  if (!NIL_P(gz->orig_name)) {
2249  zstream_append_buffer2(&gz->z, gz->orig_name);
2250  zstream_append_buffer(&gz->z, (Bytef*)"\0", 1);
2251  }
2252  if (!NIL_P(gz->comment)) {
2253  zstream_append_buffer2(&gz->z, gz->comment);
2254  zstream_append_buffer(&gz->z, (Bytef*)"\0", 1);
2255  }
2256 
2258 }
2259 
2260 static void
2262 {
2263  Bytef buf[8]; /* 8 is the size of gzip footer */
2264 
2265  gzfile_set32(gz->crc, buf);
2266  gzfile_set32(gz->z.stream.total_in, &buf[4]);
2267  zstream_append_buffer(&gz->z, buf, sizeof(buf));
2269 }
2270 
2271 static void
2273 {
2274  const unsigned char *head;
2275  long len;
2276  char flags, *p;
2277 
2278  if (!gzfile_read_raw_ensure(gz, 10)) { /* 10 is the size of gzip header */
2279  gzfile_raise(gz, cGzError, "not in gzip format");
2280  }
2281 
2282  head = (unsigned char*)RSTRING_PTR(gz->z.input);
2283 
2284  if (head[0] != GZ_MAGIC1 || head[1] != GZ_MAGIC2) {
2285  gzfile_raise(gz, cGzError, "not in gzip format");
2286  }
2287  if (head[2] != GZ_METHOD_DEFLATE) {
2288  rb_raise(cGzError, "unsupported compression method %d", head[2]);
2289  }
2290 
2291  flags = head[3];
2292  if (flags & GZ_FLAG_MULTIPART) {
2293  rb_raise(cGzError, "multi-part gzip file is not supported");
2294  }
2295  else if (flags & GZ_FLAG_ENCRYPT) {
2296  rb_raise(cGzError, "encrypted gzip file is not supported");
2297  }
2298  else if (flags & GZ_FLAG_UNKNOWN_MASK) {
2299  rb_raise(cGzError, "unknown flags 0x%02x", flags);
2300  }
2301 
2302  if (head[8] & GZ_EXTRAFLAG_FAST) {
2303  gz->level = Z_BEST_SPEED;
2304  }
2305  else if (head[8] & GZ_EXTRAFLAG_SLOW) {
2306  gz->level = Z_BEST_COMPRESSION;
2307  }
2308  else {
2309  gz->level = Z_DEFAULT_COMPRESSION;
2310  }
2311 
2312  gz->mtime = gzfile_get32(&head[4]);
2313  gz->os_code = head[9];
2314  zstream_discard_input(&gz->z, 10);
2315 
2316  if (flags & GZ_FLAG_EXTRA) {
2317  if (!gzfile_read_raw_ensure(gz, 2)) {
2318  rb_raise(cGzError, "unexpected end of file");
2319  }
2320  len = gzfile_get16((Bytef*)RSTRING_PTR(gz->z.input));
2321  if (!gzfile_read_raw_ensure(gz, 2 + len)) {
2322  rb_raise(cGzError, "unexpected end of file");
2323  }
2324  zstream_discard_input(&gz->z, 2 + len);
2325  }
2326  if (flags & GZ_FLAG_ORIG_NAME) {
2327  if (!gzfile_read_raw_ensure(gz, 1)) {
2328  rb_raise(cGzError, "unexpected end of file");
2329  }
2330  p = gzfile_read_raw_until_zero(gz, 0);
2331  len = p - RSTRING_PTR(gz->z.input);
2332  gz->orig_name = rb_str_new(RSTRING_PTR(gz->z.input), len);
2333  OBJ_TAINT(gz->orig_name); /* for safe */
2334  zstream_discard_input(&gz->z, len + 1);
2335  }
2336  if (flags & GZ_FLAG_COMMENT) {
2337  if (!gzfile_read_raw_ensure(gz, 1)) {
2338  rb_raise(cGzError, "unexpected end of file");
2339  }
2340  p = gzfile_read_raw_until_zero(gz, 0);
2341  len = p - RSTRING_PTR(gz->z.input);
2342  gz->comment = rb_str_new(RSTRING_PTR(gz->z.input), len);
2343  OBJ_TAINT(gz->comment); /* for safe */
2344  zstream_discard_input(&gz->z, len + 1);
2345  }
2346 
2347  if (gz->z.input != Qnil && RSTRING_LEN(gz->z.input) > 0) {
2348  zstream_run(&gz->z, 0, 0, Z_SYNC_FLUSH);
2349  }
2350 }
2351 
2352 static void
2354 {
2355  unsigned long crc, length;
2356 
2358 
2359  if (!gzfile_read_raw_ensure(gz, 8)) { /* 8 is the size of gzip footer */
2360  gzfile_raise(gz, cNoFooter, "footer is not found");
2361  }
2362 
2363  crc = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input));
2364  length = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input) + 4);
2365 
2366  gz->z.stream.total_in += 8; /* to rewind correctly */
2367  zstream_discard_input(&gz->z, 8);
2368 
2369  if (gz->crc != crc) {
2370  rb_raise(cCRCError, "invalid compressed data -- crc error");
2371  }
2372  if ((uint32_t)gz->z.stream.total_out != length) {
2373  rb_raise(cLengthError, "invalid compressed data -- length error");
2374  }
2375 }
2376 
2377 static void
2378 gzfile_write(struct gzfile *gz, Bytef *str, long len)
2379 {
2380  if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2381  gzfile_make_header(gz);
2382  }
2383 
2384  if (len > 0 || (gz->z.flags & GZFILE_FLAG_SYNC)) {
2385  gz->crc = checksum_long(crc32, gz->crc, str, len);
2386  zstream_run(&gz->z, str, len, (gz->z.flags & GZFILE_FLAG_SYNC)
2387  ? Z_SYNC_FLUSH : Z_NO_FLUSH);
2388  }
2389  gzfile_write_raw(gz);
2390 }
2391 
2392 static long
2394 {
2395  volatile VALUE str;
2396 
2397  while (!ZSTREAM_IS_FINISHED(&gz->z)) {
2398  str = gzfile_read_raw(gz);
2399  if (NIL_P(str)) {
2400  if (!ZSTREAM_IS_FINISHED(&gz->z)) {
2401  rb_raise(cGzError, "unexpected end of file");
2402  }
2403  break;
2404  }
2405  if (RSTRING_LEN(str) > 0) { /* prevent Z_BUF_ERROR */
2406  zstream_run(&gz->z, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str),
2407  Z_SYNC_FLUSH);
2408  }
2409  if (gz->z.buf_filled > 0) break;
2410  }
2411  return gz->z.buf_filled;
2412 }
2413 
2414 static void
2416 {
2417  if (RSTRING_LEN(str) <= gz->ungetc) {
2418  gz->ungetc -= RSTRING_LEN(str);
2419  }
2420  else {
2421  gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc,
2422  RSTRING_LEN(str) - gz->ungetc);
2423  gz->ungetc = 0;
2424  }
2425 }
2426 
2427 static VALUE
2429 {
2430  if (!gz->enc2) {
2431  rb_enc_associate(str, gz->enc);
2432  OBJ_TAINT(str); /* for safe */
2433  return str;
2434  }
2435  if (gz->ec && rb_enc_dummy_p(gz->enc2)) {
2436  str = rb_econv_str_convert(gz->ec, str, ECONV_PARTIAL_INPUT);
2437  rb_enc_associate(str, gz->enc);
2438  OBJ_TAINT(str);
2439  return str;
2440  }
2441  return rb_str_conv_enc_opts(str, gz->enc2, gz->enc,
2442  gz->ecflags, gz->ecopts);
2443 }
2444 
2445 static long
2446 gzfile_fill(struct gzfile *gz, long len)
2447 {
2448  if (len < 0)
2449  rb_raise(rb_eArgError, "negative length %ld given", len);
2450  if (len == 0)
2451  return 0;
2452  while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) {
2453  gzfile_read_more(gz);
2454  }
2455  if (GZFILE_IS_FINISHED(gz)) {
2456  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2457  gzfile_check_footer(gz);
2458  }
2459  return -1;
2460  }
2461  return len < gz->z.buf_filled ? len : gz->z.buf_filled;
2462 }
2463 
2464 static VALUE
2465 gzfile_read(struct gzfile *gz, long len)
2466 {
2467  VALUE dst;
2468 
2469  len = gzfile_fill(gz, len);
2470  if (len == 0) return rb_str_new(0, 0);
2471  if (len < 0) return Qnil;
2472  dst = zstream_shift_buffer(&gz->z, len);
2473  gzfile_calc_crc(gz, dst);
2474  return dst;
2475 }
2476 
2477 static VALUE
2478 gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
2479 {
2480  VALUE dst;
2481 
2482  if (len < 0)
2483  rb_raise(rb_eArgError, "negative length %ld given", len);
2484 
2485  if (!NIL_P(outbuf))
2486  OBJ_TAINT(outbuf);
2487 
2488  if (len == 0) {
2489  if (NIL_P(outbuf))
2490  return rb_str_new(0, 0);
2491  else {
2492  rb_str_resize(outbuf, 0);
2493  return outbuf;
2494  }
2495  }
2496  while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled == 0) {
2497  gzfile_read_more(gz);
2498  }
2499  if (GZFILE_IS_FINISHED(gz)) {
2500  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2501  gzfile_check_footer(gz);
2502  }
2503  if (!NIL_P(outbuf))
2504  rb_str_resize(outbuf, 0);
2505  rb_raise(rb_eEOFError, "end of file reached");
2506  }
2507 
2508  dst = zstream_shift_buffer(&gz->z, len);
2509  gzfile_calc_crc(gz, dst);
2510 
2511  if (!NIL_P(outbuf)) {
2512  rb_str_resize(outbuf, RSTRING_LEN(dst));
2513  memcpy(RSTRING_PTR(outbuf), RSTRING_PTR(dst), RSTRING_LEN(dst));
2514  dst = outbuf;
2515  }
2516  OBJ_TAINT(dst); /* for safe */
2517  return dst;
2518 }
2519 
2520 static VALUE
2522 {
2523  VALUE dst;
2524 
2525  while (!ZSTREAM_IS_FINISHED(&gz->z)) {
2526  gzfile_read_more(gz);
2527  }
2528  if (GZFILE_IS_FINISHED(gz)) {
2529  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2530  gzfile_check_footer(gz);
2531  }
2532  return rb_str_new(0, 0);
2533  }
2534 
2535  dst = zstream_detach_buffer(&gz->z);
2536  gzfile_calc_crc(gz, dst);
2537  OBJ_TAINT(dst);
2538  return gzfile_newstr(gz, dst);
2539 }
2540 
2541 static VALUE
2543 {
2544  VALUE buf, dst = 0;
2545  int len;
2546 
2547  len = rb_enc_mbmaxlen(gz->enc);
2548  while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) {
2549  gzfile_read_more(gz);
2550  }
2551  if (GZFILE_IS_FINISHED(gz)) {
2552  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2553  gzfile_check_footer(gz);
2554  }
2555  return Qnil;
2556  }
2557 
2558  if (gz->ec && rb_enc_dummy_p(gz->enc2)) {
2559  const unsigned char *ss, *sp, *se;
2560  unsigned char *ds, *dp, *de;
2562 
2563  if (!gz->cbuf) {
2564  gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA);
2565  }
2566  ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf);
2567  se = sp + gz->z.buf_filled;
2568  ds = dp = (unsigned char *)gz->cbuf;
2569  de = (unsigned char *)ds + GZFILE_CBUF_CAPA;
2570  res = rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT);
2571  rb_econv_check_error(gz->ec);
2572  dst = zstream_shift_buffer(&gz->z, sp - ss);
2573  gzfile_calc_crc(gz, dst);
2574  dst = rb_str_new(gz->cbuf, dp - ds);
2575  rb_enc_associate(dst, gz->enc);
2576  OBJ_TAINT(dst);
2577  return dst;
2578  }
2579  else {
2580  buf = gz->z.buf;
2581  len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), gz->enc);
2582  dst = gzfile_read(gz, len);
2583  return gzfile_newstr(gz, dst);
2584  }
2585 }
2586 
2587 static void
2588 gzfile_ungets(struct gzfile *gz, const Bytef *b, long len)
2589 {
2590  zstream_buffer_ungets(&gz->z, b, len);
2591  gz->ungetc+=len;
2592 }
2593 
2594 static void
2596 {
2597  zstream_buffer_ungetbyte(&gz->z, c);
2598  gz->ungetc++;
2599 }
2600 
2601 static VALUE
2603 {
2604  struct gzfile *gz = (struct gzfile *)arg;
2605 
2606  if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) {
2607  gzfile_make_header(gz);
2608  }
2609 
2610  zstream_run(&gz->z, (Bytef*)"", 0, Z_FINISH);
2611  gzfile_make_footer(gz);
2612  gzfile_write_raw(gz);
2613 
2614  return Qnil;
2615 }
2616 
2617 static void
2619 {
2620  if (ZSTREAM_IS_CLOSING(&gz->z)) return;
2621  gz->z.flags |= ZSTREAM_FLAG_CLOSING;
2622 
2624 }
2625 
2626 static VALUE
2628 {
2629  struct gzfile *gz = (struct gzfile *)arg;
2630 
2631  if (GZFILE_IS_FINISHED(gz)
2632  && !(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2633  gzfile_check_footer(gz);
2634  }
2635 
2636  return Qnil;
2637 }
2638 
2639 static void
2641 {
2642  if (ZSTREAM_IS_CLOSING(&gz->z)) return;
2643  gz->z.flags |= ZSTREAM_FLAG_CLOSING;
2644 
2646 }
2647 
2648 static void
2650 {
2651  long n;
2652 
2653  n = gz->z.stream.total_in;
2654  if (!NIL_P(gz->z.input)) {
2655  n += RSTRING_LEN(gz->z.input);
2656  }
2657 
2658  rb_funcall(gz->io, id_seek, 2, rb_int2inum(-n), INT2FIX(1));
2659  gzfile_reset(gz);
2660 }
2661 
2662 static VALUE
2664 {
2665  VALUE str;
2666 
2667  if (!ZSTREAM_IS_READY(&gz->z)) return Qnil;
2668  if (!GZFILE_IS_FINISHED(gz)) return Qnil;
2669  if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) {
2670  gzfile_check_footer(gz);
2671  }
2672  if (NIL_P(gz->z.input)) return Qnil;
2673 
2674  str = rb_str_resurrect(gz->z.input);
2675  OBJ_TAINT(str); /* for safe */
2676  return str;
2677 }
2678 
2679 static struct gzfile *
2681 {
2682  struct gzfile *gz;
2683 
2684  Data_Get_Struct(obj, struct gzfile, gz);
2685  if (!ZSTREAM_IS_READY(&gz->z)) {
2686  rb_raise(cGzError, "closed gzip stream");
2687  }
2688  return gz;
2689 }
2690 
2691 
2692 /* ------------------------------------------------------------------------- */
2693 
2694 /*
2695  * Document-class: Zlib::GzipFile
2696  *
2697  * Zlib::GzipFile is an abstract class for handling a gzip formatted
2698  * compressed file. The operations are defined in the subclasses,
2699  * Zlib::GzipReader for reading, and Zlib::GzipWriter for writing.
2700  *
2701  * GzipReader should be used by associating an IO, or IO-like, object.
2702  *
2703  * == Method Catalogue
2704  *
2705  * - ::wrap
2706  * - ::open (Zlib::GzipReader::open and Zlib::GzipWriter::open)
2707  * - #close
2708  * - #closed?
2709  * - #comment
2710  * - comment= (Zlib::GzipWriter#comment=)
2711  * - #crc
2712  * - eof? (Zlib::GzipReader#eof?)
2713  * - #finish
2714  * - #level
2715  * - lineno (Zlib::GzipReader#lineno)
2716  * - lineno= (Zlib::GzipReader#lineno=)
2717  * - #mtime
2718  * - mtime= (Zlib::GzipWriter#mtime=)
2719  * - #orig_name
2720  * - orig_name (Zlib::GzipWriter#orig_name=)
2721  * - #os_code
2722  * - path (when the underlying IO supports #path)
2723  * - #sync
2724  * - #sync=
2725  * - #to_io
2726  *
2727  * (due to internal structure, documentation may appear under Zlib::GzipReader
2728  * or Zlib::GzipWriter)
2729  */
2730 
2731 
2732 typedef struct {
2733  int argc;
2736 } new_wrap_arg_t;
2737 
2738 static VALUE
2740 {
2741  new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp;
2742  return rb_class_new_instance(arg->argc, arg->argv, arg->klass);
2743 }
2744 
2745 static VALUE
2747 {
2748  struct gzfile *gz;
2749 
2750  Data_Get_Struct(obj, struct gzfile, gz);
2751  if (ZSTREAM_IS_READY(&gz->z)) {
2752  gzfile_close(gz, 1);
2753  }
2754  return Qnil;
2755 }
2756 
2757 static VALUE
2758 gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error)
2759 {
2760  VALUE obj;
2761 
2762  if (close_io_on_error) {
2763  int state = 0;
2765  arg.argc = argc;
2766  arg.argv = argv;
2767  arg.klass = klass;
2768  obj = rb_protect(new_wrap, (VALUE)&arg, &state);
2769  if (state) {
2770  rb_io_close(argv[0]);
2771  rb_jump_tag(state);
2772  }
2773  }
2774  else {
2775  obj = rb_class_new_instance(argc, argv, klass);
2776  }
2777 
2778  if (rb_block_given_p()) {
2779  return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj);
2780  }
2781  else {
2782  return obj;
2783  }
2784 }
2785 
2786 /*
2787  * Document-method: Zlib::GzipFile.wrap
2788  *
2789  * call-seq: Zlib::GzipFile.wrap(io) { |gz| ... }
2790  *
2791  * Creates a GzipFile object associated with +io+, and
2792  * executes the block with the newly created GzipFile object,
2793  * just like File.open. The GzipFile object will be closed
2794  * automatically after executing the block. If you want to keep
2795  * the associated IO object opening, you may call
2796  * +Zlib::GzipFile#finish+ method in the block.
2797  */
2798 static VALUE
2799 rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass)
2800 {
2801  return gzfile_wrap(argc, argv, klass, 0);
2802 }
2803 
2804 /*
2805  * Document-method: Zlib::GzipFile.open
2806  *
2807  * See Zlib::GzipReader#open and Zlib::GzipWriter#open.
2808  */
2809 static VALUE
2810 gzfile_s_open(int argc, VALUE *argv, VALUE klass, const char *mode)
2811 {
2812  VALUE io, filename;
2813 
2814  if (argc < 1) {
2815  rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)");
2816  }
2817  filename = argv[0];
2818  io = rb_file_open_str(filename, mode);
2819  argv[0] = io;
2820  return gzfile_wrap(argc, argv, klass, 1);
2821 }
2822 
2823 /*
2824  * Document-method: Zlib::GzipFile#to_io
2825  *
2826  * Same as IO.
2827  */
2828 static VALUE
2830 {
2831  return get_gzfile(obj)->io;
2832 }
2833 
2834 /*
2835  * Document-method: Zlib::GzipFile#crc
2836  *
2837  * Returns CRC value of the uncompressed data.
2838  */
2839 static VALUE
2841 {
2842  return rb_uint2inum(get_gzfile(obj)->crc);
2843 }
2844 
2845 /*
2846  * Document-method: Zlib::GzipFile#mtime
2847  *
2848  * Returns last modification time recorded in the gzip file header.
2849  */
2850 static VALUE
2852 {
2853  return rb_time_new(get_gzfile(obj)->mtime, (time_t)0);
2854 }
2855 
2856 /*
2857  * Document-method: Zlib::GzipFile#level
2858  *
2859  * Returns compression level.
2860  */
2861 static VALUE
2863 {
2864  return INT2FIX(get_gzfile(obj)->level);
2865 }
2866 
2867 /*
2868  * Document-method: Zlib::GzipFile#os_code
2869  *
2870  * Returns OS code number recorded in the gzip file header.
2871  */
2872 static VALUE
2874 {
2875  return INT2FIX(get_gzfile(obj)->os_code);
2876 }
2877 
2878 /*
2879  * Document-method: Zlib::GzipFile#orig_name
2880  *
2881  * Returns original filename recorded in the gzip file header, or +nil+ if
2882  * original filename is not present.
2883  */
2884 static VALUE
2886 {
2887  VALUE str = get_gzfile(obj)->orig_name;
2888  if (!NIL_P(str)) {
2889  str = rb_str_dup(str);
2890  }
2891  OBJ_TAINT(str); /* for safe */
2892  return str;
2893 }
2894 
2895 /*
2896  * Document-method: Zlib::GzipFile#comment
2897  *
2898  * Returns comments recorded in the gzip file header, or nil if the comments
2899  * is not present.
2900  */
2901 static VALUE
2903 {
2904  VALUE str = get_gzfile(obj)->comment;
2905  if (!NIL_P(str)) {
2906  str = rb_str_dup(str);
2907  }
2908  OBJ_TAINT(str); /* for safe */
2909  return str;
2910 }
2911 
2912 /*
2913  * Document-method: Zlib::GzipFile#lineno
2914  *
2915  * The line number of the last row read from this file.
2916  */
2917 static VALUE
2919 {
2920  return INT2NUM(get_gzfile(obj)->lineno);
2921 }
2922 
2923 /*
2924  * Document-method: Zlib::GzipReader#lineno=
2925  *
2926  * Specify line number of the last row read from this file.
2927  */
2928 static VALUE
2930 {
2931  struct gzfile *gz = get_gzfile(obj);
2932  gz->lineno = NUM2INT(lineno);
2933  return lineno;
2934 }
2935 
2936 /*
2937  * Document-method: Zlib::GzipWriter#mtime=
2938  *
2939  * Specify the modification time (+mtime+) in the gzip header.
2940  * Using a Fixnum or Integer
2941  */
2942 static VALUE
2944 {
2945  struct gzfile *gz = get_gzfile(obj);
2946  VALUE val;
2947 
2948  if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) {
2949  rb_raise(cGzError, "header is already written");
2950  }
2951 
2952  if (FIXNUM_P(mtime)) {
2953  gz->mtime = FIX2INT(mtime);
2954  }
2955  else {
2956  val = rb_Integer(mtime);
2957  gz->mtime = FIXNUM_P(val) ? FIX2UINT(val) : rb_big2ulong(val);
2958  }
2959  return mtime;
2960 }
2961 
2962 /*
2963  * Document-method: Zlib::GzipFile#orig_name=
2964  *
2965  * Specify the original name (+str+) in the gzip header.
2966  */
2967 static VALUE
2969 {
2970  struct gzfile *gz = get_gzfile(obj);
2971  VALUE s;
2972  char *p;
2973 
2974  if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) {
2975  rb_raise(cGzError, "header is already written");
2976  }
2977  s = rb_str_dup(rb_str_to_str(str));
2978  p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s));
2979  if (p) {
2980  rb_str_resize(s, p - RSTRING_PTR(s));
2981  }
2982  gz->orig_name = s;
2983  return str;
2984 }
2985 
2986 /*
2987  * Document-method: Zlib::GzipFile#comment=
2988  *
2989  * Specify the comment (+str+) in the gzip header.
2990  */
2991 static VALUE
2993 {
2994  struct gzfile *gz = get_gzfile(obj);
2995  VALUE s;
2996  char *p;
2997 
2998  if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) {
2999  rb_raise(cGzError, "header is already written");
3000  }
3001  s = rb_str_dup(rb_str_to_str(str));
3002  p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s));
3003  if (p) {
3004  rb_str_resize(s, p - RSTRING_PTR(s));
3005  }
3006  gz->comment = s;
3007  return str;
3008 }
3009 
3010 /*
3011  * Document-method: Zlib::GzipFile#close
3012  *
3013  * Closes the GzipFile object. This method calls close method of the
3014  * associated IO object. Returns the associated IO object.
3015  */
3016 static VALUE
3018 {
3019  struct gzfile *gz = get_gzfile(obj);
3020  VALUE io;
3021 
3022  io = gz->io;
3023  gzfile_close(gz, 1);
3024  return io;
3025 }
3026 
3027 /*
3028  * Document-method: Zlib::GzipFile#finish
3029  *
3030  * Closes the GzipFile object. Unlike Zlib::GzipFile#close, this method never
3031  * calls the close method of the associated IO object. Returns the associated IO
3032  * object.
3033  */
3034 static VALUE
3036 {
3037  struct gzfile *gz = get_gzfile(obj);
3038  VALUE io;
3039 
3040  io = gz->io;
3041  gzfile_close(gz, 0);
3042  return io;
3043 }
3044 
3045 /*
3046  * Document-method: Zlib::GzipFile#closed?
3047  *
3048  * Same as IO#closed?
3049  *
3050  */
3051 static VALUE
3053 {
3054  struct gzfile *gz;
3055  Data_Get_Struct(obj, struct gzfile, gz);
3056  return NIL_P(gz->io) ? Qtrue : Qfalse;
3057 }
3058 
3059 /*
3060  * Document-method: Zlib::GzipFile#eof?
3061  *
3062  * Returns +true+ or +false+ whether the stream has reached the end.
3063  */
3064 static VALUE
3066 {
3067  struct gzfile *gz = get_gzfile(obj);
3068  return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse;
3069 }
3070 
3071 /*
3072  * Document-method: Zlib::GzipFile#sync
3073  *
3074  * Same as IO#sync
3075  *
3076  */
3077 static VALUE
3079 {
3080  return (get_gzfile(obj)->z.flags & GZFILE_FLAG_SYNC) ? Qtrue : Qfalse;
3081 }
3082 
3083 /*
3084  * Document-method: Zlib::GzipFile#sync=
3085  *
3086  * call-seq: sync = flag
3087  *
3088  * Same as IO. If flag is +true+, the associated IO object must respond to the
3089  * +flush+ method. While +sync+ mode is +true+, the compression ratio
3090  * decreases sharply.
3091  */
3092 static VALUE
3094 {
3095  struct gzfile *gz = get_gzfile(obj);
3096 
3097  if (RTEST(mode)) {
3098  gz->z.flags |= GZFILE_FLAG_SYNC;
3099  }
3100  else {
3101  gz->z.flags &= ~GZFILE_FLAG_SYNC;
3102  }
3103  return mode;
3104 }
3105 
3106 /*
3107  * Document-method: Zlib::GzipFile#total_in
3108  *
3109  * Total number of input bytes read so far.
3110  */
3111 static VALUE
3113 {
3114  return rb_uint2inum(get_gzfile(obj)->z.stream.total_in);
3115 }
3116 
3117 /*
3118  * Document-method: Zlib::GzipFile#total_out
3119  *
3120  * Total number of output bytes output so far.
3121  */
3122 static VALUE
3124 {
3125  struct gzfile *gz = get_gzfile(obj);
3126  return rb_uint2inum(gz->z.stream.total_out - gz->z.buf_filled);
3127 }
3128 
3129 /*
3130  * Document-method: Zlib::GzipFile#path
3131  *
3132  * call-seq: path
3133  *
3134  * Returns the path string of the associated IO-like object. This
3135  * method is only defined when the IO-like object responds to #path().
3136  */
3137 static VALUE
3139 {
3140  struct gzfile *gz;
3141  Data_Get_Struct(obj, struct gzfile, gz);
3142  return gz->path;
3143 }
3144 
3145 static void
3147 {
3148  if (!NIL_P(opts)) {
3149  rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2, NULL);
3150  }
3151  if (gz->enc2) {
3152  gz->ecflags = rb_econv_prepare_opts(opts, &opts);
3153  gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name,
3154  gz->ecflags, opts);
3155  gz->ecopts = opts;
3156  }
3157 }
3158 
3159 /* ------------------------------------------------------------------------- */
3160 
3161 /*
3162  * Document-class: Zlib::GzipWriter
3163  *
3164  * Zlib::GzipWriter is a class for writing gzipped files. GzipWriter should
3165  * be used with an instance of IO, or IO-like, object.
3166  *
3167  * Following two example generate the same result.
3168  *
3169  * Zlib::GzipWriter.open('hoge.gz') do |gz|
3170  * gz.write 'jugemu jugemu gokou no surikire...'
3171  * end
3172  *
3173  * File.open('hoge.gz', 'w') do |f|
3174  * gz = Zlib::GzipWriter.new(f)
3175  * gz.write 'jugemu jugemu gokou no surikire...'
3176  * gz.close
3177  * end
3178  *
3179  * To make like gzip(1) does, run following:
3180  *
3181  * orig = 'hoge.txt'
3182  * Zlib::GzipWriter.open('hoge.gz') do |gz|
3183  * gz.mtime = File.mtime(orig)
3184  * gz.orig_name = orig
3185  * gz.write IO.binread(orig)
3186  * end
3187  *
3188  * NOTE: Due to the limitation of Ruby's finalizer, you must explicitly close
3189  * GzipWriter objects by Zlib::GzipWriter#close etc. Otherwise, GzipWriter
3190  * will be not able to write the gzip footer and will generate a broken gzip
3191  * file.
3192  */
3193 
3194 static VALUE
3196 {
3197  return gzfile_writer_new(klass);
3198 }
3199 
3200 /*
3201  * call-seq: Zlib::GzipWriter.open(filename, level=nil, strategy=nil) { |gz| ... }
3202  *
3203  * Opens a file specified by +filename+ for writing gzip compressed data, and
3204  * returns a GzipWriter object associated with that file. Further details of
3205  * this method are found in Zlib::GzipWriter.new and Zlib::GzipFile.wrap.
3206  */
3207 static VALUE
3208 rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass)
3209 {
3210  return gzfile_s_open(argc, argv, klass, "wb");
3211 }
3212 
3213 /*
3214  * call-seq: Zlib::GzipWriter.new(io, level, strategy)
3215  *
3216  * Creates a GzipWriter object associated with +io+. +level+ and +strategy+
3217  * should be the same as the arguments of Zlib::Deflate.new. The GzipWriter
3218  * object writes gzipped data to +io+. At least, +io+ must respond to the
3219  * +write+ method that behaves same as write method in IO class.
3220  */
3221 static VALUE
3223 {
3224  struct gzfile *gz;
3225  VALUE io, level, strategy, opt = Qnil;
3226  int err;
3227 
3228  if (argc > 1) {
3229  opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash");
3230  if (!NIL_P(opt)) argc--;
3231  }
3232 
3233  rb_scan_args(argc, argv, "12", &io, &level, &strategy);
3234  Data_Get_Struct(obj, struct gzfile, gz);
3235 
3236  /* this is undocumented feature of zlib */
3237  gz->level = ARG_LEVEL(level);
3238  err = deflateInit2(&gz->z.stream, gz->level, Z_DEFLATED,
3239  -MAX_WBITS, DEF_MEM_LEVEL, ARG_STRATEGY(strategy));
3240  if (err != Z_OK) {
3241  raise_zlib_error(err, gz->z.stream.msg);
3242  }
3243  gz->io = io;
3244  ZSTREAM_READY(&gz->z);
3245  rb_gzfile_ecopts(gz, opt);
3246 
3247  if (rb_respond_to(io, id_path)) {
3248  gz->path = rb_funcall(gz->io, id_path, 0);
3249  rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
3250  }
3251 
3252  return obj;
3253 }
3254 
3255 /*
3256  * call-seq: flush(flush=nil)
3257  *
3258  * Flushes all the internal buffers of the GzipWriter object. The meaning of
3259  * +flush+ is same as in Zlib::Deflate#deflate. <tt>Zlib::SYNC_FLUSH</tt> is used if
3260  * +flush+ is omitted. It is no use giving flush <tt>Zlib::NO_FLUSH</tt>.
3261  */
3262 static VALUE
3264 {
3265  struct gzfile *gz = get_gzfile(obj);
3266  VALUE v_flush;
3267  int flush;
3268 
3269  rb_scan_args(argc, argv, "01", &v_flush);
3270 
3271  flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH);
3272  if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */
3273  zstream_run(&gz->z, (Bytef*)"", 0, flush);
3274  }
3275 
3276  gzfile_write_raw(gz);
3277  if (rb_respond_to(gz->io, id_flush)) {
3278  rb_funcall(gz->io, id_flush, 0);
3279  }
3280  return obj;
3281 }
3282 
3283 /*
3284  * Same as IO.
3285  */
3286 static VALUE
3288 {
3289  struct gzfile *gz = get_gzfile(obj);
3290 
3291  if (TYPE(str) != T_STRING)
3292  str = rb_obj_as_string(str);
3293  if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3294  str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2);
3295  }
3296  gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str));
3297  return INT2FIX(RSTRING_LEN(str));
3298 }
3299 
3300 /*
3301  * Same as IO.
3302  */
3303 static VALUE
3305 {
3306  struct gzfile *gz = get_gzfile(obj);
3307  char c = NUM2CHR(ch);
3308 
3309  gzfile_write(gz, (Bytef*)&c, 1);
3310  return ch;
3311 }
3312 
3313 
3314 
3315 /*
3316  * Document-method: <<
3317  * Same as IO.
3318  */
3319 #define rb_gzwriter_addstr rb_io_addstr
3320 /*
3321  * Document-method: printf
3322  * Same as IO.
3323  */
3324 #define rb_gzwriter_printf rb_io_printf
3325 /*
3326  * Document-method: print
3327  * Same as IO.
3328  */
3329 #define rb_gzwriter_print rb_io_print
3330 /*
3331  * Document-method: puts
3332  * Same as IO.
3333  */
3334 #define rb_gzwriter_puts rb_io_puts
3335 
3336 
3337 /* ------------------------------------------------------------------------- */
3338 
3339 /*
3340  * Document-class: Zlib::GzipReader
3341  *
3342  * Zlib::GzipReader is the class for reading a gzipped file. GzipReader should
3343  * be used an IO, or -IO-lie, object.
3344  *
3345  * Zlib::GzipReader.open('hoge.gz') {|gz|
3346  * print gz.read
3347  * }
3348  *
3349  * File.open('hoge.gz') do |f|
3350  * gz = Zlib::GzipReader.new(f)
3351  * print gz.read
3352  * gz.close
3353  * end
3354  *
3355  * == Method Catalogue
3356  *
3357  * The following methods in Zlib::GzipReader are just like their counterparts
3358  * in IO, but they raise Zlib::Error or Zlib::GzipFile::Error exception if an
3359  * error was found in the gzip file.
3360  * - #each
3361  * - #each_line
3362  * - #each_byte
3363  * - #gets
3364  * - #getc
3365  * - #lineno
3366  * - #lineno=
3367  * - #read
3368  * - #readchar
3369  * - #readline
3370  * - #readlines
3371  * - #ungetc
3372  *
3373  * Be careful of the footer of the gzip file. A gzip file has the checksum of
3374  * pre-compressed data in its footer. GzipReader checks all uncompressed data
3375  * against that checksum at the following cases, and if it fails, raises
3376  * <tt>Zlib::GzipFile::NoFooter</tt>, <tt>Zlib::GzipFile::CRCError</tt>, or
3377  * <tt>Zlib::GzipFile::LengthError</tt> exception.
3378  *
3379  * - When an reading request is received beyond the end of file (the end of
3380  * compressed data). That is, when Zlib::GzipReader#read,
3381  * Zlib::GzipReader#gets, or some other methods for reading returns nil.
3382  * - When Zlib::GzipFile#close method is called after the object reaches the
3383  * end of file.
3384  * - When Zlib::GzipReader#unused method is called after the object reaches
3385  * the end of file.
3386  *
3387  * The rest of the methods are adequately described in their own
3388  * documentation.
3389  */
3390 
3391 static VALUE
3393 {
3394  return gzfile_reader_new(klass);
3395 }
3396 
3397 /*
3398  * Document-method: Zlib::GzipReader.open
3399  *
3400  * call-seq: Zlib::GzipReader.open(filename) {|gz| ... }
3401  *
3402  * Opens a file specified by +filename+ as a gzipped file, and returns a
3403  * GzipReader object associated with that file. Further details of this method
3404  * are in Zlib::GzipReader.new and ZLib::GzipFile.wrap.
3405  */
3406 static VALUE
3407 rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass)
3408 {
3409  return gzfile_s_open(argc, argv, klass, "rb");
3410 }
3411 
3412 /*
3413  * Document-method: Zlib::GzipReader.new
3414  *
3415  * call-seq: Zlib::GzipReader.new(io)
3416  *
3417  * Creates a GzipReader object associated with +io+. The GzipReader object reads
3418  * gzipped data from +io+, and parses/decompresses them. At least, +io+ must have
3419  * a +read+ method that behaves same as the +read+ method in IO class.
3420  *
3421  * If the gzip file header is incorrect, raises an Zlib::GzipFile::Error
3422  * exception.
3423  */
3424 static VALUE
3426 {
3427  VALUE io, opt = Qnil;
3428  struct gzfile *gz;
3429  int err;
3430 
3431  Data_Get_Struct(obj, struct gzfile, gz);
3432  rb_scan_args(argc, argv, "1:", &io, &opt);
3433 
3434  /* this is undocumented feature of zlib */
3435  err = inflateInit2(&gz->z.stream, -MAX_WBITS);
3436  if (err != Z_OK) {
3437  raise_zlib_error(err, gz->z.stream.msg);
3438  }
3439  gz->io = io;
3440  ZSTREAM_READY(&gz->z);
3441  gzfile_read_header(gz);
3442  rb_gzfile_ecopts(gz, opt);
3443 
3444  if (rb_respond_to(io, id_path)) {
3445  gz->path = rb_funcall(gz->io, id_path, 0);
3446  rb_define_singleton_method(obj, "path", rb_gzfile_path, 0);
3447  }
3448 
3449  return obj;
3450 }
3451 
3452 /*
3453  * Document-method: Zlib::GzipReader#rewind
3454  *
3455  * Resets the position of the file pointer to the point created the GzipReader
3456  * object. The associated IO object needs to respond to the +seek+ method.
3457  */
3458 static VALUE
3460 {
3461  struct gzfile *gz = get_gzfile(obj);
3463  return INT2FIX(0);
3464 }
3465 
3466 /*
3467  * Document-method: Zlib::GzipReader#unused
3468  *
3469  * Returns the rest of the data which had read for parsing gzip format, or
3470  * +nil+ if the whole gzip file is not parsed yet.
3471  */
3472 static VALUE
3474 {
3475  struct gzfile *gz;
3476  Data_Get_Struct(obj, struct gzfile, gz);
3477  return gzfile_reader_get_unused(gz);
3478 }
3479 
3480 /*
3481  * Document-method: Zlib::GzipReader#read
3482  *
3483  * See Zlib::GzipReader documentation for a description.
3484  */
3485 static VALUE
3486 rb_gzreader_read(int argc, VALUE *argv, VALUE obj)
3487 {
3488  struct gzfile *gz = get_gzfile(obj);
3489  VALUE vlen;
3490  long len;
3491 
3492  rb_scan_args(argc, argv, "01", &vlen);
3493  if (NIL_P(vlen)) {
3494  return gzfile_read_all(gz);
3495  }
3496 
3497  len = NUM2INT(vlen);
3498  if (len < 0) {
3499  rb_raise(rb_eArgError, "negative length %ld given", len);
3500  }
3501  return gzfile_read(gz, len);
3502 }
3503 
3504 /*
3505  * Document-method: Zlib::GzipReader#readpartial
3506  *
3507  * call-seq:
3508  * gzipreader.readpartial(maxlen [, outbuf]) => string, outbuf
3509  *
3510  * Reads at most <i>maxlen</i> bytes from the gziped stream but
3511  * it blocks only if <em>gzipreader</em> has no data immediately available.
3512  * If the optional <i>outbuf</i> argument is present,
3513  * it must reference a String, which will receive the data.
3514  * It raises <code>EOFError</code> on end of file.
3515  */
3516 static VALUE
3518 {
3519  struct gzfile *gz = get_gzfile(obj);
3520  VALUE vlen, outbuf;
3521  long len;
3522 
3523  rb_scan_args(argc, argv, "11", &vlen, &outbuf);
3524 
3525  len = NUM2INT(vlen);
3526  if (len < 0) {
3527  rb_raise(rb_eArgError, "negative length %ld given", len);
3528  }
3529  if (!NIL_P(outbuf))
3530  Check_Type(outbuf, T_STRING);
3531  return gzfile_readpartial(gz, len, outbuf);
3532 }
3533 
3534 /*
3535  * Document-method: Zlib::GzipReader#getc
3536  *
3537  * See Zlib::GzipReader documentation for a description.
3538  */
3539 static VALUE
3541 {
3542  struct gzfile *gz = get_gzfile(obj);
3543 
3544  return gzfile_getc(gz);
3545 }
3546 
3547 /*
3548  * Document-method: Zlib::GzipReader#readchar
3549  *
3550  * See Zlib::GzipReader documentation for a description.
3551  */
3552 static VALUE
3554 {
3555  VALUE dst;
3556  dst = rb_gzreader_getc(obj);
3557  if (NIL_P(dst)) {
3558  rb_raise(rb_eEOFError, "end of file reached");
3559  }
3560  return dst;
3561 }
3562 
3563 /*
3564  * Document-method: Zlib::GzipReader#getbyte
3565  *
3566  * See Zlib::GzipReader documentation for a description.
3567  */
3568 static VALUE
3570 {
3571  struct gzfile *gz = get_gzfile(obj);
3572  VALUE dst;
3573 
3574  dst = gzfile_read(gz, 1);
3575  if (!NIL_P(dst)) {
3576  dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff);
3577  }
3578  return dst;
3579 }
3580 
3581 /*
3582  * Document-method: Zlib::GzipReader#readbyte
3583  *
3584  * See Zlib::GzipReader documentation for a description.
3585  */
3586 static VALUE
3588 {
3589  VALUE dst;
3590  dst = rb_gzreader_getbyte(obj);
3591  if (NIL_P(dst)) {
3592  rb_raise(rb_eEOFError, "end of file reached");
3593  }
3594  return dst;
3595 }
3596 
3597 /*
3598  * Document-method: Zlib::GzipReader#each_char
3599  *
3600  * See Zlib::GzipReader documentation for a description.
3601  */
3602 static VALUE
3604 {
3605  VALUE c;
3606 
3607  RETURN_ENUMERATOR(obj, 0, 0);
3608 
3609  while (!NIL_P(c = rb_gzreader_getc(obj))) {
3610  rb_yield(c);
3611  }
3612  return Qnil;
3613 }
3614 
3615 /*
3616  * Document-method: Zlib::GzipReader#each_byte
3617  *
3618  * See Zlib::GzipReader documentation for a description.
3619  */
3620 static VALUE
3622 {
3623  VALUE c;
3624 
3625  RETURN_ENUMERATOR(obj, 0, 0);
3626 
3627  while (!NIL_P(c = rb_gzreader_getbyte(obj))) {
3628  rb_yield(c);
3629  }
3630  return Qnil;
3631 }
3632 
3633 /*
3634  * Document-method: Zlib::GzipReader#ungetc
3635  *
3636  * See Zlib::GzipReader documentation for a description.
3637  */
3638 static VALUE
3640 {
3641  struct gzfile *gz;
3642 
3643  if (FIXNUM_P(s))
3644  return rb_gzreader_ungetbyte(obj, s);
3645  gz = get_gzfile(obj);
3646  StringValue(s);
3647  if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) {
3648  s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2);
3649  }
3650  gzfile_ungets(gz, (const Bytef*)RSTRING_PTR(s), RSTRING_LEN(s));
3651  return Qnil;
3652 }
3653 
3654 /*
3655  * Document-method: Zlib::GzipReader#ungetbyte
3656  *
3657  * See Zlib::GzipReader documentation for a description.
3658  */
3659 static VALUE
3661 {
3662  struct gzfile *gz = get_gzfile(obj);
3663  gzfile_ungetbyte(gz, NUM2CHR(ch));
3664  return Qnil;
3665 }
3666 
3667 static void
3669 {
3670  VALUE str;
3671  char *p;
3672  int n;
3673 
3674  while (gz->z.buf_filled == 0) {
3675  if (GZFILE_IS_FINISHED(gz)) return;
3676  gzfile_read_more(gz);
3677  }
3678  n = 0;
3679  p = RSTRING_PTR(gz->z.buf);
3680 
3681  while (n++, *(p++) == '\n') {
3682  if (n >= gz->z.buf_filled) {
3683  str = zstream_detach_buffer(&gz->z);
3684  gzfile_calc_crc(gz, str);
3685  while (gz->z.buf_filled == 0) {
3686  if (GZFILE_IS_FINISHED(gz)) return;
3687  gzfile_read_more(gz);
3688  }
3689  n = 0;
3690  p = RSTRING_PTR(gz->z.buf);
3691  }
3692  }
3693 
3694  str = zstream_shift_buffer(&gz->z, n - 1);
3695  gzfile_calc_crc(gz, str);
3696 }
3697 
3698 static void
3699 rscheck(const char *rsptr, long rslen, VALUE rs)
3700 {
3701  if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen)
3702  rb_raise(rb_eRuntimeError, "rs modified");
3703 }
3704 
3705 static long
3707 {
3708  char *s = RSTRING_PTR(gz->z.buf);
3709  char *e = s + gz->z.buf_filled;
3710  char *p = rb_enc_left_char_head(s, s + n, e, gz->enc);
3711  long l = p - s;
3712  if (l < n) {
3713  n = rb_enc_precise_mbclen(p, e, gz->enc);
3714  if (MBCLEN_NEEDMORE_P(n)) {
3715  if ((l = gzfile_fill(gz, l + MBCLEN_NEEDMORE_LEN(n))) > 0) {
3716  return l;
3717  }
3718  }
3719  else if (MBCLEN_CHARFOUND_P(n)) {
3720  return l + MBCLEN_CHARFOUND_LEN(n);
3721  }
3722  }
3723  return n;
3724 }
3725 
3726 static VALUE
3727 gzreader_gets(int argc, VALUE *argv, VALUE obj)
3728 {
3729  struct gzfile *gz = get_gzfile(obj);
3730  volatile VALUE rs;
3731  VALUE dst;
3732  const char *rsptr;
3733  char *p, *res;
3734  long rslen, n, limit = -1;
3735  int rspara;
3736  rb_encoding *enc = gz->enc;
3737  int maxlen = rb_enc_mbmaxlen(enc);
3738 
3739  if (argc == 0) {
3740  rs = rb_rs;
3741  }
3742  else {
3743  VALUE lim, tmp;
3744 
3745  rb_scan_args(argc, argv, "11", &rs, &lim);
3746  if (!NIL_P(lim)) {
3747  if (!NIL_P(rs)) StringValue(rs);
3748  }
3749  else if (!NIL_P(rs)) {
3750  tmp = rb_check_string_type(rs);
3751  if (NIL_P(tmp)) {
3752  lim = rs;
3753  rs = rb_rs;
3754  }
3755  else {
3756  rs = tmp;
3757  }
3758  }
3759  if (!NIL_P(lim)) {
3760  limit = NUM2LONG(lim);
3761  if (limit == 0) return rb_str_new(0,0);
3762  }
3763  }
3764 
3765  if (NIL_P(rs)) {
3766  if (limit < 0) {
3767  dst = gzfile_read_all(gz);
3768  if (RSTRING_LEN(dst) == 0) return Qnil;
3769  }
3770  else if ((n = gzfile_fill(gz, limit)) <= 0) {
3771  return Qnil;
3772  }
3773  else {
3774  if (maxlen > 1 && n >= limit && !GZFILE_IS_FINISHED(gz)) {
3775  n = gzreader_charboundary(gz, n);
3776  }
3777  else {
3778  n = limit;
3779  }
3780  dst = zstream_shift_buffer(&gz->z, n);
3781  gzfile_calc_crc(gz, dst);
3782  dst = gzfile_newstr(gz, dst);
3783  }
3784  gz->lineno++;
3785  return dst;
3786  }
3787 
3788  if (RSTRING_LEN(rs) == 0) {
3789  rsptr = "\n\n";
3790  rslen = 2;
3791  rspara = 1;
3792  } else {
3793  rsptr = RSTRING_PTR(rs);
3794  rslen = RSTRING_LEN(rs);
3795  rspara = 0;
3796  }
3797 
3798  if (rspara) {
3800  }
3801 
3802  while (gz->z.buf_filled < rslen) {
3803  if (ZSTREAM_IS_FINISHED(&gz->z)) {
3804  if (gz->z.buf_filled > 0) gz->lineno++;
3805  return gzfile_read(gz, rslen);
3806  }
3807  gzfile_read_more(gz);
3808  }
3809 
3810  p = RSTRING_PTR(gz->z.buf);
3811  n = rslen;
3812  for (;;) {
3813  long filled;
3814  if (n > gz->z.buf_filled) {
3815  if (ZSTREAM_IS_FINISHED(&gz->z)) break;
3816  gzfile_read_more(gz);
3817  p = RSTRING_PTR(gz->z.buf) + n - rslen;
3818  }
3819  if (!rspara) rscheck(rsptr, rslen, rs);
3820  filled = gz->z.buf_filled;
3821  if (limit > 0 && filled >= limit) {
3822  filled = limit;
3823  }
3824  res = memchr(p, rsptr[0], (filled - n + 1));
3825  if (!res) {
3826  n = filled;
3827  if (limit > 0 && filled >= limit) break;
3828  n++;
3829  } else {
3830  n += (long)(res - p);
3831  p = res;
3832  if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break;
3833  p++, n++;
3834  }
3835  }
3836  if (maxlen > 1 && n == limit && (gz->z.buf_filled > n || !ZSTREAM_IS_FINISHED(&gz->z))) {
3837  n = gzreader_charboundary(gz, n);
3838  }
3839 
3840  gz->lineno++;
3841  dst = gzfile_read(gz, n);
3842  if (rspara) {
3844  }
3845 
3846  return gzfile_newstr(gz, dst);
3847 }
3848 
3849 /*
3850  * Document-method: Zlib::GzipReader#gets
3851  *
3852  * See Zlib::GzipReader documentation for a description.
3853  */
3854 static VALUE
3855 rb_gzreader_gets(int argc, VALUE *argv, VALUE obj)
3856 {
3857  VALUE dst;
3858  dst = gzreader_gets(argc, argv, obj);
3859  if (!NIL_P(dst)) {
3860  rb_lastline_set(dst);
3861  }
3862  return dst;
3863 }
3864 
3865 /*
3866  * Document-method: Zlib::GzipReader#readline
3867  *
3868  * See Zlib::GzipReader documentation for a description.
3869  */
3870 static VALUE
3872 {
3873  VALUE dst;
3874  dst = rb_gzreader_gets(argc, argv, obj);
3875  if (NIL_P(dst)) {
3876  rb_raise(rb_eEOFError, "end of file reached");
3877  }
3878  return dst;
3879 }
3880 
3881 /*
3882  * Document-method: Zlib::GzipReader#each
3883  *
3884  * See Zlib::GzipReader documentation for a description.
3885  */
3886 static VALUE
3887 rb_gzreader_each(int argc, VALUE *argv, VALUE obj)
3888 {
3889  VALUE str;
3890 
3891  RETURN_ENUMERATOR(obj, 0, 0);
3892 
3893  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
3894  rb_yield(str);
3895  }
3896  return obj;
3897 }
3898 
3899 /*
3900  * Document-method: Zlib::GzipReader#readlines
3901  *
3902  * See Zlib::GzipReader documentation for a description.
3903  */
3904 static VALUE
3906 {
3907  VALUE str, dst;
3908  dst = rb_ary_new();
3909  while (!NIL_P(str = gzreader_gets(argc, argv, obj))) {
3910  rb_ary_push(dst, str);
3911  }
3912  return dst;
3913 }
3914 
3915 #endif /* GZIP_SUPPORT */
3916 
3917 
3918 
3919 /*
3920  * Document-module: Zlib
3921  *
3922  * The Zlib module contains several classes for compressing and decompressing
3923  * streams, and for working with "gzip" files.
3924  *
3925  * == Classes
3926  *
3927  * Following are the classes that are most likely to be of interest to the
3928  * user:
3929  * Zlib::Inflate
3930  * Zlib::Deflate
3931  * Zlib::GzipReader
3932  * Zlib::GzipWriter
3933  *
3934  * There are two important base classes for the classes above: Zlib::ZStream
3935  * and Zlib::GzipFile. Everything else is an error class.
3936  *
3937  * == Constants
3938  *
3939  * Here's a list.
3940  *
3941  * Zlib::VERSION
3942  * The Ruby/zlib version string.
3943  *
3944  * Zlib::ZLIB_VERSION
3945  * The string which represents the version of zlib.h.
3946  *
3947  * Zlib::BINARY
3948  * Zlib::ASCII
3949  * Zlib::UNKNOWN
3950  * The integers representing data types which Zlib::ZStream#data_type
3951  * method returns.
3952  *
3953  * Zlib::NO_COMPRESSION
3954  * Zlib::BEST_SPEED
3955  * Zlib::BEST_COMPRESSION
3956  * Zlib::DEFAULT_COMPRESSION
3957  * The integers representing compression levels which are an argument
3958  * for Zlib::Deflate.new, Zlib::Deflate#deflate, and so on.
3959  *
3960  * Zlib::FILTERED
3961  * Zlib::HUFFMAN_ONLY
3962  * Zlib::DEFAULT_STRATEGY
3963  * The integers representing compression methods which are an argument
3964  * for Zlib::Deflate.new and Zlib::Deflate#params.
3965  *
3966  * Zlib::DEF_MEM_LEVEL
3967  * Zlib::MAX_MEM_LEVEL
3968  * The integers representing memory levels which are an argument for
3969  * Zlib::Deflate.new, Zlib::Deflate#params, and so on.
3970  *
3971  * Zlib::MAX_WBITS
3972  * The default value of windowBits which is an argument for
3973  * Zlib::Deflate.new and Zlib::Inflate.new.
3974  *
3975  * Zlib::NO_FLUSH
3976  * Zlib::SYNC_FLUSH
3977  * Zlib::FULL_FLUSH
3978  * Zlib::FINISH
3979  * The integers to control the output of the deflate stream, which are
3980  * an argument for Zlib::Deflate#deflate and so on.
3981  *
3982  * Zlib::OS_CODE
3983  * Zlib::OS_MSDOS
3984  * Zlib::OS_AMIGA
3985  * Zlib::OS_VMS
3986  * Zlib::OS_UNIX
3987  * Zlib::OS_VMCMS
3988  * Zlib::OS_ATARI
3989  * Zlib::OS_OS2
3990  * Zlib::OS_MACOS
3991  * Zlib::OS_ZSYSTEM
3992  * Zlib::OS_CPM
3993  * Zlib::OS_TOPS20
3994  * Zlib::OS_WIN32
3995  * Zlib::OS_QDOS
3996  * Zlib::OS_RISCOS
3997  * Zlib::OS_UNKNOWN
3998  * The return values of Zlib::GzipFile#os_code method.
3999  */
4000 void
4002 {
4003  VALUE mZlib, cZStream, cDeflate, cInflate;
4004 #if GZIP_SUPPORT
4005  VALUE cGzipFile, cGzipWriter, cGzipReader;
4006 #endif
4007 
4008  mZlib = rb_define_module("Zlib");
4009 
4010  cZError = rb_define_class_under(mZlib, "Error", rb_eStandardError);
4011  cStreamEnd = rb_define_class_under(mZlib, "StreamEnd", cZError);
4012  cNeedDict = rb_define_class_under(mZlib, "NeedDict", cZError);
4013  cDataError = rb_define_class_under(mZlib, "DataError", cZError);
4014  cStreamError = rb_define_class_under(mZlib, "StreamError", cZError);
4015  cMemError = rb_define_class_under(mZlib, "MemError", cZError);
4016  cBufError = rb_define_class_under(mZlib, "BufError", cZError);
4017  cVersionError = rb_define_class_under(mZlib, "VersionError", cZError);
4018 
4019  rb_define_module_function(mZlib, "zlib_version", rb_zlib_version, 0);
4020  rb_define_module_function(mZlib, "adler32", rb_zlib_adler32, -1);
4021  rb_define_module_function(mZlib, "adler32_combine", rb_zlib_adler32_combine, 3);
4022  rb_define_module_function(mZlib, "crc32", rb_zlib_crc32, -1);
4023  rb_define_module_function(mZlib, "crc32_combine", rb_zlib_crc32_combine, 3);
4024  rb_define_module_function(mZlib, "crc_table", rb_zlib_crc_table, 0);
4025 
4026  /* The Ruby/zlib version string. */
4027  rb_define_const(mZlib, "VERSION", rb_str_new2(RUBY_ZLIB_VERSION));
4028  /* The string which represents the version of zlib.h */
4029  rb_define_const(mZlib, "ZLIB_VERSION", rb_str_new2(ZLIB_VERSION));
4030 
4031  cZStream = rb_define_class_under(mZlib, "ZStream", rb_cObject);
4032  rb_undef_alloc_func(cZStream);
4033  rb_define_method(cZStream, "avail_out", rb_zstream_avail_out, 0);
4034  rb_define_method(cZStream, "avail_out=", rb_zstream_set_avail_out, 1);
4035  rb_define_method(cZStream, "avail_in", rb_zstream_avail_in, 0);
4036  rb_define_method(cZStream, "total_in", rb_zstream_total_in, 0);
4037  rb_define_method(cZStream, "total_out", rb_zstream_total_out, 0);
4038  rb_define_method(cZStream, "data_type", rb_zstream_data_type, 0);
4039  rb_define_method(cZStream, "adler", rb_zstream_adler, 0);
4040  rb_define_method(cZStream, "finished?", rb_zstream_finished_p, 0);
4041  rb_define_method(cZStream, "stream_end?", rb_zstream_finished_p, 0);
4042  rb_define_method(cZStream, "closed?", rb_zstream_closed_p, 0);
4043  rb_define_method(cZStream, "ended?", rb_zstream_closed_p, 0);
4044  rb_define_method(cZStream, "close", rb_zstream_end, 0);
4045  rb_define_method(cZStream, "end", rb_zstream_end, 0);
4046  rb_define_method(cZStream, "reset", rb_zstream_reset, 0);
4047  rb_define_method(cZStream, "finish", rb_zstream_finish, 0);
4048  rb_define_method(cZStream, "flush_next_in", rb_zstream_flush_next_in, 0);
4049  rb_define_method(cZStream, "flush_next_out", rb_zstream_flush_next_out, 0);
4050 
4051  /* Integer representing date types which
4052  * ZStream#data_type method returns */
4053  rb_define_const(mZlib, "BINARY", INT2FIX(Z_BINARY));
4054  /* Integer representing date types which
4055  * ZStream#data_type method returns */
4056  rb_define_const(mZlib, "ASCII", INT2FIX(Z_ASCII));
4057  /* Integer representing date types which
4058  * ZStream#data_type method returns */
4059  rb_define_const(mZlib, "UNKNOWN", INT2FIX(Z_UNKNOWN));
4060 
4061  cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream);
4062  rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1);
4063  rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1);
4065  rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1);
4066  rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1);
4067  rb_define_method(cDeflate, "deflate", rb_deflate_deflate, -1);
4068  rb_define_method(cDeflate, "<<", rb_deflate_addstr, 1);
4069  rb_define_method(cDeflate, "flush", rb_deflate_flush, -1);
4070  rb_define_method(cDeflate, "params", rb_deflate_params, 2);
4071  rb_define_method(cDeflate, "set_dictionary", rb_deflate_set_dictionary, 1);
4072 
4073  cInflate = rb_define_class_under(mZlib, "Inflate", cZStream);
4074  rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1);
4075  rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1);
4077  rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1);
4078  rb_define_method(cInflate, "inflate", rb_inflate_inflate, 1);
4079  rb_define_method(cInflate, "<<", rb_inflate_addstr, 1);
4080  rb_define_method(cInflate, "sync", rb_inflate_sync, 1);
4081  rb_define_method(cInflate, "sync_point?", rb_inflate_sync_point_p, 0);
4082  rb_define_method(cInflate, "set_dictionary", rb_inflate_set_dictionary, 1);
4083 
4084  /* compression level 0
4085  *
4086  * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
4087  rb_define_const(mZlib, "NO_COMPRESSION", INT2FIX(Z_NO_COMPRESSION));
4088  /* compression level 1
4089  *
4090  * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
4091  rb_define_const(mZlib, "BEST_SPEED", INT2FIX(Z_BEST_SPEED));
4092  /* compression level 9
4093  *
4094  * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
4095  rb_define_const(mZlib, "BEST_COMPRESSION", INT2FIX(Z_BEST_COMPRESSION));
4096  /* compression level -1
4097  *
4098  * Which is an argument for Deflate.new, Deflate#deflate, and so on. */
4099  rb_define_const(mZlib, "DEFAULT_COMPRESSION",
4100  INT2FIX(Z_DEFAULT_COMPRESSION));
4101 
4102  /* compression method 1
4103  *
4104  * Which is an argument for Deflate.new and Deflate#params. */
4105  rb_define_const(mZlib, "FILTERED", INT2FIX(Z_FILTERED));
4106  /* compression method 2
4107  *
4108  * Which is an argument for Deflate.new and Deflate#params. */
4109  rb_define_const(mZlib, "HUFFMAN_ONLY", INT2FIX(Z_HUFFMAN_ONLY));
4110  /* compression method 0
4111  *
4112  * Which is an argument for Deflate.new and Deflate#params. */
4113  rb_define_const(mZlib, "DEFAULT_STRATEGY", INT2FIX(Z_DEFAULT_STRATEGY));
4114 
4115  /* The default value of windowBits which is an argument for
4116  * Deflate.new and Inflate.new.
4117  */
4118  rb_define_const(mZlib, "MAX_WBITS", INT2FIX(MAX_WBITS));
4119  /* Default value is 8
4120  *
4121  * The integer representing memory levels.
4122  * Which are an argument for Deflate.new, Deflate#params, and so on. */
4123  rb_define_const(mZlib, "DEF_MEM_LEVEL", INT2FIX(DEF_MEM_LEVEL));
4124  /* Maximum level is 9
4125  *
4126  * The integers representing memory levels which are an argument for
4127  * Deflate.new, Deflate#params, and so on. */
4128  rb_define_const(mZlib, "MAX_MEM_LEVEL", INT2FIX(MAX_MEM_LEVEL));
4129 
4130  /* Output control - 0
4131  *
4132  * The integers to control the output of the deflate stream, which are
4133  * an argument for Deflate#deflate and so on. */
4134  rb_define_const(mZlib, "NO_FLUSH", INT2FIX(Z_NO_FLUSH));
4135  /* Output control - 2
4136  *
4137  * The integers to control the output of the deflate stream, which are
4138  * an argument for Deflate#deflate and so on. */
4139  rb_define_const(mZlib, "SYNC_FLUSH", INT2FIX(Z_SYNC_FLUSH));
4140  /* Output control - 3
4141  *
4142  * The integers to control the output of the deflate stream, which are
4143  * an argument for Deflate#deflate and so on. */
4144  rb_define_const(mZlib, "FULL_FLUSH", INT2FIX(Z_FULL_FLUSH));
4145  /* Oputput control - 4
4146  *
4147  * The integers to control the output of the deflate stream, which are
4148  * an argument for Deflate#deflate and so on. */
4149  rb_define_const(mZlib, "FINISH", INT2FIX(Z_FINISH));
4150 
4151 #if GZIP_SUPPORT
4152  id_write = rb_intern("write");
4153  id_read = rb_intern("read");
4154  id_readpartial = rb_intern("readpartial");
4155  id_flush = rb_intern("flush");
4156  id_seek = rb_intern("seek");
4157  id_close = rb_intern("close");
4158  id_path = rb_intern("path");
4159  id_input = rb_intern("@input");
4160 
4161  cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject);
4162  cGzError = rb_define_class_under(cGzipFile, "Error", cZError);
4163 
4164  /* input gzipped string */
4165  rb_define_attr(cGzError, "input", 1, 0);
4166  rb_define_method(cGzError, "inspect", gzfile_error_inspect, 0);
4167 
4168  cNoFooter = rb_define_class_under(cGzipFile, "NoFooter", cGzError);
4169  cCRCError = rb_define_class_under(cGzipFile, "CRCError", cGzError);
4170  cLengthError = rb_define_class_under(cGzipFile,"LengthError",cGzError);
4171 
4172  cGzipWriter = rb_define_class_under(mZlib, "GzipWriter", cGzipFile);
4173  cGzipReader = rb_define_class_under(mZlib, "GzipReader", cGzipFile);
4174  rb_include_module(cGzipReader, rb_mEnumerable);
4175 
4176  rb_define_singleton_method(cGzipFile, "wrap", rb_gzfile_s_wrap, -1);
4177  rb_undef_alloc_func(cGzipFile);
4178  rb_define_method(cGzipFile, "to_io", rb_gzfile_to_io, 0);
4179  rb_define_method(cGzipFile, "crc", rb_gzfile_crc, 0);
4180  rb_define_method(cGzipFile, "mtime", rb_gzfile_mtime, 0);
4181  rb_define_method(cGzipFile, "level", rb_gzfile_level, 0);
4182  rb_define_method(cGzipFile, "os_code", rb_gzfile_os_code, 0);
4183  rb_define_method(cGzipFile, "orig_name", rb_gzfile_orig_name, 0);
4184  rb_define_method(cGzipFile, "comment", rb_gzfile_comment, 0);
4185  rb_define_method(cGzipReader, "lineno", rb_gzfile_lineno, 0);
4186  rb_define_method(cGzipReader, "lineno=", rb_gzfile_set_lineno, 1);
4187  rb_define_method(cGzipWriter, "mtime=", rb_gzfile_set_mtime, 1);
4188  rb_define_method(cGzipWriter, "orig_name=", rb_gzfile_set_orig_name,1);
4189  rb_define_method(cGzipWriter, "comment=", rb_gzfile_set_comment, 1);
4190  rb_define_method(cGzipFile, "close", rb_gzfile_close, 0);
4191  rb_define_method(cGzipFile, "finish", rb_gzfile_finish, 0);
4192  rb_define_method(cGzipFile, "closed?", rb_gzfile_closed_p, 0);
4193  rb_define_method(cGzipReader, "eof", rb_gzfile_eof_p, 0);
4194  rb_define_method(cGzipReader, "eof?", rb_gzfile_eof_p, 0);
4195  rb_define_method(cGzipFile, "sync", rb_gzfile_sync, 0);
4196  rb_define_method(cGzipFile, "sync=", rb_gzfile_set_sync, 1);
4197  rb_define_method(cGzipReader, "pos", rb_gzfile_total_out, 0);
4198  rb_define_method(cGzipWriter, "pos", rb_gzfile_total_in, 0);
4199  rb_define_method(cGzipReader, "tell", rb_gzfile_total_out, 0);
4200  rb_define_method(cGzipWriter, "tell", rb_gzfile_total_in, 0);
4201 
4202  rb_define_singleton_method(cGzipWriter, "open", rb_gzwriter_s_open,-1);
4204  rb_define_method(cGzipWriter, "initialize", rb_gzwriter_initialize,-1);
4205  rb_define_method(cGzipWriter, "flush", rb_gzwriter_flush, -1);
4206  rb_define_method(cGzipWriter, "write", rb_gzwriter_write, 1);
4207  rb_define_method(cGzipWriter, "putc", rb_gzwriter_putc, 1);
4208  rb_define_method(cGzipWriter, "<<", rb_gzwriter_addstr, 1);
4209  rb_define_method(cGzipWriter, "printf", rb_gzwriter_printf, -1);
4210  rb_define_method(cGzipWriter, "print", rb_gzwriter_print, -1);
4211  rb_define_method(cGzipWriter, "puts", rb_gzwriter_puts, -1);
4212 
4213  rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1);
4215  rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, -1);
4216  rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0);
4217  rb_define_method(cGzipReader, "unused", rb_gzreader_unused, 0);
4218  rb_define_method(cGzipReader, "read", rb_gzreader_read, -1);
4219  rb_define_method(cGzipReader, "readpartial", rb_gzreader_readpartial, -1);
4220  rb_define_method(cGzipReader, "getc", rb_gzreader_getc, 0);
4221  rb_define_method(cGzipReader, "getbyte", rb_gzreader_getbyte, 0);
4222  rb_define_method(cGzipReader, "readchar", rb_gzreader_readchar, 0);
4223  rb_define_method(cGzipReader, "readbyte", rb_gzreader_readbyte, 0);
4224  rb_define_method(cGzipReader, "each_byte", rb_gzreader_each_byte, 0);
4225  rb_define_method(cGzipReader, "each_char", rb_gzreader_each_char, 0);
4226  rb_define_method(cGzipReader, "bytes", rb_gzreader_each_byte, 0);
4227  rb_define_method(cGzipReader, "ungetc", rb_gzreader_ungetc, 1);
4228  rb_define_method(cGzipReader, "ungetbyte", rb_gzreader_ungetbyte, 1);
4229  rb_define_method(cGzipReader, "gets", rb_gzreader_gets, -1);
4230  rb_define_method(cGzipReader, "readline", rb_gzreader_readline, -1);
4231  rb_define_method(cGzipReader, "each", rb_gzreader_each, -1);
4232  rb_define_method(cGzipReader, "each_line", rb_gzreader_each, -1);
4233  rb_define_method(cGzipReader, "lines", rb_gzreader_each, -1);
4234  rb_define_method(cGzipReader, "readlines", rb_gzreader_readlines, -1);
4235 
4236  /* From GzipFile#os_code - code of current host */
4237  rb_define_const(mZlib, "OS_CODE", INT2FIX(OS_CODE));
4238  /* From GzipFile#os_code - 0x00 */
4239  rb_define_const(mZlib, "OS_MSDOS", INT2FIX(OS_MSDOS));
4240  /* From GzipFile#os_code - 0x01 */
4241  rb_define_const(mZlib, "OS_AMIGA", INT2FIX(OS_AMIGA));
4242  /* From GzipFile#os_code - 0x02 */
4243  rb_define_const(mZlib, "OS_VMS", INT2FIX(OS_VMS));
4244  /* From GzipFile#os_code - 0x03 */
4245  rb_define_const(mZlib, "OS_UNIX", INT2FIX(OS_UNIX));
4246  /* From GzipFile#os_code - 0x05 */
4247  rb_define_const(mZlib, "OS_ATARI", INT2FIX(OS_ATARI));
4248  /* From GzipFile#os_code - 0x06 */
4249  rb_define_const(mZlib, "OS_OS2", INT2FIX(OS_OS2));
4250  /* From GzipFile#os_code - 0x07 */
4251  rb_define_const(mZlib, "OS_MACOS", INT2FIX(OS_MACOS));
4252  /* From GzipFile#os_code - 0x0a */
4253  rb_define_const(mZlib, "OS_TOPS20", INT2FIX(OS_TOPS20));
4254  /* From GzipFile#os_code - 0x0b */
4255  rb_define_const(mZlib, "OS_WIN32", INT2FIX(OS_WIN32));
4256 
4257  /* From GzipFile#os_code - 0x04 */
4258  rb_define_const(mZlib, "OS_VMCMS", INT2FIX(OS_VMCMS));
4259  /* From GzipFile#os_code - 0x08 */
4260  rb_define_const(mZlib, "OS_ZSYSTEM", INT2FIX(OS_ZSYSTEM));
4261  /* From GzipFile#os_code - 0x09 */
4262  rb_define_const(mZlib, "OS_CPM", INT2FIX(OS_CPM));
4263  /* From GzipFile#os_code - 0x0c */
4264  rb_define_const(mZlib, "OS_QDOS", INT2FIX(OS_QDOS));
4265  /* From GzipFile#os_code - 0x0d */
4266  rb_define_const(mZlib, "OS_RISCOS", INT2FIX(OS_RISCOS));
4267  /* From GzipFile#os_code - 0xff */
4268  rb_define_const(mZlib, "OS_UNKNOWN", INT2FIX(OS_UNKNOWN));
4269 
4270 #endif /* GZIP_SUPPORT */
4271 }
4272 
4273 /* Document error classes. */
4274 
4275 /*
4276  * Document-class: Zlib::Error
4277  *
4278  * The superclass for all exceptions raised by Ruby/zlib.
4279  *
4280  * The following exceptions are defined as subclasses of Zlib::Error. These
4281  * exceptions are raised when zlib library functions return with an error
4282  * status.
4283  *
4284  * - Zlib::StreamEnd
4285  * - Zlib::NeedDict
4286  * - Zlib::DataError
4287  * - Zlib::StreamError
4288  * - Zlib::MemError
4289  * - Zlib::BufError
4290  * - Zlib::VersionError
4291  *
4292  */
4293 
4294 /*
4295  * Document-class: Zlib::StreamEnd
4296  *
4297  * Subclass of Zlib::Error
4298  *
4299  * When zlib returns a Z_STREAM_END
4300  * is return if the end of the compressed data has been reached
4301  * and all uncompressed out put has been produced.
4302  *
4303  */
4304 
4305 /*
4306  * Document-class: Zlib::NeedDict
4307  *
4308  * Subclass of Zlib::Error
4309  *
4310  * When zlib returns a Z_NEED_DICT
4311  * if a preset dictionary is needed at this point.
4312  *
4313  * Used by Zlib::Inflate.inflate and <tt>Zlib.inflate</tt>
4314  */
4315 
4316 /*
4317  * Document-class: Zlib::VersionError
4318  *
4319  * Subclass of Zlib::Error
4320  *
4321  * When zlib returns a Z_VERSION_ERROR,
4322  * usually if the zlib library version is incompatible with the
4323  * version assumed by the caller.
4324  *
4325  */
4326 
4327 /*
4328  * Document-class: Zlib::MemError
4329  *
4330  * Subclass of Zlib::Error
4331  *
4332  * When zlib returns a Z_MEM_ERROR,
4333  * usually if there was not enough memory.
4334  *
4335  */
4336 
4337 /*
4338  * Document-class: Zlib::StreamError
4339  *
4340  * Subclass of Zlib::Error
4341  *
4342  * When zlib returns a Z_STREAM_ERROR,
4343  * usually if the stream state was inconsistent.
4344  *
4345  */
4346 
4347 /*
4348  * Document-class: Zlib::BufError
4349  *
4350  * Subclass of Zlib::Error when zlib returns a Z_BUF_ERROR.
4351  *
4352  * Usually if no progress is possible.
4353  *
4354  */
4355 
4356 /*
4357  * Document-class: Zlib::DataError
4358  *
4359  * Subclass of Zlib::Error when zlib returns a Z_DATA_ERROR.
4360  *
4361  * Usually if a stream was prematurely freed.
4362  *
4363  */
4364 
4365 /*
4366  * Document-class: Zlib::GzipFile::Error
4367  *
4368  * Base class of errors that occur when processing GZIP files.
4369  */
4370 
4371 /*
4372  * Document-class: Zlib::GzipFile::NoFooter
4373  *
4374  * Raised when gzip file footer is not found.
4375  */
4376 
4377 /*
4378  * Document-class: Zlib::GzipFile::CRCError
4379  *
4380  * Raised when the CRC checksum recorded in gzip file footer is not equivalent
4381  * to the CRC checksum of the actual uncompressed data.
4382  */
4383 
4384 /*
4385  * Document-class: Zlib::GzipFile::LengthError
4386  *
4387  * Raised when the data length recorded in the gzip file footer is not equivalent
4388  * to the length of the actual uncompressed data.
4389  */
4390 
4391 
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1276
static VALUE rb_gzreader_ungetbyte(VALUE, VALUE)
Definition: zlib.c:3660
#define RSTRING_LEN(string)
Definition: generator.h:45
static long NUM2LONG(VALUE x)
Definition: ruby.h:510
static VALUE rb_zstream_closed_p(VALUE)
Definition: zlib.c:1210
#define ZSTREAM_IS_CLOSING(z)
Definition: zlib.c:507
static VALUE rb_zstream_avail_out(VALUE)
Definition: zlib.c:1125
#define OS_RISCOS
Definition: zlib.c:1939
static VALUE rb_inflate_s_allocate(VALUE)
Definition: zlib.c:1610
static VALUE rb_gzreader_unused(VALUE)
Definition: zlib.c:3473
VALUE rb_eStandardError
Definition: error.c:465
static VALUE rb_gzfile_os_code(VALUE)
Definition: zlib.c:2873
struct zstream_funcs * funcs
Definition: zlib.c:2016
static VALUE rb_gzfile_set_comment(VALUE, VALUE)
Definition: zlib.c:2992
static void gzfile_set32(unsigned long n, unsigned char *)
Definition: zlib.c:2178
ssize_t n
Definition: bigdecimal.c:5519
static VALUE rb_deflate_params(VALUE, VALUE, VALUE)
Definition: zlib.c:1537
#define MBCLEN_CHARFOUND_P(ret)
Definition: encoding.h:135
static VALUE rb_gzfile_comment(VALUE)
Definition: zlib.c:2902
rb_econv_t * ec
Definition: zlib.c:1967
static VALUE rb_gzfile_to_io(VALUE)
Definition: zlib.c:2829
static VALUE new_wrap(VALUE tmp)
Definition: zlib.c:2739
VP_EXPORT int
Definition: bigdecimal.c:4911
rb_econv_result_t
Definition: encoding.h:238
#define MBCLEN_CHARFOUND_LEN(ret)
Definition: encoding.h:136
static VALUE rb_gzfile_level(VALUE)
Definition: zlib.c:2862
static void finalizer_warn(const char *)
Definition: zlib.c:308
#define GZFILE_FLAG_SYNC
Definition: zlib.c:1975
static void zstream_mark(struct zstream *)
Definition: zlib.c:935
static VALUE rb_zstream_set_avail_out(VALUE, VALUE)
Definition: zlib.c:1139
#define T_FIXNUM
Definition: ruby.h:425
void(* end)(struct gzfile *)
Definition: zlib.c:1964
static VALUE rb_gzwriter_putc(VALUE, VALUE)
Definition: zlib.c:3304
static VALUE rb_zstream_end(VALUE)
Definition: zlib.c:1056
static void gzfile_check_footer(struct gzfile *)
Definition: zlib.c:2353
#define NUM2INT(x)
Definition: ruby.h:536
#define OS_UNKNOWN
Definition: zlib.c:1940
VALUE rb_big2ulong(VALUE x)
Definition: bignum.c:1211
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:345
parser parser_yylval val
Definition: ripper.c:14289
#define Data_Get_Struct(obj, type, sval)
Definition: ruby.h:835
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1343
static VALUE rb_gzreader_readbyte(VALUE obj)
Definition: zlib.c:3587
#define zstream_init_inflate(z)
Definition: zlib.c:561
#define ZSTREAM_AVAIL_OUT_STEP_MAX
Definition: zlib.c:512
static void zstream_expand_buffer_into(struct zstream *, unsigned long)
Definition: zlib.c:596
VALUE comment
Definition: zlib.c:1960
static VALUE rb_gzfile_mtime(VALUE)
Definition: zlib.c:2851
void(* endfunc)(struct gzfile *)
Definition: zlib.c:2017
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:104
#define rb_zlib_adler32_combine
Definition: zlib.c:415
static VALUE rb_gzfile_set_mtime(VALUE, VALUE)
Definition: zlib.c:2943
static VALUE rb_gzwriter_initialize(int, VALUE *, VALUE)
Definition: zlib.c:3222
static VALUE cNoFooter
Definition: zlib.c:1947
static VALUE cMemError
Definition: zlib.c:260
static ID id_readpartial
Definition: zlib.c:1946
#define OS_AMIGA
Definition: zlib.c:1926
#define GZ_FLAG_ORIG_NAME
Definition: zlib.c:1916
static void gzfile_read_header(struct gzfile *)
Definition: zlib.c:2272
static void zstream_buffer_ungetbyte(struct zstream *, int)
Definition: zlib.c:711
static void gzfile_ungets(struct gzfile *, const Bytef *, long)
Definition: zlib.c:2588
static void do_inflate(struct zstream *, VALUE)
Definition: zlib.c:1730
static void do_deflate(struct zstream *, VALUE, int)
Definition: zlib.c:1410
static VALUE gzfile_reader_get_unused(struct gzfile *)
Definition: zlib.c:2663
int rb_econv_prepare_opts(VALUE opthash, VALUE *ecopts)
Definition: transcode.c:2582
void rb_econv_close(rb_econv_t *ec)
Definition: transcode.c:1708
static void zstream_reset_input(struct zstream *)
Definition: zlib.c:759
#define OS_OS2
Definition: zlib.c:1930
SYMID SyckParser * p
Definition: yaml2byte.c:119
static char * gzfile_read_raw_until_zero(struct gzfile *, long)
Definition: zlib.c:2138
unsigned long VALUE
Definition: ruby.h:88
VALUE exc
Definition: tcltklib.c:3085
static VALUE rb_gzreader_getc(VALUE)
Definition: zlib.c:3540
VALUE enc
Definition: tcltklib.c:10402
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:740
VALUE io
Definition: zlib.c:1955
static VALUE rb_zstream_finished_p(VALUE)
Definition: zlib.c:1201
int os_code
Definition: zlib.c:1958
static VALUE INT2NUM(int v)
Definition: ruby.h:981
VALUE path
Definition: zlib.c:1971
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:704
#define RSTRING_PTR(string)
Definition: generator.h:42
static VALUE rb_inflate_set_dictionary(VALUE, VALUE)
Definition: zlib.c:1883
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:515
#define Check_Type(v, t)
Definition: ruby.h:459
static VALUE rb_deflate_s_allocate(VALUE)
Definition: zlib.c:1239
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1574
static VALUE zstream_detach_input(struct zstream *)
Definition: zlib.c:774
gz os_code
Definition: zlib.c:2027
return Qtrue
Definition: tcltklib.c:9597
static void zstream_append_input(struct zstream *, const Bytef *, long)
Definition: zlib.c:727
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:727
#define ARG_FLUSH(val)
Definition: zlib.c:1235
static void gzfile_reader_end(struct gzfile *)
Definition: zlib.c:2640
#define GZFILE_FLAG_FOOTER_FINISHED
Definition: zlib.c:1977
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:525
#define T_HASH
Definition: ruby.h:421
static void zstream_discard_input(struct zstream *, long)
Definition: zlib.c:746
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:847
static struct zstream_funcs deflate_funcs
Definition: zlib.c:515
return str
Definition: ruby.c:1183
#define MAX_UINT(n)
Definition: zlib.c:54
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:663
#define GZFILE_IS_FINISHED(gz)
Definition: zlib.c:1979
#define GZFILE_FLAG_HEADER_FINISHED
Definition: zlib.c:1976
static VALUE cBufError
Definition: zlib.c:260
static VALUE rb_zlib_version(VALUE)
Definition: zlib.c:322
static ID id_input
Definition: zlib.c:1946
static VALUE rb_gzreader_each_char(VALUE obj)
Definition: zlib.c:3603
#define ZSTREAM_IS_READY(z)
Definition: zlib.c:505
static VALUE gzfile_read_all(struct gzfile *)
Definition: zlib.c:2521
static VALUE cStreamEnd
Definition: zlib.c:259
VALUE rb_file_open_str(VALUE, const char *)
Definition: io.c:5142
static VALUE rb_inflate_s_inflate(VALUE, VALUE)
Definition: zlib.c:1707
#define ZSTREAM_FLAG_FINISHED
Definition: zlib.c:500
#define rb_enc_mbmaxlen(enc)
Definition: encoding.h:125
static void gzreader_skip_linebreaks(struct gzfile *)
Definition: zlib.c:3668
static VALUE rb_deflate_addstr(VALUE, VALUE)
Definition: zlib.c:1480
static VALUE cGzError
Definition: zlib.c:1947
#define ARG_WBITS(val)
Definition: zlib.c:1232
#define ZSTREAM_FLAG_IN_STREAM
Definition: zlib.c:499
static VALUE gzfile_new(VALUE, const struct zstream_funcs *, void(*) _((struct gzfile *)))
static void zstream_init(struct zstream *, const struct zstream_funcs *)
Definition: zlib.c:543
#define FIXNUM_P(f)
Definition: ruby.h:338
#define ZSTREAM_FLAG_CLOSING
Definition: zlib.c:501
static void gzfile_mark(struct gzfile *)
Definition: zlib.c:1986
static VALUE rb_gzwriter_flush(int, VALUE *, VALUE)
Definition: zlib.c:3263
VALUE VALUE args
Definition: tcltklib.c:2550
static VALUE rb_gzreader_rewind(VALUE)
Definition: zlib.c:3459
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
rb_thread_schedule()
Definition: thread.c:1048
static VALUE rb_gzreader_readchar(VALUE)
Definition: zlib.c:3553
static VALUE rb_gzfile_finish(VALUE)
Definition: zlib.c:3035
gz lineno
Definition: zlib.c:2031
static void zstream_buffer_ungets(struct zstream *, const Bytef *, unsigned long)
Definition: zlib.c:694
VALUE input
Definition: zlib.c:489
RUBY_EXTERN void * memmove(void *, const void *, size_t)
Definition: memmove.c:7
static void gzfile_writer_end(struct gzfile *)
Definition: zlib.c:2618
static ID id_close
Definition: zlib.c:1946
#define rb_gzwriter_printf
Definition: zlib.c:3324
static VALUE rb_zstream_total_in(VALUE)
Definition: zlib.c:1163
VALUE rb_str_buf_cat(VALUE, const char *, long)
Definition: string.c:1873
#define dp(v)
Definition: debug.h:23
static VALUE cCRCError
Definition: zlib.c:1947
int level
Definition: zlib.c:1956
Real * b
Definition: bigdecimal.c:1140
#define NORETURN(x)
Definition: ruby.h:31
void rb_exc_raise(VALUE mesg)
Definition: eval.c:460
static ID id_seek
Definition: zlib.c:1946
#define Qnil
Definition: ruby.h:367
int lineno
Definition: zlib.c:1962
return rb_str_append(rb_str_new2(cmd_id_head), id_num)
static VALUE cLengthError
Definition: zlib.c:1947
#define gzfile_writer_new(gz)
Definition: zlib.c:2045
void rb_econv_check_error(rb_econv_t *ec)
Definition: transcode.c:4213
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1245
n NULL
Definition: yaml2byte.c:134
int(* end)(z_streamp)
Definition: zlib.c:493
static void gzfile_close(struct gzfile *, int)
Definition: zlib.c:2063
#define OS_ZSYSTEM
Definition: zlib.c:1936
static ID id_flush
Definition: zlib.c:1946
static VALUE rb_gzfile_path(VALUE)
Definition: zlib.c:3138
static long gzfile_fill(struct gzfile *gz, long len)
Definition: zlib.c:2446
unsigned int input
Definition: nkf.c:3916
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1639
return Qfalse
Definition: tcltklib.c:6768
static unsigned long gzfile_get32(const unsigned char *)
Definition: zlib.c:2167
static void gzfile_calc_crc(struct gzfile *, VALUE)
Definition: zlib.c:2415
static VALUE rb_zstream_adler(VALUE)
Definition: zlib.c:1192
static VALUE rb_deflate_s_deflate(int, VALUE *, VALUE)
Definition: zlib.c:1384
#define ALLOC_N(type, n)
Definition: ruby.h:1034
int rb_block_given_p(void)
Definition: eval.c:604
#define ZSTREAM_FLAG_READY
Definition: zlib.c:498
static VALUE cVersionError
Definition: zlib.c:260
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1246
VALUE rb_eRuntimeError
Definition: error.c:466
static VALUE rb_gzwriter_s_allocate(VALUE)
Definition: zlib.c:3195
static struct gzfile * get_gzfile(VALUE)
Definition: zlib.c:2680
#define checksum_long(func, sum, ptr, len)
Definition: zlib.c:346
#define VALGRIND_MAKE_MEM_DEFINED(p, n)
Definition: zlib.c:23
#define MBCLEN_NEEDMORE_P(ret)
Definition: encoding.h:138
#define GZ_MAGIC1
Definition: zlib.c:1911
#define RSTRING_END(str)
Definition: ruby.h:680
gz crc
Definition: zlib.c:2030
static VALUE rb_gzreader_gets(int, VALUE *, VALUE)
Definition: zlib.c:3855
#define GZ_MAGIC2
Definition: zlib.c:1912
static void gzfile_free(struct gzfile *)
Definition: zlib.c:1997
static VALUE gzfile_getc(struct gzfile *gz)
Definition: zlib.c:2542
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:1908
VALUE rb_obj_as_string(VALUE)
Definition: string.c:854
static VALUE rb_gzreader_read(int, VALUE *, VALUE)
Definition: zlib.c:3486
VALUE rb_ary_new(void)
Definition: array.c:339
static void gzfile_make_footer(struct gzfile *)
Definition: zlib.c:2261
rb_econv_t * rb_econv_open_opts(const char *source_encoding, const char *destination_encoding, int ecflags, VALUE ecopts)
Definition: transcode.c:2588
int flags
Definition: tcltklib.c:3012
VALUE klass
Definition: zlib.c:2735
#define ECONV_PARTIAL_INPUT
Definition: encoding.h:335
static int gzfile_read_raw_ensure(struct gzfile *, long)
Definition: zlib.c:2125
#define ECONV_AFTER_OUTPUT
Definition: encoding.h:336
static void zstream_passthrough_input(struct zstream *)
Definition: zlib.c:765
#define NIL_P(v)
Definition: ruby.h:374
static void raise_zlib_error(int err, const char *msg)
Definition: zlib.c:263
static void zstream_expand_buffer(struct zstream *)
Definition: zlib.c:564
#define OS_VMS
Definition: zlib.c:1927
VALUE rb_eNoMethodError
Definition: error.c:475
static VALUE VALUE obj
Definition: tcltklib.c:3147
static VALUE rb_inflate_addstr(VALUE, VALUE)
Definition: zlib.c:1812
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:1923
#define ARG_LEVEL(val)
Definition: zlib.c:1231
#define GZ_EXTRAFLAG_FAST
Definition: zlib.c:1921
#define zstream_deflate_new(klass)
Definition: zlib.c:972
void rb_lastline_set(VALUE)
Definition: vm.c:761
static VALUE gzfile_read_raw(struct gzfile *)
Definition: zlib.c:2117
#define MBCLEN_NEEDMORE_LEN(ret)
Definition: encoding.h:139
static VALUE rb_gzfile_crc(VALUE)
Definition: zlib.c:2840
static VALUE rb_gzreader_initialize(int, VALUE *, VALUE)
Definition: zlib.c:3425
#define OS_WIN32
Definition: zlib.c:1933
#define TYPE(x)
Definition: ruby.h:441
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Definition: string.c:528
int(* run)(z_streamp, int)
Definition: zlib.c:494
static VALUE rb_zstream_reset(VALUE)
Definition: zlib.c:1067
#define DEF_MEM_LEVEL
Definition: zlib.c:41
static ID id_write
Definition: zlib.c:1946
#define OS_MACOS
Definition: zlib.c:1931
VALUE rb_Integer(VALUE)
Definition: object.c:2193
static VALUE gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf)
Definition: zlib.c:2478
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:3913
int err
Definition: win32.c:78
static void gzfile_write_raw(struct gzfile *)
Definition: zlib.c:2077
static VALUE rb_gzfile_lineno(VALUE)
Definition: zlib.c:2918
static VALUE rb_zstream_flush_next_out(VALUE)
Definition: zlib.c:1109
#define GZ_FLAG_ENCRYPT
Definition: zlib.c:1918
VALUE rb_time_new(time_t, long)
Definition: time.c:2341
static VALUE rb_zlib_crc_table(VALUE)
Definition: zlib.c:462
#define OS_CODE
Definition: zlib.c:1943
#define ZSTREAM_IS_FINISHED(z)
Definition: zlib.c:506
#define ARG_STRATEGY(val)
Definition: zlib.c:1234
VALUE rb_str_resurrect(VALUE str)
Definition: string.c:911
static ID id_read
Definition: zlib.c:1946
#define OS_ATARI
Definition: zlib.c:1929
#define ZSTREAM_READY(z)
Definition: zlib.c:504
VALUE rb_str_resize(VALUE, long)
Definition: string.c:1779
gz level
Definition: zlib.c:2025
VALUE * argv
Definition: tcltklib.c:1962
#define rb_zlib_crc32_combine
Definition: zlib.c:453
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:1609
static VALUE gzfile_writer_end_run(VALUE)
Definition: zlib.c:2602
static VALUE gzfile_error_inspect(VALUE)
Definition: zlib.c:2202
memcpy(buf+1, str, len)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1358
VALUE rb_yield(VALUE)
Definition: vm_eval.c:781
static VALUE rb_deflate_deflate(int, VALUE *, VALUE)
Definition: zlib.c:1456
int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p)
Definition: io.c:4681
struct zstream z
Definition: zlib.c:1954
long ungetc
Definition: zlib.c:1963
#define OS_CPM
Definition: zlib.c:1937
VALUE rb_mEnumerable
Definition: enum.c:17
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1203
static VALUE rb_gzfile_sync(VALUE)
Definition: zlib.c:3078
static VALUE rb_gzfile_closed_p(VALUE)
Definition: zlib.c:3052
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:859
#define RB_GC_GUARD(object)
Definition: generator.h:50
#define const
Definition: strftime.c:101
register char * s
Definition: os2.c:56
static long gzreader_charboundary(struct gzfile *gz, long n)
Definition: zlib.c:3706
static VALUE gzreader_gets(int, VALUE *, VALUE)
Definition: zlib.c:3727
#define ZSTREAM_INITIAL_BUFSIZE
Definition: zlib.c:511
VP_EXPORT void
Definition: bigdecimal.c:4944
VALUE mode
Definition: tcltklib.c:1655
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1416
static VALUE gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error)
Definition: zlib.c:2758
size_t length
Definition: tcltklib.c:4548
static VALUE gzfile_newstr(struct gzfile *gz, VALUE str)
Definition: zlib.c:2428
unsigned long ID
Definition: ruby.h:89
static VALUE rb_zstream_avail_in(VALUE)
Definition: zlib.c:1152
static VALUE gzfile_read(struct gzfile *, long)
Definition: zlib.c:2465
VALUE rb_exc_new2(VALUE etype, const char *s)
Definition: error.c:498
static VALUE rb_gzfile_set_orig_name(VALUE, VALUE)
Definition: zlib.c:2968
static void zlib_mem_free(voidpf, voidpf)
Definition: zlib.c:537
#define zstream_append_input2(z, v)
Definition: zlib.c:741
static VALUE rb_gzreader_s_open(int, VALUE *, VALUE)
Definition: zlib.c:3407
const char * name
Definition: oniguruma.h:158
static VALUE zstream_new(VALUE, const struct zstream_funcs *)
Definition: zlib.c:961
VALUE rb_io_close(VALUE)
Definition: io.c:3911
#define OBJ_TAINT(x)
Definition: ruby.h:964
int argc
Definition: tcltklib.c:1961
#define RBASIC(obj)
Definition: ruby.h:904
struct gzfile * gz
Definition: zlib.c:2020
#define FIX2INT(x)
Definition: ruby.h:538
static struct zstream * get_zstream(VALUE)
Definition: zlib.c:976
#define GZ_FLAG_MULTIPART
Definition: zlib.c:1914
#define rb_gzwriter_print
Definition: zlib.c:3329
static VALUE rb_gzfile_eof_p(VALUE)
Definition: zlib.c:3065
int(* reset)(z_streamp)
Definition: zlib.c:492
Definition: zlib.c:485
static VALUE rb_gzreader_each_byte(VALUE)
Definition: zlib.c:3621
VALUE rb_rescue2(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*r_proc)(ANYARGS), VALUE data2,...)
Definition: eval.c:634
struct zstream::zstream_funcs * func
static int rb_enc_dummy_p(rb_encoding *enc)
Definition: encoding.h:231
register unsigned int len
Definition: name2ctype.h:22210
static VALUE zstream_shift_buffer(struct zstream *, long)
Definition: zlib.c:669
#define xfree
Definition: defines.h:69
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:189
#define RUBY_ZLIB_VERSION
Definition: zlib.c:27
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
Definition: eval.c:737
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
static voidpf zlib_mem_alloc(voidpf, uInt, uInt)
Definition: zlib.c:525
static VALUE rb_gzreader_readlines(int, VALUE *, VALUE)
Definition: zlib.c:3905
static VALUE rb_deflate_initialize(int, VALUE *, VALUE)
Definition: zlib.c:1303
RUBY_EXTERN VALUE rb_rs
Definition: intern.h:473
#define GZ_FLAG_EXTRA
Definition: zlib.c:1915
VALUE orig_name
Definition: zlib.c:1959
void rb_sys_fail(const char *mesg)
Definition: error.c:1671
static VALUE zstream_end(struct zstream *)
Definition: zlib.c:808
static struct zstream_funcs inflate_funcs
Definition: zlib.c:519
void rb_jump_tag(int tag)
Definition: eval.c:598
VALUE rb_str_dup(VALUE)
Definition: string.c:905
#define GZFILE_READ_SIZE
Definition: zlib.c:1982
return ptr
Definition: tcltklib.c:780
#define FIXNUMARG(val, ifnil)
Definition: zlib.c:1227
VpDivd * c
Definition: bigdecimal.c:1163
static void gzfile_reset(struct gzfile *)
Definition: zlib.c:2049
static VALUE rb_gzfile_total_in(VALUE)
Definition: zlib.c:3112
static VALUE rb_deflate_flush(int, VALUE *, VALUE)
Definition: zlib.c:1500
#define _(args)
Definition: dln.h:28
state
Definition: gb18030.c:213
VALUE msg
Definition: tcltklib.c:842
#define Data_Make_Struct(klass, type, mark, free, sval)
Definition: ruby.h:820
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1231
#define rb_gzwriter_puts
Definition: zlib.c:3334
unsigned int uint32_t
Definition: sha2.h:101
static VALUE rb_gzwriter_write(VALUE, VALUE)
Definition: zlib.c:3287
static unsigned int gzfile_get16(const unsigned char *)
Definition: zlib.c:2158
#define OS_MSDOS
Definition: zlib.c:1925
#define OS_TOPS20
Definition: zlib.c:1932
arg
Definition: ripper.y:1287
Definition: zlib.c:1953
VALUE src
Definition: tcltklib.c:7940
static VALUE rb_zlib_crc32(int, VALUE *, VALUE)
Definition: zlib.c:430
#define OS_QDOS
Definition: zlib.c:1938
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:733
static VALUE inflate_run(VALUE)
Definition: zlib.c:1675
static VALUE rb_gzfile_total_out(VALUE)
Definition: zlib.c:3123
int size
Definition: encoding.c:51
static VALUE cZError
Definition: zlib.c:259
#define INT2FIX(i)
Definition: ruby.h:225
static VALUE do_checksum(int, VALUE *, uLong(*)(uLong, const Bytef *, uInt))
static void gzfile_make_header(struct gzfile *)
Definition: zlib.c:2217
static void gzfile_ungetbyte(struct gzfile *, int)
Definition: zlib.c:2595
#define ZSTREAM_AVAIL_OUT_STEP_MIN
Definition: zlib.c:513
static VALUE rb_inflate_initialize(int, VALUE *, VALUE)
Definition: zlib.c:1656
VALUE rb_exc_new3(VALUE etype, VALUE str)
Definition: error.c:504
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)
Definition: transcode.c:1452
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Definition: object.c:2094
static VALUE cDataError
Definition: zlib.c:260
static VALUE ULONG2NUM(unsigned long v)
Definition: ruby.h:1015
void Init_zlib(void)
Definition: zlib.c:4001
#define xmalloc
Definition: defines.h:64
static VALUE gzfile_ensure_close(VALUE)
Definition: zlib.c:2746
void rb_define_attr(VALUE klass, const char *name, int read, int write)
Defines (a) public accessor method(s) for an attribute.
Definition: class.c:1398
static VALUE rb_gzfile_set_lineno(VALUE, VALUE)
Definition: zlib.c:2929
static VALUE rb_zstream_flush_next_in(VALUE)
Definition: zlib.c:1094
#define NUM2ULONG(x)
Definition: ruby.h:515
static VALUE zstream_detach_buffer(struct zstream *)
Definition: zlib.c:648
static VALUE rb_gzreader_ungetc(VALUE, VALUE)
Definition: zlib.c:3639
static VALUE cStreamError
Definition: zlib.c:260
static VALUE rb_inflate_sync_point_p(VALUE)
Definition: zlib.c:1860
static void gzfile_raise(struct gzfile *gz, VALUE klass, const char *message)
Definition: zlib.c:2187
#define OS_VMCMS
Definition: zlib.c:1935
rb_ivar_set(einfo, ID_at_interp, interp)
#define ARG_MEMLEVEL(val)
Definition: zlib.c:1233
VALUE rb_check_string_type(VALUE)
Definition: string.c:1450
static VALUE zstream_sync(struct zstream *, Bytef *, long)
Definition: zlib.c:896
static VALUE gzfile_read_raw_rescue(VALUE)
Definition: zlib.c:2103
static void zstream_run(struct zstream *, Bytef *, long, int)
Definition: zlib.c:831
#define RTEST(v)
Definition: ruby.h:373
static VALUE rb_gzwriter_s_open(int, VALUE *, VALUE)
Definition: zlib.c:3208
#define T_STRING
Definition: ruby.h:418
static void zstream_append_buffer(struct zstream *, const Bytef *, long)
Definition: zlib.c:615
klass
Definition: tcltklib.c:3493
#define gzfile_reader_new(gz)
Definition: zlib.c:2046
char ch
Definition: yaml2byte.c:124
VALUE ecopts
Definition: zlib.c:1969
#define OBJ_INFECT(x, s)
Definition: ruby.h:967
static void zstream_free(struct zstream *)
Definition: zlib.c:952
VALUE * argv
Definition: zlib.c:2734
unsigned long flags
Definition: zlib.c:486
VALUE rb_uint2inum(VALUE n)
Definition: bignum.c:326
static VALUE rb_gzfile_orig_name(VALUE)
Definition: zlib.c:2885
static ID id_path
Definition: zlib.c:1946
static void zstream_finalize(struct zstream *)
Definition: zlib.c:942
#define rb_enc_left_char_head(s, p, e, enc)
Definition: encoding.h:163
static VALUE rb_inflate_inflate(VALUE, VALUE)
Definition: zlib.c:1775
VALUE rb_str_inspect(VALUE)
Definition: string.c:4410
gz io
Definition: zlib.c:2024
static VALUE rb_gzreader_s_allocate(VALUE)
Definition: zlib.c:3392
static VALUE rb_gzfile_set_sync(VALUE, VALUE)
Definition: zlib.c:3093
#define RETURN_ENUMERATOR(obj, argc, argv)
Definition: intern.h:210
VALUE rb_ary_new2(long capa)
Definition: array.c:332
#define rb_gzwriter_addstr
Definition: zlib.c:3319
static VALUE cNeedDict
Definition: zlib.c:259
static VALUE rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj)
Definition: zlib.c:3517
Real * res
Definition: bigdecimal.c:1189
static VALUE rb_gzreader_readline(int, VALUE *, VALUE)
Definition: zlib.c:3871
#define GZ_EXTRAFLAG_SLOW
Definition: zlib.c:1922
static VALUE rb_gzfile_close(VALUE)
Definition: zlib.c:3017
static char NUM2CHR(VALUE x)
Definition: ruby.h:1027
char * cbuf
Definition: zlib.c:1970
int ecflags
Definition: zlib.c:1968
#define rb_errinfo()
Definition: tcltklib.c:85
return rb_funcall(q->proc, ID_call, 0)
static VALUE gzfile_s_open(int, VALUE *, VALUE, const char *)
Definition: zlib.c:2810
unsigned long sum
Definition: zlib.c:354
static VALUE rb_gzreader_each(int, VALUE *, VALUE)
Definition: zlib.c:3887
#define GZFILE_CBUF_CAPA
Definition: zlib.c:1973
static VALUE rb_zstream_data_type(VALUE)
Definition: zlib.c:1183
static VALUE rb_deflate_init_copy(VALUE, VALUE)
Definition: zlib.c:1329
VALUE rb_int2inum(SIGNED_VALUE n)
Definition: bignum.c:333
static void gzfile_write(struct gzfile *, Bytef *, long)
Definition: zlib.c:2378
BDIGIT e
Definition: bigdecimal.c:4946
static VALUE rb_zstream_total_out(VALUE)
Definition: zlib.c:1172
VALUE opts
Definition: tcltklib.c:6135
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1110
#define FIX2UINT(x)
Definition: ruby.h:539
void rb_warning(const char *fmt,...)
Definition: error.c:212
#define GZ_METHOD_DEFLATE
Definition: zlib.c:1913
#define RSTRING_LENINT(str)
Definition: ruby.h:684
RUBY_EXTERN VALUE rb_eEOFError
Definition: ruby.h:1291
z_stream stream
Definition: zlib.c:490
#define GZ_FLAG_COMMENT
Definition: zlib.c:1917
rb_encoding * enc
Definition: zlib.c:1965
time_t mtime
Definition: zlib.c:1957
#define long
Definition: name2ctype.h:37
ssize_t i
Definition: bigdecimal.c:5519
#define zstream_init_deflate(z)
Definition: zlib.c:560
VALUE rb_define_module(const char *name)
Definition: class.c:587
static void zstream_reset(struct zstream *)
Definition: zlib.c:791
static long gzfile_read_more(struct gzfile *)
Definition: zlib.c:2393
static VALUE rb_inflate_sync(VALUE, VALUE)
Definition: zlib.c:1843
#define GZ_FLAG_UNKNOWN_MASK
Definition: zlib.c:1919
#define rb_intern(str)
long buf_filled
Definition: zlib.c:488
static void rb_gzfile_ecopts(struct gzfile *gz, VALUE opts)
Definition: zlib.c:3146
VALUE rb_str_buf_new(long)
Definition: string.c:736
static void gzfile_reader_rewind(struct gzfile *)
Definition: zlib.c:2649
VALUE buf
Definition: zlib.c:487
static VALUE rb_zstream_finish(VALUE)
Definition: zlib.c:1078
#define OS_UNIX
Definition: zlib.c:1928
rb_gc_mark(ptr->aliases)
#define zstream_append_buffer2(z, v)
Definition: zlib.c:644
VALUE time
Definition: tcltklib.c:1857
static void rscheck(const char *rsptr, long rslen, VALUE rs)
Definition: zlib.c:3699
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1210
static VALUE gzfile_reader_end_run(VALUE)
Definition: zlib.c:2627
VALUE rb_str_new2(const char *)
char * dst
Definition: tcltklib.c:9855
static VALUE rb_gzfile_s_wrap(int, VALUE *, VALUE)
Definition: zlib.c:2799
VALUE rb_str_to_str(VALUE)
Definition: string.c:808
static VALUE deflate_run(VALUE)
Definition: zlib.c:1351
unsigned long crc
Definition: zlib.c:1961
static VALUE rb_deflate_set_dictionary(VALUE, VALUE)
Definition: zlib.c:1579
VALUE rb_eArgError
Definition: error.c:468
#define zstream_inflate_new(klass)
Definition: zlib.c:973
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1032
static VALUE rb_gzreader_getbyte(VALUE obj)
Definition: zlib.c:3569
static VALUE gzfile_read_raw_partial(VALUE)
Definition: zlib.c:2092
VALUE rb_econv_str_convert(rb_econv_t *ec, VALUE src, int flags)
Definition: transcode.c:1875
#define StringValue(v)
Definition: ruby.h:466
rb_encoding * enc2
Definition: zlib.c:1966
gz mtime
Definition: zlib.c:2026
static VALUE rb_zlib_adler32(int, VALUE *, VALUE)
Definition: zlib.c:392
VALUE rb_str_new(const char *, long)
Definition: string.c:410