Example #1
0
static int zlib_decomp_init(void)
{
	struct z_stream_s *stream = &ubifs_zlib_stream;
	int ret;

	stream->workspace = xzalloc(zlib_inflate_workspacesize());

	ret = zlib_inflateInit2(stream, -MAX_WBITS);
	if (ret != Z_OK) {
		free(stream->workspace);
		return -EINVAL;
	}

	return 0;
}
Example #2
0
static void
gzip1_decompress(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-843", coa != NULL);
    assert("edward-876", src_len != 0);

    stream.workspace = coa;
    ret = zlib_inflateInit2(&stream, -GZIP1_DEF_WINBITS);
    if (ret != Z_OK) {
        warning("edward-774", "zlib_inflateInit2 returned %d\n", ret);
        return;
    }
    ret = zlib_inflateReset(&stream);
    if (ret != Z_OK) {
        warning("edward-775", "zlib_inflateReset returned %d\n", ret);
        return;
    }

    stream.next_in = src_first;
    stream.avail_in = src_len;
    stream.next_out = dst_first;
    stream.avail_out = *dst_len;

    ret = zlib_inflate(&stream, Z_SYNC_FLUSH);
    /*
     * Work around a bug in zlib, which sometimes wants to taste an extra
     * byte when being used in the (undocumented) raw deflate mode.
     * (From USAGI).
     */
    if (ret == Z_OK && !stream.avail_in && stream.avail_out) {
        u8 zerostuff = 0;
        stream.next_in = &zerostuff;
        stream.avail_in = 1;
        ret = zlib_inflate(&stream, Z_FINISH);
    }
    if (ret != Z_STREAM_END) {
        warning("edward-776", "zlib_inflate returned %d\n", ret);
        return;
    }
    *dst_len = stream.total_out;
    return;
}
Example #3
0
void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
{
	z_stream s;
	int r, i, flags;

	/* skip header */
	i = 10;
	flags = src[3];
	if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) {
		puts("bad gzipped data\n");
		exit();
	}
	if ((flags & EXTRA_FIELD) != 0)
		i = 12 + src[10] + (src[11] << 8);
	if ((flags & ORIG_NAME) != 0)
		while (src[i++] != 0)
			;
	if ((flags & COMMENT) != 0)
		while (src[i++] != 0)
			;
	if ((flags & HEAD_CRC) != 0)
		i += 2;
	if (i >= *lenp) {
		puts("gunzip: ran out of data in header\n");
		exit();
	}

	/* Initialize ourself. */
	s.workspace = zalloc(zlib_inflate_workspacesize());
	r = zlib_inflateInit2(&s, -MAX_WBITS);
	if (r != Z_OK) {
		puts("zlib_inflateInit2 returned "); puthex(r); puts("\n");
		exit();
	}
	s.next_in = src + i;
	s.avail_in = *lenp - i;
	s.next_out = dst;
	s.avail_out = dstlen;
	r = zlib_inflate(&s, Z_FINISH);
	if (r != Z_OK && r != Z_STREAM_END) {
		puts("inflate returned "); puthex(r); puts("\n");
		exit();
	}
	*lenp = s.next_out - (unsigned char *) dst;
	zlib_inflateEnd(&s);
}
void gunzip (void *dst, int dstlen, unsigned char *src, int *lenp)
{
	z_stream s;
	int r, i, flags;

        
        i = 10;
        flags = src[3];
        if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
                
                exit();
        }
        if ((flags & EXTRA_FIELD) != 0)
                i = 12 + src[10] + (src[11] << 8);
        if ((flags & ORIG_NAME) != 0)
                while (src[i++] != 0)
                        ;
        if ((flags & COMMENT) != 0)
                while (src[i++] != 0)
                        ;
        if ((flags & HEAD_CRC) != 0)
                i += 2;
        if (i >= *lenp) {
                
                exit();
        }

	s.workspace = zalloc(zlib_inflate_workspacesize());
        r = zlib_inflateInit2(&s, -MAX_WBITS);
        if (r != Z_OK) {
                
                exit();
        }
        s.next_in = src + i;
        s.avail_in = *lenp - i;
        s.next_out = dst;
        s.avail_out = dstlen;
        r = zlib_inflate(&s, Z_FINISH);
        if (r != Z_OK && r != Z_STREAM_END) {
                
                exit();
        }
        *lenp = s.next_out - (unsigned char *) dst;
        zlib_inflateEnd(&s);
}
/* _VMKLNX_CODECHECK_: zlib_inflate_blob */
int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
                      const void *buf, unsigned int len)
{
        const u8 *zbuf = buf;
        struct z_stream_s *strm;
        int rc;
        
#if defined(__VMKLNX__)
	VMK_ASSERT(vmk_PreemptionIsEnabled() == VMK_FALSE);
#endif
        rc = -ENOMEM;
        strm = kmalloc(sizeof(*strm), GFP_KERNEL);
        if (strm == NULL)
                goto gunzip_nomem1;
        strm->workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
        if (strm->workspace == NULL)
                goto gunzip_nomem2;
        
        /* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)
         * expected to be stripped from input
         */
        strm->next_in = zbuf;
        strm->avail_in = len;
        strm->next_out = gunzip_buf;
        strm->avail_out = sz;
        
        rc = zlib_inflateInit2(strm, -MAX_WBITS);
        if (rc == Z_OK) {
                rc = zlib_inflate(strm, Z_FINISH);
                /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
                if (rc == Z_STREAM_END)
                        rc = sz - strm->avail_out;
                else
                        rc = -EINVAL;
                zlib_inflateEnd(strm);
        } else
                rc = -EINVAL;
        
        kfree(strm->workspace);
 gunzip_nomem2:
        kfree(strm);
 gunzip_nomem1:
        return rc; /* returns Z_OK (0) if successful */
}
void gunzip_start(struct gunzip_state *state, void *src, int srclen)
{
	char *hdr = src;
	int hdrlen = 0;

	memset(state, 0, sizeof(*state));

	/* Check for gzip magic number */
	if ((hdr[0] == 0x1f) && (hdr[1] == 0x8b)) {
		/* gzip data, initialize zlib parameters */
		int r, flags;

		state->s.workspace = state->scratch;
		if (zlib_inflate_workspacesize() > sizeof(state->scratch))
			fatal("insufficient scratch space for gunzip\n\r");

		/* skip header */
		hdrlen = 10;
		flags = hdr[3];
		if (hdr[2] != Z_DEFLATED || (flags & RESERVED) != 0)
			fatal("bad gzipped data\n\r");
		if ((flags & EXTRA_FIELD) != 0)
			hdrlen = 12 + hdr[10] + (hdr[11] << 8);
		if ((flags & ORIG_NAME) != 0)
			while (hdr[hdrlen++] != 0)
				;
		if ((flags & COMMENT) != 0)
			while (hdr[hdrlen++] != 0)
				;
		if ((flags & HEAD_CRC) != 0)
			hdrlen += 2;
		if (hdrlen >= srclen)
			fatal("gunzip_start: ran out of data in header\n\r");

		r = zlib_inflateInit2(&state->s, -MAX_WBITS);
		if (r != Z_OK)
			fatal("inflateInit2 returned %d\n\r", r);
	}

	state->s.total_in = hdrlen;
	state->s.next_in = src + hdrlen;
	state->s.avail_in = srclen - hdrlen;
}
Example #7
0
/* Utility function: initialize zlib, unpack binary blob, clean up zlib,
 * return len or negative error code.
 */
