Exemplo n.º 1
0
static int deflate_compress(struct crypto_tfm *tfm, const u8 *src,
                            unsigned int slen, u8 *dst, unsigned int *dlen)
{
    int ret = 0;
    struct deflate_ctx *dctx = crypto_tfm_ctx(tfm);
    struct z_stream_s *stream = &dctx->comp_stream;

    ret = zlib_deflateReset(stream);
    if (ret != Z_OK) {
        ret = -EINVAL;
        goto out;
    }

    stream->next_in = (u8 *)src;
    stream->avail_in = slen;
    stream->next_out = (u8 *)dst;
    stream->avail_out = *dlen;

    ret = zlib_deflate(stream, Z_FINISH);
    if (ret != Z_STREAM_END) {
        ret = -EINVAL;
        goto out;
    }
    ret = 0;
    *dlen = stream->total_out;
out:
    return ret;
}
Exemplo n.º 2
0
/*Compress Date on a zlib stream*/
CpaStatus deflate_compress(struct z_stream_s *stream,
        const Cpa8U *src,
        Cpa32U slen,
        Cpa8U *dst,
        Cpa32U dlen)
{
#ifdef USE_ZLIB
    int ret = 0;
    int inflate_type = Z_FINISH;

    ret = zlib_deflateReset(stream);
    if(ret != Z_OK)
    {
        PRINT_ERR("Error in deflateReset\n");
        vfree(stream->workspace);
        return CPA_STATUS_FAIL;
    }

    stream->next_in = (Cpa8U *)src;
    stream->avail_in = slen;
    stream->next_out = (Cpa8U *)dst;
    stream->avail_out = dlen;

    ret = zlib_deflate(stream, inflate_type);
    if (ret != Z_STREAM_END)
    {
        PRINT_ERR("Error in zlib_deflate, ret = %d\n", ret);
        vfree(stream->workspace);
        return CPA_STATUS_FAIL;
    }
#endif
    return CPA_STATUS_SUCCESS;

}
Exemplo n.º 3
0
/**
 *	z_comp_reset - reset a previously-allocated compressor.
 *	@arg:	pointer to private state for the compressor.
 *
 *	This clears the history for the compressor and makes it
 *	ready to start emitting a new compressed stream.
 */
