1

我只使用 C++ 大约 6 个月,所以对于我在代码中犯的任何愚蠢错误,我深表歉意。

我正在开发一个项目,该项目会将二进制文件读入streambuf,然后在streambuf中搜索特定序列并报告二进制文件中的序列起始偏移量以及在二进制文件中找到特定序列的标识符。

要搜索的序列存储在一个称为 snortRules 的向量中,其中 snortRules 向量的每个索引都是存储为整数向量的单个序列。

最终,我需要创建一个文本文件,该文件将显示找到的偏移量和特定序列,以便与另一个应该匹配的文件进行比较,但现在我只是将结果打印到控制台窗口。

我的代码似乎正在工作,因为它报告找到了除单个问题之外的所有序列。出于某种原因,我有时会重复找到序列的偏移量。例如输出将是:

101521 #655
101816 #656    <--This output is correct
101816 #657    <--This output is wrong and should have offset 101865 
101893 #658    <--This output is correct
101893 #659    <--This output is wrong and should have offset 102084
102105 #660
102325 #661
102378 #662

我认为我的问题是我没有使用 streambuf 的 pubseekoff 正确获得偏移量。

我的代码是:

fstream binFS(binName, ios::in | ios::binary); //create stream 
streambuf* binBuff = binFS.rdbuf(); //create streambuf from filestream
int currentChar; //ascii code of current character from binary file
streamsize offset; //location of first character of rule in binary file
do
{ //until the end of binary file
    for(unsigned i=0; i<snortRules.size(); ++i)
    {//start by finding the begining of current rule    
        do
        { //check binBuff until first char of first rule is found.
            currentChar = binBuff->sgetc();
            if( snortRules[i][0] == currentChar )//binBuff->sgetc() ) //compare short rule 1st char to curtent position 
            { //match save offset and compare rest of rule
                offset = binBuff->pubseekoff(0,ios::cur); //set the offset to current postion
                bool checkNext = true; //bool to break loop when checking rules if not matching
                for(unsigned srIdx=0; srIdx < snortRules[i].size() && checkNext == true; ++srIdx)
                { //loop through current rule comparing characters                        
                    if(snortRules[i][srIdx] == currentChar)
                    { //match check if end of rule or advance character to compare to next in rule
                        if( srIdx == snortRules[i].size()-1)
                        { //match write the offset of fist character of rule and which was matched
                            std::cout<<offset<<" #"<<i+1<<endl; //write to console for debugging purposes will write to text file when working
                            ++i; //increment rule to next when exact match found
                        }
                        else
                        { //not at the end of the rule so continue to compare next characters
                            currentChar = binBuff->snextc(); //set currentChar to next char to be checked by for loop
                        }
                    }
                    else
                    { //no match break out and continue 
                        checkNext == false; //set flag to break for loop because characters didnt match
                    }
                }
            }
            else
            { //no match check next character in binBuff
                //offset = binBuff->pubseekoff (0,binFS.cur, binFS.in); //set the offset to current postion
                currentChar = binBuff->snextc(); //get next char in binBuff
                //offset = binBuff->pubseekoff (0,binFS.cur, binFS.in); //set the offset to current postion
            }
        }while( currentChar != streambuf::traits_type::eof() ); //stop checking if end of file reached
    }
}while( binBuff->snextc() != streambuf::traits_type::eof() ); //stop checking if end of file reached

我试图在 stackoverflow.com 和 cplusplus.com 上找到 pubseekoff 的文档和示例,但我能找到的对我来说没有多大意义,老实说,我不是 100% 确定这是我的问题......

非常感谢任何帮助或建议!

4

1 回答 1

1

整个问题是这条线:

checkNext == false;

“==”无效并导致错误,因为它需要:

checkNext = false;

这样它就会分配布尔值并在适当的时间终止循环。

我忽略了 Visual Studio 中编译器发出的警告,追了几个小时,现在我觉得自己像个傻瓜。

因此,如果您需要相关的 streambuf 示例,这可能会有所帮助。如果您遇到一些没有任何意义的随机运行时错误,请检查警告。

于 2013-04-27T14:08:39.833 回答