Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************
- * Name: Yair Benamou.*
- * Lesson: 8. *
- * Ex: Mid Project *
- **********************/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<conio.h>
- #include<malloc.h>
- // No Prototypes
- int fileSize(FILE* pToFile);
- int fastScan(FILE* pToFile, FILE* pToVirus, int size);
- int choseScan(void);
- /**
- The function 'fileLen' get a file and count how much characters he have.
- Input: pToFile - Getting the pointer to the file.
- Output: numCharacters - The counter.
- */
- int fileSize(FILE* pToFile)
- {
- int count = 0;
- while (fgetc(pToFile) != EOF) // Counting the chars
- {
- count++;
- }
- return (count);
- }
- /**
- The function 'choseScan' ask the user which scan he wants to do.
- Inputs: None.
- Outputs: The user's choice.
- */
- int choseScan(void)
- {
- int choice;
- printf("\nWhich scan you want to do?\n1) FAST SCAN.\n2) REGULAR SCAN.\n");
- scanf("%d", &choice);
- return(choice);
- }
- /**
- The function 'fastScan' gets a file and his size. The function will scan the file
- on the fast way: 20% of the file beginning, and 20% from the end.
- Input: pToFile - The file to scan
- pToVirus - The pointer to the virus file.
- size - The file's size.
- Output: 1 if there is a virus.
- 0 if the file is clean.
- */
- int fastScan(FILE* pToFile, FILE* pToVirus, int size)
- {
- float heuristic;
- int i, position;
- int j = 0;
- int ch, ch2;
- long virusLen;
- int flag, bigFlag = 0; // Turn up the flag to 2 when the virus is detected.
- heuristic = (float)(size * 0.2); // Getting 20% of the file size.
- fseek(pToVirus, 0, SEEK_END);
- virusLen = ftell(pToVirus);
- fseek(pToVirus, 0, SEEK_SET);
- fseek(pToFile, 0, SEEK_SET); // To put the seek on the file beginning.
- while (j < (int)heuristic)
- {
- flag = 1;
- fseek(pToVirus, 0, SEEK_SET);
- ch = fgetc(pToFile);
- ch2 = fgetc(pToVirus);
- position = ftell(pToFile);
- if (ch == ch2) // Verifying if the first char of the virus sign is equal to the current char on the file.
- {
- for (i = 1; i < virusLen; i++)
- {
- ch = fgetc(pToFile);
- ch2 = fgetc(pToVirus);
- if (ch != ch2)
- {
- flag = 0;
- }
- }
- if (!flag)
- {
- fseek(pToFile, -(position - 1), SEEK_CUR);
- }
- else
- {
- printf("There is a VIRUS!!!!\n");
- break;
- }
- }
- j++;
- }
- printf("\n\n\n");
- return (bigFlag);
- }
- /**
- This is the main function of the program, which is responsible
- to both start and end the running of the program.
- input: argc - How much strings are.
- argv - The strings.
- output: The program returns 0 upon successful completion of its running (windows convention).
- */
- int main(int argc, char** argv)
- {
- /*
- char* myPath = (char*)malloc(strlen("dir /b /A:-d") + strlen(argv[1]));
- strcpy(myPath, "dir ");
- strcat(myPath, argv[1]);
- strcat(myPath, " /a:-d");
- system(myPath);
- */
- int size;
- FILE* pToFile = fopen("C:\\Users\\User\\Desktop\\Mid Project\\Songs\\Happy.avi", "rb");
- FILE* pToVirus = fopen("C:\\Users\\User\\Desktop\\Mid Project\\YouTubeSign", "rb");
- if (!pToFile) // Verifying if the file didn't opened successfuly.
- {
- printf("An error occured opening the file.\n");
- return 1;
- }
- if (!pToVirus)
- {
- printf("An error occured opening the file.\n");
- return 1;
- }
- size = fileSize(pToFile); // Getting the file's size
- printf("File Size: %d.\n", size);
- if (choseScan() == 1)
- {
- printf("bigFlag: %d\n", fastScan(pToFile, pToVirus, size));
- }
- else
- {
- // regularScan();
- }
- fclose(pToFile);
- system("PAUSE");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement