Blame minilzo/testmini.c

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