| 1 |
#include "codec_huff.h" |
| 2 |
|
| 3 |
#include <stddef.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "../include/libchdr/huffman.h" |
| 8 |
|
| 9 |
chd_error huff_codec_init(void* codec, uint32_t hunkbytes) |
| 10 |
{ |
| 11 |
huff_codec_data* huff_codec = (huff_codec_data*) codec; |
| 12 |
(void)hunkbytes; |
| 13 |
huff_codec->decoder = create_huffman_decoder(256, 16); |
| 14 |
return CHDERR_NONE; |
| 15 |
} |
| 16 |
|
| 17 |
void huff_codec_free(void *codec) |
| 18 |
{ |
| 19 |
huff_codec_data* huff_codec = (huff_codec_data*) codec; |
| 20 |
delete_huffman_decoder(huff_codec->decoder); |
| 21 |
} |
| 22 |
|
| 23 |
chd_error huff_codec_decompress(void *codec, const uint8_t *src, uint32_t complen, uint8_t *dest, uint32_t destlen) |
| 24 |
{ |
| 25 |
huff_codec_data* huff_codec = (huff_codec_data*) codec; |
| 26 |
struct bitstream* bitbuf = create_bitstream(src, complen); |
| 27 |
uint32_t cur; |
| 28 |
chd_error result; |
| 29 |
|
| 30 |
|
| 31 |
enum huffman_error err = huffman_import_tree_huffman(huff_codec->decoder, bitbuf); |
| 32 |
if (err != HUFFERR_NONE) |
| 33 |
{ |
| 34 |
free(bitbuf); |
| 35 |
return CHDERR_DECOMPRESSION_ERROR; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
for (cur = 0; cur < destlen; cur++) |
| 40 |
dest[cur] = huffman_decode_one(huff_codec->decoder, bitbuf); |
| 41 |
bitstream_flush(bitbuf); |
| 42 |
result = bitstream_overflow(bitbuf) ? CHDERR_DECOMPRESSION_ERROR : CHDERR_NONE; |
| 43 |
|
| 44 |
free(bitbuf); |
| 45 |
return result; |
| 46 |
} |