bbexample.c (lzlib-1.12.tar.lz) | : | bbexample.c (lzlib-1.13.tar.lz) | ||
---|---|---|---|---|
/* Buffer to buffer example - Test program for the library lzlib | /* Buffer to buffer example - Test program for the library lzlib | |||
Copyright (C) 2010-2021 Antonio Diaz Diaz. | Copyright (C) 2010-2022 Antonio Diaz Diaz. | |||
This program is free software: you have unlimited permission | This program is free software: you have unlimited permission | |||
to copy, distribute, and modify it. | to copy, distribute, and modify it. | |||
Usage: bbexample filename | Usage: bbexample filename | |||
This program is an example of how buffer-to-buffer | This program is an example of how buffer-to-buffer | |||
compression/decompression can be implemented using lzlib. | compression/decompression can be implemented using lzlib. | |||
*/ | */ | |||
skipping to change at line 30 | skipping to change at line 30 | |||
#include <stdlib.h> | #include <stdlib.h> | |||
#include <string.h> | #include <string.h> | |||
#include <unistd.h> | #include <unistd.h> | |||
#include "lzlib.h" | #include "lzlib.h" | |||
#ifndef min | #ifndef min | |||
#define min(x,y) ((x) <= (y) ? (x) : (y)) | #define min(x,y) ((x) <= (y) ? (x) : (y)) | |||
#endif | #endif | |||
/* Returns the address of a malloc'd buffer containing the file data and | /* Return the address of a malloc'd buffer containing the file data and | |||
the file size in '*file_sizep'. | the file size in '*file_sizep'. | |||
In case of error, returns 0 and does not modify '*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 ) | uint8_t * read_file( const char * const name, long * const file_sizep ) | |||
{ | { | |||
long buffer_size = 1 << 20, file_size; | long buffer_size = 1 << 20, file_size; | |||
uint8_t * buffer, * tmp; | uint8_t * buffer, * tmp; | |||
FILE * const f = fopen( name, "rb" ); | FILE * const f = fopen( name, "rb" ); | |||
if( !f ) | if( !f ) | |||
{ fprintf( stderr, "bbexample: Can't open input file '%s': %s\n", | { fprintf( stderr, "bbexample: Can't open input file '%s': %s\n", | |||
name, strerror( errno ) ); return 0; } | name, strerror( errno ) ); return 0; } | |||
skipping to change at line 74 | skipping to change at line 74 | |||
{ | { | |||
fprintf( stderr, "bbexample: Error reading file '%s': %s\n", | fprintf( stderr, "bbexample: Error reading file '%s': %s\n", | |||
name, strerror( errno ) ); | name, strerror( errno ) ); | |||
free( buffer ); fclose( f ); return 0; | free( buffer ); fclose( f ); return 0; | |||
} | } | |||
fclose( f ); | fclose( f ); | |||
*file_sizep = file_size; | *file_sizep = file_size; | |||
return buffer; | return buffer; | |||
} | } | |||
/* Compresses 'insize' bytes from 'inbuf'. | /* Compress 'insize' bytes from 'inbuf'. | |||
Returns the address of a malloc'd buffer containing the compressed data, | Return the address of a malloc'd buffer containing the compressed data, | |||
and the size of the data in '*outlenp'. | and the size of the data in '*outlenp'. | |||
In case of error, returns 0 and does not modify '*outlenp'. | In case of error, return 0 and do not modify '*outlenp'. | |||
*/ | */ | |||
uint8_t * bbcompressl( const uint8_t * const inbuf, const long insize, | uint8_t * bbcompressl( const uint8_t * const inbuf, const long insize, | |||
const int level, long * const outlenp ) | const int level, long * const outlenp ) | |||
{ | { | |||
struct Lzma_options | struct Lzma_options | |||
{ | { | |||
int dictionary_size; /* 4 KiB .. 512 MiB */ | int dictionary_size; /* 4 KiB .. 512 MiB */ | |||
int match_len_limit; /* 5 .. 273 */ | int match_len_limit; /* 5 .. 273 */ | |||
}; | }; | |||
/* Mapping from gzip/bzip2 style 1..9 compression modes | /* Mapping from gzip/bzip2 style 1..9 compression modes | |||
skipping to change at line 152 | skipping to change at line 152 | |||
outbuf = tmp; | outbuf = tmp; | |||
} | } | |||
} | } | |||
if( LZ_compress_close( encoder ) < 0 ) error = true; | if( LZ_compress_close( encoder ) < 0 ) error = true; | |||
if( error ) { free( outbuf ); return 0; } | if( error ) { free( outbuf ); return 0; } | |||
*outlenp = outpos; | *outlenp = outpos; | |||
return outbuf; | return outbuf; | |||
} | } | |||
/* Decompresses 'insize' bytes from 'inbuf'. | /* Decompress 'insize' bytes from 'inbuf'. | |||
Returns the address of a malloc'd buffer containing the decompressed | Return the address of a malloc'd buffer containing the decompressed | |||
data, and the size of the data in '*outlenp'. | data, and the size of the data in '*outlenp'. | |||
In case of error, returns 0 and does not modify '*outlenp'. | In case of error, return 0 and do not modify '*outlenp'. | |||
*/ | */ | |||
uint8_t * bbdecompressl( const uint8_t * const inbuf, const long insize, | uint8_t * bbdecompressl( const uint8_t * const inbuf, const long insize, | |||
long * const outlenp ) | long * const outlenp ) | |||
{ | { | |||
struct LZ_Decoder * const decoder = LZ_decompress_open(); | struct LZ_Decoder * const decoder = LZ_decompress_open(); | |||
const long delta_size = insize; /* insize must be > zero */ | const long delta_size = insize; /* insize must be > zero */ | |||
long outsize = delta_size; /* initial outsize */ | long outsize = delta_size; /* initial outsize */ | |||
uint8_t * outbuf = (uint8_t *)malloc( outsize ); | uint8_t * outbuf = (uint8_t *)malloc( outsize ); | |||
long inpos = 0; | long inpos = 0; | |||
long outpos = 0; | long outpos = 0; | |||
skipping to change at line 228 | skipping to change at line 228 | |||
if( insize != outsize || | if( insize != outsize || | |||
( insize > 0 && memcmp( inbuf, outbuf, insize ) != 0 ) ) | ( insize > 0 && memcmp( inbuf, outbuf, insize ) != 0 ) ) | |||
{ fputs( "bbexample: full_test: Decompressed data differs from original.\n ", | { fputs( "bbexample: full_test: Decompressed data differs from original.\n ", | |||
stderr ); free( outbuf ); return 1; } | stderr ); free( outbuf ); return 1; } | |||
free( outbuf ); | free( outbuf ); | |||
} | } | |||
return 0; | return 0; | |||
} | } | |||
/* Compresses 'insize' bytes from 'inbuf' to 'outbuf'. | /* Compress 'insize' bytes from 'inbuf' to 'outbuf'. | |||
Returns the size of the compressed data in '*outlenp'. | Return the size of the compressed data in '*outlenp'. | |||
In case of error, or if 'outsize' is too small, returns false and does | In case of error, or if 'outsize' is too small, return false and do not | |||
not modify '*outlenp'. | modify '*outlenp'. | |||
*/ | */ | |||
bool bbcompress( const uint8_t * const inbuf, const int insize, | bool bbcompress( const uint8_t * const inbuf, const int insize, | |||
const int dictionary_size, const int match_len_limit, | const int dictionary_size, const int match_len_limit, | |||
uint8_t * const outbuf, const int outsize, | uint8_t * const outbuf, const int outsize, | |||
int * const outlenp ) | int * const outlenp ) | |||
{ | { | |||
int inpos = 0, outpos = 0; | int inpos = 0, outpos = 0; | |||
bool error = false; | bool error = false; | |||
struct LZ_Encoder * const encoder = | struct LZ_Encoder * const encoder = | |||
LZ_compress_open( dictionary_size, match_len_limit, INT64_MAX ); | LZ_compress_open( dictionary_size, match_len_limit, INT64_MAX ); | |||
skipping to change at line 264 | skipping to change at line 264 | |||
if( LZ_compress_finished( encoder ) == 1 ) break; | if( LZ_compress_finished( encoder ) == 1 ) break; | |||
if( outpos >= outsize ) { error = true; break; } | if( outpos >= outsize ) { error = true; break; } | |||
} | } | |||
if( LZ_compress_close( encoder ) < 0 ) error = true; | if( LZ_compress_close( encoder ) < 0 ) error = true; | |||
if( error ) return false; | if( error ) return false; | |||
*outlenp = outpos; | *outlenp = outpos; | |||
return true; | return true; | |||
} | } | |||
/* Decompresses 'insize' bytes from 'inbuf' to 'outbuf'. | /* Decompress 'insize' bytes from 'inbuf' to 'outbuf'. | |||
Returns the size of the decompressed data in '*outlenp'. | Return the size of the decompressed data in '*outlenp'. | |||
In case of error, or if 'outsize' is too small, returns false and does | In case of error, or if 'outsize' is too small, return false and do not | |||
not modify '*outlenp'. | modify '*outlenp'. | |||
*/ | */ | |||
bool bbdecompress( const uint8_t * const inbuf, const int insize, | bool bbdecompress( const uint8_t * const inbuf, const int insize, | |||
uint8_t * const outbuf, const int outsize, | uint8_t * const outbuf, const int outsize, | |||
int * const outlenp ) | int * const outlenp ) | |||
{ | { | |||
int inpos = 0, outpos = 0; | int inpos = 0, outpos = 0; | |||
bool error = false; | bool error = false; | |||
struct LZ_Decoder * const decoder = LZ_decompress_open(); | struct LZ_Decoder * const decoder = LZ_decompress_open(); | |||
if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) | if( !decoder || LZ_decompress_errno( decoder ) != LZ_ok ) | |||
{ LZ_decompress_close( decoder ); return false; } | { LZ_decompress_close( decoder ); return false; } | |||
End of changes. 9 change blocks. | ||||
17 lines changed or deleted | 17 lines changed or added |