| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
namespace AWS\CRT\Internal; |
| 7 |
|
| 8 |
final class Encoding { |
| 9 |
public static function readString(&$buffer) { |
| 10 |
list($len, $str) = self::decodeString($buffer); |
| 11 |
|
| 12 |
$buffer = substr($buffer, 4 + $len); |
| 13 |
return $str; |
| 14 |
} |
| 15 |
|
| 16 |
public static function readStrings($buffer) { |
| 17 |
$strings = []; |
| 18 |
while (strlen($buffer)) { |
| 19 |
$strings []= self::readString($buffer); |
| 20 |
} |
| 21 |
return $strings; |
| 22 |
} |
| 23 |
|
| 24 |
public static function decodeString($buffer) { |
| 25 |
$len = unpack("N", $buffer)[1]; |
| 26 |
$buffer = substr($buffer, 4); |
| 27 |
$str = unpack("a{$len}", $buffer)[1]; |
| 28 |
return [$len, $str]; |
| 29 |
} |
| 30 |
|
| 31 |
public static function encodeString($str) { |
| 32 |
if (is_array($str)) { |
| 33 |
$str = $str[0]; |
| 34 |
} |
| 35 |
return pack("Na*", strlen($str), $str); |
| 36 |
} |
| 37 |
} |