ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/ermyth/src/match.C
(Generate patch)

Comparing ermyth/src/match.C (file contents):
Revision 1.5 by pippijn, Sun Sep 16 18:54:45 2007 UTC vs.
Revision 1.6 by pippijn, Sat Sep 22 14:27:30 2007 UTC

8 * Portions of this file were derived from sources bearing the following license: 8 * Portions of this file were derived from sources bearing the following license:
9 * Rights to this code are documented in doc/pod/license.pod. 9 * Rights to this code are documented in doc/pod/license.pod.
10 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org) 10 * Copyright © 2005-2007 Atheme Project (http://www.atheme.org)
11 */ 11 */
12 12
13static char const rcsid[] = "$Id: match.C,v 1.5 2007/09/16 18:54:45 pippijn Exp $"; 13static char const rcsid[] = "$Id: match.C,v 1.6 2007/09/22 14:27:30 pippijn Exp $";
14 14
15#include "atheme.h" 15#include "atheme.h"
16 16
17#define BadPtr(x) (!(x) || (*(x) == '\0')) 17#define BadPtr(x) (!(x) || (*(x) == '\0'))
18
19int match_mapping = MATCH_RFC1459;
20
21const unsigned char ToLowerTab[] = {
22 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
23 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
24 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
25 0x1e, 0x1f,
26 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
27 '*', '+', ',', '-', '.', '/',
28 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
29 ':', ';', '<', '=', '>', '?',
30 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
31 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
32 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
33 '_',
34 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
35 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
36 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
37 0x7f,
38 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
39 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
40 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
41 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
42 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
43 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
44 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
45 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
46 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
47 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
48 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
49 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
50 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
51 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
52 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
53 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
54};
55
56const unsigned char ToUpperTab[] = {
57 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
58 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
59 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
60 0x1e, 0x1f,
61 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
62 '*', '+', ',', '-', '.', '/',
63 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
64 ':', ';', '<', '=', '>', '?',
65 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
66 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
67 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
68 0x5f,
69 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
70 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
71 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
72 0x7f,
73 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
74 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
75 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
76 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
77 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
78 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
79 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
80 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
81 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
82 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
83 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
84 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
85 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
86 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
87 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
88 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
89};
90
91int
92ToLower (int c)
93{
94 if (match_mapping == MATCH_ASCII)
95 return tolower (c);
96 /* else */
97 return (ToLowerTab[(unsigned char) (c)]);
98}
99
100int
101ToUpper (int c)
102{
103 if (match_mapping == MATCH_ASCII)
104 return toupper (c);
105 /* else */
106 return (ToUpperTab[(unsigned char) (c)]);
107}
108
109void
110set_match_mapping (int type)
111{
112 match_mapping = type;
113}
114 18
115#define MAX_ITERATIONS 512 19#define MAX_ITERATIONS 512
116/* 20/*
117** Compare if a given string (name) matches the given 21** Compare if a given string (name) matches the given
118** mask (which can contain wild cards: '*' - match any 22** mask (which can contain wild cards: '*' - match any
236 } 140 }
237 return pattern; 141 return pattern;
238} 142}
239 143
240/* 144/*
241** Case insensitive comparison of two null terminated strings.
242**
243** returns 0, if s1 equal to s2
244** <0, if s1 lexicographically less than s2
245** >0, if s1 lexicographically greater than s2
246*/
247int
248irccasecmp (char const * const s1, char const * const s2)
249{
250 const unsigned char *str1 = (const unsigned char *) s1;
251 const unsigned char *str2 = (const unsigned char *) s2;
252 int res;
253
254 if (!s1 || !s2)
255 return -1;
256
257 if (match_mapping == MATCH_ASCII)
258 return strcasecmp (s1, s2);
259
260 while ((res = ToUpper (*str1) - ToUpper (*str2)) == 0)
261 {
262 if (*str1 == '\0')
263 return 0;
264 str1++;
265 str2++;
266 }
267 return (res);
268}
269
270int
271ircncasecmp (char const * const str1, char const * const str2, int n)
272{
273 const unsigned char *s1 = (const unsigned char *) str1;
274 const unsigned char *s2 = (const unsigned char *) str2;
275 int res;
276
277 if (match_mapping == MATCH_ASCII)
278 return strncasecmp (str1, str2, n);
279
280 while ((res = ToUpper (*s1) - ToUpper (*s2)) == 0)
281 {
282 s1++;
283 s2++;
284 n--;
285 if (n == 0 || (*s1 == '\0' && *s2 == '\0'))
286 return 0;
287 }
288 return (res);
289}
290
291const unsigned int charattrs[] = {
292 /* 0 */ 0,
293 /* 1 */ 0,
294 /* 2 */ 0,
295 /* 3 */ 0,
296 /* 4 */ 0,
297 /* 5 */ 0,
298 /* 6 */ 0,
299 /* 7 BEL */ 0,
300 /* 8 \b */ 0,
301 /* 9 \t */ 0,
302 /* 10 \n */ 0,
303 /* 11 \v */ 0,
304 /* 12 \f */ 0,
305 /* 13 \r */ 0,
306 /* 14 */ 0,
307 /* 15 */ 0,
308 /* 16 */ 0,
309 /* 17 */ 0,
310 /* 18 */ 0,
311 /* 19 */ 0,
312 /* 20 */ 0,
313 /* 21 */ 0,
314 /* 22 */ 0,
315 /* 23 */ 0,
316 /* 24 */ 0,
317 /* 25 */ 0,
318 /* 26 */ 0,
319 /* 27 */ 0,
320 /* 28 */ 0,
321 /* 29 */ 0,
322 /* 30 */ 0,
323 /* 31 */ 0,
324 /* SP */ 0,
325 /* ! */ 0,
326 /* " */ 0,
327 /* # */ 0,
328 /* $ */ 0,
329 /* % */ 0,
330 /* & */ 0,
331 /* ' */ 0,
332 /* ( */ 0,
333 /* ) */ 0,
334 /* * */ 0,
335 /* + */ 0,
336 /* , */ 0,
337 /* - */ 0,
338 /* . */ 0,
339 /* / */ 0,
340 /* 0 */ C_DIGIT,
341 /* 1 */ C_DIGIT,
342 /* 2 */ C_DIGIT,
343 /* 3 */ C_DIGIT,
344 /* 4 */ C_DIGIT,
345 /* 5 */ C_DIGIT,
346 /* 6 */ C_DIGIT,
347 /* 7 */ C_DIGIT,
348 /* 8 */ C_DIGIT,
349 /* 9 */ C_DIGIT,
350 /* : */ 0,
351 /* ; */ 0,
352 /* < */ 0,
353 /* = */ 0,
354 /* > */ 0,
355 /* ? */ 0,
356 /* @ */ 0,
357 /* A */ C_ALPHA,
358 /* B */ C_ALPHA,
359 /* C */ C_ALPHA,
360 /* D */ C_ALPHA,
361 /* E */ C_ALPHA,
362 /* F */ C_ALPHA,
363 /* G */ C_ALPHA,
364 /* H */ C_ALPHA,
365 /* I */ C_ALPHA,
366 /* J */ C_ALPHA,
367 /* K */ C_ALPHA,
368 /* L */ C_ALPHA,
369 /* M */ C_ALPHA,
370 /* N */ C_ALPHA,
371 /* O */ C_ALPHA,
372 /* P */ C_ALPHA,
373 /* Q */ C_ALPHA,
374 /* R */ C_ALPHA,
375 /* S */ C_ALPHA,
376 /* T */ C_ALPHA,
377 /* U */ C_ALPHA,
378 /* V */ C_ALPHA,
379 /* W */ C_ALPHA,
380 /* X */ C_ALPHA,
381 /* Y */ C_ALPHA,
382 /* Z */ C_ALPHA,
383 /* [ */ 0,
384 /* \ */ 0,
385 /* ] */ 0,
386 /* ^ */ 0,
387 /* _ */ 0,
388 /* ` */ 0,
389 /* a */ C_ALPHA,
390 /* b */ C_ALPHA,
391 /* c */ C_ALPHA,
392 /* d */ C_ALPHA,
393 /* e */ C_ALPHA,
394 /* f */ C_ALPHA,
395 /* g */ C_ALPHA,
396 /* h */ C_ALPHA,
397 /* i */ C_ALPHA,
398 /* j */ C_ALPHA,
399 /* k */ C_ALPHA,
400 /* l */ C_ALPHA,
401 /* m */ C_ALPHA,
402 /* n */ C_ALPHA,
403 /* o */ C_ALPHA,
404 /* p */ C_ALPHA,
405 /* q */ C_ALPHA,
406 /* r */ C_ALPHA,
407 /* s */ C_ALPHA,
408 /* t */ C_ALPHA,
409 /* u */ C_ALPHA,
410 /* v */ C_ALPHA,
411 /* w */ C_ALPHA,
412 /* x */ C_ALPHA,
413 /* y */ C_ALPHA,
414 /* z */ C_ALPHA,
415 /* { */ 0,
416 /* | */ 0,
417 /* } */ 0,
418 /* ~ */ 0,
419 /* del */ 0,
420 /* 0x80 */ 0,
421 /* 0x81 */ 0,
422 /* 0x82 */ 0,
423 /* 0x83 */ 0,
424 /* 0x84 */ 0,
425 /* 0x85 */ 0,
426 /* 0x86 */ 0,
427 /* 0x87 */ 0,
428 /* 0x88 */ 0,
429 /* 0x89 */ 0,
430 /* 0x8A */ 0,
431 /* 0x8B */ 0,
432 /* 0x8C */ 0,
433 /* 0x8D */ 0,
434 /* 0x8E */ 0,
435 /* 0x8F */ 0,
436 /* 0x90 */ 0,
437 /* 0x91 */ 0,
438 /* 0x92 */ 0,
439 /* 0x93 */ 0,
440 /* 0x94 */ 0,
441 /* 0x95 */ 0,
442 /* 0x96 */ 0,
443 /* 0x97 */ 0,
444 /* 0x98 */ 0,
445 /* 0x99 */ 0,
446 /* 0x9A */ 0,
447 /* 0x9B */ 0,
448 /* 0x9C */ 0,
449 /* 0x9D */ 0,
450 /* 0x9E */ 0,
451 /* 0x9F */ 0,
452 /* 0xA0 */ 0,
453 /* 0xA1 */ 0,
454 /* 0xA2 */ 0,
455 /* 0xA3 */ 0,
456 /* 0xA4 */ 0,
457 /* 0xA5 */ 0,
458 /* 0xA6 */ 0,
459 /* 0xA7 */ 0,
460 /* 0xA8 */ 0,
461 /* 0xA9 */ 0,
462 /* 0xAA */ 0,
463 /* 0xAB */ 0,
464 /* 0xAC */ 0,
465 /* 0xAD */ 0,
466 /* 0xAE */ 0,
467 /* 0xAF */ 0,
468 /* 0xB0 */ 0,
469 /* 0xB1 */ 0,
470 /* 0xB2 */ 0,
471 /* 0xB3 */ 0,
472 /* 0xB4 */ 0,
473 /* 0xB5 */ 0,
474 /* 0xB6 */ 0,
475 /* 0xB7 */ 0,
476 /* 0xB8 */ 0,
477 /* 0xB9 */ 0,
478 /* 0xBA */ 0,
479 /* 0xBB */ 0,
480 /* 0xBC */ 0,
481 /* 0xBD */ 0,
482 /* 0xBE */ 0,
483 /* 0xBF */ 0,
484 /* 0xC0 */ 0,
485 /* 0xC1 */ 0,
486 /* 0xC2 */ 0,
487 /* 0xC3 */ 0,
488 /* 0xC4 */ 0,
489 /* 0xC5 */ 0,
490 /* 0xC6 */ 0,
491 /* 0xC7 */ 0,
492 /* 0xC8 */ 0,
493 /* 0xC9 */ 0,
494 /* 0xCA */ 0,
495 /* 0xCB */ 0,
496 /* 0xCC */ 0,
497 /* 0xCD */ 0,
498 /* 0xCE */ 0,
499 /* 0xCF */ 0,
500 /* 0xD0 */ 0,
501 /* 0xD1 */ 0,
502 /* 0xD2 */ 0,
503 /* 0xD3 */ 0,
504 /* 0xD4 */ 0,
505 /* 0xD5 */ 0,
506 /* 0xD6 */ 0,
507 /* 0xD7 */ 0,
508 /* 0xD8 */ 0,
509 /* 0xD9 */ 0,
510 /* 0xDA */ 0,
511 /* 0xDB */ 0,
512 /* 0xDC */ 0,
513 /* 0xDD */ 0,
514 /* 0xDE */ 0,
515 /* 0xDF */ 0,
516 /* 0xE0 */ 0,
517 /* 0xE1 */ 0,
518 /* 0xE2 */ 0,
519 /* 0xE3 */ 0,
520 /* 0xE4 */ 0,
521 /* 0xE5 */ 0,
522 /* 0xE6 */ 0,
523 /* 0xE7 */ 0,
524 /* 0xE8 */ 0,
525 /* 0xE9 */ 0,
526 /* 0xEA */ 0,
527 /* 0xEB */ 0,
528 /* 0xEC */ 0,
529 /* 0xED */ 0,
530 /* 0xEE */ 0,
531 /* 0xEF */ 0,
532 /* 0xF0 */ 0,
533 /* 0xF1 */ 0,
534 /* 0xF2 */ 0,
535 /* 0xF3 */ 0,
536 /* 0xF4 */ 0,
537 /* 0xF5 */ 0,
538 /* 0xF6 */ 0,
539 /* 0xF7 */ 0,
540 /* 0xF8 */ 0,
541 /* 0xF9 */ 0,
542 /* 0xFA */ 0,
543 /* 0xFB */ 0,
544 /* 0xFC */ 0,
545 /* 0xFD */ 0,
546 /* 0xFE */ 0,
547 /* 0xFF */ 0,
548};
549
550/*
551 * regex_compile() 145 * regex_compile()
552 * Compile a regex of `pattern' and return it. 146 * Compile a regex of `pattern' and return it.
553 */ 147 */
554regex_t * 148regex_t *
555regex_create (char *pattern, int flags) 149regex_create (char *pattern, int flags)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines