This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathu64.php
728 lines (589 loc) · 19.7 KB
/
u64.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
function SHR($x, $c) {
$nmaxBits = PHP_INT_SIZE * 8;
$c %= $nmaxBits;
return (int)$x >> $c & ~ (-1 << $nmaxBits - $c);
}
final class o_u64 {
function __construct($h, $l) {
$this->hi = $h; // >>> 0;
$this->lo = $l; // >>> 0;
}
function set(o_u64 $oWord) {
throw new Exception('No plz');
/* $this->lo = $oWord->lo;
$this->hi = $oWord->hi; */
}
function __add(o_u64 $oWord) {
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$o_h = $oWord->hi & 0xffffffff;
$o_l = $oWord->lo & 0xffffffff;
//var lowest, lowMid, highMid, highest; //four parts of the whole 64 bit number..
//need to add the respective parts from each number and the carry if on is present..
$lowest = (($this_l & 0XFFFF) + ($o_l & 0XFFFF))& 0xffffffff;
$lowMid = (SHR($this_l, 16) + SHR($o_l, 16) + SHR($lowest, 16)) & 0xffffffff;
$highMid = ($this_h & 0XFFFF) + ($o_h & 0XFFFF) + SHR($lowMid, 16) & 0xffffffff;
$highest = SHR($this_h, 16) + SHR($o_h, 16) + SHR($highMid, 16) ;
//now set the hgih and the low accordingly..
$this->lo = (($lowMid << 16) | ($lowest & 0XFFFF));
$this->hi = (($highest << 16) | ($highMid & 0XFFFF));
return $this; //for chaining..
}
function addOne() {
if ($this->lo === -1 || $this->lo === 0xFFFFFFFF) {
$this->lo = 0;
$this->hi++;
} else {
$this->lo++;
}
}
function plus(o_u64 $oWord) {
$c = new o_u64(0, 0);
// var lowest, lowMid, highMid, highest; //four parts of the whole 64 bit number..
//need to add the respective parts from each number and the carry if on is present..
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$o_h = $oWord->hi & 0xffffffff;
$o_l = $oWord->lo & 0xffffffff;
//var lowest, lowMid, highMid, highest; //four parts of the whole 64 bit number..
//need to add the respective parts from each number and the carry if on is present..
$lowest = (($this_l & 0XFFFF) + ($o_l & 0XFFFF))& 0xffffffff;
$lowMid = (SHR($this_l, 16) + SHR($o_l, 16) + SHR($lowest, 16)) & 0xffffffff;
$highMid = (($this_h & 0XFFFF) + ($o_h & 0XFFFF) + SHR($lowMid, 16)) & 0xffffffff;
$highest = (SHR($this_h, 16) + SHR($o_h, 16) + SHR($highMid, 16)) & 0xffffffff;
//now set the hgih and the low accordingly..
$c->lo = (($lowMid << 16) | ($lowest & 0XFFFF))& 0xffffffff;
$c->hi = ((($highest << 16) | ($highMid & 0XFFFF))) & 0xffffffff;
return $c; //for chaining..
}
function not() {
return new o_u64(~$this->hi, ~$this->lo);
}
function one() {
// throw new Exception('No plz');
return new o_u64(0x0, 0x1);
}
function zero() {
//throw new Exception('No plz');
return new o_u64(0x0, 0x0);
}
function neg() {
// throw new Exception('No plz');
return $this->not()->plus($this->one());
}
function minus(o_u64 $oWord) {
//throw new Exception('No plz');
return $this->plus($oWord->neg());
}
function isZero() {
//throw new Exception('No plz');
return ($this->lo === 0) && ($this->hi === 0);
}
#function isLong($obj) {
# return ($obj && $obj["__isLong__"]) === true;
#}
#function fromNumber(value) {
# if (isNaN(value) || !isFinite(value))
# return this.zero();
# var pow32 = (1 << 32);
# return new u64((value % pow32) | 0, (value / pow32) | 0);
#}
function multiply(o_u64 $multiplier) {
#throw new Exception('No plz');
//if ($this->isZero())
// return $this->zero();
#if (!isLong(multiplier))
# multiplier = fromNumber(multiplier);
// if ($multiplier->isZero())
// return $this->zero();
// Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
// We can skip products that would overflow.
$a48 = $this->hi >> 16 & 0xFFFF;
$a32 = $this->hi & 0xFFFF;
$a16 = $this->lo >> 16 & 0xFFFF;
$a00 = $this->lo & 0xFFFF;
$b48 = $multiplier->hi >> 16 & 0xFFFF;
$b32 = $multiplier->hi & 0xFFFF;
$b16 = $multiplier->lo >> 16 & 0xFFFF;
$b00 = $multiplier->lo & 0xFFFF;
$c48 = 0;
$c32 = 0;
$c16 = 0;
$c00 = 0;
$c00 += $a00 * $b00;
$c16 += $c00 >> 16;
$c00 &= 0xFFFF;
$c16 += $a16 * $b00;
$c32 += $c16 >> 16;
$c16 &= 0xFFFF;
$c16 += $a00 * $b16;
$c32 += $c16 >> 16;
$c16 &= 0xFFFF;
$c32 += $a32 * $b00;
$c48 += $c32 >> 16;
$c32 &= 0xFFFF;
$c32 += $a16 * $b16;
$c48 += $c32 >> 16;
$c32 &= 0xFFFF;
$c32 += $a00 * $b32;
$c48 += $c32 >> 16;
$c32 &= 0xFFFF;
$c48 += $a48 * $b00 + $a32 * $b16 + $a16 * $b32 + $a00 * $b48;
$c48 &= 0xFFFF;
return new o_u64((($c48 << 16) | $c32)& 0xffffffff, (($c16 << 16) | $c00 )& 0xffffffff);
}
function shiftLeft($bits) {
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$bits %= 64;
$c = new o_u64(0, 0);
if ($bits === 0) {
return clone $this;
} else if($bits == 32){
$c->lo = 0;
$c->hi = $this_l;
} else if ($bits > 32) {
$c->lo = 0;
$c->hi = $this_l << ($bits - 32);
} else {
$toMoveUp = SHR($this_l, 32 - $bits);
$c->lo = ($this_l << $bits)& 0xffffffff;
$c->hi =(($this_h << $bits) | $toMoveUp)& 0xffffffff;
}
return $c; //for chaining..
}
function setShiftLeft($bits) {
throw new Exception('No plz');
if ($bits === 0) {
return $this;
}
if ($bits > 63) {
$bits = $bits % 64;
}
if ($bits > 31) {
$this->hi = $this->lo << ($bits - 32);
$this->lo = 0;
} else {
$toMoveUp = $this->lo >> 32 - $bits;
$this->lo <<= $bits;
$this->hi = ($this->hi << $bits) | $toMoveUp;
}
return $this; //for chaining..
}
//Shifts this word by the given number of bits to the right (max 32)..
function shiftRight($bits) {
$bits %= 64;
if ($bits === 0)
return clone $this;
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$nmaxBits = PHP_INT_SIZE * 8;
$c = new o_u64(0, 0);
if ($bits >= 32) {//больше 32
$c->hi = -1;
$c->lo = ($this_h >> ($bits - 32)) | (-1 << (64 - $bits));
} else {//до 32
$c->hi = ($this_h >> $bits) | (-1 << (32 - $bits));
$c->lo = (SHR($this_l, $bits) | ($this_h << (32 - $bits))) &
~(-1 << ($nmaxBits - $bits));
}
return $c; //for chaining..
}
function shiftRightUnsigned($bits) {
$bits %= 64;
if ($bits === 0)
return clone $this;
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$c = new o_u64(0,0);
if ($bits == 32) {
$c->lo = $this_h ;
}
else
if ($bits > 32) {
$c->hi = 0;
$c->lo = ($this_h >> ($bits - 32));
} else {
$bitsOff32 = 32 - $bits;
$toMoveDown = $this_h << $bitsOff32 >> $bitsOff32;
$c->hi = $this_h >> $bits & 0xffffffff;
$c->lo = ($this_l >> $bits | ($toMoveDown << $bitsOff32) ) & 0xffffffff;
}
return $c; //for chaining..
}
//Rotates the bits of this word round to the left (max 32)..
function rotateLeft($bits) {
#throw new Exception('No plz');
if ($bits > 32) {
return $this->rotateRight(64 - $bits);
}
$c = new o_u64(0, 0);
if ($bits === 0) {
$c->lo = $this->lo >> 0;
$c->hi = $this->hi >> 0;
} else if ($bits === 32) { //just switch high and low over in this case..
$c->lo = $this->hi;
$c->hi = $this->lo;
} else {
$c->lo = (($this->lo << $bits) | ($this->hi >> (32 - $bits))) & 0xffffffff;
$c->hi = (($this->hi << $bits) | ($this->lo >> (32 - $bits))) & 0xffffffff;
}
return $c; //for chaining..
}
function setRotateLeft($bits) {
throw new Exception('No plz');
if ($bits > 32) {
return $this->setRotateRight(64 - $bits);
}
$newHigh = 0;
if ($bits === 0) {
return $this;
} else if ($bits === 32) { //just switch high and low over in this case..
$newHigh = $this->lo;
$this->lo = $this->hi;
$this->hi = $newHigh;
} else {
$newHigh = ($this->hi << $bits) | ($this->lo >> (32 - $bits));
$this->lo = ($this->lo << $bits) | ($this->hi >> (32 - $bits));
$this->hi = $newHigh;
}
return $this; //for chaining..
}
//Rotates the bits of this word round to the right (max 32)..
function rotateRight($bits) {
#throw new Exception('No plz');
if ($bits > 32) {
return $this->rotateLeft(64 - $bits);
}
$c = new o_u64(0, 0);
if ($bits === 0) {
$c->lo = $this->lo >> 0;
$c->hi = $this->hi >> 0;
} else if ($bits === 32) { //just switch high and low over in this case..
$c->lo = $this->hi;
$c->hi = $this->lo;
} else {
$c->lo = (($this->hi << (32 - $bits)) | ($this->lo >> $bits)) & 0xffffffff;
$c->hi = (($this->lo << (32 - $bits)) | ($this->hi >> $bits)) & 0xffffffff;
}
return $c; //for chaining..
}
function setFlip() {
#$newHigh;
$newHigh = $this->lo;
$this->lo = $this->hi;
$this->hi = $newHigh;
return $this;
}
function Flip() {
#$newHigh;
$new=clone $this;
$new->lo = $this->hi;
$new->hi = $this->lo;
return $new;
}
//Rotates the bits of this word round to the right (max 32)..
function setRotateRight($bits) {
throw new Exception('No plz');
if ($bits > 32) {
return $this->setRotateLeft(64 - $bits);
}
if ($bits === 0) {
return $this;
} else if ($bits === 32) { //just switch high and low over in this case..
#$newHigh;
$newHigh = $this->lo;
$this->lo = $this->hi;
$this->hi = $newHigh;
} else {
$newHigh = ($this->lo << (32 - $bits)) | ($this->hi >> $bits);
$this->lo = ($this->hi << (32 - $bits)) | ($this->lo >> $bits);
$this->hi = $newHigh;
}
return $this; //for chaining..
}
//Xors this word with the given other..
function __xor(o_u64 $oWord) {
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$o_h = $oWord->hi & 0xffffffff;
$o_l = $oWord->lo & 0xffffffff;
$c = new o_u64(0, 0);
$c->hi = $this_h ^ $o_h;
$c->lo = $this_l ^ $o_l;
return $c; //for chaining..
}
//Xors this word with the given other..
function setxorOne(o_u64 $oWord) {
$o_h = $oWord->hi & 0xffffffff;
$o_l = $oWord->lo & 0xffffffff;
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$this->hi = $this_h ^ $o_h;
$this->lo = $this_l ^ $o_l;
return $this; //for chaining..
}
//Ands this word with the given other..
function __and(o_u64 $oWord) {
$c = new o_u64(0, 0);
$c->hi = $this->hi & $oWord->hi;
$c->lo = $this->lo & $oWord->lo;
return $c; //for chaining..
}
function __or(o_u64 $oWord) {
$c = new o_u64(0, 0);
$c->hi = $this->hi | $oWord->hi;
$c->lo = $this->lo | $oWord->lo;
return $c; //for chaining..
}
//Creates a deep copy of this Word..
function __clone() {
return new o_u64($this->hi, $this->lo);
}
function setxor64() {
$a = func_get_args();
$i = func_num_args();
while ($i--) {
$this_h = $this->hi & 0xffffffff;
$this_l = $this->lo & 0xffffffff;
$el_h = $a[$i]->hi & 0xffffffff;
$el_l = $a[$i]->lo & 0xffffffff;
$this->hi = $this_h ^ $el_h;
$this->lo = $this_l ^ $el_l;
}
return $this;
}
function __toString() {
#return sprintf('u64 (hi:%x lo:%x)'
return sprintf("%08x %08x\n###\n%032b %032b"
, $this->hi & 0xffffffff, $this->lo & 0xffffffff
, $this->hi & 0xffffffff, $this->lo & 0xffffffff);
}
}
function o_u($h, $l) {
return new o_u64($h, $l);
}
function xor64() {
$a = func_get_args();
$h = $a[0]->hi;
$l = $a[0]->lo;
$i = count($a) - 1;
do {
$h ^= $a[$i]->hi;
$l ^= $a[$i]->lo;
$i--;
} while ($i > 0);
return new o_u64($h, $l);
}
function clone64Array(array $arr) {
$i = 0;
$len = count($arr);
$a = Array();
while ($i < $len) {
$a[$i] = $arr[$i];
$i++;
}
return $a;
}
//this shouldn't be a problem, but who knows in the future javascript might support 64bits
function t32($x) {
return ($x & 0xFFFFFFFF);
}
function rotl32($x, $c) {
return ((($x) << ($c)) | (($x) >> (32 - ($c)))) & (0xFFFFFFFF);
}
function rotr32($x, $c) {
return rotl32($x, (32 - ($c)));
}
function swap32($val) {
return (($val & 0xFF) << 24) |
(($val & 0xFF00) << 8) |
(($val >> 8) & 0xFF00) |
(($val >> 24) & 0xFF);
}
function swap32Array(array $a) {
//can't do this with map because of support for IE8 (Don't hate me plz).
$i = 0;
$len = count($a);
$r = Array();
while ($i < $len) {
$r[$i] = (swap32($a[$i]));
$i++;
}
return $r;
}
function xnd64($x, $y, $z) {
return new o_u64($x->hi ^ ((~$y->hi) & $z->hi), $x->lo ^ ((~$y->lo) & $z->lo));
}
/*
module.exports.load64 = function(x, i) {
var l = x[i] | (x[i + 1] << 8) | (x[i + 2] << 16) | (x[i + 3] << 24);
var h = x[i + 4] | (x[i + 5] << 8) | (x[i + 6] << 16) | (x[i + 7] << 24);
return new this.u64(h, l);
}
*/
function bufferInsert(&$buffer, $bufferOffset, $data, $len, $dataOffset = 0) {
$i = 0;
while ($i < $len) {
$buffer[$i + $bufferOffset] = $data[$i + $dataOffset];
$i++;
}
}
function bufferInsert64(&$buffer, $bufferOffset, $data, $len) {
$i = 0;
while ($i < $len) {
$buffer[$i + $bufferOffset] = clone $data[$i];
$i++;
}
}
/*
module.exports.buffer2Insert = function(buffer, bufferOffset, bufferOffset2, data, len, len2) {
while (len--) {
var j = len2;
while (j--) {
buffer[len + bufferOffset][j + bufferOffset2] = data[len][j];
}
}
}
*/
function bufferInsertBackwards(&$buffer, $bufferOffset, $data, $len) {
$i = 0;
while ($i < $len) {
$buffer[$i + $bufferOffset] = $data[$len - 1 - $i];
$i++;
}
}
function bufferSet(&$buffer, $bufferOffset, $value, $len) {
$i = 0;
while ($i < $len) {
$buffer[$i + $bufferOffset] = $value;
$i++;
}
}
function bufferXORInsert(&$buffer, $bufferOffset, $data, $dataOffset, $len) {
$i = 0;
while ($i < $len) {
$buffer[$i + $bufferOffset] ^= $data[$i + $dataOffset];
$i++;
}
}
function xORTable(&$d, $s1, $s2, $len) {
$i = 0;
while ($i < $len) {
$d[$i] = $s1[$i] ^ $s2[$i];
$i++;
}
}
function strReplace(&$buffer,$rm,$offset,$len){
$left=substr($buffer,0,$offset);
$right=substr($buffer,$offset+$len,strlen($buffer));
}
function bufferEncode64_str(&$buffer, $offset, $uint64) {
$ret="";
$ret.= chr($uint64->hi >> 24 & 0xFF);
$ret.= chr($uint64->hi >> 16 & 0xFF);
$ret.= chr($uint64->hi >> 8 & 0xFF);
$ret.= chr($uint64->hi & 0xFF);
$ret.= chr($uint64->lo >> 24 & 0xFF);
$ret.= chr($uint64->lo >> 16 & 0xFF);
$ret.= chr($uint64->lo >> 8 & 0xFF);
$ret.= chr($uint64->lo & 0xFF);
strReplace($buffer,$ret,$offset,8);
echo "$tm\n";
exit();
}
function bufferEncode64_str_(&$buffer, $offset, $uint64) {
$ret="";
$ret.= chr($uint64->hi >> 0 & 0xFF);
$ret.= chr($uint64->hi >> 8 & 0xFF);
$ret.= chr($uint64->hi >> 16 & 0xFF);
$ret.= chr($uint64->hi >>24 & 0xFF);
$ret.= chr($uint64->lo >> 0 & 0xFF);
$ret.= chr($uint64->lo >> 8 & 0xFF);
$ret.= chr($uint64->lo >> 16 & 0xFF);
$ret.= chr($uint64->lo >> 24 & 0xFF);
//$buffer=strReplace($buffer,$ret,$offset,8);
}
function bufferEncode64(&$buffer, $offset, $uint64) {
$buffer[$offset] = $uint64->hi >> 24 & 0xFF;
$buffer[$offset + 1] = $uint64->hi >> 16 & 0xFF;
$buffer[$offset + 2] = $uint64->hi >> 8 & 0xFF;
$buffer[$offset + 3] = $uint64->hi & 0xFF;
$buffer[$offset + 4] = $uint64->lo >> 24 & 0xFF;
$buffer[$offset + 5] = $uint64->lo >> 16 & 0xFF;
$buffer[$offset + 6] = $uint64->lo >> 8 & 0xFF;
$buffer[$offset + 7] = $uint64->lo & 0xFF;
}
function getBuffer64_B( $offset, $uint64) {
$buffer=array_fill(0,8,0);
$buffer[$offset] = $uint64->lo >> 0 & 0xFF;
$buffer[$offset + 1] = $uint64->lo >> 8 & 0xFF;
$buffer[$offset + 2] = $uint64->lo >> 16 & 0xFF;
$buffer[$offset + 3] = $uint64->lo>> 24 & 0xFF;
$buffer[$offset + 4] = $uint64->hi >> 0 & 0xFF;
$buffer[$offset + 5] = $uint64->hi >> 8 & 0xFF;
$buffer[$offset + 6] = $uint64->hi >> 16 & 0xFF;
$buffer[$offset + 7] = $uint64->hi >> 24 & 0xFF;
return $buffer;
}
function bufferEncode64_(&$buffer, $offset, $uint64) {
$buffer[$offset] = $uint64->hi >> 0 & 0xFF;
$buffer[$offset + 1] = $uint64->hi >> 8 & 0xFF;
$buffer[$offset + 2] = $uint64->hi >> 16 & 0xFF;
$buffer[$offset + 3] = $uint64->hi >>24 & 0xFF;
$buffer[$offset + 4] = $uint64->lo >> 0 & 0xFF;
$buffer[$offset + 5] = $uint64->lo >> 8 & 0xFF;
$buffer[$offset + 6] = $uint64->lo >> 16 & 0xFF;
$buffer[$offset + 7] = $uint64->lo >> 24 & 0xFF;
}
function b2int64_offset(&$b,$of){
return new o_u64(
($b[$of+0] << 24) | ($b[ $of+1] << 16) | ($b[ $of+ 2] << 8) | $b[ $of+3]
, ($b[ $of+4] << 24) | ($b[ $of+5] << 16) | ($b[$of+6] << 8) | $b[$of+ 7]);
}
function b2int64($b){
return new o_u64(
($b[0] << 24) | ($b[ 1] << 16) | ($b[ 2] << 8) | $b[ 3]
, ($b[ 4] << 24) | ($b[ 5] << 16) | ($b[6] << 8) | $b[ 7]);
}
function b2int64_B($b){
return new o_u64(
($b[ 4] << 24) | ($b[ 5] << 16) | ($b[6] << 8) | ($b[ 7])
, ($b[0] << 24) | ($b[ 1] << 16) | ($b[ 2] << 8) | ($b[ 3]) );
}
function b2int64_($b){
return new o_u64(
($b[0] << 0) | ($b[ 1] << 8) | ($b[ 2] <<16) | $b[ 3]<<24
, ($b[ 4] ) | ($b[ 5] << 8) | ($b[6] << 16) | $b[ 7]<<24);
}
function b32toint($x,$offset=0){
return ($x[$offset+0] |$x[$offset+1]<<8 |$x[$offset+2]<<16 |$x[$offset+3]<<24);
}
function bytes2Int64Buffer($b) {
if (!$b)
return [];
$len = count($b) ? (((count($b) - 1) >> 3) + 1) : 0;
$buffer = Array();
$j = 0;
while ($j < $len) {
$buffer[$j] = new o_u64(
($b[$j * 8] << 24) | ($b[$j * 8 + 1] << 16) | ($b[$j * 8 + 2] << 8) | $b[$j * 8 + 3]
, ($b[$j * 8 + 4] << 24) | ($b[$j * 8 + 5] << 16) | ($b[$j * 8 + 6] << 8) | $b[$j * 8 + 7]);
$j++;
}
return $buffer;
}
function int32Buffer2Bytes($b) {
$buffer = array_fill(0, count($b), 0);
$len = count($b);
$i = 0;
while ($i < $len) {
$buffer[$i * 4] = ($b[$i] & 0xFF000000) >> 24;
$buffer[$i * 4 + 1] = ($b[$i] & 0x00FF0000) >> 16;
$buffer[$i * 4 + 2] = ($b[$i] & 0x0000FF00) >> 8;
$buffer[$i * 4 + 3] = ($b[$i] & 0x000000FF);
$i++;
}
return $buffer;
}