Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- inline int Extract_file(FileEntryHeader * fh, FILE * f, HuffmanTree * ht)
- {
- unsigned char * buff = calloc(read_buff_size, sizeof(unsigned char));
- uint32_t checksum= 0;
- register unsigned int i, poz = 0, l = 0, node = ht->root, bw = 0;
- i = 0;
- FILE * out = fopen(fh->file_name, "wb");
- if (out == NULL)
- {
- //Just in case we can't open the file for writing
- free(buff);
- return -1;
- }
- //bw means bytes written, it is used to check when the file was written
- while ( bw < fh->file_size)
- {
- if (poz == l*8)
- {
- i+=l;
- l = fread(buff, sizeof(unsigned char), min(read_buff_size, fh->bytes_n-i), f);
- poz = 0;
- debug("Just read %d bytes, i=%d, bytes_n is %d\n", l, i, fh->bytes_n);
- }
- if (ht->nodes[node].right == -1 && ht->nodes[node].left == -1)
- {
- //This means that we can write the value into the file
- fwrite(&(ht->nodes[node].value), sizeof(uint8_t), 1, out);
- checksum += ht->nodes[node].value;
- node = ht->root;
- ++bw;
- }
- //Reading the current bit
- if (buff[poz/8] & (1<<(7-poz%8)))
- node = ht->nodes[node].right;
- else
- node = ht->nodes[node].left;
- //Jumping to the new position
- ++poz;
- }
- //Cleanup
- fclose(out);
- free(buff);
- //If the checksum aint' good
- if (checksum != fh->checksum)
- {
- fprintf(stderr, "File %s is CORRUPT\n", fh->file_name);
- unlink(fh->file_name);
- debug("Checksum is %d it should be %d\n", checksum , fh->checksum);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement