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