summaryrefslogtreecommitdiffstats
path: root/bbexample.c
diff options
context:
space:
mode:
authorDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 13:45:54 +0000
committerDaniel Baumann <mail@daniel-baumann.ch>2015-11-07 13:45:54 +0000
commit53eee7f4e2ef4d4211df3e860e3c787fbd83c3e8 (patch)
treecff02b70b5a48f7cefa4a5ad4c4ab745e2b64dd6 /bbexample.c
parentAdding upstream version 1.2. (diff)
downloadlzlib-53eee7f4e2ef4d4211df3e860e3c787fbd83c3e8.tar.xz
lzlib-53eee7f4e2ef4d4211df3e860e3c787fbd83c3e8.zip
Adding upstream version 1.3.upstream/1.3
Signed-off-by: Daniel Baumann <mail@daniel-baumann.ch>
Diffstat (limited to '')
-rw-r--r--bbexample.c (renamed from bbexample.cc)57
1 files changed, 30 insertions, 27 deletions
diff --git a/bbexample.cc b/bbexample.c
index df91300..f174875 100644
--- a/bbexample.cc
+++ b/bbexample.c
@@ -1,5 +1,5 @@
/* Buff to buff example - A test program for the lzlib library
- Copyright (C) 2010, 2011 Antonio Diaz Diaz.
+ Copyright (C) 2010, 2011, 2012 Antonio Diaz Diaz.
This program is free software: you have unlimited permission
to copy, distribute and modify it.
@@ -14,10 +14,10 @@
#ifndef __cplusplus
#include <stdbool.h>
#endif
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdint.h>
#include <unistd.h>
#include "lzlib.h"
@@ -33,27 +33,27 @@
#endif
-// Compresses `size' bytes from `data'. Returns the address of a
-// malloc'd buffer containing the compressed data and its size in
-// `*out_sizep'.
-// In case of error, returns 0 and does not modify `*out_sizep'.
-
+/* Compresses 'size' bytes from 'data'. Returns the address of a
+ malloc'd buffer containing the compressed data and its size in
+ '*out_sizep'.
+ In case of error, returns 0 and does not modify '*out_sizep'.
+*/
uint8_t * bbcompress( const uint8_t * const data, const int size,
int * const out_sizep )
{
- int dict_size = 8 << 20; // 8 MiB
const int match_len_limit = 36;
const long long member_size = LLONG_MAX;
- if( dict_size > size ) dict_size = size;
+ 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 * encoder =
+ struct LZ_Encoder * const 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
+ 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 );
if( !new_data )
{ LZ_compress_close( encoder ); return 0; }
@@ -81,9 +81,10 @@ uint8_t * bbcompress( const uint8_t * const data, const int size,
if( LZ_compress_finished( encoder ) == 1 ) break;
if( new_pos >= new_data_size )
{
- void * const tmp = realloc( new_data, new_data_size + delta_size );
+ uint8_t * const tmp =
+ (uint8_t *)realloc( new_data, new_data_size + delta_size );
if( !tmp ) { error = true; break; }
- new_data = (uint8_t *)tmp;
+ new_data = tmp;
new_data_size += delta_size;
}
}
@@ -95,20 +96,20 @@ uint8_t * bbcompress( const uint8_t * const data, const int size,
}
-// Decompresses `size' bytes from `data'. Returns the address of a
-// malloc'd buffer containing the decompressed data and its size in
-// `*out_sizep'.
-// In case of error, returns 0 and does not modify `*out_sizep'.
-
+/* Decompresses 'size' bytes from 'data'. Returns the address of a
+ malloc'd buffer containing the decompressed data and its size in
+ '*out_sizep'.
+ In case of error, returns 0 and does not modify '*out_sizep'.
+*/
uint8_t * bbdecompress( const uint8_t * const data, const int size,
int * const out_sizep )
{
- struct LZ_Decoder * decoder = LZ_decompress_open();
+ struct LZ_Decoder * const decoder = LZ_decompress_open();
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok )
{ LZ_decompress_close( decoder ); return 0; }
- const int delta_size = size;
- int new_data_size = delta_size; // initial size
+ 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 );
if( !new_data )
{ LZ_decompress_close( decoder ); return 0; }
@@ -136,9 +137,10 @@ uint8_t * bbdecompress( const uint8_t * const data, const int size,
if( LZ_decompress_finished( decoder ) == 1 ) break;
if( new_pos >= new_data_size )
{
- void * const tmp = realloc( new_data, new_data_size + delta_size );
+ uint8_t * const tmp =
+ (uint8_t *)realloc( new_data, new_data_size + delta_size );
if( !tmp ) { error = true; break; }
- new_data = (uint8_t *)tmp;
+ new_data = tmp;
new_data_size += delta_size;
}
}
@@ -161,7 +163,7 @@ int main( const int argc, const char * const argv[] )
FILE *file = fopen( argv[1], "rb" );
if( !file )
{
- fprintf( stderr, "bbexample: Can't open file `%s' for reading\n", argv[1] );
+ fprintf( stderr, "bbexample: Can't open file '%s' for reading\n", argv[1] );
return 1;
}
@@ -172,10 +174,11 @@ int main( const int argc, const char * const argv[] )
fprintf( stderr, "bbexample: Not enough memory.\n" );
return 1;
}
+
const int 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] );
+ fprintf( stderr, "bbexample: Input file '%s' is too big.\n", argv[1] );
return 1;
}
fclose( file );
@@ -197,7 +200,7 @@ int main( const int argc, const char * const argv[] )
}
if( in_size != out_size ||
- ( out_size > 0 && memcmp( in_buffer, out_buffer, out_size ) ) )
+ ( in_size > 0 && memcmp( in_buffer, out_buffer, in_size ) != 0 ) )
{
fprintf( stderr, "bbexample: Decompressed data differs from original.\n" );
return 1;