Advertisement
Guest User

Untitled

a guest
May 29th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /*********************
  2. * Name: Yair Benamou.*
  3. * Lesson: 8. *
  4. * Ex: Mid Project *
  5. **********************/
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<string.h>
  10. #include<conio.h>
  11. #include<malloc.h>
  12.  
  13.  
  14. // No Prototypes
  15. int fileSize(FILE* pToFile);
  16. int fastScan(FILE* pToFile, FILE* pToVirus, int size);
  17. int choseScan(void);
  18.  
  19.  
  20. /**
  21. The function 'fileLen' get a file and count how much characters he have.
  22.  
  23. Input: pToFile - Getting the pointer to the file.
  24.  
  25. Output: numCharacters - The counter.
  26. */
  27. int fileSize(FILE* pToFile)
  28. {
  29. int count = 0;
  30. while (fgetc(pToFile) != EOF) // Counting the chars
  31. {
  32. count++;
  33. }
  34. return (count);
  35. }
  36.  
  37.  
  38.  
  39. /**
  40. The function 'choseScan' ask the user which scan he wants to do.
  41.  
  42. Inputs: None.
  43.  
  44. Outputs: The user's choice.
  45. */
  46. int choseScan(void)
  47. {
  48. int choice;
  49.  
  50. printf("\nWhich scan you want to do?\n1) FAST SCAN.\n2) REGULAR SCAN.\n");
  51. scanf("%d", &choice);
  52.  
  53. return(choice);
  54. }
  55.  
  56.  
  57.  
  58. /**
  59. The function 'fastScan' gets a file and his size. The function will scan the file
  60. on the fast way: 20% of the file beginning, and 20% from the end.
  61.  
  62. Input: pToFile - The file to scan
  63. pToVirus - The pointer to the virus file.
  64. size - The file's size.
  65.  
  66. Output: 1 if there is a virus.
  67. 0 if the file is clean.
  68. */
  69. int fastScan(FILE* pToFile, FILE* pToVirus, int size)
  70. {
  71. float heuristic;
  72. int i, position;
  73. int j = 0;
  74. int ch, ch2;
  75. long virusLen;
  76. int flag, bigFlag = 0; // Turn up the flag to 2 when the virus is detected.
  77.  
  78. heuristic = (float)(size * 0.2); // Getting 20% of the file size.
  79.  
  80. fseek(pToVirus, 0, SEEK_END);
  81. virusLen = ftell(pToVirus);
  82.  
  83. fseek(pToVirus, 0, SEEK_SET);
  84. fseek(pToFile, 0, SEEK_SET); // To put the seek on the file beginning.
  85.  
  86. while (j < (int)heuristic)
  87. {
  88. flag = 1;
  89. fseek(pToVirus, 0, SEEK_SET);
  90. ch = fgetc(pToFile);
  91. ch2 = fgetc(pToVirus);
  92.  
  93. position = ftell(pToFile);
  94. if (ch == ch2) // Verifying if the first char of the virus sign is equal to the current char on the file.
  95. {
  96. for (i = 1; i < virusLen; i++)
  97. {
  98. ch = fgetc(pToFile);
  99. ch2 = fgetc(pToVirus);
  100. if (ch != ch2)
  101. {
  102. flag = 0;
  103. }
  104. }
  105.  
  106. if (!flag)
  107. {
  108. fseek(pToFile, -(position - 1), SEEK_CUR);
  109. }
  110. else
  111. {
  112. printf("There is a VIRUS!!!!\n");
  113. break;
  114. }
  115. }
  116. j++;
  117. }
  118.  
  119. printf("\n\n\n");
  120.  
  121. return (bigFlag);
  122. }
  123.  
  124.  
  125.  
  126. /**
  127. This is the main function of the program, which is responsible
  128. to both start and end the running of the program.
  129. input: argc - How much strings are.
  130. argv - The strings.
  131.  
  132. output: The program returns 0 upon successful completion of its running (windows convention).
  133. */
  134. int main(int argc, char** argv)
  135. {
  136. /*
  137. char* myPath = (char*)malloc(strlen("dir /b /A:-d") + strlen(argv[1]));
  138. strcpy(myPath, "dir ");
  139. strcat(myPath, argv[1]);
  140. strcat(myPath, " /a:-d");
  141.  
  142. system(myPath);
  143. */
  144.  
  145.  
  146. int size;
  147. FILE* pToFile = fopen("C:\\Users\\User\\Desktop\\Mid Project\\Songs\\Happy.avi", "rb");
  148. FILE* pToVirus = fopen("C:\\Users\\User\\Desktop\\Mid Project\\YouTubeSign", "rb");
  149.  
  150. if (!pToFile) // Verifying if the file didn't opened successfuly.
  151. {
  152. printf("An error occured opening the file.\n");
  153. return 1;
  154. }
  155.  
  156. if (!pToVirus)
  157. {
  158. printf("An error occured opening the file.\n");
  159. return 1;
  160. }
  161.  
  162. size = fileSize(pToFile); // Getting the file's size
  163. printf("File Size: %d.\n", size);
  164.  
  165. if (choseScan() == 1)
  166. {
  167. printf("bigFlag: %d\n", fastScan(pToFile, pToVirus, size));
  168. }
  169. else
  170. {
  171. // regularScan();
  172. }
  173.  
  174. fclose(pToFile);
  175.  
  176. system("PAUSE");
  177. return (0);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement