diff options
Diffstat (limited to 'lzo/examples/overlap.c')
-rw-r--r-- | lzo/examples/overlap.c | 99 |
1 files changed, 42 insertions, 57 deletions
diff --git a/lzo/examples/overlap.c b/lzo/examples/overlap.c index 7eeb52f..b233768 100644 --- a/lzo/examples/overlap.c +++ b/lzo/examples/overlap.c @@ -2,6 +2,9 @@ This file is part of the LZO real-time data compression library. + Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer + Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer + Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer @@ -49,15 +52,17 @@ #include "lzo/lzo1x.h" /* portability layer */ +static const char *progname = NULL; #define WANT_LZO_MALLOC 1 #define WANT_LZO_FREAD 1 #define WANT_LZO_WILDARGV 1 +#define WANT_XMALLOC 1 #include "examples/portab.h" /* Overhead (in bytes) for the in-place decompression buffer. * Most files need only 16 ! - * (try `overlap -16 file' or even `overlap -8 file') + * (try 'overlap -16 file' or even 'overlap -8 file') * * Worst case (for files that are compressible by only a few bytes) * is 'in_len / 16 + 64 + 3'. See step 5a) below. @@ -76,30 +81,10 @@ static lzo_uint opt_overhead = 0; /* assume worst case */ #endif -/************************************************************************* -// -**************************************************************************/ - -static const char *progname = NULL; - static unsigned long total_files = 0; static unsigned long total_in = 0; -static lzo_bytep xmalloc(lzo_uint len) -{ - lzo_bytep p; - - p = (lzo_bytep) lzo_malloc(len > 0 ? len : 1); - if (p == NULL) - { - printf("%s: out of memory\n", progname); - exit(1); - } - return p; -} - - /************************************************************************* // **************************************************************************/ @@ -107,10 +92,10 @@ static lzo_bytep xmalloc(lzo_uint len) int do_file ( const char *in_name ) { int r; - FILE *f = NULL; + FILE *fp = NULL; long l; - lzo_bytep wrkmem = NULL; + lzo_voidp wrkmem = NULL; lzo_bytep in = NULL; lzo_uint in_len; /* uncompressed length */ @@ -127,15 +112,15 @@ int do_file ( const char *in_name ) /* * Step 1: open the input file */ - f = fopen(in_name,"rb"); - if (f == NULL) + fp = fopen(in_name, "rb"); + if (fp == NULL) { printf("%s: %s: cannot open file\n", progname, in_name); goto next_file; } - fseek(f,0,SEEK_END); - l = ftell(f); - fseek(f,0,SEEK_SET); + fseek(fp, 0, SEEK_END); + l = ftell(fp); + fseek(fp, 0, SEEK_SET); if (l <= 0) { printf("%s: %s: empty file -- skipping\n", progname, in_name); @@ -146,18 +131,18 @@ int do_file ( const char *in_name ) /* * Step 2: allocate compression buffers and read the file */ - in = xmalloc(in_len); - out = xmalloc(in_len + in_len / 16 + 64 + 3); - wrkmem = xmalloc(LZO1X_1_MEM_COMPRESS); - in_len = (lzo_uint) lzo_fread(f,in,in_len); - fclose(f); f = NULL; - printf("%s: %s: read %ld bytes\n", progname, in_name, (long) in_len); + in = (lzo_bytep) xmalloc(in_len); + out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3); + wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS); + in_len = (lzo_uint) lzo_fread(fp, in, in_len); + fclose(fp); fp = NULL; + printf("%s: %s: read %lu bytes\n", progname, in_name, (unsigned long) in_len); total_files++; total_in += (unsigned long) in_len; /* - * Step 3: compress from `in' to `out' with LZO1X-1 + * Step 3: compress from 'in' to 'out' with LZO1X-1 */ r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem); if (r != LZO_E_OK || out_len > in_len + in_len / 16 + 64 + 3) @@ -166,20 +151,20 @@ int do_file ( const char *in_name ) printf("internal error - compression failed: %d\n", r); exit(1); } - printf("%-26s %8lu -> %8lu\n", "LZO1X-1:", (long) in_len, (long) out_len); + printf("%-26s %8lu -> %8lu\n", "LZO1X-1:", (unsigned long) in_len, (unsigned long) out_len); /***** Step 4: overlapping compression *****/ /* - * Step 4a: allocate the `overlap' buffer for overlapping compression + * Step 4a: allocate the 'overlap' buffer for overlapping compression */ overhead = in_len > 0xbfff ? 0xbfff : in_len; overhead += in_len / 16 + 64 + 3; - overlap = xmalloc(in_len + overhead); + overlap = (lzo_bytep) xmalloc(in_len + overhead); /* - * Step 4b: prepare data in `overlap' buffer. + * Step 4b: prepare data in 'overlap' buffer. * copy uncompressed data at the top of the overlap buffer */ /*** offset = in_len + overhead - in_len; ***/ @@ -187,7 +172,7 @@ int do_file ( const char *in_name ) lzo_memcpy(overlap + offset, in, in_len); /* - * Step 4c: do an in-place compression within the `overlap' buffer + * Step 4c: do an in-place compression within the 'overlap' buffer */ r = lzo1x_1_compress(overlap+offset,in_len,overlap,&new_len,wrkmem); if (r != LZO_E_OK) @@ -207,10 +192,10 @@ int do_file ( const char *in_name ) * happens very seldom). So we have to verify the overlapping * compression by doing a temporary decompression. */ - lzo_uint l = in_len; - lzo_bytep tmp = xmalloc(l); - r = lzo1x_decompress_safe(overlap,new_len,tmp,&l,NULL); - if (r != LZO_E_OK || l != in_len || lzo_memcmp(in,tmp,l) != 0) + lzo_uint ll = in_len; + lzo_bytep tmp = (lzo_bytep) xmalloc(ll); + r = lzo1x_decompress_safe(overlap, new_len, tmp, &ll, NULL); + if (r != LZO_E_OK || ll != in_len || lzo_memcmp(in, tmp, ll) != 0) { /* this should NEVER happen */ printf("overlapping compression data error\n"); @@ -219,38 +204,38 @@ int do_file ( const char *in_name ) lzo_free(tmp); } - printf("overlapping compression: %8lu -> %8lu overhead: %7ld\n", - (long) in_len, (long) new_len, (long) overhead); + printf("overlapping compression: %8lu -> %8lu overhead: %7lu\n", + (unsigned long) in_len, (unsigned long) new_len, (unsigned long) overhead); lzo_free(overlap); overlap = NULL; /***** Step 5: overlapping decompression *****/ /* - * Step 5a: allocate the `overlap' buffer for in-place decompression + * Step 5a: allocate the 'overlap' buffer for in-place decompression */ if (opt_overhead == 0 || out_len >= in_len) overhead = in_len / 16 + 64 + 3; else overhead = opt_overhead; - overlap = xmalloc(in_len + overhead); + overlap = (lzo_bytep) xmalloc(in_len + overhead); /* - * Step 5b: prepare data in `overlap' buffer. + * Step 5b: prepare data in 'overlap' buffer. * copy compressed data at the top of the overlap buffer */ offset = in_len + overhead - out_len; lzo_memcpy(overlap + offset, out, out_len); /* - * Step 5c: do an in-place decompression within the `overlap' buffer + * Step 5c: do an in-place decompression within the 'overlap' buffer */ new_len = in_len; r = lzo1x_decompress(overlap+offset,out_len,overlap,&new_len,NULL); if (r != LZO_E_OK) { /* this may happen if overhead is too small */ - printf("overlapping decompression failed: %d - increase `opt_overhead'\n", r); + printf("overlapping decompression failed: %d - increase 'opt_overhead'\n", r); exit(1); } @@ -260,11 +245,11 @@ int do_file ( const char *in_name ) if (new_len != in_len || lzo_memcmp(in,overlap,in_len) != 0) { /* this may happen if overhead is too small */ - printf("overlapping decompression data error - increase `opt_overhead'\n"); + printf("overlapping decompression data error - increase 'opt_overhead'\n"); exit(1); } - printf("overlapping decompression: %8lu -> %8lu overhead: %7ld\n", - (long) out_len, (long) new_len, (long) overhead); + printf("overlapping decompression: %8lu -> %8lu overhead: %7lu\n", + (unsigned long) out_len, (unsigned long) new_len, (unsigned long) overhead); lzo_free(overlap); overlap = NULL; @@ -273,7 +258,7 @@ next_file: lzo_free(wrkmem); lzo_free(out); lzo_free(in); - if (f) fclose(f); + if (fp) fclose(fp); return 0; } @@ -292,7 +277,7 @@ int __lzo_cdecl_main main(int argc, char *argv[]) printf("\nLZO real-time data compression library (v%s, %s).\n", lzo_version_string(), lzo_version_date()); - printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); + printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); progname = argv[0]; if (i < argc && argv[i][0] == '-') @@ -316,7 +301,7 @@ int __lzo_cdecl_main main(int argc, char *argv[]) if (lzo_init() != LZO_E_OK) { printf("internal error - lzo_init() failed !!!\n"); - printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); + printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n"); exit(1); } |