1 | /*␊ |
2 | * Convert.c␊ |
3 | * Implement conversion utility functions␊ |
4 | * Create UUID parsing functions and gather other conversion routines␊ |
5 | * --Rek␊ |
6 | */␊ |
7 | ␊ |
8 | #include "convert.h"␊ |
9 | ␊ |
10 | /** Transform a 16 bytes hexadecimal value UUID to a string */␊ |
11 | const char* getStringFromUUID(const EFI_CHAR8* eUUID)␊ |
12 | {␊ |
13 | static char msg[UUID_LEN*2 + 8] = "";␊ |
14 | if (!eUUID) return NULL;␊ |
15 | const unsigned char * uuid = (unsigned char*) eUUID;␊ |
16 | sprintf(msg, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",␊ |
17 | ␉␉ uuid[0], uuid[1], uuid[2], uuid[3],␊ |
18 | ␉␉ uuid[4], uuid[5], uuid[6], uuid[7],␊ |
19 | ␉␉ uuid[8], uuid[9], uuid[10],uuid[11],␊ |
20 | ␉␉ uuid[12],uuid[13],uuid[14],uuid[15]);␊ |
21 | return msg;␊ |
22 | }␊ |
23 | ␊ |
24 | /** Parse an UUID string into an (EFI_CHAR8*) buffer */␊ |
25 | EFI_CHAR8* getUUIDFromString(const char *source)␊ |
26 | {␊ |
27 | ␉if (! source) return NULL;␊ |
28 | ␊ |
29 | ␉char* p = (char*) source;␊ |
30 | ␉int i;␊ |
31 | ␉char buf[3];␊ |
32 | ␉static EFI_CHAR8 uuid[UUID_LEN+1]="";␊ |
33 | ␊ |
34 | ␉buf[2] = '\0';␊ |
35 | ␉for (i=0; i < UUID_LEN; i++) {␊ |
36 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
37 | ␉␉␉return NULL;␊ |
38 | ␉␉}␊ |
39 | ␉␉buf[0] = *p++;␊ |
40 | ␉␉buf[1] = *p++;␊ |
41 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
42 | ␉␉if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {␊ |
43 | ␉␉␉p++;␊ |
44 | ␉␉}␊ |
45 | ␉}␊ |
46 | ␉uuid[UUID_LEN]='\0';␊ |
47 | ␊ |
48 | ␉if (*p != '\0') {␊ |
49 | ␉␉return NULL;␊ |
50 | ␉}␊ |
51 | ␉return uuid;␊ |
52 | }␊ |
53 | ␊ |
54 | /** XXX AsereBLN replace by strtoul */␊ |
55 | uint32_t ascii_hex_to_int(char *buff)␊ |
56 | {␊ |
57 | ␉uint32_t␉value = 0, i, digit;␊ |
58 | ␉for(i = 0; i < strlen(buff); i++)␊ |
59 | ␉{␊ |
60 | ␉␉if (buff[i] >= 48 && buff[i] <= 57) // '0' through '9'␊ |
61 | ␉␉␉digit = buff[i] - 48;␊ |
62 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70) // 'A' through 'F'␊ |
63 | ␉␉␉digit = buff[i] - 55;␊ |
64 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102) // 'a' through 'f'␊ |
65 | ␉␉␉digit = buff[i] - 87;␊ |
66 | ␉␉else␊ |
67 | ␉␉␉return value;␊ |
68 | ␊ |
69 | ␉␉value = digit + 16 * value;␊ |
70 | ␉}␊ |
71 | ␉return␉value;␊ |
72 | }␊ |
73 | ␊ |
74 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
75 | {␊ |
76 | int len;␊ |
77 | char hexNibble;␊ |
78 | char hexByte[2];␊ |
79 | uint8_t binChar;␊ |
80 | uint8_t *binStr;␊ |
81 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
82 | ␊ |
83 | len = strlen(hexStr);␊ |
84 | if (len > 1)␊ |
85 | {␊ |
86 | // the resulting binary will be the half size of the input hex string␊ |
87 | binStr = malloc(len / 2);␊ |
88 | binStrIdx = 0;␊ |
89 | hexNibbleIdx = 0;␊ |
90 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
91 | {␊ |
92 | hexNibble = hexStr[hexStrIdx];␊ |
93 | ␊ |
94 | // ignore all chars except valid hex numbers␊ |
95 | if (hexNibble >= '0' && hexNibble <= '9'␊ |
96 | || hexNibble >= 'A' && hexNibble <= 'F'␊ |
97 | || hexNibble >= 'a' && hexNibble <= 'f')␊ |
98 | {␊ |
99 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
100 | ␊ |
101 | // found both two nibbles, convert to binary␊ |
102 | if (hexNibbleIdx == 2)␊ |
103 | {␊ |
104 | binChar = 0;␊ |
105 | ␊ |
106 | for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
107 | {␊ |
108 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
109 | ␊ |
110 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
111 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
112 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
113 | }␊ |
114 | ␊ |
115 | binStr[binStrIdx++] = binChar;␊ |
116 | hexNibbleIdx = 0;␊ |
117 | }␊ |
118 | }␊ |
119 | }␊ |
120 | *outLength = binStrIdx;␊ |
121 | return binStr;␊ |
122 | }␊ |
123 | else␊ |
124 | {␊ |
125 | *outLength = 0;␊ |
126 | return NULL;␊ |
127 | }␊ |
128 | }␊ |
129 | ␊ |
130 | // FIXME: can't use my original code here,␊ |
131 | // Ironically, trying to reuse convertHexStr2Binary() would RESET the system!␊ |
132 | /*␊ |
133 | static EFI_CHAR8* getUUIDFromString2(const char * szInUUID)␊ |
134 | {␊ |
135 | char szUUID[UUID_LEN+1], *p=szUUID;␊ |
136 | int size=0;␊ |
137 | void* ret;␊ |
138 | ␊ |
139 | if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;␊ |
140 | ␊ |
141 | while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;␊ |
142 | *p='\0';␊ |
143 | ret = convertHexStr2Binary(szUUID, &size);␊ |
144 | if (!ret || size!=UUID_LEN)␊ |
145 | {␊ |
146 | verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);␊ |
147 | return (EFI_CHAR8*) 0;␊ |
148 | }␊ |
149 | return (EFI_CHAR8*) ret; // new allocated buffer containing the converted string to bin␊ |
150 | }␊ |
151 | */␊ |
152 | |