Yonka2019

Yonka-Final-AntiVirusProject.c

May 20th, 2021 (edited)
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.91 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7. #include <dirent.h>
  8.  
  9. #define ARGS_COUNT 2 // Not including the program name
  10. #define PERCENT_20 0.2 // 20%
  11. #define PERCENT_80 0.8 // 80%
  12. #define PERCENT_100 1 // 100%
  13. #define BUFFER 1000 // Buffer for the logger (log builder)
  14.  
  15. void writeLog();
  16. void myExit(int code);
  17. void checkArgs(int argc);
  18. void chooseAndScan(char** argv);
  19. void printWelcome(char** argv);
  20. void freeMemory(char** filesList, int filesNum);
  21. void sortFiles(char** filesList, int filesNum);
  22. void normalScan(char** filesList, char* virusSign, int filesNum);
  23. void quickScan(char** filesList, char* virusSign, int filesNum);
  24. int nextOne(FILE* file);
  25. int countFiles(char* folderDirectory);
  26. int getFiles(char* folderDirectory, char** filesList);
  27. bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo);
  28.  
  29. enum scanTypes
  30. {
  31.     normalScanType, // 0
  32.     quickScanType // not 0
  33. } scanType;
  34.  
  35. //String builder that will copied later into Log File (func writeLog())
  36. char logBuilder[BUFFER] = { 0 };
  37.  
  38. int main(int argc, char** argv)
  39. {
  40.     checkArgs(argc);
  41.     /*
  42.     * argv[0] = Program Name
  43.     * argv[1] = Folder to scan
  44.     * argv[2] = Virus signature
  45.     */
  46.  
  47.     printWelcome(argv);
  48.  
  49.     chooseAndScan(argv);
  50.  
  51.     writeLog();
  52.  
  53.     getchar();
  54.     return 0;
  55.  
  56. }
  57. /// <summary>
  58. /// Checks if the arguments of the program correctly
  59. /// </summary>
  60. /// <param name="argc">Number of the given arguments</param>
  61. void checkArgs(int argc)
  62. {
  63.     if (argc != ARGS_COUNT + 1)
  64.     {
  65.         printf("Invalid execution.\nUsage:\n"
  66.             "> Antivirus <Folder_to_scan [directory]> <Virus_signature [directory]>\n");
  67.  
  68.         myExit(1);
  69.     }
  70. }
  71. /// <summary>
  72. /// Prints the welcome screen with the given information (folder to scan, virus sign.)
  73. /// </summary>
  74. /// <param name="argv">Arguments that was passed with the program</param>
  75. void printWelcome(char** argv)
  76. {
  77.     printf("Welcome to my Virus Scan!\n\n");
  78.  
  79.     printf("Folder to scan: %s\n"
  80.         "Virus signature: %s\n\n", argv[1], argv[2]);
  81.  
  82.     strcat(logBuilder, "Anti-virus began! Welcome!\n\n");
  83.     strcat(logBuilder, "Folder to scan:\n");
  84.     strcat(logBuilder, argv[1]);
  85.     strcat(logBuilder, "\nVirus signature:\n");
  86.     strcat(logBuilder, argv[2]);
  87.     strcat(logBuilder, "\n\n");
  88. }
  89. /// <summary>
  90. /// The user choses which type of scanning he prefer, and begin the scan
  91. /// </summary>
  92. /// <param name="argv">Arguments that was passed with the program</param>
  93. void chooseAndScan(char** argv)
  94. {
  95.     char** filesList = (char**)malloc(sizeof(char*) * countFiles(argv[1]));
  96.     if (filesList == NULL)
  97.     {
  98.         printf("Unsuccessful malloc!\n");
  99.  
  100.         myExit(1);
  101.     }
  102.  
  103.     int filesNum = getFiles(argv[1], filesList);
  104.     sortFiles(filesList, filesNum); // sort alpha-betical
  105.  
  106.     printf("Press 0 for a normal scan or any other key for quick scan: ");
  107.     scanf("%d", &scanType);
  108.     getchar();
  109.  
  110.  
  111.     strcat(logBuilder, "Scanning option:\n");
  112.  
  113.     printf("Scanning began...\n"
  114.         "This process may take several minutes...\n\n");
  115.  
  116.     switch (scanType)
  117.     {
  118.     case normalScanType: // 0
  119.     {
  120.         strcat(logBuilder, "Normal Scan\n\n");
  121.         normalScan(filesList, argv[2], filesNum);
  122.         break;
  123.     }
  124.     default: // quickScanType (not 0)
  125.     {
  126.         strcat(logBuilder, "Quick Scan\n\n");
  127.         quickScan(filesList, argv[2], filesNum);
  128.         break;
  129.     }
  130.     }
  131.     printf("Scan Completed.\n");
  132.  
  133.     freeMemory(filesList, filesNum);
  134. }
  135. /// <summary>
  136. /// Writes the contect of the builder string (logBuilder) into the file (AntiVirusLog.txt)
  137. /// </summary>
  138. void writeLog()
  139. {
  140.     FILE* fileLog = fopen("AntiVirusLog.txt", "w+");
  141.     if (fileLog == NULL)
  142.     {
  143.         printf("Error to open the file\n");
  144.         myExit(1);
  145.     }
  146.  
  147.     fputs(logBuilder, fileLog);
  148.  
  149.     fclose(fileLog);
  150. }
  151. /// <summary>
  152. /// Checks if the given fileToCheck infected with the given virus signature
  153. /// </summary>
  154. /// <param name="fileToCheck">File to check</param>
  155. /// <param name="virusSign">Virus signature</param>
  156. /// <param name="scanFrom">Scans from (from which pos. scan the fileToCheck)</param>
  157. /// <param name="scanTo">Scans until (till which pos. scan the fileToCheck)</param>
  158. /// <returns>True if infected, false if not</returns>
  159. bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo)
  160. {
  161.     int checkData = 0, virusData = 0;
  162.     double fileSize = 0;
  163.  
  164.     FILE* checkFile = fopen(fileToCheck, "rb");
  165.     if (checkFile == NULL)
  166.     {
  167.         printf("Error opening file: %s\n", fileToCheck);
  168.  
  169.         myExit(1);
  170.     }
  171.  
  172.     FILE* virusFile = fopen(virusSign, "rb");
  173.     if (virusFile == NULL)
  174.     {
  175.         fclose(checkFile);
  176.         printf("Error opening file: %s\n", virusSign);
  177.  
  178.         myExit(1);
  179.     }
  180.  
  181.     fseek(checkFile, 0L, SEEK_END);
  182.     fileSize = ftell(checkFile); // gets the file "size"
  183.     fseek(checkFile, 0L, SEEK_SET);
  184.  
  185.     /*
  186.      * 0.2 - 20% || 0.8 - 80% || 1 = 100%
  187.      * from 0 to 0.2 -> first 20%
  188.      * from 0.8 to 1 -> last 20% (1 - 8 = 0.2 ~ 20%)
  189.      */
  190.  
  191.     while ((checkData = fgetc(checkFile)) != EOF) {
  192.         if ((fileSize * scanFrom <= ftell(checkFile)) && (fileSize * scanTo >= ftell(checkFile))) // scan only in the given range
  193.         {
  194.             virusData = fgetc(virusFile);
  195.             if (nextOne(virusFile) == EOF) // Check if the virus sign FULLY exists in the given file to check
  196.             {
  197.                 fclose(checkFile);
  198.                 fclose(virusFile);
  199.  
  200.                 return true;
  201.             }
  202.             else if (virusData != checkData) // If the virus data doesn't equals to the given file data, "reset" the data "counter"
  203.             {
  204.                 fseek(virusFile, 0, SEEK_SET); // start over the virus data
  205.             }
  206.         }
  207.     }
  208.  
  209.     fclose(checkFile);
  210.     fclose(virusFile);
  211.  
  212.     return false;
  213. }
  214. /// <summary>
  215. /// Normal scan the files in the given folder according the virus signature
  216. /// </summary>
  217. /// <param name="virusSign">Virus signature</param>
  218. /// <param name="filesList">List of the files to check in the folder</param>
  219. /// <param name="filesNum">Number of the files to check</param>
  220. void normalScan(char** filesList, char* virusSign, int filesNum)
  221. {
  222.     int i = 0;
  223.  
  224.     strcat(logBuilder, "Results:\n");
  225.  
  226.     printf("Scanning:\n");
  227.     for (i = 0; i < filesNum; i++)
  228.     {
  229.         strcat(logBuilder, filesList[i]);
  230.         if (isInfected(filesList[i], virusSign, 0, PERCENT_100))
  231.         {
  232.             strcat(logBuilder, " Infected!\n");
  233.             printf("%s - Infected!\n", filesList[i]);
  234.         }
  235.         else
  236.         {
  237.             strcat(logBuilder, " Clean\n");
  238.             printf("%s - Clean\n", filesList[i]);
  239.         }
  240.     }
  241.  
  242. }
  243. /// <summary>
  244. /// Quick scan the files in the given folder according the virus signature
  245. /// </summary>
  246. /// <param name="virusSign">Virus signature</param>
  247. /// <param name="filesList">List of the files to check in the folder</param>
  248. /// <param name="filesNum">Number of the files to check</param>
  249. void quickScan(char** filesList, char* virusSign, int filesNum)
  250. {
  251.     int i = 0;
  252.  
  253.     strcat(logBuilder, "Results:\n");
  254.  
  255.     printf("Scanning:\n");
  256.     for (i = 0; i < filesNum; i++)
  257.     {
  258.         strcat(logBuilder, filesList[i]);
  259.         if (isInfected(filesList[i], virusSign, 0, PERCENT_20))
  260.         {
  261.             strcat(logBuilder, " Infected! (first 20%)\n");
  262.             printf("%s - Infected! (first 20%%)\n", filesList[i]);
  263.         }
  264.         else if (isInfected(filesList[i], virusSign, PERCENT_80, PERCENT_100))
  265.         {
  266.             strcat(logBuilder, " Infected! (last 20%)\n");
  267.             printf("%s - Infected! (last 20%%)\n", filesList[i]);
  268.         }
  269.         else if (isInfected(filesList[i], virusSign, 0, PERCENT_100))
  270.         {
  271.             strcat(logBuilder, " Infected!\n");
  272.             printf("%s - Infected!\n", filesList[i]);
  273.         }
  274.         else
  275.         {
  276.             strcat(logBuilder, " Clean\n");
  277.             printf("%s - Clean\n", filesList[i]);
  278.         }
  279.     }
  280. }
  281. /// <summary>
  282. /// Counts the number of the files in the given folder directory
  283. /// </summary>
  284. /// <param name="folderDirectory">Folder directory to count the files</param>
  285. /// <returns>Number of files in the given directory</returns>
  286. int countFiles(char* folderDirectory)
  287. {
  288.     int counter = 0;
  289.     DIR* d = 0;
  290.     struct dirent* dir = 0;
  291.  
  292.     d = opendir(folderDirectory);
  293.  
  294.     if (d == NULL)
  295.     {
  296.         printf("Error opening directory: %s\n", folderDirectory);
  297.  
  298.         myExit(1);
  299.     }
  300.  
  301.     while ((dir = readdir(d)) != NULL)
  302.     {
  303.         if (strcmp(dir->d_name, ".") &&
  304.             strcmp(dir->d_name, "..") &&
  305.             dir->d_type != DT_DIR)
  306.         {
  307.             counter++;
  308.         }
  309.     }
  310.     closedir(d);
  311.  
  312.     return counter;
  313. }
  314. /// <summary>
  315. /// Frees the dynimaclly allocated memory
  316. /// </summary>
  317. /// <param name="filesList">List (String Array) of the files</param>
  318. /// <param name="filesNum">Number of the files in the list</param>
  319. void freeMemory(char** filesList, int filesNum)
  320. {
  321.     int i = 0;
  322.  
  323.     for (i = 0; i < filesNum; i++)
  324.     {
  325.         free(filesList[i]);
  326.     }
  327.  
  328.     free(filesList);
  329. }
  330. /// <summary>
  331. /// Custom exit with getchar() made-in
  332. /// </summary>
  333. /// <param name="code">Code to exit with</param>
  334. void myExit(int code)
  335. {
  336.     getchar();
  337.     exit(code);
  338. }
  339. /// <summary>
  340. /// Returns the files into filesList (String Array)
  341. /// </summary>
  342. /// <param name="folderDirectory">Get files from this folder</param>
  343. /// <param name="filesList">Where to storage the files names</param>
  344. /// <returns>Number of the files</returns>
  345. int getFiles(char* folderDirectory, char** filesList)
  346. {
  347.     int i = 0;
  348.     DIR* d = 0;
  349.     struct dirent* dir = 0;
  350.  
  351.     d = opendir(folderDirectory);
  352.  
  353.     if (d == NULL)
  354.     {
  355.         printf("Error opening directory: %s\n", folderDirectory);
  356.  
  357.         myExit(1);
  358.     }
  359.  
  360.     while ((dir = readdir(d)) != NULL)
  361.     {
  362.         if (strcmp(dir->d_name, ".") &&
  363.             strcmp(dir->d_name, "..") &&
  364.             dir->d_type != DT_DIR)
  365.         {
  366.             filesList[i] = (char*)malloc(sizeof(char) * (strlen(folderDirectory) + strlen(dir->d_name) + 3));
  367.             /* Why 3?
  368.             * 1 - "/"
  369.             * 2 - '\0' of folderDirectory
  370.             * 3 - '\0' of dir->d_name
  371.             * strlen(char* str) returns the length of the string WITHOUT the \0 which is +1 to the length that strlen() returns
  372.             */
  373.             if (filesList[i] == NULL)
  374.             {
  375.                 while (i)
  376.                 {
  377.                     i--;
  378.                     free(filesList[i]);
  379.                 }
  380.                 free(filesList);
  381.                 printf("Unsuccessful malloc!\n");
  382.  
  383.                 myExit(1);
  384.             }
  385.  
  386.             strcpy(filesList[i], folderDirectory);
  387.             strcat(filesList[i], "/");
  388.             strcat(filesList[i], dir->d_name);
  389.  
  390.             i++;
  391.         }
  392.     }
  393.     closedir(d);
  394.  
  395.     return i;
  396. }
  397. /// <summary>
  398. /// Reads the next data from the given file (only read and returns him, without moving the current position)
  399. /// </summary>
  400. /// <param name="file">File</param>
  401. /// <returns>Data (int) that was read from the file</returns>
  402. int nextOne(FILE* file)
  403. {
  404.     int position = ftell(file);
  405.     int data = fgetc(file); // get the "nextOne" data
  406.     fseek(file, position, SEEK_SET); // returns  to the position that was before the last move
  407.  
  408.     return data;
  409. }
  410. /// <summary>
  411. /// Sorts the files in the array alpha-betical
  412. /// </summary>
  413. /// <param name="filesList">List of the files (String array)</param>
  414. /// <param name="filesNum">Number of files in the list</param>
  415. void sortFiles(char** filesList, int filesNum)
  416. {
  417.     int i = 0, j = 0;
  418.     char* temp = 0;
  419.  
  420.     for (i = 0; i < filesNum; i++) //bubble sort strings
  421.     {
  422.         for (j = 0; j < filesNum - 1; j++)
  423.         {
  424.             if (strcmp(filesList[j], filesList[i]) > 0) //dictionary comparison
  425.             {
  426.                 //pointers swap
  427.                 temp = filesList[i];
  428.                 filesList[i] = filesList[j];
  429.                 filesList[j] = temp;
  430.             }
  431.         }
  432.     }
  433. }
  434.  
Add Comment
Please, Sign In to add comment