home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mlzo100.zip / minilzo.100 / testmini.c < prev   
C/C++ Source or Header  |  1997-07-09  |  4KB  |  141 lines

  1. /* testmini.c -- very simple test program for the miniLZO library
  2.  
  3.    This file is part of the LZO real-time data compression library.
  4.  
  5.    Copyright (C) 1997 Markus Franz Xaver Johannes Oberhumer
  6.    Copyright (C) 1996 Markus Franz Xaver Johannes Oberhumer
  7.  
  8.    The LZO library is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of
  11.    the License, or (at your option) any later version.
  12.  
  13.    The LZO library is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with the LZO library; see the file COPYING.
  20.    If not, write to the Free Software Foundation, Inc.,
  21.    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23.    Markus F.X.J. Oberhumer
  24.    markus.oberhumer@jk.uni-linz.ac.at
  25.  */
  26.  
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30.  
  31.  
  32. /*************************************************************************
  33. // This program shows the basic usage of the LZO library.
  34. // We will compress a block of data and decompress again.
  35. //
  36. // Please see also LZO.FAQ
  37. **************************************************************************/
  38.  
  39. /* First let's include "minizo.h". */
  40.  
  41. #include "minilzo.h"
  42.  
  43.  
  44. /* We want to compress the data block at `in' with length `IN_LEN' to
  45.  * the block at `out'. Because the input block may be incompressible,
  46.  * we must provide a little more output space in case that compression
  47.  * is not possible.
  48.  */
  49.  
  50. #define IN_LEN        (128*1024L)
  51. #define OUT_LEN        (IN_LEN + IN_LEN / 64 + 16 + 3)
  52.  
  53. static lzo_byte in  [ IN_LEN ];
  54. static lzo_byte out [ OUT_LEN ];
  55.  
  56.  
  57. /* Work-memory allocation.
  58.  * Because we will align our work memory on a multiple of 16 (for reasons
  59.  * of speed) we allocate 16 extra bytes.
  60.  */
  61.  
  62. static lzo_byte _wrkmem [ LZO1X_MEM_COMPRESS + 16 ];
  63.  
  64.  
  65. /*************************************************************************
  66. //
  67. **************************************************************************/
  68.  
  69. int main(int argc, char *argv[])
  70. {
  71.     int r;
  72.     lzo_uint in_len;
  73.     lzo_uint out_len;
  74.     lzo_byte *wrkmem;
  75.  
  76.     if (argc < 0 || argv == NULL)    /* avoid warning about unused args */
  77.         return 0;
  78.  
  79. /*
  80.  * Step 1: prepare the input block that will get compressed.
  81.  *         We just fill it with zeros in this example program,
  82.  *         but you would use your real-world data here.
  83.  */
  84.     in_len = IN_LEN;
  85.     lzo_memset(in,0,in_len);
  86.  
  87. /*
  88.  * Step 2: align the work-memory
  89.  */
  90.     wrkmem = LZO_ALIGN(_wrkmem,16);
  91.  
  92. /*
  93.  * Step 3: initialize the LZO library
  94.  */
  95.     if (lzo_init() != LZO_E_OK)
  96.     {
  97.         printf("lzo_init() failed !!!\n");
  98.         return 3;
  99.     }
  100.  
  101. /*
  102.  * Step 4: compress from `in' to `out' with LZO1X-1
  103.  */
  104.     r = lzo1x_1_compress(in,in_len,out,&out_len,wrkmem);
  105.     if (r == LZO_E_OK)
  106.         printf("compressed %lu bytes into %lu bytes\n",
  107.             (long) in_len, (long) out_len);
  108.     else
  109.     {
  110.         printf("compression failed: %d\n", r);
  111.         return 2;
  112.     }
  113.     /* check for an incompressible block */
  114.     if (out_len >= in_len)
  115.     {
  116.         printf("This block contains incompressible data.\n");
  117.         return 0;
  118.     }
  119.  
  120. /*
  121.  * Step 5: decompress again, now going from `out' to `in'
  122.  */
  123.     r = lzo1x_decompress(out,out_len,in,&in_len,NULL);
  124.     if (r == LZO_E_OK)
  125.         printf("decompressed %lu bytes back into %lu bytes\n",
  126.             (long) out_len, (long) in_len);
  127.     else
  128.     {
  129.         printf("decompression failed: %d\n", r);
  130.         return 1;
  131.     }
  132.  
  133.     printf("miniLZO simple compression test passed.\n");
  134.     return 0;
  135. }
  136.  
  137. /*
  138. vi:ts=4
  139. */
  140.  
  141.