Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / lzo / examples / dict.c
blob3ccc9a534c0765e7a636455d20c38f92d10b3e06
1 /* dict.c -- example program: how to use preset dictionaries
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 use preset dictionaries.
32 // Please study LZO.FAQ and simple.c first.
33 **************************************************************************/
35 #include "lzo/lzoconf.h"
36 #include "lzo/lzo1x.h"
38 /* portability layer */
39 static const char *progname = NULL;
40 #define WANT_LZO_MALLOC 1
41 #define WANT_LZO_FREAD 1
42 #define WANT_LZO_WILDARGV 1
43 #define WANT_XMALLOC 1
44 #include "examples/portab.h"
47 #define DICT_LEN 0xbfff
48 static lzo_bytep dict;
49 static lzo_uint dict_len = 0;
50 static lzo_uint32_t dict_adler32;
53 /*************************************************************************
55 **************************************************************************/
57 static lzo_uint total_n = 0;
58 static lzo_uint total_c_len = 0;
59 static lzo_uint total_d_len = 0;
61 static void print_info(const char *name, lzo_uint d_len, lzo_uint c_len)
63 double perc;
65 perc = (d_len > 0) ? c_len * 100.0 / d_len : 0.0;
66 printf(" | %-30s %9ld -> %9ld %7.2f%% |\n",
67 name, (long) d_len, (long) c_len, perc);
69 total_n++;
70 total_c_len += c_len;
71 total_d_len += d_len;
75 /*************************************************************************
77 **************************************************************************/
79 static int do_file(const char *in_name, int compression_level)
81 int r;
82 lzo_bytep in;
83 lzo_bytep out;
84 lzo_bytep newb;
85 lzo_voidp wrkmem;
86 lzo_uint in_len;
87 lzo_uint out_len;
88 lzo_uint new_len;
89 long l;
90 FILE *fp;
93 * Step 1: open the input file
95 fp = fopen(in_name,"rb");
96 if (fp == NULL)
98 printf("%s: %s: cannot open file\n", progname, in_name);
99 return 0; /* no error */
101 fseek(fp, 0, SEEK_END);
102 l = ftell(fp);
103 fseek(fp, 0, SEEK_SET);
104 if (l <= 0)
106 printf("%s: %s: empty file -- skipping\n", progname, in_name);
107 fclose(fp); fp = NULL;
108 return 0; /* no error */
110 in_len = (lzo_uint) l;
111 if ((long) in_len != l || l > 256L * 1024L * 1024L)
113 printf("%s: %s: file is too big -- skipping\n", progname, in_name);
114 fclose(fp); fp = NULL;
115 return 0; /* no error */
119 * Step 2: allocate compression buffers and read the file
121 in = (lzo_bytep) xmalloc(in_len);
122 out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3);
123 newb = (lzo_bytep) xmalloc(in_len);
124 wrkmem = (lzo_voidp) xmalloc(LZO1X_999_MEM_COMPRESS);
125 if (in == NULL || out == NULL || newb == NULL || wrkmem == NULL)
127 printf("%s: out of memory\n", progname);
128 exit(1);
130 in_len = (lzo_uint) lzo_fread(fp, in, in_len);
131 fclose(fp); fp = NULL;
134 * Step 3: compress from 'in' to 'out' with LZO1X-999
136 r = lzo1x_999_compress_level(in,in_len,out,&out_len,wrkmem,
137 dict, dict_len, 0, compression_level);
138 if (r != LZO_E_OK)
140 /* this should NEVER happen */
141 printf("internal error - compression failed: %d\n", r);
142 return 1;
145 print_info(in_name, in_len, out_len);
148 * Step 4: decompress again, now going from 'out' to 'newb'
150 new_len = in_len;
151 r = lzo1x_decompress_dict_safe(out, out_len, newb, &new_len, NULL, dict, dict_len);
152 if (r != LZO_E_OK)
154 /* this should NEVER happen */
155 printf("internal error - decompression failed: %d\n", r);
156 return 1;
160 * Step 5: verify decompression
162 if (new_len != in_len || lzo_memcmp(in, newb, in_len) != 0)
164 /* this should NEVER happen */
165 printf("internal error - decompression data error\n");
166 return 1;
169 /* free buffers in reverse order to help malloc() */
170 lzo_free(wrkmem);
171 lzo_free(newb);
172 lzo_free(out);
173 lzo_free(in);
174 return 0;
178 /*************************************************************************
180 **************************************************************************/
182 int __lzo_cdecl_main main(int argc, char *argv[])
184 int i = 1;
185 int r = 0;
186 const char *dict_name;
187 FILE *fp;
188 time_t t_total;
189 int compression_level = 7;
191 lzo_wildargv(&argc, &argv);
193 printf("\nLZO real-time data compression library (v%s, %s).\n",
194 lzo_version_string(), lzo_version_date());
195 printf("Copyright (C) 1996-2014 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
197 progname = argv[0];
199 if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1]))
200 compression_level = atoi(&argv[i++][1]);
202 if (i + 1 >= argc || compression_level < 1 || compression_level > 9)
204 printf("usage: %s [-level] [ dictionary-file | -n ] file...\n", progname);
205 exit(1);
207 printf("Compression level is LZO1X-999/%d\n", compression_level);
210 * Step 1: initialize the LZO library
212 if (lzo_init() != LZO_E_OK)
214 printf("internal error - lzo_init() failed !!!\n");
215 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
216 exit(1);
220 * Step 2: prepare the dictionary
222 dict = (lzo_bytep) xmalloc(DICT_LEN);
223 if (dict == NULL)
225 printf("%s: out of memory\n", progname);
226 exit(1);
228 dict_name = argv[i++];
229 if (strcmp(dict_name,"-n") == 0)
231 dict_name = "empty";
232 dict_len = 0;
234 else
236 fp = fopen(dict_name,"rb");
237 if (fp == NULL)
239 printf("%s: cannot open dictionary file %s\n", progname, dict_name);
240 exit(1);
242 dict_len = (lzo_uint) lzo_fread(fp, dict, DICT_LEN);
243 fclose(fp); fp = NULL;
246 dict_adler32 = lzo_adler32(0, NULL, 0);
247 dict_adler32 = lzo_adler32(dict_adler32, dict, dict_len);
248 printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n",
249 dict_name, (long) dict_len, (unsigned long) dict_adler32);
252 * Step 3: process files
254 t_total = time(NULL);
255 for ( ; i < argc; i++) {
256 if (do_file(argv[i], compression_level) != 0) {
257 r = 1;
258 break;
261 t_total = time(NULL) - t_total;
263 lzo_free(dict);
265 if (total_n > 1)
266 print_info("***TOTALS***", total_d_len, total_c_len);
268 printf("Dictionary compression test %s, execution time %lu seconds.\n",
269 r == 0 ? "passed" : "FAILED", (unsigned long) t_total);
270 return r;
274 /* vim:set ts=4 sw=4 et: */