Blame minilzo/testmini.c

Packit 679830
/* testmini.c -- very simple test program for the miniLZO 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
#include <stdio.h>
Packit 679830
#include <stdlib.h>
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
// For more information, documentation, example programs and other support
Packit 679830
// files (like Makefiles and build scripts) please download the full LZO
Packit 679830
// package from
Packit 679830
//    http://www.oberhumer.com/opensource/lzo/
Packit 679830
**************************************************************************/
Packit 679830
Packit 679830
/* First let's include "minizo.h". */
Packit 679830
Packit 679830
#include "minilzo.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
#define IN_LEN      (128*1024ul)
Packit 679830
#define OUT_LEN     (IN_LEN + IN_LEN / 16 + 64 + 3)
Packit 679830
Packit 679830
static unsigned char __LZO_MMODEL in  [ IN_LEN ];
Packit 679830
static unsigned char __LZO_MMODEL out [ OUT_LEN ];
Packit 679830
Packit 679830
Packit 679830
/* Work-memory needed for compression. Allocate memory in units
Packit 679830
 * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned.
Packit 679830
 */
Packit 679830
Packit 679830
#define HEAP_ALLOC(var,size) \
Packit 679830
    lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
Packit 679830
Packit 679830
static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
Packit 679830
Packit 679830
Packit 679830
/*************************************************************************
Packit 679830
//
Packit 679830
**************************************************************************/
Packit 679830
Packit 679830
int main(int argc, char *argv[])
Packit 679830
{
Packit 679830
    int r;
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
/*
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 3;
Packit 679830
    }
Packit 679830
Packit 679830
/*
Packit 679830
 * Step 2: 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 3: 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 4: 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
    printf("\nminiLZO simple compression test passed.\n");
Packit 679830
    return 0;
Packit 679830
}
Packit 679830
Packit 679830
/*
Packit 679830
vi:ts=4:et
Packit 679830
*/
Packit 679830