-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathguid.c
52 lines (43 loc) · 1.37 KB
/
guid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2017 lambdadroid
#include "guid.h"
#include <efilib.h>
#define DECLARE_PARSE_HEX(n)\
static BOOLEAN parse_hex##n(const CHAR16 **input, UINT##n *u) {\
for (unsigned i = 0; i < sizeof(UINT##n) * 2; ++i) {\
CHAR16 c = *((*input)++);\
if (c >= '0' && c <= '9')\
*u = *u << 4 | (c - '0');\
else if (c >= 'A' && c <= 'F')\
*u = *u << 4 | (c - 'A' + 10);\
else if (c >= 'a' && c <= 'f')\
*u = *u << 4 | (c - 'a' + 10);\
else{\
return FALSE;\
}\
}\
return TRUE;\
}
DECLARE_PARSE_HEX(8)
DECLARE_PARSE_HEX(16)
DECLARE_PARSE_HEX(32)
#define GUID_LENGTH 36
#define CHECK_DASH(input) *((input)++) == '-'
BOOLEAN guid_parse(EFI_GUID *guid, const CHAR16 *input, UINTN length) {
if (length != GUID_LENGTH) {
return FALSE;
}
if (parse_hex32(&input, &guid->Data1)
&& CHECK_DASH(input) && parse_hex16(&input, &guid->Data2)
&& CHECK_DASH(input) && parse_hex16(&input, &guid->Data3)
&& CHECK_DASH(input) && parse_hex8(&input, &guid->Data4[0]) && parse_hex8(&input, &guid->Data4[1])
&& CHECK_DASH(input)) {
for (UINTN i = 2; i < 8; ++i) {
if (!parse_hex8(&input, &guid->Data4[i])) {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}