Root/
Source at commit 943 created 13 years 1 month ago. By azimutz, Replace getc by getchar. | |
---|---|
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 | #ifndef OPTION_ROM␊ |
11 | /** Transform a 16 bytes hexadecimal value UUID to a string */␊ |
12 | const char * getStringFromUUID(const EFI_CHAR8* eUUID)␊ |
13 | {␊ |
14 | static char msg[UUID_LEN*2 + 8] = "";␊ |
15 | if (!eUUID) return "";␊ |
16 | const unsigned char * uuid = (unsigned char*) eUUID;␊ |
17 | sprintf(msg, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",␊ |
18 | ␉␉ uuid[0], uuid[1], uuid[2], uuid[3], ␊ |
19 | ␉␉ uuid[4], uuid[5], uuid[6], uuid[7],␊ |
20 | ␉␉ uuid[8], uuid[9], uuid[10],uuid[11],␊ |
21 | ␉␉ uuid[12],uuid[13],uuid[14],uuid[15]);␊ |
22 | return msg ;␊ |
23 | }␊ |
24 | #endif␊ |
25 | /** Parse an UUID string into an (EFI_CHAR8*) buffer */␊ |
26 | EFI_CHAR8* getUUIDFromString(const char *source)␊ |
27 | {␊ |
28 | if (!source) return 0;␊ |
29 | ␊ |
30 | ␉char␉*p = (char *)source;␊ |
31 | ␉int␉i;␊ |
32 | ␉char␉buf[3];␊ |
33 | ␉static EFI_CHAR8 uuid[UUID_LEN+1]="";␊ |
34 | ␊ |
35 | ␉buf[2] = '\0';␊ |
36 | ␉for (i=0; i<UUID_LEN; i++) {␊ |
37 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
38 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
39 | ␉␉␉return 0;␊ |
40 | ␉␉}␊ |
41 | ␉␉buf[0] = *p++;␊ |
42 | ␉␉buf[1] = *p++;␊ |
43 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
44 | ␉␉if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {␊ |
45 | ␉␉␉p++;␊ |
46 | ␉␉}␊ |
47 | ␉}␊ |
48 | ␉uuid[UUID_LEN]='\0';␊ |
49 | ␊ |
50 | ␉if (*p != '\0') {␊ |
51 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
52 | ␉␉return 0;␊ |
53 | ␉}␊ |
54 | ␉return uuid;␊ |
55 | }␊ |
56 | ␊ |
57 | /** XXX AsereBLN replace by strtoul */␊ |
58 | uint32_t ascii_hex_to_int(char *buff) ␊ |
59 | {␊ |
60 | ␉uint32_t␉value = 0, i, digit;␊ |
61 | ␉for(i = 0; i < 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 | ␊ |
77 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
78 | {␊ |
79 | int len;␊ |
80 | char hexNibble;␊ |
81 | char hexByte[2];␊ |
82 | uint8_t binChar;␊ |
83 | uint8_t *binStr;␊ |
84 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
85 | ␊ |
86 | len = strlen(hexStr);␊ |
87 | if (len > 1)␊ |
88 | {␊ |
89 | // the resulting binary will be the half size of the input hex string␊ |
90 | binStr = malloc(len / 2);␊ |
91 | binStrIdx = 0;␊ |
92 | hexNibbleIdx = 0;␊ |
93 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
94 | {␊ |
95 | hexNibble = hexStr[hexStrIdx];␊ |
96 | ␊ |
97 | // ignore all chars except valid hex numbers␊ |
98 | if (hexNibble >= '0' && hexNibble <= '9'␊ |
99 | || hexNibble >= 'A' && hexNibble <= 'F'␊ |
100 | || hexNibble >= 'a' && hexNibble <= 'f')␊ |
101 | {␊ |
102 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
103 | ␊ |
104 | // found both two nibbles, convert to binary␊ |
105 | if (hexNibbleIdx == 2)␊ |
106 | {␊ |
107 | binChar = 0;␊ |
108 | ␊ |
109 | for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
110 | {␊ |
111 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
112 | ␊ |
113 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
114 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
115 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
116 | }␊ |
117 | ␊ |
118 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
119 | hexNibbleIdx = 0;␊ |
120 | }␊ |
121 | }␊ |
122 | }␊ |
123 | *outLength = binStrIdx;␊ |
124 | return binStr;␊ |
125 | }␊ |
126 | else␊ |
127 | {␊ |
128 | *outLength = 0;␊ |
129 | return NULL;␊ |
130 | }␊ |
131 | }␊ |
132 | ␊ |
133 | // FIXME: can't use my original code here,␊ |
134 | // Ironically, trying to reuse convertHexStr2Binary() would RESET the system!␊ |
135 | /*␊ |
136 | static EFI_CHAR8* getUUIDFromString2(const char * szInUUID)␊ |
137 | {␊ |
138 | char szUUID[UUID_LEN+1], *p=szUUID;␊ |
139 | int size=0;␊ |
140 | void* ret;␊ |
141 | ␊ |
142 | if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;␊ |
143 | ␊ |
144 | while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;␊ |
145 | *p='\0';␊ |
146 | ret = convertHexStr2Binary(szUUID, &size);␊ |
147 | if (!ret || size!=UUID_LEN) ␊ |
148 | {␊ |
149 | verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);␊ |
150 | return (EFI_CHAR8*) 0;␊ |
151 | }␊ |
152 | return (EFI_CHAR8*) ret; // new allocated buffer containing the converted string to bin␊ |
153 | }␊ |
154 | */␊ |
155 |