From 4c17768ae363b56cd937444528c58de3fc592a4d Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Mon, 18 Jun 2012 23:17:22 +0100 Subject: [PATCH] Try to make huffman decoder a bit more efficient. --- src/huffman.c | 23 ++++++++++++----------- src/huffman.h | 3 ++- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/huffman.c b/src/huffman.c index 16a539a0..1a72c8e9 100644 --- a/src/huffman.c +++ b/src/huffman.c @@ -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; } diff --git a/src/huffman.h b/src/huffman.h index 1bdd0408..184a5a8f 100644 --- a/src/huffman.h +++ b/src/huffman.h @@ -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