2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* This file is a part of Pcompress, a chunked parallel multi-
|
|
|
|
* algorithm lossless compression and decompression program.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Moinak Ghosh. All rights reserved.
|
|
|
|
* Use is subject to license terms.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2012-07-07 16:48:29 +00:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2012-05-28 14:49:29 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* moinakg@belenix.org, http://moinakg.wordpress.com/
|
|
|
|
*
|
|
|
|
* This program includes partly-modified public domain source
|
|
|
|
* code from the LZMA SDK: http://www.7-zip.org/sdk.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <pcompress.h>
|
|
|
|
#include <allocator.h>
|
|
|
|
|
2012-07-26 18:41:01 +00:00
|
|
|
/*
|
|
|
|
* Max buffer size allowed for a single zlib compress/decompress call.
|
|
|
|
*/
|
|
|
|
#define SINGLE_CALL_MAX (2147483648UL)
|
|
|
|
|
2012-11-09 15:37:23 +00:00
|
|
|
static void zerr(int ret, int cmp);
|
2012-08-08 17:10:58 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
static void *
|
|
|
|
slab_alloc_ui(void *p, unsigned int items, unsigned int size) {
|
|
|
|
void *ptr;
|
2012-12-09 04:45:06 +00:00
|
|
|
uint64_t tot = (uint64_t)items * (uint64_t)size;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
ptr = slab_alloc(p, tot);
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:33:24 +00:00
|
|
|
uint32_t
|
2012-12-27 17:36:48 +00:00
|
|
|
zlib_buf_extra(uint64_t buflen)
|
2012-07-27 16:33:24 +00:00
|
|
|
{
|
|
|
|
if (buflen > SINGLE_CALL_MAX)
|
|
|
|
buflen = SINGLE_CALL_MAX;
|
|
|
|
return (compressBound(buflen) - buflen);
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
int
|
2012-12-27 17:36:48 +00:00
|
|
|
zlib_init(void **data, int *level, int nthreads, uint64_t chunksize,
|
2012-11-22 15:32:50 +00:00
|
|
|
int file_version, compress_op_t op)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
2012-08-08 17:10:58 +00:00
|
|
|
z_stream *zs;
|
|
|
|
int ret;
|
|
|
|
|
2012-12-27 17:36:48 +00:00
|
|
|
zs = (z_stream *)slab_alloc(NULL, sizeof (z_stream));
|
2012-08-08 17:10:58 +00:00
|
|
|
zs->zalloc = slab_alloc_ui;
|
|
|
|
zs->zfree = slab_free;
|
|
|
|
zs->opaque = NULL;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
if (*level > 9) *level = 9;
|
2012-11-22 15:32:50 +00:00
|
|
|
if (op == COMPRESS) {
|
|
|
|
ret = deflateInit2(zs, *level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
|
|
|
|
} else {
|
|
|
|
if (file_version < 5) {
|
|
|
|
ret = inflateInit(zs);
|
|
|
|
} else {
|
|
|
|
ret = inflateInit2(zs, -15);
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 17:10:58 +00:00
|
|
|
if (ret != Z_OK) {
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(ret, 0);
|
2012-08-08 17:10:58 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
*data = zs;
|
2012-05-28 14:49:29 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
void
|
|
|
|
zlib_stats(int show)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-04 18:39:47 +00:00
|
|
|
void
|
2012-12-27 17:36:48 +00:00
|
|
|
zlib_props(algo_props_t *data, int level, uint64_t chunksize) {
|
2012-12-15 16:33:23 +00:00
|
|
|
data->delta2_span = 100;
|
2013-01-14 07:50:07 +00:00
|
|
|
data->deltac_min_distance = EIGHTM;
|
2012-12-04 18:39:47 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 17:10:58 +00:00
|
|
|
int
|
|
|
|
zlib_deinit(void **data)
|
|
|
|
{
|
|
|
|
if (*data) {
|
2012-08-24 14:46:21 +00:00
|
|
|
z_stream *zs = (z_stream *)(*data);
|
|
|
|
deflateEnd(zs);
|
2012-08-08 17:10:58 +00:00
|
|
|
slab_free(NULL, *data);
|
|
|
|
}
|
2012-12-27 17:36:48 +00:00
|
|
|
return (0);
|
2012-08-08 17:10:58 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
static
|
2012-11-09 15:37:23 +00:00
|
|
|
void zerr(int ret, int cmp)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
switch (ret) {
|
|
|
|
case Z_ERRNO:
|
|
|
|
perror(" ");
|
|
|
|
break;
|
|
|
|
case Z_STREAM_ERROR:
|
2012-07-26 18:41:01 +00:00
|
|
|
fprintf(stderr, "Zlib: Invalid stream structure\n");
|
2012-05-28 14:49:29 +00:00
|
|
|
break;
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
fprintf(stderr, "Zlib: Invalid or incomplete deflate data\n");
|
|
|
|
break;
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
fprintf(stderr, "Zlib: Out of memory\n");
|
|
|
|
break;
|
|
|
|
case Z_VERSION_ERROR:
|
|
|
|
fprintf(stderr, "Zlib: Version mismatch!\n");
|
|
|
|
break;
|
2012-07-26 18:41:01 +00:00
|
|
|
case Z_BUF_ERROR:
|
2012-11-09 15:37:23 +00:00
|
|
|
/* This error is non-fatal during compression. */
|
|
|
|
if (!cmp)
|
|
|
|
fprintf(stderr, "Zlib: Buffer error decompression failed.\n");
|
2012-07-26 18:41:01 +00:00
|
|
|
break;
|
|
|
|
case Z_NEED_DICT:
|
|
|
|
fprintf(stderr, "Zlib: Need present dictionary.\n");
|
|
|
|
break;
|
2012-05-28 14:49:29 +00:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "Zlib: Unknown error code: %d\n", ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
zlib_compress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen,
|
2012-07-01 16:14:02 +00:00
|
|
|
int level, uchar_t chdr, void *data)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
2012-07-26 18:41:01 +00:00
|
|
|
int ret, ending;
|
|
|
|
unsigned int slen, dlen;
|
2012-12-09 04:45:06 +00:00
|
|
|
uint64_t _srclen = srclen;
|
|
|
|
uint64_t _dstlen = *dstlen;
|
2012-12-27 17:36:48 +00:00
|
|
|
uchar_t *dst1 = (uchar_t *)dst;
|
|
|
|
uchar_t *src1 = (uchar_t *)src;
|
2012-08-08 17:10:58 +00:00
|
|
|
z_stream *zs = (z_stream *)data;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-07-26 18:41:01 +00:00
|
|
|
ending = 0;
|
|
|
|
while (_srclen > 0) {
|
|
|
|
if (_srclen > SINGLE_CALL_MAX) {
|
|
|
|
slen = SINGLE_CALL_MAX;
|
|
|
|
} else {
|
|
|
|
slen = _srclen;
|
|
|
|
ending = 1;
|
|
|
|
}
|
|
|
|
if (_dstlen > SINGLE_CALL_MAX) {
|
|
|
|
dlen = SINGLE_CALL_MAX;
|
|
|
|
} else {
|
|
|
|
dlen = _dstlen;
|
|
|
|
}
|
|
|
|
|
2012-08-08 17:10:58 +00:00
|
|
|
zs->next_in = src1;
|
|
|
|
zs->avail_in = slen;
|
|
|
|
zs->next_out = dst1;
|
|
|
|
zs->avail_out = dlen;
|
2012-07-26 18:41:01 +00:00
|
|
|
if (!ending) {
|
2012-08-08 17:10:58 +00:00
|
|
|
ret = deflate(zs, Z_NO_FLUSH);
|
2012-07-26 18:41:01 +00:00
|
|
|
if (ret != Z_OK) {
|
2012-08-08 17:10:58 +00:00
|
|
|
deflateReset(zs);
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(ret, 1);
|
2012-07-26 18:41:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-08 17:10:58 +00:00
|
|
|
ret = deflate(zs, Z_FINISH);
|
2012-07-26 18:41:01 +00:00
|
|
|
if (ret != Z_STREAM_END) {
|
2012-08-08 17:10:58 +00:00
|
|
|
deflateReset(zs);
|
2012-07-26 18:41:01 +00:00
|
|
|
if (ret == Z_OK)
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(Z_BUF_ERROR, 1);
|
2012-07-26 18:41:01 +00:00
|
|
|
else
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(ret, 1);
|
2012-07-26 18:41:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2012-08-08 17:10:58 +00:00
|
|
|
dst1 += (dlen - zs->avail_out);
|
|
|
|
_dstlen -= (dlen - zs->avail_out);
|
2012-07-26 18:41:01 +00:00
|
|
|
src1 += slen;
|
|
|
|
_srclen -= slen;
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
2012-07-26 18:41:01 +00:00
|
|
|
|
|
|
|
*dstlen = *dstlen - _dstlen;
|
2012-08-08 17:10:58 +00:00
|
|
|
ret = deflateReset(zs);
|
2012-05-28 14:49:29 +00:00
|
|
|
if (ret != Z_OK) {
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(ret, 1);
|
2012-05-28 14:49:29 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
zlib_decompress(void *src, uint64_t srclen, void *dst, uint64_t *dstlen,
|
2012-07-01 16:14:02 +00:00
|
|
|
int level, uchar_t chdr, void *data)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
int err;
|
2012-07-26 18:41:01 +00:00
|
|
|
unsigned int slen, dlen;
|
2012-12-09 04:45:06 +00:00
|
|
|
uint64_t _srclen = srclen;
|
|
|
|
uint64_t _dstlen = *dstlen;
|
2012-12-27 17:36:48 +00:00
|
|
|
uchar_t *dst1 = (uchar_t *)dst;
|
|
|
|
uchar_t *src1 = (uchar_t *)src;
|
2012-11-22 15:32:50 +00:00
|
|
|
z_stream *zs = (z_stream *)data;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-07-26 18:41:01 +00:00
|
|
|
while (_srclen > 0) {
|
|
|
|
if (_srclen > SINGLE_CALL_MAX) {
|
|
|
|
slen = SINGLE_CALL_MAX;
|
|
|
|
} else {
|
|
|
|
slen = _srclen;
|
|
|
|
}
|
|
|
|
if (_dstlen > SINGLE_CALL_MAX) {
|
|
|
|
dlen = SINGLE_CALL_MAX;
|
|
|
|
} else {
|
|
|
|
dlen = _dstlen;
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:32:50 +00:00
|
|
|
zs->next_in = src1;
|
|
|
|
zs->avail_in = slen;
|
|
|
|
zs->next_out = dst1;
|
|
|
|
zs->avail_out = dlen;
|
2012-07-26 18:41:01 +00:00
|
|
|
|
2012-11-22 15:32:50 +00:00
|
|
|
err = inflate(zs, Z_NO_FLUSH);
|
2012-07-26 18:41:01 +00:00
|
|
|
if (err != Z_OK && err != Z_STREAM_END) {
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(err, 0);
|
2012-07-26 18:41:01 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:32:50 +00:00
|
|
|
dst1 += (dlen - zs->avail_out);
|
|
|
|
_dstlen -= (dlen - zs->avail_out);
|
|
|
|
src1 += (slen - zs->avail_in);
|
|
|
|
_srclen -= (slen - zs->avail_in);
|
2012-07-26 18:41:01 +00:00
|
|
|
|
|
|
|
if (err == Z_STREAM_END) {
|
|
|
|
if (_srclen > 0) {
|
2012-11-09 15:37:23 +00:00
|
|
|
zerr(Z_DATA_ERROR, 0);
|
2012-07-26 18:41:01 +00:00
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 17:49:38 +00:00
|
|
|
*dstlen = *dstlen - _dstlen;
|
2012-11-22 15:32:50 +00:00
|
|
|
err = inflateReset(zs);
|
|
|
|
if (err != Z_OK) {
|
|
|
|
zerr(err, 1);
|
|
|
|
return (-1);
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
return (0);
|
|
|
|
}
|