Try to make huffman decoder a bit more efficient.

This commit is contained in:
Adam Sutton 2012-06-18 23:17:22 +01:00
parent 54fe7e97b2
commit 4c17768ae3
2 changed files with 14 additions and 12 deletions

View file

@ -75,15 +75,14 @@ huffman_node_t *huffman_tree_build ( htsmsg_t *m )
}
char *huffman_decode
( huffman_node_t *tree, const uint8_t *data, size_t len, uint8_t mask )
( huffman_node_t *tree, const uint8_t *data, size_t len, uint8_t mask,
char *outb, int outl )
{
char* ret;
size_t nalloc = len*4, nused = 0;
char *ret = outb;
huffman_node_t *node = tree;
if (!len) return NULL;
ret = malloc(nalloc);
ret[0] = '\0';
outl--; // leave space for NULL
while (len) {
len--;
while (mask) {
@ -93,19 +92,21 @@ char *huffman_decode
node = node->b0;
}
mask >>= 1;
if (!node) return ret;
if (!node) goto end;
if (node->data) {
size_t l = strlen(node->data);
if ((nalloc - nused) < l) {
nalloc *= 2;
ret = realloc(ret, nalloc);
char *t = node->data;
while (*t && outl) {
*outb = *t;
outb++; t++; outl--;
}
strcat(ret, node->data);
if (!outl) goto end;
node = tree;
}
}
mask = 0x80;
data++;
}
end:
*outb = '\0';
return ret;
}

View file

@ -33,6 +33,7 @@ void huffman_tree_destroy ( huffman_node_t *tree );
huffman_node_t *huffman_tree_load ( const char *path );
huffman_node_t *huffman_tree_build ( htsmsg_t *codes );
char *huffman_decode
( huffman_node_t *tree, const uint8_t *data, size_t len, uint8_t mask );
( huffman_node_t *tree, const uint8_t *data, size_t len, uint8_t mask,
char *outb, int outl );
#endif