summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/dict.c281
-rw-r--r--examples/lzopack.c645
-rw-r--r--examples/overlap.c322
-rw-r--r--examples/portab.h187
-rw-r--r--examples/portab_a.h140
-rw-r--r--examples/precomp.c349
-rw-r--r--examples/precomp2.c401
-rw-r--r--examples/simple.c173
8 files changed, 2498 insertions, 0 deletions
diff --git a/examples/dict.c b/examples/dict.c
new file mode 100644
index 0000000..04db245
--- /dev/null
+++ b/examples/dict.c
@@ -0,0 +1,281 @@
+/* dict.c -- example program: how to use preset dictionaries
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// This program shows how to use preset dictionaries.
+//
+// Please study LZO.FAQ and simple.c first.
+**************************************************************************/
+
+#include "lzo/lzoconf.h"
+#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"
+
+
+#define DICT_LEN 0xbfff
+static lzo_bytep dict;
+static lzo_uint dict_len = 0;
+static lzo_uint32 dict_adler32;
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+static lzo_uint total_n = 0;
+static lzo_uint total_c_len = 0;
+static lzo_uint total_d_len = 0;
+
+static void print_file ( const char *name, lzo_uint d_len, lzo_uint c_len )
+{
+ double perc;
+
+ perc = (d_len > 0) ? c_len * 100.0 / d_len : 0;
+ printf(" | %-30s %9ld -> %9ld %7.2f%% |\n",
+ name, (long) d_len, (long) c_len, perc);
+
+ total_n++;
+ total_c_len += c_len;
+ total_d_len += d_len;
+}
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int do_file ( const char *in_name, int compression_level )
+{
+ int r;
+ lzo_bytep in;
+ lzo_bytep out;
+ lzo_bytep newb;
+ lzo_voidp wrkmem;
+ lzo_uint in_len;
+ lzo_uint out_len;
+ lzo_uint new_len;
+ long l;
+ FILE *fp;
+
+/*
+ * Step 1: open the input file
+ */
+ fp = fopen(in_name,"rb");
+ if (fp == 0)
+ {
+ printf("%s: cannot open file %s\n", progname, in_name);
+ return 0; /* no error */
+ }
+ 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);
+ fclose(fp); fp = NULL;
+ return 0;
+ }
+ in_len = (lzo_uint) l;
+
+/*
+ * Step 2: allocate compression buffers and read the file
+ */
+ in = (lzo_bytep) xmalloc(in_len);
+ out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3);
+ newb = (lzo_bytep) xmalloc(in_len);
+ wrkmem = (lzo_voidp) xmalloc(LZO1X_999_MEM_COMPRESS);
+ if (in == NULL || out == NULL || newb == NULL || wrkmem == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+ in_len = (lzo_uint) lzo_fread(fp, in, in_len);
+ fclose(fp); fp = NULL;
+
+/*
+ * Step 3: compress from 'in' to 'out' with LZO1X-999
+ */
+ r = lzo1x_999_compress_level(in,in_len,out,&out_len,wrkmem,
+ dict, dict_len, 0, compression_level);
+ if (r != LZO_E_OK)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ return 1;
+ }
+
+ print_file(in_name,in_len,out_len);
+
+/*
+ * Step 4: decompress again, now going from 'out' to 'newb'
+ */
+ new_len = in_len;
+ r = lzo1x_decompress_dict_safe(out,out_len,newb,&new_len,NULL,dict,dict_len);
+ if (r != LZO_E_OK)
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression failed: %d\n", r);
+ return 1;
+ }
+
+/*
+ * Step 5: verify decompression
+ */
+ if (new_len != in_len || lzo_memcmp(in,newb,in_len) != 0)
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression data error\n");
+ return 1;
+ }
+
+ /* free buffers in reverse order to help malloc() */
+ lzo_free(wrkmem);
+ lzo_free(newb);
+ lzo_free(out);
+ lzo_free(in);
+ return 0;
+}
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int __lzo_cdecl_main main(int argc, char *argv[])
+{
+ int i = 1;
+ int r;
+ const char *dict_name;
+ FILE *fp;
+ time_t t_total;
+ int compression_level = 7;
+
+ lzo_wildargv(&argc, &argv);
+
+ printf("\nLZO real-time data compression library (v%s, %s).\n",
+ lzo_version_string(), lzo_version_date());
+ printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
+
+ progname = argv[0];
+
+ if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1]))
+ compression_level = atoi(&argv[i++][1]);
+
+ if (i + 1 >= argc || compression_level < 1 || compression_level > 9)
+ {
+ printf("usage: %s [-level] [ dictionary-file | -n ] file...\n", progname);
+ exit(1);
+ }
+ printf("Compression level is LZO1X-999/%d\n", compression_level);
+
+/*
+ * Step 1: initialize the LZO library
+ */
+ 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");
+ exit(1);
+ }
+
+/*
+ * Step 2: prepare the dictionary
+ */
+ dict = (lzo_bytep) xmalloc(DICT_LEN);
+ if (dict == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+ dict_name = argv[i++];
+ if (strcmp(dict_name,"-n") == 0)
+ {
+ dict_name = "empty";
+ dict_len = 0;
+ }
+ else
+ {
+ fp = fopen(dict_name,"rb");
+ if (!fp)
+ {
+ printf("%s: cannot open dictionary file %s\n", progname, dict_name);
+ exit(1);
+ }
+ dict_len = (lzo_uint) lzo_fread(fp, dict, DICT_LEN);
+ fclose(fp); fp = NULL;
+ }
+
+ dict_adler32 = lzo_adler32(0,NULL,0);
+ dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
+ printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n",
+ dict_name, (long) dict_len, (long) dict_adler32);
+
+/*
+ * Step 3: process files
+ */
+ t_total = time(NULL);
+ for (r = 0; r == 0 && i < argc; i++)
+ r = do_file(argv[i], compression_level);
+ t_total = time(NULL) - t_total;
+
+ lzo_free(dict);
+
+ if (total_n > 1)
+ print_file("***TOTALS***",total_d_len,total_c_len);
+
+ printf("Dictionary compression test %s, execution time %lu seconds.\n",
+ r == 0 ? "passed" : "FAILED", (unsigned long) t_total);
+ return r;
+}
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/lzopack.c b/examples/lzopack.c
new file mode 100644
index 0000000..e4e745d
--- /dev/null
+++ b/examples/lzopack.c
@@ -0,0 +1,645 @@
+/* lzopack.c -- LZO example program: a simple file packer
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// NOTE: this is an example program, so do not use to backup your data.
+//
+// This program lacks things like sophisticated file handling but is
+// pretty complete regarding compression - it should provide a good
+// starting point for adaption for your applications.
+//
+// Please study LZO.FAQ and simple.c first.
+**************************************************************************/
+
+#include "lzo/lzoconf.h"
+#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"
+
+
+static unsigned long total_in = 0;
+static unsigned long total_out = 0;
+static lzo_bool opt_debug = 0;
+
+/* magic file header for lzopack-compressed files */
+static const unsigned char magic[7] =
+ { 0x00, 0xe9, 0x4c, 0x5a, 0x4f, 0xff, 0x1a };
+
+
+/*************************************************************************
+// file IO
+**************************************************************************/
+
+lzo_uint xread(FILE *fp, lzo_voidp buf, lzo_uint len, lzo_bool allow_eof)
+{
+ lzo_uint l;
+
+ l = (lzo_uint) lzo_fread(fp, buf, len);
+ if (l > len)
+ {
+ fprintf(stderr, "\nsomething's wrong with your C library !!!\n");
+ exit(1);
+ }
+ if (l != len && !allow_eof)
+ {
+ fprintf(stderr, "\nread error - premature end of file\n");
+ exit(1);
+ }
+ total_in += (unsigned long) l;
+ return l;
+}
+
+lzo_uint xwrite(FILE *fp, const lzo_voidp buf, lzo_uint len)
+{
+ if (fp != NULL && lzo_fwrite(fp, buf, len) != len)
+ {
+ fprintf(stderr, "\nwrite error (disk full ?)\n");
+ exit(1);
+ }
+ total_out += (unsigned long) len;
+ return len;
+}
+
+
+int xgetc(FILE *fp)
+{
+ unsigned char c;
+ xread(fp, (lzo_voidp) &c, 1, 0);
+ return c;
+}
+
+void xputc(FILE *fp, int c)
+{
+ unsigned char cc = (unsigned char) (c & 0xff);
+ xwrite(fp, (const lzo_voidp) &cc, 1);
+}
+
+/* read and write portable 32-bit integers */
+
+lzo_uint32 xread32(FILE *fp)
+{
+ unsigned char b[4];
+ lzo_uint32 v;
+
+ xread(fp, b, 4, 0);
+ v = (lzo_uint32) b[3] << 0;
+ v |= (lzo_uint32) b[2] << 8;
+ v |= (lzo_uint32) b[1] << 16;
+ v |= (lzo_uint32) b[0] << 24;
+ return v;
+}
+
+void xwrite32(FILE *fp, lzo_xint v)
+{
+ unsigned char b[4];
+
+ b[3] = (unsigned char) ((v >> 0) & 0xff);
+ b[2] = (unsigned char) ((v >> 8) & 0xff);
+ b[1] = (unsigned char) ((v >> 16) & 0xff);
+ b[0] = (unsigned char) ((v >> 24) & 0xff);
+ xwrite(fp, b, 4);
+}
+
+
+/*************************************************************************
+// compress
+//
+// possible improvement: we could use overlapping compression to
+// save some memory - see overlap.c. This would require some minor
+// changes in the decompression code as well, because if a block
+// turns out to be incompressible we would still have to store it in its
+// "compressed" (i.e. then slightly enlarged) form because the original
+// (uncompressed) data would have been lost during the overlapping
+// compression.
+**************************************************************************/
+
+int do_compress(FILE *fi, FILE *fo, int compression_level, lzo_uint block_size)
+{
+ int r = 0;
+ lzo_bytep in = NULL;
+ lzo_bytep out = NULL;
+ lzo_voidp wrkmem = NULL;
+ lzo_uint in_len;
+ lzo_uint out_len;
+ lzo_uint32 wrk_len = 0;
+ lzo_uint32 flags = 1; /* do compute a checksum */
+ int method = 1; /* compression method: LZO1X */
+ lzo_uint32 checksum;
+
+ total_in = total_out = 0;
+
+/*
+ * Step 1: write magic header, flags & block size, init checksum
+ */
+ xwrite(fo, magic, sizeof(magic));
+ xwrite32(fo, flags);
+ xputc(fo, method); /* compression method */
+ xputc(fo, compression_level); /* compression level */
+ xwrite32(fo, block_size);
+ checksum = lzo_adler32(0, NULL, 0);
+
+/*
+ * Step 2: allocate compression buffers and work-memory
+ */
+ in = (lzo_bytep) xmalloc(block_size);
+ out = (lzo_bytep) xmalloc(block_size + block_size / 16 + 64 + 3);
+ if (compression_level == 9)
+ wrk_len = LZO1X_999_MEM_COMPRESS;
+ else
+ wrk_len = LZO1X_1_MEM_COMPRESS;
+ wrkmem = (lzo_voidp) xmalloc(wrk_len);
+ if (in == NULL || out == NULL || wrkmem == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ r = 1;
+ goto err;
+ }
+
+/*
+ * Step 3: process blocks
+ */
+ for (;;)
+ {
+ /* read block */
+ in_len = xread(fi, in, block_size, 1);
+ if (in_len == 0)
+ break;
+
+ /* update checksum */
+ if (flags & 1)
+ checksum = lzo_adler32(checksum, in, in_len);
+
+ /* clear wrkmem (not needed, only for debug/benchmark purposes) */
+ if (opt_debug)
+ lzo_memset(wrkmem, 0xff, wrk_len);
+
+ /* compress block */
+ if (compression_level == 9)
+ r = lzo1x_999_compress(in, in_len, out, &out_len, wrkmem);
+ else
+ 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)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ r = 2;
+ goto err;
+ }
+
+ /* write uncompressed block size */
+ xwrite32(fo, in_len);
+
+ if (out_len < in_len)
+ {
+ /* write compressed block */
+ xwrite32(fo, out_len);
+ xwrite(fo, out, out_len);
+ }
+ else
+ {
+ /* not compressible - write uncompressed block */
+ xwrite32(fo, in_len);
+ xwrite(fo, in, in_len);
+ }
+ }
+
+ /* write EOF marker */
+ xwrite32(fo, 0);
+
+ /* write checksum */
+ if (flags & 1)
+ xwrite32(fo, checksum);
+
+ r = 0;
+err:
+ lzo_free(wrkmem);
+ lzo_free(out);
+ lzo_free(in);
+ return r;
+}
+
+
+/*************************************************************************
+// decompress / test
+//
+// We are using overlapping (in-place) decompression to save some
+// memory - see overlap.c.
+**************************************************************************/
+
+int do_decompress(FILE *fi, FILE *fo)
+{
+ int r = 0;
+ lzo_bytep buf = NULL;
+ lzo_uint buf_len;
+ unsigned char m [ sizeof(magic) ];
+ lzo_uint32 flags;
+ int method;
+ int compression_level;
+ lzo_uint block_size;
+ lzo_uint32 checksum;
+
+ total_in = total_out = 0;
+
+/*
+ * Step 1: check magic header, read flags & block size, init checksum
+ */
+ if (xread(fi, m, sizeof(magic),1) != sizeof(magic) ||
+ memcmp(m, magic, sizeof(magic)) != 0)
+ {
+ printf("%s: header error - this file is not compressed by lzopack\n", progname);
+ r = 1;
+ goto err;
+ }
+ flags = xread32(fi);
+ method = xgetc(fi);
+ compression_level = xgetc(fi);
+ if (method != 1)
+ {
+ printf("%s: header error - invalid method %d (level %d)\n",
+ progname, method, compression_level);
+ r = 2;
+ goto err;
+ }
+ block_size = xread32(fi);
+ if (block_size < 1024 || block_size > 8*1024*1024L)
+ {
+ printf("%s: header error - invalid block size %ld\n",
+ progname, (long) block_size);
+ r = 3;
+ goto err;
+ }
+ checksum = lzo_adler32(0,NULL,0);
+
+/*
+ * Step 2: allocate buffer for in-place decompression
+ */
+ buf_len = block_size + block_size / 16 + 64 + 3;
+ buf = (lzo_bytep) xmalloc(buf_len);
+ if (buf == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ r = 4;
+ goto err;
+ }
+
+/*
+ * Step 3: process blocks
+ */
+ for (;;)
+ {
+ lzo_bytep in;
+ lzo_bytep out;
+ lzo_uint in_len;
+ lzo_uint out_len;
+
+ /* read uncompressed size */
+ out_len = xread32(fi);
+
+ /* exit if last block (EOF marker) */
+ if (out_len == 0)
+ break;
+
+ /* read compressed size */
+ in_len = xread32(fi);
+
+ /* sanity check of the size values */
+ if (in_len > block_size || out_len > block_size ||
+ in_len == 0 || in_len > out_len)
+ {
+ printf("%s: block size error - data corrupted\n", progname);
+ r = 5;
+ goto err;
+ }
+
+ /* place compressed block at the top of the buffer */
+ in = buf + buf_len - in_len;
+ out = buf;
+
+ /* read compressed block data */
+ xread(fi, in, in_len, 0);
+
+ if (in_len < out_len)
+ {
+ /* decompress - use safe decompressor as data might be corrupted
+ * during a file transfer */
+ lzo_uint new_len = out_len;
+
+ r = lzo1x_decompress_safe(in, in_len, out, &new_len, NULL);
+ if (r != LZO_E_OK || new_len != out_len)
+ {
+ printf("%s: compressed data violation\n", progname);
+ r = 6;
+ goto err;
+ }
+ /* write decompressed block */
+ xwrite(fo, out, out_len);
+ /* update checksum */
+ if (flags & 1)
+ checksum = lzo_adler32(checksum, out, out_len);
+ }
+ else
+ {
+ /* write original (incompressible) block */
+ xwrite(fo, in, in_len);
+ /* update checksum */
+ if (flags & 1)
+ checksum = lzo_adler32(checksum, in, in_len);
+ }
+ }
+
+ /* read and verify checksum */
+ if (flags & 1)
+ {
+ lzo_uint32 c = xread32(fi);
+ if (c != checksum)
+ {
+ printf("%s: checksum error - data corrupted\n", progname);
+ r = 7;
+ goto err;
+ }
+ }
+
+ r = 0;
+err:
+ lzo_free(buf);
+ return r;
+}
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+static void usage(void)
+{
+ printf("usage: %s [-9] input-file output-file (compress)\n", progname);
+ printf("usage: %s -d input-file output-file (decompress)\n", progname);
+ printf("usage: %s -t input-file... (test)\n", progname);
+ exit(1);
+}
+
+
+/* open input file */
+static FILE *xopen_fi(const char *name)
+{
+ FILE *fp;
+
+ fp = fopen(name, "rb");
+ if (fp == NULL)
+ {
+ printf("%s: cannot open input file %s\n", progname, name);
+ exit(1);
+ }
+#if defined(HAVE_STAT) && defined(S_ISREG)
+ {
+ struct stat st;
+ int is_regular = 1;
+ if (stat(name, &st) != 0 || !S_ISREG(st.st_mode))
+ is_regular = 0;
+ if (!is_regular)
+ {
+ printf("%s: %s is not a regular file\n", progname, name);
+ fclose(fp); fp = NULL;
+ exit(1);
+ }
+ }
+#endif
+ return fp;
+}
+
+
+/* open output file */
+static FILE *xopen_fo(const char *name)
+{
+ FILE *fp;
+
+#if 0
+ /* this is an example program, so make sure we don't overwrite a file */
+ fp = fopen(name, "rb");
+ if (fp != NULL)
+ {
+ printf("%s: file %s already exists -- not overwritten\n", progname, name);
+ fclose(fp); fp = NULL;
+ exit(1);
+ }
+#endif
+ fp = fopen(name, "wb");
+ if (fp == NULL)
+ {
+ printf("%s: cannot open output file %s\n", progname, name);
+ exit(1);
+ }
+ return fp;
+}
+
+
+/* close file */
+static void xclose(FILE *fp)
+{
+ if (fp)
+ {
+ int err;
+ err = ferror(fp);
+ if (fclose(fp) != 0)
+ err = 1;
+ if (err)
+ {
+ printf("%s: error while closing file\n", progname);
+ exit(1);
+ }
+ }
+}
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int __lzo_cdecl_main main(int argc, char *argv[])
+{
+ int i = 1;
+ int r = 0;
+ FILE *fi = NULL;
+ FILE *fo = NULL;
+ const char *in_name = NULL;
+ const char *out_name = NULL;
+ unsigned opt_decompress = 0;
+ unsigned opt_test = 0;
+ int opt_compression_level = 1;
+ lzo_uint opt_block_size;
+ const char *s;
+
+ lzo_wildargv(&argc, &argv);
+
+ progname = argv[0];
+ for (s = progname; *s; s++)
+ if ((*s == '/' || *s == '\\') && s[1])
+ progname = s + 1;
+
+ printf("\nLZO real-time data compression library (v%s, %s).\n",
+ lzo_version_string(), lzo_version_date());
+ printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
+
+#if 0
+ printf(
+"*** DISCLAIMER ***\n"
+" This is an example program, do not use to backup your data !\n"
+" Get LZOP if you're interested into a full-featured packer.\n"
+" See http://www.oberhumer.com/opensource/lzop/\n"
+"\n");
+#endif
+
+
+/*
+ * Step 1: initialize the LZO library
+ */
+ 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");
+ exit(1);
+ }
+
+
+/*
+ * Step 2: setup memory
+ */
+ opt_block_size = 256 * 1024L;
+
+#if defined(ACC_MM_AHSHIFT)
+ /* reduce memory requirements for ancient 16-bit DOS 640kB real-mode */
+ if (ACC_MM_AHSHIFT != 3)
+ opt_block_size = 16 * 1024L;
+#endif
+
+
+/*
+ * Step 3: get options
+ */
+
+ while (i < argc && argv[i][0] == '-')
+ {
+ if (strcmp(argv[i],"-d") == 0)
+ opt_decompress = 1;
+ else if (strcmp(argv[i],"-t") == 0)
+ opt_test = 1;
+ else if (strcmp(argv[i],"-9") == 0)
+ opt_compression_level = 9;
+ else if (argv[i][1] == 'b' && argv[i][2])
+ {
+ long b = atol(&argv[i][2]);
+ if (b >= 1024L && b <= 8*1024*1024L)
+ opt_block_size = (lzo_uint) b;
+ else
+ {
+ printf("%s: invalid block_size in option '%s'.\n", progname, argv[i]);
+ usage();
+ }
+ }
+ else if (strcmp(argv[i],"--debug") == 0)
+ opt_debug += 1;
+ else
+ usage();
+ i++;
+ }
+ if (opt_test && i >= argc)
+ usage();
+ if (!opt_test && i + 2 != argc)
+ usage();
+
+
+/*
+ * Step 4: process file(s)
+ */
+
+ if (opt_test)
+ {
+ while (i < argc && r == 0)
+ {
+ in_name = argv[i++];
+ fi = xopen_fi(in_name);
+ r = do_decompress(fi, NULL);
+ if (r == 0)
+ printf("%s: %s tested ok (%lu -> %lu bytes)\n",
+ progname, in_name, total_in, total_out);
+ xclose(fi); fi = NULL;
+ }
+ }
+ else if (opt_decompress)
+ {
+ in_name = argv[i++];
+ out_name = argv[i++];
+ fi = xopen_fi(in_name);
+ fo = xopen_fo(out_name);
+ r = do_decompress(fi, fo);
+ if (r == 0)
+ printf("%s: decompressed %lu into %lu bytes\n",
+ progname, total_in, total_out);
+ }
+ else /* compress */
+ {
+ in_name = argv[i++];
+ out_name = argv[i++];
+ fi = xopen_fi(in_name);
+ fo = xopen_fo(out_name);
+ r = do_compress(fi, fo, opt_compression_level, opt_block_size);
+ if (r == 0)
+ printf("%s: compressed %lu into %lu bytes\n",
+ progname, total_in, total_out);
+ }
+
+ xclose(fi); fi = NULL;
+ xclose(fo); fo = NULL;
+ return r;
+}
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/overlap.c b/examples/overlap.c
new file mode 100644
index 0000000..b233768
--- /dev/null
+++ b/examples/overlap.c
@@ -0,0 +1,322 @@
+/* overlap.c -- example program: overlapping (de)compression
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// This program shows how to do overlapping compression and
+// in-place decompression.
+//
+// Please study LZO.FAQ and simple.c first.
+**************************************************************************/
+
+#include "lzo/lzoconf.h"
+#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')
+ *
+ * Worst case (for files that are compressible by only a few bytes)
+ * is 'in_len / 16 + 64 + 3'. See step 5a) below.
+ *
+ * For overlapping compression '0xbfff + in_len / 16 + 64 + 3' bytes
+ * will be needed. See step 4a) below.
+ */
+
+static lzo_uint opt_overhead = 0; /* assume worst case */
+
+
+#if 0 && defined(LZO_USE_ASM)
+ /* just for testing */
+# include <lzo_asm.h>
+# define lzo1x_decompress lzo1x_decompress_asm_fast
+#endif
+
+
+static unsigned long total_files = 0;
+static unsigned long total_in = 0;
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int do_file ( const char *in_name )
+{
+ int r;
+ FILE *fp = NULL;
+ long l;
+
+ lzo_voidp wrkmem = NULL;
+
+ lzo_bytep in = NULL;
+ lzo_uint in_len; /* uncompressed length */
+
+ lzo_bytep out = NULL;
+ lzo_uint out_len; /* compressed length */
+
+ lzo_bytep overlap = NULL;
+ lzo_uint overhead;
+ lzo_uint offset;
+
+ lzo_uint new_len = 0;
+
+/*
+ * Step 1: open the input file
+ */
+ fp = fopen(in_name, "rb");
+ if (fp == NULL)
+ {
+ printf("%s: %s: cannot open file\n", progname, in_name);
+ goto next_file;
+ }
+ 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);
+ goto next_file;
+ }
+ in_len = (lzo_uint) l;
+
+/*
+ * Step 2: allocate compression buffers and read the file
+ */
+ 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
+ */
+ 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)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ exit(1);
+ }
+ 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
+ */
+ overhead = in_len > 0xbfff ? 0xbfff : in_len;
+ overhead += in_len / 16 + 64 + 3;
+ overlap = (lzo_bytep) xmalloc(in_len + overhead);
+
+/*
+ * Step 4b: prepare data in 'overlap' buffer.
+ * copy uncompressed data at the top of the overlap buffer
+ */
+ /*** offset = in_len + overhead - in_len; ***/
+ offset = overhead;
+ lzo_memcpy(overlap + offset, in, in_len);
+
+/*
+ * 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)
+ {
+ /* this should NEVER happen */
+ printf("overlapping compression failed: %d\n", r);
+ exit(1);
+ }
+
+/*
+ * Step 4d: verify overlapping compression
+ */
+ if (new_len != out_len || lzo_memcmp(out,overlap,out_len) != 0)
+ {
+ /* As compression is non-deterministic there can be a difference
+ * in the representation of the compressed data (but this usually
+ * happens very seldom). So we have to verify the overlapping
+ * compression by doing a temporary decompression.
+ */
+ 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");
+ exit(1);
+ }
+ lzo_free(tmp);
+ }
+
+ 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
+ */
+ if (opt_overhead == 0 || out_len >= in_len)
+ overhead = in_len / 16 + 64 + 3;
+ else
+ overhead = opt_overhead;
+ overlap = (lzo_bytep) xmalloc(in_len + overhead);
+
+/*
+ * 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
+ */
+ 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);
+ exit(1);
+ }
+
+/*
+ * Step 5d: verify decompression
+ */
+ 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");
+ exit(1);
+ }
+ printf("overlapping decompression: %8lu -> %8lu overhead: %7lu\n",
+ (unsigned long) out_len, (unsigned long) new_len, (unsigned long) overhead);
+ lzo_free(overlap); overlap = NULL;
+
+
+next_file:
+ lzo_free(overlap);
+ lzo_free(wrkmem);
+ lzo_free(out);
+ lzo_free(in);
+ if (fp) fclose(fp);
+
+ return 0;
+}
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int __lzo_cdecl_main main(int argc, char *argv[])
+{
+ int r;
+ int i = 1;
+
+ lzo_wildargv(&argc, &argv);
+
+ printf("\nLZO real-time data compression library (v%s, %s).\n",
+ lzo_version_string(), lzo_version_date());
+ printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
+
+ progname = argv[0];
+ if (i < argc && argv[i][0] == '-')
+ opt_overhead = atoi(&argv[i++][1]);
+#if 1
+ if (opt_overhead != 0 && opt_overhead < 8)
+ {
+ printf("%s: invalid overhead value %ld\n", progname, (long)opt_overhead);
+ exit(1);
+ }
+#endif
+ if (i >= argc)
+ {
+ printf("usage: %s [-overhead_in_bytes] file..\n", progname);
+ exit(1);
+ }
+
+/*
+ * Step 1: initialize the LZO library
+ */
+ 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");
+ exit(1);
+ }
+
+/*
+ * Step 2: process files
+ */
+ for (r = 0; r == 0 && i < argc; i++)
+ r = do_file(argv[i]);
+
+ printf("\nDone. Successfully processed %lu bytes in %lu files.\n",
+ total_in, total_files);
+ return r;
+}
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/portab.h b/examples/portab.h
new file mode 100644
index 0000000..b4b5a21
--- /dev/null
+++ b/examples/portab.h
@@ -0,0 +1,187 @@
+/* portab.h -- portability layer
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+#include "lzo/lzoconf.h"
+
+#if (LZO_CC_MSC && (_MSC_VER >= 1000 && _MSC_VER < 1200))
+ /* avoid '-W4' warnings in system header files */
+# pragma warning(disable: 4201 4214 4514)
+#endif
+#if (LZO_CC_MSC && (_MSC_VER >= 1300))
+ /* avoid '-Wall' warnings in system header files */
+# pragma warning(disable: 4163 4255 4820)
+ /* avoid warnings about inlining */
+# pragma warning(disable: 4710 4711)
+#endif
+#if (LZO_CC_MSC && (_MSC_VER >= 1400))
+ /* avoid warnings when using "deprecated" POSIX functions */
+# pragma warning(disable: 4996)
+#endif
+#if (LZO_CC_PELLESC && (__POCC__ >= 290))
+# pragma warn(disable:2002)
+#endif
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+#if defined(__LZO_MMODEL_HUGE) || defined(ACC_WANT_ACCLIB_GETOPT) || !(defined(LZO_LIBC_ISOC90) || defined(LZO_LIBC_ISOC99))
+
+#include "examples/portab_a.h"
+
+#else
+
+/* INFO:
+ * The "portab_a.h" version above uses the ACC library to add
+ * support for ancient systems (like 16-bit DOS) and to provide
+ * some gimmicks like Windows high-resolution timers.
+ * Still, on any halfway modern machine you can also use the
+ * following pure ANSI-C code instead.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <time.h>
+#if defined(CLK_TCK) && !defined(CLOCKS_PER_SEC)
+# define CLOCKS_PER_SEC CLK_TCK
+#endif
+
+#if defined(WANT_LZO_MALLOC)
+# define lzo_malloc(a) (malloc(a))
+# define lzo_free(a) (free(a))
+#endif
+#if defined(WANT_LZO_FREAD)
+# define lzo_fread(f,b,s) (fread(b,1,s,f))
+# define lzo_fwrite(f,b,s) (fwrite(b,1,s,f))
+#endif
+#if defined(WANT_LZO_UCLOCK)
+# define lzo_uclock_handle_t int
+# define lzo_uclock_t double
+# define lzo_uclock_open(a) ((void)(a))
+# define lzo_uclock_close(a) ((void)(a))
+# define lzo_uclock_read(a,b) *(b) = (clock() / (double)(CLOCKS_PER_SEC))
+# define lzo_uclock_get_elapsed(a,b,c) (*(c) - *(b))
+#endif
+#if defined(WANT_LZO_WILDARGV)
+# define lzo_wildargv(a,b) ((void)0)
+#endif
+
+#endif
+
+
+/*************************************************************************
+// misc
+**************************************************************************/
+
+/* turn on assertions */
+#undef NDEBUG
+#include <assert.h>
+
+/* just in case */
+#undef xmalloc
+#undef xfree
+#undef xread
+#undef xwrite
+#undef xputc
+#undef xgetc
+#undef xread32
+#undef xwrite32
+
+
+#if defined(WANT_XMALLOC)
+static lzo_voidp xmalloc(lzo_uint len)
+{
+ lzo_voidp p;
+ lzo_uint align = (lzo_uint) sizeof(lzo_align_t);
+
+ p = (lzo_voidp) lzo_malloc(len > 0 ? len : 1);
+ if (p == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+ if (len >= align && __lzo_align_gap(p, align) != 0)
+ {
+ printf("%s: C library problem: malloc() returned misaligned pointer!\n", progname);
+ exit(1);
+ }
+ return p;
+}
+#endif
+
+
+#if defined(WANT_LZO_UCLOCK)
+
+/* High quality benchmarking.
+ *
+ * Flush the CPU cache to get more accurate benchmark values.
+ * This needs custom kernel patches. As a result - in combination with
+ * the perfctr Linux kernel patches - accurate high-quality benchmarking
+ * is possible.
+ *
+ * All other methods (rdtsc, QueryPerformanceCounter, gettimeofday, ...)
+ * are completely unreliable for our purposes, and the only other
+ * option is to boot into a legacy single-task operating system
+ * like plain MSDOS and to directly reprogram the hardware clock.
+ * [The djgpp2 port of the gcc compiler has support functions for this.]
+ *
+ * Also, for embedded systems it's best to benchmark by using a
+ * CPU emulator/simulator software that can exactly count all
+ * virtual clock ticks.
+ */
+
+#if !defined(lzo_uclock_flush_cpu_cache)
+# define lzo_uclock_flush_cpu_cache(h,flags) ((void)(h))
+#endif
+
+#endif
+
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/portab_a.h b/examples/portab_a.h
new file mode 100644
index 0000000..2706022
--- /dev/null
+++ b/examples/portab_a.h
@@ -0,0 +1,140 @@
+/* portab_a.h -- advanced portability layer
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// use the ACC library for the hard work
+**************************************************************************/
+
+#if defined(LZO_HAVE_CONFIG_H)
+# define ACC_CONFIG_NO_HEADER 1
+#endif
+
+#define ACC_WANT_ACC_INCD_H 1
+#define ACC_WANT_ACC_INCE_H 1
+#if defined(__LZO_MMODEL_HUGE) || defined(ACC_WANT_ACCLIB_GETOPT) || defined(WANT_LZO_UCLOCK) || defined(WANT_LZO_WILDARGV)
+# define ACC_WANT_ACC_INCI_H 1
+# define ACC_WANT_ACC_LIB_H 1
+#endif
+#include "src/miniacc.h"
+
+#if defined(WANT_LZO_MALLOC)
+# if defined(__LZO_MMODEL_HUGE)
+# define ACC_WANT_ACCLIB_HALLOC 1
+# else
+# define acc_halloc(a) (malloc(a))
+# define acc_hfree(a) (free(a))
+# endif
+#endif
+#if defined(WANT_LZO_FREAD)
+# if defined(__LZO_MMODEL_HUGE)
+# define ACC_WANT_ACCLIB_HFREAD 1
+# else
+# define acc_hfread(f,b,s) (fread(b,1,s,f))
+# define acc_hfwrite(f,b,s) (fwrite(b,1,s,f))
+# endif
+#endif
+#if defined(WANT_LZO_UCLOCK)
+# define ACC_WANT_ACCLIB_PCLOCK 1
+# if 0 && (LZO_ARCH_AMD64 || LZO_ARCH_I386)
+# define __ACCLIB_PCLOCK_USE_RDTSC 1
+# define ACC_WANT_ACCLIB_RDTSC 1
+# endif
+#endif
+#if defined(WANT_LZO_WILDARGV)
+# define ACC_WANT_ACCLIB_WILDARGV 1
+#endif
+#if (__ACCLIB_REQUIRE_HMEMCPY_CH) && !defined(__ACCLIB_HMEMCPY_CH_INCLUDED)
+# define ACC_WANT_ACCLIB_HMEMCPY 1
+#endif
+#include "src/miniacc.h"
+
+
+/*************************************************************************
+// finally pull into the LZO namespace
+**************************************************************************/
+
+#undef lzo_malloc
+#undef lzo_free
+#undef lzo_fread
+#undef lzo_fwrite
+#if defined(WANT_LZO_MALLOC)
+# if defined(acc_halloc)
+# define lzo_malloc(a) acc_halloc(a)
+# else
+# define lzo_malloc(a) __ACCLIB_FUNCNAME(acc_halloc)(a)
+# endif
+# if defined(acc_hfree)
+# define lzo_free(a) acc_hfree(a)
+# else
+# define lzo_free(a) __ACCLIB_FUNCNAME(acc_hfree)(a)
+# endif
+#endif
+#if defined(WANT_LZO_FREAD)
+# if defined(acc_hfread)
+# define lzo_fread(f,b,s) acc_hfread(f,b,s)
+# else
+# define lzo_fread(f,b,s) __ACCLIB_FUNCNAME(acc_hfread)(f,b,s)
+# endif
+# if defined(acc_hfwrite)
+# define lzo_fwrite(f,b,s) acc_hfwrite(f,b,s)
+# else
+# define lzo_fwrite(f,b,s) __ACCLIB_FUNCNAME(acc_hfwrite)(f,b,s)
+# endif
+#endif
+#if defined(WANT_LZO_UCLOCK)
+# define lzo_uclock_handle_t acc_pclock_handle_t
+# define lzo_uclock_t acc_pclock_t
+# define lzo_uclock_open(a) __ACCLIB_FUNCNAME(acc_pclock_open_default)(a)
+# define lzo_uclock_close(a) __ACCLIB_FUNCNAME(acc_pclock_close)(a)
+# define lzo_uclock_read(a,b) __ACCLIB_FUNCNAME(acc_pclock_read)(a,b)
+# define lzo_uclock_get_elapsed(a,b,c) __ACCLIB_FUNCNAME(acc_pclock_get_elapsed)(a,b,c)
+#endif
+#if defined(WANT_LZO_WILDARGV)
+# define lzo_wildargv(a,b) __ACCLIB_FUNCNAME(acc_wildargv)(a,b)
+#endif
+
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/precomp.c b/examples/precomp.c
new file mode 100644
index 0000000..a953827
--- /dev/null
+++ b/examples/precomp.c
@@ -0,0 +1,349 @@
+/* precomp.c -- example program: how to generate pre-compressed data
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// This program shows how to generate pre-compressed data.
+//
+// Please study LZO.FAQ and simple.c first.
+//
+// We will be trying both LZO1X-999 and LZO1Y-999 and choose
+// the algorithm that achieves the best compression ratio.
+**************************************************************************/
+
+#include "lzo/lzoconf.h"
+#include "lzo/lzo1x.h"
+#include "lzo/lzo1y.h"
+
+#define USE_LZO1X 1
+#define USE_LZO1Y 1
+
+#define PARANOID 1
+
+
+/* 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"
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int __lzo_cdecl_main main(int argc, char *argv[])
+{
+ int r;
+
+ lzo_bytep in;
+ lzo_uint in_len;
+
+ lzo_bytep out;
+ lzo_uint out_bufsize;
+ lzo_uint out_len = 0;
+
+ lzo_voidp wrkmem;
+ lzo_uint wrk_len;
+
+ lzo_uint best_len;
+ int best_compress = -1;
+
+ lzo_uint orig_len;
+ lzo_uint32 uncompressed_checksum;
+ lzo_uint32 compressed_checksum;
+
+ FILE *fp;
+ const char *in_name = NULL;
+ const char *out_name = NULL;
+ long l;
+
+
+ lzo_wildargv(&argc, &argv);
+
+ printf("\nLZO real-time data compression library (v%s, %s).\n",
+ lzo_version_string(), lzo_version_date());
+ printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
+
+ progname = argv[0];
+ if (argc < 2 || argc > 3)
+ {
+ printf("usage: %s file [output-file]\n", progname);
+ exit(1);
+ }
+ in_name = argv[1];
+ if (argc > 2) out_name = argv[2];
+
+/*
+ * Step 1: initialize the LZO library
+ */
+ 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");
+ exit(1);
+ }
+
+/*
+ * Step 2: allocate the work-memory
+ */
+ wrk_len = 1;
+#ifdef USE_LZO1X
+ if (wrk_len < LZO1X_999_MEM_COMPRESS)
+ wrk_len = LZO1X_999_MEM_COMPRESS;
+#endif
+#ifdef USE_LZO1Y
+ if (wrk_len < LZO1Y_999_MEM_COMPRESS)
+ wrk_len = LZO1Y_999_MEM_COMPRESS;
+#endif
+ wrkmem = (lzo_voidp) xmalloc(wrk_len);
+ if (wrkmem == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+
+/*
+ * Step 3: open the input file
+ */
+ fp = fopen(in_name,"rb");
+ if (fp == NULL)
+ {
+ printf("%s: cannot open file %s\n", progname, in_name);
+ exit(1);
+ }
+ fseek(fp, 0, SEEK_END);
+ l = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ if (l <= 0)
+ {
+ printf("%s: %s: empty file\n", progname, in_name);
+ fclose(fp); fp = NULL;
+ exit(1);
+ }
+ in_len = (lzo_uint) l;
+ out_bufsize = in_len + in_len / 16 + 64 + 3;
+ best_len = in_len;
+
+/*
+ * Step 4: allocate compression buffers and read the file
+ */
+ in = (lzo_bytep) xmalloc(in_len);
+ out = (lzo_bytep) xmalloc(out_bufsize);
+ if (in == NULL || out == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+ in_len = (lzo_uint) lzo_fread(fp, in, in_len);
+ printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
+ fclose(fp); fp = NULL;
+
+/*
+ * Step 5: compute a checksum of the uncompressed data
+ */
+ uncompressed_checksum = lzo_adler32(0,NULL,0);
+ uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);
+
+/*
+ * Step 6a: compress from 'in' to 'out' with LZO1X-999
+ */
+#ifdef USE_LZO1X
+ out_len = out_bufsize;
+ r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
+ if (r != LZO_E_OK)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ exit(1);
+ }
+ printf("LZO1X-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
+ if (out_len < best_len)
+ {
+ best_len = out_len;
+ best_compress = 1; /* LZO1X-999 */
+ }
+#endif /* USE_LZO1X */
+
+/*
+ * Step 6b: compress from 'in' to 'out' with LZO1Y-999
+ */
+#ifdef USE_LZO1Y
+ out_len = out_bufsize;
+ r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
+ if (r != LZO_E_OK)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ exit(1);
+ }
+ printf("LZO1Y-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
+ if (out_len < best_len)
+ {
+ best_len = out_len;
+ best_compress = 2; /* LZO1Y-999 */
+ }
+#endif /* USE_LZO1Y */
+
+/*
+ * Step 7: check if compressible
+ */
+ if (best_len >= in_len)
+ {
+ printf("This file contains incompressible data.\n");
+ return 0;
+ }
+
+/*
+ * Step 8: compress data again using the best compressor found
+ */
+ out_len = out_bufsize;
+ if (best_compress == 1)
+ r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
+ else if (best_compress == 2)
+ r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
+ else
+ r = -100;
+ assert(r == LZO_E_OK);
+ assert(out_len == best_len);
+
+/*
+ * Step 9: optimize compressed data (compressed data is in 'out' buffer)
+ */
+#if 1
+ /* Optimization does not require any data in the buffer that will
+ * hold the uncompressed data. To prove this, we clear the buffer.
+ */
+ lzo_memset(in,0,in_len);
+#endif
+
+ orig_len = in_len;
+ r = -100;
+#ifdef USE_LZO1X
+ if (best_compress == 1)
+ r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
+#endif
+#ifdef USE_LZO1Y
+ if (best_compress == 2)
+ r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
+#endif
+ if (r != LZO_E_OK || orig_len != in_len)
+ {
+ /* this should NEVER happen */
+ printf("internal error - optimization failed: %d\n", r);
+ exit(1);
+ }
+
+/*
+ * Step 10: compute a checksum of the compressed data
+ */
+ compressed_checksum = lzo_adler32(0,NULL,0);
+ compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);
+
+/*
+ * Step 11: write compressed data to a file
+ */
+ printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
+ progname, in_name, (long) in_len, (long) out_len,
+ (long) uncompressed_checksum, (long) compressed_checksum);
+
+ if (out_name && out_name[0])
+ {
+ printf("%s: writing to file %s\n", progname, out_name);
+ fp = fopen(out_name,"wb");
+ if (fp == NULL)
+ {
+ printf("%s: cannot open output file %s\n", progname, out_name);
+ exit(1);
+ }
+ if (lzo_fwrite(fp, out, out_len) != out_len || fclose(fp) != 0)
+ {
+ printf("%s: write error !!\n", progname);
+ exit(1);
+ }
+ }
+
+/*
+ * Step 12: verify decompression
+ */
+#ifdef PARANOID
+ lzo_memset(in,0,in_len); /* paranoia - clear output buffer */
+ orig_len = in_len;
+ r = -100;
+#ifdef USE_LZO1X
+ if (best_compress == 1)
+ r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
+#endif
+#ifdef USE_LZO1Y
+ if (best_compress == 2)
+ r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
+#endif
+ if (r != LZO_E_OK || orig_len != in_len)
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression failed: %d\n", r);
+ exit(1);
+ }
+ if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression data error\n");
+ exit(1);
+ }
+ /* Now you could also verify decompression under similar conditions as in
+ * your application, e.g. overlapping assembler decompression etc.
+ */
+#endif
+
+ lzo_free(in);
+ lzo_free(out);
+ lzo_free(wrkmem);
+
+ return 0;
+}
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/precomp2.c b/examples/precomp2.c
new file mode 100644
index 0000000..d9e1b47
--- /dev/null
+++ b/examples/precomp2.c
@@ -0,0 +1,401 @@
+/* precomp2.c -- example program: how to generate pre-compressed data
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// This program shows how to generate pre-compressed data.
+//
+// Please study precomp.c first.
+//
+// We will be trying LZO1X-999 and LZO1Y-999, and we will be trying
+// various parameters using the internal interface to squeeze out
+// a little bit of extra compression.
+//
+// NOTE: this program can be quite slow for highly redundant files
+**************************************************************************/
+
+#include "lzo/lzoconf.h"
+#include "lzo/lzo1x.h"
+#include "lzo/lzo1y.h"
+
+LZO_EXTERN(int)
+lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
+ lzo_bytep out, lzo_uintp out_len,
+ lzo_voidp wrkmem,
+ const lzo_bytep dict, lzo_uint dict_len,
+ lzo_callback_p cb,
+ int try_lazy,
+ lzo_uint good_length,
+ lzo_uint max_lazy,
+ lzo_uint nice_length,
+ lzo_uint max_chain,
+ lzo_uint32 flags );
+
+LZO_EXTERN(int)
+lzo1y_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
+ lzo_bytep out, lzo_uintp out_len,
+ lzo_voidp wrkmem,
+ const lzo_bytep dict, lzo_uint dict_len,
+ lzo_callback_p cb,
+ int try_lazy,
+ lzo_uint good_length,
+ lzo_uint max_lazy,
+ lzo_uint nice_length,
+ lzo_uint max_chain,
+ lzo_uint32 flags );
+
+#define USE_LZO1X 1
+#define USE_LZO1Y 1
+
+#define PARANOID 1
+
+
+/* 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"
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int __lzo_cdecl_main main(int argc, char *argv[])
+{
+ int r;
+ int lazy;
+ const int max_try_lazy = 5;
+ const lzo_uint big = 65536L; /* can result in very slow compression */
+ const lzo_uint32 flags = 0x1;
+
+ lzo_bytep in;
+ lzo_uint in_len;
+
+ lzo_bytep out;
+ lzo_uint out_bufsize;
+ lzo_uint out_len = 0;
+
+ lzo_voidp wrkmem;
+ lzo_uint wrk_len;
+
+ lzo_uint best_len;
+ int best_compress = -1;
+ int best_lazy = -1;
+
+ lzo_uint orig_len;
+ lzo_uint32 uncompressed_checksum;
+ lzo_uint32 compressed_checksum;
+
+ FILE *fp;
+ const char *in_name = NULL;
+ const char *out_name = NULL;
+ long l;
+
+
+ lzo_wildargv(&argc, &argv);
+
+ printf("\nLZO real-time data compression library (v%s, %s).\n",
+ lzo_version_string(), lzo_version_date());
+ printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
+
+ progname = argv[0];
+ if (argc < 2 || argc > 3)
+ {
+ printf("usage: %s file [output-file]\n", progname);
+ exit(1);
+ }
+ in_name = argv[1];
+ if (argc > 2) out_name = argv[2];
+
+/*
+ * Step 1: initialize the LZO library
+ */
+ 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");
+ exit(1);
+ }
+
+/*
+ * Step 2: allocate the work-memory
+ */
+ wrk_len = 1;
+#ifdef USE_LZO1X
+ if (wrk_len < LZO1X_999_MEM_COMPRESS)
+ wrk_len = LZO1X_999_MEM_COMPRESS;
+#endif
+#ifdef USE_LZO1Y
+ if (wrk_len < LZO1Y_999_MEM_COMPRESS)
+ wrk_len = LZO1Y_999_MEM_COMPRESS;
+#endif
+ wrkmem = (lzo_voidp) xmalloc(wrk_len);
+ if (wrkmem == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+
+/*
+ * Step 3: open the input file
+ */
+ fp = fopen(in_name,"rb");
+ if (fp == NULL)
+ {
+ printf("%s: cannot open file %s\n", progname, in_name);
+ exit(1);
+ }
+ fseek(fp, 0, SEEK_END);
+ l = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ if (l <= 0)
+ {
+ printf("%s: %s: empty file\n", progname, in_name);
+ fclose(fp); fp = NULL;
+ exit(1);
+ }
+ in_len = (lzo_uint) l;
+ out_bufsize = in_len + in_len / 16 + 64 + 3;
+ best_len = in_len;
+
+/*
+ * Step 4: allocate compression buffers and read the file
+ */
+ in = (lzo_bytep) xmalloc(in_len);
+ out = (lzo_bytep) xmalloc(out_bufsize);
+ if (in == NULL || out == NULL)
+ {
+ printf("%s: out of memory\n", progname);
+ exit(1);
+ }
+ in_len = (lzo_uint) lzo_fread(fp, in, in_len);
+ printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
+ fclose(fp); fp = NULL;
+
+/*
+ * Step 5: compute a checksum of the uncompressed data
+ */
+ uncompressed_checksum = lzo_adler32(0,NULL,0);
+ uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);
+
+/*
+ * Step 6a: compress from 'in' to 'out' with LZO1X-999
+ */
+#ifdef USE_LZO1X
+ for (lazy = 0; lazy <= max_try_lazy; lazy++)
+ {
+ out_len = out_bufsize;
+ r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
+ NULL, 0, 0,
+ lazy, big, big, big, big, flags);
+ if (r != LZO_E_OK)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ exit(1);
+ }
+ printf("LZO1X-999: lazy =%2d: %8lu -> %8lu\n",
+ lazy, (unsigned long) in_len, (unsigned long) out_len);
+ if (out_len < best_len)
+ {
+ best_len = out_len;
+ best_lazy = lazy;
+ best_compress = 1; /* LZO1X-999 */
+ }
+ }
+#endif /* USE_LZO1X */
+
+/*
+ * Step 6b: compress from 'in' to 'out' with LZO1Y-999
+ */
+#ifdef USE_LZO1Y
+ for (lazy = 0; lazy <= max_try_lazy; lazy++)
+ {
+ out_len = out_bufsize;
+ r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
+ NULL, 0, 0,
+ lazy, big, big, big, big, flags);
+ if (r != LZO_E_OK)
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ exit(1);
+ }
+ printf("LZO1Y-999: lazy =%2d: %8lu -> %8lu\n",
+ lazy, (unsigned long) in_len, (unsigned long) out_len);
+ if (out_len < best_len)
+ {
+ best_len = out_len;
+ best_lazy = lazy;
+ best_compress = 2; /* LZO1Y-999 */
+ }
+ }
+#endif /* USE_LZO1Y */
+
+/*
+ * Step 7: check if compressible
+ */
+ if (best_len >= in_len)
+ {
+ printf("This file contains incompressible data.\n");
+ return 0;
+ }
+
+/*
+ * Step 8: compress data again using the best compressor found
+ */
+ out_len = out_bufsize;
+ if (best_compress == 1)
+ r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
+ NULL, 0, 0,
+ best_lazy, big, big, big, big, flags);
+ else if (best_compress == 2)
+ r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
+ NULL, 0, 0,
+ best_lazy, big, big, big, big, flags);
+ else
+ r = -100;
+ assert(r == LZO_E_OK);
+ assert(out_len == best_len);
+
+/*
+ * Step 9: optimize compressed data (compressed data is in 'out' buffer)
+ */
+#if 1
+ /* Optimization does not require any data in the buffer that will
+ * hold the uncompressed data. To prove this, we clear the buffer.
+ */
+ lzo_memset(in,0,in_len);
+#endif
+
+ orig_len = in_len;
+ r = -100;
+#ifdef USE_LZO1X
+ if (best_compress == 1)
+ r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
+#endif
+#ifdef USE_LZO1Y
+ if (best_compress == 2)
+ r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
+#endif
+ if (r != LZO_E_OK || orig_len != in_len)
+ {
+ /* this should NEVER happen */
+ printf("internal error - optimization failed: %d\n", r);
+ exit(1);
+ }
+
+/*
+ * Step 10: compute a checksum of the compressed data
+ */
+ compressed_checksum = lzo_adler32(0,NULL,0);
+ compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);
+
+/*
+ * Step 11: write compressed data to a file
+ */
+ printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
+ progname, in_name, (long) in_len, (long) out_len,
+ (long) uncompressed_checksum, (long) compressed_checksum);
+
+ if (out_name && out_name[0])
+ {
+ printf("%s: writing to file %s\n", progname, out_name);
+ fp = fopen(out_name,"wb");
+ if (fp == NULL)
+ {
+ printf("%s: cannot open output file %s\n", progname, out_name);
+ exit(1);
+ }
+ if (lzo_fwrite(fp, out, out_len) != out_len || fclose(fp) != 0)
+ {
+ printf("%s: write error !!\n", progname);
+ exit(1);
+ }
+ }
+
+/*
+ * Step 12: verify decompression
+ */
+#ifdef PARANOID
+ lzo_memset(in,0,in_len); /* paranoia - clear output buffer */
+ orig_len = in_len;
+ r = -100;
+#ifdef USE_LZO1X
+ if (best_compress == 1)
+ r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
+#endif
+#ifdef USE_LZO1Y
+ if (best_compress == 2)
+ r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
+#endif
+ if (r != LZO_E_OK || orig_len != in_len)
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression failed: %d\n", r);
+ exit(1);
+ }
+ if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression data error\n");
+ exit(1);
+ }
+ /* Now you could also verify decompression under similar conditions as in
+ * your application, e.g. overlapping assembler decompression etc.
+ */
+#endif
+
+ lzo_free(in);
+ lzo_free(out);
+ lzo_free(wrkmem);
+
+ return 0;
+}
+
+/*
+vi:ts=4:et
+*/
+
diff --git a/examples/simple.c b/examples/simple.c
new file mode 100644
index 0000000..9d06cb5
--- /dev/null
+++ b/examples/simple.c
@@ -0,0 +1,173 @@
+/* simple.c -- the annotated simple example program for the LZO library
+
+ 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
+ Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
+ Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
+ All Rights Reserved.
+
+ The LZO library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2 of
+ the License, or (at your option) any later version.
+
+ The LZO library 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the LZO library; see the file COPYING.
+ If not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+ Markus F.X.J. Oberhumer
+ <markus@oberhumer.com>
+ http://www.oberhumer.com/opensource/lzo/
+ */
+
+
+/*************************************************************************
+// This program shows the basic usage of the LZO library.
+// We will compress a block of data and decompress again.
+//
+// See also LZO.FAQ
+**************************************************************************/
+
+/* We will be using the LZO1X-1 algorithm, so we have
+ * to include <lzo1x.h>
+ */
+
+#include "lzo/lzoconf.h"
+#include "lzo/lzo1x.h"
+
+/* portability layer */
+static const char *progname = NULL;
+#define WANT_LZO_MALLOC 1
+#define WANT_XMALLOC 1
+#include "examples/portab.h"
+
+
+/* We want to compress the data block at 'in' with length 'IN_LEN' to
+ * the block at 'out'. Because the input block may be incompressible,
+ * we must provide a little more output space in case that compression
+ * is not possible.
+ */
+
+#ifndef IN_LEN
+#define IN_LEN (128*1024L)
+#endif
+#define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
+
+
+/*************************************************************************
+//
+**************************************************************************/
+
+int __lzo_cdecl_main main(int argc, char *argv[])
+{
+ int r;
+ lzo_bytep in;
+ lzo_bytep out;
+ lzo_voidp wrkmem;
+ lzo_uint in_len;
+ lzo_uint out_len;
+ lzo_uint new_len;
+
+ if (argc < 0 && argv == NULL) /* avoid warning about unused args */
+ return 0;
+
+ printf("\nLZO real-time data compression library (v%s, %s).\n",
+ lzo_version_string(), lzo_version_date());
+ printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
+
+/*
+ * Step 1: initialize the LZO library
+ */
+ 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");
+ return 4;
+ }
+
+/*
+ * Step 2: allocate blocks and the work-memory
+ */
+ in = (lzo_bytep) xmalloc(IN_LEN);
+ out = (lzo_bytep) xmalloc(OUT_LEN);
+ wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
+ if (in == NULL || out == NULL || wrkmem == NULL)
+ {
+ printf("out of memory\n");
+ return 3;
+ }
+
+/*
+ * Step 3: prepare the input block that will get compressed.
+ * We just fill it with zeros in this example program,
+ * but you would use your real-world data here.
+ */
+ in_len = IN_LEN;
+ lzo_memset(in,0,in_len);
+
+/*
+ * Step 4: compress from 'in' to 'out' with LZO1X-1
+ */
+ r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
+ if (r == LZO_E_OK)
+ printf("compressed %lu bytes into %lu bytes\n",
+ (unsigned long) in_len, (unsigned long) out_len);
+ else
+ {
+ /* this should NEVER happen */
+ printf("internal error - compression failed: %d\n", r);
+ return 2;
+ }
+ /* check for an incompressible block */
+ if (out_len >= in_len)
+ {
+ printf("This block contains incompressible data.\n");
+ return 0;
+ }
+
+/*
+ * Step 5: decompress again, now going from 'out' to 'in'
+ */
+ new_len = in_len;
+ r = lzo1x_decompress(out,out_len,in,&new_len,NULL);
+ if (r == LZO_E_OK && new_len == in_len)
+ printf("decompressed %lu bytes back into %lu bytes\n",
+ (unsigned long) out_len, (unsigned long) in_len);
+ else
+ {
+ /* this should NEVER happen */
+ printf("internal error - decompression failed: %d\n", r);
+ return 1;
+ }
+
+ lzo_free(wrkmem);
+ lzo_free(out);
+ lzo_free(in);
+ printf("Simple compression test passed.\n");
+ return 0;
+}
+
+/*
+vi:ts=4:et
+*/
+