/web/includes/SourceQuery/Buffer.php

https://gitlab.com/Rushaway/sourcebanspp · PHP · 199 lines · 89 code · 31 blank · 79 comment · 9 complexity · f1e92cd1beb2c3bc30ca873f803b9a73 MD5 · raw file

  1. <?php
  2. /**
  3. * @author Pavel Djundik <sourcequery@xpaw.me>
  4. *
  5. * @link https://xpaw.me
  6. * @link https://github.com/xPaw/PHP-Source-Query
  7. *
  8. * @license GNU Lesser General Public License, version 2.1
  9. *
  10. * @internal
  11. */
  12. namespace xPaw\SourceQuery;
  13. use xPaw\SourceQuery\Exception\InvalidPacketException;
  14. /**
  15. * Class Buffer
  16. *
  17. * @package xPaw\SourceQuery
  18. *
  19. * @uses xPaw\SourceQuery\Exception\InvalidPacketException
  20. */
  21. class Buffer
  22. {
  23. /**
  24. * Buffer
  25. *
  26. * @var string
  27. */
  28. private $Buffer;
  29. /**
  30. * Buffer length
  31. *
  32. * @var int
  33. */
  34. private $Length;
  35. /**
  36. * Current position in buffer
  37. *
  38. * @var int
  39. */
  40. private $Position;
  41. /**
  42. * Sets buffer
  43. *
  44. * @param string $Buffer Buffer
  45. */
  46. public function Set( $Buffer )
  47. {
  48. $this->Buffer = $Buffer;
  49. $this->Length = StrLen( $Buffer );
  50. $this->Position = 0;
  51. }
  52. /**
  53. * Get remaining bytes
  54. *
  55. * @return int Remaining bytes in buffer
  56. */
  57. public function Remaining( )
  58. {
  59. return $this->Length - $this->Position;
  60. }
  61. /**
  62. * Gets data from buffer
  63. *
  64. * @param int $Length Bytes to read
  65. *
  66. * @return string
  67. */
  68. public function Get( $Length = -1 )
  69. {
  70. if( $Length === 0 )
  71. {
  72. return '';
  73. }
  74. $Remaining = $this->Remaining( );
  75. if( $Length === -1 )
  76. {
  77. $Length = $Remaining;
  78. }
  79. else if( $Length > $Remaining )
  80. {
  81. return '';
  82. }
  83. $Data = SubStr( $this->Buffer, $this->Position, $Length );
  84. $this->Position += $Length;
  85. return $Data;
  86. }
  87. /**
  88. * Get byte from buffer
  89. *
  90. * @return int
  91. */
  92. public function GetByte( )
  93. {
  94. return Ord( $this->Get( 1 ) );
  95. }
  96. /**
  97. * Get short from buffer
  98. *
  99. * @return int
  100. */
  101. public function GetShort( )
  102. {
  103. if( $this->Remaining( ) < 2 )
  104. {
  105. throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY );
  106. }
  107. $Data = UnPack( 'v', $this->Get( 2 ) );
  108. return $Data[ 1 ];
  109. }
  110. /**
  111. * Get long from buffer
  112. *
  113. * @return int
  114. */
  115. public function GetLong( )
  116. {
  117. if( $this->Remaining( ) < 4 )
  118. {
  119. throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY );
  120. }
  121. $Data = UnPack( 'l', $this->Get( 4 ) );
  122. return $Data[ 1 ];
  123. }
  124. /**
  125. * Get float from buffer
  126. *
  127. * @return float
  128. */
  129. public function GetFloat( )
  130. {
  131. if( $this->Remaining( ) < 4 )
  132. {
  133. throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY );
  134. }
  135. $Data = UnPack( 'f', $this->Get( 4 ) );
  136. return $Data[ 1 ];
  137. }
  138. /**
  139. * Get unsigned long from buffer
  140. *
  141. * @return int
  142. */
  143. public function GetUnsignedLong( )
  144. {
  145. if( $this->Remaining( ) < 4 )
  146. {
  147. throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY );
  148. }
  149. $Data = UnPack( 'V', $this->Get( 4 ) );
  150. return $Data[ 1 ];
  151. }
  152. /**
  153. * Read one string from buffer ending with null byte
  154. *
  155. * @return string
  156. */
  157. public function GetString( )
  158. {
  159. $ZeroBytePosition = StrPos( $this->Buffer, "\0", $this->Position );
  160. if( $ZeroBytePosition === false )
  161. {
  162. return '';
  163. }
  164. $String = $this->Get( $ZeroBytePosition - $this->Position );
  165. $this->Position++;
  166. return $String;
  167. }
  168. }