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