Chameleon

Chameleon Svn Source Tree

Root/branches/meklortOld/i386/libsaio/convert.c

Source at commit 1146 created 12 years 10 months ago.
By azimutz, Sync with trunk (r1145). Add nVidia dev id's, 0DF4 for "GeForce GT 450M" (issue 99) and 1251 for "GeForce GTX 560M" (thanks to oSxFr33k for testing).
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#ifndef OPTION_ROM
11/** Transform a 16 bytes hexadecimal value UUID to a string */
12const char * getStringFromUUID(const EFI_CHAR8* eUUID)
13{
14 static char msg[UUID_LEN*2 + 8] = "";
15 if (!eUUID) return "";
16 const unsigned char * uuid = (unsigned char*) eUUID;
17 sprintf(msg, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
18 uuid[0], uuid[1], uuid[2], uuid[3],
19 uuid[4], uuid[5], uuid[6], uuid[7],
20 uuid[8], uuid[9], uuid[10],uuid[11],
21 uuid[12],uuid[13],uuid[14],uuid[15]);
22 return msg ;
23}
24#endif
25/** Parse an UUID string into an (EFI_CHAR8*) buffer */
26EFI_CHAR8* getUUIDFromString(const char *source)
27{
28 if (!source) return 0;
29
30char*p = (char *)source;
31inti;
32charbuf[3];
33static EFI_CHAR8 uuid[UUID_LEN+1]="";
34
35buf[2] = '\0';
36for (i=0; i<UUID_LEN; i++) {
37if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
38verbose("[ERROR] UUID='%s' syntax error\n", source);
39return 0;
40}
41buf[0] = *p++;
42buf[1] = *p++;
43uuid[i] = (unsigned char) strtoul(buf, NULL, 16);
44if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {
45p++;
46}
47}
48uuid[UUID_LEN]='\0';
49
50if (*p != '\0') {
51verbose("[ERROR] UUID='%s' syntax error\n", source);
52return 0;
53}
54return uuid;
55}
56
57/** XXX AsereBLN replace by strtoul */
58uint32_t ascii_hex_to_int(char *buff)
59{
60uint32_tvalue = 0, i, digit;
61for(i = 0; i < strlen(buff); i++)
62{
63if (buff[i] >= 48 && buff[i] <= 57)// '0' through '9'
64digit = buff[i] - 48;
65else if (buff[i] >= 65 && buff[i] <= 70)// 'A' through 'F'
66digit = buff[i] - 55;
67else if (buff[i] >= 97 && buff[i] <= 102)// 'a' through 'f'
68digit = buff[i] - 87;
69else
70return value;
71
72value = digit + 16 * value;
73}
74returnvalue;
75}
76
77void *convertHexStr2Binary(const char *hexStr, int *outLength)
78{
79 int len;
80 char hexNibble;
81 char hexByte[2];
82 uint8_t binChar;
83 uint8_t *binStr;
84 int hexStrIdx, binStrIdx, hexNibbleIdx;
85
86 len = strlen(hexStr);
87 if (len > 1)
88 {
89 // the resulting binary will be the half size of the input hex string
90 binStr = malloc(len / 2);
91 binStrIdx = 0;
92 hexNibbleIdx = 0;
93 for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)
94 {
95 hexNibble = hexStr[hexStrIdx];
96
97 // ignore all chars except valid hex numbers
98 if (hexNibble >= '0' && hexNibble <= '9'
99 || hexNibble >= 'A' && hexNibble <= 'F'
100 || hexNibble >= 'a' && hexNibble <= 'f')
101 {
102 hexByte[hexNibbleIdx++] = hexNibble;
103
104 // found both two nibbles, convert to binary
105 if (hexNibbleIdx == 2)
106 {
107 binChar = 0;
108
109 for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)
110 {
111 if (hexNibbleIdx > 0) binChar = binChar << 4;
112
113 if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';
114 else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);
115 else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);
116 }
117
118 binStr[binStrIdx++] = binChar;
119 hexNibbleIdx = 0;
120 }
121 }
122 }
123 *outLength = binStrIdx;
124 return binStr;
125 }
126 else
127 {
128 *outLength = 0;
129 return NULL;
130 }
131}
132
133// FIXME: can't use my original code here,
134// Ironically, trying to reuse convertHexStr2Binary() would RESET the system!
135/*
136static EFI_CHAR8* getUUIDFromString2(const char * szInUUID)
137{
138 char szUUID[UUID_LEN+1], *p=szUUID;
139 int size=0;
140 void* ret;
141
142 if (!szInUUID || strlen(szInUUID)<UUID_LEN) return (EFI_CHAR8*) 0;
143
144 while(*szInUUID) if (*szInUUID!='-') *p++=*szInUUID++; else szInUUID++;
145 *p='\0';
146 ret = convertHexStr2Binary(szUUID, &size);
147 if (!ret || size!=UUID_LEN)
148 {
149 verbose("UUID: cannot convert string <%s> to valid UUID.\n", szUUID);
150 return (EFI_CHAR8*) 0;
151 }
152 return (EFI_CHAR8*) ret; // new allocated buffer containing the converted string to bin
153}
154*/
155

Archive Download this file

Revision: 1146