Tabs + spaces

This commit is contained in:
slawkens 2024-05-30 08:20:59 +02:00
parent 30107222d4
commit 48f6ca0eba

View File

@ -15,15 +15,15 @@
/** /**
* Binary buffer container. * Binary buffer container.
* *
* <p> * <p>
* This is generic class for classes that uses buffer-baser read-write operations (it can also emulate C-like pointers). * This is generic class for classes that uses buffer-baser read-write operations (it can also emulate C-like pointers).
* <p> * <p>
* *
* <p> * <p>
* Note that unlike <var>NetworkMessage</var> class from OTServ C++ source code, in this one reading and writing positions are separated so you can pararelly read and write it's content like for example using object of this class as stack. * Note that unlike <var>NetworkMessage</var> class from OTServ C++ source code, in this one reading and writing positions are separated so you can pararelly read and write it's content like for example using object of this class as stack.
* </p> * </p>
* *
* @package POT * @package POT
* @version 0.1.3 * @version 0.1.3
* @property string $buffer Properties binary string. * @property string $buffer Properties binary string.
@ -39,162 +39,162 @@ class OTS_Buffer
{ {
/** /**
* Node properties stream. * Node properties stream.
* *
* @var string * @var string
*/ */
protected $buffer; protected $buffer;
/** /**
* Properties stream pointer. * Properties stream pointer.
* *
* @var int * @var int
*/ */
protected $pos; protected $pos;
/** /**
* Initializes new buffered reader. * Initializes new buffered reader.
* *
* @param string $buffer Buffer content. * @param string $buffer Buffer content.
*/ */
public function __construct($buffer = '') public function __construct($buffer = '')
{ {
$this->buffer = $buffer; $this->buffer = $buffer;
$this->pos = 0; $this->pos = 0;
} }
/** /**
* Magic PHP5 method. * Magic PHP5 method.
* *
* <p> * <p>
* Allows object importing from {@link http://www.php.net/manual/en/function.var-export.php var_export()}. * Allows object importing from {@link http://www.php.net/manual/en/function.var-export.php var_export()}.
* </p> * </p>
* *
* @param array $properties List of object properties. * @param array $properties List of object properties.
*/ */
public static function __set_state($properties) public static function __set_state($properties)
{ {
$object = new self(); $object = new self();
// loads properties // loads properties
foreach($properties as $name => $value) foreach($properties as $name => $value)
{ {
$object->$name = $value; $object->$name = $value;
} }
return $object; return $object;
} }
/** /**
* Returs properties stream. * Returs properties stream.
* *
* @return string Properties stream. * @return string Properties stream.
*/ */
public function getBuffer() public function getBuffer()
{ {
return $this->buffer; return $this->buffer;
} }
/** /**
* Sets properties stream. * Sets properties stream.
* *
* @param string Properties stream. * @param string Properties stream.
*/ */
public function setBuffer($buffer) public function setBuffer($buffer)
{ {
$this->buffer = $buffer; $this->buffer = $buffer;
$this->pos = 0; $this->pos = 0;
} }
/** /**
* Checks if there is anything left in stream. * Checks if there is anything left in stream.
* *
* @return bool False if pointer is at the end of stream. * @return bool False if pointer is at the end of stream.
*/ */
public function isValid() public function isValid()
{ {
return $this->pos < strlen($this->buffer); return $this->pos < strlen($this->buffer);
} }
/** /**
* Checks stream end state. * Checks stream end state.
* *
* @param int $size Amount of bytes that are going to be read. * @param int $size Amount of bytes that are going to be read.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
protected function check($size = 1) protected function check($size = 1)
{ {
if( strlen($this->buffer) < $this->pos + $size) if( strlen($this->buffer) < $this->pos + $size)
{ {
throw new E_OTS_OutOfBuffer(); throw new E_OTS_OutOfBuffer();
} }
} }
/** /**
* Returns single byte. * Returns single byte.
* *
* @return int Byte (char) value. * @return int Byte (char) value.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
public function getChar() public function getChar()
{ {
// checks buffer size // checks buffer size
$this->check(); $this->check();
$value = ord($this->buffer[$this->pos]); $value = ord($this->buffer[$this->pos]);
$this->pos++; $this->pos++;
return $value; return $value;
} }
/** /**
* Appends single byte to buffer. * Appends single byte to buffer.
* *
* @param int $char Byte (char) value. * @param int $char Byte (char) value.
*/ */
public function putChar($char) public function putChar($char)
{ {
$this->buffer .= chr($char); $this->buffer .= chr($char);
} }
/** /**
* Returns double byte. * Returns double byte.
* *
* @return int Word (short) value. * @return int Word (short) value.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
public function getShort() public function getShort()
{ {
// checks buffer size // checks buffer size
$this->check(2); $this->check(2);
$value = unpack('v', substr($this->buffer, $this->pos, 2) ); $value = unpack('v', substr($this->buffer, $this->pos, 2) );
$this->pos += 2; $this->pos += 2;
return $value[1]; return $value[1];
} }
/** /**
* Appends double byte to buffer. * Appends double byte to buffer.
* *
* @param int $short Word (short) value. * @param int $short Word (short) value.
*/ */
public function putShort($short) public function putShort($short)
{ {
$this->buffer .= pack('v', $short); $this->buffer .= pack('v', $short);
} }
/** /**
* Returns quater byte. * Returns quater byte.
* *
* @return int Double word (long) value. * @return int Double word (long) value.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
public function getLong() public function getLong()
{ {
// checks buffer size // checks buffer size
$this->check(4); $this->check(4);
$value = unpack('V', substr($this->buffer, $this->pos, 4) ); $value = unpack('V', substr($this->buffer, $this->pos, 4) );
$this->pos += 4; $this->pos += 4;
return $value[1]; return $value[1];
} }
public function getLongLong() public function getLongLong()
{ {
@ -208,222 +208,222 @@ class OTS_Buffer
/** /**
* Appends quater byte to buffer. * Appends quater byte to buffer.
* *
* @param int $long Double word (long) value. * @param int $long Double word (long) value.
*/ */
public function putLong($long) public function putLong($long)
{ {
$this->buffer .= pack('V', $long); $this->buffer .= pack('V', $long);
} }
/** /**
* Returns string from buffer. * Returns string from buffer.
* *
* <p> * <p>
* If length is not given then treats first short value from current buffer as string length. * If length is not given then treats first short value from current buffer as string length.
* </p> * </p>
* *
* @param int|bool $length String length. * @param int|bool $length String length.
* @return string First substring. * @return string First substring.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
public function getString($length = false) public function getString($length = false)
{ {
// reads string length if not given // reads string length if not given
if($length === false) if($length === false)
{ {
$length = $this->getShort(); $length = $this->getShort();
} }
// checks buffer size // checks buffer size
$this->check($length); $this->check($length);
// copies substring // copies substring
$value = substr($this->buffer, $this->pos, $length); $value = substr($this->buffer, $this->pos, $length);
$this->pos += $length; $this->pos += $length;
return $value; return $value;
} }
/** /**
* Appends string to buffer. * Appends string to buffer.
* *
* @param string $string Binary length. * @param string $string Binary length.
* @param bool $dynamic Whether if string length is fixed or not (if it is dynamic then length will be inserted as short before string chunk). * @param bool $dynamic Whether if string length is fixed or not (if it is dynamic then length will be inserted as short before string chunk).
*/ */
public function putString($string, $dynamic = true) public function putString($string, $dynamic = true)
{ {
// appends string length if requires // appends string length if requires
if($dynamic) if($dynamic)
{ {
$this->putShort( strlen($string) ); $this->putShort( strlen($string) );
} }
$this->buffer .= $string; $this->buffer .= $string;
} }
/** /**
* Empties buffer. * Empties buffer.
*/ */
public function reset() public function reset()
{ {
$this->__construct(); $this->__construct();
} }
/** /**
* Returns current read position. * Returns current read position.
* *
* @return int Read position. * @return int Read position.
*/ */
public function getPos() public function getPos()
{ {
return $this->pos; return $this->pos;
} }
/** /**
* Seeks current reading position. * Seeks current reading position.
* *
* @param int $pos Read position. * @param int $pos Read position.
*/ */
public function setPos($pos) public function setPos($pos)
{ {
$this->pos = $pos; $this->pos = $pos;
} }
/** /**
* Returns buffer size. * Returns buffer size.
* *
* @return int Buffer length. * @return int Buffer length.
*/ */
public function getSize() public function getSize()
{ {
return strlen($this->buffer); return strlen($this->buffer);
} }
/** /**
* Skips given amount of bytes. * Skips given amount of bytes.
* *
* @param int $n Bytes to skip. * @param int $n Bytes to skip.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
public function skip($n) public function skip($n)
{ {
$this->check($n); $this->check($n);
$this->pos += $n; $this->pos += $n;
} }
/** /**
* Magic PHP5 method. * Magic PHP5 method.
* *
* @param string $name Property name. * @param string $name Property name.
* @return mixed Property value. * @return mixed Property value.
* @throws OutOfBoundsException For non-supported properties. * @throws OutOfBoundsException For non-supported properties.
* @throws E_OTS_OutOfBuffer When there is read attemp after end of stream. * @throws E_OTS_OutOfBuffer When there is read attemp after end of stream.
*/ */
public function __get($name) public function __get($name)
{ {
switch($name) switch($name)
{ {
// simple properties // simple properties
case 'buffer': case 'buffer':
return $this->buffer; return $this->buffer;
// isValid() wrapper // isValid() wrapper
case 'valid': case 'valid':
return $this->isValid(); return $this->isValid();
// getChar() wrapper // getChar() wrapper
case 'char': case 'char':
return $this->getChar(); return $this->getChar();
// getShort() wrapper // getShort() wrapper
case 'short': case 'short':
return $this->getShort(); return $this->getShort();
// getLong() wrapper // getLong() wrapper
case 'long': case 'long':
return $this->getLong(); return $this->getLong();
// getString() wrapper // getString() wrapper
case 'string': case 'string':
return $this->getString(); return $this->getString();
// getPos() wrapper // getPos() wrapper
case 'pos': case 'pos':
return $this->getPos(); return $this->getPos();
// getSize() wrapper // getSize() wrapper
case 'size': case 'size':
return $this->getSize(); return $this->getSize();
default: default:
throw new OutOfBoundsException(); throw new OutOfBoundsException();
} }
} }
/** /**
* Magic PHP5 method. * Magic PHP5 method.
* *
* @version 0.1.3 * @version 0.1.3
* @param string $name Property name. * @param string $name Property name.
* @param mixed $value Property value. * @param mixed $value Property value.
* @throws OutOfBoundsException For non-supported properties. * @throws OutOfBoundsException For non-supported properties.
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
switch($name) switch($name)
{ {
// buffer needs to be reset // buffer needs to be reset
case 'buffer': case 'buffer':
$this->setBuffer($value); $this->setBuffer($value);
break; break;
// putChar() wrapper // putChar() wrapper
case 'char': case 'char':
$this->putChar($value); $this->putChar($value);
break; break;
// putShort() wrapper // putShort() wrapper
case 'short': case 'short':
$this->putShort($value); $this->putShort($value);
break; break;
// putLong() wrapper // putLong() wrapper
case 'long': case 'long':
$this->putLong($value); $this->putLong($value);
break; break;
// putString() wrapper // putString() wrapper
case 'string': case 'string':
$this->putString($value); $this->putString($value);
break; break;
// setPos() wrapper // setPos() wrapper
case 'pos': case 'pos':
$this->setPos($value); $this->setPos($value);
break; break;
default: default:
throw new OutOfBoundsException(); throw new OutOfBoundsException();
} }
} }
/** /**
* Returns string representation of buffer object. * Returns string representation of buffer object.
* *
* @return string Object's buffer. * @return string Object's buffer.
*/ */
public function __toString() public function __toString()
{ {
return $this->buffer; return $this->buffer;
} }
/** /**
* Resets pointer of cloned object. * Resets pointer of cloned object.
*/ */
public function __clone() public function __clone()
{ {
$this->pos = 0; $this->pos = 0;
} }
} }
/**#@-*/ /**#@-*/