-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.php
393 lines (351 loc) · 7.58 KB
/
util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
class Uint
{
public $u8;
public $u4;
public $u5;
public $hex;
public $string;
public function __construct()
{
return $this;
}
protected function clean()
{
$this->u8 = $this->u4 = $this->u5 = $this->hex = $this->string = false;
}
public static function fromHex($hex)
{
$ret = new Uint();
$ret->hex = strtoupper($hex);
$ret->u8 = $ret->hexU8($hex);
$ret->u4 = $ret->hexU4($hex);
return $ret;
}
public static function fromUint8Array($u8)
{
$ret = new Uint();
$ret->u8 = $u8;
$ret->hex = $ret->u8Hex($u8);
return $ret;
}
public static function fromUint4Array($u4)
{
$ret = new Uint();
$ret->u4 = $u4;
$ret->hex = $ret->u4Hex($u4);
$ret->u8 = $ret->u4U8($u4);
return $ret;
}
public static function fromString($str)
{
$ret = new Uint();
$ret->u5 = $ret->stringU5($str);
$ret->string = $str;
$ret->u4 = $ret->u5U4($ret->u5);
$ret->u8 = $ret->u4U8($ret->u4);
return $ret;
}
public static function fromDec($dec)
{
$ret = new Uint();
$ret->dec = $dec;
$ret->hex = decToHex($dec);
$ret->u8 = $ret->hexU8($ret->hex);
return $ret;
}
public function toHexString()
{
if($this->hex)
return $this->hex;
else
return $this->u8Hex($this->u8);
}
public function toString()
{
if($this->string)
return $this->string;
if($this->u5)
return $this->u5String($this->u5);
if($this->u4)
return $this->u5String($this->u4U5($this->u4));
if($this->u8)
return $this->u5String($this->u4U5($this->u8U4($this->u8)));
return false;
}
public function toUint8()
{
return $this->u8;
}
public function toUint4()
{
if($this->u4)
return $this->u4;
if($this->u8)
return $this->u8U4($this->u8);
return false;
}
public function hexU8($hex)
{
if(strlen($hex) % 2 != 0)
$hex = '0' . $hex;
$arr = new SplFixedArray(strlen($hex) / 2);
for($i = 0; $i < strlen($hex); $i+=2)
{
$arr[$i/2] = base_convert($hex[$i] . $hex[$i+1], 16, 10);
}
return $arr;
}
public function hexU4($hex)
{
$arr = new SplFixedArray(strlen($hex));
for($i = 0; $i < strlen($hex); $i++)
{
$arr[$i] = base_convert($hex[$i], 16, 10);
}
return $arr;
}
public function u8Hex($bytes)
{
$hex = '';
foreach($bytes as $byte)
{
$aux = base_convert($byte, 10, 16);
if(strlen($aux) == 1)
$aux = '0' . $aux;
$hex .= $aux;
}
return strtoupper($hex);
}
public function u4Hex($bytes)
{
$hex = '';
foreach($bytes as $byte)
$hex .= base_convert($byte, 10, 16);
return strtoupper($hex);
}
public function u8U4($u8)
{
$u4 = new SplFixedArray(count($u8) * 2);
for($i = 0; $i < count($u8); $i++)
{
$u4[$i*2] = $u8[$i] / 16 | 0;
$u4[$i*2 + 1] = $u8[$i] % 16;
}
return $u4;
}
public function u4U8($u4)
{
$u8 = new SplFixedArray(count($u4) / 2);
for($i = 0; $i < count($u8); $i++)
$u8[$i] = $u4[$i*2] * 16 + $u4[$i*2 + 1];
return $u8;
}
public function u4U5($u4)
{
$u5 = new SplFixedArray(count($u4) / 5 * 4);
for($i = 1; $i <= count($u5); $i++)
{
$n = $i - 1;
$m = $i % 4;
$z = $n + (($i - $m) / 4);
$right = $u4[$z] << $m;
if((count($u5) - $i) % 4 == 0)
$left = $u4[$z - 1] << 4;
else
$left = $u4[$z + 1] >> (4 - $m);
$u5[$n] = ($left + $right) % 32;
}
return $u5;
}
public function u5U4($u5)
{
$u4 = new SplFixedArray(count($u5) / 4 * 5);
for($i = 1; $i <= count($u4); $i++)
{
$n = $i - 1;
$m = $i % 5;
$z = $n - (($i - $m) / 5);
if($i > 1)
$right = $u5[$z - 1] << (5 - $m);
else $right = 0;
$left = $u5[$z] >> $m;
$u4[$n] = ($left + $right) % 16;
}
return $u4;
}
public function stringU5($str)
{
$letters = '13456789abcdefghijkmnopqrstuwxyz';
$len = strlen($str);
$arr = str_split($str);
$u5 = new SplFixedArray($len);
for($i = 0; $i < $len; $i++)
$u5[$i] = strpos($letters, $arr[$i]);
return $u5;
}
public function u5String($u5)
{
$letters = str_split('13456789abcdefghijkmnopqrstuwxyz');
$str = "";
for($i = 0; $i < count($u5); $i++)
$str .= $letters[$u5[$i]];
return $str;
}
public function dec2hex($str, $bytes = null)
{
$dec = str_split($hex);
$sum = [];
$hex = [];
$i = $s = 0;
while(count($dec))
{
$s = 1 * array_shift($dec);
for($i = 0; $s || $i < count($sum); $i++)
{
$s += ($sum[$i] || 0) * 10;
$sum[$i] = $s % 16;
$s = ($s - $sum[$i]) / 16;
}
}
while(count($sum))
{
$hex[] = base_convert(array_shift($sum), 10, 16);
}
$hex = implode('', $hex);
if(strlen($hex) % 2 != 0)
$hex = "0" . $hex;
if($bytes > strlen($hex) / 2)
{
$diff = $bytes - strlen($hex) / 2;
for($i = 0; $i < $diff; $i++)
$hex = "00" . $hex;
}
return $hex;
}
public function reverse()
{
if($this->u8)
{
$len = count($this->u8);
for($i = 0; $i != $len - 1 - $i; $i++)
{
$aux = $this->u8[$i];
$this->u8[$i] = $this->u8[$len - 1 - $i];
$this->u8[$len - 1 - $i] = $aux;
}
}
return self::fromUint8Array($this->toUint8());
}
public static function expandSize($size, $uint)
{
if(get_class($uint) != 'Uint')
return false;
if(count($uint->toUint8()) < $size)
{
$u8 = $uint->toUint8();
$new = new SplFixedArray($size);
$diff = $size - count($u8);
for($i = 0; $i < $size; $i++)
{
if($i < $diff)
$new[$i] = 0;
else
$new[$i] = $u8[$i - $diff];
}
return self::fromUint8Array($new);
}
return $uint;
}
}
// from here > http://www.danvk.org/hex2dec.html
/**
* A function for converting hex <-> dec w/o loss of precision.
*
* The problem is that parseInt("0x12345...") isn't precise enough to convert
* 64-bit integers correctly.
*
* Internally, this uses arrays to encode decimal digits starting with the least
* significant:
* 8 = [8]
* 16 = [6, 1]
* 1024 = [4, 2, 0, 1]
*/
// Adds two arrays for the given base (10 or 16), returning the result.
// This turns out to be the only "primitive" operation we need.
function add($x, $y, $base)
{
$z = [];
$n = max(count($x), count($y));
$carry = 0;
$i = 0;
while ($i < $n || $carry)
{
$xi = $i < count($x) ? $x[$i] : 0;
$yi = $i < count($y) ? $y[$i] : 0;
$zi = $carry + $xi + $yi;
$z[] = $zi % $base;
$carry = floor($zi / $base);
$i++;
}
return $z;
}
function multiplyByNumber($num, $x, $base) {
if ($num < 0) return null;
if ($num == 0) return [];
$result = [];
$power = $x;
while (true)
{
if ($num & 1)
{
$result = add($result, $power, $base);
}
$num = $num >> 1;
if ($num === 0)
break;
$power = add($power, $power, $base);
}
return $result;
}
function parseToDigitsArray($str, $base) {
$digits = str_split($str);
$ary = [];
for ($i = count($digits) - 1; $i >= 0; $i--)
{
$n = intval($digits[$i], $base);
if (!is_numeric($n)) return null;
$ary[] = $n;
}
return $ary;
}
function convertBase($str, $fromBase, $toBase) {
$digits = parseToDigitsArray($str, $fromBase);
if ($digits === null) return null;
$outArray = [];
$power = [1];
for ($i = 0; $i < count($digits); $i++) {
// invariant: at this point, fromBase^i = power
if ($digits[$i])
{
$outArray = add($outArray, multiplyByNumber($digits[$i], $power, $toBase), $toBase);
}
$power = multiplyByNumber($fromBase, $power, $toBase);
}
$out = '';
for ($i = count($outArray) - 1; $i >= 0; $i--) {
$out .= base_convert($outArray[$i], $fromBase, $toBase);
}
return $out;
}
function decToHex($decStr) {
$hex = convertBase($decStr, 10, 16);
if(strlen($hex) % 2 != 0)
$hex = '0' . $hex;
return $hex ? $hex : null;
}
function hexToDec($hexStr) {
if (substr($hexStr, 0, 2) === '0x') $hexStr = substr($hexStr, 2);
$hexStr = strtolower($hexStr);
return convertBase($hexStr, 16, 10);
}