Beispiel #1
0
int __init jffs2_zlib_init(void)
{
	deflate_workspace = vmalloc(zlib_deflate_workspacesize());
	if (!deflate_workspace) {
		printk(KERN_WARNING "Failed to allocate %d bytes for deflate workspace\n", zlib_deflate_workspacesize());
		return -ENOMEM;
	}
	D1(printk(KERN_DEBUG "Allocated %d bytes for deflate workspace\n", zlib_deflate_workspacesize()));
	inflate_workspace = vmalloc(zlib_inflate_workspacesize());
	if (!inflate_workspace) {
		printk(KERN_WARNING "Failed to allocate %d bytes for inflate workspace\n", zlib_inflate_workspacesize());
		vfree(deflate_workspace);
		return -ENOMEM;
	}
	D1(printk(KERN_DEBUG "Allocated %d bytes for inflate workspace\n", zlib_inflate_workspacesize()));
	return 0;
}
Beispiel #2
0
int __init logfs_compr_init(void)
{
	size_t size = max(zlib_deflate_workspacesize(),
			zlib_inflate_workspacesize());
	stream.workspace = vmalloc(size);
	if (!stream.workspace)
		return -ENOMEM;
	return 0;
}
Beispiel #3
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;
}
Beispiel #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());
    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;
}
Beispiel #5
0
static void allocate_buf_for_compression(void)
{
    size_t size;
    size_t cmpr;

    switch (psinfo->bufsize) {
    /* buffer range for efivars */
    case 1000 ... 2000:
        cmpr = 56;
        break;
    case 2001 ... 3000:
        cmpr = 54;
        break;
    case 3001 ... 3999:
        cmpr = 52;
        break;
    /* buffer range for nvram, erst */
    case 4000 ... 10000:
        cmpr = 45;
        break;
    default:
        cmpr = 60;
        break;
    }

    big_oops_buf_sz = (psinfo->bufsize * 100) / cmpr;
    big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
    if (big_oops_buf) {
        size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL),
                   zlib_inflate_workspacesize());
        stream.workspace = kmalloc(size, GFP_KERNEL);
        if (!stream.workspace) {
            pr_err("No memory for compression workspace; skipping compression\n");
            kfree(big_oops_buf);
            big_oops_buf = NULL;
        }
    } else {
        pr_err("No memory for uncompressed data; skipping compression\n");
        stream.workspace = NULL;
    }

}
/**
 *	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;
}
Beispiel #8
0
Datei: cgzio.c Projekt: psfu/ULib
      {
      case Z_ERRNO:           p = "Error occured while reading file";                                          break;  
      case Z_STREAM_ERROR:    p = "The stream state was inconsistent (e.g., next_in or next_out was NULL)";    break;  
      case Z_DATA_ERROR:      p = "The deflate data was invalid or incomplete";                                break;  
      case Z_MEM_ERROR:       p = "Memory could not be allocated for processing";                              break;  
      case Z_BUF_ERROR:       p = "Ran out of output buffer for writing compressed bytes";                     break;  
      case Z_VERSION_ERROR:   p = "The version of zlib.h and the version of the library linked do not match";  break;  
      default:                p = "Unknown error code.";                                                       break;
      }

   return p;
}
#endif

#ifdef U_ZLIB_DEFLATE_WORKSPACESIZE
static int workspacesize = zlib_deflate_workspacesize();
static char workspace[workspacesize];
#endif

uint32_t u_gz_deflate(const char* restrict input, uint32_t len, char* restrict result, bool bheader)
{
   int err;
   z_stream stream;

   /**
    * stream.zalloc    = // Set zalloc, zfree, and opaque to Z_NULL so  
    * stream.zfree     = // that when we call deflateInit2 they will be  
    * stream.opaque    = // updated to use default allocation functions.  
    * stream.total_out = // Total number of output bytes produced so far  
    * stream.next_in   = // Pointer to input bytes  
    * stream.avail_in  = // Number of input bytes left to process
Beispiel #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;
}