Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / lzo / examples / precomp2.c
blobd9e1b47b94e2a7a6abb7cddfe1a6ffab37170b83
1 /* precomp2.c -- example program: how to generate pre-compressed data
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 how to generate pre-compressed data.
47 // Please study precomp.c first.
49 // We will be trying LZO1X-999 and LZO1Y-999, and we will be trying
50 // various parameters using the internal interface to squeeze out
51 // a little bit of extra compression.
53 // NOTE: this program can be quite slow for highly redundant files
54 **************************************************************************/
56 #include "lzo/lzoconf.h"
57 #include "lzo/lzo1x.h"
58 #include "lzo/lzo1y.h"
60 LZO_EXTERN(int)
61 lzo1x_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
62 lzo_bytep out, lzo_uintp out_len,
63 lzo_voidp wrkmem,
64 const lzo_bytep dict, lzo_uint dict_len,
65 lzo_callback_p cb,
66 int try_lazy,
67 lzo_uint good_length,
68 lzo_uint max_lazy,
69 lzo_uint nice_length,
70 lzo_uint max_chain,
71 lzo_uint32 flags );
73 LZO_EXTERN(int)
74 lzo1y_999_compress_internal ( const lzo_bytep in , lzo_uint in_len,
75 lzo_bytep out, lzo_uintp out_len,
76 lzo_voidp wrkmem,
77 const lzo_bytep dict, lzo_uint dict_len,
78 lzo_callback_p cb,
79 int try_lazy,
80 lzo_uint good_length,
81 lzo_uint max_lazy,
82 lzo_uint nice_length,
83 lzo_uint max_chain,
84 lzo_uint32 flags );
86 #define USE_LZO1X 1
87 #define USE_LZO1Y 1
89 #define PARANOID 1
92 /* portability layer */
93 static const char *progname = NULL;
94 #define WANT_LZO_MALLOC 1
95 #define WANT_LZO_FREAD 1
96 #define WANT_LZO_WILDARGV 1
97 #define WANT_XMALLOC 1
98 #include "examples/portab.h"
101 /*************************************************************************
103 **************************************************************************/
105 int __lzo_cdecl_main main(int argc, char *argv[])
107 int r;
108 int lazy;
109 const int max_try_lazy = 5;
110 const lzo_uint big = 65536L; /* can result in very slow compression */
111 const lzo_uint32 flags = 0x1;
113 lzo_bytep in;
114 lzo_uint in_len;
116 lzo_bytep out;
117 lzo_uint out_bufsize;
118 lzo_uint out_len = 0;
120 lzo_voidp wrkmem;
121 lzo_uint wrk_len;
123 lzo_uint best_len;
124 int best_compress = -1;
125 int best_lazy = -1;
127 lzo_uint orig_len;
128 lzo_uint32 uncompressed_checksum;
129 lzo_uint32 compressed_checksum;
131 FILE *fp;
132 const char *in_name = NULL;
133 const char *out_name = NULL;
134 long l;
137 lzo_wildargv(&argc, &argv);
139 printf("\nLZO real-time data compression library (v%s, %s).\n",
140 lzo_version_string(), lzo_version_date());
141 printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
143 progname = argv[0];
144 if (argc < 2 || argc > 3)
146 printf("usage: %s file [output-file]\n", progname);
147 exit(1);
149 in_name = argv[1];
150 if (argc > 2) out_name = argv[2];
153 * Step 1: initialize the LZO library
155 if (lzo_init() != LZO_E_OK)
157 printf("internal error - lzo_init() failed !!!\n");
158 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
159 exit(1);
163 * Step 2: allocate the work-memory
165 wrk_len = 1;
166 #ifdef USE_LZO1X
167 if (wrk_len < LZO1X_999_MEM_COMPRESS)
168 wrk_len = LZO1X_999_MEM_COMPRESS;
169 #endif
170 #ifdef USE_LZO1Y
171 if (wrk_len < LZO1Y_999_MEM_COMPRESS)
172 wrk_len = LZO1Y_999_MEM_COMPRESS;
173 #endif
174 wrkmem = (lzo_voidp) xmalloc(wrk_len);
175 if (wrkmem == NULL)
177 printf("%s: out of memory\n", progname);
178 exit(1);
182 * Step 3: open the input file
184 fp = fopen(in_name,"rb");
185 if (fp == NULL)
187 printf("%s: cannot open file %s\n", progname, in_name);
188 exit(1);
190 fseek(fp, 0, SEEK_END);
191 l = ftell(fp);
192 fseek(fp, 0, SEEK_SET);
193 if (l <= 0)
195 printf("%s: %s: empty file\n", progname, in_name);
196 fclose(fp); fp = NULL;
197 exit(1);
199 in_len = (lzo_uint) l;
200 out_bufsize = in_len + in_len / 16 + 64 + 3;
201 best_len = in_len;
204 * Step 4: allocate compression buffers and read the file
206 in = (lzo_bytep) xmalloc(in_len);
207 out = (lzo_bytep) xmalloc(out_bufsize);
208 if (in == NULL || out == NULL)
210 printf("%s: out of memory\n", progname);
211 exit(1);
213 in_len = (lzo_uint) lzo_fread(fp, in, in_len);
214 printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
215 fclose(fp); fp = NULL;
218 * Step 5: compute a checksum of the uncompressed data
220 uncompressed_checksum = lzo_adler32(0,NULL,0);
221 uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);
224 * Step 6a: compress from 'in' to 'out' with LZO1X-999
226 #ifdef USE_LZO1X
227 for (lazy = 0; lazy <= max_try_lazy; lazy++)
229 out_len = out_bufsize;
230 r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
231 NULL, 0, 0,
232 lazy, big, big, big, big, flags);
233 if (r != LZO_E_OK)
235 /* this should NEVER happen */
236 printf("internal error - compression failed: %d\n", r);
237 exit(1);
239 printf("LZO1X-999: lazy =%2d: %8lu -> %8lu\n",
240 lazy, (unsigned long) in_len, (unsigned long) out_len);
241 if (out_len < best_len)
243 best_len = out_len;
244 best_lazy = lazy;
245 best_compress = 1; /* LZO1X-999 */
248 #endif /* USE_LZO1X */
251 * Step 6b: compress from 'in' to 'out' with LZO1Y-999
253 #ifdef USE_LZO1Y
254 for (lazy = 0; lazy <= max_try_lazy; lazy++)
256 out_len = out_bufsize;
257 r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
258 NULL, 0, 0,
259 lazy, big, big, big, big, flags);
260 if (r != LZO_E_OK)
262 /* this should NEVER happen */
263 printf("internal error - compression failed: %d\n", r);
264 exit(1);
266 printf("LZO1Y-999: lazy =%2d: %8lu -> %8lu\n",
267 lazy, (unsigned long) in_len, (unsigned long) out_len);
268 if (out_len < best_len)
270 best_len = out_len;
271 best_lazy = lazy;
272 best_compress = 2; /* LZO1Y-999 */
275 #endif /* USE_LZO1Y */
278 * Step 7: check if compressible
280 if (best_len >= in_len)
282 printf("This file contains incompressible data.\n");
283 return 0;
287 * Step 8: compress data again using the best compressor found
289 out_len = out_bufsize;
290 if (best_compress == 1)
291 r = lzo1x_999_compress_internal(in,in_len,out,&out_len,wrkmem,
292 NULL, 0, 0,
293 best_lazy, big, big, big, big, flags);
294 else if (best_compress == 2)
295 r = lzo1y_999_compress_internal(in,in_len,out,&out_len,wrkmem,
296 NULL, 0, 0,
297 best_lazy, big, big, big, big, flags);
298 else
299 r = -100;
300 assert(r == LZO_E_OK);
301 assert(out_len == best_len);
304 * Step 9: optimize compressed data (compressed data is in 'out' buffer)
306 #if 1
307 /* Optimization does not require any data in the buffer that will
308 * hold the uncompressed data. To prove this, we clear the buffer.
310 lzo_memset(in,0,in_len);
311 #endif
313 orig_len = in_len;
314 r = -100;
315 #ifdef USE_LZO1X
316 if (best_compress == 1)
317 r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
318 #endif
319 #ifdef USE_LZO1Y
320 if (best_compress == 2)
321 r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
322 #endif
323 if (r != LZO_E_OK || orig_len != in_len)
325 /* this should NEVER happen */
326 printf("internal error - optimization failed: %d\n", r);
327 exit(1);
331 * Step 10: compute a checksum of the compressed data
333 compressed_checksum = lzo_adler32(0,NULL,0);
334 compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);
337 * Step 11: write compressed data to a file
339 printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
340 progname, in_name, (long) in_len, (long) out_len,
341 (long) uncompressed_checksum, (long) compressed_checksum);
343 if (out_name && out_name[0])
345 printf("%s: writing to file %s\n", progname, out_name);
346 fp = fopen(out_name,"wb");
347 if (fp == NULL)
349 printf("%s: cannot open output file %s\n", progname, out_name);
350 exit(1);
352 if (lzo_fwrite(fp, out, out_len) != out_len || fclose(fp) != 0)
354 printf("%s: write error !!\n", progname);
355 exit(1);
360 * Step 12: verify decompression
362 #ifdef PARANOID
363 lzo_memset(in,0,in_len); /* paranoia - clear output buffer */
364 orig_len = in_len;
365 r = -100;
366 #ifdef USE_LZO1X
367 if (best_compress == 1)
368 r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
369 #endif
370 #ifdef USE_LZO1Y
371 if (best_compress == 2)
372 r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
373 #endif
374 if (r != LZO_E_OK || orig_len != in_len)
376 /* this should NEVER happen */
377 printf("internal error - decompression failed: %d\n", r);
378 exit(1);
380 if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
382 /* this should NEVER happen */
383 printf("internal error - decompression data error\n");
384 exit(1);
386 /* Now you could also verify decompression under similar conditions as in
387 * your application, e.g. overlapping assembler decompression etc.
389 #endif
391 lzo_free(in);
392 lzo_free(out);
393 lzo_free(wrkmem);
395 return 0;
399 vi:ts=4:et