Chameleon

Chameleon Svn Source Tree

Root/branches/Bungo/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 "";
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 */
25EFI_CHAR8* getUUIDFromString(const char *source)
26{
27 if (!source) return 0;
28 inti = strlen(source);
29 if (i != 36) { // e.g 00112233-4455-6677-8899-AABBCCDDEEFF
30 verbose("[ERROR] UUID='%s' has incorrect length=%d. Use format: 00112233-4455-6677-8899-AABBCCDDEEFF.\n", source, i);
31 return 0;
32 }
33
34char*p = (char *)source;
35charbuf[3];
36static EFI_CHAR8 uuid[UUID_LEN+1] = "";
37
38buf[2] = '\0';
39for (i = 0; i < UUID_LEN; i++) {
40//if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
41 if (!isxdigit(p[0]) || !isxdigit(p[1])) {
42verbose("[ERROR] UUID='%s' syntax error.\n", source);
43return 0;
44}
45buf[0] = *p++;
46buf[1] = *p++;
47uuid[i] = (unsigned char) strtoul(buf, NULL, 16);
48if ((*p == '-') && ((i % 2) == 1) && (i < UUID_LEN - 1)) {
49p++;
50}
51}
52uuid[UUID_LEN]='\0';
53
54if (*p != '\0') {
55verbose("[ERROR] UUID='%s' syntax error.\n", source);
56return 0;
57}
58return uuid;
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
102if ( (hexNibble >= '0' && hexNibble <= '9') ||
103(hexNibble >= 'A' && hexNibble <= 'F') ||
104(hexNibble >= 'a' && hexNibble <= 'f') ) {
105 hexByte[hexNibbleIdx++] = hexNibble;
106
107 // found both two nibbles, convert to binary
108 if (hexNibbleIdx == 2)
109 {
110 binChar = 0;
111
112 for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++) {
113 if (hexNibbleIdx > 0) {
114binChar = binChar << 4;
115}
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: 2531