mirror of
https://github.com/danog/PHPStruct.git
synced 2024-12-02 09:17:53 +01:00
Applied fixes from StyleCI
This commit is contained in:
parent
7fa51c02b2
commit
908634c614
@ -12,20 +12,20 @@ require 'lib/danog/PHP/Struct.php';
|
|||||||
3.03424, 'df', 'asdfghjkl', 1283912
|
3.03424, 'df', 'asdfghjkl', 1283912
|
||||||
|
|
||||||
);die;*/
|
);die;*/
|
||||||
var_dump(\danog\PHP\Struct::calcsize('>l'));die;
|
var_dump(\danog\PHP\Struct::calcsize('>l')); die;
|
||||||
|
|
||||||
// Dynamic usage with format definition on istantiation
|
// Dynamic usage with format definition on istantiation
|
||||||
$struct = new \danog\PHP\StructClass('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP');
|
$struct = new \danog\PHP\StructClass('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP');
|
||||||
var_dump($struct->unpack($struct->pack('n', 'v', -127, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, -888888888888,888888888888, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
var_dump($struct->unpack($struct->pack('n', 'v', -127, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, -888888888888, 888888888888, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
||||||
|
|
||||||
// Dynamic usage with format definition on function call
|
// Dynamic usage with format definition on function call
|
||||||
$struct = new \danog\PHP\StructClass();
|
$struct = new \danog\PHP\StructClass();
|
||||||
var_dump($struct->unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', $struct->pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, -888888888888,888888888888, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
var_dump($struct->unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', $struct->pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, -888888888888, 888888888888, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
||||||
|
|
||||||
// Static usage
|
// Static usage
|
||||||
var_dump(\danog\PHP\Struct::unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', \danog\PHP\Struct::pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, -888888888888, 888888888888, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
var_dump(\danog\PHP\Struct::unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', \danog\PHP\Struct::pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP', 'n', 'v', -127, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, -888888888888, 888888888888, 34434, 344434, 2.2343, 3.03424, 'df', 'asdfghjkl', 1283912)));
|
||||||
|
|
||||||
// S'more examples
|
// S'more examples
|
||||||
var_dump(\danog\PHP\Struct::calcsize('f'));
|
var_dump(\danog\PHP\Struct::calcsize('f'));
|
||||||
var_dump(bin2hex(pack("Q", 999998999999999)));
|
var_dump(bin2hex(pack('Q', 999998999999999)));
|
||||||
var_dump(bin2hex(\danog\PHP\Struct::pack('Q', 999998999999999)));
|
var_dump(bin2hex(\danog\PHP\Struct::pack('Q', 999998999999999)));
|
||||||
|
@ -28,6 +28,7 @@ class Struct
|
|||||||
public static function pack($format, ...$data)
|
public static function pack($format, ...$data)
|
||||||
{
|
{
|
||||||
$struct = new \danog\PHP\StructClass($format);
|
$struct = new \danog\PHP\StructClass($format);
|
||||||
|
|
||||||
return $struct->pack(...$data);
|
return $struct->pack(...$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +45,7 @@ class Struct
|
|||||||
public static function unpack($format, $data)
|
public static function unpack($format, $data)
|
||||||
{
|
{
|
||||||
$struct = new \danog\PHP\StructClass($format);
|
$struct = new \danog\PHP\StructClass($format);
|
||||||
|
|
||||||
return $struct->unpack($data);
|
return $struct->unpack($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,6 +62,7 @@ class Struct
|
|||||||
public static function calcsize($format)
|
public static function calcsize($format)
|
||||||
{
|
{
|
||||||
$struct = new \danog\PHP\StructClass($format);
|
$struct = new \danog\PHP\StructClass($format);
|
||||||
|
|
||||||
return $struct->size;
|
return $struct->size;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,22 +13,26 @@ namespace danog\PHP;
|
|||||||
* @license MIT license
|
* @license MIT license
|
||||||
*/
|
*/
|
||||||
// Struct class (for dynamic access)
|
// Struct class (for dynamic access)
|
||||||
class StructClass {
|
class StructClass
|
||||||
|
{
|
||||||
public $struct = null; // Will store an instance of the StructTools class
|
public $struct = null; // Will store an instance of the StructTools class
|
||||||
public $format = null; // Will contain the format
|
public $format = null; // Will contain the format
|
||||||
public $size = null; // Will contain the size
|
public $size = null; // Will contain the size
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* Stores instance of the StructTools class and optional format/size
|
* Stores instance of the StructTools class and optional format/size
|
||||||
*/
|
*/
|
||||||
public function __construct($format = null) {
|
public function __construct($format = null)
|
||||||
|
{
|
||||||
$this->struct = new \danog\PHP\StructTools();
|
$this->struct = new \danog\PHP\StructTools();
|
||||||
if($format !== null) {
|
if ($format !== null) {
|
||||||
$this->format = $format;
|
$this->format = $format;
|
||||||
$this->size = $this->struct->calcsize($format);
|
$this->size = $this->struct->calcsize($format);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pack.
|
* pack.
|
||||||
*
|
*
|
||||||
@ -40,9 +44,12 @@ class StructClass {
|
|||||||
*/
|
*/
|
||||||
public function pack(...$data)
|
public function pack(...$data)
|
||||||
{
|
{
|
||||||
if($this->format === null) {
|
if ($this->format === null) {
|
||||||
$format = array_shift($data);
|
$format = array_shift($data);
|
||||||
} else $format = $this->format;
|
} else {
|
||||||
|
$format = $this->format;
|
||||||
|
}
|
||||||
|
|
||||||
return $this->struct->pack($format, ...$data);
|
return $this->struct->pack($format, ...$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,12 +65,13 @@ class StructClass {
|
|||||||
*/
|
*/
|
||||||
public function unpack($format_maybe_data, $data = null)
|
public function unpack($format_maybe_data, $data = null)
|
||||||
{
|
{
|
||||||
if($this->format === null) {
|
if ($this->format === null) {
|
||||||
$format = $format_maybe_data;
|
$format = $format_maybe_data;
|
||||||
} else {
|
} else {
|
||||||
$format = $this->format;
|
$format = $this->format;
|
||||||
$data = $format_maybe_data;
|
$data = $format_maybe_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->struct->unpack($format, $data);
|
return $this->struct->unpack($format, $data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,4 +89,4 @@ class StructClass {
|
|||||||
{
|
{
|
||||||
return ($this->format !== null && $this->size !== null) ? $this->size : $this->struct->calcsize($format);
|
return ($this->format !== null && $this->size !== null) ? $this->size : $this->struct->calcsize($format);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace danog\PHP;
|
namespace danog\PHP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PHPStruct
|
* PHPStruct
|
||||||
* PHP implementation of Python's struct module.
|
* PHP implementation of Python's struct module.
|
||||||
|
@ -192,7 +192,7 @@ class StructTools
|
|||||||
'SIZE' => $this->SIZE,
|
'SIZE' => $this->SIZE,
|
||||||
'FORMATS' => $this->FORMATS,
|
'FORMATS' => $this->FORMATS,
|
||||||
'TYPE' => $this->TYPE,
|
'TYPE' => $this->TYPE,
|
||||||
'MODIFIER' => '<'
|
'MODIFIER' => '<',
|
||||||
],
|
],
|
||||||
'>' => [
|
'>' => [
|
||||||
'BIG_ENDIAN' => true,
|
'BIG_ENDIAN' => true,
|
||||||
@ -200,7 +200,7 @@ class StructTools
|
|||||||
'SIZE' => $this->SIZE,
|
'SIZE' => $this->SIZE,
|
||||||
'FORMATS' => $this->FORMATS,
|
'FORMATS' => $this->FORMATS,
|
||||||
'TYPE' => $this->TYPE,
|
'TYPE' => $this->TYPE,
|
||||||
'MODIFIER' => '>'
|
'MODIFIER' => '>',
|
||||||
],
|
],
|
||||||
'!' => [
|
'!' => [
|
||||||
'BIG_ENDIAN' => true,
|
'BIG_ENDIAN' => true,
|
||||||
@ -208,7 +208,7 @@ class StructTools
|
|||||||
'SIZE' => $this->SIZE,
|
'SIZE' => $this->SIZE,
|
||||||
'FORMATS' => $this->FORMATS,
|
'FORMATS' => $this->FORMATS,
|
||||||
'TYPE' => $this->TYPE,
|
'TYPE' => $this->TYPE,
|
||||||
'MODIFIER' => '!'
|
'MODIFIER' => '!',
|
||||||
],
|
],
|
||||||
'=' => [
|
'=' => [
|
||||||
'BIG_ENDIAN' => $this->BIG_ENDIAN,
|
'BIG_ENDIAN' => $this->BIG_ENDIAN,
|
||||||
@ -216,7 +216,7 @@ class StructTools
|
|||||||
'SIZE' => $this->SIZE,
|
'SIZE' => $this->SIZE,
|
||||||
'FORMATS' => $this->FORMATS,
|
'FORMATS' => $this->FORMATS,
|
||||||
'TYPE' => $this->TYPE,
|
'TYPE' => $this->TYPE,
|
||||||
'MODIFIER' => '='
|
'MODIFIER' => '=',
|
||||||
],
|
],
|
||||||
'@' => [
|
'@' => [
|
||||||
'BIG_ENDIAN' => $this->BIG_ENDIAN,
|
'BIG_ENDIAN' => $this->BIG_ENDIAN,
|
||||||
@ -224,10 +224,9 @@ class StructTools
|
|||||||
'SIZE' => $this->NATIVE_SIZE,
|
'SIZE' => $this->NATIVE_SIZE,
|
||||||
'FORMATS' => $this->NATIVE_FORMATS,
|
'FORMATS' => $this->NATIVE_FORMATS,
|
||||||
'TYPE' => $this->NATIVE_TYPE,
|
'TYPE' => $this->NATIVE_TYPE,
|
||||||
'MODIFIER' => '@'
|
'MODIFIER' => '@',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -294,7 +293,7 @@ class StructTools
|
|||||||
case 'p':
|
case 'p':
|
||||||
$curresult = pack('c', ($command['count'] - 1 > 255) ? 255 : $command['count'] - 1).pack('a'.($command['count'] - 1), $data[$command['datakey']]);
|
$curresult = pack('c', ($command['count'] - 1 > 255) ? 255 : $command['count'] - 1).pack('a'.($command['count'] - 1), $data[$command['datakey']]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'q':
|
case 'q':
|
||||||
case 'Q':
|
case 'Q':
|
||||||
case 'l':
|
case 'l':
|
||||||
@ -307,7 +306,7 @@ class StructTools
|
|||||||
case 'C':
|
case 'C':
|
||||||
$curresult = $this->num_pack($data[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));
|
$curresult = $this->num_pack($data[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '?':
|
case '?':
|
||||||
$curresult = pack('c'.$command['count'], $data[$command['datakey']]); // Pack current char
|
$curresult = pack('c'.$command['count'], $data[$command['datakey']]); // Pack current char
|
||||||
break;
|
break;
|
||||||
@ -315,9 +314,9 @@ class StructTools
|
|||||||
$curresult = pack($command['phpformat'].$command['count'], $data[$command['datakey']]); // Pack current char
|
$curresult = pack($command['phpformat'].$command['count'], $data[$command['datakey']]); // Pack current char
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (strlen($curresult) != $command['modifiers']['SIZE'] * $command['count']){
|
if (strlen($curresult) != $command['modifiers']['SIZE'] * $command['count']) {
|
||||||
trigger_error('Size of packed data '.strlen($curresult)." isn't equal to expected size ".$command['modifiers']['SIZE'] * $command['count'].'.');
|
trigger_error('Size of packed data '.strlen($curresult)." isn't equal to expected size ".$command['modifiers']['SIZE'] * $command['count'].'.');
|
||||||
}
|
}
|
||||||
} catch (StructException $e) {
|
} catch (StructException $e) {
|
||||||
throw new StructException('An error occurred while packing '.$data[$command['datakey']].' at offset '.$command['datakey'].' ('.$e->getMessage().').');
|
throw new StructException('An error occurred while packing '.$data[$command['datakey']].' at offset '.$command['datakey'].' ('.$e->getMessage().').');
|
||||||
}
|
}
|
||||||
@ -393,7 +392,7 @@ class StructTools
|
|||||||
$result[$arraycount] = true;
|
$result[$arraycount] = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'q':
|
case 'q':
|
||||||
case 'Q':
|
case 'Q':
|
||||||
case 'l':
|
case 'l':
|
||||||
@ -406,7 +405,7 @@ class StructTools
|
|||||||
case 'C':
|
case 'C':
|
||||||
$result[$arraycount] = $this->num_unpack($dataarray[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));
|
$result[$arraycount] = $this->num_unpack($dataarray[$command['datakey']], $command['modifiers']['SIZE'], ctype_upper($command['phpformat']));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$result[$arraycount] = implode('', unpack($command['phpformat'].$command['count'], $dataarray[$command['datakey']])); // Unpack current char
|
$result[$arraycount] = implode('', unpack($command['phpformat'].$command['count'], $dataarray[$command['datakey']])); // Unpack current char
|
||||||
break;
|
break;
|
||||||
@ -464,7 +463,9 @@ class StructTools
|
|||||||
$size = 0;
|
$size = 0;
|
||||||
$modifier = $this->MODIFIERS['@'];
|
$modifier = $this->MODIFIERS['@'];
|
||||||
$count = null;
|
$count = null;
|
||||||
if($format == '') return 0;
|
if ($format == '') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
foreach (str_split($format) as $offset => $currentformatchar) {
|
foreach (str_split($format) as $offset => $currentformatchar) {
|
||||||
if (isset($this->MODIFIERS[$currentformatchar])) {
|
if (isset($this->MODIFIERS[$currentformatchar])) {
|
||||||
$modifier = $this->MODIFIERS[$currentformatchar]; // Set the modifiers for the current format char
|
$modifier = $this->MODIFIERS[$currentformatchar]; // Set the modifiers for the current format char
|
||||||
@ -480,6 +481,7 @@ class StructTools
|
|||||||
throw new StructException('Unkown format or modifier supplied ('.$currentformatchar.' at offset '.$offset.').');
|
throw new StructException('Unkown format or modifier supplied ('.$currentformatchar.' at offset '.$offset.').');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $size;
|
return $size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,7 +559,8 @@ class StructTools
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
/**
|
||||||
* binadd.
|
* binadd.
|
||||||
*
|
*
|
||||||
* Convert a binary string to an array based on the given format string
|
* Convert a binary string to an array based on the given format string
|
||||||
@ -606,9 +609,11 @@ class StructTools
|
|||||||
throw new StructException('Unkown format or modifier supplied ('.$currentformatchar.' at offset '.$offset.').');
|
throw new StructException('Unkown format or modifier supplied ('.$currentformatchar.' at offset '.$offset.').');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dataarray;
|
return $dataarray;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
/**
|
||||||
* pdaformt.
|
* pdaformt.
|
||||||
*
|
*
|
||||||
* Pad format string with x format where needed
|
* Pad format string with x format where needed
|
||||||
@ -617,7 +622,8 @@ class StructTools
|
|||||||
*
|
*
|
||||||
* @return Padded format string
|
* @return Padded format string
|
||||||
**/
|
**/
|
||||||
public function padformat($format) {
|
public function padformat($format)
|
||||||
|
{
|
||||||
$modifier = $this->MODIFIERS['@'];
|
$modifier = $this->MODIFIERS['@'];
|
||||||
$result = null; // Result gormat string
|
$result = null; // Result gormat string
|
||||||
$count = null;
|
$count = null;
|
||||||
@ -638,8 +644,8 @@ class StructTools
|
|||||||
$totallength += $modifier['SIZE'][$currentformatchar] * $count;
|
$totallength += $modifier['SIZE'][$currentformatchar] * $count;
|
||||||
} else {
|
} else {
|
||||||
for ($x = 0; $x < $count; $x++) {
|
for ($x = 0; $x < $count; $x++) {
|
||||||
if($modifier['MODIFIER'] == '@'){
|
if ($modifier['MODIFIER'] == '@') {
|
||||||
$result .= str_pad("", $this->posmod(-$totallength, $modifier['SIZE'][$currentformatchar]), "x");
|
$result .= str_pad('', $this->posmod(-$totallength, $modifier['SIZE'][$currentformatchar]), 'x');
|
||||||
|
|
||||||
$totallength += $this->posmod(-$totallength, $modifier['SIZE'][$currentformatchar]) + $modifier['SIZE'][$currentformatchar];
|
$totallength += $this->posmod(-$totallength, $modifier['SIZE'][$currentformatchar]) + $modifier['SIZE'][$currentformatchar];
|
||||||
}
|
}
|
||||||
@ -666,42 +672,56 @@ class StructTools
|
|||||||
*
|
*
|
||||||
* @return binary version of the given number
|
* @return binary version of the given number
|
||||||
**/
|
**/
|
||||||
public function decbin($number, $length) {
|
public function decbin($number, $length)
|
||||||
|
{
|
||||||
$concat = '';
|
$concat = '';
|
||||||
if($number < 0){
|
if ($number < 0) {
|
||||||
$negative = true;
|
$negative = true;
|
||||||
$number = -$number;
|
$number = -$number;
|
||||||
} else $negative = false;
|
} else {
|
||||||
|
$negative = false;
|
||||||
|
}
|
||||||
while ($number > 0) {
|
while ($number > 0) {
|
||||||
$concat = ((string)$number % 2) . $concat;
|
$concat = ((string) $number % 2).$concat;
|
||||||
$number = floor($number / 2);
|
$number = floor($number / 2);
|
||||||
}
|
}
|
||||||
$concat = str_pad($concat, $length, "0", STR_PAD_LEFT);
|
$concat = str_pad($concat, $length, '0', STR_PAD_LEFT);
|
||||||
if($negative) $concat = $this->binadd($this->stringnot($concat), '1');
|
if ($negative) {
|
||||||
if(strlen($concat) > $length) trigger_error("Converted binary number is too long (".strlen($concat)." > ".$length.").");
|
$concat = $this->binadd($this->stringnot($concat), '1');
|
||||||
|
}
|
||||||
|
if (strlen($concat) > $length) {
|
||||||
|
trigger_error('Converted binary number is too long ('.strlen($concat).' > '.$length.').');
|
||||||
|
}
|
||||||
|
|
||||||
return $concat;
|
return $concat;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* bindec.
|
* bindec.
|
||||||
*
|
*
|
||||||
* Converts a binary number to a decimal.
|
* Converts a binary number to a decimal.
|
||||||
*
|
*
|
||||||
* @param $binary binary number to turn into decimal
|
* @param $binary binary number to turn into decimal
|
||||||
* @param $unsigned if set to false will interpret binary string as signed
|
* @param $unsigned if set to false will interpret binary string as signed
|
||||||
*
|
*
|
||||||
* @return deciaml version of the given number
|
* @return deciaml version of the given number
|
||||||
**/
|
**/
|
||||||
public function bindec($binary, $unsigned = true) {
|
public function bindec($binary, $unsigned = true)
|
||||||
|
{
|
||||||
$decimal = 0;
|
$decimal = 0;
|
||||||
if(!$unsigned && $binary[0] == "1") {
|
if (!$unsigned && $binary[0] == '1') {
|
||||||
$binary = $this->binadd($this->stringnot($binary), '1');
|
$binary = $this->binadd($this->stringnot($binary), '1');
|
||||||
$negative = -1;
|
$negative = -1;
|
||||||
} else $negative = 1;
|
} else {
|
||||||
|
$negative = 1;
|
||||||
|
}
|
||||||
foreach (str_split(strrev($binary)) as $n => $bit) {
|
foreach (str_split(strrev($binary)) as $n => $bit) {
|
||||||
$decimal += (pow(2, $n) * $bit);
|
$decimal += (pow(2, $n) * $bit);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $decimal * $negative;
|
return $decimal * $negative;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* stringnot.
|
* stringnot.
|
||||||
*
|
*
|
||||||
@ -711,19 +731,22 @@ class StructTools
|
|||||||
*
|
*
|
||||||
* @return xored string
|
* @return xored string
|
||||||
**/
|
**/
|
||||||
public function stringnot($string) {
|
public function stringnot($string)
|
||||||
|
{
|
||||||
foreach (str_split($string) as $key => $char) {
|
foreach (str_split($string) as $key => $char) {
|
||||||
if($char == "0") {
|
if ($char == '0') {
|
||||||
$string[$key] = "1";
|
$string[$key] = '1';
|
||||||
} else if($char == "1") {
|
} elseif ($char == '1') {
|
||||||
$string[$key] = "0";
|
$string[$key] = '0';
|
||||||
} else {
|
} else {
|
||||||
trigger_error("Found unrecognized char ".$char." at string offset " . $key);
|
trigger_error('Found unrecognized char '.$char.' at string offset '.$key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
/**
|
||||||
* binadd.
|
* binadd.
|
||||||
*
|
*
|
||||||
* Add two binary numbers
|
* Add two binary numbers
|
||||||
@ -733,30 +756,35 @@ class StructTools
|
|||||||
*
|
*
|
||||||
* @return sum of the two numbers
|
* @return sum of the two numbers
|
||||||
**/
|
**/
|
||||||
public function binadd($x,$y){
|
public function binadd($x, $y)
|
||||||
|
{
|
||||||
$maxlen = max(strlen($x), strlen($y));
|
$maxlen = max(strlen($x), strlen($y));
|
||||||
|
|
||||||
#Normalize lengths
|
//Normalize lengths
|
||||||
$x = str_pad($x, $maxlen, "0", STR_PAD_LEFT);
|
$x = str_pad($x, $maxlen, '0', STR_PAD_LEFT);
|
||||||
$y = str_pad($y, $maxlen, "0", STR_PAD_LEFT);
|
$y = str_pad($y, $maxlen, '0', STR_PAD_LEFT);
|
||||||
|
|
||||||
$result = '';
|
$result = '';
|
||||||
$carry = 0;
|
$carry = 0;
|
||||||
foreach(array_reverse($this->range(0, $maxlen)) as $i) {
|
foreach (array_reverse($this->range(0, $maxlen)) as $i) {
|
||||||
$r = $carry;
|
$r = $carry;
|
||||||
$r += ($x[$i] == '1') ? 1 : 0;
|
$r += ($x[$i] == '1') ? 1 : 0;
|
||||||
$r += ($y[$i] == '1') ? 1 : 0;
|
$r += ($y[$i] == '1') ? 1 : 0;
|
||||||
|
|
||||||
# r can be 0,1,2,3 (carry + x[i] + y[i])
|
// r can be 0,1,2,3 (carry + x[i] + y[i])
|
||||||
# and among these, for r==1 and r==3 you will have result bit = 1
|
// and among these, for r==1 and r==3 you will have result bit = 1
|
||||||
# for r==2 and r==3 you will have carry = 1
|
// for r==2 and r==3 you will have carry = 1
|
||||||
|
|
||||||
$result = (($r % 2 == 1) ? '1' : '0') . $result;
|
$result = (($r % 2 == 1) ? '1' : '0').$result;
|
||||||
$carry = ($r < 2) ? 0 : 1;
|
$carry = ($r < 2) ? 0 : 1;
|
||||||
}
|
}
|
||||||
if ($carry != 0) $result = '1' . $result;
|
if ($carry != 0) {
|
||||||
return str_pad($result, $maxlen, "0", STR_PAD_LEFT);
|
$result = '1'.$result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str_pad($result, $maxlen, '0', STR_PAD_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* num_pack.
|
* num_pack.
|
||||||
*
|
*
|
||||||
@ -786,7 +814,7 @@ class StructTools
|
|||||||
}
|
}
|
||||||
$bits = $this->decbin($n, $bitnumber);
|
$bits = $this->decbin($n, $bitnumber);
|
||||||
$s = null;
|
$s = null;
|
||||||
foreach (explode("2", wordwrap($bits, 8, "2", true)) as $byte) {
|
foreach (explode('2', wordwrap($bits, 8, '2', true)) as $byte) {
|
||||||
$s .= chr($this->bindec($byte));
|
$s .= chr($this->bindec($byte));
|
||||||
}
|
}
|
||||||
$break = true;
|
$break = true;
|
||||||
@ -833,13 +861,15 @@ class StructTools
|
|||||||
foreach (str_split($s) as $i) {
|
foreach (str_split($s) as $i) {
|
||||||
$bits .= $this->decbin(ord($i), 8);
|
$bits .= $this->decbin(ord($i), 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->bindec($bits, $unsigned);
|
return $this->bindec($bits, $unsigned);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* posmod(numeric,numeric) : numeric
|
* posmod(numeric,numeric) : numeric
|
||||||
* Works just like the % (modulus) operator, only returns always a postive number.
|
* Works just like the % (modulus) operator, only returns always a postive number.
|
||||||
*/
|
*/
|
||||||
function posmod($a, $b)
|
public function posmod($a, $b)
|
||||||
{
|
{
|
||||||
$resto = $a % $b;
|
$resto = $a % $b;
|
||||||
if ($resto < 0) {
|
if ($resto < 0) {
|
||||||
@ -848,6 +878,7 @@ class StructTools
|
|||||||
|
|
||||||
return $resto;
|
return $resto;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* array_each_strlen.
|
* array_each_strlen.
|
||||||
*
|
*
|
||||||
@ -884,6 +915,7 @@ class StructTools
|
|||||||
|
|
||||||
return $count;
|
return $count;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* range.
|
* range.
|
||||||
*
|
*
|
||||||
@ -935,4 +967,3 @@ class StructTools
|
|||||||
return strlen($input);
|
return strlen($input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user