Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define ARGS_COUNT 4 // Not including the program name
- #define ONE_BYTE 1
- void myExit(int code);
- void checkArgs(int argc);
- void extractVirusSignature(FILE* srcFile, int from, int to, FILE* dstFile);
- int main(int argc, char** argv)
- {
- int from = 0, to = 0;
- checkArgs(argc);
- /*
- * argv[0] = program name
- * argv[1] = infected file
- * argv[2] = virus signature (from)
- * argv[3] = virus signature (to)
- * argv[4] = extract the virus signature to
- */
- from = atoi(argv[2]);
- to = atoi(argv[3]);
- FILE* srcFile = fopen(argv[1], "r");
- if (srcFile == NULL)
- {
- printf("Error to open the file!\n");
- myExit(1);
- }
- FILE* dstFile = fopen(argv[4], "w+");
- if (dstFile == NULL)
- {
- printf("Error to open the file!\n");
- fclose(srcFile);
- myExit(1);
- }
- extractVirusSignature(srcFile, from, to, dstFile);
- printf("- - - Done - - -\n"
- "From file: %s\n"
- "Extracted virus signature from %d to %d\n"
- "Extracted to: %s\n", argv[1], from, to, argv[4]);
- getchar();
- return 0;
- }
- void extractVirusSignature(FILE* srcFile, int from, int to, FILE* dstFile)
- {
- char buffer = 0;
- int currentByte = 0;
- size_t numBytesRead = 0;
- while (!feof(srcFile))
- {
- currentByte++;
- numBytesRead = fread(&buffer, sizeof(char), ONE_BYTE, srcFile);
- if (currentByte >= from && currentByte <= to) // copy data only if the data in the given range (RANGE: from->to)
- {
- fwrite(&buffer, sizeof(char), numBytesRead, dstFile);
- }
- }
- }
- void checkArgs(int argc)
- {
- if (argc != ARGS_COUNT + 1)
- {
- printf("Invalid execution.\nUsage:\n"
- "> extractVirusSign <file_to_extract_virus_sign> "
- "<virus_signature_position_from> "
- "<virus_signature_posotion_to> "
- "<file_to_extract_virus_signature>\n");
- myExit(1);
- }
- }
- void myExit(int code)
- {
- getchar();
- exit(code);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement