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