1 /* precomp2.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 precomp.c first.
49 // We will be trying LZO1X-999 and LZO1Y-999, and we will be trying
50 // various parameters using the internal interface to squeeze out
51 // a little bit of extra compression.
53 // NOTE: this program can be quite slow for highly redundant files
54 **************************************************************************/
56 #include "lzo/lzoconf.h"
57 #include "lzo/lzo1x.h"
58 #include "lzo/lzo1y.h"
61 lzo1x_999_compress_internal ( const lzo_bytep in
, lzo_uint in_len
,
62 lzo_bytep out
, lzo_uintp out_len
,
64 const lzo_bytep dict
, lzo_uint dict_len
,
74 lzo1y_999_compress_internal ( const lzo_bytep in
, lzo_uint in_len
,
75 lzo_bytep out
, lzo_uintp out_len
,
77 const lzo_bytep dict
, lzo_uint dict_len
,
92 /* portability layer */
93 static const char *progname
= NULL
;
94 #define WANT_LZO_MALLOC 1
95 #define WANT_LZO_FREAD 1
96 #define WANT_LZO_WILDARGV 1
97 #define WANT_XMALLOC 1
98 #include "examples/portab.h"
101 /*************************************************************************
103 **************************************************************************/
105 int __lzo_cdecl_main
main(int argc
, char *argv
[])
109 const int max_try_lazy
= 5;
110 const lzo_uint big
= 65536L; /* can result in very slow compression */
111 const lzo_uint32 flags
= 0x1;
117 lzo_uint out_bufsize
;
118 lzo_uint out_len
= 0;
124 int best_compress
= -1;
128 lzo_uint32 uncompressed_checksum
;
129 lzo_uint32 compressed_checksum
;
132 const char *in_name
= NULL
;
133 const char *out_name
= NULL
;
137 lzo_wildargv(&argc
, &argv
);
139 printf("\nLZO real-time data compression library (v%s, %s).\n",
140 lzo_version_string(), lzo_version_date());
141 printf("Copyright (C) 1996-2011 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
144 if (argc
< 2 || argc
> 3)
146 printf("usage: %s file [output-file]\n", progname
);
150 if (argc
> 2) out_name
= argv
[2];
153 * Step 1: initialize the LZO library
155 if (lzo_init() != LZO_E_OK
)
157 printf("internal error - lzo_init() failed !!!\n");
158 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
163 * Step 2: allocate the work-memory
167 if (wrk_len
< LZO1X_999_MEM_COMPRESS
)
168 wrk_len
= LZO1X_999_MEM_COMPRESS
;
171 if (wrk_len
< LZO1Y_999_MEM_COMPRESS
)
172 wrk_len
= LZO1Y_999_MEM_COMPRESS
;
174 wrkmem
= (lzo_voidp
) xmalloc(wrk_len
);
177 printf("%s: out of memory\n", progname
);
182 * Step 3: open the input file
184 fp
= fopen(in_name
,"rb");
187 printf("%s: cannot open file %s\n", progname
, in_name
);
190 fseek(fp
, 0, SEEK_END
);
192 fseek(fp
, 0, SEEK_SET
);
195 printf("%s: %s: empty file\n", progname
, in_name
);
196 fclose(fp
); fp
= NULL
;
199 in_len
= (lzo_uint
) l
;
200 out_bufsize
= in_len
+ in_len
/ 16 + 64 + 3;
204 * Step 4: allocate compression buffers and read the file
206 in
= (lzo_bytep
) xmalloc(in_len
);
207 out
= (lzo_bytep
) xmalloc(out_bufsize
);
208 if (in
== NULL
|| out
== NULL
)
210 printf("%s: out of memory\n", progname
);
213 in_len
= (lzo_uint
) lzo_fread(fp
, in
, in_len
);
214 printf("%s: loaded file %s: %ld bytes\n", progname
, in_name
, (long) in_len
);
215 fclose(fp
); fp
= NULL
;
218 * Step 5: compute a checksum of the uncompressed data
220 uncompressed_checksum
= lzo_adler32(0,NULL
,0);
221 uncompressed_checksum
= lzo_adler32(uncompressed_checksum
,in
,in_len
);
224 * Step 6a: compress from 'in' to 'out' with LZO1X-999
227 for (lazy
= 0; lazy
<= max_try_lazy
; lazy
++)
229 out_len
= out_bufsize
;
230 r
= lzo1x_999_compress_internal(in
,in_len
,out
,&out_len
,wrkmem
,
232 lazy
, big
, big
, big
, big
, flags
);
235 /* this should NEVER happen */
236 printf("internal error - compression failed: %d\n", r
);
239 printf("LZO1X-999: lazy =%2d: %8lu -> %8lu\n",
240 lazy
, (unsigned long) in_len
, (unsigned long) out_len
);
241 if (out_len
< best_len
)
245 best_compress
= 1; /* LZO1X-999 */
248 #endif /* USE_LZO1X */
251 * Step 6b: compress from 'in' to 'out' with LZO1Y-999
254 for (lazy
= 0; lazy
<= max_try_lazy
; lazy
++)
256 out_len
= out_bufsize
;
257 r
= lzo1y_999_compress_internal(in
,in_len
,out
,&out_len
,wrkmem
,
259 lazy
, big
, big
, big
, big
, flags
);
262 /* this should NEVER happen */
263 printf("internal error - compression failed: %d\n", r
);
266 printf("LZO1Y-999: lazy =%2d: %8lu -> %8lu\n",
267 lazy
, (unsigned long) in_len
, (unsigned long) out_len
);
268 if (out_len
< best_len
)
272 best_compress
= 2; /* LZO1Y-999 */
275 #endif /* USE_LZO1Y */
278 * Step 7: check if compressible
280 if (best_len
>= in_len
)
282 printf("This file contains incompressible data.\n");
287 * Step 8: compress data again using the best compressor found
289 out_len
= out_bufsize
;
290 if (best_compress
== 1)
291 r
= lzo1x_999_compress_internal(in
,in_len
,out
,&out_len
,wrkmem
,
293 best_lazy
, big
, big
, big
, big
, flags
);
294 else if (best_compress
== 2)
295 r
= lzo1y_999_compress_internal(in
,in_len
,out
,&out_len
,wrkmem
,
297 best_lazy
, big
, big
, big
, big
, flags
);
300 assert(r
== LZO_E_OK
);
301 assert(out_len
== best_len
);
304 * Step 9: optimize compressed data (compressed data is in 'out' buffer)
307 /* Optimization does not require any data in the buffer that will
308 * hold the uncompressed data. To prove this, we clear the buffer.
310 lzo_memset(in
,0,in_len
);
316 if (best_compress
== 1)
317 r
= lzo1x_optimize(out
,out_len
,in
,&orig_len
,NULL
);
320 if (best_compress
== 2)
321 r
= lzo1y_optimize(out
,out_len
,in
,&orig_len
,NULL
);
323 if (r
!= LZO_E_OK
|| orig_len
!= in_len
)
325 /* this should NEVER happen */
326 printf("internal error - optimization failed: %d\n", r
);
331 * Step 10: compute a checksum of the compressed data
333 compressed_checksum
= lzo_adler32(0,NULL
,0);
334 compressed_checksum
= lzo_adler32(compressed_checksum
,out
,out_len
);
337 * Step 11: write compressed data to a file
339 printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
340 progname
, in_name
, (long) in_len
, (long) out_len
,
341 (long) uncompressed_checksum
, (long) compressed_checksum
);
343 if (out_name
&& out_name
[0])
345 printf("%s: writing to file %s\n", progname
, out_name
);
346 fp
= fopen(out_name
,"wb");
349 printf("%s: cannot open output file %s\n", progname
, out_name
);
352 if (lzo_fwrite(fp
, out
, out_len
) != out_len
|| fclose(fp
) != 0)
354 printf("%s: write error !!\n", progname
);
360 * Step 12: verify decompression
363 lzo_memset(in
,0,in_len
); /* paranoia - clear output buffer */
367 if (best_compress
== 1)
368 r
= lzo1x_decompress_safe(out
,out_len
,in
,&orig_len
,NULL
);
371 if (best_compress
== 2)
372 r
= lzo1y_decompress_safe(out
,out_len
,in
,&orig_len
,NULL
);
374 if (r
!= LZO_E_OK
|| orig_len
!= in_len
)
376 /* this should NEVER happen */
377 printf("internal error - decompression failed: %d\n", r
);
380 if (uncompressed_checksum
!= lzo_adler32(lzo_adler32(0,NULL
,0),in
,in_len
))
382 /* this should NEVER happen */
383 printf("internal error - decompression data error\n");
386 /* Now you could also verify decompression under similar conditions as in
387 * your application, e.g. overlapping assembler decompression etc.