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 | 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 0;␊ |
28 | int␉i = strlen(source);␊ |
29 | if (i != 36) { // e.g 00112233-4455-6677-8899-AABBCCDDEEFF␊ |
30 | verbose("[ERROR] UUID='%s' has incorrect length=%d. Use format: 00112233-4455-6677-8899-AABBCCDDEEFF.\n", source, i);␊ |
31 | return 0;␊ |
32 | }␊ |
33 | ␊ |
34 | ␉char␉*p = (char *)source;␊ |
35 | ␉char␉buf[3];␊ |
36 | ␉static EFI_CHAR8 uuid[UUID_LEN+1] = "";␊ |
37 | ␊ |
38 | ␉buf[2] = '\0';␊ |
39 | ␉for (i = 0; i < UUID_LEN; i++)␊ |
40 | ␉{␊ |
41 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
42 | ␉␉{␊ |
43 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error.\n", source);␊ |
44 | ␉␉␉return 0;␊ |
45 | ␉␉}␊ |
46 | ␉␉buf[0] = *p++;␊ |
47 | ␉␉buf[1] = *p++;␊ |
48 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
49 | ␉␉if ((*p == '-') && ((i % 2) == 1) && (i < UUID_LEN - 1))␊ |
50 | ␉␉{␊ |
51 | ␉␉␉p++;␊ |
52 | ␉␉}␊ |
53 | ␉}␊ |
54 | ␉uuid[UUID_LEN]='\0';␊ |
55 | ␊ |
56 | ␉if (*p != '\0')␊ |
57 | ␉{␊ |
58 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
59 | ␉␉return 0;␊ |
60 | ␉}␊ |
61 | ␉return uuid;␊ |
62 | }␊ |
63 | ␊ |
64 | /** XXX AsereBLN replace by strtoul */␊ |
65 | uint32_t ascii_hex_to_int(char *buff) ␊ |
66 | {␊ |
67 | ␉uint32_t␉value = 0, i, digit;␊ |
68 | ␉for(i = 0; i < strlen(buff); i++)␊ |
69 | ␉{␊ |
70 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉␉// '0' through '9'␊ |
71 | ␉␉␉digit = buff[i] - 48;␉␊ |
72 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
73 | ␉␉␉digit = buff[i] - 55;␊ |
74 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
75 | ␉␉␉digit = buff[i] - 87;␊ |
76 | ␉␉else␊ |
77 | ␉␉␉return value;␊ |
78 | ␊ |
79 | ␉␉value = digit + 16 * value;␊ |
80 | ␉}␊ |
81 | ␉return␉value;␊ |
82 | }␊ |
83 | ␊ |
84 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
85 | {␊ |
86 | int len;␊ |
87 | char hexNibble;␊ |
88 | char hexByte[2];␊ |
89 | uint8_t binChar;␊ |
90 | uint8_t *binStr;␊ |
91 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
92 | ␊ |
93 | len = strlen(hexStr);␊ |
94 | if (len > 1)␊ |
95 | {␊ |
96 | // the resulting binary will be the half size of the input hex string␊ |
97 | binStr = malloc(len / 2);␊ |
98 | ␊ |
99 | binStrIdx = 0;␊ |
100 | hexNibbleIdx = 0;␊ |
101 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
102 | {␊ |
103 | hexNibble = hexStr[hexStrIdx];␊ |
104 | ␊ |
105 | ␉// ignore all chars except valid hex numbers␊ |
106 | ␉if ( (hexNibble >= '0' && hexNibble <= '9') ||␊ |
107 | ␉␉(hexNibble >= 'A' && hexNibble <= 'F') ||␊ |
108 | ␉␉␉␉(hexNibble >= 'a' && hexNibble <= 'f') )␊ |
109 | ␉␉␉{␊ |
110 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
111 | ␊ |
112 | // found both two nibbles, convert to binary␊ |
113 | if (hexNibbleIdx == 2)␊ |
114 | {␊ |
115 | binChar = 0;␊ |
116 | ␊ |
117 | ␉␉␉␉for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
118 | ␉␉␉␉{␊ |
119 | ␉␉␉␉␉if (hexNibbleIdx > 0)␊ |
120 | ␉␉␉␉␉{␊ |
121 | ␉␉␉binChar = binChar << 4;␊ |
122 | ␉␉}␊ |
123 | ␊ |
124 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
125 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
126 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
127 | }␊ |
128 | ␊ |
129 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
130 | hexNibbleIdx = 0;␊ |
131 | }␊ |
132 | }␊ |
133 | }␊ |
134 | *outLength = binStrIdx;␊ |
135 | return binStr;␊ |
136 | }␊ |
137 | else␊ |
138 | {␊ |
139 | *outLength = 0;␊ |
140 | return NULL;␊ |
141 | }␊ |
142 | }␊ |
143 | ␊ |
144 | // FIXME: can't use my original code here,␊ |
145 | // Ironically, trying to reuse convertHexStr2Binary() would RESET the system!␊ |
146 | /*␊ |
147 | static EFI_CHAR8* getUUIDFromString2(const char * szInUUID)␊ |
148 | {␊ |
149 | char szUUID[UUID_LEN+1], *p=szUUID;␊ |
150 | int size=0;␊ |
151 | void* ret;␊ |
152 | ␊ |
153 | if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;␊ |
154 | ␊ |
155 | while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;␊ |
156 | *p='\0';␊ |
157 | ret = convertHexStr2Binary(szUUID, &size);␊ |
158 | if (!ret || size!=UUID_LEN) ␊ |
159 | {␊ |
160 | verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);␊ |
161 | return (EFI_CHAR8*) 0;␊ |
162 | }␊ |
163 | return (EFI_CHAR8*) ret; // new allocated buffer containing the converted string to bin␊ |
164 | }␊ |
165 | */␊ |
166 | |