1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include <ext/hash_map>
17 #define nspace __gnu_cxx
19 /******************************************************
21 * The hash functions of InspIRCd are the centrepoint
22 * of the entire system. If these functions are
23 * inefficient or wasteful, the whole program suffers
24 * as a result. A lot of C programmers in the ircd
25 * scene spend a lot of time debating (arguing) about
26 * the best way to write hash functions to hash irc
27 * nicknames, channels etc.
28 * We are lucky as C++ developers as hash_map does
29 * a lot of this for us. It does intellegent memory
30 * requests, bucketing, search functions, insertion
31 * and deletion etc. All we have to do is write some
32 * overloaded comparison and hash value operators which
33 * cause it to act in an irc-like way. The features we
34 * add to the standard hash_map are:
36 * Case insensitivity: The hash_map will be case
39 * Scandanavian Comparisons: The characters [, ], \ will
40 * be considered the lowercase of {, } and |.
42 ******************************************************/
44 using namespace irc::sockets;
46 /* convert a string to lowercase. Note following special circumstances
47 * taken from RFC 1459. Many "official" server branches still hold to this
50 * Because of IRC's scandanavian origin, the characters {}| are
51 * considered to be the lower case equivalents of the characters []\,
52 * respectively. This is a critical issue when determining the
53 * equivalence of two nicknames.
55 void nspace::strlower(char *n)
59 for (char* t = n; *t; t++)
60 *t = lowermap[(unsigned char)*t];
64 size_t nspace::hash<string>::operator()(const string &s) const
66 /* XXX: NO DATA COPIES! :)
67 * The hash function here is practically
68 * a copy of the one in STL's hash_fun.h,
69 * only with *x replaced with lowermap[*x].
70 * This avoids a copy to use hash<const char*>
72 register size_t t = 0;
73 for (std::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
74 t = 5 * t + lowermap[(unsigned char)*x];
78 size_t nspace::hash<irc::string>::operator()(const irc::string &s) const
80 register size_t t = 0;
81 for (irc::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
82 t = 5 * t + lowermap[(unsigned char)*x];
86 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
88 unsigned char* n1 = (unsigned char*)s1.c_str();
89 unsigned char* n2 = (unsigned char*)s2.c_str();
90 for (; *n1 && *n2; n1++, n2++)
91 if (lowermap[*n1] != lowermap[*n2])
93 return (lowermap[*n1] == lowermap[*n2]);
96 /******************************************************
98 * This is the implementation of our special irc::string
99 * class which is a case-insensitive equivalent to
100 * std::string which is not only case-insensitive but
101 * can also do scandanavian comparisons, e.g. { = [, etc.
103 * This class depends on the const array 'lowermap'.
105 ******************************************************/
107 bool irc::irc_char_traits::eq(char c1st, char c2nd)
109 return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
112 bool irc::irc_char_traits::ne(char c1st, char c2nd)
114 return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
117 bool irc::irc_char_traits::lt(char c1st, char c2nd)
119 return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
122 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
124 for(unsigned int i = 0; i < n; i++)
126 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
129 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
132 if(*str1 == 0 || *str2 == 0)
141 std::string operator+ (std::string& leftval, irc::string& rightval)
143 return leftval + std::string(rightval.c_str());
146 irc::string operator+ (irc::string& leftval, std::string& rightval)
148 return leftval + irc::string(rightval.c_str());
151 bool operator== (std::string& leftval, irc::string& rightval)
153 return (leftval.c_str() == rightval);
156 bool operator== (irc::string& leftval, std::string& rightval)
158 return (leftval == rightval.c_str());
161 const char* irc::irc_char_traits::find(const char* s1, int n, char c)
163 while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
168 /* See hashcomp.h if you care about these... */
169 std::ostream& operator<<(std::ostream &os, const irc::string &str)
171 return os << str.c_str();
174 std::istream& operator>>(std::istream &is, irc::string &str)
182 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
184 /* Record starting position and current position */
185 last_starting_position = tokens.begin();
189 irc::tokenstream::~tokenstream()
193 const std::string irc::tokenstream::GetToken()
195 std::string::iterator lsp = last_starting_position;
197 while (n != tokens.end())
199 /** Skip multi space, converting " " into " "
201 while ((n+1 != tokens.end()) && (*n == ' ') && (*(n+1) == ' '))
204 if ((last_pushed) && (*n == ':'))
206 /* If we find a token thats not the first and starts with :,
207 * this is the last token on the line
209 std::string::iterator curr = ++n;
211 return std::string(curr, tokens.end());
216 if ((*n == ' ') || (n+1 == tokens.end()))
218 /* If we find a space, or end of string, this is the end of a token.
220 last_starting_position = n+1;
223 std::string strip(lsp, n+1 == tokens.end() ? n+1 : n++);
224 while ((strip.length()) && (strip.find_last_of(' ') == strip.length() - 1))
225 strip.erase(strip.end() - 1);
235 irc::sepstream::sepstream(const std::string &source, char seperator) : tokens(source), sep(seperator)
237 last_starting_position = tokens.begin();
241 const std::string irc::sepstream::GetToken()
243 std::string::iterator lsp = last_starting_position;
245 while (n != tokens.end())
247 if ((*n == sep) || (n+1 == tokens.end()))
249 last_starting_position = n+1;
250 std::string strip = std::string(lsp, n+1 == tokens.end() ? n+1 : n++);
252 while ((strip.length()) && (strip.find_last_of(sep) == strip.length() - 1))
253 strip.erase(strip.end() - 1);
264 const std::string irc::sepstream::GetRemaining()
266 return std::string(n, tokens.end());
269 bool irc::sepstream::StreamEnd()
271 return ((n + 1) == tokens.end());
274 irc::sepstream::~sepstream()
278 std::string irc::hex(const unsigned char *raw, size_t rawsz)
283 /* EWW! This used to be using sprintf, which is WAY inefficient. -Special */
285 const char *hex = "0123456789abcdef";
290 for (i = 0, j = 0; j < rawsz; ++j)
292 buf[i++] = hex[raw[j] / 16];
293 buf[i++] = hex[raw[j] % 16];
300 const char* irc::Spacify(const char* n)
302 static char x[MAXBUF];
304 for (char* y = x; *y; y++)
311 irc::modestacker::modestacker(bool add) : adding(add)
314 sequence.push_back("");
317 void irc::modestacker::Push(char modeletter, const std::string ¶meter)
319 *(sequence.begin()) += modeletter;
320 sequence.push_back(parameter);
323 void irc::modestacker::Push(char modeletter)
325 this->Push(modeletter,"");
328 void irc::modestacker::PushPlus()
333 void irc::modestacker::PushMinus()
338 int irc::modestacker::GetStackedLine(std::deque<std::string> &result)
342 result.push_back(adding ? "+" : "-");
344 while (!sequence[0].empty() && (sequence.size() > 1) && (result.size() < MAXMODES+1))
346 result[0] += *(sequence[0].begin());
347 if (!sequence[1].empty())
348 result.push_back(sequence[1]);
349 sequence[0].erase(sequence[0].begin());
350 sequence.erase(sequence.begin() + 1);
357 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end)
359 for (int v = begin; v < end; v++)
360 joined.append(sequence[v]).append(seperator);
361 joined.append(sequence[end]);
364 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end)
366 for (int v = begin; v < end; v++)
367 joined.append(sequence[v]).append(seperator);
368 joined.append(sequence[end]);
371 irc::stringjoiner::stringjoiner(const std::string &seperator, const char** sequence, int begin, int end)
373 for (int v = begin; v < end; v++)
374 joined.append(sequence[v]).append(seperator);
375 joined.append(sequence[end]);
378 std::string& irc::stringjoiner::GetJoined()
383 irc::portparser::portparser(const std::string &source, bool allow_overlapped) : in_range(0), range_begin(0), range_end(0), overlapped(allow_overlapped)
385 sep = new irc::commasepstream(source);
389 irc::portparser::~portparser()
394 bool irc::portparser::Overlaps(long val)
399 if (overlap_set.find(val) == overlap_set.end())
401 overlap_set[val] = true;
408 long irc::portparser::GetToken()
413 if (in_range <= range_end)
415 if (!Overlaps(in_range))
421 while (((Overlaps(in_range)) && (in_range <= range_end)))
424 if (in_range <= range_end)
432 std::string x = sep->GetToken();
437 while (Overlaps(atoi(x.c_str())))
445 std::string::size_type dash = x.rfind('-');
446 if (dash != std::string::npos)
448 std::string sbegin = x.substr(0, dash);
449 std::string send = x.substr(dash+1, x.length());
450 range_begin = atoi(sbegin.c_str());
451 range_end = atoi(send.c_str());
453 if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
455 in_range = range_begin;
460 /* Assume its just the one port */
461 return atoi(sbegin.c_str());
466 return atoi(x.c_str());
470 irc::dynamicbitmask::dynamicbitmask() : bits_size(4)
472 /* We start with 4 bytes allocated which is room
473 * for 4 items. Something makes me doubt its worth
474 * allocating less than 4 bytes.
476 bits = new unsigned char[bits_size];
477 memset(bits, 0, bits_size);
480 irc::dynamicbitmask::~dynamicbitmask()
482 /* Tidy up the entire used memory on delete */
486 irc::bitfield irc::dynamicbitmask::Allocate()
488 /* Yeah, this isnt too efficient, however a module or the core
489 * should only be allocating bitfields on load, the Toggle and
490 * Get methods are O(1) as these are called much more often.
492 unsigned char* freebits = this->GetFreeBits();
493 for (unsigned char i = 0; i < bits_size; i++)
495 /* Yes, this is right. You'll notice we terminate the loop when !current_pos,
496 * this is because we logic shift our bit off the end of unsigned char, and its
497 * lost, making the loop counter 0 when we're done.
499 for (unsigned char current_pos = 1; current_pos; current_pos = current_pos << 1)
501 if (!(freebits[i] & current_pos))
503 freebits[i] |= current_pos;
504 return std::make_pair(i, current_pos);
508 /* We dont have any free space left, increase by one */
510 if (bits_size == 255)
511 /* Oh dear, cant grow it any further */
512 throw std::bad_alloc();
514 unsigned char old_bits_size = bits_size;
516 /* Allocate new bitfield space */
517 unsigned char* temp_bits = new unsigned char[bits_size];
518 unsigned char* temp_freebits = new unsigned char[bits_size];
519 /* Copy the old data in */
520 memcpy(temp_bits, bits, old_bits_size);
521 memcpy(temp_freebits, freebits, old_bits_size);
522 /* Delete the old data pointers */
525 /* Swap the pointers over so now the new
526 * pointers point to our member values
529 freebits = temp_freebits;
530 this->SetFreeBits(freebits);
531 /* Initialize the new byte on the end of
532 * the bitfields, pre-allocate the one bit
533 * for this allocation
535 bits[old_bits_size] = 0;
536 freebits[old_bits_size] = 1;
537 /* We already know where we just allocated
538 * the bitfield, so no loop needed
540 return std::make_pair(old_bits_size, 1);
543 bool irc::dynamicbitmask::Deallocate(irc::bitfield &pos)
545 /* We dont bother to shrink the bitfield
546 * on deallocation, the most we could do
547 * is save one byte (!) and this would cost
548 * us a loop (ugly O(n) stuff) so we just
549 * clear the bit and leave the memory
550 * claimed -- nobody will care about one
553 if (pos.first < bits_size)
555 this->GetFreeBits()[pos.first] &= ~pos.second;
558 /* They gave a bitfield outside of the
559 * length of our array. BAD programmer.
564 void irc::dynamicbitmask::Toggle(irc::bitfield &pos, bool state)
566 /* Range check the value */
567 if (pos.first < bits_size)
570 /* Set state, OR the state in */
571 bits[pos.first] |= pos.second;
573 /* Clear state, AND the !state out */
574 bits[pos.first] &= ~pos.second;
578 bool irc::dynamicbitmask::Get(irc::bitfield &pos)
580 /* Range check the value */
581 if (pos.first < bits_size)
582 return (bits[pos.first] & pos.second);
584 /* We can't return false, otherwise we can't
585 * distinguish between failure and a cleared bit!
586 * Our only sensible choice is to throw (ew).
588 throw ModuleException("irc::dynamicbitmask::Get(): Invalid bitfield, out of range");
591 unsigned char irc::dynamicbitmask::GetSize()
596 std::string assign(const irc::string &other)
598 return other.c_str();
601 irc::string assign(const std::string &other)
603 return other.c_str();