Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / lzo / examples / overlap.c
blob114fe3b4f27ccdca828594a6c7b3c2694a8421a8
1 /* overlap.c -- example program: overlapping (de)compression
3 This file is part of the LZO real-time data compression library.
5 Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer
6 All Rights Reserved.
8 The LZO library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
13 The LZO library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with the LZO library; see the file COPYING.
20 If not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 Markus F.X.J. Oberhumer
24 <markus@oberhumer.com>
25 http://www.oberhumer.com/opensource/lzo/
29 /*************************************************************************
30 // This program shows how to do overlapping compression and
31 // in-place decompression.
33 // Please study LZO.FAQ and simple.c first.
34 **************************************************************************/
36 #include "lzo/lzoconf.h"
37 #include "lzo/lzo1x.h"
39 /* portability layer */
40 static const char *progname = NULL;
41 #define WANT_LZO_MALLOC 1
42 #define WANT_LZO_FREAD 1
43 #define WANT_LZO_WILDARGV 1
44 #define WANT_XMALLOC 1
45 #include "examples/portab.h"
48 /* Overhead (in bytes) for the in-place decompression buffer.
49 * Most files need only 16 !
50 * (try 'overlap -16 file' or even 'overlap -8 file')
52 * Worst case (for files that are compressible by only a few bytes)
53 * is 'in_len / 16 + 64 + 3'. See step 5a) below.
55 * For overlapping compression '0xbfff + in_len / 16 + 64 + 3' bytes
56 * will be needed. See step 4a) below.
59 static long opt_overhead = 0; /* assume worst case */
61 static unsigned long total_files = 0;
62 static unsigned long total_in = 0;
65 /*************************************************************************
67 **************************************************************************/
69 static int do_file(const char *in_name)
71 int r;
72 FILE *fp = NULL;
73 long l;
75 lzo_voidp wrkmem = NULL;
77 lzo_bytep in = NULL;
78 lzo_uint in_len; /* uncompressed length */
80 lzo_bytep out = NULL;
81 lzo_uint out_len; /* compressed length */
83 lzo_bytep overlap = NULL;
84 lzo_uint overhead;
85 lzo_uint offset;
87 lzo_uint new_len = 0;
90 * Step 1: open the input file
92 fp = fopen(in_name, "rb");
93 if (fp == NULL)
95 printf("%s: %s: cannot open file\n", progname, in_name);
96 goto next_file;
98 fseek(fp, 0, SEEK_END);
99 l = ftell(fp);
100 fseek(fp, 0, SEEK_SET);
101 if (l <= 0)
103 printf("%s: %s: empty file -- skipping\n", progname, in_name);
104 goto next_file;
106 in_len = (lzo_uint) l;
107 if ((long) in_len != l || l > 256L * 1024L * 1024L)
109 printf("%s: %s: file is too big -- skipping\n", progname, in_name);
110 goto next_file;
114 * Step 2: allocate compression buffers and read the file
116 in = (lzo_bytep) xmalloc(in_len);
117 out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3);
118 wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
119 in_len = (lzo_uint) lzo_fread(fp, in, in_len);
120 fclose(fp); fp = NULL;
121 printf("%s: %s: read %lu bytes\n", progname, in_name, (unsigned long) in_len);
123 total_files++;
124 total_in += (unsigned long) in_len;
127 * Step 3: compress from 'in' to 'out' with LZO1X-1
129 r = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
130 if (r != LZO_E_OK || out_len > in_len + in_len / 16 + 64 + 3)
132 /* this should NEVER happen */
133 printf("internal error - compression failed: %d\n", r);
134 exit(1);
136 printf("%-25s %8lu -> %8lu\n", "LZO1X-1:", (unsigned long) in_len, (unsigned long) out_len);
139 /***** Step 4: overlapping compression *****/
142 * Step 4a: allocate the 'overlap' buffer for overlapping compression
144 overhead = in_len > 0xbfff ? 0xbfff : in_len;
145 overhead += in_len / 16 + 64 + 3;
146 overlap = (lzo_bytep) xmalloc(in_len + overhead);
149 * Step 4b: prepare data in 'overlap' buffer.
150 * copy uncompressed data at the top of the overlap buffer
152 /*** offset = in_len + overhead - in_len; ***/
153 offset = overhead;
154 lzo_memcpy(overlap + offset, in, in_len);
157 * Step 4c: do an in-place compression within the 'overlap' buffer
159 r = lzo1x_1_compress(overlap + offset, in_len, overlap, &new_len, wrkmem);
160 if (r != LZO_E_OK)
162 /* this should NEVER happen */
163 printf("in-place compression failed: %d\n", r);
164 exit(1);
168 * Step 4d: verify overlapping compression
170 if (new_len != out_len || lzo_memcmp(out, overlap, out_len) != 0)
172 /* As compression is non-deterministic there can be a difference
173 * in the representation of the compressed data (but this usually
174 * happens very seldom). So we have to verify the overlapping
175 * compression by doing a temporary decompression.
177 lzo_uint ll = in_len;
178 lzo_bytep tmp = (lzo_bytep) xmalloc(ll);
179 r = lzo1x_decompress_safe(overlap, new_len, tmp, &ll, NULL);
180 if (r != LZO_E_OK || ll != in_len || lzo_memcmp(in, tmp, ll) != 0)
182 /* this should NEVER happen */
183 printf("in-place compression data error\n");
184 exit(1);
186 lzo_free(tmp);
189 printf(" in-place compression: %8lu -> %8lu overhead: %7lu\n",
190 (unsigned long) in_len, (unsigned long) new_len, (unsigned long) overhead);
191 lzo_free(overlap); overlap = NULL;
194 /***** Step 5: in-place decompression *****/
197 * Step 5a: allocate the 'overlap' buffer for in-place decompression
199 if (opt_overhead == 0 || out_len >= in_len)
200 overhead = in_len / 16 + 64 + 3;
201 else
202 overhead = (lzo_uint) opt_overhead;
203 overlap = (lzo_bytep) xmalloc(in_len + overhead);
206 * Step 5b: prepare data in 'overlap' buffer.
207 * copy compressed data at the top of the overlap buffer
209 offset = in_len + overhead - out_len;
210 lzo_memcpy(overlap + offset, out, out_len);
213 * Step 5c: do an in-place decompression within the 'overlap' buffer
215 new_len = in_len;
216 r = lzo1x_decompress_safe(overlap + offset, out_len, overlap, &new_len, NULL);
217 if (r != LZO_E_OK)
219 /* this may happen if overhead is too small */
220 printf("in-place decompression failed: %d - increase 'opt_overhead'\n", r);
221 exit(1);
225 * Step 5d: verify decompression
227 if (new_len != in_len || lzo_memcmp(in, overlap, in_len) != 0)
229 /* this may happen if overhead is too small */
230 printf("in-place decompression data error - increase 'opt_overhead'\n");
231 exit(1);
233 printf(" in-place decompression: %8lu -> %8lu overhead: %7lu\n",
234 (unsigned long) out_len, (unsigned long) new_len, (unsigned long) overhead);
235 lzo_free(overlap); overlap = NULL;
238 next_file:
239 lzo_free(overlap);
240 lzo_free(wrkmem);
241 lzo_free(out);
242 lzo_free(in);
243 if (fp) fclose(fp);
245 return 0;
249 /*************************************************************************
251 **************************************************************************/
253 int __lzo_cdecl_main main(int argc, char *argv[])
255 int r;
256 int i = 1;
258 lzo_wildargv(&argc, &argv);
260 printf("\nLZO real-time data compression library (v%s, %s).\n",
261 lzo_version_string(), lzo_version_date());
262 printf("Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
264 progname = argv[0];
265 if (i < argc && argv[i][0] == '-')
266 opt_overhead = atol(&argv[i++][1]);
267 #if 1
268 if (opt_overhead != 0 && opt_overhead < 4)
270 printf("%s: invalid overhead value %ld\n", progname, opt_overhead);
271 exit(1);
273 #endif
274 if (i >= argc)
276 printf("usage: %s [-overhead_in_bytes] file..\n", progname);
277 exit(1);
281 * Step 1: initialize the LZO library
283 if (lzo_init() != LZO_E_OK)
285 printf("internal error - lzo_init() failed !!!\n");
286 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
287 exit(1);
291 * Step 2: process files
293 for (r = 0; r == 0 && i < argc; i++)
294 r = do_file(argv[i]);
296 printf("\nDone. Successfully processed %lu bytes in %lu files.\n",
297 total_in, total_files);
298 return r;
302 /* vim:set ts=4 sw=4 et: */