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 */␊ |
11 | char * newStringFromURL(char* string)␊ |
12 | { ␊ |
13 | ␉char * buffer = newString(string);␊ |
14 | ␉␊ |
15 | ␉int maxlen = strlen(buffer);␉␊ |
16 | ␉␊ |
17 | ␉// replace "%20" by spaces.␊ |
18 | ␉int len = 0;␊ |
19 | ␉while (buffer[len] != '\0') {␊ |
20 | ␉␉if (buffer[len] == '%' && buffer[len+1] == '2' && buffer[len+2] == '0')␊ |
21 | ␉␉{␊ |
22 | ␉␉␉buffer[len] = ' ';␊ |
23 | ␉␉␉␊ |
24 | ␉␉␉strlcpy(&buffer[len+1], &buffer[len+3], maxlen - (len+1)); ␉␉␉␊ |
25 | ␉␉}␊ |
26 | ␉␉len++;␊ |
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 */␊ |
37 | const 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 */␊ |
51 | EFI_CHAR8* getUUIDFromString(const char *source)␊ |
52 | {␊ |
53 | if (!source) return 0;␊ |
54 | ␊ |
55 | ␉char␉*p = (char *)source;␊ |
56 | ␉int␉i;␊ |
57 | ␉char␉buf[3];␊ |
58 | ␉static EFI_CHAR8 uuid[UUID_LEN+1]="";␊ |
59 | ␊ |
60 | ␉buf[2] = '\0';␊ |
61 | ␉for (i=0; i<UUID_LEN; i++) {␊ |
62 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
63 | ␉␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
64 | ␉␉␉return 0;␊ |
65 | ␉␉}␊ |
66 | ␉␉buf[0] = *p++;␊ |
67 | ␉␉buf[1] = *p++;␊ |
68 | ␉␉uuid[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
69 | ␉␉if (*p == '-' && (i % 2) == 1 && i < UUID_LEN - 1) {␊ |
70 | ␉␉␉p++;␊ |
71 | ␉␉}␊ |
72 | ␉}␊ |
73 | ␉uuid[UUID_LEN]='\0';␊ |
74 | ␊ |
75 | ␉if (*p != '\0') {␊ |
76 | ␉␉verbose("[ERROR] UUID='%s' syntax error\n", source);␊ |
77 | ␉␉return 0;␊ |
78 | ␉}␊ |
79 | ␉return uuid;␊ |
80 | }␊ |
81 | ␊ |
82 | /** XXX AsereBLN replace by strtoul */␊ |
83 | #if UNUSED␊ |
84 | uint32_t ascii_hex_to_int(char *buff) ␊ |
85 | {␊ |
86 | ␉uint32_t␉value = 0, i, digit;␊ |
87 | ␉for(i = 0; i < (uint32_t)strlen(buff); i++)␊ |
88 | ␉{␊ |
89 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉␉// '0' through '9'␊ |
90 | ␉␉␉digit = buff[i] - 48;␉␊ |
91 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
92 | ␉␉␉digit = buff[i] - 55;␊ |
93 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
94 | ␉␉␉digit = buff[i] - 87;␊ |
95 | ␉␉else␊ |
96 | ␉␉␉return value;␊ |
97 | ␉␉␊ |
98 | ␉␉value = digit + 16 * value;␊ |
99 | ␉}␊ |
100 | ␉return␉value;␊ |
101 | }␊ |
102 | #endif␊ |
103 | ␊ |
104 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
105 | {␊ |
106 | int len;␊ |
107 | char hexNibble;␊ |
108 | char hexByte[2];␊ |
109 | uint8_t binChar;␊ |
110 | uint8_t *binStr;␊ |
111 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
112 | ␊ |
113 | len = strlen(hexStr);␊ |
114 | if (len > 1)␊ |
115 | {␊ |
116 | // the resulting binary will be the half size of the input hex string␊ |
117 | binStr = malloc(len / 2);␊ |
118 | if (!binStr) {␊ |
119 | *outLength = 0;␊ |
120 | return NULL;␊ |
121 | }␊ |
122 | binStrIdx = 0;␊ |
123 | hexNibbleIdx = 0;␊ |
124 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
125 | {␊ |
126 | hexNibble = hexStr[hexStrIdx];␊ |
127 | ␊ |
128 | // ignore all chars except valid hex numbers␊ |
129 | if ((hexNibble >= '0' && hexNibble <= '9')␊ |
130 | || (hexNibble >= 'A' && hexNibble <= 'F')␊ |
131 | || (hexNibble >= 'a' && hexNibble <= 'f'))␊ |
132 | {␊ |
133 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
134 | ␊ |
135 | // found both two nibbles, convert to binary␊ |
136 | if (hexNibbleIdx == 2)␊ |
137 | {␊ |
138 | binChar = 0;␊ |
139 | ␊ |
140 | for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
141 | {␊ |
142 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
143 | ␊ |
144 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
145 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
146 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
147 | }␊ |
148 | ␊ |
149 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
150 | hexNibbleIdx = 0;␊ |
151 | }␊ |
152 | }␊ |
153 | }␊ |
154 | *outLength = binStrIdx;␊ |
155 | return binStr;␊ |
156 | }␊ |
157 | else␊ |
158 | {␊ |
159 | *outLength = 0;␊ |
160 | return NULL;␊ |
161 | }␊ |
162 | } |