/*

All Seeing Eye 'v' ping (aka scanner) algorithm 0.1
by Luigi Auriemma
e-mail: aluigi@autistici.org
web:    aluigi.org


This algorithm is used in the games that use the ASE protocol and
specifically the 'v' ping type.
The 'v' is a type of ASE ping that seems used only by the scanner
servers (as 194.251.249.103) but is not clear what is its real usage.
As you can see by my code, there is nothing of really useful or
interesting in this algorithm so I continue to don't understand why
it is used and moreover why only some scanner servers can use it
(all the others hosts are REJECTED).

The algorithm needs 3 parameters:

- packet containing the 8 bytes to manipulate: example "v12345678"
- number 1: this is a unique number that is assigned to each game.
  For example for the game IGI2 the first number is 0x1763c845
- number 2: the same as above.
  For IGI2 the second number is 0xb20fa2f7

The result of the algorithm is a 32 bit number that your game server
sends back to who has made the query (the scanner server or who is
allowed to ping you with the 'v' ping type).

The following is an example referred to "v12345678" and IGI2-ds:

  buff         = "v12345678";
  u_int value = ase_v_algo(buff + 1, 0x1763c845, 0xb20fa2f7);

  value will be 0xe2bef648 and so the complete reply packet to send
  back will be "V\x48\xf6\xbe\xe2"

It is really very simple and probably useless however if you find a
real usage of this algorithm or you think it is interesting let me
know it.


    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

*/



#include <stdio.h>
#include <stdlib.h>



#define ASE_V_ALGO_BASE1  0xce28deb2
#define ASE_V_ALGO_BASE2  0x8be0a759
typedef unsigned int     u_int;
typedef unsigned char     u_char;



