代码解析 static int32_t CompareFileContent() { int32_t ret = -1; ifstream infile1, infile2; infile1.open("/data/test_aes.txt", ios::in|ios::binary); infile1.seekg (0, infile1.end); uint32_t length1 = infile1.tellg(); infile1.seekg (0, infile1.beg); infile2.open("/data/test_aes_new.txt", ios::in|ios::binary); infile2.seekg (0, infile2.end); uint32_t length2 = infile2.tellg(); infile2.seekg (0, infile2.beg); if (length1 != length2) { return ret; } uint8_t buffer1[FILE_BLOCK_SIZE] = {0}; uint8_t buffer2[FILE_BLOCK_SIZE] = {0}; for (uint32_t i = 0; i < length1 / FILE_BLOCK_SIZE; i++) { infile1.read(reinterpret_cast<char *>(buffer1), FILE_BLOCK_SIZE); infile2.read(reinterpret_cast<char *>(buffer2), FILE_BLOCK_SIZE); ret = memcmp(buffer1, buffer2, FILE_BLOCK_SIZE); if (ret != 0) { goto CLEAR_UP; } } CLEAR_UP: infile1.close(); infile2.close(); return ret; }
时间: 2024-04-03 09:37:24 浏览: 89
这段代码的作用是比较两个文件的内容是否相同。
首先,代码打开两个文件`/data/test_aes.txt`和`/data/test_aes_new.txt`,并获取它们的长度。
然后,代码比较两个文件的长度是否相同,如果长度不同,说明文件内容不同,返回`-1`。
接下来,代码定义两个缓冲区`buffer1`和`buffer2`,大小为`FILE_BLOCK_SIZE`,并循环读取两个文件的数据到这两个缓冲区中,再通过`memcmp`函数比较两个缓冲区的内容是否相同。如果不同,则说明文件内容不同,返回`-1`,否则继续循环。
最后,代码关闭两个文件句柄并返回比较结果。
相关问题
代码解析 static int32_t AesMultiBlockEncrypt(HcfCipher *cipher, HcfSymKey *key, HcfParamsSpec *params) { HcfBlob output = {}; ifstream infile; ofstream outfile; infile.open("/data/test_aes.txt", ios::in|ios::binary); infile.seekg (0, infile.end); uint32_t length = infile.tellg(); infile.seekg (0, infile.beg); uint8_t buffer[1024] = {0}; outfile.open("/data/test_aes_enc.txt", ios::out|ios::binary); HcfBlob input = {.data = (uint8_t *)buffer, .len = FILE_BLOCK_SIZE}; uint32_t count = length / FILE_BLOCK_SIZE; int32_t ret = cipher->init(cipher, ENCRYPT_MODE, (HcfKey *)key, params); if (ret != 0) { LOGE("init failed! %d", ret); goto CLEAR_UP; } for (uint32_t i = 0; i < count; i++) { infile.read(reinterpret_cast<char *>(buffer), FILE_BLOCK_SIZE); ret = cipher->update(cipher, &input, &output); if (ret != 0) { LOGE("update failed!"); goto CLEAR_UP; } if (output.data != nullptr && output.len > 0) { outfile.write(reinterpret_cast<const char *>(output.data), output.len); } if (output.data != nullptr) { HcfFree(output.data); output.data = nullptr; } } ret = cipher->doFinal(cipher, nullptr, &output); if (ret != 0) { LOGE("doFinal failed!"); goto CLEAR_UP; } if (output.data != nullptr && output.len > 0) { outfile.write((const char *)output.data, output.len); } if (output.data != nullptr) { HcfFree(output.data); output.data = nullptr; } CLEAR_UP: outfile.close(); infile.close(); return ret; }
这段代码使用了一个加密库(`cipher`)对一个文件进行多块加密,加密后将结果写入到另一个文件中。
首先,代码打开一个文件`/data/test_aes.txt`,获取它的长度,并定义一个大小为`1024`的缓冲区`buffer`。另外,代码打开一个输出文件`/data/test_aes_enc.txt`,以便将加密后的结果写入其中。
然后,代码根据加密算法、密钥和参数初始化加密库,并循环读取`count`个块的数据到缓冲区中,再将缓冲区的内容加密并输出到输出缓冲区`output`中。如果加密失败,代码会打印错误信息并跳转到`CLEAR_UP`标签处执行清除操作。
接下来,代码调用`doFinal`函数对最后一块数据进行加密,并将结果输出到输出缓冲区`output`中。如果加密失败,代码会打印错误信息并跳转到`CLEAR_UP`标签处执行清除操作。
最后,代码关闭输入和输出文件句柄并返回加密结果。
/// TODO: print each path once this method is called, and /// (1) add each path (a sequence of node IDs) as a string into std::set<std::string> paths /// in the format "START: 1->2->4->5->END", where -> indicate an ICFGEdge connects two ICFGNode IDs /// bonus: dump and append each program path to a `ICFGPaths.txt` in the form of /// ‘{ln: number cl: number, fl:name} -> {ln: number, cl: number, fl: name} -> {ln:number, cl: number, fl: name} /// ln : line number cl: column number fl:file name for further learning, you can review the code in SVF, SVFUtil void TaintGraphTraversal::printICFGPath(std::vector<const ICFGNode *> &path){ } // TODO: Implement your code to parse the two lines from `SrcSnk.txt` in the form of // line 1 for sources "{ api1 api2 api3 }" // line 2 for sinks "{ api1 api2 api3 }" void TaintGraphTraversal::readSrcSnkFromFile(const string& filename){ } /// TODO: Checking aliases of the two variables at source and sink. For example: /// src instruction: actualRet = source(); /// snk instruction: sink(actualParm,...); /// return true if actualRet is aliased with any parameter at the snk node (e.g., via ander->alias(..,..)) bool TaintGraphTraversal::aliasCheck(const CallICFGNode *src, const CallICFGNode *snk) { return true; } 这三个空分别应该填什么代码
第一个空:
```c++
void TaintGraphTraversal::printICFGPath(std::vector<const ICFGNode *> &path){
std::stringstream ss;
ss << "START: ";
for(auto node: path){
ss << node->getId();
if(node->getOutDegree() > 0){
ss << "->";
}
}
ss << "END";
std::string pathStr = ss.str();
paths.insert(pathStr);
std::ofstream file("ICFGPaths.txt", std::ios_base::app);
if(file.is_open()){
file << "{ln:" << path.front()->getInst()->getLine()
<< " cl:" << path.front()->getInst()->getColumn()
<< " fl:" << path.front()->getInst()->getFilename() << "}";
for(auto node: path){
file << " -> ";
file << "{ln:" << node->getInst()->getLine()
<< " cl:" << node->getInst()->getColumn()
<< " fl:" << node->getInst()->getFilename() << "}";
}
file << "\n";
file.close();
}
}
```
第二个空:
```c++
void TaintGraphTraversal::readSrcSnkFromFile(const string& filename){
std::ifstream file(filename);
if(file.is_open()){
std::string line1, line2;
std::getline(file, line1);
std::getline(file, line2);
std::regex re("\\{([^\\}]*)\\}");
std::smatch match;
std::vector<std::string> srcVec, snkVec;
while(std::regex_search(line1, match, re)){
srcVec.push_back(match[1]);
line1 = match.suffix().str();
}
while(std::regex_search(line2, match, re)){
snkVec.push_back(match[1]);
line2 = match.suffix().str();
}
file.close();
}
}
```
第三个空:
```c++
bool TaintGraphTraversal::aliasCheck(const CallICFGNode *src, const CallICFGNode *snk) {
const SVFFunction *srcFunc = src->getCallee();
const SVFFunction *snkFunc = snk->getCallee();
const SVFVariable *retVar = srcFunc->getRetVar();
std::vector<const SVFVariable *> paramVars = snkFunc->getParamVars();
for(auto paramVar: paramVars){
if(paramVar->alias(retVar)){
return true;
}
}
return false;
}
```
阅读全文