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