The code is as follows.
Code:
struct encrypt
{
unsigned int signature;
unsigned long hash;
unsigned start_offset;
unsigned long long length;
} encfile;
void main()
{
unsigned long crchash;
// unsigned long long adlerhash;
char filename[25];
char filename2[25] ="enc";
char password[20];
unsigned long long len;
unsigned int *sig;
FILE *file1, *file2;
char *buffer;
char *buffer2; //buffer to hold the file data.
clrscr();
fflush(stdin);
printf("\n\n\t\t\t\tFILE ENCRYPTER V 0.1B");
printf("\\n\nEnter Filename: ");
gets(filename);
if(file1 = fopen(filename,"rb") == NULL)
{
printf("\n\n\n\n\t\t\tError: Cannot open File. ");
exit(-1);
}
fseek(file1, 0, SEEK_END);
len = ftell(file1);
fseek(file1, 0, SEEK_SET);
if((buffer = (char *) malloc(len+1)) == NULL)
{
clrscr();
printf("Cannot allocate memory...in first malloc");
fclose(file1);
exit(-2);
}
fread(buffer, len, 1, file1);
memcpy(sig, buffer, 2);
if(*sig != 0x0E1C)
{
clrscr();
printf("Enter password to encrypt the file: ");
gets(password);
crchash = crc32(password, strlen(password));
encfile.signature = 0x0E1C;
encfile.hash = crchash;
encfile.start_offset = 0x06;
encfile.length = len;
if((buffer2 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
{
clrscr();
printf("Cannot allocate memory...in second malloc");
fclose(file1);
exit(-2);
}
memcpy(buffer2, &encfile, sizeof(encfile));
memcpy(buffer2, buffer, len);
strcat(filename2, filename);
if(fopen(filename2, "wb") == NULL)
{
printf("\n\n\n\t\tCannot create file...");
fclose(file1);
exit(-1);
}
fwrite(buffer, sizeof(encfile)+len+1, 1, file2);
printf("\n\nFile encrypted....");
getch();
}
}
Pls take note that i have only done the encrypting conditon . but the error is on the fairst malloc call to allocate memeory to the buffer..
Can you please guide me what am i doing wrong.