program donkey;
uses dos,crt,bbskv,bbskern;

type keytype=record
               keynumber:longint;
               bbsname:string[60];
               sysopname:string[60];
               cksum:longint;
               cksum2:longint;
               datemade:longint;
               product:word;
               nodes:word;
               version:word;
               gameid:longint;
               emptyspace:array[1..108] of byte;
             end;
     karray= array[1..256] of byte;

const mask='QUICKENING';
      mask2='%$1993!~';
      gameid:longint=$FBEAD9C8;

var keyfile: file of keytype;
    key    : keytype;
    ka     : karray absolute key;
    index  : integer;
    tmp    : longint;
    prods  : array[1..255] of string[40];
    fname  : string;

procedure descramble;
var index:integer;
    x,y,a,b,tmp:byte;
    m1,m2:string;
begin
  m1:=mask; m2:=mask2;
  index:=1;
  a:=length(m1);
  b:=length(m2);
  for index:=1 to 256 do
  begin
    x:=(index mod a)+1;
    y:=(index mod b)+1;
    tmp:=ka[index];
    tmp:=tmp xor ord(m2[y]);
    tmp:=tmp-ord(m1[x]);
    ka[index]:=tmp;
  end;
end;

begin
  prods[1]:='Rancho Nevada';
  prods[2]:='Purity Test';
  prods[3]:='UFO Communications Engine';
  prods[4]:='PagePlay';
  fname:=paramstr(1);
  if not(existfile(fname)) then halt;
  assign(keyfile,fname);
  reset(keyfile);
  read(keyfile,key);
  close(keyfile);
  descramble;
  tmp:=key.datemade;
  for index:=1 to length(key.bbsname) do tmp:=tmp-(1171*53*ord(key.bbsname[index]));
  for index:=1 to length(key.sysopname) do tmp:=tmp-(1171*53*ord(key.sysopname[index]));
  textattr:=lightred;
  writeln(' Prod: ',key.product,' (',prods[key.product],')');
  writeln('Sysop: ',key.sysopname);
  writeln('  BBS: ',key.bbsname);
  writeln('Key #: ',key.keynumber);
  if (key.gameid<>gameid) then writeln('Nodes: 4') else writeln('Nodes: ',key.nodes);
    write('Ver. : ');
    if (key.gameid=gameid) then writeln(key.version) else writeln('1');
  writeln('Cksum: ',key.cksum);
  writeln('Found: ',tmp);
  writeln('');
  if
    key.cksum=tmp
  then
    writeln('Valid Key!')
  else
    writeln('Invalid Key!');
end.
