forget update de.rc
[reactos.git] / reactos / ntoskrnl / cm / regfile.c
index e6ad6b1..237638a 100644 (file)
@@ -1,18 +1,14 @@
-/*
- * COPYRIGHT:        See COPYING in the top level directory
- * PROJECT:          ReactOS kernel
- * FILE:             ntoskrnl/cm/regfile.c
- * PURPOSE:          Registry file manipulation routines
- * UPDATE HISTORY:
-*/
-
-#include <ddk/ntddk.h>
-#include <string.h>
-#include <roscfg.h>
-#include <internal/ob.h>
-#include <ntos/minmax.h>
-#include <reactos/bugcodes.h>
-
+/* $Id$
+ *
+ * COPYRIGHT:       See COPYING in the top level directory
+ * PROJECT:         ReactOS kernel
+ * FILE:            ntoskrnl/cm/regfile.c
+ * PURPOSE:         Registry file manipulation routines
+ *
+ * PROGRAMMERS:     No programmer listed.
+ */
+
+#include <ntoskrnl.h>
 #define NDEBUG
 #include <internal/debug.h>
 
@@ -24,8 +20,6 @@
 
 /* LOCAL MACROS *************************************************************/
 
-#define ROUND_DOWN(N, S) ((N) - ((N) % (S)))
-
 #define ABS_VALUE(V) (((V) < 0) ? -(V) : (V))
 
 BOOLEAN CmiDoVerify = FALSE;
@@ -38,21 +32,20 @@ CmiCalcChecksum(PULONG Buffer);
 VOID
 CmiCreateDefaultHiveHeader(PHIVE_HEADER Header)
 {
-  assert(Header);
+  ASSERT(Header);
   RtlZeroMemory(Header, sizeof(HIVE_HEADER));
   Header->BlockId = REG_HIVE_ID;
   Header->UpdateCounter1 = 0;
   Header->UpdateCounter2 = 0;
   Header->DateModified.u.LowPart = 0;
   Header->DateModified.u.HighPart = 0;
-  Header->Unused3 = 1;
-  Header->Unused4 = 3;
-  Header->Unused5 = 0;
-  Header->Unused6 = 1;
+  Header->MajorVersion = 1;
+  Header->MinorVersion = 3;
+  Header->Type = 0;
+  Header->Format = 1;
   Header->Unused7 = 1;
   Header->RootKeyOffset = (BLOCK_OFFSET)-1;
   Header->BlockSize = REG_BLOCK_SIZE;
-  Header->Unused6 = 1;
   Header->Checksum = 0;
 }
 
@@ -60,7 +53,7 @@ CmiCreateDefaultHiveHeader(PHIVE_HEADER Header)
 VOID
 CmiCreateDefaultBinHeader(PHBIN BinHeader)
 {
-  assert(BinHeader);
+  ASSERT(BinHeader);
   RtlZeroMemory(BinHeader, sizeof(HBIN));
   BinHeader->HeaderId = REG_BIN_ID;
   BinHeader->DateModified.u.LowPart = 0;
@@ -72,12 +65,12 @@ CmiCreateDefaultBinHeader(PHBIN BinHeader)
 VOID
 CmiCreateDefaultRootKeyCell(PKEY_CELL RootKeyCell)
 {
-  assert(RootKeyCell);
+  ASSERT(RootKeyCell);
   RtlZeroMemory(RootKeyCell, sizeof(KEY_CELL));
   RootKeyCell->CellSize = -sizeof(KEY_CELL);
   RootKeyCell->Id = REG_KEY_CELL_ID;
   RootKeyCell->Flags = REG_KEY_ROOT_CELL | REG_KEY_NAME_PACKED;
-  NtQuerySystemTime(&RootKeyCell->LastWriteTime);
+  KeQuerySystemTime(&RootKeyCell->LastWriteTime);
   RootKeyCell->ParentKeyOffset = 0;
   RootKeyCell->NumberOfSubKeys = 0;
   RootKeyCell->HashTableOffset = -1;
@@ -96,25 +89,25 @@ CmiVerifyBinHeader(PHBIN BinHeader)
   if (CmiDoVerify)
     {
 
-  assert(BinHeader);
+  ASSERT(BinHeader);
 
   if (BinHeader->HeaderId != REG_BIN_ID)
     {
       DbgPrint("Bin header ID is %.08x (should be %.08x)\n",
         BinHeader->HeaderId, REG_BIN_ID);
-      assert(BinHeader->HeaderId == REG_BIN_ID);
+      ASSERT(BinHeader->HeaderId == REG_BIN_ID);
     }
 
   //BinHeader->DateModified.dwLowDateTime
 
   //BinHeader->DateModified.dwHighDateTime
 
-  
+
   if (BinHeader->BinSize != REG_BLOCK_SIZE)
     {
       DbgPrint("BinSize is %.08x (should be a multiple of %.08x)\n",
         BinHeader->BinSize, REG_BLOCK_SIZE);
-      assert(BinHeader->BinSize % REG_BLOCK_SIZE == 0);
+      ASSERT(BinHeader->BinSize % REG_BLOCK_SIZE == 0);
     }
 
     }
@@ -127,20 +120,20 @@ CmiVerifyKeyCell(PKEY_CELL KeyCell)
   if (CmiDoVerify)
     {
 
-  assert(KeyCell);
+  ASSERT(KeyCell);
 
   if (KeyCell->CellSize == 0)
     {
       DbgPrint("CellSize is %d (must not be 0)\n",
         KeyCell->CellSize);
-      assert(KeyCell->CellSize != 0);
+      ASSERT(KeyCell->CellSize != 0);
     }
 
   if (KeyCell->Id != REG_KEY_CELL_ID)
     {
       DbgPrint("Id is %.08x (should be %.08x)\n",
         KeyCell->Id, REG_KEY_CELL_ID);
-      assert(KeyCell->Id == REG_KEY_CELL_ID);
+      ASSERT(KeyCell->Id == REG_KEY_CELL_ID);
     }
 
   //KeyCell->Flags;
@@ -151,14 +144,14 @@ CmiVerifyKeyCell(PKEY_CELL KeyCell)
     {
       DbgPrint("ParentKeyOffset is %d (must not be < 0)\n",
         KeyCell->ParentKeyOffset);
-      assert(KeyCell->ParentKeyOffset >= 0);
+      ASSERT(KeyCell->ParentKeyOffset >= 0);
     }
 
   if (KeyCell->NumberOfSubKeys < 0)
     {
       DbgPrint("NumberOfSubKeys is %d (must not be < 0)\n",
         KeyCell->NumberOfSubKeys);
-      assert(KeyCell->NumberOfSubKeys >= 0);
+      ASSERT(KeyCell->NumberOfSubKeys >= 0);
     }
 
   //KeyCell->HashTableOffset;
@@ -167,7 +160,7 @@ CmiVerifyKeyCell(PKEY_CELL KeyCell)
     {
       DbgPrint("NumberOfValues is %d (must not be < 0)\n",
         KeyCell->NumberOfValues);
-      assert(KeyCell->NumberOfValues >= 0);
+      ASSERT(KeyCell->NumberOfValues >= 0);
     }
 
   //KeyCell->ValuesOffset = -1;
@@ -176,7 +169,7 @@ CmiVerifyKeyCell(PKEY_CELL KeyCell)
     {
       DbgPrint("SecurityKeyOffset is %d (must not be < 0)\n",
         KeyCell->SecurityKeyOffset);
-      assert(KeyCell->SecurityKeyOffset >= 0);
+      ASSERT(KeyCell->SecurityKeyOffset >= 0);
     }
 
   //KeyCell->ClassNameOffset = -1;
@@ -201,7 +194,7 @@ CmiVerifyRootKeyCell(PKEY_CELL RootKeyCell)
     {
       DbgPrint("Flags is %.08x (should be %.08x)\n",
         RootKeyCell->Flags, REG_KEY_ROOT_CELL | REG_KEY_NAME_PACKED);
-      assert(!(RootKeyCell->Flags & (REG_KEY_ROOT_CELL | REG_KEY_NAME_PACKED)));
+      ASSERT(!(RootKeyCell->Flags & (REG_KEY_ROOT_CELL | REG_KEY_NAME_PACKED)));
     }
 
     }
@@ -214,20 +207,20 @@ CmiVerifyValueCell(PVALUE_CELL ValueCell)
   if (CmiDoVerify)
     {
 
-  assert(ValueCell);
+  ASSERT(ValueCell);
 
   if (ValueCell->CellSize == 0)
     {
       DbgPrint("CellSize is %d (must not be 0)\n",
         ValueCell->CellSize);
-      assert(ValueCell->CellSize != 0);
+      ASSERT(ValueCell->CellSize != 0);
     }
 
   if (ValueCell->Id != REG_VALUE_CELL_ID)
     {
       DbgPrint("Id is %.08x (should be %.08x)\n",
         ValueCell->Id, REG_VALUE_CELL_ID);
-      assert(ValueCell->Id == REG_VALUE_CELL_ID);
+      ASSERT(ValueCell->Id == REG_VALUE_CELL_ID);
     }
 
   //ValueCell->NameSize;
@@ -251,7 +244,7 @@ CmiVerifyValueListCell(PVALUE_LIST_CELL ValueListCell)
     {
       DbgPrint("CellSize is %d (must not be 0)\n",
         ValueListCell->CellSize);
-      assert(ValueListCell->CellSize != 0);
+      ASSERT(ValueListCell->CellSize != 0);
     }
 
     }
@@ -268,21 +261,21 @@ CmiVerifyKeyObject(PKEY_OBJECT KeyObject)
     {
       DbgPrint("RegistryHive is NULL (must not be NULL)\n",
         KeyObject->RegistryHive);
-      assert(KeyObject->RegistryHive != NULL);
+      ASSERT(KeyObject->RegistryHive != NULL);
     }
 
   if (KeyObject->KeyCell == NULL)
     {
       DbgPrint("KeyCell is NULL (must not be NULL)\n",
         KeyObject->KeyCell);
-      assert(KeyObject->KeyCell != NULL);
+      ASSERT(KeyObject->KeyCell != NULL);
     }
 
   if (KeyObject->ParentKey == NULL)
     {
       DbgPrint("ParentKey is NULL (must not be NULL)\n",
         KeyObject->ParentKey);
-      assert(KeyObject->ParentKey != NULL);
+      ASSERT(KeyObject->ParentKey != NULL);
     }
 
     }
@@ -300,42 +293,42 @@ CmiVerifyHiveHeader(PHIVE_HEADER Header)
       DbgPrint("BlockId is %.08x (must be %.08x)\n",
         Header->BlockId,
         REG_HIVE_ID);
-      assert(Header->BlockId == REG_HIVE_ID);
+      ASSERT(Header->BlockId == REG_HIVE_ID);
     }
 
