From 4315b08b56c49df828224b6847b3deaa7e7ffbc0 Mon Sep 17 00:00:00 2001 From: Hash Borgir Date: Sun, 4 Jun 2023 23:01:07 -0600 Subject: [PATCH] Update/clean --- processFiles.php | 16 +--- src/D2BitReader.php | 25 +++++-- src/D2ByteReader.php | 173 ++++++++++++++++++++++++++++++------------- 3 files changed, 142 insertions(+), 72 deletions(-) diff --git a/processFiles.php b/processFiles.php index a9bfcd8..31320bd 100755 --- a/processFiles.php +++ b/processFiles.php @@ -49,18 +49,7 @@ include "./_pdo.php"; include "./config.php"; function processFilesManuallyInSqlite() { - - $bin = getcwd() . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "sqlite3.exe "; - $dbfile = getcwd() . DIRECTORY_SEPARATOR . DB_FILE; - $cubemain = TXT_PATH . "cubemain.txt"; - $misc = TXT_PATH . "Misc.txt"; - exec("$bin $dbfile \".separator \\\"\\t\\\"\" \".import \\\"$cubemain\\\" cubemain\""); - exec("$bin $dbfile \".separator \\\"\\t\\\"\" \".import \\\"$misc\\\" misc\""); -} - -function processFilesManuallyInSqliteLinux() { - - $bin = "/usr/bin/sqlite3"; + $bin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? getcwd() . DIRECTORY_SEPARATOR . "bin" . DIRECTORY_SEPARATOR . "sqlite3.exe" : "/usr/bin/sqlite3"; $dbfile = getcwd() . DIRECTORY_SEPARATOR . DB_FILE; $cubemain = TXT_PATH . "CubeMain.txt"; $misc = TXT_PATH . "Misc.txt"; @@ -105,7 +94,6 @@ if (file_exists(APP_DB)) { // Process Files Manually In Sqlite processFilesManuallyInSqlite(); - processFilesManuallyInSqliteLinux(); function toPngAll() { $q = realpath("bin\qdc6.exe"); @@ -159,6 +147,8 @@ if (file_exists(APP_DB)) { // if config db does not exist, go to configure page header("Location: /src/D2Config.php"); } +require_once './src/D2Strings.php'; +new D2Strings(); // put in html redirect as backup, because diff --git a/src/D2BitReader.php b/src/D2BitReader.php index 299a08e..5fe86aa 100644 --- a/src/D2BitReader.php +++ b/src/D2BitReader.php @@ -42,15 +42,26 @@ class D2BitReader { * @param bool $str Determines whether to return a string or an array of bits. * @return string|array The read bits. */ - public function read(int $numBits = 0, bool $str = true): string|array { - $bits = null; - for ($i = $this->offset; $i < $this->offset + $numBits; $i++) { - $str ? $bits .= $this->bits[$i] : $bits[] = $this->bits[$i]; - } - $this->offset += $numBits; - return $bits; +public function read(int $numBits = 0, bool $str = true): string|array { + $bits = null; // Initialize a variable to store the extracted bits + + // Loop from the current offset to the current offset plus the specified number of bits + // Extract each bit from the internal property $this->bits at index $i + + // If $str is true, concatenate the extracted bit to the $bits string + // Otherwise, append the extracted bit to the $bits array + for ($i = $this->offset; $i < $this->offset + $numBits; $i++) { + $str ? $bits .= $this->bits[$i] : $bits[] = $this->bits[$i]; + // Depending on the value of the $str parameter, concatenate the bits to form a string + // or append them to an array } + $this->offset += $numBits; // Update the offset to move to the next position in the data + + return $bits; // Return the extracted bits as a string or an array, based on the $str parameter +} + + /** * Writes bits to a specified offset in the bit string. * diff --git a/src/D2ByteReader.php b/src/D2ByteReader.php index ee0e779..52e6fd1 100644 --- a/src/D2ByteReader.php +++ b/src/D2ByteReader.php @@ -24,10 +24,11 @@ class D2ByteReader { * @param string $data The byte data to read. */ public function __construct(string $data) { - if (!$data) { - return false; + if (!$data) { // Check if the provided data is empty or false + return false; // Return false if the data is empty or false } - $this->data = $data; + + $this->data = $data; // Assign the provided data to the internal property "data" } /** @@ -37,11 +38,12 @@ class D2ByteReader { * @return bool Returns true on success, false otherwise. */ public function skip(int $numBytes): bool { - if ($numBytes < 0 || $numBytes > strlen($this->data)) { - return false; + if ($numBytes < 0 || $numBytes > strlen($this->data)) { // Check if the specified number of bytes is within the valid range + return false; // Return false if the number of bytes is invalid } - $this->offset += $numBytes; - return true; + + $this->offset += $numBytes; // Increment the internal offset by the specified number of bytes + return true; // Return true to indicate a successful skip operation } /** @@ -51,11 +53,12 @@ class D2ByteReader { * @return bool Returns true if the offset position is valid, false otherwise. */ public function seek(int $pos): bool { - if ($pos < 0 || $pos > strlen($this->data)) { - return false; + if ($pos < 0 || $pos > strlen($this->data)) { // Check if the specified position is within the valid range + return false; // Return false if the position is invalid } - $this->offset = $pos; - return true; + + $this->offset = $pos; // Set the internal offset to the specified position + return true; // Return true to indicate a successful seek operation } /** @@ -67,14 +70,17 @@ class D2ByteReader { * @return string|array The read bytes. */ public function read(int $offset, int $numBytes, bool $str = true) { - $this->seek($offset); - $bytes = null; + $this->seek($offset); // Set the internal offset to the specified position + $bytes = null; // Initialize a variable to store the read bytes + // Loop through the specified number of bytes for ($i = $this->offset; $i < $this->offset + $numBytes; $i++) { + // Depending on the $str parameter, concatenate the byte to $bytes as a string or add it to $bytes array $str ? $bytes .= $this->data[$i] : $bytes[] = $this->data[$i]; } - return $bytes; + + return $bytes; // Return the read bytes as either a string or an array } - + /** * Reads a specified number of bytes from the current offset position and returns them as a hex string. * @@ -84,14 +90,17 @@ class D2ByteReader { * @return string The read bytes as a hex string. */ public function readh(int $offset, int $numBytes, bool $str = true) { - $this->seek($offset); - $bytes = null; + $this->seek($offset); // Set the internal offset to the specified position + $bytes = null; // Initialize a variable to store the read bytes + // Loop through the specified number of bytes for ($i = $this->offset; $i < $this->offset + $numBytes; $i++) { + // Depending on the $str parameter, concatenate the byte to $bytes as a string or add it to $bytes array $str ? $bytes .= $this->data[$i] : $bytes[] = $this->data[$i]; } - return unpack('H*', $bytes)[1]; + + return unpack('H*', $bytes)[1]; // Unpack the bytes into a hexadecimal string and return it } - + /** * Reads a specified number of bytes from the current offset position and returns them as an array of integers. * @@ -101,12 +110,15 @@ class D2ByteReader { * @return array The read bytes as an array of integers. */ public function readc(int $offset, int $numBytes, bool $str = true) { - $this->seek($offset); - $bytes = null; + $this->seek($offset); // Set the internal offset to the specified position + $bytes = null; // Initialize a variable to store the read bytes + // Loop through the specified number of bytes for ($i = $this->offset; $i < $this->offset + $numBytes; $i++) { + // Depending on the $str parameter, concatenate the byte to $bytes as a string or add it to $bytes array $str ? $bytes .= $this->data[$i] : $bytes[] = $this->data[$i]; } - return unpack('C*', $bytes); + + return unpack('C*', $bytes); // Unpack the bytes into an array of unsigned characters } /** @@ -115,8 +127,8 @@ class D2ByteReader { * @return bool Returns true on success. */ public function rewind(): bool { - $this->offset = 0; - return true; + $this->offset = 0; // Set the offset property to 0, indicating the start position + return true; // Return true to indicate that the rewind operation was successful } /** @@ -139,12 +151,12 @@ class D2ByteReader { */ public function writeBytes(int $offset, string $bytes) { if ($offset < 0 || $offset > strlen($this->data) || $bytes == '') { - return false; + return false; // Return false if the offset is invalid or the bytes string is empty } - $_bytes = str_split($bytes, 2); + $_bytes = str_split($bytes, 2); // Split the bytes string into an array of two-character chunks foreach ($_bytes as $k => $byte) { - $pos = $offset + $k; - $this->data[$pos] = pack('H*', $byte); + $pos = $offset + $k; // Calculate the position in the data to write the byte + $this->data[$pos] = pack('H*', $byte); // Pack the hexadecimal byte into binary format and write it at the specified position } } @@ -157,11 +169,11 @@ class D2ByteReader { */ public function insertBytes(int $offset, string $bytes) { if ($offset < 0 || $offset > strlen($this->data) || $bytes == '') { - return false; + return false; // Return false if the offset is invalid or the bytes string is empty } - $data = bin2hex($this->data); // convert to hex bytes string - $newData = substr_replace($data, $bytes, $offset, 0); - $this->data = hex2bin($newData); + $data = bin2hex($this->data); // Convert the binary data to a hexadecimal string + $newData = substr_replace($data, $bytes, $offset, 0); // Insert the new bytes at the specified offset in the hexadecimal string + $this->data = hex2bin($newData); // Convert the modified hexadecimal string back to binary data and update the 'data' property } /** @@ -170,7 +182,7 @@ class D2ByteReader { * @return false|string Returns the byte data on success, or false if no data is set. */ public function getData() { - return $this->data ? $this->data : false; + return $this->data ? $this->data : false; // Return the value of the 'data' property if it exists, otherwise return false } /** @@ -179,8 +191,8 @@ class D2ByteReader { * @param $data The byte data to set. * @return void */ - public function setData($data){ - $this->data = $data; + public function setData($data) { + $this->data = $data; // Set the value of the 'data' property to the provided data } /** @@ -189,7 +201,7 @@ class D2ByteReader { * @return int The current offset position. */ public function getOffset(): int { - return $this->offset; + return $this->offset; // Return the value of the 'offset' property } /** @@ -199,10 +211,12 @@ class D2ByteReader { * @return bool Returns true if the string is a valid hexadecimal string, false otherwise. */ public function isHexString(string $str): bool { + // Check if the length of the string is even and if all characters are hexadecimal using ctype_xdigit function if (strlen($str) % 2 == 0 && ctype_xdigit($str)) { - return true; + return true; // Return true if the string is a valid hexadecimal string } - return false; + + return false; // Return false if the string is not a valid hexadecimal string } /** @@ -212,24 +226,51 @@ class D2ByteReader { * @return string The converted string of bits. */ public function toBits($input): string { - $output = ''; + $output = ''; // Initialize an empty string to store the resulting bits + // Check if the input is a hexadecimal string if ($this->isHexString($input)) { + // Convert each byte of the hexadecimal string to binary and concatenate the bits to the output + // Split the hexadecimal string into an array of bytes (2 characters per byte) foreach (str_split($input, 2) as $byte) { + // Convert the byte from hexadecimal to decimal using `hexdec` function, + // then convert the decimal to binary using `decbin` function, + // reverse the resulting binary string using `strrev` function, + // and pad the binary string with leading zeros to make it 8 bits long using `str_pad` function. + // Finally, concatenate the resulting bits to the output string. $output .= (strrev(str_pad(decbin(hexdec($byte)), 8, 0, STR_PAD_LEFT))); } - return $output; + + return $output; // Return the resulting bits } else if (is_string($input)) { + // Convert each character of the string to binary and concatenate the bits to the output + // Split the string into an array of characters foreach (str_split($input) as $i) { + // Convert the character to its ASCII value using `ord` function, + // then convert the ASCII value to binary using `decbin` function, + // reverse the resulting binary string using `strrev` function, + // and pad the binary string with leading zeros to make it 8 bits long using `str_pad` function. + // Finally, concatenate the resulting bits to the output string. $output .= strrev(str_pad(decbin(ord($i)), 8, 0, STR_PAD_LEFT)); } - return $output; + + return $output; // Return the resulting bits } else if (is_int($input)) { + // Convert the integer to binary and return the resulting bits + // Convert the integer to binary using `decbin` function, + // reverse the resulting binary string using `strrev` function, + // and pad the binary string with leading zeros to make it 8 bits long using `str_pad` function. + // Finally, return the resulting bits. return strrev(str_pad(decbin($input), 8, 0, STR_PAD_LEFT)); } else if (is_array($input)) { + // If the input is an array, recursively call the `toBits` function for each element and concatenate the bits to the output + // Iterate over each element in the input array foreach ($input as $i) { + // Recursively call the `toBits` function for the current element, + // and concatenate the resulting bits to the output string. $output .= $this->toBits($i); } - return $output; + + return $output; // Return the resulting bits } } @@ -239,12 +280,18 @@ class D2ByteReader { * @param string $bits The string of bits to convert. * @return string The converted bytes. */ - public function toBytesR(string $bits) : string { - $bytes = ''; + public function toBytesR(string $bits): string { + $bytes = ''; // Initialize an empty string to store the resulting bytes + // Split the bits string into an array of byte-sized chunks (8 bits per chunk) foreach (str_split($bits, 8) as $byteString) { + // Convert each byte-sized chunk from binary to decimal using `bindec` function, + // then convert the decimal to hexadecimal using `dechex` function, + // and pad the resulting hexadecimal string with leading zeros to make it 2 characters long using `str_pad` function. + // Finally, concatenate the resulting byte to the bytes string. $bytes .= strtoupper(str_pad(dechex(bindec($byteString)), 2, 0, STR_PAD_LEFT)); } - return $bytes; + + return $bytes; // Return the resulting bytes } /** @@ -253,12 +300,19 @@ class D2ByteReader { * @param string $bits The string of bits to convert. * @return string The converted bytes. */ - public function toBytes(string $bits) : string { - $bytes = ''; + public function toBytes(string $bits): string { + $bytes = ''; // Initialize an empty string to store the resulting bytes + // Split the bits string into an array of byte-sized chunks (8 bits per chunk) foreach (str_split($bits, 8) as $byteString) { + // Reverse the order of bits within each byte-sized chunk using `strrev` function, + // then convert the reversed binary string to decimal using `bindec` function, + // then convert the decimal to hexadecimal using `dechex` function, + // and pad the resulting hexadecimal string with leading zeros to make it 2 characters long using `str_pad` function. + // Finally, concatenate the resulting byte to the bytes string. $bytes .= strtoupper(str_pad(dechex(bindec(strrev($byteString))), 2, 0, STR_PAD_LEFT)); } - return $bytes; + + return $bytes; // Return the resulting bytes } /** @@ -268,11 +322,18 @@ class D2ByteReader { * @return string The converted hexadecimal string. */ public function bitsToHexString(string $bits): string { - $bytes = ''; + $bytes = ''; // Initialize an empty string to store the resulting hexadecimal bytes + // Split the bits string into an array of byte-sized chunks (8 bits per chunk) foreach (str_split($bits, 8) as $byte) { + // Reverse the order of bits within each byte-sized chunk using `strrev` function, + // then convert the reversed binary string to decimal using `bindec` function, + // then convert the decimal to hexadecimal using `dechex` function, + // and pad the resulting hexadecimal string with leading zeros to make it 2 characters long using `str_pad` function. + // Finally, concatenate the resulting hexadecimal byte to the bytes string. $bytes .= (str_pad(dechex(bindec(strrev($byte))), 2, 0, STR_PAD_LEFT)); } - return $bytes; + + return $bytes; // Return the resulting hexadecimal bytes } /** @@ -282,10 +343,18 @@ class D2ByteReader { * @return array The converted array of hexadecimal values. */ public function bitsToHexArray(string $bits): array { - $bytes = []; + $bytes = []; // Initialize an empty array to store the resulting hexadecimal bytes + // Split the bits string into an array of byte-sized chunks (8 bits per chunk) foreach (str_split($bits, 8) as $byte) { + // Reverse the order of bits within each byte-sized chunk using `strrev` function, + // then convert the reversed binary string to decimal using `bindec` function, + // then convert the decimal to hexadecimal using `dechex` function, + // and pad the resulting hexadecimal string with leading zeros to make it 2 characters long using `str_pad` function. + // Finally, add the resulting hexadecimal byte to the bytes array. $bytes[] = (str_pad(dechex(bindec(strrev($byte))), 2, 0, STR_PAD_LEFT)); } - return $bytes; + + return $bytes; // Return the resulting array of hexadecimal bytes } + }