1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
20 /*******************************************************
21 * This file contains classes and templates that deal
22 * with the comparison and hashing of 'irc strings'.
23 * An 'irc string' is a string which compares in a
24 * case insensitive manner, and as per RFC 1459 will
25 * treat [ identical to {, ] identical to }, and \
28 * Our hashing functions are designed to accept
29 * std::string and compare/hash them as type irc::string
30 * by converting them internally. This makes them
31 * backwards compatible with other code which is not
32 * aware of irc::string.
33 *******************************************************/
38 /** A mapping of uppercase to lowercase, including scandinavian
39 * 'oddities' as specified by RFC1459, e.g. { -> [, and | -> \
41 unsigned const char rfc_case_insensitive_map[256] = {
42 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 0-19 */
43 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, /* 20-39 */
44 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, /* 40-59 */
45 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, /* 60-79 */
46 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 94, 95, 96, 97, 98, 99, /* 80-99 */
47 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, /* 100-119 */
48 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, /* 120-139 */
49 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, /* 140-159 */
50 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, /* 160-179 */
51 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, /* 180-199 */
52 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, /* 200-219 */
53 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, /* 220-239 */
54 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 /* 240-255 */
57 /** Case insensitive map, ASCII rules.
61 unsigned const char ascii_case_insensitive_map[256] = {
62 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 0-19 */
63 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, /* 20-39 */
64 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, /* 40-59 */
65 60, 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, /* 60-79 */
66 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, /* 80-99 */
67 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, /* 100-119 */
68 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, /* 120-139 */
69 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, /* 140-159 */
70 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, /* 160-179 */
71 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, /* 180-199 */
72 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, /* 200-219 */
73 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, /* 220-239 */
74 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 /* 240-255 */
77 /** Case sensitive map.
78 * Can technically also be used for ASCII case sensitive comparisons, as [ != {, etc.
80 unsigned const char rfc_case_sensitive_map[256] = {
81 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
82 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
83 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
84 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
85 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
86 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
87 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140,
88 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160,
89 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180,
90 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200,
91 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
92 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
93 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
98 /** The irc namespace contains a number of helper classes.
103 /** This class returns true if two strings match.
104 * Case sensitivity is ignored, and the RFC 'character set'
109 /** The operator () does the actual comparison in hash_map
111 bool operator()(const std::string& s1, const std::string& s2) const;
114 /** The irc_char_traits class is used for RFC-style comparison of strings.
115 * This class is used to implement irc::string, a case-insensitive, RFC-
116 * comparing string class.
118 struct irc_char_traits : std::char_traits<char> {
120 /** Check if two chars match.
121 * @param c1st First character
122 * @param c2nd Second character
123 * @return true if the characters are equal
125 static bool eq(char c1st, char c2nd);
127 /** Check if two chars do NOT match.
128 * @param c1st First character
129 * @param c2nd Second character
130 * @return true if the characters are unequal
132 static bool ne(char c1st, char c2nd);
134 /** Check if one char is less than another.
135 * @param c1st First character
136 * @param c2nd Second character
137 * @return true if c1st is less than c2nd
139 static bool lt(char c1st, char c2nd);
141 /** Compare two strings of size n.
142 * @param str1 First string
143 * @param str2 Second string
144 * @param n Length to compare to
145 * @return similar to strcmp, zero for equal, less than zero for str1
146 * being less and greater than zero for str1 being greater than str2.
148 static CoreExport int compare(const char* str1, const char* str2, size_t n);
150 /** Find a char within a string up to position n.
151 * @param s1 String to find in
152 * @param n Position to search up to
153 * @param c Character to search for
154 * @return Pointer to the first occurance of c in s1
156 static CoreExport const char* find(const char* s1, int n, char c);
159 /** Compose a hex string from raw data.
160 * @param raw The raw data to compose hex from
161 * @pram rawsz The size of the raw data buffer
162 * @return The hex string.
164 CoreExport std::string hex(const unsigned char *raw, size_t rawsz);
166 /** This typedef declares irc::string based upon irc_char_traits.
168 typedef std::basic_string<char, irc_char_traits, std::allocator<char> > string;
170 /** irc::stringjoiner joins string lists into a string, using
171 * the given seperator string.
172 * This class can join a vector of std::string, a deque of
173 * std::string, or a const char* const* array, using overloaded
176 class CoreExport stringjoiner : public classbase
186 /** Join elements of a vector, between (and including) begin and end
187 * @param seperator The string to seperate values with
188 * @param sequence One or more items to seperate
189 * @param begin The starting element in the sequence to be joined
190 * @param end The ending element in the sequence to be joined
192 stringjoiner(const std::string &seperator, const std::vector<std::string> &sequence, int begin, int end);
194 /** Join elements of a deque, between (and including) begin and end
195 * @param seperator The string to seperate values with
196 * @param sequence One or more items to seperate
197 * @param begin The starting element in the sequence to be joined
198 * @param end The ending element in the sequence to be joined
200 stringjoiner(const std::string &seperator, const std::deque<std::string> &sequence, int begin, int end);
202 /** Join elements of an array of char arrays, between (and including) begin and end
203 * @param seperator The string to seperate values with
204 * @param sequence One or more items to seperate
205 * @param begin The starting element in the sequence to be joined
206 * @param end The ending element in the sequence to be joined
208 stringjoiner(const std::string &seperator, const char* const* sequence, int begin, int end);
210 /** Get the joined sequence
211 * @return A reference to the joined string
213 std::string& GetJoined();
216 /** irc::modestacker stacks mode sequences into a list.
217 * It can then reproduce this list, clamped to a maximum of MAXMODES
220 class CoreExport modestacker : public classbase
223 InspIRCd* ServerInstance;
225 /** The mode sequence and its parameters
227 std::deque<std::string> sequence;
229 /** True if the mode sequence is initially adding
230 * characters, false if it is initially removing
236 /** Construct a new modestacker.
237 * @param add True if the stack is adding modes,
238 * false if it is removing them
240 modestacker(InspIRCd* Instance, bool add);
242 /** Push a modeletter and its parameter onto the stack.
243 * No checking is performed as to if this mode actually
244 * requires a parameter. If you stack invalid mode
245 * sequences, they will be tidied if and when they are
246 * passed to a mode parser.
247 * @param modeletter The mode letter to insert
248 * @param parameter The parameter for the mode
250 void Push(char modeletter, const std::string ¶meter);
252 /** Push a modeletter without parameter onto the stack.
253 * No checking is performed as to if this mode actually
254 * requires a parameter. If you stack invalid mode
255 * sequences, they will be tidied if and when they are
256 * passed to a mode parser.
257 * @param modeletter The mode letter to insert
259 void Push(char modeletter);
261 /** Push a '+' symbol onto the stack.
265 /** Push a '-' symbol onto the stack.
269 /** Return zero or more elements which form the
270 * mode line. This will be clamped to a max of
271 * MAXMODES items (MAXMODES-1 mode parameters and
272 * one mode sequence string), and max_line_size
273 * characters. As specified below, this function
274 * should be called in a loop until it returns zero,
275 * indicating there are no more modes to return.
276 * @param result The deque to populate. This will
277 * be cleared before it is used.
278 * @param max_line_size The maximum size of the line
279 * to build, in characters, seperate to MAXMODES.
280 * @return The number of elements in the deque.
281 * The function should be called repeatedly until it
282 * returns 0, in case there are multiple lines of
283 * mode changes to be obtained.
285 int GetStackedLine(std::deque<std::string> &result, int max_line_size = 360);
288 /** irc::tokenstream reads a string formatted as per RFC1459 and RFC2812.
289 * It will split the string into 'tokens' each containing one parameter
291 * For instance, if it is instantiated with the string:
292 * "PRIVMSG #test :foo bar baz qux"
293 * then each successive call to tokenstream::GetToken() will return
294 * "PRIVMSG", "#test", "foo bar baz qux", "".
295 * Note that if the whole string starts with a colon this is not taken
296 * to mean the string is all one parameter, and the first item in the
297 * list will be ":item". This is to allow for parsing 'source' fields
300 class CoreExport tokenstream : public classbase
308 /** Last position of a seperator token
310 std::string::iterator last_starting_position;
312 /** Current string position
314 std::string::iterator n;
316 /** True if the last value was an ending value
321 /** Create a tokenstream and fill it with the provided data
323 tokenstream(const std::string &source);
329 /** Fetch the next token from the stream as a std::string
330 * @param token The next token available, or an empty string if none remain
331 * @return True if tokens are left to be read, false if the last token was just retrieved.
333 bool GetToken(std::string &token);
335 /** Fetch the next token from the stream as an irc::string
336 * @param token The next token available, or an empty string if none remain
337 * @return True if tokens are left to be read, false if the last token was just retrieved.
339 bool GetToken(irc::string &token);
341 /** Fetch the next token from the stream as an integer
342 * @param token The next token available, or undefined if none remain
343 * @return True if tokens are left to be read, false if the last token was just retrieved.
345 bool GetToken(int &token);
347 /** Fetch the next token from the stream as a long integer
348 * @param token The next token available, or undefined if none remain
349 * @return True if tokens are left to be read, false if the last token was just retrieved.
351 bool GetToken(long &token);
354 /** irc::sepstream allows for splitting token seperated lists.
355 * Each successive call to sepstream::GetToken() returns
356 * the next token, until none remain, at which point the method returns
359 class CoreExport sepstream : public classbase
365 /** Last position of a seperator token
367 std::string::iterator last_starting_position;
368 /** Current string position
370 std::string::iterator n;
375 /** Create a sepstream and fill it with the provided data
377 sepstream(const std::string &source, char seperator);
381 virtual ~sepstream();
383 /** Fetch the next token from the stream
384 * @param token The next token from the stream is placed here
385 * @return True if tokens still remain, false if there are none left
387 virtual bool GetToken(std::string &token);
389 /** Fetch the entire remaining stream, without tokenizing
390 * @return The remaining part of the stream
392 virtual const std::string GetRemaining();
394 /** Returns true if the end of the stream has been reached
395 * @return True if the end of the stream has been reached, otherwise false
397 virtual bool StreamEnd();
400 /** A derived form of sepstream, which seperates on commas
402 class CoreExport commasepstream : public sepstream
405 /** Initialize with comma seperator
407 commasepstream(const std::string &source) : sepstream(source, ',')
412 /** A derived form of sepstream, which seperates on spaces
414 class CoreExport spacesepstream : public sepstream
417 /** Initialize with space seperator
419 spacesepstream(const std::string &source) : sepstream(source, ' ')
424 /** The portparser class seperates out a port range into integers.
425 * A port range may be specified in the input string in the form
426 * "6660,6661,6662-6669,7020". The end of the stream is indicated by
427 * a return value of 0 from portparser::GetToken(). If you attempt
428 * to specify an illegal range (e.g. one where start >= end, or
429 * start or end < 0) then GetToken() will return the first element
430 * of the pair of numbers.
432 class CoreExport portparser : public classbase
436 /** Used to split on commas
440 /** Current position in a range of ports
444 /** Starting port in a range of ports
448 /** Ending port in a range of ports
452 /** Allow overlapped port ranges
456 /** Used to determine overlapping of ports
457 * without O(n) algorithm being used
459 std::map<long, bool> overlap_set;
461 /** Returns true if val overlaps an existing range
463 bool Overlaps(long val);
466 /** Create a portparser and fill it with the provided data
467 * @param source The source text to parse from
468 * @param allow_overlapped Allow overlapped ranges
470 portparser(const std::string &source, bool allow_overlapped = true);
472 /** Frees the internal commasepstream object
476 /** Fetch the next token from the stream
477 * @return The next port number is returned, or 0 if none remain
482 /** Turn _ characters in a string into spaces
483 * @param n String to translate
484 * @return The new value with _ translated to space.
486 CoreExport const char* Spacify(const char* n);
489 /* Define operators for using >> and << with irc::string to an ostream on an istream. */
490 /* This was endless fun. No. Really. */
491 /* It was also the first core change Ommeh made, if anyone cares */
493 /** Operator << for irc::string
495 inline std::ostream& operator<<(std::ostream &os, const irc::string &str) { return os << str.c_str(); }
497 /** Operator >> for irc::string
499 inline std::istream& operator>>(std::istream &is, irc::string &str)
507 /* Define operators for + and == with irc::string to std::string for easy assignment
512 inline std::string operator+ (std::string& leftval, irc::string& rightval)
514 return leftval + std::string(rightval.c_str());
517 /* Define operators for + and == with irc::string to std::string for easy assignment
522 inline irc::string operator+ (irc::string& leftval, std::string& rightval)
524 return leftval + irc::string(rightval.c_str());
527 /* Define operators for + and == with irc::string to std::string for easy assignment
532 inline bool operator== (const std::string& leftval, const irc::string& rightval)
534 return (leftval.c_str() == rightval);
537 /* Define operators for + and == with irc::string to std::string for easy assignment
542 inline bool operator== (const irc::string& leftval, const std::string& rightval)
544 return (leftval == rightval.c_str());
547 /* Define operators != for irc::string to std::string for easy comparison
549 inline bool operator!= (const irc::string& leftval, const std::string& rightval)
551 return !(leftval == rightval.c_str());
554 /* Define operators != for std::string to irc::string for easy comparison
556 inline bool operator!= (const std::string& leftval, const irc::string& rightval)
558 return !(leftval.c_str() == rightval);
561 // FIXME MAXBUF messes up these
563 template<std::size_t N>
564 static inline bool operator == (std::string const &lhs, char const (&rhs)[N])
566 return lhs.length() == N - 1 && !std::memcmp(lhs.data(), rhs, N - 1);
569 template<std::size_t N>
570 static inline bool operator != (std::string const &lhs, char const (&rhs)[N])
572 return !(lhs == rhs);
576 /** Assign an irc::string to a std::string.
578 inline std::string assign(const irc::string &other) { return other.c_str(); }
580 /** Assign a std::string to an irc::string.
582 inline irc::string assign(const std::string &other) { return other.c_str(); }
584 /** Trim the leading and trailing spaces from a std::string.
586 inline std::string& trim(std::string &str)
588 std::string::size_type start = str.find_first_not_of(" ");
589 std::string::size_type end = str.find_last_not_of(" ");
590 if (start == std::string::npos || end == std::string::npos)
593 str = str.substr(start, end-start+1);
598 /** Hashing stuff is totally different on vc++'s hash_map implementation, so to save a buttload of
599 * #ifdefs we'll just do it all at once
601 BEGIN_HASHMAP_NAMESPACE
603 /** Hashing function to hash irc::string
606 template<> class CoreExport hash_compare<irc::string, std::less<irc::string> >
609 enum { bucket_size = 4, min_buckets = 8 }; /* Got these numbers from the CRT source, if anyone wants to change them feel free. */
611 /** Compare two irc::string values for hashing in hash_map
613 bool operator()(const irc::string & s1, const irc::string & s2) const
615 if(s1.length() != s2.length()) return true;
616 return (irc::irc_char_traits::compare(s1.c_str(), s2.c_str(), (size_t)s1.length()) < 0);
619 /** Hash an irc::string value for hash_map
621 size_t operator()(const irc::string & s) const;
624 template<> class CoreExport hash_compare<std::string, std::less<std::string> >
627 enum { bucket_size = 4, min_buckets = 8 }; /* Again, from the CRT source */
629 /** Compare two std::string values for hashing in hash_map
631 bool operator()(const std::string & s1, const std::string & s2) const
633 if(s1.length() != s2.length()) return true;
634 return (irc::irc_char_traits::compare(s1.c_str(), s2.c_str(), (size_t)s1.length()) < 0);
637 /** Hash a std::string using RFC1459 case sensitivity rules
638 * @param s A string to hash
639 * @return The hash value
641 size_t operator()(const std::string & s) const;
645 template<> struct hash<irc::string>
647 /** Hash an irc::string using RFC1459 case sensitivity rules
648 * @param s A string to hash
649 * @return The hash value
651 size_t operator()(const irc::string &s) const;
654 /* XXX FIXME: Implement a hash function overriding std::string's that works with TR1! */
656 #ifdef HASHMAP_DEPRECATED
659 template<> struct hash<std::string>
662 size_t operator()(const std::string &s) const;
667 /** Convert a string to lower case respecting RFC1459
668 * @param n A string to lowercase
670 void strlower(char *n);
672 END_HASHMAP_NAMESPACE