-  if (Header->Unused3 != 1)
+  if (Header->MajorVersion != 1)
     {
-      DbgPrint("Unused3 is %.08x (must be 1)\n",
-        Header->Unused3);
-      assert(Header->Unused3 == 1);
+      DbgPrint("MajorVersion is %.08x (must be 1)\n",
+        Header->MajorVersion);
+      ASSERT(Header->MajorVersion == 1);
     }
 
-  if (Header->Unused4 != 3)
+  if (Header->MinorVersion != 3)
     {
-      DbgPrint("Unused4 is %.08x (must be 3)\n",
-        Header->Unused4);
-      assert(Header->Unused4 == 3);
+      DbgPrint("MinorVersion is %.08x (must be 3)\n",
+        Header->MajorVersion);
+      ASSERT(Header->MajorVersion == 3);
     }
 
-  if (Header->Unused5 != 0)
+  if (Header->Type != 0)
     {
-      DbgPrint("Unused5 is %.08x (must be 0)\n",
-        Header->Unused5);
-      assert(Header->Unused5 == 0);
+      DbgPrint("Type is %.08x (must be 0)\n",
+        Header->Type);
+      ASSERT(Header->Type == 0);
     }
 
-  if (Header->Unused6 != 1)
+  if (Header->Format != 1)
     {
-      DbgPrint("Unused6 is %.08x (must be 1)\n",
-        Header->Unused6);
-      assert(Header->Unused6 == 1);
+      DbgPrint("Format is %.08x (must be 1)\n",
+        Header->Format);
+      ASSERT(Header->Format == 1);
     }
 
   if (Header->Unused7 != 1)
     {
       DbgPrint("Unused7 is %.08x (must be 1)\n",
         Header->Unused7);
-      assert(Header->Unused7 == 1);
+      ASSERT(Header->Unused7 == 1);
     }
 
     }
@@ -390,7 +383,7 @@ CmiCreateNewRegFile(HANDLE FileHandle)
   /* The rest of the block is free */
   FreeCell->CellSize = REG_BLOCK_SIZE - (REG_HBIN_DATA_OFFSET + sizeof(KEY_CELL));
 
