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