Blame examples/precomp.c

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