Blame examples/lzopack.c

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