Checksum code fixed. Added DocBlocks to D2Classes. TODO: Fill in docblocks, refactor, code cleanup

This commit is contained in:
Hash Borgir
2022-07-06 17:41:24 -06:00
parent f3b24de2a8
commit eb28039f88
28 changed files with 1285 additions and 340 deletions

View File

@@ -3,17 +3,33 @@
require_once './src/D2Functions.php';
require_once './src/D2BitReader.php';
/**
*
*/
class D2ByteReader {
/**
* @var string
*/
private string $data = '';
/**
* @var int
*/
private int $offset = 0;
/**
* @param string $data
*/
public function __construct(string $data) {
if (!$data)
return false;
$this->data = $data;
}
/**
* @param int $numBytes
* @return bool
*/
public function skip(int $numBytes): bool {
if ($numBytes < 0 || $numBytes > strlen($this->data))
return false;
@@ -21,6 +37,10 @@ class D2ByteReader {
return $true;
}
/**
* @param int $pos
* @return bool
*/
public function seek(int $pos): bool {
if ($pos < 0 || $pos > strlen($this->data))
return false;
@@ -28,6 +48,12 @@ class D2ByteReader {
return true;
}
/**
* @param int $offset
* @param int $numBytes
* @param bool $str
* @return string
*/
public function readh(int $offset, int $numBytes, bool $str = true): string {
$this->seek($offset);
$bytes = null;
@@ -37,6 +63,12 @@ class D2ByteReader {
return unpack('H*', $bytes)[1];
}
/**
* @param int $offset
* @param int $numBytes
* @param bool $str
* @return array
*/
public function readc(int $offset, int $numBytes, bool $str = true): array {
$this->seek($offset);
$bytes = null;
@@ -46,15 +78,28 @@ class D2ByteReader {
return unpack('C*', $bytes);
}
/**
* @return bool
*/
public function rewind(): bool {
$this->offset = 0;
return true;
}
/**
* @param int $offset
* @param int $byte
* @return void
*/
public function writeByte(int $offset, int $byte) {
$this->data[$offset] = pack('C', $byte);
}
/**
* @param int $offset
* @param string $bytes
* @return false|void
*/
public function writeBytes(int $offset, string $bytes) {
if ($offset < 0 || $offset > strlen($this->data) || $bytes == '')
return false;
@@ -64,26 +109,46 @@ class D2ByteReader {
$this->data[$pos] = pack('H*', $byte);
}
}
/**
* @param int $offset
* @param string $bytes
* @return false|void
*/
public function insertBytes(int $offset, string $bytes) {
if ($offset < 0 || $offset > strlen($this->data) || $bytes == '')
return false;
$data = bin2hex($this->data); // convert to hex bytes string
$newData = substr_replace($data, $bytes, $offset, 0);
$this->data = hex2bin($newData);
}
}
/**
* @return false|string
*/
public function getData() {
return $this->data ? $this->data : false;
}
/**
* @param $data
* @return void
*/
public function setData($data){
$this->data = $data;
}
/**
* @return int
*/
public function getOffset(): int {
return $this->offset;
}
/**
* @param string $str
* @return bool
*/
public function isHexString(string $str): bool {
if (strlen($str) % 2 == 0 && (ctype_xdigit($str))) {
return true;
@@ -91,6 +156,10 @@ class D2ByteReader {
return false;
}
/**
* @param $input
* @return string
*/
public function toBits($input): string {
$output = '';
if ($this->isHexString($input)) {
@@ -113,13 +182,21 @@ class D2ByteReader {
}
}
/**
* @param string $bits
* @return string
*/
public function toBytesR(string $bits) : string {
foreach (str_split($bits, 8) as $byteString) {
$bytes .= strtoupper(str_pad(dechex(bindec(($byteString))), 2, 0, STR_PAD_LEFT));
}
return $bytes;
}
/**
* @param string $bits
* @return string
*/
public function toBytes(string $bits) : string {
foreach (str_split($bits, 8) as $byteString) {
$bytes .= strtoupper(str_pad(dechex(bindec(strrev($byteString))), 2, 0, STR_PAD_LEFT));
@@ -127,6 +204,10 @@ class D2ByteReader {
return $bytes;
}
/**
* @param string $bits
* @return string
*/
public function bitsToHexString(string $bits): string {
$bytes = '';
foreach (str_split($bits, 8) as $byte) {
@@ -135,6 +216,10 @@ class D2ByteReader {
return $bytes;
}
/**
* @param string $bits
* @return array
*/
public function bitsToHexArray(string $bits): array {
$bytes = [];
foreach (str_split($bits, 8) as $byte) {
@@ -143,6 +228,10 @@ class D2ByteReader {
return $bytes;
}
/**
* @param string $bits
* @return array
*/
public function bitsToIntArray(string $bits): array {
$bytes = [];
foreach (str_split($bits, 8) as $byte) {
@@ -151,18 +240,21 @@ class D2ByteReader {
return $bytes;
}
/*
@return Byte with Nth bit set to X
/**
* @param int $byte
* @param int $pos
* @param bool $bit
* @return int
*/
public function setBit(int $byte, int $pos, bool $bit) {
return ($bit ? ($byte | (1 << $pos)) : ($byte & ~(1 << $pos)) );
}
/*
@return Bit at Nth position in Byte
/**
* @param int $byte
* @param int $pos
* @return int
*/
public function getBit(int $byte, int $pos): int {
return intval(($byte & (1 << $pos)) != 0);
}