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 | ␊ |
11 | /* ======================================================= */␊ |
12 | ␊ |
13 | /** Transform a 16 bytes hexadecimal value UUID to a string */␊ |
14 | const char *getStringFromUUID(const uint8_t *eUUID)␊ |
15 | {␊ |
16 | ␉static char msg[UUID_LEN*2 + 8] = "";␊ |
17 | ␉if (!eUUID) return "";␊ |
18 | ␉const unsigned char *uuid = (unsigned char *) eUUID;␊ |
19 | ␉snprintf(msg, sizeof(msg), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",␊ |
20 | ␉␉ uuid[0], uuid[1], uuid[2], uuid[3], ␊ |
21 | ␉␉ uuid[4], uuid[5], uuid[6], uuid[7],␊ |
22 | ␉␉ uuid[8], uuid[9], uuid[10],uuid[11],␊ |
23 | ␉␉ uuid[12],uuid[13],uuid[14],uuid[15]);␊ |
24 | ␉return msg ;␊ |
25 | }␊ |
26 | ␊ |
27 | /* ======================================================= */␊ |
28 | ␊ |
29 | /** Parse an UUID string into an (EFI_CHAR8 *) buffer */␊ |
30 | EFI_CHAR8 *getUUIDFromString(const char *source)␊ |
31 | {␊ |
32 | ␉if (!source)␊ |
33 | ␉{␊ |
34 | ␉␉return 0;␊ |
35 | ␉}␊ |
36 | ␊ |
37 | ␉int␉i = strlen(source);␊ |
38 | ␉if (i != 36)␊ |
39 | ␉{ // e.g 00112233-4455-6677-8899-AABBCCDDEEFF␊ |
40 | ␉␉verbose("[ERROR] UUID='%s' has incorrect length=%d. Use format: 00112233-4455-6677-8899-AABBCCDDEEFF.\n", source, i);␊ |
41 | ␉␉return 0;␊ |
42 | ␉}␊ |
43 | ␊ |
44 | ␉char␉*p = (char *)source;␊ |
45 | ␉char␉buf[3];␊ |
46 | ␉static EFI_CHAR8 uuid[UUID_LEN+1] = "";␊ |
47 | ␊ |
48 | ␉buf[2] = '\0';␊ |
49 | ␉for (i = 0; i < UUID_LEN; i++)␊ |
50 | ␉{␊ |
51 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
52 | ␉␉{␊ |
53 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error.\n", source);␊ |
54 | ␉␉␉return 0;␊ |
55 | ␉␉}␊ |
56 | ␉␉buf[0] = *p++;␊ |
57 | ␉␉buf[1] = *p++;␊ |
58 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
59 | ␉␉if ((*p == '-') && ((i % 2) == 1) && (i < UUID_LEN - 1))␊ |
60 | ␉␉{␊ |
61 | ␉␉␉p++;␊ |
62 | ␉␉}␊ |
63 | ␉}␊ |
64 | ␉uuid[UUID_LEN]='\0';␊ |
65 | ␊ |
66 | ␉if (*p != '\0')␊ |
67 | ␉{␊ |
68 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
69 | ␉␉return 0;␊ |
70 | ␉}␊ |
71 | ␉return uuid;␊ |
72 | }␊ |
73 | ␊ |
74 | /* ======================================================= */␊ |
75 | ␊ |
76 | /** XXX AsereBLN replace by strtoul */␊ |
77 | uint32_t ascii_hex_to_int(char *buff) ␊ |
78 | {␊ |
79 | ␉uint32_t␉value = 0, i, digit;␊ |
80 | ␉for(i = 0; i < strlen(buff); i++)␊ |
81 | ␉{␊ |
82 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉// '0' through '9'␊ |
83 | ␉␉␉digit = buff[i] - 48;␊ |
84 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
85 | ␉␉␉digit = buff[i] - 55;␊ |
86 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
87 | ␉␉␉digit = buff[i] - 87;␊ |
88 | ␉␉else␊ |
89 | ␉␉␉return value;␊ |
90 | ␊ |
91 | ␉␉value = digit + 16 * value;␊ |
92 | ␉}␊ |
93 | ␉return␉value;␊ |
94 | }␊ |
95 | ␊ |
96 | /* ======================================================= */␊ |
97 | ␊ |
98 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
99 | {␊ |
100 | ␉int len;␊ |
101 | ␉char hexNibble;␊ |
102 | ␉char hexByte[2];␊ |
103 | ␉uint8_t binChar;␊ |
104 | ␉uint8_t *binStr;␊ |
105 | ␉int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
106 | ␊ |
107 | ␉len = strlen(hexStr);␊ |
108 | ␉if (len > 1)␊ |
109 | ␉{␊ |
110 | ␉␉// the resulting binary will be the half size of the input hex string␊ |
111 | ␉␉binStr = malloc(len / 2);␊ |
112 | ␉␉if (!binStr)␊ |
113 | ␉␉{␊ |
114 | ␉␉␉*outLength = 0;␊ |
115 | ␉␉␉return NULL;␊ |
116 | ␉␉}␊ |
117 | ␉␉bzero(binStr,len / 2 );␊ |
118 | ␊ |
119 | ␉␉binStrIdx = 0;␊ |
120 | ␉␉hexNibbleIdx = 0;␊ |
121 | ␉␉for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
122 | ␉␉{␊ |
123 | ␉␉␉hexNibble = hexStr[hexStrIdx];␊ |
124 | ␊ |
125 | ␉␉␉// ignore all chars except valid hex numbers␊ |
126 | ␉␉␉if ( (hexNibble >= '0' && hexNibble <= '9') ||␊ |
127 | ␉␉␉␉(hexNibble >= 'A' && hexNibble <= 'F') ||␊ |
128 | ␉␉␉␉(hexNibble >= 'a' && hexNibble <= 'f') )␊ |
129 | ␉␉␉{␊ |
130 | ␉␉␉␉hexByte[hexNibbleIdx++] = hexNibble;␊ |
131 | ␊ |
132 | ␉␉␉␉// found both two nibbles, convert to binary␊ |
133 | ␉␉␉␉if (hexNibbleIdx == 2)␊ |
134 | ␉␉␉␉{␊ |
135 | ␉␉␉␉␉binChar = 0;␊ |
136 | ␊ |
137 | ␉␉␉␉for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
138 | ␉␉␉␉{␊ |
139 | ␉␉␉␉␉if (hexNibbleIdx > 0)␊ |
140 | ␉␉␉␉␉{␊ |
141 | ␉␉␉␉␉␉binChar = binChar << 4;␊ |
142 | ␉␉␉␉␉}␊ |
143 | ␊ |
144 | ␉␉␉␉␉if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
145 | ␉␉␉␉␉else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
146 | ␉␉␉␉␉else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
147 | ␉␉␉␉}␊ |
148 | ␊ |
149 | ␉␉␉␉binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
150 | ␉␉␉␉hexNibbleIdx = 0;␊ |
151 | ␉␉␉␉}␊ |
152 | ␉␉␉}␊ |
153 | ␉␉}␊ |
154 | ␉␉*outLength = binStrIdx;␊ |
155 | ␉␉return binStr;␊ |
156 | ␉}␊ |
157 | ␉else␊ |
158 | ␉{␊ |
159 | ␉␉*outLength = 0;␊ |
160 | ␉␉return NULL;␊ |
161 | ␉}␊ |
162 | }␊ |
163 | ␊ |
164 | /* ======================================================= */␊ |
165 | ␊ |
166 | /*******************************************************************␊ |
167 | * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *␊ |
168 | * a binary. returns -1 in case of error (i.e. badly formed chars) *␊ |
169 | *******************************************************************/␊ |
170 | int hex2bin( const char *hex, uint8_t *bin, int len )␊ |
171 | {␊ |
172 | ␉char␉*p;␊ |
173 | ␉int␉i;␊ |
174 | ␉char␉buf[3];␊ |
175 | ␊ |
176 | ␉if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2)␊ |
177 | ␉{␊ |
178 | ␉␉printf("[ERROR] bin2hex input error\n");␊ |
179 | ␉␉return -1;␊ |
180 | ␉}␊ |
181 | ␊ |
182 | ␉buf[2] = '\0';␊ |
183 | ␉p = (char *) hex;␊ |
184 | ␊ |
185 | ␉for (i = 0; i < len; i++)␊ |
186 | ␉{␊ |
187 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
188 | ␉␉{␊ |
189 | ␉␉␉printf("[ERROR] bin2hex '%s' syntax error\n", hex);␊ |
190 | ␉␉␉return -2;␊ |
191 | ␉␉}␊ |
192 | ␉␉buf[0] = *p++;␊ |
193 | ␉␉buf[1] = *p++;␊ |
194 | ␉␉bin[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
195 | ␉}␊ |
196 | ␉return 0;␊ |
197 | }␊ |
198 | ␊ |
199 | /* ======================================================= */␊ |
200 | |