prestige

ShahnovskiyLab1

Sep 24th, 2012
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <Windows.h>
  5. #include <string>
  6. #include <map>
  7. #include <ctime>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. ifstream in;
  12. ofstream out("report.txt");;
  13.  
  14. int numIter = 1000;
  15. int blockSize = 0;
  16. string path, virus_mask;
  17. map<string, int> used;
  18.  
  19. vector<int> z_function(const string &s) {
  20.     int n = (int)s.length();
  21.     vector<int> z(n, 0);
  22.     for (int i = 1, l = 0, r = 0; i < n; i++) {
  23.         if(i <= r)
  24.             z[i] = min(z[i - l], r - i + 1);
  25.         while(z[i] + i < n && s[z[i]] == s[i + z[i]])
  26.             ++z[i];
  27.         if(i  + z[i] - 1 > r)
  28.             l = i, r = i + z[i] - 1;
  29.     }
  30.     return z;
  31. }
  32.  
  33. int isVirus(const string &s) {
  34.     vector<int> z = z_function(virus_mask + '#' + s);
  35.     int n = z.size();
  36.     for (int i = 0; i < n; i++) {
  37.         if (z[i] == virus_mask.length()) {
  38.             return z[i];
  39.         }
  40.     }
  41.     return z[n - 1];
  42. }
  43.  
  44. bool CheckFile(string &s) {
  45.     in.open(s);
  46.     if(!in.is_open())
  47.         return false;
  48.     string virusTemp, buf;
  49.     virusTemp.resize(blockSize);
  50.     while(!in.fail()) {
  51.         //virusTemp = new char [blockSize];
  52.         in.read(&virusTemp[0], blockSize);
  53.         buf += virusTemp;
  54.         int temp = isVirus(buf);
  55.         if(temp == virus_mask.length()) {
  56.             in.close();
  57.             //in.clear();
  58.             return true;
  59.         }
  60.         else if(temp > 0) {
  61.             buf = virus_mask.substr(0,temp);
  62.         }
  63.     }
  64.     //in.clear();
  65.     in.close();
  66.     return false;
  67. }
  68.  
  69. void CheckFolder(string path) {
  70.     HANDLE hFindFile;
  71.     WIN32_FIND_DATA FindFileData;
  72.     //path += "*.*";
  73.     hFindFile = FindFirstFile(path.c_str(), &FindFileData);
  74.     if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
  75.         if(CheckFile(path.substr(0, path.length() - 3) + FindFileData.cFileName))
  76.             cout << path.substr(0, path.length() - 3) << FindFileData.cFileName << endl;
  77.     while (FindNextFile(hFindFile, &FindFileData)) {
  78.         if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
  79.             if(CheckFile(path.substr(0, path.length() - 3) + FindFileData.cFileName))
  80.                 cout << path.substr(0, path.length() - 3) << FindFileData.cFileName << endl;
  81.     }
  82.     //FindClose(hFindFile);
  83. }//check folder's files on virus
  84.  
  85. void Pop(string &s) {
  86.     s.pop_back();
  87.     s.pop_back();
  88.     s.pop_back();
  89. }
  90.  
  91. void ScanFolders(string lcPath) {
  92.     HANDLE hFindFile;
  93.     WIN32_FIND_DATA FindFileData;
  94.     lcPath += "\\*.*";
  95.     string CurLcPath = lcPath;
  96.     hFindFile = FindFirstFile(lcPath.c_str(), &FindFileData);
  97.     while(FindNextFile(hFindFile, &FindFileData)) {
  98.         if(FindFileData.cFileName[0] == '.' || FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
  99.             continue;
  100.         if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && lcPath[lcPath.length() - 1] == '*')
  101.             Pop(lcPath);
  102.         ++used[lcPath + FindFileData.cFileName + "\\*.*"];
  103.         if (used[lcPath + FindFileData.cFileName + "\\*.*"] == 1) {
  104.             ScanFolders(lcPath + FindFileData.cFileName);
  105.             //cout << FindFileData.cFileName << endl;
  106.             //lcPath += "\*.*";
  107.         }
  108.     }
  109.     CheckFolder(CurLcPath);
  110. } // going by folders
  111.  
  112. int main(int argc, char * argv[]) {
  113.     clock_t start = clock();
  114.     if(argc == 1) {
  115.         cout << "Input Directory:" << endl;
  116.         cin >> path;
  117.         cout << "Input size of reading block" << endl;
  118.         cin >> blockSize;
  119.         cout << "Input virus's template" << endl;
  120.         cin >> virus_mask;
  121.     }
  122.     else {
  123.         path = argv[1];
  124.         blockSize = atoi(argv[2]);
  125.         virus_mask = argv[3];
  126.     }
  127.     ScanFolders(path);
  128.     cout << "It took me " << (float)(clock() - start) / (CLOCKS_PER_SEC) << " seconds"<< endl;
  129.     out << blockSize << " " << (float)(clock() - start) / (CLOCKS_PER_SEC) << endl;
  130.     return 0;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment