kusano 7d535a
/* precomp2.c -- example program: how to generate pre-compressed data
kusano 7d535a
kusano 7d535a
   This file is part of the LZO real-time data compression library.
kusano 7d535a
kusano 7d535a
   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   All Rights Reserved.
kusano 7d535a
kusano 7d535a
   The LZO library is free software; you can redistribute it and/or
kusano 7d535a
   modify it under the terms of the GNU General Public License as
kusano 7d535a
   published by the Free Software Foundation; either version 2 of
kusano 7d535a
   the License, or (at your option) any later version.
kusano 7d535a
kusano 7d535a
   The LZO library is distributed in the hope that it will be useful,
kusano 7d535a
   but WITHOUT ANY WARRANTY; without even the implied warranty of
kusano 7d535a
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
kusano 7d535a
   GNU General Public License for more details.
kusano 7d535a
kusano 7d535a
   You should have received a copy of the GNU General Public License
kusano 7d535a
   along with the LZO library; see the file COPYING.
kusano 7d535a
   If not, write to the Free Software Foundation, Inc.,
kusano 7d535a
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
kusano 7d535a
kusano 7d535a
   Markus F.X.J. Oberhumer
kusano 7d535a
   <markus@oberhumer.com></markus@oberhumer.com>
kusano 7d535a
   http://www.oberhumer.com/opensource/lzo/
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
// This program shows how to generate pre-compressed data.
kusano 7d535a
//
kusano 7d535a
// Please study precomp.c first.
kusano 7d535a
//
kusano 7d535a
// We will be trying LZO1X-999 and LZO1Y-999, and we will be trying
kusano 7d535a
// various parameters using the internal interface to squeeze out
kusano 7d535a
// a little bit of extra compression.
kusano 7d535a
//
kusano 7d535a
// NOTE: this program can be quite slow for highly redundant files
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
#include "lzo/lzoconf.h"
kusano 7d535a
#include "lzo/lzo1x.h"
kusano 7d535a
#include "lzo/lzo1y.h"
kusano 7d535a
kusano 7d535a
LZO_EXTERN(int)
kusano 7d535a
lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                              const lzo_bytep dict, lzo_uint dict_len,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    int try_lazy,
kusano 7d535a
                                    lzo_uint good_length,
kusano 7d535a
                                    lzo_uint max_lazy,
kusano 7d535a
                                    lzo_uint nice_length,
kusano 7d535a
                                    lzo_uint max_chain,
kusano 7d535a
                                    lzo_uint32 flags );
kusano 7d535a
kusano 7d535a
LZO_EXTERN(int)
kusano 7d535a
lzo1y_999_compress_internal ( const lzo_bytep in , lzo_uint  in_len,
kusano 7d535a
                                    lzo_bytep out, lzo_uintp out_len,
kusano 7d535a
                                    lzo_voidp wrkmem,
kusano 7d535a
                              const lzo_bytep dict, lzo_uint dict_len,
kusano 7d535a
                                    lzo_callback_p cb,
kusano 7d535a
                                    int try_lazy,
kusano 7d535a
                                    lzo_uint good_length,
kusano 7d535a
                                    lzo_uint max_lazy,
kusano 7d535a
                                    lzo_uint nice_length,
kusano 7d535a
                                    lzo_uint max_chain,
kusano 7d535a
                                    lzo_uint32 flags );
kusano 7d535a
kusano 7d535a
#define USE_LZO1X 1
kusano 7d535a
#define USE_LZO1Y 1
kusano 7d535a
kusano 7d535a
#define PARANOID 1
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* portability layer */
kusano 7d535a
#define WANT_LZO_MALLOC 1
kusano 7d535a
#define WANT_LZO_FREAD 1
kusano 7d535a
#define WANT_LZO_WILDARGV 1
kusano 7d535a
#include "examples/portab.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
int __lzo_cdecl_main main(int argc, char *argv[])
kusano 7d535a
{
kusano 7d535a
    int r;
kusano 7d535a
    int lazy;
kusano 7d535a
    const int max_try_lazy = 5;
kusano 7d535a
    const lzo_uint big = 65536L;    /* can result in very slow compression */
kusano 7d535a
    const lzo_uint32 flags = 0x1;
kusano 7d535a
kusano 7d535a
    lzo_bytep in;
kusano 7d535a
    lzo_uint in_len;
kusano 7d535a
kusano 7d535a
    lzo_bytep out;
kusano 7d535a
    lzo_uint out_bufsize;
kusano 7d535a
    lzo_uint out_len = 0;
kusano 7d535a
kusano 7d535a
    lzo_bytep wrkmem;
kusano 7d535a
    lzo_uint wrk_len;
kusano 7d535a
kusano 7d535a
    lzo_uint best_len;
kusano 7d535a
    int best_compress = -1;
kusano 7d535a
    int best_lazy = -1;
kusano 7d535a
kusano 7d535a
    lzo_uint orig_len;
kusano 7d535a
    lzo_uint32 uncompressed_checksum;
kusano 7d535a
    lzo_uint32 compressed_checksum;
kusano 7d535a
kusano 7d535a
    FILE *f;
kusano 7d535a
    const char *progname = NULL;
kusano 7d535a
    const char *in_name = NULL;
kusano 7d535a
    const char *out_name = NULL;
kusano 7d535a
    long l;
kusano 7d535a
kusano 7d535a
kusano 7d535a
    lzo_wildargv(&argc, &argv);
kusano 7d535a
kusano 7d535a
    printf("\nLZO real-time data compression library (v%s, %s).\n",
kusano 7d535a
           lzo_version_string(), lzo_version_date());
kusano 7d535a
    printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
kusano 7d535a
kusano 7d535a
    progname = argv[0];
kusano 7d535a
    if (argc < 2 || argc > 3)
kusano 7d535a
    {
kusano 7d535a
        printf("usage: %s file [output-file]\n", progname);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    in_name = argv[1];
kusano 7d535a
    if (argc > 2) out_name = argv[2];
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 1: initialize the LZO library
kusano 7d535a
 */
kusano 7d535a
    if (lzo_init() != LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        printf("internal error - lzo_init() failed !!!\n");
kusano 7d535a
        printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n");
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 2: allocate the work-memory
kusano 7d535a
 */
kusano 7d535a
    wrk_len = 1;
kusano 7d535a
#ifdef USE_LZO1X
kusano 7d535a
    if (wrk_len < LZO1X_999_MEM_COMPRESS)
kusano 7d535a
        wrk_len = LZO1X_999_MEM_COMPRESS;
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_LZO1Y
kusano 7d535a
    if (wrk_len < LZO1Y_999_MEM_COMPRESS)
kusano 7d535a
        wrk_len = LZO1Y_999_MEM_COMPRESS;
kusano 7d535a
#endif
kusano 7d535a
    wrkmem = (lzo_bytep) lzo_malloc(wrk_len);
kusano 7d535a
    if (wrkmem == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: out of memory\n", progname);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 3: open the input file
kusano 7d535a
 */
kusano 7d535a
    f = fopen(in_name,"rb");
kusano 7d535a
    if (f == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: cannot open file %s\n", progname, in_name);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    fseek(f,0,SEEK_END);
kusano 7d535a
    l = ftell(f);
kusano 7d535a
    fseek(f,0,SEEK_SET);
kusano 7d535a
    if (l <= 0)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: %s: empty file\n", progname, in_name);
kusano 7d535a
        fclose(f);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    in_len = (lzo_uint) l;
kusano 7d535a
    out_bufsize = in_len + in_len / 16 + 64 + 3;
kusano 7d535a
    best_len = in_len;
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 4: allocate compression buffers and read the file
kusano 7d535a
 */
kusano 7d535a
    in = (lzo_bytep) lzo_malloc(in_len);
kusano 7d535a
    out = (lzo_bytep) lzo_malloc(out_bufsize);
kusano 7d535a
    if (in == NULL || out == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("%s: out of memory\n", progname);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    in_len = (lzo_uint) lzo_fread(f,in,in_len);
kusano 7d535a
    printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
kusano 7d535a
    fclose(f);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 5: compute a checksum of the uncompressed data
kusano 7d535a
 */
kusano 7d535a
    uncompressed_checksum = lzo_adler32(0,NULL,0);
kusano 7d535a
    uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 6a: compress from `in' to `out' with LZO1X-999
kusano 7d535a
 */
kusano 7d535a
#ifdef USE_LZO1X
kusano 7d535a
    for (lazy = 0; lazy <= max_try_lazy; lazy++)
kusano 7d535a
    {
kusano 7d535a
        out_len = out_bufsize;
kusano 7d535a
        r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
kusano 7d535a
                                        NULL, 0, 0,
kusano 7d535a
                                        lazy, big, big, big, big, flags);
kusano 7d535a
        if (r != LZO_E_OK)
kusano 7d535a
        {
kusano 7d535a
            /* this should NEVER happen */
kusano 7d535a
            printf("internal error - compression failed: %d\n", r);
kusano 7d535a
            exit(1);
kusano 7d535a
        }
kusano 7d535a
        printf("LZO1X-999: lazy =%2d: %8lu -> %8lu\n",
kusano 7d535a
                lazy, (unsigned long) in_len, (unsigned long) out_len);
kusano 7d535a
        if (out_len < best_len)
kusano 7d535a
        {
kusano 7d535a
            best_len = out_len;
kusano 7d535a
            best_lazy = lazy;
kusano 7d535a
            best_compress = 1;      /* LZO1X-999 */
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
#endif /* USE_LZO1X */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 6b: compress from `in' to `out' with LZO1Y-999
kusano 7d535a
 */
kusano 7d535a
#ifdef USE_LZO1Y
kusano 7d535a
    for (lazy = 0; lazy <= max_try_lazy; lazy++)
kusano 7d535a
    {
kusano 7d535a
        out_len = out_bufsize;
kusano 7d535a
        r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
kusano 7d535a
                                        NULL, 0, 0,
kusano 7d535a
                                        lazy, big, big, big, big, flags);
kusano 7d535a
        if (r != LZO_E_OK)
kusano 7d535a
        {
kusano 7d535a
            /* this should NEVER happen */
kusano 7d535a
            printf("internal error - compression failed: %d\n", r);
kusano 7d535a
            exit(1);
kusano 7d535a
        }
kusano 7d535a
        printf("LZO1Y-999: lazy =%2d: %8lu -> %8lu\n",
kusano 7d535a
                lazy, (unsigned long) in_len, (unsigned long) out_len);
kusano 7d535a
        if (out_len < best_len)
kusano 7d535a
        {
kusano 7d535a
            best_len = out_len;
kusano 7d535a
            best_lazy = lazy;
kusano 7d535a
            best_compress = 2;      /* LZO1Y-999 */
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
#endif /* USE_LZO1Y */
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 7: check if compressible
kusano 7d535a
 */
kusano 7d535a
    if (best_len >= in_len)
kusano 7d535a
    {
kusano 7d535a
        printf("This file contains incompressible data.\n");
kusano 7d535a
        return 0;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 8: compress data again using the best compressor found
kusano 7d535a
 */
kusano 7d535a
    out_len = out_bufsize;
kusano 7d535a
    if (best_compress == 1)
kusano 7d535a
        r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
kusano 7d535a
                                        NULL, 0, 0,
kusano 7d535a
                                        best_lazy, big, big, big, big, flags);
kusano 7d535a
    else if (best_compress == 2)
kusano 7d535a
        r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
kusano 7d535a
                                        NULL, 0, 0,
kusano 7d535a
                                        best_lazy, big, big, big, big, flags);
kusano 7d535a
    else
kusano 7d535a
        r = -100;
kusano 7d535a
    assert(r == LZO_E_OK);
kusano 7d535a
    assert(out_len == best_len);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 9: optimize compressed data (compressed data is in `out' buffer)
kusano 7d535a
 */
kusano 7d535a
#if 1
kusano 7d535a
    /* Optimization does not require any data in the buffer that will
kusano 7d535a
     * hold the uncompressed data. To prove this, we clear the buffer.
kusano 7d535a
     */
kusano 7d535a
    lzo_memset(in,0,in_len);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    orig_len = in_len;
kusano 7d535a
    r = -100;
kusano 7d535a
#ifdef USE_LZO1X
kusano 7d535a
    if (best_compress == 1)
kusano 7d535a
        r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_LZO1Y
kusano 7d535a
    if (best_compress == 2)
kusano 7d535a
        r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
kusano 7d535a
#endif
kusano 7d535a
    if (r != LZO_E_OK || orig_len != in_len)
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - optimization failed: %d\n", r);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 10: compute a checksum of the compressed data
kusano 7d535a
 */
kusano 7d535a
    compressed_checksum = lzo_adler32(0,NULL,0);
kusano 7d535a
    compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 11: write compressed data to a file
kusano 7d535a
 */
kusano 7d535a
    printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
kusano 7d535a
            progname, in_name, (long) in_len, (long) out_len,
kusano 7d535a
            (long) uncompressed_checksum, (long) compressed_checksum);
kusano 7d535a
kusano 7d535a
    if (out_name && out_name[0])
kusano 7d535a
    {
kusano 7d535a
        printf("%s: writing to file %s\n", progname, out_name);
kusano 7d535a
        f = fopen(out_name,"wb");
kusano 7d535a
        if (f == NULL)
kusano 7d535a
        {
kusano 7d535a
            printf("%s: cannot open output file %s\n", progname, out_name);
kusano 7d535a
            exit(1);
kusano 7d535a
        }
kusano 7d535a
        if (lzo_fwrite(f,out,out_len) != out_len || fclose(f) != 0)
kusano 7d535a
        {
kusano 7d535a
            printf("%s: write error !!\n", progname);
kusano 7d535a
            exit(1);
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 12: verify decompression
kusano 7d535a
 */
kusano 7d535a
#ifdef PARANOID
kusano 7d535a
    lzo_memset(in,0,in_len);    /* paranoia - clear output buffer */
kusano 7d535a
    orig_len = in_len;
kusano 7d535a
    r = -100;
kusano 7d535a
#ifdef USE_LZO1X
kusano 7d535a
    if (best_compress == 1)
kusano 7d535a
        r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
kusano 7d535a
#endif
kusano 7d535a
#ifdef USE_LZO1Y
kusano 7d535a
    if (best_compress == 2)
kusano 7d535a
        r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
kusano 7d535a
#endif
kusano 7d535a
    if (r != LZO_E_OK || orig_len != in_len)
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - decompression failed: %d\n", r);
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - decompression data error\n");
kusano 7d535a
        exit(1);
kusano 7d535a
    }
kusano 7d535a
    /* Now you could also verify decompression under similar conditions as in
kusano 7d535a
     * your application, e.g. overlapping assembler decompression etc.
kusano 7d535a
     */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    lzo_free(in);
kusano 7d535a
    lzo_free(out);
kusano 7d535a
    lzo_free(wrkmem);
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a