[CMAKE]
[reactos.git] / dll / ntdll / ldr / utils.c
index 1451219..1cfb8c8 100644 (file)
@@ -88,10 +88,10 @@ static __inline LONG LdrpDecrementLoadCount(PLDR_DATA_TABLE_ENTRY Module, BOOLEA
        RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock);
      }
    LoadCount = Module->LoadCount;
-   if (Module->LoadCount > 0 && Module->LoadCount != LDRP_PROCESS_CREATION_TIME)
-     {
+    if (Module->LoadCount > 0 && Module->LoadCount != LDRP_PROCESS_CREATION_TIME)
+    {
        Module->LoadCount--;
-     }
+    }
    if (!Locked)
      {
        RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock);
@@ -107,10 +107,10 @@ static __inline LONG LdrpIncrementLoadCount(PLDR_DATA_TABLE_ENTRY Module, BOOLEA
        RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock);
      }
    LoadCount = Module->LoadCount;
-   if (Module->LoadCount != LDRP_PROCESS_CREATION_TIME)
-     {
+    if (Module->LoadCount != LDRP_PROCESS_CREATION_TIME)
+    {
        Module->LoadCount++;
-     }
+    }
    if (!Locked)
      {
        RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock);
@@ -256,7 +256,7 @@ LdrpQueryAppPaths(IN PCWSTR ImageName)
     /* Copy it to the heap allocd memory */
     Path = RtlAllocateHeap(RtlGetProcessHeap(),
                            0,
-                           wcslen(SearchPathBuffer) * sizeof(WCHAR));
+                           (wcslen(SearchPathBuffer) + 1) * sizeof(WCHAR));
 
     if (!Path)
     {
@@ -1388,7 +1388,7 @@ LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders,
   ULONG Count, ProtectSize, OldProtect, OldProtect2;
   PVOID Page, ProtectPage, ProtectPage2;
   PUSHORT TypeOffset;
-  ULONG_PTR Delta;
+  LONG_PTR Delta;
   NTSTATUS Status;
 
   if (NTHeaders->FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED)
@@ -3337,6 +3337,7 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
                                    OUT PULONG ReturnedLength OPTIONAL)
 {
   PKEY_VALUE_PARTIAL_INFORMATION KeyInfo;
+  CHAR KeyInfoBuffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 32];
   OBJECT_ATTRIBUTES ObjectAttributes;
   UNICODE_STRING ValueNameString;
   UNICODE_STRING KeyName;
@@ -3377,15 +3378,8 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
       return Status;
     }
 
-  KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 32;
-  KeyInfo = RtlAllocateHeap (RtlGetProcessHeap(),
-                             HEAP_ZERO_MEMORY,
-                             KeyInfoSize);
-  if (KeyInfo == NULL)
-    {
-      NtClose (KeyHandle);
-      return STATUS_INSUFFICIENT_RESOURCES;
-    }
+  KeyInfoSize = sizeof(KeyInfoBuffer);
+  KeyInfo = (PKEY_VALUE_PARTIAL_INFORMATION)KeyInfoBuffer;
 
   RtlInitUnicodeString (&ValueNameString,
                         (PWSTR)ValueName);
@@ -3397,10 +3391,13 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
                             &ResultSize);
   if (Status == STATUS_BUFFER_OVERFLOW)
     {
+        /* We can allocate only if there is a process heap already */
+        if (!RtlGetProcessHeap())
+          {
+              NtClose (KeyHandle);
+              return STATUS_NO_MEMORY;
+          }
       KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + KeyInfo->DataLength;
-      RtlFreeHeap (RtlGetProcessHeap(),
-                   0,
-                   KeyInfo);
       KeyInfo = RtlAllocateHeap (RtlGetProcessHeap(),
                                  HEAP_ZERO_MEMORY,
                                  KeyInfoSize);
@@ -3421,7 +3418,7 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
 
   if (!NT_SUCCESS(Status))
     {
-      if (KeyInfo != NULL)
+      if ((PCHAR)KeyInfo != KeyInfoBuffer)
         {
           RtlFreeHeap (RtlGetProcessHeap(),
                        0,
@@ -3432,9 +3429,12 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
 
   if (KeyInfo->Type != Type)
     {
-      RtlFreeHeap (RtlGetProcessHeap(),
-                   0,
-                   KeyInfo);
+      if ((PCHAR)KeyInfo != KeyInfoBuffer)
+        {
+          RtlFreeHeap (RtlGetProcessHeap(),
+                       0,
+                       KeyInfo);
+        }
       return STATUS_OBJECT_TYPE_MISMATCH;
     }
 
@@ -3451,9 +3451,12 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
                  &KeyInfo->Data,
                  ResultSize);
 
-  RtlFreeHeap (RtlGetProcessHeap(),
-               0,
-               KeyInfo);
+  if ((PCHAR)KeyInfo != KeyInfoBuffer)
+   {
+     RtlFreeHeap (RtlGetProcessHeap(),
+                  0,
+                  KeyInfo);
+  }
 
   if (ReturnedLength != NULL)
     {
@@ -3464,54 +3467,15 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey,
 }
 
 
-PIMAGE_BASE_RELOCATION NTAPI
-LdrProcessRelocationBlock(IN ULONG_PTR Address,
-                         IN ULONG Count,
-                         IN PUSHORT TypeOffset,
-                         IN LONG_PTR Delta)
+PIMAGE_BASE_RELOCATION
+NTAPI
+LdrProcessRelocationBlock(
+    IN ULONG_PTR Address,
+    IN ULONG Count,
+    IN PUSHORT TypeOffset,
+    IN LONG_PTR Delta)
 {
-  SHORT Offset;
-  USHORT Type;
-  USHORT i;
-  PUSHORT ShortPtr;
-  PULONG LongPtr;
-
-  for (i = 0; i < Count; i++)
-    {
-      Offset = *TypeOffset & 0xFFF;
-      Type = *TypeOffset >> 12;
-
-      switch (Type)
-        {
-          case IMAGE_REL_BASED_ABSOLUTE:
-            break;
-
-          case IMAGE_REL_BASED_HIGH:
-            ShortPtr = (PUSHORT)((ULONG_PTR)Address + Offset);
-            *ShortPtr += HIWORD(Delta);
-            break;
-
-          case IMAGE_REL_BASED_LOW:
-            ShortPtr = (PUSHORT)((ULONG_PTR)Address + Offset);
-            *ShortPtr += LOWORD(Delta);
-            break;
-
-          case IMAGE_REL_BASED_HIGHLOW:
-            LongPtr = (PULONG)((ULONG_PTR)Address + Offset);
-            *LongPtr += Delta;
-            break;
-
-          case IMAGE_REL_BASED_HIGHADJ:
-          case IMAGE_REL_BASED_MIPS_JMPADDR:
-          default:
-            DPRINT1("Unknown/unsupported fixup type %hu.\n", Type);
-            return NULL;
-        }
-
-      TypeOffset++;
-    }
-
-  return (PIMAGE_BASE_RELOCATION)TypeOffset;
+    return LdrProcessRelocationBlockLongLong(Address, Count, TypeOffset, Delta);
 }
 
 NTSTATUS