Blame examples/lzopack.c

Packit Service 5195f2
/* lzopack.c -- LZO example program: a simple file packer
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
// NOTE: this is an example program, so do not use to backup your data.
Packit Service 5195f2
//
Packit Service 5195f2
// This program lacks things like sophisticated file handling but is
Packit Service 5195f2
// pretty complete regarding compression - it should provide a good
Packit Service 5195f2
// starting point for adaption for your applications.
Packit Service 5195f2
//
Packit Service 5195f2
// Please study LZO.FAQ and simple.c first.
Packit Service 5195f2
**************************************************************************/
Packit Service 5195f2
Packit Service 5195f2
#include "lzo/lzoconf.h"
Packit Service 5195f2
#include "lzo/lzo1x.h"
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
static unsigned long total_in = 0;
Packit Service 5195f2
static unsigned long total_out = 0;
Packit Service 5195f2
static lzo_bool opt_debug = 0;
Packit Service 5195f2
Packit Service 5195f2
/* magic file header for lzopack-compressed files */
Packit Service 5195f2
static const unsigned char magic[7] =
Packit Service 5195f2
    { 0x00, 0xe9, 0x4c, 0x5a, 0x4f, 0xff, 0x1a };
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/*************************************************************************
Packit Service 5195f2
// file IO
Packit Service 5195f2
**************************************************************************/
Packit Service 5195f2
Packit Service 5195f2
static lzo_uint xread(FILE *fp, lzo_voidp buf, lzo_uint len, lzo_bool allow_eof)
Packit Service 5195f2
{
Packit Service 5195f2
    lzo_uint l;
Packit Service 5195f2
Packit Service 5195f2
    l = (lzo_uint) lzo_fread(fp, buf, len);
Packit Service 5195f2
    if (l > len)
Packit Service 5195f2
    {
Packit Service 5195f2
        fprintf(stderr, "\n%s: internal error - something is wrong with your C library !!!\n", progname);
Packit Service 5195f2
        exit(1);
Packit Service 5195f2
    }
Packit Service 5195f2
    if (l != len && !allow_eof)
Packit Service 5195f2
    {
Packit Service 5195f2
        fprintf(stderr, "\n%s: read error - premature end of file\n", progname);
Packit Service 5195f2
        exit(1);
Packit Service 5195f2
    }
Packit Service 5195f2
    total_in += (unsigned long) l;
Packit Service 5195f2
    return l;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
static lzo_uint xwrite(FILE *fp, const lzo_voidp buf, lzo_uint len)
Packit Service 5195f2
{
Packit Service 5195f2
    if (fp != NULL && lzo_fwrite(fp, buf, len) != len)
Packit Service 5195f2
    {
Packit Service 5195f2
        fprintf(stderr, "\n%s: write error  (disk full ?)\n", progname);
Packit Service 5195f2
        exit(1);
Packit Service 5195f2
    }
Packit Service 5195f2
    total_out += (unsigned long) len;
Packit Service 5195f2
    return len;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
static int xgetc(FILE *fp)
Packit Service 5195f2
{
Packit Service 5195f2
    unsigned char c;
Packit Service 5195f2
    xread(fp, (lzo_voidp) &c, 1, 0);
Packit Service 5195f2
    return c;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
static void xputc(FILE *fp, int c)
Packit Service 5195f2
{
Packit Service 5195f2
    unsigned char cc = (unsigned char) (c & 0xff);
Packit Service 5195f2
    xwrite(fp, (const lzo_voidp) &cc, 1);
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
/* read and write portable 32-bit integers */
Packit Service 5195f2
Packit Service 5195f2
static lzo_uint32_t xread32(FILE *fp)
Packit Service 5195f2
{
Packit Service 5195f2
    unsigned char b[4];
Packit Service 5195f2
    lzo_uint32_t v;
Packit Service 5195f2
Packit Service 5195f2
    xread(fp, b, 4, 0);
Packit Service 5195f2
    v  = (lzo_uint32_t) b[3] <<  0;
Packit Service 5195f2
    v |= (lzo_uint32_t) b[2] <<  8;
Packit Service 5195f2
    v |= (lzo_uint32_t) b[1] << 16;
Packit Service 5195f2
    v |= (lzo_uint32_t) b[0] << 24;
Packit Service 5195f2
    return v;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
static void xwrite32(FILE *fp, lzo_uint v)
Packit Service 5195f2
{
Packit Service 5195f2
    unsigned char b[4];
Packit Service 5195f2
Packit Service 5195f2
    b[3] = (unsigned char) ((v >>  0) & 0xff);
Packit Service 5195f2
    b[2] = (unsigned char) ((v >>  8) & 0xff);
Packit Service 5195f2
    b[1] = (unsigned char) ((v >> 16) & 0xff);
Packit Service 5195f2
    b[0] = (unsigned char) ((v >> 24) & 0xff);
Packit Service 5195f2
    xwrite(fp, b, 4);
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/*************************************************************************
Packit Service 5195f2
// compress
Packit Service 5195f2
//
Packit Service 5195f2
// possible improvement: we could use overlapping compression to
Packit Service 5195f2
//   save some memory - see overlap.c. This would require some minor
Packit Service 5195f2
//   changes in the decompression code as well, because if a block
Packit Service 5195f2
//   turns out to be incompressible we would still have to store it in its
Packit Service 5195f2
//   "compressed" (i.e. then slightly enlarged) form because the original
Packit Service 5195f2
//   (uncompressed) data would have been lost during the overlapping
Packit Service 5195f2
//   compression.
Packit Service 5195f2
**************************************************************************/
Packit Service 5195f2
Packit Service 5195f2
static int do_compress(FILE *fi, FILE *fo, int compression_level, lzo_uint block_size)
Packit Service 5195f2
{
Packit Service 5195f2
    int r = 0;
Packit Service 5195f2
    lzo_bytep in = NULL;
Packit Service 5195f2
    lzo_bytep out = NULL;
Packit Service 5195f2
    lzo_voidp wrkmem = NULL;
Packit Service 5195f2
    lzo_uint in_len;
Packit Service 5195f2
    lzo_uint out_len;
Packit Service 5195f2
    lzo_uint wrkmem_size;
Packit Service 5195f2
    lzo_uint32_t flags = 1;     /* do compute a checksum */
Packit Service 5195f2
    int method = 1;             /* compression method: LZO1X */
Packit Service 5195f2
    lzo_uint32_t checksum;
Packit Service 5195f2
Packit Service 5195f2
    total_in = total_out = 0;
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 1: write magic header, flags & block size, init checksum
Packit Service 5195f2
 */
Packit Service 5195f2
    xwrite(fo, magic, sizeof(magic));
Packit Service 5195f2
    xwrite32(fo, flags);
Packit Service 5195f2
    xputc(fo, method);              /* compression method */
Packit Service 5195f2
    xputc(fo, compression_level);   /* compression level */
Packit Service 5195f2
    xwrite32(fo, block_size);
Packit Service 5195f2
    checksum = lzo_adler32(0, NULL, 0);
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 2: allocate compression buffers and work-memory
Packit Service 5195f2
 */
Packit Service 5195f2
    in = (lzo_bytep) xmalloc(block_size);
Packit Service 5195f2
    out = (lzo_bytep) xmalloc(block_size + block_size / 16 + 64 + 3);
Packit Service 5195f2
    if (compression_level == 9)
Packit Service 5195f2
        wrkmem_size = LZO1X_999_MEM_COMPRESS;
Packit Service 5195f2
    else
Packit Service 5195f2
        wrkmem_size = LZO1X_1_MEM_COMPRESS;
Packit Service 5195f2
    wrkmem = (lzo_voidp) xmalloc(wrkmem_size);
Packit Service 5195f2
    if (in == NULL || out == NULL || wrkmem == NULL)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: out of memory\n", progname);
Packit Service 5195f2
        r = 1;
Packit Service 5195f2
        goto err;
Packit Service 5195f2
    }
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 3: process blocks
Packit Service 5195f2
 */
Packit Service 5195f2
    for (;;)
Packit Service 5195f2
    {
Packit Service 5195f2
        /* read block */
Packit Service 5195f2
        in_len = xread(fi, in, block_size, 1);
Packit Service 5195f2
        if (in_len == 0)
Packit Service 5195f2
            break;
Packit Service 5195f2
Packit Service 5195f2
        /* update checksum */
Packit Service 5195f2
        if (flags & 1)
Packit Service 5195f2
            checksum = lzo_adler32(checksum, in, in_len);
Packit Service 5195f2
Packit Service 5195f2
        /* clear wrkmem (not needed, only for debug/benchmark purposes) */
Packit Service 5195f2
        if (opt_debug)
Packit Service 5195f2
            lzo_memset(wrkmem, 0xff, wrkmem_size);
Packit Service 5195f2
Packit Service 5195f2
        /* compress block */
Packit Service 5195f2
        if (compression_level == 9)
Packit Service 5195f2
            r = lzo1x_999_compress(in, in_len, out, &out_len, wrkmem);
Packit Service 5195f2
        else
Packit Service 5195f2
            r = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
Packit Service 5195f2
        if (r != LZO_E_OK || out_len > in_len + in_len / 16 + 64 + 3)
Packit Service 5195f2
        {
Packit Service 5195f2
            /* this should NEVER happen */
Packit Service 5195f2
            printf("internal error - compression failed: %d\n", r);
Packit Service 5195f2
            r = 2;
Packit Service 5195f2
            goto err;
Packit Service 5195f2
        }
Packit Service 5195f2
Packit Service 5195f2
        /* write uncompressed block size */
Packit Service 5195f2
        xwrite32(fo, in_len);
Packit Service 5195f2
Packit Service 5195f2
        if (out_len < in_len)
Packit Service 5195f2
        {
Packit Service 5195f2
            /* write compressed block */
Packit Service 5195f2
            xwrite32(fo, out_len);
Packit Service 5195f2
            xwrite(fo, out, out_len);
Packit Service 5195f2
        }
Packit Service 5195f2
        else
Packit Service 5195f2
        {
Packit Service 5195f2
            /* not compressible - write uncompressed block */
Packit Service 5195f2
            xwrite32(fo, in_len);
Packit Service 5195f2
            xwrite(fo, in, in_len);
Packit Service 5195f2
        }
Packit Service 5195f2
    }
Packit Service 5195f2
Packit Service 5195f2
    /* write EOF marker */
Packit Service 5195f2
    xwrite32(fo, 0);
Packit Service 5195f2
Packit Service 5195f2
    /* write checksum */
Packit Service 5195f2
    if (flags & 1)
Packit Service 5195f2
        xwrite32(fo, checksum);
Packit Service 5195f2
Packit Service 5195f2
    r = 0;
Packit Service 5195f2
err:
Packit Service 5195f2
    lzo_free(wrkmem);
Packit Service 5195f2
    lzo_free(out);
Packit Service 5195f2
    lzo_free(in);
Packit Service 5195f2
    return r;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/*************************************************************************
Packit Service 5195f2
// decompress / test
Packit Service 5195f2
//
Packit Service 5195f2
// We are using overlapping (in-place) decompression to save some
Packit Service 5195f2
// memory - see overlap.c.
Packit Service 5195f2
**************************************************************************/
Packit Service 5195f2
Packit Service 5195f2
static int do_decompress(FILE *fi, FILE *fo)
Packit Service 5195f2
{
Packit Service 5195f2
    int r = 0;
Packit Service 5195f2
    lzo_bytep buf = NULL;
Packit Service 5195f2
    lzo_uint buf_len;
Packit Service 5195f2
    unsigned char m [ sizeof(magic) ];
Packit Service 5195f2
    lzo_uint32_t flags;
Packit Service 5195f2
    int method;
Packit Service 5195f2
    int compression_level;
Packit Service 5195f2
    lzo_uint block_size;
Packit Service 5195f2
    lzo_uint32_t checksum;
Packit Service 5195f2
Packit Service 5195f2
    total_in = total_out = 0;
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 1: check magic header, read flags & block size, init checksum
Packit Service 5195f2
 */
Packit Service 5195f2
    if (xread(fi, m, sizeof(magic), 1) != sizeof(magic) ||
Packit Service 5195f2
        memcmp(m, magic, sizeof(magic)) != 0)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: header error - this file is not compressed by lzopack\n", progname);
Packit Service 5195f2
        r = 1;
Packit Service 5195f2
        goto err;
Packit Service 5195f2
    }
Packit Service 5195f2
    flags = xread32(fi);
Packit Service 5195f2
    method = xgetc(fi);
Packit Service 5195f2
    compression_level = xgetc(fi);
Packit Service 5195f2
    if (method != 1)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: header error - invalid method %d (level %d)\n",
Packit Service 5195f2
                progname, method, compression_level);
Packit Service 5195f2
        r = 2;
Packit Service 5195f2
        goto err;
Packit Service 5195f2
    }
Packit Service 5195f2
    block_size = xread32(fi);
Packit Service 5195f2
    if (block_size < 1024 || block_size > 8L * 1024L * 1024L)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: header error - invalid block size %ld\n",
Packit Service 5195f2
                progname, (long) block_size);
Packit Service 5195f2
        r = 3;
Packit Service 5195f2
        goto err;
Packit Service 5195f2
    }
Packit Service 5195f2
    checksum = lzo_adler32(0, NULL, 0);
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 2: allocate buffer for in-place decompression
Packit Service 5195f2
 */
Packit Service 5195f2
    buf_len = block_size + block_size / 16 + 64 + 3;
Packit Service 5195f2
    buf = (lzo_bytep) xmalloc(buf_len);
Packit Service 5195f2
    if (buf == NULL)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: out of memory\n", progname);
Packit Service 5195f2
        r = 4;
Packit Service 5195f2
        goto err;
Packit Service 5195f2
    }
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 3: process blocks
Packit Service 5195f2
 */
Packit Service 5195f2
    for (;;)
Packit Service 5195f2
    {
Packit Service 5195f2
        lzo_bytep in;
Packit Service 5195f2
        lzo_bytep out;
Packit Service 5195f2
        lzo_uint in_len;
Packit Service 5195f2
        lzo_uint out_len;
Packit Service 5195f2
Packit Service 5195f2
        /* read uncompressed size */
Packit Service 5195f2
        out_len = xread32(fi);
Packit Service 5195f2
Packit Service 5195f2
        /* exit if last block (EOF marker) */
Packit Service 5195f2
        if (out_len == 0)
Packit Service 5195f2
            break;
Packit Service 5195f2
Packit Service 5195f2
        /* read compressed size */
Packit Service 5195f2
        in_len = xread32(fi);
Packit Service 5195f2
Packit Service 5195f2
        /* sanity check of the size values */
Packit Service 5195f2
        if (in_len > block_size || out_len > block_size ||
Packit Service 5195f2
            in_len == 0 || in_len > out_len)
Packit Service 5195f2
        {
Packit Service 5195f2
            printf("%s: block size error - data corrupted\n", progname);
Packit Service 5195f2
            r = 5;
Packit Service 5195f2
            goto err;
Packit Service 5195f2
        }
Packit Service 5195f2
Packit Service 5195f2
        /* place compressed block at the top of the buffer */
Packit Service 5195f2
        in = buf + buf_len - in_len;
Packit Service 5195f2
        out = buf;
Packit Service 5195f2
Packit Service 5195f2
        /* read compressed block data */
Packit Service 5195f2
        xread(fi, in, in_len, 0);
Packit Service 5195f2
Packit Service 5195f2
        if (in_len < out_len)
Packit Service 5195f2
        {
Packit Service 5195f2
            /* decompress - use safe decompressor as data might be corrupted
Packit Service 5195f2
             * during a file transfer */
Packit Service 5195f2
            lzo_uint new_len = out_len;
Packit Service 5195f2
Packit Service 5195f2
            r = lzo1x_decompress_safe(in, in_len, out, &new_len, NULL);
Packit Service 5195f2
            if (r != LZO_E_OK || new_len != out_len)
Packit Service 5195f2
            {
Packit Service 5195f2
                printf("%s: compressed data violation\n", progname);
Packit Service 5195f2
                r = 6;
Packit Service 5195f2
                goto err;
Packit Service 5195f2
            }
Packit Service 5195f2
            /* write decompressed block */
Packit Service 5195f2
            xwrite(fo, out, out_len);
Packit Service 5195f2
            /* update checksum */
Packit Service 5195f2
            if (flags & 1)
Packit Service 5195f2
                checksum = lzo_adler32(checksum, out, out_len);
Packit Service 5195f2
        }
Packit Service 5195f2
        else
Packit Service 5195f2
        {
Packit Service 5195f2
            /* write original (incompressible) block */
Packit Service 5195f2
            xwrite(fo, in, in_len);
Packit Service 5195f2
            /* update checksum */
Packit Service 5195f2
            if (flags & 1)
Packit Service 5195f2
                checksum = lzo_adler32(checksum, in, in_len);
Packit Service 5195f2
        }
Packit Service 5195f2
    }
Packit Service 5195f2
Packit Service 5195f2
    /* read and verify checksum */
Packit Service 5195f2
    if (flags & 1)
Packit Service 5195f2
    {
Packit Service 5195f2
        lzo_uint32_t c = xread32(fi);
Packit Service 5195f2
        if (c != checksum)
Packit Service 5195f2
        {
Packit Service 5195f2
            printf("%s: checksum error - data corrupted\n", progname);
Packit Service 5195f2
            r = 7;
Packit Service 5195f2
            goto err;
Packit Service 5195f2
        }
Packit Service 5195f2
    }
Packit Service 5195f2
Packit Service 5195f2
    r = 0;
Packit Service 5195f2
err:
Packit Service 5195f2
    lzo_free(buf);
Packit Service 5195f2
    return r;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/*************************************************************************
Packit Service 5195f2
//
Packit Service 5195f2
**************************************************************************/
Packit Service 5195f2
Packit Service 5195f2
static void usage(void)
Packit Service 5195f2
{
Packit Service 5195f2
    printf("usage: %s [-9] input-file output-file  (compress)\n", progname);
Packit Service 5195f2
    printf("usage: %s -d   input-file output-file  (decompress)\n", progname);
Packit Service 5195f2
    printf("usage: %s -t   input-file...           (test)\n", progname);
Packit Service 5195f2
    exit(1);
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/* open input file */
Packit Service 5195f2
static FILE *xopen_fi(const char *name)
Packit Service 5195f2
{
Packit Service 5195f2
    FILE *fp;
Packit Service 5195f2
Packit Service 5195f2
    fp = fopen(name, "rb");
Packit Service 5195f2
    if (fp == NULL)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: cannot open input file %s\n", progname, name);
Packit Service 5195f2
        exit(1);
Packit Service 5195f2
    }
Packit Service 5195f2
#if defined(HAVE_STAT) && defined(S_ISREG)
Packit Service 5195f2
    {
Packit Service 5195f2
        struct stat st;
Packit Service 5195f2
        int is_regular = 1;
Packit Service 5195f2
        if (stat(name, &st) != 0 || !S_ISREG(st.st_mode))
Packit Service 5195f2
            is_regular = 0;
Packit Service 5195f2
        if (!is_regular)
Packit Service 5195f2
        {
Packit Service 5195f2
            printf("%s: %s is not a regular file\n", progname, name);
Packit Service 5195f2
            fclose(fp); fp = NULL;
Packit Service 5195f2
            exit(1);
Packit Service 5195f2
        }
Packit Service 5195f2
    }
Packit Service 5195f2
#endif
Packit Service 5195f2
    return fp;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/* open output file */
Packit Service 5195f2
static FILE *xopen_fo(const char *name)
Packit Service 5195f2
{
Packit Service 5195f2
    FILE *fp;
Packit Service 5195f2
Packit Service 5195f2
#if 0
Packit Service 5195f2
    /* this is an example program, so make sure we don't overwrite a file */
Packit Service 5195f2
    fp = fopen(name, "rb");
Packit Service 5195f2
    if (fp != NULL)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: file %s already exists -- not overwritten\n", progname, name);
Packit Service 5195f2
        fclose(fp); fp = NULL;
Packit Service 5195f2
        exit(1);
Packit Service 5195f2
    }
Packit Service 5195f2
#endif
Packit Service 5195f2
    fp = fopen(name, "wb");
Packit Service 5195f2
    if (fp == NULL)
Packit Service 5195f2
    {
Packit Service 5195f2
        printf("%s: cannot open output file %s\n", progname, name);
Packit Service 5195f2
        exit(1);
Packit Service 5195f2
    }
Packit Service 5195f2
    return fp;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/* close file */
Packit Service 5195f2
static void xclose(FILE *fp)
Packit Service 5195f2
{
Packit Service 5195f2
    if (fp)
Packit Service 5195f2
    {
Packit Service 5195f2
        int err;
Packit Service 5195f2
        err = ferror(fp);
Packit Service 5195f2
        if (fclose(fp) != 0)
Packit Service 5195f2
            err = 1;
Packit Service 5195f2
        if (err)
Packit Service 5195f2
        {
Packit Service 5195f2
            printf("%s: error while closing file\n", progname);
Packit Service 5195f2
            exit(1);
Packit Service 5195f2
        }
Packit Service 5195f2
    }
Packit Service 5195f2
}
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 i = 1;
Packit Service 5195f2
    int r = 0;
Packit Service 5195f2
    FILE *fi = NULL;
Packit Service 5195f2
    FILE *fo = NULL;
Packit Service 5195f2
    const char *in_name = NULL;
Packit Service 5195f2
    const char *out_name = NULL;
Packit Service 5195f2
    unsigned opt_decompress = 0;
Packit Service 5195f2
    unsigned opt_test = 0;
Packit Service 5195f2
    int opt_compression_level = 1;
Packit Service 5195f2
    lzo_uint opt_block_size;
Packit Service 5195f2
    const char *s;
Packit Service 5195f2
Packit Service 5195f2
    lzo_wildargv(&argc, &argv);
Packit Service 5195f2
Packit Service 5195f2
    progname = argv[0];
Packit Service 5195f2
    for (s = progname; *s; s++)
Packit Service 5195f2
        if ((*s == '/' || *s == '\\') && s[1])
Packit Service 5195f2
            progname = s + 1;
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
#if 0
Packit Service 5195f2
    printf(
Packit Service 5195f2
"*** DISCLAIMER ***\n"
Packit Service 5195f2
"   This is an example program, do not use to backup your data !\n"
Packit Service 5195f2
"   Get LZOP if you're interested into a full-featured packer.\n"
Packit Service 5195f2
"   See http://www.oberhumer.com/opensource/lzop/\n"
Packit Service 5195f2
"\n");
Packit Service 5195f2
#endif
Packit Service 5195f2
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
/*
Packit Service 5195f2
 * Step 2: setup memory
Packit Service 5195f2
 */
Packit Service 5195f2
    opt_block_size = 256 * 1024L;
Packit Service 5195f2
Packit Service 5195f2
#if defined(LZO_MM_AHSHIFT)
Packit Service 5195f2
    /* reduce memory requirements for ancient 16-bit DOS 640kB real-mode */
Packit Service 5195f2
    if (LZO_MM_AHSHIFT != 3)
Packit Service 5195f2
        opt_block_size = 16 * 1024L;
Packit Service 5195f2
#endif
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 3: get options
Packit Service 5195f2
 */
Packit Service 5195f2
Packit Service 5195f2
    while (i < argc && argv[i][0] == '-')
Packit Service 5195f2
    {
Packit Service 5195f2
        if (strcmp(argv[i],"-d") == 0)
Packit Service 5195f2
            opt_decompress = 1;
Packit Service 5195f2
        else if (strcmp(argv[i],"-t") == 0)
Packit Service 5195f2
            opt_test = 1;
Packit Service 5195f2
        else if (strcmp(argv[i],"-9") == 0)
Packit Service 5195f2
            opt_compression_level = 9;
Packit Service 5195f2
        else if (argv[i][1] == 'b' && argv[i][2])
Packit Service 5195f2
        {
Packit Service 5195f2
            long b = atol(&argv[i][2]);
Packit Service 5195f2
            if (b >= 1024L && b <= 8*1024*1024L)
Packit Service 5195f2
                opt_block_size = (lzo_uint) b;
Packit Service 5195f2
            else
Packit Service 5195f2
            {
Packit Service 5195f2
                printf("%s: invalid block_size in option '%s'.\n", progname, argv[i]);
Packit Service 5195f2
                usage();
Packit Service 5195f2
            }
Packit Service 5195f2
        }
Packit Service 5195f2
        else if (strcmp(argv[i],"--debug") == 0)
Packit Service 5195f2
            opt_debug += 1;
Packit Service 5195f2
        else
Packit Service 5195f2
            usage();
Packit Service 5195f2
        i++;
Packit Service 5195f2
    }
Packit Service 5195f2
    if (opt_test && i >= argc)
Packit Service 5195f2
        usage();
Packit Service 5195f2
    if (!opt_test && i + 2 != argc)
Packit Service 5195f2
        usage();
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/*
Packit Service 5195f2
 * Step 4: process file(s)
Packit Service 5195f2
 */
Packit Service 5195f2
Packit Service 5195f2
    if (opt_test)
Packit Service 5195f2
    {
Packit Service 5195f2
        while (i < argc && r == 0)
Packit Service 5195f2
        {
Packit Service 5195f2
            in_name = argv[i++];
Packit Service 5195f2
            fi = xopen_fi(in_name);
Packit Service 5195f2
            r = do_decompress(fi, NULL);
Packit Service 5195f2
            if (r == 0)
Packit Service 5195f2
                printf("%s: %s tested ok (%lu -> %lu bytes)\n",
Packit Service 5195f2
                        progname, in_name, total_in, total_out);
Packit Service 5195f2
            xclose(fi); fi = NULL;
Packit Service 5195f2
        }
Packit Service 5195f2
    }
Packit Service 5195f2
    else if (opt_decompress)
Packit Service 5195f2
    {
Packit Service 5195f2
        in_name = argv[i++];
Packit Service 5195f2
        out_name = argv[i++];
Packit Service 5195f2
        fi = xopen_fi(in_name);
Packit Service 5195f2
        fo = xopen_fo(out_name);
Packit Service 5195f2
        r = do_decompress(fi, fo);
Packit Service 5195f2
        if (r == 0)
Packit Service 5195f2
            printf("%s: decompressed %lu into %lu bytes\n",
Packit Service 5195f2
                    progname, total_in, total_out);
Packit Service 5195f2
    }
Packit Service 5195f2
    else /* compress */
Packit Service 5195f2
    {
Packit Service 5195f2
        in_name = argv[i++];
Packit Service 5195f2
        out_name = argv[i++];
Packit Service 5195f2
        fi = xopen_fi(in_name);
Packit Service 5195f2
        fo = xopen_fo(out_name);
Packit Service 5195f2
        r = do_compress(fi, fo, opt_compression_level, opt_block_size);
Packit Service 5195f2
        if (r == 0)
Packit Service 5195f2
            printf("%s: compressed %lu into %lu bytes\n",
Packit Service 5195f2
                    progname, total_in, total_out);
Packit Service 5195f2
    }
Packit Service 5195f2
Packit Service 5195f2
    xclose(fi); fi = NULL;
Packit Service 5195f2
    xclose(fo); fo = NULL;
Packit Service 5195f2
    return r;
Packit Service 5195f2
}
Packit Service 5195f2
Packit Service 5195f2
Packit Service 5195f2
/* vim:set ts=4 sw=4 et: */