Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/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/** convert URL to a string */
11char * newStringFromURL(char* string)
12{
13char * buffer = newString(string);
14
15int maxlen = strlen(buffer);
16
17// replace "%20" by spaces.
18int len = 0;
19while (buffer[len] != '\0') {
20if (buffer[len] == '%' && buffer[len+1] == '2' && buffer[len+2] == '0')
21{
22buffer[len] = ' ';
23
24strlcpy(&buffer[len+1], &buffer[len+3], maxlen - (len+1));
25}
26len++;
27}
28
29//DBG("%s maxlen : %d, newlen : %lu\n",buffer, maxlen, strlen(buffer));
30// This will leak a little bit, i mean as you can see the final string will be slightly smaller than the allocated string buffer,
31// to fix this you can realloc the buffer, or do another newString(xxx) then free the first buffer, i choose to do nothing.
32
33 return buffer ;
34}
35
36/** Transform a 16 bytes hexadecimal value UUID to a string */
37const char * getStringFromUUID(const EFI_CHAR8* eUUID)
38{
39 static char msg[UUID_STR_LEN] = "";
40 if (!eUUID) return "";
41 const unsigned char * uuid = (unsigned char*) eUUID;
42 snprintf(msg, sizeof(msg), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
43 uuid[0], uuid[1], uuid[2], uuid[3],
44 uuid[4], uuid[5], uuid[6], uuid[7],
45 uuid[8], uuid[9], uuid[10],uuid[11],
46 uuid[12],uuid[13],uuid[14],uuid[15]);
47 return msg ;
48}
49
50/** Parse an UUID string into an (EFI_CHAR8*) buffer */
51EFI_CHAR8* getUUIDFromString(const char *source)
52{
53 if (!source) return 0;
54
55char*p = (char *)source;
56inti;
57charbuf[3];
58static EFI_CHAR8 uuid[UUID_LEN+1]="";
59
60buf[2] = '\0';
61for (i=0; i<UUID_LEN; i++) {
62if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
63verbose("[ERROR] UUID='%s' syntax error\n", source);
64return 0;
65}
66buf[0] = *p++;
67buf[1] = *p++;
68uuid[i] = (unsigned char) strtoul(buf, NULL, 16);
69if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {
70p++;
71}
72}
73uuid[UUID_LEN]='\0';
74
75if (*p != '\0') {
76verbose("[ERROR] UUID='%s' syntax error\n", source);
77return 0;
78}
79return uuid;
80}
81
82/** XXX AsereBLN replace by strtoul */
83#if UNUSED
84uint32_t ascii_hex_to_int(char *buff)
85{
86uint32_tvalue = 0, i, digit;
87for(i = 0; i < (uint32_t)strlen(buff); i++)
88{
89if (buff[i] >= 48 && buff[i] <= 57)// '0' through '9'
90digit = buff[i] - 48;
91else if (buff[i] >= 65 && buff[i] <= 70)// 'A' through 'F'
92digit = buff[i] - 55;
93else if (buff[i] >= 97 && buff[i] <= 102)// 'a' through 'f'
94digit = buff[i] - 87;
95else
96return value;
97
98value = digit + 16 * value;
99}
100returnvalue;
101}
102#endif
103
104void *convertHexStr2Binary(const char *hexStr, int *outLength)
105{
106 int len;
107 char hexNibble;
108 char hexByte[2];
109 uint8_t binChar;
110 uint8_t *binStr;
111 int hexStrIdx, binStrIdx, hexNibbleIdx;
112
113 len = strlen(hexStr);
114 if (len > 1)
115 {
116 // the resulting binary will be the half size of the input hex string
117 binStr = malloc(len / 2);
118 if (!binStr) {
119 *outLength = 0;
120 return NULL;
121 }
122 binStrIdx = 0;
123 hexNibbleIdx = 0;
124 for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)
125 {
126 hexNibble = hexStr[hexStrIdx];
127
128 // ignore all chars except valid hex numbers
129 if ((hexNibble >= '0' && hexNibble <= '9')
130 || (hexNibble >= 'A' && hexNibble <= 'F')
131 || (hexNibble >= 'a' && hexNibble <= 'f'))
132 {
133 hexByte[hexNibbleIdx++] = hexNibble;
134
135 // found both two nibbles, convert to binary
136 if (hexNibbleIdx == 2)
137 {
138 binChar = 0;
139
140 for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)
141 {
142 if (hexNibbleIdx > 0) binChar = binChar << 4;
143
144 if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';
145 else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);
146 else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);
147 }
148
149 binStr[binStrIdx++] = binChar;
150 hexNibbleIdx = 0;
151 }
152 }
153 }
154 *outLength = binStrIdx;
155 return binStr;
156 }
157 else
158 {
159 *outLength = 0;
160 return NULL;
161 }
162}

Archive Download this file

Revision: 2115