|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knik\Binn; |
|
4
|
|
|
|
|
5
|
|
|
use Knik\Binn\Decoder\BinnDecode; |
|
6
|
|
|
use Knik\Binn\Decoder\DecoderCollectionFactory; |
|
7
|
|
|
use Knik\Binn\Encoder\BinnEncode; |
|
8
|
|
|
use Knik\Binn\Encoder\EncoderCollectionFactory; |
|
9
|
|
|
|
|
10
|
|
|
abstract class BinnAbstract |
|
11
|
|
|
{ |
|
12
|
|
|
// Consts from original C++ Library |
|
13
|
|
|
const BINN_LIST = 0xE0; |
|
14
|
|
|
const BINN_MAP = 0xE1; |
|
15
|
|
|
const BINN_OBJECT = 0xE2; |
|
16
|
|
|
|
|
17
|
|
|
const BINN_UINT8 = 0x20; |
|
18
|
|
|
const BINN_INT8 = 0x21; |
|
19
|
|
|
const BINN_UINT16 = 0x40; |
|
20
|
|
|
const BINN_INT16 = 0x41; |
|
21
|
|
|
const BINN_UINT32 = 0x60; |
|
22
|
|
|
const BINN_INT32 = 0x61; |
|
23
|
|
|
const BINN_UINT64 = 0x80; |
|
24
|
|
|
const BINN_INT64 = 0x81; |
|
25
|
|
|
const BINN_STRING = 0xA0; |
|
26
|
|
|
|
|
27
|
|
|
const BINN_FLOAT32 = 0x62; // (DWORD) |
|
28
|
|
|
const BINN_FLOAT64 = 0x82; // (QWORD) |
|
29
|
|
|
const BINN_FLOAT = self::BINN_FLOAT32; |
|
30
|
|
|
|
|
31
|
|
|
const BINN_BOOL = 0x80061; |
|
32
|
|
|
|
|
33
|
|
|
const BINN_STORAGE_NOBYTES = 0x00; |
|
34
|
|
|
const BINN_STORAGE_BYTE = 0x20; // 8 bits |
|
35
|
|
|
const BINN_STORAGE_WORD = 0x40; // 16 bits -- the endianess (byte order) is automatically corrected |
|
36
|
|
|
const BINN_STORAGE_DWORD = 0x60; // 32 bits -- the endianess (byte order) is automatically corrected |
|
37
|
|
|
const BINN_STORAGE_QWORD = 0x80; // 64 bits -- the endianess (byte order) is automatically corrected |
|
38
|
|
|
const BINN_STORAGE_STRING = 0xA0; // Are stored with null termination |
|
39
|
|
|
const BINN_STORAGE_BLOB = 0xC0; |
|
40
|
|
|
const BINN_STORAGE_CONTAINER = 0xE0; |
|
41
|
|
|
|
|
42
|
|
|
const BINN_NULL = 0x00; |
|
43
|
|
|
const BINN_TRUE = 0x01; |
|
44
|
|
|
const BINN_FALSE = 0x02; |
|
45
|
|
|
|
|
46
|
|
|
const UINT8_MAX = 255; |
|
47
|
|
|
const UINT16_MAX = 65535; |
|
48
|
|
|
const UINT32_MAX = 4294967295; |
|
49
|
|
|
const UINT64_MAX = 18446744073709551615; |
|
50
|
|
|
|
|
51
|
|
|
const INT8_MIN = -128; |
|
52
|
|
|
const INT8_MAX = 127; |
|
53
|
|
|
const INT16_MIN = -32768; |
|
54
|
|
|
const INT16_MAX = 32767; |
|
55
|
|
|
const INT32_MIN = -2147483648; |
|
56
|
|
|
const INT32_MAX = 2147483647; |
|
57
|
|
|
const INT64_MIN = -9223372036854775808; |
|
58
|
|
|
const INT64_MAX = 9223372036854775807; |
|
59
|
|
|
|
|
60
|
|
|
const BINN_STORAGE_MASK = 0xE0; |
|
61
|
|
|
const BINN_TYPE_MASK = 0x0F; |
|
62
|
|
|
|
|
63
|
|
|
const BINN_STORAGE_HAS_MORE = 0x10; |
|
64
|
|
|
|
|
65
|
|
|
const BINN_JPEG = 0xD001; |
|
66
|
|
|
const BINN_GIF = 0xD002; |
|
67
|
|
|
const BINN_PNG = 0xD003; |
|
68
|
|
|
const BINN_BMP = 0xD004; |
|
69
|
|
|
|
|
70
|
|
|
const MIN_BINN_SIZE = 3; |
|
71
|
|
|
|
|
72
|
|
|
const BINN_MAX_ONE_BYTE_SIZE = 127; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Binn object type: self::BINN_LIST, self::BINN_MAP, self::BINN_OBJECT |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $binnType = self::BINN_NULL; |
|
78
|
|
|
|
|
79
|
|
|
/** @var BinnEncode */ |
|
80
|
|
|
protected $encoder; |
|
81
|
|
|
|
|
82
|
|
|
/** @var BinnDecode */ |
|
83
|
|
|
protected $decoder; |
|
84
|
|
|
|
|
85
|
|
|
public function __construct( |
|
86
|
|
|
?BinnEncode $encoder = null, |
|
87
|
|
|
?BinnDecode $decoder = null |
|
88
|
|
|
) { |
|
89
|
|
|
if ($encoder === null) { |
|
90
|
|
|
$factory = new EncoderCollectionFactory(); |
|
91
|
|
|
$this->encoder = new BinnEncode($factory->getCollection()); |
|
92
|
|
|
} else { |
|
93
|
|
|
$this->encoder = $encoder; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if ($decoder === null) { |
|
97
|
|
|
$factory = new DecoderCollectionFactory(); |
|
98
|
|
|
$this->decoder = $decoder ?? new BinnDecode($factory->getCollection()); |
|
99
|
|
|
} else { |
|
100
|
|
|
$this->decoder = $decoder; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|