/*

Painkiller packet's password encoder/decoder 0.1
by Luigi Auriemma
e-mail: aluigi@autistici.org
web:    aluigi.org


INTRODUCTION
============
When you want to join a password protected game server of Painkiller
(http://www.painkillergame.com) your client sends a packet containing
the packet ID (0x04), your client version, the 72 bytes of the Gamespy
auth key (http://aluigi.org/papers/gskey-auth.txt) plus a
"strange" text string after it.
This text string is just the password you have used to join and it is
encrypted using the other text string (the server challenge) sent by
the server in its previous packet.
My optimized algorithm is able to decode/encode the password stored in
the packet sent by the client.


HOW TO USE
==========
The function is an algorithm used for both encoding and decoding
without differences.
It needs only 2 parameters:
- pwd: the client's password stored in the packet
- enc: the server challenge string

Example:
  #include "painkiller_pckpwd.h"

    unsigned char   pwd[] = "5mjblOpV8N",
                    enc[] = "k7bEv4cGcw";
    painkiller_pckpwd(pwd, enc);
    printf("Password: %s\n", pwd);  // the password is "mypassword"


LICENSE
=======
    Copyright 2004,2005,2006 Luigi Auriemma

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

    http://www.gnu.org/licenses/gpl.txt

*/



void painkiller_pckpwd(unsigned char *pwd, unsigned char *enc) {
    unsigned char   buff[64],
                    encbuff[64],
                    *p1,
                    *p2;
    int             len,
                    i,
                    cl,
                    dl,
                    esi,
                    edi;

    p1 = pwd;
    while(1) {
        if((*p1 >= '0') && (*p1 <= '9')) {
            *p1 -= 0x30;
        } else if((*p1 >= 'a') && (*p1 <= 'z')) {
            *p1 -= 0x57;
        } else if((*p1 >= 'A') && (*p1 <= '\\')) {
            *p1 -= 0x1d;
        } else {
            break;
        }
        p1++;
    }
    len = p1 - pwd;

    p1 = buff;
    for(i = 0; i < 64; i++) {
        *p1++ = i;
    }

    p1 = enc;
    p2 = encbuff;
    for(i = 0; i < 64; i++) {
        *p2++ = *p1++;
        if(!*p1) p1 = enc;
    }

    p1 = buff;
    p2 = encbuff;
    for(i = esi = 0; i < 64; i++) {
        cl = *p1;
        esi = (*p2 + cl + esi) & 63;
        *p1++ = buff[esi];
        buff[esi] = cl;
        p2++;
    }

    esi = edi = 0;
    p1 = pwd;
    for(i = 0; i < len; i++) {
        esi = (esi + 1) & 63;
        cl = buff[esi];
        edi = (cl + edi) & 63;
        dl = buff[edi];
        buff[esi] = dl;
        buff[edi] = cl;
        *p1++ ^= buff[(dl + cl) & 63];
    }

    p1 = pwd;
    while(len--) {
        if(*p1 <= 9) {
            *p1 += 0x30;
        } else if((*p1 >= 0xa) && (*p1 <= 0x23)) {
            *p1 += 0x57;
        } else {
            *p1 += 0x1d;
        }
        p1++;
    }
}

