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