Skip to content

Commit

Permalink
Make changes to BCL
Browse files Browse the repository at this point in the history
  • Loading branch information
AT0myks committed Dec 1, 2023
1 parent bcce60c commit 5555256
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 74 deletions.
35 changes: 35 additions & 0 deletions bcl/src/bcl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#define BCL_MAGIC "BCL1"
#define BCL_HEADER_SIZE 12

#define BCL_ALGO_RLE 1
#define BCL_ALGO_HUFFMAN 2
#define BCL_ALGO_RICE8 3
#define BCL_ALGO_RICE16 4
#define BCL_ALGO_RICE32 5
#define BCL_ALGO_RICE8S 6
#define BCL_ALGO_RICE16S 7
#define BCL_ALGO_RICE32S 8
#define BCL_ALGO_LZ77 9
#define BCL_ALGO_SF 10

#define BCL_E_OK 0
#define BCL_E_ERROR (-1)
#define BCL_E_INPUT_OVERRUN (-4)
#define BCL_E_OUTPUT_OVERRUN (-5)

#define BCL_COMP_BUF_HUFF(insize) ((insize) * (101.0 / 100.0) + 320)
#define BCL_COMP_BUF_RICE(insize) ((insize) + 1)
#define BCL_COMP_BUF_RLE(insize) ((insize) * (257.0 / 256.0) + 1)
#define BCL_COMP_BUF_SF(insize) ((insize) * (101.0 / 100.0) + 384)

#define BCL_COMP_MAX_HUFF (unsigned int)((UINT_MAX - 320) / (101.0 / 100.0))
#define BCL_COMP_MAX_RICE (UINT_MAX - 1)
#define BCL_COMP_MAX_RLE (unsigned int)((UINT_MAX - 1) / (257.0 / 256.0))
#define BCL_COMP_MAX_SF (unsigned int)((UINT_MAX - 384) / (101.0 / 100.0))

typedef int (*bcl_compress_t) (unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *work,
int format);
typedef int (*bcl_decompress_t)(unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *outsize,
int format);
23 changes: 17 additions & 6 deletions bcl/src/huffman.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
* marcus.geelnard at home.se
*************************************************************************/

#include "bcl.h"

/*************************************************************************
* Types used for Huffman coding
*************************************************************************/
Expand Down Expand Up @@ -394,18 +396,20 @@ static huff_decodenode_t * _Huffman_RecoverTree( huff_decodenode_t *nodes,
* out - Output (compressed) buffer. This buffer must be 384 bytes
* larger than the input buffer.
* insize - Number of input bytes.
* work - Unused.
* format - Unused.
* The function returns the size of the compressed data.
*************************************************************************/

int Huffman_Compress( unsigned char *in, unsigned char *out,
unsigned int insize )
unsigned int insize, unsigned int *work, int format )
{
huff_sym_t sym[256], tmp;
huff_bitstream_t stream;
unsigned int k, total_bytes, swaps, symbol;

/* Do we have anything to compress? */
if( insize < 1 ) return 0;
if( insize < 1 ) return BCL_E_OK;

/* Initialize bitstream */
_Huffman_InitBitstream( &stream, out );
Expand Down Expand Up @@ -460,18 +464,23 @@ int Huffman_Compress( unsigned char *in, unsigned char *out,
* enough to hold the uncompressed data.
* insize - Number of input bytes.
* outsize - Number of output bytes.
* format - Unused.
*************************************************************************/

void Huffman_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int outsize )
int Huffman_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *outsize, int format )
{
huff_decodenode_t nodes[MAX_TREE_NODES], *root, *node;
huff_bitstream_t stream;
unsigned int k, node_count;
unsigned char *buf;

/* Do we have anything to decompress? */
if( insize < 1 ) return;
if( insize < 1 )
{
*outsize = 0;
return BCL_E_OK;
}

/* Initialize bitstream */
_Huffman_InitBitstream( &stream, in );
Expand All @@ -482,7 +491,7 @@ void Huffman_Uncompress( unsigned char *in, unsigned char *out,

/* Decode input stream */
buf = out;
for( k = 0; k < outsize; ++ k )
for( k = 0; k < *outsize; ++ k )
{
/* Traverse tree until we find a matching leaf node */
node = root;
Expand All @@ -498,4 +507,6 @@ void Huffman_Uncompress( unsigned char *in, unsigned char *out,
/* We found the matching leaf node and have the symbol */
*buf ++ = (unsigned char) node->Symbol;
}
/* No need to change outsize. */
return BCL_E_OK;
}
6 changes: 3 additions & 3 deletions bcl/src/huffman.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ extern "C" {
*************************************************************************/

int Huffman_Compress( unsigned char *in, unsigned char *out,
unsigned int insize );
void Huffman_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int outsize );
unsigned int insize, unsigned int *work, int format );
int Huffman_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *outsize, int format );


