Chameleon

Chameleon Svn Source Tree

Root/branches/slice/old749m/i386/libsaio/convert.c

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//Slice - LE byte order!
10#ifndef OPTION_ROM
11/** Transform a 16 bytes hexadecimal value UUID to a string */
12const char * getStringFromUUID(EFI_GUID* 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//Slice
19 uuid[3], uuid[2], uuid[1], uuid[0],
20 uuid[5], uuid[4], uuid[7], uuid[6],
21 uuid[8], uuid[9], uuid[10],uuid[11],
22 uuid[12],uuid[13],uuid[14],uuid[15]);
23 return msg ;
24}
25#endif
26/** Parse an UUID string into an (EFI_CHAR8*) buffer */
27EFI_GUID* getUUIDFromString(const char *source)
28{
29if (!source) return 0;
30static EFI_GUID uuid;
31char*p = (char *)source;
32uuid.time_low = strtoul(p, NULL, 16);
33p += 8;
34if (*p == '-' || *p==' ') p++;
35uuid.time_mid = strtoul(p, NULL, 16);
36p += 4;
37if (*p == '-' || *p==' ') p++;
38uuid.time_hi_and_version = strtoul(p, NULL, 16);
39p += 4;
40if (*p == '-' || *p==' ') p++;
41UInt16 clock = strtoul(p, NULL, 16);
42uuid.clock_seq_hi_and_reserved = (clock & 0xff00) >> 8;
43uuid.clock_seq_low = clock & 0xff;
44p += 4;
45if (*p == '-' || *p==' ') p++;
46charbuf[3];
47int i;
48buf[2] = '\0';
49for (i=0; i<6; i++) {
50if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
51verbose("[ERROR] UUID='%s' syntax error\n", source);
52return 0;
53}
54buf[0] = *p++;
55buf[1] = *p++;
56uuid.node[i] = (unsigned char) strtoul(buf, NULL, 16);
57if (*p == '-' && (i % 2) == 1 && i < 6) {
58p++;
59}
60}
61return &uuid;
62}
63#if OLD_METHOD
64{
65 if (!source) return 0;
66
67char*p = (char *)source;
68inti;
69charbuf[3];
70static EFI_CHAR8 uuid[UUID_LEN+1]="";
71
72buf[2] = '\0';
73for (i=0; i<UUID_LEN; i++) {
74if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
75verbose("[ERROR] UUID='%s' syntax error\n", source);
76return 0;
77}
78buf[0] = *p++;
79buf[1] = *p++;
80uuid[i] = (unsigned char) strtoul(buf, NULL, 16);
81if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {
82p++;
83}
84}
85uuid[UUID_LEN]='\0';
86
87if (*p != '\0') {
88verbose("[ERROR] UUID='%s' syntax error\n", source);
89return 0;
90}
91return uuid;
92}
93#endif
94
95/** XXX AsereBLN replace by strtoul */
96uint32_t ascii_hex_to_int(char *buff)
97{
98uint32_tvalue = 0, i, digit;
99for(i = 0; i < strlen(buff); i++)
100{
101if (buff[i] >= 48 && buff[i] <= 57)// '0' through '9'
102digit = buff[i] - 48;
103else if (buff[i] >= 65 && buff[i] <= 70)// 'A' through 'F'
104digit = buff[i] - 55;
105else if (buff[i] >= 97 && buff[i] <= 102)// 'a' through 'f'
106digit = buff[i] - 87;
107else
108return value;
109
110value = digit + 16 * value;
111}
112returnvalue;
113}
114
115void *convertHexStr2Binary(const char *hexStr, int *outLength)
116{
117 int len;
118 char hexNibble;
119 char hexByte[2];
120 uint8_t binChar;
121 uint8_t *binStr;
122 int hexStrIdx, binStrIdx, hexNibbleIdx;
123
124 len = strlen(hexStr);
125 if (len > 1)
126 {
127 // the resulting binary will be the half size of the input hex string
128 binStr = malloc(len / 2);
129 binStrIdx = 0;
130 hexNibbleIdx = 0;
131 for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)
132 {
133 hexNibble = hexStr[hexStrIdx];
134
135 // ignore all chars except valid hex numbers
136 if (hexNibble >= '0' && hexNibble <= '9'
137 || hexNibble >= 'A' && hexNibble <= 'F'
138 || hexNibble >= 'a' && hexNibble <= 'f')
139 {
140 hexByte[hexNibbleIdx++] = hexNibble;
141
142 // found both two nibbles, convert to binary
143 if (hexNibbleIdx == 2)
144 {
145 binChar = 0;
146
147 for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)
148 {
149 if (hexNibbleIdx > 0) binChar = binChar << 4;
150
151 if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';
152 else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);
153 else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);
154 }
155
156 binStr[binStrIdx++] = binChar;
157 hexNibbleIdx = 0;
158 }
159 }
160 }
161 *outLength = binStrIdx;
162 return binStr;
163 }
164 else
165 {
166 *outLength = 0;
167 return NULL;
168 }
169}
170
171// FIXME: can't use my original code here,
172// Ironically, trying to reuse convertHexStr2Binary() would RESET the system!
173/*
174static EFI_CHAR8* getUUIDFromString2(const char * szInUUID)
175{
176 char szUUID[UUID_LEN+1], *p=szUUID;
177 int size=0;
178 void* ret;
179
180 if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;
181
182 while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;
183 *p='\0';
184 ret = convertHexStr2Binary(szUUID, &size);
185 if (!ret || size!=UUID_LEN)
186 {
187 verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);
188 return (EFI_CHAR8*) 0;
189 }
190 return (EFI_CHAR8*) ret; // new allocated buffer containing the converted string to bin
191}
192*/
193

Archive Download this file

Revision: 1174