static void z_comp_reset(void *arg)
{
	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;

	state->seqno = 0;
	zlib_deflateReset(&state->strm);
}
Exemplo n.º 4
0
int tux_gzip_compress (tux_req_t *req, unsigned char *data_in, unsigned char *data_out, __u32 *in_len, __u32 *out_len)
{
	z_stream *s = &req->ti->gzip_state;
	int ret, left;

	down(&req->ti->gzip_sem);
	if (zlib_deflateReset(s) != Z_OK)
		BUG();

	s->next_in = data_in;
	s->next_out = data_out;
	s->avail_in = *in_len;
	s->avail_out = *out_len;

	Dprintk("calling zlib_deflate with avail_in %d, avail_out %d\n", s->avail_in, s->avail_out);
	ret = zlib_deflate(s, Z_FINISH);
	Dprintk("deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n", s->avail_in, s->avail_out, s->total_in, s->total_out);

	if (ret != Z_STREAM_END) {
		printk("bad: deflate returned with %d! avail_in %d, avail_out %d, total_in %ld, total_out %ld\n", ret, s->avail_in, s->avail_out, s->total_in, s->total_out);
		BUG();
	}
	*in_len = s->avail_in;
	*out_len = s->avail_out;
	left = s->avail_in;

	up(&req->ti->gzip_sem);

	return left;
}
Exemplo n.º 5
0
static int zlib_compress_init(struct crypto_pcomp *tfm)
{
	int ret;
	struct zlib_ctx *dctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
	struct z_stream_s *stream = &dctx->comp_stream;

	ret = zlib_deflateReset(stream);
	if (ret != Z_OK)
		return -EINVAL;

	return 0;
}
Exemplo n.º 6
0
static INT32 wcn_gzip_compressor(void *worker, UINT8 *in_buf, INT32 in_sz, UINT8 *out_buf, INT32 *out_sz, INT32 finish)
{
    INT32 ret = 0;
    z_stream *stream = NULL;
    INT32 tmp = *out_sz;

    STP_DBG_INFO_FUNC("in buf 0x%08x, in sz %d\n", (unsigned int)in_buf, in_sz);
    STP_DBG_INFO_FUNC("out buf 0x%08x, out sz %d\n", (unsigned int)out_buf, tmp);

    stream = (z_stream*)worker;
    if (!stream) {
        STP_DBG_ERR_FUNC("invalid workspace!\n");
        return -1;
    }

    if (in_sz > 0) {
#if 0
        ret = zlib_deflateReset(stream);
        if (ret != Z_OK) {
            STP_DBG_ERR_FUNC("reset failed!\n");
            return -2;
        }
#endif

        stream->next_in = in_buf;
        stream->avail_in = in_sz;
        stream->next_out = out_buf;
        stream->avail_out = tmp;

        zlib_deflate(stream, Z_FULL_FLUSH);

        if (finish) {
            while (1) {
                int val = zlib_deflate(stream, Z_FINISH);
                if (val == Z_OK) {
                    continue;
                } else if (val == Z_STREAM_END) {
                    break;
                } else {
                    STP_DBG_ERR_FUNC("finish operation failed %d\n", val);
                    return -3;
                }
            }
        }

        *out_sz = tmp - stream->avail_out;
    }

    STP_DBG_INFO_FUNC("out buf 0x%08x, out sz %d\n", (unsigned int)out_buf, *out_sz);

    return ret;
}
Exemplo n.º 7
0
static void
gzip1_compress(coa_t coa, __u8 * src_first, size_t src_len,
               __u8 * dst_first, size_t *dst_len)
{
    int ret = 0;
    struct z_stream_s stream;

    assert("edward-842", coa != NULL);
    assert("edward-875", src_len != 0);

    stream.workspace = coa;
    ret = zlib_deflateInit2(&stream, GZIP1_DEF_LEVEL, Z_DEFLATED,
                            -GZIP1_DEF_WINBITS, GZIP1_DEF_MEMLEVEL,
                            Z_DEFAULT_STRATEGY);
    if (ret != Z_OK) {
        warning("edward-771", "zlib_deflateInit2 returned %d\n", ret);
        goto rollback;
    }
    ret = zlib_deflateReset(&stream);
    if (ret != Z_OK) {
        warning("edward-772", "zlib_deflateReset returned %d\n", ret);
        goto rollback;
    }
    stream.next_in = src_first;
    stream.avail_in = src_len;
    stream.next_out = dst_first;
    stream.avail_out = *dst_len;

    ret = zlib_deflate(&stream, Z_FINISH);
    if (ret != Z_STREAM_END) {
        if (ret != Z_OK)
            warning("edward-773",
                    "zlib_deflate returned %d\n", ret);
        goto rollback;
    }
    *dst_len = stream.total_out;
    return;
rollback:
    *dst_len = src_len;
    return;
}
Exemplo n.º 8
0
/**
 *	z_comp_init - initialize a previously-allocated compressor.
 *	@arg:	pointer to the private state for the compressor
 *	@options: pointer to the CCP option data describing the
 *		compression that was negotiated with the peer
 *	@opt_len: length of the CCP option data at @options
 *	@unit:	PPP unit number for diagnostic messages
 *	@hdrlen: ignored (present for backwards compatibility)
 *	@debug:	debug flag; if non-zero, debug messages are printed.
 *
 *	The CCP options described by @options must match the options
 *	specified when the compressor was allocated.  The compressor
 *	history is reset.  Returns 0 for failure (CCP options don't
 *	match) or 1 for success.
 */
