#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <dos.h>
#include <process.h>

//void writeKeyFile(char *str);
int readKeyFile(char *str);


//char *keysite = "ELECTRON BBS";
//int keymakerversion = 100;

void main(/*int argc, char *argv[]*/)
{
char chaine[255]="";


/*if (argc < 3)
	{
	printf("PowerPPL KeyFile Reader v1.0\n"
		   "\n"
		   "Usage :\n"
		   "READKEY FILENAME.EXT\n");
	sound(1000);
	delay(500);
	nosound();
	exit(255);
	}*/

/*struct date d;
getdate(&d);
struct time t;
gettime(&t);*/

/*sprintf(chaine,"%s %s\xFF%02d\xFF%02d\xFF%04d\xFF%2d\xFF%02d\xFF%02d"
			   "\xFF%s"
			   "\xFF\x01"
			   "\xFF%03d"
			   ,argv[1]
			   ,argv[2]
			   ,d.da_mon
			   ,d.da_day
			   ,d.da_year
			   ,t.ti_hour
			   ,t.ti_min
			   ,t.ti_sec
			   ,keysite
			   ,keymakerversion);

writeKeyFile(chaine);*/

chaine[0]=0;

if (readKeyFile(chaine))
	printf("No keyfile found / Invalid keyfile\n");
else
//readKeyFile(chaine);
	printf("%s\n", chaine);

}


/*void writeKeyFile(char *str)
{
int a;
int b;
unsigned int seed;
FILE *out;
unsigned char c;
unsigned int sum=0;

restartall:

if ((out = fopen("PP.KEY", "wb"))
	== NULL)
{
   fprintf(stderr, "Cannot open output file.\n");
   return;
}
fwrite("PowerPPL KeyFile ", 17, 1, out);

randomize();
retir:
seed = rand();

if (((seed & 0xFF) < 32) || ((seed & 0xFF) > 127) || (((seed & 0xFF00) >> 8) < 32) || (((seed & 0xFF00) >> 8) > 127))
	goto retir;

fwrite(&seed, sizeof(seed), 1, out);

srand(seed);
sum = strlen(str);
fwrite(&sum, sizeof(sum), 1, out);
sum=0;
for (a=0;a<strlen(str);a++)
	{
	c = (unsigned char)random(95)+33;
	if (c > 80)
		{
		while(1)
			{
			c = random(95)+33;
			fputc(c,out);
			if (random(30) == 6) break;
			}
		c = (unsigned char)random(95)+33;
		}
	if (c > 50 && c < 100)
		c += random(95)+33;
	lp1:
	if (c <= 32)
		c += random(95)+33;
	if (c >= 127)
		c -= random(95)+33;
	if (c <= 32)
		goto lp1;
	sum += str[a];
	c = str[a] ^ c;
	b = 0;
	while (c < 33 || c > 127)
		{
		b++;
		c += random(95)+33;
		if (b > 127-33)
			{
			fclose(out);
			goto restartall;
			}
		}
	fputc(c,out);
	c = b + 33;
	fputc(c,out);
	}

b=0;
c = (sum & 0xFF00) >> 8;
while (c < 33 || c > 127)
	{
	b++;
	c += random(95)+33;
	if (b > 127-33)
		{
		fclose(out);
		goto restartall;
		}
	}
fputc(c,out);
c = b + 33;
fputc(c,out);

b=0;
c = (sum & 0xFF);
while (c < 33 || c > 127)
	{
	b++;
	c += random(95)+33;
	if (b > 127-33)
		{
		fclose(out);
		goto restartall;
		}
	}
fputc(c,out);
c = b + 33;
fputc(c,out);

fclose(out);

}*/


int readKeyFile(char *str)
{

FILE *in;
int a;
unsigned int seed;
unsigned int sum=0;
unsigned int siz=0;
unsigned char c;
unsigned char e;
unsigned char f;
char *d = "X";
char hdr[19];

if ((in = fopen("PP.KEY", "rb"))
	== NULL)
{
   fprintf(stderr, "Cannot open input file.\n");
   return 1;
}

fread(&hdr, 17, 1, in);
hdr[17]=0;
if (!strcmp(hdr,"PowerPPL KeyFile "))
	{
	fread(&seed, sizeof(seed), 1, in);
	srand(seed);
	}
else
	randomize();


fread(&siz, sizeof(siz), 1, in);

for (a=0;a < siz;a++)
	{
	c = (unsigned char)random(95)+33;
	if (c > 80)
		{
		while (1)
			{
			if (fgetc(in) != random(95)+33)
				while (1)
					if (random(50) == 6) break;
			if (random(30) == 6) break;
			}
		c = (unsigned char)random(95)+33;
		}
	if (c > 50 && c < 100)
		c += random(95)+33;
	lp2:
	if (c <= 32)
		c += random(95)+33;
	if (c >= 127)
		c -= random(95)+33;
	if (c <= 32)
		goto lp2;
	f = fgetc(in);
	e = getc(in)-33;
	for (;e > 0;e--)
		f -= random(95)+33;

	d[0] = f ^ c;
	sum += d[0];
	if (feof(in) || strlen(str) > 250)
		break;
	strcat(str, d);
	}

f = fgetc(in);
e = getc(in)-33;
for (;e > 0;e--)
	f -= random(95)+33;

siz = f << 8;

f = fgetc(in);
e = getc(in)-33;
for (;e > 0;e--)
	f -= random(95)+33;

siz += f;

fclose(in);

return(siz != sum);

}