PHP checksum working need to refactor and make more useful, easier to use, refactor lots of code

This commit is contained in:
Hash Borgir 2022-06-22 01:47:49 -06:00
parent 69d4728d8c
commit 3b03e54fa5
2 changed files with 11 additions and 9 deletions

View File

@ -87,10 +87,14 @@ function setBit(int $n, int $p, bool $b){
function getBit(int $b, int $p){ function getBit(int $b, int $p){
return intval(($b & (1 << $p)) !== 0); return intval(($b & (1 << $p)) !== 0);
} }
/**
function checksum($fileData) { * Calculate D2S Checksum
* @param $data
* @return string
*/
function checksum($data) : string {
$nSignature = 0; $nSignature = 0;
foreach($fileData as $byte){ foreach($data as $byte){
$nSignature = ((($nSignature << 1) | ($nSignature >> 31)) + $byte) & 0xFFFFFFFF; $nSignature = ((($nSignature << 1) | ($nSignature >> 31)) + $byte) & 0xFFFFFFFF;
} }
return swapEndianness(dechex($nSignature)); return swapEndianness(dechex($nSignature));

View File

@ -6,9 +6,6 @@
require_once './src/D2Functions.php'; require_once './src/D2Functions.php';
require_once './src/D2BitReader.php'; require_once './src/D2BitReader.php';
$file = "D:\Diablo II\MODS\ironman-dev\save\Barb.d2s";
$data = file_get_contents($file);
class D2ByteReader { class D2ByteReader {
private string $data; 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 = new D2ByteReader($data);
$c->seek(643); $c->seek(643);
$bytes = $c->readh(5); $bytes = $c->readh(5);
@ -173,6 +173,4 @@ $c->writeBytes(12, "00000000"); // zero old checksum
$newChecksum = checksum(unpack('C*', $c->getData())); // get new checksum $newChecksum = checksum(unpack('C*', $c->getData())); // get new checksum
dump($newChecksum); dump($newChecksum);
$c->writeBytes(12, $newChecksum); // write new checksum $c->writeBytes(12, $newChecksum); // write new checksum
file_put_contents($file, $c->getData()); // write bytestream to file file_put_contents($file, $c->getData()); // write bytestream to file
//shell_exec("bin\d2scs.exe \"$file\"");