]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/hashcomp.h
Merge pull request #677 from Robby-/master-dnsblzline
[user/henk/code/inspircd.git] / include / hashcomp.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2011 Adam <Adam@anope.org>
5  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2005-2009 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #pragma once
26
27 #include <cstring>
28 #include <string>
29 #include <vector>
30 #include <deque>
31 #include <map>
32 #include <set>
33 #include "inspircd.h"
34
35 /*******************************************************
36  * This file contains classes and templates that deal
37  * with the comparison and hashing of 'irc strings'.
38  * An 'irc string' is a string which compares in a
39  * case insensitive manner, and as per RFC 1459 will
40  * treat [ identical to {, ] identical to }, and \
41  * as identical to |.
42  *
43  * There are functors that accept std::string and
44  * compare/hash them as type irc::string by using
45  * mapping arrays internally.
46  *******************************************************/
47
48 /** Seperate from the other casemap tables so that code *can* still exclusively rely on RFC casemapping
49  * if it must.
50  *
51  * This is provided as a pointer so that modules can change it to their custom mapping tables,
52  * e.g. for national character support.
53  */
54 CoreExport extern unsigned const char *national_case_insensitive_map;
55
56 /** A mapping of uppercase to lowercase, including scandinavian
57  * 'oddities' as specified by RFC1459, e.g. { -> [, and | -> \
58  */
59 CoreExport extern unsigned const char rfc_case_insensitive_map[256];
60
61 /** Case insensitive map, ASCII rules.
62  * That is;
63  * [ != {, but A == a.
64  */
65 CoreExport extern unsigned const char ascii_case_insensitive_map[256];
66
67 /** Case sensitive (identity) map.
68  */
69 CoreExport extern unsigned const char rfc_case_sensitive_map[256];
70
71 /** The irc namespace contains a number of helper classes.
72  */
73 namespace irc
74 {
75         /** Check if two IRC object (e.g. nick or channel) names are equal.
76          * This function uses national_case_insensitive_map to determine equality, which, by default does comparison
77          * according to RFC 1459, treating certain otherwise non-identical characters as identical.
78          * @param s1 First string to compare
79          * @param s2 Second string to compare
80          * @return True if the two names are equal, false otherwise
81          */
82         CoreExport bool equals(const std::string& s1, const std::string& s2);
83
84         /** This class returns true if two strings match.
85          * Case sensitivity is ignored, and the RFC 'character set'
86          * is adhered to
87          */
88         struct StrHashComp
89         {
90                 /** The operator () does the actual comparison in hash_map
91                  */
92                 bool operator()(const std::string& s1, const std::string& s2) const
93                 {
94                         return equals(s1, s2);
95                 }
96         };
97
98         struct insensitive
99         {
100                 size_t CoreExport operator()(const std::string &s) const;
101         };
102
103         struct insensitive_swo
104         {
105                 bool CoreExport operator()(const std::string& a, const std::string& b) const;
106         };
107
108         /** The irc_char_traits class is used for RFC-style comparison of strings.
109          * This class is used to implement irc::string, a case-insensitive, RFC-
110          * comparing string class.
111          */
112         struct CoreExport irc_char_traits : public std::char_traits<char>
113         {
114                 /** Check if two chars match.
115                  * @param c1st First character
116                  * @param c2nd Second character
117                  * @return true if the characters are equal
118                  */
119                 static bool eq(char c1st, char c2nd);
120
121                 /** Check if two chars do NOT match.
122                  * @param c1st First character
123                  * @param c2nd Second character
124                  * @return true if the characters are unequal
125                  */
126                 static bool ne(char c1st, char c2nd);
127
128                 /** Check if one char is less than another.
129                  * @param c1st First character
130                  * @param c2nd Second character
131                  * @return true if c1st is less than c2nd
132                  */
133                 static bool lt(char c1st, char c2nd);
134
135                 /** Compare two strings of size n.
136                  * @param str1 First string
137                  * @param str2 Second string
138                  * @param n Length to compare to
139                  * @return similar to strcmp, zero for equal, less than zero for str1
140                  * being less and greater than zero for str1 being greater than str2.
141                  */
142                 static int compare(const char* str1, const char* str2, size_t n);
143
144                 /** Find a char within a string up to position n.
145                  * @param s1 String to find in
146                  * @param n Position to search up to
147                  * @param c Character to search for
148                  * @return Pointer to the first occurance of c in s1
149                  */
150                 static const char* find(const char* s1, int  n, char c);
151         };
152
153         /** This typedef declares irc::string based upon irc_char_traits.
154          */
155         typedef std::basic_string<char, irc_char_traits, std::allocator<char> > string;
156
157         /** Joins the contents of a vector to a string.
158          * @param sequence Zero or more items to join.
159          * @param separator The character to place between the items, defaults to ' ' (space).
160          * @return Joined string.
161          */
162         std::string CoreExport stringjoiner(const std::vector<std::string>& sequence, char separator = ' ');
163
164         /** irc::sepstream allows for splitting token seperated lists.
165          * Each successive call to sepstream::GetToken() returns
166          * the next token, until none remain, at which point the method returns
167          * false.
168          */
169         class CoreExport sepstream
170         {
171          protected:
172                 /** Original string.
173                  */
174                 std::string tokens;
175                 /** Separator value
176                  */
177                 char sep;
178                 /** Current string position
179                  */
180                 size_t pos;
181                 /** If set then GetToken() can return an empty string
182                  */
183                 bool allow_empty;
184          public:
185                 /** Create a sepstream and fill it with the provided data
186                  */
187                 sepstream(const std::string &source, char separator, bool allowempty = false);
188
189                 /** Fetch the next token from the stream
190                  * @param token The next token from the stream is placed here
191                  * @return True if tokens still remain, false if there are none left
192                  */
193                 bool GetToken(std::string& token);
194
195                 /** Fetch the entire remaining stream, without tokenizing
196                  * @return The remaining part of the stream
197                  */
198                 const std::string GetRemaining();
199
200                 /** Returns true if the end of the stream has been reached
201                  * @return True if the end of the stream has been reached, otherwise false
202                  */
203                 bool StreamEnd();
204         };
205
206         /** A derived form of sepstream, which seperates on commas
207          */
208         class CoreExport commasepstream : public sepstream
209         {
210          public:
211                 /** Initialize with comma separator
212                  */
213                 commasepstream(const std::string &source, bool allowempty = false) : sepstream(source, ',', allowempty)
214                 {
215                 }
216         };
217
218         /** A derived form of sepstream, which seperates on spaces
219          */
220         class CoreExport spacesepstream : public sepstream
221         {
222          public:
223                 /** Initialize with space separator
224                  */
225                 spacesepstream(const std::string &source, bool allowempty = false) : sepstream(source, ' ', allowempty)
226                 {
227                 }
228         };
229
230         /** irc::tokenstream reads a string formatted as per RFC1459 and RFC2812.
231          * It will split the string into 'tokens' each containing one parameter
232          * from the string.
233          * For instance, if it is instantiated with the string:
234          * "PRIVMSG #test :foo bar baz qux"
235          * then each successive call to tokenstream::GetToken() will return
236          * "PRIVMSG", "#test", "foo bar baz qux", "".
237          * Note that if the whole string starts with a colon this is not taken
238          * to mean the string is all one parameter, and the first item in the
239          * list will be ":item". This is to allow for parsing 'source' fields
240          * from data.
241          */
242         class CoreExport tokenstream : private spacesepstream
243         {
244          public:
245                 /** Create a tokenstream and fill it with the provided data
246                  */
247                 tokenstream(const std::string &source);
248
249                 /** Fetch the next token from the stream as a std::string
250                  * @param token The next token available, or an empty string if none remain
251                  * @return True if tokens are left to be read, false if the last token was just retrieved.
252                  */
253                 bool GetToken(std::string &token);
254
255                 /** Fetch the next token from the stream as an integer
256                  * @param token The next token available, or undefined if none remain
257                  * @return True if tokens are left to be read, false if the last token was just retrieved.
258                  */
259                 bool GetToken(int &token);
260
261                 /** Fetch the next token from the stream as a long integer
262                  * @param token The next token available, or undefined if none remain
263                  * @return True if tokens are left to be read, false if the last token was just retrieved.
264                  */
265                 bool GetToken(long &token);
266         };
267
268         /** The portparser class seperates out a port range into integers.
269          * A port range may be specified in the input string in the form
270          * "6660,6661,6662-6669,7020". The end of the stream is indicated by
271          * a return value of 0 from portparser::GetToken(). If you attempt
272          * to specify an illegal range (e.g. one where start >= end, or
273          * start or end < 0) then GetToken() will return the first element
274          * of the pair of numbers.
275          */
276         class CoreExport portparser
277         {
278          private:
279
280                 /** Used to split on commas
281                  */
282                 commasepstream sep;
283
284                 /** Current position in a range of ports
285                  */
286                 long in_range;
287
288                 /** Starting port in a range of ports
289                  */
290                 long range_begin;
291
292                 /** Ending port in a range of ports
293                  */
294                 long range_end;
295
296                 /** Allow overlapped port ranges
297                  */
298                 bool overlapped;
299
300                 /** Used to determine overlapping of ports
301                  * without O(n) algorithm being used
302                  */
303                 std::set<long> overlap_set;
304
305                 /** Returns true if val overlaps an existing range
306                  */
307                 bool Overlaps(long val);
308          public:
309
310                 /** Create a portparser and fill it with the provided data
311                  * @param source The source text to parse from
312                  * @param allow_overlapped Allow overlapped ranges
313                  */
314                 portparser(const std::string &source, bool allow_overlapped = true);
315
316                 /** Fetch the next token from the stream
317                  * @return The next port number is returned, or 0 if none remain
318                  */
319                 long GetToken();
320         };
321 }