-  Status = NtWriteFile(FileHandle,
+  Status = ZwWriteFile(FileHandle,
                       NULL,
                       NULL,
                       NULL,
@@ -402,14 +395,12 @@ CmiCreateNewRegFile(HANDLE FileHandle)
 
   ExFreePool(Buffer);
 
-  assertmsg(NT_SUCCESS(Status), ("Status: 0x%X\n", Status));
-
   if (!NT_SUCCESS(Status))
     {
       return(Status);
     }
 
-  Status = NtFlushBuffersFile(FileHandle,
+  Status = ZwFlushBuffersFile(FileHandle,
                              &IoStatusBlock);
 
   return(Status);
@@ -439,11 +430,11 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
   /* Try to open the hive file */
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->HiveFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&HiveHandle,
+  Status = ZwCreateFile(&HiveHandle,
                        FILE_READ_DATA | FILE_READ_ATTRIBUTES,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -460,18 +451,18 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
     }
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       return(Status);
     }
 
   /* Try to open the log file */
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->LogFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&LogHandle,
+  Status = ZwCreateFile(&LogHandle,
                        FILE_READ_DATA | FILE_READ_ATTRIBUTES,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -488,8 +479,8 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
     }
   else if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
-      NtClose(HiveHandle);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
+      ZwClose(HiveHandle);
       return(Status);
     }
 
@@ -505,7 +496,7 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
 
   /* Read hive base block */
   FileOffset.QuadPart = 0ULL;
-  Status = NtReadFile(HiveHandle,
+  Status = ZwReadFile(HiveHandle,
                      0,
                      0,
                      0,
@@ -516,7 +507,7 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
                      0);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtReadFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwReadFile() failed (Status %lx)\n", Status);
       goto ByeBye;
     }
 
@@ -547,7 +538,7 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
 
       /* Read log file header */
       FileOffset.QuadPart = 0ULL;
-      Status = NtReadFile(LogHandle,
+      Status = ZwReadFile(LogHandle,
                          0,
                          0,
                          0,
@@ -558,7 +549,7 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
                          0);
       if (!NT_SUCCESS(Status))
        {
-         DPRINT("NtReadFile() failed (Status %lx)\n", Status);
+         DPRINT("ZwReadFile() failed (Status %lx)\n", Status);
          goto ByeBye;
        }
 
@@ -591,14 +582,14 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
        */
 
       /* Get file size */
-      Status = NtQueryInformationFile(LogHandle,
+      Status = ZwQueryInformationFile(LogHandle,
                                      &IoStatusBlock,
                                      &fsi,
                                      sizeof(fsi),
                                      FileStandardInformation);
       if (!NT_SUCCESS(Status))
        {
-         DPRINT("NtQueryInformationFile() failed (Status %lx)\n", Status);
+         DPRINT("ZwQueryInformationFile() failed (Status %lx)\n", Status);
          goto ByeBye;
        }
       FileSize = fsi.EndOfFile.u.LowPart;
@@ -623,7 +614,7 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
 
       /* Read log file header */
       FileOffset.QuadPart = 0ULL;
-      Status = NtReadFile(LogHandle,
+      Status = ZwReadFile(LogHandle,
                          0,
                          0,
                          0,
@@ -634,13 +625,13 @@ CmiCheckAndFixHive(PREGISTRY_HIVE RegistryHive)
                          0);
       if (!NT_SUCCESS(Status))
        {
-         DPRINT("NtReadFile() failed (Status %lx)\n", Status);
+         DPRINT("ZwReadFile() failed (Status %lx)\n", Status);
          goto ByeBye;
        }
 
       /* Initialize bitmap */
       RtlInitializeBitMap(&BlockBitMap,
-                         (PVOID)((ULONG)LogHeader + REG_BLOCK_SIZE + sizeof(ULONG)),
+                         (PVOID)((ULONG_PTR)LogHeader + REG_BLOCK_SIZE + sizeof(ULONG)),
                          BitmapSize * 8);
 
       /* FIXME: Update dirty blocks */
@@ -662,9 +653,9 @@ ByeBye:
     ExFreePool(LogHeader);
 
   if (LogHandle != INVALID_HANDLE_VALUE)
-    NtClose(LogHandle);
+    ZwClose(LogHandle);
 
-  NtClose(HiveHandle);
+  ZwClose(HiveHandle);
 
   return(Status);
 }
@@ -692,9 +683,8 @@ CmiImportHiveBins(PREGISTRY_HIVE Hive,
          return STATUS_REGISTRY_CORRUPT;
        }
 
-      assertmsg((Bin->BinSize % REG_BLOCK_SIZE) == 0,
-               ("Bin size (0x%.08x) must be multiple of 4K\n",
-               Bin->BinSize));
+      ASSERTMSG("Bin size must be multiple of 4K\n",
+                (Bin->BinSize % REG_BLOCK_SIZE) == 0);
 
       /* Allocate the hive block */
       Hive->BlockList[BlockIndex].Bin = ExAllocatePool (PagedPool,
@@ -858,7 +848,7 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
   ULONG CreateDisposition;
   IO_STATUS_BLOCK IoSB;
   HANDLE FileHandle;
-  HANDLE SectionHandle;
+  PSECTION_OBJECT SectionObject;
   PUCHAR ViewBase;
   ULONG ViewSize;
   NTSTATUS Status;
@@ -868,7 +858,7 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
 
   /* Duplicate Filename */
   Status = RtlCreateUnicodeString(&RegistryHive->HiveFileName,
-                                 Filename);
+                                  Filename);
   if (!NT_SUCCESS(Status))
     {
       DPRINT("RtlCreateUnicodeString() failed (Status %lx)\n", Status);
@@ -878,8 +868,9 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
   /* Create log file name */
   RegistryHive->LogFileName.Length = (wcslen(Filename) + 4) * sizeof(WCHAR);
   RegistryHive->LogFileName.MaximumLength = RegistryHive->LogFileName.Length + sizeof(WCHAR);
-  RegistryHive->LogFileName.Buffer = ExAllocatePool(NonPagedPool,
-                                                   RegistryHive->LogFileName.MaximumLength);
+  RegistryHive->LogFileName.Buffer = ExAllocatePoolWithTag(PagedPool,
+                                                          RegistryHive->LogFileName.MaximumLength,
+                                                           TAG('U', 'S', 'T', 'R'));
   if (RegistryHive->LogFileName.Buffer == NULL)
     {
       RtlFreeUnicodeString(&RegistryHive->HiveFileName);
@@ -905,12 +896,12 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
 
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->HiveFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
   CreateDisposition = FILE_OPEN_IF;
-  Status = NtCreateFile(&FileHandle,
+  Status = ZwCreateFile(&FileHandle,
                        FILE_ALL_ACCESS,
                        &ObjectAttributes,
                        &IoSB,
@@ -925,7 +916,7 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
     {
       RtlFreeUnicodeString(&RegistryHive->HiveFileName);
       RtlFreeUnicodeString(&RegistryHive->LogFileName);
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       return(Status);
     }
 
@@ -935,7 +926,7 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
       if (!NT_SUCCESS(Status))
        {
          DPRINT("CmiCreateNewRegFile() failed (Status %lx)\n", Status);
-         NtClose(FileHandle);
+         ZwClose(FileHandle);
          RtlFreeUnicodeString(&RegistryHive->HiveFileName);
          RtlFreeUnicodeString(&RegistryHive->LogFileName);
          return(Status);
@@ -943,17 +934,17 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
     }
 
   /* Create the hive section */
-  Status = NtCreateSection(&SectionHandle,
+  Status = MmCreateSection(&SectionObject,
                           SECTION_ALL_ACCESS,
                           NULL,
                           NULL,
                           PAGE_READWRITE,
                           SEC_COMMIT,
-                          FileHandle);
-  NtClose(FileHandle);
+                          FileHandle,
+                          NULL);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT1("NtCreateSection() failed (Status %lx)\n", Status);
+      DPRINT1("MmCreateSection() failed (Status %lx)\n", Status);
       RtlFreeUnicodeString(&RegistryHive->HiveFileName);
       RtlFreeUnicodeString(&RegistryHive->LogFileName);
       return(Status);
@@ -962,8 +953,8 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
   /* Map the hive file */
   ViewBase = NULL;
   ViewSize = 0;
-  Status = NtMapViewOfSection(SectionHandle,
-                             NtCurrentProcess(),
+  Status = MmMapViewOfSection(SectionObject,
+                             PsGetCurrentProcess(),
                              (PVOID*)&ViewBase,
                              0,
                              ViewSize,
@@ -974,10 +965,11 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
                              PAGE_READWRITE);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT1("MmMapViewInSystemSpace() failed (Status %lx)\n", Status);
-      NtClose(SectionHandle);
+      DPRINT1("MmMapViewOfSection() failed (Status %lx)\n", Status);
+      ObDereferenceObject(SectionObject);
       RtlFreeUnicodeString(&RegistryHive->HiveFileName);
       RtlFreeUnicodeString(&RegistryHive->LogFileName);
+      ZwClose(FileHandle);
       return(Status);
     }
   DPRINT("ViewBase %p  ViewSize %lx\n", ViewBase, ViewSize);
@@ -996,11 +988,12 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
   if (RegistryHive->BlockList == NULL)
     {
       DPRINT1("Failed to allocate the hive block list\n");
-      NtUnmapViewOfSection(NtCurrentProcess(),
+      MmUnmapViewOfSection(PsGetCurrentProcess(),
                           ViewBase);
-      NtClose(SectionHandle);
+      ObDereferenceObject(SectionObject);
       RtlFreeUnicodeString(&RegistryHive->HiveFileName);
       RtlFreeUnicodeString(&RegistryHive->LogFileName);
+      ZwClose(FileHandle);
       return STATUS_INSUFFICIENT_RESOURCES;
     }
   RtlZeroMemory (RegistryHive->BlockList,
@@ -1012,18 +1005,22 @@ CmiInitNonVolatileRegistryHive (PREGISTRY_HIVE RegistryHive,
   if (!NT_SUCCESS(Status))
     {
       ExFreePool(RegistryHive->BlockList);
-      NtUnmapViewOfSection(NtCurrentProcess(),
+      MmUnmapViewOfSection(PsGetCurrentProcess(),
                           ViewBase);
-      NtClose(SectionHandle);
+      ObDereferenceObject(SectionObject);
       RtlFreeUnicodeString(&RegistryHive->HiveFileName);
       RtlFreeUnicodeString(&RegistryHive->LogFileName);
+      ZwClose(FileHandle);
       return Status;
     }
 
   /* Unmap and dereference the hive section */
-  NtUnmapViewOfSection(NtCurrentProcess(),
-                      ViewBase);
-  NtClose(SectionHandle);
+  MmUnmapViewOfSection(PsGetCurrentProcess(),
+                       ViewBase);
+  ObDereferenceObject(SectionObject);
+
+  /* Close the hive file */
+  ZwClose(FileHandle);
 
   /* Initialize the free cell list */
   Status = CmiCreateHiveFreeCellList (RegistryHive);
@@ -1071,7 +1068,7 @@ CmiCreateVolatileHive(PREGISTRY_HIVE *RegistryHive)
   RtlZeroMemory (Hive,
                 sizeof(REGISTRY_HIVE));
 
-  DPRINT("Hive %x\n", Hive);
+  DPRINT("Hive 0x%p\n", Hive);
 
   Hive->HiveHeader = (PHIVE_HEADER)ExAllocatePool (NonPagedPool,
                                                   sizeof(HIVE_HEADER));
@@ -1099,19 +1096,16 @@ CmiCreateVolatileHive(PREGISTRY_HIVE *RegistryHive)
   CmiCreateDefaultRootKeyCell (RootKeyCell);
   Hive->HiveHeader->RootKeyOffset = (BLOCK_OFFSET)RootKeyCell;
 
-  ExInitializeResourceLite (&Hive->HiveResource);
-
   /* Acquire hive list lock exclusively */
   KeEnterCriticalRegion();
-  ExAcquireResourceExclusiveLite (&CmiHiveListLock,
-                                 TRUE);
+  ExAcquireResourceExclusiveLite (&CmiRegistryLock, TRUE);
 
   /* Add the new hive to the hive list */
   InsertTailList (&CmiHiveListHead,
                  &Hive->HiveList);
 
   /* Release hive list lock */
-  ExReleaseResourceLite (&CmiHiveListLock);
+  ExReleaseResourceLite (&CmiRegistryLock);
   KeLeaveCriticalRegion();
 
   VERIFY_REGISTRY_HIVE (Hive);
@@ -1144,7 +1138,7 @@ CmiCreateTempHive(PREGISTRY_HIVE *RegistryHive)
   RtlZeroMemory (Hive,
                 sizeof(REGISTRY_HIVE));
 
-  DPRINT ("Hive %x\n", Hive);
+  DPRINT ("Hive 0x%p\n", Hive);
 
   Hive->HiveHeader = (PHIVE_HEADER)ExAllocatePool (NonPagedPool,
                                                   REG_BLOCK_SIZE);
@@ -1157,7 +1151,7 @@ CmiCreateTempHive(PREGISTRY_HIVE *RegistryHive)
   RtlZeroMemory (Hive->HiveHeader,
                 REG_BLOCK_SIZE);
 
-  DPRINT ("HiveHeader %x\n", Hive->HiveHeader);
+  DPRINT ("HiveHeader 0x%p\n", Hive->HiveHeader);
 
   Hive->Flags = HIVE_NO_FILE;
 
@@ -1223,20 +1217,16 @@ CmiCreateTempHive(PREGISTRY_HIVE *RegistryHive)
       return Status;
     }
 
-
-  ExInitializeResourceLite (&Hive->HiveResource);
-
   /* Acquire hive list lock exclusively */
   KeEnterCriticalRegion();
-  ExAcquireResourceExclusiveLite (&CmiHiveListLock,
-                                 TRUE);
+  ExAcquireResourceExclusiveLite(&CmiRegistryLock, TRUE);
 
   /* Add the new hive to the hive list */
   InsertTailList (&CmiHiveListHead,
                  &Hive->HiveList);
 
   /* Release hive list lock */
-  ExReleaseResourceLite (&CmiHiveListLock);
+  ExReleaseResourceLite(&CmiRegistryLock);
   KeLeaveCriticalRegion();
 
   VERIFY_REGISTRY_HIVE (Hive);
@@ -1270,7 +1260,7 @@ CmiLoadHive(IN POBJECT_ATTRIBUTES KeyObjectAttributes,
   RtlZeroMemory (Hive,
                 sizeof(REGISTRY_HIVE));
 
-  DPRINT ("Hive %x\n", Hive);
+  DPRINT ("Hive 0x%p\n", Hive);
   Hive->Flags = (Flags & REG_NO_LAZY_FLUSH) ? HIVE_NO_SYNCH : 0;
 
   Hive->HiveHeader = (PHIVE_HEADER)ExAllocatePool(NonPagedPool,
@@ -1295,21 +1285,12 @@ CmiLoadHive(IN POBJECT_ATTRIBUTES KeyObjectAttributes,
       return Status;
     }
 
-  ExInitializeResourceLite (&Hive->HiveResource);
-
   /* Add the new hive to the hive list */
-  KeEnterCriticalRegion();
-  ExAcquireResourceExclusiveLite (&CmiHiveListLock,
-                                 TRUE);
   InsertTailList (&CmiHiveListHead,
                  &Hive->HiveList);
-  ExReleaseResourceLite (&CmiHiveListLock);
-  KeLeaveCriticalRegion();
-
 
   VERIFY_REGISTRY_HIVE(Hive);
 
-
   Status = CmiConnectHive (KeyObjectAttributes,
                           Hive);
   if (!NT_SUCCESS(Status))
@@ -1330,18 +1311,9 @@ CmiRemoveRegistryHive(PREGISTRY_HIVE RegistryHive)
   if (RegistryHive->Flags & HIVE_POINTER)
     return STATUS_UNSUCCESSFUL;
 
-  /* Acquire hive list lock exclusively */
-  KeEnterCriticalRegion();
-  ExAcquireResourceExclusiveLite (&CmiHiveListLock,
-                                 TRUE);
-
   /* Remove hive from hive list */
   RemoveEntryList (&RegistryHive->HiveList);
 
-  /* Release hive list lock */
-  ExReleaseResourceLite (&CmiHiveListLock);
-  KeLeaveCriticalRegion();
-
   /* Release file names */
   RtlFreeUnicodeString (&RegistryHive->HiveFileName);
   RtlFreeUnicodeString (&RegistryHive->LogFileName);
@@ -1353,9 +1325,6 @@ CmiRemoveRegistryHive(PREGISTRY_HIVE RegistryHive)
   ExFreePool (RegistryHive->FreeList);
   ExFreePool (RegistryHive->FreeListOffset);
 
-  /* Release hive resource */
-  ExDeleteResource (&RegistryHive->HiveResource);
-
   /* Release bins and bin list */
   CmiFreeHiveBins (RegistryHive);
   ExFreePool (RegistryHive->BlockList);
@@ -1397,6 +1366,7 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
   PUCHAR Buffer;
   PUCHAR Ptr;
   ULONG BlockIndex;
+  ULONG LastIndex;
   PVOID BlockPtr;
   NTSTATUS Status;
 
@@ -1423,11 +1393,11 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
   /* Open log file for writing */
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->LogFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&FileHandle,
+  Status = ZwCreateFile(&FileHandle,
                        FILE_ALL_ACCESS,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -1440,7 +1410,7 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
                        0);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       ExFreePool(Buffer);
       return(Status);
     }
@@ -1465,7 +1435,7 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
 
   /* Write hive block and block bitmap */
   FileOffset.QuadPart = (ULONGLONG)0;
-  Status = NtWriteFile(FileHandle,
+  Status = ZwWriteFile(FileHandle,
                       NULL,
                       NULL,
                       NULL,
@@ -1476,8 +1446,8 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
                       NULL);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwWriteFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       ExFreePool(Buffer);
       return(Status);
     }
@@ -1486,13 +1456,13 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
   /* Write dirty blocks */
   FileOffset.QuadPart = (ULONGLONG)BufferSize;
   BlockIndex = 0;
-  while (TRUE)
+  while (BlockIndex < RegistryHive->BlockListSize)
     {
+      LastIndex = BlockIndex;
       BlockIndex = RtlFindSetBits(&RegistryHive->DirtyBitMap,
                                  1,
                                  BlockIndex);
-      if ((BlockIndex == (ULONG)-1) ||
-         (BlockIndex >= RegistryHive->BlockListSize))
+      if (BlockIndex == (ULONG)-1 || BlockIndex < LastIndex)
        {
          DPRINT("No more set bits\n");
          Status = STATUS_SUCCESS;
@@ -1506,7 +1476,7 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
       DPRINT("File offset %I64x\n", FileOffset.QuadPart);
 
       /* Write hive block */
-      Status = NtWriteFile(FileHandle,
+      Status = ZwWriteFile(FileHandle,
                           NULL,
                           NULL,
                           NULL,
@@ -1517,8 +1487,8 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
                           NULL);
       if (!NT_SUCCESS(Status))
        {
-         DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
-         NtClose(FileHandle);
+         DPRINT1("ZwWriteFile() failed (Status %lx)\n", Status);
+         ZwClose(FileHandle);
          return(Status);
        }
 
@@ -1528,40 +1498,40 @@ CmiStartLogUpdate(PREGISTRY_HIVE RegistryHive)
 
   /* Truncate log file */
   EndOfFileInfo.EndOfFile.QuadPart = FileOffset.QuadPart;
-  Status = NtSetInformationFile(FileHandle,
+  Status = ZwSetInformationFile(FileHandle,
                                &IoStatusBlock,
                                &EndOfFileInfo,
                                sizeof(FILE_END_OF_FILE_INFORMATION),
                                FileEndOfFileInformation);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtSetInformationFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwSetInformationFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       return(Status);
     }
 
   FileAllocationInfo.AllocationSize.QuadPart = FileOffset.QuadPart;
-  Status = NtSetInformationFile(FileHandle,
+  Status = ZwSetInformationFile(FileHandle,
                                &IoStatusBlock,
                                &FileAllocationInfo,
                                sizeof(FILE_ALLOCATION_INFORMATION),
                                FileAllocationInformation);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtSetInformationFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwSetInformationFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       return(Status);
     }
 
   /* Flush the log file */
-  Status = NtFlushBuffersFile(FileHandle,
+  Status = ZwFlushBuffersFile(FileHandle,
                              &IoStatusBlock);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtFlushBuffersFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwFlushBuffersFile() failed (Status %lx)\n", Status);
     }
 
-  NtClose(FileHandle);
+  ZwClose(FileHandle);
 
   return(Status);
 }
@@ -1600,11 +1570,11 @@ CmiFinishLogUpdate(PREGISTRY_HIVE RegistryHive)
   /* Open log file for writing */
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->LogFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&FileHandle,
+  Status = ZwCreateFile(&FileHandle,
                        FILE_ALL_ACCESS,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -1617,7 +1587,7 @@ CmiFinishLogUpdate(PREGISTRY_HIVE RegistryHive)
                        0);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       ExFreePool(Buffer);
       return(Status);
     }
@@ -1643,7 +1613,7 @@ CmiFinishLogUpdate(PREGISTRY_HIVE RegistryHive)
 
   /* Write hive block and block bitmap */
   FileOffset.QuadPart = (ULONGLONG)0;
-  Status = NtWriteFile(FileHandle,
+  Status = ZwWriteFile(FileHandle,
                       NULL,
                       NULL,
                       NULL,
@@ -1654,8 +1624,8 @@ CmiFinishLogUpdate(PREGISTRY_HIVE RegistryHive)
                       NULL);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwWriteFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       ExFreePool(Buffer);
       return(Status);
     }
@@ -1663,14 +1633,14 @@ CmiFinishLogUpdate(PREGISTRY_HIVE RegistryHive)
   ExFreePool(Buffer);
 
   /* Flush the log file */
-  Status = NtFlushBuffersFile(FileHandle,
+  Status = ZwFlushBuffersFile(FileHandle,
                              &IoStatusBlock);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtFlushBuffersFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwFlushBuffersFile() failed (Status %lx)\n", Status);
     }
 
-  NtClose(FileHandle);
+  ZwClose(FileHandle);
 
   return(Status);
 }
@@ -1701,11 +1671,11 @@ CmiCleanupLogUpdate(PREGISTRY_HIVE RegistryHive)
   /* Open log file for writing */
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->LogFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&FileHandle,
+  Status = ZwCreateFile(&FileHandle,
                        FILE_ALL_ACCESS,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -1718,26 +1688,26 @@ CmiCleanupLogUpdate(PREGISTRY_HIVE RegistryHive)
                        0);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       return(Status);
     }
 
   /* Truncate log file */
   EndOfFileInfo.EndOfFile.QuadPart = (ULONGLONG)BufferSize;
-  Status = NtSetInformationFile(FileHandle,
+  Status = ZwSetInformationFile(FileHandle,
                                &IoStatusBlock,
                                &EndOfFileInfo,
                                sizeof(FILE_END_OF_FILE_INFORMATION),
                                FileEndOfFileInformation);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtSetInformationFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwSetInformationFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       return(Status);
     }
 
   FileAllocationInfo.AllocationSize.QuadPart = (ULONGLONG)BufferSize;
-  Status = NtSetInformationFile(FileHandle,
+  Status = ZwSetInformationFile(FileHandle,
                                &IoStatusBlock,
                                &FileAllocationInfo,
                                sizeof(FILE_ALLOCATION_INFORMATION),
@@ -1745,19 +1715,19 @@ CmiCleanupLogUpdate(PREGISTRY_HIVE RegistryHive)
   if (!NT_SUCCESS(Status))
     {
       DPRINT("NtSetInformationFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      ZwClose(FileHandle);
       return(Status);
     }
 
   /* Flush the log file */
-  Status = NtFlushBuffersFile(FileHandle,
+  Status = ZwFlushBuffersFile(FileHandle,
                              &IoStatusBlock);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtFlushBuffersFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwFlushBuffersFile() failed (Status %lx)\n", Status);
     }
 
-  NtClose(FileHandle);
+  ZwClose(FileHandle);
 
   return(Status);
 }
@@ -1771,6 +1741,7 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
   HANDLE FileHandle;
   LARGE_INTEGER FileOffset;
   ULONG BlockIndex;
+  ULONG LastIndex;
   PVOID BlockPtr;
   NTSTATUS Status;
 
@@ -1779,11 +1750,11 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
   /* Open hive for writing */
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->HiveFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&FileHandle,
+  Status = ZwCreateFile(&FileHandle,
                        FILE_ALL_ACCESS,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -1796,7 +1767,7 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
                        0);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       return(Status);
     }
 
@@ -1806,7 +1777,7 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
 
   /* Write hive block */
   FileOffset.QuadPart = (ULONGLONG)0;
-  Status = NtWriteFile(FileHandle,
+  Status = ZwWriteFile(FileHandle,
                       NULL,
                       NULL,
                       NULL,
@@ -1817,19 +1788,19 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
                       NULL);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwWriteFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       return(Status);
     }
 
   BlockIndex = 0;
-  while (TRUE)
+  while (BlockIndex < RegistryHive->BlockListSize)
     {
+      LastIndex = BlockIndex;
       BlockIndex = RtlFindSetBits(&RegistryHive->DirtyBitMap,
                                  1,
                                  BlockIndex);
-      if ((BlockIndex == (ULONG)-1) ||
-         (BlockIndex >= RegistryHive->BlockListSize))
+      if (BlockIndex == (ULONG)-1 || BlockIndex < LastIndex)
        {
          DPRINT("No more set bits\n");
          Status = STATUS_SUCCESS;
@@ -1845,7 +1816,7 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
       DPRINT("  File offset %I64x\n", FileOffset.QuadPart);
 
       /* Write hive block */
-      Status = NtWriteFile(FileHandle,
+      Status = ZwWriteFile(FileHandle,
                           NULL,
                           NULL,
                           NULL,
@@ -1856,22 +1827,22 @@ CmiStartHiveUpdate(PREGISTRY_HIVE RegistryHive)
                           NULL);
       if (!NT_SUCCESS(Status))
        {
-         DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
-         NtClose(FileHandle);
+         DPRINT("ZwWriteFile() failed (Status %lx)\n", Status);
+         ZwClose(FileHandle);
          return(Status);
        }
 
       BlockIndex++;
     }
 
-  Status = NtFlushBuffersFile(FileHandle,
+  Status = ZwFlushBuffersFile(FileHandle,
                              &IoStatusBlock);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtFlushBuffersFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwFlushBuffersFile() failed (Status %lx)\n", Status);
     }
 
-  NtClose(FileHandle);
+  ZwClose(FileHandle);
 
   return(Status);
 }
@@ -1890,11 +1861,11 @@ CmiFinishHiveUpdate(PREGISTRY_HIVE RegistryHive)
 
   InitializeObjectAttributes(&ObjectAttributes,
                             &RegistryHive->HiveFileName,
-                            0,
+                            OBJ_CASE_INSENSITIVE,
                             NULL,
                             NULL);
 
-  Status = NtCreateFile(&FileHandle,
+  Status = ZwCreateFile(&FileHandle,
                        FILE_ALL_ACCESS,
                        &ObjectAttributes,
                        &IoStatusBlock,
@@ -1907,7 +1878,7 @@ CmiFinishHiveUpdate(PREGISTRY_HIVE RegistryHive)
                        0);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtCreateFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwCreateFile() failed (Status %lx)\n", Status);
       return(Status);
     }
 
@@ -1918,7 +1889,7 @@ CmiFinishHiveUpdate(PREGISTRY_HIVE RegistryHive)
 
   /* Write hive block */
   FileOffset.QuadPart = (ULONGLONG)0;
-  Status = NtWriteFile(FileHandle,
+  Status = ZwWriteFile(FileHandle,
                       NULL,
                       NULL,
                       NULL,
@@ -1929,19 +1900,19 @@ CmiFinishHiveUpdate(PREGISTRY_HIVE RegistryHive)
                       NULL);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
-      NtClose(FileHandle);
+      DPRINT("ZwWriteFile() failed (Status %lx)\n", Status);
+      ZwClose(FileHandle);
       return(Status);
     }
 
-  Status = NtFlushBuffersFile(FileHandle,
+  Status = ZwFlushBuffersFile(FileHandle,
                              &IoStatusBlock);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT("NtFlushBuffersFile() failed (Status %lx)\n", Status);
+      DPRINT("ZwFlushBuffersFile() failed (Status %lx)\n", Status);
     }
 
-  NtClose(FileHandle);
+  ZwClose(FileHandle);
 
   return(Status);
 }
@@ -1965,7 +1936,7 @@ CmiFlushRegistryHive(PREGISTRY_HIVE RegistryHive)
         &RegistryHive->LogFileName);
 
   /* Update hive header modification time */
-  NtQuerySystemTime(&RegistryHive->HiveHeader->DateModified);
+  KeQuerySystemTime(&RegistryHive->HiveHeader->DateModified);
 
   /* Start log update */
   Status = CmiStartLogUpdate(RegistryHive);
@@ -2317,7 +2288,7 @@ CmiScanForSubKey(IN PREGISTRY_HIVE RegistryHive,
 
   DPRINT("Scanning for sub key %wZ\n", KeyName);
 
-  assert(RegistryHive);
+  ASSERT(RegistryHive);
 
   *SubKeyCell = NULL;
 
@@ -2462,7 +2433,7 @@ CmiAddSubKey(PREGISTRY_HIVE RegistryHive,
     {
       NewKeyCell->Id = REG_KEY_CELL_ID;
       NewKeyCell->Flags = 0;
-      NtQuerySystemTime(&NewKeyCell->LastWriteTime);
+      KeQuerySystemTime(&NewKeyCell->LastWriteTime);
       NewKeyCell->ParentKeyOffset = -1;
       NewKeyCell->NumberOfSubKeys = 0;
       NewKeyCell->HashTableOffset = -1;
@@ -2578,7 +2549,7 @@ CmiAddSubKey(PREGISTRY_HIVE RegistryHive,
       ParentKeyCell->NumberOfSubKeys++;
     }
 
-  NtQuerySystemTime (&ParentKeyCell->LastWriteTime);
+  KeQuerySystemTime (&ParentKeyCell->LastWriteTime);
   CmiMarkBlockDirty (RegistryHive, ParentKey->KeyCellOffset);
 
   return(Status);
@@ -2611,43 +2582,44 @@ CmiRemoveSubKey(PREGISTRY_HIVE RegistryHive,
          return STATUS_UNSUCCESSFUL;
        }
 
-      if (ValueList != NULL)
+      /* Enumerate all values */
+      for (i = 0; i < SubKey->KeyCell->NumberOfValues; i++)
        {
-         /* Enumerate all values */
-         for (i = 0; i < SubKey->KeyCell->NumberOfValues; i++)
+         /* Get pointer to value cell */
+         ValueCell = CmiGetCell(RegistryHive,
+                                ValueList->ValueOffset[i],
+                                NULL);
+         if (ValueCell == NULL)
            {
-             /* Get pointer to value cell */
-             ValueCell = CmiGetCell (RegistryHive,
-                                     ValueList->ValueOffset[i],
-                                     NULL);
-             if (ValueCell != NULL)
+             DPRINT("CmiGetCell() failed\n");
+             return STATUS_UNSUCCESSFUL;
+           }
+
+         if (!(ValueCell->DataSize & REG_DATA_IN_OFFSET)
+              && ValueCell->DataSize > sizeof(BLOCK_OFFSET))
+           {
+             DataCell = CmiGetCell (RegistryHive,
+                                    ValueCell->DataOffset,
+                                    NULL);
+             if (DataCell == NULL)
                {
-                 if (ValueCell->DataSize > sizeof(BLOCK_OFFSET))
-                   {
-                     DataCell = CmiGetCell (RegistryHive,
-                                            ValueCell->DataOffset,
-                                            NULL);
-                     if (DataCell == NULL)
-                       {
-                         DPRINT("CmiGetCell() failed\n");
-                         return STATUS_UNSUCCESSFUL;
-                       }
-
-                     if (DataCell != NULL)
-                       {
-                         /* Destroy data cell */
-                         CmiDestroyCell (RegistryHive,
-                                         DataCell,
-                                         ValueCell->DataOffset);
-                       }
-                   }
-
-                 /* Destroy value cell */
+                 DPRINT("CmiGetCell() failed\n");
+                 return STATUS_UNSUCCESSFUL;
+               }
+
+             if (DataCell != NULL)
+               {
+                 /* Destroy data cell */
                  CmiDestroyCell (RegistryHive,
-                                 ValueCell,
-                                 ValueList->ValueOffset[i]);
+                                 DataCell,
+                                 ValueCell->DataOffset);
                }
            }
+
+         /* Destroy value cell */
+         CmiDestroyCell (RegistryHive,
+                         ValueCell,
+                         ValueList->ValueOffset[i]);
        }
 
       /* Destroy value list cell */
@@ -2657,6 +2629,9 @@ CmiRemoveSubKey(PREGISTRY_HIVE RegistryHive,
 
       SubKey->KeyCell->NumberOfValues = 0;
       SubKey->KeyCell->ValueListOffset = (BLOCK_OFFSET)-1;
+
+      CmiMarkBlockDirty(RegistryHive,
+                       SubKey->KeyCellOffset);
     }
 
   /* Remove the key from the parent key's hash block */
@@ -2732,7 +2707,7 @@ CmiRemoveSubKey(PREGISTRY_HIVE RegistryHive,
            }
        }
 
-      NtQuerySystemTime(&ParentKey->KeyCell->LastWriteTime);
+      KeQuerySystemTime(&ParentKey->KeyCell->LastWriteTime);
       CmiMarkBlockDirty(ParentKey->RegistryHive,
                        ParentKey->KeyCellOffset);
     }
@@ -2744,7 +2719,9 @@ CmiRemoveSubKey(PREGISTRY_HIVE RegistryHive,
   SubKey->KeyCell = NULL;
   SubKey->KeyCellOffset = (BLOCK_OFFSET)-1;
 
-  return(STATUS_SUCCESS);
+  DPRINT("CmiRemoveSubKey() done\n");
+
+  return STATUS_SUCCESS;
 }
 
 
@@ -2891,11 +2868,14 @@ CmiAddValueToKey(IN PREGISTRY_HIVE RegistryHive,
       CmiMarkBlockDirty(RegistryHive, KeyCellOffset);
       CmiMarkBlockDirty(RegistryHive, ValueListCellOffset);
     }
-  else if (KeyCell->NumberOfValues >= 
+  else if (KeyCell->NumberOfValues >=
           (((ULONG)ABS_VALUE(ValueListCell->CellSize) - sizeof(VALUE_LIST_CELL)) / sizeof(BLOCK_OFFSET)))
     {
+#if 0
       CellSize = sizeof(VALUE_LIST_CELL) +
                 ((KeyCell->NumberOfValues + REG_VALUE_LIST_CELL_MULTIPLE) * sizeof(BLOCK_OFFSET));
+#endif
+      CellSize = 2 * (ULONG)ABS_VALUE(ValueListCell->CellSize);
       Status = CmiAllocateCell (RegistryHive,
                                CellSize,
                                (PVOID) &NewValueListCell,
@@ -2954,13 +2934,13 @@ CmiDeleteValueFromKey(IN PREGISTRY_HIVE RegistryHive,
 {
   PVALUE_LIST_CELL ValueListCell;
   PVALUE_CELL CurValueCell;
-  ULONG  i;
+  ULONG i;
+  NTSTATUS Status;
 
   ValueListCell = CmiGetCell (RegistryHive, KeyCell->ValueListOffset, NULL);
-
   if (ValueListCell == NULL)
     {
-      DPRINT("CmiGetBlock() failed\n");
+      DPRINT1("CmiGetBlock() failed\n");
       return STATUS_SUCCESS;
     }
 
@@ -2971,50 +2951,58 @@ CmiDeleteValueFromKey(IN PREGISTRY_HIVE RegistryHive,
       CurValueCell = CmiGetCell (RegistryHive, ValueListCell->ValueOffset[i], NULL);
       if (CurValueCell == NULL)
        {
-         DPRINT("CmiGetBlock() failed\n");
+         DPRINT1("CmiGetBlock() failed\n");
          return STATUS_UNSUCCESSFUL;
        }
 
-      if ((CurValueCell != NULL) &&
-         CmiComparePackedNames(ValueName,
+      if (CmiComparePackedNames(ValueName,
                                CurValueCell->Name,
                                CurValueCell->NameSize,
                                (BOOLEAN)((CurValueCell->Flags & REG_VALUE_NAME_PACKED) ? TRUE : FALSE)))
        {
-         CmiDestroyValueCell(RegistryHive, CurValueCell, ValueListCell->ValueOffset[i]);
+         Status = CmiDestroyValueCell(RegistryHive,
+                                      CurValueCell,
+                                      ValueListCell->ValueOffset[i]);
+         if (CurValueCell == NULL)
+           {
+             DPRINT1("CmiDestroyValueCell() failed\n");
+             return Status;
+           }
 
-         if ((KeyCell->NumberOfValues - 1) < i)
+         if (i < (KeyCell->NumberOfValues - 1))
            {
-             RtlCopyMemory(&ValueListCell->ValueOffset[i],
+             RtlMoveMemory(&ValueListCell->ValueOffset[i],
                            &ValueListCell->ValueOffset[i + 1],
                            sizeof(BLOCK_OFFSET) * (KeyCell->NumberOfValues - 1 - i));
            }
+         ValueListCell->ValueOffset[KeyCell->NumberOfValues - 1] = 0;
+
+
+         KeyCell->NumberOfValues--;
+
+         if (KeyCell->NumberOfValues == 0)
+           {
+             CmiDestroyCell(RegistryHive,
+                            ValueListCell,
+                            KeyCell->ValueListOffset);
+             KeyCell->ValueListOffset = -1;
+           }
          else
            {
-             RtlZeroMemory(&ValueListCell->ValueOffset[i], sizeof(BLOCK_OFFSET));
+             CmiMarkBlockDirty(RegistryHive,
+                               KeyCell->ValueListOffset);
            }
 
-         KeyCell->NumberOfValues -= 1;
-         break;
-       }
-    }
+         CmiMarkBlockDirty(RegistryHive,
+                           KeyCellOffset);
 
-  if (KeyCell->NumberOfValues == 0)
-    {
-      CmiDestroyCell (RegistryHive,
-                     ValueListCell,
-                     KeyCell->ValueListOffset);
-    }
-  else
-    {
-      CmiMarkBlockDirty(RegistryHive,
-                       KeyCell->ValueListOffset);
+         return STATUS_SUCCESS;
+       }
     }
 
-  CmiMarkBlockDirty(RegistryHive,
-                   KeyCellOffset);
+  DPRINT("Couldn't find the desired value\n");
 
-  return STATUS_SUCCESS;
+  return STATUS_OBJECT_NAME_NOT_FOUND;
 }
 
 
@@ -3030,7 +3018,7 @@ CmiAllocateHashTableCell (IN PREGISTRY_HIVE RegistryHive,
 
   Status = STATUS_SUCCESS;
   *HashBlock = NULL;
-  NewHashSize = sizeof(HASH_TABLE_CELL) + 
+  NewHashSize = sizeof(HASH_TABLE_CELL) +
                (SubKeyCount * sizeof(HASH_RECORD));
   Status = CmiAllocateCell (RegistryHive,
                            NewHashSize,
@@ -3043,7 +3031,7 @@ CmiAllocateHashTableCell (IN PREGISTRY_HIVE RegistryHive,
     }
   else
     {
-      assert(SubKeyCount <= 0xffff); /* should really be USHORT_MAX or similar */
+      ASSERT(SubKeyCount <= 0xffff); /* should really be USHORT_MAX or similar */
       NewHashBlock->Id = REG_HASH_TABLE_CELL_ID;
       NewHashBlock->HashTableSize = (USHORT)SubKeyCount;
       *HashBlock = NewHashBlock;
@@ -3158,7 +3146,7 @@ CmiAllocateValueCell(PREGISTRY_HIVE RegistryHive,
     }
   else
     {
-      assert(NameSize <= 0xffff); /* should really be USHORT_MAX or similar */
+      ASSERT(NameSize <= 0xffff); /* should really be USHORT_MAX or similar */
       NewValueCell->Id = REG_VALUE_CELL_ID;
       NewValueCell->NameSize = (USHORT)NameSize;
       if (Packable)
@@ -3189,44 +3177,46 @@ CmiAllocateValueCell(PREGISTRY_HIVE RegistryHive,
 NTSTATUS
 CmiDestroyValueCell(PREGISTRY_HIVE RegistryHive,
                    PVALUE_CELL ValueCell,
-                   BLOCK_OFFSET VBOffset)
+                   BLOCK_OFFSET ValueCellOffset)
 {
   NTSTATUS Status;
-  PVOID pBlock;
-  PHBIN pBin;
+  PVOID DataCell;
+  PHBIN Bin;
 
-  DPRINT("CmiDestroyValueCell(Cell %p  Offset %lx)\n", ValueCell, VBOffset);
+  DPRINT("CmiDestroyValueCell(Cell %p  Offset %lx)\n",
+        ValueCell, ValueCellOffset);
 
   VERIFY_VALUE_CELL(ValueCell);
 
   /* Destroy the data cell */
-  if (ValueCell->DataSize > sizeof(BLOCK_OFFSET))
+  if (!(ValueCell->DataSize & REG_DATA_IN_OFFSET)
+      && ValueCell->DataSize > sizeof(BLOCK_OFFSET))
     {
-      pBlock = CmiGetCell (RegistryHive, ValueCell->DataOffset, &pBin);
-      if (pBlock == NULL)
+      DataCell = CmiGetCell (RegistryHive, ValueCell->DataOffset, &Bin);
+      if (DataCell == NULL)
        {
-         DPRINT("CmiGetBlock() failed\n");
+         DPRINT("CmiGetCell() failed\n");
          return STATUS_UNSUCCESSFUL;
        }
 
-      Status = CmiDestroyCell (RegistryHive, pBlock, ValueCell->DataOffset);
+      Status = CmiDestroyCell (RegistryHive, DataCell, ValueCell->DataOffset);
       if (!NT_SUCCESS(Status))
        {
-         return  Status;
+         return Status;
        }
 
       /* Update time of heap */
       if (!IsNoFileHive(RegistryHive))
-       NtQuerySystemTime(&pBin->DateModified);
+       KeQuerySystemTime(&Bin->DateModified);
     }
 
   /* Destroy the value cell */
-  Status = CmiDestroyCell (RegistryHive, ValueCell, VBOffset);
+  Status = CmiDestroyCell (RegistryHive, ValueCell, ValueCellOffset);
 
   /* Update time of heap */
-  if (!IsNoFileHive(RegistryHive) && CmiGetCell (RegistryHive, VBOffset, &pBin))
+  if (!IsNoFileHive(RegistryHive) && CmiGetCell (RegistryHive, ValueCellOffset, &Bin))
     {
-      NtQuerySystemTime(&pBin->DateModified);
+      KeQuerySystemTime(&Bin->DateModified);
     }
 
   return Status;
@@ -3244,6 +3234,7 @@ CmiAddBin(PREGISTRY_HIVE RegistryHive,
   PHBIN tmpBin;
   ULONG BinSize;
   ULONG i;
+  ULONG BitmapSize;
 
   DPRINT ("CmiAddBin (BlockCount %lu)\n", BlockCount);
 
@@ -3260,9 +3251,8 @@ CmiAddBin(PREGISTRY_HIVE RegistryHive,
   tmpBin->BinOffset = RegistryHive->FileSize - REG_BLOCK_SIZE;
   RegistryHive->FileSize += BinSize;
   tmpBin->BinSize = BinSize;
-  tmpBin->Unused1 = 0;
-  NtQuerySystemTime(&tmpBin->DateModified);
-  tmpBin->Unused2 = 0;
+  KeQuerySystemTime(&tmpBin->DateModified);
+  tmpBin->MemAlloc = 0;
 
   DPRINT ("  BinOffset %lx  BinSize %lx\n", tmpBin->BinOffset,tmpBin->BinSize);
 
@@ -3296,17 +3286,17 @@ CmiAddBin(PREGISTRY_HIVE RegistryHive,
   tmpBlock = (PCELL_HEADER)((ULONG_PTR) tmpBin + REG_HBIN_DATA_OFFSET);
   tmpBlock->CellSize = (BinSize - REG_HBIN_DATA_OFFSET);
 
+  /* Calculate bitmap size in bytes (always a multiple of 32 bits) */
+  BitmapSize = ROUND_UP(RegistryHive->BlockListSize, sizeof(ULONG) * 8) / 8;
+
   /* Grow bitmap if necessary */
-  if (IsNoFileHive(RegistryHive) &&
-      (RegistryHive->BlockListSize % (sizeof(ULONG) * 8) == 0))
+  if (!IsNoFileHive(RegistryHive) &&
+      BitmapSize > RegistryHive->DirtyBitMap.SizeOfBitMap / 8)
     {
       PULONG BitmapBuffer;
-      ULONG BitmapSize;
 
       DPRINT("Grow hive bitmap\n");
 
-      /* Calculate bitmap size in bytes (always a multiple of 32 bits) */
-      BitmapSize = ROUND_UP(RegistryHive->BlockListSize, sizeof(ULONG) * 8) / 8;
       DPRINT("RegistryHive->BlockListSize: %lu\n", RegistryHive->BlockListSize);
       DPRINT("BitmapSize:  %lu Bytes  %lu Bits\n", BitmapSize, BitmapSize * 8);
       BitmapBuffer = (PULONG)ExAllocatePool(PagedPool,
@@ -3314,7 +3304,7 @@ CmiAddBin(PREGISTRY_HIVE RegistryHive,
       RtlZeroMemory(BitmapBuffer, BitmapSize);
       RtlCopyMemory(BitmapBuffer,
                    RegistryHive->DirtyBitMap.Buffer,
-                   RegistryHive->DirtyBitMap.SizeOfBitMap);
+                   RegistryHive->DirtyBitMap.SizeOfBitMap / 8);
       ExFreePool(RegistryHive->BitmapBuffer);
       RegistryHive->BitmapBuffer = BitmapBuffer;
       RtlInitializeBitMap(&RegistryHive->DirtyBitMap,
@@ -3389,7 +3379,7 @@ CmiAllocateCell (PREGISTRY_HIVE RegistryHive,
                  return STATUS_UNSUCCESSFUL;
                }
 
-             NtQuerySystemTime(&Bin->DateModified);
+             KeQuerySystemTime(&Bin->DateModified);
              CmiMarkBlockDirty(RegistryHive, RegistryHive->FreeListOffset[i]);
 
              if ((i + 1) < RegistryHive->FreeListSize)
@@ -3478,7 +3468,7 @@ CmiDestroyCell (PREGISTRY_HIVE RegistryHive,
 
       /* Update time of heap */
       if (!IsNoFileHive(RegistryHive) && CmiGetCell (RegistryHive, CellOffset,&pBin))
-       NtQuerySystemTime(&pBin->DateModified);
+       KeQuerySystemTime(&pBin->DateModified);
 
       CmiMarkBlockDirty(RegistryHive, CellOffset);
     }
@@ -3640,8 +3630,8 @@ CmiAddFree(PREGISTRY_HIVE RegistryHive,
   LONG maxInd;
   LONG medInd;
 
-  assert(RegistryHive);
-  assert(FreeBlock);
+  ASSERT(RegistryHive);
+  ASSERT(FreeBlock);
 
   DPRINT("FreeBlock %.08lx  FreeOffset %.08lx\n",
         FreeBlock, FreeOffset);
@@ -3832,7 +3822,7 @@ CmiGetPackedNameLength(IN PUNICODE_STRING Name,
 
 BOOLEAN
 CmiComparePackedNames(IN PUNICODE_STRING Name,
-                     IN PCHAR NameBuffer,
+                     IN PUCHAR NameBuffer,
                      IN USHORT NameBufferSize,
                      IN BOOLEAN NamePacked)
 {
@@ -3870,7 +3860,7 @@ CmiComparePackedNames(IN PUNICODE_STRING Name,
 
 VOID
 CmiCopyPackedName(PWCHAR NameBuffer,
-                 PCHAR PackedNameBuffer,
+                 PUCHAR PackedNameBuffer,
                  ULONG PackedNameSize)
 {
   ULONG i;
@@ -4074,6 +4064,10 @@ CmiCopyKey (PREGISTRY_HIVE DstHive,
        }
       NewKeyCell->HashTableOffset = NewHashTableOffset;
     }
+  else
+    {
+      NewHashTableCell = NULL;
+    }
 
   /* Allocate and copy value list and values */
   if (SrcKeyCell->NumberOfValues != 0)
@@ -4254,7 +4248,7 @@ CmiSaveTempHive (PREGISTRY_HIVE Hive,
 
   /* Write hive block */
   FileOffset.QuadPart = (ULONGLONG)0;
-  Status = NtWriteFile (FileHandle,
+  Status = ZwWriteFile (FileHandle,
                        NULL,
                        NULL,
                        NULL,
@@ -4265,7 +4259,7 @@ CmiSaveTempHive (PREGISTRY_HIVE Hive,
                        NULL);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT1 ("NtWriteFile() failed (Status %lx)\n", Status);
+      DPRINT1 ("ZwWriteFile() failed (Status %lx)\n", Status);
       return Status;
     }
 
@@ -4279,7 +4273,7 @@ CmiSaveTempHive (PREGISTRY_HIVE Hive,
       DPRINT ("File offset %I64x\n", FileOffset.QuadPart);
 
       /* Write hive block */
-      Status = NtWriteFile (FileHandle,
+      Status = ZwWriteFile (FileHandle,
                            NULL,
                            NULL,
                            NULL,
@@ -4290,16 +4284,16 @@ CmiSaveTempHive (PREGISTRY_HIVE Hive,
                            NULL);
       if (!NT_SUCCESS(Status))
        {
-         DPRINT1 ("NtWriteFile() failed (Status %lx)\n", Status);
+         DPRINT1 ("ZwWriteFile() failed (Status %lx)\n", Status);
          return Status;
        }
     }
 
-  Status = NtFlushBuffersFile (FileHandle,
+  Status = ZwFlushBuffersFile (FileHandle,
                               &IoStatusBlock);
   if (!NT_SUCCESS(Status))
     {
-      DPRINT1 ("NtFlushBuffersFile() failed (Status %lx)\n", Status);
+      DPRINT1 ("ZwFlushBuffersFile() failed (Status %lx)\n", Status);
     }
 
   DPRINT ("CmiSaveTempHive() done\n");