Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #include "dirent.h"
- #define ARGS_COUNT 2 // Not including the program name
- #define BUFFER 1000
- void writeLog();
- void myExit(int code);
- void chooseAndScan(char** argv);
- void printWelcome(char** argv);
- void checkArgs(int argc);
- void freeMemory(char** filesList, int filesNum);
- void normalScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum);
- void quickScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum);
- int nextOne(FILE* file);
- int getFiles(char* folderDirectory, char** filesList);
- int countFiles(char* folderDirectory);
- bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo);
- enum scanTypes
- {
- normalScanType,
- quickScanType
- } scanType;
- char logBuilder[BUFFER] = { 0 };
- int main(int argc, char** argv)
- {
- checkArgs(argc);
- /*
- * argv[0] = Program Name
- * argv[1] = Folder to scan
- * argv[2] = Virus signature
- */
- printWelcome(argv);
- chooseAndScan(argv);
- writeLog();
- getchar();
- return 0;
- }
- /// <summary>
- /// Checks if the arguments of the program correctly
- /// </summary>
- /// <param name="argc">Number of the given arguments</param>
- void checkArgs(int argc)
- {
- if (argc != ARGS_COUNT + 1)
- {
- printf("Invalid execution.\nUsage:\n"
- "> Antivirus <Folder_to_scan [directory]> <Virus_signature [directory]>\n");
- myExit(1);
- }
- }
- void printWelcome(char** argv)
- {
- printf("Welcome to my Virus Scan!\n\n");
- printf("Folder to scan: %s\n"
- "Virus signature: %s\n\n", argv[1], argv[2]);
- strcat(logBuilder, "Anti-virus began! Welcome!\n\n");
- strcat(logBuilder, "Folder to scan:\n");
- strcat(logBuilder, argv[1]);
- strcat(logBuilder, "\nVirus signature:\n");
- strcat(logBuilder, argv[2]);
- strcat(logBuilder, "\n\n");
- }
- void chooseAndScan(char** argv)
- {
- char** filesList = (char**)malloc(sizeof(char*) * countFiles(argv[1]));
- if (filesList == NULL)
- {
- printf("Unsuccessful malloc!\n");
- myExit(1);
- }
- int filesNum = getFiles(argv[1], filesList);
- printf("Press 0 for a normal scan or any other key for quick scan: ");
- scanf("%d", &scanType);
- getchar();
- strcat(logBuilder, "Scanning option:\n");
- printf("Scanning began...\n"
- "This process may take several minutes...\n\n");
- switch (scanType)
- {
- case normalScanType:
- {
- strcat(logBuilder, "Normal Scan\n\n");
- normalScan(argv[1], argv[2], filesList, filesNum);
- break;
- }
- default: // quickScanType
- {
- strcat(logBuilder, "Quick Scan\n\n");
- quickScan(argv[1], argv[2], filesList, filesNum);
- break;
- }
- }
- printf("Scan Completed.\n");
- freeMemory(filesList, filesNum);
- }
- void writeLog()
- {
- FILE* fileLog = NULL;
- switch (scanType)
- {
- case normalScanType:
- fileLog = fopen("LogExample.txt", "w+");
- break;
- default:
- fileLog = fopen("LogExampleQuick.txt", "w+");
- break;
- }
- if (fileLog == NULL)
- {
- printf("Error to open the file\n");
- exit(1);
- }
- fputs(logBuilder, fileLog);
- fclose(fileLog);
- }
- bool isInfected(char* fileToCheck, char* virusSign, double scanFrom, double scanTo)
- {
- int checkData = 0, virusData = 0;
- double fileSize = 0;
- FILE* checkFile = fopen(fileToCheck, "rb");
- if (checkFile == NULL)
- {
- printf("Error opening file: %s\n", fileToCheck);
- myExit(1);
- }
- FILE* virusFile = fopen(virusSign, "rb");
- if (virusFile == NULL)
- {
- fclose(checkFile);
- printf("Error opening file: %s\n", virusSign);
- myExit(1);
- }
- fseek(checkFile, 0L, SEEK_END);
- fileSize = ftell(checkFile); // get file size
- fseek(checkFile, 0L, SEEK_SET);
- // 0.2 - 20% || 0.8 - 80% || 1 = 100%
- // from 0 to 0.2 -> first 20%
- // from 0.8 to 1 -> last 20% (1 - 8 = 0.2 ~ 20%)
- while ((checkData = fgetc(checkFile)) != EOF) {
- if ((fileSize * scanFrom <= ftell(checkFile)) && (fileSize * scanTo >= ftell(checkFile))) // scan only in the given range
- {
- virusData = fgetc(virusFile);
- if (nextOne(virusFile) == EOF) // Check if the virus sign FULLY exists in the given file to check
- {
- fclose(checkFile);
- fclose(virusFile);
- return true;
- }
- else if (virusData != checkData) // If the virus data doesn't equals to the given file data, "reset" the data "counter"
- {
- fseek(virusFile, 0, SEEK_SET); // start over the virus data
- }
- }
- }
- fclose(checkFile);
- fclose(virusFile);
- return false;
- }
- void normalScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum)
- {
- int i = 0;
- strcat(logBuilder, "Reuslts:\n");
- printf("Scanning:\n");
- for (i = 0; i < filesNum; i++)
- {
- strcat(logBuilder, filesList[i]);
- if (isInfected(filesList[i], virusSign, 0, 1))
- {
- strcat(logBuilder, " Infected!\n");
- printf("%s - Infected!\n", filesList[i]);
- }
- else
- {
- strcat(logBuilder, " Clean\n");
- printf("%s - Clean\n", filesList[i]);
- }
- }
- }
- void quickScan(char* folderToCheck, char* virusSign, char** filesList, int filesNum)
- {
- int i = 0;
- printf("Scanning:\n");
- for (i = 0; i < filesNum; i++)
- {
- printf("%s", filesList[i]);
- if (isInfected(filesList[i], virusSign, 0, 0.2))
- {
- printf("%s - Infected! (first 20%%)\n", filesList[i]);
- }
- else if (isInfected(filesList[i], virusSign, 0.8, 1))
- {
- printf("%s - Infected! (last 20%%)\n", filesList[i]);
- }
- else if (isInfected(filesList[i], virusSign, 0, 1))
- {
- printf("%s - Infected!\n", filesList[i]);
- }
- else
- {
- printf("%s - Clean\n", filesList[i]);
- }
- }
- }
- int countFiles(char* folderDirectory)
- {
- int counter = 0;
- DIR* d = 0;
- struct dirent* dir = 0;
- d = opendir(folderDirectory);
- if (d == NULL)
- {
- printf("Error opening directory: %s\n", folderDirectory);
- exit(1); // Stop program
- }
- while ((dir = readdir(d)) != NULL)
- {
- if (strcmp(dir->d_name, ".") &&
- strcmp(dir->d_name, "..") &&
- dir->d_type != DT_DIR)
- {
- counter++;
- }
- }
- closedir(d);
- return counter;
- }
- void freeMemory(char** filesList, int filesNum)
- {
- int i = 0;
- for (i = 0; i < filesNum; i++)
- {
- free(filesList[i]);
- }
- free(filesList);
- }
- void myExit(int code)
- {
- getchar();
- exit(code);
- }
- int getFiles(char* folderDirectory, char** filesList)
- {
- DIR* d = 0;
- int i = 0;
- struct dirent* dir = 0;
- d = opendir(folderDirectory);
- if (d == NULL)
- {
- printf("Error opening directory: %s\n", folderDirectory);
- myExit(1);
- }
- while ((dir = readdir(d)) != NULL)
- {
- if (strcmp(dir->d_name, ".") &&
- strcmp(dir->d_name, "..") &&
- dir->d_type != DT_DIR)
- {
- filesList[i] = (char*)malloc(sizeof(char) * (strlen(folderDirectory) + strlen(dir->d_name) + 3));
- if (filesList[i] == NULL)
- {
- while (i)
- {
- i--;
- free(filesList[i]);
- }
- free(filesList);
- printf("Unsuccessful malloc!\n");
- myExit(1);
- }
- strcpy(filesList[i], folderDirectory);
- strcat(filesList[i], "/");
- strcat(filesList[i], dir->d_name);
- i++;
- }
- }
- closedir(d);
- return i;
- }
- int nextOne(FILE* file)
- {
- int a = ftell(file);
- int data = fgetc(file);
- fseek(file, a, SEEK_SET);
- return data;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement