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