Fix all UBSAN warnings triggered by tests.
[tinc] / src / utils.c
1 /*
2     utils.c -- gathering of some stupid small functions
3     Copyright (C) 1999-2005 Ivo Timmermans
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "logger.h"
22 #include "system.h"
23 #include "utils.h"
24 #include "xalloc.h"
25
26 static const char hexadecimals[] = "0123456789ABCDEF";
27 static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28 static const char base64_urlsafe[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29 static const char base64_decode[256] = {
30         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
33                 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
34                 -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
35                 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
36                 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
37                 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
38                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45                 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46         };
47
48 static int charhex2bin(char c) {
49         if(isdigit(c)) {
50                 return c - '0';
51         } else {
52                 return toupper(c) - 'A' + 10;
53         }
54 }
55
56 size_t hex2bin(const char *src, void *vdst, size_t length) {
57         uint8_t *dst = vdst;
58         size_t i;
59
60         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++) {
61                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
62         }
63
64         return i;
65 }
66
67 size_t bin2hex(const void *vsrc, char *dst, size_t length) {
68         const char *src = vsrc;
69
70         for(size_t i = length; i > 0;) {
71                 --i;
72                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
73                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
74         }
75
76         dst[length * 2] = 0;
77         return length * 2;
78 }
79
80 size_t b64decode(const char *src, void *dst, size_t length) {
81         size_t i;
82         uint32_t triplet = 0;
83         unsigned char *udst = (unsigned char *)dst;
84
85         for(i = 0; i < length && src[i]; i++) {
86                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
87
88                 if((i & 3) == 3) {
89                         if(triplet & 0xff000000U) {
90                                 return 0;
91                         }
92
93                         udst[0] = triplet & 0xff;
94                         triplet >>= 8;
95                         udst[1] = triplet & 0xff;
96                         triplet >>= 8;
97                         udst[2] = triplet;
98                         triplet = 0;
99                         udst += 3;
100                 }
101         }
102
103         if(triplet & 0xff000000U) {
104                 return 0;
105         }
106
107         if((i & 3) == 3) {
108                 udst[0] = triplet & 0xff;
109                 triplet >>= 8;
110                 udst[1] = triplet & 0xff;
111                 return i / 4 * 3 + 2;
112         } else if((i & 3) == 2) {
113                 udst[0] = triplet & 0xff;
114                 return i / 4 * 3 + 1;
115         } else {
116                 return i / 4 * 3;
117         }
118 }
119
120 static size_t b64encode_internal(const void *src, char *dst, size_t length, const char *alphabet) {
121         uint32_t triplet;
122         const unsigned char *usrc = (unsigned char *)src;
123         size_t si = length / 3 * 3;
124         size_t di = length / 3 * 4;
125
126         switch(length % 3) {
127         case 2:
128                 triplet = usrc[si] | usrc[si + 1] << 8;
129                 dst[di] = alphabet[triplet & 63];
130                 triplet >>= 6;
131                 dst[di + 1] = alphabet[triplet & 63];
132                 triplet >>= 6;
133                 dst[di + 2] = alphabet[triplet];
134                 dst[di + 3] = 0;
135                 length = di + 3;
136                 break;
137
138         case 1:
139                 triplet = usrc[si];
140                 dst[di] = alphabet[triplet & 63];
141                 triplet >>= 6;
142                 dst[di + 1] = alphabet[triplet];
143                 dst[di + 2] = 0;
144                 length = di + 2;
145                 break;
146
147         default:
148                 dst[di] = 0;
149                 length = di;
150                 break;
151         }
152
153         while(si > 0) {
154                 di -= 4;
155                 si -= 3;
156                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
157                 dst[di] = alphabet[triplet & 63];
158                 triplet >>= 6;
159                 dst[di + 1] = alphabet[triplet & 63];
160                 triplet >>= 6;
161                 dst[di + 2] = alphabet[triplet & 63];
162                 triplet >>= 6;
163                 dst[di + 3] = alphabet[triplet];
164         }
165
166         return length;
167 }
168
169 size_t b64encode(const void *src, char *dst, size_t length) {
170         return b64encode_internal(src, dst, length, base64_original);
171 }
172
173 size_t b64encode_urlsafe(const void *src, char *dst, size_t length) {
174         return b64encode_internal(src, dst, length, base64_urlsafe);
175 }
176
177 #ifdef HAVE_MINGW
178 const char *winerror(int err) {
179         static char buf[1024], *ptr;
180
181         ptr = buf + snprintf(buf, sizeof(buf), "(%d) ", err);
182
183         if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
184                           NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
185                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
186         };
187
188         if((ptr = strchr(buf, '\r'))) {
189                 *ptr = '\0';
190         }
191
192         return buf;
193 }
194 #endif
195
196 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
197         unsigned int value = 0;
198
199         if(size > sizeof(value)) {
200                 size = sizeof(value);
201         }
202
203         memcpy(&value, bitfield, size);
204         return value;
205 }
206
207 bool check_id(const char *id) {
208         if(!id || !*id) {
209                 return false;
210         }
211
212         for(; *id; id++)
213                 if(!isalnum(*id) && *id != '_') {
214                         return false;
215                 }
216
217         return true;
218 }
219
220 bool check_netname(const char *netname, bool strict) {
221         if(!netname || !*netname || *netname == '.') {
222                 return false;
223         }
224
225         for(const char *c = netname; *c; c++) {
226                 if(iscntrl(*c)) {
227                         return false;
228                 }
229
230                 if(*c == '/' || *c == '\\') {
231                         return false;
232                 }
233
234                 if(strict && strchr(" $%<>:`\"|?*", *c)) {
235                         return false;
236                 }
237         }
238
239         return true;
240 }
241
242 /* Windows doesn't define HOST_NAME_MAX. */
243 #ifndef HOST_NAME_MAX
244 #define HOST_NAME_MAX 255
245 #endif
246
247 char *replace_name(const char *name) {
248         char *ret_name;
249
250         if(name[0] == '$') {
251                 char *envname = getenv(name + 1);
252                 char hostname[HOST_NAME_MAX + 1];
253
254                 if(!envname) {
255                         if(strcmp(name + 1, "HOST")) {
256                                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid Name: environment variable %s does not exist\n", name + 1);
257                                 return NULL;
258                         }
259
260                         if(gethostname(hostname, sizeof(hostname)) || !*hostname) {
261                                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not get hostname: %s\n", sockstrerror(sockerrno));
262                                 return NULL;
263                         }
264
265                         hostname[HOST_NAME_MAX] = 0;
266                         envname = hostname;
267                 }
268
269                 ret_name = xstrdup(envname);
270
271                 for(char *c = ret_name; *c; c++)
272                         if(!isalnum(*c)) {
273                                 *c = '_';
274                         }
275         } else {
276                 ret_name = xstrdup(name);
277         }
278
279         if(!check_id(ret_name)) {
280                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid name for myself!");
281                 free(ret_name);
282                 return NULL;
283         }
284
285         return ret_name;
286 }