/* This function is used to determine the file size of * two input files * * Inputs: * infilename1 the filename for input file 1 * infilename2 the filename for input file 2 * * Return: * 1 if the two files have the same size * 0 if the two files have a different size */ int verifyDecompressedFile(char *infilename1, char *infilename2) { unsigned int value1, value2; int counter1 = -1, counter2 = -1; InStream *in1, *in2; in1 = initInStream(); in2 = initInStream(); in1 = openInStream(infilename1, "rb+" , in1); in2 = openInStream(infilename2, "rb+" , in2); do { value1 = streamReadBits(in1, 8); counter1++; }while(!checkEndOfFile(in1) ); do { value2 = streamReadBits(in2, 8); counter2++; }while(!checkEndOfFile(in2) ); closeInStream(in1); closeInStream(in2); freeInStream(in1); freeInStream(in2); if( counter2 == counter1) return 1; else return 0; }
void assertFile(char *Source, char *Target,int lineNumber) { long unsigned int srcFileSize, targetFileSize, position ; unsigned int srcData,targetData ,srcfileStatus = 0,targetFileStatus = 0; char ErrorMsg[1024] ={}; InStream *src = initInStream(); InStream *target = initInStream(); src = openInStream(Source, "rb" , src); target = openInStream(Target, "rb" , target); fseek(src->file,0,SEEK_END); fseek(target->file,0,SEEK_END); srcFileSize = ftell(src->file); targetFileSize = ftell(target->file); UNITY_TEST_ASSERT_EQUAL_UINT32(srcFileSize,targetFileSize,lineNumber,"The target file size does not match the source file size!"); rewind(src->file); rewind(target->file); while(1) { srcData = streamReadBits(src,8); targetData = streamReadBits(target,8); srcfileStatus = checkEndOfFile(src) ; targetFileStatus = checkEndOfFile(target); if(srcfileStatus) { closeInStream(src); freeInStream(src); } if(targetFileStatus) { closeInStream(target); freeInStream(target); } UNITY_TEST_ASSERT_EQUAL_UINT8(srcfileStatus,targetFileStatus,lineNumber,"Target file ended earlier or later than the source file!"); if (srcData != targetData) { position = ftell(target->file); sprintf(ErrorMsg,"At position 0x%x, target data does not match source data!",position); UNITY_TEST_ASSERT_EQUAL_UINT8(srcData,targetData,lineNumber,ErrorMsg); } if(srcfileStatus == 1 || targetFileStatus == 1) break; } }
QVariant parseAtom(ParserState &st) { checkEndOfFile(st); if (isalpha(*st.ptr)) { return parseIdentifier(st); } else if (isdigit(*st.ptr)) { return parseNumber(st); } else if (*st.ptr == '-') { st.ptr++; return -parseNumber(st); } else if (*st.ptr == '"') { return parseString(st); } else { throw ParserError(QString("unexpected character '%1'").arg(*st.ptr)); } }
/* Continuously read byte and find last match of the data in the dictionary * * Input : dictionary : dictionary is the pointer to the LZ78 dictionary which contains the data to be compared * in : in is the pointer to InStream for reading a byte purpose * dataString : dataString is used later to add the missing data into the dictionary * dataStringSize : dataStringSize is used to store the size of the string and will be recorded into dictionaryEntry * readByte : readByte is used to output the byte of data in LZ78_Output * returnedIndex : returnedIndex contains the first index of the match dictionaryEntry * EOFstate : EOFstate is used to remember EOF has been encountered * * Output : Return index of last match entry * */ int findLastMatchEntry(Dictionary *dictionary, InStream *in,unsigned char *dataString,int *dataStringSize,unsigned char *readByte, int returnedIndex, int *EOFstate) { int lastIndex = returnedIndex ; // store the index of first match in dictionaryEntry memset (dataString,0,1024); //clear dataString copy_DictionaryDataInputData(dataString,dictionary,lastIndex); //merge input character with data in dictionary *dataStringSize = dictionary->Entry[lastIndex].entrySize ; // get the size of dataString while(returnedIndex != -1) { readByte[0] = (unsigned char)(streamReadBits(in,8)) ;// read next character if (checkEndOfFile(in)) //if EOF detected { *EOFstate = 1 ; // use to remember EOF encountered for later uses returnedIndex = -1 ; //quit loop } else { memcpy( (dataString+*dataStringSize),readByte,1); //add next character to dataString *dataStringSize += 1; //increment dataStringSize returnedIndex = compare_DictionaryData(dataString,dictionary,*dataStringSize); //check again is there any matched data if (returnedIndex != -1 ) // if there is still existing a match in dictionaryEntry lastIndex = returnedIndex ; // store the index of last match in dictionaryEntry } } return lastIndex ; }
QVariantList parseList(ParserState &st) { skipSpaces(st); checkEndOfFile(st); if (*st.ptr != '(') { throw ParserError("list must be started with '('"); } st.ptr++; QVariantList list; QString i = parseIdentifier(st); list.append(i); while (st.ptr < st.end) { if (*st.ptr == ')') { st.ptr++; return list; } parseSpace(st); if (*st.ptr == ')') { st.ptr++; return list; } if (*st.ptr == '(') { QVariant v = parseList(st); list.append(v); } else { list.append(parseAtom(st)); } } throw ParserError("unexpected end of file"); }
void parseSpace(ParserState &st) { checkEndOfFile(st); if (!isspace(*st.ptr)) { throw ParserError("space expected"); } while (st.ptr < st.end) { if (isspace(*st.ptr)) { st.ptr++; } else break; } }
/* Modified LZ78 Compressor * * Index 0 is used to represent EOF for decompression usage * Index 1 is used to represent data not found in dictionary * * Input : dictionary : dictionary is the pointer to the LZ78 dictionary * in : in is the pointer to InStream * out : out is the pointer to OutStream * mode : Fixed -> fixed output dictionary index to 16bits * Variable -> use just sufficient number of bits to represent the dictionary index */ void LZ78_Compressor(Dictionary *dictionary, InStream *in, OutStream *out, int mode) { unsigned char readByte[2] ={}, dataString[1024] ; int returnedIndex, lastIndex, EOFstate = 0 , i = 0 ,dataStringSize; while (1) { readByte[0] = (unsigned char)(streamReadBits(in,8)); if (checkEndOfFile(in)) // if EOF encountered break; // break loop if(isDictionaryFull(dictionary)) refreshDictionaryEntryData(dictionary,dictionary->dictionarySize); if(isDictionaryEmpty(dictionary)) // if dictionary is empty { addEntryData(dictionary, readByte,1); // directly add it into dictionary LZ78_Output(dictionary,out,readByte[0],1,EOFstate,mode); // output (1,x) *without () } else // dictionary is not empty { returnedIndex = compare_DictionaryData(readByte,dictionary,1); //check is there any matched data in dictionaryEntry if ( returnedIndex >= 0 ) // if true { lastIndex = findLastMatchEntry(dictionary,in,dataString,&dataStringSize,readByte,returnedIndex, &EOFstate); if (EOFstate != 1)//prevent adding EOF into dictionary addEntryData(dictionary,dataString,dataStringSize); // add dataString into dictionary LZ78_Output(dictionary,out,readByte[0],lastIndex+2,EOFstate,mode); // produce output (lastIndex+1 , X) *without () } else // no matched data { addEntryData(dictionary,readByte,1); LZ78_Output(dictionary,out,readByte[0],1,EOFstate,mode); // output (1,x) *without () } } if (EOFstate == 1) //EOF encountered previously break; } }
QString parseString(ParserState &st) { QString s; checkEndOfFile(st); if (*st.ptr != '"') { throw ParserError("string must begin with quote"); } s += *st.ptr++; while (st.ptr < st.end) { char c = *st.ptr++; s += c; if (c == '"') return s; } throw ParserError("unexpected end of file"); }
QString parseIdentifier(ParserState &st) { checkEndOfFile(st); QString s; if (!isalpha(*st.ptr)) { throw ParserError("identifier must begin with alpha character"); } s += *st.ptr++; while (st.ptr < st.end) { char c = *st.ptr; if (isalnum(c) || (c == '_')) { s += c; st.ptr++; } else break; } return s; }
qint64 parseNumber(ParserState &st) { checkEndOfFile(st); qint64 v = 0; if (!isdigit(*st.ptr)) { throw ParserError("number must begin with digit"); } while (st.ptr < st.end) { char c = *st.ptr; if (isdigit(c)) { v *= 10; v += c - '0'; st.ptr++; } else break; } return v; }