/* decode.c */ /* Program for Lempel-Ziv decompression */ #include #include #include #include #include "lz.h" /* contains global constants and datastructures*/ #include "decode.h" /* defines the ADT for encoding */ #include "bits.h" #include "dict.h" /*-------------------------------------------------------------------*/ int main(int argc, char *argv[]) { FILE *f_in, *f_out; double start_time, end_time; char input_file_name[100],output_file_name[100]; int BPC; /*bits per code*/ /* From the standard input, read the name of input and output file */ /* names. Encode the input file and store the compressed data into */ /* output file */ /* read the input and output filenames */ printf("ENTER THE NAME OF FILE TO BE UNCOMPRESSED "); scanf("%s",input_file_name); printf("\nENTER THE OUTPUT FILE NAME "); scanf("%s",output_file_name); printf("\nBITS PER CODE USED (SHOULD BE BETWEEN 8 and 31) "); scanf("%d",&BPC); printf("\n"); /* open the input file for reading */ /* the option "rb" signifies that we will be reading */ /* from the input file as a binary file */ /* "r" is for read and "b" for binary */ f_in = fopen(input_file_name,"rb"); assert(f_in != NULL); /* if error in opening file, exit */ /* open the output file for writing */ /* the option "wb" is for binary write */ f_out = fopen(output_file_name,"wb"); assert(f_out != NULL); /* start clock */ start_time = clock(); /* decode the data */ LZdecode(f_in, f_out,BPC); /* end clock */ end_time = clock(); /* Print the statistics */ /* Print statistics */ printf("Total time = %f secs\n",(end_time-start_time)/CLOCKS_PER_SEC); /* close the files*/ fclose(f_in); fclose(f_out); return(1); } /* end of main */ /*-----------------------------------------------------------------------*/ void LZdecode(FILE *in, FILE *out, int BPC) { /* encode the data */ /* an inefficient implementation !!! */ int input; char curr_string[MAX_MATCH_LENGTH], prev_string[MAX_MATCH_LENGTH]; DICT_TYPE dict; /* initialize the dictionary */ dict = init_dict(BPC); /* read the first input code */ input = getCode(BPC,in); prev_string[0] = '\0'; /* read codes from the input file. LAST_CODE is the last code in */ /* the input file. So, if we encounter that, we know that we have*/ /* reached the end of file */ while(input != LAST_CODE) { int length; length = strlen(prev_string); /* Look in the dictionary associated with the code input */ dict_string_lookup(input,curr_string,dict); if (curr_string[0] != '\0') { /* we found the string */ /* insert into the dictionary the code corresponding to the */ /* string formed by taking prev_string and attaching the */ /* first character of curr_string to it */ /* also, write curr_string in the output string */ /* also output curr_string into the output file */ fprintf(out,"%s",curr_string); if (prev_string[0] != '\0' ) { prev_string[length] = curr_string[0]; prev_string[length+1] = '\0'; insert_dict(prev_string,dict); } strcpy(prev_string, curr_string); input = getCode(BPC,in); } else { /* we must figure out the string ourselves */ /* the string must be same as prev_string */ /* followed by the first character of */ /* prev_string */ prev_string[length] = prev_string[0]; prev_string[length+1] = '\0'; /* insert this string into the dictionary */ /* and output is as well */ fprintf(out,"%s",prev_string); insert_dict(prev_string,dict); input = getCode(BPC,in); } } /* while ... */ }