コード例 #1
0
ファイル: ltest.c プロジェクト: santolucito/CCache
static void read_dict(const char *file_name)
{
	FILE *f;

	dict_len = 0;
	f = fopen(file_name,"rb");
	if (f)
	{
		dict_len = lzo_fread(f,dict,DICT_LEN);
		fclose(f);
		dict_adler32 = lzo_adler32(0,NULL,0);
		dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
	}
}
コード例 #2
0
ファイル: dict.c プロジェクト: AmEv7Fam/opentoonz
int do_file ( const char *in_name, int level )
{
    int r;
    lzo_bytep in;
    lzo_bytep out;
    lzo_bytep newb;
    lzo_bytep wrkmem;
    lzo_uint in_len;
    lzo_uint out_len;
    lzo_uint new_len;
    long l;
    FILE *f;

/*
 * Step 1: open the input file
 */
    f = fopen(in_name,"rb");
    if (f == 0)
    {
        printf("%s: cannot open file %s\n", progname, in_name);
        return 0;   /* no error */
    }
    fseek(f,0,SEEK_END);
    l = ftell(f);
    fseek(f,0,SEEK_SET);
    if (l <= 0)
    {
        printf("%s: %s: empty file -- skipping\n", progname, in_name);
        fclose(f);
        return 0;
    }
    in_len = (lzo_uint) l;

/*
 * Step 2: allocate compression buffers and read the file
 */
    in = (lzo_bytep) lzo_malloc(in_len);
    out = (lzo_bytep) lzo_malloc(in_len + in_len / 16 + 64 + 3);
    newb = (lzo_bytep) lzo_malloc(in_len);
    wrkmem = (lzo_bytep) lzo_malloc(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(f,in,in_len);
    fclose(f);

/*
 * 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, 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;
}
コード例 #3
0
ファイル: dict.c プロジェクト: AmEv7Fam/opentoonz
int __lzo_cdecl_main main(int argc, char *argv[])
{
    int i = 1;
    int r;
    const char *dict_name;
    FILE *f;
    time_t t_total;
    int 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-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");

    progname = argv[0];

    if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1]))
        level = atoi(&argv[i++][1]);

    if (i + 1 >= argc || level < 1 || level > 9)
    {
        printf("usage: %s [-level] [ dictionary-file | -n ]  file...\n", progname);
        exit(1);
    }
    printf("Compression level is LZO1X-999/%d\n", 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) lzo_malloc(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
    {
        f = fopen(dict_name,"rb");
        if (!f)
        {
            printf("%s: cannot open dictionary file %s\n", progname, dict_name);
            exit(1);
        }
        dict_len = (lzo_uint) lzo_fread(f,dict,DICT_LEN);
        fclose(f);
    }

    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], 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;
}
コード例 #4
0
ファイル: precomp.c プロジェクト: TomMD/ics-openvpn
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 wrkmem_size;

    lzo_uint best_len;
    int best_compress = -1;

    lzo_uint orig_len;
    lzo_uint32_t uncompressed_checksum;
    lzo_uint32_t 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-2017 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
 */
    wrkmem_size = 1;
#ifdef USE_LZO1X
    wrkmem_size = (LZO1X_999_MEM_COMPRESS > wrkmem_size) ? LZO1X_999_MEM_COMPRESS : wrkmem_size;
#endif
#ifdef USE_LZO1Y
    wrkmem_size = (LZO1Y_999_MEM_COMPRESS > wrkmem_size) ? LZO1Y_999_MEM_COMPRESS : wrkmem_size;
#endif
    wrkmem = (lzo_voidp) xmalloc(wrkmem_size);
    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;
}
コード例 #5
0
ファイル: ltest.c プロジェクト: santolucito/CCache
int load_file ( const char *file_name, lzo_uintp len )
{
	FILE *f;
	long ll = -1;
	unsigned long ul;
	lzo_uint l;
	int r;

#if 0
	if (last_file && strcmp(file_name,last_file) == 0)
	{
		*len = last_len;
		return EXIT_OK;
	}
#endif

	*len = 0;
	unload_file();

	f = fopen(file_name,"rb");
	if (f == NULL)
	{
		fprintf(stderr,"%s: ",file_name);
		perror("fopen");
		fflush(stderr);
		return EXIT_FILE;
	}
    r = fseek(f,0,SEEK_END);
	if (r == 0)
	{
		ll = ftell(f);
    	r = fseek(f,0,SEEK_SET);
	}
	if (r != 0 || ll < 0)
	{
		fprintf(stderr,"%s: ",file_name);
		perror("fseek");
		fflush(stderr);
		return EXIT_FILE;
	}
	ul = ll;

#ifdef USE_MALLOC
	l = (ul >= LZO_UINT_MAX - 512 ? LZO_UINT_MAX - 512 : (lzo_uint) ul);
	if (opt_max_data_len > 0 && l > opt_max_data_len)
		l = opt_max_data_len;
	_data = malloc (l + 256);
	if (_data == NULL)
	{
		perror("malloc");
		fflush(stderr);
		return EXIT_MEM;
	}
#else
	l = (ul >= DATA_LEN ? (lzo_uint) DATA_LEN : (lzo_uint) ul);
	if (opt_max_data_len > 0 && l > opt_max_data_len)
		l = opt_max_data_len;
#endif
	data = LZO_PTR_ALIGN_UP(_data,256);
	l = lzo_fread(f,data,l);
	if (fclose(f) != 0)
	{
		unload_file();
		fprintf(stderr,"%s: ",file_name);
		perror("fclose");
		fflush(stderr);
		return EXIT_FILE;
	}

	last_file = file_name;
	last_len = l;
	*len = l;
	return EXIT_OK;
}
コード例 #6
0
ファイル: overlap.c プロジェクト: jimmy-kuo/websearch
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;
}
コード例 #7
0
ファイル: precomp2.c プロジェクト: santolucito/CCache
int 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_byte *in;
	lzo_uint in_len;

	lzo_byte *out;
	lzo_uint out_len = 0;

	lzo_byte *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 *f;
	const char *progname = NULL;
	const char *in_name = NULL;
	const char *out_name = NULL;
	long l;


