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 <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <LzmaEnc.h>
|
|
|
|
#include <LzmaDec.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <pcompress.h>
|
|
|
|
#include <allocator.h>
|
|
|
|
|
|
|
|
#define SZ_ERROR_DESTLEN 100
|
|
|
|
#define LZMA_DEFAULT_DICT (1 << 24)
|
|
|
|
|
|
|
|
CLzmaEncProps *p = NULL;
|
|
|
|
|
|
|
|
static ISzAlloc g_Alloc = {
|
|
|
|
slab_alloc,
|
|
|
|
slab_free,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
void
|
|
|
|
lzma_stats(int show)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
void
|
2012-12-09 04:45:06 +00:00
|
|
|
lzma_mt_props(algo_props_t *data, int level, int64_t chunksize) {
|
2012-08-18 04:50:52 +00:00
|
|
|
data->compress_mt_capable = 1;
|
|
|
|
data->decompress_mt_capable = 0;
|
|
|
|
data->buf_extra = 0;
|
|
|
|
data->c_max_threads = 2;
|
2012-12-04 18:39:47 +00:00
|
|
|
data->delta2_stride = 150;
|
2012-08-18 04:50:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-18 16:30:14 +00:00
|
|
|
void
|
2012-12-09 04:45:06 +00:00
|
|
|
lzma_props(algo_props_t *data, int level, int64_t chunksize) {
|
2012-08-18 16:30:14 +00:00
|
|
|
data->compress_mt_capable = 0;
|
|
|
|
data->decompress_mt_capable = 0;
|
|
|
|
data->buf_extra = 0;
|
2012-12-04 18:39:47 +00:00
|
|
|
data->delta2_stride = 150;
|
2012-08-18 16:30:14 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* The two functions below are not thread-safe, by design.
|
|
|
|
*/
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
lzma_init(void **data, int *level, int nthreads, int64_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-11-22 15:32:50 +00:00
|
|
|
if (!p && op == COMPRESS) {
|
2012-05-28 14:49:29 +00:00
|
|
|
p = (CLzmaEncProps *)slab_alloc(NULL, sizeof (CLzmaEncProps));
|
|
|
|
LzmaEncProps_Init(p);
|
|
|
|
/*
|
|
|
|
* Set the dictionary size and fast bytes based on level.
|
|
|
|
*/
|
|
|
|
if (*level < 8) {
|
|
|
|
/*
|
|
|
|
* Choose a dict size with a balance between perf and
|
|
|
|
* compression.
|
|
|
|
*/
|
|
|
|
p->dictSize = LZMA_DEFAULT_DICT;
|
2012-07-30 18:00:13 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Let LZMA determine best dict size.
|
|
|
|
*/
|
|
|
|
p->dictSize = 0;
|
|
|
|
}
|
2012-07-30 18:00:13 +00:00
|
|
|
|
|
|
|
/* Determine the fast bytes value and also adjust dict size further. */
|
2012-07-09 17:58:11 +00:00
|
|
|
if (*level < 7) {
|
2012-05-28 14:49:29 +00:00
|
|
|
p->fb = 32;
|
2012-07-09 17:58:11 +00:00
|
|
|
|
|
|
|
} else if (*level < 10) {
|
|
|
|
p->fb = 64;
|
|
|
|
|
2012-07-30 18:00:13 +00:00
|
|
|
} else if (*level == 11) {
|
2012-05-28 14:49:29 +00:00
|
|
|
p->fb = 64;
|
2012-07-09 17:58:11 +00:00
|
|
|
p->mc = 128;
|
|
|
|
|
2012-07-30 18:00:13 +00:00
|
|
|
} else if (*level == 12) {
|
|
|
|
p->fb = 128;
|
|
|
|
p->mc = 256;
|
|
|
|
|
|
|
|
} else if (*level == 13) {
|
|
|
|
p->fb = 64;
|
|
|
|
p->mc = 128;
|
|
|
|
p->dictSize = (1 << 27);
|
|
|
|
|
|
|
|
} else if (*level == 14) {
|
2012-05-28 14:49:29 +00:00
|
|
|
p->fb = 128;
|
2012-07-09 17:58:11 +00:00
|
|
|
p->mc = 256;
|
2012-07-30 18:00:13 +00:00
|
|
|
p->dictSize = (1 << 28);
|
2012-07-09 17:58:11 +00:00
|
|
|
}
|
2012-07-10 14:44:23 +00:00
|
|
|
if (*level > 9) *level = 9;
|
2012-05-28 14:49:29 +00:00
|
|
|
p->level = *level;
|
2012-08-18 04:50:52 +00:00
|
|
|
p->numThreads = nthreads;
|
2012-05-28 14:49:29 +00:00
|
|
|
LzmaEncProps_Normalize(p);
|
2012-05-31 07:36:40 +00:00
|
|
|
slab_cache_add(p->litprob_sz);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
if (*level > 9) *level = 9;
|
2012-05-28 14:49:29 +00:00
|
|
|
*data = p;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lzma_deinit(void **data)
|
|
|
|
{
|
|
|
|
if (p) {
|
|
|
|
slab_free(NULL, p);
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
*data = NULL;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-11-09 15:37:23 +00:00
|
|
|
lzerr(int err, int cmp)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
switch (err) {
|
|
|
|
case SZ_ERROR_MEM:
|
|
|
|
fprintf(stderr, "LZMA: Memory allocation error\n");
|
|
|
|
break;
|
|
|
|
case SZ_ERROR_PARAM:
|
|
|
|
fprintf(stderr, "LZMA: Incorrect paramater\n");
|
|
|
|
break;
|
|
|
|
case SZ_ERROR_WRITE:
|
|
|
|
fprintf(stderr, "LZMA: Write callback error\n");
|
|
|
|
break;
|
|
|
|
case SZ_ERROR_PROGRESS:
|
|
|
|
fprintf(stderr, "LZMA: Progress callback errored\n");
|
|
|
|
break;
|
2012-07-01 16:14:02 +00:00
|
|
|
case SZ_ERROR_INPUT_EOF:
|
|
|
|
fprintf(stderr, "LZMA: More compressed input bytes expected\n");
|
|
|
|
break;
|
2012-05-28 14:49:29 +00:00
|
|
|
case SZ_ERROR_OUTPUT_EOF:
|
2012-11-09 15:37:23 +00:00
|
|
|
/* This error is non-fatal during compression */
|
|
|
|
if (!cmp)
|
|
|
|
fprintf(stderr, "LZMA: Output buffer overflow\n");
|
2012-05-28 14:49:29 +00:00
|
|
|
break;
|
|
|
|
case SZ_ERROR_UNSUPPORTED:
|
|
|
|
fprintf(stderr, "LZMA: Unsupported properties\n");
|
|
|
|
break;
|
|
|
|
case SZ_ERROR_DESTLEN:
|
|
|
|
fprintf(stderr, "LZMA: Output chunk size too small\n");
|
|
|
|
break;
|
|
|
|
case SZ_ERROR_DATA:
|
|
|
|
fprintf(stderr, "LZMA: Data Error\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "LZMA: Unknown error code: %d\n", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LZMA compressed segment format(simplified)
|
|
|
|
* ------------------------------------------
|
|
|
|
* Offset Size Description
|
|
|
|
* 0 1 Special LZMA properties for compressed data
|
|
|
|
* 1 4 Dictionary size (little endian)
|
|
|
|
* 13 Compressed data
|
|
|
|
*
|
|
|
|
* Derived from http://docs.bugaco.com/7zip/lzma.txt
|
|
|
|
* We do not store the uncompressed chunk size here. It is stored in
|
|
|
|
* our chunk header.
|
|
|
|
*/
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
lzma_compress(void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *dstlen, int level, uchar_t chdr, void *data)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
2012-12-09 04:45:06 +00:00
|
|
|
uint64_t props_len = LZMA_PROPS_SIZE;
|
2012-05-28 14:49:29 +00:00
|
|
|
SRes res;
|
|
|
|
Byte *_dst;
|
|
|
|
CLzmaEncProps *props = (CLzmaEncProps *)data;
|
|
|
|
|
|
|
|
if (*dstlen < LZMA_PROPS_SIZE) {
|
2012-11-09 15:37:23 +00:00
|
|
|
lzerr(SZ_ERROR_DESTLEN, 1);
|
2012-05-28 14:49:29 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
props->level = level;
|
|
|
|
|
|
|
|
_dst = (Byte *)dst;
|
|
|
|
*dstlen -= LZMA_PROPS_SIZE;
|
|
|
|
res = LzmaEncode(_dst + LZMA_PROPS_SIZE, dstlen, src, srclen,
|
|
|
|
props, _dst, &props_len, 0, NULL, &g_Alloc, &g_Alloc);
|
|
|
|
|
|
|
|
if (res != 0) {
|
2012-11-09 15:37:23 +00:00
|
|
|
lzerr(res, 1);
|
2012-05-28 14:49:29 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
*dstlen += LZMA_PROPS_SIZE;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
lzma_decompress(void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *dstlen, int level, uchar_t chdr, void *data)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
2012-12-09 04:45:06 +00:00
|
|
|
uint64_t _srclen;
|
2012-05-28 14:49:29 +00:00
|
|
|
const uchar_t *_src;
|
|
|
|
SRes res;
|
|
|
|
ELzmaStatus status;
|
|
|
|
|
|
|
|
_srclen = srclen - LZMA_PROPS_SIZE;
|
2012-07-01 16:14:02 +00:00
|
|
|
_src = (uchar_t *)src + LZMA_PROPS_SIZE;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
if ((res = LzmaDecode((uchar_t *)dst, dstlen, _src, &_srclen,
|
2012-07-01 16:14:02 +00:00
|
|
|
src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY,
|
2012-05-28 14:49:29 +00:00
|
|
|
&status, &g_Alloc)) != SZ_OK) {
|
2012-11-09 15:37:23 +00:00
|
|
|
lzerr(res, 0);
|
2012-05-28 14:49:29 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|