From 4d9d4bbd1a26e4305ab039ad9a395eee42735041 Mon Sep 17 00:00:00 2001 From: vit9696 Date: Mon, 25 Mar 2019 19:39:32 +0300 Subject: [PATCH] OcAppleKernelLib: Fix number to string conversion --- Include/Library/OcStringLib.h | 14 ++++++++ Library/OcAppleKernelLib/PrelinkedContext.c | 36 +++---------------- Library/OcStringLib/OcAsciiLib.c | 39 +++++++++++++++++++++ 3 files changed, 58 insertions(+), 31 deletions(-) diff --git a/Include/Library/OcStringLib.h b/Include/Library/OcStringLib.h index 736c2cbe..0ec5de7a 100755 --- a/Include/Library/OcStringLib.h +++ b/Include/Library/OcStringLib.h @@ -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_ diff --git a/Library/OcAppleKernelLib/PrelinkedContext.c b/Library/OcAppleKernelLib/PrelinkedContext.c index 02628668..bc97d277 100644 --- a/Library/OcAppleKernelLib/PrelinkedContext.c +++ b/Library/OcAppleKernelLib/PrelinkedContext.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #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; } diff --git a/Library/OcStringLib/OcAsciiLib.c b/Library/OcStringLib/OcAsciiLib.c index 88667564..162a4e79 100755 --- a/Library/OcStringLib/OcAsciiLib.c +++ b/Library/OcStringLib/OcAsciiLib.c @@ -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; +} +