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