Blame examples/simple.c

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