static int z_comp_init(void *arg, unsigned char *options, int opt_len,
		       int unit, int hdrlen, int debug)
{
	struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;

	if (opt_len < CILEN_DEFLATE
	    || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
	    || options[1] != CILEN_DEFLATE
	    || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
	    || DEFLATE_SIZE(options[2]) != state->w_size
	    || options[3] != DEFLATE_CHK_SEQUENCE)
		return 0;

	state->seqno = 0;
	state->unit  = unit;
	state->debug = debug;

	zlib_deflateReset(&state->strm);

	return 1;
}
Exemplo n.º 9
0
/* ========================================================================= */
int zlib_deflateInit2(
	z_streamp strm,
	int  level,
	int  method,
	int  windowBits,
	int  memLevel,
	int  strategy
)
{
    deflate_state *s;
    int noheader = 0;
    deflate_workspace *mem;
    char *next;

    ush *overlay;
    /* We overlay pending_buf and d_buf+l_buf. This works since the average
     * output size for (length,distance) codes is <= 24 bits.
     */

    if (strm == NULL) return Z_STREAM_ERROR;

    strm->msg = NULL;

    if (level == Z_DEFAULT_COMPRESSION) level = 6;

    mem = (deflate_workspace *) strm->workspace;

    if (windowBits < 0) { /* undocumented feature: suppress zlib header */
        noheader = 1;
        windowBits = -windowBits;
    }
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
        windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
	strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
        return Z_STREAM_ERROR;
    }
    
    /*
     * Direct the workspace's pointers to the chunks that were allocated
     * along with the deflate_workspace struct.
     */
    next = (char *) mem;
    next += sizeof(*mem);
    mem->window_memory = (Byte *) next;
    next += zlib_deflate_window_memsize(windowBits);
    mem->prev_memory = (Pos *) next;
    next += zlib_deflate_prev_memsize(windowBits);
    mem->head_memory = (Pos *) next;
    next += zlib_deflate_head_memsize(memLevel);
    mem->overlay_memory = next;
    
    s = (deflate_state *) &(mem->deflate_memory);
    strm->state = (struct internal_state *)s;
    s->strm = strm;

    s->noheader = noheader;
    s->w_bits = windowBits;
    s->w_size = 1 << s->w_bits;
    s->w_mask = s->w_size - 1;

    s->hash_bits = memLevel + 7;
    s->hash_size = 1 << s->hash_bits;
    s->hash_mask = s->hash_size - 1;
    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);

    s->window = (Byte *) mem->window_memory;
    s->prev   = (Pos *)  mem->prev_memory;
    s->head   = (Pos *)  mem->head_memory;

    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */

    overlay = (ush *) mem->overlay_memory;
    s->pending_buf = (uch *) overlay;
    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);

    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;

    s->level = level;
    s->strategy = strategy;
    s->method = (Byte)method;

    return zlib_deflateReset(strm);
}
Exemplo n.º 10
0
/* wcn_compressor_out - get the result data from L2 buffer
 * @ cprs - compressor's pointer
 * @ pbuf - point to L2 buffer
 * @ plen - out len
 *
 * Retunr 0 if success, else NULL
 */
