]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
Move whowas into InspIRCd
[user/henk/code/inspircd.git] / src / hashcomp.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *              <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include <string>
22 #include "hashcomp.h"
23 #include "helperfuncs.h"
24 #include <ext/hash_map>
25
26 #define nspace __gnu_cxx
27
28 char lowermap[255];
29
30 /******************************************************
31  *
32  * The hash functions of InspIRCd are the centrepoint
33  * of the entire system. If these functions are
34  * inefficient or wasteful, the whole program suffers
35  * as a result. A lot of C programmers in the ircd
36  * scene spend a lot of time debating (arguing) about
37  * the best way to write hash functions to hash irc
38  * nicknames, channels etc.
39  * We are lucky as C++ developers as hash_map does
40  * a lot of this for us. It does intellegent memory
41  * requests, bucketing, search functions, insertion
42  * and deletion etc. All we have to do is write some
43  * overloaded comparison and hash value operators which
44  * cause it to act in an irc-like way. The features we
45  * add to the standard hash_map are:
46  *
47  * Case insensitivity: The hash_map will be case
48  * insensitive.
49  *
50  * Scandanavian Comparisons: The characters [, ], \ will
51  * be considered the lowercase of {, } and |.
52  *
53  * This file also contains hashing methods for hashing
54  * insp_inaddr structs, we use this if we want to cache IP
55  * addresses.
56  *
57  ******************************************************/
58
59 using namespace std;
60 using namespace irc::sockets;
61
62 /* convert a string to lowercase. Note following special circumstances
63  * taken from RFC 1459. Many "official" server branches still hold to this
64  * rule so i will too;
65  *
66  *  Because of IRC's scandanavian origin, the characters {}| are
67  *  considered to be the lower case equivalents of the characters []\,
68  *  respectively. This is a critical issue when determining the
69  *  equivalence of two nicknames.
70  */
71 void nspace::strlower(char *n)
72 {
73         if (n)
74         {
75                 for (char* t = n; *t; t++)
76                         *t = lowermap[(unsigned char)*t];
77         }
78 }
79
80 size_t nspace::hash<insp_inaddr>::operator()(const insp_inaddr &a) const
81 {
82         size_t q;
83         memcpy(&q,&a,sizeof(size_t));
84         return q;
85 }
86
87 size_t nspace::hash<string>::operator()(const string &s) const
88 {
89         char a[s.length()];
90         size_t t = 0;
91         static struct hash<const char *> strhash;
92
93         for (const char* x = s.c_str(); *x; x++)        /* Faster to do it this way than */
94                 a[t++] = lowermap[(unsigned char)*x];   /* Seperate strlcpy and strlower */
95
96         a[t] = 0;
97
98         return strhash(a);
99 }
100
101 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
102 {
103         unsigned char* n1 = (unsigned char*)s1.c_str();
104         unsigned char* n2 = (unsigned char*)s2.c_str();
105         for (; *n1 && *n2; n1++, n2++)
106                 if (lowermap[*n1] != lowermap[*n2])
107                         return false;
108         return (lowermap[*n1] == lowermap[*n2]);
109 }
110
111 bool irc::InAddr_HashComp::operator()(const insp_inaddr &s1, const insp_inaddr &s2) const
112 {
113 #ifdef IPV6
114         for (int n = 0; n < 16; n++)
115                 if (s2.s6_addr[n] != s1.s6_addr[n])
116                         return false;
117         return true;
118 #else
119         return (s1.s_addr == s1.s_addr);
120 #endif
121 }
122
123 /******************************************************
124  *
125  * This is the implementation of our special irc::string
126  * class which is a case-insensitive equivalent to
127  * std::string which is not only case-insensitive but
128  * can also do scandanavian comparisons, e.g. { = [, etc.
129  *
130  * This class depends on the global 'lowermap' which is
131  * initialized at startup by inspircd.cpp, and contains
132  * the 'scandanavian' casemappings for fast irc compare.
133  *
134  ******************************************************/
135
136 bool irc::irc_char_traits::eq(char c1st, char c2nd)
137 {
138         return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
139 }
140
141 bool irc::irc_char_traits::ne(char c1st, char c2nd)
142 {
143         return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
144 }
145
146 bool irc::irc_char_traits::lt(char c1st, char c2nd)
147 {
148         return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
149 }
150
151 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
152 {
153         for(unsigned int i = 0; i < n; i++)
154         {
155                 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
156                         return 1;
157
158                 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
159                         return -1;
160
161                 if(*str1 == 0 || *str2 == 0)
162                         return 0;
163
164                 str1++;
165                 str2++;
166         }
167         return 0;
168 }
169
170 std::string operator+ (std::string& leftval, irc::string& rightval)
171 {
172         return leftval + std::string(rightval.c_str());
173 }
174
175 irc::string operator+ (irc::string& leftval, std::string& rightval)
176 {
177         return leftval + irc::string(rightval.c_str());
178 }
179
180 bool operator== (std::string& leftval, irc::string& rightval)
181 {
182         return (leftval == std::string(rightval.c_str()));
183 }
184
185 bool operator== (irc::string& leftval, std::string& rightval)
186 {
187         return (rightval == std::string(leftval.c_str()));
188 }
189
190 const char* irc::irc_char_traits::find(const char* s1, int  n, char c)
191 {
192         while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
193                 s1++;
194         return s1;
195 }
196
197 /* See hashcomp.h if you care about these... */
198 std::ostream& operator<<(std::ostream &os, const irc::string &str)
199 {
200         return os << str.c_str();
201 }
202
203 std::istream& operator>>(std::istream &is, irc::string &str)
204 {
205         std::string tmp;
206         is >> tmp;
207         str = tmp.c_str();
208         return is;
209 }
210
211 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
212 {
213         /* Remove trailing spaces, these muck up token parsing */
214         while (tokens.find_last_of(' ') == tokens.length() - 1)
215                 tokens.erase(tokens.end() - 1);
216
217         /* Record starting position and current position */
218         last_starting_position = tokens.begin();
219         n = tokens.begin();
220 }
221
222 irc::tokenstream::~tokenstream()
223 {
224 }
225
226 const std::string irc::tokenstream::GetToken()
227 {
228         std::string::iterator lsp = last_starting_position;
229
230         while (n != tokens.end())
231         {
232                 if ((last_pushed) && (*n == ':'))
233                 {
234                         /* If we find a token thats not the first and starts with :,
235                          * this is the last token on the line
236                          */
237                         std::string::iterator curr = ++n;
238                         n = tokens.end();
239                         return std::string(curr, tokens.end());
240                 }
241
242                 last_pushed = false;
243
244                 if ((*n == ' ') || (n+1 == tokens.end()))
245                 {
246                         /* If we find a space, or end of string, this is the end of a token.
247                          */
248                         last_starting_position = n+1;
249                         last_pushed = true;
250                         return std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
251                 }
252
253                 n++;
254         }
255         return "";
256 }
257
258 irc::commasepstream::commasepstream(const std::string &source) : tokens(source)
259 {
260         last_starting_position = tokens.begin();
261         n = tokens.begin();
262 }
263
264 const std::string irc::commasepstream::GetToken()
265 {
266         std::string::iterator lsp = last_starting_position;
267
268         while (n != tokens.end())
269         {
270                 if ((*n == ',') || (n+1 == tokens.end()))
271                 {
272                         last_starting_position = n+1;
273                         return std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
274                 }
275
276                 n++;
277         }
278
279         return "";
280 }
281
282 irc::commasepstream::~commasepstream()
283 {
284 }
285
286 void InspIRCd::MakeLowerMap()
287 {       
288         // initialize the lowercase mapping table
289         for (unsigned char cn = 0; cn < 255; cn++)
290                 lowermap[cn] = cn;
291         // lowercase the uppercase chars
292         for (unsigned char cn = 65; cn < 91; cn++)
293                 lowermap[cn] = tolower(cn);
294         // now replace the specific chars for scandanavian comparison
295         lowermap[(unsigned char)'['] = '{';
296         lowermap[(unsigned char)']'] = '}';
297         lowermap[(unsigned char)'\\'] = '|';
298 }
299