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