|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knik\Binn\Decoder; |
|
4
|
|
|
|
|
5
|
|
|
use Knik\Binn\Binn; |
|
6
|
|
|
|
|
7
|
|
|
class Unpacker |
|
8
|
|
|
{ |
|
9
|
|
|
public static function unpackType8(string $bytes): int |
|
10
|
|
|
{ |
|
11
|
|
|
return unpack("C", $bytes)[1]; |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public static function unpackType16(string $bytes): int |
|
15
|
|
|
{ |
|
16
|
|
|
return unpack("n", $bytes)[1]; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public static function unpackUint64($bytes): int |
|
20
|
|
|
{ |
|
21
|
|
|
return unpack("J", $bytes)[1]; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public static function unpackUint32($bytes): int |
|
25
|
|
|
{ |
|
26
|
|
|
if (strlen($bytes) !== 4) { |
|
27
|
|
|
$a = 1; |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
return unpack("N", $bytes)[1]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public static function unpackUint16($bytes): int |
|
33
|
|
|
{ |
|
34
|
|
|
return unpack("n", $bytes)[1]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function unpackUint8($bytes): int |
|
38
|
|
|
{ |
|
39
|
|
|
return unpack("C", $bytes)[1]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public static function unpackInt8($bytes): int |
|
43
|
|
|
{ |
|
44
|
|
|
return unpack("c", $bytes)[1]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function unpackInt16($bytes): int |
|
48
|
|
|
{ |
|
49
|
|
|
return unpack("s", strrev($bytes))[1]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function unpackInt32($bytes): int |
|
53
|
|
|
{ |
|
54
|
|
|
return unpack("i", strrev($bytes))[1]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function unpackInt64($bytes): int |
|
58
|
|
|
{ |
|
59
|
|
|
return unpack("q", strrev($bytes))[1]; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public static function unpackFloat32($bytes): float |
|
63
|
|
|
{ |
|
64
|
|
|
return unpack("G", strrev($bytes))[1]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function unpackFloat64($bytes): float |
|
68
|
|
|
{ |
|
69
|
|
|
return unpack("E", strrev($bytes))[1]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public static function unpackString($bytes): string |
|
73
|
|
|
{ |
|
74
|
|
|
return unpack("a*", $bytes)[1]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public static function unpackSize8($bytes): int |
|
78
|
|
|
{ |
|
79
|
|
|
return self::unpackUint8($bytes); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public static function unpackSize32($bytes): int |
|
83
|
|
|
{ |
|
84
|
|
|
$size = self::unpackUint32($bytes); |
|
85
|
|
|
return ($size ^ 0x80000000); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|