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