void recreate_file(node_t * head, FILE * compressed, FILE * destination) { node_t * temp= head; rewind(compressed); unsigned char ch= fgetc(compressed); int ascii = (int)ch; int ascii_convert[8] = {0}; ascii_to_binary(ascii_convert, ascii); int i=0; int check=0; int count = count_last / 8 +1; while(check < count) { while(temp->left && temp->right && (8-i)&& count_last--) { if(ascii_convert[i]==0) { temp=temp->left; } else { temp=temp->right; } ++i; } if(temp->left==0 && temp->right==0 && i!=8 && count_last) { fprintf(destination,"%c", *(temp->character)); temp=head; } else if(temp->left==0 && temp->right==0 && i==8 && count_last) { fprintf(destination,"%c", *(temp->character)); temp=head; ch = fgetc(compressed); ascii = (int)ch; ascii_to_binary(ascii_convert, ascii); check++; i=0; } else { ch = fgetc(compressed); ascii = (int)ch; ascii_to_binary(ascii_convert, ascii); check++; i=0; } } }
static void test_ascii_to_binary(void) { uint32_t i; int32_t rtrn = 0; char *string = "dn39r93ho*(#98893rnf@N(EHM(EI"; char *binary_string = NULL; uint64_t size = 0; /* When we pass a zero size, ascii_to_binary should fail. */ rtrn = ascii_to_binary(string, &binary_string, 0, &size); TEST_ASSERT(rtrn < 0); TEST_ASSERT(binary_string == NULL); TEST_ASSERT(size == 0); /* Should work now that we have a non zero length argument. */ rtrn = ascii_to_binary(string, &binary_string, strlen(string), &size); TEST_ASSERT(rtrn == 0); TEST_ASSERT(binary_string != NULL); TEST_ASSERT(size > 0); /* Clean up. */ // mem_free((void **)&binary_string); /* Loop and compare conversions to known good values to validate ascii_to_binary(); */ for(i = 0; i < binary_test; i++) { /* Convert the string in */ rtrn = ascii_to_binary(string_array[i], &binary_string, strlen(string_array[i]), &size); TEST_ASSERT(rtrn == 0); TEST_ASSERT(binary_string != NULL); TEST_ASSERT(size > 0); TEST_ASSERT(strncmp(binary_array[i], binary_string, 8) == 0); // mem_free((void **)&binary_string); } return; }