1/* simple.c -- the annotated simple example program for the LZO 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/************************************************************************* 42// This program shows the basic usage of the LZO library. 43// We will compress a block of data and decompress again. 44// 45// See also LZO.FAQ 46**************************************************************************/ 47 48/* We will be using the LZO1X-1 algorithm, so we have 49 * to include <lzo1x.h> 50 */ 51 52#include "lzo/lzoconf.h" 53#include "lzo/lzo1x.h" 54 55/* portability layer */ 56#define WANT_LZO_MALLOC 1 57#include "examples/portab.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#ifndef IN_LEN 67#define IN_LEN (128*1024L) 68#endif 69#define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3) 70 71 72/************************************************************************* 73// 74**************************************************************************/ 75 76int __lzo_cdecl_main main(int argc, char *argv[]) 77{ 78 int r; 79 lzo_bytep in; 80 lzo_bytep out; 81 lzo_bytep wrkmem; 82 lzo_uint in_len; 83 lzo_uint out_len; 84 lzo_uint new_len; 85 86 if (argc < 0 && argv == NULL) /* avoid warning about unused args */ 87 return 0; 88 89 printf("\nLZO real-time data compression library (v%s, %s).\n", 90 lzo_version_string(), lzo_version_date()); 91 printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); 92 93/* 94 * Step 1: initialize the LZO library 95 */ 96 if (lzo_init() != LZO_E_OK) 97 { 98 printf("internal error - lzo_init() failed !!!\n"); 99 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); 100 return 4; 101 } 102 103/* 104 * Step 2: allocate blocks and the work-memory 105 */ 106 in = (lzo_bytep) lzo_malloc(IN_LEN); 107 out = (lzo_bytep) lzo_malloc(OUT_LEN); 108 wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_MEM_COMPRESS); 109 if (in == NULL || out == NULL || wrkmem == NULL) 110 { 111 printf("out of memory\n"); 112 return 3; 113 } 114 115/* 116 * Step 3: prepare the input block that will get compressed. 117 * We just fill it with zeros in this example program, 118 * but you would use your real-world data here. 119 */ 120 in_len = IN_LEN; 121 lzo_memset(in,0,in_len); 122 123/* 124 * Step 4: compress from `in' to `out' with LZO1X-1 125 */ 126 r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem); 127 if (r == LZO_E_OK) 128 printf("compressed %lu bytes into %lu bytes\n", 129 (unsigned long) in_len, (unsigned long) out_len); 130 else 131 { 132 /* this should NEVER happen */ 133 printf("internal error - compression failed: %d\n", r); 134 return 2; 135 } 136 /* check for an incompressible block */ 137 if (out_len >= in_len) 138 { 139 printf("This block contains incompressible data.\n"); 140 return 0; 141 } 142 143/* 144 * Step 5: decompress again, now going from `out' to `in' 145 */ 146 new_len = in_len; 147 r = lzo1x_decompress(out,out_len,in,&new_len,NULL); 148 if (r == LZO_E_OK && new_len == in_len) 149 printf("decompressed %lu bytes back into %lu bytes\n", 150 (unsigned long) out_len, (unsigned long) in_len); 151 else 152 { 153 /* this should NEVER happen */ 154 printf("internal error - decompression failed: %d\n", r); 155 return 1; 156 } 157 158 lzo_free(wrkmem); 159 lzo_free(out); 160 lzo_free(in); 161 printf("Simple compression test passed.\n"); 162 return 0; 163} 164 165/* 166vi:ts=4:et 167*/ 168 169