1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
21 #include <ext/hash_map>
22 #define nspace __gnu_cxx
24 /******************************************************
26 * The hash functions of InspIRCd are the centrepoint
27 * of the entire system. If these functions are
28 * inefficient or wasteful, the whole program suffers
29 * as a result. A lot of C programmers in the ircd
30 * scene spend a lot of time debating (arguing) about
31 * the best way to write hash functions to hash irc
32 * nicknames, channels etc.
33 * We are lucky as C++ developers as hash_map does
34 * a lot of this for us. It does intellegent memory
35 * requests, bucketing, search functions, insertion
36 * and deletion etc. All we have to do is write some
37 * overloaded comparison and hash value operators which
38 * cause it to act in an irc-like way. The features we
39 * add to the standard hash_map are:
41 * Case insensitivity: The hash_map will be case
44 * Scandanavian Comparisons: The characters [, ], \ will
45 * be considered the lowercase of {, } and |.
47 * This file also contains hashing methods for hashing
48 * insp_inaddr structs, we use this if we want to cache IP
51 ******************************************************/
54 using namespace irc::sockets;
56 /* convert a string to lowercase. Note following special circumstances
57 * taken from RFC 1459. Many "official" server branches still hold to this
60 * Because of IRC's scandanavian origin, the characters {}| are
61 * considered to be the lower case equivalents of the characters []\,
62 * respectively. This is a critical issue when determining the
63 * equivalence of two nicknames.
65 void nspace::strlower(char *n)
69 for (char* t = n; *t; t++)
70 *t = lowermap[(unsigned char)*t];
74 size_t nspace::hash<insp_inaddr>::operator()(const insp_inaddr &a) const
77 memcpy(&q,&a,sizeof(size_t));
81 size_t nspace::hash<string>::operator()(const string &s) const
83 /* XXX: NO DATA COPIES! :)
84 * The hash function here is practically
85 * a copy of the one in STL's hash_fun.h,
86 * only with *x replaced with lowermap[*x].
87 * This avoids a copy to use hash<const char*>
89 register size_t t = 0;
90 for (std::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
91 t = 5 * t + lowermap[(unsigned char)*x];
95 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
97 unsigned char* n1 = (unsigned char*)s1.c_str();
98 unsigned char* n2 = (unsigned char*)s2.c_str();
99 for (; *n1 && *n2; n1++, n2++)
100 if (lowermap[*n1] != lowermap[*n2])
102 return (lowermap[*n1] == lowermap[*n2]);
105 bool irc::InAddr_HashComp::operator()(const insp_inaddr &s1, const insp_inaddr &s2) const
108 for (int n = 0; n < 16; n++)
109 if (s2.s6_addr[n] != s1.s6_addr[n])
113 return (s1.s_addr == s1.s_addr);
117 /******************************************************
119 * This is the implementation of our special irc::string
120 * class which is a case-insensitive equivalent to
121 * std::string which is not only case-insensitive but
122 * can also do scandanavian comparisons, e.g. { = [, etc.
124 * This class depends on the const array 'lowermap'.
126 ******************************************************/
128 bool irc::irc_char_traits::eq(char c1st, char c2nd)
130 return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
133 bool irc::irc_char_traits::ne(char c1st, char c2nd)
135 return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
138 bool irc::irc_char_traits::lt(char c1st, char c2nd)
140 return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
143 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
145 for(unsigned int i = 0; i < n; i++)
147 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
150 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
153 if(*str1 == 0 || *str2 == 0)
162 std::string operator+ (std::string& leftval, irc::string& rightval)
164 return leftval + std::string(rightval.c_str());
167 irc::string operator+ (irc::string& leftval, std::string& rightval)
169 return leftval + irc::string(rightval.c_str());
172 bool operator== (std::string& leftval, irc::string& rightval)
174 return (leftval == std::string(rightval.c_str()));
177 bool operator== (irc::string& leftval, std::string& rightval)
179 return (rightval == std::string(leftval.c_str()));
182 const char* irc::irc_char_traits::find(const char* s1, int n, char c)
184 while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
189 /* See hashcomp.h if you care about these... */
190 std::ostream& operator<<(std::ostream &os, const irc::string &str)
192 return os << str.c_str();
195 std::istream& operator>>(std::istream &is, irc::string &str)
203 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
205 /* Record starting position and current position */
206 last_starting_position = tokens.begin();
210 irc::tokenstream::~tokenstream()
214 const std::string irc::tokenstream::GetToken()
216 std::string::iterator lsp = last_starting_position;
218 while (n != tokens.end())
220 /** Skip multi space, converting " " into " "
222 while ((n+1 != tokens.end()) && (*n == ' ') && (*(n+1) == ' '))
225 if ((last_pushed) && (*n == ':'))
227 /* If we find a token thats not the first and starts with :,
228 * this is the last token on the line
230 std::string::iterator curr = ++n;
232 return std::string(curr, tokens.end());
237 if ((*n == ' ') || (n+1 == tokens.end()))
239 /* If we find a space, or end of string, this is the end of a token.
241 last_starting_position = n+1;
244 std::string strip(lsp, n+1 == tokens.end() ? n+1 : n++);
245 while ((strip.length()) && (strip.find_last_of(' ') == strip.length() - 1))
246 strip.erase(strip.end() - 1);
256 irc::sepstream::sepstream(const std::string &source, char seperator) : tokens(source), sep(seperator)
258 last_starting_position = tokens.begin();
262 const std::string irc::sepstream::GetToken()
264 std::string::iterator lsp = last_starting_position;
266 while (n != tokens.end())
268 if ((*n == sep) || (n+1 == tokens.end()))
270 last_starting_position = n+1;
271 std::string strip = std::string(lsp, n+1 == tokens.end() ? n+1 : n++);
273 while ((strip.length()) && (strip.find_last_of(sep) == strip.length() - 1))
274 strip.erase(strip.end() - 1);
285 irc::sepstream::~sepstream()
289 std::string irc::hex(const unsigned char *raw, size_t rawsz)
297 for (i = 0; i < rawsz; i++)
299 sprintf (&(buf[i*2]), "%02x", raw[i]);
306 const char* irc::Spacify(char* n)
308 static char x[MAXBUF];
310 for (char* y = x; *y; y++)
317 irc::modestacker::modestacker(bool add) : adding(add)
320 sequence.push_back("");
323 void irc::modestacker::Push(char modeletter, const std::string ¶meter)
325 *(sequence.begin()) += modeletter;
326 sequence.push_back(parameter);
329 void irc::modestacker::Push(char modeletter)
331 this->Push(modeletter,"");
334 void irc::modestacker::PushPlus()
339 void irc::modestacker::PushMinus()
344 int irc::modestacker::GetStackedLine(std::deque<std::string> &result)
348 result.push_back(adding ? "+" : "-");
350 while (!sequence[0].empty() && (sequence.size() > 1) && (result.size() < MAXMODES+1))
352 result[0] += *(sequence[0].begin());
353 if (!sequence[1].empty())
354 result.push_back(sequence[1]);
355 sequence[0].erase(sequence[0].begin());
356 sequence.erase(sequence.begin() + 1);
363 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end)
365 for (int v = begin; v < end; v++)
366 joined.append(sequence[v]).append(seperator);
367 joined.append(sequence[end]);
370 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end)
372 for (int v = begin; v < end; v++)
373 joined.append(sequence[v]).append(seperator);
374 joined.append(sequence[end]);
377 irc::stringjoiner::stringjoiner(const std::string &seperator, const char** sequence, int begin, int end)
379 for (int v = begin; v < end; v++)
380 joined.append(sequence[v]).append(seperator);
381 joined.append(sequence[end]);
384 std::string& irc::stringjoiner::GetJoined()
389 irc::portparser::portparser(const std::string &source, bool allow_overlapped) : in_range(0), range_begin(0), range_end(0), overlapped(allow_overlapped)
391 sep = new irc::commasepstream(source);
395 irc::portparser::~portparser()
400 bool irc::portparser::Overlaps(long val)
405 if (overlap_set.find(val) == overlap_set.end())
407 overlap_set[val] = true;
414 long irc::portparser::GetToken()
419 if (in_range <= range_end)
421 if (!Overlaps(in_range))
427 while (((Overlaps(in_range)) && (in_range <= range_end)))
430 if (in_range <= range_end)
438 std::string x = sep->GetToken();
443 while (Overlaps(atoi(x.c_str())))
451 std::string::size_type dash = x.rfind('-');
452 if (dash != std::string::npos)
454 std::string sbegin = x.substr(0, dash);
455 std::string send = x.substr(dash+1, x.length());
456 range_begin = atoi(sbegin.c_str());
457 range_end = atoi(send.c_str());
459 if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
461 in_range = range_begin;
466 /* Assume its just the one port */
467 return atoi(sbegin.c_str());
472 return atoi(x.c_str());