Blame examples/overlap.c

Packit 679830
/* overlap.c -- example program: overlapping (de)compression
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 do overlapping compression and
Packit 679830
// in-place decompression.
Packit 679830
//
Packit 679830
// Please study LZO.FAQ and simple.c first.
Packit 679830
**************************************************************************/
Packit 679830
Packit 679830
#include "lzo/lzoconf.h"
Packit 679830
#include "lzo/lzo1x.h"
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
/* Overhead (in bytes) for the in-place decompression buffer.
Packit 679830
 * Most files need only 16 !
Packit 679830
 * (try 'overlap -16 file' or even 'overlap -8 file')
Packit 679830
 *
Packit 679830
 * Worst case (for files that are compressible by only a few bytes)
Packit 679830
 * is 'in_len / 16 + 64 + 3'. See step 5a) below.
Packit 679830
 *
Packit 679830
 * For overlapping compression '0xbfff + in_len / 16 + 64 + 3' bytes
Packit 679830
 * will be needed. See step 4a) below.
Packit 679830
 */
Packit 679830
Packit 679830
static long opt_overhead = 0;   /* assume worst case */
Packit 679830
Packit 679830
static unsigned long total_files = 0;
Packit 679830
static unsigned long total_in = 0;
Packit 679830
Packit 679830
Packit 679830
/*************************************************************************
Packit 679830
//
Packit 679830
**************************************************************************/
Packit 679830
Packit 679830
static int do_file(const char *in_name)
Packit 679830
{
Packit 679830
    int r;
Packit 679830
    FILE *fp = NULL;
Packit 679830
    long l;
Packit 679830
Packit 679830
    lzo_voidp wrkmem = NULL;
Packit 679830
Packit 679830
    lzo_bytep in = NULL;
Packit 679830
    lzo_uint in_len;                /* uncompressed length */
Packit 679830
Packit 679830
    lzo_bytep out = NULL;
Packit 679830
    lzo_uint out_len;               /* compressed length */
Packit 679830
Packit 679830
    lzo_bytep overlap = NULL;
Packit 679830
    lzo_uint overhead;
Packit 679830
    lzo_uint offset;
Packit 679830
Packit 679830
    lzo_uint new_len = 0;
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 1: open the input file
Packit 679830
 */
Packit 679830
    fp = fopen(in_name, "rb");
Packit 679830
    if (fp == NULL)
Packit 679830
    {
Packit 679830
        printf("%s: %s: cannot open file\n", progname, in_name);
Packit 679830
        goto next_file;
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 -- skipping\n", progname, in_name);
Packit 679830
        goto next_file;
Packit 679830
    }
Packit 679830
    in_len = (lzo_uint) l;
Packit 679830
    if ((long) in_len != l || l > 256L * 1024L * 1024L)
Packit 679830
    {
Packit 679830
        printf("%s: %s: file is too big -- skipping\n", progname, in_name);
Packit 679830
        goto next_file;
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 2: allocate compression buffers and read the file
Packit 679830
 */
Packit 679830
    in = (lzo_bytep) xmalloc(in_len);
Packit 679830
    out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3);
Packit 679830
    wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
Packit 679830
    in_len = (lzo_uint) lzo_fread(fp, in, in_len);
Packit 679830
    fclose(fp); fp = NULL;
Packit 679830
    printf("%s: %s: read %lu bytes\n", progname, in_name, (unsigned long) in_len);
Packit 679830
Packit 679830
    total_files++;
Packit 679830
    total_in += (unsigned long) in_len;
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 3: compress from 'in' to 'out' with LZO1X-1
Packit 679830
 */
Packit 679830
    r = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
Packit 679830
    if (r != LZO_E_OK || out_len > in_len + in_len / 16 + 64 + 3)
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("%-25s %8lu -> %8lu\n", "LZO1X-1:", (unsigned long) in_len, (unsigned long) out_len);
Packit 679830
Packit 679830
Packit 679830
/***** Step 4: overlapping compression *****/
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 4a: allocate the 'overlap' buffer for overlapping compression
Packit 679830
 */
Packit 679830
    overhead  = in_len > 0xbfff ? 0xbfff : in_len;
Packit 679830
    overhead += in_len / 16 + 64 + 3;
Packit 679830
    overlap = (lzo_bytep) xmalloc(in_len + overhead);
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 4b: prepare data in 'overlap' buffer.
Packit 679830
 *          copy uncompressed data at the top of the overlap buffer
Packit 679830
 */
Packit 679830
    /*** offset = in_len + overhead - in_len; ***/
Packit 679830
    offset = overhead;
Packit 679830
    lzo_memcpy(overlap + offset, in, in_len);
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 4c: do an in-place compression within the 'overlap' buffer
Packit 679830
 */
Packit 679830
    r = lzo1x_1_compress(overlap + offset, in_len, overlap, &new_len, wrkmem);
Packit 679830
    if (r != LZO_E_OK)
Packit 679830
    {
Packit 679830
        /* this should NEVER happen */
Packit 679830
        printf("in-place compression failed: %d\n", r);
Packit 679830
        exit(1);
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 4d: verify overlapping compression
Packit 679830
 */
Packit 679830
    if (new_len != out_len || lzo_memcmp(out, overlap, out_len) != 0)
Packit 679830
    {
Packit 679830
        /* As compression is non-deterministic there can be a difference
Packit 679830
         * in the representation of the compressed data (but this usually
Packit 679830
         * happens very seldom). So we have to verify the overlapping
Packit 679830
         * compression by doing a temporary decompression.
Packit 679830
         */
Packit 679830
        lzo_uint ll = in_len;
Packit 679830
        lzo_bytep tmp = (lzo_bytep) xmalloc(ll);
Packit 679830
        r = lzo1x_decompress_safe(overlap, new_len, tmp, &ll, NULL);
Packit 679830
        if (r != LZO_E_OK || ll != in_len || lzo_memcmp(in, tmp, ll) != 0)
Packit 679830
        {
Packit 679830
            /* this should NEVER happen */
Packit 679830
            printf("in-place compression data error\n");
Packit 679830
            exit(1);
Packit 679830
        }
Packit 679830
        lzo_free(tmp);
Packit 679830
    }
Packit 679830
Packit 679830
    printf("  in-place compression:   %8lu -> %8lu    overhead: %7lu\n",
Packit 679830
            (unsigned long) in_len, (unsigned long) new_len, (unsigned long) overhead);
Packit 679830
    lzo_free(overlap); overlap = NULL;
Packit 679830
Packit 679830
Packit 679830
/***** Step 5: in-place decompression *****/
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 5a: allocate the 'overlap' buffer for in-place decompression
Packit 679830
 */
Packit 679830
    if (opt_overhead == 0 || out_len >= in_len)
Packit 679830
        overhead = in_len / 16 + 64 + 3;
Packit 679830
    else
Packit 679830
        overhead = (lzo_uint) opt_overhead;
Packit 679830
    overlap = (lzo_bytep) xmalloc(in_len + overhead);
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 5b: prepare data in 'overlap' buffer.
Packit 679830
 *          copy compressed data at the top of the overlap buffer
Packit 679830
 */
Packit 679830
    offset = in_len + overhead - out_len;
Packit 679830
    lzo_memcpy(overlap + offset, out, out_len);
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 5c: do an in-place decompression within the 'overlap' buffer
Packit 679830
 */
Packit 679830
    new_len = in_len;
Packit 679830
    r = lzo1x_decompress_safe(overlap + offset, out_len, overlap, &new_len, NULL);
Packit 679830
    if (r != LZO_E_OK)
Packit 679830
    {
Packit 679830
        /* this may happen if overhead is too small */
Packit 679830
        printf("in-place decompression failed: %d - increase 'opt_overhead'\n", r);
Packit 679830
        exit(1);
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 5d: verify decompression
Packit 679830
 */
Packit 679830
    if (new_len != in_len || lzo_memcmp(in, overlap, in_len) != 0)
Packit 679830
    {
Packit 679830
        /* this may happen if overhead is too small */
Packit 679830
        printf("in-place decompression data error - increase 'opt_overhead'\n");
Packit 679830
        exit(1);
Packit 679830
    }
Packit 679830
    printf("  in-place decompression: %8lu -> %8lu    overhead: %7lu\n",
Packit 679830
            (unsigned long) out_len, (unsigned long) new_len, (unsigned long) overhead);
Packit 679830
    lzo_free(overlap); overlap = NULL;
Packit 679830
Packit 679830
Packit 679830
next_file:
Packit 679830
    lzo_free(overlap);
Packit 679830
    lzo_free(wrkmem);
Packit 679830
    lzo_free(out);
Packit 679830
    lzo_free(in);
Packit 679830
    if (fp) fclose(fp);
Packit 679830
Packit 679830
    return 0;
Packit 679830
}
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
    int i = 1;
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 (i < argc && argv[i][0] == '-')
Packit 679830
        opt_overhead = atol(&argv[i++][1]);
Packit 679830
#if 1
Packit 679830
    if (opt_overhead != 0 && opt_overhead < 4)
Packit 679830
    {
Packit 679830
        printf("%s: invalid overhead value %ld\n", progname, opt_overhead);
Packit 679830
        exit(1);
Packit 679830
    }
Packit 679830
#endif
Packit 679830
    if (i >= argc)
Packit 679830
    {
Packit 679830
        printf("usage: %s [-overhead_in_bytes] file..\n", progname);
Packit 679830
        exit(1);
Packit 679830
    }
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: process files
Packit 679830
 */
Packit 679830
    for (r = 0; r == 0 && i < argc; i++)
Packit 679830
        r = do_file(argv[i]);
Packit 679830
Packit 679830
    printf("\nDone. Successfully processed %lu bytes in %lu files.\n",
Packit 679830
            total_in, total_files);
Packit 679830
    return r;
Packit 679830
}
Packit 679830
Packit 679830
Packit 679830
/* vim:set ts=4 sw=4 et: */