1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
14 /* $Core: libIRCDhash */
19 #include <ext/hash_map>
20 #define nspace __gnu_cxx
24 using stdext::hash_map;
27 /******************************************************
29 * The hash functions of InspIRCd are the centrepoint
30 * of the entire system. If these functions are
31 * inefficient or wasteful, the whole program suffers
32 * as a result. A lot of C programmers in the ircd
33 * scene spend a lot of time debating (arguing) about
34 * the best way to write hash functions to hash irc
35 * nicknames, channels etc.
36 * We are lucky as C++ developers as hash_map does
37 * a lot of this for us. It does intellegent memory
38 * requests, bucketing, search functions, insertion
39 * and deletion etc. All we have to do is write some
40 * overloaded comparison and hash value operators which
41 * cause it to act in an irc-like way. The features we
42 * add to the standard hash_map are:
44 * Case insensitivity: The hash_map will be case
47 * Scandanavian Comparisons: The characters [, ], \ will
48 * be considered the lowercase of {, } and |.
50 ******************************************************/
52 /* convert a string to lowercase. Note following special circumstances
53 * taken from RFC 1459. Many "official" server branches still hold to this
56 * Because of IRC's scandanavian origin, the characters {}| are
57 * considered to be the lower case equivalents of the characters []\,
58 * respectively. This is a critical issue when determining the
59 * equivalence of two nicknames.
61 void nspace::strlower(char *n)
65 for (char* t = n; *t; t++)
66 *t = lowermap[(unsigned char)*t];
71 size_t nspace::hash<std::string>::operator()(const std::string &s) const
73 size_t nspace::hash_compare<std::string, std::less<std::string> >::operator()(const std::string &s) const
76 /* XXX: NO DATA COPIES! :)
77 * The hash function here is practically
78 * a copy of the one in STL's hash_fun.h,
79 * only with *x replaced with lowermap[*x].
80 * This avoids a copy to use hash<const char*>
82 register size_t t = 0;
83 for (std::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
84 t = 5 * t + lowermap[(unsigned char)*x];
89 size_t nspace::hash<irc::string>::operator()(const irc::string &s) const
91 size_t nspace::hash_compare<irc::string, std::less<irc::string> >::operator()(const irc::string &s) const
94 register size_t t = 0;
95 for (irc::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
96 t = 5 * t + lowermap[(unsigned char)*x];
100 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
102 unsigned char* n1 = (unsigned char*)s1.c_str();
103 unsigned char* n2 = (unsigned char*)s2.c_str();
104 for (; *n1 && *n2; n1++, n2++)
105 if (lowermap[*n1] != lowermap[*n2])
107 return (lowermap[*n1] == lowermap[*n2]);
110 /******************************************************
112 * This is the implementation of our special irc::string
113 * class which is a case-insensitive equivalent to
114 * std::string which is not only case-insensitive but
115 * can also do scandanavian comparisons, e.g. { = [, etc.
117 * This class depends on the const array 'lowermap'.
119 ******************************************************/
121 bool irc::irc_char_traits::eq(char c1st, char c2nd)
123 return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
126 bool irc::irc_char_traits::ne(char c1st, char c2nd)
128 return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
131 bool irc::irc_char_traits::lt(char c1st, char c2nd)
133 return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
136 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
138 for(unsigned int i = 0; i < n; i++)
140 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
143 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
146 if(*str1 == 0 || *str2 == 0)
155 const char* irc::irc_char_traits::find(const char* s1, int n, char c)
157 while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
162 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
164 /* Record starting position and current position */
165 last_starting_position = tokens.begin();
169 irc::tokenstream::~tokenstream()
173 bool irc::tokenstream::GetToken(std::string &token)
175 std::string::iterator lsp = last_starting_position;
177 while (n != tokens.end())
179 /** Skip multi space, converting " " into " "
181 while ((n+1 != tokens.end()) && (*n == ' ') && (*(n+1) == ' '))
184 if ((last_pushed) && (*n == ':'))
186 /* If we find a token thats not the first and starts with :,
187 * this is the last token on the line
189 std::string::iterator curr = ++n;
191 token = std::string(curr, tokens.end());
197 if ((*n == ' ') || (n+1 == tokens.end()))
199 /* If we find a space, or end of string, this is the end of a token.
201 last_starting_position = n+1;
204 std::string strip(lsp, n+1 == tokens.end() ? n+1 : n++);
205 while ((strip.length()) && (strip.find_last_of(' ') == strip.length() - 1))
206 strip.erase(strip.end() - 1);
209 return !token.empty();
218 bool irc::tokenstream::GetToken(irc::string &token)
220 std::string stdstring;
221 bool returnval = GetToken(stdstring);
222 token = assign(stdstring);
226 bool irc::tokenstream::GetToken(int &token)
229 bool returnval = GetToken(tok);
230 token = ConvToInt(tok);
234 bool irc::tokenstream::GetToken(long &token)
237 bool returnval = GetToken(tok);
238 token = ConvToInt(tok);
242 irc::sepstream::sepstream(const std::string &source, char seperator) : tokens(source), sep(seperator)
244 last_starting_position = tokens.begin();
248 bool irc::sepstream::GetToken(std::string &token)
250 std::string::iterator lsp = last_starting_position;
252 while (n != tokens.end())
254 if ((*n == sep) || (n+1 == tokens.end()))
256 last_starting_position = n+1;
257 token = std::string(lsp, n+1 == tokens.end() ? n+1 : n++);
259 while ((token.length()) && (token.find_last_of(sep) == token.length() - 1))
260 token.erase(token.end() - 1);
265 return n == tokens.end() ? false : true;
275 const std::string irc::sepstream::GetRemaining()
277 return std::string(n, tokens.end());
280 bool irc::sepstream::StreamEnd()
282 return ((n + 1) == tokens.end());
285 irc::sepstream::~sepstream()
289 std::string irc::hex(const unsigned char *raw, size_t rawsz)
294 /* EWW! This used to be using sprintf, which is WAY inefficient. -Special */
296 const char *hex = "0123456789abcdef";
297 static char hexbuf[MAXBUF];
300 for (i = 0, j = 0; j < rawsz; ++j)
302 hexbuf[i++] = hex[raw[j] / 16];
303 hexbuf[i++] = hex[raw[j] % 16];
310 CoreExport const char* irc::Spacify(const char* n)
312 static char x[MAXBUF];
314 for (char* y = x; *y; y++)
321 irc::modestacker::modestacker(bool add) : adding(add)
324 sequence.push_back("");
327 void irc::modestacker::Push(char modeletter, const std::string ¶meter)
329 *(sequence.begin()) += modeletter;
330 sequence.push_back(parameter);
333 void irc::modestacker::Push(char modeletter)
335 this->Push(modeletter,"");
338 void irc::modestacker::PushPlus()
343 void irc::modestacker::PushMinus()
348 int irc::modestacker::GetStackedLine(std::deque<std::string> &result, int max_line_size)
350 if (sequence.empty())
357 int size = 1; /* Account for initial +/- char */
360 result.push_back(adding ? "+" : "-");
362 if (sequence.size() > 1)
363 nextsize = sequence[1].length() + 2;
365 while (!sequence[0].empty() && (sequence.size() > 1) && (result.size() < MAXMODES) && ((size + nextsize) < max_line_size))
367 result[0] += *(sequence[0].begin());
368 if (!sequence[1].empty())
370 result.push_back(sequence[1]);
371 size += nextsize; /* Account for mode character and whitespace */
373 sequence[0].erase(sequence[0].begin());
374 sequence.erase(sequence.begin() + 1);
376 if (sequence.size() > 1)
377 nextsize = sequence[1].length() + 2;
385 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end)
387 for (int v = begin; v < end; v++)
388 joined.append(sequence[v]).append(seperator);
389 joined.append(sequence[end]);
392 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end)
394 for (int v = begin; v < end; v++)
395 joined.append(sequence[v]).append(seperator);
396 joined.append(sequence[end]);
399 irc::stringjoiner::stringjoiner(const std::string &seperator, const char* const* sequence, int begin, int end)
401 for (int v = begin; v < end; v++)
402 joined.append(sequence[v]).append(seperator);
403 joined.append(sequence[end]);
406 std::string& irc::stringjoiner::GetJoined()
411 irc::portparser::portparser(const std::string &source, bool allow_overlapped) : in_range(0), range_begin(0), range_end(0), overlapped(allow_overlapped)
413 sep = new irc::commasepstream(source);
417 irc::portparser::~portparser()
422 bool irc::portparser::Overlaps(long val)
427 if (overlap_set.find(val) == overlap_set.end())
429 overlap_set[val] = true;
436 long irc::portparser::GetToken()
441 if (in_range <= range_end)
443 if (!Overlaps(in_range))
449 while (((Overlaps(in_range)) && (in_range <= range_end)))
452 if (in_range <= range_end)
466 while (Overlaps(atoi(x.c_str())))
468 if (!sep->GetToken(x))
472 std::string::size_type dash = x.rfind('-');
473 if (dash != std::string::npos)
475 std::string sbegin = x.substr(0, dash);
476 std::string send = x.substr(dash+1, x.length());
477 range_begin = atoi(sbegin.c_str());
478 range_end = atoi(send.c_str());
480 if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
482 in_range = range_begin;
487 /* Assume its just the one port */
488 return atoi(sbegin.c_str());
493 return atoi(x.c_str());
497 irc::dynamicbitmask::dynamicbitmask() : bits_size(4)
499 /* We start with 4 bytes allocated which is room
500 * for 4 items. Something makes me doubt its worth
501 * allocating less than 4 bytes.
503 bits = new unsigned char[bits_size];
504 memset(bits, 0, bits_size);
507 irc::dynamicbitmask::~dynamicbitmask()
509 /* Tidy up the entire used memory on delete */
513 irc::bitfield irc::dynamicbitmask::Allocate()
515 /* Yeah, this isnt too efficient, however a module or the core
516 * should only be allocating bitfields on load, the Toggle and
517 * Get methods are O(1) as these are called much more often.
519 unsigned char* freebits = this->GetFreeBits();
520 for (unsigned char i = 0; i < bits_size; i++)
522 /* Yes, this is right. You'll notice we terminate the loop when !current_pos,
523 * this is because we logic shift our bit off the end of unsigned char, and its
524 * lost, making the loop counter 0 when we're done.
526 for (unsigned char current_pos = 1; current_pos; current_pos = current_pos << 1)
528 if (!(freebits[i] & current_pos))
530 freebits[i] |= current_pos;
531 return std::make_pair(i, current_pos);
535 /* We dont have any free space left, increase by one */
537 if (bits_size == 255)
538 /* Oh dear, cant grow it any further */
539 throw std::bad_alloc();
541 unsigned char old_bits_size = bits_size;
543 /* Allocate new bitfield space */
544 unsigned char* temp_bits = new unsigned char[bits_size];
545 unsigned char* temp_freebits = new unsigned char[bits_size];
546 /* Copy the old data in */
547 memcpy(temp_bits, bits, old_bits_size);
548 memcpy(temp_freebits, freebits, old_bits_size);
549 /* Delete the old data pointers */
552 /* Swap the pointers over so now the new
553 * pointers point to our member values
556 freebits = temp_freebits;
557 this->SetFreeBits(freebits);
558 /* Initialize the new byte on the end of
559 * the bitfields, pre-allocate the one bit
560 * for this allocation
562 bits[old_bits_size] = 0;
563 freebits[old_bits_size] = 1;
564 /* We already know where we just allocated
565 * the bitfield, so no loop needed
567 return std::make_pair(old_bits_size, 1);
570 bool irc::dynamicbitmask::Deallocate(irc::bitfield &pos)
572 /* We dont bother to shrink the bitfield
573 * on deallocation, the most we could do
574 * is save one byte (!) and this would cost
575 * us a loop (ugly O(n) stuff) so we just
576 * clear the bit and leave the memory
577 * claimed -- nobody will care about one
580 if (pos.first < bits_size)
582 this->GetFreeBits()[pos.first] &= ~pos.second;
585 /* They gave a bitfield outside of the
586 * length of our array. BAD programmer.
591 void irc::dynamicbitmask::Toggle(irc::bitfield &pos, bool state)
593 /* Range check the value */
594 if (pos.first < bits_size)
597 /* Set state, OR the state in */
598 bits[pos.first] |= pos.second;
600 /* Clear state, AND the !state out */
601 bits[pos.first] &= ~pos.second;
605 bool irc::dynamicbitmask::Get(irc::bitfield &pos)
607 /* Range check the value */
608 if (pos.first < bits_size)
609 return (bits[pos.first] & pos.second);
611 /* We can't return false, otherwise we can't
612 * distinguish between failure and a cleared bit!
613 * Our only sensible choice is to throw (ew).
615 throw ModuleException("irc::dynamicbitmask::Get(): Invalid bitfield, out of range");
618 unsigned char irc::dynamicbitmask::GetSize()