Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [174]

By Root 1526 0

token = 0x00000000;

/***********************************************************************

* *

* Set the unmatched symbol. *

* *

***********************************************************************/

token = token | next;

/***********************************************************************

* *

* Set the number of bits in the token. *

* *

***********************************************************************/

tbits = LZ77_SYMBOL_BITS;

}

/**************************************************************************

* *

* Ensure that the token is in big-endian format. *

* *

**************************************************************************/

token = htonl(token);

/**************************************************************************

* *

* Write the token to the buffer of compressed data. *

* *

**************************************************************************/

for (i = 0; i < tbits; i++) {

if (opos % 8 == 0) {

/********************************************************************

* *

* Allocate another byte for the buffer of compressed data. *

* *

********************************************************************/

if ((temp = (unsigned char *)realloc(comp,(opos / 8) + 1)) == NULL) {

free(comp);

return -1;

}

comp = temp;

}

tpos = (sizeof(unsigned long) * 8) - tbits + i;

bit_set(comp, opos, bit_get((unsigned char *)&token, tpos));

opos++;

}

/**************************************************************************

* *

* Adjust the phrase length to account for the unmatched symbol. *

* *

**************************************************************************/

length++;

/**************************************************************************

* *

* Copy data from the look-ahead buffer to the sliding window. *

* *

**************************************************************************/

memmove(&window[0], &window[length], LZ77_WINDOW_SIZE - length);

memmove(&window[LZ77_WINDOW_SIZE - length], &buffer[0], length);

/**************************************************************************

* *

* Read more data into the look-ahead buffer. *

* *

**************************************************************************/

memmove(&buffer[0], &buffer[length], LZ77_BUFFER_SIZE - length);

for (i = LZ77_BUFFER_SIZE - length; i

buffer[i] = original[ipos];

ipos++;

}

/**************************************************************************

* *

* Adjust the total symbols remaining by the phrase length. *

* *

**************************************************************************/

remaining = remaining - length;

}

/*****************************************************************************

* *

* Point to the buffer of compressed data. *

* *

*****************************************************************************/

*compressed = comp;

/*****************************************************************************

* *

* Return the number of bytes in the compressed data. *

* *

*****************************************************************************/

return ((opos - 1) / 8) + 1;

}

/*****************************************************************************

* *

* ---------------------------- lz77_uncompress

--------------------------- *

* *

*****************************************************************************/

int lz77_uncompress(const unsigned char *compressed, unsigned char

**original) {

unsigned char window[LZ77_WINDOW_SIZE],

buffer[LZ77_BUFFER_SIZE],

*orig,

*temp,

next;

int offset,

length,

remaining,

hsize,

size,

ipos,

opos,

tpos,

state,

i;

/*****************************************************************************

* *

* Make the pointer to the original data not valid until later. *

* *

*****************************************************************************/

*original = orig = NULL;

/*****************************************************************************

Return Main Page Previous Page Next Page

®Online Book Reader