-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBuffer.php
More file actions
1187 lines (1091 loc) · 27 KB
/
Buffer.php
File metadata and controls
1187 lines (1091 loc) · 27 KB
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Nelexa\Buffer;
/**
* Read And Write Binary Data
*
* This is class defines methods for reading and writing values of all primitive types. Primitive values are translated
* to (or from) sequences of bytes according to the buffer's current byte order, which may be retrieved and modified
* via the order methods. The initial order of a byte buffer is always Buffer::BIG_ENDIAN.
*
* @author Ne-Lexa alexey@nelexa.ru
* @license MIT
*/
abstract class Buffer
{
const BIG_ENDIAN = 'BIG_ENDIAN';
const LITTLE_ENDIAN = 'LITTLE_ENDIAN';
/**
* @var int
*/
protected $position = 0;
/**
* @var int
*/
private $limit = 0;
/**
* @var bool Is little endian order
*/
private $orderLittleEndian = false;
/**
* @var boolean
*/
private $isReadOnly = false;
protected static function checkPhpSupport()
{
if (PHP_VERSION_ID < 70015 || PHP_VERSION_ID === 70100) {
throw new \RuntimeException('Operation not supported for PHP versions less than 7.0.15 and 7.1.1. Current version ' . PHP_VERSION);
}
}
/**
* Get buffer position
*
* @return int
*/
final public function position()
{
return $this->position;
}
/**
* Rewinds this buffer. The position is set to zero.
*
* Invoke this method before a sequence of channel-write or get
* operations, assuming that the limit has already been set
* appropriately.
*
* For example:
*
* $buf->writeString("Hello"); // Write remaining data
* $buf->rewind(); // Rewind buffer
* $buf->get(5); // get 5 bytes (Hello)
*
* @return Buffer
* @throws BufferException
*/
final public function rewind()
{
return $this->setPosition(0);
}
/**
* Set buffer position.
*
* @param int $position
* @return Buffer
* @throws BufferException
*/
public function setPosition($position)
{
$position = (int)$position;
if ($position > $this->limit) {
throw new BufferException('Set position ' . $position . ' invalid. Exceeded limit ' . $this->limit);
}
$this->position = $position;
return $this;
}
/**
* Flips this buffer. The limit is set to the current position and then
* the position is set to zero.
*
* After a sequence of channel-read or put operations, invoke
* this method to prepare for a sequence of channel-write or relative
* get operations.
*
* @return Buffer
*/
abstract public function flip();
/**
* Returns the number of elements between the current position and the
* limit.
*
* @return int The number of elements remaining in this buffer
*/
public function remaining()
{
return $this->limit - $this->position;
}
/**
* Tells whether there are any elements between the current position and
* the limit.
*
* @return boolean true if, and only if, there is at least one element remaining in this buffer
*/
public function hasRemaining()
{
return $this->position < $this->limit;
}
/**
* Modifies this buffer's byte order.
*
* @param string $order The new byte order, either Buffer::BIG_ENDIAN or Buffer::LITTLE_ENDIAN
* @return Buffer
* @see Buffer::BIG_ENDIAN
* @see Buffer::LITTLE_ENDIAN
*
*/
final public function setOrder($order)
{
$this->orderLittleEndian = $order === self::LITTLE_ENDIAN;
return $this;
}
/**
* Set read only buffer.
*
* @param boolean $isReadOnly
* @return Buffer
*/
public function setReadOnly($isReadOnly)
{
$this->isReadOnly = $isReadOnly;
return $this;
}
/**
* Skip 1 byte
*
* @return Buffer
* @throws BufferException
*/
public function skipByte()
{
return $this->skip(1);
}
/**
* Skip number bytes.
*
* @param int $n The number of bytes to be skipped. The value may be negative.
* @return Buffer
* @throws BufferException
*/
public function skip($n)
{
return $this->setPosition($this->position + $n);
}
/**
* Skip short (2 bytes)
*
* @return Buffer
* @throws BufferException
*/
public function skipShort()
{
return $this->skip(2);
}
/**
* Skip int (4 bytes)
*
* @return Buffer
* @throws BufferException
*/
public function skipInt()
{
return $this->skip(4);
}
/**
* Skip long (8 bytes)
*
* @return Buffer
* @throws BufferException
*/
public function skipLong()
{
return $this->skip(8);
}
/**
* Skip float (4 bytes)
*
* @return $this
* @throws BufferException
*/
public function skipFloat()
{
return $this->skip(4);
}
/**
* Skip double (8 bytes)
*
* @return $this
* @throws BufferException
*/
public function skipDouble()
{
return $this->skip(8);
}
/**
* Reads one input byte and returns true if that byte is nonzero,
* false if that byte is zero.
*
* @return bool the boolean value read.
* @throws BufferException
*/
public function getBoolean()
{
return (bool)$this->getUnsignedByte();
}
/**
* Reads one input byte, zero-extends
* it to type int, and returns
* the result, which is therefore in the range
* 0 through 255.
*
* @return int the unsigned 8-bit value read.
* @throws BufferException
*/
public function getUnsignedByte()
{
return unpack('C', $this->get(1))[1];
}
/**
* Relative get method.
* Reads the string at this buffer's current position, and then increments the position.
*
* @param int $length
* @return string The strings at the buffer's current position
* @throws BufferException
*/
abstract protected function get($length);
/**
* Reads and returns one input byte.
* The byte is treated as a signed value in
* the range -128 through 127, inclusive.
*
* @return int the 8-bit value read.
* @throws BufferException
*/
public function getByte()
{
return Cast::toByte($this->getUnsignedByte());
}
/**
* Reads two input bytes and returns
* a short value in the range -32768 through 32767.
*
* @return int the 16-bit value read.
* @throws BufferException
*/
public function getShort()
{
return Cast::toShort($this->getUnsignedShort());
}
/**
* Reads two input bytes and returns
* an int value in the range 0 through 65535.
*
* @return int the unsigned 16-bit value read.
* @throws BufferException
*/
public function getUnsignedShort()
{
return unpack($this->orderLittleEndian ? 'v' : 'n', $this->get(2))[1];
}
/**
* Reads four input bytes and returns an unsigned short value
* in the range -2147483648 through 2147483647.
*
* @return int the int value read.
* @throws BufferException
*/
public function getInt()
{
return Cast::toInt($this->getUnsignedInt());
}
/**
* Reads four input bytes and returns an unsigned int value
* in the range 0 through 4294967296.
*
* @return int the unsigned int value read.
* @throws BufferException
*/
public function getUnsignedInt()
{
return unpack($this->orderLittleEndian ? 'V' : 'N', $this->get(4))[1];
}
/**
* Reads eight input bytes and returns a long value
* in the range -9223372036854775808 through 9223372036854775807.
*
* @return string|int the long value read.
* @throws BufferException
*/
public function getLong()
{
$data = $this->get(8);
if (PHP_VERSION_ID >= 50603) {
return unpack($this->orderLittleEndian ? 'P' : 'J', $data)[1];
}
if ($this->orderLittleEndian) {
$unpack = unpack('Va/Vb', $data);
return $unpack['a'] + ($unpack['b'] << 32);
}
$unpack = unpack('Na/Nb', $data);
return ($unpack['a'] << 32) | $unpack['b'];
}
/**
* Reads four input bytes and returns a float value
*
* @return float the float value read.
* @throws BufferException
*/
public function getFloat()
{
self::checkPhpSupport();
return unpack($this->orderLittleEndian ? 'g' : 'G', $this->get(4))[1];
}
/**
* Reads four input bytes and returns a double value
*
* @return double the double value read.
* @throws BufferException
*/
public function getDouble()
{
self::checkPhpSupport();
return unpack($this->orderLittleEndian ? 'e' : 'E', $this->get(8))[1];
}
/**
* Reads $length bytes from an input stream.
*
* @param $length int
* @return int[]
* @throws BufferException
*/
public function getArrayBytes($length)
{
if ($length > 0) {
return array_values(
unpack('c*', $this->get($length))
);
}
return [];
}
/**
* Reads $length unsigned bytes from an input stream.
*
* @param $length int
* @return int[]
* @throws BufferException
*/
public function getArrayUnsignedBytes($length)
{
if ($length > 0) {
return array_values(
unpack('C*', $this->get($length))
);
}
return [];
}
/**
* Reads in a string that has been encoded using
* a modified UTF-8 format.
*
* First, two bytes are read and used to
* construct an unsigned 16-bit integer in
* exactly the manner of the Buffer::readUnsignedShort()
* method. This integer value is called the UTF length
* and specifies the number of additional bytes to be read.
*
* Analog java @see java.io.DataOutputStream#readUTF()
*
* @return string
* @throws BufferException
*/
public function getUTF()
{
$size = $this->getUnsignedShort();
if ($size > 0) {
return $this->getString($size);
}
return '';
}
/**
* Reads $length input bytes and returns a string value.
*
* @param $length int
* @return string
* @throws BufferException
*/
public function getString($length)
{
if ($length > 0) {
return $this->get($length);
}
return '';
}
/**
* Reads $length * 2 input bytes and returns a string value.
*
* @param $length int
* @return string
* @throws BufferException
* @deprecated
*/
public function getUTF16($length)
{
if ($length > 0) {
return implode('', array_map('chr', array_values(unpack('S*', $this->get($length << 1)))));
}
return '';
}
/**
* Insert boolean value
*
* @param $bool
* @return Buffer
* @throws BufferException
*/
public function insertBoolean($bool)
{
return $this->insert($this->writeBoolean($bool));
}
/**
* Insert Buffer or string.
*
* @param Buffer|string $buffer
* @return Buffer
* @throws BufferException
*/
abstract public function insert($buffer);
/**
* @param bool $bool
* @return string
* @throws BufferException
*/
protected function writeBoolean($bool)
{
if ($bool === null) {
throw new BufferException('null boolean');
}
return pack('c', $bool ? 1 : 0);
}
/**
* Insert byte (-128 >= byte <= 127)
*
* @param int|string $byte
* @return Buffer
* @throws BufferException
*/
public function insertByte($byte)
{
return $this->insert($this->writeByte($byte));
}
/**
* @param int|string $byte
* @return string
* @throws BufferException
*/
protected function writeByte($byte)
{
if ($byte === null) {
throw new BufferException('null byte');
}
return pack('c', $byte);
}
/**
* Insert short value (-32768 >= short <= 32767)
*
* @param int|string $v
* @return Buffer
* @throws BufferException
*/
public function insertShort($v)
{
return $this->insert($this->writeShort($v));
}
/**
* @param int|string $v
* @return string
* @throws BufferException
*/
protected function writeShort($v)
{
if ($v === null) {
throw new BufferException('null short');
}
return pack($this->orderLittleEndian ? 'v' : 'n', $v);
}
/**
* Insert integer value (-2147483648 >= int <= 2147483647)
*
* @param int|string $v
* @return Buffer
* @throws BufferException
*/
public function insertInt($v)
{
return $this->insert($this->writeInt($v));
}
/**
* @param int|string $v
* @return string
* @throws BufferException
*/
protected function writeInt($v)
{
if ($v === null) {
throw new BufferException('null int');
}
return pack($this->orderLittleEndian ? 'V' : 'N', $v);
}
/**
* Insert long value (-9223372036854775808 >= long <= 9223372036854775807)
*
* @param int|string $v
* @return Buffer
* @throws BufferException
*/
public function insertLong($v)
{
return $this->insert($this->writeLong($v));
}
/**
* @param int|string $v
* @return string
* @throws BufferException
*/
protected function writeLong($v)
{
if ($v === null) {
throw new BufferException('null long');
}
if (PHP_VERSION_ID >= 50603) {
return pack($this->orderLittleEndian ? 'P' : 'J', $v);
}
$left = 0xffffffff00000000;
$right = 0x00000000ffffffff;
if ($this->orderLittleEndian) {
$r = ($v & $left) >> 32;
$l = $v & $right;
return pack('VV', $l, $r);
}
$l = ($v & $left) >> 32;
$r = $v & $right;
return pack('NN', $l, $r);
}
/**
* Insert float value
*
* @param float $v
* @return Buffer
* @throws BufferException
*/
public function insertFloat($v)
{
return $this->insert($this->writeFloat($v));
}
/**
* @param float $v
* @return string
* @throws BufferException
*/
protected function writeFloat($v)
{
self::checkPhpSupport();
if ($v === null) {
throw new BufferException('null float');
}
return pack($this->orderLittleEndian ? 'g' : 'G', $v);
}
/**
* Insert double value
*
* @param double $v
* @return Buffer
* @throws BufferException
*/
public function insertDouble($v)
{
return $this->insert($this->writeDouble($v));
}
/**
* @param double $v
* @return string
* @throws BufferException
*/
protected function writeDouble($v)
{
self::checkPhpSupport();
if ($v === null) {
throw new BufferException('null double');
}
return pack($this->orderLittleEndian ? 'e' : 'E', $v);
}
/**
* Insert string
*
* @param string $string
* @return Buffer
* @throws BufferException
*/
public function insertString($string)
{
return $this->insert($this->writeString($string));
}
/**
* @param string $string
* @return string
*/
protected function writeString($string)
{
return $string;
}
/**
* Insert array bytes
*
* @param array $bytes
* @return Buffer
* @throws BufferException
*/
public function insertArrayBytes(array $bytes)
{
return $this->insert($this->writeArrayBytes($bytes));
}
/**
* @param array $bytes
* @return string
*/
protected function writeArrayBytes(array $bytes)
{
return call_user_func_array('pack', array_merge(['c*'], $bytes));
}
/**
* Writes a string to the underlying output stream using
* modified UTF-8 encoding in a machine-independent manner.
*
* @param string $string
* @return Buffer
* @throws BufferException
* @see Buffer::writeUTF()
*
*/
public function insertUTF($string)
{
return $this->insert($this->writeUTF($string));
}
/**
* Writes a string to the underlying output stream using
* modified UTF-8 encoding in a machine-independent manner.
*
* First, two bytes are written to the output stream as if by the
* Buffer::writeShort() method giving the number of bytes to
* follow. This value is the number of bytes actually written out,
* not the length of the string.
*
* Analog java @see java.io.DataOutputStream#writeUTF()
*
* @param string $str
* @return string
* @throws BufferException
*/
protected function writeUTF($str)
{
if ($str === null) {
throw new BufferException('$str is null');
}
$bytes = unpack('c*', $str);
$length = count($bytes);
if ($length > 65535) {
throw new BufferException('Encoded string too long: ' . $length . ' bytes');
}
array_unshift($bytes, 'c*');
return $this->writeShort($length) . call_user_func_array('pack', $bytes);
}
/**
* Insert UTF16 string
*
* @param string $string
* @return Buffer
* @throws BufferException
* @deprecated
*/
public function insertUTF16($string)
{
return $this->insert($this->writeUTF16($string));
}
/**
* @param string $string
* @return string
* @throws BufferException
* @deprecated
*/
protected function writeUTF16($string)
{
if ($string === null) {
throw new BufferException('$string is null');
}
$args = array_map('ord', str_split($string));
array_unshift($args, 'S*');
return call_user_func_array('pack', $args);
}
/**
* Put boolean value
*
* @param $bool
* @return Buffer
* @throws BufferException
*/
public function putBoolean($bool)
{
return $this->put($this->writeBoolean($bool));
}
/**
* Relative put method (optional operation).
*
* Writes the given string into this buffer at the current
* position, and then increments the position.
*
* @param Buffer|string $buffer
* @return Buffer
* @throws BufferException
*/
abstract public function put($buffer);
/**
* Put byte (-128 >= byte <= 127)
*
* @param int|string $byte
* @return Buffer
* @throws BufferException
*/
public function putByte($byte)
{
return $this->put($this->writeByte($byte));
}
/**
* Put short value (-32768 >= short <= 32767)
*
* @param int|string $v
* @return Buffer
* @throws BufferException
*/
public function putShort($v)
{
return $this->put($this->writeShort($v));
}
/**
* Put integer value (-2147483648 >= int <= 2147483647)
*
* @param int|string $v
* @return Buffer
* @throws BufferException
*/
public function putInt($v)
{
return $this->put($this->writeInt($v));
}
/**
* Put long value (-9223372036854775808 >= long <= 9223372036854775807)
*
* @param int|string $v
* @return Buffer
* @throws BufferException
*/
public function putLong($v)
{
return $this->put($this->writeLong($v));
}
/**
* Put float value
*
* @param float $v
* @return Buffer
* @throws BufferException
*/
public function putFloat($v)
{
return $this->put($this->writeFloat($v));
}
/**
* Put double value
*
* @param double $v
* @return Buffer
* @throws BufferException
*/
public function putDouble($v)
{
return $this->put($this->writeDouble($v));
}
/**
* Put string
*
* @param string $string
* @return Buffer
* @throws BufferException
*/
public function putString($string)
{
return $this->put($this->writeString($string));
}
/**
* Put array bytes
*
* @param array $bytes
* @return Buffer
* @throws BufferException
*/
public function putArrayBytes(array $bytes)
{
return $this->put($this->writeArrayBytes($bytes));
}
/**
* Put UTF string (Format - java DataOutputStream.writeUTF)
*
* @param string $str
* @return Buffer
* @throws BufferException
*/
public function putUTF($str)
{
return $this->put($this->writeUTF($str));
}
/**
* Put UTF16 string
*
* @param string $str
* @return Buffer
* @throws BufferException
* @deprecated
*/
public function putUTF16($str)
{
return $this->put($this->writeUTF16($str));
}
/**
* Replace by boolean value
*
* @param bool $bool
* @param int $length
* @return Buffer
* @throws BufferException
*/
public function replaceBoolean($bool, $length)
{
return $this->replace($this->writeBoolean($bool), $length);
}
/**
* Replace $length bytes in a string or Buffer.
*
* @param Buffer|string $buffer
* @param int $length remove length bytes
* @return Buffer
* @throws BufferException
*/
abstract public function replace($buffer, $length);
/**
* Replace by byte (-128 >= byte <= 127)
*
* @param int|string $byte
* @param int $length
* @return Buffer
* @throws BufferException
*/
public function replaceByte($byte, $length)
{
return $this->replace($this->writeByte($byte), $length);
}
/**
* Replace short value (-32768 >= short <= 32767)
*
* @param int|string $v
* @param int $length
* @return Buffer
* @throws BufferException
*/
public function replaceShort($v, $length)
{
return $this->replace($this->writeShort($v), $length);
}
/**
* Replace integer value (-2147483648 >= int <= 2147483647)
*
* @param int|string $v
* @param int $length
* @return Buffer
* @throws BufferException
*/
public function replaceInt($v, $length)
{
return $this->replace($this->writeInt($v), $length);
}
/**
* Replace long value (-9223372036854775808 >= long <= 9223372036854775807)
*
* @param int|string $v
* @param int $length
* @return Buffer
* @throws BufferException
*/
public function replaceLong($v, $length)
{
return $this->replace($this->writeLong($v), $length);
}
/**
* Replace float value