Advertisement
Yonka2019

Yonka-AntiVirusProject-Bonus.c

May 20th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define ARGS_COUNT 4 // Not including the program name
  5. #define ONE_BYTE 1
  6.  
  7. void myExit(int code);
  8. void checkArgs(int argc);
  9. void extractVirusSignature(FILE* srcFile, int from, int to, FILE* dstFile);
  10.  
  11. int main(int argc, char** argv)
  12. {
  13.     int from = 0, to = 0;
  14.    
  15.     checkArgs(argc);
  16.     /*
  17.      * argv[0] = program name
  18.      * argv[1] = infected file
  19.      * argv[2] = virus signature (from)
  20.      * argv[3] = virus signature (to)
  21.      * argv[4] = extract the virus signature to
  22.     */
  23.     from = atoi(argv[2]);
  24.     to = atoi(argv[3]);
  25.  
  26.     FILE* srcFile = fopen(argv[1], "r");
  27.     if (srcFile == NULL)
  28.     {
  29.         printf("Error to open the file!\n");
  30.  
  31.         myExit(1);
  32.     }
  33.     FILE* dstFile = fopen(argv[4], "w+");
  34.     if (dstFile == NULL)
  35.     {
  36.         printf("Error to open the file!\n");
  37.         fclose(srcFile);
  38.  
  39.         myExit(1);
  40.     }
  41.  
  42.     extractVirusSignature(srcFile, from, to, dstFile);
  43.  
  44.     printf("- - - Done - - -\n"
  45.            "From file: %s\n"
  46.            "Extracted virus signature from %d to %d\n"
  47.            "Extracted to: %s\n", argv[1], from, to, argv[4]);
  48.  
  49.     getchar();
  50.     return 0;
  51. }
  52. void extractVirusSignature(FILE* srcFile, int from, int to, FILE* dstFile)
  53. {  
  54.     char buffer = 0;
  55.     int currentByte = 0;
  56.     size_t numBytesRead = 0;
  57.    
  58.     while (!feof(srcFile))
  59.     {
  60.         currentByte++;
  61.         numBytesRead = fread(&buffer, sizeof(char), ONE_BYTE, srcFile);
  62.         if (currentByte >= from && currentByte <= to) // copy data only if the data in the given range (RANGE: from->to)
  63.         {
  64.             fwrite(&buffer, sizeof(char), numBytesRead, dstFile);
  65.         }
  66.     }
  67. }
  68. void checkArgs(int argc)
  69. {
  70.     if (argc != ARGS_COUNT + 1)
  71.     {
  72.         printf("Invalid execution.\nUsage:\n"
  73.             "> extractVirusSign <file_to_extract_virus_sign> "
  74.             "<virus_signature_position_from> "
  75.             "<virus_signature_posotion_to> "
  76.             "<file_to_extract_virus_signature>\n");
  77.  
  78.         myExit(1);
  79.     }
  80. }
  81. void myExit(int code)
  82. {
  83.     getchar();
  84.     exit(code);
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement