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 <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <strings.h>
|
2012-05-31 12:31:34 +00:00
|
|
|
#if defined(sun) || defined(__sun)
|
|
|
|
#include <sys/byteorder.h>
|
|
|
|
#else
|
2012-05-28 14:49:29 +00:00
|
|
|
#include <byteswap.h>
|
2012-05-31 12:31:34 +00:00
|
|
|
#endif
|
2012-05-28 14:49:29 +00:00
|
|
|
#include <utils.h>
|
|
|
|
#include <pcompress.h>
|
|
|
|
#include <allocator.h>
|
|
|
|
|
2012-09-27 16:59:08 +00:00
|
|
|
#define FIFTY_PCT(x) (((x)/10) * 5)
|
2012-11-04 15:43:26 +00:00
|
|
|
#define FORTY_PCT(x) (((x)/10) * 4)
|
|
|
|
#define ONE_PCT(x) ((x)/100)
|
2012-09-27 16:59:08 +00:00
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
static unsigned int lzma_count = 0;
|
|
|
|
static unsigned int bzip2_count = 0;
|
2012-11-04 15:43:26 +00:00
|
|
|
static unsigned int bsc_count = 0;
|
2012-05-31 16:06:33 +00:00
|
|
|
static unsigned int ppmd_count = 0;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int lzma_compress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *destlen, int level, uchar_t chdr, void *data);
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int bzip2_compress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *destlen, int level, uchar_t chdr, void *data);
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int ppmd_compress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-11-04 15:43:26 +00:00
|
|
|
extern int libbsc_compress(void *src, size_t srclen, void *dst,
|
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
extern int lzma_decompress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int bzip2_decompress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int ppmd_decompress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-11-04 15:43:26 +00:00
|
|
|
extern int libbsc_decompress(void *src, size_t srclen, void *dst,
|
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data);
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
extern int lzma_init(void **data, int *level, int nthreads, ssize_t chunksize);
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int lzma_deinit(void **data);
|
2012-08-18 04:50:52 +00:00
|
|
|
extern int ppmd_init(void **data, int *level, int nthreads, ssize_t chunksize);
|
2012-05-28 14:49:29 +00:00
|
|
|
extern int ppmd_deinit(void **data);
|
2012-11-04 15:43:26 +00:00
|
|
|
extern int libbsc_init(void **data, int *level, int nthreads, ssize_t chunksize);
|
|
|
|
extern int libbsc_deinit(void **data);
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
struct adapt_data {
|
|
|
|
void *lzma_data;
|
|
|
|
void *ppmd_data;
|
2012-11-04 15:43:26 +00:00
|
|
|
void *bsc_data;
|
2012-05-28 14:49:29 +00:00
|
|
|
int adapt_mode;
|
|
|
|
};
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
void
|
|
|
|
adapt_stats(int show)
|
|
|
|
{
|
|
|
|
if (show) {
|
|
|
|
fprintf(stderr, "Adaptive mode stats:\n");
|
|
|
|
fprintf(stderr, " BZIP2 chunk count: %u\n", bzip2_count);
|
2012-11-04 15:43:26 +00:00
|
|
|
fprintf(stderr, " LIBBSC chunk count: %u\n", bsc_count);
|
2012-05-31 16:06:33 +00:00
|
|
|
fprintf(stderr, " PPMd chunk count: %u\n", ppmd_count);
|
|
|
|
fprintf(stderr, " LZMA chunk count: %u\n\n", lzma_count);
|
|
|
|
}
|
|
|
|
lzma_count = 0;
|
|
|
|
bzip2_count = 0;
|
2012-11-04 15:43:26 +00:00
|
|
|
bsc_count = 0;
|
2012-05-31 16:06:33 +00:00
|
|
|
ppmd_count = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
int
|
2012-08-18 04:50:52 +00:00
|
|
|
adapt_init(void **data, int *level, int nthreads, ssize_t chunksize)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
struct adapt_data *adat = (struct adapt_data *)(*data);
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (!adat) {
|
|
|
|
adat = (struct adapt_data *)slab_alloc(NULL, sizeof (struct adapt_data));
|
|
|
|
adat->adapt_mode = 1;
|
2012-08-18 04:50:52 +00:00
|
|
|
rv = ppmd_init(&(adat->ppmd_data), level, nthreads, chunksize);
|
2012-08-10 04:45:20 +00:00
|
|
|
adat->lzma_data = NULL;
|
2012-11-04 15:43:26 +00:00
|
|
|
adat->bsc_data = NULL;
|
2012-05-28 14:49:29 +00:00
|
|
|
*data = adat;
|
|
|
|
if (*level > 9) *level = 9;
|
|
|
|
}
|
2012-05-31 16:06:33 +00:00
|
|
|
lzma_count = 0;
|
|
|
|
bzip2_count = 0;
|
|
|
|
ppmd_count = 0;
|
2012-11-04 15:43:26 +00:00
|
|
|
bsc_count = 0;
|
2012-05-28 14:49:29 +00:00
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-18 04:50:52 +00:00
|
|
|
adapt2_init(void **data, int *level, int nthreads, ssize_t chunksize)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
struct adapt_data *adat = (struct adapt_data *)(*data);
|
|
|
|
int rv, lv;
|
|
|
|
|
|
|
|
if (!adat) {
|
|
|
|
adat = (struct adapt_data *)slab_alloc(NULL, sizeof (struct adapt_data));
|
|
|
|
adat->adapt_mode = 2;
|
|
|
|
adat->ppmd_data = NULL;
|
|
|
|
lv = *level;
|
2012-08-18 04:50:52 +00:00
|
|
|
rv = ppmd_init(&(adat->ppmd_data), &lv, nthreads, chunksize);
|
2012-05-28 14:49:29 +00:00
|
|
|
lv = *level;
|
|
|
|
if (rv == 0)
|
2012-08-18 04:50:52 +00:00
|
|
|
rv = lzma_init(&(adat->lzma_data), &lv, nthreads, chunksize);
|
2012-11-04 15:43:26 +00:00
|
|
|
lv = *level;
|
|
|
|
if (rv == 0)
|
|
|
|
rv = libbsc_init(&(adat->bsc_data), &lv, nthreads, chunksize);
|
2012-05-28 14:49:29 +00:00
|
|
|
*data = adat;
|
|
|
|
if (*level > 9) *level = 9;
|
|
|
|
}
|
2012-11-04 15:43:26 +00:00
|
|
|
lzma_count = 0;
|
|
|
|
bzip2_count = 0;
|
|
|
|
ppmd_count = 0;
|
|
|
|
bsc_count = 0;
|
2012-05-28 14:49:29 +00:00
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
adapt_deinit(void **data)
|
|
|
|
{
|
|
|
|
struct adapt_data *adat = (struct adapt_data *)(*data);
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (adat) {
|
2012-08-10 04:45:20 +00:00
|
|
|
rv = ppmd_deinit(&(adat->ppmd_data));
|
|
|
|
if (adat->lzma_data)
|
|
|
|
rv += lzma_deinit(&(adat->lzma_data));
|
2012-05-28 14:49:29 +00:00
|
|
|
slab_free(NULL, adat);
|
|
|
|
*data = NULL;
|
|
|
|
}
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
adapt_compress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
struct adapt_data *adat = (struct adapt_data *)(data);
|
2012-09-26 14:17:32 +00:00
|
|
|
uchar_t *src1 = (uchar_t *)src;
|
2012-11-04 15:43:26 +00:00
|
|
|
size_t i, tot8b, tagcnt;
|
|
|
|
int rv, tag;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-09-26 14:17:32 +00:00
|
|
|
/*
|
2012-11-04 15:43:26 +00:00
|
|
|
* Count number of 8-bit binary bytes and XML tags in source.
|
2012-09-26 14:17:32 +00:00
|
|
|
*/
|
2012-09-27 16:59:08 +00:00
|
|
|
tot8b = 0;
|
2012-11-04 15:43:26 +00:00
|
|
|
tagcnt = 0;
|
|
|
|
for (i = 0; i < srclen; i++) {
|
2012-09-27 16:59:08 +00:00
|
|
|
tot8b += (src1[i] >> 7);
|
2012-11-04 15:43:26 +00:00
|
|
|
tag = ((src1[i] == '<') | (src1[i] == '>'));
|
|
|
|
tagcnt += tag;
|
|
|
|
}
|
2012-09-26 14:17:32 +00:00
|
|
|
|
|
|
|
/*
|
2012-09-27 16:59:08 +00:00
|
|
|
* Use PPMd if some percentage of source is 7-bit textual bytes, otherwise
|
2012-09-26 14:17:32 +00:00
|
|
|
* use Bzip2 or LZMA.
|
|
|
|
*/
|
2012-11-04 15:43:26 +00:00
|
|
|
if (adat->adapt_mode == 2 && tot8b > FORTY_PCT(srclen)) {
|
2012-09-27 16:59:08 +00:00
|
|
|
rv = lzma_compress(src, srclen, dst, dstlen, level, chdr, adat->lzma_data);
|
|
|
|
if (rv < 0)
|
|
|
|
return (rv);
|
|
|
|
rv = COMPRESS_LZMA;
|
|
|
|
lzma_count++;
|
|
|
|
|
|
|
|
} else if (adat->adapt_mode == 1 && tot8b > FIFTY_PCT(srclen)) {
|
|
|
|
rv = bzip2_compress(src, srclen, dst, dstlen, level, chdr, NULL);
|
|
|
|
if (rv < 0)
|
|
|
|
return (rv);
|
|
|
|
rv = COMPRESS_BZIP2;
|
|
|
|
bzip2_count++;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
} else {
|
2012-11-04 16:16:04 +00:00
|
|
|
if (adat->bsc_data && tagcnt > ONE_PCT(srclen)) {
|
2012-11-04 15:43:26 +00:00
|
|
|
rv = libbsc_compress(src, srclen, dst, dstlen, level, chdr, adat->bsc_data);
|
|
|
|
if (rv < 0)
|
|
|
|
return (rv);
|
|
|
|
rv = COMPRESS_BSC;
|
|
|
|
bsc_count++;
|
|
|
|
} else {
|
|
|
|
rv = ppmd_compress(src, srclen, dst, dstlen, level, chdr, adat->ppmd_data);
|
|
|
|
if (rv < 0)
|
|
|
|
return (rv);
|
|
|
|
rv = COMPRESS_PPMD;
|
|
|
|
ppmd_count++;
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
adapt_decompress(void *src, size_t srclen, void *dst,
|
2012-07-01 16:14:02 +00:00
|
|
|
size_t *dstlen, int level, uchar_t chdr, void *data)
|
2012-05-28 14:49:29 +00:00
|
|
|
{
|
|
|
|
struct adapt_data *adat = (struct adapt_data *)(data);
|
2012-08-10 05:17:11 +00:00
|
|
|
uchar_t cmp_flags;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-10 05:17:11 +00:00
|
|
|
cmp_flags = (chdr>>4) & CHDR_ALGO_MASK;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-10 05:17:11 +00:00
|
|
|
if (cmp_flags == COMPRESS_LZMA) {
|
2012-07-01 16:14:02 +00:00
|
|
|
return (lzma_decompress(src, srclen, dst, dstlen, level, chdr, adat->lzma_data));
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-10 05:17:11 +00:00
|
|
|
} else if (cmp_flags == COMPRESS_BZIP2) {
|
2012-07-01 16:14:02 +00:00
|
|
|
return (bzip2_decompress(src, srclen, dst, dstlen, level, chdr, NULL));
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-10 05:17:11 +00:00
|
|
|
} else if (cmp_flags == COMPRESS_PPMD) {
|
2012-07-01 16:14:02 +00:00
|
|
|
return (ppmd_decompress(src, srclen, dst, dstlen, level, chdr, adat->ppmd_data));
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-11-04 15:43:26 +00:00
|
|
|
} else if (cmp_flags == COMPRESS_BSC) {
|
|
|
|
return (libbsc_decompress(src, srclen, dst, dstlen, level, chdr, adat->bsc_data));
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
} else {
|
2012-11-04 16:16:04 +00:00
|
|
|
fprintf(stderr, "Unrecognized compression mode: %d, file corrupt.\n", cmp_flags);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|