Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dirent.h>
- #define MAX_ELEM 256
- #define MAX_LEN 256
- #define NOT_PHOTOS 2
- #define SLASH "/"
- #define INFECTED_MSG " - Infected!"
- #define CLEAN_MSG " - Clean"
- #define INFECTED 1
- #define CLEAN 2
- #define LOG_FILE_NAME "Log.txt"
- #define NEW_LINE "\n"
- int pathsOpener(char** paths);
- void dirsParser(char** paths, char files[][MAX_LEN], char virusSign[], int photosCounter);
- void scan(char** argv, char photosPaths[][MAX_LEN], char virusSignPath[], int choice, int photosCounter);
- void createLog(char** photosFolder, char fileFoundPath[], int mode);
- int photosCounter = 0;
- char photosPaths[MAX_ELEM][MAX_LEN] = { 0 };
- char virusSignPath[MAX_LEN] = { 0 };
- char* logFilePath = 0;
- int main(int argc, char** argv)
- {
- int choice = 0;
- if (pathsOpener(argv))
- {
- printf("Welcome to my Virus Scan!\n\n");
- printf("Folder to scan: %s\n", argv[1]);
- printf("Virus signature: %s\n\n", argv[2]);
- printf("Press 0, for normal scan.\nPress any other key for fast scan.\nYour choice: ");
- scanf("%d", &choice);
- getchar();
- scan(argv, photosPaths, virusSignPath, choice, photosCounter);
- }
- else
- {
- return 0;
- }
- printf("Scan completed.\nSee log path for results: %s", logFilePath);
- //free(logFilePath);
- getchar();
- return 0;
- }
- int pathsOpener(char** paths)
- {
- struct dirent* de;
- DIR* dir;
- FILE* virusSign;
- char files[MAX_ELEM][MAX_LEN] = { 0 };
- char tempFiles[MAX_ELEM][MAX_LEN] = { 0 };
- char virusSignFile[MAX_LEN] = "";
- char tempSort[256] = { 0 };
- de = opendir(paths[1]);
- int i = 0, j = 0;
- if (de)
- {
- while ((dir = readdir(de)) != NULL)
- {
- strcpy(files[photosCounter], de->d_name);
- photosCounter++;
- }
- closedir(de);
- }
- else
- {
- printf("Could not open the folder.\n");
- return 0;
- }
- virusSign = fopen(paths[2], "rb");
- if (!virusSign)
- {
- printf("Could not open the virus Signature file.\n");
- return 0;
- }
- else
- {
- strcpy(virusSignFile, paths[2]);
- }
- fclose(virusSign);
- for (i = photosCounter - NOT_PHOTOS; i < photosCounter; i++)
- {
- for (j = photosCounter - NOT_PHOTOS; j < photosCounter; j++)
- {
- if (strcmp(files[i], files[j]) < 0)
- {
- strcpy(tempSort, files[i]);
- strcpy(files[i], files[j]);
- strcpy(files[j], tempSort);
- }
- }
- }
- dirsParser(paths, files, virusSignFile, photosCounter);
- }
- void dirsParser(char** paths, char files[][MAX_LEN], char virusSign[], int photosCounter)
- {
- char temp[MAX_LEN] = { 0 };
- int i = 0, j = 0;
- strcpy(temp, paths[1]);
- strcat(temp, SLASH);
- for (i = 0, j = NOT_PHOTOS; i < photosCounter - NOT_PHOTOS, j < photosCounter; i++, j++)
- {
- strcpy(photosPaths[i], temp);
- strcat(photosPaths[i], files[j]);
- }
- strcat(virusSignPath, virusSign);
- }
- void scan(char** argv, char photosPaths[][MAX_LEN], char virusSignPath[], int choice, int photosCounter)
- {
- FILE* virusSign = fopen(virusSignPath, "rb");
- FILE* photo;
- long virusFileSize = 0;
- char* virusSignArr = 0;
- char* tempBuffer = 0;
- int i = 0, j = 0, k = 0, matchCounter = 0, flag = 0, flagFound = 0;
- fseek(virusSign, 0, SEEK_END);
- virusFileSize = ftell(virusSign);
- fseek(virusSign, 0, SEEK_SET);
- virusSignArr = (char*)malloc(virusFileSize * sizeof(char));
- tempBuffer = (char*)malloc(1 * sizeof(char));
- fread(virusSignArr, sizeof(char), virusFileSize, virusSign); //Saves virus signature
- fclose(virusSign);
- if (!choice)
- {
- for (i = 0; i < photosCounter - NOT_PHOTOS; i++)
- {
- flagFound = 0;
- photo = fopen(photosPaths[i], "rb");
- matchCounter = 0;
- while (!feof(photo) && flagFound == 0)
- {
- matchCounter = 0;
- flag = 0;
- fread(tempBuffer, 1, 1, photo);
- for (j = 0; j < virusFileSize && flag == 0; j++)
- {
- if (virusSignArr[j] == *tempBuffer)
- {
- fread(tempBuffer, 1, 1, photo);
- matchCounter++;
- if (matchCounter == virusFileSize)
- {
- printf("%s - Infected!\n", photosPaths[i]);
- flagFound = 1;
- flag = 1;
- createLog(argv, photosPaths[i], INFECTED);
- }
- }
- else
- {
- flag = 1;
- }
- }
- }
- fclose(photo);
- if (flagFound == 0)
- {
- printf("%s - Clean\n", photosPaths[i]);
- createLog(argv, photosPaths[i], CLEAN);
- }
- }
- }
- free(tempBuffer);
- free(virusSignArr);
- }
- void createLog(char** photosFolder, char fileFoundPath[], int mode)
- {
- logFilePath = (char*)malloc(strlen(photosFolder[1]) + strlen(LOG_FILE_NAME) + 1 * sizeof(char));
- strcpy(logFilePath, photosFolder[1]);
- strcat(logFilePath, SLASH);
- strcat(logFilePath, LOG_FILE_NAME);
- FILE* log = 0;
- char welcome[] = "Anti-virus began! Welcome!\n";
- char folderToScnPath[] = "Folder to scan:\n";
- char virusSignature[] = "Virus signature:\n";
- char results[] = "Results:\n";
- char scnOptn[] = "Scanning option:\nNormal Scan\n";
- char* photoData = 0;
- if (fopen(logFilePath, "r") == 0)
- {
- log = fopen(logFilePath, "w");
- fwrite(welcome, 1, sizeof(welcome) - 1, log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- fwrite(folderToScnPath, 1, sizeof(folderToScnPath) - 1, log);
- fwrite(photosFolder[1], 1, strlen(photosFolder[1]), log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- fwrite(virusSignature, 1, sizeof(virusSignature) - 1, log);
- fwrite(photosFolder[2], 1, strlen(photosFolder[2]), log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- fwrite(scnOptn, 1, sizeof(scnOptn) - 1, log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- fwrite(results, 1, sizeof(results) - 1, log);
- if (mode == INFECTED)
- {
- photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(INFECTED_MSG)) * sizeof(char));
- strcpy(photoData, fileFoundPath);
- strcat(photoData, INFECTED_MSG);
- fwrite(photoData, 1, strlen(photoData), log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- }
- else if (mode = CLEAN)
- {
- photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(CLEAN_MSG)) * sizeof(char));
- strcpy(photoData, fileFoundPath);
- strcat(photoData, CLEAN_MSG);
- fwrite(photoData, 1, strlen(photoData), log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- }
- }
- else
- {
- log = fopen(logFilePath, "a");
- if (mode == INFECTED)
- {
- photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(INFECTED_MSG)) * sizeof(char));
- strcpy(photoData, fileFoundPath);
- strcat(photoData, INFECTED_MSG);
- fwrite(photoData, 1, strlen(photoData), log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- }
- else if (mode = CLEAN)
- {
- photoData = (char*)malloc((strlen(fileFoundPath) + sizeof(CLEAN_MSG)) * sizeof(char));
- strcpy(photoData, fileFoundPath);
- strcat(photoData, CLEAN_MSG);
- fwrite(photoData, 1, strlen(photoData), log);
- fwrite(NEW_LINE, 1, sizeof(NEW_LINE) - 1, log);
- }
- }
- free(photoData);
- fclose(log);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement