1/* testmini.c -- very simple test program for the miniLZO library 2 3 This file is part of the LZO real-time data compression library. 4 5 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer 6 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer 7 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer 8 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer 9 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer 10 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer 11 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer 12 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer 13 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer 14 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer 15 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer 16 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer 17 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer 18 All Rights Reserved. 19 20 The LZO library is free software; you can redistribute it and/or 21 modify it under the terms of the GNU General Public License as 22 published by the Free Software Foundation; either version 2 of 23 the License, or (at your option) any later version. 24 25 The LZO library is distributed in the hope that it will be useful, 26 but WITHOUT ANY WARRANTY; without even the implied warranty of 27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 28 GNU General Public License for more details. 29 30 You should have received a copy of the GNU General Public License 31 along with the LZO library; see the file COPYING. 32 If not, write to the Free Software Foundation, Inc., 33 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 34 35 Markus F.X.J. Oberhumer 36 <markus@oberhumer.com> 37 http://www.oberhumer.com/opensource/lzo/ 38 */ 39 40 41#include <stdio.h> 42#include <stdlib.h> 43 44 45/************************************************************************* 46// This program shows the basic usage of the LZO library. 47// We will compress a block of data and decompress again. 48// 49// For more information, documentation, example programs and other support 50// files (like Makefiles and build scripts) please download the full LZO 51// package from 52// http://www.oberhumer.com/opensource/lzo/ 53**************************************************************************/ 54 55/* First let's include "minizo.h". */ 56 57#include "minilzo.h" 58 59 60/* We want to compress the data block at `in' with length `IN_LEN' to 61 * the block at `out'. Because the input block may be incompressible, 62 * we must provide a little more output space in case that compression 63 * is not possible. 64 */ 65 66#if defined(__LZO_STRICT_16BIT) 67#define IN_LEN (8*1024u) 68#elif defined(LZO_ARCH_I086) && !defined(LZO_HAVE_MM_HUGE_ARRAY) 69#define IN_LEN (60*1024u) 70#else 71#define IN_LEN (128*1024ul) 72#endif 73#define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3) 74 75static unsigned char __LZO_MMODEL in [ IN_LEN ]; 76static unsigned char __LZO_MMODEL out [ OUT_LEN ]; 77 78 79/* Work-memory needed for compression. Allocate memory in units 80 * of `lzo_align_t' (instead of `char') to make sure it is properly aligned. 81 */ 82 83#define HEAP_ALLOC(var,size) \ 84 lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ] 85 86static HEAP_ALLOC(wrkmem,LZO1X_1_MEM_COMPRESS); 87 88 89/************************************************************************* 90// 91**************************************************************************/ 92 93int main(int argc, char *argv[]) 94{ 95 int r; 96 lzo_uint in_len; 97 lzo_uint out_len; 98 lzo_uint new_len; 99 100 if (argc < 0 && argv == NULL) /* avoid warning about unused args */ 101 return 0; 102 103 printf("\nLZO real-time data compression library (v%s, %s).\n", 104 lzo_version_string(), lzo_version_date()); 105 printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); 106 107 108/* 109 * Step 1: initialize the LZO library 110 */ 111 if (lzo_init() != LZO_E_OK) 112 { 113 printf("internal error - lzo_init() failed !!!\n"); 114 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); 115 return 3; 116 } 117 118/* 119 * Step 2: prepare the input block that will get compressed. 120 * We just fill it with zeros in this example program, 121 * but you would use your real-world data here. 122 */ 123 in_len = IN_LEN; 124 lzo_memset(in,0,in_len); 125 126/* 127 * Step 3: compress from `in' to `out' with LZO1X-1 128 */ 129 r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem); 130 if (r == LZO_E_OK) 131 printf("compressed %lu bytes into %lu bytes\n", 132 (unsigned long) in_len, (unsigned long) out_len); 133 else 134 { 135 /* this should NEVER happen */ 136 printf("internal error - compression failed: %d\n", r); 137 return 2; 138 } 139 /* check for an incompressible block */ 140 if (out_len >= in_len) 141 { 142 printf("This block contains incompressible data.\n"); 143 return 0; 144 } 145 146/* 147 * Step 4: decompress again, now going from `out' to `in' 148 */ 149 new_len = in_len; 150 r = lzo1x_decompress(out,out_len,in,&new_len,NULL); 151 if (r == LZO_E_OK && new_len == in_len) 152 printf("decompressed %lu bytes back into %lu bytes\n", 153 (unsigned long) out_len, (unsigned long) in_len); 154 else 155 { 156 /* this should NEVER happen */ 157 printf("internal error - decompression failed: %d\n", r); 158 return 1; 159 } 160 161 printf("\nminiLZO simple compression test passed.\n"); 162 return 0; 163} 164 165/* 166vi:ts=4:et 167*/ 168 169