Blame examples/precomp.c

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