Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2679