-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathBuffer.php
195 lines (159 loc) · 3.44 KB
/
Buffer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
/**
* @author Pavel Djundik
*
* @link https://xpaw.me
* @link https://github.com/xPaw/PHP-Source-Query
*
* @license GNU Lesser General Public License, version 2.1
*
* @internal
*/
namespace xPaw\SourceQuery;
use xPaw\SourceQuery\Exception\InvalidPacketException;
/**
* Class Buffer
*/
class Buffer
{
/**
* Buffer
*/
private string $Buffer = '';
/**
* Buffer length
*/
private int $Length = 0;
/**
* Current position in buffer
*/
private int $Position = 0;
/**
* Sets buffer
*/
public function Set( string $Buffer ) : void
{
$this->Buffer = $Buffer;
$this->Length = strlen( $Buffer );
$this->Position = 0;
}
/**
* Get remaining bytes
*
* @return int Remaining bytes in buffer
*/
public function Remaining( ) : int
{
return $this->Length - $this->Position;
}
/**
* Reads the specified number of bytes.
*
* @param int $Length Bytes to read
*/
public function Read( int $Length = -1 ) : string
{
if( $Length === 0 )
{
return '';
}
$Remaining = $this->Remaining( );
if( $Length === -1 )
{
$Length = $Remaining;
}
else if( $Length > $Remaining )
{
return '';
}
$Data = substr( $this->Buffer, $this->Position, $Length );
$this->Position += $Length;
return $Data;
}
/**
* Reads the next byte.
*/
public function ReadByte( ) : int
{
return ord( $this->Read( 1 ) );
}
/**
* Reads a 2-byte signed integer.
*/
public function ReadInt16( ) : int
{
if( $this->Remaining( ) < 2 )
{
throw new InvalidPacketException( 'Not enough data to unpack.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = unpack( 'v', $this->Read( 2 ) );
if( $Data === false )
{
throw new InvalidPacketException( 'Failed to unpack.', InvalidPacketException::UNPACK_FAILED );
}
return (int)$Data[ 1 ];
}
/**
* Reads a 4-byte signed integer.
*/
public function ReadInt32( ) : int
{
if( $this->Remaining( ) < 4 )
{
throw new InvalidPacketException( 'Not enough data to unpack.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = unpack( 'l', $this->Read( 4 ) );
if( $Data === false )
{
throw new InvalidPacketException( 'Failed to unpack.', InvalidPacketException::UNPACK_FAILED );
}
return (int)$Data[ 1 ];
}
/**
* Reads a 4-byte floating point value.
*/
public function ReadFloat32( ) : float
{
if( $this->Remaining( ) < 4 )
{
throw new InvalidPacketException( 'Not enough data to unpack.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = unpack( 'f', $this->Read( 4 ) );
if( $Data === false )
{
throw new InvalidPacketException( 'Failed to unpack.', InvalidPacketException::UNPACK_FAILED );
}
return (float)$Data[ 1 ];
}
/**
* Reads a 4-byte unsigned integer.
*/
public function ReadUInt32( ) : int
{
if( $this->Remaining( ) < 4 )
{
throw new InvalidPacketException( 'Not enough data to unpack.', InvalidPacketException::BUFFER_EMPTY );
}
$Data = unpack( 'V', $this->Read( 4 ) );
if( $Data === false )
{
throw new InvalidPacketException( 'Failed to unpack.', InvalidPacketException::UNPACK_FAILED );
}
return (int)$Data[ 1 ];
}
/**
* Read a null-terminated string.
*/
public function ReadNullTermString( ) : string
{
$ZeroBytePosition = strpos( $this->Buffer, "\0", $this->Position );
if( $ZeroBytePosition === false )
{
return '';
}
$String = $this->Read( $ZeroBytePosition - $this->Position );
$this->Position++;
return $String;
}
}