1 /*
2 * rfc1459.c
3 */
4 /*
5 * Copyright (C) 1990 Jarkko Oikarinen
6 * Copyright (C) 1999 - 2021 Eggheads Development Team
7 *
8 * This code was more or less cloned from the ircd-hybrid 5.3 source.
9 * The original code was written by Otto Harkoonen and even though it
10 * it not entirely in synch with section 2.2 of RFC1459 in that it
11 * additionally defines carat as an uppercase version of tilde, it's
12 * what is in the servers themselves so we're going with it this way.
13 *
14 * If for some reason someone who maintains the source for ircd decides
15 * to change the code to be completely RFC compliant, the change here
16 * would be absolutely minuscule.
17 *
18 * BTW, since carat characters are allowed in nicknames and tildes are
19 * not, I strongly suggest that people convert to uppercase when doing
20 * comparisons or creation of hash elements (which tcl laughably calls
21 * arrays) to avoid making entries with impossible nicknames in them.
22 *
23 * --+ Dagmar
24 */
25
26 #include "main.h"
27
_rfc_casecmp(const char * s1,const char * s2)28 int _rfc_casecmp(const char *s1, const char *s2)
29 {
30 unsigned char *str1 = (unsigned char *) s1;
31 unsigned char *str2 = (unsigned char *) s2;
32 int res;
33
34 while (!(res = rfc_toupper(*str1) - rfc_toupper(*str2))) {
35 if (*str1 == '\0')
36 return 0;
37 str1++;
38 str2++;
39 }
40 return res;
41 }
42
_rfc_ncasecmp(const char * str1,const char * str2,int n)43 int _rfc_ncasecmp(const char *str1, const char *str2, int n)
44 {
45 unsigned char *s1 = (unsigned char *) str1;
46 unsigned char *s2 = (unsigned char *) str2;
47 int res;
48
49 while (!(res = rfc_toupper(*s1) - rfc_toupper(*s2))) {
50 s1++;
51 s2++;
52 n--;
53 if (!n || (*s1 == '\0' && *s2 == '\0'))
54 return 0;
55 }
56 return res;
57 }
58
59 unsigned char rfc_tolowertab[] =
60 { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
61 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
62 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
63 0x1e, 0x1f,
64 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
65 '*', '+', ',', '-', '.', '/',
66 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
67 ':', ';', '<', '=', '>', '?',
68 '@', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
69 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
70 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
71 '_',
72 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
73 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
74 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
75 0x7f,
76 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
77 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
78 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
79 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
80 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
81 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
82 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
83 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
84 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
85 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
86 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
87 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
88 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
89 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
90 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
91 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
92 };
93
94 unsigned char rfc_touppertab[] =
95 { 0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa,
96 0xb, 0xc, 0xd, 0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14,
97 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
98 0x1e, 0x1f,
99 ' ', '!', '"', '#', '$', '%', '&', 0x27, '(', ')',
100 '*', '+', ',', '-', '.', '/',
101 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
102 ':', ';', '<', '=', '>', '?',
103 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
104 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
105 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
106 0x5f,
107 '`', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
108 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
109 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^',
110 0x7f,
111 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
112 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
113 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
114 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
115 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
116 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
117 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
118 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
119 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9,
120 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
121 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9,
122 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
123 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
124 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
125 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
126 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
127 };
128
_rfc_tolower(int c)129 int _rfc_tolower(int c)
130 {
131 return rfc_tolowertab[(unsigned char) (c)];
132 }
133
_rfc_toupper(int c)134 int _rfc_toupper(int c)
135 {
136 return rfc_touppertab[(unsigned char) (c)];
137 }
138