#if defined(__EMX__)
	_response(&argc,&argv);
	_wildcard(&argc,&argv);
#endif

	printf("\nLZO real-time data compression library (v%s, %s).\n",
	        lzo_version_string(), lzo_version_date());
	printf("Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer\n\n");

	progname = argv[0];
	if (argc < 2 || argc > 3)
	{
		printf("usage: %s file [output-file]\n", progname);
		exit(1);
	}
 	in_name = argv[1];
 	out_name = (argc > 2) ? argv[2] : NULL;

/*
 * Step 1: initialize the LZO library
 */
	if (lzo_init() != LZO_E_OK)
	{
		printf("lzo_init() failed !!!\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_bytep) lzo_malloc(wrk_len);
	if (wrkmem == NULL)
	{
		printf("%s: out of memory\n", progname);
		exit(1);
	}

/*
 * Step 3: open the input file
 */
 	f = fopen(in_name,"rb");
	if (f == NULL)
	{
		printf("%s: cannot open file %s\n", progname, in_name);
		exit(1);
	}
	fseek(f,0,SEEK_END);
	l = ftell(f);
	fseek(f,0,SEEK_SET);
	if (l <= 0)
	{
		printf("%s: %s: empty file\n", progname, in_name);
		fclose(f);
		exit(1);
	}
	in_len = (lzo_uint) l;
	best_len = in_len;

/*
 * Step 4: allocate compression buffers and read the file
 */
	in = (lzo_bytep) lzo_malloc(in_len);
	out = (lzo_bytep) lzo_malloc(in_len + in_len / 64 + 16 + 3);
	if (in == NULL || out == NULL)
	{
		printf("%s: out of memory\n", progname);
		exit(1);
	}
	in_len = lzo_fread(f,in,in_len);
	printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
	fclose(f);

/*
 * 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++)
	{
		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, (long) in_len, (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++)
	{
		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, (long) in_len, (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
 */
	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;
	if (best_compress == 1)
		r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
	else if (best_compress == 2)
		r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
	else
		r = -100;
	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);
		f = fopen(out_name,"wb");
		if (f == NULL)
		{
			printf("%s: cannot open output file %s\n", progname, out_name);
			exit(1);
		}
		if (lzo_fwrite(f,out,out_len) != out_len || fclose(f) != 0)
		{
			printf("%s: write error !!\n", progname);
			exit(1);
		}
	}

/*
 * Step 12: verify decompression
 */
#ifdef PARANOID
 	orig_len = in_len;
	if (best_compress == 1)
		r = lzo1x_decompress(out,out_len,in,&orig_len,NULL);
	else if (best_compress == 2)
		r = lzo1y_decompress(out,out_len,in,&orig_len,NULL);
	else
		r = -100;
	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;
}