krisives / d2s-format

Diablo II Save File Format (.d2s format)
121 stars 19 forks source link

[Suggestion]: My PHP version of the checksum algorithm should it help anyone #12

Open hashborgir opened 2 years ago

hashborgir commented 2 years ago

Hi. Thanks for the great resource. Please add my version of the checksum algorithm in PHP should this help anyone.

<?php
/*
  Diablo 2 D2S Save File Checksum Calculator 
  By Hash Borgir (https://www.psychedelicsdaily.com)
  @date 6/4/2022

 Checksum field is at byte 12. 
 Bytes 12/13/14/15 as a uint32. 
 Set this to 0. 
 After clearing the checksum field add up the values of all the bytes in the file and rotate the running total one bit to the left before adding the next byte.
*/

/**
 * @param string $hex
 * @return string
 */
function swapEndianness(string $hex) {
    return implode('', array_reverse(str_split($hex, 2)));
}

/**
 * Calculate D2S Checksum
 * @param $data
 * @return string
 */
function checksum($fileData) {
    $nSignature = 0;
    foreach ($fileData as $k => $byte) {
        if ($k == 12 || $k == 13 || $k == 14 || $k == 15)  { // skip bytes 12,13,14,15
            $byte = 0;
        }
        $nSignature = ((($nSignature << 1) | ($nSignature >> 31)) + $byte & 0xFFFFFFFF);
    }
    return swapEndianness(str_pad(dechex($nSignature), 8, 0, STR_PAD_LEFT));
}

$filename = "D:\Diablo II\MODS\ironman-dev\save\Sorc.d2s";
$fp = fopen($filename, "rb+");

fseek($fp, 12); // go to byte 12
fwrite($fp, pack('I', 0)); // clear the checksum field uInt32
$fileData = unpack('C*', file_get_contents($filename)); // open file and unpack

fseek($fp, 12); // go to byte 12
fwrite($fp, pack('H8', checksum($fileData))); // write new checksum
fclose($fp);
hashborgir commented 2 years ago