#ifdef __cplusplus
Expand Down
52 changes: 42 additions & 10 deletions bcl/src/lz.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
* marcus.geelnard at home.se
*************************************************************************/

#include "bcl.h"

/*************************************************************************
* Constants used for LZ77 coding
Expand Down Expand Up @@ -150,7 +151,7 @@ static int _LZ_WriteVarSize( unsigned int x, unsigned char * buf )
* bytes depending on value.
*************************************************************************/

static int _LZ_ReadVarSize( unsigned int * x, unsigned char * buf )
static int _LZ_ReadVarSize( unsigned int * x, unsigned char * buf, unsigned int maxread, int *err )
{
unsigned int y, b, num_bytes;

Expand All @@ -159,6 +160,10 @@ static int _LZ_ReadVarSize( unsigned int * x, unsigned char * buf )
num_bytes = 0;
do
{
if (num_bytes >= maxread) {
*err = BCL_E_INPUT_OVERRUN;
return num_bytes;
}
b = (unsigned int) (*buf ++);
y = (y << 7) | (b & 0x0000007f);
++ num_bytes;
Expand All @@ -185,11 +190,13 @@ static int _LZ_ReadVarSize( unsigned int * x, unsigned char * buf )
* out - Output (compressed) buffer. This buffer must be 0.4% larger
* than the input buffer, plus one byte.
* insize - Number of input bytes.
* work - Unused.
* format - Unused.
* The function returns the size of the compressed data.
*************************************************************************/

int LZ_Compress( unsigned char *in, unsigned char *out,
unsigned int insize )
unsigned int insize, unsigned int *work, int format )
{
unsigned char marker, symbol;
unsigned int inpos, outpos, bytesleft, i;
Expand All @@ -201,7 +208,7 @@ int LZ_Compress( unsigned char *in, unsigned char *out,
/* Do we have anything to compress? */
if( insize < 1 )
{
return 0;
return BCL_E_OK;
}

/* Create histogram */
Expand Down Expand Up @@ -320,11 +327,12 @@ int LZ_Compress( unsigned char *in, unsigned char *out,
* insize - Number of input bytes.
* work - Pointer to a temporary buffer (internal working buffer), which
* must be able to hold (insize+65536) unsigned integers.
* format - Unused.
* The function returns the size of the compressed data.
*************************************************************************/

int LZ_CompressFast( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *work )
unsigned int insize, unsigned int *work, int format )
{
unsigned char marker, symbol;
unsigned int inpos, outpos, bytesleft, i, index, symbols;
Expand All @@ -336,7 +344,7 @@ int LZ_CompressFast( unsigned char *in, unsigned char *out,
/* Do we have anything to compress? */
if( insize < 1 )
{
return 0;
return BCL_E_OK;
}

/* Assign arrays to the working area */
Expand Down Expand Up @@ -477,18 +485,22 @@ int LZ_CompressFast( unsigned char *in, unsigned char *out,
* out - Output (uncompressed) buffer. This buffer must be large
* enough to hold the uncompressed data.
* insize - Number of input bytes.
* outsize - Number of output bytes.
* format - Unused
*************************************************************************/

void LZ_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize )
int LZ_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *outsize, int format )
{
unsigned char marker, symbol;
unsigned int i, inpos, outpos, length, offset;
int err = 0;

/* Do we have anything to uncompress? */
if( insize < 1 )
{
return;
*outsize = 0;
return BCL_E_OK;
}

/* Get marker symbol from input stream */
Expand All @@ -499,9 +511,18 @@ void LZ_Uncompress( unsigned char *in, unsigned char *out,
outpos = 0;
do
{
if (outpos >= *outsize) {
return BCL_E_OUTPUT_OVERRUN;
}
if (inpos >= insize) {
return BCL_E_INPUT_OVERRUN;
}
symbol = in[ inpos ++ ];
if( symbol == marker )
{
if (inpos >= insize) {
return BCL_E_INPUT_OVERRUN;
}
/* We had a marker byte */
if( in[ inpos ] == 0 )
{
Expand All @@ -512,12 +533,21 @@ void LZ_Uncompress( unsigned char *in, unsigned char *out,
else
{
/* Extract true length and offset */
inpos += _LZ_ReadVarSize( &length, &in[ inpos ] );
inpos += _LZ_ReadVarSize( &offset, &in[ inpos ] );
inpos += _LZ_ReadVarSize( &length, &in[ inpos ], insize - inpos, &err );
if (inpos >= insize || err != 0) {
return BCL_E_INPUT_OVERRUN;
}
inpos += _LZ_ReadVarSize( &offset, &in[ inpos ], insize - inpos, &err );
if (err != 0) {
return BCL_E_INPUT_OVERRUN;
}

/* Copy corresponding data from history window */
for( i = 0; i < length; ++ i )
{
if (outpos >= *outsize || (outpos - offset) >= *outsize) {
return BCL_E_OUTPUT_OVERRUN;
}
out[ outpos ] = out[ outpos - offset ];
++ outpos;
}
Expand All @@ -530,4 +560,6 @@ void LZ_Uncompress( unsigned char *in, unsigned char *out,
}
}
while( inpos < insize );
*outsize = outpos;
return BCL_E_OK;
}
8 changes: 4 additions & 4 deletions bcl/src/lz.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ extern "C" {
*************************************************************************/

int LZ_Compress( unsigned char *in, unsigned char *out,
unsigned int insize );
unsigned int insize, unsigned int *work, int format );
int LZ_CompressFast( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *work );
void LZ_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize );
unsigned int insize, unsigned int *work, int format );
int LZ_Uncompress( unsigned char *in, unsigned char *out,
unsigned int insize, unsigned int *outsize, int format );


#ifdef __cplusplus
Expand Down
21 changes: 12 additions & 9 deletions bcl/src/rice.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
* marcus.geelnard at home.se
*************************************************************************/

#include "bcl.h"
#include "rice.h"



/*************************************************************************
* Constants used for Rice coding
*************************************************************************/
Expand Down Expand Up @@ -402,11 +402,12 @@ static void _Rice_WriteWord( void *ptr, unsigned int idx, int format,
* out - Output (compressed) buffer. This buffer must one byte larger
* than the input buffer.
* insize - Number of input bytes.
* work - Unused.
* format - Binary format (see rice.h)
* The function returns the size of the compressed data.
*************************************************************************/

int Rice_Compress( void *in, void *out, unsigned int insize, int format )
int Rice_Compress( void *in, void *out, unsigned int insize, unsigned int *work, int format )
{
rice_bitstream_t stream;
unsigned int i, x, k, n, wordsize, incount;
Expand All @@ -422,14 +423,14 @@ int Rice_Compress( void *in, void *out, unsigned int insize, int format )
case RICE_FMT_UINT16: wordsize = 16; break;
case RICE_FMT_INT32:
case RICE_FMT_UINT32: wordsize = 32; break;
default: return 0;
default: return BCL_E_ERROR;
}
incount = insize / (wordsize>>3);

/* Do we have anything to compress? */
if( incount == 0 )
{
return 0;
return BCL_E_OK;
}

/* Initialize output bitsream */
Expand Down Expand Up @@ -505,8 +506,8 @@ int Rice_Compress( void *in, void *out, unsigned int insize, int format )
* format - Binary format (see rice.h)
*************************************************************************/

void Rice_Uncompress( void *in, void *out, unsigned int insize,
unsigned int outsize, int format )
int Rice_Uncompress( void *in, void *out, unsigned int insize,
unsigned int *outsize, int format )
{
rice_bitstream_t stream;
unsigned int i, x, k, wordsize, outcount;
Expand All @@ -522,14 +523,15 @@ void Rice_Uncompress( void *in, void *out, unsigned int insize,
case RICE_FMT_UINT16: wordsize = 16; break;
case RICE_FMT_INT32:
case RICE_FMT_UINT32: wordsize = 32; break;
default: return;
default: return BCL_E_ERROR;
}
outcount = outsize / (wordsize>>3);
outcount = *outsize / (wordsize>>3);

/* Do we have anything to decompress? */
if( outcount == 0 )
{
return;
*outsize = 0;
return BCL_E_OK;
}

/* Initialize input bitsream */
Expand Down Expand Up @@ -579,4 +581,5 @@ void Rice_Uncompress( void *in, void *out, unsigned int insize,
hist[ i % RICE_HISTORY ] = _Rice_NumBits( x );
}
}
return BCL_E_OK;
}
6 changes: 3 additions & 3 deletions bcl/src/rice.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ extern "C" {
* Function prototypes
*************************************************************************/

int Rice_Compress( void *in, void *out, unsigned int insize, int format );
void Rice_Uncompress( void *in, void *out, unsigned int insize,
unsigned int outsize, int format );
int Rice_Compress( void *in, void *out, unsigned int insize, unsigned int *work, int format );
int Rice_Uncompress( void *in, void *out, unsigned int insize,
unsigned int *outsize, int format );


#ifdef __cplusplus
Expand Down
Loading

0 comments on commit 5555256

Please sign in to comment.