1/* precomp.c -- example program: how to generate pre-compressed data 2 3 This file is part of the LZO real-time data compression library. 4 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 18 All Rights Reserved. 19 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. 24 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. 29 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. 34 35 Markus F.X.J. Oberhumer 36 <markus@oberhumer.com> 37 http://www.oberhumer.com/opensource/lzo/ 38 */ 39 40 41/************************************************************************* 42// This program shows how to generate pre-compressed data. 43// 44// Please study LZO.FAQ and simple.c first. 45// 46// We will be trying both LZO1X-999 and LZO1Y-999 and choose 47// the algorithm that achieves the best compression ratio. 48**************************************************************************/ 49 50#include "lzo/lzoconf.h" 51#include "lzo/lzo1x.h" 52#include "lzo/lzo1y.h" 53 54#define USE_LZO1X 1 55#define USE_LZO1Y 1 56 57#define PARANOID 1 58 59 60/* portability layer */ 61#define WANT_LZO_MALLOC 1 62#define WANT_LZO_FREAD 1 63#define WANT_LZO_WILDARGV 1 64#include "examples/portab.h" 65 66 67/************************************************************************* 68// 69**************************************************************************/ 70 71int __lzo_cdecl_main main(int argc, char *argv[]) 72{ 73 int r; 74 75 lzo_bytep in; 76 lzo_uint in_len; 77 78 lzo_bytep out; 79 lzo_uint out_bufsize; 80 lzo_uint out_len = 0; 81 82 lzo_bytep wrkmem; 83 lzo_uint wrk_len; 84 85 lzo_uint best_len; 86 int best_compress = -1; 87 88 lzo_uint orig_len; 89 lzo_uint32 uncompressed_checksum; 90 lzo_uint32 compressed_checksum; 91 92 FILE *f; 93 const char *progname = NULL; 94 const char *in_name = NULL; 95 const char *out_name = NULL; 96 long l; 97 98 99 lzo_wildargv(&argc, &argv); 100 101 printf("\nLZO real-time data compression library (v%s, %s).\n", 102 lzo_version_string(), lzo_version_date()); 103 printf("Copyright (C) 1996-2008 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n"); 104 105 progname = argv[0]; 106 if (argc < 2 || argc > 3) 107 { 108 printf("usage: %s file [output-file]\n", progname); 109 exit(1); 110 } 111 in_name = argv[1]; 112 if (argc > 2) out_name = argv[2]; 113 114/* 115 * Step 1: initialize the LZO library 116 */ 117 if (lzo_init() != LZO_E_OK) 118 { 119 printf("internal error - lzo_init() failed !!!\n"); 120 printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable `-DLZO_DEBUG' for diagnostics)\n"); 121 exit(1); 122 } 123 124/* 125 * Step 2: allocate the work-memory 126 */ 127 wrk_len = 1; 128#ifdef USE_LZO1X 129 if (wrk_len < LZO1X_999_MEM_COMPRESS) 130 wrk_len = LZO1X_999_MEM_COMPRESS; 131#endif 132#ifdef USE_LZO1Y 133 if (wrk_len < LZO1Y_999_MEM_COMPRESS) 134 wrk_len = LZO1Y_999_MEM_COMPRESS; 135#endif 136 wrkmem = (lzo_bytep) lzo_malloc(wrk_len); 137 if (wrkmem == NULL) 138 { 139 printf("%s: out of memory\n", progname); 140 exit(1); 141 } 142 143/* 144 * Step 3: open the input file 145 */ 146 f = fopen(in_name,"rb"); 147 if (f == NULL) 148 { 149 printf("%s: cannot open file %s\n", progname, in_name); 150 exit(1); 151 } 152 fseek(f,0,SEEK_END); 153 l = ftell(f); 154 fseek(f,0,SEEK_SET); 155 if (l <= 0) 156 { 157 printf("%s: %s: empty file\n", progname, in_name); 158 fclose(f); 159 exit(1); 160 } 161 in_len = (lzo_uint) l; 162 out_bufsize = in_len + in_len / 16 + 64 + 3; 163 best_len = in_len; 164 165/* 166 * Step 4: allocate compression buffers and read the file 167 */ 168 in = (lzo_bytep) lzo_malloc(in_len); 169 out = (lzo_bytep) lzo_malloc(out_bufsize); 170 if (in == NULL || out == NULL) 171 { 172 printf("%s: out of memory\n", progname); 173 exit(1); 174 } 175 in_len = (lzo_uint) lzo_fread(f,in,in_len); 176 printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len); 177 fclose(f); 178 179/* 180 * Step 5: compute a checksum of the uncompressed data 181 */ 182 uncompressed_checksum = lzo_adler32(0,NULL,0); 183 uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len); 184 185/* 186 * Step 6a: compress from `in' to `out' with LZO1X-999 187 */ 188#ifdef USE_LZO1X 189 out_len = out_bufsize; 190 r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem); 191 if (r != LZO_E_OK) 192 { 193 /* this should NEVER happen */ 194 printf("internal error - compression failed: %d\n", r); 195 exit(1); 196 } 197 printf("LZO1X-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len); 198 if (out_len < best_len) 199 { 200 best_len = out_len; 201 best_compress = 1; /* LZO1X-999 */ 202 } 203#endif /* USE_LZO1X */ 204 205/* 206 * Step 6b: compress from `in' to `out' with LZO1Y-999 207 */ 208#ifdef USE_LZO1Y 209 out_len = out_bufsize; 210 r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem); 211 if (r != LZO_E_OK) 212 { 213 /* this should NEVER happen */ 214 printf("internal error - compression failed: %d\n", r); 215 exit(1); 216 } 217 printf("LZO1Y-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len); 218 if (out_len < best_len) 219 { 220 best_len = out_len; 221 best_compress = 2; /* LZO1Y-999 */ 222 } 223#endif /* USE_LZO1Y */ 224 225/* 226 * Step 7: check if compressible 227 */ 228 if (best_len >= in_len) 229 { 230 printf("This file contains incompressible data.\n"); 231 return 0; 232 } 233 234/* 235 * Step 8: compress data again using the best compressor found 236 */ 237 out_len = out_bufsize; 238 if (best_compress == 1) 239 r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem); 240 else if (best_compress == 2) 241 r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem); 242 else 243 r = -100; 244 assert(r == LZO_E_OK); 245 assert(out_len == best_len); 246 247/* 248 * Step 9: optimize compressed data (compressed data is in `out' buffer) 249 */ 250#if 1 251 /* Optimization does not require any data in the buffer that will 252 * hold the uncompressed data. To prove this, we clear the buffer. 253 */ 254 lzo_memset(in,0,in_len); 255#endif 256 257 orig_len = in_len; 258 r = -100; 259#ifdef USE_LZO1X 260 if (best_compress == 1) 261 r = lzo1x_optimize(out,out_len,in,&orig_len,NULL); 262#endif 263#ifdef USE_LZO1Y 264 if (best_compress == 2) 265 r = lzo1y_optimize(out,out_len,in,&orig_len,NULL); 266#endif 267 if (r != LZO_E_OK || orig_len != in_len) 268 { 269 /* this should NEVER happen */ 270 printf("internal error - optimization failed: %d\n", r); 271 exit(1); 272 } 273 274/* 275 * Step 10: compute a checksum of the compressed data 276 */ 277 compressed_checksum = lzo_adler32(0,NULL,0); 278 compressed_checksum = lzo_adler32(compressed_checksum,out,out_len); 279 280/* 281 * Step 11: write compressed data to a file 282 */ 283 printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n", 284 progname, in_name, (long) in_len, (long) out_len, 285 (long) uncompressed_checksum, (long) compressed_checksum); 286 287 if (out_name && out_name[0]) 288 { 289 printf("%s: writing to file %s\n", progname, out_name); 290 f = fopen(out_name,"wb"); 291 if (f == NULL) 292 { 293 printf("%s: cannot open output file %s\n", progname, out_name); 294 exit(1); 295 } 296 if (lzo_fwrite(f,out,out_len) != out_len || fclose(f) != 0) 297 { 298 printf("%s: write error !!\n", progname); 299 exit(1); 300 } 301 } 302 303/* 304 * Step 12: verify decompression 305 */ 306#ifdef PARANOID 307 lzo_memset(in,0,in_len); /* paranoia - clear output buffer */ 308 orig_len = in_len; 309 r = -100; 310#ifdef USE_LZO1X 311 if (best_compress == 1) 312 r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL); 313#endif 314#ifdef USE_LZO1Y 315 if (best_compress == 2) 316 r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL); 317#endif 318 if (r != LZO_E_OK || orig_len != in_len) 319 { 320 /* this should NEVER happen */ 321 printf("internal error - decompression failed: %d\n", r); 322 exit(1); 323 } 324 if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len)) 325 { 326 /* this should NEVER happen */ 327 printf("internal error - decompression data error\n"); 328 exit(1); 329 } 330 /* Now you could also verify decompression under similar conditions as in 331 * your application, e.g. overlapping assembler decompression etc. 332 */ 333#endif 334 335 lzo_free(in); 336 lzo_free(out); 337 lzo_free(wrkmem); 338 339 return 0; 340} 341 342/* 343vi:ts=4:et 344*/ 345 346