12 changed files with 403 additions and 314 deletions
-
25src/APIConnector.php
-
130src/APILengthCoDec.php
-
40src/Client.php
-
39src/Helpers/BinaryStringHelper.php
-
23src/Interfaces/StreamInterface.php
-
21src/SocketTrait.php
-
70src/Streams/ResourceStream.php
-
48src/Streams/StringStream.php
-
78tests/APIConnectorTest.php
-
4tests/APILengthCoDecTest.php
-
159tests/Streams/ResourceStreamTest.php
-
80tests/Streams/StringStreamTest.php
@ -1,60 +1,65 @@ |
|||||
<?php |
<?php |
||||
|
|
||||
namespace RouterOS\Helpers; |
namespace RouterOS\Helpers; |
||||
|
|
||||
/** |
/** |
||||
* class BinaryStringHelper |
* class BinaryStringHelper |
||||
* |
|
||||
* Strings and binary datas manipulations |
|
||||
|
* |
||||
|
* Strings and binary data manipulations |
||||
* |
* |
||||
* @package RouterOS\Helpers |
* @package RouterOS\Helpers |
||||
* @since 0.9 |
* @since 0.9 |
||||
*/ |
*/ |
||||
|
|
||||
class BinaryStringHelper |
class BinaryStringHelper |
||||
{ |
{ |
||||
/** |
/** |
||||
* Convert an integer value in a "Network Byte Ordered" binary string (most significant value first) |
* Convert an integer value in a "Network Byte Ordered" binary string (most significant value first) |
||||
* |
* |
||||
* Reads the integer, starting from the most signficant byte, one byte a time. |
|
||||
|
* Reads the integer, starting from the most significant byte, one byte a time. |
||||
* Once reach a non 0 byte, construct a binary string representing this values |
* Once reach a non 0 byte, construct a binary string representing this values |
||||
* ex : |
|
||||
|
* ex : |
||||
* 0xFF7 => chr(0x0F).chr(0xF7) |
* 0xFF7 => chr(0x0F).chr(0xF7) |
||||
* 0x12345678 => chr(0x12).chr(0x34).chr(0x56).chr(0x76) |
* 0x12345678 => chr(0x12).chr(0x34).chr(0x56).chr(0x76) |
||||
* Compatible with 8, 16, 32, 64 etc.. bits systems |
* Compatible with 8, 16, 32, 64 etc.. bits systems |
||||
* |
* |
||||
* @see https://en.wikipedia.org/wiki/Endianness |
* @see https://en.wikipedia.org/wiki/Endianness |
||||
* @param int $value the integer value to be converted |
|
||||
* @return string the binary string |
|
||||
*/ |
|
||||
public static function IntegerToNBOBinaryString(int $value) |
|
||||
|
* @param int $value the integer value to be converted |
||||
|
* @return string the binary string |
||||
|
*/ |
||||
|
public static function IntegerToNBOBinaryString(int $value): string |
||||
{ |
{ |
||||
// Initialize an empty string
|
// Initialize an empty string
|
||||
$buffer = ''; |
$buffer = ''; |
||||
|
|
||||
// Lets start from the most significant byte
|
// Lets start from the most significant byte
|
||||
for ($i=(PHP_INT_SIZE-1); $i>=0; $i--) { |
|
||||
|
for ($i = (PHP_INT_SIZE - 1); $i >= 0; $i--) { |
||||
// Prepare a mask to keep only the most significant byte of $value
|
// Prepare a mask to keep only the most significant byte of $value
|
||||
$mask = 0xFF << ($i*8); |
|
||||
|
$mask = 0xFF << ($i * 8); |
||||
|
|
||||
// If the most significant byte is not 0, the final string must contain it
|
// If the most significant byte is not 0, the final string must contain it
|
||||
// If we have already started to construct the string (i.e. there are more signficant digits)
|
// If we have already started to construct the string (i.e. there are more signficant digits)
|
||||
// we must set the byte, even if it is a 0.
|
// we must set the byte, even if it is a 0.
|
||||
// 0xFF00FF, for example, require to set the second byte byte with a 0 value
|
// 0xFF00FF, for example, require to set the second byte byte with a 0 value
|
||||
if (($value & $mask) || strlen($buffer)!=0) { |
|
||||
// Get the curent byte by shifting it to least significant position and add it to the string
|
|
||||
|
if (($value & $mask) || $buffer !== '') { |
||||
|
// Get the current byte by shifting it to least significant position and add it to the string
|
||||
// 0xFF12345678 => 0xFF
|
// 0xFF12345678 => 0xFF
|
||||
$byte = $value>>(8*$i); |
|
||||
|
$byte = $value >> (8 * $i); |
||||
$buffer .= chr($byte); |
$buffer .= chr($byte); |
||||
|
|
||||
// Set the most significant byte to 0 so we can restart the process being shure
|
// Set the most significant byte to 0 so we can restart the process being shure
|
||||
// that the value is left padded with 0
|
// that the value is left padded with 0
|
||||
// 0xFF12345678 => 0x12345678
|
// 0xFF12345678 => 0x12345678
|
||||
// -1 = 0xFFFFF.... (number of F depend of PHP_INT_SIZE )
|
// -1 = 0xFFFFF.... (number of F depend of PHP_INT_SIZE )
|
||||
$mask = -1 >> ((PHP_INT_SIZE-$i)*8); |
|
||||
|
$mask = -1 >> ((PHP_INT_SIZE - $i) * 8); |
||||
$value &= $mask; |
$value &= $mask; |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
// Special case, 0 will not fill the buffer, have to construct it manualy
|
// Special case, 0 will not fill the buffer, have to construct it manualy
|
||||
if (0==$value) { |
|
||||
|
if (0 === $value) { |
||||
$buffer = chr(0); |
$buffer = chr(0); |
||||
} |
} |
||||
|
|
||||
return $buffer; |
return $buffer; |
||||
} |
} |
||||
} |
|
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue