summaryrefslogtreecommitdiffstats
path: root/bbexample.c
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 13:47:52 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 13:47:52 +0000
commit63df7a48df604cb3c9fd274713f6d65824ecaacd (patch)
treeef1140ed66892ae6e65ce9402e39727a74ebf4fd /bbexample.c
parentAdding upstream version 1.3. (diff)
downloadlzlib-63df7a48df604cb3c9fd274713f6d65824ecaacd.tar.xz
lzlib-63df7a48df604cb3c9fd274713f6d65824ecaacd.zip
Adding upstream version 1.4~rc2.upstream/1.4_rc2
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to 'bbexample.c')
-rw-r--r--bbexample.c74
1 files changed, 36 insertions, 38 deletions
diff --git a/bbexample.c b/bbexample.c
index f174875..1b33978 100644
--- a/bbexample.c
+++ b/bbexample.c
@@ -1,5 +1,5 @@
/* Buff to buff example - A test program for the lzlib library
- Copyright (C) 2010, 2011, 2012 Antonio Diaz Diaz.
+ Copyright (C) 2010, 2011, 2012, 2013 Antonio Diaz Diaz.
This program is free software: you have unlimited permission
to copy, distribute and modify it.
@@ -22,16 +22,6 @@
#include "lzlib.h"
-#ifndef LLONG_MAX
-#define LLONG_MAX 0x7FFFFFFFFFFFFFFFLL
-#endif
-#ifndef LLONG_MIN
-#define LLONG_MIN (-LLONG_MAX - 1LL)
-#endif
-#ifndef ULLONG_MAX
-#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL
-#endif
-
/* Compresses 'size' bytes from 'data'. Returns the address of a
malloc'd buffer containing the compressed data and its size in
@@ -41,28 +31,32 @@
uint8_t * bbcompress( const uint8_t * const data, const int size,
int * const out_sizep )
{
+ struct LZ_Encoder * encoder;
+ uint8_t * new_data;
const int match_len_limit = 36;
- const long long member_size = LLONG_MAX;
+ const unsigned long long member_size = INT64_MAX;
+ int delta_size, new_data_size;
+ int new_pos = 0;
+ int written = 0;
+ bool error = false;
int dict_size = 8 << 20; /* 8 MiB */
+
if( dict_size > size ) dict_size = size; /* saves memory */
if( dict_size < LZ_min_dictionary_size() )
dict_size = LZ_min_dictionary_size();
- struct LZ_Encoder * const encoder =
- LZ_compress_open( dict_size, match_len_limit, member_size );
+ encoder = LZ_compress_open( dict_size, match_len_limit, member_size );
if( !encoder || LZ_compress_errno( encoder ) != LZ_ok )
{ LZ_compress_close( encoder ); return 0; }
- const int delta_size = (size < 256) ? 64 : size / 4; /* size may be zero */
- int new_data_size = delta_size; /* initial size */
- uint8_t * new_data = (uint8_t *)malloc( new_data_size );
+ delta_size = (size < 256) ? 64 : size / 4; /* size may be zero */
+ new_data_size = delta_size; /* initial size */
+ new_data = (uint8_t *)malloc( new_data_size );
if( !new_data )
{ LZ_compress_close( encoder ); return 0; }
- int new_pos = 0;
- int written = 0;
- bool error = false;
while( true )
{
+ int rd;
if( LZ_compress_write_size( encoder ) > 0 )
{
if( written < size )
@@ -74,8 +68,8 @@ uint8_t * bbcompress( const uint8_t * const data, const int size,
}
if( written >= size ) LZ_compress_finish( encoder );
}
- const int rd = LZ_compress_read( encoder, new_data + new_pos,
- new_data_size - new_pos );
+ rd = LZ_compress_read( encoder, new_data + new_pos,
+ new_data_size - new_pos );
if( rd < 0 ) { error = true; break; }
new_pos += rd;
if( LZ_compress_finished( encoder ) == 1 ) break;
@@ -105,20 +99,22 @@ uint8_t * bbdecompress( const uint8_t * const data, const int size,
int * const out_sizep )
{
struct LZ_Decoder * const decoder = LZ_decompress_open();
+ uint8_t * new_data;
+ const int delta_size = size; /* size must be > zero */
+ int new_data_size = delta_size; /* initial size */
+ int new_pos = 0;
+ int written = 0;
+ bool error = false;
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ LZ_decompress_close( decoder ); return 0; }
- const int delta_size = size; /* size must be > zero */
- int new_data_size = delta_size; /* initial size */
- uint8_t * new_data = (uint8_t *)malloc( new_data_size );
+ new_data = (uint8_t *)malloc( new_data_size );
if( !new_data )
{ LZ_decompress_close( decoder ); return 0; }
- int new_pos = 0;
- int written = 0;
- bool error = false;
while( true )
{
+ int rd;
if( LZ_decompress_write_size( decoder ) > 0 )
{
if( written < size )
@@ -130,8 +126,8 @@ uint8_t * bbdecompress( const uint8_t * const data, const int size,
}
if( written >= size ) LZ_decompress_finish( decoder );
}
- const int rd = LZ_decompress_read( decoder, new_data + new_pos,
- new_data_size - new_pos );
+ rd = LZ_decompress_read( decoder, new_data + new_pos,
+ new_data_size - new_pos );
if( rd < 0 ) { error = true; break; }
new_pos += rd;
if( LZ_decompress_finished( decoder ) == 1 ) break;
@@ -154,28 +150,32 @@ uint8_t * bbdecompress( const uint8_t * const data, const int size,
int main( const int argc, const char * const argv[] )
{
+ FILE * file;
+ uint8_t * in_buffer, * mid_buffer, * out_buffer;
+ const int in_buffer_size = 1 << 20;
+ int in_size, mid_size = 0, out_size = 0;
+
if( argc < 2 )
{
fprintf( stderr, "Usage: bbexample filename\n" );
return 1;
}
- FILE *file = fopen( argv[1], "rb" );
+ file = fopen( argv[1], "rb" );
if( !file )
{
fprintf( stderr, "bbexample: Can't open file '%s' for reading\n", argv[1] );
return 1;
}
- const int in_buffer_size = 1 << 20;
- uint8_t * const in_buffer = (uint8_t *)malloc( in_buffer_size );
+ in_buffer = (uint8_t *)malloc( in_buffer_size );
if( !in_buffer )
{
fprintf( stderr, "bbexample: Not enough memory.\n" );
return 1;
}
- const int in_size = fread( in_buffer, 1, in_buffer_size, file );
+ in_size = fread( in_buffer, 1, in_buffer_size, file );
if( in_size >= in_buffer_size )
{
fprintf( stderr, "bbexample: Input file '%s' is too big.\n", argv[1] );
@@ -183,16 +183,14 @@ int main( const int argc, const char * const argv[] )
}
fclose( file );
- int mid_size = 0;
- uint8_t * const mid_buffer = bbcompress( in_buffer, in_size, &mid_size );
+ mid_buffer = bbcompress( in_buffer, in_size, &mid_size );
if( !mid_buffer )
{
fprintf( stderr, "bbexample: Not enough memory or compress error.\n" );
return 1;
}
- int out_size = 0;
- uint8_t * const out_buffer = bbdecompress( mid_buffer, mid_size, &out_size );
+ out_buffer = bbdecompress( mid_buffer, mid_size, &out_size );
if( !out_buffer )
{
fprintf( stderr, "bbexample: Not enough memory or decompress error.\n" );