#include <lzo/lzoconf.h>
#include <lzo/lzo1x.h>
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 long opt_overhead = 0;
static unsigned long total_files = 0;
static unsigned long total_in = 0;
static 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;
lzo_bytep out = NULL;
lzo_uint out_len;
lzo_bytep overlap = NULL;
lzo_uint overhead;
lzo_uint offset;
lzo_uint new_len = 0;
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;
if ((long) in_len != l || l > 256L * 1024L * 1024L)
{
printf("%s: %s: file is too big -- skipping\n", progname, in_name);
goto next_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;
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)
{
printf("internal error - compression failed: %d\n", r);
exit(1);
}
printf("%-25s %8lu -> %8lu\n", "LZO1X-1:", (unsigned long) in_len, (unsigned long) out_len);
overhead = in_len > 0xbfff ? 0xbfff : in_len;
overhead += in_len / 16 + 64 + 3;
overlap = (lzo_bytep) xmalloc(in_len + overhead);
offset = overhead;
lzo_memcpy(overlap + offset, in, in_len);
r = lzo1x_1_compress(overlap + offset, in_len, overlap, &new_len, wrkmem);
if (r != LZO_E_OK)
{
printf("in-place compression failed: %d\n", r);
exit(1);
}
if (new_len != out_len || lzo_memcmp(out, overlap, out_len) != 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)
{
printf("in-place compression data error\n");
exit(1);
}
lzo_free(tmp);
}
printf(" in-place compression: %8lu -> %8lu overhead: %7lu\n",
(unsigned long) in_len, (unsigned long) new_len, (unsigned long) overhead);
lzo_free(overlap); overlap = NULL;
if (opt_overhead == 0 || out_len >= in_len)
overhead = in_len / 16 + 64 + 3;
else
overhead = (lzo_uint) opt_overhead;
overlap = (lzo_bytep) xmalloc(in_len + overhead);
offset = in_len + overhead - out_len;
lzo_memcpy(overlap + offset, out, out_len);
new_len = in_len;
r = lzo1x_decompress_safe(overlap + offset, out_len, overlap, &new_len, NULL);
if (r != LZO_E_OK)
{
printf("in-place decompression failed: %d - increase 'opt_overhead'\n", r);
exit(1);
}
if (new_len != in_len || lzo_memcmp(in, overlap, in_len) != 0)
{
printf("in-place decompression data error - increase 'opt_overhead'\n");
exit(1);
}
printf(" in-place 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-2017 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
progname = argv[0];
if (i < argc && argv[i][0] == '-')
opt_overhead = atol(&argv[i++][1]);
#if 1
if (opt_overhead != 0 && opt_overhead < 4)
{
printf("%s: invalid overhead value %ld\n", progname, opt_overhead);
exit(1);
}
#endif
if (i >= argc)
{
printf("usage: %s [-overhead_in_bytes] file..\n", progname);
exit(1);
}
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);
}
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;
}