2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
31 #include <netinet/in.h>
39 static char *base64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40 static int base64rev[] = {
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, 62, -1, -1, -1, 63,
44 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
45 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
46 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
47 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
48 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
56 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
58 static char *base32set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
59 static int base32rev[] = {
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, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, -1, -1, -1, -1,
64 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
65 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
66 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
67 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
68 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78 char *vsprintf2(char *format, va_list al)
83 ret = vsnprintf(NULL, 0, format, al);
84 if((buf = malloc(ret + 1)) == NULL)
89 vsnprintf(buf, ret + 1, format, al);
93 char *sprintf2(char *format, ...)
98 va_start(args, format);
99 buf = vsprintf2(format, args);
104 wchar_t *vswprintf2(wchar_t *format, va_list al)
110 buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
111 while((ret = vswprintf(buf, bufsize, format, al)) < 0)
112 buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
113 if(bufsize > ret + 1)
114 buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
118 wchar_t *swprintf2(wchar_t *format, ...)
123 va_start(args, format);
124 buf = vswprintf2(format, args);
129 wchar_t *icmbstowcs(char *mbs, char *charset)
134 size_t len1, len2, bufsize, data;
137 len1 = strlen(mbs) + 1;
138 bufsize = len2 = len1 * sizeof(wchar_t);
139 if((buf = malloc(bufsize)) == NULL)
145 charset = nl_langinfo(CODESET);
146 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
148 flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno));
155 ret = iconv(cd, &mbs, &len1, &p, &len2);
163 if((p2 = realloc(buf, bufsize)) == NULL)
180 buf = realloc(buf, p - buf);
182 return((wchar_t *)buf);
185 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
187 static wchar_t *buf = NULL;
191 if((buf = icmbstowcs(mbs, charset)) == NULL)
193 if((def != NULL) && (*def == L'~'))
195 flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno));
203 char *icwcstombs(wchar_t *wcs, char *charset)
208 size_t len1, len2, bufsize, data;
211 len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
212 bufsize = len2 = len1;
213 if((buf = malloc(bufsize)) == NULL)
219 charset = nl_langinfo(CODESET);
220 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
222 flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno));
229 ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
237 if((p2 = realloc(buf, bufsize)) == NULL)
254 buf = realloc(buf, p - buf);
259 char *icswcstombs(wchar_t *wcs, char *charset, char *def)
261 static char *buf = NULL;
265 if((buf = icwcstombs(wcs, charset)) == NULL)
267 if((def != NULL) && (*def == '~'))
269 flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno));
277 wchar_t *wcstolower(wchar_t *wcs)
281 for(p = wcs; *p != L'\0'; p++)
286 wchar_t ucptowc(int ucp)
289 unsigned long ucpbuf;
293 size_t len1, len2, bufsize, data;
297 mbsp = (char *)&ucpbuf;
299 bufsize = len2 = len1 * sizeof(wchar_t);
300 if((buf = malloc(bufsize)) == NULL)
305 if((cd = iconv_open("wchar_t", "UCS-4BE")) == (iconv_t)-1)
307 flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno));
314 ret = iconv(cd, &mbsp, &len1, &p, &len2);
322 if((p2 = realloc(buf, bufsize)) == NULL)
339 buf = realloc(buf, p - buf);
341 res = *(wchar_t *)buf;
346 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
348 if(*bufsize >= reqsize)
353 *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
358 while(*bufsize < reqsize)
360 *buf = srealloc(*buf, elsize * (*bufsize));
369 gettimeofday(&tv, NULL);
370 return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
373 int wcsexists(wchar_t *h, wchar_t *n)
378 ln = alloca(sizeof(*ln) * (nl = wcslen(n)));
379 for(i = 0; i < nl; i++)
380 ln[i] = towlower(n[i]);
381 lh = alloca(sizeof(*lh) * (hl = wcslen(h)));
384 for(i = 0; i < nl; i++)
385 lh[i] = towlower(h[i]);
389 for(o = 0; o < nl; o++)
391 if(lh[i + o] != ln[o])
398 lh[i + nl] = towlower(h[i + nl]);
403 #ifndef HAVE_WCSCASECMP
404 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
406 while(towlower(*s1) == towlower(*s2))
411 return(towlower(*s1) - towlower(*s2));
415 char *hexencode(char *data, size_t datalen)
418 size_t bufsize, bufdata;
422 bufsize = bufdata = 0;
423 for(; datalen > 0; datalen--, data++)
425 dig = (*data & 0xF0) >> 4;
427 this = 'A' + dig - 10;
433 this = 'A' + dig - 10;
442 char *hexdecode(char *data, size_t *len)
445 size_t bufsize, bufdata;
448 bufsize = bufdata = 0;
451 if((*data >= 'A') && (*data <= 'F'))
453 this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
454 } else if((*data >= '0') && (*data <= '9')) {
455 this = (this & 0x0F) | ((*data - '0') << 4);
468 if((*data >= 'A') && (*data <= 'F'))
470 this = (this & 0xF0) | (*data - 'A' + 10);
471 } else if((*data >= '0') && (*data <= '9')) {
472 this = (this & 0xF0) | (*data - '0');
486 char *base64encode(char *data, size_t datalen)
489 size_t bufsize, bufdata;
494 bufsize = bufdata = 0;
497 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
498 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
499 addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]);
500 addtobuf(buf, base64set[data[2] & 0x3f]);
506 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
507 addtobuf(buf, base64set[(data[0] & 0x03) << 4]);
508 bufcat(buf, "==", 2);
512 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
513 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
514 addtobuf(buf, base64set[(data[1] & 0x0f) << 2]);
521 char *base64decode(char *data, size_t *datalen)
525 size_t bufsize, bufdata;
528 bufsize = bufdata = 0;
531 for(; *data > 0; data++)
533 c = (int)(unsigned char)*data;
538 if(base64rev[c] == -1)
547 cur |= base64rev[c] >> -b;
552 cur |= base64rev[c] << b;
560 char *base32encode(char *data, size_t datalen)
563 size_t bufsize, bufdata;
568 bufsize = bufdata = 0;
571 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
572 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
573 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
574 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
575 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
576 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
577 addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]);
578 addtobuf(buf, base32set[data[4] & 0x1f]);
584 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
585 addtobuf(buf, base32set[((data[0] & 0x07) << 2)]);
586 bufcat(buf, "======", 6);
590 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
591 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
592 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
593 addtobuf(buf, base32set[((data[1] & 0x01) << 4)]);
594 bufcat(buf, "====", 4);
598 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
599 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
600 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
601 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
602 addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]);
603 bufcat(buf, "===", 3);
607 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
608 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
609 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
610 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
611 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
612 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
613 addtobuf(buf, base32set[((data[3] & 0x03) << 3)]);
620 char *base32decode(char *data, size_t *datalen)
624 size_t bufsize, bufdata;
627 bufsize = bufdata = 0;
630 for(; *data > 0; data++)
632 c = (int)(unsigned char)*data;
637 if(base32rev[c] == -1)
646 cur |= base32rev[c] >> -b;
651 cur |= base32rev[c] << b;
659 void _freeparr(void **arr)
665 for(buf = arr; *buf != NULL; buf++)
670 int _parrlen(void **arr)
676 for(i = 0; *arr != NULL; arr++)
681 char *getetcpath(char *binpath)
685 size_t etcpathsize, etcpathdata;
688 etcpathsize = etcpathdata = 0;
696 for(p = binpath; *p && (*p != ':'); p++);
697 for(; (p >= binpath) && (*p != '/'); p--);
701 addtobuf(etcpath, ':');
702 bufcat(etcpath, binpath, p - binpath + 1);
703 bufcat(etcpath, "etc", 3);
705 } while((binpath = strchr(binpath, ':')) != NULL);
706 addtobuf(etcpath, 0);
710 char *findfile(char *gname, char *uname, char *homedir)
712 char *path, *binpath, *etcpath, *p;
714 if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, uname)) != NULL))
716 if(!access(path, F_OK))
722 if(strchr(gname, '/') != NULL)
724 if(!access(gname, F_OK))
725 return(sstrdup(gname));
727 if((binpath = getenv("PATH")) == NULL)
728 etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc");
730 etcpath = getetcpath(binpath);
731 for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":"))
733 if((path = sprintf2("%s/%s", p, gname)) != NULL)
735 if(!access(path, F_OK))
749 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
751 struct wcspair *pair;
753 pair = smalloc(sizeof(*pair));
754 memset(pair, 0, sizeof(*pair));
756 pair->key = swcsdup(key);
758 pair->val = swcsdup(val);
769 void freewcspair(struct wcspair *pair, struct wcspair **list)
773 for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
786 wchar_t *wpfind(struct wcspair *list, wchar_t *key)
788 for(; list != NULL; list = list->next)
790 if(!wcscmp(list->key, key))