]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/hashcomp.cpp
Make SAJOIN/SAPART more spanningtree friendly, they are throwbacks to the inspircd...
[user/henk/code/inspircd.git] / src / hashcomp.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "hashcomp.h"
16 #include <ext/hash_map>
17 #define nspace __gnu_cxx
18
19 /******************************************************
20  *
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:
35  *
36  * Case insensitivity: The hash_map will be case
37  * insensitive.
38  *
39  * Scandanavian Comparisons: The characters [, ], \ will
40  * be considered the lowercase of {, } and |.
41  *
42  ******************************************************/
43
44 using namespace irc::sockets;
45
46 /* convert a string to lowercase. Note following special circumstances
47  * taken from RFC 1459. Many "official" server branches still hold to this
48  * rule so i will too;
49  *
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.
54  */
55 void nspace::strlower(char *n)
56 {
57         if (n)
58         {
59                 for (char* t = n; *t; t++)
60                         *t = lowermap[(unsigned char)*t];
61         }
62 }
63
64 size_t nspace::hash<string>::operator()(const string &s) const
65 {
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*>
71          */
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];
75         return t;
76 }
77
78 size_t nspace::hash<irc::string>::operator()(const irc::string &s) const
79 {
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];
83         return t;
84 }
85
86 bool irc::StrHashComp::operator()(const std::string& s1, const std::string& s2) const
87 {
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])
92                         return false;
93         return (lowermap[*n1] == lowermap[*n2]);
94 }
95
96 /******************************************************
97  *
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.
102  *
103  * This class depends on the const array 'lowermap'.
104  *
105  ******************************************************/
106
107 bool irc::irc_char_traits::eq(char c1st, char c2nd)
108 {
109         return lowermap[(unsigned char)c1st] == lowermap[(unsigned char)c2nd];
110 }
111
112 bool irc::irc_char_traits::ne(char c1st, char c2nd)
113 {
114         return lowermap[(unsigned char)c1st] != lowermap[(unsigned char)c2nd];
115 }
116
117 bool irc::irc_char_traits::lt(char c1st, char c2nd)
118 {
119         return lowermap[(unsigned char)c1st] < lowermap[(unsigned char)c2nd];
120 }
121
122 int irc::irc_char_traits::compare(const char* str1, const char* str2, size_t n)
123 {
124         for(unsigned int i = 0; i < n; i++)
125         {
126                 if(lowermap[(unsigned char)*str1] > lowermap[(unsigned char)*str2])
127                         return 1;
128
129                 if(lowermap[(unsigned char)*str1] < lowermap[(unsigned char)*str2])
130                         return -1;
131
132                 if(*str1 == 0 || *str2 == 0)
133                         return 0;
134
135                 str1++;
136                 str2++;
137         }
138         return 0;
139 }
140
141 std::string operator+ (std::string& leftval, irc::string& rightval)
142 {
143         return leftval + std::string(rightval.c_str());
144 }
145
146 irc::string operator+ (irc::string& leftval, std::string& rightval)
147 {
148         return leftval + irc::string(rightval.c_str());
149 }
150
151 bool operator== (const std::string& leftval, const irc::string& rightval)
152 {
153         return (leftval.c_str() == rightval);
154 }
155
156 bool operator== (const irc::string& leftval, const std::string& rightval)
157 {
158         return (leftval == rightval.c_str());
159 }
160
161 const char* irc::irc_char_traits::find(const char* s1, int  n, char c)
162 {
163         while(n-- > 0 && lowermap[(unsigned char)*s1] != lowermap[(unsigned char)c])
164                 s1++;
165         return s1;
166 }
167
168 /* See hashcomp.h if you care about these... */
169 std::ostream& operator<<(std::ostream &os, const irc::string &str)
170 {
171         return os << str.c_str();
172 }
173
174 std::istream& operator>>(std::istream &is, irc::string &str)
175 {
176         std::string tmp;
177         is >> tmp;
178         str = tmp.c_str();
179         return is;
180 }
181
182 irc::tokenstream::tokenstream(const std::string &source) : tokens(source), last_pushed(false)
183 {
184         /* Record starting position and current position */
185         last_starting_position = tokens.begin();
186         n = tokens.begin();
187 }
188
189 irc::tokenstream::~tokenstream()
190 {
191 }
192
193 bool irc::tokenstream::GetToken(std::string &token)
194 {
195         std::string::iterator lsp = last_starting_position;
196
197         while (n != tokens.end())
198         {
199                 /** Skip multi space, converting "  " into " "
200                  */
201                 while ((n+1 != tokens.end()) && (*n == ' ') && (*(n+1) == ' '))
202                         n++;
203
204                 if ((last_pushed) && (*n == ':'))
205                 {
206                         /* If we find a token thats not the first and starts with :,
207                          * this is the last token on the line
208                          */
209                         std::string::iterator curr = ++n;
210                         n = tokens.end();
211                         token = std::string(curr, tokens.end());
212                         return true;
213                 }
214
215                 last_pushed = false;
216
217                 if ((*n == ' ') || (n+1 == tokens.end()))
218                 {
219                         /* If we find a space, or end of string, this is the end of a token.
220                          */
221                         last_starting_position = n+1;
222                         last_pushed = true;
223
224                         std::string strip(lsp, n+1 == tokens.end() ? n+1  : n++);
225                         while ((strip.length()) && (strip.find_last_of(' ') == strip.length() - 1))
226                                 strip.erase(strip.end() - 1);
227
228                         token = strip;
229                         return !token.empty();
230                 }
231
232                 n++;
233         }
234         token = "";
235         return false;
236 }
237
238 irc::sepstream::sepstream(const std::string &source, char seperator) : tokens(source), sep(seperator)
239 {
240         last_starting_position = tokens.begin();
241         n = tokens.begin();
242 }
243
244 const std::string irc::sepstream::GetToken()
245 {
246         std::string::iterator lsp = last_starting_position;
247
248         while (n != tokens.end())
249         {
250                 if ((*n == sep) || (n+1 == tokens.end()))
251                 {
252                         last_starting_position = n+1;
253                         std::string strip = std::string(lsp, n+1 == tokens.end() ? n+1  : n++);
254
255                         while ((strip.length()) && (strip.find_last_of(sep) == strip.length() - 1))
256                                 strip.erase(strip.end() - 1);
257
258                         return strip;
259                 }
260
261                 n++;
262         }
263
264         return "";
265 }
266
267 const std::string irc::sepstream::GetRemaining()
268 {
269         return std::string(n, tokens.end());
270 }
271
272 bool irc::sepstream::StreamEnd()
273 {
274         return ((n + 1) == tokens.end());
275 }
276
277 irc::sepstream::~sepstream()
278 {
279 }
280
281 std::string irc::hex(const unsigned char *raw, size_t rawsz)
282 {
283         if (!rawsz)
284                 return "";
285
286         /* EWW! This used to be using sprintf, which is WAY inefficient. -Special */
287         
288         const char *hex = "0123456789abcdef";
289         
290         char buf[rawsz*2+1];
291
292         size_t i, j;
293         for (i = 0, j = 0; j < rawsz; ++j)
294         {
295                 buf[i++] = hex[raw[j] / 16];
296                 buf[i++] = hex[raw[j] % 16];
297         }
298         buf[i] = '\0';
299
300         return buf;
301 }
302
303 const char* irc::Spacify(const char* n)
304 {
305         static char x[MAXBUF];
306         strlcpy(x,n,MAXBUF);
307         for (char* y = x; *y; y++)
308                 if (*y == '_')
309                         *y = ' ';
310         return x;
311 }
312
313
314 irc::modestacker::modestacker(bool add) : adding(add)
315 {
316         sequence.clear();
317         sequence.push_back("");
318 }
319
320 void irc::modestacker::Push(char modeletter, const std::string &parameter)
321 {
322         *(sequence.begin()) += modeletter;
323         sequence.push_back(parameter);
324 }
325
326 void irc::modestacker::Push(char modeletter)
327 {
328         this->Push(modeletter,"");
329 }
330
331 void irc::modestacker::PushPlus()
332 {
333         this->Push('+',"");
334 }
335
336 void irc::modestacker::PushMinus()
337 {
338         this->Push('-',"");
339 }
340
341 int irc::modestacker::GetStackedLine(std::deque<std::string> &result, int max_line_size)
342 {
343         if (sequence.empty())
344         {
345                 result.clear();
346                 return 0;
347         }
348
349         int n = 0;
350         int size = 1; /* Account for initial +/- char */
351         int nextsize = 0;
352         result.clear();
353         result.push_back(adding ? "+" : "-");
354
355         if (sequence.size() > 1)
356                 nextsize = sequence[1].length() + 2;
357
358         while (!sequence[0].empty() && (sequence.size() > 1) && (result.size() < MAXMODES+1) && ((size + nextsize) < max_line_size))
359         {
360                 result[0] += *(sequence[0].begin());
361                 if (!sequence[1].empty())
362                 {
363                         result.push_back(sequence[1]);
364                         size += nextsize; /* Account for mode character and whitespace */
365                 }
366                 sequence[0].erase(sequence[0].begin());
367                 sequence.erase(sequence.begin() + 1);
368
369                 if (sequence.size() > 1)
370                         nextsize = sequence[1].length() + 2;
371
372                 n++;
373         }
374
375         return n;
376 }
377
378 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end)
379 {
380         for (int v = begin; v < end; v++)
381                 joined.append(sequence[v]).append(seperator);
382         joined.append(sequence[end]);
383 }
384
385 irc::stringjoiner::stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end)
386 {
387         for (int v = begin; v < end; v++)
388                 joined.append(sequence[v]).append(seperator);
389         joined.append(sequence[end]);
390 }
391
392 irc::stringjoiner::stringjoiner(const std::string &seperator, const char** sequence, int begin, int end)
393 {
394         for (int v = begin; v < end; v++)
395                 joined.append(sequence[v]).append(seperator);
396         joined.append(sequence[end]);
397 }
398
399 std::string& irc::stringjoiner::GetJoined()
400 {
401         return joined;
402 }
403
404 irc::portparser::portparser(const std::string &source, bool allow_overlapped) : in_range(0), range_begin(0), range_end(0), overlapped(allow_overlapped)
405 {
406         sep = new irc::commasepstream(source);
407         overlap_set.clear();
408 }
409
410 irc::portparser::~portparser()
411 {
412         delete sep;
413 }
414
415 bool irc::portparser::Overlaps(long val)
416 {
417         if (!overlapped)
418                 return false;
419
420         if (overlap_set.find(val) == overlap_set.end())
421         {
422                 overlap_set[val] = true;
423                 return false;
424         }
425         else
426                 return true;
427 }
428
429 long irc::portparser::GetToken()
430 {
431         if (in_range > 0)
432         {
433                 in_range++;
434                 if (in_range <= range_end)
435                 {
436                         if (!Overlaps(in_range))
437                         {
438                                 return in_range;
439                         }
440                         else
441                         {
442                                 while (((Overlaps(in_range)) && (in_range <= range_end)))
443                                         in_range++;
444                                 
445                                 if (in_range <= range_end)
446                                         return in_range;
447                         }
448                 }
449                 else
450                         in_range = 0;
451         }
452
453         std::string x = sep->GetToken();
454
455         if (x == "")
456                 return 0;
457
458         while (Overlaps(atoi(x.c_str())))
459         {
460                 x = sep->GetToken();
461
462                 if (x == "")
463                         return 0;
464         }
465
466         std::string::size_type dash = x.rfind('-');
467         if (dash != std::string::npos)
468         {
469                 std::string sbegin = x.substr(0, dash);
470                 std::string send = x.substr(dash+1, x.length());
471                 range_begin = atoi(sbegin.c_str());
472                 range_end = atoi(send.c_str());
473
474                 if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
475                 {
476                         in_range = range_begin;
477                         return in_range;
478                 }
479                 else
480                 {
481                         /* Assume its just the one port */
482                         return atoi(sbegin.c_str());
483                 }
484         }
485         else
486         {
487                 return atoi(x.c_str());
488         }
489 }
490
491 irc::dynamicbitmask::dynamicbitmask() : bits_size(4)
492 {
493         /* We start with 4 bytes allocated which is room
494          * for 4 items. Something makes me doubt its worth
495          * allocating less than 4 bytes.
496          */
497         bits = new unsigned char[bits_size];
498         memset(bits, 0, bits_size);
499 }
500
501 irc::dynamicbitmask::~dynamicbitmask()
502 {
503         /* Tidy up the entire used memory on delete */
504         delete[] bits;
505 }
506                           
507 irc::bitfield irc::dynamicbitmask::Allocate()
508 {
509         /* Yeah, this isnt too efficient, however a module or the core
510          * should only be allocating bitfields on load, the Toggle and
511          * Get methods are O(1) as these are called much more often.
512          */
513         unsigned char* freebits = this->GetFreeBits();
514         for (unsigned char i = 0; i < bits_size; i++)
515         {
516                 /* Yes, this is right. You'll notice we terminate the  loop when !current_pos,
517                  * this is because we logic shift our bit off the end of unsigned char, and its
518                  * lost, making the loop counter 0 when we're done.
519                  */
520                 for (unsigned char current_pos = 1; current_pos; current_pos = current_pos << 1)
521                 {
522                         if (!(freebits[i] & current_pos))
523                         {
524                                 freebits[i] |= current_pos;
525                                 return std::make_pair(i, current_pos);
526                         }
527                 }
528         }
529         /* We dont have any free space left, increase by one */
530
531         if (bits_size == 255)
532                 /* Oh dear, cant grow it any further */
533                 throw std::bad_alloc();
534
535         unsigned char old_bits_size = bits_size;
536         bits_size++;
537         /* Allocate new bitfield space */
538         unsigned char* temp_bits = new unsigned char[bits_size];
539         unsigned char* temp_freebits = new unsigned char[bits_size];
540         /* Copy the old data in */
541         memcpy(temp_bits, bits, old_bits_size);
542         memcpy(temp_freebits, freebits, old_bits_size);
543         /* Delete the old data pointers */
544         delete[] bits;
545         delete[] freebits;
546         /* Swap the pointers over so now the new 
547          * pointers point to our member values
548          */
549         bits = temp_bits;
550         freebits = temp_freebits;
551         this->SetFreeBits(freebits);
552         /* Initialize the new byte on the end of
553          * the bitfields, pre-allocate the one bit
554          * for this allocation
555          */
556         bits[old_bits_size] = 0;
557         freebits[old_bits_size] = 1;
558         /* We already know where we just allocated
559          * the bitfield, so no loop needed
560          */
561         return std::make_pair(old_bits_size, 1);
562 }
563
564 bool irc::dynamicbitmask::Deallocate(irc::bitfield &pos)
565 {
566         /* We dont bother to shrink the bitfield
567          * on deallocation, the most we could do
568          * is save one byte (!) and this would cost
569          * us a loop (ugly O(n) stuff) so we just
570          * clear the bit and leave the memory
571          * claimed -- nobody will care about one
572          * byte.
573          */
574         if (pos.first < bits_size)
575         {
576                 this->GetFreeBits()[pos.first] &= ~pos.second;
577                 return true;
578         }
579         /* They gave a bitfield outside of the
580          * length of our array. BAD programmer.
581          */
582         return false;
583 }
584
585 void irc::dynamicbitmask::Toggle(irc::bitfield &pos, bool state)
586 {
587         /* Range check the value */
588         if (pos.first < bits_size)
589         {
590                 if (state)
591                         /* Set state, OR the state in */
592                         bits[pos.first] |= pos.second;
593                 else
594                         /* Clear state, AND the !state out */
595                         bits[pos.first] &= ~pos.second;
596         }
597 }
598
599 bool irc::dynamicbitmask::Get(irc::bitfield &pos)
600 {
601         /* Range check the value */
602         if (pos.first < bits_size)
603                 return (bits[pos.first] & pos.second);
604         else
605                 /* We can't return false, otherwise we can't
606                  * distinguish between failure and a cleared bit!
607                  * Our only sensible choice is to throw (ew).
608                  */
609                 throw ModuleException("irc::dynamicbitmask::Get(): Invalid bitfield, out of range");
610 }
611
612 unsigned char irc::dynamicbitmask::GetSize()
613 {
614         return bits_size;
615 }
616
617 std::string assign(const irc::string &other)
618 {
619         return other.c_str();
620 }
621
622 irc::string assign(const std::string &other)
623 {
624         return other.c_str();
625 }
626
627 std::string& trim(std::string &str)
628 {
629         std::string::size_type start = str.find_first_not_of(" ");
630         std::string::size_type end = str.find_last_not_of(" ");
631         if (start == std::string::npos || end == std::string::npos)
632                 str = "";
633         else
634                 str = str.substr(start, end-start+1);
635
636         return str;
637 }