bb297818a036e9fccc73df8dc3ef8512b820eea0
[doldaconnect.git] / common / utils.c
1 /*
2  *  Dolda Connect - Modular multiuser Direct Connect-style client
3  *  Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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 */
19 #include <stdlib.h>
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
36 #include <utils.h>
37 #include <log.h>
38
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,
57 };
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,
76 };
77
78 char *vsprintf2(char *format, va_list al)
79 {
80     int ret;
81     char *buf;
82     va_list al2;
83     
84     va_copy(al2, al);
85     ret = vsnprintf(NULL, 0, format, al2);
86     va_end(al2);
87     if((buf = malloc(ret + 1)) == NULL)
88     {
89         LOGOOM(ret + 1);
90         return(NULL);
91     }
92     va_copy(al2, al);
93     vsnprintf(buf, ret + 1, format, al2);
94     va_end(al2);
95     return(buf);
96 }
97
98 char *sprintf2(char *format, ...)
99 {
100     va_list args;
101     char *buf;
102     
103     va_start(args, format);
104     buf = vsprintf2(format, args);
105     va_end(args);
106     return(buf);
107 }
108
109 wchar_t *vswprintf2(wchar_t *format, va_list al)
110 {
111     int ret;
112     wchar_t *buf;
113     size_t bufsize;
114     va_list al2;
115     
116     buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
117     while(1)
118     {
119         va_copy(al2, al);
120         ret = vswprintf(buf, bufsize, format, al2);
121         va_end(al2);
122         if(ret >= 0)
123             break;
124         buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
125     }
126     if(bufsize > ret + 1)
127         buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
128     return(buf);
129 }
130
131 wchar_t *swprintf2(wchar_t *format, ...)
132 {
133     va_list args;
134     wchar_t *buf;
135     
136     va_start(args, format);
137     buf = vswprintf2(format, args);
138     va_end(args);
139     return(buf);
140 }
141
142 int havecharset(char *charset)
143 {
144     iconv_t cd;
145     
146     if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
147         return(0);
148     iconv_close(cd);
149     if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
150         return(0);
151     iconv_close(cd);
152     return(1);
153 }
154
155 wchar_t *icmbstowcs(char *mbs, char *charset)
156 {
157     int ret;
158     char *buf;
159     char *p, *p2;
160     size_t len1, len2, bufsize, data;
161     iconv_t cd;
162     
163     len1 = strlen(mbs) + 1;
164     bufsize = len2 = len1 * sizeof(wchar_t);
165     if((buf = malloc(bufsize)) == NULL)
166     {
167         LOGOOM(bufsize);
168         return(NULL);
169     }
170     if(charset == NULL)
171         charset = nl_langinfo(CODESET);
172     if((cd = iconv_open("wchar_t", charset)) == (iconv_t)-1)
173     {
174 #ifdef DAEMON
175         flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno));
176 #endif
177         free(buf);
178         return(NULL);
179     }
180     p = buf;
181     while(len1 > 0)
182     {
183         ret = iconv(cd, &mbs, &len1, &p, &len2);
184         if(ret < 0)
185         {
186             if(errno == E2BIG)
187             {
188                 data = p - buf;
189                 len2 += 128;
190                 bufsize += 128;
191                 if((p2 = realloc(buf, bufsize)) == NULL)
192                 {
193                     LOGOOM(bufsize);
194                     free(buf);
195                     iconv_close(cd);
196                     return(NULL);
197                 }
198                 buf = p2;
199                 p = buf + data;
200             } else {
201                 free(buf);
202                 iconv_close(cd);
203                 return(NULL);
204             }
205         }
206     }
207     if(len2 > 0)
208         buf = realloc(buf, p - buf);
209     iconv_close(cd);
210     return((wchar_t *)buf);
211 }
212
213 wchar_t *icsmbstowcs(char *mbs, char *charset, wchar_t *def)
214 {
215     static wchar_t *buf = NULL;
216     
217     if(buf != NULL)
218         free(buf);
219     if((buf = icmbstowcs(mbs, charset)) == NULL)
220     {
221         if((def != NULL) && (*def == L'~'))
222         {
223 #ifdef DAEMON
224             flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno));
225 #endif
226             def++;
227         }
228         return(def);
229     }
230     return(buf);
231 }
232
233 char *icwcstombs(wchar_t *wcs, char *charset)
234 {
235     int ret;
236     char *buf;
237     char *p, *p2;
238     size_t len1, len2, bufsize, data;
239     iconv_t cd;
240     
241     len1 = sizeof(wchar_t) * (wcslen(wcs) + 1);
242     bufsize = len2 = len1;
243     if((buf = malloc(bufsize)) == NULL)
244     {
245 #ifdef DAEMON
246         LOGOOM(bufsize);
247 #endif
248         return(NULL);
249     }
250     if(charset == NULL)
251         charset = nl_langinfo(CODESET);
252     if((cd = iconv_open(charset, "wchar_t")) == (iconv_t)-1)
253     {
254 #ifdef DAEMON
255         flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno));
256 #endif
257         free(buf);
258         return(NULL);
259     }
260     p = buf;
261     while(len1 > 0)
262     {
263         ret = iconv(cd, (char **)&wcs, &len1, &p, &len2);
264         if(ret < 0)
265         {
266             if(errno == E2BIG)
267             {
268                 data = p - buf;
269                 len2 += 128;
270                 bufsize += 128;
271                 if((p2 = realloc(buf, bufsize)) == NULL)
272                 {
273                     LOGOOM(bufsize);
274                     free(buf);
275                     iconv_close(cd);
276                     return(NULL);
277                 }
278                 buf = p2;
279                 p = buf + data;
280             } else {
281                 free(buf);
282                 iconv_close(cd);
283                 return(NULL);
284             }
285         }
286     }
287     if(len2 > 0)
288         buf = realloc(buf, p - buf);
289     iconv_close(cd);
290     return(buf);
291 }
292
293 char *icswcstombs(wchar_t *wcs, char *charset, char *def)
294 {
295     static char *buf = NULL;
296     
297     if(buf != NULL)
298         free(buf);
299     if((buf = icwcstombs(wcs, charset)) == NULL)
300     {
301         if((def != NULL) && (*def == '~'))
302         {
303 #ifdef DAEMON
304             flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno));
305 #endif
306             def++;
307         }
308         return(def);
309     }
310     return(buf);
311 }
312
313 wchar_t *wcstolower(wchar_t *wcs)
314 {
315     wchar_t *p;
316     
317     for(p = wcs; *p != L'\0'; p++)
318         *p = towlower(*p);
319     return(wcs);
320 }
321
322 wchar_t ucptowc(int ucp)
323 {
324     int ret;
325     unsigned long ucpbuf;
326     char *buf;
327     char *mbsp, *p, *p2;
328     wchar_t res;
329     size_t len1, len2, bufsize, data;
330     iconv_t cd;
331     
332     ucpbuf = htonl(ucp);
333     mbsp = (char *)&ucpbuf;
334     len1 = 4;
335     bufsize = len2 = len1 * sizeof(wchar_t);
336     if((buf = malloc(bufsize)) == NULL)
337     {
338         LOGOOM(bufsize);
339         return(L'\0');
340     }
341     if((cd = iconv_open("wchar_t", "UCS-4BE")) == (iconv_t)-1)
342     {
343 #ifdef DAEMON
344         flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno));
345 #endif
346         free(buf);
347         return(L'\0');
348     }
349     p = buf;
350     while(len1 > 0)
351     {
352         ret = iconv(cd, &mbsp, &len1, &p, &len2);
353         if(ret < 0)
354         {
355             if(errno == E2BIG)
356             {
357                 data = p - buf;
358                 len2 += 128;
359                 bufsize += 128;
360                 if((p2 = realloc(buf, bufsize)) == NULL)
361                 {
362                     LOGOOM(bufsize);
363                     free(buf);
364                     iconv_close(cd);
365                     return(L'\0');
366                 }
367                 buf = p2;
368                 p = buf + data;
369             } else {
370                 free(buf);
371                 iconv_close(cd);
372                 return(L'\0');
373             }
374         }
375     }
376     if(len2 > 0)
377         buf = realloc(buf, p - buf);
378     iconv_close(cd);
379     res = *(wchar_t *)buf;
380     free(buf);
381     return(res);
382 }
383
384 void _sizebuf(void **buf, size_t *bufsize, size_t reqsize, size_t elsize, int algo)
385 {
386     if(*bufsize >= reqsize)
387         return;
388     switch(algo)
389     {
390     case 0:
391         *buf = srealloc(*buf, elsize * ((*bufsize) = reqsize));
392         break;
393     case 1:
394         if(*bufsize == 0)
395             *bufsize = 1;
396         while(*bufsize < reqsize)
397             *bufsize <<= 1;
398         *buf = srealloc(*buf, elsize * (*bufsize));
399         break;
400     }
401 }
402
403 double ntime(void)
404 {
405     struct timeval tv;
406     
407     gettimeofday(&tv, NULL);
408     return((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
409 }
410
411 int wcsexists(wchar_t *h, wchar_t *n)
412 {
413     int i, o, nl, hl;
414     wchar_t *ln, *lh;
415     
416     ln = alloca(sizeof(*ln) * (nl = wcslen(n)));
417     for(i = 0; i < nl; i++)
418         ln[i] = towlower(n[i]);
419     lh = alloca(sizeof(*lh) * (hl = wcslen(h)));
420     if(nl > hl)
421         return(0);
422     for(i = 0; i < nl; i++)
423         lh[i] = towlower(h[i]);
424     i = 0;
425     while(1)
426     {
427         for(o = 0; o < nl; o++)
428         {
429             if(lh[i + o] != ln[o])
430                 break;
431         }
432         if(o == nl)
433             return(1);
434         if(i == hl - nl)
435             return(0);
436         lh[i + nl] = towlower(h[i + nl]);
437         i++;
438     }
439 }
440
441 #ifndef HAVE_WCSCASECMP
442 int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
443 {
444     while(towlower(*s1) == towlower(*s2))
445     {
446         if(*s1 == L'\0')
447             return(0);
448     }
449     return(towlower(*s1) - towlower(*s2));
450 }
451 #endif
452
453 char *hexencode(char *data, size_t datalen)
454 {
455     char *buf, this;
456     size_t bufsize, bufdata;
457     int dig;
458     
459     buf = NULL;
460     bufsize = bufdata = 0;
461     for(; datalen > 0; datalen--, data++)
462     {
463         dig = (*data & 0xF0) >> 4;
464         if(dig > 9)
465             this = 'A' + dig - 10;
466         else
467             this = dig + '0';
468         addtobuf(buf, this);
469         dig = *data & 0x0F;
470         if(dig > 9)
471             this = 'A' + dig - 10;
472         else
473             this = dig + '0';
474         addtobuf(buf, this);
475     }
476     addtobuf(buf, 0);
477     return(buf);
478 }
479
480 char *hexdecode(char *data, size_t *len)
481 {
482     char *buf, this;
483     size_t bufsize, bufdata;
484     
485     buf = NULL;
486     bufsize = bufdata = 0;
487     for(; *data; data++)
488     {
489         if((*data >= 'A') && (*data <= 'F'))
490         {
491             this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
492         } else if((*data >= '0') && (*data <= '9')) {
493             this = (this & 0x0F) | ((*data - '0') << 4);
494         } else {
495             if(buf != NULL)
496                 free(buf);
497             return(NULL);
498         }
499         data++;
500         if(!*data)
501         {
502             if(buf != NULL)
503                 free(buf);
504             return(NULL);
505         }
506         if((*data >= 'A') && (*data <= 'F'))
507         {
508             this = (this & 0xF0) | (*data - 'A' + 10);
509         } else if((*data >= '0') && (*data <= '9')) {
510             this = (this & 0xF0) | (*data - '0');
511         } else {
512             if(buf != NULL)
513                 free(buf);
514             return(NULL);
515         }
516         addtobuf(buf, this);
517     }
518     addtobuf(buf, 0);
519     if(len != NULL)
520         *len = bufdata - 1;
521     return(buf);
522 }
523
524 char *base64encode(char *data, size_t datalen)
525 {
526     char *buf;
527     size_t bufsize, bufdata;
528     
529     if(datalen == 0)
530         return(sstrdup(""));
531     buf = NULL;
532     bufsize = bufdata = 0;
533     while(datalen >= 3)
534     {
535         addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
536         addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
537         addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]);
538         addtobuf(buf, base64set[data[2] & 0x3f]);
539         datalen -= 3;
540         data += 3;
541     }
542     if(datalen == 1)
543     {
544         addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
545         addtobuf(buf, base64set[(data[0] & 0x03) << 4]);
546         bufcat(buf, "==", 2);
547     }
548     if(datalen == 2)
549     {
550         addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
551         addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
552         addtobuf(buf, base64set[(data[1] & 0x0f) << 2]);
553         addtobuf(buf, '=');
554     }
555     addtobuf(buf, 0);
556     return(buf);
557 }
558
559 char *base64decode(char *data, size_t *datalen)
560 {
561     int b, c;
562     char *buf, cur;
563     size_t bufsize, bufdata;
564     
565     buf = NULL;
566     bufsize = bufdata = 0;
567     cur = 0;
568     b = 8;
569     for(; *data > 0; data++)
570     {
571         c = (int)(unsigned char)*data;
572         if(c == '=')
573             break;
574         if(c == '\n')
575             continue;
576         if(base64rev[c] == -1)
577         {
578             if(buf != NULL)
579                 free(buf);
580             return(NULL);
581         }
582         b -= 6;
583         if(b <= 0)
584         {
585             cur |= base64rev[c] >> -b;
586             addtobuf(buf, cur);
587             b += 8;
588             cur = 0;
589         }
590         cur |= base64rev[c] << b;
591     }
592     if(datalen != NULL)
593         *datalen = bufdata;
594     addtobuf(buf, 0);
595     return(buf);
596 }
597
598 char *base32encode(char *data, size_t datalen)
599 {
600     char *buf;
601     size_t bufsize, bufdata;
602     
603     if(datalen == 0)
604         return(sstrdup(""));
605     buf = NULL;
606     bufsize = bufdata = 0;
607     while(datalen >= 5)
608     {
609         addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
610         addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
611         addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
612         addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
613         addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
614         addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
615         addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]);
616         addtobuf(buf, base32set[data[4] & 0x1f]);
617         datalen -= 5;
618         data += 5;
619     }
620     if(datalen == 1)
621     {
622         addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
623         addtobuf(buf, base32set[((data[0] & 0x07) << 2)]);
624         bufcat(buf, "======", 6);
625     }
626     if(datalen == 2)
627     {
628         addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
629         addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
630         addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
631         addtobuf(buf, base32set[((data[1] & 0x01) << 4)]);
632         bufcat(buf, "====", 4);
633     }
634     if(datalen == 3)
635     {
636         addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
637         addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
638         addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
639         addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
640         addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]);
641         bufcat(buf, "===", 3);
642     }
643     if(datalen == 4)
644     {
645         addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
646         addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
647         addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
648         addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
649         addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
650         addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
651         addtobuf(buf, base32set[((data[3] & 0x03) << 3)]);
652         bufcat(buf, "=", 1);
653     }
654     addtobuf(buf, 0);
655     return(buf);
656 }
657
658 char *base32decode(char *data, size_t *datalen)
659 {
660     int b, c;
661     char *buf, cur;
662     size_t bufsize, bufdata;
663     
664     buf = NULL;
665     bufsize = bufdata = 0;
666     cur = 0;
667     b = 8;
668     for(; *data > 0; data++)
669     {
670         c = (int)(unsigned char)*data;
671         if(c == '=')
672             break;
673         if(c == '\n')
674             continue;
675         if(base32rev[c] == -1)
676         {
677             if(buf != NULL)
678                 free(buf);
679             return(NULL);
680         }
681         b -= 5;
682         if(b <= 0)
683         {
684             cur |= base32rev[c] >> -b;
685             addtobuf(buf, cur);
686             b += 8;
687             cur = 0;
688         }
689         cur |= base32rev[c] << b;
690     }
691     if(datalen != NULL)
692         *datalen = bufdata;
693     addtobuf(buf, 0);
694     return(buf);
695 }
696
697 void _freeparr(void **arr)
698 {
699     void **buf;
700     
701     if(arr == NULL)
702         return;
703     for(buf = arr; *buf != NULL; buf++)
704         free(*buf);
705     free(arr);
706 }
707
708 int _parrlen(void **arr)
709 {
710     int i;
711     
712     if(arr == NULL)
713         return(0);
714     for(i = 0; *arr != NULL; arr++)
715         i++;
716     return(i);
717 }
718
719 char *getetcpath(char *binpath)
720 {
721     int f;
722     char *etcpath, *p;
723     size_t etcpathsize, etcpathdata;
724
725     etcpath = NULL;
726     etcpathsize = etcpathdata = 0;
727     f = 1;
728     do
729     {
730         if(f)
731             f = 0;
732         else
733             binpath++;
734         for(p = binpath; *p && (*p != ':'); p++);
735         for(; (p >= binpath) && (*p != '/'); p--);
736         if(p >= binpath)
737         {
738             if(etcpathdata > 0)
739                 addtobuf(etcpath, ':');
740             bufcat(etcpath, binpath, p - binpath + 1);
741             bufcat(etcpath, "etc", 3);
742         }
743     } while((binpath = strchr(binpath, ':')) != NULL);
744     addtobuf(etcpath, 0);
745     return(etcpath);
746 }
747
748 char *findfile(char *name, char *homedir, int filldef)
749 {
750     char *path, *binpath, *etcpath, *p;
751     struct passwd *pw;
752     int mode, homeonly;
753     
754     if(name == NULL)
755         return(NULL);
756
757     mode = R_OK | (filldef ? W_OK : 0);
758     homeonly = homedir != NULL;
759
760     if(!strchr(name, '/'))
761     {
762         if(homedir == NULL)
763             homedir = getenv("HOME");
764         if((homedir == NULL) && ((pw = getpwuid(getuid())) != NULL))
765             homedir = pw->pw_dir;
766         if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, name)) != NULL))
767         {
768             if(!access(path, mode))
769                 return(path);
770             free(path);
771         }
772     }
773     
774     if(!homeonly)
775     {
776         if(strchr(name, '/') != NULL)
777         {
778             if(!access(name, mode))
779                 return(sstrdup(name));
780         } else {
781             if((binpath = getenv("PATH")) == NULL)
782                 etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc");
783             else
784                 etcpath = getetcpath(binpath);
785             for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":"))
786             {
787                 if((path = sprintf2("%s/%s", p, name)) != NULL)
788                 {
789                     if(!access(path, mode))
790                     {
791                         free(etcpath);
792                         return(path);
793                     }
794                     free(path);
795                 }
796             }
797             free(etcpath);
798         }
799     }
800     
801     if(filldef) {
802         if(homedir)
803             return(sprintf2("%s/.%s", homedir, name));
804         return(sprintf2("/etc/%s", name));
805     } else {
806         return(NULL);
807     }
808 }
809
810 struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
811 {
812     struct wcspair *pair;
813     
814     pair = smalloc(sizeof(*pair));
815     memset(pair, 0, sizeof(*pair));
816     if(key != NULL)
817         pair->key = swcsdup(key);
818     if(val != NULL)
819         pair->val = swcsdup(val);
820     if(list == NULL)
821     {
822         pair->next = NULL;
823     } else {
824         pair->next = *list;
825         *list = pair;
826     }
827     return(pair);
828 }
829
830 void freewcspair(struct wcspair *pair, struct wcspair **list)
831 {
832     struct wcspair *cur;
833     
834     for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
835     {
836         if(cur == pair)
837         {
838             *list = cur->next;
839             break;
840         }
841     }
842     free(pair->key);
843     free(pair->val);
844     free(pair);
845 }
846
847 wchar_t *wpfind(struct wcspair *list, wchar_t *key)
848 {
849     for(; list != NULL; list = list->next)
850     {
851         if(!wcscmp(list->key, key))
852             return(list->val);
853     }
854     return(NULL);
855 }