Blame examples/simple.c

Packit 679830
/* simple.c -- the annotated simple example program for the LZO library
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 the basic usage of the LZO library.
Packit 679830
// We will compress a block of data and decompress again.
Packit 679830
//
Packit 679830
// See also LZO.FAQ
Packit 679830
**************************************************************************/
Packit 679830
Packit 679830
/* We will be using the LZO1X-1 algorithm, so we have
Packit 679830
 * to include <lzo1x.h>
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_XMALLOC 1
Packit 679830
#include "examples/portab.h"
Packit 679830
Packit 679830
Packit 679830
/* We want to compress the data block at 'in' with length 'IN_LEN' to
Packit 679830
 * the block at 'out'. Because the input block may be incompressible,
Packit 679830
 * we must provide a little more output space in case that compression
Packit 679830
 * is not possible.
Packit 679830
 */
Packit 679830
Packit 679830
#ifndef IN_LEN
Packit 679830
#define IN_LEN      (128*1024L)
Packit 679830
#endif
Packit 679830
#define OUT_LEN     (IN_LEN + IN_LEN / 16 + 64 + 3)
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
    lzo_bytep in;
Packit 679830
    lzo_bytep out;
Packit 679830
    lzo_voidp wrkmem;
Packit 679830
    lzo_uint in_len;
Packit 679830
    lzo_uint out_len;
Packit 679830
    lzo_uint new_len;
Packit 679830
Packit 679830
    if (argc < 0 && argv == NULL)   /* avoid warning about unused args */
Packit 679830
        return 0;
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
/*
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
        return 4;
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 2: allocate blocks and the work-memory
Packit 679830
 */
Packit 679830
    in = (lzo_bytep) xmalloc(IN_LEN);
Packit 679830
    out = (lzo_bytep) xmalloc(OUT_LEN);
Packit 679830
    wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
Packit 679830
    if (in == NULL || out == NULL || wrkmem == NULL)
Packit 679830
    {
Packit 679830
        printf("out of memory\n");
Packit 679830
        return 3;
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 3: prepare the input block that will get compressed.
Packit 679830
 *         We just fill it with zeros in this example program,
Packit 679830
 *         but you would use your real-world data here.
Packit 679830
 */
Packit 679830
    in_len = IN_LEN;
Packit 679830
    lzo_memset(in,0,in_len);
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 4: 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)
Packit 679830
        printf("compressed %lu bytes into %lu bytes\n",
Packit 679830
               (unsigned long) in_len, (unsigned long) out_len);
Packit 679830
    else
Packit 679830
    {
Packit 679830
        /* this should NEVER happen */
Packit 679830
        printf("internal error - compression failed: %d\n", r);
Packit 679830
        return 2;
Packit 679830
    }
Packit 679830
    /* check for an incompressible block */
Packit 679830
    if (out_len >= in_len)
Packit 679830
    {
Packit 679830
        printf("This block contains incompressible data.\n");
Packit 679830
        return 0;
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 5: decompress again, now going from 'out' to 'in'
Packit 679830
 */
Packit 679830
    new_len = in_len;
Packit 679830
    r = lzo1x_decompress(out, out_len, in, &new_len, NULL);
Packit 679830
    if (r == LZO_E_OK && new_len == in_len)
Packit 679830
        printf("decompressed %lu bytes back into %lu bytes\n",
Packit 679830
               (unsigned long) out_len, (unsigned long) in_len);
Packit 679830
    else
Packit 679830
    {
Packit 679830
        /* this should NEVER happen */
Packit 679830
        printf("internal error - decompression failed: %d\n", r);
Packit 679830
        return 1;
Packit 679830
    }
Packit 679830
Packit 679830
    lzo_free(wrkmem);
Packit 679830
    lzo_free(out);
Packit 679830
    lzo_free(in);
Packit 679830
    printf("Simple compression test passed.\n");
Packit 679830
    return 0;
Packit 679830
}
Packit 679830
Packit 679830
Packit 679830
/* vim:set ts=4 sw=4 et: */