Skip to content

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

@hashborgir

Description

@hashborgir

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions