simple.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* simple.c -- the annotated simple example program for the LZO library
  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 the basic usage of the LZO library.
  23. // We will compress a block of data and decompress again.
  24. //
  25. // See also LZO.FAQ
  26. **************************************************************************/
  27. /* We will be using the LZO1X-1 algorithm, so we have
  28. * to include <lzo/lzo1x.h>
  29. */
  30. #include <lzo/lzoconf.h>
  31. #include <lzo/lzo1x.h>
  32. /* portability layer */
  33. static const char *progname = NULL;
  34. #define WANT_LZO_MALLOC 1
  35. #define WANT_XMALLOC 1
  36. #include "examples/portab.h"
  37. /* We want to compress the data block at 'in' with length 'IN_LEN' to
  38. * the block at 'out'. Because the input block may be incompressible,
  39. * we must provide a little more output space in case that compression
  40. * is not possible.
  41. */
  42. #ifndef IN_LEN
  43. #define IN_LEN (128*1024L)
  44. #endif
  45. #define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
  46. /*************************************************************************
  47. //
  48. **************************************************************************/
  49. int __lzo_cdecl_main main(int argc, char *argv[])
  50. {
  51. int r;
  52. lzo_bytep in;
  53. lzo_bytep out;
  54. lzo_voidp wrkmem;
  55. lzo_uint in_len;
  56. lzo_uint out_len;
  57. lzo_uint new_len;
  58. if (argc < 0 && argv == NULL) /* avoid warning about unused args */
  59. return 0;
  60. printf("\nLZO real-time data compression library (v%s, %s).\n",
  61. lzo_version_string(), lzo_version_date());
  62. printf("Copyright (C) 1996-2015 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
  63. /*
  64. * Step 1: initialize the LZO library
  65. */
  66. if (lzo_init() != LZO_E_OK)
  67. {
  68. printf("internal error - lzo_init() failed !!!\n");
  69. printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
  70. return 4;
  71. }
  72. /*
  73. * Step 2: allocate blocks and the work-memory
  74. */
  75. in = (lzo_bytep) xmalloc(IN_LEN);
  76. out = (lzo_bytep) xmalloc(OUT_LEN);
  77. wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
  78. if (in == NULL || out == NULL || wrkmem == NULL)
  79. {
  80. printf("out of memory\n");
  81. return 3;
  82. }
  83. /*
  84. * Step 3: prepare the input block that will get compressed.
  85. * We just fill it with zeros in this example program,
  86. * but you would use your real-world data here.
  87. */
  88. in_len = IN_LEN;
  89. lzo_memset(in,0,in_len);
  90. /*
  91. * Step 4: compress from 'in' to 'out' with LZO1X-1
  92. */
  93. r = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
  94. if (r == LZO_E_OK)
  95. printf("compressed %lu bytes into %lu bytes\n",
  96. (unsigned long) in_len, (unsigned long) out_len);
  97. else
  98. {
  99. /* this should NEVER happen */
  100. printf("internal error - compression failed: %d\n", r);
  101. return 2;
  102. }
  103. /* check for an incompressible block */
  104. if (out_len >= in_len)
  105. {
  106. printf("This block contains incompressible data.\n");
  107. return 0;
  108. }
  109. /*
  110. * Step 5: decompress again, now going from 'out' to 'in'
  111. */
  112. new_len = in_len;
  113. r = lzo1x_decompress(out, out_len, in, &new_len, NULL);
  114. if (r == LZO_E_OK && new_len == in_len)
  115. printf("decompressed %lu bytes back into %lu bytes\n",
  116. (unsigned long) out_len, (unsigned long) in_len);
  117. else
  118. {
  119. /* this should NEVER happen */
  120. printf("internal error - decompression failed: %d\n", r);
  121. return 1;
  122. }
  123. lzo_free(wrkmem);
  124. lzo_free(out);
  125. lzo_free(in);
  126. printf("Simple compression test passed.\n");
  127. return 0;
  128. }
  129. /* vim:set ts=4 sw=4 et: */