void DeleteTrie(TNode * root) { if (!root) return; DeleteTrie(root->l); DeleteTrie(root->r); delete root; }
void DeleteTrie(struct node *p) { if (p->l) DeleteTrie(p->l); if (p->r) DeleteTrie(p->r); FREE(p); } /* DeleteTrie */
long DecodeHuffman(const void *inData, void **outData) { struct node *root, *n; unsigned short *code; unsigned char *ascii, *sp, *dp; long i, j, l; struct HuffmanHeader *h; h = (struct HuffmanHeader *)inData; h->hSourceLength = ntohl(h->hSourceLength); h->hCompressedLength = ntohl(h->hCompressedLength); h->hCodeCount = ntohs(h->hCodeCount); *outData = NULL; /* Check de header, we willen geen bogus data decoderen... */ if (h->hVersion != kVersion) return -1; /* pointers naar de verschillende data onderdelen */ code = (unsigned short *)((char *)inData + kHuffmanHeaderSize); ascii = (unsigned char *)(code + h->hCodeCount); /* Bouw de decodeer Trie, dit is een radix search trie */ root = NULL; for (i = 0; i < h->hCodeCount; i++) RadixInsert(ntohs(code[i]), ascii[i], 15, &root); /* Initialiseer de data */ sp = ascii + h->hCodeCount; dp = (unsigned char *)MALLOC(h->hSourceLength); if (dp) *outData = dp; else return -1; /* en start decoderen */ l = 0; n = root; for (i = 0; i < h->hCompressedLength && l < h->hSourceLength; i++) { for (j = 7; j >= 0; j--) { n = (sp[i] & (1 << j)) ? n->r : n->l; if (!n) { FREE(dp); *outData = NULL; return -1; } if (n->t == nLeaf) { if (l < h->hSourceLength) { dp[l] = n->info; if (++l == h->hSourceLength) break; n = root; } } } } DeleteTrie(root); return h->hSourceLength; } /* DecodeHuffman */