ext/zlib/zlib.c in zlib-0.0.1 vs ext/zlib/zlib.c in zlib-0.1.0

- old
+ new

@@ -70,10 +70,11 @@ struct zstream_funcs; struct zstream_run_args; static void zstream_init(struct zstream*, const struct zstream_funcs*); static void zstream_expand_buffer(struct zstream*); static void zstream_expand_buffer_into(struct zstream*, unsigned long); +static int zstream_expand_buffer_non_stream(struct zstream *z); static void zstream_append_buffer(struct zstream*, const Bytef*, long); static VALUE zstream_detach_buffer(struct zstream*); static VALUE zstream_shift_buffer(struct zstream*, long); static void zstream_buffer_ungets(struct zstream*, const Bytef*, unsigned long); static void zstream_buffer_ungetbyte(struct zstream*, int); @@ -525,11 +526,10 @@ /*-------- zstream - internal APIs --------*/ struct zstream { unsigned long flags; VALUE buf; - long buf_filled; VALUE input; z_stream stream; const struct zstream_funcs { int (*reset)(z_streamp); int (*end)(z_streamp); @@ -548,10 +548,11 @@ #define ZSTREAM_READY(z) ((z)->flags |= ZSTREAM_FLAG_READY) #define ZSTREAM_IS_READY(z) ((z)->flags & ZSTREAM_FLAG_READY) #define ZSTREAM_IS_FINISHED(z) ((z)->flags & ZSTREAM_FLAG_FINISHED) #define ZSTREAM_IS_CLOSING(z) ((z)->flags & ZSTREAM_FLAG_CLOSING) #define ZSTREAM_IS_GZFILE(z) ((z)->flags & ZSTREAM_FLAG_GZFILE) +#define ZSTREAM_BUF_FILLED(z) (NIL_P((z)->buf) ? 0 : RSTRING_LEN((z)->buf)) #define ZSTREAM_EXPAND_BUFFER_OK 0 /* I think that more better value should be found, but I gave up finding it. B) */ @@ -597,11 +598,10 @@ static void zstream_init(struct zstream *z, const struct zstream_funcs *func) { z->flags = 0; z->buf = Qnil; - z->buf_filled = 0; z->input = Qnil; z->stream.zalloc = zlib_mem_alloc; z->stream.zfree = zlib_mem_free; z->stream.opaque = Z_NULL; z->stream.msg = Z_NULL; @@ -622,15 +622,15 @@ zstream_expand_buffer_into(z, ZSTREAM_INITIAL_BUFSIZE); return; } if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) { - if (z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { + long buf_filled = ZSTREAM_BUF_FILLED(z); + if (buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { int state = 0; VALUE self = (VALUE)z->stream.opaque; - rb_str_resize(z->buf, z->buf_filled); rb_obj_reveal(z->buf, rb_cString); OBJ_INFECT(z->buf, self); rb_protect(rb_yield, z->buf, &state); @@ -642,45 +642,32 @@ return; } else { zstream_expand_buffer_into(z, - ZSTREAM_AVAIL_OUT_STEP_MAX - z->buf_filled); + ZSTREAM_AVAIL_OUT_STEP_MAX - buf_filled); } } else { - if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { - z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; - } - else { - long inc = z->buf_filled / 2; - if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) { - inc = ZSTREAM_AVAIL_OUT_STEP_MIN; - } - rb_str_resize(z->buf, z->buf_filled + inc); - z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ? - (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX; - } - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; + zstream_expand_buffer_non_stream(z); } } static void zstream_expand_buffer_into(struct zstream *z, unsigned long size) { if (NIL_P(z->buf)) { /* I uses rb_str_new here not rb_str_buf_new because rb_str_buf_new makes a zero-length string. */ - z->buf = rb_str_new(0, size); - z->buf_filled = 0; + z->buf = rb_str_buf_new(size); z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); z->stream.avail_out = MAX_UINT(size); rb_obj_hide(z->buf); } else if (z->stream.avail_out != size) { - rb_str_resize(z->buf, z->buf_filled + size); - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; + rb_str_modify_expand(z->buf, size); + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); z->stream.avail_out = MAX_UINT(size); } } static void * @@ -693,70 +680,58 @@ return (void *)(VALUE)state; } static int -zstream_expand_buffer_without_gvl(struct zstream *z) +zstream_expand_buffer_non_stream(struct zstream *z) { - char * new_str; - long inc, len; + long inc, len = ZSTREAM_BUF_FILLED(z); - if (RSTRING_LEN(z->buf) - z->buf_filled >= ZSTREAM_AVAIL_OUT_STEP_MAX) { + if (rb_str_capacity(z->buf) - len >= ZSTREAM_AVAIL_OUT_STEP_MAX) { z->stream.avail_out = ZSTREAM_AVAIL_OUT_STEP_MAX; } else { - inc = z->buf_filled / 2; + inc = len / 2; if (inc < ZSTREAM_AVAIL_OUT_STEP_MIN) { inc = ZSTREAM_AVAIL_OUT_STEP_MIN; } - len = z->buf_filled + inc; - - new_str = ruby_xrealloc(RSTRING(z->buf)->as.heap.ptr, len + 1); - - /* from rb_str_resize */ - RSTRING(z->buf)->as.heap.ptr = new_str; - RSTRING(z->buf)->as.heap.ptr[len] = '\0'; /* sentinel */ - RSTRING(z->buf)->as.heap.len = - RSTRING(z->buf)->as.heap.aux.capa = len; - + rb_str_modify_expand(z->buf, inc); z->stream.avail_out = (inc < ZSTREAM_AVAIL_OUT_STEP_MAX) ? (int)inc : ZSTREAM_AVAIL_OUT_STEP_MAX; } - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); return ZSTREAM_EXPAND_BUFFER_OK; } static void zstream_append_buffer(struct zstream *z, const Bytef *src, long len) { if (NIL_P(z->buf)) { z->buf = rb_str_buf_new(len); rb_str_buf_cat(z->buf, (const char*)src, len); - z->buf_filled = len; z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf); z->stream.avail_out = 0; rb_obj_hide(z->buf); return; } - if (RSTRING_LEN(z->buf) < z->buf_filled + len) { - rb_str_resize(z->buf, z->buf_filled + len); + if ((long)rb_str_capacity(z->buf) < ZSTREAM_BUF_FILLED(z) + len) { + rb_str_modify_expand(z->buf, len); z->stream.avail_out = 0; } else { if (z->stream.avail_out >= (uInt)len) { z->stream.avail_out -= (uInt)len; } else { z->stream.avail_out = 0; } } - memcpy(RSTRING_PTR(z->buf) + z->buf_filled, src, len); - z->buf_filled += len; - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; + rb_str_cat(z->buf, (const char *)src, len); + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); } #define zstream_append_buffer2(z,v) \ zstream_append_buffer((z),(Bytef*)RSTRING_PTR(v),RSTRING_LEN(v)) @@ -775,18 +750,16 @@ if (NIL_P(z->buf)) { dst = rb_str_new(0, 0); } else { dst = z->buf; - rb_str_resize(dst, z->buf_filled); rb_obj_reveal(dst, rb_cString); } OBJ_INFECT(dst, self); z->buf = Qnil; - z->buf_filled = 0; z->stream.next_out = 0; z->stream.avail_out = 0; if (!ZSTREAM_IS_GZFILE(z) && rb_block_given_p()) { rb_yield(dst); @@ -798,22 +771,24 @@ static VALUE zstream_shift_buffer(struct zstream *z, long len) { VALUE dst; - long buflen; + char *bufptr; + long buflen = ZSTREAM_BUF_FILLED(z); - if (z->buf_filled <= len) { + if (buflen <= len) { return zstream_detach_buffer(z); } - dst = rb_str_new(RSTRING_PTR(z->buf), len); - z->buf_filled -= len; - memmove(RSTRING_PTR(z->buf), RSTRING_PTR(z->buf) + len, - z->buf_filled); - z->stream.next_out = (Bytef*)RSTRING_PTR(z->buf) + z->buf_filled; - buflen = RSTRING_LEN(z->buf) - z->buf_filled; + bufptr = RSTRING_PTR(z->buf); + dst = rb_str_new(bufptr, len); + buflen -= len; + memmove(bufptr, bufptr + len, buflen); + rb_str_set_len(z->buf, buflen); + z->stream.next_out = (Bytef*)RSTRING_END(z->buf); + buflen = (long)rb_str_capacity(z->buf) - ZSTREAM_BUF_FILLED(z); if (buflen > ZSTREAM_AVAIL_OUT_STEP_MAX) { buflen = ZSTREAM_AVAIL_OUT_STEP_MAX; } z->stream.avail_out = (uInt)buflen; @@ -821,38 +796,33 @@ } static void zstream_buffer_ungets(struct zstream *z, const Bytef *b, unsigned long len) { - if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) { + char *bufptr; + long filled; + + if (NIL_P(z->buf) || (long)rb_str_capacity(z->buf) <= ZSTREAM_BUF_FILLED(z)) { zstream_expand_buffer_into(z, len); } - memmove(RSTRING_PTR(z->buf) + len, RSTRING_PTR(z->buf), z->buf_filled); - memmove(RSTRING_PTR(z->buf), b, len); - z->buf_filled+=len; + RSTRING_GETMEM(z->buf, bufptr, filled); + memmove(bufptr + len, bufptr, filled); + memmove(bufptr, b, len); + rb_str_set_len(z->buf, filled + len); if (z->stream.avail_out > 0) { if (len > z->stream.avail_out) len = z->stream.avail_out; z->stream.next_out+=len; z->stream.avail_out-=(uInt)len; } } static void zstream_buffer_ungetbyte(struct zstream *z, int c) { - if (NIL_P(z->buf) || RSTRING_LEN(z->buf) - z->buf_filled == 0) { - zstream_expand_buffer(z); - } - - memmove(RSTRING_PTR(z->buf) + 1, RSTRING_PTR(z->buf), z->buf_filled); - RSTRING_PTR(z->buf)[0] = (char)c; - z->buf_filled++; - if (z->stream.avail_out > 0) { - z->stream.next_out++; - z->stream.avail_out--; - } + Bytef cc = (Bytef)c; + zstream_buffer_ungets(z, &cc, 1); } static void zstream_append_input(struct zstream *z, const Bytef *src, long len) { @@ -925,11 +895,10 @@ if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } z->flags = ZSTREAM_FLAG_READY; z->buf = Qnil; - z->buf_filled = 0; z->stream.next_out = 0; z->stream.avail_out = 0; zstream_reset_input(z); } @@ -966,11 +935,11 @@ err = Z_OK; while (!args->interrupt) { n = z->stream.avail_out; err = z->func->run(&z->stream, flush); - z->buf_filled += n - z->stream.avail_out; + rb_str_set_len(z->buf, ZSTREAM_BUF_FILLED(z) + (n - z->stream.avail_out)); if (err == Z_STREAM_END) { z->flags &= ~ZSTREAM_FLAG_IN_STREAM; z->flags |= ZSTREAM_FLAG_FINISHED; break; @@ -995,11 +964,11 @@ if (args->stream_output) { state = (int)(VALUE)rb_thread_call_with_gvl(zstream_expand_buffer_protect, (void *)z); } else { - state = zstream_expand_buffer_without_gvl(z); + state = zstream_expand_buffer_non_stream(z); } if (state) { err = Z_OK; /* buffer expanded but stream processing was stopped */ args->jump_state = state; @@ -1575,11 +1544,10 @@ if (err != Z_OK) { raise_zlib_error(err, 0); } z1->input = NIL_P(z2->input) ? Qnil : rb_str_dup(z2->input); z1->buf = NIL_P(z2->buf) ? Qnil : rb_str_dup(z2->buf); - z1->buf_filled = z2->buf_filled; z1->flags = z2->flags; return self; } @@ -1759,27 +1727,30 @@ { struct zstream *z = get_zstream(obj); int level, strategy; int err; uInt n; + long filled; level = ARG_LEVEL(v_level); strategy = ARG_STRATEGY(v_strategy); n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); - z->buf_filled += n - z->stream.avail_out; + filled = n - z->stream.avail_out; while (err == Z_BUF_ERROR) { rb_warning("deflateParams() returned Z_BUF_ERROR"); zstream_expand_buffer(z); + rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled); n = z->stream.avail_out; err = deflateParams(&z->stream, level, strategy); - z->buf_filled += n - z->stream.avail_out; + filled = n - z->stream.avail_out; } if (err != Z_OK) { raise_zlib_error(err, z->stream.msg); } + rb_str_set_len(z->buf, RSTRING_LEN(z->buf) + filled); return Qnil; } /* @@ -2228,11 +2199,11 @@ #define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED #define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1) #define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2) #define GZFILE_IS_FINISHED(gz) \ - (ZSTREAM_IS_FINISHED(&(gz)->z) && (gz)->z.buf_filled == 0) + (ZSTREAM_IS_FINISHED(&(gz)->z) && ZSTREAM_BUF_FILLED(&(gz)->z) == 0) #define GZFILE_READ_SIZE 2048 static void @@ -2354,11 +2325,11 @@ static void gzfile_write_raw(struct gzfile *gz) { VALUE str; - if (gz->z.buf_filled > 0) { + if (ZSTREAM_BUF_FILLED(&gz->z) > 0) { str = zstream_detach_buffer(&gz->z); OBJ_TAINT(str); /* for safe */ rb_funcall(gz->io, id_write, 1, str); if ((gz->z.flags & GZFILE_FLAG_SYNC) && rb_respond_to(gz->io, id_flush)) @@ -2687,13 +2658,13 @@ if (RSTRING_LEN(str) > 0) { /* prevent Z_BUF_ERROR */ zstream_run(&gz->z, (Bytef*)RSTRING_PTR(str), RSTRING_LEN(str), Z_SYNC_FLUSH); RB_GC_GUARD(str); } - if (gz->z.buf_filled > 0) break; + if (ZSTREAM_BUF_FILLED(&gz->z) > 0) break; } - return gz->z.buf_filled; + return ZSTREAM_BUF_FILLED(&gz->z); } static void gzfile_calc_crc(struct gzfile *gz, VALUE str) { @@ -2730,20 +2701,20 @@ { if (len < 0) rb_raise(rb_eArgError, "negative length %ld given", len); if (len == 0) return 0; - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) { gzfile_read_more(gz); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { gzfile_check_footer(gz); } return -1; } - return len < gz->z.buf_filled ? len : gz->z.buf_filled; + return len < ZSTREAM_BUF_FILLED(&gz->z) ? len : ZSTREAM_BUF_FILLED(&gz->z); } static VALUE gzfile_read(struct gzfile *gz, long len) { @@ -2774,11 +2745,11 @@ else { rb_str_resize(outbuf, 0); return outbuf; } } - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled == 0) { + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) == 0) { gzfile_read_more(gz); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { gzfile_check_footer(gz); @@ -2828,11 +2799,11 @@ { VALUE buf, dst = 0; int len; len = rb_enc_mbmaxlen(gz->enc); - while (!ZSTREAM_IS_FINISHED(&gz->z) && gz->z.buf_filled < len) { + while (!ZSTREAM_IS_FINISHED(&gz->z) && ZSTREAM_BUF_FILLED(&gz->z) < len) { gzfile_read_more(gz); } if (GZFILE_IS_FINISHED(gz)) { if (!(gz->z.flags & GZFILE_FLAG_FOOTER_FINISHED)) { gzfile_check_footer(gz); @@ -2846,11 +2817,11 @@ if (!gz->cbuf) { gz->cbuf = ALLOC_N(char, GZFILE_CBUF_CAPA); } ss = sp = (const unsigned char*)RSTRING_PTR(gz->z.buf); - se = sp + gz->z.buf_filled; + se = sp + ZSTREAM_BUF_FILLED(&gz->z); ds = dp = (unsigned char *)gz->cbuf; de = (unsigned char *)ds + GZFILE_CBUF_CAPA; (void)rb_econv_convert(gz->ec, &sp, se, &dp, de, ECONV_PARTIAL_INPUT|ECONV_AFTER_OUTPUT); rb_econv_check_error(gz->ec); dst = zstream_shift_buffer(&gz->z, sp - ss); @@ -3420,11 +3391,18 @@ */ static VALUE rb_gzfile_total_out(VALUE obj) { struct gzfile *gz = get_gzfile(obj); - return rb_uint2inum(gz->z.stream.total_out - gz->z.buf_filled); + uLong total_out = gz->z.stream.total_out; + long buf_filled = ZSTREAM_BUF_FILLED(&gz->z); + + if (total_out >= (uLong)buf_filled) { + return rb_uint2inum(total_out - buf_filled); + } else { + return LONG2FIX(-(buf_filled - (long)total_out)); + } } /* * Document-method: Zlib::GzipFile#path * @@ -3994,22 +3972,22 @@ { VALUE str; char *p; int n; - while (gz->z.buf_filled == 0) { + while (ZSTREAM_BUF_FILLED(&gz->z) == 0) { if (GZFILE_IS_FINISHED(gz)) return; gzfile_read_more(gz); } n = 0; p = RSTRING_PTR(gz->z.buf); while (n++, *(p++) == '\n') { - if (n >= gz->z.buf_filled) { + if (n >= ZSTREAM_BUF_FILLED(&gz->z)) { str = zstream_detach_buffer(&gz->z); gzfile_calc_crc(gz, str); - while (gz->z.buf_filled == 0) { + while (ZSTREAM_BUF_FILLED(&gz->z) == 0) { if (GZFILE_IS_FINISHED(gz)) return; gzfile_read_more(gz); } n = 0; p = RSTRING_PTR(gz->z.buf); @@ -4029,11 +4007,11 @@ static long gzreader_charboundary(struct gzfile *gz, long n) { char *s = RSTRING_PTR(gz->z.buf); - char *e = s + gz->z.buf_filled; + char *e = s + ZSTREAM_BUF_FILLED(&gz->z); char *p = rb_enc_left_char_head(s, s + n, e, gz->enc); long l = p - s; if (l < n) { n = rb_enc_precise_mbclen(p, e, gz->enc); if (MBCLEN_NEEDMORE_P(n)) { @@ -4124,29 +4102,29 @@ if (rspara) { gzreader_skip_linebreaks(gz); } - while (gz->z.buf_filled < rslen) { + while (ZSTREAM_BUF_FILLED(&gz->z) < rslen) { if (ZSTREAM_IS_FINISHED(&gz->z)) { - if (gz->z.buf_filled > 0) gz->lineno++; + if (ZSTREAM_BUF_FILLED(&gz->z) > 0) gz->lineno++; return gzfile_read(gz, rslen); } gzfile_read_more(gz); } p = RSTRING_PTR(gz->z.buf); n = rslen; for (;;) { long filled; - if (n > gz->z.buf_filled) { + if (n > ZSTREAM_BUF_FILLED(&gz->z)) { if (ZSTREAM_IS_FINISHED(&gz->z)) break; gzfile_read_more(gz); p = RSTRING_PTR(gz->z.buf) + n - rslen; } if (!rspara) rscheck(rsptr, rslen, rs); - filled = gz->z.buf_filled; + filled = ZSTREAM_BUF_FILLED(&gz->z); if (limit > 0 && filled >= limit) { filled = limit; } res = memchr(p, rsptr[0], (filled - n + 1)); if (!res) { @@ -4159,10 +4137,10 @@ p = res; if (rslen == 1 || memcmp(p, rsptr, rslen) == 0) break; p++, n++; } } - if (maxlen > 1 && n == limit && (gz->z.buf_filled > n || !ZSTREAM_IS_FINISHED(&gz->z))) { + if (maxlen > 1 && n == limit && (ZSTREAM_BUF_FILLED(&gz->z) > n || !ZSTREAM_IS_FINISHED(&gz->z))) { n = gzreader_charboundary(gz, n); } gz->lineno++; dst = gzfile_read(gz, n);