I've been sitting for hours checking this code after I found out there's a memory leak/error somewhere Where is that leak? How can it be fixed? here is Dr.Memory report:
Dr. Memory version 2.3.0
Running "C:\Users\Beni\source\repos\Magshimim_EX8\Debug\Magshimim_EX8.exe"
Using system call file C:\Users\Beni\AppData\Roaming\Dr. Memory\symcache\syscalls_wow64.txt
Error #1: UNADDRESSABLE ACCESS: reading 1 byte(s)
replace_strlen
d:\drmemory_package\drmemory\replace.c(412):
Magshimim_EX8.exe!?
??:0
Magshimim_EX8.exe!?
??:0
Magshimim_EX8.exe!?
??:0
Magshimim_EX8.exe!?
??:0
Magshimim_EX8.exe!?
??:0
KERNEL32.dll!BaseThreadInitThunk
??:0
ERRORS FOUND:
1 unique, 1 total unaddressable access(es)
0 unique, 0 total uninitialized access(es)
0 unique, 0 total invalid heap argument(s)
0 unique, 0 total GDI usage error(s)
0 unique, 0 total handle leak(s)
0 unique, 0 total warning(s)
0 unique, 0 total, 0 byte(s) of leak(s)
0 unique, 0 total, 0 byte(s) of possible leak(s)
Details: C:\Users\Beni\AppData\Roaming\Dr. Memory\DrMemory-Magshimim_EX8.exe.5208.000\results.txt
WARNING: application exited with abnormal code 0xc0000005
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE !FALSE
#define FIRST_TWO_FILES 2
#define FIRST_TWENTY_PRECENTS 1
#define MIDDLE_SIXTY_PRECENTS 2
#define LAST_TWENTY_PRECENTS 3
long findLenOfFile(FILE * file);
char* readFile(FILE* f, char* dest, long len);
char menu(char* scanFolder, char* virusSignature);
char** writeFilesFromFolder(char* scanFolder, char ** filesList, int* len);
char* writePart(char* src, char* dest, int length, int* newLen, int part);
int findSignature(char* virusSignature, char* buffer, int sigLen, int bufferLen);
void scanFiles(char* scanFolder, char** filesList, int amountOfFiles, char* virusSignature, long virusLength, char option);
int main(int argc, char* argv[])
{
char* log = malloc(sizeof(char)*strlen(argv[1]) + sizeof(char)*strlen("\\Log.txt") + 4);
FILE* virusSignatureFile = fopen(argv[2], "rb");
long virusLength = 0;
char** filesList = (char**)malloc(sizeof(char) * 0);
char* virusSignature = 0;
int amountOfFiles = 0;
char option = 0;
int i = 0;
virusLength = findLenOfFile(virusSignatureFile);
// get the virusSignature as a string and write the files to check into the filesList
virusSignature = readFile(virusSignatureFile, virusSignature, virusLength);
filesList = writeFilesFromFolder(argv[1], filesList, &amountOfFiles);
// create log file
strcpy(log, "");
strcat(log, argv[1]);
strcat(log, "\\Log.txt");
FILE * logFile = fopen(log, "w");
fprintf(logFile, "Anti-virus began! Welcome!\n\nFolder to scan:\n%s\nVirus signature:\n%s\n\nScanning option:\n", argv[1], argv[2]);
// get scanning option (normal or quick) and continue accordingly
option = menu(argv[1], argv[2]);
if (option == '0') {
fprintf(logFile, "Normal Scan\n\n");
}
else {
fprintf(logFile, "Quick Scan\n\n");
}
fprintf(logFile, "Results:\n");
fclose(logFile);
// initiate scan
scanFiles(argv[1], filesList, amountOfFiles, virusSignature, virusLength, option);
fclose(virusSignatureFile);
free(log);
free(filesList);
free(virusSignature);
getchar();
return 0;
}
/*
This function will print the scanning folder path and signature path, also will print the option menu to the user of quick or normal scan,
after that function will return user option(0, or other key)
input: scanFolder path (string), virus signature path (also string)
output: user option (char: '0', or other key)
*/
char menu(char * scanFolder, char * virusSignature)
{
char userOption = '\0';
printf("Welcome to my Virus Scan!\n\nFolder to scan: %s\nVirus signature: %s\n\nPress 0 for a norman scan or any other key for a quick scan: ", scanFolder, virusSignature);
userOption = getchar();
printf("Scanning began...\nThis process may take several minutes...\n\n");
return userOption;
}
/*
This function writes all files name from folder to the filesList
input: the scanning folder path
output: amount of files
*/
char** writeFilesFromFolder(char * scanFolder, char ** filesList, int* len)
{
DIR *d = 0;
struct dirent *dir;
d = opendir(scanFolder);
int i = 0;
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if (i > 1)
{
filesList = (char**)realloc(filesList, sizeof(filesList) + sizeof(char*) + 4);
*(filesList + (i - FIRST_TWO_FILES)) = (char*)malloc(sizeof(char) * strlen(dir->d_name) + 1);
strcpy(*(filesList + (i - FIRST_TWO_FILES)), (dir->d_name));
}
i++;
}
closedir(d);
}
*len = i - FIRST_TWO_FILES; //first two names is "." and ".."
return filesList;
}
/*
This function will read the contents of a file into a string
input: a file (FILE *) to read from
output: char* with the contents of the file
*/
char* readFile(FILE* f, char * dest, long len)
{
dest = (char*)malloc(sizeof(char) * len);
fread(dest, 1, len, f);
return dest;
}
void scanFiles(char * scanFolder, char ** filesList, int amountOfFiles, char * virusSignature, long virusLength, char option)
{
char* log = malloc(sizeof(char)*strlen(scanFolder) + sizeof(char)*strlen("\\Log.txt") + 1);
char * buffer = (char*)malloc(sizeof(char) * 0);
char* subBuffer = 0;
char* slash = "\\";
long length = 0;
char* name = 0;
int subLen = 0;
int i = 0;
FILE * f;
// reopen log file and append to it
strcpy(log, "");
strcat(log, scanFolder);
strcat(log, "\\Log.txt");
FILE * logFile = fopen(log, "a");
// iterate over each file
for (i = 0; i < amountOfFiles; i++)
{
name = (char*)malloc(sizeof(char) * strlen(scanFolder) + 1 + sizeof(char) * strlen(slash) + sizeof(char) * strlen(*(filesList + i)) + 20);
// open current file
strcpy(name, "");
strcat(name, scanFolder);
strcat(name, slash);
strcat(name, *(filesList + i));
f = fopen(name, "rb");
length = findLenOfFile(f);
if (f != NULL) // if file can be accessed
{
buffer = readFile(f, buffer, length);
if (option == '0') { // Normal Mode
if (findSignature(virusSignature, buffer, virusLength, length))
{
printf("%s - Infected!\n", name);
fprintf(logFile, "%s - Infected!\n", name);
}
else
{
printf("%s - Clean\n", name);
fprintf(logFile, "%s - Clean\n", name);
}
}
else { // Quick Mode
subBuffer = writePart(buffer, subBuffer, length, &subLen, FIRST_TWENTY_PRECENTS); // get first 20%
if (findSignature(virusSignature, subBuffer, virusLength, subLen))
{
printf("%s - infected! (first 20%%)\n", name);
fprintf(logFile, "%s - infected! (first 20%%)\n", name);
}
else {
free(subBuffer);
subBuffer = writePart(buffer, subBuffer, length, &subLen, LAST_TWENTY_PRECENTS); // get last 20%
if (findSignature(virusSignature, subBuffer, virusLength, subLen))
{
printf("%s - Infected! (last 20%%)\n", name);
fprintf(logFile, "%s - Infected! (last 20%%)\n", name);
}
else {
subBuffer = writePart(buffer, subBuffer, length, &subLen, MIDDLE_SIXTY_PRECENTS); // get the 60% left in the middle
if (findSignature(virusSignature, subBuffer, virusLength, subLen))
{
printf("%s - Infected!\n", name);
fprintf(logFile, "%s - Infected!\n", name);
}
else {
printf("%s - clean\n", name);
fprintf(logFile, "%s - Clean\n", name);
}
}
}
free(subBuffer);
}
fclose(f);
}
else
{
printf("No file found\n");
}
free(*(filesList + i));
free(name);
}
fclose(logFile);
free(log);
free(buffer);
getchar();
}
/*
This function will write part of the file (beginning, middle or end) to a string
input: source (string) to take the information from, destination (string) to write a part of the source to it,
length (int) of the source string, a pointer (int*) to store the new length of the destination string and
part of the file to write from (int) 1,2 or 3: first 20%, 60% in the middle and last 20% accordingly
output: string containing the desired part of the source string
*/
char* writePart(char *src, char *dest, int length, int *newLen, int part) {
int i = 0;
int percentedLength = 0;
int count = 0;
percentedLength = (int)(length / 5); // this len is 20% of the entire file's length
if (part == FIRST_TWENTY_PRECENTS) // return beginning
{
dest = (char*)malloc(sizeof(char) * percentedLength);
*newLen = percentedLength;
for (i = 0; i < percentedLength; i++)
{
*(dest + i) = *(src + i);
}
}
else if (part == MIDDLE_SIXTY_PRECENTS) // return middle
{
// allocate space for the middle: The entire file size minus 20% from the start and 20% from the end
dest = (char*)malloc(sizeof(char) * (length - 2 * percentedLength));
*newLen = length - 2 * percentedLength;
for (i = percentedLength; i < length - percentedLength; i++) {
*(dest + count) = *(src + i);
count++;
}
}
else if (part == LAST_TWENTY_PRECENTS) // return end
{
dest = (char*)malloc(sizeof(char) * percentedLength);
*newLen = percentedLength;
for (i = length - percentedLength; i < length; i++)
{
*(dest + count) = *(src + i);
count++;
}
}
return dest;
}
/*
function that finds the length of a file
input: file (FILE *)
output: the file's length (long)
*/
long findLenOfFile(FILE * file)
{
long length = 0;
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
return length;
}
/*
function checks whether a file contains the virusSignature. It iterates over each letter of the file and checks
if it is the same as the first letter in the virusSignature. If it is, it checks the rest of the characters and
returns True if a match is found. if not it continues the same process until the end of the file is reached.
input: The virusSignature (string), a buffer with the content of a file (string), the signature's length (int)
and the buffer's length (int)
output: True if signature is in file, False otherwise
*/
int findSignature(char* virusSignature, char* buffer, int sigLen, int bufferLen)
{
int found = 0;
int i = 0;
int j = 0;
for (i = 0; i < bufferLen - (sigLen - 1); i++) {
if (*(buffer + i) == *virusSignature) // check if a letter is the same as first letter in virusSignature
{
found = TRUE;
// check if the rest of the letters match the signature and stop if one doesn't
for (j = 1; (j < sigLen) && found; j++) {
if (*(buffer + (i + j)) != *(virusSignature + j)) {
found = FALSE;
}
}
if (found) {
return TRUE; // if we got a match, return true!
}
}
}
return FALSE;
}
The realloc
call in writeFilesFromFolder
has a bug.
It is:
filesList = (char **) realloc(filesList, sizeof(filesList) + sizeof(char *) + 4);
Notice that the space allocated for filesList
is constant. It does not grow as new elements are added, so you have undefined behavior.
This is not a memory leak as the tool detected. A memory leak means that you fail to free
a pointer that goes out of scope.
Rather, you're storing data beyond the end of the area you've allocated, trashing whatever is there, which is probably the [hidden] chain pointer area that malloc
et. al. use to keep track of allocations.
I'm not sure how either of the sizeof
factor in, but, filesList
is a pointer, so sizeof(filesList)
is constant [either 4 on a 32 bit machine or 8 for 64 bit].
The allocated space has to increase in proportion to i
.
Here is a refactored version of that function that fixes the bug along with some simplification and cleanup:
BTW, don't cast malloc
: Do I cast the result of malloc?
Also, note that sizeof(char)
is [by definition] always 1 regardless of how many bits a char
actually has for a given architecture. So, remove any sizeof(char) *
Instead of (e.g.):
*(filesList + i)
It's usually simpler/cleaner to do:
filesList[i]
Anyway, here's the code:
/*
This function writes all files name from folder to the filesList
input: the scanning folder path
output: amount of files
*/
char **
writeFilesFromFolder(char *scanFolder, char **filesList, int *len)
{
DIR *d = 0;
struct dirent *dir;
d = opendir(scanFolder);
int i = -FIRST_TWO_FILES;
if (d) {
while ((dir = readdir(d)) != NULL) {
if (i >= 0) {
filesList = realloc(filesList,sizeof(*filesList) * (i + 1));
filesList[i] = strdup(dir->d_name);
}
i++;
}
closedir(d);
}
*len = i; // first two names is "." and ".."
return filesList;
}
UPDATE:
Okay, you do have memory leaks. And, I've coded up detection and a fix.
Primarily, what you are doing is passing down a buffer pointer to a function (e.g. readFile
or writePart
) as an argument (e.g. dest
).
Then, you are doing:
dest = malloc(percentedLength);
This leaks the previous value of dest
.
Normally, functions that allocate a buffer and return it do not take it as an argument. But, after analyzing your code, replacing the malloc
with a realloc
prevents the leak.
I had to do a complete code review to find this. And, I did several simplifications and cleanups along the way to try to understand your code and isolate possible further issues.
I replaced your allocation/concatenation of filenames with a new function filejoin
.
I replaced the other malloc
calls with a macro: ALLOCME
that detects the memory leaks before they happen. This works in conjunction with the [new] FREEME
macro that replaces the free
calls.
The default mode is to detect the leak and abort. If you give the program a -f
option, it will fix the problem. After you analyze and understand what happened, you can change the default to the "fix" mode.
Where possible, I left your original code under #if 0
A few more style tips:
Keep lines to <= 80 chars.
Don't use "sidebar" comments, particularly on if
clauses (e.g.):
if (...) { // process the file
Replace with:
// process the file
if (...) {
Don't replicate code. When you are replicating similar code [as in where I replaced the code with filejoin
], this indicates a good place to write a modular function
Anyway, here's the refactored and fixed code:
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#define FALSE 0
#define TRUE (! FALSE)
#define FIRST_TWO_FILES 2
#define FIRST_TWENTY_PRECENTS 1
#define MIDDLE_SIXTY_PRECENTS 2
#define LAST_TWENTY_PRECENTS 3
#define ALLOCME(_ptr,_len) \
do { \
_ptr = allocme(_ptr,_len,__FUNCTION__,__LINE__); \
} while (0)
#define FREEME(_ptr) \
do { \
if (_ptr != NULL) \
free(_ptr); \
_ptr = NULL; \
} while (0)
void
sysfault(const char *fmt,...)
{
va_list ap;
va_start(ap,fmt);
vfprintf(stderr,fmt,ap);
va_end(ap);
exit(1);
}
long findLenOfFile(FILE * file);
char *readFile(FILE * f, char *dest, long len);
char menu(const char *scanFolder, const char *virusSignature);
char **writeFilesFromFolder(const char *scanFolder, char **filesList, int *len);
char *writePart(char *src, char *dest, int length, int *newLen, int part);
int findSignature(char *virusSignature, char *buffer, int sigLen,
int bufferLen);
void scanFiles(const char *scanFolder, char **filesList, int amountOfFiles,
char *virusSignature, long virusLength, char option);
int opt_fixme = 0;
// allocme -- guarded allocation
void *
allocme(void *ptr,size_t len,const char *fnc,int lno)
{
if (! opt_fixme) {
if (ptr != NULL)
sysfault("allocme: leaking ptr=%p len=%zu (from %s at line %d)\n",
ptr,len,fnc,lno);
}
ptr = realloc(ptr,len);
if (ptr == NULL)
sysfault("allocme: realloc failure\n");
return ptr;
}
#ifdef __linux__
const char *slash = "/";
#else
const char *slash = "\\";
#endif
// filejoin -- create filename from directory and file tail
char *
filejoin(const char *dir,const char *tail)
{
size_t len;
char *file;
len = 0;
len += strlen(dir);
len += strlen(slash);
len += strlen(tail);
len += 1;
file = malloc(len);
if (file == NULL)
sysfault("filejoin: unable to alloc -- %s\n",strerror(errno));
*file = 0;
strcat(file,dir);
strcat(file,slash);
strcat(file,tail);
return file;
}
int
main(int argc, char **argv)
{
#if 0
char *log = malloc(strlen(argv[1]) + strlen("\\Log.txt") + 4);
#else
char *log;
#endif
--argc;
++argv;
for (; argc > 0; --argc, ++argv) {
char *cp = *argv;
if (*cp != '-')
break;
switch (cp[1]) {
case 'f':
opt_fixme = ! opt_fixme;
break;
}
}
if (argc != 2)
sysfault("usage: <folder_to_scan> <virus_signature_file>\n");
const char *topdir = argv[0];
const char *sigfile = argv[1];
FILE *virusSignatureFile = fopen(sigfile, "rb");
if (virusSignatureFile == NULL)
sysfault("main: unable to open '%s' -- %s\n",sigfile,strerror(errno));
long virusLength = 0;
#if 0
char **filesList = malloc(0);
#else
char **filesList = NULL;
#endif
char *virusSignature = NULL;
int amountOfFiles = 0;
char option = 0;
virusLength = findLenOfFile(virusSignatureFile);
// get the virusSignature as a string and write the files to check into the
// filesList
virusSignature = readFile(virusSignatureFile, virusSignature, virusLength);
#if 1
fclose(virusSignatureFile);
#endif
filesList = writeFilesFromFolder(topdir, filesList, &amountOfFiles);
// create log file
#if 0
strcpy(log, "");
strcat(log, argv[1]);
strcat(log, "\\Log.txt");
#else
log = filejoin(topdir,"Log.txt");
#endif
FILE *logFile = fopen(log, "w");
fprintf(logFile, "Anti-virus began! Welcome!\n\n"
"Folder to scan:\n%s\n"
"Virus signature:\n%s\n\n"
"Scanning option:\n", topdir, sigfile);
// get scanning option (normal or quick) and continue accordingly
option = menu(topdir, sigfile);
if (option == '0') {
fprintf(logFile, "Normal Scan\n\n");
}
else {
fprintf(logFile, "Quick Scan\n\n");
}
fprintf(logFile, "Results:\n");
fclose(logFile);
// initiate scan
scanFiles(topdir, filesList, amountOfFiles, virusSignature, virusLength,
option);
#if 0
fclose(virusSignatureFile);
#endif
FREEME(log);
FREEME(filesList);
FREEME(virusSignature);
#ifndef __linux__
getchar();
#endif
return 0;
}
/*
This function will print the scanning folder path and signature path, also
will print the option menu to the user of quick or normal scan,
after that function will return user option(0, or other key)
input: scanFolder path (string), virus signature path (also string)
output: user option (char: '0', or other key)
*/
char
menu(const char *scanFolder, const char *virusSignature)
{
char userOption = '\0';
printf("Welcome to my Virus Scan!\n\n"
"Folder to scan: %s\n"
"Virus signature: %s\n\n"
"Press 0 for a norman scan or any other key for a quick scan: ",
scanFolder, virusSignature);
userOption = getchar();
printf("Scanning began...\nThis process may take several minutes...\n\n");
return userOption;
}
/*
This function writes all files name from folder to the filesList
input: the scanning folder path
output: amount of files
*/
char **
writeFilesFromFolder(const char *scanFolder, char **filesList, int *len)
{
DIR *d = 0;
struct dirent *dir;
d = opendir(scanFolder);
int i = -FIRST_TWO_FILES;
if (d) {
while ((dir = readdir(d)) != NULL) {
if (i >= 0) {
filesList = realloc(filesList,sizeof(*filesList) * (i + 1));
filesList[i] = strdup(dir->d_name);
}
i++;
}
closedir(d);
}
// first two names is "." and ".."
*len = i;
return filesList;
}
/*
This function will read the contents of a file into a string
input: a file (FILE *) to read from
output: char* with the contents of the file
*/
char *
readFile(FILE * f, char *dest, long len)
{
// NOTE/BUG: this does _not_ free the prior value -- memory leak!
ALLOCME(dest,len);
fread(dest, 1, len, f);
return dest;
}
void
scanFiles(const char *scanFolder, char **filesList, int amountOfFiles,
char *virusSignature, long virusLength, char option)
{
#if 0
char *log = malloc(strlen(scanFolder) + strlen("\\Log.txt") + 1);
#else
char *log;
#endif
#if 0
char *buffer = malloc(0);
#else
char *buffer = NULL;
#endif
char *subBuffer = NULL;
long length = 0;
char *name = NULL;
int subLen = 0;
int i = 0;
FILE *f;
// reopen log file and append to it
#if 0
strcpy(log, "");
strcat(log, scanFolder);
strcat(log, "\\Log.txt");
#else
log = filejoin(scanFolder,"Log.txt");
#endif
FILE *logFile = fopen(log, "a");
// iterate over each file
for (i = 0; i < amountOfFiles; i++) {
#if 0
name = malloc(strlen(scanFolder) + 1 + strlen(slash) + strlen(*(filesList + i)) + 20);
#endif
// open current file
#if 0
strcpy(name, "");
strcat(name, scanFolder);
strcat(name, slash);
strcat(name, *(filesList + i));
#else
name = filejoin(scanFolder,filesList[i]);
#endif
f = fopen(name, "rb");
length = findLenOfFile(f);
// if file can be accessed
if (f != NULL) {
buffer = readFile(f, buffer, length);
// Normal Mode
if (option == '0') {
if (findSignature(virusSignature, buffer, virusLength, length)) {
printf("%s - Infected!\n", name);
fprintf(logFile, "%s - Infected!\n", name);
}
else {
printf("%s - Clean\n", name);
fprintf(logFile, "%s - Clean\n", name);
}
}
// Quick Mode
else {
// get first 20%
subBuffer = writePart(buffer, subBuffer, length, &subLen,
FIRST_TWENTY_PRECENTS);
if (findSignature(virusSignature, subBuffer, virusLength,
subLen)) {
printf("%s - infected! (first 20%%)\n", name);
fprintf(logFile, "%s - infected! (first 20%%)\n", name);
}
else {
FREEME(subBuffer);
// get last 20%
subBuffer = writePart(buffer, subBuffer, length, &subLen,
LAST_TWENTY_PRECENTS);
if (findSignature(virusSignature, subBuffer, virusLength,
subLen)) {
printf("%s - Infected! (last 20%%)\n", name);
fprintf(logFile, "%s - Infected! (last 20%%)\n", name);
}
else {
// get the 60% left in the middle
subBuffer = writePart(buffer, subBuffer, length,
&subLen, MIDDLE_SIXTY_PRECENTS);
if (findSignature(virusSignature, subBuffer,
virusLength, subLen)) {
printf("%s - Infected!\n", name);
fprintf(logFile, "%s - Infected!\n", name);
}
else {
printf("%s - clean\n", name);
fprintf(logFile, "%s - Clean\n", name);
}
}
}
FREEME(subBuffer);
}
fclose(f);
}
else {
printf("No file found\n");
}
FREEME(filesList[i]);
FREEME(name);
}
fclose(logFile);
FREEME(log);
FREEME(buffer);
getchar();
}
/*
This function will write part of the file (beginning, middle or end) to a string
input: source (string) to take the information from, destination (string) to
write a part of the source to it, length (int) of the source string, a pointer
(int*) to store the new length of the destination string and part of the file
to write from (int) 1,2 or 3: first 20%, 60% in the middle and last 20%
accordingly
output: string containing the desired part of the source string
*/
char *
writePart(char *src, char *dest, int length, int *newLen, int part)
{
int i = 0;
int percentedLength = 0;
int count = 0;
// this len is 20% of the entire file's length
percentedLength = (int) (length / 5);
// NOTE/BUG: this does _not_ free the prior value -- memory leak!
switch (part) {
case FIRST_TWENTY_PRECENTS: // return beginning
ALLOCME(dest,percentedLength);
*newLen = percentedLength;
for (i = 0; i < percentedLength; i++) {
*(dest + i) = *(src + i);
}
break;
case MIDDLE_SIXTY_PRECENTS: // return middle
// allocate space for the middle: The entire file size minus 20% from
// the start and 20% from the end
ALLOCME(dest,length - 2 * percentedLength);
*newLen = length - 2 * percentedLength;
for (i = percentedLength; i < length - percentedLength; i++) {
*(dest + count) = *(src + i);
count++;
}
break;
case LAST_TWENTY_PRECENTS: // return end
ALLOCME(dest,percentedLength);
*newLen = percentedLength;
for (i = length - percentedLength; i < length; i++) {
*(dest + count) = *(src + i);
count++;
}
break;
}
return dest;
}
/*
function that finds the length of a file
input: file (FILE *)
output: the file's length (long)
*/
long
findLenOfFile(FILE * file)
{
long length = 0;
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
return length;
}
/*
function checks whether a file contains the virusSignature.
It iterates over each letter of the file and checks if it is the same as the
first letter in the virusSignature.
If it is, it checks the rest of the characters and returns True if a match is
found.
if not it continues the same process until the end of the file is reached.
input: The virusSignature (string), a buffer with the content of a file
(string), the signature's length (int)
and the buffer's length (int)
output: True if signature is in file, False otherwise
*/
int
findSignature(char *virusSignature, char *buffer, int sigLen, int bufferLen)
{
int found = 0;
int i = 0;
int j = 0;
for (i = 0; i < bufferLen - (sigLen - 1); i++) {
// check if a letter is the same as first letter in virusSignature
if (*(buffer + i) == *virusSignature)
{
found = TRUE;
// check if the rest of the letters match the signature and stop
// if one doesn't
for (j = 1; (j < sigLen) && found; j++) {
if (*(buffer + (i + j)) != *(virusSignature + j)) {
found = FALSE;
}
}
// if we got a match, return true!
if (found) {
return TRUE;
}
}
}
return FALSE;
}
User contributions licensed under CC BY-SA 3.0