Refactored items to proper classes

This commit is contained in:
Hash Borgir
2022-06-26 13:41:06 -06:00
parent eb3bc1cb7d
commit 6c4feed124
5 changed files with 215 additions and 60 deletions

154
src/D2ByteReader.php Normal file
View File

@@ -0,0 +1,154 @@
<?php
require_once './src/D2Functions.php';
require_once './src/D2BitReader.php';
class D2ByteReader {
private string $data;
private int $offset = 0;
public function __construct(string $data) {
if (!$data)
return false;
$this->data = $data;
}
public function skip(int $numBytes): bool {
if ($numBytes < 0 || $numBytes > strlen($this->data))
return false;
$this->offset += $numBytes;
return $true;
}
public function seek(int $pos): bool {
if ($pos < 0 || $pos > strlen($this->data))
return false;
$this->offset = $pos;
return true;
}
public function readh(int $offset, int $numBytes, bool $str = true): string {
$this->seek($offset);
$bytes = null;
for ($i = $this->offset; $i < $this->offset + $numBytes; $i++) {
$str ? $bytes .= $this->data[$i] : $bytes[] = $this->data[$i];
}
return unpack('H*', $bytes)[1];
}
public function readc(int $offset, int $numBytes, bool $str = true): array {
$this->seek($offset);
$bytes = null;
for ($i = $this->offset; $i < $this->offset + $numBytes; $i++) {
$str ? $bytes .= $this->data[$i] : $bytes[] = $this->data[$i];
}
return unpack('C*', $bytes);
}
public function rewind(): bool {
$this->offset = 0;
return true;
}
public function writeByte(int $offset, int $byte) {
$this->data[$offset] = pack('C', $byte);
}
public function writeBytes(int $offset, string $bytes) {
if ($offset < 0 || $offset > strlen($this->data) || $bytes == '')
return false;
$_bytes = str_split($bytes, 2);
foreach ($_bytes as $k => $byte) {
$pos = $offset + $k;
$this->data[$pos] = pack('H*', $byte);
}
}
public function getData() {
return $this->data ? $this->data : false;
}
public function getOffset(): int {
return $this->offset;
}
public function isHexString(string $str): bool {
if (strlen($str) % 2 == 0 && (ctype_xdigit($str))) {
return true;
}
return false;
}
public function toBits($input): string {
$output = '';
if ($this->isHexString($input)) {
foreach (str_split($input, 2) as $byte) {
$output .= (strrev(str_pad(decbin(hexdec($byte)), 8, 0, STR_PAD_LEFT)));
}
return $output;
} else if (is_string($input)) {
foreach (str_split($input) as $i) {
$output .= strrev(str_pad(decbin(ord($i)), 8, 0, STR_PAD_LEFT));
}
return $output;
} else if (is_int($input)) {
return strrev(str_pad(decbin($input), 8, 0, STR_PAD_LEFT));
} else if (is_array($input)) {
foreach ($input as $i) {
$output .= $this->tobits($i);
}
return $output;
}
}
public function toBytes(string $bits) {
foreach (str_split($bits, 8) as $byteString) {
$bytes[] = (bindec(strrev($byteString)));
}
foreach ($bytes as $byte) {
dump($byte);
}
}
public function bitsToHexString(string $bits): string {
$bytes = '';
foreach (str_split($bits, 8) as $byte) {
$bytes .= (str_pad(dechex((bindec(strrev($byte)))), 2, 0, STR_PAD_LEFT));
}
return $bytes;
}
public function bitsToHexArray(string $bits): array {
$bytes = [];
foreach (str_split($bits, 8) as $byte) {
$bytes[] = (str_pad(dechex((bindec(strrev($byte)))), 2, 0, STR_PAD_LEFT));
}
return $bytes;
}
public function bitsToIntArray(string $bits): array {
$bytes = [];
foreach (str_split($bits, 8) as $byte) {
$bytes[] = (int) (str_pad(dechex((bindec(strrev($byte)))), 2, 0, STR_PAD_LEFT));
}
return $bytes;
}
/*
@return Byte with Nth bit set to X
*/
public function setBit(int $byte, int $pos, bool $bit) {
return ($bit ? ($byte | (1 << $pos)) : ($byte & ~(1 << $pos)) );
}
/*
@return Bit at Nth position in Byte
*/
public function getBit(int $byte, int $pos): int {
return intval(($byte & (1 << $pos)) != 0);
}
}