示例#1
0
文件: platform.c 项目: infidel/linux
/* Derived from logfs_compress() */
static int pstore_compress(const void *in, void *out, size_t inlen,
                           size_t outlen)
{
    int err, ret;

    ret = -EIO;
    err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
                            MEM_LEVEL, Z_DEFAULT_STRATEGY);
    if (err != Z_OK)
        goto error;

    stream.next_in = in;
    stream.avail_in = inlen;
    stream.total_in = 0;
    stream.next_out = out;
    stream.avail_out = outlen;
    stream.total_out = 0;

    err = zlib_deflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END)
        goto error;

    err = zlib_deflateEnd(&stream);
    if (err != Z_OK)
        goto error;

    if (stream.total_out >= stream.total_in)
        goto error;

    ret = stream.total_out;
error:
    return ret;
}
示例#2
0
/* initilise a zlib stream*/
CpaStatus deflate_init(struct z_stream_s *stream)
{
#ifdef USE_ZLIB
    int ret = 0;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
    stream->workspace = vmalloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
#else
	stream->workspace = vmalloc(zlib_deflate_workspacesize());
#endif
    if(NULL == stream->workspace)
    {
        PRINT_ERR("Could not allocate zlib workspace memory\n");
        return CPA_STATUS_FAIL;
    }
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
    memset(stream->workspace, 0,
            zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
#else
    memset(stream->workspace, 0,
            zlib_deflate_workspacesize());
#endif
    ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
              -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL, Z_DEFAULT_STRATEGY);
    if(ret != Z_OK)
    {
        PRINT_ERR("Error in zlib_deflateInit2\n");
        vfree(stream->workspace);
        return CPA_STATUS_FAIL;
    }
#endif
    return CPA_STATUS_SUCCESS;
}
示例#3
0
文件: deflate.c 项目: avagin/linux
static int deflate_comp_init(struct deflate_ctx *ctx, int format)
{
	int ret = 0;
	struct z_stream_s *stream = &ctx->comp_stream;

	stream->workspace = vzalloc(zlib_deflate_workspacesize(
				    MAX_WBITS, MAX_MEM_LEVEL));
	if (!stream->workspace) {
		ret = -ENOMEM;
		goto out;
	}
	if (format)
		ret = zlib_deflateInit(stream, 3);
	else
		ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
					-DEFLATE_DEF_WINBITS,
					DEFLATE_DEF_MEMLEVEL,
					Z_DEFAULT_STRATEGY);
	if (ret != Z_OK) {
		ret = -EINVAL;
		goto out_free;
	}
out:
	return ret;
out_free:
	vfree(stream->workspace);
	goto out;
}
示例#4
0
static int deflate_comp_init(struct deflate_ctx *ctx)
{
    int ret = 0;
    struct z_stream_s *stream = &ctx->comp_stream;

    stream->workspace = vmalloc(zlib_deflate_workspacesize(
                                    -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL));
    if (!stream->workspace ) {
        ret = -ENOMEM;
        goto out;
    }
//	memset(stream->workspace, 0, zlib_deflate_workspacesize());
    ret = zlib_deflateInit2(stream, DEFLATE_DEF_LEVEL, Z_DEFLATED,
                            -DEFLATE_DEF_WINBITS, DEFLATE_DEF_MEMLEVEL,
                            Z_DEFAULT_STRATEGY);
    if (ret != Z_OK) {
        ret = -EINVAL;
        goto out_free;
    }
out:
    return ret;
out_free:
    vfree(stream->workspace);
    goto out;
}
示例#5
0
static int zlib_compress_setup(struct crypto_pcomp *tfm, void *params,
			       unsigned int len)
{
	struct zlib_ctx *ctx = crypto_tfm_ctx(crypto_pcomp_tfm(tfm));
	struct z_stream_s *stream = &ctx->comp_stream;
	struct nlattr *tb[ZLIB_COMP_MAX + 1];
	int window_bits, mem_level;
	size_t workspacesize;
	int ret;

	ret = nla_parse(tb, ZLIB_COMP_MAX, params, len, NULL);
	if (ret)
		return ret;

	zlib_comp_exit(ctx);

		window_bits = tb[ZLIB_COMP_WINDOWBITS]
					? nla_get_u32(tb[ZLIB_COMP_WINDOWBITS])
					: MAX_WBITS;
		mem_level = tb[ZLIB_COMP_MEMLEVEL]
					? nla_get_u32(tb[ZLIB_COMP_MEMLEVEL])
					: DEF_MEM_LEVEL;

	workspacesize = zlib_deflate_workspacesize(window_bits, mem_level);

	stream->workspace = vmalloc(workspacesize);
	if (!stream->workspace)
		return -ENOMEM;

	memset(stream->workspace, 0, workspacesize);
	ret = zlib_deflateInit2(stream,
				tb[ZLIB_COMP_LEVEL]
					? nla_get_u32(tb[ZLIB_COMP_LEVEL])
					: Z_DEFAULT_COMPRESSION,
				tb[ZLIB_COMP_METHOD]
					? nla_get_u32(tb[ZLIB_COMP_METHOD])
					: Z_DEFLATED,
				tb[ZLIB_COMP_WINDOWBITS]
					? nla_get_u32(tb[ZLIB_COMP_WINDOWBITS])
					: MAX_WBITS,
				tb[ZLIB_COMP_MEMLEVEL]
					? nla_get_u32(tb[ZLIB_COMP_MEMLEVEL])
					: DEF_MEM_LEVEL,
				tb[ZLIB_COMP_STRATEGY]
					? nla_get_u32(tb[ZLIB_COMP_STRATEGY])
					: Z_DEFAULT_STRATEGY);
	if (ret != Z_OK) {
		vfree(stream->workspace);
		stream->workspace = NULL;
		return -EINVAL;
	}

	return 0;
}
示例#6
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;
}
示例#7
0
/**
 *	z_comp_alloc - allocate space for a compressor.
 *	@options: pointer to CCP option data
 *	@opt_len: length of the CCP option at @options.
 *
 *	The @options pointer points to the a buffer containing the
 *	CCP option data for the compression being negotiated.  It is
 *	formatted according to RFC1979, and describes the window
 *	size that the peer is requesting that we use in compressing
 *	data to be sent to it.
 *
 *	Returns the pointer to the private state for the compressor,
 *	or NULL if we could not allocate enough memory.
 */
