Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / common.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  * CODERS WARNING: DO _NOT_ EDIT THE TABLES IN THIS FILE
22  * Instead:
23  * a) Edit the table generator below, specifically the makeTables() function
24  * b) Recreate the common.c tables with 'make ctables'.
25  */
26
27
28 /*=============================================================================
29  * Actual source of the library stuff, not generated when making the tables
30  */
31
32 #ifndef MAKETABLES
33
34 /*=============================================================================
35  * Headers needed in this source file
36  */
37
38 #include "common.h"
39
40 /*=============================================================================
41  * Functions eventually inlined or visible externally
42  */
43
44 #ifndef FORCEINLINE
45 /* *INDENT-OFF* */
46 NTL_HDR_strChattr { NTL_SRC_strChattr }
47 NTL_HDR_strCasediff { NTL_SRC_strCasediff }
48 /* *INDENT-ON* */
49 #endif /* !FORCEINLINE */
50
51 /*=============================================================================
52  * Other functions visible externally
53  */
54
55 int strnChattr(const char *s, const size_t n)
56 {
57   register const char *rs = s;
58   register int x = ~0;
59   register int r = n;
60   while (*rs && r--)
61     x &= NTL_char_attrib[*rs++ - CHAR_MIN];
62   return x;
63 }
64
65 int strCasecmp(const char *a, const char *b)
66 {
67   register const char *ra = a;
68   register const char *rb = b;
69   while (toLower(*ra) == toLower(*rb))
70     if (!*ra++)
71       return 0;
72     else
73       rb++;
74   return (*ra - *rb);
75 }
76
77 int strnCasecmp(const char *a, const char *b, const size_t n)
78 {
79   register const char *ra = a;
80   register const char *rb = b;
81   register int left = n;
82   if (!left--)
83     return 0;
84   while (toLower(*ra) == toLower(*rb))
85     if ((!(*(ra++))) || (!(left--)))
86       return 0;
87     else
88       rb++;
89   return (*ra - *rb);
90 }
91
92 /*=============================================================================
93  * Automatically generated tables, don't touch these by hand !
94  */
95
96 /* *INDENT-OFF* */
97 /*
98  * DO not touch anything below this line !
99  * NTL_TOK_START
100  */
101 const char NTL_tolower_tab[] = {
102 #if (CHAR_MIN<0)
103 /* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
104 /* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
105 /* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
106 /* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
107 /* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
108 /* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
109 /* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
110 /* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
111 /* xc0-xc7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
112 /* xc8-xcf */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
113 /* xd0-xd7 */ '\xd0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xd7',
114 /* xd8-xdf */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xdf',
115 /* xe0-xe7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
116 /* xe8-xef */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
117 /* xf0-xf7 */ '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xf7',
118 /* xf8-xff */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xff'
119                 ,
120 #endif /* (CHAR_MIN<0) */
121 /* x00-x07 */ '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
122 /* x08-x0f */ '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
123 /* x10-x17 */ '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
124 /* x18-x1f */ '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
125 /* ' '-x27 */    ' ',    '!',    '"',    '#',    '$',    '%',    '&', '\x27',
126 /* '('-'/' */    '(',    ')',    '*',    '+',    ',',    '-',    '.',    '/',
127 /* '0'-'7' */    '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
128 /* '8'-'?' */    '8',    '9',    ':',    ';',    '<',    '=',    '>',    '?',
129 /* '@'-'G' */    '@',    'a',    'b',    'c',    'd',    'e',    'f',    'g',
130 /* 'H'-'O' */    'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
131 /* 'P'-'W' */    'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
132 /* 'X'-'_' */    'x',    'y',    'z',    '{',    '|',    '}',    '~',    '_',
133 /* '`'-'g' */    '`',    'a',    'b',    'c',    'd',    'e',    'f',    'g',
134 /* 'h'-'o' */    'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
135 /* 'p'-'w' */    'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
136 /* 'x'-x7f */    'x',    'y',    'z',    '{',    '|',    '}',    '~', '\x7f'
137 #if (!(CHAR_MIN<0))
138                 ,
139 /* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
140 /* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
141 /* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
142 /* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
143 /* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
144 /* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
145 /* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
146 /* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
147 /* xc0-xc7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
148 /* xc8-xcf */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
149 /* xd0-xd7 */ '\xd0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xd7',
150 /* xd8-xdf */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xdf',
151 /* xe0-xe7 */ '\xe0', '\xe1', '\xe2', '\xe3', '\xe4', '\xe5', '\xe6', '\xe7',
152 /* xe8-xef */ '\xe8', '\xe9', '\xea', '\xeb', '\xec', '\xed', '\xee', '\xef',
153 /* xf0-xf7 */ '\xf0', '\xf1', '\xf2', '\xf3', '\xf4', '\xf5', '\xf6', '\xf7',
154 /* xf8-xff */ '\xf8', '\xf9', '\xfa', '\xfb', '\xfc', '\xfd', '\xfe', '\xff'
155 #endif /* (!(CHAR_MIN<0)) */
156   };
157
158 const char NTL_toupper_tab[] = {
159 #if (CHAR_MIN<0)
160 /* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
161 /* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
162 /* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
163 /* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
164 /* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
165 /* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
166 /* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
167 /* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
168 /* xc0-xc7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
169 /* xc8-xcf */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
170 /* xd0-xd7 */ '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xd7',
171 /* xd8-xdf */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xdf',
172 /* xe0-xe7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
173 /* xe8-xef */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
174 /* xf0-xf7 */ '\xf0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xf7',
175 /* xf8-xff */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xff'
176                 ,
177 #endif /* (CHAR_MIN<0) */
178 /* x00-x07 */ '\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07',
179 /* x08-x0f */ '\x08', '\x09', '\x0a', '\x0b', '\x0c', '\x0d', '\x0e', '\x0f',
180 /* x10-x17 */ '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17',
181 /* x18-x1f */ '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f',
182 /* ' '-x27 */    ' ',    '!',    '"',    '#',    '$',    '%',    '&', '\x27',
183 /* '('-'/' */    '(',    ')',    '*',    '+',    ',',    '-',    '.',    '/',
184 /* '0'-'7' */    '0',    '1',    '2',    '3',    '4',    '5',    '6',    '7',
185 /* '8'-'?' */    '8',    '9',    ':',    ';',    '<',    '=',    '>',    '?',
186 /* '@'-'G' */    '@',    'A',    'B',    'C',    'D',    'E',    'F',    'G',
187 /* 'H'-'O' */    'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
188 /* 'P'-'W' */    'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
189 /* 'X'-'_' */    'X',    'Y',    'Z',    '[', '\x5c',    ']',    '^',    '_',
190 /* '`'-'g' */    '`',    'A',    'B',    'C',    'D',    'E',    'F',    'G',
191 /* 'h'-'o' */    'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
192 /* 'p'-'w' */    'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
193 /* 'x'-x7f */    'X',    'Y',    'Z',    '[', '\x5c',    ']',    '^', '\x7f'
194 #if (!(CHAR_MIN<0))
195                 ,
196 /* x80-x87 */ '\x80', '\x81', '\x82', '\x83', '\x84', '\x85', '\x86', '\x87',
197 /* x88-x8f */ '\x88', '\x89', '\x8a', '\x8b', '\x8c', '\x8d', '\x8e', '\x8f',
198 /* x90-x97 */ '\x90', '\x91', '\x92', '\x93', '\x94', '\x95', '\x96', '\x97',
199 /* x98-x9f */ '\x98', '\x99', '\x9a', '\x9b', '\x9c', '\x9d', '\x9e', '\x9f',
200 /* xa0-xa7 */ '\xa0', '\xa1', '\xa2', '\xa3', '\xa4', '\xa5', '\xa6', '\xa7',
201 /* xa8-xaf */ '\xa8', '\xa9', '\xaa', '\xab', '\xac', '\xad', '\xae', '\xaf',
202 /* xb0-xb7 */ '\xb0', '\xb1', '\xb2', '\xb3', '\xb4', '\xb5', '\xb6', '\xb7',
203 /* xb8-xbf */ '\xb8', '\xb9', '\xba', '\xbb', '\xbc', '\xbd', '\xbe', '\xbf',
204 /* xc0-xc7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
205 /* xc8-xcf */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
206 /* xd0-xd7 */ '\xd0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xd7',
207 /* xd8-xdf */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xdf',
208 /* xe0-xe7 */ '\xc0', '\xc1', '\xc2', '\xc3', '\xc4', '\xc5', '\xc6', '\xc7',
209 /* xe8-xef */ '\xc8', '\xc9', '\xca', '\xcb', '\xcc', '\xcd', '\xce', '\xcf',
210 /* xf0-xf7 */ '\xf0', '\xd1', '\xd2', '\xd3', '\xd4', '\xd5', '\xd6', '\xf7',
211 /* xf8-xff */ '\xd8', '\xd9', '\xda', '\xdb', '\xdc', '\xdd', '\xde', '\xff'
212 #endif /* (!(CHAR_MIN<0)) */
213   };
214
215 const unsigned int NTL_char_attrib[] = {
216 #if (CHAR_MIN<0)
217 /* x80-x87 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
218 /* x88-x8f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
219 /* x90-x97 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
220 /* x98-x9f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
221 /* xa0-xa7 */ 0x0000, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
222 /* xa8-xaf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
223 /* xb0-xb7 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
224 /* xb8-xbf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
225 /* xc0-xc7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
226 /* xc8-xcf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
227 /* xd0-xd7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
228 /* xd8-xdf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
229 /* xe0-xe7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
230 /* xe8-xef */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
231 /* xf0-xf7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400,
232 /* xf8-xff */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400
233                 ,
234 #endif /* (CHAR_MIN<0) */
235 /* x00-x07 */ 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0004,
236 /* x08-x0f */ 0x0404, 0x0504, 0x10504, 0x0504, 0x0504, 0x10504, 0x0404, 0x0404,
237 /* x10-x17 */ 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
238 /* x18-x1f */ 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404, 0x0404,
239 /* ' '-x27 */ 0x0140, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x24d0,
240 /* '('-'/' */ 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x00d0, 0x74d0, 0xe4d0, 0x04d0,
241 /* '0'-'7' */ 0xf459, 0xf459, 0xf459, 0xf459, 0xf459, 0xf459, 0xf459, 0xf459,
242 /* '8'-'?' */ 0xf459, 0xf459, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x04d0, 0x04d0,
243 /* '@'-'G' */ 0x04d0, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653,
244 /* 'H'-'O' */ 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653,
245 /* 'P'-'W' */ 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653,
246 /* 'X'-'_' */ 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x7653, 0x74d0,
247 /* '`'-'g' */ 0x34d0, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473,
248 /* 'h'-'o' */ 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473,
249 /* 'p'-'w' */ 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473,
250 /* 'x'-x7f */ 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x7473, 0x0400
251 #if (!(CHAR_MIN<0))
252                 ,
253 /* x80-x87 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
254 /* x88-x8f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
255 /* x90-x97 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
256 /* x98-x9f */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
257 /* xa0-xa7 */ 0x0000, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
258 /* xa8-xaf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
259 /* xb0-xb7 */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
260 /* xb8-xbf */ 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400,
261 /* xc0-xc7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
262 /* xc8-xcf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00,
263 /* xd0-xd7 */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
264 /* xd8-xdf */ 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x2c00, 0x0400,
265 /* xe0-xe7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
266 /* xe8-xef */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400,
267 /* xf0-xf7 */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400,
268 /* xf8-xff */ 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x2400, 0x0400
269 #endif /* (!(CHAR_MIN<0)) */
270   };
271
272 /*
273  * NTL_TOK_END
274  * DO not touch anything above this line !
275  */
276 /* *INDENT-ON* */
277
278 #endif /* !MAKETABLES */
279
280 /*=============================================================================
281  * TABLE GENERATOR
282  * The following part of code is NOT included in the actual server's
283  * or library source, it's just used to build the above tables
284  *
285  * This should rebuild the actual tables and automatically place them
286  * into this source file, note that this part of code is for developers
287  * only, it's supposed to work on both signed and unsigned chars but I
288  * actually tested it only on signed-char architectures, the code and
289  * macros actually used by the server instead DO work and have been tested
290  * on platforms where0 char is both signed or unsigned, this is true as long
291  * as the <limits.h> macros are set properly and without any need to rebuild
292  * the tables (wich as said an admin should NEVER do, tables need to be rebuilt
293  * only when one wants to really change the results or when one has to
294  * compile on architectures where a char is NOT eight bits [?!], yes
295  * it all is supposed to work in that case too... but I can't test it
296  * because I've not found a machine in the world where this happes).
297  *
298  * NEVER -f[un]signed-char on gcc since that does NOT fix the named macros
299  * and you end up in a non-ANSI environment where CHAR_MIN and CHAR_MAX
300  * are _not_ the real limits of a default 'char' type. This is true for
301  * both admins and coders.
302  *
303  */
304
305 #ifdef MAKETABLES
306
307 #include <stdlib.h>
308 #include <stdio.h>
309 #include <ctype.h>
310
311 #include "common.h"
312
313 static void zeroTables(void);
314 static void markString(int macro, const char *s);
315 static void unMarkString(int macro, const char *s);
316 static void markRange(int macro, char from, char to);
317 static void moveMacro(int from, int to);
318 static void setLowHi(const char firstlow, const char lastlow,
319     const char firsthi);
320
321 char NTL_tolower_tab[1 + CHAR_MAX - CHAR_MIN];  /* 256 bytes */
322 char NTL_toupper_tab[1 + CHAR_MAX - CHAR_MIN];  /* 256 bytes */
323 int NTL_char_attrib[1 + CHAR_MAX - CHAR_MIN];   /* 256 ints = 0.5 to 2 kilobytes */
324
325 /*
326  * makeTables() 
327  * Where we make the tables, edit ONLY this to change the tables.
328  */
329
330 static void makeTables(void)
331 {
332
333   /* Start from a known status */
334   zeroTables();
335
336   /* Make the very elementary sets */
337   markRange(NTL_LOWER, 'a', 'z');
338   markString(NTL_LOWER, "{|}~");
339
340   markRange(NTL_UPPER, 'A', 'Z');
341   markString(NTL_UPPER, "[\\]^");
342
343   markRange(NTL_DIGIT, '0', '9');
344
345   markRange(NTL_CNTRL, '\000', '\037');
346
347   markString(NTL_PUNCT, "!\"#$%&'()*+,-./:;<=>?@_`");
348
349   markString(NTL_SPACE, "\011\012\013\014\015\040");
350
351   /* Make the derived sets, 
352    * WARNING: The order of these calls is important, some depend on 
353    * the results of the previous ones ! */
354
355   moveMacro(NTL_LOWER | NTL_UPPER, NTL_ALPHA);
356   moveMacro(NTL_ALPHA | NTL_DIGIT, NTL_ALNUM);
357   moveMacro(NTL_ALNUM | NTL_PUNCT, NTL_GRAPH);
358
359   moveMacro(NTL_GRAPH, NTL_PRINT);
360   markString(NTL_PRINT, " ");
361
362   markRange(NTL_IRCCH, 0, UCHAR_MAX);
363   unMarkString(NTL_IRCCH, "\007\040\054\240");
364
365   markRange(NTL_IRCCL, '\300', '\326');
366   markRange(NTL_IRCCL, '\330', '\336');
367
368   moveMacro(NTL_ALNUM, NTL_IRCHN);
369   markString(NTL_IRCHN, "-_."); /* Some DNS might allow '_' per RFC 1033 ! */
370
371   moveMacro(NTL_DIGIT, NTL_IRCIP);
372   markString(NTL_IRCIP, ".");
373
374   moveMacro(NTL_DIGIT | NTL_ALPHA, NTL_IRCNK);
375   markString(NTL_IRCNK, "-_`");
376
377   moveMacro(NTL_ALNUM, NTL_IRCUI);
378   markRange(NTL_IRCUI, '\xe0', '\xf6');
379   markRange(NTL_IRCUI, '\xf8', '\xfe');
380   markRange(NTL_IRCUI, '\xc0', '\xd6');
381   markRange(NTL_IRCUI, '\xd8', '\xde');
382   markString(NTL_IRCUI, ".-_^'`~");
383   markString(NTL_EOL, "\n\r");
384
385   /* And finally let's take care of the toLower/toUpper stuff */
386
387   setLowHi('a', 'z', 'A');
388   setLowHi('\xe0', '\xf6', '\xc0');
389   setLowHi('\xf8', '\xfe', '\xd8');
390   setLowHi('{', '~', '[');
391
392 #ifndef FIXME                   /* Just to remember that this is to be removed in u10.06 */
393   setLowHi('\xd0', '\xd0', '\xd0');     /* Freeze the 0xD0 lower/upper */
394   setLowHi('\xf0', '\xf0', '\xf0');     /* Freeze the 0xF0 lower/upper */
395 #endif /* FIXME */
396
397
398 }
399
400 /* 
401  * main()
402  * This is the main program to be executed for -DMAKETABLES
403  */
404
405 static void dumphw(int *p, int beg);
406 static void dumphb(char *p, int beg);
407
408 int main(void)
409 {
410   int i, j, k;
411   char c, c1, c2;
412
413   /* Make the tables */
414   makeTables();
415
416   /* Dump them as ANSI C source to be included below */
417
418   /* NTL_tolower_tab */
419   printf("const char NTL_tolower_tab[] = {\n");
420   printf("#if (CHAR_MIN<0)\n");
421   i = (int)((char)SCHAR_MIN);
422   dumphb(NTL_tolower_tab, i);
423   printf("                ,\n");
424   printf("#endif /* (CHAR_MIN<0) */\n");
425   i = 0;
426   dumphb(NTL_tolower_tab, i);
427   printf("#if (!(CHAR_MIN<0))\n");
428   printf("                ,\n");
429   i = (int)((char)SCHAR_MIN);
430   dumphb(NTL_tolower_tab, i);
431   printf("#endif /* (!(CHAR_MIN<0)) */\n");
432   printf("  };\n\n");
433
434   /* NTL_toupper_tab */
435   printf("const char NTL_toupper_tab[] = {\n");
436   printf("#if (CHAR_MIN<0)\n");
437   i = (int)((char)SCHAR_MIN);
438   dumphb(NTL_toupper_tab, i);
439   printf("                ,\n");
440   printf("#endif /* (CHAR_MIN<0) */\n");
441   i = 0;
442   dumphb(NTL_toupper_tab, i);
443   printf("#if (!(CHAR_MIN<0))\n");
444   printf("                ,\n");
445   i = (int)((char)SCHAR_MIN);
446   dumphb(NTL_toupper_tab, i);
447   printf("#endif /* (!(CHAR_MIN<0)) */\n");
448   printf("  };\n\n");
449
450   /* NTL_char_attrib */
451   printf("const unsigned int NTL_char_attrib[] = {\n");
452   printf("#if (CHAR_MIN<0)\n");
453   i = (int)((char)SCHAR_MIN);
454   dumphw(NTL_char_attrib, i);
455   printf("                ,\n");
456   printf("#endif /* (CHAR_MIN<0) */\n");
457   i = 0;
458   dumphw(NTL_char_attrib, i);
459   printf("#if (!(CHAR_MIN<0))\n");
460   printf("                ,\n");
461   i = (int)((char)SCHAR_MIN);
462   dumphw(NTL_char_attrib, i);
463   printf("#endif /* (!(CHAR_MIN<0)) */\n");
464   printf("  };\n\n");
465
466   return 0;
467
468 }
469
470 /* A few utility functions for makeTables() */
471
472 static void zeroTables(void)
473 {
474   int i;
475   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
476   {
477     NTL_tolower_tab[i - CHAR_MIN] = (char)i;    /* Unchanged */
478     NTL_toupper_tab[i - CHAR_MIN] = (char)i;    /* Unchanged */
479     NTL_char_attrib[i - CHAR_MIN] = 0x0000;     /* Nothing */
480   };
481 }
482
483 static void markString(int macro, const char *s)
484 {
485   while (*s)
486     NTL_char_attrib[*(s++) - CHAR_MIN] |= macro;
487 }
488
489 static void unMarkString(int macro, const char *s)
490 {
491   while (*s)
492     NTL_char_attrib[*(s++) - CHAR_MIN] &= ~macro;
493 }
494
495 static void markRange(int macro, char from, char to)
496 {
497   int i;
498   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
499     if (((unsigned char)i >= (unsigned char)from)
500         && ((unsigned char)i <= (unsigned char)to))
501       NTL_char_attrib[(char)i - CHAR_MIN] |= macro;
502 }
503
504 static void moveMacro(int from, int to)
505 {
506   int i;
507   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
508     if (NTL_char_attrib[i - CHAR_MIN] & from)
509       NTL_char_attrib[i - CHAR_MIN] |= to;
510 }
511
512 static void setLowHi(const char firstlow, const char lastlow,
513     const char firsthi)
514 {
515   int i, j;
516   for (i = CHAR_MIN; i <= CHAR_MAX; i++)
517     if (((unsigned char)i >= (unsigned char)firstlow)
518         && ((unsigned char)i <= (unsigned char)lastlow))
519     {
520       j = ((int)((char)(i + (int)(firsthi - firstlow))));
521       NTL_tolower_tab[((char)j) - CHAR_MIN] = (char)i;
522       NTL_toupper_tab[((char)i) - CHAR_MIN] = (char)j;
523     };
524 }
525
526 /* These are used in main() to actually dump the tables, each function
527    dumps half table as hex/char constants... */
528
529 #define ROWSIZE 8
530
531 static void dumphb(char *tbl, int beg)
532 {
533   int i, j, k, z;
534   char *p = &tbl[beg - CHAR_MIN];
535   char c;
536   for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
537   {
538     k = i + ROWSIZE - 1;
539     if (k > SCHAR_MAX)
540       k = SCHAR_MAX;
541
542     c = (char)(beg + i);
543     printf("/*");
544     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
545         && (c != '\''))
546       printf(" '%c'", c);
547     else
548       printf(" x%02x", ((int)((unsigned char)c)));
549
550     c = (char)(beg + k);
551     printf("-");
552     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
553         && (c != '\''))
554       printf("'%c'", c);
555     else
556       printf("x%02x", ((int)((unsigned char)c)));
557     printf(" */");
558
559     for (j = i; j <= k; j++)
560     {
561       c = p[j];
562       if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
563           && (c != '\''))
564         printf("    '%c'", c);
565       else
566         printf(" '\\x%02x'", ((int)((unsigned char)c)));
567       if (j < SCHAR_MAX)
568         printf(",");
569     };
570     printf("\n");
571   };
572 }
573
574 static void dumphw(int *tbl, int beg)
575 {
576   int i, j, k, z;
577   int *p = &tbl[beg - CHAR_MIN];
578   char c;
579   for (i = 0; i <= SCHAR_MAX; i += ROWSIZE)
580   {
581     k = i + ROWSIZE - 1;
582     if (k > SCHAR_MAX)
583       k = SCHAR_MAX;
584
585     c = (char)(beg + i);
586     printf("/*");
587     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
588         && (c != '\''))
589       printf(" '%c'", c);
590     else
591       printf(" x%02x", ((int)((unsigned char)c)));
592
593     c = (char)(beg + k);
594     printf("-");
595     if ((c > 0) && (c < SCHAR_MAX) && (isprint(c)) && (c != '\\')
596         && (c != '\''))
597       printf("'%c'", c);
598     else
599       printf("x%02x", ((int)((unsigned char)c)));
600     printf(" */");
601
602     for (j = i; j <= k; j++)
603     {
604       printf(" 0x%04x", p[j] & 0xffffffff);
605       if (j < SCHAR_MAX)
606         printf(",");
607     };
608     printf("\n");
609   };
610 }
611
612 #endif /* MAKETABLES */