mirror of
https://github.com/danog/PHPStruct.git
synced 2024-11-26 11:44:39 +01:00
Fixed unpacking of signed negative numbers.
This commit is contained in:
parent
0a7aa3f918
commit
ab273b30c7
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/vendor/
|
/vendor/
|
||||||
|
*swp
|
@ -5,7 +5,7 @@ require 'lib/danog/PHP/Struct.php';
|
|||||||
//var_dump(["nv", 61, 61, false, 333, 444, 232423, 234342, 243342423424, 234234234234, 234234234234, 234234234234, 34434, 344434, 2.2343, 3.03424, "dd"]);
|
//var_dump(["nv", 61, 61, false, 333, 444, 232423, 234342, 243342423424, 234234234234, 234234234234, 234234234234, 34434, 344434, 2.2343, 3.03424, "dd"]);
|
||||||
var_dump(\danog\PHP\Struct::unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP',
|
var_dump(\danog\PHP\Struct::unpack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP',
|
||||||
\danog\PHP\Struct::pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP',
|
\danog\PHP\Struct::pack('2cxbxBx?xhxHxixIxlxLxqxQxnxNxfxdx2sx5pP',
|
||||||
'n', 'v', 100, 100, false, 333, 444, 232423, 234342, 999999999999, 999999999999, 888888888888,
|
'n', 'v', -100, 100, true, 333, 444, 232423, 234342, 999999999999, 999999999999, 888888888888,
|
||||||
888888888888, 34434, 344434, 2.2343,
|
888888888888, 34434, 344434, 2.2343,
|
||||||
3.03424, 'df', 'asdfghjkl', 1283912
|
3.03424, 'df', 'asdfghjkl', 1283912
|
||||||
|
|
||||||
|
@ -263,16 +263,24 @@ class Struct
|
|||||||
try {
|
try {
|
||||||
switch ($command['modifiers']['TYPE']) {
|
switch ($command['modifiers']['TYPE']) {
|
||||||
case 'int':
|
case 'int':
|
||||||
$data[$command['datakey']] = (int) $data[$command['datakey']];
|
if (!is_int($data[$command['datakey']]) && !is_float($data[$command['datakey']])) {
|
||||||
|
$data[$command['datakey']] = (int)$data[$command['datakey']];
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'float':
|
case 'float':
|
||||||
$data[$command['datakey']] = (float) $data[$command['datakey']];
|
if (!is_float($data[$command['datakey']])) {
|
||||||
|
$data[$command['datakey']] = (float) $data[$command['datakey']];
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'string':
|
case 'string':
|
||||||
$data[$command['datakey']] = (string) $data[$command['datakey']];
|
if (!is_string($data[$command['datakey']])) {
|
||||||
|
$data[$command['datakey']] = (string) $data[$command['datakey']];
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'bool':
|
case 'bool':
|
||||||
$data[$command['datakey']] = (bool) $data[$command['datakey']];
|
if (!is_bool($data[$command['datakey']])) {
|
||||||
|
$data[$command['datakey']] = (bool) $data[$command['datakey']];
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -304,7 +312,7 @@ class Struct
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (StructException $e) {
|
} catch (StructException $e) {
|
||||||
throw new StructException('An error occurred while packing data at offset '.$key.' ('.$e->getMessage().').');
|
throw new StructException('An error occurred while packing data at offset '.$data[$command['datakey']].' ('.$e->getMessage().').');
|
||||||
}
|
}
|
||||||
if ($command['modifiers']['FORMAT_ENDIANNESS'] != $command['modifiers']['BIG_ENDIAN']){
|
if ($command['modifiers']['FORMAT_ENDIANNESS'] != $command['modifiers']['BIG_ENDIAN']){
|
||||||
$curresult = strrev($curresult);
|
$curresult = strrev($curresult);
|
||||||
@ -401,7 +409,7 @@ class Struct
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (StructException $e) {
|
} catch (StructException $e) {
|
||||||
throw new StructException('An error occurred while unpacking data at offset '.$key.' ('.$e->getMessage().').');
|
throw new StructException('An error occurred while unpacking data at offset '.$command['datakey'].' ('.$e->getMessage().').');
|
||||||
}
|
}
|
||||||
switch ($command['modifiers']['TYPE']) {
|
switch ($command['modifiers']['TYPE']) {
|
||||||
case 'int':
|
case 'int':
|
||||||
@ -712,7 +720,7 @@ class Struct
|
|||||||
$acc = ($acc << 8) + unpack('C', substr($s, $i, 1))[1];
|
$acc = ($acc << 8) + unpack('C', substr($s, $i, 1))[1];
|
||||||
}
|
}
|
||||||
if(!$unsigned && $acc > (pow(2, ($bitnumber)-1) - 1)) {
|
if(!$unsigned && $acc > (pow(2, ($bitnumber)-1) - 1)) {
|
||||||
$acc = (($acc) ^ (pow(2, $bitnumber) - 1)) + 1;
|
$acc = -((($acc) ^ (pow(2, $bitnumber) - 1)) + 1);
|
||||||
}
|
}
|
||||||
return $acc;
|
return $acc;
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user