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 | /* Return a string that is the representation of a 16 bytes UUID */␊ |
11 | void getStringFromUUID(const uuid_t uuid, uuid_string_t out)␊ |
12 | {␊ |
13 | sprintf((char*) out, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",␊ |
14 | uuid[0], uuid[1], uuid[2], uuid[3],␊ |
15 | uuid[4], uuid[5], uuid[6], uuid[7],␊ |
16 | uuid[8], uuid[9], uuid[10],uuid[11],␊ |
17 | uuid[12],uuid[13],uuid[14],uuid[15]);␊ |
18 | }␊ |
19 | ␊ |
20 | /* Parse an UUID string and return a new allocated UUID */␊ |
21 | uuid_t* newUUIDFromString(const char *source)␊ |
22 | {␊ |
23 | if (! source) return NULL;␊ |
24 | ␊ |
25 | const char* p = source;␊ |
26 | int i;␊ |
27 | int len;␊ |
28 | char uuid_hex[UUID_LEN*2+1];␊ |
29 | ␊ |
30 | // Check if UUID is valid␊ |
31 | for (i=0; *p != 0 && i < UUID_LEN*2; p++) {␊ |
32 | if (*p == '-')␊ |
33 | continue;␊ |
34 | ␊ |
35 | if (!isxdigit(*p)) {␊ |
36 | return NULL;␊ |
37 | }␊ |
38 | uuid_hex[i++] = *p;␊ |
39 | }␊ |
40 | ␊ |
41 | // invalid size␊ |
42 | if (*p != 0 || i != UUID_LEN*2) {␊ |
43 | return NULL;␊ |
44 | }␊ |
45 | ␊ |
46 | uuid_hex[i] = 0; // null terminated string␊ |
47 | ␊ |
48 | return convertHexStr2Binary(uuid_hex, &len);␊ |
49 | }␊ |
50 | ␊ |
51 | /** XXX AsereBLN replace by strtoul */␊ |
52 | uint32_t ascii_hex_to_int(char *buff)␊ |
53 | {␊ |
54 | ␉uint32_t␉value = 0, i, digit;␊ |
55 | ␉for(i = 0; i < strlen(buff); i++)␊ |
56 | ␉{␊ |
57 | ␉␉if (buff[i] >= 48 && buff[i] <= 57) // '0' through '9'␊ |
58 | ␉␉␉digit = buff[i] - 48;␊ |
59 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70) // 'A' through 'F'␊ |
60 | ␉␉␉digit = buff[i] - 55;␊ |
61 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102) // 'a' through 'f'␊ |
62 | ␉␉␉digit = buff[i] - 87;␊ |
63 | ␉␉else␊ |
64 | ␉␉␉return value;␊ |
65 | ␊ |
66 | ␉␉value = digit + 16 * value;␊ |
67 | ␉}␊ |
68 | ␉return␉value;␊ |
69 | }␊ |
70 | ␊ |
71 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
72 | {␊ |
73 | int len;␊ |
74 | char hexNibble;␊ |
75 | char hexByte[2];␊ |
76 | uint8_t binChar;␊ |
77 | uint8_t *binStr;␊ |
78 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
79 | ␊ |
80 | len = strlen(hexStr);␊ |
81 | if (len > 1)␊ |
82 | {␊ |
83 | // the resulting binary will be the half size of the input hex string␊ |
84 | binStr = malloc(len / 2);␊ |
85 | binStrIdx = 0;␊ |
86 | hexNibbleIdx = 0;␊ |
87 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
88 | {␊ |
89 | hexNibble = hexStr[hexStrIdx];␊ |
90 | ␊ |
91 | // ignore all chars except valid hex numbers␊ |
92 | if (hexNibble >= '0' && hexNibble <= '9'␊ |
93 | || hexNibble >= 'A' && hexNibble <= 'F'␊ |
94 | || hexNibble >= 'a' && hexNibble <= 'f')␊ |
95 | {␊ |
96 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
97 | ␊ |
98 | // found both two nibbles, convert to binary␊ |
99 | if (hexNibbleIdx == 2)␊ |
100 | {␊ |
101 | binChar = 0;␊ |
102 | ␊ |
103 | for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
104 | {␊ |
105 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
106 | ␊ |
107 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
108 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
109 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
110 | }␊ |
111 | ␊ |
112 | binStr[binStrIdx++] = binChar;␊ |
113 | hexNibbleIdx = 0;␊ |
114 | }␊ |
115 | }␊ |
116 | }␊ |
117 | *outLength = binStrIdx;␊ |
118 | return binStr;␊ |
119 | }␊ |
120 | else␊ |
121 | {␊ |
122 | *outLength = 0;␊ |
123 | return NULL;␊ |
124 | }␊ |
125 | }␊ |
126 | |