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 int havecharset(char *charset)
133 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
136 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
142 wchar_t *icmbstowcs(char *mbs, char *charset)
147 size_t len1, len2, bufsize, data;
150 len1 = strlen(mbs) + 1;
151 bufsize = len2 = len1 * sizeof(wchar_t);
152 if((buf = malloc(bufsize)) == NULL)
158 charset = nl_langinfo(CODESET);
159 if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
161 flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno));
168 ret = iconv(cd, &mbs, &len1, &p, &len2);
176 if((p2 = realloc(buf, bufsize)) == NULL)
193 buf = realloc(buf, p - buf);
195 return((wchar_t *)buf);
198 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
200 static wchar_t *buf = NULL;
204 if((buf = icmbstowcs(mbs, charset)) == NULL)
206 if((def != NULL) && (*def == L'~'))
208 flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno));
216 char *icwcstombs(wchar_t *wcs, char *charset)
221 size_t len1, len2, bufsize, data;
224 len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
225 bufsize = len2 = len1;
226 if((buf = malloc(bufsize)) == NULL)
232 charset = nl_langinfo(CODESET);
233 if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
235 flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno));
242 ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
250 if((p2 = realloc(buf, bufsize)) == NULL)
267 buf = realloc(buf, p - buf);
272 char *icswcstombs(wchar_t *wcs, char *charset, char *def)
274 static char *buf = NULL;
278 if((buf = icwcstombs(wcs, charset)) == NULL)
280 if((def != NULL) && (*def == '~'))
282 flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno));
290 wchar_t *wcstolower(wchar_t *wcs)
294 for(p = wcs; *p != L'\0'; p++)
299 wchar_t ucptowc(int ucp)
302 unsigned long ucpbuf;
306 size_t len1, len2, bufsize, data;
310 mbsp = (char *)&ucpbuf;
312 bufsize = len2 = len1 * sizeof(wchar_t);
313 if((buf = malloc(bufsize)) == NULL)
318 if((cd = iconv_open("wchar_t", "UCS-4BE")) == (iconv_t)-1)
320 flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno));
327 ret = iconv(cd, &mbsp, &len1, &p, &len2);
335 if((p2 = realloc(buf, bufsize)) == NULL)
352 buf = realloc(buf, p - buf);
354 res = *(wchar_t *)buf;
359 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
361 if(*bufsize >= reqsize)
366 *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
371 while(*bufsize < reqsize)
373 *buf = srealloc(*buf, elsize * (*bufsize));
382 gettimeofday(&tv, NULL);
383 return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
386 int wcsexists(wchar_t *h, wchar_t *n)
391 ln = alloca(sizeof(*ln) * (nl = wcslen(n)));
392 for(i = 0; i < nl; i++)
393 ln[i] = towlower(n[i]);
394 lh = alloca(sizeof(*lh) * (hl = wcslen(h)));
397 for(i = 0; i < nl; i++)
398 lh[i] = towlower(h[i]);
402 for(o = 0; o < nl; o++)
404 if(lh[i + o] != ln[o])
411 lh[i + nl] = towlower(h[i + nl]);
416 #ifndef HAVE_WCSCASECMP
417 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
419 while(towlower(*s1) == towlower(*s2))
424 return(towlower(*s1) - towlower(*s2));
428 char *hexencode(char *data, size_t datalen)
431 size_t bufsize, bufdata;
435 bufsize = bufdata = 0;
436 for(; datalen > 0; datalen--, data++)
438 dig = (*data & 0xF0) >> 4;
440 this = 'A' + dig - 10;
446 this = 'A' + dig - 10;
455 char *hexdecode(char *data, size_t *len)
458 size_t bufsize, bufdata;
461 bufsize = bufdata = 0;
464 if((*data >= 'A') && (*data <= 'F'))
466 this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
467 } else if((*data >= '0') && (*data <= '9')) {
468 this = (this & 0x0F) | ((*data - '0') << 4);
481 if((*data >= 'A') && (*data <= 'F'))
483 this = (this & 0xF0) | (*data - 'A' + 10);
484 } else if((*data >= '0') && (*data <= '9')) {
485 this = (this & 0xF0) | (*data - '0');
499 char *base64encode(char *data, size_t datalen)
502 size_t bufsize, bufdata;
507 bufsize = bufdata = 0;
510 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
511 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
512 addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]);
513 addtobuf(buf, base64set[data[2] & 0x3f]);
519 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
520 addtobuf(buf, base64set[(data[0] & 0x03) << 4]);
521 bufcat(buf, "==", 2);
525 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
526 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
527 addtobuf(buf, base64set[(data[1] & 0x0f) << 2]);
534 char *base64decode(char *data, size_t *datalen)
538 size_t bufsize, bufdata;
541 bufsize = bufdata = 0;
544 for(; *data > 0; data++)
546 c = (int)(unsigned char)*data;
551 if(base64rev[c] == -1)
560 cur |= base64rev[c] >> -b;
565 cur |= base64rev[c] << b;
573 char *base32encode(char *data, size_t datalen)
576 size_t bufsize, bufdata;
581 bufsize = bufdata = 0;
584 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
585 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
586 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
587 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
588 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
589 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
590 addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]);
591 addtobuf(buf, base32set[data[4] & 0x1f]);
597 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
598 addtobuf(buf, base32set[((data[0] & 0x07) << 2)]);
599 bufcat(buf, "======", 6);
603 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
604 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
605 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
606 addtobuf(buf, base32set[((data[1] & 0x01) << 4)]);
607 bufcat(buf, "====", 4);
611 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
612 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
613 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
614 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
615 addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]);
616 bufcat(buf, "===", 3);
620 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
621 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
622 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
623 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
624 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
625 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
626 addtobuf(buf, base32set[((data[3] & 0x03) << 3)]);
633 char *base32decode(char *data, size_t *datalen)
637 size_t bufsize, bufdata;
640 bufsize = bufdata = 0;
643 for(; *data > 0; data++)
645 c = (int)(unsigned char)*data;
650 if(base32rev[c] == -1)
659 cur |= base32rev[c] >> -b;
664 cur |= base32rev[c] << b;
672 void _freeparr(void **arr)
678 for(buf = arr; *buf != NULL; buf++)
683 int _parrlen(void **arr)
689 for(i = 0; *arr != NULL; arr++)
694 char *getetcpath(char *binpath)
698 size_t etcpathsize, etcpathdata;
701 etcpathsize = etcpathdata = 0;
709 for(p = binpath; *p && (*p != ':'); p++);
710 for(; (p >= binpath) && (*p != '/'); p--);
714 addtobuf(etcpath, ':');
715 bufcat(etcpath, binpath, p - binpath + 1);
716 bufcat(etcpath, "etc", 3);
718 } while((binpath = strchr(binpath, ':')) != NULL);
719 addtobuf(etcpath, 0);
723 char *findfile(char *gname, char *uname, char *homedir)
725 char *path, *binpath, *etcpath, *p;
727 if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, uname)) != NULL))
729 if(!access(path, F_OK))
735 if(strchr(gname, '/') != NULL)
737 if(!access(gname, F_OK))
738 return(sstrdup(gname));
740 if((binpath = getenv("PATH")) == NULL)
741 etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc");
743 etcpath = getetcpath(binpath);
744 for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":"))
746 if((path = sprintf2("%s/%s", p, gname)) != NULL)
748 if(!access(path, F_OK))
762 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
764 struct wcspair *pair;
766 pair = smalloc(sizeof(*pair));
767 memset(pair, 0, sizeof(*pair));
769 pair->key = swcsdup(key);
771 pair->val = swcsdup(val);
782 void freewcspair(struct wcspair *pair, struct wcspair **list)
786 for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
799 wchar_t *wpfind(struct wcspair *list, wchar_t *key)
801 for(; list != NULL; list = list->next)
803 if(!wcscmp(list->key, key))