ExtraFieldCodec.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\FileHeader; |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | final class ExtraFieldCodec |
| 28 | { |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | const MAGIC = "\x57\x54"; |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | const MAX_VALUE_LENGTH = 65535; |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | public function encode(array $entries): string |
| 56 | { |
| 57 | if (empty($entries)) { |
| 58 | return ''; |
| 59 | } |
| 60 | |
| 61 | $out = self::MAGIC; |
| 62 | foreach ($entries as $type => $value) { |
| 63 | if ($type === ExtraFieldType::LEGACY_RAW) { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | if (!is_int($type) || $type < 0 || $type > 0xFF) { |
| 68 | throw new \UnexpectedValueException(sprintf('ExtraFieldCodec: type %s is out of the 0x00-0xFF range.', var_export($type, true))); |
| 69 | } |
| 70 | |
| 71 | $length = strlen($value); |
| 72 | if ($length > self::MAX_VALUE_LENGTH) { |
| 73 | throw new \UnexpectedValueException(sprintf('ExtraFieldCodec: value for type 0x%02X is %d bytes, exceeding the %d-byte limit.', $type, $length, self::MAX_VALUE_LENGTH)); |
| 74 | } |
| 75 | |
| 76 | if (isset(ExtraFieldType::FIXED_WIRE_SIZES[$type]) && $length !== ExtraFieldType::FIXED_WIRE_SIZES[$type]) { |
| 77 | throw new \UnexpectedValueException(sprintf('ExtraFieldCodec: type 0x%02X requires exactly %d bytes, got %d.', $type, ExtraFieldType::FIXED_WIRE_SIZES[$type], $length)); |
| 78 | } |
| 79 | |
| 80 | $out .= chr($type) . pack('n', $length) . $value; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | if ($out === self::MAGIC) { |
| 85 | return ''; |
| 86 | } |
| 87 | |
| 88 | return $out; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | public function decode(string $bytes): array |
| 102 | { |
| 103 | if ($bytes === '') { |
| 104 | return []; |
| 105 | } |
| 106 | |
| 107 | if (substr($bytes, 0, 2) !== self::MAGIC) { |
| 108 | return [ExtraFieldType::LEGACY_RAW => $bytes]; |
| 109 | } |
| 110 | |
| 111 | $entries = []; |
| 112 | $offset = 2; |
| 113 | $total = strlen($bytes); |
| 114 | |
| 115 | while ($offset < $total) { |
| 116 | if ($total - $offset < 3) { |
| 117 | throw new \UnexpectedValueException('ExtraFieldCodec: truncated entry header.'); |
| 118 | } |
| 119 | |
| 120 | $type = ord($bytes[$offset]); |
| 121 | $length = unpack('n', substr($bytes, $offset + 1, 2))[1]; |
| 122 | $offset += 3; |
| 123 | |
| 124 | if ($type === ExtraFieldType::LEGACY_RAW) { |
| 125 | throw new \UnexpectedValueException(sprintf('ExtraFieldCodec: type 0x%02X is reserved as a parser-only sentinel and is not valid on the wire.', ExtraFieldType::LEGACY_RAW)); |
| 126 | } |
| 127 | |
| 128 | if ($total - $offset < $length) { |
| 129 | throw new \UnexpectedValueException(sprintf('ExtraFieldCodec: declared length %d for type 0x%02X overruns end of input.', $length, $type)); |
| 130 | } |
| 131 | |
| 132 | if (array_key_exists($type, $entries)) { |
| 133 | throw new \UnexpectedValueException(sprintf('ExtraFieldCodec: duplicate entry of type 0x%02X.', $type)); |
| 134 | } |
| 135 | |
| 136 | $entries[$type] = substr($bytes, $offset, $length); |
| 137 | $offset += $length; |
| 138 | } |
| 139 | |
| 140 | return $entries; |
| 141 | } |
| 142 | } |
| 143 |