1 /* overlap.c -- example program: overlapping (de)compression
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 how to do overlapping compression and
31 // in-place decompression.
32 //
33 // Please study LZO.FAQ and simple.c first.
34 **************************************************************************/
35 
36 #include <lzo/lzoconf.h>
37 #include <lzo/lzo1x.h>
38 
39 /* portability layer */
40 static const char *progname = NULL;
41 #define WANT_LZO_MALLOC 1
42 #define WANT_LZO_FREAD 1
43 #define WANT_LZO_WILDARGV 1
44 #define WANT_XMALLOC 1
45 #include "examples/portab.h"
46 
47 
48 /* Overhead (in bytes) for the in-place decompression buffer.
49  * Most files need only 16 !
50  * (try 'overlap -16 file' or even 'overlap -8 file')
51  *
52  * Worst case (for files that are compressible by only a few bytes)
53  * is 'in_len / 16 + 64 + 3'. See step 5a) below.
54  *
55  * For overlapping compression '0xbfff + in_len / 16 + 64 + 3' bytes
56  * will be needed. See step 4a) below.
57  */
58 
59 static long opt_overhead = 0;   /* assume worst case */
60 
61 static unsigned long total_files = 0;
62 static unsigned long total_in = 0;
63 
64 
65 /*************************************************************************
66 //
67 **************************************************************************/
68 
do_file(const char * in_name)69 static int do_file(const char *in_name)
70 {
71     int r;
72     FILE *fp = NULL;
73     long l;
74 
75     lzo_voidp wrkmem = NULL;
76 
77     lzo_bytep in = NULL;
78     lzo_uint in_len;                /* uncompressed length */
79 
80     lzo_bytep out = NULL;
81     lzo_uint out_len;               /* compressed length */
82 
83     lzo_bytep overlap = NULL;
84     lzo_uint overhead;
85     lzo_uint offset;
86 
87     lzo_uint new_len = 0;
88 
89 /*
90  * Step 1: open the input file
91  */
92     fp = fopen(in_name, "rb");
93     if (fp == NULL)
94     {
95         printf("%s: %s: cannot open file\n", progname, in_name);
96         goto next_file;
97     }
98     fseek(fp, 0, SEEK_END);
99     l = ftell(fp);
100     fseek(fp, 0, SEEK_SET);
101     if (l <= 0)
102     {
103         printf("%s: %s: empty file -- skipping\n", progname, in_name);
104         goto next_file;
105     }
106     in_len = (lzo_uint) l;
107     if ((long) in_len != l || l > 256L * 1024L * 1024L)
108     {
109         printf("%s: %s: file is too big -- skipping\n", progname, in_name);
110         goto next_file;
111     }
112 
113 /*
114  * Step 2: allocate compression buffers and read the file
115  */
116     in = (lzo_bytep) xmalloc(in_len);
117     out = (lzo_bytep) xmalloc(in_len + in_len / 16 + 64 + 3);
118     wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS);
119     in_len = (lzo_uint) lzo_fread(fp, in, in_len);
120     fclose(fp); fp = NULL;
121     printf("%s: %s: read %lu bytes\n", progname, in_name, (unsigned long) in_len);
122 
123     total_files++;
124     total_in += (unsigned long) in_len;
125 
126 /*
127  * Step 3: compress from 'in' to 'out' with LZO1X-1
128  */
129     r = lzo1x_1_compress(in, in_len, out, &out_len, wrkmem);
130     if (r != LZO_E_OK || out_len > in_len + in_len / 16 + 64 + 3)
131     {
132         /* this should NEVER happen */
133         printf("internal error - compression failed: %d\n", r);
134         exit(1);
135     }
136     printf("%-25s %8lu -> %8lu\n", "LZO1X-1:", (unsigned long) in_len, (unsigned long) out_len);
137 
138 
139 /***** Step 4: overlapping compression *****/
140 
141 /*
142  * Step 4a: allocate the 'overlap' buffer for overlapping compression
143  */
144     overhead  = in_len > 0xbfff ? 0xbfff : in_len;
145     overhead += in_len / 16 + 64 + 3;
146     overlap = (lzo_bytep) xmalloc(in_len + overhead);
147 
148 /*
149  * Step 4b: prepare data in 'overlap' buffer.
150  *          copy uncompressed data at the top of the overlap buffer
151  */
152     /*** offset = in_len + overhead - in_len; ***/
153     offset = overhead;
154     lzo_memcpy(overlap + offset, in, in_len);
155 
156 /*
157  * Step 4c: do an in-place compression within the 'overlap' buffer
158  */
159     r = lzo1x_1_compress(overlap + offset, in_len, overlap, &new_len, wrkmem);
160     if (r != LZO_E_OK)
161     {
162         /* this should NEVER happen */
163         printf("in-place compression failed: %d\n", r);
164         exit(1);
165     }
166 
167 /*
168  * Step 4d: verify overlapping compression
169  */
170     if (new_len != out_len || lzo_memcmp(out, overlap, out_len) != 0)
171     {
172         /* As compression is non-deterministic there can be a difference
173          * in the representation of the compressed data (but this usually
174          * happens very seldom). So we have to verify the overlapping
175          * compression by doing a temporary decompression.
176          */
177         lzo_uint ll = in_len;
178         lzo_bytep tmp = (lzo_bytep) xmalloc(ll);
179         r = lzo1x_decompress_safe(overlap, new_len, tmp, &ll, NULL);
180         if (r != LZO_E_OK || ll != in_len || lzo_memcmp(in, tmp, ll) != 0)
181         {
182             /* this should NEVER happen */
183             printf("in-place compression data error\n");
184             exit(1);
185         }
186         lzo_free(tmp);
187     }
188 
189     printf("  in-place compression:   %8lu -> %8lu    overhead: %7lu\n",
190             (unsigned long) in_len, (unsigned long) new_len, (unsigned long) overhead);
191     lzo_free(overlap); overlap = NULL;
192 
193 
194 /***** Step 5: in-place decompression *****/
195 
196 /*
197  * Step 5a: allocate the 'overlap' buffer for in-place decompression
198  */
199     if (opt_overhead == 0 || out_len >= in_len)
200         overhead = in_len / 16 + 64 + 3;
201     else
202         overhead = (lzo_uint) opt_overhead;
203     overlap = (lzo_bytep) xmalloc(in_len + overhead);
204 
205 /*
206  * Step 5b: prepare data in 'overlap' buffer.
207  *          copy compressed data at the top of the overlap buffer
208  */
209     offset = in_len + overhead - out_len;
210     lzo_memcpy(overlap + offset, out, out_len);
211 
212 /*
213  * Step 5c: do an in-place decompression within the 'overlap' buffer
214  */
215     new_len = in_len;
216     r = lzo1x_decompress_safe(overlap + offset, out_len, overlap, &new_len, NULL);
217     if (r != LZO_E_OK)
218     {
219         /* this may happen if overhead is too small */
220         printf("in-place decompression failed: %d - increase 'opt_overhead'\n", r);
221         exit(1);
222     }
223 
224 /*
225  * Step 5d: verify decompression
226  */
227     if (new_len != in_len || lzo_memcmp(in, overlap, in_len) != 0)
228     {
229         /* this may happen if overhead is too small */
230         printf("in-place decompression data error - increase 'opt_overhead'\n");
231         exit(1);
232     }
233     printf("  in-place decompression: %8lu -> %8lu    overhead: %7lu\n",
234             (unsigned long) out_len, (unsigned long) new_len, (unsigned long) overhead);
235     lzo_free(overlap); overlap = NULL;
236 
237 
238 next_file:
239     lzo_free(overlap);
240     lzo_free(wrkmem);
241     lzo_free(out);
242     lzo_free(in);
243     if (fp) fclose(fp);
244 
245     return 0;
246 }
247 
248 
249 /*************************************************************************
250 //
251 **************************************************************************/
252 
main(int argc,char * argv[])253 int __lzo_cdecl_main main(int argc, char *argv[])
254 {
255     int r;
256     int i = 1;
257 
258     lzo_wildargv(&argc, &argv);
259 
260     printf("\nLZO real-time data compression library (v%s, %s).\n",
261            lzo_version_string(), lzo_version_date());
262     printf("Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer\nAll Rights Reserved.\n\n");
263 
264     progname = argv[0];
265     if (i < argc && argv[i][0] == '-')
266         opt_overhead = atol(&argv[i++][1]);
267 #if 1
268     if (opt_overhead != 0 && opt_overhead < 4)
269     {
270         printf("%s: invalid overhead value %ld\n", progname, opt_overhead);
271         exit(1);
272     }
273 #endif
274     if (i >= argc)
275     {
276         printf("usage: %s [-overhead_in_bytes] file..\n", progname);
277         exit(1);
278     }
279 
280 /*
281  * Step 1: initialize the LZO library
282  */
283     if (lzo_init() != LZO_E_OK)
284     {
285         printf("internal error - lzo_init() failed !!!\n");
286         printf("(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)\n");
287         exit(1);
288     }
289 
290 /*
291  * Step 2: process files
292  */
293     for (r = 0; r == 0 && i < argc; i++)
294         r = do_file(argv[i]);
295 
296     printf("\nDone. Successfully processed %lu bytes in %lu files.\n",
297             total_in, total_files);
298     return r;
299 }
300 
301 
302 /* vim:set ts=4 sw=4 et: */
303