-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinaryParser.php
executable file
·190 lines (171 loc) · 8.08 KB
/
BinaryParser.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
<?php
/*
* Binary Parser: serializes and unserializes binary data.
* Jonas Raoni Soares da Silva <http://raoni.org>
* https://github.com/jonasraoni/binary-parser
*/
#--[ Constants ]---------------------------------------
//as i didnt found these constants, i emulated them with some help from the manual XD
define( "NOT_A_NUMBER", acos(1.01) );
define( "POSITIVE_INFINITY", -log(0) );
define( "NEGATIVE_INFINITY", log(0) );
#--[ BinaryBuffer Class ]------------------------------------
class BinaryBuffer{
private
$bigEndian = false,
$buffer = array();
public $length = 0;
public function __construct( $bigEndian = false, $buffer = '' ){
$this->bigEndian = $bigEndian;
$this->setBuffer( $buffer );
}
private function shl( $a, $b ){
for( ; $b--; $a = ( ( $a %= 0x7fffffff + 1 ) & 0x40000000 ) == 0x40000000 ? $a * 2 : ( $a - 0x40000000 ) * 2 + 0x7fffffff + 1 );
return $a;
}
public function setBuffer( $data ){
if( gettype( $data ) == 'string' && strlen( $data ) ){
for( $this->buffer = array(), $i = strlen( $data ); $i; $this->buffer[] = ord( $data[--$i] ) );
if( $this->bigEndian )
$this->buffer = array_reverse( $this->buffer );
$this->length = count( $this->buffer );
}
}
public function getBuffer(){
return $this->buffer;
}
public function hasNeededBits( $neededBits ){
return count( $this->buffer ) >= -( -$neededBits >> 3 );
}
public function checkBuffer( $neededBits ){
if( !$this->hasNeededBits( $neededBits ) )
throw new Exception( __METHOD__ . ": missing bytes" );
}
public function byte( $i ){
return $this->buffer[ $i ];
}
public function setEndian( $bigEndian ){
if( $this->$bigEndian != $bigEndian )
$this->buffer = array_reverse( $this->buffer );
}
public function readBits( $start, $length ){
if( $start < 0 || $length <= 0 )
return 0;
$this->checkBuffer( $start + $length );
$offsetRight = $start % 8;
$curByte = count( $this->buffer ) - ( $start >> 3 ) - 1;
$lastByte = count( $this->buffer ) + ( -( $start + $length ) >> 3 );
$diff = $curByte - $lastByte;
$sum = ( ( $this->buffer[ $curByte ] >> $offsetRight ) & ( ( 1 << ( $diff ? 8 - $offsetRight : $length ) ) - 1 ) ) + ( $diff && ( $offsetLeft = ( $start + $length ) % 8 ) ? ( $this->buffer[ $lastByte++ ] & ( ( 1 << $offsetLeft ) - 1 ) ) << ( $diff-- << 3 ) - $offsetRight : 0 );
for( ; $diff; $sum += self::shl( $this->buffer[ $lastByte++ ], ( $diff-- << 3 ) - $offsetRight ) );
return $sum;
}
}
#--[ BinaryParser Class ]------------------------------
class BinaryParser{
public $bigEndian = false;
public function __construct( $bigEndian = false ){
$this->bigEndian = $bigEndian;
}
protected function decodeFloat( $data, $precisionBits, $exponentBits ){
$bias = pow( 2, $exponentBits - 1 ) - 1;
$buffer = new BinaryBuffer( $this->bigEndian, $data );
$buffer->checkBuffer( $precisionBits + $exponentBits + 1 );
$signal = $buffer->readBits( $precisionBits + $exponentBits, 1 );
$exponent = $buffer->readBits( $precisionBits, $exponentBits );
$significand = 0;
$divisor = 2;
$curByte = $buffer->length + ( -$precisionBits >> 3 ) - 1;
do {
for( $byteValue = $buffer->byte( ++$curByte ), $startBit = ( $startBit = $precisionBits % 8 ) ? $startBit : 8, $mask = 1 << $startBit; $mask >>= 1; $divisor *= 2 )
if( $byteValue & $mask )
$significand += 1 / $divisor;
} while( $precisionBits -= $startBit );
return $exponent == ( $bias << 1 ) + 1 ? ( $significand ? NOT_A_NUMBER : ( $signal ? NEGATIVE_INFINITY : POSITIVE_INFINITY ) ) : ( 1 + $signal * -2 ) * ( $exponent || $significand ? ( !$exponent ? pow( 2, -$bias + 1 ) * $significand : pow( 2, $exponent - $bias ) * ( 1 + $significand ) ) : 0 );
}
protected function decodeInt( $data, $bits, $signed ){
$buffer = new BinaryBuffer( $this->bigEndian, $data );
$x = $buffer->readBits( 0, $bits );
return $signed && $x >= ( $max = pow( 2, $bits ) ) / 2 ? $x - $max : $x;
}
protected function encodeFloat( $data, $precisionBits, $exponentBits ){
$bias = pow( 2, $exponentBits - 1 ) - 1;
$minExp = -$bias + 1;
$maxExp = $bias;
$minUnnormExp = $minExp - $precisionBits;
$status = is_nan( $n = (float)$data ) || $n == NEGATIVE_INFINITY || $n == POSITIVE_INFINITY ? $n : 0;
$exp = 0;
$len = 2 * $bias + 1 + $precisionBits + 3;
$bin = array_pad( array(), $len, 0 );
$signal = ( $n = $status !== 0 ? 0 : $n ) < 0;
$n = abs( $n );
$intPart = floor( $n );
$floatPart = $n - $intPart;
for( $i = $bias + 2; $intPart && $i; $bin[--$i] = ( $intPart % 2 ) & 1, $intPart = floor( $intPart / 2 ) );
for( $i = $bias + 1; $floatPart > 0 && $i; )
if( $bin[++$i] = ( ( $floatPart *= 2 ) >= 1 ) - 0 )
--$floatPart;
for( $i = -1; ++$i < $len && !$bin[$i]; );
$i = ( $exp = $bias + 1 - $i ) >= $minExp && $exp <= $maxExp ? $i + 1 : $bias + 1 - ( $exp = $minExp - 1 );
if( $bin[( $lastBit = $precisionBits - 1 + $i ) + 1] ){
if( !( $rounded = $bin[$lastBit] ) )
for( $j = $lastBit + 2; !$rounded && $j < $len; $rounded = $bin[$j++] );
for( $j = $lastBit + 1; $rounded && --$j >= 0; )
if( $bin[$j] = !$bin[$j] - 0 )
$rounded = 0;
}
for( $i = $i - 2 < 0 ? -1 : $i - 3; ++$i < $len && !$bin[$i]; );
if( ( $exp = $bias + 1 - $i ) >= $minExp && $exp <= $maxExp )
++$i;
else if( $exp < $minExp ){
if( $exp != $bias + 1 - $len && $exp < $minUnnormExp )
throw new Exception( __METHOD__ . ": underflow" );
$i = $bias + 1 - ( $exp = $minExp - 1 );
}
if( $intPart || $status !== 0 ){
throw new Exception( __METHOD__ . ": " . ( $intPart ? "overflow" : $status ) );
$exp = $maxExp + 1;
$i = $bias + 2;
if( $status == NEGATIVE_INFINITY )
$signal = 1;
else if( is_nan( $status ) )
$bin[$i] = 1;
}
for( $n = abs( $exp + $bias ), $j = $exponentBits + 1, $result = ""; --$j; $result = ( $n % 2 ) . $result, $n = $n >>= 1 );
$result = ( $signal ? "1" : "0" ) . $result . implode( "", array_slice( $bin, $i, $precisionBits ) );
for( $n = 0, $j = 0, $i = strlen( $result ), $r = array(); $i; $j = ( $j + 1 ) % 8 ){
$n += ( 1 << $j ) * $result[--$i];
if( $j == 7 ){
$r[] = chr( $n );
$n = 0;
}
}
$r[] = $n ? chr( $n ) : "";
return implode( "", ( $this->bigEndian ? array_reverse( $r ) : $r ) );
}
protected function encodeInt( $data, $bits, $signed ){
if( $data >= ( $max = pow( 2, $bits ) ) || $data < -( $max >> 1 ) )
throw new Exception( __METHOD__ . ": overflow" );
if( $data < 0 )
$data += $max;
for( $r = array(); $data; $r[] = chr( $data % 256 ), $data = floor( $data / 256 ) );
for( $bits = -( -$bits >> 3 ) - count( $r ); $bits--; $r[] = "\0" );
return implode( "", ( $this->bigEndian ? array_reverse( $r ) : $r ) );
}
public function toSmall ( $data ){ return $this->decodeInt( $data, 8, true ); }
public function fromSmall ( $data ){ return $this->encodeInt( $data, 8, true ); }
public function toByte ( $data ){ return $this->decodeInt( $data, 8, false ); }
public function fromByte ( $data ){ return $this->encodeInt( $data, 8, false ); }
public function toShort ( $data ){ return $this->decodeInt( $data, 16, true ); }
public function fromShort ( $data ){ return $this->encodeInt( $data, 16, true ); }
public function toWord ( $data ){ return $this->decodeInt( $data, 16, false ); }
public function fromWord ( $data ){ return $this->encodeInt( $data, 16, false ); }
public function toInt ( $data ){ return $this->decodeInt( $data, 32, true ); }
public function fromInt ( $data ){ return $this->encodeInt( $data, 32, true ); }
public function toDWord ( $data ){ return $this->decodeInt( $data, 32, false ); }
public function fromDWord ( $data ){ return $this->encodeInt( $data, 32, false ); }
public function toFloat ( $data ){ return $this->decodeFloat( $data, 23, 8 ); }
public function fromFloat ( $data ){ return $this->encodeFloat( $data, 23, 8 ); }
public function toDouble ( $data ){ return $this->decodeFloat( $data, 52, 11 ); }
public function fromDouble( $data ){ return $this->encodeFloat( $data, 52, 11 ); }
}