https://git.reactos.org/?p=reactos.git;a=commitdiff;h=8ed7d4b3414746097abe9b41303602515cfa97c2

commit 8ed7d4b3414746097abe9b41303602515cfa97c2
Author:     Hermès Bélusca-Maïto <[email protected]>
AuthorDate: Sun Apr 17 16:43:17 2022 +0200
Commit:     Hermès Bélusca-Maïto <[email protected]>
CommitDate: Sun Apr 17 16:46:47 2022 +0200

    [SDK:CMLIB][FREELDR:NTLDR] Revert commit 168fea0ee and provide a better 
fix. Addendum to 93d8a1b7b.
    
    Correct fix was to fix the HCELL_INDEX <-> HKEY conversions, much like
    is being done with UlongToHandle / HandleToUlong.
    
    The on-disk/in-memory hive file structures are platform-independent:
    their layout must not depend on whether code is compiled in 32 or 64
    bits.
---
 boot/freeldr/freeldr/ntldr/registry.c   | 29 +++++++++++++++++------------
 boot/freeldr/freeldr/ntldr/registry.h   |  2 ++
 boot/freeldr/freeldr/ntldr/wlregistry.c |  4 ++--
 sdk/lib/cmlib/hivedata.h                |  7 +------
 4 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/boot/freeldr/freeldr/ntldr/registry.c 
b/boot/freeldr/freeldr/ntldr/registry.c
index 566f1bfa189..adc0f9d075c 100644
--- a/boot/freeldr/freeldr/ntldr/registry.c
+++ b/boot/freeldr/freeldr/ntldr/registry.c
@@ -32,9 +32,14 @@ static HCELL_INDEX SystemRootCell;
 PHHIVE SystemHive = NULL;
 HKEY CurrentControlSetKey = NULL;
 
+#define HCI_TO_HKEY(CellIndex)          ((HKEY)(ULONG_PTR)(CellIndex))
+#ifndef HKEY_TO_HCI // See also registry.h
+#define HKEY_TO_HCI(hKey)               ((HCELL_INDEX)(ULONG_PTR)(hKey))
+#endif
+
 #define GET_HHIVE(CmHive)               (&((CmHive)->Hive))
 #define GET_HHIVE_FROM_HKEY(hKey)       GET_HHIVE(CmSystemHive)
-#define GET_CM_KEY_NODE(hHive, hKey)    ((PCM_KEY_NODE)HvGetCell(hHive, 
(HCELL_INDEX)hKey))
+#define GET_CM_KEY_NODE(hHive, hKey)    ((PCM_KEY_NODE)HvGetCell(hHive, 
HKEY_TO_HCI(hKey)))
 
 PVOID
 NTAPI
@@ -129,7 +134,7 @@ RegInitCurrentControlSet(
         return FALSE;
     }
 
-    CurrentControlSetKey = (HKEY)ControlCell;
+    CurrentControlSetKey = HCI_TO_HKEY(ControlCell);
 
     /* Verify it is accessible */
     KeyNode = (PCM_KEY_NODE)HvGetCell(SystemHive, ControlCell);
