automatic arch-version.h update; CTCP support; helpfile fixes; disk-out-of-space...
[srvx.git] / src / tools.c
1 /* tools.c - miscellaneous utility functions
2  * Copyright 2000-2004 srvx Development Team
3  *
4  * This file is part of srvx.
5  *
6  * srvx is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with srvx; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
19  */
20
21 #include "log.h"
22 #include "nickserv.h"
23 #include "recdb.h"
24
25 #ifdef HAVE_NETDB_H
26 #include <netdb.h>
27 #endif
28 #ifdef HAVE_SYS_SOCKET_H
29 #include <sys/socket.h>
30 #endif
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
33 #endif
34
35 #define NUMNICKLOG 6
36 #define NUMNICKBASE (1 << NUMNICKLOG)
37 #define NUMNICKMASK (NUMNICKBASE - 1)
38
39 /* Yes, P10's encoding here is almost-but-not-quite MIME Base64.  Yay
40  * for gratuitous incompatibilities. */
41 static const char convert2y[256] = {
42   'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
43   'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
44   'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
45   'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
46 };
47
48 static const unsigned char convert2n[256] = {
49    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52   52,53,54,55,56,57,58,59,60,61, 0, 0, 0, 0, 0, 0, 
53    0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
54   15,16,17,18,19,20,21,22,23,24,25,62, 0,63, 0, 0,
55    0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
56   41,42,43,44,45,46,47,48,49,50,51, 0, 0, 0, 0, 0
57 };
58
59 unsigned long int
60 base64toint(const char* s, int count)
61 {
62     unsigned int i = 0;
63     while (*s && count) {
64         i = (i << NUMNICKLOG) + convert2n[(unsigned char)*s++];
65         count--;
66     }
67     return i;
68 }
69
70 const char* inttobase64(char* buf, unsigned int v, unsigned int count)
71 {
72   buf[count] = '\0';
73   while (count > 0) {
74       buf[--count] = convert2y[(unsigned char)(v & NUMNICKMASK)];
75       v >>= NUMNICKLOG;
76   }
77   return buf;
78 }
79
80 static char irc_tolower[256];
81 #undef tolower
82 #define tolower(X) irc_tolower[(unsigned char)(X)]
83
84 int
85 irccasecmp(const char *stra, const char *strb) {
86     while (*stra && (tolower(*stra) == tolower(*strb)))
87         stra++, strb++;
88     return tolower(*stra) - tolower(*strb);
89 }
90
91 int
92 ircncasecmp(const char *stra, const char *strb, unsigned int len) {
93     len--;
94     while (*stra && (tolower(*stra) == tolower(*strb)) && len)
95         stra++, strb++, len--;
96     return tolower(*stra) - tolower(*strb);
97 }
98
99 const char *
100 irccasestr(const char *haystack, const char *needle) {
101     unsigned int hay_len = strlen(haystack), needle_len = strlen(needle), pos;
102     if (hay_len < needle_len)
103         return NULL;
104     for (pos=0; pos<hay_len+1-needle_len; ++pos) {
105         if ((tolower(haystack[pos]) == tolower(*needle))
106             && !ircncasecmp(haystack+pos, needle, needle_len))
107             return haystack+pos;
108     }
109     return NULL;
110 }
111
112 int
113 split_line(char *line, int irc_colon, int argv_size, char *argv[])
114 {
115     int argc = 0;
116     int n;
117     while (*line && (argc < argv_size)) {
118         while (*line == ' ')
119             *line++ = 0;
120         if (*line == ':' && irc_colon && argc > 0) {
121             /* the rest is a single parameter */
122             argv[argc++] = line + 1;
123             break;
124         }
125         if (!*line)
126             break;
127         argv[argc++] = line;
128         if (argc >= argv_size)
129             break;
130         while (*line != ' ' && *line)
131             line++;
132     }
133 #ifdef NDEBUG
134     n = 0;
135 #else
136     for (n=argc; n<argv_size; n++)
137         argv[n] = (char*)0xFEEDBEEF;
138 #endif
139     return argc;
140 }
141
142 /* This is ircu's mmatch() function, from match.c. */
143 int mmatch(const char *old_mask, const char *new_mask)
144 {
145   register const char *m = old_mask;
146   register const char *n = new_mask;
147   const char *ma = m;
148   const char *na = n;
149   int wild = 0;
150   int mq = 0, nq = 0;
151
152   while (1)
153   {
154     if (*m == '*')
155     {
156       while (*m == '*')
157         m++;
158       wild = 1;
159       ma = m;
160       na = n;
161     }
162
163     if (!*m)
164     {
165       if (!*n)
166         return 0;
167       for (m--; (m > old_mask) && (*m == '?'); m--)
168         ;
169       if ((*m == '*') && (m > old_mask) && (m[-1] != '\\'))
170         return 0;
171       if (!wild)
172         return 1;
173       m = ma;
174
175       /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
176       if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?')))
177         ++na;
178
179       n = ++na;
180     }
181     else if (!*n)
182     {
183       while (*m == '*')
184         m++;
185       return (*m != 0);
186     }
187     if ((*m == '\\') && ((m[1] == '*') || (m[1] == '?')))
188     {
189       m++;
190       mq = 1;
191     }
192     else
193       mq = 0;
194
195     /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
196     if ((*n == '\\') && ((n[1] == '*') || (n[1] == '?')))
197     {
198       n++;
199       nq = 1;
200     }
201     else
202       nq = 0;
203
204 /*
205  * This `if' has been changed compared to match() to do the following:
206  * Match when:
207  *   old (m)         new (n)         boolean expression
208  *    *               any             (*m == '*' && !mq) ||
209  *    ?               any except '*'  (*m == '?' && !mq && (*n != '*' || nq)) ||
210  * any except * or ?  same as m       (!((*m == '*' || *m == '?') && !mq) &&
211  *                                      toLower(*m) == toLower(*n) &&
212  *                                        !((mq && !nq) || (!mq && nq)))
213  *
214  * Here `any' also includes \* and \? !
215  *
216  * After reworking the boolean expressions, we get:
217  * (Optimized to use boolean shortcircuits, with most frequently occuring
218  *  cases upfront (which took 2 hours!)).
219  */
220     if ((*m == '*' && !mq) ||
221         ((!mq || nq) && tolower(*m) == tolower(*n)) ||
222         (*m == '?' && !mq && (*n != '*' || nq)))
223     {
224       if (*m)
225         m++;
226       if (*n)
227         n++;
228     }
229     else
230     {
231       if (!wild)
232         return 1;
233       m = ma;
234
235       /* Added to `mmatch' : Because '\?' and '\*' now is one character: */
236       if ((*na == '\\') && ((na[1] == '*') || (na[1] == '?')))
237         ++na;
238
239       n = ++na;
240     }
241   }
242 }
243
244 int
245 match_ircglob(const char *text, const char *glob)
246 {
247     unsigned int star_p, q_cnt;
248     while (1) {
249         switch (*glob) {
250         case 0:
251             return !*text;
252         case '\\':
253             glob++;
254             /* intentionally not tolower(...) so people can force
255              * capitalization, or we can overload \ in the future */
256             if (*text++ != *glob++)
257                 return 0;
258             break;
259         case '*':
260         case '?':
261             star_p = q_cnt = 0;
262             do {
263                 if (*glob == '*')
264                     star_p = 1;
265                 else if (*glob == '?')
266                     q_cnt++;
267                 else
268                     break;
269                 glob++;
270             } while (1);
271             while (q_cnt) {
272                 if (!*text++)
273                     return 0;
274                 q_cnt--;
275             }
276             if (star_p) {
277                 /* if this is the last glob character, it will match any text */
278                 if (!*glob)
279                     return 1;
280                 /* Thanks to the loop above, we know that the next
281                  * character is a normal character.  So just look for
282                  * the right character.
283                  */
284                 for (; *text; text++) {
285                     if ((tolower(*text) == tolower(*glob))
286                         && match_ircglob(text+1, glob+1)) {
287                         return 1;
288                     }
289                 }
290                 return 0;
291             }
292             /* if !star_p, fall through to normal character case,
293              * first checking to see if ?s carried us to the end */
294             if (!*glob && !*text)
295                 return 1;
296         default:
297             if (!*text)
298                 return 0;
299             while (*text && *glob && *glob != '*' && *glob != '?' && *glob != '\\') {
300                 if (tolower(*text++) != tolower(*glob++))
301                     return 0;
302             }
303         }
304     }
305 }
306
307 extern const char *hidden_host_suffix;
308
309 int
310 user_matches_glob(struct userNode *user, const char *orig_glob, int include_nick)
311 {
312     char *glob, *marker;
313
314     /* Make a writable copy of the glob */
315     glob = alloca(strlen(orig_glob)+1);
316     strcpy(glob, orig_glob);
317     /* Check the nick, if it's present */
318     if (include_nick) {
319         if (!(marker = strchr(glob, '!'))) {
320             log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include a '!'", user->nick, orig_glob, include_nick);
321             return 0;
322         }
323         *marker = 0;
324         if (!match_ircglob(user->nick, glob)) return 0;
325         glob = marker + 1;
326     }
327     /* Check the ident */
328     if (!(marker = strchr(glob, '@'))) {
329         log_module(MAIN_LOG, LOG_ERROR, "user_matches_glob(\"%s\", \"%s\", %d) called, and glob doesn't include an '@'", user->nick, orig_glob, include_nick);
330         return 0;
331     }
332     *marker = 0;
333     if (!match_ircglob(user->ident, glob))
334         return 0;
335     glob = marker + 1;
336     /* Now check the host part */
337     if (isdigit(*glob) && !glob[strspn(glob, "0123456789./*?")]) {
338         /* Looks like an IP-based mask */
339         return match_ircglob(inet_ntoa(user->ip), glob);
340     } else {
341         /* The host part of the mask isn't IP-based */
342         if (hidden_host_suffix && user->handle_info) {
343             char hidden_host[HOSTLEN+1];
344             snprintf(hidden_host, sizeof(hidden_host), "%s.%s", user->handle_info->handle, hidden_host_suffix);
345             if (match_ircglob(hidden_host, glob))
346                 return 1;
347         }
348         return match_ircglob(user->hostname, glob);
349     }
350 }
351
352 int
353 is_ircmask(const char *text)
354 {
355     while (*text && (isalnum((char)*text) || strchr("-_[]|\\`^{}?*", *text)))
356         text++;
357     if (*text++ != '!')
358         return 0;
359     while (*text && *text != '@' && !isspace((char)*text))
360         text++;
361     if (*text++ != '@')
362         return 0;
363     while (*text && !isspace((char)*text))
364         text++;
365     return !*text;
366 }
367
368 int
369 is_gline(const char *text)
370 {
371     if (*text == '@')
372         return 0;
373     text += strcspn(text, "@!% \t\r\n");
374     if (*text++ != '@')
375         return 0;
376     if (!*text)
377         return 0;
378     while (*text && (isalnum((char)*text) || strchr(".-?*", *text)))
379         text++;
380     return !*text;
381 }
382
383 int
384 split_ircmask(char *text, char **nick, char **ident, char **host)
385 {
386     char *start;
387
388     start = text;
389     while (isalnum((char)*text) || strchr("=[]\\`^{}?*", *text))
390         text++;
391     if (*text != '!' || ((text - start) > NICKLEN))
392         return 0;
393     *text = 0;
394     if (nick)
395         *nick = start;
396
397     start = ++text;
398     while (*text && *text != '@' && !isspace((char)*text))
399         text++;
400     if (*text != '@' || ((text - start) > USERLEN))
401         return 0;
402     *text = 0;
403     if (ident)
404         *ident = start;
405     
406     start = ++text;
407     while (*text && (isalnum((char)*text) || strchr(".-?*", *text)))
408         text++;
409     if (host)
410         *host = start;
411     return !*text && ((text - start) <= HOSTLEN) && nick && ident && host;
412 }
413
414 char *
415 sanitize_ircmask(char *input)
416 {
417     unsigned int length, flag;
418     char *mask, *start, *output;
419
420     /* Sanitize everything in place; input *must* be a valid
421        hostmask. */
422     output = input;
423     flag = 0;
424
425     /* The nick is truncated at the end. */
426     length = 0;
427     mask = input;
428     while(*input++ != '!')
429     {
430         length++;
431     }
432     if(length > NICKLEN)
433     {
434         mask += NICKLEN;
435         *mask++ = '!';
436
437         /* This flag is used to indicate following parts should
438            be shifted. */
439         flag = 1;
440     }
441     else
442     {
443         mask = input;
444     }
445
446     /* The ident and host must be truncated at the beginning and
447        replaced with a '*' to be compatible with ircu. */
448     length = 0;
449     start = input;
450     while(*input++ != '@')
451     {
452         length++;
453     }
454     if(length > USERLEN || flag)
455     {
456         if(length > USERLEN)
457         {
458             start = input - USERLEN;
459             *mask++ = '*';
460         }
461         while(*start != '@')
462         {
463             *mask++ = *start++;
464         }
465         *mask++ = '@';
466
467         flag = 1;
468     }
469     else
470     {
471         mask = input;
472     }
473
474     length = 0;
475     start = input;
476     while(*input++)
477     {
478         length++;
479     }
480     if(length > HOSTLEN || flag)
481     {
482         if(length > HOSTLEN)
483         {
484             start = input - HOSTLEN;
485             *mask++ = '*';
486         }
487         while(*start)
488         {
489             *mask++ = *start++;
490         }
491         *mask = '\0';
492     }
493
494     return output;
495 }
496
497 static long
498 TypeLength(char type)
499 {
500     switch (type) {
501     case 'y': return 365*24*60*60;
502     case 'M': return 31*24*60*60;
503     case 'w': return 7*24*60*60;
504     case 'd': return 24*60*60;
505     case 'h': return 60*60;
506     case 'm': return 60;
507     case 's': return 1;
508     default: return 0;
509     }
510 }
511
512 unsigned long
513 ParseInterval(const char *interval)
514 {
515     unsigned long seconds = 0;
516     int partial = 0;
517     char c;
518
519     /* process the string, resetting the count if we find a unit character */
520     while ((c = *interval++)) {
521         if (isdigit((int)c)) {
522             partial = partial*10 + c - '0';
523         } else {
524             seconds += TypeLength(c) * partial;
525             partial = 0;
526         }
527     }
528     /* assume the last chunk is seconds (the normal case) */
529     return seconds + partial;
530 }
531
532 static long
533 GetSizeMultiplier(char type)
534 {
535     switch (type) {
536     case 'G': case 'g': return 1024*1024*1024;
537     case 'M': case 'm': return 1024*1024;
538     case 'K': case 'k': return 1024;
539     case 'B': case 'b': return 1;
540     default: return 0;
541     }
542 }
543
544 unsigned long
545 ParseVolume(const char *volume)
546 {
547     unsigned long accum = 0, partial = 0;
548     char c;
549     while ((c = *volume++)) {
550         if (isdigit((int)c)) {
551             partial = partial*10 + c - '0';
552         } else {
553             accum += GetSizeMultiplier(c) * partial;
554             partial = 0;
555         }
556     }
557     return accum + partial;
558 }
559
560 int
561 parse_ipmask(const char *str, struct in_addr *addr, unsigned long *mask)
562 {
563     int accum, pos;
564     unsigned long t_a, t_m;
565
566     t_a = t_m = pos = 0;
567     if (addr)
568         addr->s_addr = htonl(t_a);
569     if (mask)
570         *mask = t_m;
571     while (*str) {
572         if (!isdigit(*str))
573             return 0;
574         accum = 0;
575         do {
576             accum = (accum * 10) + *str++ - '0';
577         } while (isdigit(*str));
578         if (accum > 255)
579             return 0;
580         t_a = (t_a << 8) | accum;
581         t_m = (t_m << 8) | 255;
582         pos += 8;
583         if (*str == '.') {
584             str++;
585             while (*str == '*') {
586                 str++;
587                 if (*str == '.') {
588                     t_a <<= 8;
589                     t_m <<= 8;
590                     pos += 8;
591                     str++;
592                 } else if (*str == 0) {
593                     t_a <<= 32 - pos;
594                     t_m <<= 32 - pos;
595                     pos = 32;
596                     goto out;
597                 } else
598                     return 0;
599             }
600         } else if (*str == '/') {
601             int start = pos;
602             accum = 0;
603             do {
604                 accum = (accum * 10) + *str++ - '0';
605             } while (isdigit(*str));
606             while (pos < start+accum && pos < 32) {
607                 t_a = (t_a << 1) | 0;
608                 t_m = (t_m << 1) | 1;
609                 pos++;
610             }
611             if (pos != start+accum)
612                 return 0;
613         } else if (*str == 0)
614             break;
615         else
616             return 0;
617     }
618 out:
619     if (pos != 32)
620         return 0;
621     if (addr)
622         addr->s_addr = htonl(t_a);
623     if (mask)
624         *mask = t_m;
625     return 1;
626 }
627
628 char *
629 unsplit_string(char *set[], unsigned int max, char *dest)
630 {
631     static char unsplit_buffer[MAXLEN*2];
632     unsigned int ii, jj, pos;
633
634     if (!dest)
635         dest = unsplit_buffer;
636     for (ii=pos=0; ii<max; ii++) {
637         for (jj=0; set[ii][jj]; jj++)
638             dest[pos++] = set[ii][jj];
639         dest[pos++] = ' ';
640     }
641     dest[--pos] = 0;
642     return dest;
643 }
644
645 char *
646 intervalString2(char *output, time_t interval, int brief)
647 {
648     static const struct {
649         const char *name;
650         long length;
651     } unit[] = {
652         { "year", 365 * 24 * 60 * 60 },
653         { "week",   7 * 24 * 60 * 60 },
654         { "day",        24 * 60 * 60 },
655         { "hour",            60 * 60 },
656         { "minute",               60 },
657         { "second",                1 }
658     };
659     unsigned int type, words, pos, count;
660
661     if(!interval)
662     {
663         strcpy(output, brief ? "0s" : "0 seconds");
664         return output;
665     }
666
667     for (type = 0, words = pos = 0;
668          interval && (words < 2) && (type < ArrayLength(unit));
669          type++) {
670         if (interval < unit[type].length)
671             continue;
672         count = interval / unit[type].length;
673         interval = interval % unit[type].length;
674
675         if (brief)
676             pos += sprintf(output + pos, "%d%c", count, unit[type].name[0]);
677         else if (words == 1)
678             pos += sprintf(output + pos, " and %d %s", count, unit[type].name);
679         else
680             pos += sprintf(output + pos, "%d %s", count, unit[type].name);
681         if (count != 1)
682             output[pos++] = 's';
683         words++;
684     }
685
686     output[pos] = 0;
687     return output;
688 }
689
690 int
691 getipbyname(const char *name, unsigned long *ip)
692 {
693     struct hostent *he = gethostbyname(name);
694     if (!he)
695         return 0;
696     if (he->h_addrtype != AF_INET)
697         return 0;
698     memcpy(ip, he->h_addr_list[0], sizeof(*ip));
699     return 1;
700 }
701
702 DEFINE_LIST(string_buffer, char)
703
704 void
705 string_buffer_append_substring(struct string_buffer *buf, const char *tail, unsigned int len)
706 {
707     while (buf->used + len >= buf->size) {
708         if (!buf->size)
709             buf->size = 16;
710         else
711             buf->size <<= 1;
712         buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
713     }
714     memcpy(buf->list + buf->used, tail, len+1);
715     buf->used += len;
716 }
717
718 void
719 string_buffer_append_string(struct string_buffer *buf, const char *tail)
720 {
721     string_buffer_append_substring(buf, tail, strlen(tail));
722 }
723
724 void
725 string_buffer_append_vprintf(struct string_buffer *buf, const char *fmt, va_list args)
726 {
727     va_list working;
728     unsigned int len;
729     int ret;
730
731     VA_COPY(working, args);
732     len = strlen(fmt);
733     if (!buf->list || ((buf->used + buf->size) < len)) {
734         buf->size = buf->used + len;
735         buf->list = realloc(buf->list, buf->size);
736     }
737     ret = vsnprintf(buf->list + buf->used, buf->size - buf->used, fmt, working);
738     if (ret <= 0) {
739         /* pre-C99 behavior; double buffer size until it is big enough */
740         va_end(working);
741         VA_COPY(working, args);
742         while ((ret = vsnprintf(buf->list + buf->used, buf->size, fmt, working)) == -1) {
743             buf->size += len;
744             buf->list = realloc(buf->list, buf->size);
745             va_end(working);
746             VA_COPY(working, args);
747         }
748         buf->used += ret;
749     } else if (buf->used + ret < buf->size) {
750         /* no need to increase allocation size */
751         buf->used += ret;
752     } else {
753         /* now we know exactly how much space we need */
754         if (buf->size <= buf->used + ret) {
755             buf->size = buf->used + ret + 1;
756             buf->list = realloc(buf->list, buf->size);
757         }
758         va_end(working);
759         VA_COPY(working, args);
760         buf->used += vsnprintf(buf->list + buf->used, buf->size, fmt, working);
761     }
762     va_end(working);
763     va_end(args);
764 }
765
766 void string_buffer_append_printf(struct string_buffer *buf, const char *fmt, ...)
767 {
768     va_list args;
769     va_start(args, fmt);
770     string_buffer_append_vprintf(buf, fmt, args);
771 }
772
773 void
774 string_buffer_replace(struct string_buffer *buf, unsigned int from, unsigned int len, const char *repl)
775 {
776     unsigned int repl_len = strlen(repl);
777     if (from > buf->used)
778         return;
779     if (len + from > buf->used)
780         len = buf->used - from;
781     buf->used = buf->used + repl_len - len;
782     if (buf->size <= buf->used) {
783         while (buf->used >= buf->size)
784             buf->size <<= 1;
785         buf->list = realloc(buf->list, buf->size*sizeof(buf->list[0]));
786     }
787     memmove(buf->list+from+repl_len, buf->list+from+len, strlen(buf->list+from+len));
788     strcpy(buf->list+from, repl);
789 }
790
791 struct string_list str_tab;
792
793 const char *
794 strtab(unsigned int ii) {
795     if (ii > 65536)
796         return NULL;
797     if (ii > str_tab.size) {
798         unsigned int old_size = str_tab.size;
799         while (ii >= str_tab.size)
800             str_tab.size <<= 1;
801         str_tab.list = realloc(str_tab.list, str_tab.size*sizeof(str_tab.list[0]));
802         memset(str_tab.list+old_size, 0, (str_tab.size-old_size)*sizeof(str_tab.list[0]));
803     }
804     if (!str_tab.list[ii]) {
805         str_tab.list[ii] = malloc(12);
806         sprintf(str_tab.list[ii], "%u", ii);
807     }
808     return str_tab.list[ii];
809 }
810
811 void
812 tools_init(void)
813 {
814     unsigned int upr, lwr;
815     for (lwr=0; lwr<256; ++lwr)
816         tolower(lwr) = lwr;
817     for (upr='A', lwr='a'; lwr <= 'z'; ++upr, ++lwr)
818         tolower(upr) = lwr;
819 #ifdef WITH_PROTOCOL_P10
820     for (upr='[', lwr='{'; lwr <= '~'; ++upr, ++lwr)
821         tolower(upr) = lwr;
822     for (upr=0xc0, lwr=0xe0; lwr <= 0xf6; ++upr, ++lwr)
823         tolower(upr) = lwr;
824     for (upr=0xd8, lwr=0xf8; lwr <= 0xfe; ++upr, ++lwr)
825         tolower(upr) = lwr;
826 #endif
827     str_tab.size = 1001;
828     str_tab.list = calloc(str_tab.size, sizeof(str_tab.list[0]));
829 }
830
831 void
832 tools_cleanup(void)
833 {
834     unsigned int ii;
835     for (ii=0; ii<str_tab.size; ++ii)
836         free(str_tab.list[ii]);
837     free(str_tab.list);
838 }