precomp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* precomp.c -- example program: how to generate pre-compressed data
  2. This file is part of the LZO real-time data compression library.
  3. Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer
  4. All Rights Reserved.
  5. The LZO library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. The LZO library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with the LZO library; see the file COPYING.
  15. If not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. Markus F.X.J. Oberhumer
  18. <markus@oberhumer.com>
  19. http://www.oberhumer.com/opensource/lzo/
  20. */
  21. /*************************************************************************
  22. // This program shows how to generate pre-compressed data.
  23. //
  24. // Please study LZO.FAQ and simple.c first.
  25. //
  26. // We will be trying both LZO1X-999 and LZO1Y-999 and choose
  27. // the algorithm that achieves the best compression ratio.
  28. **************************************************************************/
  29. #include <lzo/lzoconf.h>
  30. #include <lzo/lzo1x.h>
  31. #include <lzo/lzo1y.h>
  32. #define USE_LZO1X 1
  33. #define USE_LZO1Y 1
  34. #define PARANOID 1
  35. /* portability layer */
  36. static const char *progname = NULL;
  37. #define WANT_LZO_MALLOC 1
  38. #define WANT_LZO_FREAD 1
  39. #define WANT_LZO_WILDARGV 1
  40. #define WANT_XMALLOC 1
  41. #include "examples/portab.h"
  42. /*************************************************************************
  43. //
  44. **************************************************************************/
  45. int __lzo_cdecl_main main(int argc, char *argv[])
  46. {
  47. int r;
  48. lzo_bytep in;
  49. lzo_uint in_len;
  50. lzo_bytep out;
  51. lzo_uint out_bufsize;
  52. lzo_uint out_len = 0;
  53. lzo_voidp wrkmem;
  54. lzo_uint wrkmem_size;
  55. lzo_uint best_len;
  56. int best_compress = -1;
  57. lzo_uint orig_len;
  58. lzo_uint32_t uncompressed_checksum;
  59. lzo_uint32_t compressed_checksum;
  60. FILE *fp;
  61. const char *in_name = NULL;
  62. const char *out_name = NULL;
  63. long l;
  64. lzo_wildargv(&argc, &argv);
  65. printf("\nLZO real-time data compression library (v%s, %s).\n",
  66. lzo_version_string(), lzo_version_date());
  67. printf("Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
  68. progname = argv[0];
  69. if (argc < 2 || argc > 3)
  70. {
  71. printf("usage: %s file [output-file]\n", progname);
  72. exit(1);
  73. }
  74. in_name = argv[1];
  75. if (argc > 2) out_name = argv[2];
  76. /*
  77. * Step 1: initialize the LZO library
  78. */
  79. if (lzo_init() != LZO_E_OK)
  80. {
  81. printf("internal error - lzo_init() failed !!!\n");
  82. printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
  83. exit(1);
  84. }
  85. /*
  86. * Step 2: allocate the work-memory
  87. */
  88. wrkmem_size = 1;
  89. #ifdef USE_LZO1X
  90. wrkmem_size = (LZO1X_999_MEM_COMPRESS > wrkmem_size) ? LZO1X_999_MEM_COMPRESS : wrkmem_size;
  91. #endif
  92. #ifdef USE_LZO1Y
  93. wrkmem_size = (LZO1Y_999_MEM_COMPRESS > wrkmem_size) ? LZO1Y_999_MEM_COMPRESS : wrkmem_size;
  94. #endif
  95. wrkmem = (lzo_voidp) xmalloc(wrkmem_size);
  96. if (wrkmem == NULL)
  97. {
  98. printf("%s: out of memory\n", progname);
  99. exit(1);
  100. }
  101. /*
  102. * Step 3: open the input file
  103. */
  104. fp = fopen(in_name,"rb");
  105. if (fp == NULL)
  106. {
  107. printf("%s: cannot open file %s\n", progname, in_name);
  108. exit(1);
  109. }
  110. fseek(fp, 0, SEEK_END);
  111. l = ftell(fp);
  112. fseek(fp, 0, SEEK_SET);
  113. if (l <= 0)
  114. {
  115. printf("%s: %s: empty file\n", progname, in_name);
  116. fclose(fp); fp = NULL;
  117. exit(1);
  118. }
  119. in_len = (lzo_uint) l;
  120. out_bufsize = in_len + in_len / 16 + 64 + 3;
  121. best_len = in_len;
  122. /*
  123. * Step 4: allocate compression buffers and read the file
  124. */
  125. in = (lzo_bytep) xmalloc(in_len);
  126. out = (lzo_bytep) xmalloc(out_bufsize);
  127. if (in == NULL || out == NULL)
  128. {
  129. printf("%s: out of memory\n", progname);
  130. exit(1);
  131. }
  132. in_len = (lzo_uint) lzo_fread(fp, in, in_len);
  133. printf("%s: loaded file %s: %ld bytes\n", progname, in_name, (long) in_len);
  134. fclose(fp); fp = NULL;
  135. /*
  136. * Step 5: compute a checksum of the uncompressed data
  137. */
  138. uncompressed_checksum = lzo_adler32(0,NULL,0);
  139. uncompressed_checksum = lzo_adler32(uncompressed_checksum,in,in_len);
  140. /*
  141. * Step 6a: compress from 'in' to 'out' with LZO1X-999
  142. */
  143. #ifdef USE_LZO1X
  144. out_len = out_bufsize;
  145. r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
  146. if (r != LZO_E_OK)
  147. {
  148. /* this should NEVER happen */
  149. printf("internal error - compression failed: %d\n", r);
  150. exit(1);
  151. }
  152. printf("LZO1X-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
  153. if (out_len < best_len)
  154. {
  155. best_len = out_len;
  156. best_compress = 1; /* LZO1X-999 */
  157. }
  158. #endif /* USE_LZO1X */
  159. /*
  160. * Step 6b: compress from 'in' to 'out' with LZO1Y-999
  161. */
  162. #ifdef USE_LZO1Y
  163. out_len = out_bufsize;
  164. r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
  165. if (r != LZO_E_OK)
  166. {
  167. /* this should NEVER happen */
  168. printf("internal error - compression failed: %d\n", r);
  169. exit(1);
  170. }
  171. printf("LZO1Y-999: %8lu -> %8lu\n", (unsigned long) in_len, (unsigned long) out_len);
  172. if (out_len < best_len)
  173. {
  174. best_len = out_len;
  175. best_compress = 2; /* LZO1Y-999 */
  176. }
  177. #endif /* USE_LZO1Y */
  178. /*
  179. * Step 7: check if compressible
  180. */
  181. if (best_len >= in_len)
  182. {
  183. printf("This file contains incompressible data.\n");
  184. return 0;
  185. }
  186. /*
  187. * Step 8: compress data again using the best compressor found
  188. */
  189. out_len = out_bufsize;
  190. if (best_compress == 1)
  191. r = lzo1x_999_compress(in,in_len,out,&out_len,wrkmem);
  192. else if (best_compress == 2)
  193. r = lzo1y_999_compress(in,in_len,out,&out_len,wrkmem);
  194. else
  195. r = -100;
  196. assert(r == LZO_E_OK);
  197. assert(out_len == best_len);
  198. /*
  199. * Step 9: optimize compressed data (compressed data is in 'out' buffer)
  200. */
  201. #if 1
  202. /* Optimization does not require any data in the buffer that will
  203. * hold the uncompressed data. To prove this, we clear the buffer.
  204. */
  205. lzo_memset(in,0,in_len);
  206. #endif
  207. orig_len = in_len;
  208. r = -100;
  209. #ifdef USE_LZO1X
  210. if (best_compress == 1)
  211. r = lzo1x_optimize(out,out_len,in,&orig_len,NULL);
  212. #endif
  213. #ifdef USE_LZO1Y
  214. if (best_compress == 2)
  215. r = lzo1y_optimize(out,out_len,in,&orig_len,NULL);
  216. #endif
  217. if (r != LZO_E_OK || orig_len != in_len)
  218. {
  219. /* this should NEVER happen */
  220. printf("internal error - optimization failed: %d\n", r);
  221. exit(1);
  222. }
  223. /*
  224. * Step 10: compute a checksum of the compressed data
  225. */
  226. compressed_checksum = lzo_adler32(0,NULL,0);
  227. compressed_checksum = lzo_adler32(compressed_checksum,out,out_len);
  228. /*
  229. * Step 11: write compressed data to a file
  230. */
  231. printf("%s: %s: %ld -> %ld, checksum 0x%08lx 0x%08lx\n",
  232. progname, in_name, (long) in_len, (long) out_len,
  233. (long) uncompressed_checksum, (long) compressed_checksum);
  234. if (out_name && out_name[0])
  235. {
  236. printf("%s: writing to file %s\n", progname, out_name);
  237. fp = fopen(out_name,"wb");
  238. if (fp == NULL)
  239. {
  240. printf("%s: cannot open output file %s\n", progname, out_name);
  241. exit(1);
  242. }
  243. if (lzo_fwrite(fp, out, out_len) != out_len || fclose(fp) != 0)
  244. {
  245. printf("%s: write error !!\n", progname);
  246. exit(1);
  247. }
  248. }
  249. /*
  250. * Step 12: verify decompression
  251. */
  252. #ifdef PARANOID
  253. lzo_memset(in,0,in_len); /* paranoia - clear output buffer */
  254. orig_len = in_len;
  255. r = -100;
  256. #ifdef USE_LZO1X
  257. if (best_compress == 1)
  258. r = lzo1x_decompress_safe(out,out_len,in,&orig_len,NULL);
  259. #endif
  260. #ifdef USE_LZO1Y
  261. if (best_compress == 2)
  262. r = lzo1y_decompress_safe(out,out_len,in,&orig_len,NULL);
  263. #endif
  264. if (r != LZO_E_OK || orig_len != in_len)
  265. {
  266. /* this should NEVER happen */
  267. printf("internal error - decompression failed: %d\n", r);
  268. exit(1);
  269. }
  270. if (uncompressed_checksum != lzo_adler32(lzo_adler32(0,NULL,0),in,in_len))
  271. {
  272. /* this should NEVER happen */
  273. printf("internal error - decompression data error\n");
  274. exit(1);
  275. }
  276. /* Now you could also verify decompression under similar conditions as in
  277. * your application, e.g. overlapping assembler decompression etc.
  278. */
  279. #endif
  280. lzo_free(in);
  281. lzo_free(out);
  282. lzo_free(wrkmem);
  283. return 0;
  284. }
  285. /* vim:set ts=4 sw=4 et: */