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) 2008 Markus Franz Xaver Johannes Oberhumer
6 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
7 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
8 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
9 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
10 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
11 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
12 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
13 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
14 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
15 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
16 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
17 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
20 The LZO library is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation; either version 2 of
23 the License, or (at your option) any later version.
25 The LZO library is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with the LZO library; see the file COPYING.
32 If not, write to the Free Software Foundation, Inc.,
33 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 Markus F.X.J. Oberhumer
36 <markus@oberhumer.com>
37 http://www.oberhumer.com/opensource/lzo/
41 /*************************************************************************
42 // This program shows how to use preset dictionaries.
44 // Please study LZO.FAQ and simple.c first.
45 **************************************************************************/
47 #include "lzo/lzoconf.h"
48 #include "lzo/lzo1x.h"
50 /* portability layer */
51 #define WANT_LZO_MALLOC 1
52 #define WANT_LZO_FREAD 1
53 #define WANT_LZO_WILDARGV 1
54 #include "examples/portab.h"
57 static const char *progname = NULL;
59 #define DICT_LEN 0xbfff
60 static lzo_bytep dict;
61 static lzo_uint dict_len = 0;
62 static lzo_uint32 dict_adler32;
65 /*************************************************************************
67 **************************************************************************/
69 static lzo_uint total_n = 0;
70 static lzo_uint total_c_len = 0;
71 static lzo_uint total_d_len = 0;
73 static void print_file ( const char *name, lzo_uint d_len, lzo_uint c_len )
77 perc = (d_len > 0) ? c_len * 100.0 / d_len : 0;
78 printf(" | %-30s %9ld -> %9ld %7.2f%% |\n",
79 name, (long) d_len, (long) c_len, perc);
87 /*************************************************************************
89 **************************************************************************/
91 int do_file ( const char *in_name, int level )
105 * Step 1: open the input file
107 f = fopen(in_name,"rb");
110 printf("%s: cannot open file %s\n", progname, in_name);
111 return 0; /* no error */
118 printf("%s: %s: empty file -- skipping\n", progname, in_name);
122 in_len = (lzo_uint) l;
125 * Step 2: allocate compression buffers and read the file
127 in = (lzo_bytep) lzo_malloc(in_len);
128 out = (lzo_bytep) lzo_malloc(in_len + in_len / 16 + 64 + 3);
129 newb = (lzo_bytep) lzo_malloc(in_len);
130 wrkmem = (lzo_bytep) lzo_malloc(LZO1X_999_MEM_COMPRESS);
131 if (in == NULL || out == NULL || newb == NULL || wrkmem == NULL)
133 printf("%s: out of memory\n", progname);
136 in_len = (lzo_uint) lzo_fread(f,in,in_len);
140 * Step 3: compress from `in' to `out' with LZO1X-999
142 r = lzo1x_999_compress_level(in,in_len,out,&out_len,wrkmem,
143 dict, dict_len, 0, level);
146 /* this should NEVER happen */
147 printf("internal error - compression failed: %d\n", r);
151 print_file(in_name,in_len,out_len);
154 * Step 4: decompress again, now going from `out' to `newb'
157 r = lzo1x_decompress_dict_safe(out,out_len,newb,&new_len,NULL,dict,dict_len);
160 /* this should NEVER happen */
161 printf("internal error - decompression failed: %d\n", r);
166 * Step 5: verify decompression
168 if (new_len != in_len || lzo_memcmp(in,newb,in_len) != 0)
170 /* this should NEVER happen */
171 printf("internal error - decompression data error\n");
175 /* free buffers in reverse order to help malloc() */
184 /*************************************************************************
186 **************************************************************************/
188 int __lzo_cdecl_main main(int argc, char *argv[])
192 const char *dict_name;
197 lzo_wildargv(&argc, &argv);
199 printf("\nLZO real-time data compression library (v%s, %s).\n",
200 lzo_version_string(), lzo_version_date());
201 printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
205 if (i < argc && argv[i][0] == '-' && isdigit(argv[i][1]))
206 level = atoi(&argv[i++][1]);
208 if (i + 1 >= argc || level < 1 || level > 9)
210 printf("usage: %s [-level] [ dictionary-file | -n ] file...\n", progname);
213 printf("Compression level is LZO1X-999/%d\n", level);
216 * Step 1: initialize the LZO library
218 if (lzo_init() != LZO_E_OK)
220 printf("internal error - lzo_init() failed !!!\n");
221 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n");
226 * Step 2: prepare the dictionary
228 dict = (lzo_bytep) lzo_malloc(DICT_LEN);
231 printf("%s: out of memory\n", progname);
234 dict_name = argv[i++];
235 if (strcmp(dict_name,"-n") == 0)
242 f = fopen(dict_name,"rb");
245 printf("%s: cannot open dictionary file %s\n", progname, dict_name);
248 dict_len = (lzo_uint) lzo_fread(f,dict,DICT_LEN);
252 dict_adler32 = lzo_adler32(0,NULL,0);
253 dict_adler32 = lzo_adler32(dict_adler32,dict,dict_len);
254 printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n",
255 dict_name, (long) dict_len, (long) dict_adler32);
258 * Step 3: process files
260 t_total = time(NULL);
261 for (r = 0; r == 0 && i < argc; i++)
262 r = do_file(argv[i], level);
263 t_total = time(NULL) - t_total;
268 print_file("***TOTALS***",total_d_len,total_c_len);
270 printf("Dictionary compression test %s, execution time %lu seconds.\n",
271 r == 0 ? "passed" : "FAILED", (unsigned long) t_total);