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/LGPL source
|
|
|
|
* code from the LZMA SDK: http://www.7-zip.org/sdk.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pcompress - Do a chunked parallel compression/decompression of a file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.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 <libgen.h>
|
|
|
|
#include <utils.h>
|
|
|
|
#include <pcompress.h>
|
|
|
|
#include <allocator.h>
|
2012-09-02 15:10:32 +00:00
|
|
|
#include <rabin_dedup.h>
|
2012-08-23 17:28:44 +00:00
|
|
|
#include <lzp.h>
|
2012-12-12 18:30:47 +00:00
|
|
|
#include <transpose.h>
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use 5MB chunks by default.
|
|
|
|
*/
|
|
|
|
#define DEFAULT_CHUNKSIZE (5 * 1024 * 1024)
|
2012-10-17 18:02:35 +00:00
|
|
|
#define EIGHTY_PCT(x) ((x) - ((x)/5))
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
struct wdata {
|
|
|
|
struct cmp_data **dary;
|
|
|
|
int wfd;
|
|
|
|
int nprocs;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t chunksize;
|
2012-05-28 14:49:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void * writer_thread(void *dat);
|
|
|
|
static int init_algo(const char *algo, int bail);
|
2012-12-09 04:45:06 +00:00
|
|
|
extern uint32_t lzma_crc32(const uint8_t *buf, uint64_t size, uint32_t crc);
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
static compress_func_ptr _compress_func;
|
|
|
|
static compress_func_ptr _decompress_func;
|
|
|
|
static init_func_ptr _init_func;
|
|
|
|
static deinit_func_ptr _deinit_func;
|
2012-05-31 16:06:33 +00:00
|
|
|
static stats_func_ptr _stats_func;
|
2012-08-18 04:50:52 +00:00
|
|
|
static props_func_ptr _props_func;
|
2012-05-31 16:06:33 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
static int main_cancel;
|
|
|
|
static int adapt_mode = 0;
|
|
|
|
static int pipe_mode = 0;
|
|
|
|
static int nthreads = 0;
|
2012-05-31 16:06:33 +00:00
|
|
|
static int hide_mem_stats = 1;
|
|
|
|
static int hide_cmp_stats = 1;
|
2012-06-21 14:57:05 +00:00
|
|
|
static int enable_rabin_scan = 0;
|
2012-07-19 16:11:07 +00:00
|
|
|
static int enable_delta_encode = 0;
|
2012-12-04 18:39:47 +00:00
|
|
|
static int enable_delta2_encode = 0;
|
2012-07-08 16:14:08 +00:00
|
|
|
static int enable_rabin_split = 1;
|
2012-09-15 05:44:58 +00:00
|
|
|
static int enable_fixed_scan = 0;
|
2012-08-23 17:28:44 +00:00
|
|
|
static int lzp_preprocess = 0;
|
2012-10-15 06:40:00 +00:00
|
|
|
static int encrypt_type = 0;
|
2012-05-28 14:49:29 +00:00
|
|
|
static unsigned int chunk_num;
|
2012-05-31 16:06:33 +00:00
|
|
|
static uint64_t largest_chunk, smallest_chunk, avg_chunk;
|
2012-05-28 14:49:29 +00:00
|
|
|
static const char *exec_name;
|
|
|
|
static const char *algo = NULL;
|
2012-05-31 16:06:33 +00:00
|
|
|
static int do_compress = 0;
|
|
|
|
static int do_uncompress = 0;
|
2012-10-17 18:02:35 +00:00
|
|
|
static int cksum_bytes, mac_bytes;
|
2012-11-25 09:42:45 +00:00
|
|
|
static int cksum = 0, t_errored = 0;
|
2012-09-05 17:13:54 +00:00
|
|
|
static int rab_blk_size = 0;
|
2012-09-16 05:42:58 +00:00
|
|
|
static dedupe_context_t *rctx;
|
2012-10-15 06:40:00 +00:00
|
|
|
static crypto_ctx_t crypto_ctx;
|
|
|
|
static char *pwd_file = NULL;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2012-08-31 17:06:06 +00:00
|
|
|
"\nPcompress Version %s\n\n"
|
2012-05-28 14:49:29 +00:00
|
|
|
"Usage:\n"
|
|
|
|
"1) To compress a file:\n"
|
|
|
|
" %s -c <algorithm> [-l <compress level>] [-s <chunk size>] <file>\n"
|
|
|
|
" Where <algorithm> can be the folowing:\n"
|
2012-07-25 15:42:30 +00:00
|
|
|
" lzfx - Very fast and small algorithm based on LZF.\n"
|
|
|
|
" lz4 - Ultra fast, high-throughput algorithm reaching RAM B/W at level1.\n"
|
2012-05-28 14:49:29 +00:00
|
|
|
" zlib - The base Zlib format compression (not Gzip).\n"
|
|
|
|
" lzma - The LZMA (Lempel-Ziv Markov) algorithm from 7Zip.\n"
|
2012-08-18 16:30:14 +00:00
|
|
|
" lzmaMt - Multithreaded version of LZMA. This is a faster version but\n"
|
|
|
|
" uses more memory for the dictionary. Thread count is balanced\n"
|
|
|
|
" between chunk processing threads and algorithm threads.\n"
|
2012-05-28 14:49:29 +00:00
|
|
|
" bzip2 - Bzip2 Algorithm from libbzip2.\n"
|
|
|
|
" ppmd - The PPMd algorithm excellent for textual data. PPMd requires\n"
|
|
|
|
" at least 64MB X CPUs more memory than the other modes.\n"
|
2012-08-27 16:21:55 +00:00
|
|
|
#ifdef ENABLE_PC_LIBBSC
|
|
|
|
" libbsc - A Block Sorting Compressor using the Burrows Wheeler Transform\n"
|
|
|
|
" like Bzip2 but runs faster and gives better compression than\n"
|
|
|
|
" Bzip2 (See: libbsc.com).\n"
|
|
|
|
#endif
|
2012-06-01 16:34:08 +00:00
|
|
|
" adapt - Adaptive mode where ppmd or bzip2 will be used per chunk,\n"
|
2012-05-28 14:49:29 +00:00
|
|
|
" depending on which one produces better compression. This mode\n"
|
2012-06-01 16:34:08 +00:00
|
|
|
" is obviously fairly slow and requires lots of memory.\n"
|
|
|
|
" adapt2 - Adaptive mode which includes ppmd and lzma. This requires\n"
|
|
|
|
" more memory than adapt mode, is slower and potentially gives\n"
|
|
|
|
" the best compression.\n"
|
2012-08-18 16:30:14 +00:00
|
|
|
" none - No compression. This is only meaningful with -D and -E so Dedupe\n"
|
|
|
|
" can be done for post-processing with an external utility.\n"
|
2012-05-28 14:49:29 +00:00
|
|
|
" <chunk_size> - This can be in bytes or can use the following suffixes:\n"
|
|
|
|
" g - Gigabyte, m - Megabyte, k - Kilobyte.\n"
|
|
|
|
" Larger chunks produce better compression at the cost of memory.\n"
|
|
|
|
" <compress_level> - Can be a number from 0 meaning minimum and 14 meaning\n"
|
|
|
|
" maximum compression.\n\n"
|
|
|
|
"2) To decompress a file compressed using above command:\n"
|
|
|
|
" %s -d <compressed file> <target file>\n"
|
|
|
|
"3) To operate as a pipe, read from stdin and write to stdout:\n"
|
2012-06-21 14:57:05 +00:00
|
|
|
" %s -p ...\n"
|
2012-06-29 18:15:06 +00:00
|
|
|
"4) Attempt Rabin fingerprinting based deduplication on chunks:\n"
|
|
|
|
" %s -D ...\n"
|
2012-09-24 16:50:27 +00:00
|
|
|
" %s -D -r ... - Do NOT split chunks at a rabin boundary. Default is to split.\n\n"
|
|
|
|
"5) Perform Delta Encoding in addition to Identical Dedup:\n"
|
|
|
|
" %s -E ... - This also implies '-D'. This checks for at least 60%% similarity.\n"
|
|
|
|
" The flag can be repeated as in '-EE' to indicate at least 40%% similarity.\n\n"
|
2012-07-19 16:11:07 +00:00
|
|
|
"6) Number of threads can optionally be specified: -t <1 - 256 count>\n"
|
2012-08-23 17:28:44 +00:00
|
|
|
"7) Other flags:\n"
|
2012-09-05 17:13:54 +00:00
|
|
|
" '-L' - Enable LZP pre-compression. This improves compression ratio of all\n"
|
|
|
|
" algorithms with some extra CPU and very low RAM overhead.\n"
|
2012-12-04 18:39:47 +00:00
|
|
|
" '-P' - Enable Adaptive Delta Encoding. This implies '-L' as well. It improves\n"
|
|
|
|
" compresion ratio further at the cost of more CPU overhead.\n"
|
2012-09-05 17:13:54 +00:00
|
|
|
" '-S' <cksum>\n"
|
2012-10-03 17:13:02 +00:00
|
|
|
" - Specify chunk checksum to use: CRC64, SKEIN256, SKEIN512, SHA256 and\n"
|
|
|
|
" SHA512. Default one is SKEIN256.\n"
|
2012-09-16 05:42:58 +00:00
|
|
|
" '-F' - Perform Fixed-Block Deduplication. Faster than '-D' in some cases\n"
|
|
|
|
" but with lower deduplication ratio.\n"
|
2012-09-05 17:13:54 +00:00
|
|
|
" '-B' <1..5>\n"
|
2012-09-19 14:59:44 +00:00
|
|
|
" - Specify an average Dedupe block size. 1 - 4K, 2 - 8K ... 5 - 64K.\n"
|
2012-09-05 17:13:54 +00:00
|
|
|
" '-M' - Display memory allocator statistics\n"
|
|
|
|
" '-C' - Display compression statistics\n\n",
|
2012-07-27 16:37:56 +00:00
|
|
|
UTILITY_VERSION, exec_name, exec_name, exec_name, exec_name, exec_name, exec_name);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
void
|
|
|
|
show_compression_stats(uint64_t chunksize)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "\nCompression Statistics\n");
|
|
|
|
fprintf(stderr, "======================\n");
|
|
|
|
fprintf(stderr, "Total chunks : %u\n", chunk_num);
|
|
|
|
fprintf(stderr, "Best compressed chunk : %s(%.2f%%)\n",
|
|
|
|
bytes_to_size(smallest_chunk), (double)smallest_chunk/(double)chunksize*100);
|
|
|
|
fprintf(stderr, "Worst compressed chunk : %s(%.2f%%)\n",
|
|
|
|
bytes_to_size(largest_chunk), (double)largest_chunk/(double)chunksize*100);
|
|
|
|
avg_chunk /= chunk_num;
|
|
|
|
fprintf(stderr, "Avg compressed chunk : %s(%.2f%%)\n\n",
|
|
|
|
bytes_to_size(avg_chunk), (double)avg_chunk/(double)chunksize*100);
|
|
|
|
}
|
|
|
|
|
2012-08-23 17:28:44 +00:00
|
|
|
/*
|
|
|
|
* Wrapper functions to pre-process the buffer and then call the main compression routine.
|
|
|
|
* At present only LZP pre-compression is used below. Some extra metadata is added:
|
|
|
|
*
|
|
|
|
* Byte 0: A flag to indicate which pre-processor was used.
|
|
|
|
* Byte 1 - Byte 8: Size of buffer after pre-processing
|
|
|
|
*
|
|
|
|
* It is possible for a buffer to be only pre-processed and not compressed by the final
|
|
|
|
* algorithm if the final one fails to compress for some reason. However the vice versa
|
|
|
|
* is not allowed.
|
|
|
|
*/
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
preproc_compress(compress_func_ptr cmp_func, void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *dstlen, int level, uchar_t chdr, void *data, algo_props_t *props)
|
2012-08-23 17:28:44 +00:00
|
|
|
{
|
|
|
|
uchar_t *dest = (uchar_t *)dst, type = 0;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t result, _dstlen;
|
2012-08-23 17:28:44 +00:00
|
|
|
|
2012-12-14 13:42:48 +00:00
|
|
|
_dstlen = *dstlen;
|
2012-08-23 17:28:44 +00:00
|
|
|
if (lzp_preprocess) {
|
|
|
|
int hashsize;
|
|
|
|
|
|
|
|
type = PREPROC_TYPE_LZP;
|
|
|
|
hashsize = lzp_hash_size(level);
|
|
|
|
result = lzp_compress(src, dst, srclen, hashsize, LZP_DEFAULT_LZPMINLEN, 0);
|
|
|
|
if (result < 0 || result == srclen) return (-1);
|
|
|
|
srclen = result;
|
|
|
|
memcpy(src, dst, srclen);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Execution won't come here but just in case ...
|
2012-12-11 06:08:42 +00:00
|
|
|
* Even Delta2 encoding below enables LZP.
|
2012-08-23 17:28:44 +00:00
|
|
|
*/
|
|
|
|
fprintf(stderr, "Invalid preprocessing mode\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2012-12-13 15:48:16 +00:00
|
|
|
if (enable_delta2_encode && props->delta2_span > 0) {
|
2012-12-04 18:39:47 +00:00
|
|
|
_dstlen = srclen;
|
2012-12-13 15:48:16 +00:00
|
|
|
result = delta2_encode(src, srclen, dst, &_dstlen, props->delta2_span);
|
2012-12-04 18:39:47 +00:00
|
|
|
if (result != -1) {
|
|
|
|
memcpy(src, dst, _dstlen);
|
|
|
|
srclen = _dstlen;
|
|
|
|
type |= PREPROC_TYPE_DELTA2;
|
|
|
|
}
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 17:28:44 +00:00
|
|
|
*dest = type;
|
|
|
|
*((int64_t *)(dest + 1)) = htonll(srclen);
|
|
|
|
_dstlen = srclen;
|
|
|
|
result = cmp_func(src, srclen, dest+9, &_dstlen, level, chdr, data);
|
2012-09-07 14:01:35 +00:00
|
|
|
if (result > -1 && _dstlen < srclen) {
|
2012-08-23 17:28:44 +00:00
|
|
|
*dest |= PREPROC_COMPRESSED;
|
|
|
|
*dstlen = _dstlen + 9;
|
|
|
|
} else {
|
2012-12-04 18:39:47 +00:00
|
|
|
memcpy(dest+1, src, srclen);
|
|
|
|
*dstlen = srclen + 1;
|
|
|
|
result = 0;
|
2012-08-23 17:28:44 +00:00
|
|
|
}
|
2012-12-04 18:39:47 +00:00
|
|
|
|
2012-09-07 14:01:35 +00:00
|
|
|
return (result);
|
2012-08-23 17:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-12-09 04:45:06 +00:00
|
|
|
preproc_decompress(compress_func_ptr dec_func, void *src, uint64_t srclen, void *dst,
|
|
|
|
uint64_t *dstlen, int level, uchar_t chdr, void *data, algo_props_t *props)
|
2012-08-23 17:28:44 +00:00
|
|
|
{
|
|
|
|
uchar_t *sorc = (uchar_t *)src, type;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t result;
|
2012-12-03 18:15:41 +00:00
|
|
|
uint64_t _dstlen = *dstlen;
|
2012-08-23 17:28:44 +00:00
|
|
|
|
|
|
|
type = *sorc;
|
|
|
|
sorc++;
|
|
|
|
srclen--;
|
|
|
|
if (type & PREPROC_COMPRESSED) {
|
|
|
|
*dstlen = ntohll(*((int64_t *)(sorc)));
|
|
|
|
sorc += 8;
|
|
|
|
srclen -= 8;
|
|
|
|
result = dec_func(sorc, srclen, dst, dstlen, level, chdr, data);
|
|
|
|
if (result < 0) return (result);
|
|
|
|
memcpy(src, dst, *dstlen);
|
|
|
|
srclen = *dstlen;
|
|
|
|
}
|
|
|
|
|
2012-12-04 18:39:47 +00:00
|
|
|
if (type & PREPROC_TYPE_DELTA2) {
|
|
|
|
result = delta2_decode(src, srclen, dst, &_dstlen);
|
|
|
|
if (result != -1) {
|
|
|
|
memcpy(src, dst, _dstlen);
|
|
|
|
srclen = _dstlen;
|
|
|
|
} else {
|
|
|
|
return (result);
|
|
|
|
}
|
2012-12-03 18:15:41 +00:00
|
|
|
}
|
|
|
|
|
2012-08-23 17:28:44 +00:00
|
|
|
if (type & PREPROC_TYPE_LZP) {
|
|
|
|
int hashsize;
|
|
|
|
hashsize = lzp_hash_size(level);
|
|
|
|
result = lzp_decompress(src, dst, srclen, hashsize, LZP_DEFAULT_LZPMINLEN, 0);
|
|
|
|
if (result < 0) {
|
|
|
|
fprintf(stderr, "LZP decompression failed.\n");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
*dstlen = result;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Invalid preprocessing flags: %d\n", type);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* This routine is called in multiple threads. Calls the decompression handler
|
|
|
|
* as encoded in the file header. For adaptive mode the handler adapt_decompress()
|
2012-08-03 17:49:38 +00:00
|
|
|
* in turns looks at the chunk header and calls the actual decompression
|
2012-05-28 14:49:29 +00:00
|
|
|
* routine.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
perform_decompress(void *dat)
|
|
|
|
{
|
|
|
|
struct cmp_data *tdat = (struct cmp_data *)dat;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t _chunksize;
|
|
|
|
int64_t dedupe_index_sz, dedupe_data_sz, dedupe_index_sz_cmp, dedupe_data_sz_cmp;
|
2012-05-28 14:49:29 +00:00
|
|
|
int type, rv;
|
2012-07-01 16:14:02 +00:00
|
|
|
unsigned int blknum;
|
2012-08-31 17:06:06 +00:00
|
|
|
uchar_t checksum[CKSUM_MAX_BYTES];
|
2012-05-28 14:49:29 +00:00
|
|
|
uchar_t HDR;
|
|
|
|
uchar_t *cseg;
|
|
|
|
|
|
|
|
redo:
|
|
|
|
sem_wait(&tdat->start_sem);
|
|
|
|
if (unlikely(tdat->cancel)) {
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the last read returned a 0 quit.
|
|
|
|
*/
|
|
|
|
if (tdat->rbytes == 0) {
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
goto cont;
|
|
|
|
}
|
|
|
|
|
2012-10-19 16:21:27 +00:00
|
|
|
cseg = tdat->compressed_chunk + cksum_bytes + mac_bytes;
|
|
|
|
HDR = *cseg;
|
|
|
|
cseg += CHUNK_FLAG_SZ;
|
|
|
|
_chunksize = tdat->chunksize;
|
|
|
|
if (HDR & CHSIZE_MASK) {
|
|
|
|
uchar_t *rseg;
|
|
|
|
|
|
|
|
tdat->rbytes -= ORIGINAL_CHUNKSZ;
|
|
|
|
tdat->len_cmp -= ORIGINAL_CHUNKSZ;
|
|
|
|
rseg = tdat->compressed_chunk + tdat->rbytes;
|
2012-12-09 04:45:06 +00:00
|
|
|
_chunksize = ntohll(*((int64_t *)rseg));
|
2012-10-19 16:21:27 +00:00
|
|
|
}
|
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
/*
|
2012-10-19 16:21:27 +00:00
|
|
|
* If this was encrypted:
|
|
|
|
* Verify HMAC first before anything else and then decrypt compressed data.
|
2012-10-18 17:25:41 +00:00
|
|
|
*/
|
|
|
|
if (encrypt_type) {
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
len = mac_bytes;
|
|
|
|
deserialize_checksum(checksum, tdat->compressed_chunk + cksum_bytes, mac_bytes);
|
|
|
|
memset(tdat->compressed_chunk + cksum_bytes, 0, mac_bytes);
|
|
|
|
hmac_reinit(&tdat->chunk_hmac);
|
|
|
|
hmac_update(&tdat->chunk_hmac, (uchar_t *)&tdat->len_cmp_be, sizeof (tdat->len_cmp_be));
|
2012-11-23 16:57:14 +00:00
|
|
|
hmac_update(&tdat->chunk_hmac, tdat->compressed_chunk, tdat->rbytes);
|
2012-10-19 16:21:27 +00:00
|
|
|
if (HDR & CHSIZE_MASK) {
|
|
|
|
uchar_t *rseg;
|
|
|
|
rseg = tdat->compressed_chunk + tdat->rbytes;
|
|
|
|
hmac_update(&tdat->chunk_hmac, rseg, ORIGINAL_CHUNKSZ);
|
|
|
|
}
|
2012-10-18 17:25:41 +00:00
|
|
|
hmac_final(&tdat->chunk_hmac, tdat->checksum, &len);
|
|
|
|
if (memcmp(checksum, tdat->checksum, len) != 0) {
|
|
|
|
/*
|
|
|
|
* HMAC verification failure is fatal.
|
|
|
|
*/
|
|
|
|
fprintf(stderr, "Chunk %d, HMAC verification failed\n", tdat->id);
|
|
|
|
main_cancel = 1;
|
|
|
|
tdat->len_cmp = 0;
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-10-18 17:25:41 +00:00
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
/*
|
|
|
|
* Encryption algorithm should not change the size and
|
|
|
|
* encryption is in-place.
|
|
|
|
*/
|
|
|
|
rv = crypto_buf(&crypto_ctx, cseg, cseg, tdat->len_cmp, tdat->id);
|
|
|
|
if (rv == -1) {
|
|
|
|
/*
|
|
|
|
* Decryption failure is fatal.
|
|
|
|
*/
|
|
|
|
main_cancel = 1;
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
return;
|
|
|
|
}
|
2012-11-23 18:30:05 +00:00
|
|
|
} else if (mac_bytes > 0) {
|
2012-11-22 18:30:39 +00:00
|
|
|
/*
|
|
|
|
* Verify header CRC32 in non-crypto mode.
|
|
|
|
*/
|
|
|
|
uint32_t crc1, crc2;
|
|
|
|
|
|
|
|
crc1 = htonl(*((uint32_t *)(tdat->compressed_chunk + cksum_bytes)));
|
|
|
|
memset(tdat->compressed_chunk + cksum_bytes, 0, mac_bytes);
|
|
|
|
crc2 = lzma_crc32((uchar_t *)&tdat->len_cmp_be, sizeof (tdat->len_cmp_be), 0);
|
|
|
|
crc2 = lzma_crc32(tdat->compressed_chunk,
|
|
|
|
cksum_bytes + mac_bytes + CHUNK_FLAG_SZ, crc2);
|
|
|
|
if (HDR & CHSIZE_MASK) {
|
|
|
|
uchar_t *rseg;
|
|
|
|
rseg = tdat->compressed_chunk + tdat->rbytes;
|
|
|
|
crc2 = lzma_crc32(rseg, ORIGINAL_CHUNKSZ, crc2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crc1 != crc2) {
|
|
|
|
/*
|
|
|
|
* Header CRC32 verification failure is fatal.
|
|
|
|
*/
|
|
|
|
fprintf(stderr, "Chunk %d, Header CRC verification failed\n", tdat->id);
|
|
|
|
main_cancel = 1;
|
|
|
|
tdat->len_cmp = 0;
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-11-22 18:30:39 +00:00
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
return;
|
|
|
|
}
|
2012-10-15 06:40:00 +00:00
|
|
|
|
2012-11-23 16:57:14 +00:00
|
|
|
/*
|
|
|
|
* Now that header CRC32 was verified, recover the stored message
|
|
|
|
* digest.
|
|
|
|
*/
|
|
|
|
deserialize_checksum(tdat->checksum, tdat->compressed_chunk, cksum_bytes);
|
|
|
|
}
|
2012-10-19 16:21:27 +00:00
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan) && (HDR & CHUNK_FLAG_DEDUP)) {
|
2012-08-05 17:05:51 +00:00
|
|
|
uchar_t *cmpbuf, *ubuf;
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-09-27 16:59:08 +00:00
|
|
|
/* Extract various sizes from dedupe header. */
|
|
|
|
parse_dedupe_hdr(cseg, &blknum, &dedupe_index_sz, &dedupe_data_sz,
|
|
|
|
&dedupe_index_sz_cmp, &dedupe_data_sz_cmp, &_chunksize);
|
2012-08-05 17:05:51 +00:00
|
|
|
memcpy(tdat->uncompressed_chunk, cseg, RABIN_HDR_SIZE);
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-08-05 17:05:51 +00:00
|
|
|
/*
|
|
|
|
* Uncompress the data chunk first and then uncompress the index.
|
|
|
|
* The uncompress routines can use extra bytes at the end for temporary
|
|
|
|
* state/dictionary info. Since data chunk directly follows index
|
|
|
|
* uncompressing index first corrupts the data.
|
|
|
|
*/
|
2012-09-15 05:44:58 +00:00
|
|
|
cmpbuf = cseg + RABIN_HDR_SIZE + dedupe_index_sz_cmp;
|
|
|
|
ubuf = tdat->uncompressed_chunk + RABIN_HDR_SIZE + dedupe_index_sz;
|
2012-08-05 17:05:51 +00:00
|
|
|
if (HDR & COMPRESSED) {
|
2012-08-23 17:28:44 +00:00
|
|
|
if (HDR & CHUNK_FLAG_PREPROC) {
|
2012-09-27 16:59:08 +00:00
|
|
|
rv = preproc_decompress(tdat->decompress, cmpbuf, dedupe_data_sz_cmp,
|
2012-12-04 18:39:47 +00:00
|
|
|
ubuf, &_chunksize, tdat->level, HDR, tdat->data, tdat->props);
|
2012-08-23 17:28:44 +00:00
|
|
|
} else {
|
2012-09-27 16:59:08 +00:00
|
|
|
rv = tdat->decompress(cmpbuf, dedupe_data_sz_cmp, ubuf, &_chunksize,
|
2012-08-23 17:28:44 +00:00
|
|
|
tdat->level, HDR, tdat->data);
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
if (rv == -1) {
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
fprintf(stderr, "ERROR: Chunk %d, decompression failed.\n", tdat->id);
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-07-01 16:14:02 +00:00
|
|
|
goto cont;
|
|
|
|
}
|
2012-08-05 17:05:51 +00:00
|
|
|
} else {
|
|
|
|
memcpy(ubuf, cmpbuf, _chunksize);
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-08-05 17:05:51 +00:00
|
|
|
rv = 0;
|
|
|
|
cmpbuf = cseg + RABIN_HDR_SIZE;
|
|
|
|
ubuf = tdat->uncompressed_chunk + RABIN_HDR_SIZE;
|
2012-12-12 18:30:47 +00:00
|
|
|
|
|
|
|
if (dedupe_index_sz >= 90 && dedupe_index_sz > dedupe_index_sz_cmp) {
|
2012-08-05 17:05:51 +00:00
|
|
|
/* Index should be at least 90 bytes to have been compressed. */
|
2012-09-15 05:44:58 +00:00
|
|
|
rv = lzma_decompress(cmpbuf, dedupe_index_sz_cmp, ubuf,
|
|
|
|
&dedupe_index_sz, tdat->rctx->level, 0, tdat->rctx->lzma_data);
|
2012-07-01 16:14:02 +00:00
|
|
|
} else {
|
2012-09-15 05:44:58 +00:00
|
|
|
memcpy(ubuf, cmpbuf, dedupe_index_sz);
|
2012-07-01 16:14:02 +00:00
|
|
|
}
|
2012-12-12 18:30:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Recover from transposed index.
|
|
|
|
*/
|
|
|
|
transpose(ubuf, cmpbuf, dedupe_index_sz, sizeof (uint32_t), COL);
|
|
|
|
memcpy(ubuf, cmpbuf, dedupe_index_sz);
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
} else {
|
2012-08-05 17:05:51 +00:00
|
|
|
if (HDR & COMPRESSED) {
|
2012-08-23 17:28:44 +00:00
|
|
|
if (HDR & CHUNK_FLAG_PREPROC) {
|
|
|
|
rv = preproc_decompress(tdat->decompress, cseg, tdat->len_cmp,
|
2012-12-04 18:39:47 +00:00
|
|
|
tdat->uncompressed_chunk, &_chunksize, tdat->level, HDR, tdat->data,
|
|
|
|
tdat->props);
|
2012-08-23 17:28:44 +00:00
|
|
|
} else {
|
|
|
|
rv = tdat->decompress(cseg, tdat->len_cmp, tdat->uncompressed_chunk,
|
|
|
|
&_chunksize, tdat->level, HDR, tdat->data);
|
|
|
|
}
|
2012-08-05 17:05:51 +00:00
|
|
|
} else {
|
|
|
|
memcpy(tdat->uncompressed_chunk, cseg, _chunksize);
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
tdat->len_cmp = _chunksize;
|
|
|
|
|
2012-07-01 16:14:02 +00:00
|
|
|
if (rv == -1) {
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
fprintf(stderr, "ERROR: Chunk %d, decompression failed.\n", tdat->id);
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-07-01 16:14:02 +00:00
|
|
|
goto cont;
|
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
/* Rebuild chunk from dedup blocks. */
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan) && (HDR & CHUNK_FLAG_DEDUP)) {
|
|
|
|
dedupe_context_t *rctx;
|
2012-06-29 12:53:55 +00:00
|
|
|
uchar_t *tmp;
|
|
|
|
|
|
|
|
rctx = tdat->rctx;
|
2012-09-16 05:42:58 +00:00
|
|
|
reset_dedupe_context(tdat->rctx);
|
2012-06-29 12:53:55 +00:00
|
|
|
rctx->cbuf = tdat->compressed_chunk;
|
2012-09-16 05:42:58 +00:00
|
|
|
dedupe_decompress(rctx, tdat->uncompressed_chunk, &(tdat->len_cmp));
|
2012-06-29 12:53:55 +00:00
|
|
|
if (!rctx->valid) {
|
|
|
|
fprintf(stderr, "ERROR: Chunk %d, dedup recovery failed.\n", tdat->id);
|
|
|
|
rv = -1;
|
|
|
|
tdat->len_cmp = 0;
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-06-29 12:53:55 +00:00
|
|
|
goto cont;
|
|
|
|
}
|
|
|
|
_chunksize = tdat->len_cmp;
|
|
|
|
tmp = tdat->uncompressed_chunk;
|
|
|
|
tdat->uncompressed_chunk = tdat->compressed_chunk;
|
|
|
|
tdat->compressed_chunk = tmp;
|
|
|
|
tdat->cmp_seg = tdat->uncompressed_chunk;
|
|
|
|
}
|
|
|
|
|
2012-11-23 16:57:14 +00:00
|
|
|
if (!encrypt_type) {
|
|
|
|
/*
|
|
|
|
* Re-compute checksum of original uncompressed chunk.
|
|
|
|
* If it does not match we set length of chunk to 0 to indicate
|
|
|
|
* exit to the writer thread.
|
|
|
|
*/
|
|
|
|
compute_checksum(checksum, cksum, tdat->uncompressed_chunk, _chunksize);
|
|
|
|
if (memcmp(checksum, tdat->checksum, cksum_bytes) != 0) {
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
fprintf(stderr, "ERROR: Chunk %d, checksums do not match.\n", tdat->id);
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-11-23 16:57:14 +00:00
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cont:
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File decompression routine.
|
|
|
|
*
|
|
|
|
* Compressed file Format
|
|
|
|
* ----------------------
|
|
|
|
* File Header:
|
|
|
|
* Algorithm string: 8 bytes.
|
2012-06-29 12:53:55 +00:00
|
|
|
* Version number: 2 bytes.
|
|
|
|
* Global Flags: 2 bytes.
|
2012-05-28 14:49:29 +00:00
|
|
|
* Chunk size: 8 bytes.
|
2012-06-29 12:53:55 +00:00
|
|
|
* Compression Level: 4 bytes.
|
2012-05-28 14:49:29 +00:00
|
|
|
*
|
|
|
|
* Chunk Header:
|
|
|
|
* Compressed length: 8 bytes.
|
2012-08-31 17:06:06 +00:00
|
|
|
* Checksum: Upto 64 bytes.
|
2012-05-28 14:49:29 +00:00
|
|
|
* Chunk flags: 1 byte.
|
|
|
|
*
|
|
|
|
* Chunk Flags, 8 bits:
|
|
|
|
* I I I I I I I I
|
2012-12-11 06:08:42 +00:00
|
|
|
* | | | | | |
|
|
|
|
* | '-----' | | `- 0 - Uncompressed
|
|
|
|
* | | | | 1 - Compressed
|
|
|
|
* | | | |
|
|
|
|
* | | | `---- 1 - Chunk was Deduped
|
|
|
|
* | | `------- 1 - Chunk was pre-compressed
|
2012-06-29 12:53:55 +00:00
|
|
|
* | |
|
|
|
|
* | | 1 - Bzip2 (Adaptive Mode)
|
|
|
|
* | `---------------- 2 - Lzma (Adaptive Mode)
|
|
|
|
* | 3 - PPMD (Adaptive Mode)
|
2012-05-28 14:49:29 +00:00
|
|
|
* |
|
2012-08-23 17:28:44 +00:00
|
|
|
* `---------------------- 1 - Chunk size flag (if original chunk is of variable length)
|
2012-05-28 14:49:29 +00:00
|
|
|
*
|
|
|
|
* A file trailer to indicate end.
|
|
|
|
* Zero Compressed length: 8 zero bytes.
|
|
|
|
*/
|
|
|
|
#define UNCOMP_BAIL err = 1; goto uncomp_done
|
|
|
|
|
2012-11-24 18:23:07 +00:00
|
|
|
static int
|
2012-05-28 14:49:29 +00:00
|
|
|
start_decompress(const char *filename, const char *to_filename)
|
|
|
|
{
|
|
|
|
char tmpfile[MAXPATHLEN];
|
|
|
|
char algorithm[ALGO_SZ];
|
|
|
|
struct stat sbuf;
|
|
|
|
struct wdata w;
|
|
|
|
int compfd = -1, i, p;
|
|
|
|
int uncompfd = -1, err, np, bail;
|
2012-06-29 12:53:55 +00:00
|
|
|
int nprocs, thread = 0, level;
|
|
|
|
short version, flags;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t chunksize, compressed_chunksize;
|
2012-05-28 14:49:29 +00:00
|
|
|
struct cmp_data **dary, *tdat;
|
|
|
|
pthread_t writer_thr;
|
2012-08-18 04:50:52 +00:00
|
|
|
algo_props_t props;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
err = 0;
|
2012-06-29 12:53:55 +00:00
|
|
|
flags = 0;
|
|
|
|
thread = 0;
|
2012-11-24 18:23:07 +00:00
|
|
|
dary = NULL;
|
2012-08-18 04:50:52 +00:00
|
|
|
init_algo_props(&props);
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* Open files and do sanity checks.
|
|
|
|
*/
|
|
|
|
if (!pipe_mode) {
|
|
|
|
if ((compfd = open(filename, O_RDONLY, 0)) == -1)
|
|
|
|
err_exit(1, "Cannot open: %s", filename);
|
|
|
|
|
|
|
|
if (fstat(compfd, &sbuf) == -1)
|
|
|
|
err_exit(1, "Cannot stat: %s", filename);
|
|
|
|
if (sbuf.st_size == 0)
|
2012-11-24 18:23:07 +00:00
|
|
|
return (1);
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
if ((uncompfd = open(to_filename, O_WRONLY|O_CREAT|O_TRUNC, 0)) == -1) {
|
|
|
|
close(compfd);
|
|
|
|
err_exit(1, "Cannot open: %s", to_filename);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
compfd = fileno(stdin);
|
|
|
|
if (compfd == -1) {
|
|
|
|
perror("fileno ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
uncompfd = fileno(stdout);
|
|
|
|
if (uncompfd == -1) {
|
|
|
|
perror("fileno ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read file header pieces and verify.
|
|
|
|
*/
|
|
|
|
if (Read(compfd, algorithm, ALGO_SZ) < ALGO_SZ) {
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
if (init_algo(algorithm, 0) != 0) {
|
|
|
|
fprintf(stderr, "%s is not a pcompressed file.\n", filename);
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
2012-07-03 17:17:24 +00:00
|
|
|
algo = algorithm;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
if (Read(compfd, &version, sizeof (version)) < sizeof (version) ||
|
2012-06-29 12:53:55 +00:00
|
|
|
Read(compfd, &flags, sizeof (flags)) < sizeof (flags) ||
|
2012-05-28 14:49:29 +00:00
|
|
|
Read(compfd, &chunksize, sizeof (chunksize)) < sizeof (chunksize) ||
|
|
|
|
Read(compfd, &level, sizeof (level)) < sizeof (level)) {
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
version = ntohs(version);
|
|
|
|
flags = ntohs(flags);
|
2012-05-28 14:49:29 +00:00
|
|
|
chunksize = ntohll(chunksize);
|
|
|
|
level = ntohl(level);
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
/*
|
|
|
|
* Check for ridiculous values (malicious tampering or otherwise).
|
|
|
|
*/
|
|
|
|
if (version > VERSION) {
|
|
|
|
fprintf(stderr, "Cannot handle newer archive version %d, capability %d\n",
|
|
|
|
version, VERSION);
|
|
|
|
err = 1;
|
|
|
|
goto uncomp_done;
|
|
|
|
}
|
|
|
|
if (chunksize > EIGHTY_PCT(get_total_ram())) {
|
|
|
|
fprintf(stderr, "Chunk size must not exceed 80%% of total RAM.\n");
|
|
|
|
err = 1;
|
|
|
|
goto uncomp_done;
|
|
|
|
}
|
|
|
|
if (level > MAX_LEVEL || level < 0) {
|
|
|
|
fprintf(stderr, "Invalid compression level in header: %d\n", level);
|
|
|
|
err = 1;
|
|
|
|
goto uncomp_done;
|
|
|
|
}
|
2012-10-15 06:40:00 +00:00
|
|
|
if (version < VERSION-2) {
|
2012-05-28 14:49:29 +00:00
|
|
|
fprintf(stderr, "Unsupported version: %d\n", version);
|
|
|
|
err = 1;
|
|
|
|
goto uncomp_done;
|
|
|
|
}
|
|
|
|
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize = chunksize + CHUNK_HDR_SZ + zlib_buf_extra(chunksize);
|
2012-08-04 12:25:20 +00:00
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
if (_props_func) {
|
|
|
|
_props_func(&props, level, chunksize);
|
|
|
|
if (chunksize + props.buf_extra > compressed_chunksize) {
|
|
|
|
compressed_chunksize += (chunksize + props.buf_extra -
|
2012-08-04 12:25:20 +00:00
|
|
|
compressed_chunksize);
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
if (flags & FLAG_DEDUP) {
|
|
|
|
enable_rabin_scan = 1;
|
2012-09-15 05:44:58 +00:00
|
|
|
|
|
|
|
} else if (flags & FLAG_DEDUP_FIXED) {
|
|
|
|
enable_fixed_scan = 1;
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
|
|
|
|
2012-08-27 16:54:23 +00:00
|
|
|
if (flags & FLAG_SINGLE_CHUNK) {
|
|
|
|
props.is_single_chunk = 1;
|
|
|
|
}
|
|
|
|
|
2012-08-31 17:06:06 +00:00
|
|
|
cksum = flags & CKSUM_MASK;
|
2012-10-17 18:02:35 +00:00
|
|
|
if (get_checksum_props(NULL, &cksum, &cksum_bytes, &mac_bytes) == -1) {
|
2012-09-01 09:10:15 +00:00
|
|
|
fprintf(stderr, "Invalid checksum algorithm code: %d. File corrupt ?\n", cksum);
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
2012-08-31 17:06:06 +00:00
|
|
|
|
2012-11-23 18:30:05 +00:00
|
|
|
if (version < 5)
|
|
|
|
mac_bytes = 0;
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
/*
|
|
|
|
* If encryption is enabled initialize crypto.
|
|
|
|
*/
|
|
|
|
if (flags & MASK_CRYPTO_ALG) {
|
|
|
|
int saltlen;
|
|
|
|
uchar_t *salt1, *salt2;
|
|
|
|
uint64_t nonce;
|
|
|
|
uchar_t pw[MAX_PW_LEN];
|
|
|
|
int pw_len;
|
2012-10-17 18:02:35 +00:00
|
|
|
mac_ctx_t hdr_mac;
|
|
|
|
uchar_t hdr_hash1[mac_bytes], hdr_hash2[mac_bytes];
|
|
|
|
unsigned int hlen;
|
|
|
|
unsigned short d1;
|
|
|
|
unsigned int d2;
|
2012-10-15 06:40:00 +00:00
|
|
|
|
2012-11-23 16:57:14 +00:00
|
|
|
/*
|
|
|
|
* In encrypted files we do not have a normal digest. The HMAC
|
|
|
|
* is computed over header and encrypted data.
|
|
|
|
*/
|
|
|
|
cksum_bytes = 0;
|
2012-10-17 18:02:35 +00:00
|
|
|
compressed_chunksize += mac_bytes;
|
2012-10-15 06:40:00 +00:00
|
|
|
encrypt_type = flags & MASK_CRYPTO_ALG;
|
|
|
|
if (Read(compfd, &saltlen, sizeof (saltlen)) < sizeof (saltlen)) {
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
saltlen = ntohl(saltlen);
|
|
|
|
salt1 = malloc(saltlen);
|
|
|
|
salt2 = malloc(saltlen);
|
|
|
|
if (Read(compfd, salt1, saltlen) < saltlen) {
|
|
|
|
free(salt1); free(salt2);
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
deserialize_checksum(salt2, salt1, saltlen);
|
|
|
|
memset(salt1, 0, saltlen);
|
|
|
|
free(salt1);
|
|
|
|
|
|
|
|
if (Read(compfd, &nonce, sizeof (nonce)) < sizeof (nonce)) {
|
|
|
|
memset(salt2, 0, saltlen);
|
|
|
|
free(salt2);
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
nonce = ntohll(nonce);
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
if (Read(compfd, hdr_hash1, mac_bytes) < mac_bytes) {
|
|
|
|
memset(salt2, 0, saltlen);
|
|
|
|
free(salt2);
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
deserialize_checksum(hdr_hash2, hdr_hash1, mac_bytes);
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
if (!pwd_file) {
|
|
|
|
pw_len = get_pw_string(pw,
|
2012-10-18 17:25:41 +00:00
|
|
|
"Please enter decryption password", 0);
|
2012-10-15 06:40:00 +00:00
|
|
|
if (pw_len == -1) {
|
|
|
|
memset(salt2, 0, saltlen);
|
|
|
|
free(salt2);
|
2012-10-17 18:02:35 +00:00
|
|
|
err_exit(0, "Failed to get password.\n");
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int fd, len;
|
|
|
|
uchar_t zero[MAX_PW_LEN];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read password from a file and zero out the file after reading.
|
|
|
|
*/
|
|
|
|
memset(zero, 0, MAX_PW_LEN);
|
|
|
|
fd = open(pwd_file, O_RDWR);
|
|
|
|
if (fd != -1) {
|
|
|
|
pw_len = lseek(fd, 0, SEEK_END);
|
|
|
|
if (pw_len != -1) {
|
|
|
|
if (pw_len > MAX_PW_LEN) pw_len = MAX_PW_LEN-1;
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
len = Read(fd, pw, pw_len);
|
|
|
|
if (len != -1 && len == pw_len) {
|
|
|
|
pw[pw_len] = '\0';
|
|
|
|
if (isspace(pw[pw_len - 1]))
|
|
|
|
pw[pw_len-1] = '\0';
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
Write(fd, zero, pw_len);
|
|
|
|
} else {
|
|
|
|
pw_len = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pw_len == -1) {
|
|
|
|
perror(" ");
|
|
|
|
memset(salt2, 0, saltlen);
|
|
|
|
free(salt2);
|
2012-10-18 17:25:41 +00:00
|
|
|
close(uncompfd); unlink(to_filename);
|
2012-10-17 18:02:35 +00:00
|
|
|
err_exit(0, "Failed to get password.\n");
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
if (init_crypto(&crypto_ctx, pw, pw_len, encrypt_type, salt2,
|
|
|
|
saltlen, nonce, DECRYPT_FLAG) == -1) {
|
|
|
|
memset(salt2, 0, saltlen);
|
|
|
|
free(salt2);
|
|
|
|
memset(pw, 0, MAX_PW_LEN);
|
2012-10-18 17:25:41 +00:00
|
|
|
close(uncompfd); unlink(to_filename);
|
2012-10-17 18:02:35 +00:00
|
|
|
err_exit(0, "Failed to initialize crypto\n");
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
memset(salt2, 0, saltlen);
|
|
|
|
free(salt2);
|
|
|
|
nonce = 0;
|
|
|
|
memset(pw, 0, MAX_PW_LEN);
|
2012-10-17 18:02:35 +00:00
|
|
|
|
|
|
|
/*
|
2012-11-23 16:57:14 +00:00
|
|
|
* Verify file header HMAC.
|
2012-10-17 18:02:35 +00:00
|
|
|
*/
|
|
|
|
if (hmac_init(&hdr_mac, cksum, &crypto_ctx) == -1) {
|
2012-10-18 17:25:41 +00:00
|
|
|
close(uncompfd); unlink(to_filename);
|
2012-10-17 18:02:35 +00:00
|
|
|
err_exit(0, "Cannot initialize header hmac.\n");
|
|
|
|
}
|
|
|
|
hmac_update(&hdr_mac, (uchar_t *)algo, ALGO_SZ);
|
|
|
|
d1 = htons(version);
|
|
|
|
hmac_update(&hdr_mac, (uchar_t *)&d1, sizeof (version));
|
|
|
|
d1 = htons(flags);
|
2012-12-08 08:49:01 +00:00
|
|
|
hmac_update(&hdr_mac, (uchar_t *)&d1, sizeof (flags));
|
|
|
|
nonce = htonll(chunksize);
|
2012-10-17 18:02:35 +00:00
|
|
|
hmac_update(&hdr_mac, (uchar_t *)&nonce, sizeof (nonce));
|
|
|
|
d2 = htonl(level);
|
|
|
|
hmac_update(&hdr_mac, (uchar_t *)&d2, sizeof (level));
|
|
|
|
hmac_final(&hdr_mac, hdr_hash1, &hlen);
|
|
|
|
hmac_cleanup(&hdr_mac);
|
|
|
|
if (memcmp(hdr_hash2, hdr_hash1, mac_bytes) != 0) {
|
2012-10-18 17:25:41 +00:00
|
|
|
close(uncompfd); unlink(to_filename);
|
|
|
|
err_exit(0, "Header verification failed! File tampered or wrong password.\n");
|
2012-10-17 18:02:35 +00:00
|
|
|
}
|
2012-11-23 18:30:05 +00:00
|
|
|
} else if (version >= 5) {
|
2012-11-22 18:30:39 +00:00
|
|
|
uint32_t crc1, crc2;
|
|
|
|
unsigned int hlen;
|
|
|
|
unsigned short d1;
|
|
|
|
unsigned int d2;
|
|
|
|
uint64_t ch;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify file header CRC32 in non-crypto mode.
|
|
|
|
*/
|
|
|
|
if (Read(compfd, &crc1, sizeof (crc1)) < sizeof (crc1)) {
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
crc1 = htonl(crc1);
|
|
|
|
mac_bytes = sizeof (uint32_t);
|
|
|
|
|
|
|
|
crc2 = lzma_crc32((uchar_t *)algo, ALGO_SZ, 0);
|
|
|
|
d1 = htons(version);
|
|
|
|
crc2 = lzma_crc32((uchar_t *)&d1, sizeof (version), crc2);
|
|
|
|
d1 = htons(flags);
|
|
|
|
crc2 = lzma_crc32((uchar_t *)&d1, sizeof (version), crc2);
|
|
|
|
ch = htonll(chunksize);
|
|
|
|
crc2 = lzma_crc32((uchar_t *)&ch, sizeof (ch), crc2);
|
|
|
|
d2 = htonl(level);
|
|
|
|
crc2 = lzma_crc32((uchar_t *)&d2, sizeof (level), crc2);
|
|
|
|
if (crc1 != crc2) {
|
|
|
|
close(uncompfd); unlink(to_filename);
|
|
|
|
err_exit(0, "Header verification failed! File tampered or wrong password.\n");
|
|
|
|
}
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 16:14:02 +00:00
|
|
|
nprocs = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (nthreads > 0 && nthreads < nprocs)
|
2012-05-28 14:49:29 +00:00
|
|
|
nprocs = nthreads;
|
2012-08-18 04:50:52 +00:00
|
|
|
else
|
|
|
|
nthreads = nprocs;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
set_threadcounts(&props, &nthreads, nprocs, DECOMPRESS_THREADS);
|
|
|
|
fprintf(stderr, "Scaling to %d thread", nthreads * props.nthreads);
|
2012-08-18 16:30:14 +00:00
|
|
|
if (nthreads * props.nthreads > 1) fprintf(stderr, "s");
|
2012-08-18 04:50:52 +00:00
|
|
|
fprintf(stderr, "\n");
|
2012-08-18 16:30:14 +00:00
|
|
|
nprocs = nthreads;
|
2012-08-31 17:06:06 +00:00
|
|
|
slab_cache_add(compressed_chunksize);
|
2012-05-31 16:06:33 +00:00
|
|
|
slab_cache_add(chunksize);
|
|
|
|
slab_cache_add(sizeof (struct cmp_data));
|
|
|
|
|
2012-11-22 15:32:50 +00:00
|
|
|
dary = (struct cmp_data **)slab_calloc(NULL, nprocs, sizeof (struct cmp_data *));
|
2012-05-28 14:49:29 +00:00
|
|
|
for (i = 0; i < nprocs; i++) {
|
|
|
|
dary[i] = (struct cmp_data *)slab_alloc(NULL, sizeof (struct cmp_data));
|
|
|
|
if (!dary[i]) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
tdat = dary[i];
|
2012-08-18 16:30:14 +00:00
|
|
|
tdat->compressed_chunk = NULL;
|
2012-10-15 06:40:00 +00:00
|
|
|
tdat->uncompressed_chunk = NULL;
|
2012-05-28 14:49:29 +00:00
|
|
|
tdat->chunksize = chunksize;
|
|
|
|
tdat->compress = _compress_func;
|
|
|
|
tdat->decompress = _decompress_func;
|
|
|
|
tdat->cancel = 0;
|
|
|
|
tdat->level = level;
|
2012-11-09 13:23:48 +00:00
|
|
|
tdat->data = NULL;
|
2012-12-04 18:39:47 +00:00
|
|
|
tdat->props = &props;
|
2012-05-28 14:49:29 +00:00
|
|
|
sem_init(&(tdat->start_sem), 0, 0);
|
|
|
|
sem_init(&(tdat->cmp_done_sem), 0, 0);
|
|
|
|
sem_init(&(tdat->write_done_sem), 0, 1);
|
2012-07-23 16:13:12 +00:00
|
|
|
if (_init_func) {
|
2012-11-22 15:32:50 +00:00
|
|
|
if (_init_func(&(tdat->data), &(tdat->level), props.nthreads, chunksize,
|
|
|
|
version, DECOMPRESS) != 0) {
|
2012-07-23 16:13:12 +00:00
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
2012-09-16 05:42:58 +00:00
|
|
|
if (enable_rabin_scan || enable_fixed_scan) {
|
|
|
|
tdat->rctx = create_dedupe_context(chunksize, compressed_chunksize, rab_blk_size,
|
2012-11-22 15:32:50 +00:00
|
|
|
algo, enable_delta_encode, enable_fixed_scan, version, DECOMPRESS);
|
2012-08-10 05:17:11 +00:00
|
|
|
if (tdat->rctx == NULL) {
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-29 12:53:55 +00:00
|
|
|
tdat->rctx = NULL;
|
2012-08-10 05:17:11 +00:00
|
|
|
}
|
2012-10-18 17:25:41 +00:00
|
|
|
|
|
|
|
if (encrypt_type) {
|
|
|
|
if (hmac_init(&tdat->chunk_hmac, cksum, &crypto_ctx) == -1) {
|
|
|
|
fprintf(stderr, "Cannot initialize chunk hmac.\n");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
if (pthread_create(&(tdat->thr), NULL, perform_decompress,
|
|
|
|
(void *)tdat) != 0) {
|
|
|
|
perror("Error in thread creation: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread = 1;
|
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
if (encrypt_type) {
|
|
|
|
/* Erase encryption key bytes stored as a plain array. No longer reqd. */
|
|
|
|
crypto_clean_pkey(&crypto_ctx);
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
w.dary = dary;
|
|
|
|
w.wfd = uncompfd;
|
|
|
|
w.nprocs = nprocs;
|
2012-06-29 12:53:55 +00:00
|
|
|
w.chunksize = chunksize;
|
2012-05-28 14:49:29 +00:00
|
|
|
if (pthread_create(&writer_thr, NULL, writer_thread, (void *)(&w)) != 0) {
|
|
|
|
perror("Error in thread creation: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now read from the compressed file in variable compressed chunk size.
|
|
|
|
* First the size is read from the chunk header and then as many bytes +
|
2012-08-31 17:06:06 +00:00
|
|
|
* checksum size are read and passed to decompression thread.
|
2012-05-28 14:49:29 +00:00
|
|
|
* Chunk sequencing is ensured.
|
|
|
|
*/
|
|
|
|
chunk_num = 0;
|
|
|
|
np = 0;
|
|
|
|
bail = 0;
|
|
|
|
while (!bail) {
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t rb;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
if (main_cancel) break;
|
|
|
|
for (p = 0; p < nprocs; p++) {
|
|
|
|
np = p;
|
|
|
|
tdat = dary[p];
|
|
|
|
sem_wait(&tdat->write_done_sem);
|
|
|
|
if (main_cancel) break;
|
|
|
|
tdat->id = chunk_num;
|
|
|
|
|
2012-08-24 14:46:21 +00:00
|
|
|
/*
|
|
|
|
* First read length of compressed chunk.
|
|
|
|
*/
|
|
|
|
rb = Read(compfd, &tdat->len_cmp, sizeof (tdat->len_cmp));
|
|
|
|
if (rb != sizeof (tdat->len_cmp)) {
|
|
|
|
if (rb < 0) perror("Read: ");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Incomplete chunk %d header,"
|
|
|
|
"file corrupt\n", chunk_num);
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
2012-10-18 17:25:41 +00:00
|
|
|
tdat->len_cmp_be = tdat->len_cmp; // Needed for HMAC
|
2012-08-24 14:46:21 +00:00
|
|
|
tdat->len_cmp = htonll(tdat->len_cmp);
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
/*
|
|
|
|
* Check for ridiculous length.
|
|
|
|
*/
|
|
|
|
if (tdat->len_cmp > chunksize + 256) {
|
|
|
|
fprintf(stderr, "Compressed length too big for chunk: %d\n",
|
|
|
|
chunk_num);
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
|
2012-08-24 14:46:21 +00:00
|
|
|
/*
|
|
|
|
* Zero compressed len means end of file.
|
|
|
|
*/
|
|
|
|
if (tdat->len_cmp == 0) {
|
|
|
|
bail = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-18 16:30:14 +00:00
|
|
|
/*
|
|
|
|
* Delayed allocation. Allocate chunks if not already done. The compressed
|
|
|
|
* file format does not provide any info on how many chunks are there in
|
|
|
|
* order to allow pipe mode operation. So delayed allocation during
|
|
|
|
* decompression allows to avoid allocating per-thread chunks which will
|
|
|
|
* never be used. This can happen if chunk count < thread count.
|
|
|
|
*/
|
|
|
|
if (!tdat->compressed_chunk) {
|
|
|
|
tdat->compressed_chunk = (uchar_t *)slab_alloc(NULL,
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize);
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan))
|
2012-08-18 16:30:14 +00:00
|
|
|
tdat->uncompressed_chunk = (uchar_t *)slab_alloc(NULL,
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize);
|
2012-08-18 16:30:14 +00:00
|
|
|
else
|
2012-08-31 17:06:06 +00:00
|
|
|
tdat->uncompressed_chunk = (uchar_t *)slab_alloc(NULL,
|
|
|
|
chunksize);
|
2012-08-18 16:30:14 +00:00
|
|
|
if (!tdat->compressed_chunk || !tdat->uncompressed_chunk) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
tdat->cmp_seg = tdat->uncompressed_chunk;
|
|
|
|
}
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
if (tdat->len_cmp > largest_chunk)
|
|
|
|
largest_chunk = tdat->len_cmp;
|
|
|
|
if (tdat->len_cmp < smallest_chunk)
|
|
|
|
smallest_chunk = tdat->len_cmp;
|
|
|
|
avg_chunk += tdat->len_cmp;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
2012-08-31 17:06:06 +00:00
|
|
|
* Now read compressed chunk including the checksum.
|
2012-05-28 14:49:29 +00:00
|
|
|
*/
|
|
|
|
tdat->rbytes = Read(compfd, tdat->compressed_chunk,
|
2012-10-17 18:02:35 +00:00
|
|
|
tdat->len_cmp + cksum_bytes + mac_bytes + CHUNK_FLAG_SZ);
|
2012-05-28 14:49:29 +00:00
|
|
|
if (main_cancel) break;
|
2012-10-17 18:02:35 +00:00
|
|
|
if (tdat->rbytes < tdat->len_cmp + cksum_bytes + mac_bytes + CHUNK_FLAG_SZ) {
|
2012-05-28 14:49:29 +00:00
|
|
|
if (tdat->rbytes < 0) {
|
|
|
|
perror("Read: ");
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Incomplete chunk %d, file corrupt.\n",
|
|
|
|
chunk_num);
|
|
|
|
UNCOMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sem_post(&tdat->start_sem);
|
|
|
|
chunk_num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!main_cancel) {
|
|
|
|
for (p = 0; p < nprocs; p++) {
|
|
|
|
if (p == np) continue;
|
|
|
|
tdat = dary[p];
|
|
|
|
sem_wait(&tdat->write_done_sem);
|
|
|
|
}
|
|
|
|
thread = 0;
|
|
|
|
}
|
|
|
|
uncomp_done:
|
2012-11-25 09:42:45 +00:00
|
|
|
if (t_errored) err = t_errored;
|
2012-05-28 14:49:29 +00:00
|
|
|
if (thread) {
|
|
|
|
for (i = 0; i < nprocs; i++) {
|
|
|
|
tdat = dary[i];
|
|
|
|
tdat->cancel = 1;
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
sem_post(&tdat->start_sem);
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
pthread_join(tdat->thr, NULL);
|
|
|
|
}
|
|
|
|
pthread_join(writer_thr, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ownership and mode of target should be same as original.
|
|
|
|
*/
|
|
|
|
fchmod(uncompfd, sbuf.st_mode);
|
|
|
|
if (fchown(uncompfd, sbuf.st_uid, sbuf.st_gid) == -1)
|
|
|
|
perror("Chown ");
|
|
|
|
if (dary != NULL) {
|
|
|
|
for (i = 0; i < nprocs; i++) {
|
2012-11-22 15:32:50 +00:00
|
|
|
if (!dary[i]) continue;
|
2012-08-18 16:30:14 +00:00
|
|
|
if (dary[i]->uncompressed_chunk)
|
|
|
|
slab_free(NULL, dary[i]->uncompressed_chunk);
|
|
|
|
if (dary[i]->compressed_chunk)
|
|
|
|
slab_free(NULL, dary[i]->compressed_chunk);
|
2012-05-28 14:49:29 +00:00
|
|
|
if (_deinit_func)
|
2012-08-08 17:10:58 +00:00
|
|
|
_deinit_func(&(dary[i]->data));
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan)) {
|
|
|
|
destroy_dedupe_context(dary[i]->rctx);
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
slab_free(NULL, dary[i]);
|
|
|
|
}
|
|
|
|
slab_free(NULL, dary);
|
|
|
|
}
|
|
|
|
if (!pipe_mode) {
|
|
|
|
if (compfd != -1) close(compfd);
|
|
|
|
if (uncompfd != -1) close(uncompfd);
|
|
|
|
}
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
if (!hide_cmp_stats) show_compression_stats(chunksize);
|
|
|
|
slab_cleanup(hide_mem_stats);
|
2012-11-24 18:23:07 +00:00
|
|
|
|
|
|
|
return (err);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
perform_compress(void *dat) {
|
|
|
|
struct cmp_data *tdat = (struct cmp_data *)dat;
|
2012-09-15 05:44:58 +00:00
|
|
|
typeof (tdat->chunksize) _chunksize, len_cmp, dedupe_index_sz, index_size_cmp;
|
2012-05-28 14:49:29 +00:00
|
|
|
int type, rv;
|
2012-07-01 16:14:02 +00:00
|
|
|
uchar_t *compressed_chunk;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t rbytes;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
redo:
|
|
|
|
sem_wait(&tdat->start_sem);
|
|
|
|
if (unlikely(tdat->cancel)) {
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunk = tdat->compressed_chunk + CHUNK_FLAG_SZ;
|
2012-08-05 17:05:51 +00:00
|
|
|
rbytes = tdat->rbytes;
|
2012-06-29 12:53:55 +00:00
|
|
|
/* Perform Dedup if enabled. */
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan)) {
|
|
|
|
dedupe_context_t *rctx;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
/*
|
2012-08-05 17:05:51 +00:00
|
|
|
* Compute checksum of original uncompressed chunk. When doing dedup
|
|
|
|
* cmp_seg hold original data instead of uncompressed_chunk. We dedup
|
|
|
|
* into uncompressed_chunk so that compress transforms uncompressed_chunk
|
|
|
|
* back into cmp_seg. Avoids an extra memcpy().
|
2012-06-29 12:53:55 +00:00
|
|
|
*/
|
2012-11-23 16:57:14 +00:00
|
|
|
if (!encrypt_type)
|
|
|
|
compute_checksum(tdat->checksum, cksum, tdat->cmp_seg, tdat->rbytes);
|
2012-06-29 12:53:55 +00:00
|
|
|
|
|
|
|
rctx = tdat->rctx;
|
2012-09-16 05:42:58 +00:00
|
|
|
reset_dedupe_context(tdat->rctx);
|
2012-06-29 12:53:55 +00:00
|
|
|
rctx->cbuf = tdat->uncompressed_chunk;
|
2012-09-16 05:42:58 +00:00
|
|
|
dedupe_index_sz = dedupe_compress(tdat->rctx, tdat->cmp_seg, &(tdat->rbytes), 0, NULL);
|
2012-06-29 12:53:55 +00:00
|
|
|
if (!rctx->valid) {
|
|
|
|
memcpy(tdat->uncompressed_chunk, tdat->cmp_seg, rbytes);
|
|
|
|
tdat->rbytes = rbytes;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Compute checksum of original uncompressed chunk.
|
|
|
|
*/
|
2012-11-23 16:57:14 +00:00
|
|
|
if (!encrypt_type)
|
|
|
|
compute_checksum(tdat->checksum, cksum, tdat->uncompressed_chunk, tdat->rbytes);
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-07-01 16:14:02 +00:00
|
|
|
/*
|
|
|
|
* If doing dedup we compress rabin index and deduped data separately.
|
|
|
|
* The rabin index array values can pollute the compressor's dictionary thereby
|
|
|
|
* reducing compression effectiveness of the data chunk. So we separate them.
|
|
|
|
*/
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan) && tdat->rctx->valid) {
|
2012-09-15 05:44:58 +00:00
|
|
|
_chunksize = tdat->rbytes - dedupe_index_sz - RABIN_HDR_SIZE;
|
|
|
|
index_size_cmp = dedupe_index_sz;
|
2012-07-01 16:14:02 +00:00
|
|
|
|
2012-07-21 18:30:41 +00:00
|
|
|
rv = 0;
|
2012-12-12 18:30:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a matrix transpose of the index table with the hope of improving
|
|
|
|
* compression ratio subsequently.
|
|
|
|
*/
|
|
|
|
transpose(tdat->uncompressed_chunk + RABIN_HDR_SIZE,
|
|
|
|
compressed_chunk + RABIN_HDR_SIZE, dedupe_index_sz,
|
|
|
|
sizeof (uint32_t), ROW);
|
|
|
|
memcpy(tdat->uncompressed_chunk + RABIN_HDR_SIZE,
|
|
|
|
compressed_chunk + RABIN_HDR_SIZE, dedupe_index_sz);
|
|
|
|
|
2012-09-15 05:44:58 +00:00
|
|
|
if (dedupe_index_sz >= 90) {
|
2012-07-21 18:30:41 +00:00
|
|
|
/* Compress index if it is at least 90 bytes. */
|
|
|
|
rv = lzma_compress(tdat->uncompressed_chunk + RABIN_HDR_SIZE,
|
2012-09-15 05:44:58 +00:00
|
|
|
dedupe_index_sz, compressed_chunk + RABIN_HDR_SIZE,
|
2012-08-23 17:28:44 +00:00
|
|
|
&index_size_cmp, tdat->rctx->level, 255, tdat->rctx->lzma_data);
|
2012-12-12 18:30:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If index compression fails or does not produce a smaller result
|
|
|
|
* retain it as is. In that case compressed size == original size
|
|
|
|
* and it will be handled correctly during decompression.
|
|
|
|
*/
|
|
|
|
if (rv != 0 || index_size_cmp >= dedupe_index_sz) {
|
|
|
|
index_size_cmp = dedupe_index_sz;
|
|
|
|
goto plain_index;
|
|
|
|
}
|
2012-07-21 18:30:41 +00:00
|
|
|
} else {
|
2012-12-12 18:30:47 +00:00
|
|
|
plain_index:
|
2012-07-21 18:30:41 +00:00
|
|
|
memcpy(compressed_chunk + RABIN_HDR_SIZE,
|
2012-09-15 05:44:58 +00:00
|
|
|
tdat->uncompressed_chunk + RABIN_HDR_SIZE, dedupe_index_sz);
|
2012-07-21 18:30:41 +00:00
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
|
|
|
|
index_size_cmp += RABIN_HDR_SIZE;
|
2012-09-15 05:44:58 +00:00
|
|
|
dedupe_index_sz += RABIN_HDR_SIZE;
|
2012-12-12 18:30:47 +00:00
|
|
|
memcpy(compressed_chunk, tdat->uncompressed_chunk, RABIN_HDR_SIZE);
|
|
|
|
/* Compress data chunk. */
|
|
|
|
if (lzp_preprocess) {
|
|
|
|
rv = preproc_compress(tdat->compress, tdat->uncompressed_chunk + dedupe_index_sz,
|
|
|
|
_chunksize, compressed_chunk + index_size_cmp, &_chunksize,
|
|
|
|
tdat->level, 0, tdat->data, tdat->props);
|
2012-08-05 17:05:51 +00:00
|
|
|
} else {
|
2012-12-12 18:30:47 +00:00
|
|
|
rv = tdat->compress(tdat->uncompressed_chunk + dedupe_index_sz, _chunksize,
|
|
|
|
compressed_chunk + index_size_cmp, &_chunksize, tdat->level, 0, tdat->data);
|
2012-07-01 16:14:02 +00:00
|
|
|
}
|
2012-12-12 18:30:47 +00:00
|
|
|
|
|
|
|
/* Can't compress data just retain as-is. */
|
|
|
|
if (rv < 0)
|
|
|
|
memcpy(compressed_chunk + index_size_cmp,
|
|
|
|
tdat->uncompressed_chunk + dedupe_index_sz, _chunksize);
|
|
|
|
/* Now update rabin header with the compressed sizes. */
|
|
|
|
update_dedupe_hdr(compressed_chunk, index_size_cmp - RABIN_HDR_SIZE, _chunksize);
|
2012-07-01 16:14:02 +00:00
|
|
|
_chunksize += index_size_cmp;
|
|
|
|
} else {
|
2012-08-05 17:05:51 +00:00
|
|
|
plain_compress:
|
2012-07-01 16:14:02 +00:00
|
|
|
_chunksize = tdat->rbytes;
|
2012-08-23 17:28:44 +00:00
|
|
|
if (lzp_preprocess) {
|
|
|
|
rv = preproc_compress(tdat->compress,
|
|
|
|
tdat->uncompressed_chunk, tdat->rbytes,
|
2012-12-04 18:39:47 +00:00
|
|
|
compressed_chunk, &_chunksize, tdat->level, 0, tdat->data,
|
|
|
|
tdat->props);
|
2012-08-23 17:28:44 +00:00
|
|
|
} else {
|
|
|
|
rv = tdat->compress(tdat->uncompressed_chunk, tdat->rbytes,
|
|
|
|
compressed_chunk, &_chunksize, tdat->level, 0, tdat->data);
|
|
|
|
}
|
2012-07-01 16:14:02 +00:00
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* Sanity check to ensure compressed data is lesser than original.
|
|
|
|
* If at all compression expands/does not shrink data then the chunk
|
|
|
|
* will be left uncompressed. Also if the compression errored the
|
|
|
|
* chunk will be left uncompressed.
|
|
|
|
*/
|
|
|
|
tdat->len_cmp = _chunksize;
|
2012-08-05 17:05:51 +00:00
|
|
|
if (_chunksize >= rbytes || rv < 0) {
|
2012-09-16 05:42:58 +00:00
|
|
|
if (!(enable_rabin_scan || enable_fixed_scan) || !tdat->rctx->valid)
|
2012-08-05 17:05:51 +00:00
|
|
|
memcpy(compressed_chunk, tdat->uncompressed_chunk, tdat->rbytes);
|
2012-05-28 14:49:29 +00:00
|
|
|
type = UNCOMPRESSED;
|
|
|
|
tdat->len_cmp = tdat->rbytes;
|
2012-11-09 15:37:23 +00:00
|
|
|
if (rv < 0) rv = COMPRESS_NONE;
|
2012-05-28 14:49:29 +00:00
|
|
|
} else {
|
|
|
|
type = COMPRESSED;
|
|
|
|
}
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
/*
|
|
|
|
* Now perform encryption on the compressed data, if requested.
|
|
|
|
*/
|
|
|
|
if (encrypt_type) {
|
2012-11-09 13:23:48 +00:00
|
|
|
int ret;
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
/*
|
2012-10-18 17:25:41 +00:00
|
|
|
* Encryption algorithm must not change the size and
|
2012-10-15 06:40:00 +00:00
|
|
|
* encryption is in-place.
|
|
|
|
*/
|
2012-11-09 13:23:48 +00:00
|
|
|
ret = crypto_buf(&crypto_ctx, compressed_chunk, compressed_chunk,
|
2012-10-15 06:40:00 +00:00
|
|
|
tdat->len_cmp, tdat->id);
|
2012-11-09 13:23:48 +00:00
|
|
|
if (ret == -1) {
|
2012-10-15 06:40:00 +00:00
|
|
|
/*
|
|
|
|
* Encryption failure is fatal.
|
|
|
|
*/
|
|
|
|
main_cancel = 1;
|
|
|
|
tdat->len_cmp = 0;
|
2012-11-25 09:42:45 +00:00
|
|
|
t_errored = 1;
|
2012-10-15 06:40:00 +00:00
|
|
|
sem_post(&tdat->cmp_done_sem);
|
2012-11-24 18:23:07 +00:00
|
|
|
return (0);
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan) && tdat->rctx->valid) {
|
2012-06-29 12:53:55 +00:00
|
|
|
type |= CHUNK_FLAG_DEDUP;
|
|
|
|
}
|
2012-08-23 17:28:44 +00:00
|
|
|
if (lzp_preprocess) {
|
|
|
|
type |= CHUNK_FLAG_PREPROC;
|
|
|
|
}
|
2012-08-31 17:06:06 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
2012-08-31 17:06:06 +00:00
|
|
|
* Insert compressed chunk length and checksum into chunk header.
|
2012-05-28 14:49:29 +00:00
|
|
|
*/
|
|
|
|
len_cmp = tdat->len_cmp;
|
|
|
|
*((typeof (len_cmp) *)(tdat->cmp_seg)) = htonll(tdat->len_cmp);
|
2012-11-23 16:57:14 +00:00
|
|
|
if (!encrypt_type)
|
|
|
|
serialize_checksum(tdat->checksum, tdat->cmp_seg + sizeof (tdat->len_cmp), cksum_bytes);
|
2012-08-31 17:06:06 +00:00
|
|
|
tdat->len_cmp += CHUNK_FLAG_SZ;
|
2012-05-28 14:49:29 +00:00
|
|
|
tdat->len_cmp += sizeof (len_cmp);
|
2012-10-17 18:02:35 +00:00
|
|
|
tdat->len_cmp += (cksum_bytes + mac_bytes);
|
2012-10-19 16:21:27 +00:00
|
|
|
rbytes = tdat->len_cmp - len_cmp; // HDR size for HMAC
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
if (adapt_mode)
|
|
|
|
type |= (rv << 4);
|
|
|
|
|
|
|
|
/*
|
2012-06-29 12:53:55 +00:00
|
|
|
* If chunk is less than max chunksize, store this length as well.
|
2012-05-28 14:49:29 +00:00
|
|
|
*/
|
|
|
|
if (tdat->rbytes < tdat->chunksize) {
|
|
|
|
type |= CHSIZE_MASK;
|
|
|
|
*((typeof (tdat->rbytes) *)(tdat->cmp_seg + tdat->len_cmp)) = htonll(tdat->rbytes);
|
2012-08-31 17:06:06 +00:00
|
|
|
tdat->len_cmp += ORIGINAL_CHUNKSZ;
|
|
|
|
len_cmp += ORIGINAL_CHUNKSZ;
|
2012-05-28 14:49:29 +00:00
|
|
|
*((typeof (len_cmp) *)(tdat->cmp_seg)) = htonll(len_cmp);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Set the chunk header flags.
|
|
|
|
*/
|
|
|
|
*(tdat->compressed_chunk) = type;
|
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
/*
|
2012-11-23 16:57:14 +00:00
|
|
|
* If encrypting, compute HMAC for full chunk including header.
|
2012-10-18 17:25:41 +00:00
|
|
|
*/
|
|
|
|
if (encrypt_type) {
|
|
|
|
uchar_t *mac_ptr;
|
|
|
|
unsigned int hlen;
|
|
|
|
uchar_t chash[mac_bytes];
|
|
|
|
|
2012-10-19 16:21:27 +00:00
|
|
|
/* Clean out mac_bytes to 0 for stable HMAC. */
|
2012-10-18 17:25:41 +00:00
|
|
|
mac_ptr = tdat->cmp_seg + sizeof (tdat->len_cmp) + cksum_bytes;
|
|
|
|
memset(mac_ptr, 0, mac_bytes);
|
|
|
|
hmac_reinit(&tdat->chunk_hmac);
|
2012-11-23 16:57:14 +00:00
|
|
|
hmac_update(&tdat->chunk_hmac, tdat->cmp_seg, tdat->len_cmp);
|
2012-10-18 17:25:41 +00:00
|
|
|
hmac_final(&tdat->chunk_hmac, chash, &hlen);
|
|
|
|
serialize_checksum(chash, mac_ptr, hlen);
|
2012-11-22 18:30:39 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Compute header CRC32 in non-crypto mode.
|
|
|
|
*/
|
|
|
|
uchar_t *mac_ptr;
|
|
|
|
unsigned int hlen;
|
|
|
|
uint32_t crc;
|
|
|
|
|
2012-11-23 16:57:14 +00:00
|
|
|
/* Clean out mac_bytes to 0 for stable CRC32. */
|
2012-11-22 18:30:39 +00:00
|
|
|
mac_ptr = tdat->cmp_seg + sizeof (tdat->len_cmp) + cksum_bytes;
|
|
|
|
memset(mac_ptr, 0, mac_bytes);
|
|
|
|
crc = lzma_crc32(tdat->cmp_seg, rbytes, 0);
|
|
|
|
if (type & CHSIZE_MASK)
|
|
|
|
crc = lzma_crc32(tdat->cmp_seg + tdat->len_cmp - ORIGINAL_CHUNKSZ,
|
|
|
|
ORIGINAL_CHUNKSZ, crc);
|
|
|
|
*((uint32_t *)mac_ptr) = htonl(crc);
|
2012-10-18 17:25:41 +00:00
|
|
|
}
|
2012-11-22 18:30:39 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
cont:
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *
|
|
|
|
writer_thread(void *dat) {
|
|
|
|
int p;
|
|
|
|
struct wdata *w = (struct wdata *)dat;
|
|
|
|
struct cmp_data *tdat;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t wbytes;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
repeat:
|
|
|
|
for (p = 0; p < w->nprocs; p++) {
|
|
|
|
tdat = w->dary[p];
|
|
|
|
sem_wait(&tdat->cmp_done_sem);
|
|
|
|
if (tdat->len_cmp == 0) {
|
|
|
|
goto do_cancel;
|
|
|
|
}
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
if (do_compress) {
|
|
|
|
if (tdat->len_cmp > largest_chunk)
|
|
|
|
largest_chunk = tdat->len_cmp;
|
|
|
|
if (tdat->len_cmp < smallest_chunk)
|
|
|
|
smallest_chunk = tdat->len_cmp;
|
|
|
|
avg_chunk += tdat->len_cmp;
|
|
|
|
}
|
2012-08-31 17:06:06 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
wbytes = Write(w->wfd, tdat->cmp_seg, tdat->len_cmp);
|
|
|
|
if (unlikely(wbytes != tdat->len_cmp)) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
perror("Chunk Write: ");
|
|
|
|
do_cancel:
|
|
|
|
main_cancel = 1;
|
2012-09-23 09:27:09 +00:00
|
|
|
tdat->cancel = 1;
|
|
|
|
sem_post(&tdat->start_sem);
|
|
|
|
sem_post(&tdat->write_done_sem);
|
2012-05-28 14:49:29 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
sem_post(&tdat->write_done_sem);
|
|
|
|
}
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File compression routine. Can use as many threads as there are
|
|
|
|
* logical cores unless user specified something different. There is
|
|
|
|
* not much to gain from nthreads > n logical cores however.
|
|
|
|
*/
|
|
|
|
#define COMP_BAIL err = 1; goto comp_done
|
|
|
|
|
2012-11-24 18:23:07 +00:00
|
|
|
static int
|
2012-05-28 14:49:29 +00:00
|
|
|
start_compress(const char *filename, uint64_t chunksize, int level)
|
|
|
|
{
|
|
|
|
struct wdata w;
|
2012-06-29 12:53:55 +00:00
|
|
|
char tmpfile1[MAXPATHLEN];
|
2012-05-28 14:49:29 +00:00
|
|
|
char to_filename[MAXPATHLEN];
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t compressed_chunksize;
|
|
|
|
int64_t n_chunksize, rbytes, rabin_count;
|
2012-06-29 12:53:55 +00:00
|
|
|
short version, flags;
|
2012-05-28 14:49:29 +00:00
|
|
|
struct stat sbuf;
|
|
|
|
int compfd = -1, uncompfd = -1, err;
|
2012-08-18 16:30:14 +00:00
|
|
|
int i, thread, bail, single_chunk;
|
2012-05-28 14:49:29 +00:00
|
|
|
int nprocs, np, p;
|
|
|
|
struct cmp_data **dary = NULL, *tdat;
|
|
|
|
pthread_t writer_thr;
|
|
|
|
uchar_t *cread_buf, *pos;
|
2012-09-16 05:42:58 +00:00
|
|
|
dedupe_context_t *rctx;
|
2012-08-18 04:50:52 +00:00
|
|
|
algo_props_t props;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
/*
|
2012-07-27 16:33:24 +00:00
|
|
|
* Compressed buffer size must include zlib/dedup scratch space and
|
2012-05-28 14:49:29 +00:00
|
|
|
* chunk header space.
|
|
|
|
* See http://www.zlib.net/manual.html#compress2
|
|
|
|
*
|
|
|
|
* We do this unconditionally whether user mentioned zlib or not
|
|
|
|
* to keep it simple. While zlib scratch space is only needed at
|
|
|
|
* runtime, chunk header is stored in the file.
|
|
|
|
*
|
|
|
|
* See start_decompress() routine for details of chunk header.
|
|
|
|
* We also keep extra 8-byte space for the last chunk's size.
|
|
|
|
*/
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize = chunksize + CHUNK_HDR_SZ + zlib_buf_extra(chunksize);
|
2012-08-18 04:50:52 +00:00
|
|
|
init_algo_props(&props);
|
2012-07-27 16:33:24 +00:00
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
if (_props_func) {
|
|
|
|
_props_func(&props, level, chunksize);
|
|
|
|
if (chunksize + props.buf_extra > compressed_chunksize) {
|
|
|
|
compressed_chunksize += (chunksize + props.buf_extra -
|
2012-08-04 12:25:20 +00:00
|
|
|
compressed_chunksize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
flags = 0;
|
2012-09-15 05:44:58 +00:00
|
|
|
if (enable_rabin_scan || enable_fixed_scan) {
|
|
|
|
if (enable_rabin_scan)
|
|
|
|
flags |= FLAG_DEDUP;
|
|
|
|
else
|
|
|
|
flags |= FLAG_DEDUP_FIXED;
|
2012-07-27 16:33:24 +00:00
|
|
|
/* Additional scratch space for dedup arrays. */
|
2012-09-16 05:42:58 +00:00
|
|
|
compressed_chunksize += (dedupe_buf_extra(chunksize, 0, algo,
|
2012-08-23 17:28:44 +00:00
|
|
|
enable_delta_encode) - (compressed_chunksize - chunksize));
|
2012-07-27 16:33:24 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
if (encrypt_type) {
|
|
|
|
uchar_t pw[MAX_PW_LEN];
|
|
|
|
int pw_len;
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
compressed_chunksize += mac_bytes;
|
2012-10-15 06:40:00 +00:00
|
|
|
if (!pwd_file) {
|
|
|
|
pw_len = get_pw_string(pw,
|
2012-10-18 17:25:41 +00:00
|
|
|
"Please enter encryption password", 1);
|
2012-10-15 06:40:00 +00:00
|
|
|
if (pw_len == -1) {
|
2012-12-08 08:49:01 +00:00
|
|
|
err_exit(0, "Failed to get password.\n");
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int fd, len;
|
|
|
|
uchar_t zero[MAX_PW_LEN];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read password from a file and zero out the file after reading.
|
|
|
|
*/
|
|
|
|
memset(zero, 0, MAX_PW_LEN);
|
|
|
|
fd = open(pwd_file, O_RDWR);
|
|
|
|
if (fd != -1) {
|
|
|
|
pw_len = lseek(fd, 0, SEEK_END);
|
|
|
|
if (pw_len != -1) {
|
|
|
|
if (pw_len > MAX_PW_LEN) pw_len = MAX_PW_LEN-1;
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
len = Read(fd, pw, pw_len);
|
|
|
|
if (len != -1 && len == pw_len) {
|
|
|
|
pw[pw_len] = '\0';
|
|
|
|
if (isspace(pw[pw_len - 1]))
|
|
|
|
pw[pw_len-1] = '\0';
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
Write(fd, zero, pw_len);
|
|
|
|
} else {
|
|
|
|
pw_len = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pw_len == -1) {
|
|
|
|
err_exit(1, "Failed to get password.\n");
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
if (init_crypto(&crypto_ctx, pw, pw_len, encrypt_type, NULL,
|
|
|
|
0, 0, ENCRYPT_FLAG) == -1) {
|
|
|
|
memset(pw, 0, MAX_PW_LEN);
|
2012-12-08 08:49:01 +00:00
|
|
|
err_exit(0, "Failed to initialize crypto\n");
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
memset(pw, 0, MAX_PW_LEN);
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:33:24 +00:00
|
|
|
err = 0;
|
2012-06-29 12:53:55 +00:00
|
|
|
thread = 0;
|
2012-08-18 16:30:14 +00:00
|
|
|
single_chunk = 0;
|
2012-11-24 18:23:07 +00:00
|
|
|
rctx = NULL;
|
2012-06-29 12:53:55 +00:00
|
|
|
slab_cache_add(chunksize);
|
2012-08-31 17:06:06 +00:00
|
|
|
slab_cache_add(compressed_chunksize);
|
2012-06-29 12:53:55 +00:00
|
|
|
slab_cache_add(sizeof (struct cmp_data));
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
nprocs = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
if (nthreads > 0 && nthreads < nprocs)
|
|
|
|
nprocs = nthreads;
|
|
|
|
else
|
|
|
|
nthreads = nprocs;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/* A host of sanity checks. */
|
|
|
|
if (!pipe_mode) {
|
|
|
|
if ((uncompfd = open(filename, O_RDWR, 0)) == -1)
|
|
|
|
err_exit(1, "Cannot open: %s", filename);
|
|
|
|
|
|
|
|
if (fstat(uncompfd, &sbuf) == -1) {
|
|
|
|
close(uncompfd);
|
|
|
|
err_exit(1, "Cannot stat: %s", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISREG(sbuf.st_mode)) {
|
|
|
|
close(uncompfd);
|
|
|
|
err_exit(0, "File %s is not a regular file.\n", filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sbuf.st_size == 0) {
|
|
|
|
close(uncompfd);
|
2012-11-24 18:23:07 +00:00
|
|
|
return (1);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adjust chunk size for small files. We then get an archive with
|
|
|
|
* a single chunk for the entire file.
|
|
|
|
*/
|
2012-08-05 17:05:51 +00:00
|
|
|
if (sbuf.st_size <= chunksize) {
|
2012-05-28 14:49:29 +00:00
|
|
|
chunksize = sbuf.st_size;
|
2012-08-05 17:05:51 +00:00
|
|
|
enable_rabin_split = 0; // Do not split for whole files.
|
2012-07-01 16:14:02 +00:00
|
|
|
nthreads = 1;
|
2012-08-18 16:30:14 +00:00
|
|
|
single_chunk = 1;
|
2012-08-27 16:21:55 +00:00
|
|
|
props.is_single_chunk = 1;
|
|
|
|
flags |= FLAG_SINGLE_CHUNK;
|
2012-07-01 16:14:02 +00:00
|
|
|
} else {
|
2012-07-09 17:58:11 +00:00
|
|
|
if (nthreads == 0 || nthreads > sbuf.st_size / chunksize) {
|
2012-07-03 17:17:24 +00:00
|
|
|
nthreads = sbuf.st_size / chunksize;
|
|
|
|
if (sbuf.st_size % chunksize)
|
|
|
|
nthreads++;
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a temporary file to hold compressed data which is renamed at
|
|
|
|
* the end. The target file name is same as original file with the '.pz'
|
|
|
|
* extension appended.
|
|
|
|
*/
|
2012-06-29 12:53:55 +00:00
|
|
|
strcpy(tmpfile1, filename);
|
|
|
|
strcpy(tmpfile1, dirname(tmpfile1));
|
|
|
|
strcat(tmpfile1, "/.pcompXXXXXX");
|
2012-05-28 14:49:29 +00:00
|
|
|
snprintf(to_filename, sizeof (to_filename), "%s" COMP_EXTN, filename);
|
2012-06-29 12:53:55 +00:00
|
|
|
if ((compfd = mkstemp(tmpfile1)) == -1) {
|
2012-05-28 14:49:29 +00:00
|
|
|
perror("mkstemp ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Use stdin/stdout for pipe mode.
|
|
|
|
*/
|
|
|
|
compfd = fileno(stdout);
|
|
|
|
if (compfd == -1) {
|
|
|
|
perror("fileno ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
uncompfd = fileno(stdin);
|
|
|
|
if (uncompfd == -1) {
|
|
|
|
perror("fileno ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
if (encrypt_type)
|
|
|
|
flags |= encrypt_type;
|
|
|
|
|
2012-08-18 04:50:52 +00:00
|
|
|
set_threadcounts(&props, &nthreads, nprocs, COMPRESS_THREADS);
|
|
|
|
fprintf(stderr, "Scaling to %d thread", nthreads * props.nthreads);
|
2012-08-18 16:30:14 +00:00
|
|
|
if (nthreads * props.nthreads > 1) fprintf(stderr, "s");
|
|
|
|
nprocs = nthreads;
|
2012-07-01 16:14:02 +00:00
|
|
|
fprintf(stderr, "\n");
|
2012-05-31 07:36:40 +00:00
|
|
|
|
2012-07-31 15:37:35 +00:00
|
|
|
dary = (struct cmp_data **)slab_calloc(NULL, nprocs, sizeof (struct cmp_data *));
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan))
|
2012-08-31 17:06:06 +00:00
|
|
|
cread_buf = (uchar_t *)slab_alloc(NULL, compressed_chunksize);
|
2012-06-29 12:53:55 +00:00
|
|
|
else
|
|
|
|
cread_buf = (uchar_t *)slab_alloc(NULL, chunksize);
|
2012-05-28 14:49:29 +00:00
|
|
|
if (!cread_buf) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nprocs; i++) {
|
|
|
|
dary[i] = (struct cmp_data *)slab_alloc(NULL, sizeof (struct cmp_data));
|
|
|
|
if (!dary[i]) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
tdat = dary[i];
|
2012-08-18 16:30:14 +00:00
|
|
|
tdat->cmp_seg = NULL;
|
2012-05-28 14:49:29 +00:00
|
|
|
tdat->chunksize = chunksize;
|
|
|
|
tdat->compress = _compress_func;
|
|
|
|
tdat->decompress = _decompress_func;
|
2012-10-15 06:40:00 +00:00
|
|
|
tdat->uncompressed_chunk = (uchar_t *)1;
|
2012-05-28 14:49:29 +00:00
|
|
|
tdat->cancel = 0;
|
|
|
|
tdat->level = level;
|
2012-11-09 13:23:48 +00:00
|
|
|
tdat->data = NULL;
|
2012-12-04 18:39:47 +00:00
|
|
|
tdat->props = &props;
|
2012-05-28 14:49:29 +00:00
|
|
|
sem_init(&(tdat->start_sem), 0, 0);
|
|
|
|
sem_init(&(tdat->cmp_done_sem), 0, 0);
|
|
|
|
sem_init(&(tdat->write_done_sem), 0, 1);
|
2012-07-23 16:13:12 +00:00
|
|
|
if (_init_func) {
|
2012-11-22 15:32:50 +00:00
|
|
|
if (_init_func(&(tdat->data), &(tdat->level), props.nthreads, chunksize, VERSION, COMPRESS) != 0) {
|
2012-07-23 16:13:12 +00:00
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
2012-09-16 05:42:58 +00:00
|
|
|
if (enable_rabin_scan || enable_fixed_scan) {
|
|
|
|
tdat->rctx = create_dedupe_context(chunksize, compressed_chunksize, rab_blk_size,
|
2012-11-22 15:32:50 +00:00
|
|
|
algo, enable_delta_encode, enable_fixed_scan, VERSION, COMPRESS);
|
2012-08-10 05:17:11 +00:00
|
|
|
if (tdat->rctx == NULL) {
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
} else {
|
2012-06-29 12:53:55 +00:00
|
|
|
tdat->rctx = NULL;
|
2012-08-10 05:17:11 +00:00
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
if (encrypt_type) {
|
|
|
|
if (hmac_init(&tdat->chunk_hmac, cksum, &crypto_ctx) == -1) {
|
|
|
|
fprintf(stderr, "Cannot initialize chunk hmac.\n");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
if (pthread_create(&(tdat->thr), NULL, perform_compress,
|
|
|
|
(void *)tdat) != 0) {
|
|
|
|
perror("Error in thread creation: ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
thread = 1;
|
|
|
|
|
|
|
|
w.dary = dary;
|
|
|
|
w.wfd = compfd;
|
|
|
|
w.nprocs = nprocs;
|
|
|
|
if (pthread_create(&writer_thr, NULL, writer_thread, (void *)(&w)) != 0) {
|
|
|
|
perror("Error in thread creation: ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write out file header. First insert hdr elements into mem buffer
|
|
|
|
* then write out the full hdr in one shot.
|
|
|
|
*/
|
2012-08-31 17:06:06 +00:00
|
|
|
flags |= cksum;
|
2012-05-28 14:49:29 +00:00
|
|
|
memset(cread_buf, 0, ALGO_SZ);
|
|
|
|
strncpy(cread_buf, algo, ALGO_SZ);
|
2012-06-29 12:53:55 +00:00
|
|
|
version = htons(VERSION);
|
|
|
|
flags = htons(flags);
|
2012-05-28 14:49:29 +00:00
|
|
|
n_chunksize = htonll(chunksize);
|
|
|
|
level = htonl(level);
|
|
|
|
pos = cread_buf + ALGO_SZ;
|
|
|
|
memcpy(pos, &version, sizeof (version));
|
|
|
|
pos += sizeof (version);
|
2012-06-29 12:53:55 +00:00
|
|
|
memcpy(pos, &flags, sizeof (flags));
|
|
|
|
pos += sizeof (flags);
|
2012-05-28 14:49:29 +00:00
|
|
|
memcpy(pos, &n_chunksize, sizeof (n_chunksize));
|
|
|
|
pos += sizeof (n_chunksize);
|
|
|
|
memcpy(pos, &level, sizeof (level));
|
|
|
|
pos += sizeof (level);
|
|
|
|
if (Write(compfd, cread_buf, pos - cread_buf) != pos - cread_buf) {
|
|
|
|
perror("Write ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
/*
|
2012-10-17 18:02:35 +00:00
|
|
|
* If encryption is enabled, compute header HMAC. Then
|
|
|
|
* write the salt, nonce and header hmac in that order.
|
2012-10-15 06:40:00 +00:00
|
|
|
*/
|
|
|
|
if (encrypt_type) {
|
2012-10-17 18:02:35 +00:00
|
|
|
mac_ctx_t hdr_mac;
|
|
|
|
uchar_t hdr_hash[mac_bytes];
|
|
|
|
unsigned int hlen;
|
|
|
|
|
|
|
|
if (hmac_init(&hdr_mac, cksum, &crypto_ctx) == -1) {
|
|
|
|
fprintf(stderr, "Cannot initialize header hmac.\n");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
hmac_update(&hdr_mac, cread_buf, pos - cread_buf);
|
|
|
|
hmac_final(&hdr_mac, hdr_hash, &hlen);
|
|
|
|
hmac_cleanup(&hdr_mac);
|
|
|
|
|
2012-10-18 17:25:41 +00:00
|
|
|
/* Erase encryption key bytes stored as a plain array. No longer reqd. */
|
|
|
|
crypto_clean_pkey(&crypto_ctx);
|
|
|
|
|
2012-10-17 18:02:35 +00:00
|
|
|
pos = cread_buf;
|
2012-10-15 06:40:00 +00:00
|
|
|
*((int *)pos) = htonl(crypto_ctx.saltlen);
|
|
|
|
pos += sizeof (int);
|
|
|
|
serialize_checksum(crypto_ctx.salt, pos, crypto_ctx.saltlen);
|
|
|
|
pos += crypto_ctx.saltlen;
|
|
|
|
*((uint64_t *)pos) = htonll(crypto_nonce(&crypto_ctx));
|
|
|
|
pos += 8;
|
2012-10-17 18:02:35 +00:00
|
|
|
serialize_checksum(hdr_hash, pos, hlen);
|
|
|
|
pos += hlen;
|
2012-10-15 06:40:00 +00:00
|
|
|
if (Write(compfd, cread_buf, pos - cread_buf) != pos - cread_buf) {
|
|
|
|
perror("Write ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
2012-11-22 18:30:39 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Compute header CRC32 and store that. Only archive version 5 and above.
|
|
|
|
*/
|
|
|
|
uint32_t crc = lzma_crc32(cread_buf, pos - cread_buf, 0);
|
|
|
|
*((uint32_t *)cread_buf) = htonl(crc);
|
|
|
|
if (Write(compfd, cread_buf, sizeof (uint32_t)) != sizeof (uint32_t)) {
|
|
|
|
perror("Write ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
2012-10-15 06:40:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* Now read from the uncompressed file in 'chunksize' sized chunks, independently
|
|
|
|
* compress each chunk and write it out. Chunk sequencing is ensured.
|
|
|
|
*/
|
|
|
|
chunk_num = 0;
|
|
|
|
np = 0;
|
|
|
|
bail = 0;
|
2012-05-31 16:06:33 +00:00
|
|
|
largest_chunk = 0;
|
|
|
|
smallest_chunk = chunksize;
|
|
|
|
avg_chunk = 0;
|
2012-07-08 16:14:08 +00:00
|
|
|
rabin_count = 0;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the first chunk into a spare buffer (a simple double-buffering).
|
|
|
|
*/
|
2012-07-08 16:14:08 +00:00
|
|
|
if (enable_rabin_split) {
|
2012-09-16 05:42:58 +00:00
|
|
|
rctx = create_dedupe_context(chunksize, 0, 0, algo, enable_delta_encode,
|
2012-11-22 15:32:50 +00:00
|
|
|
enable_fixed_scan, VERSION, COMPRESS);
|
2012-07-08 16:14:08 +00:00
|
|
|
rbytes = Read_Adjusted(uncompfd, cread_buf, chunksize, &rabin_count, rctx);
|
|
|
|
} else {
|
|
|
|
rbytes = Read(uncompfd, cread_buf, chunksize);
|
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
while (!bail) {
|
|
|
|
uchar_t *tmp;
|
|
|
|
|
|
|
|
if (main_cancel) break;
|
|
|
|
for (p = 0; p < nprocs; p++) {
|
|
|
|
np = p;
|
|
|
|
tdat = dary[p];
|
|
|
|
if (main_cancel) break;
|
|
|
|
/* Wait for previous chunk compression to complete. */
|
|
|
|
sem_wait(&tdat->write_done_sem);
|
|
|
|
if (main_cancel) break;
|
|
|
|
|
2012-08-24 14:46:21 +00:00
|
|
|
if (rbytes == 0) { /* EOF */
|
|
|
|
bail = 1;
|
|
|
|
break;
|
|
|
|
}
|
2012-08-18 16:30:14 +00:00
|
|
|
/*
|
|
|
|
* Delayed allocation. Allocate chunks if not already done.
|
|
|
|
*/
|
|
|
|
if (!tdat->cmp_seg) {
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan)) {
|
2012-08-18 16:30:14 +00:00
|
|
|
if (single_chunk)
|
|
|
|
tdat->cmp_seg = (uchar_t *)1;
|
|
|
|
else
|
|
|
|
tdat->cmp_seg = (uchar_t *)slab_alloc(NULL,
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize);
|
2012-08-18 16:30:14 +00:00
|
|
|
tdat->uncompressed_chunk = (uchar_t *)slab_alloc(NULL,
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize);
|
2012-08-18 16:30:14 +00:00
|
|
|
} else {
|
|
|
|
if (single_chunk)
|
|
|
|
tdat->uncompressed_chunk = (uchar_t *)1;
|
|
|
|
else
|
|
|
|
tdat->uncompressed_chunk =
|
|
|
|
(uchar_t *)slab_alloc(NULL, chunksize);
|
|
|
|
tdat->cmp_seg = (uchar_t *)slab_alloc(NULL,
|
2012-08-31 17:06:06 +00:00
|
|
|
compressed_chunksize);
|
2012-08-18 16:30:14 +00:00
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
tdat->compressed_chunk = tdat->cmp_seg + COMPRESSED_CHUNKSZ +
|
|
|
|
cksum_bytes + mac_bytes;
|
2012-08-18 16:30:14 +00:00
|
|
|
if (!tdat->cmp_seg || !tdat->uncompressed_chunk) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* Once previous chunk is done swap already read buffer and
|
|
|
|
* it's size into the thread data.
|
2012-06-29 12:53:55 +00:00
|
|
|
* Normally it goes into uncompressed_chunk, because that's what it is.
|
|
|
|
* With dedup enabled however, we do some jugglery to save additional
|
|
|
|
* memory usage and avoid a memcpy, so it goes into the compressed_chunk
|
|
|
|
* area:
|
|
|
|
* cmp_seg -> dedup -> uncompressed_chunk -> compression -> cmp_seg
|
2012-05-28 14:49:29 +00:00
|
|
|
*/
|
|
|
|
tdat->id = chunk_num;
|
|
|
|
tdat->rbytes = rbytes;
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan)) {
|
2012-06-29 12:53:55 +00:00
|
|
|
tmp = tdat->cmp_seg;
|
|
|
|
tdat->cmp_seg = cread_buf;
|
|
|
|
cread_buf = tmp;
|
2012-10-17 18:02:35 +00:00
|
|
|
tdat->compressed_chunk = tdat->cmp_seg + COMPRESSED_CHUNKSZ +
|
|
|
|
cksum_bytes + mac_bytes;
|
2012-07-08 16:14:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is data after the last rabin boundary in the chunk, then
|
|
|
|
* rabin_count will be non-zero. We carry over the data to the beginning
|
|
|
|
* of the next chunk.
|
|
|
|
*/
|
|
|
|
if (rabin_count) {
|
|
|
|
memcpy(cread_buf,
|
|
|
|
tdat->cmp_seg + rabin_count, rbytes - rabin_count);
|
|
|
|
tdat->rbytes = rabin_count;
|
|
|
|
rabin_count = rbytes - rabin_count;
|
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
} else {
|
|
|
|
tmp = tdat->uncompressed_chunk;
|
|
|
|
tdat->uncompressed_chunk = cread_buf;
|
|
|
|
cread_buf = tmp;
|
2012-06-21 14:57:05 +00:00
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
if (rbytes < chunksize) {
|
|
|
|
if (rbytes < 0) {
|
2012-06-21 14:57:05 +00:00
|
|
|
bail = 1;
|
2012-05-28 14:49:29 +00:00
|
|
|
perror("Read: ");
|
|
|
|
COMP_BAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Signal the compression thread to start */
|
|
|
|
sem_post(&tdat->start_sem);
|
2012-06-21 14:57:05 +00:00
|
|
|
chunk_num++;
|
|
|
|
|
2012-08-18 16:30:14 +00:00
|
|
|
if (single_chunk) {
|
|
|
|
rbytes = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* Read the next buffer we want to process while previous
|
|
|
|
* buffer is in progress.
|
|
|
|
*/
|
2012-07-08 16:14:08 +00:00
|
|
|
if (enable_rabin_split) {
|
|
|
|
rbytes = Read_Adjusted(uncompfd, cread_buf, chunksize, &rabin_count, rctx);
|
|
|
|
} else {
|
|
|
|
rbytes = Read(uncompfd, cread_buf, chunksize);
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!main_cancel) {
|
|
|
|
/* Wait for all remaining chunks to finish. */
|
|
|
|
for (p = 0; p < nprocs; p++) {
|
|
|
|
if (p == np) continue;
|
|
|
|
tdat = dary[p];
|
|
|
|
sem_wait(&tdat->write_done_sem);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
comp_done:
|
2012-11-25 09:42:45 +00:00
|
|
|
if (t_errored) err = t_errored;
|
2012-05-28 14:49:29 +00:00
|
|
|
if (thread) {
|
|
|
|
for (i = 0; i < nprocs; i++) {
|
|
|
|
tdat = dary[i];
|
|
|
|
tdat->cancel = 1;
|
|
|
|
tdat->len_cmp = 0;
|
|
|
|
sem_post(&tdat->start_sem);
|
|
|
|
sem_post(&tdat->cmp_done_sem);
|
|
|
|
pthread_join(tdat->thr, NULL);
|
2012-10-18 17:25:41 +00:00
|
|
|
if (encrypt_type)
|
|
|
|
hmac_cleanup(&tdat->chunk_hmac);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
pthread_join(writer_thr, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
if (compfd != -1 && !pipe_mode)
|
2012-06-29 12:53:55 +00:00
|
|
|
unlink(tmpfile1);
|
2012-07-31 15:37:35 +00:00
|
|
|
if (filename)
|
|
|
|
fprintf(stderr, "Error compressing file: %s\n", filename);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Error compressing\n");
|
2012-05-28 14:49:29 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Write a trailer of zero chunk length.
|
|
|
|
*/
|
|
|
|
compressed_chunksize = 0;
|
|
|
|
if (Write(compfd, &compressed_chunksize,
|
|
|
|
sizeof (compressed_chunksize)) < 0) {
|
|
|
|
perror("Write ");
|
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rename the temporary file to the actual compressed file
|
|
|
|
* unless we are in a pipe.
|
|
|
|
*/
|
|
|
|
if (!pipe_mode) {
|
|
|
|
/*
|
|
|
|
* Ownership and mode of target should be same as original.
|
|
|
|
*/
|
|
|
|
fchmod(compfd, sbuf.st_mode);
|
|
|
|
if (fchown(compfd, sbuf.st_uid, sbuf.st_gid) == -1)
|
|
|
|
perror("chown ");
|
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
if (rename(tmpfile1, to_filename) == -1) {
|
2012-05-28 14:49:29 +00:00
|
|
|
perror("Cannot rename temporary file ");
|
2012-06-29 12:53:55 +00:00
|
|
|
unlink(tmpfile1);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dary != NULL) {
|
|
|
|
for (i = 0; i < nprocs; i++) {
|
2012-07-31 15:37:35 +00:00
|
|
|
if (!dary[i]) continue;
|
2012-08-18 16:30:14 +00:00
|
|
|
if (dary[i]->uncompressed_chunk != (uchar_t *)1)
|
|
|
|
slab_free(NULL, dary[i]->uncompressed_chunk);
|
|
|
|
if (dary[i]->cmp_seg != (uchar_t *)1)
|
|
|
|
slab_free(NULL, dary[i]->cmp_seg);
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan)) {
|
|
|
|
destroy_dedupe_context(dary[i]->rctx);
|
2012-06-29 12:53:55 +00:00
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
if (_deinit_func)
|
|
|
|
_deinit_func(&(dary[i]->data));
|
|
|
|
slab_free(NULL, dary[i]);
|
|
|
|
}
|
|
|
|
slab_free(NULL, dary);
|
|
|
|
}
|
2012-09-16 05:42:58 +00:00
|
|
|
if (enable_rabin_split) destroy_dedupe_context(rctx);
|
2012-08-18 16:30:14 +00:00
|
|
|
if (cread_buf != (uchar_t *)1)
|
|
|
|
slab_free(NULL, cread_buf);
|
2012-05-28 14:49:29 +00:00
|
|
|
if (!pipe_mode) {
|
|
|
|
if (compfd != -1) close(compfd);
|
|
|
|
if (uncompfd != -1) close(uncompfd);
|
|
|
|
}
|
|
|
|
|
2012-05-31 16:06:33 +00:00
|
|
|
if (!hide_cmp_stats) show_compression_stats(chunksize);
|
|
|
|
_stats_func(!hide_cmp_stats);
|
|
|
|
slab_cleanup(hide_mem_stats);
|
2012-11-24 18:23:07 +00:00
|
|
|
|
|
|
|
return (err);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the algorithm requested and set the callback routine pointers.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
init_algo(const char *algo, int bail)
|
|
|
|
{
|
|
|
|
int rv = 1, i;
|
|
|
|
char algorithm[8];
|
|
|
|
|
|
|
|
/* Copy given string into known length buffer to avoid memcmp() overruns. */
|
|
|
|
strncpy(algorithm, algo, 8);
|
2012-08-18 04:50:52 +00:00
|
|
|
_props_func = NULL;
|
2012-05-28 14:49:29 +00:00
|
|
|
if (memcmp(algorithm, "zlib", 4) == 0) {
|
|
|
|
_compress_func = zlib_compress;
|
|
|
|
_decompress_func = zlib_decompress;
|
|
|
|
_init_func = zlib_init;
|
2012-08-08 17:10:58 +00:00
|
|
|
_deinit_func = zlib_deinit;
|
2012-05-31 16:06:33 +00:00
|
|
|
_stats_func = zlib_stats;
|
2012-12-04 18:39:47 +00:00
|
|
|
_props_func = zlib_props;
|
2012-05-28 14:49:29 +00:00
|
|
|
rv = 0;
|
|
|
|
|
2012-08-18 16:30:14 +00:00
|
|
|
} else if (memcmp(algorithm, "lzmaMt", 6) == 0) {
|
|
|
|
_compress_func = lzma_compress;
|
|
|
|
_decompress_func = lzma_decompress;
|
|
|
|
_init_func = lzma_init;
|
|
|
|
_deinit_func = lzma_deinit;
|
|
|
|
_stats_func = lzma_stats;
|
|
|
|
_props_func = lzma_mt_props;
|
|
|
|
rv = 0;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
} else if (memcmp(algorithm, "lzma", 4) == 0) {
|
|
|
|
_compress_func = lzma_compress;
|
|
|
|
_decompress_func = lzma_decompress;
|
|
|
|
_init_func = lzma_init;
|
|
|
|
_deinit_func = lzma_deinit;
|
2012-05-31 16:06:33 +00:00
|
|
|
_stats_func = lzma_stats;
|
2012-08-18 04:50:52 +00:00
|
|
|
_props_func = lzma_props;
|
2012-05-28 14:49:29 +00:00
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
} else if (memcmp(algorithm, "bzip2", 5) == 0) {
|
|
|
|
_compress_func = bzip2_compress;
|
|
|
|
_decompress_func = bzip2_decompress;
|
|
|
|
_init_func = bzip2_init;
|
|
|
|
_deinit_func = NULL;
|
2012-05-31 16:06:33 +00:00
|
|
|
_stats_func = bzip2_stats;
|
2012-12-04 18:39:47 +00:00
|
|
|
_props_func = bzip2_props;
|
2012-05-28 14:49:29 +00:00
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
} else if (memcmp(algorithm, "ppmd", 4) == 0) {
|
|
|
|
_compress_func = ppmd_compress;
|
|
|
|
_decompress_func = ppmd_decompress;
|
|
|
|
_init_func = ppmd_init;
|
|
|
|
_deinit_func = ppmd_deinit;
|
2012-05-31 16:06:33 +00:00
|
|
|
_stats_func = ppmd_stats;
|
2012-12-04 18:39:47 +00:00
|
|
|
_props_func = ppmd_props;
|
2012-05-28 14:49:29 +00:00
|
|
|
rv = 0;
|
|
|
|
|
2012-07-22 18:45:08 +00:00
|
|
|
} else if (memcmp(algorithm, "lzfx", 4) == 0) {
|
|
|
|
_compress_func = lz_fx_compress;
|
|
|
|
_decompress_func = lz_fx_decompress;
|
|
|
|
_init_func = lz_fx_init;
|
2012-07-23 16:13:12 +00:00
|
|
|
_deinit_func = lz_fx_deinit;
|
2012-07-22 18:45:08 +00:00
|
|
|
_stats_func = lz_fx_stats;
|
2012-12-04 18:39:47 +00:00
|
|
|
_props_func = lz_fx_props;
|
2012-07-22 18:45:08 +00:00
|
|
|
rv = 0;
|
|
|
|
|
2012-07-25 15:37:36 +00:00
|
|
|
} else if (memcmp(algorithm, "lz4", 3) == 0) {
|
|
|
|
_compress_func = lz4_compress;
|
|
|
|
_decompress_func = lz4_decompress;
|
|
|
|
_init_func = lz4_init;
|
|
|
|
_deinit_func = lz4_deinit;
|
|
|
|
_stats_func = lz4_stats;
|
2012-08-18 04:50:52 +00:00
|
|
|
_props_func = lz4_props;
|
2012-07-25 15:37:36 +00:00
|
|
|
rv = 0;
|
|
|
|
|
2012-08-05 17:05:51 +00:00
|
|
|
} else if (memcmp(algorithm, "none", 4) == 0) {
|
|
|
|
_compress_func = none_compress;
|
|
|
|
_decompress_func = none_decompress;
|
|
|
|
_init_func = none_init;
|
|
|
|
_deinit_func = none_deinit;
|
|
|
|
_stats_func = none_stats;
|
|
|
|
rv = 0;
|
|
|
|
|
2012-06-29 12:53:55 +00:00
|
|
|
/* adapt2 and adapt ordering of the checks matter here. */
|
2012-05-28 14:49:29 +00:00
|
|
|
} else if (memcmp(algorithm, "adapt2", 6) == 0) {
|
|
|
|
_compress_func = adapt_compress;
|
|
|
|
_decompress_func = adapt_decompress;
|
|
|
|
_init_func = adapt2_init;
|
|
|
|
_deinit_func = adapt_deinit;
|
2012-05-31 16:06:33 +00:00
|
|
|
_stats_func = adapt_stats;
|
2012-12-04 18:39:47 +00:00
|
|
|
_props_func = adapt_props;
|
2012-05-28 14:49:29 +00:00
|
|
|
adapt_mode = 1;
|
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
} else if (memcmp(algorithm, "adapt", 5) == 0) {
|
|
|
|
_compress_func = adapt_compress;
|
|
|
|
_decompress_func = adapt_decompress;
|
|
|
|
_init_func = adapt_init;
|
|
|
|
_deinit_func = adapt_deinit;
|
2012-05-31 16:06:33 +00:00
|
|
|
_stats_func = adapt_stats;
|
2012-12-04 18:39:47 +00:00
|
|
|
_props_func = adapt_props;
|
2012-05-28 14:49:29 +00:00
|
|
|
adapt_mode = 1;
|
|
|
|
rv = 0;
|
2012-08-27 16:21:55 +00:00
|
|
|
#ifdef ENABLE_PC_LIBBSC
|
|
|
|
} else if (memcmp(algorithm, "libbsc", 6) == 0) {
|
|
|
|
_compress_func = libbsc_compress;
|
|
|
|
_decompress_func = libbsc_decompress;
|
|
|
|
_init_func = libbsc_init;
|
|
|
|
_deinit_func = libbsc_deinit;
|
|
|
|
_stats_func = libbsc_stats;
|
|
|
|
_props_func = libbsc_props;
|
|
|
|
adapt_mode = 1;
|
|
|
|
rv = 0;
|
|
|
|
#endif
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *filename = NULL;
|
|
|
|
char *to_filename = NULL;
|
2012-12-09 04:45:06 +00:00
|
|
|
int64_t chunksize = DEFAULT_CHUNKSIZE;
|
2012-11-24 18:23:07 +00:00
|
|
|
int opt, level, num_rem, err;
|
2012-05-28 14:49:29 +00:00
|
|
|
|
|
|
|
exec_name = get_execname(argv[0]);
|
|
|
|
level = 6;
|
|
|
|
slab_init();
|
|
|
|
|
2012-12-04 18:39:47 +00:00
|
|
|
while ((opt = getopt(argc, argv, "dc:s:l:pt:MCDEew:rLPS:B:F")) != -1) {
|
2012-05-28 14:49:29 +00:00
|
|
|
int ovr;
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
|
|
|
do_uncompress = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
do_compress = 1;
|
|
|
|
algo = optarg;
|
|
|
|
if (init_algo(algo, 1) != 0) {
|
|
|
|
err_exit(1, "Invalid algorithm %s\n", optarg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
ovr = parse_numeric(&chunksize, optarg);
|
|
|
|
if (ovr == 1)
|
|
|
|
err_exit(0, "Chunk size too large %s", optarg);
|
|
|
|
else if (ovr == 2)
|
|
|
|
err_exit(0, "Invalid number %s", optarg);
|
|
|
|
|
|
|
|
if (chunksize < MIN_CHUNK) {
|
|
|
|
err_exit(0, "Minimum chunk size is %ld\n", MIN_CHUNK);
|
|
|
|
}
|
2012-10-17 18:02:35 +00:00
|
|
|
if (chunksize > EIGHTY_PCT(get_total_ram())) {
|
|
|
|
err_exit(0, "Chunk size must not exceed 80%% of total RAM.\n");
|
|
|
|
}
|
2012-05-28 14:49:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
level = atoi(optarg);
|
2012-10-17 18:02:35 +00:00
|
|
|
if (level < 0 || level > MAX_LEVEL)
|
2012-05-28 14:49:29 +00:00
|
|
|
err_exit(0, "Compression level should be in range 0 - 14\n");
|
|
|
|
break;
|
|
|
|
|
2012-09-05 17:13:54 +00:00
|
|
|
case 'B':
|
|
|
|
rab_blk_size = atoi(optarg);
|
|
|
|
if (rab_blk_size < 1 || rab_blk_size > 5)
|
2012-09-19 14:59:44 +00:00
|
|
|
err_exit(0, "Average Dedupe block size must be in range 1 (4k) - 5 (64k)\n");
|
2012-09-05 17:13:54 +00:00
|
|
|
break;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
case 'p':
|
|
|
|
pipe_mode = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
nthreads = atoi(optarg);
|
|
|
|
if (nthreads < 1 || nthreads > 256)
|
|
|
|
err_exit(0, "Thread count should be in range 1 - 256\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
2012-05-31 16:06:33 +00:00
|
|
|
hide_mem_stats = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
hide_cmp_stats = 0;
|
2012-05-28 14:49:29 +00:00
|
|
|
break;
|
|
|
|
|
2012-06-29 18:15:06 +00:00
|
|
|
case 'D':
|
|
|
|
enable_rabin_scan = 1;
|
|
|
|
break;
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-07-19 16:11:07 +00:00
|
|
|
case 'E':
|
|
|
|
enable_rabin_scan = 1;
|
2012-09-24 16:50:27 +00:00
|
|
|
if (!enable_delta_encode)
|
|
|
|
enable_delta_encode = DELTA_NORMAL;
|
|
|
|
else
|
|
|
|
enable_delta_encode = DELTA_EXTRA;
|
2012-07-19 16:11:07 +00:00
|
|
|
break;
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
case 'e':
|
|
|
|
encrypt_type = CRYPTO_ALG_AES;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
pwd_file = strdup(optarg);
|
|
|
|
break;
|
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
case 'F':
|
2012-09-15 05:44:58 +00:00
|
|
|
enable_fixed_scan = 1;
|
2012-09-16 05:42:58 +00:00
|
|
|
enable_rabin_split = 0;
|
2012-09-15 05:44:58 +00:00
|
|
|
break;
|
|
|
|
|
2012-08-23 17:28:44 +00:00
|
|
|
case 'L':
|
|
|
|
lzp_preprocess = 1;
|
|
|
|
break;
|
|
|
|
|
2012-12-04 18:39:47 +00:00
|
|
|
case 'P':
|
|
|
|
lzp_preprocess = 1;
|
|
|
|
enable_delta2_encode = 1;
|
|
|
|
break;
|
|
|
|
|
2012-07-08 16:14:08 +00:00
|
|
|
case 'r':
|
|
|
|
enable_rabin_split = 0;
|
|
|
|
break;
|
|
|
|
|
2012-08-31 17:06:06 +00:00
|
|
|
case 'S':
|
2012-10-17 18:02:35 +00:00
|
|
|
if (get_checksum_props(optarg, &cksum, &cksum_bytes, &mac_bytes) == -1) {
|
2012-08-31 17:06:06 +00:00
|
|
|
err_exit(0, "Invalid checksum type %s", optarg);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((do_compress && do_uncompress) || (!do_compress && !do_uncompress)) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remaining mandatory arguments are the filenames.
|
|
|
|
*/
|
|
|
|
num_rem = argc - optind;
|
|
|
|
if (pipe_mode && num_rem > 0 ) {
|
|
|
|
fprintf(stderr, "Filename(s) unexpected for pipe mode\n");
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
if ((enable_rabin_scan || enable_fixed_scan) && !do_compress) {
|
|
|
|
fprintf(stderr, "Deduplication is only used during compression.\n");
|
2012-06-21 14:57:05 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-07-08 17:01:09 +00:00
|
|
|
if (!enable_rabin_scan)
|
|
|
|
enable_rabin_split = 0;
|
2012-06-21 14:57:05 +00:00
|
|
|
|
2012-09-16 05:42:58 +00:00
|
|
|
if (enable_fixed_scan && (enable_rabin_scan || enable_delta_encode || enable_rabin_split)) {
|
2012-09-15 05:44:58 +00:00
|
|
|
fprintf(stderr, "Rabin Deduplication and Fixed block Deduplication are mutually exclusive\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
if (!do_compress && encrypt_type) {
|
|
|
|
fprintf(stderr, "Encryption only makes sense when compressing!\n");
|
|
|
|
exit(1);
|
2012-10-17 18:02:35 +00:00
|
|
|
|
2012-11-09 14:35:13 +00:00
|
|
|
} else if (pipe_mode && encrypt_type && !pwd_file) {
|
2012-10-15 06:40:00 +00:00
|
|
|
fprintf(stderr, "Pipe mode requires password to be provided in a file.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
if (num_rem == 0 && !pipe_mode) {
|
|
|
|
usage(); /* At least 1 filename needed. */
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
} else if (num_rem == 1) {
|
|
|
|
if (do_compress) {
|
|
|
|
char apath[MAXPATHLEN];
|
|
|
|
|
|
|
|
if ((filename = realpath(argv[optind], NULL)) == NULL)
|
|
|
|
err_exit(1, "%s", argv[optind]);
|
|
|
|
/* Check if compressed file exists */
|
|
|
|
strcpy(apath, filename);
|
|
|
|
strcat(apath, COMP_EXTN);
|
|
|
|
if ((to_filename = realpath(apath, NULL)) != NULL) {
|
|
|
|
free(filename);
|
|
|
|
err_exit(0, "Compressed file %s exists\n", to_filename);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else if (num_rem == 2) {
|
|
|
|
if (do_uncompress) {
|
|
|
|
if ((filename = realpath(argv[optind], NULL)) == NULL)
|
|
|
|
err_exit(1, "%s", argv[optind]);
|
|
|
|
optind++;
|
|
|
|
if ((to_filename = realpath(argv[optind], NULL)) != NULL) {
|
|
|
|
free(filename);
|
|
|
|
free(to_filename);
|
|
|
|
err_exit(0, "File %s exists\n", argv[optind]);
|
|
|
|
}
|
|
|
|
to_filename = argv[optind];
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-06-29 12:53:55 +00:00
|
|
|
} else if (num_rem > 2) {
|
|
|
|
fprintf(stderr, "Too many filenames.\n");
|
2012-05-28 14:49:29 +00:00
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
main_cancel = 0;
|
|
|
|
|
2012-08-31 17:06:06 +00:00
|
|
|
if (cksum == 0)
|
2012-10-17 18:02:35 +00:00
|
|
|
get_checksum_props(DEFAULT_CKSUM, &cksum, &cksum_bytes, &mac_bytes);
|
2012-10-15 06:40:00 +00:00
|
|
|
|
2012-11-23 16:57:14 +00:00
|
|
|
if (!encrypt_type) {
|
|
|
|
/*
|
|
|
|
* If not encrypting we compute a header CRC32.
|
|
|
|
*/
|
2012-11-22 18:30:39 +00:00
|
|
|
mac_bytes = sizeof (uint32_t); // CRC32 in non-crypto mode
|
2012-11-23 16:57:14 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* When encrypting we do not compute a normal digest. The HMAC
|
|
|
|
* is computed over header and encrypted data.
|
|
|
|
*/
|
|
|
|
cksum_bytes = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-28 14:49:29 +00:00
|
|
|
/*
|
|
|
|
* Start the main routines.
|
|
|
|
*/
|
|
|
|
if (do_compress)
|
2012-11-24 18:23:07 +00:00
|
|
|
err = start_compress(filename, chunksize, level);
|
2012-05-28 14:49:29 +00:00
|
|
|
else if (do_uncompress)
|
2012-11-24 18:23:07 +00:00
|
|
|
err = start_decompress(filename, to_filename);
|
2012-05-28 14:49:29 +00:00
|
|
|
|
2012-10-15 06:40:00 +00:00
|
|
|
if (pwd_file)
|
|
|
|
free(pwd_file);
|
2012-05-28 14:49:29 +00:00
|
|
|
free(filename);
|
|
|
|
free((void *)exec_name);
|
2012-11-24 18:23:07 +00:00
|
|
|
return (err);
|
2012-05-28 14:49:29 +00:00
|
|
|
}
|