1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
8 * Boston MA 02110-1301, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
14 This file is based in part on:
16 precomp2.c -- example program: how to generate pre-compressed data
18 This file is part of the LZO real-time data compression library.
20 Copyright (C) 2008 Markus Franz Xaver Johannes Oberhumer
21 Copyright (C) 2007 Markus Franz Xaver Johannes Oberhumer
22 Copyright (C) 2006 Markus Franz Xaver Johannes Oberhumer
23 Copyright (C) 2005 Markus Franz Xaver Johannes Oberhumer
24 Copyright (C) 2004 Markus Franz Xaver Johannes Oberhumer
25 Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
26 Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
27 Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
28 Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
29 Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
30 Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
31 Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
32 Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
35 The LZO library is free software; you can redistribute it and/or
36 modify it under the terms of the GNU General Public License as
37 published by the Free Software Foundation; either version 2 of
38 the License, or (at your option) any later version.
40 The LZO library is distributed in the hope that it will be useful,
41 but WITHOUT ANY WARRANTY; without even the implied warranty of
42 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 GNU General Public License for more details.
45 You should have received a copy of the GNU General Public License
46 along with the LZO library; see the file COPYING.
47 If not, write to the Free Software Foundation, Inc.,
48 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
50 Markus F.X.J. Oberhumer
51 <markus@oberhumer.com>
52 http://www.oberhumer.com/opensource/lzo/
55 #include "lzo/lzoconf.h"
56 #include "lzo/lzo1x.h"
59 lzo1x_999_compress_internal(const lzo_bytep in, lzo_uint in_len,
60 lzo_bytep out, lzo_uintp out_len,
62 const lzo_bytep dict, lzo_uint dict_len,
68 lzo_uint max_chain, lzo_uint32 flags);
71 lzo1y_999_compress_internal(const lzo_bytep in, lzo_uint in_len,
72 lzo_bytep out, lzo_uintp out_len,
74 const lzo_bytep dict, lzo_uint dict_len,
80 lzo_uint max_chain, lzo_uint32 flags);
96 # define noreturn void __attribute__((noreturn))
98 # define noreturn void
103 uint32_t pfx_compressed;
104 uint32_t pfx_cdatalen;
105 uint32_t pfx_checksum;
109 static inline uint32_t get_32(const uint32_t * p)
111 #if defined(__i386__) || defined(__x86_64__)
112 /* Littleendian and unaligned-capable */
115 const uint8_t *pp = (const uint8_t *)p;
116 return (uint32_t) pp[0] + ((uint32_t) pp[1] << 8) +
117 ((uint32_t) pp[2] << 16) + ((uint32_t) pp[3] << 24);
121 static inline void set_32(uint32_t * p, uint32_t v)
123 #if defined(__i386__) || defined(__x86_64__)
124 /* Littleendian and unaligned-capable */
127 uint8_t *pp = (uint8_t *) p;
129 pp[1] = ((v >> 8) & 0xff);
130 pp[2] = ((v >> 16) & 0xff);
131 pp[3] = ((v >> 24) & 0xff);
135 const char *progname = NULL;
136 const char *in_name = NULL;
137 const char *out_name = NULL;
139 static noreturn error(const char *fmt, ...)
144 fprintf(stderr, "%s: ", progname);
146 fprintf(stderr, "%s: ", in_name);
147 vfprintf(stderr, fmt, ap);
154 static void *xzalloc(size_t n)
156 void *p = calloc(n, 1);
158 error("out of memory");
162 /*************************************************************************
164 **************************************************************************/
166 int main(int argc, char *argv[])
170 const int max_try_lazy = 5;
171 const lzo_uint big = 65536L; /* can result in very slow compression */
172 const lzo_uint32 flags = 0x1;
176 lzo_uint in_len, infile_len, start, offset, soff;
179 lzo_uint out_bufsize;
180 lzo_uint out_len = 0;
181 lzo_uint outfile_len;
185 lzo_byte wrkmem[LZO1X_999_MEM_COMPRESS];
191 lzo_uint32 uncompressed_checksum;
192 lzo_uint32 compressed_checksum;
197 struct prefix *prefix;
201 fprintf(stderr, "Usage: %s file output-file\n", progname);
209 * Step 1: initialize the LZO library
211 if (lzo_init() != LZO_E_OK)
212 error("internal error - lzo_init() failed!");
215 * Step 3: open the input file
217 f = fopen(in_name, "rb");
219 error("cannot open file: %s", strerror(errno));
221 fseek(f, 0, SEEK_END);
223 fseek(f, 0, SEEK_SET);
225 error("empty file", progname, in_name);
229 infile_len = (lzo_uint) l;
230 out_bufsize = infile_len + infile_len / 16 + 64 + 3 + 2048;
233 * Step 4: allocate compression buffers and read the file
235 infile = xzalloc(infile_len);
236 out = xzalloc(out_bufsize);
237 infile_len = fread(infile, 1, infile_len, f);
241 * Select the portion which is for compression...
243 prefix = (struct prefix *)infile;
244 start = get_32(&prefix->pfx_start);
245 offset = get_32(&prefix->pfx_compressed);
246 in = infile + offset;
247 in_len = infile_len - offset;
251 * Step 5: compute a checksum of the uncompressed data
253 uncompressed_checksum = lzo_adler32(0, NULL, 0);
254 uncompressed_checksum = lzo_adler32(uncompressed_checksum, in, in_len);
257 * Step 6a: compress from `in' to `out' with LZO1X-999
259 for (lazy = 0; lazy <= max_try_lazy; lazy++) {
260 out_len = out_bufsize;
261 r = lzo1x_999_compress_internal(in, in_len, out, &out_len, wrkmem,
263 lazy, big, big, big, big, flags);
265 /* this should NEVER happen */
266 error("internal error - compression failed: %d", r);
268 if (out_len < best_len) {
275 * Step 7: check if compressible
277 if (best_len >= in_len) {
278 fprintf(stderr, "%s: %s: this file contains incompressible data.",
284 * Step 8: compress data again using the best compressor found
286 out_len = out_bufsize;
287 r = lzo1x_999_compress_internal(in, in_len, out, &out_len, wrkmem,
289 best_lazy, big, big, big, big, flags);
290 assert(r == LZO_E_OK);
291 assert(out_len == best_len);
294 * Step 9: optimize compressed data (compressed data is in `out' buffer)
297 /* Optimization does not require any data in the buffer that will
298 * hold the uncompressed data. To prove this, we clear the buffer.
300 memset(in, 0, in_len);
304 r = lzo1x_optimize(out, out_len, in, &orig_len, NULL);
305 if (r != LZO_E_OK || orig_len != in_len) {
306 /* this should NEVER happen */
307 error("internal error - optimization failed: %d", r);
311 * Step 10: compute a checksum of the compressed data
313 compressed_checksum = lzo_adler32(0, NULL, 0);
314 compressed_checksum = lzo_adler32(compressed_checksum, out, out_len);
317 * Step 11: write compressed data to a file
319 /* Make sure we have up to 2048 bytes of zero after the output */
320 memset(out + out_len, 0, 2048);
322 outfile_len = out_len;
324 soff = get_32(&prefix->pfx_cdatalen);
325 set_32((uint32_t *) (infile + soff), out_len);
327 soff = get_32(&prefix->pfx_checksum);
329 /* ISOLINUX padding and checksumming */
333 ((offset - start + out_len + 2047) & ~2047) - (offset - start);
334 for (ptr = 64; ptr < offset; ptr += 4)
335 csum += get_32((uint32_t *) (infile + ptr));
336 for (ptr = 0; ptr < outfile_len; ptr += 4)
337 csum += get_32((uint32_t *) (out + ptr));
339 set_32((uint32_t *) (infile + soff), offset - start + outfile_len);
340 set_32((uint32_t *) (infile + soff + 4), csum);
343 if (offset+outfile_len > get_32(&prefix->pfx_maxlma))
344 error("output too big (%lu, max %lu)",
345 (unsigned long)offset+outfile_len,
346 (unsigned long)get_32(&prefix->pfx_maxlma));
348 f = fopen(out_name, "wb");
350 error("cannot open output file %s: %s", out_name, strerror(errno));
352 if (fwrite(infile + start, 1, offset - start, f) != offset - start ||
353 fwrite(out, 1, outfile_len, f) != outfile_len || fclose(f))
354 error("write error");
357 * Step 12: verify decompression
360 orig_len = in_len * 2;
361 test = xzalloc(orig_len);
362 r = lzo1x_decompress_safe(out, out_len, test, &orig_len, NULL);
364 if (r != LZO_E_OK || orig_len != in_len) {
365 /* this should NEVER happen */
366 error("internal error - decompression failed: %d", r);
369 if (memcmp(test, in, in_len)) {
370 /* this should NEVER happen */
371 error("internal error - decompression data error");
374 /* Now you could also verify decompression under similar conditions as in
375 * your application, e.g. overlapping assembler decompression etc.