]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
Passing invalid instance to dns isnt a good idea
[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
62 size_t nspace::hash<insp_inaddr>::operator()(const insp_inaddr &a) const
63 {
64         size_t q;
65         memcpy(&q,&a,sizeof(size_t));
66         return q;
67 }
68
69 size_t nspace::hash<string>::operator()(const string &s) const
70 {
71         char a[MAXBUF];
72         static struct hash<const char *> strhash;
73         strlcpy(a,s.c_str(),MAXBUF);
74         strlower(a);
75         return strhash(a);
76 }
77
78 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
79 {
80         unsigned char* n1 = (unsigned char*)s1.c_str();
81         unsigned char* n2 = (unsigned char*)s2.c_str();
82         for (; *n1 && *n2; n1++, n2++)
83                 if (lowermap[*n1] != lowermap[*n2])
84                         return false;
85         return (lowermap[*n1] == lowermap[*n2]);
86 }
87
88 bool irc::InAddr_HashComp::operator()(const insp_inaddr &s1, const insp_inaddr &s2) const
89 {
90 #ifdef IPV6
91         for (int n = 0; n < 16; n++)
92                 if (s2.s6_addr[n] != s1.s6_addr[n])
93                         return false;
94         return true;
95 #else
96         return (s1.s_addr == s1.s_addr);
97 #endif
98 }
99
100 /******************************************************
101  *
102  * This is the implementation of our special irc::string
103  * class which is a case-insensitive equivalent to
104  * std::string which is not only case-insensitive but
105  * can also do scandanavian comparisons, e.g. { = [, etc.
106  *
107  * This class depends on the global 'lowermap' which is
108  * initialized at startup by inspircd.cpp, and contains
109  * the 'scandanavian' casemappings for fast irc compare.
110  *
111  ******************************************************/
112
113 bool irc::irc_char_traits::eq(char c1st, char c2nd)
114 {
115         return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
116 }
117
118 bool irc::irc_char_traits::ne(char c1st, char c2nd)
119 {
120         return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
121 }
122
123 bool irc::irc_char_traits::lt(char c1st, char c2nd)
124 {
125         return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
126 }
127
128 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
129 {
130         for(unsigned int i = 0; i < n; i++)
131         {
132                 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
133                         return 1;
134
135                 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
136                         return -1;
137
138                 if(*str1 == 0 || *str2 == 0)
139                         return 0;
140
141                 str1++;
142                 str2++;
143         }
144         return 0;
145 }
146
147 std::string operator+ (std::string& leftval, irc::string& rightval)
148 {
149         return leftval + std::string(rightval.c_str());
150 }
151
152 irc::string operator+ (irc::string& leftval, std::string& rightval)
153 {
154         return leftval + irc::string(rightval.c_str());
155 }
156
157 bool operator== (std::string& leftval, irc::string& rightval)
158 {
159         return (leftval == std::string(rightval.c_str()));
160 }
161
162 bool operator== (irc::string& leftval, std::string& rightval)
163 {
164         return (rightval == std::string(leftval.c_str()));
165 }
166
167 const char* irc::irc_char_traits::find(const char* s1, int  n, char c)
168 {
169         while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
170                 s1++;
171         return s1;
172 }
173
174 /* See hashcomp.h if you care about these... */
175 std::ostream& operator<<(std::ostream &os, const irc::string &str)
176 {
177         return os << str.c_str();
178 }
179
180 std::istream& operator>>(std::istream &is, irc::string &str)
181 {
182         std::string tmp;
183         is >> tmp;
184         str = tmp.c_str();
185         return is;
186 }
187
188 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
189 {
190         /* Remove trailing spaces, these muck up token parsing */
191         while (tokens.find_last_of(' ') == tokens.length() - 1)
192                 tokens.erase(tokens.end() - 1);
193
194         /* Record starting position and current position */
195         last_starting_position = tokens.begin();
196         n = tokens.begin();
197 }
198
199 irc::tokenstream::~tokenstream()
200 {
201 }
202
203 const std::string irc::tokenstream::GetToken()
204 {
205         std::string::iterator lsp = last_starting_position;
206
207         while (n != tokens.end())
208         {
209                 if ((last_pushed) && (*n == ':'))
210                 {
211                         /* If we find a token thats not the first and starts with :,
212                          * this is the last token on the line
213                          */
214                         std::string::iterator curr = ++n;
215                         n = tokens.end();
216                         return std::string(curr, tokens.end());
217                 }
218
219                 last_pushed = false;
220
221                 if ((*n == ' ') || (n+1 == tokens.end()))
222                 {
223                         /* If we find a space, or end of string, this is the end of a token.
224                          */
225                         last_starting_position = n+1;
226                         last_pushed = true;
227                         return std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
228                 }
229
230                 n++;
231         }
232         return "";
233 }
234
235 irc::commasepstream::commasepstream(const std::string &source) : tokens(source)
236 {
237         last_starting_position = tokens.begin();
238         n = tokens.begin();
239 }
240
241 const std::string irc::commasepstream::GetToken()
242 {
243         std::string::iterator lsp = last_starting_position;
244
245         while (n != tokens.end())
246         {
247                 if ((*n == ',') || (n+1 == tokens.end()))
248                 {
249                         last_starting_position = n+1;
250                         return std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
251                 }
252
253                 n++;
254         }
255
256         return "";
257 }
258
259 irc::commasepstream::~commasepstream()
260 {
261 }