-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbexample.c
367 lines (323 loc) · 12.2 KB
/
bbexample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* Buffer to buffer example - Test program for the library lzlib
Copyright (C) 2010-2024 Antonio Diaz Diaz.
This program is free software: you have unlimited permission
to copy, distribute, and modify it.
Usage: bbexample filename
This program is an example of how buffer-to-buffer
compression/decompression can be implemented using lzlib.
*/
#define _FILE_OFFSET_BITS 64
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lzlib.h"
#ifndef min
#define min(x,y) ((x) <= (y) ? (x) : (y))
#endif
/* Return the address of a malloc'd buffer containing the file data and
the file size in '*file_sizep'.
In case of error, return 0 and do not modify '*file_sizep'.
*/
uint8_t * read_file( const char * const name, long * const file_sizep )
{
long buffer_size = 1 << 20, file_size;
uint8_t * buffer, * tmp;
FILE * const f = fopen( name, "rb" );
if( !f )
{ fprintf( stderr, "bbexample: %s: Can't open input file: %s\n",
name, strerror( errno ) ); return 0; }
buffer = (uint8_t *)malloc( buffer_size );
if( !buffer )
{ fputs( "bbexample: read_file: Not enough memory.\n", stderr );
fclose( f ); return 0; }
file_size = fread( buffer, 1, buffer_size, f );
while( file_size >= buffer_size )
{
if( buffer_size >= LONG_MAX )
{
fprintf( stderr, "bbexample: %s: Input file is too large.\n", name );
free( buffer ); fclose( f ); return 0;
}
buffer_size = ( buffer_size <= LONG_MAX / 2 ) ? 2 * buffer_size : LONG_MAX;
tmp = (uint8_t *)realloc( buffer, buffer_size );
if( !tmp )
{ fputs( "bbexample: read_file: Not enough memory.\n", stderr );
free( buffer ); fclose( f ); return 0; }
buffer = tmp;
file_size += fread( buffer + file_size, 1, buffer_size - file_size, f );
}
if( ferror( f ) || !feof( f ) )
{
fprintf( stderr, "bbexample: %s: Error reading file: %s\n",
name, strerror( errno ) );
free( buffer ); fclose( f ); return 0;
}
fclose( f );
*file_sizep = file_size;
return buffer;
}
/* Compress 'insize' bytes from 'inbuf'.
Return the address of a malloc'd buffer containing the compressed data,
and the size of the data in '*outlenp'.
In case of error, return 0 and do not modify '*outlenp'.
*/
uint8_t * bbcompressl( const uint8_t * const inbuf, const long insize,
const int level, long * const outlenp )
{
struct Lzma_options
{
int dictionary_size; /* 4 KiB .. 512 MiB */
int match_len_limit; /* 5 .. 273 */
};
/* Mapping from gzip/bzip2 style 0..9 compression levels to the
corresponding LZMA compression parameters. */
const struct Lzma_options option_mapping[] =
{
{ 65535, 16 }, /* -0 (65535,16 chooses fast encoder) */
{ 1 << 20, 5 }, /* -1 */
{ 3 << 19, 6 }, /* -2 */
{ 1 << 21, 8 }, /* -3 */
{ 3 << 20, 12 }, /* -4 */
{ 1 << 22, 20 }, /* -5 */
{ 1 << 23, 36 }, /* -6 */
{ 1 << 24, 68 }, /* -7 */
{ 3 << 23, 132 }, /* -8 */
{ 1 << 25, 273 } }; /* -9 */
struct Lzma_options encoder_options;
struct LZ_Encoder * encoder;
uint8_t * outbuf;
const long delta_size = ( insize / 4 ) + 64; /* insize may be zero */
long outsize = delta_size; /* initial outsize */
long inpos = 0;
long outpos = 0;
bool error = false;
if( level < 0 || level > 9 ) return 0;
encoder_options = option_mapping[level];
if( encoder_options.dictionary_size > insize && level != 0 )
encoder_options.dictionary_size = insize; /* saves memory */
if( encoder_options.dictionary_size < LZ_min_dictionary_size() )
encoder_options.dictionary_size = LZ_min_dictionary_size();
encoder = LZ_compress_open( encoder_options.dictionary_size,
encoder_options.match_len_limit, INT64_MAX );
outbuf = (uint8_t *)malloc( outsize );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok || !outbuf )
{ free( outbuf ); LZ_compress_close( encoder ); return 0; }
while( true )
{
int ret = LZ_compress_write( encoder, inbuf + inpos,
min( INT_MAX, insize - inpos ) );
if( ret < 0 ) { error = true; break; }
inpos += ret;
if( inpos >= insize ) LZ_compress_finish( encoder );
ret = LZ_compress_read( encoder, outbuf + outpos,
min( INT_MAX, outsize - outpos ) );
if( ret < 0 ) { error = true; break; }
outpos += ret;
if( LZ_compress_finished( encoder ) == 1 ) break;
if( outpos >= outsize )
{
uint8_t * tmp;
if( outsize > LONG_MAX - delta_size ) { error = true; break; }
outsize += delta_size;
tmp = (uint8_t *)realloc( outbuf, outsize );
if( !tmp ) { error = true; break; }
outbuf = tmp;
}
}
if( LZ_compress_close( encoder ) < 0 ) error = true;
if( error ) { free( outbuf ); return 0; }
*outlenp = outpos;
return outbuf;
}
/* Decompress 'insize' bytes from 'inbuf'.
Return the address of a malloc'd buffer containing the decompressed
data, and the size of the data in '*outlenp'.
In case of error, return 0 and do not modify '*outlenp'.
*/
uint8_t * bbdecompressl( const uint8_t * const inbuf, const long insize,
long * const outlenp )
{
struct LZ_Decoder * const decoder = LZ_decompress_open();
const long delta_size = insize; /* insize must be > zero */
long outsize = delta_size; /* initial outsize */
uint8_t * outbuf = (uint8_t *)malloc( outsize );
long inpos = 0;
long outpos = 0;
bool error = false;
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok || !outbuf )
{ free( outbuf ); LZ_decompress_close( decoder ); return 0; }
while( true )
{
int ret = LZ_decompress_write( decoder, inbuf + inpos,
min( INT_MAX, insize - inpos ) );
if( ret < 0 ) { error = true; break; }
inpos += ret;
if( inpos >= insize ) LZ_decompress_finish( decoder );
ret = LZ_decompress_read( decoder, outbuf + outpos,
min( INT_MAX, outsize - outpos ) );
if( ret < 0 ) { error = true; break; }
outpos += ret;
if( LZ_decompress_finished( decoder ) == 1 ) break;
if( outpos >= outsize )
{
uint8_t * tmp;
if( outsize > LONG_MAX - delta_size ) { error = true; break; }
outsize += delta_size;
tmp = (uint8_t *)realloc( outbuf, outsize );
if( !tmp ) { error = true; break; }
outbuf = tmp;
}
}
if( LZ_decompress_close( decoder ) < 0 ) error = true;
if( error ) { free( outbuf ); return 0; }
*outlenp = outpos;
return outbuf;
}
/* Test the whole file at all levels. */
int full_test( const uint8_t * const inbuf, const long insize )
{
int level;
for( level = 0; level <= 9; ++level )
{
long midsize = 0, outsize = 0;
uint8_t * outbuf;
uint8_t * midbuf = bbcompressl( inbuf, insize, level, &midsize );
if( !midbuf )
{ fputs( "bbexample: full_test: Not enough memory or compress error.\n",
stderr ); return 1; }
outbuf = bbdecompressl( midbuf, midsize, &outsize );
free( midbuf );
if( !outbuf )
{ fputs( "bbexample: full_test: Not enough memory or decompress error.\n",
stderr ); return 1; }
if( insize != outsize ||
( insize > 0 && memcmp( inbuf, outbuf, insize ) != 0 ) )
{ fputs( "bbexample: full_test: Decompressed data differs from original.\n",
stderr ); free( outbuf ); return 1; }
free( outbuf );
}
return 0;
}
/* Compress 'insize' bytes from 'inbuf' to 'outbuf'.
Return the size of the compressed data in '*outlenp'.
In case of error, or if 'outsize' is too small, return false and do not
modify '*outlenp'.
*/
bool bbcompress( const uint8_t * const inbuf, const int insize,
const int dictionary_size, const int match_len_limit,
uint8_t * const outbuf, const int outsize,
int * const outlenp )
{
int inpos = 0, outpos = 0;
bool error = false;
struct LZ_Encoder * const encoder =
LZ_compress_open( dictionary_size, match_len_limit, INT64_MAX );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
{ LZ_compress_close( encoder ); return false; }
while( true )
{
int ret = LZ_compress_write( encoder, inbuf + inpos, insize - inpos );
if( ret < 0 ) { error = true; break; }
inpos += ret;
if( inpos >= insize ) LZ_compress_finish( encoder );
ret = LZ_compress_read( encoder, outbuf + outpos, outsize - outpos );
if( ret < 0 ) { error = true; break; }
outpos += ret;
if( LZ_compress_finished( encoder ) == 1 ) break;
if( outpos >= outsize ) { error = true; break; }
}
if( LZ_compress_close( encoder ) < 0 ) error = true;
if( error ) return false;
*outlenp = outpos;
return true;
}
/* Decompress 'insize' bytes from 'inbuf' to 'outbuf'.
Return the size of the decompressed data in '*outlenp'.
In case of error, or if 'outsize' is too small, return false and do not
modify '*outlenp'.
*/
bool bbdecompress( const uint8_t * const inbuf, const int insize,
uint8_t * const outbuf, const int outsize,
int * const outlenp )
{
int inpos = 0, outpos = 0;
bool error = false;
struct LZ_Decoder * const decoder = LZ_decompress_open();
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ LZ_decompress_close( decoder ); return false; }
while( true )
{
int ret = LZ_decompress_write( decoder, inbuf + inpos, insize - inpos );
if( ret < 0 ) { error = true; break; }
inpos += ret;
if( inpos >= insize ) LZ_decompress_finish( decoder );
ret = LZ_decompress_read( decoder, outbuf + outpos, outsize - outpos );
if( ret < 0 ) { error = true; break; }
outpos += ret;
if( LZ_decompress_finished( decoder ) == 1 ) break;
if( outpos >= outsize ) { error = true; break; }
}
if( LZ_decompress_close( decoder ) < 0 ) error = true;
if( error ) return false;
*outlenp = outpos;
return true;
}
/* Test at most INT_MAX bytes from the file with buffers of fixed size. */
int fixed_test( const uint8_t * const inbuf, const int insize )
{
int dictionary_size = 65535; /* fast encoder */
int midsize = min( INT_MAX, ( insize / 8 ) * 9LL + 44 ), outsize = insize;
uint8_t * midbuf = (uint8_t *)malloc( midsize );
uint8_t * outbuf = (uint8_t *)malloc( outsize );
if( !midbuf || !outbuf )
{ fputs( "bbexample: fixed_test: Not enough memory.\n", stderr );
free( outbuf ); free( midbuf ); return 1; }
for( ; dictionary_size <= 8 << 20; dictionary_size += 8323073 )
{
int midlen, outlen;
if( !bbcompress( inbuf, insize, dictionary_size, 16, midbuf, midsize, &midlen ) )
{ fputs( "bbexample: fixed_test: Not enough memory or compress error.\n",
stderr ); free( outbuf ); free( midbuf ); return 1; }
if( !bbdecompress( midbuf, midlen, outbuf, outsize, &outlen ) )
{ fputs( "bbexample: fixed_test: Not enough memory or decompress error.\n",
stderr ); free( outbuf ); free( midbuf ); return 1; }
if( insize != outlen ||
( insize > 0 && memcmp( inbuf, outbuf, insize ) != 0 ) )
{ fputs( "bbexample: fixed_test: Decompressed data differs from original.\n",
stderr ); free( outbuf ); free( midbuf ); return 1; }
}
free( outbuf );
free( midbuf );
return 0;
}
int main( const int argc, const char * const argv[] )
{
int retval = 0, i;
int open_failures = 0;
const bool verbose = ( argc > 2 );
if( argc < 2 )
{
fputs( "Usage: bbexample filename\n", stderr );
return 1;
}
for( i = 1; i < argc && retval == 0; ++i )
{
long insize;
uint8_t * const inbuf = read_file( argv[i], &insize );
if( !inbuf ) { ++open_failures; continue; }
if( verbose ) fprintf( stderr, " Testing file '%s'\n", argv[i] );
retval = full_test( inbuf, insize );
if( retval == 0 ) retval = fixed_test( inbuf, min( INT_MAX, insize ) );
free( inbuf );
}
if( open_failures > 0 && verbose )
fprintf( stderr, "bbexample: warning: %d %s failed to open.\n",
open_failures, ( open_failures == 1 ) ? "file" : "files" );
if( retval == 0 && open_failures ) retval = 1;
return retval;
}