Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <fstream>
- #include <Windows.h>
- #include <string>
- #include <map>
- #include <ctime>
- #include <vector>
- using namespace std;
- ifstream in;
- ofstream out("report.txt");;
- int numIter = 1000;
- int blockSize = 0;
- string path, virus_mask;
- map<string, int> used;
- vector<int> z_function(const string &s) {
- int n = (int)s.length();
- vector<int> z(n, 0);
- for (int i = 1, l = 0, r = 0; i < n; i++) {
- if(i <= r)
- z[i] = min(z[i - l], r - i + 1);
- while(z[i] + i < n && s[z[i]] == s[i + z[i]])
- ++z[i];
- if(i + z[i] - 1 > r)
- l = i, r = i + z[i] - 1;
- }
- return z;
- }
- int isVirus(const string &s) {
- vector<int> z = z_function(virus_mask + '#' + s);
- int n = z.size();
- for (int i = 0; i < n; i++) {
- if (z[i] == virus_mask.length()) {
- return z[i];
- }
- }
- return z[n - 1];
- }
- bool CheckFile(string &s) {
- in.open(s);
- if(!in.is_open())
- return false;
- string virusTemp, buf;
- virusTemp.resize(blockSize);
- while(!in.fail()) {
- //virusTemp = new char [blockSize];
- in.read(&virusTemp[0], blockSize);
- buf += virusTemp;
- int temp = isVirus(buf);
- if(temp == virus_mask.length()) {
- in.close();
- //in.clear();
- return true;
- }
- else if(temp > 0) {
- buf = virus_mask.substr(0,temp);
- }
- }
- //in.clear();
- in.close();
- return false;
- }
- void CheckFolder(string path) {
- HANDLE hFindFile;
- WIN32_FIND_DATA FindFileData;
- //path += "*.*";
- hFindFile = FindFirstFile(path.c_str(), &FindFileData);
- if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- if(CheckFile(path.substr(0, path.length() - 3) + FindFileData.cFileName))
- cout << path.substr(0, path.length() - 3) << FindFileData.cFileName << endl;
- while (FindNextFile(hFindFile, &FindFileData)) {
- if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- if(CheckFile(path.substr(0, path.length() - 3) + FindFileData.cFileName))
- cout << path.substr(0, path.length() - 3) << FindFileData.cFileName << endl;
- }
- //FindClose(hFindFile);
- }//check folder's files on virus
- void Pop(string &s) {
- s.pop_back();
- s.pop_back();
- s.pop_back();
- }
- void ScanFolders(string lcPath) {
- HANDLE hFindFile;
- WIN32_FIND_DATA FindFileData;
- lcPath += "\\*.*";
- string CurLcPath = lcPath;
- hFindFile = FindFirstFile(lcPath.c_str(), &FindFileData);
- while(FindNextFile(hFindFile, &FindFileData)) {
- if(FindFileData.cFileName[0] == '.' || FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- continue;
- if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && lcPath[lcPath.length() - 1] == '*')
- Pop(lcPath);
- ++used[lcPath + FindFileData.cFileName + "\\*.*"];
- if (used[lcPath + FindFileData.cFileName + "\\*.*"] == 1) {
- ScanFolders(lcPath + FindFileData.cFileName);
- //cout << FindFileData.cFileName << endl;
- //lcPath += "\*.*";
- }
- }
- CheckFolder(CurLcPath);
- } // going by folders
- int main(int argc, char * argv[]) {
- clock_t start = clock();
- if(argc == 1) {
- cout << "Input Directory:" << endl;
- cin >> path;
- cout << "Input size of reading block" << endl;
- cin >> blockSize;
- cout << "Input virus's template" << endl;
- cin >> virus_mask;
- }
- else {
- path = argv[1];
- blockSize = atoi(argv[2]);
- virus_mask = argv[3];
- }
- ScanFolders(path);
- cout << "It took me " << (float)(clock() - start) / (CLOCKS_PER_SEC) << " seconds"<< endl;
- out << blockSize << " " << (float)(clock() - start) / (CLOCKS_PER_SEC) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment