/*-------------------------------------------------------------------------*
 * title        : crypto                                                   *
 * version      : v0.7                                                     *
 * programmer   : nam tran [neuropunk]                                     *
 * programmed   : december 1999                                            *
 * affiliations : neuropunk productions && neuronetwork                    *
 * urls	        : neuropunk.com && neuronetwork.cx                         *
 * e-mail       : neuroticism@hotmail.com                                  *
 * description  : Encrypts/decrypts ascii based documents. Uses a double   *
 *              : key XOR scheme [by passing info twice, you get the       *
 *              : original]. Out of a 500+ line document, I heard only     *
 *              : 1 beep. The beep means the engine was shitting on        *
 *              : something. Earlier the program would beep everywhere,    *
 *              : but most of that was fixed by telling the program to     *
 *              : skip tabs and white spaces. A beep will usually mean that*
 *              : some stuff will still remain encrypted or decrypted      *
 *              : this is due to it throwing off the keys. But only that   *
 *              : section will be affected, new lines aren't affected      *
 *              : unless they're afflicted w/ the same problem. Even       *
 *              : though what was previously mentioned is usually true,    *
 *              : when hand comparing the decrypted to the original        *
 *              : decrypted, I couldn't notice any differences.            *
 *-------------------------------------------------------------------------*/




/*---------*
 * headers *
 *---------*/
 #include <stdio.h>
 #include <stdlib.h>

/*------------------*
 * global variables *
 *------------------*/
 int amnt_info = 1000;	//a "line" can only have this much elements

 char *k1;
 char *k2;
 char *input;
 char *output;



/*---------------------*
 * function prototypes *
 *---------------------*/
 //the encrypting "engine"
 void engine(char *k1, char *k2, char *data);

 //opens file(s) for read and write, and passes info to the engine
 void passer(void);

 //get the location of files wanted, and where to save the info
 void f_info(void);

 //compare strings on a char by char basis, 0==no match, 1==match
 int str_cmp(char *s1, char *s2);

/*----------------------*
 * start of the program *
 *----------------------*/
 void main()
 {

 	//defined in global variables
	k1 = (char *) malloc(amnt_info * sizeof(char));
	k2 = (char *) malloc(amnt_info * sizeof(char));

//  the data variable is defined in the passer() function
//	data = (char *) malloc(amnt_info * sizeof(char));

	if((k1==NULL)||(k2==NULL))
	{
	   printf("[error allocating memory], exiting");
	   exit(1);
	}




	//getting the key's that will unlock/lock the data
	printf("\n\n");

	printf("enter key 1: ");
	fgets(k1, amnt_info, stdin);

	printf("enter key 2: ");
	fgets(k2, amnt_info, stdin);


	//getting location of files
	f_info();

	//this should technically end when the entire file has been
	//read and either encrypted or decrypted
	passer();
 }


/*-------------------------------------------------------*
 * name      : engine()                                  *
 * purpose   : encrypts a string                         *
 * accepts   : 3x char *                                 *
 * returns   : void                                      *
 * comments  : users a lot of loops/variables            *
 *-------------------------------------------------------*/
 void engine(char *k1, char *k2, char *data)
 {
	int x = 0;	//keeps track of k1
	int y = 0;	//keeps track of k2
	int z = 0;	//keeps track of data

	//technically, this thing should stop just right before
	//the new line

	//technically, the counter for the keys should loop when
	//it's on the new line, but doesn't use the new line in the
	//conversion process

	//if a "tab" is encountered, it will be skipped
	//white space will also be skipped

	for(x=0,y=0,z=0; data[z] != '\n'; x++, y++, z++)
	{
		if(k1[x] == '\n')
		{
			x = 0;
		}

		if(k2[y] == '\n')
		{
			y = 0;
		}

		if((data[z] != '\t') || (data[z] != ' '))
		{
			data[z] = data[z] ^ k1[x] ^ k2[y];
		}
	}
 }


/*-------------------------------------------------------*
 * name      : passer()                                  *
 * purpose   : passes info from files to the engine      *
 * accepts   : void                                      *
 * returns   : void                                      *
 * comments  : none                                      *
 *-------------------------------------------------------*/
 void passer(void)
 {
	FILE *neurowizardry;     //pointer to the original file [for reading]
	FILE *neurowizardry2;    //pointer to the new file [for writing]
	char *data;


	neurowizardry = fopen(input, "r");
	neurowizardry2 = fopen(output, "w+");

	if((neurowizardry==NULL)||(neurowizardry2==NULL))
	{
		printf("\n\n[error reading/writing file(s)], exiting\n\n");
		exit(1);
	}

	//the starting allocation for the data variable
	data = (char *) malloc(amnt_info * sizeof(char));
	if(data == NULL)
	{
		printf("[error allocating memory], exiting");
	}


	//this is ONLY going to read the info from info.txt, so it'll depend
	//on the initial condition of whether or not it will get decrypted
	//or encrypted
	while((fgets(data, amnt_info, neurowizardry) != NULL))
	{
		engine(k1, k2, data);
		printf(":%s", data);
		fprintf(neurowizardry2, "%s", data);

		free(data);
		data = (char *) malloc(amnt_info * sizeof(char));
		if(data==NULL)
		{
			printf("[error allocating memory], exiting");
			exit(1);
		}
	}

 	fclose(neurowizardry);
	fclose(neurowizardry2);
 }


/*-------------------------------------------------------*
 * name      : f_info()                                  *
 * purpose   : gets the file info                        *
 * accepts   : void                                      *
 * returns   : void                                      *
 * comments  : none                                      *
 *-------------------------------------------------------*/
 void f_info(void)
 {
	int status = 0; //0 == unacceptable, 1 == acceptable

	input = (char *) malloc(amnt_info * sizeof(char));
	output = (char *) malloc(amnt_info * sizeof(char));


	if((input==NULL)||(output==NULL))
	{
		printf("[error allocating memory], exiting");
		exit(1);
	}

	do
	{
		printf("\nenter location of input: ");
		gets(input);

		printf("enter location of output: ");
		gets(output);

		//some filters for error, not perfect though

		//ok.. i'm kind of wanting to work on some other programs, but
		//this is where you would put the filters

		status = 1;	//THIS IS A TEMP THING, just to end the loop

	}while(status==0);
 }


/*-------------------------------------------------------*
 * name      : str_cmp()                                 *
 * purpose   : cmp strings, 0==nomatch, 1==match         *
 * accepts   : 2x char                                   *
 * returns   : int                                       *
 * comments  : cmp'ing by basis of character             *
 *-------------------------------------------------------*/
 int str_cmp(char *s1, char *s2)
 {
	//need to find some time to implement this part

	return(0);
 }

