-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCborEncoder.php
More file actions
236 lines (203 loc) · 5.67 KB
/
Copy pathCborEncoder.php
File metadata and controls
236 lines (203 loc) · 5.67 KB
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
233
234
235
236
<?php
declare(strict_types=1);
namespace Pps\Encoding;
use Pps\Exception\EncodingException;
/**
* Deterministic CBOR encoder.
*
* Implements RFC 8949 deterministic encoding rules:
* - Map keys sorted in ascending numeric order
* - Definite-length encoding only
* - Shortest integer encoding
* - No indefinite-length items
* - No floating-point values in signed structures
* - No CBOR tags unless explicitly defined
*
* @package Pps\Encoding
*/
final class CborEncoder
{
/**
* Encode a PHP value to deterministic CBOR.
*
* @param mixed $value Value to encode.
* @return string CBOR-encoded bytes.
* @throws EncodingException If encoding fails.
*/
public static function encode(mixed $value): string
{
return self::encodeValue($value);
}
/**
* Encode a value recursively.
*
* @param mixed $value Value to encode.
* @return string CBOR bytes.
* @throws EncodingException
*/
private static function encodeValue(mixed $value): string
{
if (is_int($value)) {
return self::encodeInteger($value);
}
if (is_string($value)) {
return self::encodeByteString($value);
}
if (is_bool($value)) {
return self::encodeBoolean($value);
}
if (is_array($value)) {
// Check if associative array (map) or sequential array
if (self::isAssociative($value)) {
return self::encodeMap($value);
}
return self::encodeArray($value);
}
if ($value === null) {
return self::encodeNull();
}
throw new EncodingException('Unsupported CBOR type: ' . gettype($value));
}
/**
* Encode an integer using shortest form.
*
* @param int $value Integer value.
* @return string CBOR bytes.
*/
private static function encodeInteger(int $value): string
{
if ($value >= 0) {
return self::encodeUnsignedInteger($value);
}
// Negative integers: major type 1
$value = -1 - $value;
return self::encodeMajorType(1, $value);
}
/**
* Encode an unsigned integer.
*
* @param int $value Non-negative integer.
* @return string CBOR bytes.
*/
private static function encodeUnsignedInteger(int $value): string
{
return self::encodeMajorType(0, $value);
}
/**
* Encode major type and value.
*
* @param int $majorType CBOR major type (0-7).
* @param int $value Value.
* @return string CBOR bytes.
*/
private static function encodeMajorType(int $majorType, int $value): string
{
$mt = $majorType << 5;
if ($value < 24) {
return chr($mt | $value);
}
if ($value < 256) {
return chr($mt | 24) . chr($value);
}
if ($value < 65536) {
return chr($mt | 25) . pack('n', $value);
}
if ($value < 4294967296) {
return chr($mt | 26) . pack('N', $value);
}
return chr($mt | 27) . pack('J', $value);
}
/**
* Encode a byte string.
*
* @param string $value Raw bytes.
* @return string CBOR bytes.
*/
private static function encodeByteString(string $value): string
{
return self::encodeMajorType(2, strlen($value)) . $value;
}
/**
* Encode a text string.
*
* @param string $value UTF-8 string.
* @return string CBOR bytes.
*/
private static function encodeTextString(string $value): string
{
return self::encodeMajorType(3, strlen($value)) . $value;
}
/**
* Encode an array (CBOR array).
*
* @param array $values Sequential array.
* @return string CBOR bytes.
*/
private static function encodeArray(array $values): string
{
$result = self::encodeMajorType(4, count($values));
foreach ($values as $value) {
$result .= self::encodeValue($value);
}
return $result;
}
/**
* Encode a map (CBOR map) with sorted keys.
*
* @param array $map Associative array.
* @return string CBOR bytes.
* @throws EncodingException
*/
private static function encodeMap(array $map): string
{
// Sort keys in ascending numeric order
ksort($map, SORT_NUMERIC);
$result = self::encodeMajorType(5, count($map));
foreach ($map as $key => $value) {
// Encode key
if (is_int($key)) {
$result .= self::encodeInteger($key);
} elseif (is_string($key)) {
$result .= self::encodeTextString($key);
} else {
throw new EncodingException('Invalid CBOR map key type.');
}
// Encode value
$result .= self::encodeValue($value);
}
return $result;
}
/**
* Encode a boolean.
*
* @param bool $value Boolean value.
* @return string CBOR bytes.
*/
private static function encodeBoolean(bool $value): string
{
// Major type 7, value 20 (false) or 21 (true)
return chr(0xF4 | ($value ? 1 : 0));
}
/**
* Encode null.
*
* @return string CBOR bytes.
*/
private static function encodeNull(): string
{
return chr(0xF6);
}
/**
* Check if an array is associative.
*
* @param array $array Array to check.
* @return bool True if associative.
*/
private static function isAssociative(array $array): bool
{
if (empty($array)) {
return false;
}
return array_keys($array) !== range(0, count($array) - 1);
}
}