Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / lzo / examples / simple.c
blob9d06cb5c6b2d750d8a04140b2cb7a0f4f9b3798e
1 /* simple.c -- the annotated simple example program for the LZO library
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 2011 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 2010 Markus Franz Xaver Johannes Oberhumer
7 Copyright (C) 2009 Markus Franz Xaver Johannes Oberhumer
8 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
9 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
10 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
11 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
12 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
13 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
14 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
15 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
16 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
17 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
18 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
19 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
20 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
21 All Rights Reserved.
23 The LZO library is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2 of
26 the License, or (at your option) any later version.
28 The LZO library is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with the LZO library; see the file COPYING.
35 If not, write to the Free Software Foundation, Inc.,
36 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
38 Markus F.X.J. Oberhumer
39 <markus@oberhumer.com>
40 http://www.oberhumer.com/opensource/lzo/
44 /*************************************************************************
45 // This program shows the basic usage of the LZO library.
46 // We will compress a block of data and decompress again.
48 // See also LZO.FAQ
49 **************************************************************************/
51 /* We will be using the LZO1X-1 algorithm, so we have
52 * to include <lzo1x.h>
55 #include "lzo/lzoconf.h"
56 #include "lzo/lzo1x.h"
58 /* portability layer */
59 static const char *progname = NULL;
60 #define WANT_LZO_MALLOC 1
61 #define WANT_XMALLOC 1
62 #include "examples/portab.h"
65 /* We want to compress the data block at 'in' with length 'IN_LEN' to
66 * the block at 'out'. Because the input block may be incompressible,
67 * we must provide a little more output space in case that compression
68 * is not possible.
71 #ifndef IN_LEN
72 #define IN_LEN (128*1024L)
73 #endif
74 #define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
77 /*************************************************************************
79 **************************************************************************/
81 int __lzo_cdecl_main main(int argc, char *argv[])
83 int r;
84 lzo_bytep in;
85 lzo_bytep out;
86 lzo_voidp wrkmem;
87 lzo_uint in_len;
88 lzo_uint out_len;
89 lzo_uint new_len;
91 if (argc < 0 && argv == NULL) /* avoid warning about unused args */
92 return 0;
94 printf("\nLZO real-time data compression library (v%s, %s).\n",
95 lzo_version_string(), lzo_version_date());
96 printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
99 * Step 1: initialize the LZO library
101 if (lzo_init() != LZO_E_OK)
103 printf("internal error - lzo_init() failed !!!\n");
104 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
105 return 4;
109 * Step 2: allocate blocks and the work-memory
111 in = (lzo_bytep) xmalloc(IN_LEN);
112 out = (lzo_bytep) xmalloc(OUT_LEN);
113 wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
114 if (in == NULL || out == NULL || wrkmem == NULL)
116 printf("out of memory\n");
117 return 3;
121 * Step 3: prepare the input block that will get compressed.
122 * We just fill it with zeros in this example program,
123 * but you would use your real-world data here.
125 in_len = IN_LEN;
126 lzo_memset(in,0,in_len);
129 * Step 4: compress from 'in' to 'out' with LZO1X-1
131 r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
132 if (r == LZO_E_OK)
133 printf("compressed %lu bytes into %lu bytes\n",
134 (unsigned long) in_len, (unsigned long) out_len);
135 else
137 /* this should NEVER happen */
138 printf("internal error - compression failed: %d\n", r);
139 return 2;
141 /* check for an incompressible block */
142 if (out_len >= in_len)
144 printf("This block contains incompressible data.\n");
145 return 0;
149 * Step 5: decompress again, now going from 'out' to 'in'
151 new_len = in_len;
152 r = lzo1x_decompress(out,out_len,in,&new_len,NULL);
153 if (r == LZO_E_OK && new_len == in_len)
154 printf("decompressed %lu bytes back into %lu bytes\n",
155 (unsigned long) out_len, (unsigned long) in_len);
156 else
158 /* this should NEVER happen */
159 printf("internal error - decompression failed: %d\n", r);
160 return 1;
163 lzo_free(wrkmem);
164 lzo_free(out);
165 lzo_free(in);
166 printf("Simple compression test passed.\n");
167 return 0;
171 vi:ts=4:et