int zlib_inflate_blob(void *gunzip_buf, unsigned int sz,
		      const void *buf, unsigned int len)
{
	const u8 *zbuf = buf;
	struct z_stream_s *strm;
	int rc;

	rc = -ENOMEM;
	strm = MALLOC(sizeof(*strm));
	if (strm == NULL)
		goto gunzip_nomem1;
	strm->workspace = MALLOC(zlib_inflate_workspacesize());
	if (strm->workspace == NULL)
		goto gunzip_nomem2;

	/* gzip header (1f,8b,08... 10 bytes total + possible asciz filename)
	 * expected to be stripped from input
	 */
	strm->next_in = zbuf;
	strm->avail_in = len;
	strm->next_out = gunzip_buf;
	strm->avail_out = sz;

	rc = zlib_inflateInit2(strm, -MAX_WBITS);
	if (rc == Z_OK) {
		rc = zlib_inflate(strm, Z_FINISH);
		/* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
		if (rc == Z_STREAM_END)
			rc = sz - strm->avail_out;
		else
			rc = -EINVAL;
		zlib_inflateEnd(strm);
	} else
		rc = -EINVAL;

	FREE(strm->workspace);
gunzip_nomem2:
	FREE(strm);
gunzip_nomem1:
	return rc; /* returns Z_OK (0) if successful */
}
Example #8
0
static int deflate_decomp_init(struct deflate_ctx *ctx)
{
    int ret = 0;
    struct z_stream_s *stream = &ctx->decomp_stream;

    stream->workspace = kzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
    if (!stream->workspace ) {
        ret = -ENOMEM;
        goto out;
    }
    ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
    if (ret != Z_OK) {
        ret = -EINVAL;
        goto out_free;
    }
out:
    return ret;
out_free:
    kfree(stream->workspace);
    goto out;
}
Example #9
0
/**
 *	z_decomp_alloc - allocate space for a decompressor.
 *	@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 we are requesting the peer to use in compressing
 *	data to be sent to us.
 *
 *	Returns the pointer to the private state for the decompressor,
 *	or NULL if we could not allocate enough memory.
 */
static void *z_decomp_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 = kzalloc(sizeof(*state), GFP_KERNEL);
	if (state == NULL)
		return NULL;

	state->w_size         = w_size;
	state->strm.next_out  = NULL;
/* LGE_CHANGE_S [LS855:[email protected]] 2011-07-11, */ 
#if 0  /* Delete Warning Log - OMAPS00243976 */
	state->strm.workspace = kmalloc(zlib_inflate_workspacesize(),
					GFP_KERNEL|__GFP_REPEAT);
#else
	state->strm.workspace = vmalloc(zlib_inflate_workspacesize());
#endif
/* LGE_CHANGE_E [LS855:[email protected]] 2011-07-11 */
	if (state->strm.workspace == NULL)
		goto out_free;

	if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
		goto out_free;
	return (void *) state;

out_free:
	z_decomp_free(state);
	return NULL;
}
Example #10
0
static int deflate_decomp_init(struct deflate_ctx *ctx, int format)
{
	int ret = 0;
	struct z_stream_s *stream = &ctx->decomp_stream;

	stream->workspace = vzalloc(zlib_inflate_workspacesize());
	if (!stream->workspace) {
		ret = -ENOMEM;
		goto out;
	}
	if (format)
		ret = zlib_inflateInit(stream);
	else
		ret = zlib_inflateInit2(stream, -DEFLATE_DEF_WINBITS);
	if (ret != Z_OK) {
		ret = -EINVAL;
		goto out_free;
	}
out:
	return ret;
out_free:
	vfree(stream->workspace);
	goto out;
}
Example #11
0
/* Included from initramfs et al code */
STATIC int INIT __gunzip(unsigned char *buf, long len,
		       long (*fill)(void*, unsigned long),
		       long (*flush)(void*, unsigned long),
		       unsigned char *out_buf, long out_len,
		       long *pos,
		       void(*error)(char *x)) {
	u8 *zbuf;
	struct z_stream_s *strm;
	int rc;

	rc = -1;
	if (flush) {
		out_len = 0x8000; /* 32 K */
		out_buf = malloc(out_len);
	} else {
		if (!out_len)
			out_len = ((size_t)~0) - (size_t)out_buf; /* no limit */
	}
	if (!out_buf) {
		error("Out of memory while allocating output buffer");
		goto gunzip_nomem1;
	}

	if (buf)
		zbuf = buf;
	else {
		zbuf = malloc(GZIP_IOBUF_SIZE);
		len = 0;
	}
	if (!zbuf) {
		error("Out of memory while allocating input buffer");
		goto gunzip_nomem2;
	}

	strm = malloc(sizeof(*strm));
	if (strm == NULL) {
		error("Out of memory while allocating z_stream");
		goto gunzip_nomem3;
	}

	strm->workspace = malloc(flush ? zlib_inflate_workspacesize() :
				 sizeof(struct inflate_state));
	if (strm->workspace == NULL) {
		error("Out of memory while allocating workspace");
		goto gunzip_nomem4;
	}

	if (!fill)
		fill = nofill;

	if (len == 0)
		len = fill(zbuf, GZIP_IOBUF_SIZE);

	/* verify the gzip header */
	if (len < 10 ||
	   zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) {
		if (pos)
			*pos = 0;
		error("Not a gzip file");
		goto gunzip_5;
	}

	/* skip over gzip header (1f,8b,08... 10 bytes total +
	 * possible asciz filename)
	 */
	strm->next_in = zbuf + 10;
	strm->avail_in = len - 10;
	/* skip over asciz filename */
	if (zbuf[3] & 0x8) {
		do {
			/*
			 * If the filename doesn't fit into the buffer,
			 * the file is very probably corrupt. Don't try
			 * to read more data.
			 */
			if (strm->avail_in == 0) {
				error("header error");
				goto gunzip_5;
			}
			--strm->avail_in;
		} while (*strm->next_in++);
	}

	strm->next_out = out_buf;
	strm->avail_out = out_len;

	rc = zlib_inflateInit2(strm, -MAX_WBITS);

	if (!flush) {
		WS(strm)->inflate_state.wsize = 0;
		WS(strm)->inflate_state.window = NULL;
	}

	while (rc == Z_OK) {
		if (strm->avail_in == 0) {
			/* TODO: handle case where both pos and fill are set */
			len = fill(zbuf, GZIP_IOBUF_SIZE);
			if (len < 0) {
				rc = -1;
				error("read error");
				break;
			}
			strm->next_in = zbuf;
			strm->avail_in = len;
		}
		rc = zlib_inflate(strm, 0);

		/* Write any data generated */
		if (flush && strm->next_out > out_buf) {
			long l = strm->next_out - out_buf;
			if (l != flush(out_buf, l)) {
				rc = -1;
				error("write error");
				break;
			}
			strm->next_out = out_buf;
			strm->avail_out = out_len;
		}

		/* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
		if (rc == Z_STREAM_END) {
			rc = 0;
			break;
		} else if (rc != Z_OK) {
			error("uncompression error");
			rc = -1;
		}
	}

	zlib_inflateEnd(strm);
	if (pos)
		/* add + 8 to skip over trailer */
		*pos = strm->next_in - zbuf+8;

gunzip_5:
	free(strm->workspace);
gunzip_nomem4:
	free(strm);
gunzip_nomem3:
	if (!buf)
		free(zbuf);
gunzip_nomem2:
	if (flush)
		free(out_buf);
gunzip_nomem1:
	return rc; /* returns Z_OK (0) if successful */
}
Example #12
0
int inflate_read(LPBYTE pbySrcBuffer, LONG lSrcBuffSize, LPBYTE *ppbyDstBuffer, LONG *plDstBuffSize, BOOL bGZip)
{
#   define  CHUNK           4096

    int     nRet            = Z_DATA_ERROR;
    LONG    lReadSize       = 0;
    BYTE    pbyBufferTmp[CHUNK];
    LONG    lTotalSize      = 0;
    LPBYTE  pbyDstBuffer    = NULL;

    z_stream strm;

    strm.zalloc     =  Z_NULL;
    strm.zfree      =  Z_NULL;
    strm.opaque     =  Z_NULL;
    strm.avail_in   =  0;
    strm.next_in    =  Z_NULL;

    if (bGZip)
        nRet = zlib_inflateInit2(&strm, 47);
    else
        nRet = zlib_inflateInit(&strm);

    if (nRet != Z_OK)
        return nRet;

    strm.avail_in = lSrcBuffSize;
    strm.next_in  = pbySrcBuffer;

    do
    {
        strm.avail_out = CHUNK;
        strm.next_out  = pbyBufferTmp;

        nRet = zlib_inflate(&strm, Z_NO_FLUSH);

        switch (nRet)
        {
            case Z_NEED_DICT:
                nRet = Z_DATA_ERROR;
            case Z_DATA_ERROR:
            case Z_MEM_ERROR:
                goto Exit0;
        }

        lReadSize    = CHUNK - strm.avail_out;
        lTotalSize  += lReadSize;
        pbyDstBuffer = (LPBYTE)realloc(pbyDstBuffer, lTotalSize);
        memcpy(pbyDstBuffer + lTotalSize - lReadSize, pbyBufferTmp, lReadSize);
    }
    while (strm.avail_out == 0);

Exit0:

    zlib_inflateEnd(&strm);

    if (Z_STREAM_END == nRet)
    {
        *ppbyDstBuffer = pbyDstBuffer;
        *plDstBuffSize = lTotalSize;
    }
    else
    {
        if (pbyDstBuffer)
        {
            free(pbyDstBuffer);
            pbyDstBuffer = NULL;
        }
        *ppbyDstBuffer = NULL;
        *plDstBuffSize = 0;
    }

    return (Z_STREAM_END == nRet) ? Z_OK : Z_DATA_ERROR;
}