[SETUPLIB] Some INI support refactoring: function/struct names, duplicated code (#6815)

And convert ANSI strings to UNICODE in a better way instead of
zero-extending them.
This commit is contained in:
Hermès Bélusca-Maïto 2024-04-23 21:09:36 +02:00
parent cb6fc76b8b
commit 817c27a54e
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
4 changed files with 291 additions and 444 deletions

View file

@ -232,7 +232,7 @@ InstallSetupInfFile(
IO_STATUS_BLOCK IoStatusBlock;
#endif
PINICACHESECTION IniSection;
PINI_SECTION IniSection;
WCHAR PathBuffer[MAX_PATH];
WCHAR UnattendInfPath[MAX_PATH];
@ -241,35 +241,31 @@ InstallSetupInfFile(
if (!IniCache)
return;
IniSection = IniCacheAppendSection(IniCache, L"SetupParams");
IniSection = IniAddSection(IniCache, L"SetupParams");
if (IniSection)
{
/* Key "skipmissingfiles" */
// RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
// L"\"%s\"", L"WinNt5.2");
// IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
// L"Version", PathBuffer);
// IniAddKey(IniSection, L"Version", PathBuffer);
}
IniSection = IniCacheAppendSection(IniCache, L"Data");
IniSection = IniAddSection(IniCache, L"Data");
if (IniSection)
{
RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
L"\"%s\"", IsUnattendedSetup ? L"yes" : L"no");
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"UnattendedInstall", PathBuffer);
IniAddKey(IniSection, L"UnattendedInstall", PathBuffer);
// "floppylessbootpath" (yes/no)
RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
L"\"%s\"", L"winnt");
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"ProductType", PathBuffer);
IniAddKey(IniSection, L"ProductType", PathBuffer);
RtlStringCchPrintfW(PathBuffer, ARRAYSIZE(PathBuffer),
L"\"%s\\\"", pSetupData->SourceRootPath.Buffer);
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"SourcePath", PathBuffer);
IniAddKey(IniSection, L"SourcePath", PathBuffer);
// "floppyless" ("0")
}

View file

