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
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"
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
[])
91 int best_compress
= -1;
94 lzo_uint32 uncompressed_checksum
;
95 lzo_uint32 compressed_checksum
;
98 const char *in_name
= NULL
;
99 const char *out_name
= NULL
;
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");
110 if (argc
< 2 || argc
> 3)
112 printf("usage: %s file [output-file]\n", progname
);
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");
129 * Step 2: allocate the work-memory
133 if (wrk_len
< LZO1X_999_MEM_COMPRESS
)
134 wrk_len
= LZO1X_999_MEM_COMPRESS
;
137 if (wrk_len
< LZO1Y_999_MEM_COMPRESS
)
138 wrk_len
= LZO1Y_999_MEM_COMPRESS
;
140 wrkmem
= (lzo_voidp
) xmalloc(wrk_len
);
143 printf("%s: out of memory\n", progname
);
148 * Step 3: open the input file
150 fp
= fopen(in_name
,"rb");
153 printf("%s: cannot open file %s\n", progname
, in_name
);
156 fseek(fp
, 0, SEEK_END
);
158 fseek(fp
, 0, SEEK_SET
);
161 printf("%s: %s: empty file\n", progname
, in_name
);
162 fclose(fp
); fp
= NULL
;
165 in_len
= (lzo_uint
) l
;
166 out_bufsize
= in_len
+ in_len
/ 16 + 64 + 3;
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
);
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
193 out_len
= out_bufsize
;
194 r
= lzo1x_999_compress(in
,in_len
,out
,&out_len
,wrkmem
);
197 /* this should NEVER happen */
198 printf("internal error - compression failed: %d\n", r
);
201 printf("LZO1X-999: %8lu -> %8lu\n", (unsigned long) in_len
, (unsigned long) out_len
);
202 if (out_len
< best_len
)
205 best_compress
= 1; /* LZO1X-999 */
207 #endif /* USE_LZO1X */
210 * Step 6b: compress from 'in' to 'out' with LZO1Y-999
213 out_len
= out_bufsize
;
214 r
= lzo1y_999_compress(in
,in_len
,out
,&out_len
,wrkmem
);
217 /* this should NEVER happen */
218 printf("internal error - compression failed: %d\n", r
);
221 printf("LZO1Y-999: %8lu -> %8lu\n", (unsigned long) in_len
, (unsigned long) out_len
);
222 if (out_len
< best_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");
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
);
248 assert(r
== LZO_E_OK
);
249 assert(out_len
== best_len
);
252 * Step 9: optimize compressed data (compressed data is in 'out' buffer)
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
);
264 if (best_compress
== 1)
265 r
= lzo1x_optimize(out
,out_len
,in
,&orig_len
,NULL
);
268 if (best_compress
== 2)
269 r
= lzo1y_optimize(out
,out_len
,in
,&orig_len
,NULL
);
271 if (r
!= LZO_E_OK
|| orig_len
!= in_len
)
273 /* this should NEVER happen */
274 printf("internal error - optimization failed: %d\n", r
);
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");
297 printf("%s: cannot open output file %s\n", progname
, out_name
);
300 if (lzo_fwrite(fp
, out
, out_len
) != out_len
|| fclose(fp
) != 0)
302 printf("%s: write error !!\n", progname
);
308 * Step 12: verify decompression
311 lzo_memset(in
,0,in_len
); /* paranoia - clear output buffer */
315 if (best_compress
== 1)
316 r
= lzo1x_decompress_safe(out
,out_len
,in
,&orig_len
,NULL
);
319 if (best_compress
== 2)
320 r
= lzo1y_decompress_safe(out
,out_len
,in
,&orig_len
,NULL
);
322 if (r
!= LZO_E_OK
|| orig_len
!= in_len
)
324 /* this should NEVER happen */
325 printf("internal error - decompression failed: %d\n", r
);
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");
334 /* Now you could also verify decompression under similar conditions as in
335 * your application, e.g. overlapping assembler decompression etc.