From 3b03e54fa5e29f17e2e151b46db698da3aae75d3 Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Wed, 22 Jun 2022 01:47:49 -0600 Subject: [PATCH] PHP checksum working need to refactor and make more useful, easier to use, refactor lots of code --- src/D2Functions.php | 10 +++++++--- test.php | 10 ++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/D2Functions.php b/src/D2Functions.php index 0b1d063..b9ae868 100755 --- a/src/D2Functions.php +++ b/src/D2Functions.php @@ -87,10 +87,14 @@ function setBit(int $n, int $p, bool $b){ function getBit(int $b, int $p){ return intval(($b & (1 << $p)) !== 0); } - -function checksum($fileData) { +/** + * Calculate D2S Checksum + * @param $data + * @return string + */ +function checksum($data) : string { $nSignature = 0; - foreach($fileData as $byte){ + foreach($data as $byte){ $nSignature = ((($nSignature << 1) | ($nSignature >> 31)) + $byte) & 0xFFFFFFFF; } return swapEndianness(dechex($nSignature)); diff --git a/test.php b/test.php index 7a77392..6e4b25c 100644 --- a/test.php +++ b/test.php @@ -6,9 +6,6 @@ require_once './src/D2Functions.php'; require_once './src/D2BitReader.php'; -$file = "D:\Diablo II\MODS\ironman-dev\save\Barb.d2s"; -$data = file_get_contents($file); - class D2ByteReader { private string $data; @@ -157,6 +154,9 @@ class D2ByteReader { } +$file = "D:\Diablo II\MODS\ironman-dev\save\Barb.d2s"; +$data = file_get_contents($file); + $c = new D2ByteReader($data); $c->seek(643); $bytes = $c->readh(5); @@ -173,6 +173,4 @@ $c->writeBytes(12, "00000000"); // zero old checksum $newChecksum = checksum(unpack('C*', $c->getData())); // get new checksum dump($newChecksum); $c->writeBytes(12, $newChecksum); // write new checksum -file_put_contents($file, $c->getData()); // write bytestream to file - -//shell_exec("bin\d2scs.exe \"$file\""); \ No newline at end of file +file_put_contents($file, $c->getData()); // write bytestream to file \ No newline at end of file