int huffman_decode_file(FILE *in, FILE *out) { huffman_node *root, *p; int c; unsigned int data_count; /* Read the Huffman code table. */ root = read_code_table(in, &data_count); if(!root) return 1; /* Decode the file. */ p = root; while(data_count > 0 && (c = fgetc(in)) != EOF) { unsigned char byte = (unsigned char)c; unsigned char mask = 1; while(data_count > 0 && mask) { p = byte & mask ? p->one : p->zero; mask <<= 1; if(p->isLeaf) { fputc(p->symbol, out); p = root; --data_count; } } } free_huffman_tree(root); return 0; }
static int start_new_block(LHANewDecoder *decoder) { int len; // Read length of new block (in commands). len = read_bits(&decoder->bit_stream_reader, 16); if (len < 0) { return 0; } decoder->block_remaining = (size_t) len; // Read the temporary decode table, used to encode the codes table. // The position table data structure is reused for this. if (!read_temp_table(decoder)) { return 0; } // Read the code table; this is encoded *using* the temp table. if (!read_code_table(decoder)) { return 0; } // Read the offset table. if (!read_offset_table(decoder)) { return 0; } return 1; }