testmini.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* testmini.c -- very simple test program for the miniLZO library
  2. This file is part of the LZO real-time data compression library.
  3. Copyright (C) 1996-2017 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. #include <stdio.h>
  22. #include <stdlib.h>
  23. /*************************************************************************
  24. // This program shows the basic usage of the LZO library.
  25. // We will compress a block of data and decompress again.
  26. //
  27. // For more information, documentation, example programs and other support
  28. // files (like Makefiles and build scripts) please download the full LZO
  29. // package from
  30. // http://www.oberhumer.com/opensource/lzo/
  31. **************************************************************************/
  32. /* First let's include "minizo.h". */
  33. #include "minilzo.h"
  34. /* We want to compress the data block at 'in' with length 'IN_LEN' to
  35. * the block at 'out'. Because the input block may be incompressible,
  36. * we must provide a little more output space in case that compression
  37. * is not possible.
  38. */
  39. #define IN_LEN (128*1024ul)
  40. #define OUT_LEN (IN_LEN + IN_LEN / 16 + 64 + 3)
  41. static unsigned char __LZO_MMODEL in [ IN_LEN ];
  42. static unsigned char __LZO_MMODEL out [ OUT_LEN ];
  43. /* Work-memory needed for compression. Allocate memory in units
  44. * of 'lzo_align_t' (instead of 'char') to make sure it is properly aligned.
  45. */
  46. #define HEAP_ALLOC(var,size) \
  47. lzo_align_t __LZO_MMODEL var [ ((size) + (sizeof(lzo_align_t) - 1)) / sizeof(lzo_align_t) ]
  48. static HEAP_ALLOC(wrkmem, LZO1X_1_MEM_COMPRESS);
  49. /*************************************************************************
  50. //
  51. **************************************************************************/
  52. int main(int argc, char *argv[])
  53. {
  54. int r;
  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-2017 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 3;
  71. }
  72. /*
  73. * Step 2: prepare the input block that will get compressed.
  74. * We just fill it with zeros in this example program,
  75. * but you would use your real-world data here.
  76. */
  77. in_len = IN_LEN;
  78. lzo_memset(in,0,in_len);
  79. /*
  80. * Step 3: compress from 'in' to 'out' with LZO1X-1
  81. */
  82. r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
  83. if (r == LZO_E_OK)
  84. printf("compressed %lu bytes into %lu bytes\n",
  85. (unsigned long) in_len, (unsigned long) out_len);
  86. else
  87. {
  88. /* this should NEVER happen */
  89. printf("internal error - compression failed: %d\n", r);
  90. return 2;
  91. }
  92. /* check for an incompressible block */
  93. if (out_len >= in_len)
  94. {
  95. printf("This block contains incompressible data.\n");
  96. return 0;
  97. }
  98. /*
  99. * Step 4: decompress again, now going from 'out' to 'in'
  100. */
  101. new_len = in_len;
  102. r = lzo1x_decompress(out,out_len,in,&new_len,NULL);
  103. if (r == LZO_E_OK && new_len == in_len)
  104. printf("decompressed %lu bytes back into %lu bytes\n",
  105. (unsigned long) out_len, (unsigned long) in_len);
  106. else
  107. {
  108. /* this should NEVER happen */
  109. printf("internal error - decompression failed: %d\n", r);
  110. return 1;
  111. }
  112. printf("\nminiLZO simple compression test passed.\n");
  113. return 0;
  114. }
  115. /* vim:set ts=4 sw=4 et: */