Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Cbor/CborEncoder.php

raw symbols SCIP
1 <?php
2 namespace Aws\Cbor;
3
4 use Aws\Cbor\Exception\CborException;
5 use DateTimeInterface;
6
7 /**
8 * Encodes PHP values to Concise Binary Object Representation according to RFC 8949
9 * https://www.rfc-editor.org/rfc/rfc8949.html
10 *
11 * Supports Major types 0-7 including:
12 * - Type 0: Unsigned integers
13 * - Type 1: Negative integers
14 * - Type 2: Byte strings (via ['__cbor_bytes' => $data] wrappers)
15 * - Type 3: Text strings (UTF-8)
16 * - Type 4: Arrays
17 * - Type 5: Maps
18 * - Type 6: Tagged values (timestamps)
19 * - Type 7: Simple values (null, bool, float)
20 *
21 * @internal
22 */
23 final class CborEncoder
24 {
25 /**
26 * Pre-encoded integers 0-23 (single byte) and common larger values
27 * CBOR major type 0 (unsigned integer)
28 */
29 private const INT_CACHE = [
30 0 => "\x00", 1 => "\x01", 2 => "\x02", 3 => "\x03",
31 4 => "\x04", 5 => "\x05", 6 => "\x06", 7 => "\x07",
32 8 => "\x08", 9 => "\x09", 10 => "\x0A", 11 => "\x0B",
33 12 => "\x0C", 13 => "\x0D", 14 => "\x0E", 15 => "\x0F",
34 16 => "\x10", 17 => "\x11", 18 => "\x12", 19 => "\x13",
35 20 => "\x14", 21 => "\x15", 22 => "\x16", 23 => "\x17",
36 24 => "\x18\x18", 25 => "\x18\x19", 26 => "\x18\x1A",
37 32 => "\x18\x20", 50 => "\x18\x32", 64 => "\x18\x40",
38 100 => "\x18\x64", 128 => "\x18\x80", 200 => "\x18\xC8",
39 255 => "\x18\xFF", 256 => "\x19\x01\x00", 500 => "\x19\x01\xF4",
40 1000 => "\x19\x03\xE8", 1023 => "\x19\x03\xFF",
41 ];
42
43 /**
44 * Pre-encoded negative integers -1 to -24 and common larger values
45 * CBOR major type 1 (negative integer)
46 */
47 private const NEG_CACHE = [
48 -1 => "\x20", -2 => "\x21", -3 => "\x22", -4 => "\x23",
49 -5 => "\x24", -10 => "\x29", -20 => "\x33", -24 => "\x37",
50 -25 => "\x38\x18", -50 => "\x38\x31", -100 => "\x38\x63",
51 ];
52
53 /**
54 * Encode a PHP value to CBOR binary string
55 *
56 * @param mixed $value The value to encode
57 *
58 * @return string
59 */
60 public function encode(mixed $value): string
61 {
62 return $this->encodeValue($value);
63 }
64
65 /**
66 * Recursively encode a value to CBOR
67 *
68 * @param mixed $value Value to encode
69 * @return string Encoded CBOR bytes
70 */
71 private function encodeValue(mixed $value): string
72 {
73 switch (gettype($value)) {
74 case 'string':
75 $len = strlen($value);
76 if ($len < 24) {
77 return chr(0x60 | $len) . $value;
78 }
79
80 if ($len < 0x100) {
81 return "\x78" . chr($len) . $value;
82 }
83
84 return $this->encodeTextString($value);
85
86 case 'array':
87 // Encode a byte string (major type 2)
88 if (isset($value['__cbor_bytes'])) {
89 $bytes = $value['__cbor_bytes'];
90 $len = strlen($bytes);
91 if ($len < 24) {
92 return chr(0x40 | $len) . $bytes;
93 }
94
95 if ($len < 0x100) {
96 return "\x58" . chr($len) . $bytes;
97 }
98
99 if ($len < 0x10000) {
100 return "\x59" . pack('n', $len) . $bytes;
101 }
102
103 return "\x5A" . pack('N', $len) . $bytes;
104 }
105
106 if (array_is_list($value)) {
107 return $this->encodeArray($value);
108 }
109
110 return $this->encodeMap($value);
111
112 case 'integer':
113 if (isset(self::INT_CACHE[$value])) {
114 return self::INT_CACHE[$value];
115 }
116
117 if (isset(self::NEG_CACHE[$value])) {
118 return self::NEG_CACHE[$value];
119 }
120
121 // Fast path for positive integers
122 // Major type 0: unsigned integer
123 if ($value >= 0) {
124 if ($value < 24) {
125 return chr($value);
126 }
127
128 if ($value < 0x100) {
129 return "\x18" . chr($value);
130 }
131
132 if ($value < 0x10000) {
133 return "\x19" . pack('n', $value);
134 }
135
136 if ($value < 0x100000000) {
137 return "\x1A" . pack('N', $value);
138 }
139
140 return "\x1B" . pack('J', $value);
141 }
142
143 return $this->encodeInteger($value);
144
145 case 'double':
146 // Encode a float (major type 7, float 64)
147 return "\xFB" . pack('E', $value);
148
149 case 'boolean':
150 // Encode a boolean (major type 7, simple)
151 return $value ? "\xF5" : "\xF4";
152
153 case 'NULL':
154 // Encode null (major type 7, simple)
155 return "\xF6";
156
157 case 'object':
158 // Encode timestamp (major type 6, tag 1)
159 if ($value instanceof DateTimeInterface) {
160 $timestamp = $value->getTimestamp();
161 $micro = (int) $value->format('u');
162 if ($micro === 0) {
163 if ($timestamp >= 0 && $timestamp < 0x100000000) {
164 return "\xC1\x1A" . pack('N', $timestamp);
165 }
166
167 return "\xC1" . $this->encodeInteger($timestamp);
168 }
169
170 return "\xC1\xFB" . pack('E', $timestamp + $micro / 1e6);
171 }
172
173 throw new CborException("Cannot encode object of type: " . get_class($value));
174
175 default:
176 throw new CborException("Cannot encode value of type: " . gettype($value));
177 }
178 }
179
180 /**
181 * Encode an integer (major type 0 or 1)
182 *
183 * @param int $value
184 * @return string
185 */
186 private function encodeInteger(int $value): string
187 {
188 if (isset(self::INT_CACHE[$value])) {
189 return self::INT_CACHE[$value];
190 }
191
192 if (isset(self::NEG_CACHE[$value])) {
193 return self::NEG_CACHE[$value];
194 }
195
196 if ($value >= 0) {
197 // Major type 0: unsigned integer
198 if ($value < 24) {
199 return chr($value);
200 }
201
202 if ($value < 0x100) {
203 return "\x18" . chr($value);
204 }
205
206 if ($value < 0x10000) {
207 return "\x19" . pack('n', $value);
208 }
209
210 if ($value < 0x100000000) {
211 return "\x1A" . pack('N', $value);
212 }
213
214 return "\x1B" . pack('J', $value);
215 }
216
217 // Major type 1: negative integer (-1 - n)
218 $value = -1 - $value;
219 if ($value < 24) {
220 return chr(0x20 | $value);
221 }
222
223 if ($value < 0x100) {
224 return "\x38" . chr($value);
225 }
226
227 if ($value < 0x10000) {
228 return "\x39" . pack('n', $value);
229 }
230
231 if ($value < 0x100000000) {
232 return "\x3A" . pack('N', $value);
233 }
234
235 return "\x3B" . pack('J', $value);
236 }
237
238 /**
239 * Encode a text string (major type 3)
240 *
241 * @param string $value
242 * @return string
243 */
244 private function encodeTextString(string $value): string
245 {
246 $len = strlen($value);
247
248 if ($len < 24) {
249 return chr(0x60 | $len) . $value;
250 }
251
252 if ($len < 0x100) {
253 return "\x78" . chr($len) . $value;
254 }
255
256 if ($len < 0x10000) {
257 return "\x79" . pack('n', $len) . $value;
258 }
259
260 if ($len < 0x100000000) {
261 return "\x7A" . pack('N', $len) . $value;
262 }
263
264 return "\x7B" . pack('J', $len) . $value;
265 }
266
267 /**
268 * Encode an array (major type 4)
269 *
270 * @param array $value
271 * @return string
272 */
273 private function encodeArray(array $value): string
274 {
275 $count = count($value);
276
277 if ($count < 24) {
278 $result = chr(0x80 | $count);
279 } elseif ($count < 0x100) {
280 $result = "\x98" . chr($count);
281 } elseif ($count < 0x10000) {
282 $result = "\x99" . pack('n', $count);
283 } elseif ($count < 0x100000000) {
284 $result = "\x9A" . pack('N', $count);
285 } else {
286 $result = "\x9B" . pack('J', $count);
287 }
288
289 foreach ($value as $item) {
290 $result .= $this->encodeValue($item);
291 }
292
293 return $result;
294 }
295
296 /**
297 * Encode a map (major type 5)
298 *
299 * @param array $value
300 * @return string
301 */
302 private function encodeMap(array $value): string
303 {
304 $count = count($value);
305
306 if ($count < 24) {
307 $result = chr(0xA0 | $count);
308 } elseif ($count < 0x100) {
309 $result = "\xB8" . chr($count);
310 } elseif ($count < 0x10000) {
311 $result = "\xB9" . pack('n', $count);
312 } elseif ($count < 0x100000000) {
313 $result = "\xBA" . pack('N', $count);
314 } else {
315 $result = "\xBB" . pack('J', $count);
316 }
317
318 foreach ($value as $k => $v) {
319 if (is_int($k)) {
320 $result .= $this->encodeInteger($k);
321 } else {
322 $len = strlen($k);
323 if ($len < 24) {
324 $result .= chr(0x60 | $len) . $k;
325 } elseif ($len < 0x100) {
326 $result .= "\x78" . chr($len) . $k;
327 } else {
328 $result .= "\x79" . pack('n', $len) . $k;
329 }
330 }
331
332 $result .= $this->encodeValue($v);
333 }
334
335 return $result;
336 }
337
338 /**
339 * Create an empty map (major type 5 with 0 elements)
340 *
341 * @return string
342 */
343 public function encodeEmptyMap(): string
344 {
345 return "\xA0";
346 }
347
348 /**
349 * Create an empty indefinite map (major type 5 indefinite length)
350 *
351 * @return string
352 */
353 public function encodeEmptyIndefiniteMap(): string
354 {
355 return "\xBF\xFF";
356 }
357 }