diff --git a/.gitignore b/.gitignore index 57872d0..65d4d2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /vendor/ +*swp \ No newline at end of file diff --git a/example.php b/example.php index 9610e45..b832e3f 100644 --- a/example.php +++ b/example.php @@ -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(\danog\PHP\Struct::unpack('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, 3.03424, 'df', 'asdfghjkl', 1283912 diff --git a/lib/danog/PHP/Struct.php b/lib/danog/PHP/Struct.php index 019b4ca..21f13c8 100644 --- a/lib/danog/PHP/Struct.php +++ b/lib/danog/PHP/Struct.php @@ -263,16 +263,24 @@ class Struct try { switch ($command['modifiers']['TYPE']) { 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; case 'float': - $data[$command['datakey']] = (float) $data[$command['datakey']]; + if (!is_float($data[$command['datakey']])) { + $data[$command['datakey']] = (float) $data[$command['datakey']]; + } break; case 'string': - $data[$command['datakey']] = (string) $data[$command['datakey']]; + if (!is_string($data[$command['datakey']])) { + $data[$command['datakey']] = (string) $data[$command['datakey']]; + } break; case 'bool': - $data[$command['datakey']] = (bool) $data[$command['datakey']]; + if (!is_bool($data[$command['datakey']])) { + $data[$command['datakey']] = (bool) $data[$command['datakey']]; + } break; default: break; @@ -304,7 +312,7 @@ class Struct break; } } 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']){ $curresult = strrev($curresult); @@ -401,7 +409,7 @@ class Struct break; } } 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']) { case 'int': @@ -712,7 +720,7 @@ class Struct $acc = ($acc << 8) + unpack('C', substr($s, $i, 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; } diff --git a/tests/danog/PHP/StructTest.php b/tests/danog/PHP/StructTest.php index cc8d963..319d221 100644 Binary files a/tests/danog/PHP/StructTest.php and b/tests/danog/PHP/StructTest.php differ