OcAppleKernelLib: Fix number to string conversion

This commit is contained in:
vit9696 2019-03-25 19:39:32 +03:00
parent 53c59b3723
commit 4d9d4bbd1a
3 changed files with 58 additions and 31 deletions

View File

@ -336,4 +336,18 @@ TrimWhiteSpace (
IN CHAR16 *String
);
/**
Convert 64-bit unsigned integer to a nul-termianted hex string.
@param[out] Buffer Destination buffer.
@param[in] BufferSize Destination buffer size in bytes.
@param[in] Value Value to convert.
**/
BOOLEAN
AsciiUint64ToLowerHex (
OUT CHAR8 *Buffer,
IN UINT32 BufferSize,
IN UINT64 Value
);
#endif // OC_STRING_LIB_H_

View File

@ -22,6 +22,7 @@
#include <Library/MemoryAllocationLib.h>
#include <Library/OcAppleKernelLib.h>
#include <Library/OcMachoLib.h>
#include <Library/OcStringLib.h>
#include <Library/PrintLib.h>
#include "PrelinkedInternal.h"
@ -474,33 +475,6 @@ PrelinkedReserveKextSize (
return EFI_SUCCESS;
}
STATIC
BOOLEAN
AsciiIntegerToLowerHex (
OUT CHAR8 *Buffer,
IN UINT32 BufferSize,
IN UINT64 Value
)
{
UINT32 Index;
UINT8 Remainder;
for (Index = 0; Value != 0; Value /= 16, ++Index) {
if (Index >= BufferSize) {
return FALSE;
}
Remainder = (Value % 16);
if (Remainder < 10) {
Buffer[Index] = ('0' + Remainder);
} else {
Buffer[Index] = ('a' + (Remainder - 10));
}
}
return TRUE;
}
EFI_STATUS
PrelinkedInjectKext (
IN OUT PRELINKED_CONTEXT *Context,
@ -596,16 +570,16 @@ PrelinkedInjectKext (
if (Executable != NULL) {
Failed |= XmlNodeAppend (InfoPlistRoot, "key", NULL, PRELINK_INFO_EXECUTABLE_RELATIVE_PATH_KEY) == NULL;
Failed |= XmlNodeAppend (InfoPlistRoot, "string", NULL, ExecutablePath) == NULL;
Failed |= !AsciiIntegerToLowerHex (ExecutableSourceAddrStr, sizeof (ExecutableSourceAddrStr), Context->PrelinkedLastAddress);
Failed |= !AsciiUint64ToLowerHex (ExecutableSourceAddrStr, sizeof (ExecutableSourceAddrStr), Context->PrelinkedLastAddress);
Failed |= XmlNodeAppend (InfoPlistRoot, "key", NULL, PRELINK_INFO_EXECUTABLE_SOURCE_ADDR_KEY) == NULL;
Failed |= XmlNodeAppend (InfoPlistRoot, "integer", PRELINK_INFO_INTEGER_ATTRIBUTES, ExecutableSourceAddrStr) == NULL;
Failed |= !AsciiIntegerToLowerHex (ExecutableLoadAddrStr, sizeof (ExecutableLoadAddrStr), Context->PrelinkedLastLoadAddress);
Failed |= !AsciiUint64ToLowerHex (ExecutableLoadAddrStr, sizeof (ExecutableLoadAddrStr), Context->PrelinkedLastLoadAddress);
Failed |= XmlNodeAppend (InfoPlistRoot, "key", NULL, PRELINK_INFO_EXECUTABLE_LOAD_ADDR_KEY) == NULL;
Failed |= XmlNodeAppend (InfoPlistRoot, "integer", PRELINK_INFO_INTEGER_ATTRIBUTES, ExecutableLoadAddrStr) == NULL;
Failed |= !AsciiIntegerToLowerHex (ExecutableSizeStr, sizeof (ExecutableSizeStr), AlignedExecutableSize);
Failed |= !AsciiUint64ToLowerHex (ExecutableSizeStr, sizeof (ExecutableSizeStr), AlignedExecutableSize);
Failed |= XmlNodeAppend (InfoPlistRoot, "key", NULL, PRELINK_INFO_EXECUTABLE_SIZE_KEY) == NULL;
Failed |= XmlNodeAppend (InfoPlistRoot, "integer", PRELINK_INFO_INTEGER_ATTRIBUTES, ExecutableSizeStr) == NULL;
Failed |= !AsciiIntegerToLowerHex (KmodInfoStr, sizeof (KmodInfoStr), KmodAddress);
Failed |= !AsciiUint64ToLowerHex (KmodInfoStr, sizeof (KmodInfoStr), KmodAddress);
Failed |= XmlNodeAppend (InfoPlistRoot, "key", NULL, PRELINK_INFO_KMOD_INFO_KEY) == NULL;
Failed |= XmlNodeAppend (InfoPlistRoot, "integer", PRELINK_INFO_INTEGER_ATTRIBUTES, KmodInfoStr) == NULL;
}

View File

@ -336,3 +336,42 @@ AsciiTrimWhiteSpace (
return String;
}
BOOLEAN
AsciiUint64ToLowerHex (
OUT CHAR8 *Buffer,
IN UINT32 BufferSize,
IN UINT64 Value
)
{
CONST UINT32 MaxShifts = (sizeof (UINT64) * 8) - 4;
UINT32 Index;
BOOLEAN Printed;
UINT8 Curr;
if (BufferSize < 4) {
return FALSE;
}
*Buffer++ = '0';
*Buffer++ = 'x';
if (Value > 0) {
BufferSize -= 2;
for (Printed = FALSE, Index = MaxShifts; Index <= MaxShifts; Index -= 4) {
Curr = (UINT8) ((Value >> Index) & 0xFU);
Printed |= Curr > 0;
if (Printed) {
*Buffer++ = "0123456789abcdef"[Curr];
if (--BufferSize == 0) {
return FALSE;
}
}
}
} else {
*Buffer++ = '0';
}
*Buffer++ = '\0';
return TRUE;
}