|
Ruby
1.9.3p537(2014-02-19revision0)
|
00001 /* 00002 * zlib.c - An interface for zlib. 00003 * 00004 * Copyright (C) UENO Katsuhiro 2000-2003 00005 * 00006 * $Id$ 00007 */ 00008 00009 #include <ruby.h> 00010 #include <zlib.h> 00011 #include <time.h> 00012 #include <ruby/io.h> 00013 00014 #ifdef HAVE_VALGRIND_MEMCHECK_H 00015 # include <valgrind/memcheck.h> 00016 # ifndef VALGRIND_MAKE_MEM_DEFINED 00017 # define VALGRIND_MAKE_MEM_DEFINED(p, n) VALGRIND_MAKE_READABLE((p), (n)) 00018 # endif 00019 # ifndef VALGRIND_MAKE_MEM_UNDEFINED 00020 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) VALGRIND_MAKE_WRITABLE((p), (n)) 00021 # endif 00022 #else 00023 # define VALGRIND_MAKE_MEM_DEFINED(p, n) /* empty */ 00024 # define VALGRIND_MAKE_MEM_UNDEFINED(p, n) /* empty */ 00025 #endif 00026 00027 #define RUBY_ZLIB_VERSION "0.6.0" 00028 00029 00030 #define OBJ_IS_FREED(val) (RBASIC(val)->flags == 0) 00031 00032 #ifndef GZIP_SUPPORT 00033 #define GZIP_SUPPORT 1 00034 #endif 00035 00036 /* from zutil.h */ 00037 #ifndef DEF_MEM_LEVEL 00038 #if MAX_MEM_LEVEL >= 8 00039 #define DEF_MEM_LEVEL 8 00040 #else 00041 #define DEF_MEM_LEVEL MAX_MEM_LEVEL 00042 #endif 00043 #endif 00044 00045 #if SIZEOF_LONG > SIZEOF_INT 00046 static inline uInt 00047 max_uint(long n) 00048 { 00049 if (n > UINT_MAX) n = UINT_MAX; 00050 return (uInt)n; 00051 } 00052 #define MAX_UINT(n) max_uint(n) 00053 #else 00054 #define MAX_UINT(n) (uInt)(n) 00055 #endif 00056 00057 #define sizeof(x) ((int)sizeof(x)) 00058 00059 /*--------- Prototypes --------*/ 00060 00061 static NORETURN(void raise_zlib_error(int, const char*)); 00062 static VALUE rb_zlib_version(VALUE); 00063 static VALUE do_checksum(int, VALUE*, uLong (*)(uLong, const Bytef*, uInt)); 00064 static VALUE rb_zlib_adler32(int, VALUE*, VALUE); 00065 static VALUE rb_zlib_crc32(int, VALUE*, VALUE); 00066 static VALUE rb_zlib_crc_table(VALUE); 00067 static voidpf zlib_mem_alloc(voidpf, uInt, uInt); 00068 static void zlib_mem_free(voidpf, voidpf); 00069 static void finalizer_warn(const char*); 00070 00071 struct zstream; 00072 struct zstream_funcs; 00073 static void zstream_init(struct zstream*, const struct zstream_funcs*); 00074 static void zstream_expand_buffer(struct zstream*); 00075 static void zstream_expand_buffer_into(struct zstream*, unsigned long); 00076 static void zstream_append_buffer(struct zstream*, const Bytef*, long); 00077 static VALUE zstream_detach_buffer(struct zstream*); 00078 static VALUE zstream_shift_buffer(struct zstream*, long); 00079 static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long); 00080 static void zstream_buffer_ungetbyte(struct zstream*, int); 00081 static void zstream_append_input(struct zstream*, const Bytef*, long); 00082 static void zstream_discard_input(struct zstream*, long); 00083 static void zstream_reset_input(struct zstream*); 00084 static void zstream_passthrough_input(struct zstream*); 00085 static VALUE zstream_detach_input(struct zstream*); 00086 static void zstream_reset(struct zstream*); 00087 static VALUE zstream_end(struct zstream*); 00088 static void zstream_run(struct zstream*, Bytef*, long, int); 00089 static VALUE zstream_sync(struct zstream*, Bytef*, long); 00090 static void zstream_mark(struct zstream*); 00091 static void zstream_free(struct zstream*); 00092 static VALUE zstream_new(VALUE, const struct zstream_funcs*); 00093 static struct zstream *get_zstream(VALUE); 00094 static void zstream_finalize(struct zstream*); 00095 00096 static VALUE rb_zstream_end(VALUE); 00097 static VALUE rb_zstream_reset(VALUE); 00098 static VALUE rb_zstream_finish(VALUE); 00099 static VALUE rb_zstream_flush_next_in(VALUE); 00100 static VALUE rb_zstream_flush_next_out(VALUE); 00101 static VALUE rb_zstream_avail_out(VALUE); 00102 static VALUE rb_zstream_set_avail_out(VALUE, VALUE); 00103 static VALUE rb_zstream_avail_in(VALUE); 00104 static VALUE rb_zstream_total_in(VALUE); 00105 static VALUE rb_zstream_total_out(VALUE); 00106 static VALUE rb_zstream_data_type(VALUE); 00107 static VALUE rb_zstream_adler(VALUE); 00108 static VALUE rb_zstream_finished_p(VALUE); 00109 static VALUE rb_zstream_closed_p(VALUE); 00110 00111 static VALUE rb_deflate_s_allocate(VALUE); 00112 static VALUE rb_deflate_initialize(int, VALUE*, VALUE); 00113 static VALUE rb_deflate_init_copy(VALUE, VALUE); 00114 static VALUE deflate_run(VALUE); 00115 static VALUE rb_deflate_s_deflate(int, VALUE*, VALUE); 00116 static void do_deflate(struct zstream*, VALUE, int); 00117 static VALUE rb_deflate_deflate(int, VALUE*, VALUE); 00118 static VALUE rb_deflate_addstr(VALUE, VALUE); 00119 static VALUE rb_deflate_flush(int, VALUE*, VALUE); 00120 static VALUE rb_deflate_params(VALUE, VALUE, VALUE); 00121 static VALUE rb_deflate_set_dictionary(VALUE, VALUE); 00122 00123 static VALUE inflate_run(VALUE); 00124 static VALUE rb_inflate_s_allocate(VALUE); 00125 static VALUE rb_inflate_initialize(int, VALUE*, VALUE); 00126 static VALUE rb_inflate_s_inflate(VALUE, VALUE); 00127 static void do_inflate(struct zstream*, VALUE); 00128 static VALUE rb_inflate_inflate(VALUE, VALUE); 00129 static VALUE rb_inflate_addstr(VALUE, VALUE); 00130 static VALUE rb_inflate_sync(VALUE, VALUE); 00131 static VALUE rb_inflate_sync_point_p(VALUE); 00132 static VALUE rb_inflate_set_dictionary(VALUE, VALUE); 00133 00134 #if GZIP_SUPPORT 00135 struct gzfile; 00136 static void gzfile_mark(struct gzfile*); 00137 static void gzfile_free(struct gzfile*); 00138 static VALUE gzfile_new(VALUE, const struct zstream_funcs*, void (*) _((struct gzfile*))); 00139 static void gzfile_reset(struct gzfile*); 00140 static void gzfile_close(struct gzfile*, int); 00141 static void gzfile_write_raw(struct gzfile*); 00142 static VALUE gzfile_read_raw_partial(VALUE); 00143 static VALUE gzfile_read_raw_rescue(VALUE); 00144 static VALUE gzfile_read_raw(struct gzfile*); 00145 static int gzfile_read_raw_ensure(struct gzfile*, long); 00146 static char *gzfile_read_raw_until_zero(struct gzfile*, long); 00147 static unsigned int gzfile_get16(const unsigned char*); 00148 static unsigned long gzfile_get32(const unsigned char*); 00149 static void gzfile_set32(unsigned long n, unsigned char*); 00150 static void gzfile_make_header(struct gzfile*); 00151 static void gzfile_make_footer(struct gzfile*); 00152 static void gzfile_read_header(struct gzfile*); 00153 static void gzfile_check_footer(struct gzfile*); 00154 static void gzfile_write(struct gzfile*, Bytef*, long); 00155 static long gzfile_read_more(struct gzfile*); 00156 static void gzfile_calc_crc(struct gzfile*, VALUE); 00157 static VALUE gzfile_read(struct gzfile*, long); 00158 static VALUE gzfile_read_all(struct gzfile*); 00159 static void gzfile_ungets(struct gzfile*, const Bytef*, long); 00160 static void gzfile_ungetbyte(struct gzfile*, int); 00161 static VALUE gzfile_writer_end_run(VALUE); 00162 static void gzfile_writer_end(struct gzfile*); 00163 static VALUE gzfile_reader_end_run(VALUE); 00164 static void gzfile_reader_end(struct gzfile*); 00165 static void gzfile_reader_rewind(struct gzfile*); 00166 static VALUE gzfile_reader_get_unused(struct gzfile*); 00167 static struct gzfile *get_gzfile(VALUE); 00168 static VALUE gzfile_ensure_close(VALUE); 00169 static VALUE rb_gzfile_s_wrap(int, VALUE*, VALUE); 00170 static VALUE gzfile_s_open(int, VALUE*, VALUE, const char*); 00171 NORETURN(static void gzfile_raise(struct gzfile *, VALUE, const char *)); 00172 static VALUE gzfile_error_inspect(VALUE); 00173 00174 static VALUE rb_gzfile_to_io(VALUE); 00175 static VALUE rb_gzfile_crc(VALUE); 00176 static VALUE rb_gzfile_mtime(VALUE); 00177 static VALUE rb_gzfile_level(VALUE); 00178 static VALUE rb_gzfile_os_code(VALUE); 00179 static VALUE rb_gzfile_orig_name(VALUE); 00180 static VALUE rb_gzfile_comment(VALUE); 00181 static VALUE rb_gzfile_lineno(VALUE); 00182 static VALUE rb_gzfile_set_lineno(VALUE, VALUE); 00183 static VALUE rb_gzfile_set_mtime(VALUE, VALUE); 00184 static VALUE rb_gzfile_set_orig_name(VALUE, VALUE); 00185 static VALUE rb_gzfile_set_comment(VALUE, VALUE); 00186 static VALUE rb_gzfile_close(VALUE); 00187 static VALUE rb_gzfile_finish(VALUE); 00188 static VALUE rb_gzfile_closed_p(VALUE); 00189 static VALUE rb_gzfile_eof_p(VALUE); 00190 static VALUE rb_gzfile_sync(VALUE); 00191 static VALUE rb_gzfile_set_sync(VALUE, VALUE); 00192 static VALUE rb_gzfile_total_in(VALUE); 00193 static VALUE rb_gzfile_total_out(VALUE); 00194 static VALUE rb_gzfile_path(VALUE); 00195 00196 static VALUE rb_gzwriter_s_allocate(VALUE); 00197 static VALUE rb_gzwriter_s_open(int, VALUE*, VALUE); 00198 static VALUE rb_gzwriter_initialize(int, VALUE*, VALUE); 00199 static VALUE rb_gzwriter_flush(int, VALUE*, VALUE); 00200 static VALUE rb_gzwriter_write(VALUE, VALUE); 00201 static VALUE rb_gzwriter_putc(VALUE, VALUE); 00202 00203 static VALUE rb_gzreader_s_allocate(VALUE); 00204 static VALUE rb_gzreader_s_open(int, VALUE*, VALUE); 00205 static VALUE rb_gzreader_initialize(int, VALUE*, VALUE); 00206 static VALUE rb_gzreader_rewind(VALUE); 00207 static VALUE rb_gzreader_unused(VALUE); 00208 static VALUE rb_gzreader_read(int, VALUE*, VALUE); 00209 static VALUE rb_gzreader_getc(VALUE); 00210 static VALUE rb_gzreader_readchar(VALUE); 00211 static VALUE rb_gzreader_each_byte(VALUE); 00212 static VALUE rb_gzreader_ungetc(VALUE, VALUE); 00213 static VALUE rb_gzreader_ungetbyte(VALUE, VALUE); 00214 static void gzreader_skip_linebreaks(struct gzfile*); 00215 static VALUE gzreader_gets(int, VALUE*, VALUE); 00216 static VALUE rb_gzreader_gets(int, VALUE*, VALUE); 00217 static VALUE rb_gzreader_readline(int, VALUE*, VALUE); 00218 static VALUE rb_gzreader_each(int, VALUE*, VALUE); 00219 static VALUE rb_gzreader_readlines(int, VALUE*, VALUE); 00220 #endif /* GZIP_SUPPORT */ 00221 00222 /* 00223 * Document-module: Zlib 00224 * 00225 * == Overview 00226 * 00227 * Access to the zlib library. 00228 * 00229 * == Class tree 00230 * 00231 * - Zlib::Deflate 00232 * - Zlib::Inflate 00233 * - Zlib::ZStream 00234 * - Zlib::Error 00235 * - Zlib::StreamEnd 00236 * - Zlib::NeedDict 00237 * - Zlib::DataError 00238 * - Zlib::StreamError 00239 * - Zlib::MemError 00240 * - Zlib::BufError 00241 * - Zlib::VersionError 00242 * 00243 * (if you have GZIP_SUPPORT) 00244 * - Zlib::GzipReader 00245 * - Zlib::GzipWriter 00246 * - Zlib::GzipFile 00247 * - Zlib::GzipFile::Error 00248 * - Zlib::GzipFile::LengthError 00249 * - Zlib::GzipFile::CRCError 00250 * - Zlib::GzipFile::NoFooter 00251 * 00252 * see also zlib.h 00253 * 00254 */ 00255 void Init_zlib(void); 00256 00257 /*--------- Exceptions --------*/ 00258 00259 static VALUE cZError, cStreamEnd, cNeedDict; 00260 static VALUE cStreamError, cDataError, cMemError, cBufError, cVersionError; 00261 00262 static void 00263 raise_zlib_error(int err, const char *msg) 00264 { 00265 VALUE exc; 00266 00267 if (!msg) { 00268 msg = zError(err); 00269 } 00270 00271 switch(err) { 00272 case Z_STREAM_END: 00273 exc = rb_exc_new2(cStreamEnd, msg); 00274 break; 00275 case Z_NEED_DICT: 00276 exc = rb_exc_new2(cNeedDict, msg); 00277 break; 00278 case Z_STREAM_ERROR: 00279 exc = rb_exc_new2(cStreamError, msg); 00280 break; 00281 case Z_DATA_ERROR: 00282 exc = rb_exc_new2(cDataError, msg); 00283 break; 00284 case Z_BUF_ERROR: 00285 exc = rb_exc_new2(cBufError, msg); 00286 break; 00287 case Z_VERSION_ERROR: 00288 exc = rb_exc_new2(cVersionError, msg); 00289 break; 00290 case Z_MEM_ERROR: 00291 exc = rb_exc_new2(cMemError, msg); 00292 break; 00293 case Z_ERRNO: 00294 rb_sys_fail(msg); 00295 /* no return */ 00296 default: 00297 exc = rb_exc_new3(cZError, 00298 rb_sprintf("unknown zlib error %d: %s", err, msg)); 00299 } 00300 00301 rb_exc_raise(exc); 00302 } 00303 00304 00305 /*--- Warning (in finalizer) ---*/ 00306 00307 static void 00308 finalizer_warn(const char *msg) 00309 { 00310 fprintf(stderr, "zlib(finalizer): %s\n", msg); 00311 } 00312 00313 00314 /*-------- module Zlib --------*/ 00315 00316 /* 00317 * Document-method: Zlib.zlib_version 00318 * 00319 * Returns the string which represents the version of zlib library. 00320 */ 00321 static VALUE 00322 rb_zlib_version(VALUE klass) 00323 { 00324 VALUE str; 00325 00326 str = rb_str_new2(zlibVersion()); 00327 OBJ_TAINT(str); /* for safe */ 00328 return str; 00329 } 00330 00331 #if SIZEOF_LONG > SIZEOF_INT 00332 static uLong 00333 checksum_long(uLong (*func)(uLong, const Bytef*, uInt), uLong sum, const Bytef *ptr, long len) 00334 { 00335 if (len > UINT_MAX) { 00336 do { 00337 sum = func(sum, ptr, UINT_MAX); 00338 ptr += UINT_MAX; 00339 len -= UINT_MAX; 00340 } while (len >= UINT_MAX); 00341 } 00342 if (len > 0) sum = func(sum, ptr, (uInt)len); 00343 return sum; 00344 } 00345 #else 00346 #define checksum_long(func, sum, ptr, len) (func)((sum), (ptr), (len)) 00347 #endif 00348 00349 static VALUE 00350 do_checksum(argc, argv, func) 00351 int argc; 00352 VALUE *argv; 00353 uLong (*func)(uLong, const Bytef*, uInt); 00354 { 00355 VALUE str, vsum; 00356 unsigned long sum; 00357 00358 rb_scan_args(argc, argv, "02", &str, &vsum); 00359 00360 if (!NIL_P(vsum)) { 00361 sum = NUM2ULONG(vsum); 00362 } 00363 else if (NIL_P(str)) { 00364 sum = 0; 00365 } 00366 else { 00367 sum = func(0, Z_NULL, 0); 00368 } 00369 00370 if (NIL_P(str)) { 00371 sum = func(sum, Z_NULL, 0); 00372 } 00373 else { 00374 StringValue(str); 00375 sum = checksum_long(func, sum, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str)); 00376 } 00377 return rb_uint2inum(sum); 00378 } 00379 00380 /* 00381 * Document-method: Zlib.adler32 00382 * 00383 * call-seq: Zlib.adler32(string, adler) 00384 * 00385 * Calculates Adler-32 checksum for +string+, and returns updated value of 00386 * +adler+. If +string+ is omitted, it returns the Adler-32 initial value. If 00387 * +adler+ is omitted, it assumes that the initial value is given to +adler+. 00388 * 00389 * FIXME: expression. 00390 */ 00391 static VALUE 00392 rb_zlib_adler32(int argc, VALUE *argv, VALUE klass) 00393 { 00394 return do_checksum(argc, argv, adler32); 00395 } 00396 00397 #ifdef HAVE_ADLER32_COMBINE 00398 /* 00399 * Document-method: Zlib.adler32_combine 00400 * 00401 * call-seq: Zlib.adler32_combine(adler1, adler2, len2) 00402 * 00403 * Combine two Adler-32 check values in to one. +alder1+ is the first Adler-32 00404 * value, +adler2+ is the second Adler-32 value. +len2+ is the length of the 00405 * string used to generate +adler2+. 00406 * 00407 */ 00408 static VALUE 00409 rb_zlib_adler32_combine(VALUE klass, VALUE adler1, VALUE adler2, VALUE len2) 00410 { 00411 return ULONG2NUM( 00412 adler32_combine(NUM2ULONG(adler1), NUM2ULONG(adler2), NUM2LONG(len2))); 00413 } 00414 #else 00415 #define rb_zlib_adler32_combine rb_f_notimplement 00416 #endif 00417 00418 /* 00419 * Document-method: Zlib.crc32 00420 * 00421 * call-seq: Zlib.crc32(string, adler) 00422 * 00423 * Calculates CRC checksum for +string+, and returns updated value of +crc+. If 00424 * +string+ is omitted, it returns the CRC initial value. If +crc+ is omitted, it 00425 * assumes that the initial value is given to +crc+. 00426 * 00427 * FIXME: expression. 00428 */ 00429 static VALUE 00430 rb_zlib_crc32(int argc, VALUE *argv, VALUE klass) 00431 { 00432 return do_checksum(argc, argv, crc32); 00433 } 00434 00435 #ifdef HAVE_CRC32_COMBINE 00436 /* 00437 * Document-method: Zlib.crc32_combine 00438 * 00439 * call-seq: Zlib.crc32_combine(crc1, crc2, len2) 00440 * 00441 * Combine two CRC-32 check values in to one. +crc1+ is the first CRC-32 00442 * value, +crc2+ is the second CRC-32 value. +len2+ is the length of the 00443 * string used to generate +crc2+. 00444 * 00445 */ 00446 static VALUE 00447 rb_zlib_crc32_combine(VALUE klass, VALUE crc1, VALUE crc2, VALUE len2) 00448 { 00449 return ULONG2NUM( 00450 crc32_combine(NUM2ULONG(crc1), NUM2ULONG(crc2), NUM2LONG(len2))); 00451 } 00452 #else 00453 #define rb_zlib_crc32_combine rb_f_notimplement 00454 #endif 00455 00456 /* 00457 * Document-method: Zlib.crc_table 00458 * 00459 * Returns the table for calculating CRC checksum as an array. 00460 */ 00461 static VALUE 00462 rb_zlib_crc_table(VALUE obj) 00463 { 00464 #if !defined(HAVE_TYPE_Z_CRC_T) 00465 /* z_crc_t is defined since zlib-1.2.7. */ 00466 typedef unsigned long z_crc_t; 00467 #endif 00468 const z_crc_t *crctbl; 00469 VALUE dst; 00470 int i; 00471 00472 crctbl = get_crc_table(); 00473 dst = rb_ary_new2(256); 00474 00475 for (i = 0; i < 256; i++) { 00476 rb_ary_push(dst, rb_uint2inum(crctbl[i])); 00477 } 00478 return dst; 00479 } 00480 00481 00482 00483 /*-------- zstream - internal APIs --------*/ 00484 00485 struct zstream { 00486 unsigned long flags; 00487 VALUE buf; 00488 long buf_filled; 00489 VALUE input; 00490 z_stream stream; 00491 const struct zstream_funcs { 00492 int (*reset)(z_streamp); 00493 int (*end)(z_streamp); 00494 int (*run)(z_streamp, int); 00495 } *func; 00496 }; 00497 00498 #define ZSTREAM_FLAG_READY 0x1 00499 #define ZSTREAM_FLAG_IN_STREAM 0x2 00500 #define ZSTREAM_FLAG_FINISHED 0x4 00501 #define ZSTREAM_FLAG_CLOSING 0x8 00502 #define ZSTREAM_FLAG_UNUSED 0x10 00503 00504 #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY) 00505 #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY) 00506 #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED) 00507 #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING) 00508 00509 /* I think that more better value should be found, 00510 but I gave up finding it. B) */ 00511 #define ZSTREAM_INITIAL_BUFSIZE 1024 00512 #define ZSTREAM_AVAIL_OUT_STEP_MAX 16384 00513 #define ZSTREAM_AVAIL_OUT_STEP_MIN 2048 00514 00515 static const struct zstream_funcs deflate_funcs = { 00516 deflateReset, deflateEnd, deflate, 00517 }; 00518 00519 static const struct zstream_funcs inflate_funcs = { 00520 inflateReset, inflateEnd, inflate, 00521 }; 00522 00523 00524 static voidpf 00525 zlib_mem_alloc(voidpf opaque, uInt items, uInt size) 00526 { 00527 voidpf p = xmalloc(items * size); 00528 /* zlib FAQ: Valgrind (or some similar memory access checker) says that 00529 deflate is performing a conditional jump that depends on an 00530 uninitialized value. Isn't that a bug? 00531 http://www.zlib.net/zlib_faq.html#faq36 */ 00532 VALGRIND_MAKE_MEM_DEFINED(p, items * size); 00533 return p; 00534 } 00535 00536 static void 00537 zlib_mem_free(voidpf opaque, voidpf address) 00538 { 00539 xfree(address); 00540 } 00541 00542 static void 00543 zstream_init(struct zstream *z, const struct zstream_funcs *func) 00544 { 00545 z->flags = 0; 00546 z->buf = Qnil; 00547 z->buf_filled = 0; 00548 z->input = Qnil; 00549 z->stream.zalloc = zlib_mem_alloc; 00550 z->stream.zfree = zlib_mem_free; 00551 z->stream.opaque = Z_NULL; 00552 z->stream.msg = Z_NULL; 00553 z->stream.next_in = Z_NULL; 00554 z->stream.avail_in = 0; 00555 z->stream.next_out = Z_NULL; 00556 z->stream.avail_out = 0; 00557 z->func = func; 00558 } 00559 00560 #define zstream_init_deflate(z) zstream_init((z), &deflate_funcs) 00561 #define zstream_init_inflate(z) zstream_init((z), &inflate_funcs) 00562 00563 static void 00564 zstream_expand_buffer(struct zstream *z) 00565 { 00566 long inc; 00567 00568 if (NIL_P(z->buf)) { 00569 /* I uses rb_str_new here not rb_str_buf_new because 00570 rb_str_buf_new makes a zero-length string. */ 00571 z->buf = rb_str_new(0, ZSTREAM_INITIAL_BUFSIZE); 00572 z->buf_filled = 0; 00573 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); 00574 z->stream.avail_out = ZSTREAM_INITIAL_BUFSIZE; 00575 RBASIC(z->buf)->klass = 0; 00576 return; 00577 } 00578 00579 if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { 00580 /* to keep other threads from freezing */ 00581 z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; 00582 } 00583 else { 00584 inc = z->buf_filled / 2; 00585 if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) { 00586 inc = ZSTREAM_AVAIL_OUT_STEP_MIN; 00587 } 00588 rb_str_resize(z->buf, z->buf_filled + inc); 00589 z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ? 00590 (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX; 00591 } 00592 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; 00593 } 00594 00595 static void 00596 zstream_expand_buffer_into(struct zstream *z, unsigned long size) 00597 { 00598 if (NIL_P(z->buf)) { 00599 /* I uses rb_str_new here not rb_str_buf_new because 00600 rb_str_buf_new makes a zero-length string. */ 00601 z->buf = rb_str_new(0, size); 00602 z->buf_filled = 0; 00603 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); 00604 z->stream.avail_out = MAX_UINT(size); 00605 RBASIC(z->buf)->klass = 0; 00606 } 00607 else if (z->stream.avail_out != size) { 00608 rb_str_resize(z->buf, z->buf_filled + size); 00609 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; 00610 z->stream.avail_out = MAX_UINT(size); 00611 } 00612 } 00613 00614 static void 00615 zstream_append_buffer(struct zstream *z, const Bytef *src, long len) 00616 { 00617 if (NIL_P(z->buf)) { 00618 z->buf = rb_str_buf_new(len); 00619 rb_str_buf_cat(z->buf, (const char*)src, len); 00620 z->buf_filled = len; 00621 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); 00622 z->stream.avail_out = 0; 00623 RBASIC(z->buf)->klass = 0; 00624 return; 00625 } 00626 00627 if (RSTRING_LEN(z->buf) < z->buf_filled + len) { 00628 rb_str_resize(z->buf, z->buf_filled + len); 00629 z->stream.avail_out = 0; 00630 } 00631 else { 00632 if (z->stream.avail_out >= (uInt)len) { 00633 z->stream.avail_out -= (uInt)len; 00634 } 00635 else { 00636 z->stream.avail_out = 0; 00637 } 00638 } 00639 memcpy(RSTRING_PTR(z->buf) + z->buf_filled, src, len); 00640 z->buf_filled += len; 00641 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; 00642 } 00643 00644 #define zstream_append_buffer2(z,v) \ 00645 zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v)) 00646 00647 static VALUE 00648 zstream_detach_buffer(struct zstream *z) 00649 { 00650 VALUE dst; 00651 00652 if (NIL_P(z->buf)) { 00653 dst = rb_str_new(0, 0); 00654 } 00655 else { 00656 dst = z->buf; 00657 rb_str_resize(dst, z->buf_filled); 00658 RBASIC(dst)->klass = rb_cString; 00659 } 00660 00661 z->buf = Qnil; 00662 z->buf_filled = 0; 00663 z->stream.next_out = 0; 00664 z->stream.avail_out = 0; 00665 return dst; 00666 } 00667 00668 static VALUE 00669 zstream_shift_buffer(struct zstream *z, long len) 00670 { 00671 VALUE dst; 00672 long buflen; 00673 00674 if (z->buf_filled <= len) { 00675 return zstream_detach_buffer(z); 00676 } 00677 00678 dst = rb_str_subseq(z->buf, 0, len); 00679 RBASIC(dst)->klass = rb_cString; 00680 z->buf_filled -= len; 00681 memmove(RSTRING_PTR(z->buf), RSTRING_PTR(z->buf) + len, 00682 z->buf_filled); 00683 z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; 00684 buflen = RSTRING_LEN(z->buf) - z->buf_filled; 00685 if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) { 00686 buflen = ZSTREAM_AVAIL_OUT_STEP_MAX; 00687 } 00688 z->stream.avail_out = (uInt)buflen; 00689 00690 return dst; 00691 } 00692 00693 static void 00694 zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len) 00695 { 00696 if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) { 00697 zstream_expand_buffer_into(z, len); 00698 } 00699 00700 memmove(RSTRING_PTR(z->buf) + len, RSTRING_PTR(z->buf), z->buf_filled); 00701 memmove(RSTRING_PTR(z->buf), b, len); 00702 z->buf_filled+=len; 00703 if (z->stream.avail_out > 0) { 00704 if (len > z->stream.avail_out) len = z->stream.avail_out; 00705 z->stream.next_out+=len; 00706 z->stream.avail_out-=(uInt)len; 00707 } 00708 } 00709 00710 static void 00711 zstream_buffer_ungetbyte(struct zstream *z, int c) 00712 { 00713 if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) { 00714 zstream_expand_buffer(z); 00715 } 00716 00717 memmove(RSTRING_PTR(z->buf) + 1, RSTRING_PTR(z->buf), z->buf_filled); 00718 RSTRING_PTR(z->buf)[0] = (char)c; 00719 z->buf_filled++; 00720 if (z->stream.avail_out > 0) { 00721 z->stream.next_out++; 00722 z->stream.avail_out--; 00723 } 00724 } 00725 00726 static void 00727 zstream_append_input(struct zstream *z, const Bytef *src, long len) 00728 { 00729 if (len <= 0) return; 00730 00731 if (NIL_P(z->input)) { 00732 z->input = rb_str_buf_new(len); 00733 rb_str_buf_cat(z->input, (const char*)src, len); 00734 RBASIC(z->input)->klass = 0; 00735 } 00736 else { 00737 rb_str_buf_cat(z->input, (const char*)src, len); 00738 } 00739 } 00740 00741 #define zstream_append_input2(z,v)\ 00742 RB_GC_GUARD(v),\ 00743 zstream_append_input((z), (Bytef*)RSTRING_PTR(v), RSTRING_LEN(v)) 00744 00745 static void 00746 zstream_discard_input(struct zstream *z, long len) 00747 { 00748 if (NIL_P(z->input) || RSTRING_LEN(z->input) <= len) { 00749 z->input = Qnil; 00750 } 00751 else { 00752 memmove(RSTRING_PTR(z->input), RSTRING_PTR(z->input) + len, 00753 RSTRING_LEN(z->input) - len); 00754 rb_str_resize(z->input, RSTRING_LEN(z->input) - len); 00755 } 00756 } 00757 00758 static void 00759 zstream_reset_input(struct zstream *z) 00760 { 00761 z->input = Qnil; 00762 } 00763 00764 static void 00765 zstream_passthrough_input(struct zstream *z) 00766 { 00767 if (!NIL_P(z->input)) { 00768 zstream_append_buffer2(z, z->input); 00769 z->input = Qnil; 00770 } 00771 } 00772 00773 static VALUE 00774 zstream_detach_input(struct zstream *z) 00775 { 00776 VALUE dst; 00777 00778 if (NIL_P(z->input)) { 00779 dst = rb_str_new(0, 0); 00780 } 00781 else { 00782 dst = z->input; 00783 RBASIC(dst)->klass = rb_cString; 00784 } 00785 z->input = Qnil; 00786 RBASIC(dst)->klass = rb_cString; 00787 return dst; 00788 } 00789 00790 static void 00791 zstream_reset(struct zstream *z) 00792 { 00793 int err; 00794 00795 err = z->func->reset(&z->stream); 00796 if (err != Z_OK) { 00797 raise_zlib_error(err, z->stream.msg); 00798 } 00799 z->flags = ZSTREAM_FLAG_READY; 00800 z->buf = Qnil; 00801 z->buf_filled = 0; 00802 z->stream.next_out = 0; 00803 z->stream.avail_out = 0; 00804 zstream_reset_input(z); 00805 } 00806 00807 static VALUE 00808 zstream_end(struct zstream *z) 00809 { 00810 int err; 00811 00812 if (!ZSTREAM_IS_READY(z)) { 00813 rb_warning("attempt to close uninitialized zstream; ignored."); 00814 return Qnil; 00815 } 00816 if (z->flags & ZSTREAM_FLAG_IN_STREAM) { 00817 rb_warning("attempt to close unfinished zstream; reset forced."); 00818 zstream_reset(z); 00819 } 00820 00821 zstream_reset_input(z); 00822 err = z->func->end(&z->stream); 00823 if (err != Z_OK) { 00824 raise_zlib_error(err, z->stream.msg); 00825 } 00826 z->flags = 0; 00827 return Qnil; 00828 } 00829 00830 static void 00831 zstream_run(struct zstream *z, Bytef *src, long len, int flush) 00832 { 00833 uInt n; 00834 int err; 00835 volatile VALUE guard = Qnil; 00836 00837 if (NIL_P(z->input) && len == 0) { 00838 z->stream.next_in = (Bytef*)""; 00839 z->stream.avail_in = 0; 00840 } 00841 else { 00842 zstream_append_input(z, src, len); 00843 z->stream.next_in = (Bytef*)RSTRING_PTR(z->input); 00844 z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input)); 00845 /* keep reference to `z->input' so as not to be garbage collected 00846 after zstream_reset_input() and prevent `z->stream.next_in' 00847 from dangling. */ 00848 guard = z->input; 00849 } 00850 00851 if (z->stream.avail_out == 0) { 00852 zstream_expand_buffer(z); 00853 } 00854 00855 for (;;) { 00856 /* VC allocates err and guard to same address. accessing err and guard 00857 in same scope prevents it. */ 00858 RB_GC_GUARD(guard); 00859 n = z->stream.avail_out; 00860 err = z->func->run(&z->stream, flush); 00861 z->buf_filled += n - z->stream.avail_out; 00862 rb_thread_schedule(); 00863 00864 if (err == Z_STREAM_END) { 00865 z->flags &= ~ZSTREAM_FLAG_IN_STREAM; 00866 z->flags |= ZSTREAM_FLAG_FINISHED; 00867 break; 00868 } 00869 if (err != Z_OK) { 00870 if (flush != Z_FINISH && err == Z_BUF_ERROR 00871 && z->stream.avail_out > 0) { 00872 z->flags |= ZSTREAM_FLAG_IN_STREAM; 00873 break; 00874 } 00875 zstream_reset_input(z); 00876 if (z->stream.avail_in > 0) { 00877 zstream_append_input(z, z->stream.next_in, z->stream.avail_in); 00878 } 00879 raise_zlib_error(err, z->stream.msg); 00880 } 00881 if (z->stream.avail_out > 0) { 00882 z->flags |= ZSTREAM_FLAG_IN_STREAM; 00883 break; 00884 } 00885 zstream_expand_buffer(z); 00886 } 00887 00888 zstream_reset_input(z); 00889 if (z->stream.avail_in > 0) { 00890 zstream_append_input(z, z->stream.next_in, z->stream.avail_in); 00891 guard = Qnil; /* prevent tail call to make guard effective */ 00892 } 00893 } 00894 00895 static VALUE 00896 zstream_sync(struct zstream *z, Bytef *src, long len) 00897 { 00898 VALUE rest; 00899 int err; 00900 00901 if (!NIL_P(z->input)) { 00902 z->stream.next_in = (Bytef*)RSTRING_PTR(z->input); 00903 z->stream.avail_in = MAX_UINT(RSTRING_LEN(z->input)); 00904 err = inflateSync(&z->stream); 00905 if (err == Z_OK) { 00906 zstream_discard_input(z, 00907 RSTRING_LEN(z->input) - z->stream.avail_in); 00908 zstream_append_input(z, src, len); 00909 return Qtrue; 00910 } 00911 zstream_reset_input(z); 00912 if (err != Z_DATA_ERROR) { 00913 rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); 00914 raise_zlib_error(err, z->stream.msg); 00915 } 00916 } 00917 00918 if (len <= 0) return Qfalse; 00919 00920 z->stream.next_in = src; 00921 z->stream.avail_in = MAX_UINT(len); 00922 err = inflateSync(&z->stream); 00923 if (err == Z_OK) { 00924 zstream_append_input(z, z->stream.next_in, z->stream.avail_in); 00925 return Qtrue; 00926 } 00927 if (err != Z_DATA_ERROR) { 00928 rest = rb_str_new((char*)z->stream.next_in, z->stream.avail_in); 00929 raise_zlib_error(err, z->stream.msg); 00930 } 00931 return Qfalse; 00932 } 00933 00934 static void 00935 zstream_mark(struct zstream *z) 00936 { 00937 rb_gc_mark(z->buf); 00938 rb_gc_mark(z->input); 00939 } 00940 00941 static void 00942 zstream_finalize(struct zstream *z) 00943 { 00944 int err = z->func->end(&z->stream); 00945 if (err == Z_STREAM_ERROR) 00946 finalizer_warn("the stream state was inconsistent."); 00947 if (err == Z_DATA_ERROR) 00948 finalizer_warn("the stream was freed prematurely."); 00949 } 00950 00951 static void 00952 zstream_free(struct zstream *z) 00953 { 00954 if (ZSTREAM_IS_READY(z)) { 00955 zstream_finalize(z); 00956 } 00957 xfree(z); 00958 } 00959 00960 static VALUE 00961 zstream_new(VALUE klass, const struct zstream_funcs *funcs) 00962 { 00963 VALUE obj; 00964 struct zstream *z; 00965 00966 obj = Data_Make_Struct(klass, struct zstream, 00967 zstream_mark, zstream_free, z); 00968 zstream_init(z, funcs); 00969 return obj; 00970 } 00971 00972 #define zstream_deflate_new(klass) zstream_new((klass), &deflate_funcs) 00973 #define zstream_inflate_new(klass) zstream_new((klass), &inflate_funcs) 00974 00975 static struct zstream * 00976 get_zstream(VALUE obj) 00977 { 00978 struct zstream *z; 00979 00980 Data_Get_Struct(obj, struct zstream, z); 00981 if (!ZSTREAM_IS_READY(z)) { 00982 rb_raise(cZError, "stream is not ready"); 00983 } 00984 return z; 00985 } 00986 00987 00988 /* ------------------------------------------------------------------------- */ 00989 00990 /* 00991 * Document-class: Zlib::ZStream 00992 * 00993 * Zlib::ZStream is the abstract class for the stream which handles the 00994 * compressed data. The operations are defined in the subclasses: 00995 * Zlib::Deflate for compression, and Zlib::Inflate for decompression. 00996 * 00997 * An instance of Zlib::ZStream has one stream (struct zstream in the source) 00998 * and two variable-length buffers which associated to the input (next_in) of 00999 * the stream and the output (next_out) of the stream. In this document, 01000 * "input buffer" means the buffer for input, and "output buffer" means the 01001 * buffer for output. 01002 * 01003 * Data input into an instance of Zlib::ZStream are temporally stored into 01004 * the end of input buffer, and then data in input buffer are processed from 01005 * the beginning of the buffer until no more output from the stream is 01006 * produced (i.e. until avail_out > 0 after processing). During processing, 01007 * output buffer is allocated and expanded automatically to hold all output 01008 * data. 01009 * 01010 * Some particular instance methods consume the data in output buffer and 01011 * return them as a String. 01012 * 01013 * Here is an ascii art for describing above: 01014 * 01015 * +================ an instance of Zlib::ZStream ================+ 01016 * || || 01017 * || +--------+ +-------+ +--------+ || 01018 * || +--| output |<---------|zstream|<---------| input |<--+ || 01019 * || | | buffer | next_out+-------+next_in | buffer | | || 01020 * || | +--------+ +--------+ | || 01021 * || | | || 01022 * +===|======================================================|===+ 01023 * | | 01024 * v | 01025 * "output data" "input data" 01026 * 01027 * If an error occurs during processing input buffer, an exception which is a 01028 * subclass of Zlib::Error is raised. At that time, both input and output 01029 * buffer keep their conditions at the time when the error occurs. 01030 * 01031 * == Method Catalogue 01032 * 01033 * Many of the methods in this class are fairly low-level and unlikely to be 01034 * of interest to users. In fact, users are unlikely to use this class 01035 * directly; rather they will be interested in Zlib::Inflate and 01036 * Zlib::Deflate. 01037 * 01038 * The higher level methods are listed below. 01039 * 01040 * - #total_in 01041 * - #total_out 01042 * - #data_type 01043 * - #adler 01044 * - #reset 01045 * - #finish 01046 * - #finished? 01047 * - #close 01048 * - #closed? 01049 */ 01050 01051 /* 01052 * Closes the stream. All operations on the closed stream will raise an 01053 * exception. 01054 */ 01055 static VALUE 01056 rb_zstream_end(VALUE obj) 01057 { 01058 zstream_end(get_zstream(obj)); 01059 return Qnil; 01060 } 01061 01062 /* 01063 * Resets and initializes the stream. All data in both input and output buffer 01064 * are discarded. 01065 */ 01066 static VALUE 01067 rb_zstream_reset(VALUE obj) 01068 { 01069 zstream_reset(get_zstream(obj)); 01070 return Qnil; 01071 } 01072 01073 /* 01074 * Finishes the stream and flushes output buffer. See Zlib::Deflate#finish and 01075 * Zlib::Inflate#finish for details of this behavior. 01076 */ 01077 static VALUE 01078 rb_zstream_finish(VALUE obj) 01079 { 01080 struct zstream *z = get_zstream(obj); 01081 VALUE dst; 01082 01083 zstream_run(z, (Bytef*)"", 0, Z_FINISH); 01084 dst = zstream_detach_buffer(z); 01085 01086 OBJ_INFECT(dst, obj); 01087 return dst; 01088 } 01089 01090 /* 01091 * Flushes input buffer and returns all data in that buffer. 01092 */ 01093 static VALUE 01094 rb_zstream_flush_next_in(VALUE obj) 01095 { 01096 struct zstream *z; 01097 VALUE dst; 01098 01099 Data_Get_Struct(obj, struct zstream, z); 01100 dst = zstream_detach_input(z); 01101 OBJ_INFECT(dst, obj); 01102 return dst; 01103 } 01104 01105 /* 01106 * Flushes output buffer and returns all data in that buffer. 01107 */ 01108 static VALUE 01109 rb_zstream_flush_next_out(VALUE obj) 01110 { 01111 struct zstream *z; 01112 VALUE dst; 01113 01114 Data_Get_Struct(obj, struct zstream, z); 01115 dst = zstream_detach_buffer(z); 01116 OBJ_INFECT(dst, obj); 01117 return dst; 01118 } 01119 01120 /* 01121 * Returns number of bytes of free spaces in output buffer. Because the free 01122 * space is allocated automatically, this method returns 0 normally. 01123 */ 01124 static VALUE 01125 rb_zstream_avail_out(VALUE obj) 01126 { 01127 struct zstream *z; 01128 Data_Get_Struct(obj, struct zstream, z); 01129 return rb_uint2inum(z->stream.avail_out); 01130 } 01131 01132 /* 01133 * Allocates +size+ bytes of free space in the output buffer. If there are more 01134 * than +size+ bytes already in the buffer, the buffer is truncated. Because 01135 * free space is allocated automatically, you usually don't need to use this 01136 * method. 01137 */ 01138 static VALUE 01139 rb_zstream_set_avail_out(VALUE obj, VALUE size) 01140 { 01141 struct zstream *z = get_zstream(obj); 01142 01143 Check_Type(size, T_FIXNUM); 01144 zstream_expand_buffer_into(z, FIX2INT(size)); 01145 return size; 01146 } 01147 01148 /* 01149 * Returns bytes of data in the input buffer. Normally, returns 0. 01150 */ 01151 static VALUE 01152 rb_zstream_avail_in(VALUE obj) 01153 { 01154 struct zstream *z; 01155 Data_Get_Struct(obj, struct zstream, z); 01156 return INT2FIX(NIL_P(z->input) ? 0 : (int)(RSTRING_LEN(z->input))); 01157 } 01158 01159 /* 01160 * Returns the total bytes of the input data to the stream. FIXME 01161 */ 01162 static VALUE 01163 rb_zstream_total_in(VALUE obj) 01164 { 01165 return rb_uint2inum(get_zstream(obj)->stream.total_in); 01166 } 01167 01168 /* 01169 * Returns the total bytes of the output data from the stream. FIXME 01170 */ 01171 static VALUE 01172 rb_zstream_total_out(VALUE obj) 01173 { 01174 return rb_uint2inum(get_zstream(obj)->stream.total_out); 01175 } 01176 01177 /* 01178 * Guesses the type of the data which have been inputed into the stream. The 01179 * returned value is either <tt>BINARY</tt>, <tt>ASCII</tt>, or 01180 * <tt>UNKNOWN</tt>. 01181 */ 01182 static VALUE 01183 rb_zstream_data_type(VALUE obj) 01184 { 01185 return INT2FIX(get_zstream(obj)->stream.data_type); 01186 } 01187 01188 /* 01189 * Returns the adler-32 checksum. 01190 */ 01191 static VALUE 01192 rb_zstream_adler(VALUE obj) 01193 { 01194 return rb_uint2inum(get_zstream(obj)->stream.adler); 01195 } 01196 01197 /* 01198 * Returns true if the stream is finished. 01199 */ 01200 static VALUE 01201 rb_zstream_finished_p(VALUE obj) 01202 { 01203 return ZSTREAM_IS_FINISHED(get_zstream(obj)) ? Qtrue : Qfalse; 01204 } 01205 01206 /* 01207 * Returns true if the stream is closed. 01208 */ 01209 static VALUE 01210 rb_zstream_closed_p(VALUE obj) 01211 { 01212 struct zstream *z; 01213 Data_Get_Struct(obj, struct zstream, z); 01214 return ZSTREAM_IS_READY(z) ? Qfalse : Qtrue; 01215 } 01216 01217 01218 /* ------------------------------------------------------------------------- */ 01219 01220 /* 01221 * Document-class: Zlib::Deflate 01222 * 01223 * Zlib::Deflate is the class for compressing data. See Zlib::ZStream for more 01224 * information. 01225 */ 01226 01227 #define FIXNUMARG(val, ifnil) \ 01228 (NIL_P((val)) ? (ifnil) \ 01229 : ((void)Check_Type((val), T_FIXNUM), FIX2INT((val)))) 01230 01231 #define ARG_LEVEL(val) FIXNUMARG((val), Z_DEFAULT_COMPRESSION) 01232 #define ARG_WBITS(val) FIXNUMARG((val), MAX_WBITS) 01233 #define ARG_MEMLEVEL(val) FIXNUMARG((val), DEF_MEM_LEVEL) 01234 #define ARG_STRATEGY(val) FIXNUMARG((val), Z_DEFAULT_STRATEGY) 01235 #define ARG_FLUSH(val) FIXNUMARG((val), Z_NO_FLUSH) 01236 01237 01238 static VALUE 01239 rb_deflate_s_allocate(VALUE klass) 01240 { 01241 return zstream_deflate_new(klass); 01242 } 01243 01244 /* 01245 * Document-method: Zlib::Deflate.new 01246 * 01247 * call-seq: Zlib::Deflate.new(level=nil, windowBits=nil, memlevel=nil, strategy=nil) 01248 * 01249 * == Arguments 01250 * 01251 * +level+:: 01252 * An Integer compression level between 01253 * BEST_SPEED and BEST_COMPRESSION 01254 * +windowBits+:: 01255 * An Integer for the windowBits size. Should be 01256 * in the range 8..15, larger values of this parameter 01257 * result in better at the expense of memory usage. 01258 * +memlevel+:: 01259 * Specifies how much memory should be allocated for 01260 * the internal compression state. 01261 * Between DEF_MEM_LEVEL and MAX_MEM_LEVEL 01262 * +strategy+:: 01263 * A parameter to tune the compression algorithm. Use the 01264 * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a 01265 * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no 01266 * string match). 01267 * 01268 * == Description 01269 * 01270 * Creates a new deflate stream for compression. See zlib.h for details of 01271 * each argument. If an argument is nil, the default value of that argument is 01272 * used. 01273 * 01274 * 01275 * == examples 01276 * 01277 * === basic 01278 * 01279 * f = File.new("compressed.file","w+") 01280 * #=> #<File:compressed.file> 01281 * f << Zlib::Deflate.new().deflate(File.read("big.file")) 01282 * #=> #<File:compressed.file> 01283 * f.close 01284 * #=> nil 01285 * 01286 * === a little more robust 01287 * 01288 * compressed_file = File.open("compressed.file", "w+") 01289 * #=> #<File:compressed.file> 01290 * zd = Zlib::Deflate.new(Zlib::BEST_COMPRESSION, 15, Zlib::MAX_MEM_LEVEL, Zlib::HUFFMAN_ONLY) 01291 * #=> #<Zlib::Deflate:0x000000008610a0> 01292 * compressed_file << zd.deflate(File.read("big.file")) 01293 * #=> "\xD4z\xC6\xDE\b\xA1K\x1Ej\x8A ..." 01294 * compressed_file.close 01295 * #=> nil 01296 * zd.close 01297 * #=> nil 01298 * 01299 * (while this example will work, for best optimization the flags need to be reviewed for your specific function) 01300 * 01301 */ 01302 static VALUE 01303 rb_deflate_initialize(int argc, VALUE *argv, VALUE obj) 01304 { 01305 struct zstream *z; 01306 VALUE level, wbits, memlevel, strategy; 01307 int err; 01308 01309 rb_scan_args(argc, argv, "04", &level, &wbits, &memlevel, &strategy); 01310 Data_Get_Struct(obj, struct zstream, z); 01311 01312 err = deflateInit2(&z->stream, ARG_LEVEL(level), Z_DEFLATED, 01313 ARG_WBITS(wbits), ARG_MEMLEVEL(memlevel), 01314 ARG_STRATEGY(strategy)); 01315 if (err != Z_OK) { 01316 raise_zlib_error(err, z->stream.msg); 01317 } 01318 ZSTREAM_READY(z); 01319 01320 return obj; 01321 } 01322 01323 /* 01324 * Document-method: Zlib::Deflate#initialize_copy 01325 * 01326 * Duplicates the deflate stream. 01327 */ 01328 static VALUE 01329 rb_deflate_init_copy(VALUE self, VALUE orig) 01330 { 01331 struct zstream *z1, *z2; 01332 int err; 01333 01334 Data_Get_Struct(self, struct zstream, z1); 01335 z2 = get_zstream(orig); 01336 01337 err = deflateCopy(&z1->stream, &z2->stream); 01338 if (err != Z_OK) { 01339 raise_zlib_error(err, 0); 01340 } 01341 z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input); 01342 z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf); 01343 z1->buf_filled = z2->buf_filled; 01344 z1->flags = z2->flags; 01345 01346 return self; 01347 } 01348 01349 static VALUE 01350 deflate_run(VALUE args) 01351 { 01352 struct zstream *z = (struct zstream*)((VALUE*)args)[0]; 01353 VALUE src = ((VALUE*)args)[1]; 01354 01355 zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_FINISH); 01356 return zstream_detach_buffer(z); 01357 } 01358 01359 /* 01360 * Document-method: Zlib::Deflate.deflate 01361 * 01362 * call-seq: Zlib.deflate(string[, level]) 01363 * Zlib::Deflate.deflate(string[, level]) 01364 * 01365 * Compresses the given +string+. Valid values of level are 01366 * <tt>NO_COMPRESSION</tt>, <tt>BEST_SPEED</tt>, 01367 * <tt>BEST_COMPRESSION</tt>, <tt>DEFAULT_COMPRESSION</tt>, and an 01368 * integer from 0 to 9 (the default is 6). 01369 * 01370 * This method is almost equivalent to the following code: 01371 * 01372 * def deflate(string, level) 01373 * z = Zlib::Deflate.new(level) 01374 * dst = z.deflate(string, Zlib::NO_FLUSH) 01375 * z.close 01376 * dst 01377 * end 01378 * 01379 * See also Zlib.inflate 01380 * 01381 */ 01382 static VALUE 01383 rb_deflate_s_deflate(int argc, VALUE *argv, VALUE klass) 01384 { 01385 struct zstream z; 01386 VALUE src, level, dst, args[2]; 01387 int err, lev; 01388 01389 rb_scan_args(argc, argv, "11", &src, &level); 01390 01391 lev = ARG_LEVEL(level); 01392 StringValue(src); 01393 zstream_init_deflate(&z); 01394 err = deflateInit(&z.stream, lev); 01395 if (err != Z_OK) { 01396 raise_zlib_error(err, z.stream.msg); 01397 } 01398 ZSTREAM_READY(&z); 01399 01400 args[0] = (VALUE)&z; 01401 args[1] = src; 01402 dst = rb_ensure(deflate_run, (VALUE)args, zstream_end, (VALUE)&z); 01403 01404 OBJ_INFECT(dst, src); 01405 return dst; 01406 } 01407 01408 static void 01409 do_deflate(struct zstream *z, VALUE src, int flush) 01410 { 01411 if (NIL_P(src)) { 01412 zstream_run(z, (Bytef*)"", 0, Z_FINISH); 01413 return; 01414 } 01415 StringValue(src); 01416 if (flush != Z_NO_FLUSH || RSTRING_LEN(src) > 0) { /* prevent BUF_ERROR */ 01417 zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), flush); 01418 } 01419 } 01420 01421 /* 01422 * Document-method: Zlib.deflate 01423 * 01424 * call-seq: deflate(string[, flush]) 01425 * 01426 * == Arguments 01427 * 01428 * +string+:: 01429 * String 01430 * 01431 * +flush+:: 01432 * Integer representing a flush code. Either NO_FLUSH, 01433 * SYNC_FLUSH, FULL_FLUSH, or FINISH. See zlib.h for details. 01434 * Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to 01435 * decide how much data to accumulate before producing output, in order to 01436 * maximize compression. 01437 * 01438 * == Description 01439 * 01440 * Inputs +string+ into the deflate stream and returns the output from the 01441 * stream. On calling this method, both the input and the output buffers of 01442 * the stream are flushed. 01443 * 01444 * If +string+ is nil, this method finishes the 01445 * stream, just like Zlib::ZStream#finish. 01446 * 01447 * == Usage 01448 * 01449 * comp = Zlib.deflate(File.read("big.file")) 01450 * or 01451 * comp = Zlib.deflate(File.read("big.file"), Zlib::FULL_FLUSH) 01452 * 01453 */ 01454 static VALUE 01455 rb_deflate_deflate(int argc, VALUE *argv, VALUE obj) 01456 { 01457 struct zstream *z = get_zstream(obj); 01458 VALUE src, flush, dst; 01459 01460 rb_scan_args(argc, argv, "11", &src, &flush); 01461 OBJ_INFECT(obj, src); 01462 do_deflate(z, src, ARG_FLUSH(flush)); 01463 dst = zstream_detach_buffer(z); 01464 01465 OBJ_INFECT(dst, obj); 01466 return dst; 01467 } 01468 01469 /* 01470 * Document-method: Zlib::Deflate.<< 01471 * 01472 * call-seq: << string 01473 * 01474 * Inputs +string+ into the deflate stream just like Zlib::Deflate#deflate, but 01475 * returns the Zlib::Deflate object itself. The output from the stream is 01476 * preserved in output buffer. 01477 */ 01478 static VALUE 01479 rb_deflate_addstr(VALUE obj, VALUE src) 01480 { 01481 OBJ_INFECT(obj, src); 01482 do_deflate(get_zstream(obj), src, Z_NO_FLUSH); 01483 return obj; 01484 } 01485 01486 /* 01487 * Document-method: Zlib::Deflate#flush 01488 * 01489 * call-seq: flush(flush) 01490 * 01491 * This method is equivalent to <tt>deflate('', flush)</tt>. If flush is omitted, 01492 * <tt>SYNC_FLUSH</tt> is used as flush. This method is just provided 01493 * to improve the readability of your Ruby program. 01494 * 01495 * Please visit your zlib.h for a deeper detail on NO_FLUSH, SYNC_FLUSH, FULL_FLUSH, and FINISH 01496 * 01497 */ 01498 static VALUE 01499 rb_deflate_flush(int argc, VALUE *argv, VALUE obj) 01500 { 01501 struct zstream *z = get_zstream(obj); 01502 VALUE v_flush, dst; 01503 int flush; 01504 01505 rb_scan_args(argc, argv, "01", &v_flush); 01506 flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH); 01507 if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */ 01508 zstream_run(z, (Bytef*)"", 0, flush); 01509 } 01510 dst = zstream_detach_buffer(z); 01511 01512 OBJ_INFECT(dst, obj); 01513 return dst; 01514 } 01515 01516 /* 01517 * Document-method: Zlib::Deflate.params 01518 * 01519 * call-seq: params(level, strategy) 01520 * 01521 * Changes the parameters of the deflate stream. See zlib.h for details. The 01522 * output from the stream by changing the params is preserved in output 01523 * buffer. 01524 * 01525 * +level+:: 01526 * An Integer compression level between 01527 * BEST_SPEED and BEST_COMPRESSION 01528 * +strategy+:: 01529 * A parameter to tune the compression algorithm. Use the 01530 * DEFAULT_STRATEGY for normal data, FILTERED for data produced by a 01531 * filter (or predictor), HUFFMAN_ONLY to force Huffman encoding only (no 01532 * string match). 01533 * 01534 */ 01535 static VALUE 01536 rb_deflate_params(VALUE obj, VALUE v_level, VALUE v_strategy) 01537 { 01538 struct zstream *z = get_zstream(obj); 01539 int level, strategy; 01540 int err; 01541 uInt n; 01542 01543 level = ARG_LEVEL(v_level); 01544 strategy = ARG_STRATEGY(v_strategy); 01545 01546 n = z->stream.avail_out; 01547 err = deflateParams(&z->stream, level, strategy); 01548 z->buf_filled += n - z->stream.avail_out; 01549 while (err == Z_BUF_ERROR) { 01550 rb_warning("deflateParams() returned Z_BUF_ERROR"); 01551 zstream_expand_buffer(z); 01552 n = z->stream.avail_out; 01553 err = deflateParams(&z->stream, level, strategy); 01554 z->buf_filled += n - z->stream.avail_out; 01555 } 01556 if (err != Z_OK) { 01557 raise_zlib_error(err, z->stream.msg); 01558 } 01559 01560 return Qnil; 01561 } 01562 01563 /* 01564 * Document-method: Zlib::Deflate.set_dictionary 01565 * 01566 * call-seq: set_dictionary(string) 01567 * 01568 * Sets the preset dictionary and returns +string+. This method is available 01569 * just only after Zlib::Deflate.new or Zlib::ZStream#reset method was called. 01570 * See zlib.h for details. 01571 * 01572 * Can raise errors of Z_STREAM_ERROR if a parameter is invalid (such as 01573 * NULL dictionary) or the stream state is inconsistent, Z_DATA_ERROR if 01574 * the given dictionary doesn't match the expected one (incorrect adler32 value) 01575 * 01576 */ 01577 static VALUE 01578 rb_deflate_set_dictionary(VALUE obj, VALUE dic) 01579 { 01580 struct zstream *z = get_zstream(obj); 01581 VALUE src = dic; 01582 int err; 01583 01584 OBJ_INFECT(obj, dic); 01585 StringValue(src); 01586 err = deflateSetDictionary(&z->stream, 01587 (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); 01588 if (err != Z_OK) { 01589 raise_zlib_error(err, z->stream.msg); 01590 } 01591 01592 return dic; 01593 } 01594 01595 01596 /* ------------------------------------------------------------------------- */ 01597 01598 /* 01599 * Document-class: Zlib::Inflate 01600 * 01601 * Zlib:Inflate is the class for decompressing compressed data. Unlike 01602 * Zlib::Deflate, an instance of this class is not able to duplicate (clone, 01603 * dup) itself. 01604 */ 01605 01606 01607 01608 static VALUE 01609 rb_inflate_s_allocate(VALUE klass) 01610 { 01611 return zstream_inflate_new(klass); 01612 } 01613 01614 /* 01615 * Document-method: Zlib::Inflate.new 01616 * 01617 * call-seq: Zlib::Inflate.new(window_bits) 01618 * 01619 * == Arguments 01620 * 01621 * +windowBits+:: 01622 * An Integer for the windowBits size. Should be 01623 * in the range 8..15, larger values of this parameter 01624 * result in better at the expense of memory usage. 01625 * 01626 * == Description 01627 * 01628 * Creates a new inflate stream for decompression. See zlib.h for details 01629 * of the argument. If +window_bits+ is +nil+, the default value is used. 01630 * 01631 * == Example 01632 * 01633 * cf = File.open("compressed.file") 01634 * ucf = File.open("uncompressed.file", "w+") 01635 * zi = Zlib::Inflate.new(Zlib::MAX_WBITS) 01636 * 01637 * ucf << zi.inflate(cf.read) 01638 * 01639 * ucf.close 01640 * zi.close 01641 * cf.close 01642 * 01643 * or 01644 * 01645 * File.open("compressed.file") {|cf| 01646 * zi = Zlib::Inflate.new 01647 * File.open("uncompressed.file", "w+") {|ucf| 01648 * ucf << zi.inflate(cf.read) 01649 * } 01650 * zi.close 01651 * } 01652 * 01653 */ 01654 static VALUE 01655 rb_inflate_initialize(int argc, VALUE *argv, VALUE obj) 01656 { 01657 struct zstream *z; 01658 VALUE wbits; 01659 int err; 01660 01661 rb_scan_args(argc, argv, "01", &wbits); 01662 Data_Get_Struct(obj, struct zstream, z); 01663 01664 err = inflateInit2(&z->stream, ARG_WBITS(wbits)); 01665 if (err != Z_OK) { 01666 raise_zlib_error(err, z->stream.msg); 01667 } 01668 ZSTREAM_READY(z); 01669 01670 return obj; 01671 } 01672 01673 static VALUE 01674 inflate_run(VALUE args) 01675 { 01676 struct zstream *z = (struct zstream*)((VALUE*)args)[0]; 01677 VALUE src = ((VALUE*)args)[1]; 01678 01679 zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH); 01680 zstream_run(z, (Bytef*)"", 0, Z_FINISH); /* for checking errors */ 01681 return zstream_detach_buffer(z); 01682 } 01683 01684 /* 01685 * Document-method: Zlib::Inflate.inflate 01686 * 01687 * call-seq: Zlib::Inflate.inflate(string) 01688 * 01689 * Decompresses +string+. Raises a Zlib::NeedDict exception if a preset 01690 * dictionary is needed for decompression. 01691 * 01692 * This method is almost equivalent to the following code: 01693 * 01694 * def inflate(string) 01695 * zstream = Zlib::Inflate.new 01696 * buf = zstream.inflate(string) 01697 * zstream.finish 01698 * zstream.close 01699 * buf 01700 * end 01701 * 01702 * See also Zlib.deflate 01703 * 01704 */ 01705 static VALUE 01706 rb_inflate_s_inflate(VALUE obj, VALUE src) 01707 { 01708 struct zstream z; 01709 VALUE dst, args[2]; 01710 int err; 01711 01712 StringValue(src); 01713 zstream_init_inflate(&z); 01714 err = inflateInit(&z.stream); 01715 if (err != Z_OK) { 01716 raise_zlib_error(err, z.stream.msg); 01717 } 01718 ZSTREAM_READY(&z); 01719 01720 args[0] = (VALUE)&z; 01721 args[1] = src; 01722 dst = rb_ensure(inflate_run, (VALUE)args, zstream_end, (VALUE)&z); 01723 01724 OBJ_INFECT(dst, src); 01725 return dst; 01726 } 01727 01728 static void 01729 do_inflate(struct zstream *z, VALUE src) 01730 { 01731 if (NIL_P(src)) { 01732 zstream_run(z, (Bytef*)"", 0, Z_FINISH); 01733 return; 01734 } 01735 StringValue(src); 01736 if (RSTRING_LEN(src) > 0 || z->stream.avail_in > 0) { /* prevent Z_BUF_ERROR */ 01737 zstream_run(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src), Z_SYNC_FLUSH); 01738 } 01739 } 01740 01741 /* 01742 * Document-method: Zlib::Inflate#inflate 01743 * 01744 * call-seq: inflate(string) 01745 * 01746 * Inputs +string+ into the inflate stream and returns the output from the 01747 * stream. Calling this method, both the input and the output buffer of the 01748 * stream are flushed. If string is +nil+, this method finishes the stream, 01749 * just like Zlib::ZStream#finish. 01750 * 01751 * Raises a Zlib::NeedDict exception if a preset dictionary is needed to 01752 * decompress. Set the dictionary by Zlib::Inflate#set_dictionary and then 01753 * call this method again with an empty string to flush the stream: 01754 * 01755 * inflater = Zlib::Inflate.new 01756 * 01757 * begin 01758 * out = inflater.inflate compressed 01759 * rescue Zlib::NeedDict 01760 * # ensure the dictionary matches the stream's required dictionary 01761 * raise unless inflater.adler == Zlib.adler32(dictionary) 01762 * 01763 * inflater.set_dictionary dictionary 01764 * inflater.inflate '' 01765 * end 01766 * 01767 * # ... 01768 * 01769 * inflater.close 01770 * 01771 * See also Zlib::Inflate.new 01772 */ 01773 static VALUE 01774 rb_inflate_inflate(VALUE obj, VALUE src) 01775 { 01776 struct zstream *z = get_zstream(obj); 01777 VALUE dst; 01778 01779 OBJ_INFECT(obj, src); 01780 01781 if (ZSTREAM_IS_FINISHED(z)) { 01782 if (NIL_P(src)) { 01783 dst = zstream_detach_buffer(z); 01784 } 01785 else { 01786 StringValue(src); 01787 zstream_append_buffer2(z, src); 01788 dst = rb_str_new(0, 0); 01789 } 01790 } 01791 else { 01792 do_inflate(z, src); 01793 dst = zstream_detach_buffer(z); 01794 if (ZSTREAM_IS_FINISHED(z)) { 01795 zstream_passthrough_input(z); 01796 } 01797 } 01798 01799 OBJ_INFECT(dst, obj); 01800 return dst; 01801 } 01802 01803 /* 01804 * call-seq: << string 01805 * 01806 * Inputs +string+ into the inflate stream just like Zlib::Inflate#inflate, but 01807 * returns the Zlib::Inflate object itself. The output from the stream is 01808 * preserved in output buffer. 01809 */ 01810 static VALUE 01811 rb_inflate_addstr(VALUE obj, VALUE src) 01812 { 01813 struct zstream *z = get_zstream(obj); 01814 01815 OBJ_INFECT(obj, src); 01816 01817 if (ZSTREAM_IS_FINISHED(z)) { 01818 if (!NIL_P(src)) { 01819 StringValue(src); 01820 zstream_append_buffer2(z, src); 01821 } 01822 } 01823 else { 01824 do_inflate(z, src); 01825 if (ZSTREAM_IS_FINISHED(z)) { 01826 zstream_passthrough_input(z); 01827 } 01828 } 01829 01830 return obj; 01831 } 01832 01833 /* 01834 * call-seq: sync(string) 01835 * 01836 * Inputs +string+ into the end of input buffer and skips data until a full 01837 * flush point can be found. If the point is found in the buffer, this method 01838 * flushes the buffer and returns false. Otherwise it returns +true+ and the 01839 * following data of full flush point is preserved in the buffer. 01840 */ 01841 static VALUE 01842 rb_inflate_sync(VALUE obj, VALUE src) 01843 { 01844 struct zstream *z = get_zstream(obj); 01845 01846 OBJ_INFECT(obj, src); 01847 StringValue(src); 01848 return zstream_sync(z, (Bytef*)RSTRING_PTR(src), RSTRING_LEN(src)); 01849 } 01850 01851 /* 01852 * Quoted verbatim from original documentation: 01853 * 01854 * What is this? 01855 * 01856 * <tt>:)</tt> 01857 */ 01858 static VALUE 01859 rb_inflate_sync_point_p(VALUE obj) 01860 { 01861 struct zstream *z = get_zstream(obj); 01862 int err; 01863 01864 err = inflateSyncPoint(&z->stream); 01865 if (err == 1) { 01866 return Qtrue; 01867 } 01868 if (err != Z_OK) { 01869 raise_zlib_error(err, z->stream.msg); 01870 } 01871 return Qfalse; 01872 } 01873 01874 /* 01875 * Document-method: Zlib::Inflate#set_dictionary 01876 * 01877 * Sets the preset dictionary and returns +string+. This method is available just 01878 * only after a Zlib::NeedDict exception was raised. See zlib.h for details. 01879 * 01880 */ 01881 static VALUE 01882 rb_inflate_set_dictionary(VALUE obj, VALUE dic) 01883 { 01884 struct zstream *z = get_zstream(obj); 01885 VALUE src = dic; 01886 int err; 01887 01888 OBJ_INFECT(obj, dic); 01889 StringValue(src); 01890 err = inflateSetDictionary(&z->stream, 01891 (Bytef*)RSTRING_PTR(src), RSTRING_LENINT(src)); 01892 if (err != Z_OK) { 01893 raise_zlib_error(err, z->stream.msg); 01894 } 01895 01896 return dic; 01897 } 01898 01899 01900 01901 #if GZIP_SUPPORT 01902 01903 /* NOTE: Features for gzip files of Ruby/zlib are written from scratch 01904 * and using undocumented feature of zlib, negative wbits. 01905 * I don't think gzFile APIs of zlib are good for Ruby. 01906 */ 01907 01908 /*------- .gz file header --------*/ 01909 01910 #define GZ_MAGIC1 0x1f 01911 #define GZ_MAGIC2 0x8b 01912 #define GZ_METHOD_DEFLATE 8 01913 #define GZ_FLAG_MULTIPART 0x2 01914 #define GZ_FLAG_EXTRA 0x4 01915 #define GZ_FLAG_ORIG_NAME 0x8 01916 #define GZ_FLAG_COMMENT 0x10 01917 #define GZ_FLAG_ENCRYPT 0x20 01918 #define GZ_FLAG_UNKNOWN_MASK 0xc0 01919 01920 #define GZ_EXTRAFLAG_FAST 0x4 01921 #define GZ_EXTRAFLAG_SLOW 0x2 01922 01923 /* from zutil.h */ 01924 #define OS_MSDOS 0x00 01925 #define OS_AMIGA 0x01 01926 #define OS_VMS 0x02 01927 #define OS_UNIX 0x03 01928 #define OS_ATARI 0x05 01929 #define OS_OS2 0x06 01930 #define OS_MACOS 0x07 01931 #define OS_TOPS20 0x0a 01932 #define OS_WIN32 0x0b 01933 01934 #define OS_VMCMS 0x04 01935 #define OS_ZSYSTEM 0x08 01936 #define OS_CPM 0x09 01937 #define OS_QDOS 0x0c 01938 #define OS_RISCOS 0x0d 01939 #define OS_UNKNOWN 0xff 01940 01941 #ifndef OS_CODE 01942 #define OS_CODE OS_UNIX 01943 #endif 01944 01945 static ID id_write, id_read, id_readpartial, id_flush, id_seek, id_close, id_path, id_input; 01946 static VALUE cGzError, cNoFooter, cCRCError, cLengthError; 01947 01948 01949 01950 /*-------- gzfile internal APIs --------*/ 01951 01952 struct gzfile { 01953 struct zstream z; 01954 VALUE io; 01955 int level; 01956 time_t mtime; /* for header */ 01957 int os_code; /* for header */ 01958 VALUE orig_name; /* for header; must be a String */ 01959 VALUE comment; /* for header; must be a String */ 01960 unsigned long crc; 01961 int lineno; 01962 long ungetc; 01963 void (*end)(struct gzfile *); 01964 rb_encoding *enc; 01965 rb_encoding *enc2; 01966 rb_econv_t *ec; 01967 int ecflags; 01968 VALUE ecopts; 01969 char *cbuf; 01970 VALUE path; 01971 }; 01972 #define GZFILE_CBUF_CAPA 10 01973 01974 #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED 01975 #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1) 01976 #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2) 01977 01978 #define GZFILE_IS_FINISHED(gz) \ 01979 (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0) 01980 01981 #define GZFILE_READ_SIZE 2048 01982 01983 01984 static void 01985 gzfile_mark(struct gzfile *gz) 01986 { 01987 rb_gc_mark(gz->io); 01988 rb_gc_mark(gz->orig_name); 01989 rb_gc_mark(gz->comment); 01990 zstream_mark(&gz->z); 01991 rb_gc_mark(gz->ecopts); 01992 rb_gc_mark(gz->path); 01993 } 01994 01995 static void 01996 gzfile_free(struct gzfile *gz) 01997 { 01998 struct zstream *z = &gz->z; 01999 02000 if (ZSTREAM_IS_READY(z)) { 02001 if (z->func == &deflate_funcs) { 02002 finalizer_warn("Zlib::GzipWriter object must be closed explicitly."); 02003 } 02004 zstream_finalize(z); 02005 } 02006 if (gz->cbuf) { 02007 xfree(gz->cbuf); 02008 } 02009 xfree(gz); 02010 } 02011 02012 static VALUE 02013 gzfile_new(klass, funcs, endfunc) 02014 VALUE klass; 02015 const struct zstream_funcs *funcs; 02016 void (*endfunc)(struct gzfile *); 02017 { 02018 VALUE obj; 02019 struct gzfile *gz; 02020 02021 obj = Data_Make_Struct(klass, struct gzfile, gzfile_mark, gzfile_free, gz); 02022 zstream_init(&gz->z, funcs); 02023 gz->io = Qnil; 02024 gz->level = 0; 02025 gz->mtime = 0; 02026 gz->os_code = OS_CODE; 02027 gz->orig_name = Qnil; 02028 gz->comment = Qnil; 02029 gz->crc = crc32(0, Z_NULL, 0); 02030 gz->lineno = 0; 02031 gz->ungetc = 0; 02032 gz->end = endfunc; 02033 gz->enc = rb_default_external_encoding(); 02034 gz->enc2 = 0; 02035 gz->ec = NULL; 02036 gz->ecflags = 0; 02037 gz->ecopts = Qnil; 02038 gz->cbuf = 0; 02039 gz->path = Qnil; 02040 02041 return obj; 02042 } 02043 02044 #define gzfile_writer_new(gz) gzfile_new((gz),&deflate_funcs,gzfile_writer_end) 02045 #define gzfile_reader_new(gz) gzfile_new((gz),&inflate_funcs,gzfile_reader_end) 02046 02047 static void 02048 gzfile_reset(struct gzfile *gz) 02049 { 02050 zstream_reset(&gz->z); 02051 gz->crc = crc32(0, Z_NULL, 0); 02052 gz->lineno = 0; 02053 gz->ungetc = 0; 02054 if (gz->ec) { 02055 rb_econv_close(gz->ec); 02056 gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name, 02057 gz->ecflags, gz->ecopts); 02058 } 02059 } 02060 02061 static void 02062 gzfile_close(struct gzfile *gz, int closeflag) 02063 { 02064 VALUE io = gz->io; 02065 02066 gz->end(gz); 02067 gz->io = Qnil; 02068 gz->orig_name = Qnil; 02069 gz->comment = Qnil; 02070 if (closeflag && rb_respond_to(io, id_close)) { 02071 rb_funcall(io, id_close, 0); 02072 } 02073 } 02074 02075 static void 02076 gzfile_write_raw(struct gzfile *gz) 02077 { 02078 VALUE str; 02079 02080 if (gz->z.buf_filled > 0) { 02081 str = zstream_detach_buffer(&gz->z); 02082 OBJ_TAINT(str); /* for safe */ 02083 rb_funcall(gz->io, id_write, 1, str); 02084 if ((gz->z.flags & GZFILE_FLAG_SYNC) 02085 && rb_respond_to(gz->io, id_flush)) 02086 rb_funcall(gz->io, id_flush, 0); 02087 } 02088 } 02089 02090 static VALUE 02091 gzfile_read_raw_partial(VALUE arg) 02092 { 02093 struct gzfile *gz = (struct gzfile*)arg; 02094 VALUE str; 02095 02096 str = rb_funcall(gz->io, id_readpartial, 1, INT2FIX(GZFILE_READ_SIZE)); 02097 Check_Type(str, T_STRING); 02098 return str; 02099 } 02100 02101 static VALUE 02102 gzfile_read_raw_rescue(VALUE arg) 02103 { 02104 struct gzfile *gz = (struct gzfile*)arg; 02105 VALUE str = Qnil; 02106 if (rb_obj_is_kind_of(rb_errinfo(), rb_eNoMethodError)) { 02107 str = rb_funcall(gz->io, id_read, 1, INT2FIX(GZFILE_READ_SIZE)); 02108 if (!NIL_P(str)) { 02109 Check_Type(str, T_STRING); 02110 } 02111 } 02112 return str; /* return nil when EOFError */ 02113 } 02114 02115 static VALUE 02116 gzfile_read_raw(struct gzfile *gz) 02117 { 02118 return rb_rescue2(gzfile_read_raw_partial, (VALUE)gz, 02119 gzfile_read_raw_rescue, (VALUE)gz, 02120 rb_eEOFError, rb_eNoMethodError, (VALUE)0); 02121 } 02122 02123 static int 02124 gzfile_read_raw_ensure(struct gzfile *gz, long size) 02125 { 02126 VALUE str; 02127 02128 while (NIL_P(gz->z.input) || RSTRING_LEN(gz->z.input) < size) { 02129 str = gzfile_read_raw(gz); 02130 if (NIL_P(str)) return 0; 02131 zstream_append_input2(&gz->z, str); 02132 } 02133 return 1; 02134 } 02135 02136 static char * 02137 gzfile_read_raw_until_zero(struct gzfile *gz, long offset) 02138 { 02139 VALUE str; 02140 char *p; 02141 02142 for (;;) { 02143 p = memchr(RSTRING_PTR(gz->z.input) + offset, '\0', 02144 RSTRING_LEN(gz->z.input) - offset); 02145 if (p) break; 02146 str = gzfile_read_raw(gz); 02147 if (NIL_P(str)) { 02148 rb_raise(cGzError, "unexpected end of file"); 02149 } 02150 offset = RSTRING_LEN(gz->z.input); 02151 zstream_append_input2(&gz->z, str); 02152 } 02153 return p; 02154 } 02155 02156 static unsigned int 02157 gzfile_get16(const unsigned char *src) 02158 { 02159 unsigned int n; 02160 n = *(src++) & 0xff; 02161 n |= (*(src++) & 0xff) << 8; 02162 return n; 02163 } 02164 02165 static unsigned long 02166 gzfile_get32(const unsigned char *src) 02167 { 02168 unsigned long n; 02169 n = *(src++) & 0xff; 02170 n |= (*(src++) & 0xff) << 8; 02171 n |= (*(src++) & 0xff) << 16; 02172 n |= (*(src++) & 0xffU) << 24; 02173 return n; 02174 } 02175 02176 static void 02177 gzfile_set32(unsigned long n, unsigned char *dst) 02178 { 02179 *(dst++) = n & 0xff; 02180 *(dst++) = (n >> 8) & 0xff; 02181 *(dst++) = (n >> 16) & 0xff; 02182 *dst = (n >> 24) & 0xff; 02183 } 02184 02185 static void 02186 gzfile_raise(struct gzfile *gz, VALUE klass, const char *message) 02187 { 02188 VALUE exc = rb_exc_new2(klass, message); 02189 if (!NIL_P(gz->z.input)) { 02190 rb_ivar_set(exc, id_input, rb_str_resurrect(gz->z.input)); 02191 } 02192 rb_exc_raise(exc); 02193 } 02194 02195 /* 02196 * Document-method: Zlib::GzipFile::Error#inspect 02197 * 02198 * Constructs a String of the GzipFile Error 02199 */ 02200 static VALUE 02201 gzfile_error_inspect(VALUE error) 02202 { 02203 VALUE str = rb_call_super(0, 0); 02204 VALUE input = rb_attr_get(error, id_input); 02205 02206 if (!NIL_P(input)) { 02207 rb_str_resize(str, RSTRING_LEN(str)-1); 02208 rb_str_cat2(str, ", input="); 02209 rb_str_append(str, rb_str_inspect(input)); 02210 rb_str_cat2(str, ">"); 02211 } 02212 return str; 02213 } 02214 02215 static void 02216 gzfile_make_header(struct gzfile *gz) 02217 { 02218 Bytef buf[10]; /* the size of gzip header */ 02219 unsigned char flags = 0, extraflags = 0; 02220 02221 if (!NIL_P(gz->orig_name)) { 02222 flags |= GZ_FLAG_ORIG_NAME; 02223 } 02224 if (!NIL_P(gz->comment)) { 02225 flags |= GZ_FLAG_COMMENT; 02226 } 02227 if (gz->mtime == 0) { 02228 gz->mtime = time(0); 02229 } 02230 02231 if (gz->level == Z_BEST_SPEED) { 02232 extraflags |= GZ_EXTRAFLAG_FAST; 02233 } 02234 else if (gz->level == Z_BEST_COMPRESSION) { 02235 extraflags |= GZ_EXTRAFLAG_SLOW; 02236 } 02237 02238 buf[0] = GZ_MAGIC1; 02239 buf[1] = GZ_MAGIC2; 02240 buf[2] = GZ_METHOD_DEFLATE; 02241 buf[3] = flags; 02242 gzfile_set32((unsigned long)gz->mtime, &buf[4]); 02243 buf[8] = extraflags; 02244 buf[9] = gz->os_code; 02245 zstream_append_buffer(&gz->z, buf, sizeof(buf)); 02246 02247 if (!NIL_P(gz->orig_name)) { 02248 zstream_append_buffer2(&gz->z, gz->orig_name); 02249 zstream_append_buffer(&gz->z, (Bytef*)"\0", 1); 02250 } 02251 if (!NIL_P(gz->comment)) { 02252 zstream_append_buffer2(&gz->z, gz->comment); 02253 zstream_append_buffer(&gz->z, (Bytef*)"\0", 1); 02254 } 02255 02256 gz->z.flags |= GZFILE_FLAG_HEADER_FINISHED; 02257 } 02258 02259 static void 02260 gzfile_make_footer(struct gzfile *gz) 02261 { 02262 Bytef buf[8]; /* 8 is the size of gzip footer */ 02263 02264 gzfile_set32(gz->crc, buf); 02265 gzfile_set32(gz->z.stream.total_in, &buf[4]); 02266 zstream_append_buffer(&gz->z, buf, sizeof(buf)); 02267 gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED; 02268 } 02269 02270 static void 02271 gzfile_read_header(struct gzfile *gz) 02272 { 02273 const unsigned char *head; 02274 long len; 02275 char flags, *p; 02276 02277 if (!gzfile_read_raw_ensure(gz, 10)) { /* 10 is the size of gzip header */ 02278 gzfile_raise(gz, cGzError, "not in gzip format"); 02279 } 02280 02281 head = (unsigned char*)RSTRING_PTR(gz->z.input); 02282 02283 if (head[0] != GZ_MAGIC1 || head[1] != GZ_MAGIC2) { 02284 gzfile_raise(gz, cGzError, "not in gzip format"); 02285 } 02286 if (head[2] != GZ_METHOD_DEFLATE) { 02287 rb_raise(cGzError, "unsupported compression method %d", head[2]); 02288 } 02289 02290 flags = head[3]; 02291 if (flags & GZ_FLAG_MULTIPART) { 02292 rb_raise(cGzError, "multi-part gzip file is not supported"); 02293 } 02294 else if (flags & GZ_FLAG_ENCRYPT) { 02295 rb_raise(cGzError, "encrypted gzip file is not supported"); 02296 } 02297 else if (flags & GZ_FLAG_UNKNOWN_MASK) { 02298 rb_raise(cGzError, "unknown flags 0x%02x", flags); 02299 } 02300 02301 if (head[8] & GZ_EXTRAFLAG_FAST) { 02302 gz->level = Z_BEST_SPEED; 02303 } 02304 else if (head[8] & GZ_EXTRAFLAG_SLOW) { 02305 gz->level = Z_BEST_COMPRESSION; 02306 } 02307 else { 02308 gz->level = Z_DEFAULT_COMPRESSION; 02309 } 02310 02311 gz->mtime = gzfile_get32(&head[4]); 02312 gz->os_code = head[9]; 02313 zstream_discard_input(&gz->z, 10); 02314 02315 if (flags & GZ_FLAG_EXTRA) { 02316 if (!gzfile_read_raw_ensure(gz, 2)) { 02317 rb_raise(cGzError, "unexpected end of file"); 02318 } 02319 len = gzfile_get16((Bytef*)RSTRING_PTR(gz->z.input)); 02320 if (!gzfile_read_raw_ensure(gz, 2 + len)) { 02321 rb_raise(cGzError, "unexpected end of file"); 02322 } 02323 zstream_discard_input(&gz->z, 2 + len); 02324 } 02325 if (flags & GZ_FLAG_ORIG_NAME) { 02326 if (!gzfile_read_raw_ensure(gz, 1)) { 02327 rb_raise(cGzError, "unexpected end of file"); 02328 } 02329 p = gzfile_read_raw_until_zero(gz, 0); 02330 len = p - RSTRING_PTR(gz->z.input); 02331 gz->orig_name = rb_str_new(RSTRING_PTR(gz->z.input), len); 02332 OBJ_TAINT(gz->orig_name); /* for safe */ 02333 zstream_discard_input(&gz->z, len + 1); 02334 } 02335 if (flags & GZ_FLAG_COMMENT) { 02336 if (!gzfile_read_raw_ensure(gz, 1)) { 02337 rb_raise(cGzError, "unexpected end of file"); 02338 } 02339 p = gzfile_read_raw_until_zero(gz, 0); 02340 len = p - RSTRING_PTR(gz->z.input); 02341 gz->comment = rb_str_new(RSTRING_PTR(gz->z.input), len); 02342 OBJ_TAINT(gz->comment); /* for safe */ 02343 zstream_discard_input(&gz->z, len + 1); 02344 } 02345 02346 if (gz->z.input != Qnil && RSTRING_LEN(gz->z.input) > 0) { 02347 zstream_run(&gz->z, 0, 0, Z_SYNC_FLUSH); 02348 } 02349 } 02350 02351 static void 02352 gzfile_check_footer(struct gzfile *gz) 02353 { 02354 unsigned long crc, length; 02355 02356 gz->z.flags |= GZFILE_FLAG_FOOTER_FINISHED; 02357 02358 if (!gzfile_read_raw_ensure(gz, 8)) { /* 8 is the size of gzip footer */ 02359 gzfile_raise(gz, cNoFooter, "footer is not found"); 02360 } 02361 02362 crc = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input)); 02363 length = gzfile_get32((Bytef*)RSTRING_PTR(gz->z.input) + 4); 02364 02365 gz->z.stream.total_in += 8; /* to rewind correctly */ 02366 zstream_discard_input(&gz->z, 8); 02367 02368 if (gz->crc != crc) { 02369 rb_raise(cCRCError, "invalid compressed data -- crc error"); 02370 } 02371 if ((uint32_t)gz->z.stream.total_out != length) { 02372 rb_raise(cLengthError, "invalid compressed data -- length error"); 02373 } 02374 } 02375 02376 static void 02377 gzfile_write(struct gzfile *gz, Bytef *str, long len) 02378 { 02379 if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) { 02380 gzfile_make_header(gz); 02381 } 02382 02383 if (len > 0 || (gz->z.flags & GZFILE_FLAG_SYNC)) { 02384 gz->crc = checksum_long(crc32, gz->crc, str, len); 02385 zstream_run(&gz->z, str, len, (gz->z.flags & GZFILE_FLAG_SYNC) 02386 ? Z_SYNC_FLUSH : Z_NO_FLUSH); 02387 } 02388 gzfile_write_raw(gz); 02389 } 02390 02391 static long 02392 gzfile_read_more(struct gzfile *gz) 02393 { 02394 volatile VALUE str; 02395 02396 while (!ZSTREAM_IS_FINISHED(&gz->z)) { 02397 str = gzfile_read_raw(gz); 02398 if (NIL_P(str)) { 02399 if (!ZSTREAM_IS_FINISHED(&gz->z)) { 02400 rb_raise(cGzError, "unexpected end of file"); 02401 } 02402 break; 02403 } 02404 if (RSTRING_LEN(str) > 0) { /* prevent Z_BUF_ERROR */ 02405 zstream_run(&gz->z, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str), 02406 Z_SYNC_FLUSH); 02407 } 02408 if (gz->z.buf_filled > 0) break; 02409 } 02410 return gz->z.buf_filled; 02411 } 02412 02413 static void 02414 gzfile_calc_crc(struct gzfile *gz, VALUE str) 02415 { 02416 if (RSTRING_LEN(str) <= gz->ungetc) { 02417 gz->ungetc -= RSTRING_LEN(str); 02418 } 02419 else { 02420 gz->crc = checksum_long(crc32, gz->crc, (Bytef*)RSTRING_PTR(str) + gz->ungetc, 02421 RSTRING_LEN(str) - gz->ungetc); 02422 gz->ungetc = 0; 02423 } 02424 } 02425 02426 static VALUE 02427 gzfile_newstr(struct gzfile *gz, VALUE str) 02428 { 02429 if (!gz->enc2) { 02430 rb_enc_associate(str, gz->enc); 02431 OBJ_TAINT(str); /* for safe */ 02432 return str; 02433 } 02434 if (gz->ec && rb_enc_dummy_p(gz->enc2)) { 02435 str = rb_econv_str_convert(gz->ec, str, ECONV_PARTIAL_INPUT); 02436 rb_enc_associate(str, gz->enc); 02437 OBJ_TAINT(str); 02438 return str; 02439 } 02440 return rb_str_conv_enc_opts(str, gz->enc2, gz->enc, 02441 gz->ecflags, gz->ecopts); 02442 } 02443 02444 static long 02445 gzfile_fill(struct gzfile *gz, long len) 02446 { 02447 if (len < 0) 02448 rb_raise(rb_eArgError, "negative length %ld given", len); 02449 if (len == 0) 02450 return 0; 02451 while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { 02452 gzfile_read_more(gz); 02453 } 02454 if (GZFILE_IS_FINISHED(gz)) { 02455 if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { 02456 gzfile_check_footer(gz); 02457 } 02458 return -1; 02459 } 02460 return len < gz->z.buf_filled ? len : gz->z.buf_filled; 02461 } 02462 02463 static VALUE 02464 gzfile_read(struct gzfile *gz, long len) 02465 { 02466 VALUE dst; 02467 02468 len = gzfile_fill(gz, len); 02469 if (len == 0) return rb_str_new(0, 0); 02470 if (len < 0) return Qnil; 02471 dst = zstream_shift_buffer(&gz->z, len); 02472 gzfile_calc_crc(gz, dst); 02473 return dst; 02474 } 02475 02476 static VALUE 02477 gzfile_readpartial(struct gzfile *gz, long len, VALUE outbuf) 02478 { 02479 VALUE dst; 02480 02481 if (len < 0) 02482 rb_raise(rb_eArgError, "negative length %ld given", len); 02483 02484 if (!NIL_P(outbuf)) 02485 OBJ_TAINT(outbuf); 02486 02487 if (len == 0) { 02488 if (NIL_P(outbuf)) 02489 return rb_str_new(0, 0); 02490 else { 02491 rb_str_resize(outbuf, 0); 02492 return outbuf; 02493 } 02494 } 02495 while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled == 0) { 02496 gzfile_read_more(gz); 02497 } 02498 if (GZFILE_IS_FINISHED(gz)) { 02499 if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { 02500 gzfile_check_footer(gz); 02501 } 02502 if (!NIL_P(outbuf)) 02503 rb_str_resize(outbuf, 0); 02504 rb_raise(rb_eEOFError, "end of file reached"); 02505 } 02506 02507 dst = zstream_shift_buffer(&gz->z, len); 02508 gzfile_calc_crc(gz, dst); 02509 02510 if (!NIL_P(outbuf)) { 02511 rb_str_resize(outbuf, RSTRING_LEN(dst)); 02512 memcpy(RSTRING_PTR(outbuf), RSTRING_PTR(dst), RSTRING_LEN(dst)); 02513 dst = outbuf; 02514 } 02515 OBJ_TAINT(dst); /* for safe */ 02516 return dst; 02517 } 02518 02519 static VALUE 02520 gzfile_read_all(struct gzfile *gz) 02521 { 02522 VALUE dst; 02523 02524 while (!ZSTREAM_IS_FINISHED(&gz->z)) { 02525 gzfile_read_more(gz); 02526 } 02527 if (GZFILE_IS_FINISHED(gz)) { 02528 if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { 02529 gzfile_check_footer(gz); 02530 } 02531 return rb_str_new(0, 0); 02532 } 02533 02534 dst = zstream_detach_buffer(&gz->z); 02535 gzfile_calc_crc(gz, dst); 02536 OBJ_TAINT(dst); 02537 return gzfile_newstr(gz, dst); 02538 } 02539 02540 static VALUE 02541 gzfile_getc(struct gzfile *gz) 02542 { 02543 VALUE buf, dst = 0; 02544 int len; 02545 02546 len = rb_enc_mbmaxlen(gz->enc); 02547 while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { 02548 gzfile_read_more(gz); 02549 } 02550 if (GZFILE_IS_FINISHED(gz)) { 02551 if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { 02552 gzfile_check_footer(gz); 02553 } 02554 return Qnil; 02555 } 02556 02557 if (gz->ec && rb_enc_dummy_p(gz->enc2)) { 02558 const unsigned char *ss, *sp, *se; 02559 unsigned char *ds, *dp, *de; 02560 rb_econv_result_t res; 02561 02562 if (!gz->cbuf) { 02563 gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA); 02564 } 02565 ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf); 02566 se = sp + gz->z.buf_filled; 02567 ds = dp = (unsigned char *)gz->cbuf; 02568 de = (unsigned char *)ds + GZFILE_CBUF_CAPA; 02569 res = rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT); 02570 rb_econv_check_error(gz->ec); 02571 dst = zstream_shift_buffer(&gz->z, sp - ss); 02572 gzfile_calc_crc(gz, dst); 02573 dst = rb_str_new(gz->cbuf, dp - ds); 02574 rb_enc_associate(dst, gz->enc); 02575 OBJ_TAINT(dst); 02576 return dst; 02577 } 02578 else { 02579 buf = gz->z.buf; 02580 len = rb_enc_mbclen(RSTRING_PTR(buf), RSTRING_END(buf), gz->enc); 02581 dst = gzfile_read(gz, len); 02582 return gzfile_newstr(gz, dst); 02583 } 02584 } 02585 02586 static void 02587 gzfile_ungets(struct gzfile *gz, const Bytef *b, long len) 02588 { 02589 zstream_buffer_ungets(&gz->z, b, len); 02590 gz->ungetc+=len; 02591 } 02592 02593 static void 02594 gzfile_ungetbyte(struct gzfile *gz, int c) 02595 { 02596 zstream_buffer_ungetbyte(&gz->z, c); 02597 gz->ungetc++; 02598 } 02599 02600 static VALUE 02601 gzfile_writer_end_run(VALUE arg) 02602 { 02603 struct gzfile *gz = (struct gzfile *)arg; 02604 02605 if (!(gz->z.flags & GZFILE_FLAG_HEADER_FINISHED)) { 02606 gzfile_make_header(gz); 02607 } 02608 02609 zstream_run(&gz->z, (Bytef*)"", 0, Z_FINISH); 02610 gzfile_make_footer(gz); 02611 gzfile_write_raw(gz); 02612 02613 return Qnil; 02614 } 02615 02616 static void 02617 gzfile_writer_end(struct gzfile *gz) 02618 { 02619 if (ZSTREAM_IS_CLOSING(&gz->z)) return; 02620 gz->z.flags |= ZSTREAM_FLAG_CLOSING; 02621 02622 rb_ensure(gzfile_writer_end_run, (VALUE)gz, zstream_end, (VALUE)&gz->z); 02623 } 02624 02625 static VALUE 02626 gzfile_reader_end_run(VALUE arg) 02627 { 02628 struct gzfile *gz = (struct gzfile *)arg; 02629 02630 if (GZFILE_IS_FINISHED(gz) 02631 && !(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { 02632 gzfile_check_footer(gz); 02633 } 02634 02635 return Qnil; 02636 } 02637 02638 static void 02639 gzfile_reader_end(struct gzfile *gz) 02640 { 02641 if (ZSTREAM_IS_CLOSING(&gz->z)) return; 02642 gz->z.flags |= ZSTREAM_FLAG_CLOSING; 02643 02644 rb_ensure(gzfile_reader_end_run, (VALUE)gz, zstream_end, (VALUE)&gz->z); 02645 } 02646 02647 static void 02648 gzfile_reader_rewind(struct gzfile *gz) 02649 { 02650 long n; 02651 02652 n = gz->z.stream.total_in; 02653 if (!NIL_P(gz->z.input)) { 02654 n += RSTRING_LEN(gz->z.input); 02655 } 02656 02657 rb_funcall(gz->io, id_seek, 2, rb_int2inum(-n), INT2FIX(1)); 02658 gzfile_reset(gz); 02659 } 02660 02661 static VALUE 02662 gzfile_reader_get_unused(struct gzfile *gz) 02663 { 02664 VALUE str; 02665 02666 if (!ZSTREAM_IS_READY(&gz->z)) return Qnil; 02667 if (!GZFILE_IS_FINISHED(gz)) return Qnil; 02668 if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { 02669 gzfile_check_footer(gz); 02670 } 02671 if (NIL_P(gz->z.input)) return Qnil; 02672 02673 str = rb_str_resurrect(gz->z.input); 02674 OBJ_TAINT(str); /* for safe */ 02675 return str; 02676 } 02677 02678 static struct gzfile * 02679 get_gzfile(VALUE obj) 02680 { 02681 struct gzfile *gz; 02682 02683 Data_Get_Struct(obj, struct gzfile, gz); 02684 if (!ZSTREAM_IS_READY(&gz->z)) { 02685 rb_raise(cGzError, "closed gzip stream"); 02686 } 02687 return gz; 02688 } 02689 02690 02691 /* ------------------------------------------------------------------------- */ 02692 02693 /* 02694 * Document-class: Zlib::GzipFile 02695 * 02696 * Zlib::GzipFile is an abstract class for handling a gzip formatted 02697 * compressed file. The operations are defined in the subclasses, 02698 * Zlib::GzipReader for reading, and Zlib::GzipWriter for writing. 02699 * 02700 * GzipReader should be used by associating an IO, or IO-like, object. 02701 * 02702 * == Method Catalogue 02703 * 02704 * - ::wrap 02705 * - ::open (Zlib::GzipReader::open and Zlib::GzipWriter::open) 02706 * - #close 02707 * - #closed? 02708 * - #comment 02709 * - comment= (Zlib::GzipWriter#comment=) 02710 * - #crc 02711 * - eof? (Zlib::GzipReader#eof?) 02712 * - #finish 02713 * - #level 02714 * - lineno (Zlib::GzipReader#lineno) 02715 * - lineno= (Zlib::GzipReader#lineno=) 02716 * - #mtime 02717 * - mtime= (Zlib::GzipWriter#mtime=) 02718 * - #orig_name 02719 * - orig_name (Zlib::GzipWriter#orig_name=) 02720 * - #os_code 02721 * - path (when the underlying IO supports #path) 02722 * - #sync 02723 * - #sync= 02724 * - #to_io 02725 * 02726 * (due to internal structure, documentation may appear under Zlib::GzipReader 02727 * or Zlib::GzipWriter) 02728 */ 02729 02730 02731 typedef struct { 02732 int argc; 02733 VALUE *argv; 02734 VALUE klass; 02735 } new_wrap_arg_t; 02736 02737 static VALUE 02738 new_wrap(VALUE tmp) 02739 { 02740 new_wrap_arg_t *arg = (new_wrap_arg_t *)tmp; 02741 return rb_class_new_instance(arg->argc, arg->argv, arg->klass); 02742 } 02743 02744 static VALUE 02745 gzfile_ensure_close(VALUE obj) 02746 { 02747 struct gzfile *gz; 02748 02749 Data_Get_Struct(obj, struct gzfile, gz); 02750 if (ZSTREAM_IS_READY(&gz->z)) { 02751 gzfile_close(gz, 1); 02752 } 02753 return Qnil; 02754 } 02755 02756 static VALUE 02757 gzfile_wrap(int argc, VALUE *argv, VALUE klass, int close_io_on_error) 02758 { 02759 VALUE obj; 02760 02761 if (close_io_on_error) { 02762 int state = 0; 02763 new_wrap_arg_t arg; 02764 arg.argc = argc; 02765 arg.argv = argv; 02766 arg.klass = klass; 02767 obj = rb_protect(new_wrap, (VALUE)&arg, &state); 02768 if (state) { 02769 rb_io_close(argv[0]); 02770 rb_jump_tag(state); 02771 } 02772 } 02773 else { 02774 obj = rb_class_new_instance(argc, argv, klass); 02775 } 02776 02777 if (rb_block_given_p()) { 02778 return rb_ensure(rb_yield, obj, gzfile_ensure_close, obj); 02779 } 02780 else { 02781 return obj; 02782 } 02783 } 02784 02785 /* 02786 * Document-method: Zlib::GzipFile.wrap 02787 * 02788 * call-seq: Zlib::GzipFile.wrap(io) { |gz| ... } 02789 * 02790 * Creates a GzipFile object associated with +io+, and 02791 * executes the block with the newly created GzipFile object, 02792 * just like File.open. The GzipFile object will be closed 02793 * automatically after executing the block. If you want to keep 02794 * the associated IO object opening, you may call 02795 * +Zlib::GzipFile#finish+ method in the block. 02796 */ 02797 static VALUE 02798 rb_gzfile_s_wrap(int argc, VALUE *argv, VALUE klass) 02799 { 02800 return gzfile_wrap(argc, argv, klass, 0); 02801 } 02802 02803 /* 02804 * Document-method: Zlib::GzipFile.open 02805 * 02806 * See Zlib::GzipReader#open and Zlib::GzipWriter#open. 02807 */ 02808 static VALUE 02809 gzfile_s_open(int argc, VALUE *argv, VALUE klass, const char *mode) 02810 { 02811 VALUE io, filename; 02812 02813 if (argc < 1) { 02814 rb_raise(rb_eArgError, "wrong number of arguments (0 for 1)"); 02815 } 02816 filename = argv[0]; 02817 io = rb_file_open_str(filename, mode); 02818 argv[0] = io; 02819 return gzfile_wrap(argc, argv, klass, 1); 02820 } 02821 02822 /* 02823 * Document-method: Zlib::GzipFile#to_io 02824 * 02825 * Same as IO. 02826 */ 02827 static VALUE 02828 rb_gzfile_to_io(VALUE obj) 02829 { 02830 return get_gzfile(obj)->io; 02831 } 02832 02833 /* 02834 * Document-method: Zlib::GzipFile#crc 02835 * 02836 * Returns CRC value of the uncompressed data. 02837 */ 02838 static VALUE 02839 rb_gzfile_crc(VALUE obj) 02840 { 02841 return rb_uint2inum(get_gzfile(obj)->crc); 02842 } 02843 02844 /* 02845 * Document-method: Zlib::GzipFile#mtime 02846 * 02847 * Returns last modification time recorded in the gzip file header. 02848 */ 02849 static VALUE 02850 rb_gzfile_mtime(VALUE obj) 02851 { 02852 return rb_time_new(get_gzfile(obj)->mtime, (time_t)0); 02853 } 02854 02855 /* 02856 * Document-method: Zlib::GzipFile#level 02857 * 02858 * Returns compression level. 02859 */ 02860 static VALUE 02861 rb_gzfile_level(VALUE obj) 02862 { 02863 return INT2FIX(get_gzfile(obj)->level); 02864 } 02865 02866 /* 02867 * Document-method: Zlib::GzipFile#os_code 02868 * 02869 * Returns OS code number recorded in the gzip file header. 02870 */ 02871 static VALUE 02872 rb_gzfile_os_code(VALUE obj) 02873 { 02874 return INT2FIX(get_gzfile(obj)->os_code); 02875 } 02876 02877 /* 02878 * Document-method: Zlib::GzipFile#orig_name 02879 * 02880 * Returns original filename recorded in the gzip file header, or +nil+ if 02881 * original filename is not present. 02882 */ 02883 static VALUE 02884 rb_gzfile_orig_name(VALUE obj) 02885 { 02886 VALUE str = get_gzfile(obj)->orig_name; 02887 if (!NIL_P(str)) { 02888 str = rb_str_dup(str); 02889 } 02890 OBJ_TAINT(str); /* for safe */ 02891 return str; 02892 } 02893 02894 /* 02895 * Document-method: Zlib::GzipFile#comment 02896 * 02897 * Returns comments recorded in the gzip file header, or nil if the comments 02898 * is not present. 02899 */ 02900 static VALUE 02901 rb_gzfile_comment(VALUE obj) 02902 { 02903 VALUE str = get_gzfile(obj)->comment; 02904 if (!NIL_P(str)) { 02905 str = rb_str_dup(str); 02906 } 02907 OBJ_TAINT(str); /* for safe */ 02908 return str; 02909 } 02910 02911 /* 02912 * Document-method: Zlib::GzipFile#lineno 02913 * 02914 * The line number of the last row read from this file. 02915 */ 02916 static VALUE 02917 rb_gzfile_lineno(VALUE obj) 02918 { 02919 return INT2NUM(get_gzfile(obj)->lineno); 02920 } 02921 02922 /* 02923 * Document-method: Zlib::GzipReader#lineno= 02924 * 02925 * Specify line number of the last row read from this file. 02926 */ 02927 static VALUE 02928 rb_gzfile_set_lineno(VALUE obj, VALUE lineno) 02929 { 02930 struct gzfile *gz = get_gzfile(obj); 02931 gz->lineno = NUM2INT(lineno); 02932 return lineno; 02933 } 02934 02935 /* 02936 * Document-method: Zlib::GzipWriter#mtime= 02937 * 02938 * Specify the modification time (+mtime+) in the gzip header. 02939 * Using a Fixnum or Integer 02940 */ 02941 static VALUE 02942 rb_gzfile_set_mtime(VALUE obj, VALUE mtime) 02943 { 02944 struct gzfile *gz = get_gzfile(obj); 02945 VALUE val; 02946 02947 if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) { 02948 rb_raise(cGzError, "header is already written"); 02949 } 02950 02951 if (FIXNUM_P(mtime)) { 02952 gz->mtime = FIX2INT(mtime); 02953 } 02954 else { 02955 val = rb_Integer(mtime); 02956 gz->mtime = FIXNUM_P(val) ? FIX2UINT(val) : rb_big2ulong(val); 02957 } 02958 return mtime; 02959 } 02960 02961 /* 02962 * Document-method: Zlib::GzipFile#orig_name= 02963 * 02964 * Specify the original name (+str+) in the gzip header. 02965 */ 02966 static VALUE 02967 rb_gzfile_set_orig_name(VALUE obj, VALUE str) 02968 { 02969 struct gzfile *gz = get_gzfile(obj); 02970 VALUE s; 02971 char *p; 02972 02973 if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) { 02974 rb_raise(cGzError, "header is already written"); 02975 } 02976 s = rb_str_dup(rb_str_to_str(str)); 02977 p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s)); 02978 if (p) { 02979 rb_str_resize(s, p - RSTRING_PTR(s)); 02980 } 02981 gz->orig_name = s; 02982 return str; 02983 } 02984 02985 /* 02986 * Document-method: Zlib::GzipFile#comment= 02987 * 02988 * Specify the comment (+str+) in the gzip header. 02989 */ 02990 static VALUE 02991 rb_gzfile_set_comment(VALUE obj, VALUE str) 02992 { 02993 struct gzfile *gz = get_gzfile(obj); 02994 VALUE s; 02995 char *p; 02996 02997 if (gz->z.flags & GZFILE_FLAG_HEADER_FINISHED) { 02998 rb_raise(cGzError, "header is already written"); 02999 } 03000 s = rb_str_dup(rb_str_to_str(str)); 03001 p = memchr(RSTRING_PTR(s), '\0', RSTRING_LEN(s)); 03002 if (p) { 03003 rb_str_resize(s, p - RSTRING_PTR(s)); 03004 } 03005 gz->comment = s; 03006 return str; 03007 } 03008 03009 /* 03010 * Document-method: Zlib::GzipFile#close 03011 * 03012 * Closes the GzipFile object. This method calls close method of the 03013 * associated IO object. Returns the associated IO object. 03014 */ 03015 static VALUE 03016 rb_gzfile_close(VALUE obj) 03017 { 03018 struct gzfile *gz = get_gzfile(obj); 03019 VALUE io; 03020 03021 io = gz->io; 03022 gzfile_close(gz, 1); 03023 return io; 03024 } 03025 03026 /* 03027 * Document-method: Zlib::GzipFile#finish 03028 * 03029 * Closes the GzipFile object. Unlike Zlib::GzipFile#close, this method never 03030 * calls the close method of the associated IO object. Returns the associated IO 03031 * object. 03032 */ 03033 static VALUE 03034 rb_gzfile_finish(VALUE obj) 03035 { 03036 struct gzfile *gz = get_gzfile(obj); 03037 VALUE io; 03038 03039 io = gz->io; 03040 gzfile_close(gz, 0); 03041 return io; 03042 } 03043 03044 /* 03045 * Document-method: Zlib::GzipFile#closed? 03046 * 03047 * Same as IO#closed? 03048 * 03049 */ 03050 static VALUE 03051 rb_gzfile_closed_p(VALUE obj) 03052 { 03053 struct gzfile *gz; 03054 Data_Get_Struct(obj, struct gzfile, gz); 03055 return NIL_P(gz->io) ? Qtrue : Qfalse; 03056 } 03057 03058 /* 03059 * Document-method: Zlib::GzipFile#eof? 03060 * 03061 * Returns +true+ or +false+ whether the stream has reached the end. 03062 */ 03063 static VALUE 03064 rb_gzfile_eof_p(VALUE obj) 03065 { 03066 struct gzfile *gz = get_gzfile(obj); 03067 return GZFILE_IS_FINISHED(gz) ? Qtrue : Qfalse; 03068 } 03069 03070 /* 03071 * Document-method: Zlib::GzipFile#sync 03072 * 03073 * Same as IO#sync 03074 * 03075 */ 03076 static VALUE 03077 rb_gzfile_sync(VALUE obj) 03078 { 03079 return (get_gzfile(obj)->z.flags & GZFILE_FLAG_SYNC) ? Qtrue : Qfalse; 03080 } 03081 03082 /* 03083 * Document-method: Zlib::GzipFile#sync= 03084 * 03085 * call-seq: sync = flag 03086 * 03087 * Same as IO. If flag is +true+, the associated IO object must respond to the 03088 * +flush+ method. While +sync+ mode is +true+, the compression ratio 03089 * decreases sharply. 03090 */ 03091 static VALUE 03092 rb_gzfile_set_sync(VALUE obj, VALUE mode) 03093 { 03094 struct gzfile *gz = get_gzfile(obj); 03095 03096 if (RTEST(mode)) { 03097 gz->z.flags |= GZFILE_FLAG_SYNC; 03098 } 03099 else { 03100 gz->z.flags &= ~GZFILE_FLAG_SYNC; 03101 } 03102 return mode; 03103 } 03104 03105 /* 03106 * Document-method: Zlib::GzipFile#total_in 03107 * 03108 * Total number of input bytes read so far. 03109 */ 03110 static VALUE 03111 rb_gzfile_total_in(VALUE obj) 03112 { 03113 return rb_uint2inum(get_gzfile(obj)->z.stream.total_in); 03114 } 03115 03116 /* 03117 * Document-method: Zlib::GzipFile#total_out 03118 * 03119 * Total number of output bytes output so far. 03120 */ 03121 static VALUE 03122 rb_gzfile_total_out(VALUE obj) 03123 { 03124 struct gzfile *gz = get_gzfile(obj); 03125 return rb_uint2inum(gz->z.stream.total_out - gz->z.buf_filled); 03126 } 03127 03128 /* 03129 * Document-method: Zlib::GzipFile#path 03130 * 03131 * call-seq: path 03132 * 03133 * Returns the path string of the associated IO-like object. This 03134 * method is only defined when the IO-like object responds to #path(). 03135 */ 03136 static VALUE 03137 rb_gzfile_path(VALUE obj) 03138 { 03139 struct gzfile *gz; 03140 Data_Get_Struct(obj, struct gzfile, gz); 03141 return gz->path; 03142 } 03143 03144 static void 03145 rb_gzfile_ecopts(struct gzfile *gz, VALUE opts) 03146 { 03147 if (!NIL_P(opts)) { 03148 rb_io_extract_encoding_option(opts, &gz->enc, &gz->enc2, NULL); 03149 } 03150 if (gz->enc2) { 03151 gz->ecflags = rb_econv_prepare_opts(opts, &opts); 03152 gz->ec = rb_econv_open_opts(gz->enc2->name, gz->enc->name, 03153 gz->ecflags, opts); 03154 gz->ecopts = opts; 03155 } 03156 } 03157 03158 /* ------------------------------------------------------------------------- */ 03159 03160 /* 03161 * Document-class: Zlib::GzipWriter 03162 * 03163 * Zlib::GzipWriter is a class for writing gzipped files. GzipWriter should 03164 * be used with an instance of IO, or IO-like, object. 03165 * 03166 * Following two example generate the same result. 03167 * 03168 * Zlib::GzipWriter.open('hoge.gz') do |gz| 03169 * gz.write 'jugemu jugemu gokou no surikire...' 03170 * end 03171 * 03172 * File.open('hoge.gz', 'w') do |f| 03173 * gz = Zlib::GzipWriter.new(f) 03174 * gz.write 'jugemu jugemu gokou no surikire...' 03175 * gz.close 03176 * end 03177 * 03178 * To make like gzip(1) does, run following: 03179 * 03180 * orig = 'hoge.txt' 03181 * Zlib::GzipWriter.open('hoge.gz') do |gz| 03182 * gz.mtime = File.mtime(orig) 03183 * gz.orig_name = orig 03184 * gz.write IO.binread(orig) 03185 * end 03186 * 03187 * NOTE: Due to the limitation of Ruby's finalizer, you must explicitly close 03188 * GzipWriter objects by Zlib::GzipWriter#close etc. Otherwise, GzipWriter 03189 * will be not able to write the gzip footer and will generate a broken gzip 03190 * file. 03191 */ 03192 03193 static VALUE 03194 rb_gzwriter_s_allocate(VALUE klass) 03195 { 03196 return gzfile_writer_new(klass); 03197 } 03198 03199 /* 03200 * call-seq: Zlib::GzipWriter.open(filename, level=nil, strategy=nil) { |gz| ... } 03201 * 03202 * Opens a file specified by +filename+ for writing gzip compressed data, and 03203 * returns a GzipWriter object associated with that file. Further details of 03204 * this method are found in Zlib::GzipWriter.new and Zlib::GzipFile.wrap. 03205 */ 03206 static VALUE 03207 rb_gzwriter_s_open(int argc, VALUE *argv, VALUE klass) 03208 { 03209 return gzfile_s_open(argc, argv, klass, "wb"); 03210 } 03211 03212 /* 03213 * call-seq: Zlib::GzipWriter.new(io, level, strategy) 03214 * 03215 * Creates a GzipWriter object associated with +io+. +level+ and +strategy+ 03216 * should be the same as the arguments of Zlib::Deflate.new. The GzipWriter 03217 * object writes gzipped data to +io+. At least, +io+ must respond to the 03218 * +write+ method that behaves same as write method in IO class. 03219 */ 03220 static VALUE 03221 rb_gzwriter_initialize(int argc, VALUE *argv, VALUE obj) 03222 { 03223 struct gzfile *gz; 03224 VALUE io, level, strategy, opt = Qnil; 03225 int err; 03226 03227 if (argc > 1) { 03228 opt = rb_check_convert_type(argv[argc-1], T_HASH, "Hash", "to_hash"); 03229 if (!NIL_P(opt)) argc--; 03230 } 03231 03232 rb_scan_args(argc, argv, "12", &io, &level, &strategy); 03233 Data_Get_Struct(obj, struct gzfile, gz); 03234 03235 /* this is undocumented feature of zlib */ 03236 gz->level = ARG_LEVEL(level); 03237 err = deflateInit2(&gz->z.stream, gz->level, Z_DEFLATED, 03238 -MAX_WBITS, DEF_MEM_LEVEL, ARG_STRATEGY(strategy)); 03239 if (err != Z_OK) { 03240 raise_zlib_error(err, gz->z.stream.msg); 03241 } 03242 gz->io = io; 03243 ZSTREAM_READY(&gz->z); 03244 rb_gzfile_ecopts(gz, opt); 03245 03246 if (rb_respond_to(io, id_path)) { 03247 gz->path = rb_funcall(gz->io, id_path, 0); 03248 rb_define_singleton_method(obj, "path", rb_gzfile_path, 0); 03249 } 03250 03251 return obj; 03252 } 03253 03254 /* 03255 * call-seq: flush(flush=nil) 03256 * 03257 * Flushes all the internal buffers of the GzipWriter object. The meaning of 03258 * +flush+ is same as in Zlib::Deflate#deflate. <tt>Zlib::SYNC_FLUSH</tt> is used if 03259 * +flush+ is omitted. It is no use giving flush <tt>Zlib::NO_FLUSH</tt>. 03260 */ 03261 static VALUE 03262 rb_gzwriter_flush(int argc, VALUE *argv, VALUE obj) 03263 { 03264 struct gzfile *gz = get_gzfile(obj); 03265 VALUE v_flush; 03266 int flush; 03267 03268 rb_scan_args(argc, argv, "01", &v_flush); 03269 03270 flush = FIXNUMARG(v_flush, Z_SYNC_FLUSH); 03271 if (flush != Z_NO_FLUSH) { /* prevent Z_BUF_ERROR */ 03272 zstream_run(&gz->z, (Bytef*)"", 0, flush); 03273 } 03274 03275 gzfile_write_raw(gz); 03276 if (rb_respond_to(gz->io, id_flush)) { 03277 rb_funcall(gz->io, id_flush, 0); 03278 } 03279 return obj; 03280 } 03281 03282 /* 03283 * Same as IO. 03284 */ 03285 static VALUE 03286 rb_gzwriter_write(VALUE obj, VALUE str) 03287 { 03288 struct gzfile *gz = get_gzfile(obj); 03289 03290 if (TYPE(str) != T_STRING) 03291 str = rb_obj_as_string(str); 03292 if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) { 03293 str = rb_str_conv_enc(str, rb_enc_get(str), gz->enc2); 03294 } 03295 gzfile_write(gz, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str)); 03296 return INT2FIX(RSTRING_LEN(str)); 03297 } 03298 03299 /* 03300 * Same as IO. 03301 */ 03302 static VALUE 03303 rb_gzwriter_putc(VALUE obj, VALUE ch) 03304 { 03305 struct gzfile *gz = get_gzfile(obj); 03306 char c = NUM2CHR(ch); 03307 03308 gzfile_write(gz, (Bytef*)&c, 1); 03309 return ch; 03310 } 03311 03312 03313 03314 /* 03315 * Document-method: << 03316 * Same as IO. 03317 */ 03318 #define rb_gzwriter_addstr rb_io_addstr 03319 /* 03320 * Document-method: printf 03321 * Same as IO. 03322 */ 03323 #define rb_gzwriter_printf rb_io_printf 03324 /* 03325 * Document-method: print 03326 * Same as IO. 03327 */ 03328 #define rb_gzwriter_print rb_io_print 03329 /* 03330 * Document-method: puts 03331 * Same as IO. 03332 */ 03333 #define rb_gzwriter_puts rb_io_puts 03334 03335 03336 /* ------------------------------------------------------------------------- */ 03337 03338 /* 03339 * Document-class: Zlib::GzipReader 03340 * 03341 * Zlib::GzipReader is the class for reading a gzipped file. GzipReader should 03342 * be used an IO, or -IO-lie, object. 03343 * 03344 * Zlib::GzipReader.open('hoge.gz') {|gz| 03345 * print gz.read 03346 * } 03347 * 03348 * File.open('hoge.gz') do |f| 03349 * gz = Zlib::GzipReader.new(f) 03350 * print gz.read 03351 * gz.close 03352 * end 03353 * 03354 * == Method Catalogue 03355 * 03356 * The following methods in Zlib::GzipReader are just like their counterparts 03357 * in IO, but they raise Zlib::Error or Zlib::GzipFile::Error exception if an 03358 * error was found in the gzip file. 03359 * - #each 03360 * - #each_line 03361 * - #each_byte 03362 * - #gets 03363 * - #getc 03364 * - #lineno 03365 * - #lineno= 03366 * - #read 03367 * - #readchar 03368 * - #readline 03369 * - #readlines 03370 * - #ungetc 03371 * 03372 * Be careful of the footer of the gzip file. A gzip file has the checksum of 03373 * pre-compressed data in its footer. GzipReader checks all uncompressed data 03374 * against that checksum at the following cases, and if it fails, raises 03375 * <tt>Zlib::GzipFile::NoFooter</tt>, <tt>Zlib::GzipFile::CRCError</tt>, or 03376 * <tt>Zlib::GzipFile::LengthError</tt> exception. 03377 * 03378 * - When an reading request is received beyond the end of file (the end of 03379 * compressed data). That is, when Zlib::GzipReader#read, 03380 * Zlib::GzipReader#gets, or some other methods for reading returns nil. 03381 * - When Zlib::GzipFile#close method is called after the object reaches the 03382 * end of file. 03383 * - When Zlib::GzipReader#unused method is called after the object reaches 03384 * the end of file. 03385 * 03386 * The rest of the methods are adequately described in their own 03387 * documentation. 03388 */ 03389 03390 static VALUE 03391 rb_gzreader_s_allocate(VALUE klass) 03392 { 03393 return gzfile_reader_new(klass); 03394 } 03395 03396 /* 03397 * Document-method: Zlib::GzipReader.open 03398 * 03399 * call-seq: Zlib::GzipReader.open(filename) {|gz| ... } 03400 * 03401 * Opens a file specified by +filename+ as a gzipped file, and returns a 03402 * GzipReader object associated with that file. Further details of this method 03403 * are in Zlib::GzipReader.new and ZLib::GzipFile.wrap. 03404 */ 03405 static VALUE 03406 rb_gzreader_s_open(int argc, VALUE *argv, VALUE klass) 03407 { 03408 return gzfile_s_open(argc, argv, klass, "rb"); 03409 } 03410 03411 /* 03412 * Document-method: Zlib::GzipReader.new 03413 * 03414 * call-seq: Zlib::GzipReader.new(io) 03415 * 03416 * Creates a GzipReader object associated with +io+. The GzipReader object reads 03417 * gzipped data from +io+, and parses/decompresses them. At least, +io+ must have 03418 * a +read+ method that behaves same as the +read+ method in IO class. 03419 * 03420 * If the gzip file header is incorrect, raises an Zlib::GzipFile::Error 03421 * exception. 03422 */ 03423 static VALUE 03424 rb_gzreader_initialize(int argc, VALUE *argv, VALUE obj) 03425 { 03426 VALUE io, opt = Qnil; 03427 struct gzfile *gz; 03428 int err; 03429 03430 Data_Get_Struct(obj, struct gzfile, gz); 03431 rb_scan_args(argc, argv, "1:", &io, &opt); 03432 03433 /* this is undocumented feature of zlib */ 03434 err = inflateInit2(&gz->z.stream, -MAX_WBITS); 03435 if (err != Z_OK) { 03436 raise_zlib_error(err, gz->z.stream.msg); 03437 } 03438 gz->io = io; 03439 ZSTREAM_READY(&gz->z); 03440 gzfile_read_header(gz); 03441 rb_gzfile_ecopts(gz, opt); 03442 03443 if (rb_respond_to(io, id_path)) { 03444 gz->path = rb_funcall(gz->io, id_path, 0); 03445 rb_define_singleton_method(obj, "path", rb_gzfile_path, 0); 03446 } 03447 03448 return obj; 03449 } 03450 03451 /* 03452 * Document-method: Zlib::GzipReader#rewind 03453 * 03454 * Resets the position of the file pointer to the point created the GzipReader 03455 * object. The associated IO object needs to respond to the +seek+ method. 03456 */ 03457 static VALUE 03458 rb_gzreader_rewind(VALUE obj) 03459 { 03460 struct gzfile *gz = get_gzfile(obj); 03461 gzfile_reader_rewind(gz); 03462 return INT2FIX(0); 03463 } 03464 03465 /* 03466 * Document-method: Zlib::GzipReader#unused 03467 * 03468 * Returns the rest of the data which had read for parsing gzip format, or 03469 * +nil+ if the whole gzip file is not parsed yet. 03470 */ 03471 static VALUE 03472 rb_gzreader_unused(VALUE obj) 03473 { 03474 struct gzfile *gz; 03475 Data_Get_Struct(obj, struct gzfile, gz); 03476 return gzfile_reader_get_unused(gz); 03477 } 03478 03479 /* 03480 * Document-method: Zlib::GzipReader#read 03481 * 03482 * See Zlib::GzipReader documentation for a description. 03483 */ 03484 static VALUE 03485 rb_gzreader_read(int argc, VALUE *argv, VALUE obj) 03486 { 03487 struct gzfile *gz = get_gzfile(obj); 03488 VALUE vlen; 03489 long len; 03490 03491 rb_scan_args(argc, argv, "01", &vlen); 03492 if (NIL_P(vlen)) { 03493 return gzfile_read_all(gz); 03494 } 03495 03496 len = NUM2INT(vlen); 03497 if (len < 0) { 03498 rb_raise(rb_eArgError, "negative length %ld given", len); 03499 } 03500 return gzfile_read(gz, len); 03501 } 03502 03503 /* 03504 * Document-method: Zlib::GzipReader#readpartial 03505 * 03506 * call-seq: 03507 * gzipreader.readpartial(maxlen [, outbuf]) => string, outbuf 03508 * 03509 * Reads at most <i>maxlen</i> bytes from the gziped stream but 03510 * it blocks only if <em>gzipreader</em> has no data immediately available. 03511 * If the optional <i>outbuf</i> argument is present, 03512 * it must reference a String, which will receive the data. 03513 * It raises <code>EOFError</code> on end of file. 03514 */ 03515 static VALUE 03516 rb_gzreader_readpartial(int argc, VALUE *argv, VALUE obj) 03517 { 03518 struct gzfile *gz = get_gzfile(obj); 03519 VALUE vlen, outbuf; 03520 long len; 03521 03522 rb_scan_args(argc, argv, "11", &vlen, &outbuf); 03523 03524 len = NUM2INT(vlen); 03525 if (len < 0) { 03526 rb_raise(rb_eArgError, "negative length %ld given", len); 03527 } 03528 if (!NIL_P(outbuf)) 03529 Check_Type(outbuf, T_STRING); 03530 return gzfile_readpartial(gz, len, outbuf); 03531 } 03532 03533 /* 03534 * Document-method: Zlib::GzipReader#getc 03535 * 03536 * See Zlib::GzipReader documentation for a description. 03537 */ 03538 static VALUE 03539 rb_gzreader_getc(VALUE obj) 03540 { 03541 struct gzfile *gz = get_gzfile(obj); 03542 03543 return gzfile_getc(gz); 03544 } 03545 03546 /* 03547 * Document-method: Zlib::GzipReader#readchar 03548 * 03549 * See Zlib::GzipReader documentation for a description. 03550 */ 03551 static VALUE 03552 rb_gzreader_readchar(VALUE obj) 03553 { 03554 VALUE dst; 03555 dst = rb_gzreader_getc(obj); 03556 if (NIL_P(dst)) { 03557 rb_raise(rb_eEOFError, "end of file reached"); 03558 } 03559 return dst; 03560 } 03561 03562 /* 03563 * Document-method: Zlib::GzipReader#getbyte 03564 * 03565 * See Zlib::GzipReader documentation for a description. 03566 */ 03567 static VALUE 03568 rb_gzreader_getbyte(VALUE obj) 03569 { 03570 struct gzfile *gz = get_gzfile(obj); 03571 VALUE dst; 03572 03573 dst = gzfile_read(gz, 1); 03574 if (!NIL_P(dst)) { 03575 dst = INT2FIX((unsigned int)(RSTRING_PTR(dst)[0]) & 0xff); 03576 } 03577 return dst; 03578 } 03579 03580 /* 03581 * Document-method: Zlib::GzipReader#readbyte 03582 * 03583 * See Zlib::GzipReader documentation for a description. 03584 */ 03585 static VALUE 03586 rb_gzreader_readbyte(VALUE obj) 03587 { 03588 VALUE dst; 03589 dst = rb_gzreader_getbyte(obj); 03590 if (NIL_P(dst)) { 03591 rb_raise(rb_eEOFError, "end of file reached"); 03592 } 03593 return dst; 03594 } 03595 03596 /* 03597 * Document-method: Zlib::GzipReader#each_char 03598 * 03599 * See Zlib::GzipReader documentation for a description. 03600 */ 03601 static VALUE 03602 rb_gzreader_each_char(VALUE obj) 03603 { 03604 VALUE c; 03605 03606 RETURN_ENUMERATOR(obj, 0, 0); 03607 03608 while (!NIL_P(c = rb_gzreader_getc(obj))) { 03609 rb_yield(c); 03610 } 03611 return Qnil; 03612 } 03613 03614 /* 03615 * Document-method: Zlib::GzipReader#each_byte 03616 * 03617 * See Zlib::GzipReader documentation for a description. 03618 */ 03619 static VALUE 03620 rb_gzreader_each_byte(VALUE obj) 03621 { 03622 VALUE c; 03623 03624 RETURN_ENUMERATOR(obj, 0, 0); 03625 03626 while (!NIL_P(c = rb_gzreader_getbyte(obj))) { 03627 rb_yield(c); 03628 } 03629 return Qnil; 03630 } 03631 03632 /* 03633 * Document-method: Zlib::GzipReader#ungetc 03634 * 03635 * See Zlib::GzipReader documentation for a description. 03636 */ 03637 static VALUE 03638 rb_gzreader_ungetc(VALUE obj, VALUE s) 03639 { 03640 struct gzfile *gz; 03641 03642 if (FIXNUM_P(s)) 03643 return rb_gzreader_ungetbyte(obj, s); 03644 gz = get_gzfile(obj); 03645 StringValue(s); 03646 if (gz->enc2 && gz->enc2 != rb_ascii8bit_encoding()) { 03647 s = rb_str_conv_enc(s, rb_enc_get(s), gz->enc2); 03648 } 03649 gzfile_ungets(gz, (const Bytef*)RSTRING_PTR(s), RSTRING_LEN(s)); 03650 return Qnil; 03651 } 03652 03653 /* 03654 * Document-method: Zlib::GzipReader#ungetbyte 03655 * 03656 * See Zlib::GzipReader documentation for a description. 03657 */ 03658 static VALUE 03659 rb_gzreader_ungetbyte(VALUE obj, VALUE ch) 03660 { 03661 struct gzfile *gz = get_gzfile(obj); 03662 gzfile_ungetbyte(gz, NUM2CHR(ch)); 03663 return Qnil; 03664 } 03665 03666 static void 03667 gzreader_skip_linebreaks(struct gzfile *gz) 03668 { 03669 VALUE str; 03670 char *p; 03671 int n; 03672 03673 while (gz->z.buf_filled == 0) { 03674 if (GZFILE_IS_FINISHED(gz)) return; 03675 gzfile_read_more(gz); 03676 } 03677 n = 0; 03678 p = RSTRING_PTR(gz->z.buf); 03679 03680 while (n++, *(p++) == '\n') { 03681 if (n >= gz->z.buf_filled) { 03682 str = zstream_detach_buffer(&gz->z); 03683 gzfile_calc_crc(gz, str); 03684 while (gz->z.buf_filled == 0) { 03685 if (GZFILE_IS_FINISHED(gz)) return; 03686 gzfile_read_more(gz); 03687 } 03688 n = 0; 03689 p = RSTRING_PTR(gz->z.buf); 03690 } 03691 } 03692 03693 str = zstream_shift_buffer(&gz->z, n - 1); 03694 gzfile_calc_crc(gz, str); 03695 } 03696 03697 static void 03698 rscheck(const char *rsptr, long rslen, VALUE rs) 03699 { 03700 if (RSTRING_PTR(rs) != rsptr && RSTRING_LEN(rs) != rslen) 03701 rb_raise(rb_eRuntimeError, "rs modified"); 03702 } 03703 03704 static long 03705 gzreader_charboundary(struct gzfile *gz, long n) 03706 { 03707 char *s = RSTRING_PTR(gz->z.buf); 03708 char *e = s + gz->z.buf_filled; 03709 char *p = rb_enc_left_char_head(s, s + n, e, gz->enc); 03710 long l = p - s; 03711 if (l < n) { 03712 n = rb_enc_precise_mbclen(p, e, gz->enc); 03713 if (MBCLEN_NEEDMORE_P(n)) { 03714 if ((l = gzfile_fill(gz, l + MBCLEN_NEEDMORE_LEN(n))) > 0) { 03715 return l; 03716 } 03717 } 03718 else if (MBCLEN_CHARFOUND_P(n)) { 03719 return l + MBCLEN_CHARFOUND_LEN(n); 03720 } 03721 } 03722 return n; 03723 } 03724 03725 static VALUE 03726 gzreader_gets(int argc, VALUE *argv, VALUE obj) 03727 { 03728 struct gzfile *gz = get_gzfile(obj); 03729 volatile VALUE rs; 03730 VALUE dst; 03731 const char *rsptr; 03732 char *p, *res; 03733 long rslen, n, limit = -1; 03734 int rspara; 03735 rb_encoding *enc = gz->enc; 03736 int maxlen = rb_enc_mbmaxlen(enc); 03737 03738 if (argc == 0) { 03739 rs = rb_rs; 03740 } 03741 else { 03742 VALUE lim, tmp; 03743 03744 rb_scan_args(argc, argv, "11", &rs, &lim); 03745 if (!NIL_P(lim)) { 03746 if (!NIL_P(rs)) StringValue(rs); 03747 } 03748 else if (!NIL_P(rs)) { 03749 tmp = rb_check_string_type(rs); 03750 if (NIL_P(tmp)) { 03751 lim = rs; 03752 rs = rb_rs; 03753 } 03754 else { 03755 rs = tmp; 03756 } 03757 } 03758 if (!NIL_P(lim)) { 03759 limit = NUM2LONG(lim); 03760 if (limit == 0) return rb_str_new(0,0); 03761 } 03762 } 03763 03764 if (NIL_P(rs)) { 03765 if (limit < 0) { 03766 dst = gzfile_read_all(gz); 03767 if (RSTRING_LEN(dst) == 0) return Qnil; 03768 } 03769 else if ((n = gzfile_fill(gz, limit)) <= 0) { 03770 return Qnil; 03771 } 03772 else { 03773 if (maxlen > 1 && n >= limit && !GZFILE_IS_FINISHED(gz)) { 03774 n = gzreader_charboundary(gz, n); 03775 } 03776 else { 03777 n = limit; 03778 } 03779 dst = zstream_shift_buffer(&gz->z, n); 03780 gzfile_calc_crc(gz, dst); 03781 dst = gzfile_newstr(gz, dst); 03782 } 03783 gz->lineno++; 03784 return dst; 03785 } 03786 03787 if (RSTRING_LEN(rs) == 0) { 03788 rsptr = "\n\n"; 03789 rslen = 2; 03790 rspara = 1; 03791 } else { 03792 rsptr = RSTRING_PTR(rs); 03793 rslen = RSTRING_LEN(rs); 03794 rspara = 0; 03795 } 03796 03797 if (rspara) { 03798 gzreader_skip_linebreaks(gz); 03799 } 03800 03801 while (gz->z.buf_filled < rslen) { 03802 if (ZSTREAM_IS_FINISHED(&gz->z)) { 03803 if (gz->z.buf_filled > 0) gz->lineno++; 03804 return gzfile_read(gz, rslen); 03805 } 03806 gzfile_read_more(gz); 03807 } 03808 03809 p = RSTRING_PTR(gz->z.buf); 03810 n = rslen; 03811 for (;;) { 03812 long filled; 03813 if (n > gz->z.buf_filled) { 03814 if (ZSTREAM_IS_FINISHED(&gz->z)) break; 03815 gzfile_read_more(gz); 03816 p = RSTRING_PTR(gz->z.buf) + n - rslen; 03817 } 03818 if (!rspara) rscheck(rsptr, rslen, rs); 03819 filled = gz->z.buf_filled; 03820 if (limit > 0 && filled >= limit) { 03821 filled = limit; 03822 } 03823 res = memchr(p, rsptr[0], (filled - n + 1)); 03824 if (!res) { 03825 n = filled; 03826 if (limit > 0 && filled >= limit) break; 03827 n++; 03828 } else { 03829 n += (long)(res - p); 03830 p = res; 03831 if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break; 03832 p++, n++; 03833 } 03834 } 03835 if (maxlen > 1 && n == limit && (gz->z.buf_filled > n || !ZSTREAM_IS_FINISHED(&gz->z))) { 03836 n = gzreader_charboundary(gz, n); 03837 } 03838 03839 gz->lineno++; 03840 dst = gzfile_read(gz, n); 03841 if (rspara) { 03842 gzreader_skip_linebreaks(gz); 03843 } 03844 03845 return gzfile_newstr(gz, dst); 03846 } 03847 03848 /* 03849 * Document-method: Zlib::GzipReader#gets 03850 * 03851 * See Zlib::GzipReader documentation for a description. 03852 */ 03853 static VALUE 03854 rb_gzreader_gets(int argc, VALUE *argv, VALUE obj) 03855 { 03856 VALUE dst; 03857 dst = gzreader_gets(argc, argv, obj); 03858 if (!NIL_P(dst)) { 03859 rb_lastline_set(dst); 03860 } 03861 return dst; 03862 } 03863 03864 /* 03865 * Document-method: Zlib::GzipReader#readline 03866 * 03867 * See Zlib::GzipReader documentation for a description. 03868 */ 03869 static VALUE 03870 rb_gzreader_readline(int argc, VALUE *argv, VALUE obj) 03871 { 03872 VALUE dst; 03873 dst = rb_gzreader_gets(argc, argv, obj); 03874 if (NIL_P(dst)) { 03875 rb_raise(rb_eEOFError, "end of file reached"); 03876 } 03877 return dst; 03878 } 03879 03880 /* 03881 * Document-method: Zlib::GzipReader#each 03882 * 03883 * See Zlib::GzipReader documentation for a description. 03884 */ 03885 static VALUE 03886 rb_gzreader_each(int argc, VALUE *argv, VALUE obj) 03887 { 03888 VALUE str; 03889 03890 RETURN_ENUMERATOR(obj, 0, 0); 03891 03892 while (!NIL_P(str = gzreader_gets(argc, argv, obj))) { 03893 rb_yield(str); 03894 } 03895 return obj; 03896 } 03897 03898 /* 03899 * Document-method: Zlib::GzipReader#readlines 03900 * 03901 * See Zlib::GzipReader documentation for a description. 03902 */ 03903 static VALUE 03904 rb_gzreader_readlines(int argc, VALUE *argv, VALUE obj) 03905 { 03906 VALUE str, dst; 03907 dst = rb_ary_new(); 03908 while (!NIL_P(str = gzreader_gets(argc, argv, obj))) { 03909 rb_ary_push(dst, str); 03910 } 03911 return dst; 03912 } 03913 03914 #endif /* GZIP_SUPPORT */ 03915 03916 03917 03918 /* 03919 * Document-module: Zlib 03920 * 03921 * The Zlib module contains several classes for compressing and decompressing 03922 * streams, and for working with "gzip" files. 03923 * 03924 * == Classes 03925 * 03926 * Following are the classes that are most likely to be of interest to the 03927 * user: 03928 * Zlib::Inflate 03929 * Zlib::Deflate 03930 * Zlib::GzipReader 03931 * Zlib::GzipWriter 03932 * 03933 * There are two important base classes for the classes above: Zlib::ZStream 03934 * and Zlib::GzipFile. Everything else is an error class. 03935 * 03936 * == Constants 03937 * 03938 * Here's a list. 03939 * 03940 * Zlib::VERSION 03941 * The Ruby/zlib version string. 03942 * 03943 * Zlib::ZLIB_VERSION 03944 * The string which represents the version of zlib.h. 03945 * 03946 * Zlib::BINARY 03947 * Zlib::ASCII 03948 * Zlib::UNKNOWN 03949 * The integers representing data types which Zlib::ZStream#data_type 03950 * method returns. 03951 * 03952 * Zlib::NO_COMPRESSION 03953 * Zlib::BEST_SPEED 03954 * Zlib::BEST_COMPRESSION 03955 * Zlib::DEFAULT_COMPRESSION 03956 * The integers representing compression levels which are an argument 03957 * for Zlib::Deflate.new, Zlib::Deflate#deflate, and so on. 03958 * 03959 * Zlib::FILTERED 03960 * Zlib::HUFFMAN_ONLY 03961 * Zlib::DEFAULT_STRATEGY 03962 * The integers representing compression methods which are an argument 03963 * for Zlib::Deflate.new and Zlib::Deflate#params. 03964 * 03965 * Zlib::DEF_MEM_LEVEL 03966 * Zlib::MAX_MEM_LEVEL 03967 * The integers representing memory levels which are an argument for 03968 * Zlib::Deflate.new, Zlib::Deflate#params, and so on. 03969 * 03970 * Zlib::MAX_WBITS 03971 * The default value of windowBits which is an argument for 03972 * Zlib::Deflate.new and Zlib::Inflate.new. 03973 * 03974 * Zlib::NO_FLUSH 03975 * Zlib::SYNC_FLUSH 03976 * Zlib::FULL_FLUSH 03977 * Zlib::FINISH 03978 * The integers to control the output of the deflate stream, which are 03979 * an argument for Zlib::Deflate#deflate and so on. 03980 * 03981 * Zlib::OS_CODE 03982 * Zlib::OS_MSDOS 03983 * Zlib::OS_AMIGA 03984 * Zlib::OS_VMS 03985 * Zlib::OS_UNIX 03986 * Zlib::OS_VMCMS 03987 * Zlib::OS_ATARI 03988 * Zlib::OS_OS2 03989 * Zlib::OS_MACOS 03990 * Zlib::OS_ZSYSTEM 03991 * Zlib::OS_CPM 03992 * Zlib::OS_TOPS20 03993 * Zlib::OS_WIN32 03994 * Zlib::OS_QDOS 03995 * Zlib::OS_RISCOS 03996 * Zlib::OS_UNKNOWN 03997 * The return values of Zlib::GzipFile#os_code method. 03998 */ 03999 void 04000 Init_zlib() 04001 { 04002 VALUE mZlib, cZStream, cDeflate, cInflate; 04003 #if GZIP_SUPPORT 04004 VALUE cGzipFile, cGzipWriter, cGzipReader; 04005 #endif 04006 04007 mZlib = rb_define_module("Zlib"); 04008 04009 cZError = rb_define_class_under(mZlib, "Error", rb_eStandardError); 04010 cStreamEnd = rb_define_class_under(mZlib, "StreamEnd", cZError); 04011 cNeedDict = rb_define_class_under(mZlib, "NeedDict", cZError); 04012 cDataError = rb_define_class_under(mZlib, "DataError", cZError); 04013 cStreamError = rb_define_class_under(mZlib, "StreamError", cZError); 04014 cMemError = rb_define_class_under(mZlib, "MemError", cZError); 04015 cBufError = rb_define_class_under(mZlib, "BufError", cZError); 04016 cVersionError = rb_define_class_under(mZlib, "VersionError", cZError); 04017 04018 rb_define_module_function(mZlib, "zlib_version", rb_zlib_version, 0); 04019 rb_define_module_function(mZlib, "adler32", rb_zlib_adler32, -1); 04020 rb_define_module_function(mZlib, "adler32_combine", rb_zlib_adler32_combine, 3); 04021 rb_define_module_function(mZlib, "crc32", rb_zlib_crc32, -1); 04022 rb_define_module_function(mZlib, "crc32_combine", rb_zlib_crc32_combine, 3); 04023 rb_define_module_function(mZlib, "crc_table", rb_zlib_crc_table, 0); 04024 04025 /* The Ruby/zlib version string. */ 04026 rb_define_const(mZlib, "VERSION", rb_str_new2(RUBY_ZLIB_VERSION)); 04027 /* The string which represents the version of zlib.h */ 04028 rb_define_const(mZlib, "ZLIB_VERSION", rb_str_new2(ZLIB_VERSION)); 04029 04030 cZStream = rb_define_class_under(mZlib, "ZStream", rb_cObject); 04031 rb_undef_alloc_func(cZStream); 04032 rb_define_method(cZStream, "avail_out", rb_zstream_avail_out, 0); 04033 rb_define_method(cZStream, "avail_out=", rb_zstream_set_avail_out, 1); 04034 rb_define_method(cZStream, "avail_in", rb_zstream_avail_in, 0); 04035 rb_define_method(cZStream, "total_in", rb_zstream_total_in, 0); 04036 rb_define_method(cZStream, "total_out", rb_zstream_total_out, 0); 04037 rb_define_method(cZStream, "data_type", rb_zstream_data_type, 0); 04038 rb_define_method(cZStream, "adler", rb_zstream_adler, 0); 04039 rb_define_method(cZStream, "finished?", rb_zstream_finished_p, 0); 04040 rb_define_method(cZStream, "stream_end?", rb_zstream_finished_p, 0); 04041 rb_define_method(cZStream, "closed?", rb_zstream_closed_p, 0); 04042 rb_define_method(cZStream, "ended?", rb_zstream_closed_p, 0); 04043 rb_define_method(cZStream, "close", rb_zstream_end, 0); 04044 rb_define_method(cZStream, "end", rb_zstream_end, 0); 04045 rb_define_method(cZStream, "reset", rb_zstream_reset, 0); 04046 rb_define_method(cZStream, "finish", rb_zstream_finish, 0); 04047 rb_define_method(cZStream, "flush_next_in", rb_zstream_flush_next_in, 0); 04048 rb_define_method(cZStream, "flush_next_out", rb_zstream_flush_next_out, 0); 04049 04050 /* Integer representing date types which 04051 * ZStream#data_type method returns */ 04052 rb_define_const(mZlib, "BINARY", INT2FIX(Z_BINARY)); 04053 /* Integer representing date types which 04054 * ZStream#data_type method returns */ 04055 rb_define_const(mZlib, "ASCII", INT2FIX(Z_ASCII)); 04056 /* Integer representing date types which 04057 * ZStream#data_type method returns */ 04058 rb_define_const(mZlib, "UNKNOWN", INT2FIX(Z_UNKNOWN)); 04059 04060 cDeflate = rb_define_class_under(mZlib, "Deflate", cZStream); 04061 rb_define_singleton_method(cDeflate, "deflate", rb_deflate_s_deflate, -1); 04062 rb_define_singleton_method(mZlib, "deflate", rb_deflate_s_deflate, -1); 04063 rb_define_alloc_func(cDeflate, rb_deflate_s_allocate); 04064 rb_define_method(cDeflate, "initialize", rb_deflate_initialize, -1); 04065 rb_define_method(cDeflate, "initialize_copy", rb_deflate_init_copy, 1); 04066 rb_define_method(cDeflate, "deflate", rb_deflate_deflate, -1); 04067 rb_define_method(cDeflate, "<<", rb_deflate_addstr, 1); 04068 rb_define_method(cDeflate, "flush", rb_deflate_flush, -1); 04069 rb_define_method(cDeflate, "params", rb_deflate_params, 2); 04070 rb_define_method(cDeflate, "set_dictionary", rb_deflate_set_dictionary, 1); 04071 04072 cInflate = rb_define_class_under(mZlib, "Inflate", cZStream); 04073 rb_define_singleton_method(cInflate, "inflate", rb_inflate_s_inflate, 1); 04074 rb_define_singleton_method(mZlib, "inflate", rb_inflate_s_inflate, 1); 04075 rb_define_alloc_func(cInflate, rb_inflate_s_allocate); 04076 rb_define_method(cInflate, "initialize", rb_inflate_initialize, -1); 04077 rb_define_method(cInflate, "inflate", rb_inflate_inflate, 1); 04078 rb_define_method(cInflate, "<<", rb_inflate_addstr, 1); 04079 rb_define_method(cInflate, "sync", rb_inflate_sync, 1); 04080 rb_define_method(cInflate, "sync_point?", rb_inflate_sync_point_p, 0); 04081 rb_define_method(cInflate, "set_dictionary", rb_inflate_set_dictionary, 1); 04082 04083 /* compression level 0 04084 * 04085 * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ 04086 rb_define_const(mZlib, "NO_COMPRESSION", INT2FIX(Z_NO_COMPRESSION)); 04087 /* compression level 1 04088 * 04089 * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ 04090 rb_define_const(mZlib, "BEST_SPEED", INT2FIX(Z_BEST_SPEED)); 04091 /* compression level 9 04092 * 04093 * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ 04094 rb_define_const(mZlib, "BEST_COMPRESSION", INT2FIX(Z_BEST_COMPRESSION)); 04095 /* compression level -1 04096 * 04097 * Which is an argument for Deflate.new, Deflate#deflate, and so on. */ 04098 rb_define_const(mZlib, "DEFAULT_COMPRESSION", 04099 INT2FIX(Z_DEFAULT_COMPRESSION)); 04100 04101 /* compression method 1 04102 * 04103 * Which is an argument for Deflate.new and Deflate#params. */ 04104 rb_define_const(mZlib, "FILTERED", INT2FIX(Z_FILTERED)); 04105 /* compression method 2 04106 * 04107 * Which is an argument for Deflate.new and Deflate#params. */ 04108 rb_define_const(mZlib, "HUFFMAN_ONLY", INT2FIX(Z_HUFFMAN_ONLY)); 04109 /* compression method 0 04110 * 04111 * Which is an argument for Deflate.new and Deflate#params. */ 04112 rb_define_const(mZlib, "DEFAULT_STRATEGY", INT2FIX(Z_DEFAULT_STRATEGY)); 04113 04114 /* The default value of windowBits which is an argument for 04115 * Deflate.new and Inflate.new. 04116 */ 04117 rb_define_const(mZlib, "MAX_WBITS", INT2FIX(MAX_WBITS)); 04118 /* Default value is 8 04119 * 04120 * The integer representing memory levels. 04121 * Which are an argument for Deflate.new, Deflate#params, and so on. */ 04122 rb_define_const(mZlib, "DEF_MEM_LEVEL", INT2FIX(DEF_MEM_LEVEL)); 04123 /* Maximum level is 9 04124 * 04125 * The integers representing memory levels which are an argument for 04126 * Deflate.new, Deflate#params, and so on. */ 04127 rb_define_const(mZlib, "MAX_MEM_LEVEL", INT2FIX(MAX_MEM_LEVEL)); 04128 04129 /* Output control - 0 04130 * 04131 * The integers to control the output of the deflate stream, which are 04132 * an argument for Deflate#deflate and so on. */ 04133 rb_define_const(mZlib, "NO_FLUSH", INT2FIX(Z_NO_FLUSH)); 04134 /* Output control - 2 04135 * 04136 * The integers to control the output of the deflate stream, which are 04137 * an argument for Deflate#deflate and so on. */ 04138 rb_define_const(mZlib, "SYNC_FLUSH", INT2FIX(Z_SYNC_FLUSH)); 04139 /* Output control - 3 04140 * 04141 * The integers to control the output of the deflate stream, which are 04142 * an argument for Deflate#deflate and so on. */ 04143 rb_define_const(mZlib, "FULL_FLUSH", INT2FIX(Z_FULL_FLUSH)); 04144 /* Oputput control - 4 04145 * 04146 * The integers to control the output of the deflate stream, which are 04147 * an argument for Deflate#deflate and so on. */ 04148 rb_define_const(mZlib, "FINISH", INT2FIX(Z_FINISH)); 04149 04150 #if GZIP_SUPPORT 04151 id_write = rb_intern("write"); 04152 id_read = rb_intern("read"); 04153 id_readpartial = rb_intern("readpartial"); 04154 id_flush = rb_intern("flush"); 04155 id_seek = rb_intern("seek"); 04156 id_close = rb_intern("close"); 04157 id_path = rb_intern("path"); 04158 id_input = rb_intern("@input"); 04159 04160 cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject); 04161 cGzError = rb_define_class_under(cGzipFile, "Error", cZError); 04162 04163 /* input gzipped string */ 04164 rb_define_attr(cGzError, "input", 1, 0); 04165 rb_define_method(cGzError, "inspect", gzfile_error_inspect, 0); 04166 04167 cNoFooter = rb_define_class_under(cGzipFile, "NoFooter", cGzError); 04168 cCRCError = rb_define_class_under(cGzipFile, "CRCError", cGzError); 04169 cLengthError = rb_define_class_under(cGzipFile,"LengthError",cGzError); 04170 04171 cGzipWriter = rb_define_class_under(mZlib, "GzipWriter", cGzipFile); 04172 cGzipReader = rb_define_class_under(mZlib, "GzipReader", cGzipFile); 04173 rb_include_module(cGzipReader, rb_mEnumerable); 04174 04175 rb_define_singleton_method(cGzipFile, "wrap", rb_gzfile_s_wrap, -1); 04176 rb_undef_alloc_func(cGzipFile); 04177 rb_define_method(cGzipFile, "to_io", rb_gzfile_to_io, 0); 04178 rb_define_method(cGzipFile, "crc", rb_gzfile_crc, 0); 04179 rb_define_method(cGzipFile, "mtime", rb_gzfile_mtime, 0); 04180 rb_define_method(cGzipFile, "level", rb_gzfile_level, 0); 04181 rb_define_method(cGzipFile, "os_code", rb_gzfile_os_code, 0); 04182 rb_define_method(cGzipFile, "orig_name", rb_gzfile_orig_name, 0); 04183 rb_define_method(cGzipFile, "comment", rb_gzfile_comment, 0); 04184 rb_define_method(cGzipReader, "lineno", rb_gzfile_lineno, 0); 04185 rb_define_method(cGzipReader, "lineno=", rb_gzfile_set_lineno, 1); 04186 rb_define_method(cGzipWriter, "mtime=", rb_gzfile_set_mtime, 1); 04187 rb_define_method(cGzipWriter, "orig_name=", rb_gzfile_set_orig_name,1); 04188 rb_define_method(cGzipWriter, "comment=", rb_gzfile_set_comment, 1); 04189 rb_define_method(cGzipFile, "close", rb_gzfile_close, 0); 04190 rb_define_method(cGzipFile, "finish", rb_gzfile_finish, 0); 04191 rb_define_method(cGzipFile, "closed?", rb_gzfile_closed_p, 0); 04192 rb_define_method(cGzipReader, "eof", rb_gzfile_eof_p, 0); 04193 rb_define_method(cGzipReader, "eof?", rb_gzfile_eof_p, 0); 04194 rb_define_method(cGzipFile, "sync", rb_gzfile_sync, 0); 04195 rb_define_method(cGzipFile, "sync=", rb_gzfile_set_sync, 1); 04196 rb_define_method(cGzipReader, "pos", rb_gzfile_total_out, 0); 04197 rb_define_method(cGzipWriter, "pos", rb_gzfile_total_in, 0); 04198 rb_define_method(cGzipReader, "tell", rb_gzfile_total_out, 0); 04199 rb_define_method(cGzipWriter, "tell", rb_gzfile_total_in, 0); 04200 04201 rb_define_singleton_method(cGzipWriter, "open", rb_gzwriter_s_open,-1); 04202 rb_define_alloc_func(cGzipWriter, rb_gzwriter_s_allocate); 04203 rb_define_method(cGzipWriter, "initialize", rb_gzwriter_initialize,-1); 04204 rb_define_method(cGzipWriter, "flush", rb_gzwriter_flush, -1); 04205 rb_define_method(cGzipWriter, "write", rb_gzwriter_write, 1); 04206 rb_define_method(cGzipWriter, "putc", rb_gzwriter_putc, 1); 04207 rb_define_method(cGzipWriter, "<<", rb_gzwriter_addstr, 1); 04208 rb_define_method(cGzipWriter, "printf", rb_gzwriter_printf, -1); 04209 rb_define_method(cGzipWriter, "print", rb_gzwriter_print, -1); 04210 rb_define_method(cGzipWriter, "puts", rb_gzwriter_puts, -1); 04211 04212 rb_define_singleton_method(cGzipReader, "open", rb_gzreader_s_open,-1); 04213 rb_define_alloc_func(cGzipReader, rb_gzreader_s_allocate); 04214 rb_define_method(cGzipReader, "initialize", rb_gzreader_initialize, -1); 04215 rb_define_method(cGzipReader, "rewind", rb_gzreader_rewind, 0); 04216 rb_define_method(cGzipReader, "unused", rb_gzreader_unused, 0); 04217 rb_define_method(cGzipReader, "read", rb_gzreader_read, -1); 04218 rb_define_method(cGzipReader, "readpartial", rb_gzreader_readpartial, -1); 04219 rb_define_method(cGzipReader, "getc", rb_gzreader_getc, 0); 04220 rb_define_method(cGzipReader, "getbyte", rb_gzreader_getbyte, 0); 04221 rb_define_method(cGzipReader, "readchar", rb_gzreader_readchar, 0); 04222 rb_define_method(cGzipReader, "readbyte", rb_gzreader_readbyte, 0); 04223 rb_define_method(cGzipReader, "each_byte", rb_gzreader_each_byte, 0); 04224 rb_define_method(cGzipReader, "each_char", rb_gzreader_each_char, 0); 04225 rb_define_method(cGzipReader, "bytes", rb_gzreader_each_byte, 0); 04226 rb_define_method(cGzipReader, "ungetc", rb_gzreader_ungetc, 1); 04227 rb_define_method(cGzipReader, "ungetbyte", rb_gzreader_ungetbyte, 1); 04228 rb_define_method(cGzipReader, "gets", rb_gzreader_gets, -1); 04229 rb_define_method(cGzipReader, "readline", rb_gzreader_readline, -1); 04230 rb_define_method(cGzipReader, "each", rb_gzreader_each, -1); 04231 rb_define_method(cGzipReader, "each_line", rb_gzreader_each, -1); 04232 rb_define_method(cGzipReader, "lines", rb_gzreader_each, -1); 04233 rb_define_method(cGzipReader, "readlines", rb_gzreader_readlines, -1); 04234 04235 /* From GzipFile#os_code - code of current host */ 04236 rb_define_const(mZlib, "OS_CODE", INT2FIX(OS_CODE)); 04237 /* From GzipFile#os_code - 0x00 */ 04238 rb_define_const(mZlib, "OS_MSDOS", INT2FIX(OS_MSDOS)); 04239 /* From GzipFile#os_code - 0x01 */ 04240 rb_define_const(mZlib, "OS_AMIGA", INT2FIX(OS_AMIGA)); 04241 /* From GzipFile#os_code - 0x02 */ 04242 rb_define_const(mZlib, "OS_VMS", INT2FIX(OS_VMS)); 04243 /* From GzipFile#os_code - 0x03 */ 04244 rb_define_const(mZlib, "OS_UNIX", INT2FIX(OS_UNIX)); 04245 /* From GzipFile#os_code - 0x05 */ 04246 rb_define_const(mZlib, "OS_ATARI", INT2FIX(OS_ATARI)); 04247 /* From GzipFile#os_code - 0x06 */ 04248 rb_define_const(mZlib, "OS_OS2", INT2FIX(OS_OS2)); 04249 /* From GzipFile#os_code - 0x07 */ 04250 rb_define_const(mZlib, "OS_MACOS", INT2FIX(OS_MACOS)); 04251 /* From GzipFile#os_code - 0x0a */ 04252 rb_define_const(mZlib, "OS_TOPS20", INT2FIX(OS_TOPS20)); 04253 /* From GzipFile#os_code - 0x0b */ 04254 rb_define_const(mZlib, "OS_WIN32", INT2FIX(OS_WIN32)); 04255 04256 /* From GzipFile#os_code - 0x04 */ 04257 rb_define_const(mZlib, "OS_VMCMS", INT2FIX(OS_VMCMS)); 04258 /* From GzipFile#os_code - 0x08 */ 04259 rb_define_const(mZlib, "OS_ZSYSTEM", INT2FIX(OS_ZSYSTEM)); 04260 /* From GzipFile#os_code - 0x09 */ 04261 rb_define_const(mZlib, "OS_CPM", INT2FIX(OS_CPM)); 04262 /* From GzipFile#os_code - 0x0c */ 04263 rb_define_const(mZlib, "OS_QDOS", INT2FIX(OS_QDOS)); 04264 /* From GzipFile#os_code - 0x0d */ 04265 rb_define_const(mZlib, "OS_RISCOS", INT2FIX(OS_RISCOS)); 04266 /* From GzipFile#os_code - 0xff */ 04267 rb_define_const(mZlib, "OS_UNKNOWN", INT2FIX(OS_UNKNOWN)); 04268 04269 #endif /* GZIP_SUPPORT */ 04270 } 04271 04272 /* Document error classes. */ 04273 04274 /* 04275 * Document-class: Zlib::Error 04276 * 04277 * The superclass for all exceptions raised by Ruby/zlib. 04278 * 04279 * The following exceptions are defined as subclasses of Zlib::Error. These 04280 * exceptions are raised when zlib library functions return with an error 04281 * status. 04282 * 04283 * - Zlib::StreamEnd 04284 * - Zlib::NeedDict 04285 * - Zlib::DataError 04286 * - Zlib::StreamError 04287 * - Zlib::MemError 04288 * - Zlib::BufError 04289 * - Zlib::VersionError 04290 * 04291 */ 04292 04293 /* 04294 * Document-class: Zlib::StreamEnd 04295 * 04296 * Subclass of Zlib::Error 04297 * 04298 * When zlib returns a Z_STREAM_END 04299 * is return if the end of the compressed data has been reached 04300 * and all uncompressed out put has been produced. 04301 * 04302 */ 04303 04304 /* 04305 * Document-class: Zlib::NeedDict 04306 * 04307 * Subclass of Zlib::Error 04308 * 04309 * When zlib returns a Z_NEED_DICT 04310 * if a preset dictionary is needed at this point. 04311 * 04312 * Used by Zlib::Inflate.inflate and <tt>Zlib.inflate</tt> 04313 */ 04314 04315 /* 04316 * Document-class: Zlib::VersionError 04317 * 04318 * Subclass of Zlib::Error 04319 * 04320 * When zlib returns a Z_VERSION_ERROR, 04321 * usually if the zlib library version is incompatible with the 04322 * version assumed by the caller. 04323 * 04324 */ 04325 04326 /* 04327 * Document-class: Zlib::MemError 04328 * 04329 * Subclass of Zlib::Error 04330 * 04331 * When zlib returns a Z_MEM_ERROR, 04332 * usually if there was not enough memory. 04333 * 04334 */ 04335 04336 /* 04337 * Document-class: Zlib::StreamError 04338 * 04339 * Subclass of Zlib::Error 04340 * 04341 * When zlib returns a Z_STREAM_ERROR, 04342 * usually if the stream state was inconsistent. 04343 * 04344 */ 04345 04346 /* 04347 * Document-class: Zlib::BufError 04348 * 04349 * Subclass of Zlib::Error when zlib returns a Z_BUF_ERROR. 04350 * 04351 * Usually if no progress is possible. 04352 * 04353 */ 04354 04355 /* 04356 * Document-class: Zlib::DataError 04357 * 04358 * Subclass of Zlib::Error when zlib returns a Z_DATA_ERROR. 04359 * 04360 * Usually if a stream was prematurely freed. 04361 * 04362 */ 04363 04364 /* 04365 * Document-class: Zlib::GzipFile::Error 04366 * 04367 * Base class of errors that occur when processing GZIP files. 04368 */ 04369 04370 /* 04371 * Document-class: Zlib::GzipFile::NoFooter 04372 * 04373 * Raised when gzip file footer is not found. 04374 */ 04375 04376 /* 04377 * Document-class: Zlib::GzipFile::CRCError 04378 * 04379 * Raised when the CRC checksum recorded in gzip file footer is not equivalent 04380 * to the CRC checksum of the actual uncompressed data. 04381 */ 04382 04383 /* 04384 * Document-class: Zlib::GzipFile::LengthError 04385 * 04386 * Raised when the data length recorded in the gzip file footer is not equivalent 04387 * to the length of the actual uncompressed data. 04388 */ 04389 04390 04391
1.7.6.1