-// Returns TRUE if *Input contains nothing but valid hexadecimal characters,
-// FALSE otherwise. Note that a leading "0x" is NOT acceptable in the input!
-BOOLEAN IsValidHex(CHAR16 *Input) {
- BOOLEAN IsHex = TRUE;
- UINTN i = 0;
-
- while ((Input[i] != L'\0') && IsHex) {
- if (!(((Input[i] >= L'0') && (Input[i] <= L'9')) ||
- ((Input[i] >= L'A') && (Input[i] <= L'F')) ||
- ((Input[i] >= L'a') && (Input[i] <= L'f')))) {
- IsHex = FALSE;
- }
- i++;
- } // while
- return IsHex;
-} // BOOLEAN IsValidHex()
-
-// Converts consecutive characters in the input string into a
-// number, interpreting the string as a hexadecimal number, starting
-// at the specified position and continuing for the specified number
-// of characters or until the end of the string, whichever is first.
-// NumChars must be between 1 and 16. Ignores invalid characters.
-UINT64 StrToHex(CHAR16 *Input, UINTN Pos, UINTN NumChars) {
- UINT64 retval = 0x00;
- UINTN NumDone = 0, InputLength;
- CHAR16 a;
-
- if ((Input == NULL) || (NumChars == 0) || (NumChars > 16)) {
- return 0;
- }
-
- InputLength = StrLen(Input);
- while ((Pos <= InputLength) && (NumDone < NumChars)) {
- a = Input[Pos];
- if ((a >= '0') && (a <= '9')) {
- retval *= 0x10;
- retval += (a - '0');
- NumDone++;
- }
- if ((a >= 'a') && (a <= 'f')) {
- retval *= 0x10;
- retval += (a - 'a' + 0x0a);
- NumDone++;
- }
- if ((a >= 'A') && (a <= 'F')) {
- retval *= 0x10;
- retval += (a - 'A' + 0x0a);
- NumDone++;
- }
- Pos++;
- } // while()
- return retval;
-} // StrToHex()
-
-// Returns TRUE if UnknownString can be interpreted as a GUID, FALSE otherwise.
-// Note that the input string must have no extraneous spaces and must be
-// conventionally formatted as a 36-character GUID, complete with dashes in
-// appropriate places.
-BOOLEAN IsGuid(CHAR16 *UnknownString) {
- UINTN Length, i;
- BOOLEAN retval = TRUE;
- CHAR16 a;
-
- if (UnknownString == NULL)
- return FALSE;
-
- Length = StrLen(UnknownString);
- if (Length != 36)
- return FALSE;
-
- for (i = 0; i < Length; i++) {
- a = UnknownString[i];
- if ((i == 8) || (i == 13) || (i == 18) || (i == 23)) {
- if (a != L'-')
- retval = FALSE;
- } else if (((a < L'a') || (a > L'f')) &&
- ((a < L'A') || (a > L'F')) &&
- ((a < L'0') && (a > L'9'))) {
- retval = FALSE;
- } // if/else if
- } // for
- return retval;
-} // BOOLEAN IsGuid()
-
-// Return the GUID as a string, suitable for display to the user. Note that the calling
-// function is responsible for freeing the allocated memory.
-CHAR16 * GuidAsString(EFI_GUID *GuidData) {
- CHAR16 *TheString;
-
- TheString = AllocateZeroPool(42 * sizeof(CHAR16));
- if (TheString != 0) {
- SPrint (TheString, 82, L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
- (UINTN)GuidData->Data1, (UINTN)GuidData->Data2, (UINTN)GuidData->Data3,
- (UINTN)GuidData->Data4[0], (UINTN)GuidData->Data4[1], (UINTN)GuidData->Data4[2],
- (UINTN)GuidData->Data4[3], (UINTN)GuidData->Data4[4], (UINTN)GuidData->Data4[5],
- (UINTN)GuidData->Data4[6], (UINTN)GuidData->Data4[7]);
- }
- return TheString;
-} // GuidAsString(EFI_GUID *GuidData)
-
-EFI_GUID StringAsGuid(CHAR16 * InString) {
- EFI_GUID Guid = NULL_GUID_VALUE;
-
- if (!IsGuid(InString)) {
- return Guid;
- }
-
- Guid.Data1 = (UINT32) StrToHex(InString, 0, 8);
- Guid.Data2 = (UINT16) StrToHex(InString, 9, 4);
- Guid.Data3 = (UINT16) StrToHex(InString, 14, 4);
- Guid.Data4[0] = (UINT8) StrToHex(InString, 19, 2);
- Guid.Data4[1] = (UINT8) StrToHex(InString, 21, 2);
- Guid.Data4[2] = (UINT8) StrToHex(InString, 23, 2);
- Guid.Data4[3] = (UINT8) StrToHex(InString, 26, 2);
- Guid.Data4[4] = (UINT8) StrToHex(InString, 28, 2);
- Guid.Data4[5] = (UINT8) StrToHex(InString, 30, 2);
- Guid.Data4[6] = (UINT8) StrToHex(InString, 32, 2);
- Guid.Data4[7] = (UINT8) StrToHex(InString, 34, 2);
-
- return Guid;
-} // EFI_GUID StringAsGuid()
-