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