/* 3d Cool Pool packets checksum 0.1 by Luigi Auriemma e-mail: aluigi@autistici.org web: aluigi.org INTRODUCTION ============ This file contains the algorithm used to calculate the 16 bits checksum used in the packets of the games 3d Ultra Cool Pool and Maximum Pool. HOW TO USE ========== The function requires the data in the packet on which calculate the checksum and the length of this data. A packet is just like the following: |##|##########################################| | | | data checksum (2 bytes = 16 bits) A simple usage example: #include "coolpool_cksum.h" ... *(u_short *)packet = coolpool_cksum(packet + 2, packet_size - 2); EXPLANATION OF THE CHECKSUM =========================== You must create a 32 bit number with the sum of all the 32 bit numbers contained in the data. So for example if data is "\x44\x33\x22\x11\x99\x88\x77\x66" you must do result = 0x11223344 + 0x66778899; Note: attention at the garbage data after the packet because if the data is not divisible by 4 (as a data of 5 bytes) the last 32 bit number to add to result could contain garbage data located outside the packet (as "\x44\x33\x22\x11\x99" "garbage", so you must get only 0x00000099 as last number). Then the result must XORed with 0xfde24acb and then the two 16 bit parts of the result must be XORed togheter. Example, if result is 0x11223344 you must do 0x1122 XOR 0x3344 and the result is your checksum. 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 */ unsigned short coolpool_cksum(unsigned char *data, int len) { int rest; unsigned int *ldata, cksum = 0; rest = len & 3; len >>= 2; ldata = (unsigned int *)data; while(len--) { cksum += *ldata; ldata++; } // needed to avoid possible garbage data if(rest == 1) { cksum += (*ldata & 0xff); } else if(rest == 2) { cksum += (*ldata & 0xffff); } else if(rest == 3) { cksum += (*ldata & 0xffffff); } cksum ^= 0xfde24acb; cksum ^= (cksum >> 16); return(cksum); }