/*
 
 PokerStars ini files decrypter
 
 (c) Fractal Guru
 
 03/05/2006

 This piece of code will allow you to decrypt any Pokerstars ini file (Windows or Mac).
 The decrypted content is gzipped, so you will need to gunzip the decrypted file.
 
 gcc -o decrypter Decrypter.c
 
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char *argv[])
  {
    FILE *fp,*op;

    unsigned long offset,offsetw;
    unsigned char data,newdata;
	char filename[150];
	char filenameout[150];
	char counter;
	// change to 0 to turn debug off
	int debug = 1;
	
	printf("\nPokerStars ini File Decryptor v0.1\n(c) Fractal Guru\n\n");
    
	if(argc<=1){
		printf("Insert filename to decrypt:\n");
		scanf("%s",filename);
		printf("Insert filename to output:\n");
		scanf("%s",filenameout);
	}else {
		strncpy(filename, argv[1],150);
		strncpy(filenameout,argv[2],150);
	}

	printf("\nDecrypting file...\n");
	/* Open the file for reading. */

     if ( (fp = fopen(filename, "rb+")) == NULL)
     {
         fprintf(stderr, "\nError opening file %s.\n", filename);
         exit(1);
     }

	      if ( (op = fopen(filenameout, "wb")) == NULL)
     {
         fprintf(stderr, "\nError opening file %s.\n", filenameout);
         exit(1);
     }

       offset=0x0;
	   counter=0;
       unsigned char table[45]={0x63,0x27,0x26,0x26,0x4F,0x35,0x1D,0x07,0x19,0x45,0x59,0x21,0x37,
		                         0x3F,0x00,0x1B,0x1B,0x1A,0x11,0x1B,0x03,0x04,0x4C,0x65,0x37,0x00,
								 0x1D,0x1D,0x48,0x20,0x0B,0x3D,0x45,0x7B,0x55,0x36,0x16,0x00,0x19,
								 0x00,0x59,0x06,0x1C,0x02,0x20};

         while(feof(fp)==0) 
		 {			 
		  /* Move the position indicator to the specified element. */
	      //offsettemp = 0x0;
		  offsetw = offset;

		  /* Get the file pointer located in the beginning offset*/
          fseek(fp, offset, 0);
         
         /* Read in a single integer. */
	     
         fread(&data, sizeof(char), 1, fp);
		 /* Make the XOR with size */
		 if (debug) printf("Position: %d Key: %X Byte to decrypt: %X ",counter,table[counter],data);
		 data = data ^ table[counter];
		 if (debug) printf("Decrypted byte: %X\n",data);
		 counter++;

		 if ( counter >= 45)
		 {
			 counter = 0;
		 }

		 /* Get the file pointer back in the before position */
         
		 fseek(op, offsetw, 0);
         
		 fprintf(op,"%c", data);

		 /* advance...*/
		 offset = offset + 0x1;
		 data = 0;
		 newdata = 0;
		 /* We need a read, so feop can detect the end of the file! */
		 fread(&data, sizeof(char), 1, fp);
		 }
     fclose(fp);
	 fclose(op);
     printf("File %s is decrypted...\nNow you can gunzip the output file!\n\n", filenameout);
	 return(0);
  } 

