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_STR_LEN] = "";␊ |
14 | if (!eUUID) return "";␊ |
15 | const unsigned char * uuid = (unsigned char*) eUUID;␊ |
16 | snprintf(msg, sizeof(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 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
37 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
38 | ␉␉␉return 0;␊ |
39 | ␉␉}␊ |
40 | ␉␉buf[0] = *p++;␊ |
41 | ␉␉buf[1] = *p++;␊ |
42 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
43 | ␉␉if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {␊ |
44 | ␉␉␉p++;␊ |
45 | ␉␉}␊ |
46 | ␉}␊ |
47 | ␉uuid[UUID_LEN]='\0';␊ |
48 | ␊ |
49 | ␉if (*p != '\0') {␊ |
50 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
51 | ␉␉return 0;␊ |
52 | ␉}␊ |
53 | ␉return uuid;␊ |
54 | }␊ |
55 | ␊ |
56 | /** XXX AsereBLN replace by strtoul */␊ |
57 | #if UNUSED␊ |
58 | uint32_t ascii_hex_to_int(char *buff) ␊ |
59 | {␊ |
60 | ␉uint32_t␉value = 0, i, digit;␊ |
61 | ␉for(i = 0; i < (uint32_t)strlen(buff); i++)␊ |
62 | ␉{␊ |
63 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉␉// '0' through '9'␊ |
64 | ␉␉␉digit = buff[i] - 48;␉␊ |
65 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
66 | ␉␉␉digit = buff[i] - 55;␊ |
67 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
68 | ␉␉␉digit = buff[i] - 87;␊ |
69 | ␉␉else␊ |
70 | ␉␉␉return value;␊ |
71 | ␉␉␊ |
72 | ␉␉value = digit + 16 * value;␊ |
73 | ␉}␊ |
74 | ␉return␉value;␊ |
75 | }␊ |
76 | #endif␊ |
77 | ␊ |
78 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
79 | {␊ |
80 | int len;␊ |
81 | char hexNibble;␊ |
82 | char hexByte[2];␊ |
83 | uint8_t binChar;␊ |
84 | uint8_t *binStr;␊ |
85 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
86 | ␊ |
87 | len = strlen(hexStr);␊ |
88 | if (len > 1)␊ |
89 | {␊ |
90 | // the resulting binary will be the half size of the input hex string␊ |
91 | binStr = malloc(len / 2);␊ |
92 | if (!binStr) {␊ |
93 | *outLength = 0;␊ |
94 | return NULL;␊ |
95 | }␊ |
96 | binStrIdx = 0;␊ |
97 | hexNibbleIdx = 0;␊ |
98 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
99 | {␊ |
100 | hexNibble = hexStr[hexStrIdx];␊ |
101 | ␊ |
102 | // ignore all chars except valid hex numbers␊ |
103 | if ((hexNibble >= '0' && hexNibble <= '9')␊ |
104 | || (hexNibble >= 'A' && hexNibble <= 'F')␊ |
105 | || (hexNibble >= 'a' && hexNibble <= 'f'))␊ |
106 | {␊ |
107 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
108 | ␊ |
109 | // found both two nibbles, convert to binary␊ |
110 | if (hexNibbleIdx == 2)␊ |
111 | {␊ |
112 | binChar = 0;␊ |
113 | ␊ |
114 | for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
115 | {␊ |
116 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
117 | ␊ |
118 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
119 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
120 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
121 | }␊ |
122 | ␊ |
123 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
124 | hexNibbleIdx = 0;␊ |
125 | }␊ |
126 | }␊ |
127 | }␊ |
128 | *outLength = binStrIdx;␊ |
129 | return binStr;␊ |
130 | }␊ |
131 | else␊ |
132 | {␊ |
133 | *outLength = 0;␊ |
134 | return NULL;␊ |
135 | }␊ |
136 | } |