1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knik\Binn\Encoder; |
4
|
|
|
|
5
|
|
|
class Packer |
6
|
|
|
{ |
7
|
|
|
public static function packUint64(int $value): string |
8
|
|
|
{ |
9
|
|
|
return pack("J", $value); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public static function packUint32(int $value): string |
13
|
|
|
{ |
14
|
|
|
return pack("N", $value); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function packUint16(int $value): string |
18
|
|
|
{ |
19
|
|
|
return pack("n", $value); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function packUint8(int $value): string |
23
|
|
|
{ |
24
|
|
|
return pack("C", $value); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function packInt64(int $value): string |
28
|
|
|
{ |
29
|
|
|
return strrev(pack("q", $value)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function packInt32(int $value): string |
33
|
|
|
{ |
34
|
|
|
return strrev(pack("i", $value)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function packInt16(int $value): string |
38
|
|
|
{ |
39
|
|
|
return strrev(pack("s", $value)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function packInt8(int $value): string |
43
|
|
|
{ |
44
|
|
|
return pack("c", $value); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function packFloat32(float $value): string |
48
|
|
|
{ |
49
|
|
|
return pack("f", $value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function packFloat64(float $value): string |
53
|
|
|
{ |
54
|
|
|
return pack("e", $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function packString(string $value): string |
58
|
|
|
{ |
59
|
|
|
return pack("a*", $value); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function packSize(int $size, bool $totalSize = false): string |
63
|
|
|
{ |
64
|
|
|
$sz = $size; |
65
|
|
|
|
66
|
|
|
if ($totalSize) { |
67
|
|
|
$sz++; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($sz <= 127) { |
71
|
|
|
return self::packUint8($sz); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($totalSize) { |
75
|
|
|
$sz += 3; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return self::packSize32($sz); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function packSize32($size = 0): string |
82
|
|
|
{ |
83
|
|
|
$sizeWithBit = $size | (1 << 31); |
84
|
|
|
return self::packUint32($sizeWithBit); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public static function packNull(): string |
88
|
|
|
{ |
89
|
|
|
return "\x00"; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|