Commit | Line | Data |
---|---|---|
d3372da9 | 1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client | |
302a2600 | 3 | * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com> |
d3372da9 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
b5010caa | 19 | #include <stdlib.h> |
d3372da9 | 20 | #include <stdarg.h> |
21 | #include <stdio.h> | |
22 | #include <wchar.h> | |
23 | #include <iconv.h> | |
24 | #include <errno.h> | |
25 | #include <string.h> | |
26 | #include <wctype.h> | |
27 | #include <langinfo.h> | |
28 | #include <pwd.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/time.h> | |
31 | #include <netinet/in.h> | |
32 | ||
33 | #ifdef HAVE_CONFIG_H | |
34 | #include <config.h> | |
35 | #endif | |
8b17e919 | 36 | #include <utils.h> |
37 | #include <log.h> | |
d3372da9 | 38 | |
a1e6d478 FT |
39 | struct treeiter { |
40 | struct { | |
41 | struct btree *n; | |
42 | int s; | |
43 | } *st; | |
0e238ce6 FT |
44 | size_t stsize; |
45 | int sp; | |
a1e6d478 FT |
46 | }; |
47 | ||
d3372da9 | 48 | static char *base64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
49 | static int base64rev[] = { | |
50 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
51 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
52 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, | |
53 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, | |
54 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
55 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, | |
56 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
57 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, | |
58 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
59 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
60 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
61 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
62 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
63 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
64 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
65 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
66 | }; | |
67 | static char *base32set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; | |
68 | static int base32rev[] = { | |
69 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
70 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
71 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
72 | -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1, | |
73 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
74 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, | |
75 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, | |
76 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, | |
77 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
78 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
79 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
80 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
81 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
82 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
83 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
84 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | |
85 | }; | |
86 | ||
87 | char *vsprintf2(char *format, va_list al) | |
88 | { | |
89 | int ret; | |
90 | char *buf; | |
4dffb251 | 91 | va_list al2; |
d3372da9 | 92 | |
4dffb251 | 93 | va_copy(al2, al); |
fe8c6c8e | 94 | ret = vsnprintf(NULL, 0, format, al2); |
95 | va_end(al2); | |
d3372da9 | 96 | if((buf = malloc(ret + 1)) == NULL) |
97 | { | |
98 | LOGOOM(ret + 1); | |
99 | return(NULL); | |
100 | } | |
4dffb251 | 101 | va_copy(al2, al); |
fe8c6c8e | 102 | vsnprintf(buf, ret + 1, format, al2); |
103 | va_end(al2); | |
d3372da9 | 104 | return(buf); |
105 | } | |
106 | ||
107 | char *sprintf2(char *format, ...) | |
108 | { | |
109 | va_list args; | |
110 | char *buf; | |
111 | ||
112 | va_start(args, format); | |
113 | buf = vsprintf2(format, args); | |
114 | va_end(args); | |
115 | return(buf); | |
116 | } | |
117 | ||
118 | wchar_t *vswprintf2(wchar_t *format, va_list al) | |
119 | { | |
120 | int ret; | |
121 | wchar_t *buf; | |
122 | size_t bufsize; | |
4dffb251 | 123 | va_list al2; |
d3372da9 | 124 | |
125 | buf = smalloc(sizeof(wchar_t) * (bufsize = 1024)); | |
4dffb251 | 126 | while(1) |
127 | { | |
128 | va_copy(al2, al); | |
129 | ret = vswprintf(buf, bufsize, format, al2); | |
fe8c6c8e | 130 | va_end(al2); |
4dffb251 | 131 | if(ret >= 0) |
132 | break; | |
d3372da9 | 133 | buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2)); |
4dffb251 | 134 | } |
d3372da9 | 135 | if(bufsize > ret + 1) |
136 | buf = srealloc(buf, sizeof(wchar_t) * (ret + 1)); | |
137 | return(buf); | |
138 | } | |
139 | ||
140 | wchar_t *swprintf2(wchar_t *format, ...) | |
141 | { | |
142 | va_list args; | |
143 | wchar_t *buf; | |
144 | ||
145 | va_start(args, format); | |
146 | buf = vswprintf2(format, args); | |
147 | va_end(args); | |
148 | return(buf); | |
149 | } | |
150 | ||
edccab15 | 151 | int havecharset(char *charset) |
152 | { | |
153 | iconv_t cd; | |
154 | ||
155 | if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1) | |
156 | return(0); | |
157 | iconv_close(cd); | |
158 | if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1) | |
159 | return(0); | |
160 | iconv_close(cd); | |
161 | return(1); | |
162 | } | |
163 | ||
d3372da9 | 164 | wchar_t *icmbstowcs(char *mbs, char *charset) |
165 | { | |
166 | int ret; | |
167 | char *buf; | |
168 | char *p, *p2; | |
169 | size_t len1, len2, bufsize, data; | |
170 | iconv_t cd; | |
171 | ||
172 | len1 = strlen(mbs) + 1; | |
173 | bufsize = len2 = len1 * sizeof(wchar_t); | |
174 | if((buf = malloc(bufsize)) == NULL) | |
175 | { | |
176 | LOGOOM(bufsize); | |
177 | return(NULL); | |
178 | } | |
179 | if(charset == NULL) | |
180 | charset = nl_langinfo(CODESET); | |
181 | if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1) | |
182 | { | |
8b17e919 | 183 | #ifdef DAEMON |
d3372da9 | 184 | flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno)); |
8b17e919 | 185 | #endif |
d3372da9 | 186 | free(buf); |
187 | return(NULL); | |
188 | } | |
189 | p = buf; | |
190 | while(len1 > 0) | |
191 | { | |
192 | ret = iconv(cd, &mbs, &len1, &p, &len2); | |
193 | if(ret < 0) | |
194 | { | |
195 | if(errno == E2BIG) | |
196 | { | |
197 | data = p - buf; | |
198 | len2 += 128; | |
199 | bufsize += 128; | |
200 | if((p2 = realloc(buf, bufsize)) == NULL) | |
201 | { | |
202 | LOGOOM(bufsize); | |
203 | free(buf); | |
204 | iconv_close(cd); | |
205 | return(NULL); | |
206 | } | |
207 | buf = p2; | |
208 | p = buf + data; | |
209 | } else { | |
210 | free(buf); | |
211 | iconv_close(cd); | |
212 | return(NULL); | |
213 | } | |
214 | } | |
215 | } | |
216 | if(len2 > 0) | |
217 | buf = realloc(buf, p - buf); | |
218 | iconv_close(cd); | |
219 | return((wchar_t *)buf); | |
220 | } | |
221 | ||
222 | wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def) | |
223 | { | |
224 | static wchar_t *buf = NULL; | |
225 | ||
226 | if(buf != NULL) | |
227 | free(buf); | |
228 | if((buf = icmbstowcs(mbs, charset)) == NULL) | |
229 | { | |
96cf7d1f | 230 | if((def != NULL) && (*def == L'~')) |
d3372da9 | 231 | { |
8b17e919 | 232 | #ifdef DAEMON |
d3372da9 | 233 | flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno)); |
8b17e919 | 234 | #endif |
d3372da9 | 235 | def++; |
236 | } | |
237 | return(def); | |
238 | } | |
239 | return(buf); | |
240 | } | |
241 | ||
242 | char *icwcstombs(wchar_t *wcs, char *charset) | |
243 | { | |
244 | int ret; | |
245 | char *buf; | |
246 | char *p, *p2; | |
247 | size_t len1, len2, bufsize, data; | |
248 | iconv_t cd; | |
249 | ||
250 | len1 = sizeof(wchar_t) * (wcslen(wcs) + 1); | |
251 | bufsize = len2 = len1; | |
252 | if((buf = malloc(bufsize)) == NULL) | |
253 | { | |
8b17e919 | 254 | #ifdef DAEMON |
d3372da9 | 255 | LOGOOM(bufsize); |
8b17e919 | 256 | #endif |
d3372da9 | 257 | return(NULL); |
258 | } | |
259 | if(charset == NULL) | |
260 | charset = nl_langinfo(CODESET); | |
261 | if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1) | |
262 | { | |
8b17e919 | 263 | #ifdef DAEMON |
d3372da9 | 264 | flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno)); |
8b17e919 | 265 | #endif |
d3372da9 | 266 | free(buf); |
267 | return(NULL); | |
268 | } | |
269 | p = buf; | |
270 | while(len1 > 0) | |
271 | { | |
272 | ret = iconv(cd, (char **)&wcs, &len1, &p, &len2); | |
273 | if(ret < 0) | |
274 | { | |
275 | if(errno == E2BIG) | |
276 | { | |
277 | data = p - buf; | |
278 | len2 += 128; | |
279 | bufsize += 128; | |
280 | if((p2 = realloc(buf, bufsize)) == NULL) | |
281 | { | |
282 | LOGOOM(bufsize); | |
283 | free(buf); | |
284 | iconv_close(cd); | |
285 | return(NULL); | |
286 | } | |
287 | buf = p2; | |
288 | p = buf + data; | |
289 | } else { | |
290 | free(buf); | |
291 | iconv_close(cd); | |
292 | return(NULL); | |
293 | } | |
294 | } | |
295 | } | |
296 | if(len2 > 0) | |
297 | buf = realloc(buf, p - buf); | |
298 | iconv_close(cd); | |
299 | return(buf); | |
300 | } | |
301 | ||
302 | char *icswcstombs(wchar_t *wcs, char *charset, char *def) | |
303 | { | |
304 | static char *buf = NULL; | |
305 | ||
306 | if(buf != NULL) | |
307 | free(buf); | |
308 | if((buf = icwcstombs(wcs, charset)) == NULL) | |
309 | { | |
96cf7d1f | 310 | if((def != NULL) && (*def == '~')) |
d3372da9 | 311 | { |
8b17e919 | 312 | #ifdef DAEMON |
d3372da9 | 313 | flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno)); |
8b17e919 | 314 | #endif |
d3372da9 | 315 | def++; |
316 | } | |
317 | return(def); | |
318 | } | |
319 | return(buf); | |
320 | } | |
321 | ||
322 | wchar_t *wcstolower(wchar_t *wcs) | |
323 | { | |
324 | wchar_t *p; | |
325 | ||
326 | for(p = wcs; *p != L'\0'; p++) | |
327 | *p = towlower(*p); | |
328 | return(wcs); | |
329 | } | |
330 | ||
331 | wchar_t ucptowc(int ucp) | |
332 | { | |
333 | int ret; | |
334 | unsigned long ucpbuf; | |
335 | char *buf; | |
336 | char *mbsp, *p, *p2; | |
337 | wchar_t res; | |
338 | size_t len1, len2, bufsize, data; | |
339 | iconv_t cd; | |
340 | ||
341 | ucpbuf = htonl(ucp); | |
342 | mbsp = (char *)&ucpbuf; | |
343 | len1 = 4; | |
344 | bufsize = len2 = len1 * sizeof(wchar_t); | |
345 | if((buf = malloc(bufsize)) == NULL) | |
346 | { | |
347 | LOGOOM(bufsize); | |
348 | return(L'\0'); | |
349 | } | |
350 | if((cd = iconv_open("wchar_t", "UCS-4BE")) == (iconv_t)-1) | |
351 | { | |
8b17e919 | 352 | #ifdef DAEMON |
d3372da9 | 353 | flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno)); |
8b17e919 | 354 | #endif |
d3372da9 | 355 | free(buf); |
356 | return(L'\0'); | |
357 | } | |
358 | p = buf; | |
359 | while(len1 > 0) | |
360 | { | |
361 | ret = iconv(cd, &mbsp, &len1, &p, &len2); | |
362 | if(ret < 0) | |
363 | { | |
364 | if(errno == E2BIG) | |
365 | { | |
366 | data = p - buf; | |
367 | len2 += 128; | |
368 | bufsize += 128; | |
369 | if((p2 = realloc(buf, bufsize)) == NULL) | |
370 | { | |
371 | LOGOOM(bufsize); | |
372 | free(buf); | |
373 | iconv_close(cd); | |
374 | return(L'\0'); | |
375 | } | |
376 | buf = p2; | |
377 | p = buf + data; | |
378 | } else { | |
379 | free(buf); | |
380 | iconv_close(cd); | |
381 | return(L'\0'); | |
382 | } | |
383 | } | |
384 | } | |
385 | if(len2 > 0) | |
386 | buf = realloc(buf, p - buf); | |
387 | iconv_close(cd); | |
388 | res = *(wchar_t *)buf; | |
389 | free(buf); | |
390 | return(res); | |
391 | } | |
392 | ||
393 | void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo) | |
394 | { | |
395 | if(*bufsize >= reqsize) | |
396 | return; | |
397 | switch(algo) | |
398 | { | |
399 | case 0: | |
400 | *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize)); | |
401 | break; | |
402 | case 1: | |
403 | if(*bufsize == 0) | |
404 | *bufsize = 1; | |
405 | while(*bufsize < reqsize) | |
406 | *bufsize <<= 1; | |
407 | *buf = srealloc(*buf, elsize * (*bufsize)); | |
408 | break; | |
409 | } | |
410 | } | |
411 | ||
412 | double ntime(void) | |
413 | { | |
414 | struct timeval tv; | |
415 | ||
416 | gettimeofday(&tv, NULL); | |
417 | return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0)); | |
418 | } | |
419 | ||
8bdbfebe | 420 | wchar_t *wcslower(wchar_t *wcs) |
d3372da9 | 421 | { |
8bdbfebe | 422 | wchar_t *p; |
d3372da9 | 423 | |
8bdbfebe FT |
424 | for(p = wcs; *p != L'\0'; p++) |
425 | *p = towlower(*p); | |
426 | return(wcs); | |
d3372da9 | 427 | } |
428 | ||
429 | #ifndef HAVE_WCSCASECMP | |
430 | int wcscasecmp(const wchar_t *s1, const wchar_t *s2) | |
431 | { | |
2b1082a6 | 432 | for(; (towlower(*s1) == towlower(*s2)) && (*s1 != L'\0'); s1++, s2++); |
d3372da9 | 433 | return(towlower(*s1) - towlower(*s2)); |
434 | } | |
435 | #endif | |
436 | ||
437 | char *hexencode(char *data, size_t datalen) | |
438 | { | |
439 | char *buf, this; | |
440 | size_t bufsize, bufdata; | |
441 | int dig; | |
442 | ||
443 | buf = NULL; | |
444 | bufsize = bufdata = 0; | |
445 | for(; datalen > 0; datalen--, data++) | |
446 | { | |
447 | dig = (*data & 0xF0) >> 4; | |
448 | if(dig > 9) | |
449 | this = 'A' + dig - 10; | |
450 | else | |
451 | this = dig + '0'; | |
452 | addtobuf(buf, this); | |
453 | dig = *data & 0x0F; | |
454 | if(dig > 9) | |
455 | this = 'A' + dig - 10; | |
456 | else | |
457 | this = dig + '0'; | |
458 | addtobuf(buf, this); | |
459 | } | |
460 | addtobuf(buf, 0); | |
461 | return(buf); | |
462 | } | |
463 | ||
464 | char *hexdecode(char *data, size_t *len) | |
465 | { | |
b9707562 | 466 | char *buf, this, bit; |
d3372da9 | 467 | size_t bufsize, bufdata; |
468 | ||
469 | buf = NULL; | |
470 | bufsize = bufdata = 0; | |
b9707562 | 471 | for(bit = 4, this = 0; *data; data++) |
d3372da9 | 472 | { |
473 | if((*data >= 'A') && (*data <= 'F')) | |
474 | { | |
b9707562 | 475 | this |= (this & 0x0F) | ((*data - 'A' + 10) << bit); |
476 | } else if((*data >= 'a') && (*data <= 'f')) { | |
477 | this |= (this & 0x0F) | ((*data - 'a' + 10) << bit); | |
d3372da9 | 478 | } else if((*data >= '0') && (*data <= '9')) { |
b9707562 | 479 | this |= (this & 0x0F) | ((*data - '0') << bit); |
480 | } else if(*data == '\n') { | |
481 | continue; | |
d3372da9 | 482 | } else { |
483 | if(buf != NULL) | |
484 | free(buf); | |
485 | return(NULL); | |
486 | } | |
b9707562 | 487 | if(bit == 4) { |
488 | bit = 0; | |
d3372da9 | 489 | } else { |
b9707562 | 490 | bit = 4; |
491 | addtobuf(buf, this); | |
492 | this = 0; | |
d3372da9 | 493 | } |
b9707562 | 494 | } |
495 | if(bit != 4) { | |
496 | if(buf != NULL) | |
497 | free(buf); | |
498 | return(NULL); | |
d3372da9 | 499 | } |
500 | addtobuf(buf, 0); | |
501 | if(len != NULL) | |
502 | *len = bufdata - 1; | |
503 | return(buf); | |
504 | } | |
505 | ||
506 | char *base64encode(char *data, size_t datalen) | |
507 | { | |
508 | char *buf; | |
509 | size_t bufsize, bufdata; | |
510 | ||
511 | if(datalen == 0) | |
512 | return(sstrdup("")); | |
513 | buf = NULL; | |
514 | bufsize = bufdata = 0; | |
515 | while(datalen >= 3) | |
516 | { | |
517 | addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]); | |
518 | addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]); | |
519 | addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]); | |
520 | addtobuf(buf, base64set[data[2] & 0x3f]); | |
521 | datalen -= 3; | |
522 | data += 3; | |
523 | } | |
524 | if(datalen == 1) | |
525 | { | |
526 | addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]); | |
527 | addtobuf(buf, base64set[(data[0] & 0x03) << 4]); | |
528 | bufcat(buf, "==", 2); | |
529 | } | |
530 | if(datalen == 2) | |
531 | { | |
532 | addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]); | |
533 | addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]); | |
534 | addtobuf(buf, base64set[(data[1] & 0x0f) << 2]); | |
535 | addtobuf(buf, '='); | |
536 | } | |
537 | addtobuf(buf, 0); | |
538 | return(buf); | |
539 | } | |
540 | ||
541 | char *base64decode(char *data, size_t *datalen) | |
542 | { | |
543 | int b, c; | |
544 | char *buf, cur; | |
545 | size_t bufsize, bufdata; | |
546 | ||
547 | buf = NULL; | |
548 | bufsize = bufdata = 0; | |
549 | cur = 0; | |
550 | b = 8; | |
551 | for(; *data > 0; data++) | |
552 | { | |
553 | c = (int)(unsigned char)*data; | |
554 | if(c == '=') | |
555 | break; | |
65399bc2 | 556 | if(c == '\n') |
557 | continue; | |
d3372da9 | 558 | if(base64rev[c] == -1) |
559 | { | |
560 | if(buf != NULL) | |
561 | free(buf); | |
562 | return(NULL); | |
563 | } | |
564 | b -= 6; | |
565 | if(b <= 0) | |
566 | { | |
567 | cur |= base64rev[c] >> -b; | |
568 | addtobuf(buf, cur); | |
569 | b += 8; | |
570 | cur = 0; | |
571 | } | |
572 | cur |= base64rev[c] << b; | |
573 | } | |
574 | if(datalen != NULL) | |
575 | *datalen = bufdata; | |
576 | addtobuf(buf, 0); | |
577 | return(buf); | |
578 | } | |
579 | ||
580 | char *base32encode(char *data, size_t datalen) | |
581 | { | |
582 | char *buf; | |
583 | size_t bufsize, bufdata; | |
584 | ||
585 | if(datalen == 0) | |
586 | return(sstrdup("")); | |
587 | buf = NULL; | |
588 | bufsize = bufdata = 0; | |
589 | while(datalen >= 5) | |
590 | { | |
591 | addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]); | |
592 | addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]); | |
593 | addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]); | |
594 | addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]); | |
595 | addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]); | |
596 | addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]); | |
597 | addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]); | |
598 | addtobuf(buf, base32set[data[4] & 0x1f]); | |
599 | datalen -= 5; | |
600 | data += 5; | |
601 | } | |
602 | if(datalen == 1) | |
603 | { | |
604 | addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]); | |
605 | addtobuf(buf, base32set[((data[0] & 0x07) << 2)]); | |
606 | bufcat(buf, "======", 6); | |
607 | } | |
608 | if(datalen == 2) | |
609 | { | |
610 | addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]); | |
611 | addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]); | |
612 | addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]); | |
613 | addtobuf(buf, base32set[((data[1] & 0x01) << 4)]); | |
614 | bufcat(buf, "====", 4); | |
615 | } | |
616 | if(datalen == 3) | |
617 | { | |
618 | addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]); | |
619 | addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]); | |
620 | addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]); | |
621 | addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]); | |
622 | addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]); | |
623 | bufcat(buf, "===", 3); | |
624 | } | |
625 | if(datalen == 4) | |
626 | { | |
627 | addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]); | |
628 | addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]); | |
629 | addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]); | |
630 | addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]); | |
631 | addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]); | |
632 | addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]); | |
633 | addtobuf(buf, base32set[((data[3] & 0x03) << 3)]); | |
634 | bufcat(buf, "=", 1); | |
635 | } | |
636 | addtobuf(buf, 0); | |
637 | return(buf); | |
638 | } | |
639 | ||
640 | char *base32decode(char *data, size_t *datalen) | |
641 | { | |
642 | int b, c; | |
643 | char *buf, cur; | |
644 | size_t bufsize, bufdata; | |
645 | ||
646 | buf = NULL; | |
647 | bufsize = bufdata = 0; | |
648 | cur = 0; | |
649 | b = 8; | |
650 | for(; *data > 0; data++) | |
651 | { | |
652 | c = (int)(unsigned char)*data; | |
653 | if(c == '=') | |
654 | break; | |
65399bc2 | 655 | if(c == '\n') |
656 | continue; | |
d3372da9 | 657 | if(base32rev[c] == -1) |
658 | { | |
659 | if(buf != NULL) | |
660 | free(buf); | |
661 | return(NULL); | |
662 | } | |
663 | b -= 5; | |
664 | if(b <= 0) | |
665 | { | |
666 | cur |= base32rev[c] >> -b; | |
667 | addtobuf(buf, cur); | |
668 | b += 8; | |
669 | cur = 0; | |
670 | } | |
671 | cur |= base32rev[c] << b; | |
672 | } | |
673 | if(datalen != NULL) | |
674 | *datalen = bufdata; | |
675 | addtobuf(buf, 0); | |
676 | return(buf); | |
677 | } | |
678 | ||
679 | void _freeparr(void **arr) | |
680 | { | |
681 | void **buf; | |
682 | ||
683 | if(arr == NULL) | |
684 | return; | |
685 | for(buf = arr; *buf != NULL; buf++) | |
686 | free(*buf); | |
687 | free(arr); | |
688 | } | |
689 | ||
690 | int _parrlen(void **arr) | |
691 | { | |
692 | int i; | |
693 | ||
694 | if(arr == NULL) | |
695 | return(0); | |
696 | for(i = 0; *arr != NULL; arr++) | |
697 | i++; | |
698 | return(i); | |
699 | } | |
700 | ||
701 | char *getetcpath(char *binpath) | |
702 | { | |
73247114 | 703 | int f; |
d3372da9 | 704 | char *etcpath, *p; |
705 | size_t etcpathsize, etcpathdata; | |
706 | ||
707 | etcpath = NULL; | |
708 | etcpathsize = etcpathdata = 0; | |
73247114 | 709 | f = 1; |
d3372da9 | 710 | do |
711 | { | |
73247114 | 712 | if(f) |
713 | f = 0; | |
714 | else | |
715 | binpath++; | |
d3372da9 | 716 | for(p = binpath; *p && (*p != ':'); p++); |
717 | for(; (p >= binpath) && (*p != '/'); p--); | |
718 | if(p >= binpath) | |
719 | { | |
720 | if(etcpathdata > 0) | |
721 | addtobuf(etcpath, ':'); | |
722 | bufcat(etcpath, binpath, p - binpath + 1); | |
723 | bufcat(etcpath, "etc", 3); | |
724 | } | |
725 | } while((binpath = strchr(binpath, ':')) != NULL); | |
726 | addtobuf(etcpath, 0); | |
727 | return(etcpath); | |
728 | } | |
729 | ||
1df8650c | 730 | char *findfile(char *name, char *homedir, int filldef) |
d3372da9 | 731 | { |
732 | char *path, *binpath, *etcpath, *p; | |
48b2d6d1 | 733 | struct passwd *pw; |
1df8650c | 734 | int mode, homeonly; |
d3372da9 | 735 | |
1df8650c | 736 | if(name == NULL) |
737 | return(NULL); | |
738 | ||
41ac4371 | 739 | mode = R_OK | (filldef ? W_OK : 0); |
1df8650c | 740 | homeonly = homedir != NULL; |
741 | ||
742 | if(!strchr(name, '/')) | |
743 | { | |
41ac4371 | 744 | if(homedir == NULL) |
745 | homedir = getenv("HOME"); | |
48b2d6d1 | 746 | if((homedir == NULL) && ((pw = getpwuid(getuid())) != NULL)) |
747 | homedir = pw->pw_dir; | |
1df8650c | 748 | if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, name)) != NULL)) |
48b2d6d1 | 749 | { |
41ac4371 | 750 | if(!access(path, mode)) |
48b2d6d1 | 751 | return(path); |
752 | free(path); | |
753 | } | |
d3372da9 | 754 | } |
1df8650c | 755 | |
756 | if(!homeonly) | |
d3372da9 | 757 | { |
1df8650c | 758 | if(strchr(name, '/') != NULL) |
d3372da9 | 759 | { |
1df8650c | 760 | if(!access(name, mode)) |
761 | return(sstrdup(name)); | |
d3372da9 | 762 | } else { |
763 | if((binpath = getenv("PATH")) == NULL) | |
764 | etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc"); | |
765 | else | |
766 | etcpath = getetcpath(binpath); | |
767 | for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":")) | |
768 | { | |
1df8650c | 769 | if((path = sprintf2("%s/%s", p, name)) != NULL) |
d3372da9 | 770 | { |
41ac4371 | 771 | if(!access(path, mode)) |
d3372da9 | 772 | { |
773 | free(etcpath); | |
774 | return(path); | |
775 | } | |
776 | free(path); | |
777 | } | |
778 | } | |
779 | free(etcpath); | |
780 | } | |
781 | } | |
1df8650c | 782 | |
48b2d6d1 | 783 | if(filldef) { |
1df8650c | 784 | if(homedir) |
785 | return(sprintf2("%s/.%s", homedir, name)); | |
786 | return(sprintf2("/etc/%s", name)); | |
48b2d6d1 | 787 | } else { |
788 | return(NULL); | |
789 | } | |
d3372da9 | 790 | } |
9ec790e8 | 791 | |
ba087e49 FT |
792 | struct strpair *newstrpair(char *key, char *val, struct strpair **list) |
793 | { | |
794 | struct strpair *pair; | |
795 | ||
796 | pair = smalloc(sizeof(*pair)); | |
797 | memset(pair, 0, sizeof(*pair)); | |
798 | if(key != NULL) | |
799 | pair->key = sstrdup(key); | |
800 | if(val != NULL) | |
801 | pair->val = sstrdup(val); | |
802 | if(list != NULL) | |
803 | { | |
804 | pair->next = *list; | |
805 | *list = pair; | |
806 | } | |
807 | return(pair); | |
808 | } | |
809 | ||
810 | void freestrpair(struct strpair *pair, struct strpair **list) | |
811 | { | |
812 | struct strpair *cur; | |
813 | ||
814 | for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next) | |
815 | { | |
816 | if(cur == pair) | |
817 | { | |
818 | *list = cur->next; | |
819 | break; | |
820 | } | |
821 | } | |
822 | free(pair->key); | |
823 | free(pair->val); | |
824 | free(pair); | |
825 | } | |
826 | ||
827 | char *spfind(struct strpair *list, char *key) | |
828 | { | |
829 | for(; list != NULL; list = list->next) | |
830 | { | |
831 | if(!strcmp(list->key, key)) | |
832 | return(list->val); | |
833 | } | |
834 | return(NULL); | |
835 | } | |
836 | ||
9ec790e8 | 837 | struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list) |
838 | { | |
839 | struct wcspair *pair; | |
840 | ||
841 | pair = smalloc(sizeof(*pair)); | |
842 | memset(pair, 0, sizeof(*pair)); | |
843 | if(key != NULL) | |
844 | pair->key = swcsdup(key); | |
845 | if(val != NULL) | |
846 | pair->val = swcsdup(val); | |
ba087e49 | 847 | if(list != NULL) |
9ec790e8 | 848 | { |
9ec790e8 | 849 | pair->next = *list; |
850 | *list = pair; | |
851 | } | |
852 | return(pair); | |
853 | } | |
854 | ||
855 | void freewcspair(struct wcspair *pair, struct wcspair **list) | |
856 | { | |
497fe07b | 857 | struct wcspair *cur; |
858 | ||
859 | for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next) | |
9ec790e8 | 860 | { |
497fe07b | 861 | if(cur == pair) |
862 | { | |
863 | *list = cur->next; | |
864 | break; | |
865 | } | |
9ec790e8 | 866 | } |
867 | free(pair->key); | |
868 | free(pair->val); | |
869 | free(pair); | |
870 | } | |
a2761258 | 871 | |
872 | wchar_t *wpfind(struct wcspair *list, wchar_t *key) | |
873 | { | |
874 | for(; list != NULL; list = list->next) | |
875 | { | |
876 | if(!wcscmp(list->key, key)) | |
877 | return(list->val); | |
878 | } | |
879 | return(NULL); | |
880 | } | |
ffb8ed40 FT |
881 | |
882 | static int btheight(struct btree *tree) | |
883 | { | |
884 | if(tree == NULL) | |
885 | return(0); | |
886 | return(tree->h); | |
887 | } | |
888 | ||
889 | static void btsetheight(struct btree *tree) | |
890 | { | |
891 | int lh, rh; | |
892 | ||
893 | if(tree == NULL) | |
894 | return; | |
895 | lh = btheight(tree->l); | |
896 | rh = btheight(tree->r); | |
897 | tree->h = ((lh > rh)?lh:rh) + 1; | |
898 | } | |
899 | ||
900 | static void bbtrl(struct btree **tree); | |
901 | ||
902 | static void bbtrr(struct btree **tree) | |
903 | { | |
904 | struct btree *m, *l, *r; | |
905 | ||
906 | if(btheight((*tree)->l->r) > btheight((*tree)->l->l)) | |
907 | bbtrl(&(*tree)->l); | |
908 | r = (*tree); | |
909 | l = r->l; | |
910 | m = l->r; | |
911 | r->l = m; | |
912 | btsetheight(r); | |
913 | l->r = r; | |
914 | btsetheight(l); | |
915 | *tree = l; | |
916 | } | |
917 | ||
918 | static void bbtrl(struct btree **tree) | |
919 | { | |
920 | struct btree *m, *l, *r; | |
921 | ||
922 | if(btheight((*tree)->r->l) > btheight((*tree)->r->r)) | |
923 | bbtrr(&(*tree)->r); | |
924 | l = (*tree); | |
925 | r = l->r; | |
926 | m = r->l; | |
927 | l->r = m; | |
928 | btsetheight(l); | |
929 | r->l = l; | |
930 | btsetheight(r); | |
931 | *tree = r; | |
932 | } | |
933 | ||
934 | int bbtreedel(struct btree **tree, void *item, int (*cmp)(void *, void *)) | |
935 | { | |
936 | int c, r; | |
937 | struct btree *s, **sp, *o; | |
938 | ||
939 | if(*tree == NULL) | |
940 | return(0); | |
941 | if((c = cmp(item, (*tree)->d)) < 0) { | |
942 | r = bbtreedel(&(*tree)->l, item, cmp); | |
943 | } else if(c > 0) { | |
944 | r = bbtreedel(&(*tree)->r, item, cmp); | |
945 | } else { | |
946 | r = 1; | |
947 | o = *tree; | |
948 | if(((*tree)->r != NULL) && ((*tree)->l != NULL)) { | |
949 | sp = &(*tree)->r; | |
950 | s = *sp; | |
951 | while(s->l != NULL) { | |
952 | sp = &(s->l); | |
953 | s = *sp; | |
954 | } | |
955 | *sp = NULL; | |
956 | s->l = (*tree)->l; | |
957 | s->r = (*tree)->r; | |
958 | *tree = s; | |
959 | } else if((*tree)->l != NULL) { | |
960 | *tree = (*tree)->l; | |
961 | } else if((*tree)->r != NULL) { | |
962 | *tree = (*tree)->r; | |
963 | } else { | |
964 | *tree = NULL; | |
965 | } | |
966 | free(o); | |
967 | } | |
968 | if(*tree != NULL) { | |
969 | btsetheight(*tree); | |
970 | if(btheight((*tree)->l) > btheight((*tree)->r) + 1) | |
971 | bbtrr(tree); | |
972 | if(btheight((*tree)->r) > btheight((*tree)->l) + 1) | |
973 | bbtrl(tree); | |
974 | } | |
975 | return(r); | |
976 | } | |
977 | ||
978 | int bbtreeput(struct btree **tree, void *item, int (*cmp)(void *, void *)) | |
979 | { | |
980 | int c, r; | |
981 | ||
982 | if(*tree == NULL) { | |
983 | *tree = smalloc(sizeof(**tree)); | |
984 | (*tree)->l = (*tree)->r = NULL; | |
985 | (*tree)->d = item; | |
986 | (*tree)->h = 1; | |
987 | return(1); | |
988 | } | |
989 | if((c = cmp(item, (*tree)->d)) < 0) | |
990 | r = bbtreeput(&(*tree)->l, item, cmp); | |
991 | else if(c > 0) | |
992 | r = bbtreeput(&(*tree)->r, item, cmp); | |
993 | else | |
994 | return(0); | |
995 | btsetheight(*tree); | |
996 | if(btheight((*tree)->l) > btheight((*tree)->r) + 1) | |
997 | bbtrr(tree); | |
998 | if(btheight((*tree)->r) > btheight((*tree)->l) + 1) | |
999 | bbtrl(tree); | |
1000 | return(r); | |
1001 | } | |
1002 | ||
1003 | void *btreeget(struct btree *tree, void *key, int (*cmp)(void *, void *)) | |
1004 | { | |
1005 | int c; | |
1006 | ||
1007 | while(1) { | |
1008 | if(tree == NULL) | |
1009 | return(NULL); | |
1010 | c = cmp(key, tree->d); | |
1011 | if(c < 0) | |
1012 | tree = tree->l; | |
1013 | else if(c > 0) | |
1014 | tree = tree->r; | |
1015 | else | |
1016 | return(tree->d); | |
1017 | } | |
1018 | } | |
1019 | ||
a1e6d478 FT |
1020 | void btreefree(struct btree *tree) |
1021 | { | |
1022 | if(tree == NULL) | |
1023 | return; | |
1024 | btreefree(tree->l); | |
1025 | btreefree(tree->r); | |
1026 | free(tree); | |
1027 | } | |
1028 | ||
1029 | static void *btreenext(void **iterp) | |
1030 | { | |
1031 | struct treeiter *iter; | |
1032 | int s; | |
1033 | struct btree *n; | |
1034 | ||
1035 | if((iter = *iterp) == NULL) | |
1036 | return(NULL); | |
1037 | while(1) { | |
1038 | s = iter->st[iter->sp].s; | |
1039 | n = iter->st[iter->sp].n; | |
1040 | if(s == 0) { | |
1041 | iter->st[iter->sp].s = 1; | |
1042 | if(n->l != NULL) { | |
1043 | iter->sp++; | |
1044 | sizebuf2(iter->st, iter->sp + 1, 1); | |
1045 | iter->st[iter->sp].s = 0; | |
1046 | iter->st[iter->sp].n = n->l; | |
1047 | } | |
1048 | } else if(s == 1) { | |
1049 | iter->st[iter->sp].s = 2; | |
1050 | return(iter->st[iter->sp].n->d); | |
1051 | } else if(s == 2) { | |
1052 | iter->st[iter->sp].s = 3; | |
1053 | if(n->r != NULL) { | |
1054 | iter->sp++; | |
1055 | sizebuf2(iter->st, iter->sp + 1, 1); | |
1056 | iter->st[iter->sp].s = 0; | |
1057 | iter->st[iter->sp].n = n->r; | |
1058 | } | |
1059 | } else { | |
1060 | iter->sp--; | |
1061 | if(iter->sp < 0) { | |
1062 | free(iter->st); | |
1063 | free(iter); | |
1064 | *iterp = NULL; | |
1065 | return(NULL); | |
1066 | } | |
1067 | } | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | static void *btreefirst(struct btree *tree, void **iterp) | |
1072 | { | |
1073 | struct treeiter *iter; | |
1074 | ||
1075 | if(tree == NULL) { | |
1076 | *iterp = NULL; | |
1077 | return(NULL); | |
1078 | } | |
1079 | *iterp = iter = memset(smalloc(sizeof(*iter)), 0, sizeof(*iter)); | |
1080 | sizebuf2(iter->st, 1, 1); | |
1081 | iter->st[0].n = tree; | |
1082 | iter->st[0].s = 0; | |
1083 | return(btreenext(iterp)); | |
1084 | } | |
1085 | ||
1086 | static void btreestop(void **iterp) | |
1087 | { | |
1088 | struct treeiter *iter; | |
1089 | ||
1090 | if((iter = *iterp) == NULL) | |
1091 | return; | |
1092 | if(iter->st != NULL) | |
1093 | free(iter->st); | |
1094 | free(iter); | |
1095 | *iterp = NULL; | |
1096 | } | |
1097 | ||
1098 | void *btreeiter(struct btree *tree) | |
1099 | { | |
1100 | static void *iter = NULL; | |
1101 | ||
1102 | if(tree == NULL) { | |
1103 | return(btreenext(&iter)); | |
1104 | } else { | |
1105 | if(iter != NULL) | |
1106 | btreestop(&iter); | |
1107 | return(btreefirst(tree, &iter)); | |
1108 | } | |
1109 | } |