Beispiel #1
0
int main(int argc, char *argv[])
{
    lzo_bytep block;
    lzo_uint block_len;
    lzo_uint32 adler, crc;

    if (argc < 0 && argv == NULL)   /* avoid warning about unused args */
        return 0;

    if (lzo_init() != LZO_E_OK)
    {
        printf("lzo_init() failed !!!\n");
        return 4;
    }

/* prepare the block */
    block_len = 128 * 1024L;
    block = (lzo_bytep) lzo_malloc(block_len);
    if (block == NULL)
    {
        printf("out of memory\n");
        return 3;
    }
    lzo_memset(block, 0, block_len);

/* adler32 checksum */
    adler = lzo_adler32(0, NULL, 0);
    adler = lzo_adler32(adler, block, block_len);
    if (adler != 0x001e0001UL)
    {
        printf("adler32 checksum error !!! (0x%08lx)\n", (long) adler);
        return 2;
    }

/* crc32 checksum */
    crc = lzo_crc32(0, NULL, 0);
    crc = lzo_crc32(crc, block, block_len);
    if (crc != 0x7ee8cdcdUL)
    {
        printf("crc32 checksum error !!! (0x%08lx)\n", (long) crc);
        return 1;
    }

    lzo_free(block);
    printf("Checksum test passed.\n");
    return 0;
}
Beispiel #2
0
static void init_default_dict(void)
{
	lzo_uint i, j, d, dd;

	dict_len = DICT_LEN;
	lzo_memset(dict,0,dict_len);

	/* this default dictionary does not provide good contexts... */
	dd = dict_len;
	if (dd >= 16 * 256)
	{
		dd -= 16 * 256;
		for (i = 0, d = dd; i < 256; i++)
			for (j = 0; j < 16; j++)
				dict[d++] = (unsigned char) i;
	}

	dict_adler32 = lzo_adler32(0,NULL,0);
	dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
}
Beispiel #3
0
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;
}
Beispiel #4
0
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;
}
Beispiel #5
0
static
int process_file ( const compress_t *c, lzo_decompress_t decompress,
                   const char *method_name,
                   const char *file_name, lzo_uint l,
                   int t_loops, int c_loops, int d_loops )
{
	int i;
	unsigned blocks = 0;
	unsigned long compressed_len = 0;
	my_clock_t t_time = 0, c_time = 0, d_time = 0;
	my_clock_t t_start, c_start, d_start;
#ifdef USE_DUMP
	FILE *dump = NULL;

	if (opt_dump_compressed_data)
		dump = fopen(opt_dump_compressed_data,"wb");
#endif

/* process the file */

	t_start = my_clock();
	for (i = 0; i < t_loops; i++)
	{
		lzo_uint len, c_len, c_len_max, d_len = 0;
		lzo_byte *d = data;

		len = l;
		c_len = 0;
		blocks = 0;

		/* process blocks */
		if (len > 0 || opt_try_to_compress_0_bytes) do
		{
			int j;
			int r;

			const lzo_uint bl = len > opt_block_size ? opt_block_size : len;
			lzo_uint bl_overwrite = bl;
#if defined(__LZO_CHECKER)
			lzo_byte *dd = NULL;
			lzo_byte *b1 = NULL;
			lzo_byte *b2 = NULL;
			const lzo_uint b1_len = bl + bl / 64 + 16 + 3;
#else
			lzo_byte * const dd = d;
			lzo_byte * const b1 = block1;
			lzo_byte * const b2 = block2;
			const lzo_uint b1_len = sizeof(_block1) - 256;
			unsigned char random_byte;
			random_byte = (unsigned char) my_clock();
#endif

			blocks++;

			/* may overwrite 3 bytes past the end of the decompressed block */
			if (opt_use_asm_fast_decompressor)
				bl_overwrite += (lzo_uint) sizeof(int) - 1;

#if defined(__LZO_CHECKER)
			/* malloc a block of the exact size to detect any overrun */
			dd = malloc(bl_overwrite > 0 ? bl_overwrite : 1);
			b1 = malloc(b1_len);
			b2 = dd;
			if (dd == NULL || b1 == NULL)
			{
				perror("malloc");
				return EXIT_MEM;
			}
			if (bl > 0)
				memcpy(dd,d,bl);
#endif

		/* compress the block */
			c_len = c_len_max = 0;
			c_start = my_clock();
			for (j = r = 0; r == 0 && j < c_loops; j++)
			{
				c_len = b1_len;
				r = do_compress(c,dd,bl,b1,&c_len);
				if (r == 0 && c_len > c_len_max)
					c_len_max = c_len;
			}
			c_time += my_clock() - c_start;
			if (r != 0)
			{
				printf("  compression failed in block %d (%d) (%lu %lu)\n",
					blocks, r, (long)c_len, (long)bl);
				return EXIT_LZO_ERROR;
			}

		/* optimize the block */
			if (opt_optimize_compressed_data)
			{
				d_len = bl;
				r = do_optimize(c,b1,c_len,b2,&d_len);
				if (r != 0 || d_len != bl)
				{
					printf("  optimization failed in block %d (%d) "
						"(%lu %lu %lu)\n", blocks, r,
						(long)c_len, (long)d_len, (long)bl);
					return EXIT_LZO_ERROR;
				}
			}

#ifdef USE_DUMP
			/* dump compressed data to disk */
			if (dump)
			{
				lzo_fwrite(dump,b1,c_len);
				fflush(dump);
			}
#endif

		/* decompress the block and verify */
#if defined(__LZO_CHECKER)
			lzo_memset(b2,0,bl_overwrite);
#else
			init_mem_checker(b2,_block2,bl_overwrite,random_byte);
#endif
			d_start = my_clock();
			for (j = r = 0; r == 0 && j < d_loops; j++)
			{
				d_len = bl;
				r = do_decompress(c,decompress,b1,c_len,b2,&d_len);
				if (d_len != bl)
					break;
			}
			d_time += my_clock() - d_start;
			if (r != 0)
			{
				printf("  decompression failed in block %d (%d) "
					"(%lu %lu %lu)\n", blocks, r,
					(long)c_len, (long)d_len, (long)bl);
				return EXIT_LZO_ERROR;
			}
			if (d_len != bl)
			{
				printf("  decompression size error in block %d (%lu %lu %lu)\n",
					blocks, (long)c_len, (long)d_len, (long)bl);
				return EXIT_LZO_ERROR;
			}
			if (is_compressor(c))
			{
				if (lzo_memcmp(d,b2,bl) != 0)
				{
					lzo_uint x = 0;
					while (x < bl && b2[x] == d[x])
						x++;
					printf("  decompression data error in block %d at offset "
						"%lu (%lu %lu)\n", blocks, (long)x,
						(long)c_len, (long)d_len);
					if (opt_compute_adler32)
						printf("      checksum: 0x%08lx 0x%08lx\n",
							(long)adler_in, (long)adler_out);
#if 0
					printf("Orig:  ");
					r = (x >= 10) ? -10 : 0 - (int) x;
					for (j = r; j <= 10 && x + j < bl; j++)
						printf(" %02x", (int)d[x+j]);
					printf("\nDecomp:");
					for (j = r; j <= 10 && x + j < bl; j++)
						printf(" %02x", (int)b2[x+j]);
					printf("\n");
#endif
					return EXIT_LZO_ERROR;
				}
				if ((opt_compute_adler32 && adler_in != adler_out) ||
				    (opt_compute_crc32 && crc_in != crc_out))
				{
					printf("  checksum error in block %d (%lu %lu)\n",
						blocks, (long)c_len, (long)d_len);
					printf("      adler32: 0x%08lx 0x%08lx\n",
						(long)adler_in, (long)adler_out);
					printf("      crc32: 0x%08lx 0x%08lx\n",
						(long)crc_in, (long)crc_out);
					return EXIT_LZO_ERROR;
				}
			}

#if defined(__LZO_CHECKER)
			/* free in reverse order of allocations */
			free(b1);
			free(dd);
#else
			if (check_mem(b2,_block2,bl_overwrite,random_byte) != 0)
			{
				printf("  decompression overwrite error in block %d "
					"(%lu %lu %lu)\n",
					blocks, (long)c_len, (long)d_len, (long)bl);
				return EXIT_LZO_ERROR;
			}
#endif

			d += bl;
			len -= bl;
			compressed_len += c_len_max;
		}
		while (len > 0);
	}
	t_time += my_clock() - t_start;

#ifdef USE_DUMP
	if (dump)
		fclose(dump);
	opt_dump_compressed_data = NULL;	/* only dump the first file */
#endif

	print_stats(method_name, file_name,
	            t_loops, c_loops, d_loops,
	            t_time, c_time, d_time,
	            compressed_len, l, blocks);

	return EXIT_OK;
}
Beispiel #6
0
bool DecompressLZOCompressedPackage(UPKReader *Package)
{
    if (!Package->IsCompressed())
    {
        _LogError("Package is not compressed!", "DecompressLZO");
        return false;
    }
    if (!Package->IsLZOCompressed() && !Package->IsFullyCompressed())
    {
        _LogError("Cannot decompress non-LZO compressed packages!", "DecompressLZO");
        return false;
    }
    /// init lzo library
    int lzo_err;
    lzo_uint in_len;
    lzo_uint out_len;
    lzo_uint new_len;
    if (lzo_init() != LZO_E_OK)
    {
        _LogError("LZO library internal error: lzo_init() failed!", "DecompressLZO");
        return false;
    }
    lzo_memset(in, 0, IN_LEN);
    std::stringstream decompressed_stream;
    unsigned int NumCompressedChunks = Package->Summary.NumCompressedChunks;
    if (Package->IsFullyCompressed())
    {
        NumCompressedChunks = 1;
    }
    else
    {
        _LogDebug("Resetting package compression flags...", "DecompressLZO");
        /// reset compression flags
        Package->Summary.CompressionFlags = 0;
        Package->Summary.PackageFlags ^= (uint32_t)UPackageFlags::Compressed;
        Package->Summary.NumCompressedChunks = 0;
        /// serialize package summary
        std::vector<char> sVect = Package->SerializeSummary();
        decompressed_stream.write(sVect.data(), sVect.size());
    }
    _LogDebug("Decompressing...", "DecompressLZO");
    for (unsigned int i = 0; i < NumCompressedChunks; ++i)
    {
        if (Package->IsFullyCompressed())
        {
            Package->UPKStream.seekg(0);
        }
        else
        {
            Package->UPKStream.seekg(Package->Summary.CompressedChunks[i].CompressedOffset);
        }
        _LogDebug("Decompressing chunk #" + ToString(i), "DecompressLZO");
        uint32_t tag = 0;
        Package->UPKStream.read(reinterpret_cast<char*>(&tag), 4);
        if (tag != 0x9E2A83C1)
        {
            _LogError("Missing 0x9E2A83C1 signature!", "DecompressLZO");
            return false;
        }
        uint32_t blockSize = 0;
        Package->UPKStream.read(reinterpret_cast<char*>(&blockSize), 4);
        if (blockSize != IN_LEN)
        {
            _LogError("Incorrect max block size!", "DecompressLZO");
            return false;
        }
        std::vector<uint32_t> sizes(2); /// compressed/uncompressed pairs
        Package->UPKStream.read(reinterpret_cast<char*>(sizes.data()), 4 * sizes.size());
        size_t dataSize = sizes[1]; /// uncompressed data chunk size
        unsigned numBlocks = (dataSize + blockSize - 1) / blockSize;
        _LogDebug("numBlocks = " + ToString(numBlocks), "DecompressLZO");
        if (numBlocks < 1)
        {
            _LogError("Bad data!", "DecompressLZO");
            return false;
        }
        sizes.resize((numBlocks + 1)*2);
        Package->UPKStream.read(reinterpret_cast<char*>(sizes.data()) + 8, 4 * sizes.size() - 8);
        for (unsigned i = 0; i <= numBlocks; ++i)
        {
            _LogDebug("Compressed size = " + ToString(sizes[i * 2]) +
                        + "\tUncompressed size = " + ToString(sizes[i * 2 + 1]), "DecompressLZO");
        }
        std::vector<unsigned char> dataChunk(dataSize);
        std::vector<unsigned char> compressedData(sizes[0]);
        Package->UPKStream.read(reinterpret_cast<char*>(compressedData.data()), compressedData.size());
        size_t blockOffset = 0;
        size_t dataOffset = 0;
        for (unsigned i = 1; i <= numBlocks; ++i)
        {
            out_len = sizes[i * 2]; /// compressed size
            lzo_memcpy(out, compressedData.data() + blockOffset, out_len);
            in_len = sizes[i * 2 + 1]; /// uncompressed size
            new_len = in_len;
            lzo_err = lzo1x_decompress(out, out_len, in, &new_len, NULL);
            if (lzo_err == LZO_E_OK && new_len == in_len)
            {
                _LogDebug("Decompressed " + ToString(out_len) + " bytes back into "
                     + ToString(in_len), "DecompressLZO");
            }
            else
            {
                _LogError("LZO library internal error: decompression failed!", "DecompressLZO");
                return false;
            }
            lzo_memcpy(dataChunk.data() + dataOffset, in, in_len);
            blockOffset += out_len;
            dataOffset += in_len;
        }
        decompressed_stream.write(reinterpret_cast<char*>(dataChunk.data()), dataSize);
    }
    _LogDebug("Package decompressed successfully.", "DecompressLZO");
    Package->UPKStream.str(decompressed_stream.str());
    return Package->ReadPackageHeader();
}
Beispiel #7
0
void _lzo1c_stats_init(lzo_stats_t *lzo_stats)
{
    lzo_memset(lzo_stats,0,sizeof(*lzo_stats));
}
Beispiel #8
0
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;
}