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/** Transform a 16 bytes hexadecimal value UUID to a string */
11const char * getStringFromUUID(const EFI_CHAR8* eUUID)
12{
13 static char msg[UUID_STR_LEN] = "";
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
29char*p = (char *)source;
30inti;
31charbuf[3];
32static EFI_CHAR8 uuid[UUID_LEN+1]="";
33
34buf[2] = '\0';
35for (i=0; i<UUID_LEN; i++) {
36if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
37verbose("[ERROR] UUID='%s' syntax error\n", source);
38return 0;
39}
40buf[0] = *p++;
41buf[1] = *p++;
42uuid[i] = (unsigned char) strtoul(buf, NULL, 16);
43if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {
44p++;
45}
46}
47uuid[UUID_LEN]='\0';
48
49if (*p != '\0') {
50verbose("[ERROR] UUID='%s' syntax error\n", source);
51return 0;
52}
53return uuid;
54}
55
56/** XXX AsereBLN replace by strtoul */
57uint32_t ascii_hex_to_int(char *buff)
58{
59uint32_tvalue = 0, i, digit;
60for(i = 0; i < (uint32_t)strlen(buff); i++)
61{
62if (buff[i] >= 48 && buff[i] <= 57)// '0' through '9'
63digit = buff[i] - 48;
64else if (buff[i] >= 65 && buff[i] <= 70)// 'A' through 'F'
65digit = buff[i] - 55;
66else if (buff[i] >= 97 && buff[i] <= 102)// 'a' through 'f'
67digit = buff[i] - 87;
68else
69return value;
70
71value = digit + 16 * value;
72}
73returnvalue;
74}
75
76void *convertHexStr2Binary(const char *hexStr, int *outLength)
77{
78 int len;
79 char hexNibble;
80 char hexByte[2];
81 uint8_t binChar;
82 uint8_t *binStr;
83 int hexStrIdx, binStrIdx, hexNibbleIdx;
84
85 len = strlen(hexStr);
86 if (len > 1)
87 {
88 // the resulting binary will be the half size of the input hex string
89 binStr = malloc(len / 2);
90 if (!binStr) {
91 *outLength = 0;
92 return NULL;
93 }
94 binStrIdx = 0;
95 hexNibbleIdx = 0;
96 for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)
97 {
98 hexNibble = hexStr[hexStrIdx];
99
100 // ignore all chars except valid hex numbers
101 if ((hexNibble >= '0' && hexNibble <= '9')
102 || (hexNibble >= 'A' && hexNibble <= 'F')
103 || (hexNibble >= 'a' && hexNibble <= 'f'))
104 {
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; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)
113 {
114 if (hexNibbleIdx > 0) binChar = binChar << 4;
115
116 if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';
117 else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);
118 else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);
119 }
120
121 binStr[binStrIdx++] = binChar;
122 hexNibbleIdx = 0;
123 }
124 }
125 }
126 *outLength = binStrIdx;
127 return binStr;
128 }
129 else
130 {
131 *outLength = 0;
132 return NULL;
133 }
134}

Archive Download this file

Revision: 1919