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