Thread: Null pointer asignement in turbo C on DOSBOX

  1. #16
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Why cant i get the file length of a binary file.
    i tired this also, but it returns 0.
    Code:
    length = lseek(file1, 0, SEEK_END);

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Why don't you post your latest code instead of picking lines out at random.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Here it is..

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <io.h>
    
    #define XOR ^
    #define DIV_BY_2(x) (x>>1)
    #define POLYNOMIAL 0xEDB88320
    
    struct encrypt
    {
    	unsigned int signature;
    	unsigned long hash;
    	unsigned start_offset;
    	unsigned long long length;
    } encfile;
    
    
    //void encrypt(char *s
    
    
    /* polynomial is  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
    That polynomial binary is
    
    	E	D	B	8	8	3	2	0
           1110   1101    1011    1000     1000    0011    0010    0000
    
    Now on reverse order from backward.
           0000   0100    1100    0001     0001    1101    1011    0111
    implying 1 on 32th bit position
      1    0000   0100    1100    0001     0001    1101    1011    0111
    Now getting from left starting with x^0 to x^32, we get the above polynomial.
    */
    
    
    
    unsigned long crc_table[256];
    
    /* Function to generate the CRC32 values using the standard polynomial, instead of generating you can copy the 256 diff values and fill the table */
    void make_crc_table(void)
    {
        unsigned long i,c;
        int j;
        for (i = 0; i < 256; i++)
        {
    	c = i;
    	for (j = 0; j < 8; j++)
    	{
    		if((c & 0x1) ==1)
    			c = POLYNOMIAL XOR DIV_BY_2(c); /* this is the shortcut to divide by 2 */
    		else
    			c = DIV_BY_2(c); /*This is acually sgiftin to right by 1 bit here... */
    	}
    	crc_table[i] = c;
        }
    }
    
    /* Function to calculate crc32 of data in address pointed by buf of size len */
    
    unsigned long crc32(unsigned char *buf, int len)
    {
        unsigned long c = 0xFFFFFFFF;
        int i;
        for (i = 0; i < len; i++)
        {
    	c = crc_table[(c XOR buf[i]) & 0xFF] XOR (c >> 8);
        }
        return c XOR 0xFFFFFFFF;
    }
    
    
    
    unsigned long long adler32(unsigned char *src, unsigned long len) /* where src is pointer to data and len is its length */
    {
        unsigned long long a = 1, b = 0;
        unsigned long i;
    
        /* Process each byte of the data in order */
        for (i = 0; i < len; ++i)
        {
    	a+= *(src+i); /* we r adding a with the ascii value of each character */
    	b+= a; /* as well as on same pass we r adding a+b into b */
        }
    
        return (b << 16) | a; /*concatenation of int b with int a on a long int
        but LSB part a and MSB part b) */
    }
    
    void main()
    {
    	unsigned long crchash;
      //	unsigned long long adlerhash;
    
    	char filename[25];
    	char filename2[25] ="enc";
    	char filename3[25] = "dec";
    	char password[20];
    	long len = 0;
    	unsigned int *sig;
    	int 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 = open(filename, O_RDONLY | O_BINARY) == -1)
    	{
    		printf("\n\n\n\n\t\t\tError: Cannot open File. ");
    		getch();
    		exit(-1);
    	}
    	len = lseek(file1, 0L, SEEK_END);
    
    
    
    	//debugging lines
    
    	printf("file len: %ld",len);
    	getch();
    
    	//debugging lines end here..
    
    	if((buffer = (char *) malloc(len)) == NULL)
    	{
    		clrscr();
    		printf("Cannot allocate memory...in first malloc");
    		close(file1);
    		getch();
    		exit(-2);
    	}
    
    	read(file1, buffer, len);
    
    	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)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in second malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		memcpy(buffer2, &encfile, sizeof(encfile));
    		memcpy(buffer2, buffer, len);
    
    		strcat(filename2, filename);
    
    		if(open(filename2, O_CREAT | O_BINARY) == -1)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			close(file1);
    			getch();
    			exit(-1);
    		}
    
    		write(file2, buffer2, sizeof(encfile)+len);
    
    		printf("\n\nFile encrypted....");
    		free(buffer);
    		free(buffer2);
    
    		getch();
    	}
    	else
    	{
    		free(buffer);
    		lseek(file1, 0, SEEK_SET);
    		if((buffer = (char *) malloc(len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in third malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		if((buffer2 = (char *) malloc(sizeof(encfile))) == NULL)
    		{
    			clrscr();
    			free(buffer);
    			printf("Cannot allocate memory...in fourth malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		read(file1, &encfile, sizeof(encfile));
    
    
    		printf("Enter password to decrypt: ");
    		gets(password);
    		crchash = crc32(password, strlen(password));
    		if(crchash != encfile.hash)
    		{
    			free(buffer);
    			free(buffer2);
    			close(file1);
    			clrscr();
    			printf("\n\n\t\t\t Incorrect password...");
    			getch();
    			exit(-1);
    		}
    
    		free(buffer2);
    		if((buffer2 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    
    		lseek(file1, encfile.start_offset, SEEK_SET);
    		memcpy(buffer2, buffer, encfile.length);
    		strcat(filename3, filename);
    		if(open(filename3, O_CREAT | O_BINARY) == -1)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			close(file1);
    			getch();
    			exit(-1);
    		}
    
    
    		write(file2,buffer,encfile.length);
    
    		printf("\n\n\nFile decrypted....");
    
    		getch();
    	}
    
    
    /*
    	char str1[20];
    	clrscr();
    	make_crc_table();
    	printf("\nEnter a string: ");
    	gets(str1);
    	crchash=crc32(str1,strlen(str1));
    	adlerhash=adler32(str1,strlen(str1));
    	printf("\n\nThe CRC32 hash of the string is %lX",crchash);
    	printf("\n\nThe ADLER32 hash of the string is %llX",adlerhash);
    	getch();
    */
    getch();
    
    }

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(file1 = open(filename, O_RDONLY | O_BINARY) == -1)
    In other words, the same mistake I pointed out 12 hours ago.

    You've implicitly written this
    if(file1 = (open(filename, O_RDONLY | O_BINARY) == -1))

    What you should have written is this
    if((file1 = open(filename, O_RDONLY | O_BINARY)) == -1)

    Not to mention all the void main / gets / fflush stdin crap.

    If you're not going to listen and just keep editing away so long as it compiles, then just let us know and we can ignore you.

    We don't write these things because we like the sound our keyboards make, we write these things because the code is WRONG!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Yeh got it finally to work with , thanks for that..

  6. #21
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Yeah finally solved all problems, but the result is not as i expected..
    Can some one help me what is wrong..
    I just wanted to encrypt the file using the passwords truncated 1 byte adler32 hash..

    But it doesnt work as i thought..


    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define XOR ^
    #define DIV_BY_2(x) (x>>1)
    #define POLYNOMIAL 0xEDB88320
    
    
    
    void display(char *src , long len)
    {
    	int i;
    	for(i=0; i<=len; i++)
    		printf("%X",*src+i);
    }
    
    void encdec (char *dest, char *src, char *password, long len)
    {
    	int i;
    	long long hash = adler32(password, strlen(password));
    	char hash1 = hash>>24;
    	for(i=0; i<=len; i++);
    	{
    		*dest = (*src XOR hash1);
    	}
    }
    	
    
    long filesize(FILE *stream)
    {
       long curpos, length;
    
       curpos = ftell(stream);
       fseek(stream, 0L, SEEK_END);
       length = ftell(stream);
       fseek(stream, curpos, SEEK_SET);
       return length;
    }
    
    struct encrypt
    {
    	unsigned int signature;
    	unsigned long hash;
    	unsigned start_offset;
    	unsigned long long length;
    } encfile;
    
    
    
    /* polynomial is  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
    That polynomial binary is
    
    	E	D	B	8	8	3	2	0
           1110   1101    1011    1000     1000    0011    0010    0000
    
    Now on reverse order from backward.
           0000   0100    1100    0001     0001    1101    1011    0111
    implying 1 on 32th bit position
      1    0000   0100    1100    0001     0001    1101    1011    0111
    Now getting from left starting with x^0 to x^32, we get the above polynomial.
    */
    
    unsigned long crc_table[256];
    
    /* Function to generate the CRC32 values using the standard polynomial, instead of generating you can copy the 256 diff values and fill the table */
    void make_crc_table(void)
    {
        unsigned long i,c;
        int j;
        for (i = 0; i < 256; i++)
        {
    	c = i;
    	for (j = 0; j < 8; j++)
    	{
    		if((c & 0x1) ==1)
    			c = POLYNOMIAL XOR DIV_BY_2(c); /* this is the shortcut to divide by 2 */
    		else
    			c = DIV_BY_2(c); /*This is acually sgiftin to right by 1 bit here... */
    	}
    	crc_table[i] = c;
        }
    }
    
    /* Function to calculate crc32 of data in address pointed by buf of size len */
    
    unsigned long crc32(unsigned char *buf, int len)
    {
        unsigned long c = 0xFFFFFFFF;
        int i;
        for (i = 0; i < len; i++)
        {
    	c = crc_table[(c XOR buf[i]) & 0xFF] XOR (c >> 8);
        }
        return c XOR 0xFFFFFFFF;
    }
    
    
    
    unsigned long long adler32(unsigned char *src, unsigned long len) /* where src is pointer to data and len is its length */
    {
        unsigned long long a = 1, b = 0;
        unsigned long i;
    
        /* Process each byte of the data in order */
        for (i = 0; i < len; ++i)
        {
    	a+= *(src+i); /* we r adding a with the ascii value of each character */
    	b+= a; /* as well as on same pass we r adding a+b into b */
        }
    
        return (b << 16) | a; /*concatenation of int b with int a on a long int
        but LSB part a and MSB part b) */
    }
    
    void main()
    {
    	unsigned long crchash;
    	//unsigned long long adlerhash;
    
    	char filename[25];
    	char filename2[25] = "enc";
    	char filename3[25] = "dec";
    	char password[20];
    	long len;
    	unsigned int *sig;
    	FILE *file1, *file2;
    	char *buffer;
    	char *buffer2;	//buffer to hold the file data.
    	char *buffer3;	//buffer to hold enc/dec content.
    
    	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);
    	}
    
    	len = filesize(file1);
    	
    	//debug start
    	printf("\n\nThe length is %ld", len);
    	getch();
    	// debug end
    	
    	fseek(file1, 0, SEEK_SET);
    
    	if((buffer = (char *) malloc(len+1)) == NULL)	// dynamically allocating block of memory for buffer of file length.
    	{
    		clrscr();
    		printf("Cannot allocate memory...");
    		fclose(file1);
    		getch();
    		exit(-2);
    	}
    	
    	//debug start
    	printf("\n\nMalloc succeded..");
    	//debug end
    
    	fread(buffer, len, 1, file1);		// Reading the whole file into buffer..
    
    	//debug start
    	clrscr();
    	printf("File content: \n\n");
    	display(buffer, len);
    	getch();
    	//debug end
    
    	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.start_offset = 0x06;
    		encfile.hash = crchash;
    		encfile.length = len;
    
    		if((buffer2 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...");
    			fclose(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n\n buffer2 malloc success..");
    		//debug end
    
    		memcpy(buffer2, &encfile, sizeof(encfile));
    		memcpy(buffer2, buffer, len);
    
    		//debug start
    		clrscr();
    		printf("File content: \n\n");
    		display(buffer2, len);
    		getch();
    		//debug end
    
    
    		if((buffer3 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    
    		encdec(buffer3, buffer2, password, encfile.length);
    
    
    
    		strcat(filename2, filename);
    
    		if((file2 = fopen(filename2, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    
    			exit(-1);
    		}
    
    		fwrite(buffer3, sizeof(encfile)+len+1, 1, file2);
    
    		printf("\n\nFile encrypted....");
    		getch();
    	}
    
    	else
    	{
    		free(buffer);
    		fseek(file1, 0, SEEK_SET);
    		if((buffer = (char *) malloc(len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in third malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n 3rd malloc sucess...");
    		//debug end
    
    		if((buffer2 = (char *) malloc(sizeof(encfile))) == NULL)
    		{
    			clrscr();
    			free(buffer);
    			printf("Cannot allocate memory...in fourth malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		fread(&encfile, sizeof(encfile), 1, file1);
    
    
    		printf("Enter password to decrypt: ");
    		gets(password);
    		crchash = crc32(password, strlen(password));
    		if(crchash != encfile.hash)
    		{
    			free(buffer);
    			free(buffer2);
    			close(file1);
    			clrscr();
    			printf("\n\n\t\t\t Incorrect password...");
    			getch();
    			exit(-1);
    		}
    
    		free(buffer2);
    		if((buffer2 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			close(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    
    		fseek(file1, encfile.start_offset, SEEK_SET);
    		memcpy(buffer2, buffer, encfile.length);
    
    		if((buffer3 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    
    		encdec(buffer3, buffer2, password, encfile.length);
    
    		strcat(filename3, filename);
    		if((file2 =fopen(filename3, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    			exit(-1);
    		}
    
    
    		fwrite(buffer3,encfile.length, 1, file2);
    
    		printf("\n\n\nFile decrypted....");
    
    		getch();
    	}
    		
    	
    /*
    	char str1[20];
    	clrscr();
    	make_crc_table();
    	printf("\nEnter a string: ");
    	gets(str1);
    	crchash=crc32(str1,strlen(str1));
    	adlerhash=adler32(str1,strlen(str1));
    	printf("\n\nThe CRC32 hash of the string is %lX",crchash);
    	printf("\n\nThe ADLER32 hash of the string is %llX",adlerhash);
    	getch();
    */
    }

  7. #22
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by bluemimmosa View Post
    But it doesnt work as i thought..
    And what is wrong? My mind-reading capabilities are rather weak.

    Maybe it has something to do with the following:
    Code:
    void encdec (char *dest, char *src, char *password, long len)
    {
    ...
        for(i=0; i<=len; i++);
        {
            *dest = (*src XOR hash1);
        }
    Neither "*src" nor "hash1" change inside the for-loop thus you are assigning len+1 times the same value to "*dest".

    Code:
    if((buffer2 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
    ...
    memcpy(buffer2, &encfile, sizeof(encfile));
    memcpy(buffer2, buffer, len);
    The malloc()-call suggests that you want to store both "encfile" and "buffer" in "buffer2". But your second memcpy() overwrites the first one.

    And if you don't throw out gets() I will ignore your future posts.

    Bye, Andreas

  8. #23
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Well i got rid of gets() and then i changed the encdec funtion to simple as well as used the i value on src to pick different bytes.. and i also updated the buffer2 pointer to point to last byte... so i can then add other content in it.. Though i am not sure it will work or not..

    and thanks Andreas...
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define XOR ^
    #define DIV_BY_2(x) (x>>1)
    #define POLYNOMIAL 0xEDB88320
    
    
    
    void display(char *src , long len)
    {
    	int i;
    	for(i=0; i<=len; i++)
    		printf("%X",*src+i);
    }
    
    void encdec (char *dest, char *src, char *password, long len)
    {
    	int i;
    	char hash = *password;
    	for(i=0; i<=len; i++);
    	{
    		*dest = *(src+i) XOR hash;
    	}
    }
    	
    
    long filesize(FILE *stream)
    {
       long curpos, length;
    
       curpos = ftell(stream);
       fseek(stream, 0L, SEEK_END);
       length = ftell(stream);
       fseek(stream, curpos, SEEK_SET);
       return length;
    }
    
    struct encrypt
    {
    	unsigned int signature;
    	unsigned long hash;
    	unsigned start_offset;
    	unsigned long long length;
    } encfile;
    
    
    
    /* polynomial is  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
    That polynomial binary is
    
    	E	D	B	8	8	3	2	0
           1110   1101    1011    1000     1000    0011    0010    0000
    
    Now on reverse order from backward.
           0000   0100    1100    0001     0001    1101    1011    0111
    implying 1 on 32th bit position
      1    0000   0100    1100    0001     0001    1101    1011    0111
    Now getting from left starting with x^0 to x^32, we get the above polynomial.
    */
    
    unsigned long crc_table[256];
    
    /* Function to generate the CRC32 values using the standard polynomial, instead of generating you can copy the 256 diff values and fill the table */
    void make_crc_table(void)
    {
        unsigned long i,c;
        int j;
        for (i = 0; i < 256; i++)
        {
    	c = i;
    	for (j = 0; j < 8; j++)
    	{
    		if((c & 0x1) ==1)
    			c = POLYNOMIAL XOR DIV_BY_2(c); /* this is the shortcut to divide by 2 */
    		else
    			c = DIV_BY_2(c); /*This is acually sgiftin to right by 1 bit here... */
    	}
    	crc_table[i] = c;
        }
    }
    
    /* Function to calculate crc32 of data in address pointed by buf of size len */
    
    unsigned long crc32(unsigned char *buf, int len)
    {
        unsigned long c = 0xFFFFFFFF;
        int i;
        for (i = 0; i < len; i++)
        {
    	c = crc_table[(c XOR buf[i]) & 0xFF] XOR (c >> 8);
        }
        return c XOR 0xFFFFFFFF;
    }
    
    
    
    //Unused function so lets stop it...
    //unsigned long long adler32(unsigned char *src, unsigned long len) /* where src is pointer to data and len is its length */
    
    //{
    //    unsigned long long a = 1, b = 0;
    //	  unsigned long i;
    //
        /* Process each byte of the data in order */
    //    for (i = 0; i < len; ++i)
    //    {
    //	a+= *(src+i); /* we r adding a with the ascii value of each character */
    //	b+= a; /* as well as on same pass we r adding a+b into b */
    //    }
    
    //    return (b << 16) | a; /*concatenation of int b with int a on a long int
    //    but LSB part a and MSB part b) */
    //}
    
    
    void main()
    {
    	unsigned long crchash;
    	//unsigned long long adlerhash;
    
    	char filename[25];
    	char filename2[25] = "enc";
    	char filename3[25] = "dec";
    	char password[20];
    	long len;
    	unsigned int *sig;
    	FILE *file1, *file2;
    	char *buffer;
    	char *buffer2;	//buffer to hold the file data.
    	char *buffer3;	//buffer to hold enc/dec content.
    
    	printf("\n\n\t\t\t\tFILE ENCRYPTER V 0.1B");
    	printf("\\n\nEnter Filename: ");
    	scanf("%s", filename);
    	if((file1 = fopen(filename,"rb")) == NULL)
    	{
    		printf("\n\n\n\n\t\t\tError: Cannot open File. ");
    		exit(-1);
    	}
    
    	len = filesize(file1);
    	
    	//debug start
    	printf("\n\nThe length is %ld", len);
    	getch();
    	// debug end
    	
    	fseek(file1, 0, SEEK_SET);
    
    	if((buffer = (char *) malloc(len+1)) == NULL)	// dynamically allocating block of memory for buffer of file length.
    	{
    		clrscr();
    		printf("Cannot allocate memory...");
    		fclose(file1);
    		getch();
    		exit(-2);
    	}
    	
    	//debug start
    	printf("\n\nMalloc succeded..");
    	//debug end
    
    	fread(buffer, len, 1, file1);		// Reading the whole file into buffer..
    
    	//debug start
    	clrscr();
    	printf("File content: \n\n");
    	display(buffer, len);
    	getch();
    	//debug end
    
    	memcpy(sig, buffer, 2);
    	if(*sig != 0x0E1C)
    	{
    
    
    		clrscr();
    		printf("Enter password to encrypt the file: ");
    		scanf("%s", password);
    		crchash = crc32(password, strlen(password));
    		encfile.signature = 0x0E1C;
    		encfile.start_offset = 0x06;
    		encfile.hash = crchash;
    		encfile.length = len;
    
    		if((buffer2 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...");
    			fclose(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n\n buffer2 malloc success..");
    		//debug end
    
    		memcpy(buffer2, &encfile, sizeof(encfile));
    		
    		//debug start
    		clrscr();
    		printf("File content: \n\n");
    		display(buffer2, len);
    		getch();
    		//debug end
    
    
    		if((buffer3 = (char *) malloc(sizeof(encfile)+len+1)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    
    		encdec(buffer3, buffer, password, encfile.length);
    		buffer2 = buffer2 + sizeof(encfile);	// I think it makes buffer to point to the last byte.
    		memcpy(buffer2, buffer3, len);
    
    
    
    		strcat(filename2, filename);
    
    		if((file2 = fopen(filename2, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    
    			exit(-1);
    		}
    
    		fwrite(buffer3, sizeof(encfile)+len+1, 1, file2);
    
    		printf("\n\nFile encrypted....");
    		getch();
    	}
    
    	else
    	{
    		free(buffer);
    		fseek(file1, 0, SEEK_SET);
    		if((buffer = (char *) malloc(len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in third malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n 3rd malloc sucess...");
    		//debug end
    
    		if((buffer2 = (char *) malloc(sizeof(encfile))) == NULL)
    		{
    			clrscr();
    			free(buffer);
    			printf("Cannot allocate memory...in fourth malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		fread(&encfile, sizeof(encfile), 1, file1);
    
    
    		printf("Enter password to decrypt: ");
    		scanf("%s",password);
    		crchash = crc32(password, strlen(password));
    		if(crchash != encfile.hash)
    		{
    			free(buffer);
    			free(buffer2);
    			close(file1);
    			clrscr();
    			printf("\n\n\t\t\t Incorrect password...");
    			getch();
    			exit(-1);
    		}
    
    		free(buffer2);
    		if((buffer2 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			close(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    
    		fseek(file1, encfile.start_offset, SEEK_SET);
    		memcpy(buffer2, buffer, encfile.length);
    
    		if((buffer3 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    
    		encdec(buffer3, buffer2, password, encfile.length);
    
    		strcat(filename3, filename);
    		if((file2 =fopen(filename3, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    			exit(-1);
    		}
    
    
    		fwrite(buffer3,encfile.length, 1, file2);
    
    		printf("\n\n\nFile decrypted....");
    
    		getch();
    	}
    		
    }

  9. #24
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Is it necessary to empty the buffer that we allocated with malloc or it is not necessary... ?

  10. #25
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by bluemimmosa View Post
    Well i got rid of gets() and then i changed the encdec funtion to simple as well as used the i value on src to pick different bytes.. and i also updated the buffer2 pointer to point to last byte... so i can then add other content in it.. Though i am not sure it will work or not..
    Well, using scanf("%s") is not really an improvement if you care about buffer overflows. Haven't you read the link I've shown you? You'll find a better solution if you read it.

    Code:
    for(i=0; i<=len; i++);
    {
        *dest = *(src+i) XOR hash;
    }
    That was only half of the solution. What's about "*dest"? You are still overwriting the old value in each iteration.

    Code:
    buffer2 = buffer2 + sizeof(encfile);    // I think it makes buffer to point to the last byte.
    memcpy(buffer2, buffer3, len);
    If you assign a new value to "buffer2" you loose the old starting position (and consequently all the content which started from that position). That's probably not what you want.
    You need something like this if you want to concatenate two parts:
    Code:
    memcpy(buffer, first_part, len_of_first_part);
    memcpy(buffer + len_of_first_part, second_part, len_of_second_part);

    Bye, Andreas

  11. #26
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by bluemimmosa View Post
    Is it necessary to empty the buffer that we allocated with malloc or it is not necessary... ?
    There is no guarantee that the memory block is cleared. But usually you write something into it anyways. Why would you allocate memory if you don't use it?
    And if you don't need all the memory right from the beginning it is your job to keep track of the length of the current valid block.

    Bye, Andreas

  12. #27
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Well thanks for the buffer overflow protecting mechanisam, i can do it later on..
    i have fixed some pointers thing temporarily holing the start address.. and restoring them,,,. in some functions..
    pls have a look. Still the code is not functioning as it should have been.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define XOR ^
    #define DIV_BY_2(x) (x>>1)
    #define POLYNOMIAL 0xEDB88320
    
    
    
    void display(char *src , long len)
    {
    	int i;
    	for(i=0; i<=len; i++)
    		printf("0X%X\t",*src+i);
    }
    
    void encdec (char *dest, char *src, char *password, long len)
    {
    	int i;
    	char *temp;
    	char hash = *password;
    	temp = dest;
    	//debug start
    	clrscr();
    	printf("charcter pasword is %c",*password);
    	getch();
    	printf("A xored to my password is %c",'a' XOR hash);
    	//debug end
    
    	for(i=0; i<=len; i++);
    	{
    		*(dest+i) = *(src+i) XOR hash;
    	}
    	dest = temp;	// restoring the original buffer pointer
    }
    
    
    long filesize(FILE *stream)
    {
       long curpos, length;
    
       curpos = ftell(stream);
       fseek(stream, 0L, SEEK_END);
       length = ftell(stream);
       fseek(stream, curpos, SEEK_SET);
       return length;
    }
    
    struct encrypt
    {
    	unsigned int signature;
    	unsigned long hash;
    	unsigned int start_offset;
    	long length;
    } encfile;
    
    
    
    /* polynomial is  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
    That polynomial binary is
    
    	E	D	B	8	8	3	2	0
           1110   1101    1011    1000     1000    0011    0010    0000
    
    Now on reverse order from backward.
           0000   0100    1100    0001     0001    1101    1011    0111
    implying 1 on 32th bit position
      1    0000   0100    1100    0001     0001    1101    1011    0111
    Now getting from left starting with x^0 to x^32, we get the above polynomial.
    */
    
    unsigned long crc_table[256];
    
    /* Function to generate the CRC32 values using the standard polynomial, instead of generating you can copy the 256 diff values and fill the table */
    void make_crc_table(void)
    {
        unsigned long i,c;
        int j;
        for (i = 0; i < 256; i++)
        {
    	c = i;
    	for (j = 0; j < 8; j++)
    	{
    		if((c & 0x1) ==1)
    			c = POLYNOMIAL XOR DIV_BY_2(c); /* this is the shortcut to divide by 2 */
    		else
    			c = DIV_BY_2(c); /*This is acually sgiftin to right by 1 bit here... */
    	}
    	crc_table[i] = c;
        }
    }
    
    /* Function to calculate crc32 of data in address pointed by buf of size len */
    
    unsigned long crc32(unsigned char *buf, int len)
    {
        unsigned long c = 0xFFFFFFFF;
        int i;
        for (i = 0; i < len; i++)
        {
    	c = crc_table[(c XOR buf[i]) & 0xFF] XOR (c >> 8);
        }
        return c XOR 0xFFFFFFFF;
    }
    
    
    
    //Unused function so lets stop it...
    //unsigned long long adler32(unsigned char *src, unsigned long len) /* where src is pointer to data and len is its length */
    
    //{
    //    unsigned long long a = 1, b = 0;
    //	  unsigned long i;
    //
        /* Process each byte of the data in order */
    //    for (i = 0; i < len; ++i)
    //    {
    //	a+= *(src+i); /* we r adding a with the ascii value of each character */
    //	b+= a; /* as well as on same pass we r adding a+b into b */
    //    }
    
    //    return (b << 16) | a; /*concatenation of int b with int a on a long int
    //    but LSB part a and MSB part b) */
    //}
    
    
    void main()
    {
    	unsigned long crchash;
    	//unsigned long long adlerhash;
    
    	char filename[25];
    	char filename2[25] = "enc";
    	char filename3[25] = "dec";
    	char password[20];
    	long len;
    	unsigned int *sig;
    	FILE *file1, *file2;
    	char *buffer;
    	char *buffer2;	//buffer to hold the file data.
    	char *buffer3;	//buffer to hold enc/dec content.
    	char *temp;
    	make_crc_table();
    
    	printf("\n\n\t\t\t\tFILE ENCRYPTER V 0.1B");
    	printf("\\n\nEnter Filename: ");
    	scanf("%s", filename);
    	if((file1 = fopen(filename,"rb")) == NULL)
    	{
    		printf("\n\n\n\n\t\t\tError: Cannot open File. ");
    		exit(-1);
    	}
    
    	len = filesize(file1);
    
    	//debug start
    	printf("\n\nThe length is %ld", len);
    	getch();
    	// debug end
    
    	fseek(file1, 0, SEEK_SET);
    
    	if((buffer = (char *) malloc(len+1)) == NULL)	// dynamically allocating block of memory for buffer of file length.
    	{
    		clrscr();
    		printf("Cannot allocate memory...");
    		fclose(file1);
    		getch();
    		exit(-2);
    	}
    
    	//debug start
    	printf("\n\nMalloc succeded..");
    	//debug end
    
    	fread(buffer, len, 1, file1);		// Reading the whole file into buffer..
    
    	//debug start
    	clrscr();
    	printf("File content: \n\n");
    	display(buffer, len);
    	getch();
    	//debug end
    
    	memcpy(sig, buffer, 2);
    	if(*sig != 0x0E1C)
    	{
    
    
    		clrscr();
    		printf("Enter password to encrypt the file: ");
    		scanf("%s", password);
    		crchash = crc32(password, strlen(password));
    		encfile.signature = 0x0E1C;
    		encfile.start_offset = 0x06;
    		encfile.hash = crchash;
    		encfile.length = len;
    
    		if((buffer2 = (char *) malloc(sizeof(encfile)+len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...");
    			fclose(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n\n buffer2 malloc success..");
    		//debug end
    
    		memcpy(buffer2, &encfile, sizeof(encfile));
    		temp =buffer2;
    		//debug start
    		clrscr();
    		printf("File content: \n\n");
    		display(buffer2, len);
    		getch();
    		//debug end
    
    
    		if((buffer3 = (char *) malloc(sizeof(encfile)+len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    		buffer2 = temp;
    		encdec(buffer3, buffer, password, encfile.length);
    		memcpy(buffer2 + sizeof(encfile), buffer3, encfile.length);
    
    
    
    		strcat(filename2, filename);
    
    		if((file2 = fopen(filename2, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    
    			exit(-1);
    		}
    
    		fwrite(buffer2, sizeof(encfile)+len, 1, file2);
    
    		printf("\n\nFile encrypted....");
    		getch();
    	}
    
    	else
    	{
    		free(buffer);
    		fseek(file1, 0, SEEK_SET);
    		if((buffer = (char *) malloc(len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in third malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n 3rd malloc sucess...");
    		//debug end
    
    		if((buffer2 = (char *) malloc(sizeof(encfile))) == NULL)
    		{
    			clrscr();
    			free(buffer);
    			printf("Cannot allocate memory...in fourth malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		fread(&encfile, sizeof(encfile), 1, file1);
    
    
    		printf("Enter password to decrypt: ");
    		scanf("%s",password);
    		crchash = crc32(password, strlen(password));
    
    		//debug start
    		printf("\nCRC hash is %lX",crchash);
    		printf("\nStored hash is %lX",encfile.hash);
    		getch();
    		//debug end
    
    		if(crchash != encfile.hash)
    		{
    			free(buffer);
    			free(buffer2);
    			close(file1);
    			clrscr();
    			printf("\n\n\t\t\t Incorrect password...");
    			getch();
    			exit(-1);
    		}
    
    		free(buffer2);
    		if((buffer2 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			close(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    
    		fseek(file1, encfile.start_offset, SEEK_SET);
    		memcpy(buffer2, buffer, encfile.length);
    
    		if((buffer3 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    
    		encdec(buffer3, buffer2, password, encfile.length);
    
    		strcat(filename3, filename);
    		if((file2 =fopen(filename3, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    			exit(-1);
    		}
    
    
    		fwrite(buffer3,encfile.length, 1, file2);
    
    		printf("\n\n\nFile decrypted....");
    
    		getch();
    	}
    		
    }

  13. #28
    Registered User
    Join Date
    Mar 2010
    Posts
    22
    Well i removed the scanf too. and used the fgets as read on the link you sent..

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    #define XOR ^
    #define DIV_BY_2(x) (x>>1)
    #define POLYNOMIAL 0xEDB88320
    
    
    //debug display function to disply content so i can see whats going on..
    void display(char *src , long len)
    {
    	int i;
    	for(i=0; i<=len; i++)
    		printf("0X%X\t",*src+i);
    }
    
    // debug function ends
    
    
    
    //encdec function...
    void encdec (char *dest, char *src, char *password, long len)
    {
    	int i;
    	char *temp; //temp pointer to hold the address of dest so after operation  it can restore the pointer to its original loaction.
    	char hash = enccode(password, strlen(password));
    	temp = dest;
    	
    	//debug start
    	clrscr();
    	printf("Hash character isis %c",hash);
    	getch();
    	printf("A xored to my password is %c",'a' XOR hash);
    	//debug end
    
    	for(i=0; i<len; i++);
    	{
    		*(dest+i) = *(src+i) XOR hash;
    	}
    	dest = temp;	// restoring the original buffer pointer
    }
    
    // function to get filesize and reset file pointer to the begining of file.
    
    long filesize(FILE *stream)
    {
       long curpos, length;
    
       curpos = ftell(stream);
       fseek(stream, 0L, SEEK_END);
       length = ftell(stream);
       fseek(stream, curpos, SEEK_SET);
       return length;
    }
    
    
    //global structure encrypt with struct variable encfile
    struct encrypt
    {
    	unsigned int signature;	//The encrypt signature 0xE1C
    	unsigned long hash;		//The crc32 hash of password
    	unsigned int start_offset;	//The starting offset of the real data.. its actually 0x06;
    	long length;		//The length of actual data....
    } encfile;
    
    
    
    /* polynomial is  X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0
    That polynomial binary is
    
    	E	D	B	8	8	3	2	0
           1110   1101    1011    1000     1000    0011    0010    0000
    
    Now on reverse order from backward.
           0000   0100    1100    0001     0001    1101    1011    0111
    implying 1 on 32th bit position
      1    0000   0100    1100    0001     0001    1101    1011    0111
    Now getting from left starting with x^0 to x^32, we get the above polynomial.
    */
    
    unsigned long crc_table[256];
    
    /* Function to generate the CRC32 values using the standard polynomial, instead of generating you can copy the 256 diff values and fill the table */
    void make_crc_table(void)
    {
        unsigned long i,c;
        int j;
        for (i = 0; i < 256; i++)
        {
    	c = i;
    	for (j = 0; j < 8; j++)
    	{
    		if((c & 0x1) ==1)
    			c = POLYNOMIAL XOR DIV_BY_2(c); /* this is the shortcut to divide by 2 */
    		else
    			c = DIV_BY_2(c); /*This is acually sgiftin to right by 1 bit here... */
    	}
    	crc_table[i] = c;
        }
    }
    
    /* Function to calculate crc32 of data in address pointed by buf of size len */
    
    unsigned long crc32(unsigned char *buf, int len)
    {
        unsigned long c = 0xFFFFFFFF;
        int i;
        for (i = 0; i < len; i++)
        {
    	c = crc_table[(c XOR buf[i]) & 0xFF] XOR (c >> 8);
        }
        return c XOR 0xFFFFFFFF;
    }
    
    
    
    //custom function to generate a char value..
    char enccode(unsigned char *src, unsigned long len) /* where src is pointer to data and len is its length */
    
    {
    	unsigned long a = 1;
        unsigned int i;
    
        /* Process each byte of the data in order */
       for (i = 0; i < len; ++i)
       {
    		a+= *(src+i); /* we r adding a with the ascii value of each character */
    	}
    	return (char)a%256;
    }
    
    
    
    void main()
    {	
    	unsigned long crchash;	//variable to hold crchash	
    
    	char filename[25];	// variable to store first filename
    	char filename2[25] = "enc";	//variable for second filename
    	char filename3[25] = "dec";	//variable for third filename
    	char password[20];	//variable to store the password
    	long len;	//variable to hold the file length
    	unsigned int *sig;	//variable to hold the signature 0x0E1C
    	FILE *file1, *file2;	//File handling pointers
    	char *buffer;	//first buffer
    	char *buffer2;	//buffer to hold the file data.
    	char *buffer3;	//buffer to hold enc/dec content.
    	char *temp;	//temporary variable to save the original buffer address befor calling other functions which alters the pointer so later we can restore.
    	 
    	make_crc_table();	//call to function that generate the crc32 table..
    
    	printf("\n\n\t\t\t\tFILE ENCRYPTER V 0.1B");
    	printf("\\n\nEnter Filename: ");
    	
    	fflush(stdin);	//clearing the stdin buffer for safety.
    	fgets(filenanme, sizeof(filename), stdin);	//getting input
    	
    	if((file1 = fopen(filename,"rb")) == NULL)
    	{
    		printf("\n\n\n\n\t\t\tError: Cannot open File. ");
    		exit(-1);
    	}
    
    	len = filesize(file1);
    
    	//debug start
    	printf("\n\nThe length is %ld", len);
    	getch();
    	// debug end
    
    	fseek(file1, 0, SEEK_SET);
    
    	if((buffer = (char *) malloc(len+1)) == NULL)	// dynamically allocating block of memory for buffer of file length.
    	{
    		clrscr();
    		printf("Cannot allocate memory...");
    		fclose(file1);
    		getch();
    		exit(-2);
    	}
    
    	//debug start
    	printf("\n\nMalloc succeded..");
    	//debug end
    
    	fread(buffer, len, 1, file1);		// Reading the whole file into buffer..
    
    	//debug start
    	clrscr();
    	printf("File content: \n\n");
    	display(buffer, len);
    	getch();
    	//debug end
    
    	memcpy(sig, buffer, 2);
    	if(*sig != 0x0E1C)
    	{
    
    
    		clrscr();
    		fflush(stdin);
    		printf("Enter password to encrypt the file: ");
    		fgets(password, sizeof(password), stdin);
    		crchash = crc32(password, strlen(password));
    		encfile.signature = 0x0E1C;
    		encfile.start_offset = 0x06;
    		encfile.hash = crchash;
    		encfile.length = len;
    
    		if((buffer2 = (char *) malloc(sizeof(encfile)+len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...");
    			fclose(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n\n buffer2 malloc success..");
    		//debug end
    
    		memcpy(buffer2, &encfile, sizeof(encfile));
    		temp =buffer2;
    		//debug start
    		clrscr();
    		printf("File content: \n\n");
    		display(buffer2, len);
    		getch();
    		//debug end
    
    
    		if((buffer3 = (char *) malloc(sizeof(encfile)+len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    		buffer2 = temp;
    		encdec(buffer3, buffer, password, encfile.length);
    		memcpy(buffer2 + sizeof(encfile), buffer3, encfile.length);
    
    
    
    		strcat(filename2, filename);
    
    		if((file2 = fopen(filename2, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    
    			exit(-1);
    		}
    
    		fwrite(buffer2, sizeof(encfile)+len, 1, file2);
    
    		printf("\n\nFile encrypted....");
    		getch();
    	}
    
    	else
    	{
    		free(buffer);
    		fseek(file1, 0, SEEK_SET);
    		if((buffer = (char *) malloc(len)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in third malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		//debug start
    		printf("\n 3rd malloc sucess...");
    		//debug end
    
    		if((buffer2 = (char *) malloc(sizeof(encfile))) == NULL)
    		{
    			clrscr();
    			free(buffer);
    			printf("Cannot allocate memory...in fourth malloc");
    			close(file1);
    			getch();
    			exit(-2);
    		}
    
    		fread(&encfile, sizeof(encfile), 1, file1);
    
    
    		printf("Enter password to decrypt: ");
    		fflush(stdin);
    		fgets(password, sizeof(password), stdin);
    		crchash = crc32(password, strlen(password));
    
    		//debug start
    		printf("\nCRC hash is %lX",crchash);
    		printf("\nStored hash is %lX",encfile.hash);
    		getch();
    		//debug end
    
    		if(crchash != encfile.hash)
    		{
    			free(buffer);
    			free(buffer2);
    			close(file1);
    			clrscr();
    			printf("\n\n\t\t\t Incorrect password...");
    			getch();
    			exit(-1);
    		}
    
    		free(buffer2);
    		if((buffer2 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			close(file1);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    
    		fseek(file1, encfile.start_offset, SEEK_SET);
    		memcpy(buffer2, buffer, encfile.length);
    
    		if((buffer3 = (char *) malloc(encfile.length)) == NULL)
    		{
    			clrscr();
    			printf("Cannot allocate memory...in fifth malloc");
    			fclose(file1);
    			free(buffer2);
    			free(buffer);
    			getch();
    			exit(-2);
    		}
    
    		//function prototype to decrypt
    		// void encdec(char * dest, char * src, unsigned long hash, long len);
    
    		encdec(buffer3, buffer2, password, encfile.length);
    
    		strcat(filename3, filename);
    		if((file2 =fopen(filename3, "wb")) == NULL)
    		{
    			printf("\n\n\n\t\tCannot create file...");
    			fclose(file1);
    			free(buffer);
    			free(buffer2);
    			free(buffer3);
    			getch();
    			exit(-1);
    		}
    
    
    		fwrite(buffer3,encfile.length, 1, file2);
    
    		printf("\n\n\nFile decrypted....");
    
    		getch();
    	}
    		
    }

  14. #29
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by bluemimmosa View Post
    Well thanks for the buffer overflow protecting mechanisam, i can do it later on..
    i have fixed some pointers thing temporarily holing the start address.. and restoring them,,,. in some functions..
    pls have a look. Still the code is not functioning as it should have been.
    It is a waste of bandwidth to post like this; if your code has a issue, STATE THE ISSUE.
    This is not a code compile and test service.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. Null Pointer
    By saswatdash83 in forum C Programming
    Replies: 3
    Last Post: 07-15-2008, 04:12 AM
  3. pointer ot NULL
    By anthonye in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2003, 03:43 PM
  4. Null pointer
    By MethodMan in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:49 PM
  5. How I can Show the pointer of mouse int 800*600 256 color mode in Turbo C++?
    By hadizadeh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-12-2001, 07:17 AM