From f2edb65f18a631c445449253704ec967e7b33b66 Mon Sep 17 00:00:00 2001 From: DigiLive Date: Tue, 2 Jun 2020 18:48:12 +0200 Subject: [PATCH] Fix bugs and typos. Bug fixes: - RegistryKey::getHive() returns invalid type on x64 distributions of php. - RegistryKey::SubKeyRecursive always results in error. - RegistryKey::getName() returns half qualified name instead of local keyName. - RegistryKey::getSubKeyRecursive() returns qualified names for subKeys instead of local names. --- src/RegistryKey.php | 40 +++++++++++++++++++++++------------ src/RegistryKeyIterator.php | 2 +- src/RegistryValueIterator.php | 2 +- tests/RegistryKeyTest.php | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/RegistryKey.php b/src/RegistryKey.php index 329323b..dad48f4 100644 --- a/src/RegistryKey.php +++ b/src/RegistryKey.php @@ -74,14 +74,27 @@ public function __construct(RegistryHandle $handle, float $hive, string $name) /** * Get the registry hive the key is located in. * - * Note: Although a hive is represented as a hex number, which is an int, php will cast this value into a float or - * double because the value causes an overflow. + * The method will return the hive as three different types, defined by the parameter and the 32bit or 64 bit + * version of php. + * + *
+     * Parameter value: 32b:    Return type:
+     * false            d.c.    VARIANT
+     * true             true    float
+     * true             false   int
+     * 
* - * @return float Hive of the key. + * @param bool $asNumber True to return the hive as number, False to return the hive as a variant. + * + * @return int|float|VARIANT Hive of the key. */ - public function getHive(): float + public function getHive($asNumber = true) { - return $this->hive; + if (!$asNumber) { + return new VARIANT($this->hive, VT_R8); + } + + return PHP_INT_SIZE == 4 ? (float)$this->hive : (int)$this->hive; } /** @@ -150,7 +163,7 @@ public function deleteSubKeyRecursive(string $name) } // Delete nested subKeys. - $this->deleteSubKeyRecursive($subKey->getName()); + $this->deleteSubKeyRecursive($subKey->getQualifiedName()); } $this->deleteSubKey($name); @@ -249,11 +262,7 @@ public function valueExists(string $name): bool */ public function getName(): string { - if (strpos($this->name, '\\') !== false) { - return substr($this->name, strpos($this->name, '\\') + 1); - } - - return $this->name; + return basename($this->name); } /** @@ -290,13 +299,13 @@ public function getSubKeyRecursive(string $name, bool $includeValues = false): a // Define the rootKey properties. $returnValue['type'] = 'key'; - $returnValue['name'] = $currentKey->getName(); + $returnValue['name'] = $name; $returnValue['keys'] = []; // Get the rootKey's subKeys. foreach ($currentKey->getSubKeyIterator() as $subKey) { // Get nested sub keys and values. - $returnValue['keys'][] = $this->getSubKeyRecursive($subKey->getQualifiedName(), $includeValues); + $returnValue['keys'][] = $currentKey->getSubKeyRecursive($subKey->getName(), $includeValues); } if ($includeValues) { @@ -314,7 +323,10 @@ public function getSubKeyRecursive(string $name, bool $includeValues = false): a } /** - * Get the fully-qualified name of the key. + * Get the qualified name of the key. + * + * The name includes: + * - All of the keyNames in the hierarchic sequence above this key and the name of the key itself. * * @return string Name of the key. */ diff --git a/src/RegistryKeyIterator.php b/src/RegistryKeyIterator.php index 1cdbf79..b637f60 100644 --- a/src/RegistryKeyIterator.php +++ b/src/RegistryKeyIterator.php @@ -127,7 +127,7 @@ public function rewind() // Enumerate subKeys. $errorCode = $this->handle->enumKey( - $this->registryKey->getHive(), + $this->registryKey->getHive(false), $this->registryKey->getQualifiedName(), $this->subKeyNames ); diff --git a/src/RegistryValueIterator.php b/src/RegistryValueIterator.php index 64ce5e8..7672e6c 100644 --- a/src/RegistryValueIterator.php +++ b/src/RegistryValueIterator.php @@ -88,7 +88,7 @@ public function rewind() // Attempt to enumerate values. $errorCode = $this->handle->enumValues( - $this->registryKey->getHive(), + $this->registryKey->getHive(false), $this->registryKey->getQualifiedName(), $this->valueNames, $this->valueTypes diff --git a/tests/RegistryKeyTest.php b/tests/RegistryKeyTest.php index 552f0e4..2c8f63b 100644 --- a/tests/RegistryKeyTest.php +++ b/tests/RegistryKeyTest.php @@ -280,7 +280,7 @@ public function testGetSubKeyIterator() /** * Test getting the type of a value. * - * Note: This test doesn't use the mocked registry handle, but instantiate a real one! + * Note: This test doesn't use the mocked registry handle, but instantiates a real one! * It will actually read from the registry. */ public function testGetValueType()