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 "";␊ |
15 | ␉const unsigned char *uuid = (unsigned char *) eUUID;␊ |
16 | ␉snprintf(msg, sizeof(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 | /* ======================================================= */␊ |
25 | ␊ |
26 | /** Parse an UUID string into an (EFI_CHAR8 *) buffer */␊ |
27 | EFI_CHAR8 *getUUIDFromString(const char *source)␊ |
28 | {␊ |
29 | ␉if (!source)␊ |
30 | ␉{␊ |
31 | ␉␉return 0;␊ |
32 | ␉}␊ |
33 | ␊ |
34 | ␉int␉i = strlen(source);␊ |
35 | ␉if (i != 36)␊ |
36 | ␉{ // e.g 00112233-4455-6677-8899-AABBCCDDEEFF␊ |
37 | ␉␉verbose("[ERROR] UUID='%s' has incorrect length=%d. Use format: 00112233-4455-6677-8899-AABBCCDDEEFF.\n", source, i);␊ |
38 | ␉␉return 0;␊ |
39 | ␉}␊ |
40 | ␊ |
41 | ␉char␉*p = (char *)source;␊ |
42 | ␉char␉buf[3];␊ |
43 | ␉static EFI_CHAR8 uuid[UUID_LEN+1] = "";␊ |
44 | ␊ |
45 | ␉buf[2] = '\0';␊ |
46 | ␉for (i = 0; i < UUID_LEN; i++)␊ |
47 | ␉{␊ |
48 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
49 | ␉␉{␊ |
50 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error.\n", source);␊ |
51 | ␉␉␉return 0;␊ |
52 | ␉␉}␊ |
53 | ␉␉buf[0] = *p++;␊ |
54 | ␉␉buf[1] = *p++;␊ |
55 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
56 | ␉␉if ((*p == '-') && ((i % 2) == 1) && (i < UUID_LEN - 1))␊ |
57 | ␉␉{␊ |
58 | ␉␉␉p++;␊ |
59 | ␉␉}␊ |
60 | ␉}␊ |
61 | ␉uuid[UUID_LEN]='\0';␊ |
62 | ␊ |
63 | ␉if (*p != '\0')␊ |
64 | ␉{␊ |
65 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
66 | ␉␉return 0;␊ |
67 | ␉}␊ |
68 | ␉return uuid;␊ |
69 | }␊ |
70 | ␊ |
71 | /* ======================================================= */␊ |
72 | ␊ |
73 | /** XXX AsereBLN replace by strtoul */␊ |
74 | uint32_t ascii_hex_to_int(char *buff) ␊ |
75 | {␊ |
76 | ␉uint32_t␉value = 0, i, digit;␊ |
77 | ␉for(i = 0; i < strlen(buff); i++)␊ |
78 | ␉{␊ |
79 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉// '0' through '9'␊ |
80 | ␉␉␉digit = buff[i] - 48;␊ |
81 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
82 | ␉␉␉digit = buff[i] - 55;␊ |
83 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
84 | ␉␉␉digit = buff[i] - 87;␊ |
85 | ␉␉else␊ |
86 | ␉␉␉return value;␊ |
87 | ␊ |
88 | ␉␉value = digit + 16 * value;␊ |
89 | ␉}␊ |
90 | ␉return␉value;␊ |
91 | }␊ |
92 | ␊ |
93 | /* ======================================================= */␊ |
94 | ␊ |
95 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
96 | {␊ |
97 | ␉int len;␊ |
98 | ␉char hexNibble;␊ |
99 | ␉char hexByte[2];␊ |
100 | ␉uint8_t binChar;␊ |
101 | ␉uint8_t *binStr;␊ |
102 | ␉int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
103 | ␊ |
104 | ␉len = strlen(hexStr);␊ |
105 | ␉if (len > 1)␊ |
106 | ␉{␊ |
107 | ␉␉// the resulting binary will be the half size of the input hex string␊ |
108 | ␉␉binStr = malloc(len / 2);␊ |
109 | ␉␉if (!binStr)␊ |
110 | ␉␉{␊ |
111 | ␉␉␉*outLength = 0;␊ |
112 | ␉␉␉return NULL;␊ |
113 | ␉␉}␊ |
114 | ␉␉bzero(binStr,len / 2 );␊ |
115 | ␊ |
116 | ␉␉binStrIdx = 0;␊ |
117 | ␉␉hexNibbleIdx = 0;␊ |
118 | ␉␉for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
119 | ␉␉{␊ |
120 | ␉␉␉hexNibble = hexStr[hexStrIdx];␊ |
121 | ␊ |
122 | ␉␉␉// ignore all chars except valid hex numbers␊ |
123 | ␉␉␉if ( (hexNibble >= '0' && hexNibble <= '9') ||␊ |
124 | ␉␉␉␉(hexNibble >= 'A' && hexNibble <= 'F') ||␊ |
125 | ␉␉␉␉(hexNibble >= 'a' && hexNibble <= 'f') )␊ |
126 | ␉␉␉{␊ |
127 | ␉␉␉␉hexByte[hexNibbleIdx++] = hexNibble;␊ |
128 | ␊ |
129 | ␉␉␉␉// found both two nibbles, convert to binary␊ |
130 | ␉␉␉␉if (hexNibbleIdx == 2)␊ |
131 | ␉␉␉␉{␊ |
132 | ␉␉␉␉␉binChar = 0;␊ |
133 | ␊ |
134 | ␉␉␉␉for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
135 | ␉␉␉␉{␊ |
136 | ␉␉␉␉␉if (hexNibbleIdx > 0)␊ |
137 | ␉␉␉␉␉{␊ |
138 | ␉␉␉␉␉␉binChar = binChar << 4;␊ |
139 | ␉␉␉␉␉}␊ |
140 | ␊ |
141 | ␉␉␉␉␉if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
142 | ␉␉␉␉␉else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
143 | ␉␉␉␉␉else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
144 | ␉␉␉␉}␊ |
145 | ␊ |
146 | ␉␉␉␉binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
147 | ␉␉␉␉hexNibbleIdx = 0;␊ |
148 | ␉␉␉␉}␊ |
149 | ␉␉␉}␊ |
150 | ␉␉}␊ |
151 | ␉␉*outLength = binStrIdx;␊ |
152 | ␉␉return binStr;␊ |
153 | ␉}␊ |
154 | ␉else␊ |
155 | ␉{␊ |
156 | ␉␉*outLength = 0;␊ |
157 | ␉␉return NULL;␊ |
158 | ␉}␊ |
159 | }␊ |
160 | ␊ |
161 | /* ======================================================= */␊ |
162 | ␊ |
163 | /*******************************************************************␊ |
164 | * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *␊ |
165 | * a binary. returns -1 in case of error (i.e. badly formed chars) *␊ |
166 | *******************************************************************/␊ |
167 | int hex2bin( const char *hex, uint8_t *bin, int len )␊ |
168 | {␊ |
169 | ␉char␉*p;␊ |
170 | ␉int␉i;␊ |
171 | ␉char␉buf[3];␊ |
172 | ␊ |
173 | ␉if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2)␊ |
174 | ␉{␊ |
175 | ␉␉printf("[ERROR] bin2hex input error\n");␊ |
176 | ␉␉return -1;␊ |
177 | ␉}␊ |
178 | ␊ |
179 | ␉buf[2] = '\0';␊ |
180 | ␉p = (char *) hex;␊ |
181 | ␊ |
182 | ␉for (i = 0; i < len; i++)␊ |
183 | ␉{␊ |
184 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
185 | ␉␉{␊ |
186 | ␉␉␉printf("[ERROR] bin2hex '%s' syntax error\n", hex);␊ |
187 | ␉␉␉return -2;␊ |
188 | ␉␉}␊ |
189 | ␉␉buf[0] = *p++;␊ |
190 | ␉␉buf[1] = *p++;␊ |
191 | ␉␉bin[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
192 | ␉}␊ |
193 | ␉return 0;␊ |
194 | }␊ |
195 | ␊ |
196 | /* ======================================================= */␊ |
197 | ␊ |
198 | // FIXME: can't use my original code here,␊ |
199 | // Ironically, trying to reuse convertHexStr2Binary() would RESET the system!␊ |
200 | /*␊ |
201 | static EFI_CHAR8 *getUUIDFromString2(const char * szInUUID)␊ |
202 | {␊ |
203 | char szUUID[UUID_LEN+1], *p=szUUID;␊ |
204 | int size=0;␊ |
205 | void* ret;␊ |
206 | ␊ |
207 | if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;␊ |
208 | ␊ |
209 | while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;␊ |
210 | *p='\0';␊ |
211 | ret = convertHexStr2Binary(szUUID, &size);␊ |
212 | if (!ret || size!=UUID_LEN) ␊ |
213 | {␊ |
214 | verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);␊ |
215 | return (EFI_CHAR8*) 0;␊ |
216 | }␊ |
217 | return (EFI_CHAR8 *) ret; // new allocated buffer containing the converted string to bin␊ |
218 | }␊ |
219 | */␊ |
220 | |