precomp2.c 11 KB

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