示例#1
0
文件: gzip.c 项目: AlexShiLucky/tbox
static tb_long_t tb_zip_gzip_spak_inflate(tb_zip_ref_t zip, tb_static_stream_ref_t ist, tb_static_stream_ref_t ost, tb_long_t sync)
{
    // check
    tb_zip_gzip_t* gzip = tb_zip_gzip_cast(zip);
    tb_assert_and_check_return_val(gzip && ist && ost, -1);

    // the input stream
    tb_byte_t* ip = ist->p;
    tb_byte_t* ie = ist->e;
    tb_check_return_val(ip && ip < ie, 0);

    // the output stream
    tb_byte_t* op = ost->p;
    tb_byte_t* oe = ost->e;
    tb_assert_and_check_return_val(op && oe, -1);

    // attach zstream
    gzip->zstream.next_in = (Bytef*)ip;
    gzip->zstream.avail_in = (uInt)(ie - ip);

    gzip->zstream.next_out = (Bytef*)op;
    gzip->zstream.avail_out = (uInt)(oe - op);

    // inflate 
    tb_int_t r = inflate(&gzip->zstream, !sync? Z_NO_FLUSH : Z_SYNC_FLUSH);
    tb_assertf_and_check_return_val(r == Z_OK || r == Z_STREAM_END, -1, "sync: %ld, error: %d", sync, r);
    tb_trace_d("inflate: %u => %u, sync: %ld", ie - ip, (tb_byte_t*)gzip->zstream.next_out - op, sync);

    // update 
    ist->p = (tb_byte_t*)gzip->zstream.next_in;
    ost->p = (tb_byte_t*)gzip->zstream.next_out;

    // end?
    tb_check_return_val(r != Z_STREAM_END || ost->p > op, -1);

    // ok?
    return (ost->p - op);
}
示例#2
0
文件: gzip.c 项目: DonkeyWs/tbox
static tb_long_t tb_zip_gzip_spak_deflate(tb_zip_ref_t zip, tb_static_stream_ref_t ist, tb_static_stream_ref_t ost, tb_long_t sync)
{
    tb_zip_gzip_t* gzip = tb_zip_gzip_cast(zip);
    tb_assert_and_check_return_val(gzip && ist && ost, -1);

    // the input stream, @note maybe null for flush the end data
    tb_byte_t* ip = ist->p;
    tb_byte_t* ie = ist->e;

    // the output stream
    tb_byte_t* op = ost->p;
    tb_byte_t* oe = ost->e;
    tb_assert_and_check_return_val(op && oe, -1);

    // attach zst
    gzip->zst.next_in = (Bytef*)ip;
    gzip->zst.avail_in = (uInt)(ie - ip);

    gzip->zst.next_out = (Bytef*)op;
    gzip->zst.avail_out = (uInt)(oe - op);

    // deflate 
    tb_int_t r = deflate(&gzip->zst, sync > 0? Z_SYNC_FLUSH : (sync < 0? Z_FINISH : Z_NO_FLUSH));
    tb_assertf_and_check_return_val(r == Z_OK || r == Z_STREAM_END, -1, "sync: %ld, error: %d", sync, r);
    tb_trace_d("deflate: %u => %u, sync: %ld", (tb_size_t)(ie - ip), (tb_size_t)((tb_byte_t*)gzip->zst.next_out - op), sync);

    // update 
    ist->p = (tb_byte_t*)gzip->zst.next_in;
    ost->p = (tb_byte_t*)gzip->zst.next_out;

    // end?
    tb_check_return_val(r != Z_STREAM_END || ost->p > op, -1);

    // ok?
    return (ost->p - op);
}