INT32 wcn_compressor_out(P_WCN_COMPRESSOR_T cprs, PUINT8 *pbuf, PINT32 plen)
{
    INT32 ret = 0;
    INT32 tmp_len = 0;

    if ((!cprs) || (!pbuf) || (!plen)) {
        STP_DBG_ERR_FUNC("invalid para!\n");
        return -1;
    }

    // check if there's L1 data need flush to L2 buffer
    if (cprs->L1_pos > 0) {
        tmp_len = cprs->L2_buf_sz - cprs->L2_pos;

        if (cprs->f_compress_en && cprs->handler) {
            // need compress
            ret = cprs->handler(cprs->worker, cprs->L1_buf, cprs->L1_pos, &cprs->L2_buf[cprs->L2_pos], &tmp_len, 1);

            if (!ret) {
                cprs->crc32 = (crc32(cprs->crc32, cprs->L1_buf, cprs->L1_pos));
                cprs->L2_pos += tmp_len;

                /* Add 8 byte suffix
                   ===
                   32 bits UNCOMPRESS SIZE
                   32 bits CRC
                */
                *(uint32_t *)(&cprs->L2_buf[cprs->L2_pos]) = (cprs->crc32 ^ 0xffffffffUL);
                *(uint32_t *)(&cprs->L2_buf[cprs->L2_pos + 4]) = cprs->uncomp_size;
                cprs->L2_pos += 8;

                STP_DBG_INFO_FUNC("compress OK!\n");
            } else {
                STP_DBG_ERR_FUNC("compress error!\n");
            }
        } else {
            // no need compress
            tmp_len = (cprs->L1_pos > tmp_len) ? tmp_len : cprs->L1_pos;
            osal_memcpy(&cprs->L2_buf[cprs->L2_pos], cprs->L1_buf, tmp_len);
            cprs->L2_pos += tmp_len;
        }

        cprs->L1_pos = 0;
    }


    *pbuf = cprs->L2_buf;
    *plen = cprs->L2_pos;

    STP_DBG_INFO_FUNC("0x%08x, len %d\n", (unsigned int)*pbuf, *plen);

#if 1
    ret = zlib_deflateReset((z_stream*)cprs->worker);
    if (ret != Z_OK) {
        STP_DBG_ERR_FUNC("reset failed!\n");
        return -2;
    }
#endif

    return 0;
}
/* ========================================================================= */
int zlib_deflateInit2_(
    z_streamp strm,
    int  level,
    int  method,
    int  windowBits,
    int  memLevel,
    int  strategy,
    const char *version,
    int stream_size
)
{
    deflate_state *s;
    int noheader = 0;
    static char* my_version = ZLIB_VERSION;
    deflate_workspace *mem;

    ush *overlay;
    /* We overlay pending_buf and d_buf+l_buf. This works since the average
     * output size for (length,distance) codes is <= 24 bits.
     */

    if (version == NULL || version[0] != my_version[0] ||
            stream_size != sizeof(z_stream)) {
        return Z_VERSION_ERROR;
    }
    if (strm == NULL) return Z_STREAM_ERROR;

    strm->msg = NULL;

    /* purpose	:	0012939
     * author	:	Frank.Chang
     * date	:	2010-07-23
     * description:	Fix VPN bug
     *
     */


    if (level == Z_DEFAULT_COMPRESSION) level = 6;

    mem = (deflate_workspace *) strm->workspace;

    if (windowBits < 0) { /* undocumented feature: suppress zlib header */
        noheader = 1;
        windowBits = -windowBits;
    }
    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
            windowBits < 9 || windowBits > 15 || level < 0 || level > 9 ||
            strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
        return Z_STREAM_ERROR;
    }
    s = (deflate_state *) &(mem->deflate_memory);
    strm->state = (struct internal_state *)s;
    s->strm = strm;

    s->noheader = noheader;
    s->w_bits = windowBits;
    s->w_size = 1 << s->w_bits;
    s->w_mask = s->w_size - 1;

    s->hash_bits = memLevel + 7;
    s->hash_size = 1 << s->hash_bits;
    s->hash_mask = s->hash_size - 1;
    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);

    s->window = (Byte *) mem->window_memory;
    s->prev   = (Pos *)  mem->prev_memory;
    s->head   = (Pos *)  mem->head_memory;

    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */

    overlay = (ush *) mem->overlay_memory;
    s->pending_buf = (uch *) overlay;
    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);

    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;

    s->level = level;
    s->strategy = strategy;
    s->method = (Byte)method;

    return zlib_deflateReset(strm);
}