fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[ircu2.10.12-pk.git] / ircd / table_gen.c
1 /*
2  * IRC - Internet Relay Chat, include/common.c
3  * Copyright (C) 1998 Andrea Cocito
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 1, or (at your option)
8  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 /*
21  * TABLE GENERATOR
22  * The following part of code is NOT included in the actual server's
23  * or library source, it's just used to build the above tables
24  *
25  * This should rebuild the actual tables and automatically place them
26  * into this source file, note that this part of code is for developers
27  * only, it's supposed to work on both signed and unsigned chars but I
28  * actually tested it only on signed-char architectures, the code and
29  * macros actually used by the server instead DO work and have been tested
30  * on platforms where0 char is both signed or unsigned, this is true as long
31  * as the <limits.h> macros are set properly and without any need to rebuild
32  * the tables (which as said an admin should NEVER do, tables need to be rebuilt
33  * only when one wants to really change the results or when one has to
34  * compile on architectures where a char is NOT eight bits [?!], yes
35  * it all is supposed to work in that case too... but I can't test it
36  * because I've not found a machine in the world where this happens).
37  *
38  * NEVER -f[un]signed-char on gcc since that does NOT fix the named macros
39  * and you end up in a non-ANSI environment where CHAR_MIN and CHAR_MAX
40  * are _not_ the real limits of a default 'char' type. This is true for
41  * both admins and coders.
42  *
43  */
44 #include "config.h"
45
46 #include "ircd_chattr.h"
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <ctype.h>
50
51
52 static void zeroTables(void);
53 static void markString(int macro, const char *s);
54 static void unMarkString(int macro, const char *s);
55 static void markRange(int macro, char from, char to);
56 static void moveMacro(int from, int to);
57 static void setLowHi(const char firstlow, const char lastlow,
58     const char firsthi);
59
60 char NTL_tolower_tab[1 + CHAR_MAX - CHAR_MIN];  /* 256 bytes */
61 char NTL_toupper_tab[1 + CHAR_MAX - CHAR_MIN];  /* 256 bytes */
62 int NTL_char_attrib[1 + CHAR_MAX - CHAR_MIN];   /* 256 ints = 0.5 to 2 kilobytes */
63
64 /*
65  * makeTables() 
66  * Where we make the tables, edit ONLY this to change the tables.
67  */
68
69 static void makeTables(void)
70 {
71
72   /* Start from a known status */
73   zeroTables();
74
75   /* Make the very elementary sets */
76   markRange(NTL_LOWER, 'a', 'z');
77   markString(NTL_LOWER, "{|}~");
78
79   markRange(NTL_UPPER, 'A', 'Z');
80   markString(NTL_UPPER, "[\\]^");
81
82   markRange(NTL_DIGIT, '0', '9');
83
84   markRange(NTL_CNTRL, '\000', '\037');
85
86   markString(NTL_PUNCT, "!\"#$%&'()*+,-./:;<=>?@_`");
87
88   markString(NTL_SPACE, "\011\012\013\014\015\040");
89
90   /* Make the derived sets, 
91    * WARNING: The order of these calls is important, some depend on 
92    * the results of the previous ones ! */
93
94   moveMacro(NTL_LOWER | NTL_UPPER, NTL_ALPHA);
95   moveMacro(NTL_ALPHA | NTL_DIGIT, NTL_ALNUM);
96   moveMacro(NTL_ALNUM | NTL_PUNCT, NTL_GRAPH);
97
98   moveMacro(NTL_GRAPH, NTL_PRINT);
99   markString(NTL_PRINT, " ");
100
101   markRange(NTL_IRCCH, '\041', (char) UCHAR_MAX);
102   unMarkString(NTL_IRCCH, "\054\240");
103
104   markRange(NTL_IRCCL, '\300', '\326');
105   markRange(NTL_IRCCL, '\330', '\336');
106
107   moveMacro(NTL_ALNUM, NTL_IRCHN);
108   markString(NTL_IRCHN, "-_."); /* Some DNS might allow '_' per RFC 1033 ! */
109
110   moveMacro(NTL_DIGIT, NTL_IRCIP);
111   markString(NTL_IRCIP, ".");
112
113   moveMacro(NTL_DIGIT, NTL_IRCIP6);
114   markString(NTL_IRCIP6, ":.ABCDEFabcdef");
115
116   moveMacro(NTL_DIGIT | NTL_ALPHA, NTL_IRCNK);
117   markString(NTL_IRCNK, "-_`");
118
119   moveMacro(NTL_ALNUM, NTL_IRCUI);
120   markRange(NTL_IRCUI, '\xe0', '\xf6');
121   markRange(NTL_IRCUI, '\xf8', '\xfe');
122   markRange(NTL_IRCUI, '\xc0', '\xd6');
123   markRange(NTL_IRCUI, '\xd8', '\xde');
124   markString(NTL_IRCUI, ".-_^'`~");
125   markString(NTL_EOL, "\n\r");
126   markString(NTL_CHPFX, "#&");
127   markString(NTL_KTIME, " ,-0123456789");
128
129   /* And finally let's take care of the toLower/toUpper stuff */
130
131   setLowHi('a', 'z', 'A');
132   setLowHi('\xe0', '\xf6', '\xc0');
133   setLowHi('\xf8', '\xfe', '\xd8');
134   setLowHi('{', '~', '[');
135 }
136
137 /* 
138  * main()
139  * This is the main program to be executed for -DMAKETABLES
140  */
141
142 static void dumphw(int *p, int beg);
143 static void dumphb(char *p, int beg);
144
145 int main(void)
146 {
147   int i;
148
149   /* Make the tables */
150   makeTables();
151
152   /* Dump them as ANSI C source to be included below */
153   printf("/*\n * Automatically Generated Tables - DO NOT EDIT\n */\n");
154   printf("#include <limits.h>\n");
155
156   /* NTL_tolower_tab */
157   printf("const char ToLowerTab_8859_1[] = {\n");
158   printf("#if (CHAR_MIN<0)\n");
159   i = (int)((char)SCHAR_MIN);
160   dumphb(NTL_tolower_tab, i);
161   printf("                ,\n");
162   printf("#endif /* (CHAR_MIN<0) */\n");
163   i = 0;
164   dumphb(NTL_tolower_tab, i);
165   printf("#if (!(CHAR_MIN<0))\n");
166   printf("                ,\n");
167   i = (int)((char)SCHAR_MIN);
168   dumphb(NTL_tolower_tab, i);
169   printf("#endif /* (!(CHAR_MIN<0)) */\n");
170   printf("  };\n\n");
171
172   /* NTL_toupper_tab */
173   printf("const char ToUpperTab_8859_1[] = {\n");
174   printf("#if (CHAR_MIN<0)\n");
175   i = (int)((char)SCHAR_MIN);
176   dumphb(NTL_toupper_tab, i);
177   printf("                ,\n");
178   printf("#endif /* (CHAR_MIN<0) */\n");
179   i = 0;
180   dumphb(NTL_toupper_tab, i);
181   printf("#if (!(CHAR_MIN<0))\n");
182   printf("                ,\n");
183   i = (int)((char)SCHAR_MIN);
184   dumphb(NTL_toupper_tab, i);
185   printf("#endif /* (!(CHAR_MIN<0)) */\n");
186   printf("  };\n\n");
187
188   /* NTL_char_attrib */
189   printf("const unsigned int IRCD_CharAttrTab[] = {\n");
190   printf("#if (CHAR_MIN<0)\n");
191   i = (int)((char)SCHAR_MIN);
192   dumphw(NTL_char_attrib, i);
193   printf("                ,\n");
194   printf("#endif /* (CHAR_MIN<0) */\n");
195   i = 0;
196   dumphw(NTL_char_attrib, i);
197   printf("#if (!(CHAR_MIN<0))\n");
198   printf("                ,\n");
199   i = (int)((char)SCHAR_MIN);
200   dumphw(NTL_char_attrib, i);
201   printf("#endif /* (!(CHAR_MIN<0)) */\n");
202   printf("  };\n\n");
203
204   return 0;
205
206 }
207
208 /* A few utility functions for makeTables() */
209
210 static void zeroTables(void)
211 {
212   int i;
213   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
214   {
215     NTL_tolower_tab[i - CHAR_MIN] = (char)i;    /* Unchanged */
216     NTL_toupper_tab[i - CHAR_MIN] = (char)i;    /* Unchanged */
217     NTL_char_attrib[i - CHAR_MIN] = 0x0000;     /* Nothing */
218   }
219 }
220
221 static void markString(int macro, const char *s)
222 {
223   while (*s)
224     NTL_char_attrib[*(s++) - CHAR_MIN] |= macro;
225 }
226
227 static void unMarkString(int macro, const char *s)
228 {
229   while (*s)
230     NTL_char_attrib[*(s++) - CHAR_MIN] &= ~macro;
231 }
232
233 static void markRange(int macro, char from, char to)
234 {
235   int i;
236   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
237     if (((unsigned char)i >= (unsigned char)from)
238         && ((unsigned char)i <= (unsigned char)to))
239       NTL_char_attrib[(char)i - CHAR_MIN] |= macro;
240 }
241
242 static void moveMacro(int from, int to)
243 {
244   int i;
245   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
246     if (NTL_char_attrib[i - CHAR_MIN] & from)
247       NTL_char_attrib[i - CHAR_MIN] |= to;
248 }
249
250 static void setLowHi(const char firstlow, const char lastlow,
251     const char firsthi)
252 {
253   int i, j;
254   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
255     if (((unsigned char)i >= (unsigned char)firstlow)
256         && ((unsigned char)i <= (unsigned char)lastlow))
257     {
258       j = ((int)((char)(i + (int)(firsthi - firstlow))));
259       NTL_tolower_tab[((char)j) - CHAR_MIN] = (char)i;
260       NTL_toupper_tab[((char)i) - CHAR_MIN] = (char)j;
261     }
262 }
263
264 /* These are used in main() to actually dump the tables, each function
265    dumps half table as hex/char constants... */
266
267 #define ROWSIZE 8
268
269 static void dumphb(char *tbl, int beg)
270 {
271   int i, j, k;
272   char *p = &tbl[beg - CHAR_MIN];
273   unsigned char c;
274   for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
275   {
276     k = i + ROWSIZE - 1;
277     if (k > SCHAR_MAX)
278       k = SCHAR_MAX;
279
280     c = (unsigned char)(beg + i);
281     printf("/*");
282     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
283         && (c != '\''))
284       printf(" '%c'", c);
285     else
286       printf(" x%02x", ((int)c));
287
288     c = (unsigned char)(beg + k);
289     printf("-");
290     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
291         && (c != '\''))
292       printf("'%c'", c);
293     else
294       printf("x%02x", ((int)c));
295     printf(" */");
296
297     for (j = i; j <= k; j++)
298     {
299       c = p[j];
300       if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
301           && (c != '\''))
302         printf("    '%c'", c);
303       else
304         printf(" '\\x%02x'", ((int)c));
305       if (j < SCHAR_MAX)
306         printf(",");
307     }
308     printf("\n");
309   }
310 }
311
312 static void dumphw(int *tbl, int beg)
313 {
314   int i, j, k;
315   int *p = &tbl[beg - CHAR_MIN];
316   unsigned char c;
317   for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
318   {
319     k = i + ROWSIZE - 1;
320     if (k > SCHAR_MAX)
321       k = SCHAR_MAX;
322
323     c = (unsigned char)(beg + i);
324     printf("/*");
325     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
326         && (c != '\''))
327       printf(" '%c'", c);
328     else
329       printf(" x%02x", ((int)c));
330
331     c = (unsigned char)(beg + k);
332     printf("-");
333     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
334         && (c != '\''))
335       printf("'%c'", c);
336     else
337       printf("x%02x", ((int)c));
338     printf(" */");
339
340     for (j = i; j <= k; j++)
341     {
342       printf(" 0x%04x", p[j] & 0xffffffff);
343       if (j < SCHAR_MAX)
344         printf(",");
345     }
346     printf("\n");
347   }
348 }
349