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