|
Packit Service |
5195f2 |
/* overlap.c -- example program: overlapping (de)compression
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
This file is part of the LZO real-time data compression library.
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer
|
|
Packit Service |
5195f2 |
All Rights Reserved.
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
The LZO library is free software; you can redistribute it and/or
|
|
Packit Service |
5195f2 |
modify it under the terms of the GNU General Public License as
|
|
Packit Service |
5195f2 |
published by the Free Software Foundation; either version 2 of
|
|
Packit Service |
5195f2 |
the License, or (at your option) any later version.
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
The LZO library is distributed in the hope that it will be useful,
|
|
Packit Service |
5195f2 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
5195f2 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
5195f2 |
GNU General Public License for more details.
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
5195f2 |
along with the LZO library; see the file COPYING.
|
|
Packit Service |
5195f2 |
If not, write to the Free Software Foundation, Inc.,
|
|
Packit Service |
5195f2 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
Markus F.X.J. Oberhumer
|
|
Packit Service |
5195f2 |
<markus@oberhumer.com>
|
|
Packit Service |
5195f2 |
http://www.oberhumer.com/opensource/lzo/
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*************************************************************************
|
|
Packit Service |
5195f2 |
// This program shows how to do overlapping compression and
|
|
Packit Service |
5195f2 |
// in-place decompression.
|
|
Packit Service |
5195f2 |
//
|
|
Packit Service |
5195f2 |
// Please study LZO.FAQ and simple.c first.
|
|
Packit Service |
5195f2 |
**************************************************************************/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
#include "lzo/lzoconf.h"
|
|
Packit Service |
5195f2 |
#include "lzo/lzo1x.h"
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/* portability layer */
|
|
Packit Service |
5195f2 |
static const char *progname = NULL;
|
|
Packit Service |
5195f2 |
#define WANT_LZO_MALLOC 1
|
|
Packit Service |
5195f2 |
#define WANT_LZO_FREAD 1
|
|
Packit Service |
5195f2 |
#define WANT_LZO_WILDARGV 1
|
|
Packit Service |
5195f2 |
#define WANT_XMALLOC 1
|
|
Packit Service |
5195f2 |
#include "examples/portab.h"
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/* Overhead (in bytes) for the in-place decompression buffer.
|
|
Packit Service |
5195f2 |
* Most files need only 16 !
|
|
Packit Service |
5195f2 |
* (try 'overlap -16 file' or even 'overlap -8 file')
|
|
Packit Service |
5195f2 |
*
|
|
Packit Service |
5195f2 |
* Worst case (for files that are compressible by only a few bytes)
|
|
Packit Service |
5195f2 |
* is 'in_len / 16 + 64 + 3'. See step 5a) below.
|
|
Packit Service |
5195f2 |
*
|
|
Packit Service |
5195f2 |
* For overlapping compression '0xbfff + in_len / 16 + 64 + 3' bytes
|
|
Packit Service |
5195f2 |
* will be needed. See step 4a) below.
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
static long opt_overhead = 0; /* assume worst case */
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
static unsigned long total_files = 0;
|
|
Packit Service |
5195f2 |
static unsigned long total_in = 0;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*************************************************************************
|
|
Packit Service |
5195f2 |
//
|
|
Packit Service |
5195f2 |
**************************************************************************/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
static int do_file(const char *in_name)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
int r;
|
|
Packit Service |
5195f2 |
FILE *fp = NULL;
|
|
Packit Service |
5195f2 |
long l;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
lzo_voidp wrkmem = NULL;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
lzo_bytep in = NULL;
|
|
Packit Service |
5195f2 |
lzo_uint in_len; /* uncompressed length */
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
lzo_bytep out = NULL;
|
|
Packit Service |
5195f2 |
lzo_uint out_len; /* compressed length */
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
lzo_bytep overlap = NULL;
|
|
Packit Service |
5195f2 |
lzo_uint overhead;
|
|
Packit Service |
5195f2 |
lzo_uint offset;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
lzo_uint new_len = 0;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 1: open the input file
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
fp = fopen(in_name, "rb");
|
|
Packit Service |
5195f2 |
if (fp == NULL)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
printf("%s: %s: cannot open file\n", progname, in_name);
|
|
Packit Service |
5195f2 |
goto next_file;
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
fseek(fp, 0, SEEK_END);
|
|
Packit Service |
5195f2 |
l = ftell(fp);
|
|
Packit Service |
5195f2 |
fseek(fp, 0, SEEK_SET);
|
|
Packit Service |
5195f2 |
if (l <= 0)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
printf("%s: %s: empty file -- skipping\n", progname, in_name);
|
|
Packit Service |
5195f2 |
goto next_file;
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
in_len = (lzo_uint) l;
|
|
Packit Service |
5195f2 |
if ((long) in_len != l || l > 256L * 1024L * 1024L)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
printf("%s: %s: file is too big -- skipping\n", progname, in_name);
|
|
Packit Service |
5195f2 |
goto next_file;
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 2: allocate compression buffers and read the file
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
in = (lzo_bytep) xmalloc(in_len);
|
|
Packit Service |
5195f2 |
out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3);
|
|
Packit Service |
5195f2 |
wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
|
|
Packit Service |
5195f2 |
in_len = (lzo_uint) lzo_fread(fp, in, in_len);
|
|
Packit Service |
5195f2 |
fclose(fp); fp = NULL;
|
|
Packit Service |
5195f2 |
printf("%s: %s: read %lu bytes\n", progname, in_name, (unsigned long) in_len);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
total_files++;
|
|
Packit Service |
5195f2 |
total_in += (unsigned long) in_len;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 3: compress from 'in' to 'out' with LZO1X-1
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
r = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
|
|
Packit Service |
5195f2 |
if (r != LZO_E_OK || out_len > in_len + in_len / 16 + 64 + 3)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
/* this should NEVER happen */
|
|
Packit Service |
5195f2 |
printf("internal error - compression failed: %d\n", r);
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
printf("%-25s %8lu -> %8lu\n", "LZO1X-1:", (unsigned long) in_len, (unsigned long) out_len);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/***** Step 4: overlapping compression *****/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 4a: allocate the 'overlap' buffer for overlapping compression
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
overhead = in_len > 0xbfff ? 0xbfff : in_len;
|
|
Packit Service |
5195f2 |
overhead += in_len / 16 + 64 + 3;
|
|
Packit Service |
5195f2 |
overlap = (lzo_bytep) xmalloc(in_len + overhead);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 4b: prepare data in 'overlap' buffer.
|
|
Packit Service |
5195f2 |
* copy uncompressed data at the top of the overlap buffer
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
/*** offset = in_len + overhead - in_len; ***/
|
|
Packit Service |
5195f2 |
offset = overhead;
|
|
Packit Service |
5195f2 |
lzo_memcpy(overlap + offset, in, in_len);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 4c: do an in-place compression within the 'overlap' buffer
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
r = lzo1x_1_compress(overlap + offset, in_len, overlap, &new_len, wrkmem);
|
|
Packit Service |
5195f2 |
if (r != LZO_E_OK)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
/* this should NEVER happen */
|
|
Packit Service |
5195f2 |
printf("in-place compression failed: %d\n", r);
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 4d: verify overlapping compression
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
if (new_len != out_len || lzo_memcmp(out, overlap, out_len) != 0)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
/* As compression is non-deterministic there can be a difference
|
|
Packit Service |
5195f2 |
* in the representation of the compressed data (but this usually
|
|
Packit Service |
5195f2 |
* happens very seldom). So we have to verify the overlapping
|
|
Packit Service |
5195f2 |
* compression by doing a temporary decompression.
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
lzo_uint ll = in_len;
|
|
Packit Service |
5195f2 |
lzo_bytep tmp = (lzo_bytep) xmalloc(ll);
|
|
Packit Service |
5195f2 |
r = lzo1x_decompress_safe(overlap, new_len, tmp, &ll, NULL);
|
|
Packit Service |
5195f2 |
if (r != LZO_E_OK || ll != in_len || lzo_memcmp(in, tmp, ll) != 0)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
/* this should NEVER happen */
|
|
Packit Service |
5195f2 |
printf("in-place compression data error\n");
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
lzo_free(tmp);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
printf(" in-place compression: %8lu -> %8lu overhead: %7lu\n",
|
|
Packit Service |
5195f2 |
(unsigned long) in_len, (unsigned long) new_len, (unsigned long) overhead);
|
|
Packit Service |
5195f2 |
lzo_free(overlap); overlap = NULL;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/***** Step 5: in-place decompression *****/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 5a: allocate the 'overlap' buffer for in-place decompression
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
if (opt_overhead == 0 || out_len >= in_len)
|
|
Packit Service |
5195f2 |
overhead = in_len / 16 + 64 + 3;
|
|
Packit Service |
5195f2 |
else
|
|
Packit Service |
5195f2 |
overhead = (lzo_uint) opt_overhead;
|
|
Packit Service |
5195f2 |
overlap = (lzo_bytep) xmalloc(in_len + overhead);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 5b: prepare data in 'overlap' buffer.
|
|
Packit Service |
5195f2 |
* copy compressed data at the top of the overlap buffer
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
offset = in_len + overhead - out_len;
|
|
Packit Service |
5195f2 |
lzo_memcpy(overlap + offset, out, out_len);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 5c: do an in-place decompression within the 'overlap' buffer
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
new_len = in_len;
|
|
Packit Service |
5195f2 |
r = lzo1x_decompress_safe(overlap + offset, out_len, overlap, &new_len, NULL);
|
|
Packit Service |
5195f2 |
if (r != LZO_E_OK)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
/* this may happen if overhead is too small */
|
|
Packit Service |
5195f2 |
printf("in-place decompression failed: %d - increase 'opt_overhead'\n", r);
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 5d: verify decompression
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
if (new_len != in_len || lzo_memcmp(in, overlap, in_len) != 0)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
/* this may happen if overhead is too small */
|
|
Packit Service |
5195f2 |
printf("in-place decompression data error - increase 'opt_overhead'\n");
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
printf(" in-place decompression: %8lu -> %8lu overhead: %7lu\n",
|
|
Packit Service |
5195f2 |
(unsigned long) out_len, (unsigned long) new_len, (unsigned long) overhead);
|
|
Packit Service |
5195f2 |
lzo_free(overlap); overlap = NULL;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
next_file:
|
|
Packit Service |
5195f2 |
lzo_free(overlap);
|
|
Packit Service |
5195f2 |
lzo_free(wrkmem);
|
|
Packit Service |
5195f2 |
lzo_free(out);
|
|
Packit Service |
5195f2 |
lzo_free(in);
|
|
Packit Service |
5195f2 |
if (fp) fclose(fp);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
return 0;
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*************************************************************************
|
|
Packit Service |
5195f2 |
//
|
|
Packit Service |
5195f2 |
**************************************************************************/
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
int __lzo_cdecl_main main(int argc, char *argv[])
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
int r;
|
|
Packit Service |
5195f2 |
int i = 1;
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
lzo_wildargv(&argc, &argv);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
printf("\nLZO real-time data compression library (v%s, %s).\n",
|
|
Packit Service |
5195f2 |
lzo_version_string(), lzo_version_date());
|
|
Packit Service |
5195f2 |
printf("Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
progname = argv[0];
|
|
Packit Service |
5195f2 |
if (i < argc && argv[i][0] == '-')
|
|
Packit Service |
5195f2 |
opt_overhead = atol(&argv[i++][1]);
|
|
Packit Service |
5195f2 |
#if 1
|
|
Packit Service |
5195f2 |
if (opt_overhead != 0 && opt_overhead < 4)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
printf("%s: invalid overhead value %ld\n", progname, opt_overhead);
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
#endif
|
|
Packit Service |
5195f2 |
if (i >= argc)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
printf("usage: %s [-overhead_in_bytes] file..\n", progname);
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 1: initialize the LZO library
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
if (lzo_init() != LZO_E_OK)
|
|
Packit Service |
5195f2 |
{
|
|
Packit Service |
5195f2 |
printf("internal error - lzo_init() failed !!!\n");
|
|
Packit Service |
5195f2 |
printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
|
|
Packit Service |
5195f2 |
exit(1);
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/*
|
|
Packit Service |
5195f2 |
* Step 2: process files
|
|
Packit Service |
5195f2 |
*/
|
|
Packit Service |
5195f2 |
for (r = 0; r == 0 && i < argc; i++)
|
|
Packit Service |
5195f2 |
r = do_file(argv[i]);
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
printf("\nDone. Successfully processed %lu bytes in %lu files.\n",
|
|
Packit Service |
5195f2 |
total_in, total_files);
|
|
Packit Service |
5195f2 |
return r;
|
|
Packit Service |
5195f2 |
}
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
|
|
Packit Service |
5195f2 |
/* vim:set ts=4 sw=4 et: */
|