#include <exec/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <dialog/crypt.h>

void MyEncrypt(char *,char *,int);


UBYTE CRC;

void main(int argc,char **argv)

{int   hfd;
 UBYTE filename[256];
 UBYTE buf     [80];
 UBYTE outbuf  [80];

 UBYTE mychar;
 UBYTE found;

 long  numchars;
 long  pos;

 if (argc != 3)  exit(5);

 strcpy(filename, argv[2]);
printf("%s\n",filename);
 if ((hfd=open(filename, O_RDWR))==-1)
    {printf("Couldn't open handler\n");
     exit(10);
    }

 pos   = lseek(hfd,-2500,2);
 found = FALSE;
 while(read(hfd,&mychar,1)==1)
      {if (mychar==':')
          {read(hfd,&mychar,1);
           if (mychar=='-')
              {read(hfd,&mychar,1);
               if (mychar==')')
                  {found = TRUE;
                   break;
                  }
              }
          }
      }

 if (!found)
    {printf("Unable to find location for string\n");
     close(hfd);
     exit(10);
    }

 pos = lseek(hfd, 165, 1);
 lseek(hfd, pos, 0);

 sprintf(buf, "Registered to %s", argv[1]);
 numchars = strlen(buf);
 MyEncrypt(buf, outbuf, numchars);

 write(hfd,  outbuf, numchars+1);
 write(hfd, &hfd, 78-numchars);
 write(hfd, &CRC, 1);

 strcpy(buf, "Unregistered pirated version -- it's not going to work");
 numchars = strlen(buf);
 MyEncrypt(buf, outbuf, numchars);

 write(hfd,  outbuf, numchars+1);
 write(hfd, &hfd, 78-numchars);
 write(hfd, &CRC, 1);

 close(hfd);
}


void MyEncrypt(char *inbuf,char *outbuf,int size)

{int counter;
 UBYTE shift;

 outbuf[size] = '\0';
 outbuf[0]    = size;
 shift        = size;

 for(counter=0;counter<size;counter++)
    {outbuf[counter+1] = inbuf[counter]+shift;
     shift             = outbuf[counter+1]-CRYPTOFFSET;
    }

 CRC = shift+MAGICNUMBER;
}
