int main(int argc, char* argv[]) { struct bitio *bd; uint64_t d, r; int i; //TEST 1: writes 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF23456789ABCDEF if ((bd = bitio_open("bitio_test.dat", 'w')) == NULL) { perror("bopen(w)"); exit(EXIT_FAILURE); } d = 0x0123456789ABCDEF; bitio_write(bd, d, 64); bitio_write(bd, d, 56); d = 0x23456789ABCDEF01; bitio_write(bd, d, 64); bitio_write(bd, d, 64); bitio_close(bd); if ((bd = bitio_open("bitio_test.dat", 'r')) == NULL) { perror("bopen(r)"); exit(EXIT_FAILURE); } for (i = 1; i <= 3; i++) { bitio_read(bd, &r, 64); if (r != 0x0123456789ABCDEF) exit(EXIT_FAILURE); } bitio_read(bd, &r, 56); if (r != 0x23456789ABCDEF) exit(EXIT_FAILURE); bitio_close(bd); //delete file unlink("bitio_test.dat"); //TEST 2: stdin and stdout while(bitio_read(bstdin, &r, 1) > 0) { bitio_write(bstdout, r, 1); } bitio_flush(bstdout); exit(EXIT_SUCCESS); }
/** * @internal * Reads @p bits bits from @p f and returns the fetched number. * * @param f Pointer to bitio context from which read the bits. * @param bits Number of bits to read. * * @return The fetched index on success, ROOT_NODE on failure. */ uint32_t fetch(struct bitio* f, uint8_t bits) { uint64_t index; if (bitio_read(f, &index, bits) < bits) return ROOT_NODE; return index; }
int main(int argc, char **argv) { bitio *b = NULL; uint64_t data = UINT64_MAX; int ret; if (!(b = bitio_open("test2", O_RDONLY))) { perror("bitio_open"); exit(-1); } for(int i = 0; i<1; i++ ) { ret = bitio_read(b, &data, 32); ret = bitio_read(b, &data, 64); if (ret) { printf("ret %d\n", ret); break; } fprintf(stderr,"%016llX -> %" PRIu64 "\n", (long long unsigned int)data, data); ret = bitio_read(b, &data, 32); /*ret = bitio_read(b, &data, 64); if (ret) { printf("ret %d\n", ret); break; } fprintf(stderr,"%016llX -> %" PRIu64 "\n", (long long unsigned int)data, data);*/ } bitio_close(b); return 0; }
//decompressor algorithm for lz78 //filename is the path of the file to decompress, size is the size of the dictionary void decompress(char* input, char* filename) { int /*i,*/index_bits, flag, aux, child_root=0, pos; bool first_read = true, res_retrieve=false; uint64_t read_index, prev_current=0; bitio* comp_file; char* tmp; dictionary* dict; word* sequence = NULL; //Variable in which store the word in each step word* print_seq; header hdr; int decomp_file; //eofc_pos = dict->symbols; comp_file = bit_open(input, "r"); //Preparing my data structures //retrieve header bitio_read(comp_file, (uint64_t*)&hdr.dictionary_size, sizeof(int)*8); //for(i=0; i<6; i++) //bitio_read(comp_file, (uint64_t*)&hdr.extension[i], sizeof(char)*8); dict = malloc(sizeof(dictionary)); dict_init(dict, hdr.dictionary_size, 256); aux = hdr.dictionary_size; //Compute the number of bits representing the indexes fprintf(stderr, "%i\n", aux); index_bits = 1; while(aux >>= 1) index_bits++; tmp=malloc(strlen(filename)); strcpy(tmp, filename); //strcat(tmp, ".\0"); //strcat(tmp, hdr.extension); decomp_file=open(tmp, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); bitio_read(comp_file, &read_index, index_bits); //Read the first index //decomp_file = open(decompname, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); //fprintf(stderr, "\n%i\n", fdim); fprintf(stderr, "%s %i\n", tmp, index_bits); while(read_index != EOFC) { //Until the end of compressed file //fprintf(stdout, "%i\n", (int)read_index); //retrieve word from index res_retrieve=dict_retrieve_word(dict, read_index, &sequence); //fprintf(stderr, "\n%x\n", (unsigned int)read_index); //critical situation if(res_retrieve==false) { pos=dict_search((int)prev_current, child_root, dict, &flag); dict_add_word(pos, (int)prev_current, child_root, dict, &flag); dict_retrieve_word(dict, read_index, &sequence); first_read=true; } //fprintf(stderr, "decompressor %i\n", decomp_file); child_root = sequence->symbol; //fprintf(stderr, "decompressor %i\n", decomp_file); print_seq = sequence; //Write word on file while(print_seq != NULL) { //fprintf(stdout, "%i\n",print_seq->symbol); write(decomp_file, (void*)&(print_seq->symbol), 1); print_seq = print_seq->next; free(sequence); //Deallocate word structure sequence = print_seq; } if(first_read==false) { //Add the first symbol of my sequence, as child of the previous current node pos=dict_search((int)prev_current, child_root, dict, &flag); dict_add_word(pos, (int)prev_current, child_root, dict, &flag); } first_read = false; prev_current = read_index; bitio_read(comp_file, &read_index, index_bits); //Read the index } //Closure of all data structures close(comp_file->fd); free(comp_file); close(decomp_file); //print_dict(dict); suppress_dictionary(dict); fprintf(stderr, "decompression executed\n"); }