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
82void *convertHexStr2Binary(const char *hexStr, int *outLength)
83{
84 int len;
85 char hexNibble;
86 char hexByte[2];
87 uint8_t binChar;
88 uint8_t *binStr;
89 int hexStrIdx, binStrIdx, hexNibbleIdx;
90
91 len = strlen(hexStr);
92 if (len > 1)
93 {
94 // the resulting binary will be the half size of the input hex string
95 binStr = malloc(len / 2);
96 if (!binStr) goto out;
97 binStrIdx = 0;
98 hexNibbleIdx = 0;
99 for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)
100 {
101 hexNibble = hexStr[hexStrIdx];
102
103 // ignore all chars except valid hex numbers
104 if ((hexNibble >= '0' && hexNibble <= '9')
105 || (hexNibble >= 'A' && hexNibble <= 'F')
106 || (hexNibble >= 'a' && hexNibble <= 'f'))
107 {
108 hexByte[hexNibbleIdx++] = hexNibble;
109
110 // found both two nibbles, convert to binary
111 if (hexNibbleIdx == 2)
112 {
113 binChar = 0;
114
115 for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)
116 {
117 if (hexNibbleIdx > 0) binChar = binChar << 4;
118
119 if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';
120 else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);
121 else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);
122 }
123
124 binStr[binStrIdx++] = binChar;
125 hexNibbleIdx = 0;
126 }
127 }
128 }
129 *outLength = binStrIdx;
130 return binStr;
131 }
132
133out:
134*outLength = 0;
135 return NULL;
136
137}

Archive Download this file

Revision: 2121