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