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