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