PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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 / Framework / Utils / DataEncoder.php
wp-staging / Framework / Utils Last commit date
Cache 2 years ago DBPermissions.php 2 years ago DataEncoder.php 2 years ago Escape.php 2 years ago Glob.php 2 years ago Hooks.php 2 years ago Math.php 2 years ago PluginInfo.php 2 years ago Sanitize.php 2 years ago ServerVars.php 2 years ago SlashMode.php 5 years ago Strings.php 2 years ago Times.php 2 years ago Urls.php 2 years ago Version.php 2 years ago WpDefaultDirectories.php 2 years ago
DataEncoder.php
170 lines
1 <?php
2
3 namespace WPStaging\Framework\Utils;
4
5 use InvalidArgumentException;
6
7 /**
8 * The class is responsible for converting data to/from binary and/or hex format
9 * It uses pack, unpack and bin2hex functions internally
10 */
11 class DataEncoder
12 {
13 /**
14 * The pack mode for 64-bit integer
15 * P -> Big Endianness
16 * @var string
17 */
18 const PACK_MODE = 'P';
19
20 /**
21 * @param string $format
22 * @param int[] $intArray
23 * @return string
24 */
25 public function intArrayToHex(string $format, array $intArray): string
26 {
27 if (empty($format)) {
28 throw new InvalidArgumentException('Format cannot be empty');
29 }
30
31 if (empty($intArray)) {
32 throw new InvalidArgumentException('Int array cannot be empty');
33 }
34
35 $formats = str_split($format);
36 if (count($formats) !== count($intArray)) {
37 throw new InvalidArgumentException('The number of characters in formats and integers in array must be equal');
38 }
39
40 if (preg_match('/[^1-8]/', $format)) {
41 throw new InvalidArgumentException('Invalid format');
42 }
43
44 $index = 0;
45 $result = '';
46 foreach ($formats as $format) {
47 // let try catch to re throw for index position
48 try {
49 $bytes = intval($format);
50 if (!is_int($bytes)) {
51 throw new InvalidArgumentException('Invalid format');
52 }
53
54 $result .= $this->intToHex($intArray[$index], $bytes);
55 } catch (InvalidArgumentException $ex) {
56 throw new InvalidArgumentException($ex->getMessage() . ' at index ' . $index);
57 }
58
59 $index++;
60 }
61
62 return $result;
63 }
64
65 public function intToHex(int $value, int $bytes = 8): string
66 {
67 if ($value < 0) {
68 throw new InvalidArgumentException('Invalid value');
69 }
70
71 if ($bytes < 1 || $bytes > 8) {
72 throw new InvalidArgumentException('Invalid number of bytes');
73 }
74
75 // convert bytes to int
76 $maxInt = (2 ** ($bytes * 8)) - 1;
77 if ($value > $maxInt) {
78 throw new InvalidArgumentException('Value is too large for the given number of bytes');
79 }
80
81 $pack = pack(self::PACK_MODE, $value);
82 return bin2hex(substr($pack, 0, $bytes));
83 }
84
85 /**
86 * @param string $format
87 * @param string $hex
88 *
89 * @throws InvalidArgumentException
90 * @return int[]
91 */
92 public function hexToIntArray(string $format, string $hex): array
93 {
94 if (empty($format)) {
95 throw new InvalidArgumentException('Format cannot be empty');
96 }
97
98 if (preg_match('/[^1-8]/', $format)) {
99 throw new InvalidArgumentException('Invalid format');
100 }
101
102 if (empty($hex)) {
103 throw new InvalidArgumentException('Hex string cannot be empty');
104 }
105
106 if (strlen($hex) % 2 !== 0) {
107 throw new InvalidArgumentException('Invalid hex string');
108 }
109
110 // check for invalid characters in hex
111 if (preg_match('/[^0-9a-fA-F]/', $hex)) {
112 throw new InvalidArgumentException('Invalid hex string');
113 }
114
115 $formats = str_split($format);
116 $index = 0;
117 $intArray = [];
118 foreach ($formats as $format) {
119 $bytes = intval($format);
120 $length = $bytes * 2;
121
122 if ($index + $length > strlen($hex)) {
123 throw new InvalidArgumentException('Hex string is short according to format');
124 }
125
126 $subHex = substr($hex, $index, $length);
127
128 $intArray[] = $this->hexToInt($subHex, $bytes);
129 $index += $length;
130 }
131
132 if ($index !== strlen($hex)) {
133 throw new InvalidArgumentException('Hex string is long according to format');
134 }
135
136 return $intArray;
137 }
138
139 public function hexToInt(string $hex, int $bytes = 8): int
140 {
141 if ($bytes < 1 || $bytes > 8) {
142 throw new InvalidArgumentException('Invalid number of bytes');
143 }
144
145 if (empty($hex)) {
146 throw new InvalidArgumentException('Hex string cannot be empty');
147 }
148
149 if (strlen($hex) / 2 > $bytes) {
150 throw new InvalidArgumentException('Hex string is longer than the given number of bytes');
151 }
152
153 if (strlen($hex) % 2 !== 0) {
154 throw new InvalidArgumentException('Invalid hex string');
155 }
156
157 // check for invalid characters in hex
158 if (preg_match('/[^0-9a-fA-F]/', $hex)) {
159 throw new InvalidArgumentException('Invalid hex string');
160 }
161
162 $binary = hex2bin($hex);
163 if ($bytes !== 8) {
164 $binary = str_pad($binary, 8, "\x00", STR_PAD_RIGHT);
165 }
166
167 return unpack(self::PACK_MODE, $binary)[1];
168 }
169 }
170