PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Backup / FileHeader / ExtraFieldCodec.php
wp-staging / Backup / FileHeader Last commit date
ExtraFieldCodec.php 3 days ago ExtraFieldType.php 3 days ago
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