kusano 7d535a
/* simple.c -- the annotated simple example program for the LZO library
kusano 7d535a
kusano 7d535a
   This file is part of the LZO real-time data compression library.
kusano 7d535a
kusano 7d535a
   Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
kusano 7d535a
   All Rights Reserved.
kusano 7d535a
kusano 7d535a
   The LZO library is free software; you can redistribute it and/or
kusano 7d535a
   modify it under the terms of the GNU General Public License as
kusano 7d535a
   published by the Free Software Foundation; either version 2 of
kusano 7d535a
   the License, or (at your option) any later version.
kusano 7d535a
kusano 7d535a
   The LZO library is distributed in the hope that it will be useful,
kusano 7d535a
   but WITHOUT ANY WARRANTY; without even the implied warranty of
kusano 7d535a
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
kusano 7d535a
   GNU General Public License for more details.
kusano 7d535a
kusano 7d535a
   You should have received a copy of the GNU General Public License
kusano 7d535a
   along with the LZO library; see the file COPYING.
kusano 7d535a
   If not, write to the Free Software Foundation, Inc.,
kusano 7d535a
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
kusano 7d535a
kusano 7d535a
   Markus F.X.J. Oberhumer
kusano 7d535a
   <markus@oberhumer.com></markus@oberhumer.com>
kusano 7d535a
   http://www.oberhumer.com/opensource/lzo/
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
// This program shows the basic usage of the LZO library.
kusano 7d535a
// We will compress a block of data and decompress again.
kusano 7d535a
//
kusano 7d535a
// See also LZO.FAQ
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
/* We will be using the LZO1X-1 algorithm, so we have
kusano 7d535a
 * to include <lzo1x.h></lzo1x.h>
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#include "lzo/lzoconf.h"
kusano 7d535a
#include "lzo/lzo1x.h"
kusano 7d535a
kusano 7d535a
/* portability layer */
kusano 7d535a
#define WANT_LZO_MALLOC 1
kusano 7d535a
#include "examples/portab.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* We want to compress the data block at `in' with length `IN_LEN' to
kusano 7d535a
 * the block at `out'. Because the input block may be incompressible,
kusano 7d535a
 * we must provide a little more output space in case that compression
kusano 7d535a
 * is not possible.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifndef IN_LEN
kusano 7d535a
#define IN_LEN      (128*1024L)
kusano 7d535a
#endif
kusano 7d535a
#define OUT_LEN     (IN_LEN + IN_LEN / 16 + 64 + 3)
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*************************************************************************
kusano 7d535a
//
kusano 7d535a
**************************************************************************/
kusano 7d535a
kusano 7d535a
int __lzo_cdecl_main main(int argc, char *argv[])
kusano 7d535a
{
kusano 7d535a
    int r;
kusano 7d535a
    lzo_bytep in;
kusano 7d535a
    lzo_bytep out;
kusano 7d535a
    lzo_bytep wrkmem;
kusano 7d535a
    lzo_uint in_len;
kusano 7d535a
    lzo_uint out_len;
kusano 7d535a
    lzo_uint new_len;
kusano 7d535a
kusano 7d535a
    if (argc < 0 && argv == NULL)   /* avoid warning about unused args */
kusano 7d535a
        return 0;
kusano 7d535a
kusano 7d535a
    printf("\nLZO real-time data compression library (v%s, %s).\n",
kusano 7d535a
           lzo_version_string(), lzo_version_date());
kusano 7d535a
    printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 1: initialize the LZO library
kusano 7d535a
 */
kusano 7d535a
    if (lzo_init() != LZO_E_OK)
kusano 7d535a
    {
kusano 7d535a
        printf("internal error - lzo_init() failed !!!\n");
kusano 7d535a
        printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n");
kusano 7d535a
        return 4;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 2: allocate blocks and the work-memory
kusano 7d535a
 */
kusano 7d535a
    in = (lzo_bytep) lzo_malloc(IN_LEN);
kusano 7d535a
    out = (lzo_bytep) lzo_malloc(OUT_LEN);
kusano 7d535a
    wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS);
kusano 7d535a
    if (in == NULL || out == NULL || wrkmem == NULL)
kusano 7d535a
    {
kusano 7d535a
        printf("out of memory\n");
kusano 7d535a
        return 3;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 3: prepare the input block that will get compressed.
kusano 7d535a
 *         We just fill it with zeros in this example program,
kusano 7d535a
 *         but you would use your real-world data here.
kusano 7d535a
 */
kusano 7d535a
    in_len = IN_LEN;
kusano 7d535a
    lzo_memset(in,0,in_len);
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 4: compress from `in' to `out' with LZO1X-1
kusano 7d535a
 */
kusano 7d535a
    r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
kusano 7d535a
    if (r == LZO_E_OK)
kusano 7d535a
        printf("compressed %lu bytes into %lu bytes\n",
kusano 7d535a
            (unsigned long) in_len, (unsigned long) out_len);
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - compression failed: %d\n", r);
kusano 7d535a
        return 2;
kusano 7d535a
    }
kusano 7d535a
    /* check for an incompressible block */
kusano 7d535a
    if (out_len >= in_len)
kusano 7d535a
    {
kusano 7d535a
        printf("This block contains incompressible data.\n");
kusano 7d535a
        return 0;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Step 5: decompress again, now going from `out' to `in'
kusano 7d535a
 */
kusano 7d535a
    new_len = in_len;
kusano 7d535a
    r = lzo1x_decompress(out,out_len,in,&new_len,NULL);
kusano 7d535a
    if (r == LZO_E_OK && new_len == in_len)
kusano 7d535a
        printf("decompressed %lu bytes back into %lu bytes\n",
kusano 7d535a
            (unsigned long) out_len, (unsigned long) in_len);
kusano 7d535a
    else
kusano 7d535a
    {
kusano 7d535a
        /* this should NEVER happen */
kusano 7d535a
        printf("internal error - decompression failed: %d\n", r);
kusano 7d535a
        return 1;
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    lzo_free(wrkmem);
kusano 7d535a
    lzo_free(out);
kusano 7d535a
    lzo_free(in);
kusano 7d535a
    printf("Simple compression test passed.\n");
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
vi:ts=4:et
kusano 7d535a
*/
kusano 7d535a