00001 /* testmini.c -- very simple test program for the miniLZO library 00002 00003 This file is part of the LZO real-time data compression library. 00004 00005 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer 00006 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer 00007 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer 00008 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer 00009 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer 00010 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer 00011 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer 00012 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer 00013 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer 00014 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer 00015 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer 00016 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer 00017 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer 00018 All Rights Reserved. 00019 00020 The LZO library is free software; you can redistribute it and/or 00021 modify it under the terms of the GNU General Public License as 00022 published by the Free Software Foundation; either version 2 of 00023 the License, or (at your option) any later version. 00024 00025 The LZO library is distributed in the hope that it will be useful, 00026 but WITHOUT ANY WARRANTY; without even the implied warranty of 00027 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00028 GNU General Public License for more details. 00029 00030 You should have received a copy of the GNU General Public License 00031 along with the LZO library; see the file COPYING. 00032 If not, write to the Free Software Foundation, Inc., 00033 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00034 00035 Markus F.X.J. Oberhumer 00036 <markus@oberhumer.com> 00037 http://www.oberhumer.com/opensource/lzo/ 00038 */ 00039 00040 00041 #include <stdio.h> 00042 #include <stdlib.h> 00043 00044 00045 /************************************************************************* 00046 // This program shows the basic usage of the LZO library. 00047 // We will compress a block of data and decompress again. 00048 // 00049 // For more information, documentation, example programs and other support 00050 // files (like Makefiles and build scripts) please download the full LZO 00051 // package from 00052 // http://www.oberhumer.com/opensource/lzo/ 00053 **************************************************************************/ 00054 00055 /* First let's include "minizo.h". */ 00056 00057 #include "minilzo.h" 00058 00059 00060 /* We want to compress the data block at `in' with length `IN_LEN' to 00061 * the block at `out'. Because the input block may be incompressible, 00062 * we must provide a little more output space in case that compression 00063 * is not possible. 00064 */ 00065 00066 #if defined(__LZO_STRICT_16BIT) 00067 #define IN_LEN (8*1024u) 00068 #elif defined(LZO_ARCH_I086) && !defined(LZO_HAVE_MM_HUGE_ARRAY) 00069 #define IN_LEN (60*1024u) 00070 #else 00071 #define IN_LEN (128*1024ul) 00072 #endif 00073 #define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3) 00074 00075 static unsigned char __LZO_MMODEL in [ IN_LEN ]; 00076 static unsigned char __LZO_MMODEL out [ OUT_LEN ]; 00077 00078 00079 /* Work-memory needed for compression. Allocate memory in units 00080 * of `lzo_align_t' (instead of `char') to make sure it is properly aligned. 00081 */ 00082 00083 #define HEAP_ALLOC(var,size) \ 00084 lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ] 00085 00086 static HEAP_ALLOC(wrkmem,LZO1X_1_MEM_COMPRESS); 00087 00088 00089 /************************************************************************* 00090 // 00091 **************************************************************************/ 00092 00093 int main(int argc, char *argv[]) 00094 { 00095 int r; 00096 lzo_uint in_len; 00097 lzo_uint out_len; 00098 lzo_uint new_len; 00099 00100 if (argc < 0 && argv == NULL) /* avoid warning about unused args */ 00101 return 0; 00102 00103 printf("\nLZO real-time data compression library (v%s, %s).\n", 00104 lzo_version_string(), lzo_version_date()); 00105 printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); 00106 00107 00108 /* 00109 * Step 1: initialize the LZO library 00110 */ 00111 if (lzo_init() != LZO_E_OK) 00112 { 00113 printf("internal error - lzo_init() failed !!!\n"); 00114 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); 00115 return 3; 00116 } 00117 00118 /* 00119 * Step 2: prepare the input block that will get compressed. 00120 * We just fill it with zeros in this example program, 00121 * but you would use your real-world data here. 00122 */ 00123 in_len = IN_LEN; 00124 lzo_memset(in,0,in_len); 00125 00126 /* 00127 * Step 3: compress from `in' to `out' with LZO1X-1 00128 */ 00129 r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem); 00130 if (r == LZO_E_OK) 00131 printf("compressed %lu bytes into %lu bytes\n", 00132 (unsigned long) in_len, (unsigned long) out_len); 00133 else 00134 { 00135 /* this should NEVER happen */ 00136 printf("internal error - compression failed: %d\n", r); 00137 return 2; 00138 } 00139 /* check for an incompressible block */ 00140 if (out_len >= in_len) 00141 { 00142 printf("This block contains incompressible data.\n"); 00143 return 0; 00144 } 00145 00146 /* 00147 * Step 4: decompress again, now going from `out' to `in' 00148 */ 00149 new_len = in_len; 00150 r = lzo1x_decompress(out,out_len,in,&new_len,NULL); 00151 if (r == LZO_E_OK && new_len == in_len) 00152 printf("decompressed %lu bytes back into %lu bytes\n", 00153 (unsigned long) out_len, (unsigned long) in_len); 00154 else 00155 { 00156 /* this should NEVER happen */ 00157 printf("internal error - decompression failed: %d\n", r); 00158 return 1; 00159 } 00160 00161 printf("\nminiLZO simple compression test passed.\n"); 00162 return 0; 00163 } 00164 00165 /* 00166 vi:ts=4:et 00167 */ 00168