forked from UltimaPHP/UltimaPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackets.class.php
232 lines (182 loc) · 6.27 KB
/
packets.class.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Ultima PHP - OpenSource Ultima Online Server written in PHP
* Version: 0.1 - Pre Alpha
*/
class Packets {
/* Default packet variables */
public $client;
public $length;
public $packet;
public $packetBytes = [];
/* Defines what is the packet server is building/receiving */
public function setPacket($packet_id = false) {
if (!$packet_id) {
return false;
}
$this->packet = $packet_id;
$this->setLength(PacketsDefs::LENGTH[$packet_id]);
}
/**
* Get the packet ID
*/
public function getPacket() {
return $this->packet;
}
/**
* Set the packet length
* -1 = dynamic packet length
*/
public function setLength($packet_length = -1) {
$this->length = $packet_length;
}
/**
* Returns the defined packet length
*/
public function getLength() {
return $this->length;
}
public function getPacketStr() {
$packet = str_pad(dechex($this->packet), 2, "0", STR_PAD_LEFT);
if ($this->length === false) {
$packet .= str_pad(dechex((strlen(implode("", $this->packetBytes)) / 2) + 3), 4, "0", STR_PAD_LEFT);
}
$packet .= implode("", $this->packetBytes);
return $packet;
}
public function addText($text = "", $maxByteSyze = false, $fill_zeros = true, $pad_direction = STR_PAD_RIGHT) {
$hexStr = str_split(str_pad(Functions::strToHex($text), ($maxByteSyze ? ($maxByteSyze * 2) : $maxByteSyze), "0", $pad_direction), 2);
foreach ($hexStr as $hex) {
$this->packetBytes[] = $hex;
}
}
public function addHexStr($hexStr) {
$bytes = str_split($hexStr, 2);
foreach ($bytes as $hex) {
$this->packetBytes[] = str_pad($hex, 2, "0", STR_PAD_LEFT);
}
}
public function addUInt8($value = 0) {
$this->packetBytes[] = $this->uInt8($value);
}
public function addInt8($value = 0) {
$this->packetBytes[] = $this->int8($value);
}
public function toChar8($int = 0) {
if ($int < -127 || $int > 127) {
return "00";
}
$this->packetBytes[] = str_pad(dechex(ord(pack("c", $int))), 2, "0", STR_PAD_LEFT);
}
public function addUInt16($value = 0) {
$hexStr = str_split(strtoupper(str_pad(dechex($this->uInt16($value)), 4, "0", STR_PAD_LEFT)), 2);
foreach ($hexStr as $hex) {
$this->packetBytes[] = $hex;
}
}
public function addInt16($value = 0) {
$hexStr = str_split($this->int16($value), 2);
foreach ($hexStr as $hex) {
$this->packetBytes[] = $hex;
}
}
public function addUInt32($value = 0) {
$this->packetBytes[] = $this->uInt32($value);
}
public function addInt32($value = 0) {
$this->packetBytes[] = $this->int32($value);
}
public function addUInt64($value = 0) {
$this->packetBytes[] = $this->uInt64($value);
}
public function addInt64($value = 0) {
$this->packetBytes[] = $this->int64(fread($this->fileHolder, 8));
}
public static function int8($i) {
return is_int($i) ? strtoupper(str_pad(dechex($i), 2, "0", STR_PAD_LEFT)) : unpack("c", $i)[1];
}
public static function uInt8($i) {
return is_int($i) ? strtoupper(str_pad(dechex($i), 2, "0", STR_PAD_LEFT)) : unpack("C", $i)[1];
}
public static function int16($i) {
return is_int($i) ? strtoupper(str_pad(dechex($i), 4, "0", STR_PAD_LEFT)) : unpack("s", $i)[1];
}
public static function uInt16($i, $endianness = false) {
$f = is_int($i) ? "pack" : "unpack";
if ($endianness === true) {
// big-endian
$i = $f("n", $i);
} else if ($endianness === false) {
// little-endian
$i = $f("v", $i);
} else if ($endianness === null) {
// machine byte order
$i = $f("S", $i);
}
return is_array($i) ? $i[1] : $i;
}
public static function int32($i) {
return sprintf("%08X", $i);
}
public static function uInt32($i, $endianness = false) {
$f = is_int($i) ? "pack" : "unpack";
if ($endianness === true) {
// big-endian
$i = $f("N", $i);
} else if ($endianness === false) {
// little-endian
$i = $f("V", $i);
} else if ($endianness === null) {
// machine byte order
$i = $f("L", $i);
}
return is_array($i) ? $i[1] : $i;
}
public static function int64($i) {
return is_int($i) ? pack("q", $i) : unpack("q", $i)[1];
}
public static function uInt64($i, $endianness = false) {
$f = is_int($i) ? "pack" : "unpack";
if ($endianness === true) {
// big-endian
$i = $f("J", $i);
} else if ($endianness === false) {
// little-endian
$i = $f("P", $i);
} else if ($endianness === null) {
// machine byte order
$i = $f("Q", $i);
}
return is_array($i) ? $i[1] : $i;
}
public static function getInt32(&$var)
{
$packet = array_slice($var, 0, 4);
$var = array_slice($var,4);
return implode($packet);
}
public static function getInt16(&$var)
{
$packet = array_slice($var, 0, 2);
$var = array_slice($var,2);
return implode($packet);
}
public static function getInt8(&$var)
{
$packet = array_slice($var, 0, 1);
$var = array_slice($var,1);
return implode($packet);
}
public static function getSByte(&$var)
{
$packet = array_slice($var, 0, 1);
$var = array_slice($var,1);
return Functions::fromChar8(implode($packet));
}
public static function getUnicodeStringSafe($sendString, $byteSize = 30)
{
$text = "";
$text = str_pad(strtoupper(Functions::strToHex($sendString)), $byteSize*2, "0", STR_PAD_RIGHT);
return $text;
}
}