u_int ase_v_algo_data[] =
{
 0x0109a70d, 0x30d621ac, 0x44d52cc7, 0x16a343ab,
 0x30c7cb66, 0x64e65400, 0x6f0c2b78, 0xe6bea4d8,
 0x80167528, 0x002ce936, 0xe465518b, 0xf28d4c4b,
 0x5866ff57, 0xd4b79029, 0x3a711818, 0x52001968,
 0xeda001ac, 0x42cba660, 0x0d5a63a5, 0xa82fb91b,
 0xc4512deb, 0xb636b73d, 0x3f30f44e, 0xa0d0279f,
 0x16bee383, 0x9aa0557b, 0x375d38e5, 0x25033006,
 0x41760f0f, 0x56e43c26, 0x7cb59306, 0x9d30d5f4,
 0xdc21629a, 0x4c2df2f7, 0x85463b50, 0xedcc1cf2,
 0x76d228f7, 0x3d7ca241, 0xd3a4f5f8, 0x273bebe3,
 0xb3bffb5d, 0x48650da8, 0xd688c129, 0xf6942ade,
 0x9536d9b6, 0xcf4e9e95, 0x73fb348e, 0xa29fd21e,
 0x9f17c38f, 0x2fbca3aa, 0x188a3293, 0xda6bc5fe,
 0xd7d8506c, 0x238afe34, 0x695244b4, 0x6a22fa7d,
 0x1b9d85cd, 0x7b97397c, 0xaec8cfdc, 0x22d77196,
 0x348ef65b, 0x6b5e3dee, 0x75ddafba, 0x29321bb7,
 0x47032744, 0xc979bb4f, 0x6005b770, 0x8ec945ff,
 0x1099655c, 0x3c0c778f, 0xd2793667, 0x83edacb1,
 0xdb95fdd6, 0x364b5827, 0xbad1d7bc, 0x04b16c21,
 0x35d03527, 0x5404f4ae, 0xbe1ee679, 0x63b8e5a7,
 0x8b32bc01, 0x6c431003, 0xa2a94b89, 0x4d69d098,
 0xa9c0b4e0, 0x2b7aa1c0, 0x616e30c5, 0x81672bd1,
 0x514e0b00, 0xcd476111, 0x8d379eb7, 0xdc356302,
 0xac4acccc, 0xbbb46b73, 0xdf128b47, 0x99507f69,
 0x53e0747b, 0x675a160f, 0x19d31aaf, 0xca3cf7be,
 0x5a60c62f, 0x9ce8858d, 0x46f1ebc0, 0x583e8146,
 0x933879fc, 0x06951ef0, 0x2201d2fb, 0x1b01e8ca,
 0xffb898a9, 0x11c21492, 0x9bf7807a, 0xd242970d,
 0xe8874609, 0x2e8f2c84, 0x64c277f2, 0x3718f36e,
 0x9abb9599, 0xbe9e6856, 0xbdf5041b, 0xd6a2c6a2,
 0x5edc64ee, 0xb4482f09, 0x849bdd0c, 0xac45b3cc,
 0xe12ae245, 0x0bb0384a, 0xb65b2ffd, 0x3e49ddad,
 0x9134caae, 0xe7f1baa6, 0x43f6c5c3, 0x2dec4eed,
 0x12293d42, 0xcb5d822e, 0xe76a334f, 0xf33469d9,
 0x08d43a43, 0x7c140a91, 0x566b4815, 0xb9c59483,
 0x4ba508d8, 0xb8724f01, 0x9e145705, 0x4edb8d40,
 0x8e4899df, 0x131b720a, 0x4d6cdf73, 0xec400f50,
 0x72e98d2c, 0x9b80fbfc, 0xdab27ec9, 0x8274d8a4,
 0x50fe699b, 0xf0625e90, 0x66e56107, 0x620edca9,
 0xb153e433, 0x33e3d3eb, 0xc7944d16, 0x152adbc8,
 0x7ff3aa9f, 0xf739cfb5, 0x782fe1a8, 0xd03dc32d,
 0x52906c1e, 0x84ef8cd2, 0x77b7da12, 0xe075c44d,
 0x8724c7a3, 0xe4ad86d7, 0x11e1a008, 0x89465954,
 0x5c35ec7f, 0xdf169985, 0x078cc964, 0x709a42d0,
 0x654c4aa7, 0xebb61862, 0xd1185e76, 0xd8c1cb3c,
 0x2f64590e, 0xe24fda24, 0x8a44bfea, 0x5e68f876,
 0x1ea20039, 0x90e1323a, 0x82e842cf, 0xc58b7e38,
 0xf3fdd36e, 0x8509bc5d, 0x329897bd, 0x5c412db2,
 0x20a3f2ed, 0x09fcfdf1, 0xf6ba9ada, 0xbff80ee4,
 0xd8dfe99c, 0x97aa7625, 0xcd636db9, 0x1dc61fa0,
 0xf7e71e80, 0x79bf40b6, 0xb5fa07d2, 0x246a0c6c,
 0xee9f206a, 0xba59791c, 0xaf4da18c, 0x9e12ae35,
 0xbc9a4f4c, 0x0f27ad65, 0x7a19cd94, 0xadaf7c93,
 0xe36931a2, 0x6e089de7, 0xfaffa559, 0xb0e265e6,
 0x5f438e10, 0x0d21281f, 0x746824e3, 0x1eee0bd4,
 0x31c54c2b, 0x453fd632, 0x2dc65b03, 0x6893a99e,
 0xcf220313, 0xfad2537a, 0xf43b1d62, 0xf99dfca5,
 0x86aa8a95, 0x65d836e8, 0x1759566d, 0x4aa6bd1d,
 0xaba79dca, 0x6db91d07, 0x881adb87, 0x12f79533,
 0xa87d5fb8, 0x7fda87e2, 0x68e49ca0, 0x5b921ad3,
 0x00e2b8f4, 0xa74af130, 0x237e3e26, 0x2c53ea71,
 0x9c3a10ef, 0x75774b17, 0x3d754eec, 0x0aa541ba,
 0x2e9c3f17, 0x351a2967, 0x9804dc0b, 0x5ade0982,
 0x04eb8f90, 0x7ae5b2d6, 0xc5bd260a, 0x01e02520,
 0xb03ffe77, 0x69dce41a, 0x7def158a, 0xb1026a86,
 0xf1477ae8, 0x945c3357, 0x6e7860d7, 0xa1c73a6a,
 0x3e82b2b1, 0xa3d99a37, 0x6dcfde3a, 0x2887354c,
 0x6b8622d5, 0x3f85b679, 0xd4b672f1, 0x92f6affd,
 0xa7b04941, 0x47284972, 0x898bbd4a, 0x8d17442c,
 0x26737cb5, 0xc0d3ab78, 0x6c5823e1, 0x61ff7358,
 0x2bc3f0f0, 0xa531cef6, 0x920d76c1, 0xfb2b262f,
 0x2120b137, 0xeabd5b53, 0xad6121a4, 0x8ad1518a,
 0xe213d181, 0x1a263fb4, 0xfc859bde, 0x4b8e05ec,
 0x5bf4c4f5, 0x77837515, 0x1d06c238, 0xfda74749,
 0x62cc39c6, 0x07888b5f, 0xdd2bc87c, 0xe8acee05,
 0x55896eb0, 0x8c576075, 0xf983ac3c, 0xc7299281,
 0x25eabe69, 0x101d8f5b, 0x4f1c0e82, 0xde7e66c1,
 0x57eea27d, 0x502e0194, 0xe6679fbf, 0x71cfccc4,
 0xa5d68365, 0xc2a85dc2, 0xbf92b586, 0x8f448a48,
 0xc370bad9, 0x80bbff4e, 0xde26d658, 0xf5734a64,
 0xa6d9ce11, 0x4f336261, 0xeffc9132, 0x18119fb0,
 0x0e576f34, 0x594d3ebc, 0x7ede375f, 0x51fdb8dc,
 0x9d7c1914, 0xbd370752, 0x49acf7ff, 0xa9fbd4c6,
 0x0f40bb2d, 0x7e86de8e, 0x48f83cc2, 0x43547d08,
 0x09b92920, 0x8707e17f, 0x2cb3a6e2, 0x2060e3cf,
 0xf0daf130, 0xe5e95659, 0xf8416b51, 0xff8113b8,
 0x4e029661, 0xcc15f618, 0xc945ead1, 0xafc80219,
 0x0a74d422, 0x5dc004b9, 0xc1841121, 0x46c3f0e9,
 0x815c5af6, 0xa684ca5a, 0xc262e59e, 0x0eeb9caf,
 0x966fa392, 0x1c5fc06f, 0xaaaea93f, 0x956cbe99,
 0x71555cf9, 0x8b38e69c, 0x79332e52, 0x600bc943,
 0x67caee3e, 0xdb9017e5, 0xd5cb0d02, 0xc67d7431,
 0x244b056f, 0x41968422, 0xa11d1fab, 0x0c76a82a,
 0xa4256768, 0xdda95abf, 0x4a2d7196, 0xab6e9366,
 0xeacd09cb, 0x38d5e010, 0xb8ade09d, 0x741f1197,
 0xcb772a53, 0x2166919a, 0xb7930236, 0xc4faa777,
 0x28f281a1, 0xc39806cb, 0x1a1b0c1c, 0x96f4d714,
 0x0b8f0691, 0xd720c77e, 0x838d9498, 0xaa0d4623,
 0x9754a4a6, 0xee8cc847, 0x70f9ae2e, 0xf8064db3,
 0x2a81735e, 0x191031fa, 0xe0b489be, 0x7d785c6d,
 0x9400a85a, 0xb264670b, 0xb4c44374, 0xb555f5dd,
 0xc0d78454, 0x765812d5, 0x8f4f53c8, 0xef4ca587,
 0xb27f55d4, 0x3aae3bdb, 0x3ca17b84, 0x267bd1ef,
 0x3b31b624, 0xfe7f98fb, 0x3696c03b, 0x98f570c9,
 0x15e68255, 0xc1ea5f89, 0xf2b168aa, 0x880a6442,
 0x5942f8e7, 0x66cd7bdf, 0xca2cb9f3, 0x6f7022e0,
 0x14abfc1d, 0xc851c1f9, 0x061f7088, 0xe399503f,
 0x0ca67fd3, 0x32abb00c, 0x4049144b, 0xae529680,
 0x296dd825, 0xfc91cd5e, 0x02db921a, 0xe90f48ea,
 0xccece7d0, 0x02f37a63, 0x633e25dd, 0x72e7bfe1,
 0x05230a71, 0xd19c2413, 0x1f1558e4, 0xa4a47851,
 0x42a878fe, 0x7389e2f3, 0xecc98685, 0x5305803e,
 0xfdcef923, 0x9f24a06b, 0x130f1b31, 0xcec41528,
 0xeb07efdb, 0x40b3c20e, 0x270a40fa, 0x3b1c6fbb,
 0xfb5e1763, 0x31ca8ec3, 0xe93952ce, 0xf11e6dce,
 0xf50b16ad, 0x3963ed74, 0xd9f04172, 0xe1192eda,
 0x4c3d1340, 0x86dfe712, 0xbb56ed19, 0x08f0ef55,
 0xe57b54c4, 0x575b8345, 0xfe27fa7e, 0x05f26e9b,
 0x7bbc4504, 0x5fd400f5, 0xc897ad49, 0x93130804,
 0x6a0ef3b2, 0xbc822039, 0x1c726660, 0xb371eca3,
 0xd03cd51f, 0x14a1d93b, 0xb911872a, 0xd9feaacd,
 0x8c8088b3, 0x44ce0388, 0x455f4797, 0x5556572b,
 0x5d506ae6, 0x036ff944, 0x99911246, 0x2a6d888c,
 0x907ad08d, 0xb7b29bc7, 0x03edab75, 0x49f93416,
 0x332eb035, 0xd523df70, 0xa0af1cbb, 0x179bb1f8,
 0x38288ce9, 0xf4b5b58b, 0xa3e3b33d, 0xd33a89a1,
 0x54105d4d, 0x91ddb45c, 0xc69e7d56, 0x78ba23bd,
 0x39c1906b, 0x1f61529d, 0xce08e848, 0x342537c5,
 0xfc6c0002
};



u_int ase_v_algo(u_char *ptr, u_int num1, u_int num2) {
    u_int  eax,
            ebx,
            ebp;
    u_char  i;

    for(i = 8; i; i--, ptr++) {
        ebx = (num2 << 24) + ASE_V_ALGO_BASE1;
        eax = ((num1 & 0xff) ^ *ptr) << 1;
        ebp = ase_v_algo_data[eax] ^ ebx;
        eax = ase_v_algo_data[eax + 1] ^ ((num1 << 24) + ASE_V_ALGO_BASE2);
        num1 = (num1 >> 8) ^ ebp;
        num2 = (num2 >> 8) ^ eax;
    }
    return(num2 + num1);
}


