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 | //Slice - LE byte order!␊ |
10 | #ifndef OPTION_ROM␊ |
11 | /** Transform a 16 bytes hexadecimal value UUID to a string */␊ |
12 | const char * getStringFromUUID(EFI_GUID* eUUID)␊ |
13 | {␊ |
14 | static char msg[UUID_LEN*2 + 8] = "";␊ |
15 | if (!eUUID) return "";␊ |
16 | const unsigned char * uuid = (unsigned char*) eUUID;␊ |
17 | sprintf(msg, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",␊ |
18 | //Slice␊ |
19 | ␉␉ uuid[3], uuid[2], uuid[1], uuid[0], ␊ |
20 | ␉␉ uuid[5], uuid[4], uuid[7], uuid[6],␊ |
21 | ␉␉ uuid[8], uuid[9], uuid[10],uuid[11],␊ |
22 | ␉␉ uuid[12],uuid[13],uuid[14],uuid[15]);␊ |
23 | return msg ;␊ |
24 | }␊ |
25 | #endif␊ |
26 | /** Parse an UUID string into an (EFI_CHAR8*) buffer */␊ |
27 | EFI_GUID* getUUIDFromString(const char *source)␊ |
28 | {␊ |
29 | ␉if (!source) return 0;␊ |
30 | ␉static EFI_GUID uuid;␊ |
31 | ␉char␉*p = (char *)source;␊ |
32 | ␉uuid.time_low = strtoul(p, NULL, 16);␊ |
33 | ␉p += 8;␊ |
34 | ␉if (*p == '-' || *p==' ') p++;␊ |
35 | ␉uuid.time_mid = strtoul(p, NULL, 16);␊ |
36 | ␉p += 4;␊ |
37 | ␉if (*p == '-' || *p==' ') p++;␊ |
38 | ␉uuid.time_hi_and_version = strtoul(p, NULL, 16);␊ |
39 | ␉p += 4;␊ |
40 | ␉if (*p == '-' || *p==' ') p++;␊ |
41 | ␉UInt16 clock = strtoul(p, NULL, 16);␊ |
42 | ␉uuid.clock_seq_hi_and_reserved = (clock & 0xff00) >> 8;␊ |
43 | ␉uuid.clock_seq_low = clock & 0xff;␊ |
44 | ␉p += 4;␊ |
45 | ␉if (*p == '-' || *p==' ') p++;␊ |
46 | ␉char␉buf[3];␊ |
47 | ␉int i;␊ |
48 | ␉buf[2] = '\0';␊ |
49 | ␉for (i=0; i<6; i++) {␊ |
50 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
51 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
52 | ␉␉␉return 0;␊ |
53 | ␉␉}␊ |
54 | ␉␉buf[0] = *p++;␊ |
55 | ␉␉buf[1] = *p++;␊ |
56 | ␉␉uuid.node[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
57 | ␉␉if (*p == '-' && (i % 2) == 1 && i < 6) {␊ |
58 | ␉␉␉p++;␊ |
59 | ␉␉}␊ |
60 | ␉}␊ |
61 | ␉return &uuid;␊ |
62 | }␊ |
63 | #if OLD_METHOD␊ |
64 | {␊ |
65 | if (!source) return 0;␊ |
66 | ␊ |
67 | ␉char␉*p = (char *)source;␊ |
68 | ␉int␉i;␊ |
69 | ␉char␉buf[3];␊ |
70 | ␉static EFI_CHAR8 uuid[UUID_LEN+1]="";␊ |
71 | ␊ |
72 | ␉buf[2] = '\0';␊ |
73 | ␉for (i=0; i<UUID_LEN; i++) {␊ |
74 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
75 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
76 | ␉␉␉return 0;␊ |
77 | ␉␉}␊ |
78 | ␉␉buf[0] = *p++;␊ |
79 | ␉␉buf[1] = *p++;␊ |
80 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
81 | ␉␉if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {␊ |
82 | ␉␉␉p++;␊ |
83 | ␉␉}␊ |
84 | ␉}␊ |
85 | ␉uuid[UUID_LEN]='\0';␊ |
86 | ␊ |
87 | ␉if (*p != '\0') {␊ |
88 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
89 | ␉␉return 0;␊ |
90 | ␉}␊ |
91 | ␉return uuid;␊ |
92 | }␊ |
93 | #endif␊ |
94 | ␊ |
95 | /** XXX AsereBLN replace by strtoul */␊ |
96 | uint32_t ascii_hex_to_int(char *buff) ␊ |
97 | {␊ |
98 | ␉uint32_t␉value = 0, i, digit;␊ |
99 | ␉for(i = 0; i < strlen(buff); i++)␊ |
100 | ␉{␊ |
101 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉␉// '0' through '9'␊ |
102 | ␉␉␉digit = buff[i] - 48;␉␊ |
103 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
104 | ␉␉␉digit = buff[i] - 55;␊ |
105 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
106 | ␉␉␉digit = buff[i] - 87;␊ |
107 | ␉␉else␊ |
108 | ␉␉␉return value;␊ |
109 | ␉␉␊ |
110 | ␉␉value = digit + 16 * value;␊ |
111 | ␉}␊ |
112 | ␉return␉value;␊ |
113 | }␊ |
114 | ␊ |
115 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
116 | {␊ |
117 | int len;␊ |
118 | char hexNibble;␊ |
119 | char hexByte[2];␊ |
120 | uint8_t binChar;␊ |
121 | uint8_t *binStr;␊ |
122 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
123 | ␊ |
124 | len = strlen(hexStr);␊ |
125 | if (len > 1)␊ |
126 | {␊ |
127 | // the resulting binary will be the half size of the input hex string␊ |
128 | binStr = malloc(len / 2);␊ |
129 | binStrIdx = 0;␊ |
130 | hexNibbleIdx = 0;␊ |
131 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
132 | {␊ |
133 | hexNibble = hexStr[hexStrIdx];␊ |
134 | ␊ |
135 | // ignore all chars except valid hex numbers␊ |
136 | if (hexNibble >= '0' && hexNibble <= '9'␊ |
137 | || hexNibble >= 'A' && hexNibble <= 'F'␊ |
138 | || hexNibble >= 'a' && hexNibble <= 'f')␊ |
139 | {␊ |
140 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
141 | ␊ |
142 | // found both two nibbles, convert to binary␊ |
143 | if (hexNibbleIdx == 2)␊ |
144 | {␊ |
145 | binChar = 0;␊ |
146 | ␊ |
147 | for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
148 | {␊ |
149 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
150 | ␊ |
151 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
152 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
153 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
154 | }␊ |
155 | ␊ |
156 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
157 | hexNibbleIdx = 0;␊ |
158 | }␊ |
159 | }␊ |
160 | }␊ |
161 | *outLength = binStrIdx;␊ |
162 | return binStr;␊ |
163 | }␊ |
164 | else␊ |
165 | {␊ |
166 | *outLength = 0;␊ |
167 | return NULL;␊ |
168 | }␊ |
169 | }␊ |
170 | ␊ |
171 | // FIXME: can't use my original code here,␊ |
172 | // Ironically, trying to reuse convertHexStr2Binary() would RESET the system!␊ |
173 | /*␊ |
174 | static EFI_CHAR8* getUUIDFromString2(const char * szInUUID)␊ |
175 | {␊ |
176 | char szUUID[UUID_LEN+1], *p=szUUID;␊ |
177 | int size=0;␊ |
178 | void* ret;␊ |
179 | ␊ |
180 | if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;␊ |
181 | ␊ |
182 | while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;␊ |
183 | *p='\0';␊ |
184 | ret = convertHexStr2Binary(szUUID, &size);␊ |
185 | if (!ret || size!=UUID_LEN) ␊ |
186 | {␊ |
187 | verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);␊ |
188 | return (EFI_CHAR8*) 0;␊ |
189 | }␊ |
190 | return (EFI_CHAR8*) ret; // new allocated buffer containing the converted string to bin␊ |
191 | }␊ |
192 | */␊ |
193 | |