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