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 * This file also contains hashing methods for hashing
43 * insp_inaddr structs, we use this if we want to cache IP
46 ******************************************************/
48 using namespace irc::sockets;
50 /* convert a string to lowercase. Note following special circumstances
51 * taken from RFC 1459. Many "official" server branches still hold to this
54 * Because of IRC's scandanavian origin, the characters {}| are
55 * considered to be the lower case equivalents of the characters []\,
56 * respectively. This is a critical issue when determining the
57 * equivalence of two nicknames.
59 void nspace::strlower(char *n)
63 for (char* t = n; *t; t++)
64 *t = lowermap[(unsigned char)*t];
68 size_t nspace::hash<insp_inaddr>::operator()(const insp_inaddr &a) const
71 memcpy(&q,&a,sizeof(size_t));
75 size_t nspace::hash<string>::operator()(const string &s) const
77 /* XXX: NO DATA COPIES! :)
78 * The hash function here is practically
79 * a copy of the one in STL's hash_fun.h,
80 * only with *x replaced with lowermap[*x].
81 * This avoids a copy to use hash<const char*>
83 register size_t t = 0;
84 for (std::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
85 t = 5 * t + lowermap[(unsigned char)*x];
89 size_t nspace::hash<irc::string>::operator()(const irc::string &s) const
91 register size_t t = 0;
92 for (irc::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
93 t = 5 * t + lowermap[(unsigned char)*x];
97 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
99 unsigned char* n1 = (unsigned char*)s1.c_str();
100 unsigned char* n2 = (unsigned char*)s2.c_str();
101 for (; *n1 && *n2; n1++, n2++)
102 if (lowermap[*n1] != lowermap[*n2])
104 return (lowermap[*n1] == lowermap[*n2]);
107 bool irc::InAddr_HashComp::operator()(const insp_inaddr &s1, const insp_inaddr &s2) const
110 for (int n = 0; n < 16; n++)
111 if (s2.s6_addr[n] != s1.s6_addr[n])
115 return (s1.s_addr == s1.s_addr);
119 /******************************************************
121 * This is the implementation of our special irc::string
122 * class which is a case-insensitive equivalent to
123 * std::string which is not only case-insensitive but
124 * can also do scandanavian comparisons, e.g. { = [, etc.
126 * This class depends on the const array 'lowermap'.
128 ******************************************************/
130 bool irc::irc_char_traits::eq(char c1st, char c2nd)
132 return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
135 bool irc::irc_char_traits::ne(char c1st, char c2nd)
137 return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
140 bool irc::irc_char_traits::lt(char c1st, char c2nd)
142 return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
145 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
147 for(unsigned int i = 0; i < n; i++)
149 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
152 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
155 if(*str1 == 0 || *str2 == 0)
164 std::string operator+ (std::string& leftval, irc::string& rightval)
166 return leftval + std::string(rightval.c_str());
169 irc::string operator+ (irc::string& leftval, std::string& rightval)
171 return leftval + irc::string(rightval.c_str());
174 bool operator== (std::string& leftval, irc::string& rightval)
176 return (leftval.c_str() == rightval);
179 bool operator== (irc::string& leftval, std::string& rightval)
181 return (leftval == rightval.c_str());
184 const char* irc::irc_char_traits::find(const char* s1, int n, char c)
186 while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
191 /* See hashcomp.h if you care about these... */
192 std::ostream& operator<<(std::ostream &os, const irc::string &str)
194 return os << str.c_str();
197 std::istream& operator>>(std::istream &is, irc::string &str)
205 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
207 /* Record starting position and current position */
208 last_starting_position = tokens.begin();
212 irc::tokenstream::~tokenstream()
216 const std::string irc::tokenstream::GetToken()
218 std::string::iterator lsp = last_starting_position;
220 while (n != tokens.end())
222 /** Skip multi space, converting " " into " "
224 while ((n+1 != tokens.end()) && (*n == ' ') && (*(n+1) == ' '))
227 if ((last_pushed) && (*n == ':'))
229 /* If we find a token thats not the first and starts with :,
230 * this is the last token on the line
232 std::string::iterator curr = ++n;
234 return std::string(curr, tokens.end());
239 if ((*n == ' ') || (n+1 == tokens.end()))
241 /* If we find a space, or end of string, this is the end of a token.
243 last_starting_position = n+1;
246 std::string strip(lsp, n+1 == tokens.end() ? n+1 : n++);
247 while ((strip.length()) && (strip.find_last_of(' ') == strip.length() - 1))
248 strip.erase(strip.end() - 1);
258 irc::sepstream::sepstream(const std::string &source, char seperator) : tokens(source), sep(seperator)
260 last_starting_position = tokens.begin();
264 const std::string irc::sepstream::GetToken()
266 std::string::iterator lsp = last_starting_position;
268 while (n != tokens.end())
270 if ((*n == sep) || (n+1 == tokens.end()))
272 last_starting_position = n+1;
273 std::string strip = std::string(lsp, n+1 == tokens.end() ? n+1 : n++);
275 while ((strip.length()) && (strip.find_last_of(sep) == strip.length() - 1))
276 strip.erase(strip.end() - 1);
287 irc::sepstream::~sepstream()
291 std::string irc::hex(const unsigned char *raw, size_t rawsz)
296 /* EWW! This used to be using sprintf, which is WAY inefficient. -Special */
298 const char *hex = "0123456789abcdef";
303 for (i = 0, j = 0; j < rawsz; ++j)
305 buf[i++] = hex[raw[j] / 16];
306 buf[i++] = hex[raw[j] % 16];
313 const char* irc::Spacify(const char* n)
315 static char x[MAXBUF];
317 for (char* y = x; *y; y++)
324 irc::modestacker::modestacker(bool add) : adding(add)
327 sequence.push_back("");
330 void irc::modestacker::Push(char modeletter, const std::string ¶meter)
332 *(sequence.begin()) += modeletter;
333 sequence.push_back(parameter);
336 void irc::modestacker::Push(char modeletter)
338 this->Push(modeletter,"");
341 void irc::modestacker::PushPlus()
346 void irc::modestacker::PushMinus()
351 int irc::modestacker::GetStackedLine(std::deque<std::string> &result)
355 result.push_back(adding ? "+" : "-");
357 while (!sequence[0].empty() && (sequence.size() > 1) && (result.size() < MAXMODES+1))
359 result[0] += *(sequence[0].begin());
360 if (!sequence[1].empty())
361 result.push_back(sequence[1]);
362 sequence[0].erase(sequence[0].begin());
363 sequence.erase(sequence.begin() + 1);
370 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::vector<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 std::deque<std::string> &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 irc::stringjoiner::stringjoiner(const std::string &seperator, const char** sequence, int begin, int end)
386 for (int v = begin; v < end; v++)
387 joined.append(sequence[v]).append(seperator);
388 joined.append(sequence[end]);
391 std::string& irc::stringjoiner::GetJoined()
396 irc::portparser::portparser(const std::string &source, bool allow_overlapped) : in_range(0), range_begin(0), range_end(0), overlapped(allow_overlapped)
398 sep = new irc::commasepstream(source);
402 irc::portparser::~portparser()
407 bool irc::portparser::Overlaps(long val)
412 if (overlap_set.find(val) == overlap_set.end())
414 overlap_set[val] = true;
421 long irc::portparser::GetToken()
426 if (in_range <= range_end)
428 if (!Overlaps(in_range))
434 while (((Overlaps(in_range)) && (in_range <= range_end)))
437 if (in_range <= range_end)
445 std::string x = sep->GetToken();
450 while (Overlaps(atoi(x.c_str())))
458 std::string::size_type dash = x.rfind('-');
459 if (dash != std::string::npos)
461 std::string sbegin = x.substr(0, dash);
462 std::string send = x.substr(dash+1, x.length());
463 range_begin = atoi(sbegin.c_str());
464 range_end = atoi(send.c_str());
466 if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
468 in_range = range_begin;
473 /* Assume its just the one port */
474 return atoi(sbegin.c_str());
479 return atoi(x.c_str());
483 irc::dynamicbitmask::dynamicbitmask() : bits_size(4)
485 /* We start with 4 bytes allocated which is room
486 * for 4 items. Something makes me doubt its worth
487 * allocating less than 4 bytes.
489 bits = new unsigned char[bits_size];
490 memset(bits, 0, bits_size);
493 irc::dynamicbitmask::~dynamicbitmask()
495 /* Tidy up the entire used memory on delete */
499 irc::bitfield irc::dynamicbitmask::Allocate()
501 /* Yeah, this isnt too efficient, however a module or the core
502 * should only be allocating bitfields on load, the Toggle and
503 * Get methods are O(1) as these are called much more often.
505 unsigned char* freebits = this->GetFreeBits();
506 for (unsigned char i = 0; i < bits_size; i++)
508 /* Yes, this is right. You'll notice we terminate the loop when !current_pos,
509 * this is because we logic shift our bit off the end of unsigned char, and its
510 * lost, making the loop counter 0 when we're done.
512 for (unsigned char current_pos = 1; current_pos; current_pos = current_pos << 1)
514 if (!(freebits[i] & current_pos))
516 freebits[i] |= current_pos;
517 return std::make_pair(i, current_pos);
521 /* We dont have any free space left, increase by one */
523 if (bits_size == 255)
524 /* Oh dear, cant grow it any further */
525 throw std::bad_alloc();
527 unsigned char old_bits_size = bits_size;
529 /* Allocate new bitfield space */
530 unsigned char* temp_bits = new unsigned char[bits_size];
531 unsigned char* temp_freebits = new unsigned char[bits_size];
532 /* Copy the old data in */
533 memcpy(temp_bits, bits, old_bits_size);
534 memcpy(temp_freebits, freebits, old_bits_size);
535 /* Delete the old data pointers */
538 /* Swap the pointers over so now the new
539 * pointers point to our member values
542 freebits = temp_freebits;
543 this->SetFreeBits(freebits);
544 /* Initialize the new byte on the end of
545 * the bitfields, pre-allocate the one bit
546 * for this allocation
548 bits[old_bits_size] = 0;
549 freebits[old_bits_size] = 1;
550 /* We already know where we just allocated
551 * the bitfield, so no loop needed
553 return std::make_pair(old_bits_size, 1);
556 bool irc::dynamicbitmask::Deallocate(irc::bitfield &pos)
558 /* We dont bother to shrink the bitfield
559 * on deallocation, the most we could do
560 * is save one byte (!) and this would cost
561 * us a loop (ugly O(n) stuff) so we just
562 * clear the bit and leave the memory
563 * claimed -- nobody will care about one
566 if (pos.first < bits_size)
568 this->GetFreeBits()[pos.first] &= ~pos.second;
571 /* They gave a bitfield outside of the
572 * length of our array. BAD programmer.
577 void irc::dynamicbitmask::Toggle(irc::bitfield &pos, bool state)
579 /* Range check the value */
580 if (pos.first < bits_size)
583 /* Set state, OR the state in */
584 bits[pos.first] |= pos.second;
586 /* Clear state, AND the !state out */
587 bits[pos.first] &= ~pos.second;
591 bool irc::dynamicbitmask::Get(irc::bitfield &pos)
593 /* Range check the value */
594 if (pos.first < bits_size)
595 return (bits[pos.first] & pos.second);
597 /* We can't return false, otherwise we can't
598 * distinguish between failure and a cleared bit!
599 * Our only sensible choice is to throw (ew).
601 throw ModuleException("irc::dynamicbitmask::Get(): Invalid bitfield, out of range");
604 unsigned char irc::dynamicbitmask::GetSize()
609 std::string assign(const irc::string &other)
611 return other.c_str();
614 irc::string assign(const std::string &other)
616 return other.c_str();