[USETUP]: Do not append an extra NULL-terminator when updating freeldr.ini
[reactos.git] / reactos / base / setup / usetup / inicache.c
index 0b8dff5..0043e9d 100644 (file)
@@ -19,7 +19,7 @@
 /*
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS text-mode setup
- * FILE:            subsys/system/usetup/inicache.c
+ * FILE:            base/setup/usetup/inicache.c
  * PURPOSE:         INI file parser that caches contents of INI file in memory
  * PROGRAMMER:      Royce Mitchell III
  *                  Eric Kohl
@@ -42,9 +42,7 @@ IniCacheFreeKey(
     PINICACHEKEY Next;
 
     if (Key == NULL)
-    {
         return NULL;
-    }
 
     Next = Key->Next;
     if (Key->Name != NULL)
@@ -73,9 +71,7 @@ IniCacheFreeSection(
     PINICACHESECTION Next;
 
     if (Section == NULL)
-    {
         return NULL;
-    }
 
     Next = Section->Next;
     while (Section->FirstKey != NULL)
@@ -146,7 +142,7 @@ IniCacheAddKey(
     }
 
     Key = (PINICACHEKEY)RtlAllocateHeap(ProcessHeap,
-                                        0,
+                                        HEAP_ZERO_MEMORY,
                                         sizeof(INICACHEKEY));
     if (Key == NULL)
     {
@@ -154,9 +150,6 @@ IniCacheAddKey(
         return NULL;
     }
 
-    RtlZeroMemory(Key,
-                  sizeof(INICACHEKEY));
-
     Key->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
                                         0,
                                         (NameLength + 1) * sizeof(WCHAR));
@@ -209,42 +202,6 @@ IniCacheAddKey(
 }
 
 
-#if 0
-static
-PINICACHESECTION
-IniCacheFindSection(
-    PINICACHE Cache,
-    PWCHAR Name,
-    ULONG NameLength)
-{
-    PINICACHESECTION Section = NULL;
-
-    if (Cache == NULL || Name == NULL || NameLength == 0)
-    {
-        return NULL;
-    }
-
-    Section = Cache->FirstSection;
-
-    /* iterate through list of sections */
-    while (Section != NULL)
-    {
-        if (NameLength == wcslen(Section->Name))
-        {
-            /* are the contents the same too? */
-            if (_wcsnicmp(Section->Name, Name, NameLength) == 0)
-                break;
-        }
-
-        /* get the next section*/
-        Section = Section->Next;
-    }
-
-    return Section;
-}
-#endif
-
-
 static
 PINICACHESECTION
 IniCacheAddSection(
@@ -262,7 +219,7 @@ IniCacheAddSection(
     }
 
     Section = (PINICACHESECTION)RtlAllocateHeap(ProcessHeap,
-                                                0,
+                                                HEAP_ZERO_MEMORY,
                                                 sizeof(INICACHESECTION));
     if (Section == NULL)
     {
@@ -270,8 +227,6 @@ IniCacheAddSection(
         return NULL;
     }
 
-    RtlZeroMemory(Section, sizeof(INICACHESECTION));
-
     /* Allocate and initialize section name */
     Section->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
                                             0,
@@ -351,7 +306,7 @@ IniCacheGetSectionName(
     *NamePtr = NULL;
     *NameSize = 0;
 
-    /* skip whitespace */
+    /* Skip whitespace */
     while (*Ptr != 0 && isspace(*Ptr))
     {
         Ptr++;
@@ -401,7 +356,7 @@ IniCacheGetKeyName(
         *NameSize = 0;
         Size = 0;
 
-        /* skip whitespace and empty lines */
+        /* Skip whitespace and empty lines */
         while (isspace(*Ptr) || *Ptr == '\n' || *Ptr == '\r')
         {
             Ptr++;
@@ -514,9 +469,10 @@ IniCacheGetKeyValue(
 NTSTATUS
 IniCacheLoad(
     PINICACHE *Cache,
-    PUNICODE_STRING FileName,
+    PWCHAR FileName,
     BOOLEAN String)
 {
+    UNICODE_STRING Name;
     OBJECT_ATTRIBUTES ObjectAttributes;
     FILE_STANDARD_INFORMATION FileInfo;
     IO_STATUS_BLOCK IoStatusBlock;
@@ -542,8 +498,10 @@ IniCacheLoad(
     *Cache = NULL;
 
     /* Open ini file */
+    RtlInitUnicodeString(&Name, FileName);
+
     InitializeObjectAttributes(&ObjectAttributes,
-                               FileName,
+                               &Name,
                                0,
                                NULL,
                                NULL);
@@ -579,7 +537,7 @@ IniCacheLoad(
 
     DPRINT("File size: %lu\n", FileLength);
 
-    /* Allocate file buffer */
+    /* Allocate file buffer with NULL-terminator */
     FileBuffer = (CHAR*)RtlAllocateHeap(ProcessHeap,
                                         0,
                                         FileLength + 1);
@@ -602,7 +560,7 @@ IniCacheLoad(
                         &FileOffset,
                         NULL);
 
-    /* Append string terminator */
+    /* Append NULL-terminator */
     FileBuffer[FileLength] = 0;
 
     NtClose(FileHandle);
@@ -616,7 +574,7 @@ IniCacheLoad(
 
     /* Allocate inicache header */
     *Cache = (PINICACHE)RtlAllocateHeap(ProcessHeap,
-                                        0,
+                                        HEAP_ZERO_MEMORY,
                                         sizeof(INICACHE));
     if (*Cache == NULL)
     {
@@ -624,9 +582,6 @@ IniCacheLoad(
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-    /* Initialize inicache header */
-    RtlZeroMemory(*Cache, sizeof(INICACHE));
-
     /* Parse ini file */
     Section = NULL;
     Ptr = FileBuffer;
@@ -700,9 +655,7 @@ IniCacheDestroy(
     PINICACHE Cache)
 {
     if (Cache == NULL)
-    {
         return;
-    }
 
     while (Cache->FirstSection != NULL)
     {
@@ -881,7 +834,7 @@ IniCacheInsertKey(
 
     /* Allocate key buffer */
     Key = (PINICACHEKEY)RtlAllocateHeap(ProcessHeap,
-                                        0,
+                                        HEAP_ZERO_MEMORY,
                                         sizeof(INICACHEKEY));
     if (Key == NULL)
     {
@@ -889,8 +842,6 @@ IniCacheInsertKey(
         return NULL;
     }
 
-   RtlZeroMemory(Key, sizeof(INICACHEKEY));
-
     /* Allocate name buffer */
     Key->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
                                         0,
@@ -969,7 +920,7 @@ IniCacheCreate(VOID)
 
     /* Allocate inicache header */
     Cache = (PINICACHE)RtlAllocateHeap(ProcessHeap,
-                                       0,
+                                       HEAP_ZERO_MEMORY,
                                        sizeof(INICACHE));
     if (Cache == NULL)
     {
@@ -977,9 +928,6 @@ IniCacheCreate(VOID)
         return NULL;
     }
 
-    /* Initialize inicache header */
-    RtlZeroMemory(Cache, sizeof(INICACHE));
-
     return Cache;
 }
 
@@ -1023,24 +971,21 @@ IniCacheSave(
 
         Section = Section->Next;
         if (Section != NULL)
-            BufferSize += 2; /* extra "\r\n" at end of each section */
+            BufferSize += 2; /* Extra "\r\n" at end of each section */
     }
-    BufferSize++; /* Null-terminator */
 
     DPRINT("BufferSize: %lu\n", BufferSize);
 
-    /* Allocate file buffer */
+    /* Allocate file buffer with NULL-terminator */
     Buffer = (CHAR*)RtlAllocateHeap(ProcessHeap,
-                                    0,
-                                    BufferSize);
+                                    HEAP_ZERO_MEMORY,
+                                    BufferSize + 1);
     if (Buffer == NULL)
     {
         DPRINT1("RtlAllocateHeap() failed\n");
         return STATUS_INSUFFICIENT_RESOURCES;
     }
 
-    RtlZeroMemory(Buffer, BufferSize);
-
     /* Fill file buffer */
     Ptr = Buffer;
     Section = Cache->FirstSection;
@@ -1066,8 +1011,7 @@ IniCacheSave(
     }
 
     /* Create ini file */
-    RtlInitUnicodeString(&Name,
-                         FileName);
+    RtlInitUnicodeString(&Name, FileName);
 
     InitializeObjectAttributes(&ObjectAttributes,
                                &Name,
@@ -1133,7 +1077,7 @@ IniCacheAppendSection(
     }
 
     Section = (PINICACHESECTION)RtlAllocateHeap(ProcessHeap,
-                                                0,
+                                                HEAP_ZERO_MEMORY,
                                                 sizeof(INICACHESECTION));
     if (Section == NULL)
     {
@@ -1141,8 +1085,6 @@ IniCacheAppendSection(
         return NULL;
     }
 
-    RtlZeroMemory(Section, sizeof(INICACHESECTION));
-
     /* Allocate and initialize section name */
     Section->Name = (WCHAR*)RtlAllocateHeap(ProcessHeap,
                                             0,