PUCHAR Buffer;
PUCHAR Ptr;
ULONG BlockIndex;
- ULONG BlockOffset;
PVOID BlockPtr;
NTSTATUS Status;
BlockIndex = RtlFindSetBits(&RegistryHive->DirtyBitMap,
1,
BlockIndex);
- if (BlockIndex == (ULONG)-1)
+ if ((BlockIndex == (ULONG)-1) ||
+ (BlockIndex >= RegistryHive->BlockListSize))
{
DPRINT("No more set bits\n");
Status = STATUS_SUCCESS;
DPRINT("Block %lu is dirty\n", BlockIndex);
- BlockOffset = RegistryHive->BlockList[BlockIndex]->BlockOffset;
- DPRINT("Block offset %lx\n", BlockOffset);
-
- BlockPtr = RegistryHive->BlockList[BlockIndex] + ((BlockIndex * 4096) - BlockOffset);
+ BlockPtr = RegistryHive->BlockList[BlockIndex];
DPRINT("BlockPtr %p\n", BlockPtr);
-
DPRINT("File offset %I64x\n", FileOffset.QuadPart);
/* Write hive block */
NULL);
if (!NT_SUCCESS(Status))
{
- DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
+ DPRINT1("NtWriteFile() failed (Status %lx)\n", Status);
NtClose(FileHandle);
return(Status);
}
ULONG BitmapSize;
NTSTATUS Status;
- DPRINT("CmiFinishLogUpdate() called\n");
+ DPRINT("CmiCleanupLogUpdate() called\n");
BitmapSize = ROUND_UP(RegistryHive->BlockListSize, sizeof(ULONG) * 8) / 8;
BufferSize = sizeof(HIVE_HEADER) +
HANDLE FileHandle;
LARGE_INTEGER FileOffset;
ULONG BlockIndex;
- ULONG BlockOffset;
PVOID BlockPtr;
NTSTATUS Status;
BlockIndex = RtlFindSetBits(&RegistryHive->DirtyBitMap,
1,
BlockIndex);
- if (BlockIndex == (ULONG)-1)
+ if ((BlockIndex == (ULONG)-1) ||
+ (BlockIndex >= RegistryHive->BlockListSize))
{
DPRINT("No more set bits\n");
Status = STATUS_SUCCESS;
DPRINT("Block %lu is dirty\n", BlockIndex);
- BlockOffset = RegistryHive->BlockList[BlockIndex]->BlockOffset;
- DPRINT("Block offset %lx\n", BlockOffset);
-
- BlockPtr = RegistryHive->BlockList[BlockIndex] + ((BlockIndex * 4096) - BlockOffset);
+ BlockPtr = RegistryHive->BlockList[BlockIndex];
DPRINT("BlockPtr %p\n", BlockPtr);
FileOffset.QuadPart = (ULONGLONG)(BlockIndex + 1) * 4096ULL;
DPRINT("File offset %I64x\n", FileOffset.QuadPart);
-
/* Write hive block */
Status = NtWriteFile(FileHandle,
NULL,
HashBlock = CmiGetBlock(RegistryHive, KeyCell->HashTableOffset, NULL);
if (HashBlock == NULL)
{
+ DPRINT("CmiGetBlock() failed\n");
return 0;
}
CurSubKeyCell = CmiGetBlock(RegistryHive,
HashBlock->Table[i].KeyOffset,
NULL);
+ if (CurSubKeyCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return 0;
+ }
+
if (MaxName < CurSubKeyCell->NameSize)
{
MaxName = CurSubKeyCell->NameSize;
HashBlock = CmiGetBlock(RegistryHive, KeyCell->HashTableOffset, NULL);
if (HashBlock == NULL)
{
+ DPRINT("CmiGetBlock() failed\n");
return 0;
}
CurSubKeyCell = CmiGetBlock(RegistryHive,
HashBlock->Table[i].KeyOffset,
NULL);
+ if (CurSubKeyCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return 0;
+ }
+
if (MaxClass < CurSubKeyCell->ClassSize)
{
MaxClass = CurSubKeyCell->ClassSize;
VERIFY_KEY_CELL(KeyCell);
+ MaxValueName = 0;
ValueListCell = CmiGetBlock(RegistryHive,
KeyCell->ValuesOffset,
NULL);
- MaxValueName = 0;
if (ValueListCell == NULL)
{
+ DPRINT("CmiGetBlock() failed\n");
return 0;
}
for (i = 0; i < KeyCell->NumberOfValues; i++)
{
- CurValueCell = CmiGetBlock(RegistryHive,
- ValueListCell->Values[i],
- NULL);
+ CurValueCell = CmiGetBlock (RegistryHive,
+ ValueListCell->Values[i],
+ NULL);
+ if (CurValueCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ }
+
if (CurValueCell != NULL &&
MaxValueName < CurValueCell->NameSize)
{
VERIFY_KEY_CELL(KeyCell);
- ValueListCell = CmiGetBlock(RegistryHive, KeyCell->ValuesOffset, NULL);
MaxValueData = 0;
+ ValueListCell = CmiGetBlock(RegistryHive, KeyCell->ValuesOffset, NULL);
if (ValueListCell == NULL)
{
return 0;
{
PHASH_TABLE_CELL HashBlock;
PKEY_CELL CurSubKeyCell;
- WORD KeyLength;
+ USHORT KeyLength;
ULONG i;
VERIFY_KEY_CELL(KeyCell);
assert(RegistryHive);
*SubKeyCell = NULL;
- KeyLength = strlen(KeyName);
+ /* The key does not have any subkeys */
+ if (KeyCell->HashTableOffset == (BLOCK_OFFSET)-1)
+ {
+ return STATUS_SUCCESS;
+ }
+
+ /* Get hash table */
HashBlock = CmiGetBlock(RegistryHive, KeyCell->HashTableOffset, NULL);
if (HashBlock == NULL)
{
- return STATUS_SUCCESS;
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
}
- for (i = 0; (i < KeyCell->NumberOfSubKeys)
- && (i < HashBlock->HashTableSize); i++)
+ KeyLength = strlen(KeyName);
+ for (i = 0; (i < KeyCell->NumberOfSubKeys) && (i < HashBlock->HashTableSize); i++)
{
if (Attributes & OBJ_CASE_INSENSITIVE)
- {
- if ((HashBlock->Table[i].KeyOffset != 0) &&
- (HashBlock->Table[i].KeyOffset != (ULONG_PTR)-1) &&
- (_strnicmp(KeyName, (PCHAR) &HashBlock->Table[i].HashValue, 4) == 0))
- {
- CurSubKeyCell = CmiGetBlock(RegistryHive,
- HashBlock->Table[i].KeyOffset,
- NULL);
+ {
+ if ((HashBlock->Table[i].KeyOffset != 0) &&
+ (HashBlock->Table[i].KeyOffset != (ULONG_PTR)-1) &&
+ (_strnicmp(KeyName, (PCHAR) &HashBlock->Table[i].HashValue, 4) == 0))
+ {
+ CurSubKeyCell = CmiGetBlock(RegistryHive,
+ HashBlock->Table[i].KeyOffset,
+ NULL);
+ if (CurSubKeyCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
if ((CurSubKeyCell->NameSize == KeyLength)
&& (_strnicmp(KeyName, CurSubKeyCell->Name, KeyLength) == 0))
{
break;
}
}
- }
+ }
else
- {
- if (HashBlock->Table[i].KeyOffset != 0 &&
- HashBlock->Table[i].KeyOffset != (ULONG_PTR) -1 &&
- !strncmp(KeyName, (PCHAR) &HashBlock->Table[i].HashValue, 4))
- {
- CurSubKeyCell = CmiGetBlock(RegistryHive,
- HashBlock->Table[i].KeyOffset,NULL);
+ {
+ if (HashBlock->Table[i].KeyOffset != 0 &&
+ HashBlock->Table[i].KeyOffset != (ULONG_PTR) -1 &&
+ !strncmp(KeyName, (PCHAR) &HashBlock->Table[i].HashValue, 4))
+ {
+ CurSubKeyCell = CmiGetBlock(RegistryHive,
+ HashBlock->Table[i].KeyOffset,
+ NULL);
+ if (CurSubKeyCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
if (CurSubKeyCell->NameSize == KeyLength
&& !_strnicmp(KeyName, CurSubKeyCell->Name, KeyLength))
{
break;
}
}
- }
+ }
}
return STATUS_SUCCESS;
NewBlockSize = sizeof(KEY_CELL) + NameSize;
Status = CmiAllocateBlock(RegistryHive,
- (PVOID) &NewKeyCell,
- NewBlockSize,
- &NKBOffset);
-
+ (PVOID) &NewKeyCell,
+ NewBlockSize,
+ &NKBOffset);
if (NewKeyCell == NULL)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
else
{
HashBlock = CmiGetBlock(RegistryHive, KeyCell->HashTableOffset, NULL);
+ if (HashBlock == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
if (((KeyCell->NumberOfSubKeys + 1) >= HashBlock->HashTableSize))
{
BLOCK_OFFSET HTOffset;
ValueList = CmiGetBlock(RegistryHive,
SubKey->KeyCell->ValuesOffset,
NULL);
+ if (ValueList == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
if (ValueList != NULL)
{
/* Enumerate all values */
DataCell = CmiGetBlock(RegistryHive,
ValueCell->DataOffset,
NULL);
+ if (DataCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
if (DataCell != NULL)
{
/* Destroy data cell */
HashBlock = CmiGetBlock(RegistryHive,
ParentKey->KeyCell->HashTableOffset,
NULL);
+ if (HashBlock == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
DPRINT("ParentKey HashBlock %p\n", HashBlock)
if (HashBlock != NULL)
{
HashBlock = CmiGetBlock(RegistryHive,
SubKey->KeyCell->HashTableOffset,
NULL);
+ if (HashBlock == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
DPRINT("SubKey HashBlock %p\n", HashBlock)
if (HashBlock != NULL)
{
HashBlock = CmiGetBlock(RegistryHive,
ParentKey->KeyCell->HashTableOffset,
NULL);
+ if (HashBlock == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
DPRINT("ParentKey HashBlock %p\n", HashBlock)
if (HashBlock != NULL)
{
*ValueCell = NULL;
+ /* The key does not have any values */
+ if (KeyCell->ValuesOffset == (BLOCK_OFFSET)-1)
+ {
+ return STATUS_SUCCESS;
+ }
+
ValueListCell = CmiGetBlock(RegistryHive, KeyCell->ValuesOffset, NULL);
if (ValueListCell == NULL)
{
DPRINT("ValueListCell is NULL\n");
- return STATUS_SUCCESS;
+ return STATUS_UNSUCCESSFUL;
}
VERIFY_VALUE_LIST_CELL(ValueListCell);
CurValueCell = CmiGetBlock(RegistryHive,
ValueListCell->Values[i],
NULL);
+ if (CurValueCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
if ((CurValueCell != NULL) &&
CmiComparePackedNames(ValueName,
*ValueCell = NULL;
- ValueListCell = CmiGetBlock(RegistryHive, KeyCell->ValuesOffset, NULL);
- if (ValueListCell == NULL)
+ if (KeyCell->ValuesOffset == (BLOCK_OFFSET)-1)
{
return STATUS_NO_MORE_ENTRIES;
}
- VERIFY_VALUE_LIST_CELL(ValueListCell);
-
if (Index >= KeyCell->NumberOfValues)
{
return STATUS_NO_MORE_ENTRIES;
}
+
+ ValueListCell = CmiGetBlock(RegistryHive, KeyCell->ValuesOffset, NULL);
+ if (ValueListCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
+ VERIFY_VALUE_LIST_CELL(ValueListCell);
+
+
CurValueCell = CmiGetBlock(RegistryHive,
ValueListCell->Values[Index],
NULL);
- if (CurValueCell != NULL)
+ if (CurValueCell == NULL)
{
- *ValueCell = CurValueCell;
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
}
+ *ValueCell = CurValueCell;
+
return STATUS_SUCCESS;
}
return Status;
}
+ DPRINT("KeyCell->ValuesOffset %lu\n", (ULONG)KeyCell->ValuesOffset);
+
ValueListCell = CmiGetBlock(RegistryHive, KeyCell->ValuesOffset, NULL);
if (ValueListCell == NULL)
if (ValueListCell == NULL)
{
+ DPRINT("CmiGetBlock() failed\n");
return STATUS_SUCCESS;
}
for (i = 0; i < KeyCell->NumberOfValues; i++)
{
CurValueCell = CmiGetBlock(RegistryHive, ValueListCell->Values[i], NULL);
+ if (CurValueCell == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
if ((CurValueCell != NULL) &&
CmiComparePackedNames(ValueName,
PKEY_CELL
CmiGetKeyFromHashByIndex(PREGISTRY_HIVE RegistryHive,
- PHASH_TABLE_CELL HashBlock,
- ULONG Index)
+ PHASH_TABLE_CELL HashBlock,
+ ULONG Index)
{
BLOCK_OFFSET KeyOffset;
PKEY_CELL KeyCell;
NTSTATUS
CmiAddKeyToHashTable(PREGISTRY_HIVE RegistryHive,
- PHASH_TABLE_CELL HashBlock,
- PKEY_CELL NewKeyCell,
- BLOCK_OFFSET NKBOffset)
+ PHASH_TABLE_CELL HashBlock,
+ PKEY_CELL NewKeyCell,
+ BLOCK_OFFSET NKBOffset)
{
ULONG i;
NTSTATUS
CmiAllocateValueCell(PREGISTRY_HIVE RegistryHive,
- PVALUE_CELL *ValueCell,
- BLOCK_OFFSET *VBOffset,
- IN PUNICODE_STRING ValueName)
+ PVALUE_CELL *ValueCell,
+ BLOCK_OFFSET *VBOffset,
+ IN PUNICODE_STRING ValueName)
{
PVALUE_CELL NewValueCell;
NTSTATUS Status;
if (ValueCell->DataSize > 4)
{
pBlock = CmiGetBlock(RegistryHive, ValueCell->DataOffset, &pBin);
+ if (pBlock == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
+
Status = CmiDestroyBlock(RegistryHive, pBlock, ValueCell->DataOffset);
if (!NT_SUCCESS(Status))
{
if (RegistryHive->BlockListSize > 0)
{
- memcpy(tmpBlockList,
- RegistryHive->BlockList,
- sizeof(PHBIN *)*(RegistryHive->BlockListSize));
+ RtlCopyMemory (tmpBlockList,
+ RegistryHive->BlockList,
+ sizeof(PHBIN *)*(RegistryHive->BlockListSize));
ExFreePool(RegistryHive->BlockList);
}
RegistryHive->BlockList = tmpBlockList;
- RegistryHive->BlockList[RegistryHive->BlockListSize++] = tmpBin;
+ RegistryHive->BlockList[RegistryHive->BlockListSize] = tmpBin;
+ RegistryHive->BlockListSize++;
/* Initialize a free block in this heap : */
tmpBlock = (PCELL_HEADER)((ULONG_PTR) tmpBin + REG_HBIN_DATA_OFFSET);
PULONG BitmapBuffer;
ULONG BitmapSize;
- DPRINT1("Grow hive bitmap\n");
+ 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;
- DPRINT1("RegistryHive->BlockListSize: %lu\n", RegistryHive->BlockListSize);
- DPRINT1("BitmapSize: %lu Bytes %lu Bits\n", BitmapSize, BitmapSize * 8);
+ DPRINT("RegistryHive->BlockListSize: %lu\n", RegistryHive->BlockListSize);
+ DPRINT("BitmapSize: %lu Bytes %lu Bits\n", BitmapSize, BitmapSize * 8);
BitmapBuffer = (PULONG)ExAllocatePool(PagedPool,
BitmapSize);
RtlZeroMemory(BitmapBuffer, BitmapSize);
PCELL_HEADER NewBlock;
NTSTATUS Status;
PHBIN pBin;
+ ULONG i;
+ PVOID Temp;
Status = STATUS_SUCCESS;
}
else
{
- ULONG i;
-
/* first search in free blocks */
NewBlock = NULL;
for (i = 0; i < RegistryHive->FreeListSize; i++)
{
if (RegistryHive->FreeList[i]->CellSize >= BlockSize)
{
- PVOID Temp;
-
NewBlock = RegistryHive->FreeList[i];
if (pBlockOffset)
*pBlockOffset = RegistryHive->FreeListOffset[i];
/* Update time of heap */
Temp = CmiGetBlock(RegistryHive, RegistryHive->FreeListOffset[i], &pBin);
+ if (Temp == NULL)
+ {
+ DPRINT("CmiGetBlock() failed\n");
+ return STATUS_UNSUCCESSFUL;
+ }
if (Temp)
{
if ((i + 2) < RegistryHive->FreeListSize)
{
+ RtlMoveMemory(&RegistryHive->FreeList[i + 1],
+ &RegistryHive->FreeList[i + 2],
+ sizeof(RegistryHive->FreeList[0])
+ * (RegistryHive->FreeListSize - i - 2));
RtlMoveMemory(&RegistryHive->FreeListOffset[i + 1],
&RegistryHive->FreeListOffset[i + 2],
sizeof(RegistryHive->FreeListOffset[0])
BLOCK_OFFSET BlockOffset,
PHBIN * ppBin)
{
+ PHBIN pBin;
+
if (ppBin)
*ppBin = NULL;
- if ((BlockOffset == 0) || (BlockOffset == (ULONG_PTR) -1))
- return NULL;
+ if (BlockOffset == (BLOCK_OFFSET)-1)
+ {
+ return NULL;
+ }
- if (IsPointerHive(RegistryHive))
+ if (IsPointerHive (RegistryHive))
{
- return (PVOID) BlockOffset;
+ return (PVOID)BlockOffset;
}
else
{
- PHBIN pBin;
-
+ if (BlockOffset > RegistryHive->BlockListSize * 4096)
+ {
+ DPRINT1("BlockOffset exceeds valid range (%lu > %lu)\n",
+ BlockOffset, RegistryHive->BlockListSize * 4096);
+ return NULL;
+ }
pBin = RegistryHive->BlockList[BlockOffset / 4096];
if (ppBin)
*ppBin = pBin;