XOR Table in PHP:

   public $xorTable = [
    "00" => 0x0,
    "01" => 0x77073096,
    "02" => 0xEE0E612C,
    "03" => 0x990951BA,
    "04" => 0x076DC419,
    "05" => 0x706AF48F,
    "06" => 0xE963A535,
    "07" => 0x9E6495A3,
    "08" => 0x0EDB8832,
    "09" => 0x79DCB8A4,
    "0A" => 0xE0D5E91E,
    "0B" => 0x97D2D988,
    "0C" => 0x09B64C2B,
    "0D" => 0x7EB17CBD,
    "0E" => 0xE7B82D07,
    "0F" => 0x90BF1D91,
    "10" => 0x1DB71064,
    "11" => 0x6AB020F2,
    "12" => 0xF3B97148,
    "13" => 0x84BE41DE,
    "14" => 0x1ADAD47D,
    "15" => 0x6DDDE4EB,
    "16" => 0xF4D4B551,
    "17" => 0x83D385C7,
    "18" => 0x136C9856,
    "19" => 0x646BA8C0,
    "1A" => 0xFD62F97A,
    "1B" => 0x8A65C9EC,
    "1C" => 0x14015C4F,
    "1D" => 0x63066CD9,
    "1E" => 0xFA0F3D63,
    "1F" => 0x8D080DF5,
    "20" => 0x3B6E20C8,
    "21" => 0x4C69105E,
    "22" => 0xD56041E4,
    "23" => 0xA2677172,
    "24" => 0x3C03E4D1,
    "25" => 0x4B04D447,
    "26" => 0xD20D85FD,
    "27" => 0xA50AB56B,
    "28" => 0x35B5A8FA,
    "29" => 0x42B2986C,
    "2A" => 0xDBBBC9D6,
    "2B" => 0xACBCF940,
    "2C" => 0x32D86CE3,
    "2D" => 0x45DF5C75,
    "2E" => 0xDCD60DCF,
    "2F" => 0xABD13D59,
    "30" => 0x26D930AC,
    "31" => 0x51DE003A,
    "32" => 0xC8D75180,
    "33" => 0xBFD06116,
    "34" => 0x21B4F4B5,
    "35" => 0x56B3C423,
    "36" => 0xCFBA9599,
    "37" => 0xB8BDA50F,
    "38" => 0x2802B89E,
    "39" => 0x5F058808,
    "3A" => 0xC60CD9B2,
    "3B" => 0xB10BE924,
    "3C" => 0x2F6F7C87,
    "3D" => 0x58684C11,
    "3E" => 0xC1611DAB,
    "3F" => 0xB6662D3D,
    "40" => 0x76DC4190,
    "41" => 0x01DB7106,
    "42" => 0x98D220BC,
    "43" => 0xEFD5102A,
    "44" => 0x71B18589,
    "45" => 0x06B6B51F,
    "46" => 0x9FBFE4A5,
    "47" => 0xE8B8D433,
    "48" => 0x7807C9A2,
    "49" => 0x0F00F934,
    "4A" => 0x9609A88E,
    "4B" => 0xE10E9818,
    "4C" => 0x7F6A0DBB,
    "4D" => 0x086D3D2D,
    "4E" => 0x91646C97,
    "4F" => 0xE6635C01,
    "50" => 0x6B6B51F4,
    "51" => 0x1C6C6162,
    "52" => 0x856530D8,
    "53" => 0xF262004E,
    "54" => 0x6C0695ED,
    "55" => 0x1B01A57B,
    "56" => 0x8208F4C1,
    "57" => 0xF50FC457,
    "58" => 0x65B0D9C6,
    "59" => 0x12B7E950,
    "5A" => 0x8BBEB8EA,
    "5B" => 0xFCB9887C,
    "5C" => 0x62DD1DDF,
    "5D" => 0x15DA2D49,
    "5E" => 0x8CD37CF3,
    "5F" => 0xFBD44C65,
    "60" => 0x4DB26158,
    "61" => 0x3AB551CE,
    "62" => 0xA3BC0074,
    "63" => 0xD4BB30E2,
    "64" => 0x4ADFA541,
    "65" => 0x3DD895D7,
    "66" => 0xA4D1C46D,
    "67" => 0xD3D6F4FB,
    "68" => 0x4369E96A,
    "69" => 0x346ED9FC,
    "6A" => 0xAD678846,
    "6B" => 0xDA60B8D0,
    "6C" => 0x44042D73,
    "6D" => 0x33031DE5,
    "6E" => 0xAA0A4C5F,
    "6F" => 0xDD0D7CC9,
    "70" => 0x5005713C,
    "71" => 0x270241AA,
    "72" => 0xBE0B1010,
    "73" => 0xC90C2086,
    "74" => 0x5768B525,
    "75" => 0x206F85B3,
    "76" => 0xB966D409,
    "77" => 0xCE61E49F,
    "78" => 0x5EDEF90E,
    "79" => 0x29D9C998,
    "7A" => 0xB0D09822,
    "7B" => 0xC7D7A8B4,
    "7C" => 0x59B33D17,
    "7D" => 0x2EB40D81,
    "7E" => 0xB7BD5C3B,
    "7F" => 0xC0BA6CAD,
    "80" => 0xEDB88320,
    "81" => 0x9ABFB3B6,
    "82" => 0x03B6E20C,
    "83" => 0x74B1D29A,
    "84" => 0xEAD54739,
    "85" => 0x9DD277AF,
    "86" => 0x04DB2615,
    "87" => 0x73DC1683,
    "88" => 0xE3630B12,
    "89" => 0x94643B84,
    "8A" => 0x0D6D6A3E,
    "8B" => 0x7A6A5AA8,
    "8C" => 0xE40ECF0B,
    "8D" => 0x9309FF9D,
    "8E" => 0x0A00AE27,
    "8F" => 0x7D079EB1,
    "90" => 0xF00F9344,
    "91" => 0x8708A3D2,
    "92" => 0x1E01F268,
    "93" => 0x6906C2FE,
    "94" => 0xF762575D,
    "95" => 0x806567CB,
    "96" => 0x196C3671,
    "97" => 0x6E6B06E7,
    "98" => 0xFED41B76,
    "99" => 0x89D32BE0,
    "9A" => 0x10DA7A5A,
    "9B" => 0x67DD4ACC,
    "9C" => 0xF9B9DF6F,
    "9D" => 0x8EBEEFF9,
    "9E" => 0x17B7BE43,
    "9F" => 0x60B08ED5,
    "A0" => 0xD6D6A3E8,
    "A1" => 0xA1D1937E,
    "A2" => 0x38D8C2C4,
    "A3" => 0x4FDFF252,
    "A4" => 0xD1BB67F1,
    "A5" => 0xA6BC5767,
    "A6" => 0x3FB506DD,
    "A7" => 0x48B2364B,
    "A8" => 0xD80D2BDA,
    "A9" => 0xAF0A1B4C,
    "AA" => 0x36034AF6,
    "AB" => 0x41047A60,
    "AC" => 0xDF60EFC3,
    "AD" => 0xA867DF55,
    "AE" => 0x316E8EEF,
    "AF" => 0x4669BE79,
    "B0" => 0xCB61B38C,
    "B1" => 0xBC66831A,
    "B2" => 0x256FD2A0,
    "B3" => 0x5268E236,
    "B4" => 0xCC0C7795,
    "B5" => 0xBB0B4703,
    "B6" => 0x220216B9,
    "B7" => 0x5505262F,
    "B8" => 0xC5BA3BBE,
    "B9" => 0xB2BD0B28,
    "BA" => 0x2BB45A92,
    "BB" => 0x5CB36A04,
    "BC" => 0xC2D7FFA7,
    "BD" => 0xB5D0CF31,
    "BE" => 0x2CD99E8B,
    "BF" => 0x5BDEAE1D,
    "C0" => 0x9B64C2B0,
    "C1" => 0xEC63F226,
    "C2" => 0x756AA39C,
    "C3" => 0x026D930A,
    "C4" => 0x9C0906A9,
    "C5" => 0xEB0E363F,
    "C6" => 0x72076785,
    "C7" => 0x05005713,
    "C8" => 0x95BF4A82,
    "C9" => 0xE2B87A14,
    "CA" => 0x7BB12BAE,
    "CB" => 0x0CB61B38,
    "CC" => 0x92D28E9B,
    "CD" => 0xE5D5BE0D,
    "CE" => 0x7CDCEFB7,
    "CF" => 0x0BDBDF21,
    "D0" => 0x86D3D2D4,
    "D1" => 0xF1D4E242,
    "D2" => 0x68DDB3F8,
    "D3" => 0x1FDA836E,
    "D4" => 0x81BE16CD,
    "D5" => 0xF6B9265B,
    "D6" => 0x6FB077E1,
    "D7" => 0x18B74777,
    "D8" => 0x88085AE6,
    "D9" => 0xFF0F6A70,
    "DA" => 0x66063BCA,
    "DB" => 0x11010B5C,
    "DC" => 0x8F659EFF,
    "DD" => 0xF862AE69,
    "DE" => 0x616BFFD3,
    "DF" => 0x166CCF45,
    "E0" => 0xA00AE278,
    "E1" => 0xD70DD2EE,
    "E2" => 0x4E048354,
    "E3" => 0x3903B3C2,
    "E4" => 0xA7672661,
    "E5" => 0xD06016F7,
    "E6" => 0x4969474D,
    "E7" => 0x3E6E77DB,
    "E8" => 0xAED16A4A,
    "E9" => 0xD9D65ADC,
    "EA" => 0x40DF0B66,
    "EB" => 0x37D83BF0,
    "EC" => 0xA9BCAE53,
    "ED" => 0xDEBB9EC5,
    "EE" => 0x47B2CF7F,
    "EF" => 0x30B5FFE9,
    "F0" => 0xBDBDF21C,
    "F1" => 0xCABAC28A,
    "F2" => 0x53B39330,
    "F3" => 0x24B4A3A6,
    "F4" => 0xBAD03605,
    "F5" => 0xCDD70693,
    "F6" => 0x54DE5729,
    "F7" => 0x23D967BF,
    "F8" => 0xB3667A2E,
    "F9" => 0xC4614AB8,
    "FA" => 0x5D681B02,
    "FB" => 0x2A6F2B94,
    "FC" => 0xB40BBE37,
    "FD" => 0xC30C8EA1,
    "FE" => 0x5A05DF1B,
    "FF" => 0x2D02EF8D        
    ];