Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 78