@@ -208,10 +213,10 @@ RegEnumKey(
     {
         TRACE("RegEnumKey index out of bounds (%d) in key (%.*s)\n",
               Index, KeyNode->NameLength, KeyNode->Name);
-        HvReleaseCell(Hive, (HCELL_INDEX)Key);
+        HvReleaseCell(Hive, HKEY_TO_HCI(Key));
         return ERROR_NO_MORE_ITEMS;
     }
-    HvReleaseCell(Hive, (HCELL_INDEX)Key);
+    HvReleaseCell(Hive, HKEY_TO_HCI(Key));
 
     /* Get the value cell */
     SubKeyNode = (PCM_KEY_NODE)HvGetCell(Hive, CellIndex);
@@ -247,7 +252,7 @@ RegEnumKey(
     HvReleaseCell(Hive, CellIndex);
 
     if (SubKey != NULL)
-        *SubKey = (HKEY)CellIndex;
+        *SubKey = HCI_TO_HKEY(CellIndex);
 
     TRACE("RegEnumKey done -> %u, '%.*S'\n", *NameSize, *NameSize, Name);
     return ERROR_SUCCESS;
@@ -315,7 +320,7 @@ RegOpenKey(
     else
     {
         /* Use the parent key */
-        CellIndex = (HCELL_INDEX)ParentKey;
+        CellIndex = HKEY_TO_HCI(ParentKey);
     }
 
     /* Check if this is the root key */
@@ -330,7 +335,7 @@ RegOpenKey(
         if (RtlEqualUnicodeString(&SubKeyName, &CurrentControlSet, TRUE))
         {
             /* Use the CurrentControlSetKey and update the remaining path */
-            CellIndex = (HCELL_INDEX)CurrentControlSetKey;
+            CellIndex = HKEY_TO_HCI(CurrentControlSetKey);
             RemainingPath = TempPath;
         }
     }
@@ -366,7 +371,7 @@ RegOpenKey(
     }
 
     HvReleaseCell(Hive, CellIndex);
-    *Key = (HKEY)CellIndex;
+    *Key = HCI_TO_HKEY(CellIndex);
 
     return ERROR_SUCCESS;
 }
@@ -438,10 +443,10 @@ RegQueryValue(
     {
         TRACE("RegQueryValue value not found in key (%.*s)\n",
               KeyNode->NameLength, KeyNode->Name);
-        HvReleaseCell(Hive, (HCELL_INDEX)Key);
+        HvReleaseCell(Hive, HKEY_TO_HCI(Key));
         return ERROR_FILE_NOT_FOUND;
     }
-    HvReleaseCell(Hive, (HCELL_INDEX)Key);
+    HvReleaseCell(Hive, HKEY_TO_HCI(Key));
 
     /* Get the value cell */
     ValueCell = (PCM_KEY_VALUE)HvGetCell(Hive, CellIndex);
@@ -490,7 +495,7 @@ RegEnumValue(
         (Index >= KeyNode->ValueList.Count))
     {
         ERR("RegEnumValue: index invalid\n");
-        HvReleaseCell(Hive, (HCELL_INDEX)Key);
+        HvReleaseCell(Hive, HKEY_TO_HCI(Key));
         return ERROR_NO_MORE_ITEMS;
     }
 
@@ -532,7 +537,7 @@ RegEnumValue(
 
     HvReleaseCell(Hive, ValueListCell->KeyList[Index]);
     HvReleaseCell(Hive, KeyNode->ValueList.List);
-    HvReleaseCell(Hive, (HCELL_INDEX)Key);
+    HvReleaseCell(Hive, HKEY_TO_HCI(Key));
 
     TRACE("RegEnumValue done -> %u, '%.*S'\n", *NameSize, *NameSize, 
ValueName);
     return ERROR_SUCCESS;
diff --git a/boot/freeldr/freeldr/ntldr/registry.h 
b/boot/freeldr/freeldr/ntldr/registry.h
index 96ed98d7e52..769d5b7c477 100644
--- a/boot/freeldr/freeldr/ntldr/registry.h
+++ b/boot/freeldr/freeldr/ntldr/registry.h
@@ -25,6 +25,8 @@
 
 typedef HANDLE HKEY, *PHKEY;
 
+#define HKEY_TO_HCI(hKey)               ((HCELL_INDEX)(ULONG_PTR)(hKey))
+
 BOOLEAN
 RegImportBinaryHive(
     _In_ PVOID ChunkBase,
diff --git a/boot/freeldr/freeldr/ntldr/wlregistry.c 
b/boot/freeldr/freeldr/ntldr/wlregistry.c
index 88f8c1731ae..23fda355ef9 100644
--- a/boot/freeldr/freeldr/ntldr/wlregistry.c
+++ b/boot/freeldr/freeldr/ntldr/wlregistry.c
@@ -515,7 +515,7 @@ WinLdrScanRegistry(
 
     /* Find all boot drivers */
     Success = CmpFindDrivers(SystemHive,
-                             (HCELL_INDEX)CurrentControlSetKey,
+                             HKEY_TO_HCI(CurrentControlSetKey),
                              BootLoad,
                              BootFileSystem,
                              BootDriverListHead);
@@ -524,7 +524,7 @@ WinLdrScanRegistry(
 
     /* Sort by group/tag */
     Success = CmpSortDriverList(SystemHive,
-                                (HCELL_INDEX)CurrentControlSetKey,
+                                HKEY_TO_HCI(CurrentControlSetKey),
                                 BootDriverListHead);
     if (!Success)
         goto Quit;
diff --git a/sdk/lib/cmlib/hivedata.h b/sdk/lib/cmlib/hivedata.h
index 20e844f8306..39d617ccbd5 100644
--- a/sdk/lib/cmlib/hivedata.h
+++ b/sdk/lib/cmlib/hivedata.h
@@ -77,7 +77,7 @@
  * the other bits specify index into the hive file. The value HCELL_NULL
  * (-1) is reserved for marking invalid cells.
  */
-typedef ULONG_PTR HCELL_INDEX, *PHCELL_INDEX;
+typedef ULONG HCELL_INDEX, *PHCELL_INDEX;
 
 //
 // Cell Magic Values
@@ -152,11 +152,7 @@ typedef struct _HBASE_BLOCK
        of the full name of the hive file */
     WCHAR FileName[HIVE_FILENAME_MAXLEN + 1];
 
-#ifdef _WIN64
-    ULONG Reserved1[98];
-#else
     ULONG Reserved1[99];
-#endif
 
     /* Checksum of first 0x200 bytes */
     ULONG CheckSum;
@@ -167,7 +163,6 @@ typedef struct _HBASE_BLOCK
 } HBASE_BLOCK, *PHBASE_BLOCK;
 
 C_ASSERT(sizeof(HBASE_BLOCK) == HBLOCK_SIZE);
-C_ASSERT(FIELD_OFFSET(HBASE_BLOCK, CheckSum) == 0x200 - sizeof(ULONG));
 
 typedef struct _HBIN
 {

Reply via email to