Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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
11/* ======================================================= */
12
13/** Transform a 16 bytes hexadecimal value UUID to a string */
14const char *getStringFromUUID(const uint8_t *eUUID)
15{
16static char msg[UUID_LEN*2 + 8] = "";
17if (!eUUID) return "";
18const unsigned char *uuid = (unsigned char *) eUUID;
19snprintf(msg, sizeof(msg), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
20 uuid[0], uuid[1], uuid[2], uuid[3],
21 uuid[4], uuid[5], uuid[6], uuid[7],
22 uuid[8], uuid[9], uuid[10],uuid[11],
23 uuid[12],uuid[13],uuid[14],uuid[15]);
24return msg ;
25}
26
27/* ======================================================= */
28
29/** Parse an UUID string into an (EFI_CHAR8 *) buffer */
30EFI_CHAR8 *getUUIDFromString(const char *source)
31{
32if (!source)
33{
34return 0;
35}
36
37inti = strlen(source);
38if (i != 36)
39{ // e.g 00112233-4455-6677-8899-AABBCCDDEEFF
40verbose("[ERROR] UUID='%s' has incorrect length=%d. Use format: 00112233-4455-6677-8899-AABBCCDDEEFF.\n", source, i);
41return 0;
42}
43
44char*p = (char *)source;
45charbuf[3];
46static EFI_CHAR8 uuid[UUID_LEN+1] = "";
47
48buf[2] = '\0';
49for (i = 0; i < UUID_LEN; i++)
50{
51if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))
52{
53verbose("[ERROR] UUID='%s' syntax error.\n", source);
54return 0;
55}
56buf[0] = *p++;
57buf[1] = *p++;
58uuid[i] = (unsigned char) strtoul(buf, NULL, 16);
59if ((*p == '-') && ((i % 2) == 1) && (i < UUID_LEN - 1))
60{
61p++;
62}
63}
64uuid[UUID_LEN]='\0';
65
66if (*p != '\0')
67{
68verbose("[ERROR] UUID='%s' syntax error\n", source);
69return 0;
70}
71return uuid;
72}
73
74/* ======================================================= */
75
76/** XXX AsereBLN replace by strtoul */
77uint32_t ascii_hex_to_int(char *buff)
78{
79uint32_tvalue = 0, i, digit;
80for(i = 0; i < strlen(buff); i++)
81{
82if (buff[i] >= 48 && buff[i] <= 57)// '0' through '9'
83digit = buff[i] - 48;
84else if (buff[i] >= 65 && buff[i] <= 70)// 'A' through 'F'
85digit = buff[i] - 55;
86else if (buff[i] >= 97 && buff[i] <= 102)// 'a' through 'f'
87digit = buff[i] - 87;
88else
89return value;
90
91value = digit + 16 * value;
92}
93returnvalue;
94}
95
96/* ======================================================= */
97
98void *convertHexStr2Binary(const char *hexStr, int *outLength)
99{
100int len;
101char hexNibble;
102char hexByte[2];
103uint8_t binChar;
104uint8_t *binStr;
105int hexStrIdx, binStrIdx, hexNibbleIdx;
106
107len = strlen(hexStr);
108if (len > 1)
109{
110// the resulting binary will be the half size of the input hex string
111binStr = malloc(len / 2);
112if (!binStr)
113{
114*outLength = 0;
115return NULL;
116}
117bzero(binStr,len / 2 );
118
119binStrIdx = 0;
120hexNibbleIdx = 0;
121for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)
122{
123hexNibble = hexStr[hexStrIdx];
124
125// ignore all chars except valid hex numbers
126if ( (hexNibble >= '0' && hexNibble <= '9') ||
127(hexNibble >= 'A' && hexNibble <= 'F') ||
128(hexNibble >= 'a' && hexNibble <= 'f') )
129{
130hexByte[hexNibbleIdx++] = hexNibble;
131
132// found both two nibbles, convert to binary
133if (hexNibbleIdx == 2)
134{
135binChar = 0;
136
137for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)
138{
139if (hexNibbleIdx > 0)
140{
141binChar = binChar << 4;
142}
143
144if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';
145else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);
146else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);
147}
148
149binStr[binStrIdx++] = binChar;
150hexNibbleIdx = 0;
151}
152}
153}
154*outLength = binStrIdx;
155return binStr;
156}
157else
158{
159*outLength = 0;
160return NULL;
161}
162}
163
164/* ======================================================= */
165
166/*******************************************************************
167 * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *
168 * a binary. returns -1 in case of error (i.e. badly formed chars) *
169 *******************************************************************/
170int hex2bin( const char *hex, uint8_t *bin, int len )
171{
172char*p;
173inti;
174charbuf[3];
175
176if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2)
177{
178printf("[ERROR] bin2hex input error\n");
179return -1;
180}
181
182buf[2] = '\0';
183p = (char *) hex;
184
185for (i = 0; i < len; i++)
186{
187if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))
188{
189printf("[ERROR] bin2hex '%s' syntax error\n", hex);
190return -2;
191}
192buf[0] = *p++;
193buf[1] = *p++;
194bin[i] = (unsigned char) strtoul(buf, NULL, 16);
195}
196return 0;
197}
198
199/* ======================================================= */
200

Archive Download this file

Revision: 2706