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 * ---------------------------------------------------
20 /******************************************************
22 * The hash functions of InspIRCd are the centrepoint
23 * of the entire system. If these functions are
24 * inefficient or wasteful, the whole program suffers
25 * as a result. A lot of C programmers in the ircd
26 * scene spend a lot of time debating (arguing) about
27 * the best way to write hash functions to hash irc
28 * nicknames, channels etc.
29 * We are lucky as C++ developers as hash_map does
30 * a lot of this for us. It does intellegent memory
31 * requests, bucketing, search functions, insertion
32 * and deletion etc. All we have to do is write some
33 * overloaded comparison and hash value operators which
34 * cause it to act in an irc-like way. The features we
35 * add to the standard hash_map are:
37 * Case insensitivity: The hash_map will be case
40 * Scandanavian Comparisons: The characters [, ], \ will
41 * be considered the lowercase of {, } and |.
43 ******************************************************/
45 /* convert a string to lowercase. Note following special circumstances
46 * taken from RFC 1459. Many "official" server branches still hold to this
49 * Because of IRC's scandanavian origin, the characters {}| are
50 * considered to be the lower case equivalents of the characters []\,
51 * respectively. This is a critical issue when determining the
52 * equivalence of two nicknames.
54 void nspace::strlower(char *n)
58 for (char* t = n; *t; t++)
59 *t = rfc_case_insensitive_map[(unsigned char)*t];
64 #ifdef HASHMAP_DEPRECATED
65 size_t nspace::insensitive::operator()(const std::string &s) const
67 size_t nspace::hash<std::string>::operator()(const std::string &s) const
70 size_t nspace::hash_compare<std::string, std::less<std::string> >::operator()(const std::string &s) const
73 /* XXX: NO DATA COPIES! :)
74 * The hash function here is practically
75 * a copy of the one in STL's hash_fun.h,
76 * only with *x replaced with rfc_case_insensitive_map[*x].
77 * This avoids a copy to use hash<const char*>
79 register size_t t = 0;
80 for (std::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
81 t = 5 * t + rfc_case_insensitive_map[(unsigned char)*x];
87 size_t nspace::hash<irc::string>::operator()(const irc::string &s) const
89 size_t nspace::hash_compare<irc::string, std::less<irc::string> >::operator()(const irc::string &s) const
92 register size_t t = 0;
93 for (irc::string::const_iterator x = s.begin(); x != s.end(); ++x) /* ++x not x++, as its faster */
94 t = 5 * t + rfc_case_insensitive_map[(unsigned char)*x];
98 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
100 const unsigned char* n1 = (const unsigned char*)s1.c_str();
101 const unsigned char* n2 = (const unsigned char*)s2.c_str();
102 for (; *n1 && *n2; n1++, n2++)
103 if (rfc_case_insensitive_map[*n1] != rfc_case_insensitive_map[*n2])
105 return (rfc_case_insensitive_map[*n1] == rfc_case_insensitive_map[*n2]);
108 /******************************************************
110 * This is the implementation of our special irc::string
111 * class which is a case-insensitive equivalent to
112 * std::string which is not only case-insensitive but
113 * can also do scandanavian comparisons, e.g. { = [, etc.
115 * This class depends on the const array 'rfc_case_insensitive_map'.
117 ******************************************************/
119 bool irc::irc_char_traits::eq(char c1st, char c2nd)
121 return rfc_case_insensitive_map[(unsigned char)c1st] == rfc_case_insensitive_map[(unsigned char)c2nd];
124 bool irc::irc_char_traits::ne(char c1st, char c2nd)
126 return rfc_case_insensitive_map[(unsigned char)c1st] != rfc_case_insensitive_map[(unsigned char)c2nd];
129 bool irc::irc_char_traits::lt(char c1st, char c2nd)
131 return rfc_case_insensitive_map[(unsigned char)c1st] < rfc_case_insensitive_map[(unsigned char)c2nd];
134 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
136 for(unsigned int i = 0; i < n; i++)
138 if(rfc_case_insensitive_map[(unsigned char)*str1] > rfc_case_insensitive_map[(unsigned char)*str2])
141 if(rfc_case_insensitive_map[(unsigned char)*str1] < rfc_case_insensitive_map[(unsigned char)*str2])
144 if(*str1 == 0 || *str2 == 0)
153 const char* irc::irc_char_traits::find(const char* s1, int n, char c)
155 while(n-- > 0 && rfc_case_insensitive_map[(unsigned char)*s1] != rfc_case_insensitive_map[(unsigned char)c])
160 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
162 /* Record starting position and current position */
163 last_starting_position = tokens.begin();
167 irc::tokenstream::~tokenstream()
171 bool irc::tokenstream::GetToken(std::string &token)
173 std::string::iterator lsp = last_starting_position;
175 while (n != tokens.end())
177 /** Skip multi space, converting " " into " "
179 while ((n+1 != tokens.end()) && (*n == ' ') && (*(n+1) == ' '))
182 if ((last_pushed) && (*n == ':'))
184 /* If we find a token thats not the first and starts with :,
185 * this is the last token on the line
187 std::string::iterator curr = ++n;
189 token = std::string(curr, tokens.end());
195 if ((*n == ' ') || (n+1 == tokens.end()))
197 /* If we find a space, or end of string, this is the end of a token.
199 last_starting_position = n+1;
202 std::string strip(lsp, n+1 == tokens.end() ? n+1 : n++);
203 while ((strip.length()) && (strip.find_last_of(' ') == strip.length() - 1))
204 strip.erase(strip.end() - 1);
207 return !token.empty();
216 bool irc::tokenstream::GetToken(irc::string &token)
218 std::string stdstring;
219 bool returnval = GetToken(stdstring);
220 token = assign(stdstring);
224 bool irc::tokenstream::GetToken(int &token)
227 bool returnval = GetToken(tok);
228 token = ConvToInt(tok);
232 bool irc::tokenstream::GetToken(long &token)
235 bool returnval = GetToken(tok);
236 token = ConvToInt(tok);
240 irc::sepstream::sepstream(const std::string &source, char seperator) : tokens(source), sep(seperator)
242 last_starting_position = tokens.begin();
246 bool irc::sepstream::GetToken(std::string &token)
248 std::string::iterator lsp = last_starting_position;
250 while (n != tokens.end())
252 if ((*n == sep) || (n+1 == tokens.end()))
254 last_starting_position = n+1;
255 token = std::string(lsp, n+1 == tokens.end() ? n+1 : n++);
257 while ((token.length()) && (token.find_last_of(sep) == token.length() - 1))
258 token.erase(token.end() - 1);
263 return n == tokens.end() ? false : true;
273 const std::string irc::sepstream::GetRemaining()
275 return std::string(n, tokens.end());
278 bool irc::sepstream::StreamEnd()
280 return ((n + 1) == tokens.end());
283 irc::sepstream::~sepstream()
287 std::string irc::hex(const unsigned char *raw, size_t rawsz)
292 /* EWW! This used to be using sprintf, which is WAY inefficient. -Special */
294 const char *hex = "0123456789abcdef";
295 static char hexbuf[MAXBUF];
298 for (i = 0, j = 0; j < rawsz; ++j)
300 hexbuf[i++] = hex[raw[j] / 16];
301 hexbuf[i++] = hex[raw[j] % 16];
308 CoreExport const char* irc::Spacify(const char* n)
310 static char x[MAXBUF];
312 for (char* y = x; *y; y++)
319 irc::modestacker::modestacker(InspIRCd* Instance, bool add) : ServerInstance(Instance), adding(add)
322 sequence.push_back("");
325 void irc::modestacker::Push(char modeletter, const std::string ¶meter)
327 *(sequence.begin()) += modeletter;
328 sequence.push_back(parameter);
331 void irc::modestacker::Push(char modeletter)
333 this->Push(modeletter,"");
336 void irc::modestacker::PushPlus()
341 void irc::modestacker::PushMinus()
346 int irc::modestacker::GetStackedLine(std::deque<std::string> &result, int max_line_size)
348 if (sequence.empty())
355 int size = 1; /* Account for initial +/- char */
358 result.push_back(adding ? "+" : "-");
360 if (sequence.size() > 1)
361 nextsize = sequence[1].length() + 2;
363 while (!sequence[0].empty() && (sequence.size() > 1) && (result.size() < ServerInstance->Config->Limits.MaxModes) && ((size + nextsize) < max_line_size))
365 result[0] += *(sequence[0].begin());
366 if (!sequence[1].empty())
368 result.push_back(sequence[1]);
369 size += nextsize; /* Account for mode character and whitespace */
371 sequence[0].erase(sequence[0].begin());
372 sequence.erase(sequence.begin() + 1);
374 if (sequence.size() > 1)
375 nextsize = sequence[1].length() + 2;
383 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end)
386 throw "stringjoiner logic error, this causes problems.";
388 for (int v = begin; v < end; v++)
389 joined.append(sequence[v]).append(seperator);
390 joined.append(sequence[end]);
393 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end)
396 throw "stringjoiner logic error, this causes problems.";
398 for (int v = begin; v < end; v++)
399 joined.append(sequence[v]).append(seperator);
400 joined.append(sequence[end]);
403 irc::stringjoiner::stringjoiner(const std::string &seperator, const char* const* sequence, int begin, int end)
406 throw "stringjoiner logic error, this causes problems.";
408 for (int v = begin; v < end; v++)
409 joined.append(sequence[v]).append(seperator);
410 joined.append(sequence[end]);
413 std::string& irc::stringjoiner::GetJoined()
418 irc::portparser::portparser(const std::string &source, bool allow_overlapped) : in_range(0), range_begin(0), range_end(0), overlapped(allow_overlapped)
420 sep = new irc::commasepstream(source);
424 irc::portparser::~portparser()
429 bool irc::portparser::Overlaps(long val)
434 if (overlap_set.find(val) == overlap_set.end())
436 overlap_set[val] = true;
443 long irc::portparser::GetToken()
448 if (in_range <= range_end)
450 if (!Overlaps(in_range))
456 while (((Overlaps(in_range)) && (in_range <= range_end)))
459 if (in_range <= range_end)
473 while (Overlaps(atoi(x.c_str())))
475 if (!sep->GetToken(x))
479 std::string::size_type dash = x.rfind('-');
480 if (dash != std::string::npos)
482 std::string sbegin = x.substr(0, dash);
483 std::string send = x.substr(dash+1, x.length());
484 range_begin = atoi(sbegin.c_str());
485 range_end = atoi(send.c_str());
487 if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
489 in_range = range_begin;
494 /* Assume its just the one port */
495 return atoi(sbegin.c_str());
500 return atoi(x.c_str());
504 irc::dynamicbitmask::dynamicbitmask() : bits_size(4)
506 /* We start with 4 bytes allocated which is room
507 * for 4 items. Something makes me doubt its worth
508 * allocating less than 4 bytes.
510 bits = new unsigned char[bits_size];
511 memset(bits, 0, bits_size);
514 irc::dynamicbitmask::~dynamicbitmask()
516 /* Tidy up the entire used memory on delete */
520 irc::bitfield irc::dynamicbitmask::Allocate()
522 /* Yeah, this isnt too efficient, however a module or the core
523 * should only be allocating bitfields on load, the Toggle and
524 * Get methods are O(1) as these are called much more often.
526 unsigned char* freebits = this->GetFreeBits();
527 for (unsigned char i = 0; i < bits_size; i++)
529 /* Yes, this is right. You'll notice we terminate the loop when !current_pos,
530 * this is because we logic shift our bit off the end of unsigned char, and its
531 * lost, making the loop counter 0 when we're done.
533 for (unsigned char current_pos = 1; current_pos; current_pos = current_pos << 1)
535 if (!(freebits[i] & current_pos))
537 freebits[i] |= current_pos;
538 return std::make_pair(i, current_pos);
542 /* We dont have any free space left, increase by one */
544 if (bits_size == 255)
545 /* Oh dear, cant grow it any further */
546 throw std::bad_alloc();
548 unsigned char old_bits_size = bits_size;
550 /* Allocate new bitfield space */
551 unsigned char* temp_bits = new unsigned char[bits_size];
552 unsigned char* temp_freebits = new unsigned char[bits_size];
553 /* Copy the old data in */
554 memcpy(temp_bits, bits, old_bits_size);
555 memcpy(temp_freebits, freebits, old_bits_size);
556 /* Delete the old data pointers */
559 /* Swap the pointers over so now the new
560 * pointers point to our member values
563 freebits = temp_freebits;
564 this->SetFreeBits(freebits);
565 /* Initialize the new byte on the end of
566 * the bitfields, pre-allocate the one bit
567 * for this allocation
569 bits[old_bits_size] = 0;
570 freebits[old_bits_size] = 1;
571 /* We already know where we just allocated
572 * the bitfield, so no loop needed
574 return std::make_pair(old_bits_size, 1);
577 bool irc::dynamicbitmask::Deallocate(irc::bitfield &pos)
579 /* We dont bother to shrink the bitfield
580 * on deallocation, the most we could do
581 * is save one byte (!) and this would cost
582 * us a loop (ugly O(n) stuff) so we just
583 * clear the bit and leave the memory
584 * claimed -- nobody will care about one
587 if (pos.first < bits_size)
589 this->GetFreeBits()[pos.first] &= ~pos.second;
592 /* They gave a bitfield outside of the
593 * length of our array. BAD programmer.
598 void irc::dynamicbitmask::Toggle(irc::bitfield &pos, bool state)
600 /* Range check the value */
601 if (pos.first < bits_size)
604 /* Set state, OR the state in */
605 bits[pos.first] |= pos.second;
607 /* Clear state, AND the !state out */
608 bits[pos.first] &= ~pos.second;
612 bool irc::dynamicbitmask::Get(irc::bitfield &pos)
614 /* Range check the value */
615 if (pos.first < bits_size)
616 return (bits[pos.first] & pos.second);
618 /* We can't return false, otherwise we can't
619 * distinguish between failure and a cleared bit!
620 * Our only sensible choice is to throw (ew).
622 throw ModuleException("irc::dynamicbitmask::Get(): Invalid bitfield, out of range");
625 unsigned char irc::dynamicbitmask::GetSize()