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 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
83 | {␊ |
84 | int len;␊ |
85 | char hexNibble;␊ |
86 | char hexByte[2];␊ |
87 | uint8_t binChar;␊ |
88 | uint8_t *binStr;␊ |
89 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
90 | ␊ |
91 | len = strlen(hexStr);␊ |
92 | if (len > 1)␊ |
93 | {␊ |
94 | // the resulting binary will be the half size of the input hex string␊ |
95 | binStr = malloc(len / 2);␊ |
96 | if (!binStr) goto out;␊ |
97 | ␉␉bzero(binStr,len / 2 );␊ |
98 | binStrIdx = 0;␊ |
99 | hexNibbleIdx = 0;␊ |
100 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
101 | {␊ |
102 | hexNibble = hexStr[hexStrIdx];␊ |
103 | ␊ |
104 | // ignore all chars except valid hex numbers␊ |
105 | if ((hexNibble >= '0' && hexNibble <= '9')␊ |
106 | || (hexNibble >= 'A' && hexNibble <= 'F')␊ |
107 | || (hexNibble >= 'a' && hexNibble <= 'f'))␊ |
108 | {␊ |
109 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
110 | ␊ |
111 | // found both two nibbles, convert to binary␊ |
112 | if (hexNibbleIdx == 2)␊ |
113 | {␊ |
114 | binChar = 0;␊ |
115 | ␊ |
116 | for (hexNibbleIdx = 0; (unsigned)hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
117 | {␊ |
118 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
119 | ␊ |
120 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
121 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
122 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
123 | }␊ |
124 | ␊ |
125 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
126 | hexNibbleIdx = 0;␊ |
127 | }␊ |
128 | }␊ |
129 | }␊ |
130 | *outLength = binStrIdx;␊ |
131 | return binStr;␊ |
132 | }␊ |
133 | ␊ |
134 | out:␊ |
135 | ␉*outLength = 0; ␊ |
136 | return NULL;␊ |
137 | ␊ |
138 | } |