static void *z_comp_alloc(unsigned char *options, int opt_len)
{
	struct ppp_deflate_state *state;
	int w_size;

	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
	    || options[3] != DEFLATE_CHK_SEQUENCE)
		return NULL;
	w_size = DEFLATE_SIZE(options[2]);
	if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
		return NULL;

	state = (struct ppp_deflate_state *) kmalloc(sizeof(*state),
						     GFP_KERNEL);
	if (state == NULL)
		return NULL;

	memset (state, 0, sizeof (struct ppp_deflate_state));
	state->strm.next_in   = NULL;
	state->w_size         = w_size;
	state->strm.workspace = vmalloc(zlib_deflate_workspacesize());
	if (state->strm.workspace == NULL)
		goto out_free;

	if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
			 DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
	    != Z_OK)
		goto out_free;
	return (void *) state;

out_free:
	z_comp_free(state);
	return NULL;
}
/* wcn_compressor_init - create a compressor and do init
 * @ name - compressor's name
 * @ L1_buf_sz - L1 buffer size
 * @ L2_buf_sz - L2 buffer size
 *
 * Retunr object's pointer if success, else NULL
 */
P_WCN_COMPRESSOR_T wcn_compressor_init(PUINT8 name, INT32 L1_buf_sz, INT32 L2_buf_sz)
{
    z_stream *pstream = NULL;
    P_WCN_COMPRESSOR_T compress = NULL;

    compress = (P_WCN_COMPRESSOR_T)osal_malloc(sizeof(WCN_COMPRESSOR_T));
    if (!compress) {
        STP_DBG_ERR_FUNC("alloc compressor failed!\n");
        goto fail;
    }

    osal_memset(compress, 0, sizeof(WCN_COMPRESSOR_T));
    osal_memcpy(compress->name, name, STP_OJB_NAME_SZ);

    compress->f_compress_en = 0;
    compress->compress_type = GZIP;

    if (compress->compress_type == GZIP) {
        compress->worker = osal_malloc(sizeof(z_stream));
        if (!compress->worker) {
            STP_DBG_ERR_FUNC("alloc stream failed!\n");
            goto fail;
        }
        pstream = (z_stream*)compress->worker;

        pstream->workspace = osal_malloc(zlib_deflate_workspacesize(MAX_WBITS, MAX_MEM_LEVEL));
        if (!pstream->workspace) {
            STP_DBG_ERR_FUNC("alloc workspace failed!\n");
            goto fail;
        }
        zlib_deflateInit2(pstream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS,
                          DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
    }

    compress->handler = wcn_gzip_compressor;
    compress->L1_buf_sz = L1_buf_sz;
    compress->L2_buf_sz = L2_buf_sz;
    compress->L1_pos = 0;
    compress->L2_pos = 0;
    compress->uncomp_size = 0;
    compress->crc32 = 0xffffffffUL;

    compress->L1_buf = osal_malloc(compress->L1_buf_sz);
    if (!compress->L1_buf) {
        STP_DBG_ERR_FUNC("alloc %d bytes for L1 buf failed!\n", compress->L1_buf_sz);
        goto fail;
    }

    compress->L2_buf = osal_malloc(compress->L2_buf_sz);
    if (!compress->L2_buf) {
        STP_DBG_ERR_FUNC("alloc %d bytes for L2 buf failed!\n", compress->L2_buf_sz);
        goto fail;
    }

    STP_DBG_INFO_FUNC("create compressor OK! L1 %d bytes, L2 %d bytes\n", L1_buf_sz, L2_buf_sz);
    return compress;

fail:
    if (compress) {
        if (compress->L2_buf) {
            osal_free(compress->L2_buf);
            compress->L2_buf = NULL;
        }

        if (compress->L1_buf) {
            osal_free(compress->L1_buf);
            compress->L1_buf = NULL;
        }

        if (compress->worker) {
            pstream = (z_stream*)compress->worker;
            if ((compress->compress_type == GZIP) && pstream->workspace) {
                zlib_deflateEnd(pstream);
                osal_free(pstream->workspace);
            }
            osal_free(compress->worker);
            compress->worker = NULL;
        }

        if (compress->worker) {
            osal_free(compress->worker);
            compress->worker = NULL;
        }

        osal_free(compress);
        compress = NULL;
    }

    STP_DBG_ERR_FUNC("init failed!\n");

    return NULL;
}
示例#9
0
bool kzip_add_file(struct kzip_file *zfile, const struct kzip_memlist *memlist, const char *zfilename)
{
    int ret, flush;
    z_stream strm;
    struct aee_timer zip_time;

    if (zfile->entries_num >= KZIP_ENTRY_MAX) {
	voprintf_error("Too manry zip entry %d\n", zfile->entries_num);
	return false;
    }

    voprintf_debug("%s: zf %p(%p) %s\n", __func__, zfile, zfile->write_cb, zfilename);
    struct kzip_entry *zentry = &zfile->zentries[zfile->entries_num++];
    zentry->filename = strdup(zfilename);
    zentry->localheader_offset = zfile->current_size;
    zentry->level = DEF_MEM_LEVEL;

    KZIP_DEBUG("%s: write local header\n", __func__);
    uint8_t zip_localheader[128];
    int hsize = put_localheader(zip_localheader, zfilename, DEF_MEM_LEVEL);
    if (kzip_write_current(zfile, zip_localheader, hsize) != hsize) {
        return false;
    }

    KZIP_DEBUG("%s: init compressor\n", __func__);
    /* allocate deflate state */
    strm.workspace = malloc(zlib_deflate_workspacesize(-MAX_WBITS, DEF_MEM_LEVEL));
    ret = zlib_deflateInit2(&strm, zentry->level, Z_DEFLATED, -MAX_WBITS,
			    DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
    if (ret != Z_OK) {
	voprintf_error("zlib compress init failed\n");
	free(strm.workspace);
        return false;
    }

    uint8_t *out = malloc(CHUNK);
 
    aee_timer_init(&zip_time);
    aee_timer_start(&zip_time);
    /* compress until end of file */
    do {
        flush = (memlist->size == 0) ? Z_FINISH : Z_NO_FLUSH;
 	
	KZIP_DEBUG("-- Compress memory %x, size %d\n", memlist->address, memlist->size);
	strm.avail_in = memlist->size;
        strm.next_in = memlist->address;
        /* run deflate() on input until output buffer not full, finish
           compression if all of source has been read in */
        do {
            strm.avail_out = CHUNK;
            strm.next_out = out;
            ret = zlib_deflate(&strm, flush);    /* no bad return value */
            assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
            int have = CHUNK - strm.avail_out;
	    if (have > 0) {
                aee_timer_stop(&zip_time);
		if (kzip_write_current(zfile, out, have) != have) {
                    goto error;
		}
                aee_timer_start(&zip_time);
	    }
        } while (strm.avail_out == 0);
        assert(strm.avail_in == 0);     /* all input will be used */

	memlist++;
        /* done when last data in file processed */
    } while (flush != Z_FINISH);
    assert(ret == Z_STREAM_END);        /* stream will be complete */

    /* clean up and return */
    (void)zlib_deflateEnd(&strm);
    free(strm.workspace);
    free(out);
    aee_timer_stop(&zip_time);
    voprintf_info("Zip time: %d sec\n", zip_time.acc_ms / 1000);

    zentry->comp_size = strm.total_out;
    zentry->uncomp_size = strm.total_in;
    zentry->crc32 = strm.crc32 ^ 0xffffffffUL;

    return true;

 error:
    free(strm.workspace);
    free(out);
    (void)zlib_deflateEnd(&strm);
    return false;
}