prepcore: style cleanups
[profile/ivi/syslinux.git] / lzo / prepcore.c
1 /* ----------------------------------------------------------------------- *
2  *   
3  *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
4  *
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.
10  *
11  * ----------------------------------------------------------------------- */
12
13 /* 
14    This file is based in part on:
15
16    precomp2.c -- example program: how to generate pre-compressed data
17
18    This file is part of the LZO real-time data compression library.
19
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
33    All Rights Reserved.
34
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.
39
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.
44
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.
49
50    Markus F.X.J. Oberhumer
51    <markus@oberhumer.com>
52    http://www.oberhumer.com/opensource/lzo/
53  */
54
55 #include "lzo/lzoconf.h"
56 #include "lzo/lzo1x.h"
57
58 LZO_EXTERN(int)
59 lzo1x_999_compress_internal(const lzo_bytep in, lzo_uint in_len,
60                             lzo_bytep out, lzo_uintp out_len,
61                             lzo_voidp wrkmem,
62                             const lzo_bytep dict, lzo_uint dict_len,
63                             lzo_callback_p cb,
64                             int try_lazy,
65                             lzo_uint good_length,
66                             lzo_uint max_lazy,
67                             lzo_uint nice_length,
68                             lzo_uint max_chain, lzo_uint32 flags);
69
70 LZO_EXTERN(int)
71 lzo1y_999_compress_internal(const lzo_bytep in, lzo_uint in_len,
72                             lzo_bytep out, lzo_uintp out_len,
73                             lzo_voidp wrkmem,
74                             const lzo_bytep dict, lzo_uint dict_len,
75                             lzo_callback_p cb,
76                             int try_lazy,
77                             lzo_uint good_length,
78                             lzo_uint max_lazy,
79                             lzo_uint nice_length,
80                             lzo_uint max_chain, lzo_uint32 flags);
81
82 #define PARANOID 1
83
84 #include <assert.h>
85 #include <ctype.h>
86 #include <errno.h>
87 #include <inttypes.h>
88 #include <stdarg.h>
89 #include <stddef.h>
90 #include <stdlib.h>
91 #include <stdio.h>
92 #include <string.h>
93 #include <time.h>
94
95 #ifdef __GNUC__
96 # define noreturn void __attribute__((noreturn))
97 #else
98 # define noreturn void
99 #endif
100
101 struct prefix {
102     uint32_t pfx_start;
103     uint32_t pfx_compressed;
104     uint32_t pfx_cdatalen;
105     uint32_t pfx_checksum;
106     uint32_t pfx_maxlma;
107 };
108
109 static inline uint32_t get_32(const uint32_t * p)
110 {
111 #if defined(__i386__) || defined(__x86_64__)
112     /* Littleendian and unaligned-capable */
113     return *p;
114 #else
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);
118 #endif
119 }
120
121 static inline void set_32(uint32_t * p, uint32_t v)
122 {
123 #if defined(__i386__) || defined(__x86_64__)
124     /* Littleendian and unaligned-capable */
125     *p = v;
126 #else
127     uint8_t *pp = (uint8_t *) p;
128     pp[0] = (v & 0xff);
129     pp[1] = ((v >> 8) & 0xff);
130     pp[2] = ((v >> 16) & 0xff);
131     pp[3] = ((v >> 24) & 0xff);
132 #endif
133 }
134
135 const char *progname = NULL;
136 const char *in_name  = NULL;
137 const char *out_name = NULL;
138
139 static noreturn error(const char *fmt, ...)
140 {
141     va_list ap;
142
143     va_start(ap, fmt);
144     fprintf(stderr, "%s: ", progname);
145     if (in_name)
146         fprintf(stderr, "%s: ", in_name);
147     vfprintf(stderr, fmt, ap);
148     va_end(ap);
149
150     exit(1);
151 }
152
153 static void *xzalloc(size_t n)
154 {
155     void *p = calloc(n, 1);
156     if (!p)
157         error("out of memory");
158     return p;
159 }
160
161 /*************************************************************************
162 //
163 **************************************************************************/
164
165 int main(int argc, char *argv[])
166 {
167     int r;
168     int lazy;
169     const int max_try_lazy = 5;
170     const lzo_uint big = 65536L;        /* can result in very slow compression */
171     const lzo_uint32 flags = 0x1;
172
173     lzo_bytep in;
174     lzo_bytep infile;
175     lzo_uint in_len, infile_len, start, offset, soff;
176
177     lzo_bytep out;
178     lzo_uint out_bufsize;
179     lzo_uint out_len = 0;
180     lzo_uint outfile_len;
181
182     lzo_bytep test;
183
184     lzo_byte wrkmem[LZO1X_999_MEM_COMPRESS];
185
186     lzo_uint best_len;
187     int best_lazy = -1;
188
189     lzo_uint orig_len;
190     lzo_uint32 uncompressed_checksum;
191     lzo_uint32 compressed_checksum;
192
193     FILE *f;
194     long l;
195
196     struct prefix *prefix;
197
198     progname = argv[0];
199     if (argc != 3) {
200         printf("Usage: %s file output-file\n", progname);
201         exit(1);
202     }
203     in_name = argv[1];
204     if (argc > 2)
205         out_name = argv[2];
206
207 /*
208  * Step 1: initialize the LZO library
209  */
210     if (lzo_init() != LZO_E_OK)
211         error("internal error - lzo_init() failed!\n");
212
213 /*
214  * Step 3: open the input file
215  */
216     f = fopen(in_name, "rb");
217     if (!f)
218         error("cannot open file: %s\n", strerror(errno));
219
220     fseek(f, 0, SEEK_END);
221     l = ftell(f);
222     fseek(f, 0, SEEK_SET);
223     if (l <= 0) {
224         error("empty file\n", progname, in_name);
225         fclose(f);
226         exit(1);
227     }
228     infile_len = (lzo_uint) l;
229     out_bufsize = infile_len + infile_len / 16 + 64 + 3 + 2048;
230
231 /*
232  * Step 4: allocate compression buffers and read the file
233  */
234     infile = xzalloc(infile_len);
235     out = xzalloc(out_bufsize);
236     infile_len = fread(infile, 1, infile_len, f);
237     fclose(f);
238
239 /*
240  * Select the portion which is for compression...
241  */
242     prefix = (struct prefix *)infile;
243     start = get_32(&prefix->pfx_start);
244     offset = get_32(&prefix->pfx_compressed);
245     in = infile + offset;
246     in_len = infile_len - offset;
247     best_len = in_len;
248
249 /*
250  * Step 5: compute a checksum of the uncompressed data
251  */
252     uncompressed_checksum = lzo_adler32(0, NULL, 0);
253     uncompressed_checksum = lzo_adler32(uncompressed_checksum, in, in_len);
254
255 /*
256  * Step 6a: compress from `in' to `out' with LZO1X-999
257  */
258     for (lazy = 0; lazy <= max_try_lazy; lazy++) {
259         out_len = out_bufsize;
260         r = lzo1x_999_compress_internal(in, in_len, out, &out_len, wrkmem,
261                                         NULL, 0, 0,
262                                         lazy, big, big, big, big, flags);
263         if (r != LZO_E_OK)
264             /* this should NEVER happen */
265             error("internal error - compression failed: %d\n", r);
266
267         if (out_len < best_len) {
268             best_len = out_len;
269             best_lazy = lazy;
270         }
271     }
272
273 /*
274  * Step 7: check if compressible
275  */
276     if (best_len >= in_len) {
277         fprintf(stderr, "%s: %s: this file contains incompressible data.\n",
278                 progname, in_name);
279         /* do it anyway */
280     }
281
282 /*
283  * Step 8: compress data again using the best compressor found
284  */
285     out_len = out_bufsize;
286     r = lzo1x_999_compress_internal(in, in_len, out, &out_len, wrkmem,
287                                     NULL, 0, 0,
288                                     best_lazy, big, big, big, big, flags);
289     assert(r == LZO_E_OK);
290     assert(out_len == best_len);
291
292 /*
293  * Step 9: optimize compressed data (compressed data is in `out' buffer)
294  */
295 #if 1
296     /* Optimization does not require any data in the buffer that will
297      * hold the uncompressed data. To prove this, we clear the buffer.
298      */
299     memset(in, 0, in_len);
300 #endif
301
302     orig_len = in_len;
303     r = lzo1x_optimize(out, out_len, in, &orig_len, NULL);
304     if (r != LZO_E_OK || orig_len != in_len) {
305         /* this should NEVER happen */
306         error("internal error - optimization failed: %d\n", r);
307     }
308
309 /*
310  * Step 10: compute a checksum of the compressed data
311  */
312     compressed_checksum = lzo_adler32(0, NULL, 0);
313     compressed_checksum = lzo_adler32(compressed_checksum, out, out_len);
314
315 /*
316  * Step 11: write compressed data to a file
317  */
318     /* Make sure we have up to 2048 bytes of zero after the output */
319     memset(out + out_len, 0, 2048);
320
321     outfile_len = out_len;
322
323     soff = get_32(&prefix->pfx_cdatalen);
324     set_32((uint32_t *) (infile + soff), out_len);
325
326     soff = get_32(&prefix->pfx_checksum);
327     if (soff) {
328         /* ISOLINUX padding and checksumming */
329         uint32_t csum = 0;
330         unsigned int ptr;
331         outfile_len =
332             ((offset - start + out_len + 2047) & ~2047) - (offset - start);
333         for (ptr = 64; ptr < offset; ptr += 4)
334             csum += get_32((uint32_t *) (infile + ptr));
335         for (ptr = 0; ptr < outfile_len; ptr += 4)
336             csum += get_32((uint32_t *) (out + ptr));
337
338         set_32((uint32_t *) (infile + soff), offset - start + outfile_len);
339         set_32((uint32_t *) (infile + soff + 4), csum);
340     }
341
342     if (offset+outfile_len > get_32(&prefix->pfx_maxlma))
343         error("output too big (%lu, max %lu)\n",
344               (unsigned long)offset+outfile_len,
345               (unsigned long)get_32(&prefix->pfx_maxlma));
346
347     f = fopen(out_name, "wb");
348     if (!f)
349         error("cannot open output file %s: %s\n", out_name, strerror(errno));
350
351     if (fwrite(infile + start, 1, offset - start, f) != offset - start ||
352         fwrite(out, 1, outfile_len, f) != outfile_len || fclose(f))
353         error("write error\n");
354
355 /*
356  * Step 12: verify decompression
357  */
358 #ifdef PARANOID
359     orig_len = in_len * 2;
360     test = xzalloc(orig_len);
361     r = lzo1x_decompress_safe(out, out_len, test, &orig_len, NULL);
362
363     if (r != LZO_E_OK || orig_len != in_len) {
364         /* this should NEVER happen */
365         error("internal error - decompression failed: %d\n", r);
366     }
367
368     if (memcmp(test, in, in_len)) {
369         /* this should NEVER happen */
370         error("internal error - decompression data error\n");
371     }
372
373     /* Now you could also verify decompression under similar conditions as in
374      * your application, e.g. overlapping assembler decompression etc.
375      */
376
377     free(test);
378 #endif
379
380     free(infile);
381     free(out);
382
383     return 0;
384 }