Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 44