A working wcscasecmp replacement.
[doldaconnect.git] / common / utils.c
CommitLineData
d3372da9 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*/
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
39static char *base64set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40static 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};
58static char *base32set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
59static 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
78char *vsprintf2(char *format, va_list al)
79{
80 int ret;
81 char *buf;
4dffb251 82 va_list al2;
d3372da9 83
4dffb251 84 va_copy(al2, al);
fe8c6c8e 85 ret = vsnprintf(NULL, 0, format, al2);
86 va_end(al2);
d3372da9 87 if((buf = malloc(ret + 1)) == NULL)
88 {
89 LOGOOM(ret + 1);
90 return(NULL);
91 }
4dffb251 92 va_copy(al2, al);
fe8c6c8e 93 vsnprintf(buf, ret + 1, format, al2);
94 va_end(al2);
d3372da9 95 return(buf);
96}
97
98char *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
109wchar_t *vswprintf2(wchar_t *format, va_list al)
110{
111 int ret;
112 wchar_t *buf;
113 size_t bufsize;
4dffb251 114 va_list al2;
d3372da9 115
116 buf = smalloc(sizeof(wchar_t) * (bufsize = 1024));
4dffb251 117 while(1)
118 {
119 va_copy(al2, al);
120 ret = vswprintf(buf, bufsize, format, al2);
fe8c6c8e 121 va_end(al2);
4dffb251 122 if(ret >= 0)
123 break;
d3372da9 124 buf = srealloc(buf, sizeof(wchar_t) * (bufsize *= 2));
4dffb251 125 }
d3372da9 126 if(bufsize > ret + 1)
127 buf = srealloc(buf, sizeof(wchar_t) * (ret + 1));
128 return(buf);
129}
130
131wchar_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
edccab15 142int 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
d3372da9 155wchar_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 {
8b17e919 174#ifdef DAEMON
d3372da9 175 flog(LOG_ERR, "icmbstowcs: could not open iconv structure for %s: %s", charset, strerror(errno));
8b17e919 176#endif
d3372da9 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
213wchar_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 {
96cf7d1f 221 if((def != NULL) && (*def == L'~'))
d3372da9 222 {
8b17e919 223#ifdef DAEMON
d3372da9 224 flog(LOG_WARNING, "icsmbstowcs: could not convert wcs string into charset %s: %s", charset, strerror(errno));
8b17e919 225#endif
d3372da9 226 def++;
227 }
228 return(def);
229 }
230 return(buf);
231}
232
233char *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 {
8b17e919 245#ifdef DAEMON
d3372da9 246 LOGOOM(bufsize);
8b17e919 247#endif
d3372da9 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 {
8b17e919 254#ifdef DAEMON
d3372da9 255 flog(LOG_ERR, "icwcstombs: could not open iconv structure for %s: %s", charset, strerror(errno));
8b17e919 256#endif
d3372da9 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
293char *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 {
96cf7d1f 301 if((def != NULL) && (*def == '~'))
d3372da9 302 {
8b17e919 303#ifdef DAEMON
d3372da9 304 flog(LOG_WARNING, "icswcstombs: could not convert mbs string from charset %s: %s", charset, strerror(errno));
8b17e919 305#endif
d3372da9 306 def++;
307 }
308 return(def);
309 }
310 return(buf);
311}
312
313wchar_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
322wchar_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 {
8b17e919 343#ifdef DAEMON
d3372da9 344 flog(LOG_ERR, "ucptowc: could not open iconv structure for UCS-4BE: %s", strerror(errno));
8b17e919 345#endif
d3372da9 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
384void _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
403double 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
411int 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
442int wcscasecmp(const wchar_t *s1, const wchar_t *s2)
443{
2b1082a6 444 for(; (towlower(*s1) == towlower(*s2)) && (*s1 != L'\0'); s1++, s2++);
d3372da9 445 return(towlower(*s1) - towlower(*s2));
446}
447#endif
448
449char *hexencode(char *data, size_t datalen)
450{
451 char *buf, this;
452 size_t bufsize, bufdata;
453 int dig;
454
455 buf = NULL;
456 bufsize = bufdata = 0;
457 for(; datalen > 0; datalen--, data++)
458 {
459 dig = (*data & 0xF0) >> 4;
460 if(dig > 9)
461 this = 'A' + dig - 10;
462 else
463 this = dig + '0';
464 addtobuf(buf, this);
465 dig = *data & 0x0F;
466 if(dig > 9)
467 this = 'A' + dig - 10;
468 else
469 this = dig + '0';
470 addtobuf(buf, this);
471 }
472 addtobuf(buf, 0);
473 return(buf);
474}
475
476char *hexdecode(char *data, size_t *len)
477{
478 char *buf, this;
479 size_t bufsize, bufdata;
480
481 buf = NULL;
482 bufsize = bufdata = 0;
483 for(; *data; data++)
484 {
485 if((*data >= 'A') && (*data <= 'F'))
486 {
487 this = (this & 0x0F) | ((*data - 'A' + 10) << 4);
488 } else if((*data >= '0') && (*data <= '9')) {
489 this = (this & 0x0F) | ((*data - '0') << 4);
490 } else {
491 if(buf != NULL)
492 free(buf);
493 return(NULL);
494 }
495 data++;
496 if(!*data)
497 {
498 if(buf != NULL)
499 free(buf);
500 return(NULL);
501 }
502 if((*data >= 'A') && (*data <= 'F'))
503 {
504 this = (this & 0xF0) | (*data - 'A' + 10);
505 } else if((*data >= '0') && (*data <= '9')) {
506 this = (this & 0xF0) | (*data - '0');
507 } else {
508 if(buf != NULL)
509 free(buf);
510 return(NULL);
511 }
512 addtobuf(buf, this);
513 }
514 addtobuf(buf, 0);
515 if(len != NULL)
516 *len = bufdata - 1;
517 return(buf);
518}
519
520char *base64encode(char *data, size_t datalen)
521{
522 char *buf;
523 size_t bufsize, bufdata;
524
525 if(datalen == 0)
526 return(sstrdup(""));
527 buf = NULL;
528 bufsize = bufdata = 0;
529 while(datalen >= 3)
530 {
531 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
532 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
533 addtobuf(buf, base64set[((data[1] & 0x0f) << 2) | ((data[2] & 0xc0) >> 6)]);
534 addtobuf(buf, base64set[data[2] & 0x3f]);
535 datalen -= 3;
536 data += 3;
537 }
538 if(datalen == 1)
539 {
540 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
541 addtobuf(buf, base64set[(data[0] & 0x03) << 4]);
542 bufcat(buf, "==", 2);
543 }
544 if(datalen == 2)
545 {
546 addtobuf(buf, base64set[(data[0] & 0xfc) >> 2]);
547 addtobuf(buf, base64set[((data[0] & 0x03) << 4) | ((data[1] & 0xf0) >> 4)]);
548 addtobuf(buf, base64set[(data[1] & 0x0f) << 2]);
549 addtobuf(buf, '=');
550 }
551 addtobuf(buf, 0);
552 return(buf);
553}
554
555char *base64decode(char *data, size_t *datalen)
556{
557 int b, c;
558 char *buf, cur;
559 size_t bufsize, bufdata;
560
561 buf = NULL;
562 bufsize = bufdata = 0;
563 cur = 0;
564 b = 8;
565 for(; *data > 0; data++)
566 {
567 c = (int)(unsigned char)*data;
568 if(c == '=')
569 break;
65399bc2 570 if(c == '\n')
571 continue;
d3372da9 572 if(base64rev[c] == -1)
573 {
574 if(buf != NULL)
575 free(buf);
576 return(NULL);
577 }
578 b -= 6;
579 if(b <= 0)
580 {
581 cur |= base64rev[c] >> -b;
582 addtobuf(buf, cur);
583 b += 8;
584 cur = 0;
585 }
586 cur |= base64rev[c] << b;
587 }
588 if(datalen != NULL)
589 *datalen = bufdata;
590 addtobuf(buf, 0);
591 return(buf);
592}
593
594char *base32encode(char *data, size_t datalen)
595{
596 char *buf;
597 size_t bufsize, bufdata;
598
599 if(datalen == 0)
600 return(sstrdup(""));
601 buf = NULL;
602 bufsize = bufdata = 0;
603 while(datalen >= 5)
604 {
605 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
606 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
607 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
608 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
609 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
610 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
611 addtobuf(buf, base32set[((data[3] & 0x03) << 3) | ((data[4] & 0xe0) >> 5)]);
612 addtobuf(buf, base32set[data[4] & 0x1f]);
613 datalen -= 5;
614 data += 5;
615 }
616 if(datalen == 1)
617 {
618 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
619 addtobuf(buf, base32set[((data[0] & 0x07) << 2)]);
620 bufcat(buf, "======", 6);
621 }
622 if(datalen == 2)
623 {
624 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
625 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
626 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
627 addtobuf(buf, base32set[((data[1] & 0x01) << 4)]);
628 bufcat(buf, "====", 4);
629 }
630 if(datalen == 3)
631 {
632 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
633 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
634 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
635 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
636 addtobuf(buf, base32set[((data[2] & 0x0f) << 1)]);
637 bufcat(buf, "===", 3);
638 }
639 if(datalen == 4)
640 {
641 addtobuf(buf, base32set[((data[0] & 0xf8) >> 3)]);
642 addtobuf(buf, base32set[((data[0] & 0x07) << 2) | ((data[1] & 0xc0) >> 6)]);
643 addtobuf(buf, base32set[((data[1] & 0x3e) >> 1)]);
644 addtobuf(buf, base32set[((data[1] & 0x01) << 4) | ((data[2] & 0xf0) >> 4)]);
645 addtobuf(buf, base32set[((data[2] & 0x0f) << 1) | ((data[3] & 0x80) >> 7)]);
646 addtobuf(buf, base32set[((data[3] & 0x7c) >> 2)]);
647 addtobuf(buf, base32set[((data[3] & 0x03) << 3)]);
648 bufcat(buf, "=", 1);
649 }
650 addtobuf(buf, 0);
651 return(buf);
652}
653
654char *base32decode(char *data, size_t *datalen)
655{
656 int b, c;
657 char *buf, cur;
658 size_t bufsize, bufdata;
659
660 buf = NULL;
661 bufsize = bufdata = 0;
662 cur = 0;
663 b = 8;
664 for(; *data > 0; data++)
665 {
666 c = (int)(unsigned char)*data;
667 if(c == '=')
668 break;
65399bc2 669 if(c == '\n')
670 continue;
d3372da9 671 if(base32rev[c] == -1)
672 {
673 if(buf != NULL)
674 free(buf);
675 return(NULL);
676 }
677 b -= 5;
678 if(b <= 0)
679 {
680 cur |= base32rev[c] >> -b;
681 addtobuf(buf, cur);
682 b += 8;
683 cur = 0;
684 }
685 cur |= base32rev[c] << b;
686 }
687 if(datalen != NULL)
688 *datalen = bufdata;
689 addtobuf(buf, 0);
690 return(buf);
691}
692
693void _freeparr(void **arr)
694{
695 void **buf;
696
697 if(arr == NULL)
698 return;
699 for(buf = arr; *buf != NULL; buf++)
700 free(*buf);
701 free(arr);
702}
703
704int _parrlen(void **arr)
705{
706 int i;
707
708 if(arr == NULL)
709 return(0);
710 for(i = 0; *arr != NULL; arr++)
711 i++;
712 return(i);
713}
714
715char *getetcpath(char *binpath)
716{
73247114 717 int f;
d3372da9 718 char *etcpath, *p;
719 size_t etcpathsize, etcpathdata;
720
721 etcpath = NULL;
722 etcpathsize = etcpathdata = 0;
73247114 723 f = 1;
d3372da9 724 do
725 {
73247114 726 if(f)
727 f = 0;
728 else
729 binpath++;
d3372da9 730 for(p = binpath; *p && (*p != ':'); p++);
731 for(; (p >= binpath) && (*p != '/'); p--);
732 if(p >= binpath)
733 {
734 if(etcpathdata > 0)
735 addtobuf(etcpath, ':');
736 bufcat(etcpath, binpath, p - binpath + 1);
737 bufcat(etcpath, "etc", 3);
738 }
739 } while((binpath = strchr(binpath, ':')) != NULL);
740 addtobuf(etcpath, 0);
741 return(etcpath);
742}
743
1df8650c 744char *findfile(char *name, char *homedir, int filldef)
d3372da9 745{
746 char *path, *binpath, *etcpath, *p;
48b2d6d1 747 struct passwd *pw;
1df8650c 748 int mode, homeonly;
d3372da9 749
1df8650c 750 if(name == NULL)
751 return(NULL);
752
41ac4371 753 mode = R_OK | (filldef ? W_OK : 0);
1df8650c 754 homeonly = homedir != NULL;
755
756 if(!strchr(name, '/'))
757 {
41ac4371 758 if(homedir == NULL)
759 homedir = getenv("HOME");
48b2d6d1 760 if((homedir == NULL) && ((pw = getpwuid(getuid())) != NULL))
761 homedir = pw->pw_dir;
1df8650c 762 if((homedir != NULL) && ((path = sprintf2("%s/.%s", homedir, name)) != NULL))
48b2d6d1 763 {
41ac4371 764 if(!access(path, mode))
48b2d6d1 765 return(path);
766 free(path);
767 }
d3372da9 768 }
1df8650c 769
770 if(!homeonly)
d3372da9 771 {
1df8650c 772 if(strchr(name, '/') != NULL)
d3372da9 773 {
1df8650c 774 if(!access(name, mode))
775 return(sstrdup(name));
d3372da9 776 } else {
777 if((binpath = getenv("PATH")) == NULL)
778 etcpath = sstrdup("/usr/local/etc:/etc:/usr/etc");
779 else
780 etcpath = getetcpath(binpath);
781 for(p = strtok(etcpath, ":"); p != NULL; p = strtok(NULL, ":"))
782 {
1df8650c 783 if((path = sprintf2("%s/%s", p, name)) != NULL)
d3372da9 784 {
41ac4371 785 if(!access(path, mode))
d3372da9 786 {
787 free(etcpath);
788 return(path);
789 }
790 free(path);
791 }
792 }
793 free(etcpath);
794 }
795 }
1df8650c 796
48b2d6d1 797 if(filldef) {
1df8650c 798 if(homedir)
799 return(sprintf2("%s/.%s", homedir, name));
800 return(sprintf2("/etc/%s", name));
48b2d6d1 801 } else {
802 return(NULL);
803 }
d3372da9 804}
9ec790e8 805
806struct wcspair *newwcspair(wchar_t *key, wchar_t *val, struct wcspair **list)
807{
808 struct wcspair *pair;
809
810 pair = smalloc(sizeof(*pair));
811 memset(pair, 0, sizeof(*pair));
812 if(key != NULL)
813 pair->key = swcsdup(key);
814 if(val != NULL)
815 pair->val = swcsdup(val);
816 if(list == NULL)
817 {
818 pair->next = NULL;
819 } else {
820 pair->next = *list;
821 *list = pair;
822 }
823 return(pair);
824}
825
826void freewcspair(struct wcspair *pair, struct wcspair **list)
827{
497fe07b 828 struct wcspair *cur;
829
830 for(cur = *list; cur != NULL; list = &(cur->next), cur = cur->next)
9ec790e8 831 {
497fe07b 832 if(cur == pair)
833 {
834 *list = cur->next;
835 break;
836 }
9ec790e8 837 }
838 free(pair->key);
839 free(pair->val);
840 free(pair);
841}
a2761258 842
843wchar_t *wpfind(struct wcspair *list, wchar_t *key)
844{
845 for(; list != NULL; list = list->next)
846 {
847 if(!wcscmp(list->key, key))
848 return(list->val);
849 }
850 return(NULL);
851}