Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / lzo / examples / precomp.c
bloba953827b23676b305fcd8a646963073c50f01c00
1 /* precomp.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 LZO.FAQ and simple.c first.
49 // We will be trying both LZO1X-999 and LZO1Y-999 and choose
50 // the algorithm that achieves the best compression ratio.
51 **************************************************************************/
53 #include "lzo/lzoconf.h"
54 #include "lzo/lzo1x.h"
55 #include "lzo/lzo1y.h"
57 #define USE_LZO1X 1
58 #define USE_LZO1Y 1
60 #define PARANOID 1
63 /* portability layer */
64 static const char *progname = NULL;
65 #define WANT_LZO_MALLOC 1
66 #define WANT_LZO_FREAD 1
67 #define WANT_LZO_WILDARGV 1
68 #define WANT_XMALLOC 1
69 #include "examples/portab.h"
72 /*************************************************************************
74 **************************************************************************/
76 int __lzo_cdecl_main main(int argc, char *argv[])
78 int r;
80 lzo_bytep in;
81 lzo_uint in_len;
83 lzo_bytep out;
84 lzo_uint out_bufsize;
85 lzo_uint out_len = 0;
87 lzo_voidp wrkmem;
88 lzo_uint wrk_len;
90 lzo_uint best_len;
91 int best_compress = -1;
93 lzo_uint orig_len;
94 lzo_uint32 uncompressed_checksum;
95 lzo_uint32 compressed_checksum;
97 FILE *fp;
98 const char *in_name = NULL;
99 const char *out_name = NULL;
100 long l;
103 lzo_wildargv(&argc, &argv);
105 printf("\nLZO real-time data compression library (v%s, %s).\n",
106 lzo_version_string(), lzo_version_date());
107 printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
109 progname = argv[0];
110 if (argc < 2 || argc > 3)
112 printf("usage: %s file [output-file]\n", progname);
113 exit(1);
115 in_name = argv[1];
116 if (argc > 2) out_name = argv[2];
119 * Step 1: initialize the LZO library
121 if (lzo_init() != LZO_E_OK)
123 printf("internal error - lzo_init() failed !!!\n");
124 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
125 exit(1);
129 * Step 2: allocate the work-memory
131 wrk_len = 1;
132 #ifdef USE_LZO1X
133 if (wrk_len < LZO1X_999_MEM_COMPRESS)
134 wrk_len = LZO1X_999_MEM_COMPRESS;
135 #endif
136 #ifdef USE_LZO1Y
137 if (wrk_len < LZO1Y_999_MEM_COMPRESS)
138 wrk_len = LZO1Y_999_MEM_COMPRESS;
139 #endif
140 wrkmem = (lzo_voidp) xmalloc(wrk_len);
141 if (wrkmem == NULL)
143 printf("%s: out of memory\n", progname);
144 exit(1);
148 * Step 3: open the input file
150 fp = fopen(in_name,"rb");
151 if (fp == NULL)
153 printf("%s: cannot open file %s\n", progname, in_name);
154 exit(1);
156 fseek(fp, 0, SEEK_END);
157 l = ftell(fp);
158 fseek(fp, 0, SEEK_SET);
159 if (l <= 0)
161 printf("%s: %s: empty file\n", progname, in_name);
162 fclose(fp); fp = NULL;
163 exit(1);
165 in_len = (lzo_uint) l;
166 out_bufsize = in_len + in_len / 16 + 64 + 3;
167 best_len = in_len;
170 * Step 4: allocate compression buffers and read the file
172 in = (lzo_bytep) xmalloc(in_len);
173 out = (lzo_bytep) xmalloc(out_bufsize);
174 if (in == NULL || out == NULL)
176 printf("%s: out of memory\n", progname);
177 exit(1);
179 in_len = (lzo_uint) lzo_fread(fp, in, in_len);
180 printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
181 fclose(fp); fp = NULL;
184 * Step 5: compute a checksum of the uncompressed data
186 uncompressed_checksum = lzo_adler32(0,NULL,0);
187 uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);
190 * Step 6a: compress from 'in' to 'out' with LZO1X-999
192 #ifdef USE_LZO1X
193 out_len = out_bufsize;
194 r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
195 if (r != LZO_E_OK)
197 /* this should NEVER happen */
198 printf("internal error - compression failed: %d\n", r);
199 exit(1);
201 printf("LZO1X-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
202 if (out_len < best_len)
204 best_len = out_len;
205 best_compress = 1; /* LZO1X-999 */
207 #endif /* USE_LZO1X */
210 * Step 6b: compress from 'in' to 'out' with LZO1Y-999
212 #ifdef USE_LZO1Y
213 out_len = out_bufsize;
214 r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
215 if (r != LZO_E_OK)
217 /* this should NEVER happen */
218 printf("internal error - compression failed: %d\n", r);
219 exit(1);
221 printf("LZO1Y-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
222 if (out_len < best_len)
224 best_len = out_len;
225 best_compress = 2; /* LZO1Y-999 */
227 #endif /* USE_LZO1Y */
230 * Step 7: check if compressible
232 if (best_len >= in_len)
234 printf("This file contains incompressible data.\n");
235 return 0;
239 * Step 8: compress data again using the best compressor found
241 out_len = out_bufsize;
242 if (best_compress == 1)
243 r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
244 else if (best_compress == 2)
245 r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
246 else
247 r = -100;
248 assert(r == LZO_E_OK);
249 assert(out_len == best_len);
252 * Step 9: optimize compressed data (compressed data is in 'out' buffer)
254 #if 1
255 /* Optimization does not require any data in the buffer that will
256 * hold the uncompressed data. To prove this, we clear the buffer.
258 lzo_memset(in,0,in_len);
259 #endif
261 orig_len = in_len;
262 r = -100;
263 #ifdef USE_LZO1X
264 if (best_compress == 1)
265 r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
266 #endif
267 #ifdef USE_LZO1Y
268 if (best_compress == 2)
269 r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
270 #endif
271 if (r != LZO_E_OK || orig_len != in_len)
273 /* this should NEVER happen */
274 printf("internal error - optimization failed: %d\n", r);
275 exit(1);
279 * Step 10: compute a checksum of the compressed data
281 compressed_checksum = lzo_adler32(0,NULL,0);
282 compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);
285 * Step 11: write compressed data to a file
287 printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
288 progname, in_name, (long) in_len, (long) out_len,
289 (long) uncompressed_checksum, (long) compressed_checksum);
291 if (out_name && out_name[0])
293 printf("%s: writing to file %s\n", progname, out_name);
294 fp = fopen(out_name,"wb");
295 if (fp == NULL)
297 printf("%s: cannot open output file %s\n", progname, out_name);
298 exit(1);
300 if (lzo_fwrite(fp, out, out_len) != out_len || fclose(fp) != 0)
302 printf("%s: write error !!\n", progname);
303 exit(1);
308 * Step 12: verify decompression
310 #ifdef PARANOID
311 lzo_memset(in,0,in_len); /* paranoia - clear output buffer */
312 orig_len = in_len;
313 r = -100;
314 #ifdef USE_LZO1X
315 if (best_compress == 1)
316 r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
317 #endif
318 #ifdef USE_LZO1Y
319 if (best_compress == 2)
320 r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
321 #endif
322 if (r != LZO_E_OK || orig_len != in_len)
324 /* this should NEVER happen */
325 printf("internal error - decompression failed: %d\n", r);
326 exit(1);
328 if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
330 /* this should NEVER happen */
331 printf("internal error - decompression data error\n");
332 exit(1);
334 /* Now you could also verify decompression under similar conditions as in
335 * your application, e.g. overlapping assembler decompression etc.
337 #endif
339 lzo_free(in);
340 lzo_free(out);
341 lzo_free(wrkmem);
343 return 0;
347 vi:ts=4:et