Hallööle..
Ich hab mir mal just4fun ne Binary Class in PHP gemacht
Hier der code:
Viel spaß!
Ich hab mir mal just4fun ne Binary Class in PHP gemacht
Hier der code:
PHP-Quellcode
- <?php
- class BinaryReader
- {
- protected $arrBytes = array();
- public function getBytes()
- {
- return $this->arrBytes;
- }
- protected $intIndex = 0;
- public function getIndex()
- {
- return $this->intIndex;
- }
- public function seekIndex($intSeek, $boolBackward = false)
- {
- $intMixReturn = false;
- if(is_numeric($intSeek) && $intSeek >= 0)
- {
- if(!$boolBackward && ($this->intIndex + $intSeek) <= count($this->arrBytes))
- {
- $this->intIndex += $intSeek;
- $intMixReturn = $this->intIndex;
- }
- elseif($boolBackward && ($this->intIndex - $intSeek) >= 0)
- {
- $this->intIndex -= $intSeek;
- $intMixReturn = $this->intIndex;
- }
- }
- return $intMixReturn;
- }
- function __construct($mixInput)
- {
- $this->intIndex = 0;
- $this->arrBytes = array();
- if(is_array($mixInput))
- {
- $this->arrBytes = $mixInput;
- }
- elseif(is_string($mixInput))
- {
- for($intIndex = 0; $intIndex < strlen($mixInput); $intIndex ++)
- {
- $this->arrBytes[] = ord($mixInput[$intIndex]);
- }
- }
- else
- {
- return false;
- }
- }
- public function canRead($intUnit = 1)
- {
- if(($this->intIndex + $intUnit) <= count($this->arrBytes))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public function readByte()
- {
- if($this->canRead(1))
- {
- $intTempByte = $this->arrBytes[$this->intIndex];
- $this->intIndex += 1;
- return $intTempByte;
- }
- else
- {
- return false;
- }
- }
- public function readShort()
- {
- if($this->canRead(2))
- {
- $intTempShort = BinaryHelper::GetShortFromBytes(array($this->arrBytes[$this->intIndex], $this->arrBytes[$this->intIndex + 1]));
- $this->intIndex += 2;
- return $intTempShort;
- }
- else
- {
- return false;
- }
- }
- public function readInteger()
- {
- if($this->canRead(4))
- {
- $intTempShort = BinaryHelper::GetIntegerFromBytes(array($this->arrBytes[$this->intIndex], $this->arrBytes[$this->intIndex + 1], $this->arrBytes[$this->intIndex + 2], $this->arrBytes[$this->intIndex + 3]));
- $this->intIndex += 4;
- return $intTempShort;
- }
- else
- {
- return false;
- }
- }
- public function readString()
- {
- $intLength = $this->readInteger();
- if($intLength !== false)
- {
- if($this->canRead($intLength))
- {
- $strTempString = "";
- for($intIndex = 0; $intIndex < $intLength; $intIndex ++)
- {
- $strTempString .= chr($this->readByte());
- }
- return $strTempString;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- class BinaryWriter
- {
- protected $arrBytes = array();
- public function getBytes()
- {
- return $this->arrBytes;
- }
- public function getBytesString()
- {
- $strFinalString = "";
- for($intIndex = 0; $intIndex < count($this->arrBytes); $intIndex ++)
- {
- $strFinalString .= chr($this->arrBytes[$intIndex]);
- }
- return $strFinalString;
- }
- public function getBytesHex()
- {
- $strFinalString = "";
- for($intIndex = 0; $intIndex < count($this->arrBytes); $intIndex ++)
- {
- $strTempString = strtoupper(dechex($this->arrBytes[$intIndex]));
- $strTempString = substr("00", 0, 2 - strlen($strTempString)).$strTempString;
- $strFinalString .= $strTempString." ";
- }
- return $strFinalString;
- }
- function __construct()
- {
- $this->arrBytes = array();
- }
- public function writeByte($intByte)
- {
- $intBytes = BinaryHelper::GetByteFromByte($intByte);
- $this->arrBytes[] = $intBytes[0];
- }
- public function writeString($strWrite)
- {
- $intStringLengthBytes = BinaryHelper::GetBytesFromInteger(strlen($strWrite));
- $this->arrBytes[] = $intStringLengthBytes[0];
- $this->arrBytes[] = $intStringLengthBytes[1];
- $this->arrBytes[] = $intStringLengthBytes[2];
- $this->arrBytes[] = $intStringLengthBytes[3];
- for($intIndex = 0; $intIndex < strlen($strWrite); $intIndex ++)
- {
- $this->arrBytes[] = ord($strWrite[$intIndex]);
- }
- }
- public function writeInteger($intWrite)
- {
- $intIntegerBytes = BinaryHelper::GetBytesFromInteger($intWrite);
- foreach($intIntegerBytes as $intIntegerByte)
- {
- $this->arrBytes[] = $intIntegerByte;
- }
- }
- public function writeShort($intWrite)
- {
- $intShortBytes = BinaryHelper::GetBytesFromShort($intWrite);
- foreach($intShortBytes as $intShortByte)
- {
- $this->arrBytes[] = $intShortByte;
- }
- }
- /*public function writeLong($intWrite)
- {
- $intLongBytes = BinaryHelper::GetBytesFromLong($intWrite);
- foreach($intLongBytes as $intLongByte)
- {
- $this->arrBytes[] = $intLongByte;
- }
- }*/
- }
- class BinaryHelper
- {
- public static function GetBytesFromInteger($intInteger)
- {
- $BitSequence = decbin($intInteger);
- $BitSequence = substr("00000000000000000000000000000000", 0, 32 - strlen($BitSequence)).$BitSequence;
- $BitSequence = substr($BitSequence, 0, 32);
- $arrBytes = array();
- $arrBytes[] = bindec(substr($BitSequence, 0, 8));
- $arrBytes[] = bindec(substr($BitSequence, 8, 8));
- $arrBytes[] = bindec(substr($BitSequence, 16, 8));
- $arrBytes[] = bindec(substr($BitSequence, 24, 8));
- return $arrBytes;
- }
- public static function GetByteFromByte($intByte)
- {
- $BitSequence = decbin($intByte);
- $BitSequence = substr("00000000", 0, 8 - strlen($BitSequence)).$BitSequence;
- $BitSequence = substr($BitSequence, 0, 8);
- $arrBytes = array();
- $arrBytes[] = bindec(substr($BitSequence, 0, 8));
- return $arrBytes;
- }
- public static function GetBytesFromShort($intShort)
- {
- $BitSequence = decbin($intShort);
- $BitSequence = substr("0000000000000000", 0, 16 - strlen($BitSequence)).$BitSequence;
- $BitSequence = substr($BitSequence, 0, 16);
- $arrBytes = array();
- $arrBytes[] = bindec(substr($BitSequence, 0, 8));
- $arrBytes[] = bindec(substr($BitSequence, 8, 8));
- return $arrBytes;
- }
- public static function GetBytesFromLong($intLong)
- {
- $BitSequence = decbin($intLong);
- $BitSequence = substr("0000000000000000000000000000000000000000000000000000000000000000", 0, 64 - strlen($BitSequence)).$BitSequence;
- $BitSequence = substr($BitSequence, 0, 64);
- $arrBytes = array();
- $arrBytes[] = bindec(substr($BitSequence, 0, 8));
- $arrBytes[] = bindec(substr($BitSequence, 8, 8));
- $arrBytes[] = bindec(substr($BitSequence, 16, 8));
- $arrBytes[] = bindec(substr($BitSequence, 24, 8));
- $arrBytes[] = bindec(substr($BitSequence, 32, 8));
- $arrBytes[] = bindec(substr($BitSequence, 40, 8));
- $arrBytes[] = bindec(substr($BitSequence, 48, 8));
- $arrBytes[] = bindec(substr($BitSequence, 56, 8));
- return $arrBytes;
- }
- public static function GetLongFromBytes($arrBytes)
- {
- if(count($arrBytes) != 8) { return false; }
- $BitSequence = "";
- for($intIndex = 0; $intIndex < 8; $intIndex ++)
- {
- $BitSequenceTemp = decbin($arrBytes[$intIndex]);
- $BitSequence .= substr("00000000", 0, 8 - strlen($BitSequenceTemp)).$BitSequenceTemp;
- }
- return bindec($BitSequence);
- }
- public static function GetIntegerFromBytes($arrBytes)
- {
- if(count($arrBytes) != 4) { return false; }
- $BitSequence = "";
- for($intIndex = 0; $intIndex < 4; $intIndex ++)
- {
- $BitSequenceTemp = decbin($arrBytes[$intIndex]);
- $BitSequence .= substr("00000000", 0, 8 - strlen($BitSequenceTemp)).$BitSequenceTemp;
- }
- return bindec($BitSequence);
- }
- public static function GetShortFromBytes($arrBytes)
- {
- if(count($arrBytes) != 2) { return false; }
- $BitSequence = "";
- for($intIndex = 0; $intIndex < 2; $intIndex ++)
- {
- $BitSequenceTemp = decbin($arrBytes[$intIndex]);
- $BitSequence .= substr("00000000", 0, 8 - strlen($BitSequenceTemp)).$BitSequenceTemp;
- }
- return bindec($BitSequence);
- }
- }
- ?>
Viel spaß!