@ -79,8 +79,8 @@ typedef struct _BOOT_STORE_INI_CONTEXT
PVOID ViewBase;
PINICACHE IniCache;
PINICACHESECTION OptionsIniSection;
PINICACHESECTION OsIniSection;
PINI_SECTION OptionsIniSection;
PINI_SECTION OsIniSection;
} BOOT_STORE_INI_CONTEXT, *PBOOT_STORE_INI_CONTEXT;
// TODO!
@ -208,56 +208,41 @@ static VOID
CreateCommonFreeLdrSections(
IN OUT PBOOT_STORE_INI_CONTEXT BootStore)
{
PINICACHESECTION IniSection;
PINI_SECTION IniSection;
/*
* Cache the "FREELOADER" section for our future usage.
*/
/* Get the "FREELOADER" section */
IniSection = IniCacheGetSection(BootStore->IniCache, L"FREELOADER");
/* Get or create the "FREELOADER" section */
IniSection = IniGetSection(BootStore->IniCache, L"FREELOADER");
if (!IniSection)
{
/* It does not exist yet, so create it */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"FREELOADER");
if (!IniSection)
{
DPRINT1("CreateCommonFreeLdrSections: Failed to create 'FREELOADER' section!\n");
}
}
IniSection = IniAddSection(BootStore->IniCache, L"FREELOADER");
if (!IniSection)
DPRINT1("CreateCommonFreeLdrSections: Failed to create 'FREELOADER' section!\n");
BootStore->OptionsIniSection = IniSection;
/* TimeOut */
IniCacheInsertKey(BootStore->OptionsIniSection, NULL, INSERT_LAST,
L"TimeOut", L"0");
IniAddKey(BootStore->OptionsIniSection, L"TimeOut", L"0");
/* Create "Display" section */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"Display");
IniSection = IniAddSection(BootStore->IniCache, L"Display");
/* TitleText */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"TitleText", L"ReactOS Boot Manager");
/* MinimalUI */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"MinimalUI", L"Yes");
/* TitleText and MinimalUI */
IniAddKey(IniSection, L"TitleText", L"ReactOS Boot Manager");
IniAddKey(IniSection, L"MinimalUI", L"Yes");
/*
* Cache the "Operating Systems" section for our future usage.
*/
/* Get the "Operating Systems" section */
IniSection = IniCacheGetSection(BootStore->IniCache, L"Operating Systems");
/* Get or create the "Operating Systems" section */
IniSection = IniGetSection(BootStore->IniCache, L"Operating Systems");
if (!IniSection)
{
/* It does not exist yet, so create it */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"Operating Systems");
if (!IniSection)
{
DPRINT1("CreateCommonFreeLdrSections: Failed to create 'Operating Systems' section!\n");
}
}
IniSection = IniAddSection(BootStore->IniCache, L"Operating Systems");
if (!IniSection)
DPRINT1("CreateCommonFreeLdrSections: Failed to create 'Operating Systems' section!\n");
BootStore->OsIniSection = IniSection;
}
@ -352,7 +337,7 @@ OpenIniBootLoaderStore(
}
else
{
PINICACHESECTION IniSection;
PINI_SECTION IniSection;
/*
* Check whether the loader configuration INI file exists,
@ -401,17 +386,12 @@ OpenIniBootLoaderStore(
* Cache the "FREELOADER" section for our future usage.
*/
/* Get the "FREELOADER" section */
IniSection = IniCacheGetSection(BootStore->IniCache, L"FREELOADER");
/* Get or create the "FREELOADER" section */
IniSection = IniGetSection(BootStore->IniCache, L"FREELOADER");
if (!IniSection)
{
/* It does not exist yet, so create it */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"FREELOADER");
if (!IniSection)
{
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'FREELOADER' section!\n");
}
}
IniSection = IniAddSection(BootStore->IniCache, L"FREELOADER");
if (!IniSection)
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'FREELOADER' section!\n");
BootStore->OptionsIniSection = IniSection;
@ -419,17 +399,12 @@ OpenIniBootLoaderStore(
* Cache the "Operating Systems" section for our future usage.
*/
/* Get the "Operating Systems" section */
IniSection = IniCacheGetSection(BootStore->IniCache, L"Operating Systems");
/* Get or create the "Operating Systems" section */
IniSection = IniGetSection(BootStore->IniCache, L"Operating Systems");
if (!IniSection)
{
/* It does not exist yet, so create it */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"Operating Systems");
if (!IniSection)
{
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'Operating Systems' section!\n");
}
}
IniSection = IniAddSection(BootStore->IniCache, L"Operating Systems");
if (!IniSection)
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'Operating Systems' section!\n");
BootStore->OsIniSection = IniSection;
}
@ -476,28 +451,26 @@ OpenIniBootLoaderStore(
*/
/* Get the "boot loader" section */
IniSection = IniCacheGetSection(BootStore->IniCache, L"boot loader");
IniSection = IniGetSection(BootStore->IniCache, L"boot loader");
if (!IniSection)
{
/* Fall back to "flexboot" */
IniSection = IniCacheGetSection(BootStore->IniCache, L"flexboot");
IniSection = IniGetSection(BootStore->IniCache, L"flexboot");
if (!IniSection)
{
/* Fall back to "multiboot" */
IniSection = IniCacheGetSection(BootStore->IniCache, L"multiboot");
IniSection = IniGetSection(BootStore->IniCache, L"multiboot");
}
}
#if 0
if (!IniSection)
{
/* It does not exist yet, so create it */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"boot loader");
if (!IniSection)
{
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'boot loader' section!\n");
}
IniSection = IniAddSection(BootStore->IniCache, L"boot loader");
}
#endif
if (!IniSection)
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'boot loader' section!\n");
BootStore->OptionsIniSection = IniSection;
@ -506,18 +479,16 @@ OpenIniBootLoaderStore(
*/
/* Get the "Operating Systems" section */
IniSection = IniCacheGetSection(BootStore->IniCache, L"operating systems");
IniSection = IniGetSection(BootStore->IniCache, L"operating systems");
#if 0
if (!IniSection)
{
#if 0
/* It does not exist yet, so create it */
IniSection = IniCacheAppendSection(BootStore->IniCache, L"operating systems");
if (!IniSection)
{
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'operating systems' section!\n");
}
#endif
IniSection = IniAddSection(BootStore->IniCache, L"operating systems");
}
#endif
if (!IniSection)
DPRINT1("OpenIniBootLoaderStore: Failed to retrieve 'operating systems' section!\n");
BootStore->OsIniSection = IniSection;
}
@ -785,15 +756,14 @@ CreateNTOSEntry(
IN ULONG_PTR BootEntryKey,
IN PBOOT_STORE_ENTRY BootEntry)
{
PINICACHESECTION IniSection;
PWCHAR Section = (PWCHAR)BootEntryKey;
PINI_SECTION IniSection;
PCWSTR Section = (PCWSTR)BootEntryKey;
/* Insert the entry into the "Operating Systems" section */
IniCacheInsertKey(BootStore->OsIniSection, NULL, INSERT_LAST,
Section, (PWSTR)BootEntry->FriendlyName);
IniAddKey(BootStore->OsIniSection, Section, BootEntry->FriendlyName);
/* Create a new section */
IniSection = IniCacheAppendSection(BootStore->IniCache, Section);
IniSection = IniAddSection(BootStore->IniCache, Section);
if (BootEntry->OsOptionsLength >= sizeof(NTOS_OPTIONS) &&
RtlCompareMemory(&BootEntry->OsOptions /* Signature */,
@ -803,17 +773,10 @@ CreateNTOSEntry(
{
PNTOS_OPTIONS Options = (PNTOS_OPTIONS)&BootEntry->OsOptions;
/* BootType */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"BootType", L"Windows2003");
/* SystemPath */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"SystemPath", (PWSTR)Options->OsLoadPath);
/* Options */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"Options", (PWSTR)Options->OsLoadOptions);
/* BootType, SystemPath and Options */
IniAddKey(IniSection, L"BootType", L"Windows2003");
IniAddKey(IniSection, L"SystemPath", Options->OsLoadPath);
IniAddKey(IniSection, L"Options", Options->OsLoadOptions);
}
else
if (BootEntry->OsOptionsLength >= sizeof(BOOT_SECTOR_OPTIONS) &&
@ -824,21 +787,11 @@ CreateNTOSEntry(
{
PBOOT_SECTOR_OPTIONS Options = (PBOOT_SECTOR_OPTIONS)&BootEntry->OsOptions;
/* BootType */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"BootType", L"BootSector");
/* BootDrive */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"BootDrive", (PWSTR)Options->Drive);
/* BootPartition */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"BootPartition", (PWSTR)Options->Partition);
/* BootSector */
IniCacheInsertKey(IniSection, NULL, INSERT_LAST,
L"BootSectorFile", (PWSTR)Options->BootSectorFileName);
/* BootType, BootDrive, BootPartition and BootSector */
IniAddKey(IniSection, L"BootType", L"BootSector");
IniAddKey(IniSection, L"BootDrive", Options->Drive);
IniAddKey(IniSection, L"BootPartition", Options->Partition);
IniAddKey(IniSection, L"BootSectorFile", Options->BootSectorFileName);
}
else
{
@ -935,8 +888,8 @@ AddBootStoreEntry(
}
/* Insert the entry into the "Operating Systems" section */
IniCacheInsertKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OsIniSection, NULL, INSERT_LAST,
(PWSTR)Options->OsLoadPath, Buffer);
IniAddKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OsIniSection,
Options->OsLoadPath, Buffer);
RtlFreeHeap(ProcessHeap, 0, Buffer);
return STATUS_SUCCESS;
@ -1087,13 +1040,13 @@ QueryBootStoreOptions(
{
BootOptions->Version = FreeLdr;
Status = IniCacheGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"DefaultOS", (PWCHAR*)&BootOptions->CurrentBootEntryKey);
Status = IniGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"DefaultOS", (PWCHAR*)&BootOptions->CurrentBootEntryKey);
if (!NT_SUCCESS(Status))
BootOptions->CurrentBootEntryKey = 0;
Status = IniCacheGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"TimeOut", &TimeoutStr);
Status = IniGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"TimeOut", &TimeoutStr);
if (NT_SUCCESS(Status) && TimeoutStr)
BootOptions->Timeout = _wtoi(TimeoutStr);
else
@ -1103,13 +1056,13 @@ QueryBootStoreOptions(
{
BootOptions->Version = NtLdr;
Status = IniCacheGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"default", (PWCHAR*)&BootOptions->CurrentBootEntryKey);
Status = IniGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"default", (PWCHAR*)&BootOptions->CurrentBootEntryKey);
if (!NT_SUCCESS(Status))
BootOptions->CurrentBootEntryKey = 0;
Status = IniCacheGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"timeout", &TimeoutStr);
Status = IniGetKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"timeout", &TimeoutStr);
if (NT_SUCCESS(Status) && TimeoutStr)
BootOptions->Timeout = _wtoi(TimeoutStr);
else
@ -1157,14 +1110,13 @@ SetBootStoreOptions(
// TODO: Depending on the flags set in 'FieldsToChange',
// change either one or both these bootloader options.
//
IniCacheInsertKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
NULL, INSERT_LAST,
L"DefaultOS", (PWCHAR)BootOptions->CurrentBootEntryKey);
IniAddKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
L"DefaultOS", (PCWSTR)BootOptions->CurrentBootEntryKey);
RtlStringCchPrintfW(TimeoutStr, ARRAYSIZE(TimeoutStr), L"%d", BootOptions->Timeout);
IniCacheInsertKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
NULL, INSERT_FIRST, // INSERT_LAST, // FIXME!! There is a bug in the INI parser where a given key can be inserted twice in the same section...
L"TimeOut", TimeoutStr);
IniInsertKey(((PBOOT_STORE_INI_CONTEXT)BootStore)->OptionsIniSection,
NULL, INSERT_FIRST, // INSERT_LAST, // FIXME!! There is a bug in the INI parser where a given key can be inserted twice in the same section...
L"TimeOut", TimeoutStr);
return STATUS_SUCCESS;
}
@ -1180,7 +1132,7 @@ FreeLdrEnumerateBootEntries(
{
NTSTATUS Status = STATUS_SUCCESS;
PINICACHEITERATOR Iterator;
PINICACHESECTION OsIniSection;
PINI_SECTION OsIniSection;
PWCHAR SectionName, KeyData;
UCHAR xxBootEntry[FIELD_OFFSET(BOOT_STORE_ENTRY, OsOptions) +
max(sizeof(NTOS_OPTIONS), sizeof(BOOT_SECTOR_OPTIONS))];
@ -1188,7 +1140,7 @@ FreeLdrEnumerateBootEntries(
PWCHAR Buffer;
/* Enumerate all the valid installations listed in the "Operating Systems" section */
Iterator = IniCacheFindFirstValue(BootStore->OsIniSection, &SectionName, &KeyData);
Iterator = IniFindFirstValue(BootStore->OsIniSection, &SectionName, &KeyData);
if (!Iterator) return STATUS_SUCCESS;
do
{
@ -1243,13 +1195,13 @@ FreeLdrEnumerateBootEntries(
BootEntry->OsOptionsLength = 0;
/* Search for an existing boot entry section */
OsIniSection = IniCacheGetSection(BootStore->IniCache, SectionName);
OsIniSection = IniGetSection(BootStore->IniCache, SectionName);
if (!OsIniSection)
goto DoEnum;
/* Check for supported boot type "Windows2003" */
Status = IniCacheGetKey(OsIniSection, L"BootType", &KeyData);
if (!NT_SUCCESS(Status) || (KeyData == NULL))
Status = IniGetKey(OsIniSection, L"BootType", &KeyData);
if (!NT_SUCCESS(Status) || !KeyData)
{
/* Certainly not a ReactOS installation */
DPRINT1("No BootType value present!\n");
@ -1273,18 +1225,16 @@ FreeLdrEnumerateBootEntries(
// BootEntry->BootFilePath = NULL;
/* Check its SystemPath */
Status = IniCacheGetKey(OsIniSection, L"SystemPath", &KeyData);
if (!NT_SUCCESS(Status))
Options->OsLoadPath = NULL;
else
Options->OsLoadPath = NULL;
Status = IniGetKey(OsIniSection, L"SystemPath", &KeyData);
if (NT_SUCCESS(Status))
Options->OsLoadPath = KeyData;
// KeyData == SystemRoot;
/* Check the optional Options */
Status = IniCacheGetKey(OsIniSection, L"Options", &KeyData);
if (!NT_SUCCESS(Status))
Options->OsLoadOptions = NULL;
else
Options->OsLoadOptions = NULL;
Status = IniGetKey(OsIniSection, L"Options", &KeyData);
if (NT_SUCCESS(Status))
Options->OsLoadOptions = KeyData;
}
else
@ -1304,24 +1254,21 @@ FreeLdrEnumerateBootEntries(
// BootEntry->BootFilePath = NULL;
/* Check its BootDrive */
Status = IniCacheGetKey(OsIniSection, L"BootDrive", &KeyData);
if (!NT_SUCCESS(Status))
Options->Drive = NULL;
else
Options->Drive = NULL;
Status = IniGetKey(OsIniSection, L"BootDrive", &KeyData);
if (NT_SUCCESS(Status))
Options->Drive = KeyData;
/* Check its BootPartition */
Status = IniCacheGetKey(OsIniSection, L"BootPartition", &KeyData);
if (!NT_SUCCESS(Status))
Options->Partition = NULL;
else
Options->Partition = NULL;
Status = IniGetKey(OsIniSection, L"BootPartition", &KeyData);
if (NT_SUCCESS(Status))
Options->Partition = KeyData;
/* Check its BootSector */
Status = IniCacheGetKey(OsIniSection, L"BootSectorFile", &KeyData);
if (!NT_SUCCESS(Status))
Options->BootSectorFileName = NULL;
else
Options->BootSectorFileName = NULL;
Status = IniGetKey(OsIniSection, L"BootSectorFile", &KeyData);
if (NT_SUCCESS(Status))
Options->BootSectorFileName = KeyData;
}
else
@ -1342,9 +1289,9 @@ DoEnum:
if (!NT_SUCCESS(Status))
break;
}
while (IniCacheFindNextValue(Iterator, &SectionName, &KeyData));
while (IniFindNextValue(Iterator, &SectionName, &KeyData));
IniCacheFindClose(Iterator);
IniFindClose(Iterator);
return Status;
}
@ -1365,7 +1312,7 @@ NtLdrEnumerateBootEntries(
ULONG BufferLength;
/* Enumerate all the valid installations */
Iterator = IniCacheFindFirstValue(BootStore->OsIniSection, &SectionName, &KeyData);
Iterator = IniFindFirstValue(BootStore->OsIniSection, &SectionName, &KeyData);
if (!Iterator) return STATUS_SUCCESS;
do
{
@ -1476,9 +1423,9 @@ NtLdrEnumerateBootEntries(
if (!NT_SUCCESS(Status))
break;
}
while (IniCacheFindNextValue(Iterator, &SectionName, &KeyData));
while (IniFindNextValue(Iterator, &SectionName, &KeyData));
IniCacheFindClose(Iterator);
IniFindClose(Iterator);
return Status;
}

View file

@ -17,11 +17,11 @@
/* PRIVATE FUNCTIONS ********************************************************/
static
PINICACHEKEY
PINI_KEYWORD
IniCacheFreeKey(
PINICACHEKEY Key)
PINI_KEYWORD Key)
{
PINICACHEKEY Next;
PINI_KEYWORD Next;
if (Key == NULL)
return NULL;
@ -44,13 +44,12 @@ IniCacheFreeKey(
return Next;
}
static
PINICACHESECTION
PINI_SECTION
IniCacheFreeSection(
PINICACHESECTION Section)
PINI_SECTION Section)
{
PINICACHESECTION Next;
PINI_SECTION Next;
if (Section == NULL)
return NULL;
@ -73,15 +72,14 @@ IniCacheFreeSection(
return Next;
}
static
PINICACHEKEY
PINI_KEYWORD
IniCacheFindKey(
PINICACHESECTION Section,
PWCHAR Name,
ULONG NameLength)
PINI_SECTION Section,
PWCHAR Name,
ULONG NameLength)
{
PINICACHEKEY Key;
PINI_KEYWORD Key;
Key = Section->FirstKey;
while (Key != NULL)
@ -98,20 +96,19 @@ IniCacheFindKey(
return Key;
}
static
PINICACHEKEY
IniCacheAddKey(
PINICACHESECTION Section,
PCHAR Name,
ULONG NameLength,
PCHAR Data,
ULONG DataLength)
PINI_KEYWORD
IniCacheAddKeyAorW(
_In_ PINI_SECTION Section,
_In_ PINI_KEYWORD AnchorKey,
_In_ INSERTION_TYPE InsertionType,
_In_ const VOID* Name,
_In_ ULONG NameLength,
_In_ const VOID* Data,
_In_ ULONG DataLength,
_In_ BOOLEAN IsUnicode)
{
PINICACHEKEY Key;
ULONG i;
Key = NULL;
PINI_KEYWORD Key = NULL;
if (Section == NULL ||
Name == NULL ||
@ -123,16 +120,18 @@ IniCacheAddKey(
return NULL;
}
Key = (PINICACHEKEY)RtlAllocateHeap(ProcessHeap,
/* Allocate key buffer */
Key = (PINI_KEYWORD)RtlAllocateHeap(ProcessHeap,
HEAP_ZERO_MEMORY,
sizeof(INICACHEKEY));
sizeof(INI_KEYWORD));
if (Key == NULL)
{
DPRINT("RtlAllocateHeap() failed\n");
return NULL;
}
Key->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
/* Allocate name buffer */
Key->Name = (PWCHAR)RtlAllocateHeap(ProcessHeap,
0,
(NameLength + 1) * sizeof(WCHAR));
if (Key->Name == NULL)
@ -142,14 +141,15 @@ IniCacheAddKey(
return NULL;
}
/* Copy value name */
for (i = 0; i < NameLength; i++)
{
Key->Name[i] = (WCHAR)Name[i];
}
Key->Name[NameLength] = 0;
/* Copy value name (ANSI or UNICODE) */
if (IsUnicode)
wcsncpy(Key->Name, (PCWCH)Name, NameLength);
else
_snwprintf(Key->Name, NameLength, L"%.*S", NameLength, (PCCH)Name);
Key->Name[NameLength] = UNICODE_NULL;
Key->Data = (WCHAR*)RtlAllocateHeap(ProcessHeap,
/* Allocate data buffer */
Key->Data = (PWCHAR)RtlAllocateHeap(ProcessHeap,
0,
(DataLength + 1) * sizeof(WCHAR));
if (Key->Data == NULL)
@ -160,39 +160,65 @@ IniCacheAddKey(
return NULL;
}
/* Copy value data */
for (i = 0; i < DataLength; i++)
{
Key->Data[i] = (WCHAR)Data[i];
}
Key->Data[DataLength] = 0;
/* Copy value data (ANSI or UNICODE) */
if (IsUnicode)
wcsncpy(Key->Data, (PCWCH)Data, DataLength);
else
_snwprintf(Key->Data, DataLength, L"%.*S", DataLength, (PCCH)Data);
Key->Data[DataLength] = UNICODE_NULL;
/* Insert key into section */
if (Section->FirstKey == NULL)
{
Section->FirstKey = Key;
Section->LastKey = Key;
}
else
else if ((InsertionType == INSERT_FIRST) ||
((InsertionType == INSERT_BEFORE) &&
((AnchorKey == NULL) || (AnchorKey == Section->FirstKey))))
{
/* Insert at the head of the list */
Section->FirstKey->Prev = Key;
Key->Next = Section->FirstKey;
Section->FirstKey = Key;
}
else if ((InsertionType == INSERT_BEFORE) && (AnchorKey != NULL))
{
/* Insert before the anchor key */
Key->Next = AnchorKey;
Key->Prev = AnchorKey->Prev;
AnchorKey->Prev->Next = Key;
AnchorKey->Prev = Key;
}
else if ((InsertionType == INSERT_LAST) ||
((InsertionType == INSERT_AFTER) &&
((AnchorKey == NULL) || (AnchorKey == Section->LastKey))))
{
Section->LastKey->Next = Key;
Key->Prev = Section->LastKey;
Section->LastKey = Key;
}
else if ((InsertionType == INSERT_AFTER) && (AnchorKey != NULL))
{
/* Insert after the anchor key */
Key->Next = AnchorKey->Next;
Key->Prev = AnchorKey;
AnchorKey->Next->Prev = Key;
AnchorKey->Next = Key;
}
return Key;
return Key;
}
static
PINICACHESECTION
IniCacheAddSection(
PINICACHE Cache,
PCHAR Name,
ULONG NameLength)
PINI_SECTION
IniCacheAddSectionAorW(
_In_ PINICACHE Cache,
_In_ const VOID* Name,
_In_ ULONG NameLength,
_In_ BOOLEAN IsUnicode)
{
PINICACHESECTION Section = NULL;
ULONG i;
PINI_SECTION Section = NULL;
if (Cache == NULL || Name == NULL || NameLength == 0)
{
@ -200,9 +226,9 @@ IniCacheAddSection(
return NULL;
}
Section = (PINICACHESECTION)RtlAllocateHeap(ProcessHeap,
HEAP_ZERO_MEMORY,
sizeof(INICACHESECTION));
Section = (PINI_SECTION)RtlAllocateHeap(ProcessHeap,
HEAP_ZERO_MEMORY,
sizeof(INI_SECTION));
if (Section == NULL)
{
DPRINT("RtlAllocateHeap() failed\n");
@ -210,7 +236,7 @@ IniCacheAddSection(
}
/* Allocate and initialize section name */
Section->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
Section->Name = (PWCHAR)RtlAllocateHeap(ProcessHeap,
0,
(NameLength + 1) * sizeof(WCHAR));
if (Section->Name == NULL)
@ -220,12 +246,12 @@ IniCacheAddSection(
return NULL;
}
/* Copy section name */
for (i = 0; i < NameLength; i++)
{
Section->Name[i] = (WCHAR)Name[i];
}
Section->Name[NameLength] = 0;
/* Copy section name (ANSI or UNICODE) */
if (IsUnicode)
wcsncpy(Section->Name, (PCWCH)Name, NameLength);
else
_snwprintf(Section->Name, NameLength, L"%.*S", NameLength, (PCCH)Name);
Section->Name[NameLength] = UNICODE_NULL;
/* Append section */
if (Cache->FirstSection == NULL)
@ -243,7 +269,6 @@ IniCacheAddSection(
return Section;
}
static
PCHAR
IniCacheSkipWhitespace(
@ -255,7 +280,6 @@ IniCacheSkipWhitespace(
return (*Ptr == 0) ? NULL : Ptr;
}
static
PCHAR
IniCacheSkipToNextSection(
@ -274,7 +298,6 @@ IniCacheSkipToNextSection(
return (*Ptr == 0) ? NULL : Ptr;
}
static
PCHAR
IniCacheGetSectionName(
@ -283,7 +306,6 @@ IniCacheGetSectionName(
PULONG NameSize)
{
ULONG Size = 0;
CHAR Name[256];
*NamePtr = NULL;
*NameSize = 0;
@ -311,15 +333,11 @@ IniCacheGetSectionName(
*NameSize = Size;
strncpy(Name, *NamePtr, Size);
Name[Size] = 0;
DPRINT("SectionName: '%s'\n", Name);
DPRINT("SectionName: '%.*s'\n", Size, *NamePtr);
return Ptr;
}
static
PCHAR
IniCacheGetKeyName(
@ -372,7 +390,6 @@ IniCacheGetKeyName(
return Ptr;
}
static
PCHAR
IniCacheGetKeyValue(
@ -457,8 +474,8 @@ IniCacheLoadFromMemory(
{
PCHAR Ptr;
PINICACHESECTION Section;
PINICACHEKEY Key;
PINI_SECTION Section;
PINI_KEYWORD Key;
PCHAR SectionName;
ULONG SectionNameSize;
@ -499,12 +516,13 @@ IniCacheLoadFromMemory(
DPRINT("[%.*s]\n", SectionNameSize, SectionName);
Section = IniCacheAddSection(*Cache,
SectionName,
SectionNameSize);
Section = IniCacheAddSectionAorW(*Cache,
SectionName,
SectionNameSize,
FALSE);
if (Section == NULL)
{
DPRINT("IniCacheAddSection() failed\n");
DPRINT("IniCacheAddSectionAorW() failed\n");
Ptr = IniCacheSkipToNextSection(Ptr);
continue;
}
@ -528,14 +546,17 @@ IniCacheLoadFromMemory(
DPRINT("'%.*s' = '%.*s'\n", KeyNameSize, KeyName, KeyValueSize, KeyValue);
Key = IniCacheAddKey(Section,
KeyName,
KeyNameSize,
KeyValue,
KeyValueSize);
Key = IniCacheAddKeyAorW(Section,
NULL,
INSERT_LAST,
KeyName,
KeyNameSize,
KeyValue,
KeyValueSize,
FALSE);
if (Key == NULL)
{
DPRINT("IniCacheAddKey() failed\n");
DPRINT("IniCacheAddKeyAorW() failed\n");
}
}
}
@ -575,7 +596,7 @@ IniCacheLoadByHandle(
DPRINT("File size: %lu\n", FileLength);
/* Allocate file buffer with NULL-terminator */
FileBuffer = (CHAR*)RtlAllocateHeap(ProcessHeap,
FileBuffer = (PCHAR)RtlAllocateHeap(ProcessHeap,
0,
FileLength + 1);
if (FileBuffer == NULL)
@ -661,7 +682,6 @@ IniCacheLoad(
return Status;
}
VOID
IniCacheDestroy(
PINICACHE Cache)
@ -679,12 +699,12 @@ IniCacheDestroy(
}
PINICACHESECTION
IniCacheGetSection(
PINI_SECTION
IniGetSection(
PINICACHE Cache,
PWCHAR Name)
{
PINICACHESECTION Section = NULL;
PINI_SECTION Section = NULL;
if (Cache == NULL || Name == NULL)
{
@ -711,14 +731,13 @@ IniCacheGetSection(
return NULL;
}
NTSTATUS
IniCacheGetKey(
PINICACHESECTION Section,
IniGetKey(
PINI_SECTION Section,
PWCHAR KeyName,
PWCHAR *KeyData)
{
PINICACHEKEY Key;
PINI_KEYWORD Key;
if (Section == NULL || KeyName == NULL || KeyData == NULL)
{
@ -741,13 +760,13 @@ IniCacheGetKey(
PINICACHEITERATOR
IniCacheFindFirstValue(
PINICACHESECTION Section,
IniFindFirstValue(
PINI_SECTION Section,
PWCHAR *KeyName,
PWCHAR *KeyData)
{
PINICACHEITERATOR Iterator;
PINICACHEKEY Key;
PINI_KEYWORD Key;
if (Section == NULL || KeyName == NULL || KeyData == NULL)
{
@ -780,14 +799,13 @@ IniCacheFindFirstValue(
return Iterator;
}
BOOLEAN
IniCacheFindNextValue(
IniFindNextValue(
PINICACHEITERATOR Iterator,
PWCHAR *KeyName,
PWCHAR *KeyData)
{
PINICACHEKEY Key;
PINI_KEYWORD Key;
if (Iterator == NULL || KeyName == NULL || KeyData == NULL)
{
@ -810,9 +828,8 @@ IniCacheFindNextValue(
return TRUE;
}
VOID
IniCacheFindClose(
IniFindClose(
PINICACHEITERATOR Iterator)
{
if (Iterator == NULL)
@ -822,106 +839,46 @@ IniCacheFindClose(
}
PINICACHEKEY
IniCacheInsertKey(
PINICACHESECTION Section,
PINICACHEKEY AnchorKey,
INSERTION_TYPE InsertionType,
PWCHAR Name,
PWCHAR Data)
PINI_SECTION
IniAddSection(
_In_ PINICACHE Cache,
_In_ PCWSTR Name)
{
PINICACHEKEY Key;
Key = NULL;
if (Section == NULL ||
Name == NULL ||
*Name == 0 ||
Data == NULL ||
*Data == 0)
if (!Cache || !Name || !*Name)
{
DPRINT("Invalid parameter\n");
return NULL;
}
return IniCacheAddSectionAorW(Cache, Name, wcslen(Name), TRUE);
}
/* Allocate key buffer */
Key = (PINICACHEKEY)RtlAllocateHeap(ProcessHeap,
HEAP_ZERO_MEMORY,
sizeof(INICACHEKEY));
if (Key == NULL)
PINI_KEYWORD
IniInsertKey(
_In_ PINI_SECTION Section,
_In_ PINI_KEYWORD AnchorKey,
_In_ INSERTION_TYPE InsertionType,
_In_ PCWSTR Name,
_In_ PCWSTR Data)
{
if (!Section || !Name || !*Name || !Data || !*Data)
{
DPRINT("RtlAllocateHeap() failed\n");
DPRINT("Invalid parameter\n");
return NULL;
}
return IniCacheAddKeyAorW(Section,
AnchorKey, InsertionType,
Name, wcslen(Name),
Data, wcslen(Data),
TRUE);
}
/* Allocate name buffer */
Key->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
0,
(wcslen(Name) + 1) * sizeof(WCHAR));
if (Key->Name == NULL)
{
DPRINT("RtlAllocateHeap() failed\n");
RtlFreeHeap(ProcessHeap, 0, Key);
return NULL;
}
/* Copy value name */
wcscpy(Key->Name, Name);
/* Allocate data buffer */
Key->Data = (WCHAR*)RtlAllocateHeap(ProcessHeap,
0,
(wcslen(Data) + 1) * sizeof(WCHAR));
if (Key->Data == NULL)
{
DPRINT("RtlAllocateHeap() failed\n");
RtlFreeHeap(ProcessHeap, 0, Key->Name);
RtlFreeHeap(ProcessHeap, 0, Key);
return NULL;
}
/* Copy value data */
wcscpy(Key->Data, Data);
/* Insert key into section */
if (Section->FirstKey == NULL)
{
Section->FirstKey = Key;
Section->LastKey = Key;
}
else if ((InsertionType == INSERT_FIRST) ||
((InsertionType == INSERT_BEFORE) && ((AnchorKey == NULL) || (AnchorKey == Section->FirstKey))))
{
/* Insert at the head of the list */
Section->FirstKey->Prev = Key;
Key->Next = Section->FirstKey;
Section->FirstKey = Key;
}
else if ((InsertionType == INSERT_BEFORE) && (AnchorKey != NULL))
{
/* Insert before the anchor key */
Key->Next = AnchorKey;
Key->Prev = AnchorKey->Prev;
AnchorKey->Prev->Next = Key;
AnchorKey->Prev = Key;
}
else if ((InsertionType == INSERT_LAST) ||
((InsertionType == INSERT_AFTER) && ((AnchorKey == NULL) || (AnchorKey == Section->LastKey))))
{
Section->LastKey->Next = Key;
Key->Prev = Section->LastKey;
Section->LastKey = Key;
}
else if ((InsertionType == INSERT_AFTER) && (AnchorKey != NULL))
{
/* Insert after the anchor key */
Key->Next = AnchorKey->Next;
Key->Prev = AnchorKey;
AnchorKey->Next->Prev = Key;
AnchorKey->Next = Key;
}
return Key;
PINI_KEYWORD
IniAddKey(
_In_ PINI_SECTION Section,
_In_ PCWSTR Name,
_In_ PCWSTR Data)
{
return IniInsertKey(Section, NULL, INSERT_LAST, Name, Data);
}
@ -943,15 +900,14 @@ IniCacheCreate(VOID)
return Cache;
}
NTSTATUS
IniCacheSaveByHandle(
PINICACHE Cache,
HANDLE FileHandle)
{
NTSTATUS Status;
PINICACHESECTION Section;
PINICACHEKEY Key;
PINI_SECTION Section;
PINI_KEYWORD Key;
ULONG BufferSize;
PCHAR Buffer;
PCHAR Ptr;
@ -984,7 +940,7 @@ IniCacheSaveByHandle(
DPRINT("BufferSize: %lu\n", BufferSize);
/* Allocate file buffer with NULL-terminator */
Buffer = (CHAR*)RtlAllocateHeap(ProcessHeap,
Buffer = (PCHAR)RtlAllocateHeap(ProcessHeap,
HEAP_ZERO_MEMORY,
BufferSize + 1);
if (Buffer == NULL)
@ -1083,57 +1039,4 @@ IniCacheSave(
return Status;
}
PINICACHESECTION
IniCacheAppendSection(
PINICACHE Cache,
PWCHAR Name)
{
PINICACHESECTION Section = NULL;
if (Cache == NULL || Name == NULL || *Name == 0)
{
DPRINT("Invalid parameter\n");
return NULL;
}
Section = (PINICACHESECTION)RtlAllocateHeap(ProcessHeap,
HEAP_ZERO_MEMORY,
sizeof(INICACHESECTION));
if (Section == NULL)
{
DPRINT("RtlAllocateHeap() failed\n");
return NULL;
}
/* Allocate and initialize section name */
Section->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
0,
(wcslen(Name) + 1) * sizeof(WCHAR));
if (Section->Name == NULL)
{
DPRINT("RtlAllocateHeap() failed\n");
RtlFreeHeap(ProcessHeap, 0, Section);
return NULL;
}
/* Copy section name */
wcscpy(Section->Name, Name);
/* Append section */
if (Cache->FirstSection == NULL)
{
Cache->FirstSection = Section;
Cache->LastSection = Section;
}
else
{
Cache->LastSection->Next = Section;
Section->Prev = Cache->LastSection;
Cache->LastSection = Section;
}
return Section;
}
/* EOF */

View file

@ -7,42 +7,38 @@
#pragma once
typedef struct _INICACHEKEY
typedef struct _INI_KEYWORD
{
PWCHAR Name;
PWCHAR Data;
struct _INICACHEKEY *Next;
struct _INICACHEKEY *Prev;
} INICACHEKEY, *PINICACHEKEY;
struct _INI_KEYWORD *Next;
struct _INI_KEYWORD *Prev;
} INI_KEYWORD, *PINI_KEYWORD;
typedef struct _INICACHESECTION
typedef struct _INI_SECTION
{
PWCHAR Name;
PINICACHEKEY FirstKey;
PINICACHEKEY LastKey;
struct _INICACHESECTION *Next;
struct _INICACHESECTION *Prev;
} INICACHESECTION, *PINICACHESECTION;
PINI_KEYWORD FirstKey;
PINI_KEYWORD LastKey;
struct _INI_SECTION *Next;
struct _INI_SECTION *Prev;
} INI_SECTION, *PINI_SECTION;
typedef struct _INICACHE
{
PINICACHESECTION FirstSection;
PINICACHESECTION LastSection;
PINI_SECTION FirstSection;
PINI_SECTION LastSection;
} INICACHE, *PINICACHE;
typedef struct _PINICACHEITERATOR
{
PINICACHESECTION Section;
PINICACHEKEY Key;
PINI_SECTION Section;
PINI_KEYWORD Key;
} INICACHEITERATOR, *PINICACHEITERATOR;
typedef enum
{
INSERT_FIRST,
@ -76,41 +72,51 @@ VOID
IniCacheDestroy(
PINICACHE Cache);
PINICACHESECTION
IniCacheGetSection(
PINI_SECTION
IniGetSection(
PINICACHE Cache,
PWCHAR Name);
NTSTATUS
IniCacheGetKey(
PINICACHESECTION Section,
IniGetKey(
PINI_SECTION Section,
PWCHAR KeyName,
PWCHAR *KeyData);
PINICACHEITERATOR
IniCacheFindFirstValue(
PINICACHESECTION Section,
IniFindFirstValue(
PINI_SECTION Section,
PWCHAR *KeyName,
PWCHAR *KeyData);
BOOLEAN
IniCacheFindNextValue(
IniFindNextValue(
PINICACHEITERATOR Iterator,
PWCHAR *KeyName,
PWCHAR *KeyData);
VOID
IniCacheFindClose(
IniFindClose(
PINICACHEITERATOR Iterator);
PINI_SECTION
IniAddSection(
_In_ PINICACHE Cache,
_In_ PCWSTR Name);
PINICACHEKEY
IniCacheInsertKey(
PINICACHESECTION Section,
PINICACHEKEY AnchorKey,
INSERTION_TYPE InsertionType,
PWCHAR Name,
PWCHAR Data);
PINI_KEYWORD
IniInsertKey(
_In_ PINI_SECTION Section,
_In_ PINI_KEYWORD AnchorKey,
_In_ INSERTION_TYPE InsertionType,
_In_ PCWSTR Name,
_In_ PCWSTR Data);
PINI_KEYWORD
IniAddKey(
_In_ PINI_SECTION Section,
_In_ PCWSTR Name,
_In_ PCWSTR Data);
PINICACHE
IniCacheCreate(VOID);
@ -125,9 +131,4 @@ IniCacheSave(
PINICACHE Cache,
PWCHAR FileName);
PINICACHESECTION
IniCacheAppendSection(
PINICACHE Cache,
PWCHAR Name);
/* EOF */