]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspstring.cpp
Convert the ISO 8859-2 nationalchars files to codepage configs.
[user/henk/code/inspircd.git] / src / inspstring.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2018 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2014 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28
29 static const char hextable[] = "0123456789abcdef";
30
31 std::string BinToHex(const void* raw, size_t l)
32 {
33         const char* data = static_cast<const char*>(raw);
34         std::string rv;
35         rv.reserve(l * 2);
36         for (size_t i = 0; i < l; i++)
37         {
38                 unsigned char c = data[i];
39                 rv.push_back(hextable[c >> 4]);
40                 rv.push_back(hextable[c & 0xF]);
41         }
42         return rv;
43 }
44
45 static const char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46
47 std::string BinToBase64(const std::string& data_str, const char* table, char pad)
48 {
49         if (!table)
50                 table = b64table;
51
52         uint32_t buffer;
53         uint8_t* data = (uint8_t*)data_str.data();
54         std::string rv;
55         size_t i = 0;
56         while (i + 2 < data_str.length())
57         {
58                 buffer = (data[i] << 16 | data[i+1] << 8 | data[i+2]);
59                 rv.push_back(table[0x3F & (buffer >> 18)]);
60                 rv.push_back(table[0x3F & (buffer >> 12)]);
61                 rv.push_back(table[0x3F & (buffer >>  6)]);
62                 rv.push_back(table[0x3F & (buffer >>  0)]);
63                 i += 3;
64         }
65         if (data_str.length() == i)
66         {
67                 // no extra characters
68         }
69         else if (data_str.length() == i + 1)
70         {
71                 buffer = data[i] << 16;
72                 rv.push_back(table[0x3F & (buffer >> 18)]);
73                 rv.push_back(table[0x3F & (buffer >> 12)]);
74                 if (pad)
75                 {
76                         rv.push_back(pad);
77                         rv.push_back(pad);
78                 }
79         }
80         else if (data_str.length() == i + 2)
81         {
82                 buffer = (data[i] << 16 | data[i+1] << 8);
83                 rv.push_back(table[0x3F & (buffer >> 18)]);
84                 rv.push_back(table[0x3F & (buffer >> 12)]);
85                 rv.push_back(table[0x3F & (buffer >>  6)]);
86                 if (pad)
87                         rv.push_back(pad);
88         }
89         return rv;
90 }
91
92 std::string Base64ToBin(const std::string& data_str, const char* table)
93 {
94         if (!table)
95                 table = b64table;
96
97         int bitcount = 0;
98         uint32_t buffer = 0;
99         const char* data = data_str.c_str();
100         std::string rv;
101         while (true)
102         {
103                 const char* find = strchr(table, *data++);
104                 if (!find || find >= table + 64)
105                         break;
106                 buffer = (buffer << 6) | (find - table);
107                 bitcount += 6;
108                 if (bitcount >= 8)
109                 {
110                         bitcount -= 8;
111                         rv.push_back((buffer >> bitcount) & 0xFF);
112                 }
113         }
114         return rv;
115 }
116
117 bool InspIRCd::TimingSafeCompare(const std::string& one, const std::string& two)
118 {
119         if (one.length() != two.length())
120                 return false;
121
122         unsigned int diff = 0;
123         for (std::string::const_iterator i = one.begin(), j = two.begin(); i != one.end(); ++i, ++j)
124         {
125                 unsigned char a = static_cast<unsigned char>(*i);
126                 unsigned char b = static_cast<unsigned char>(*j);
127                 diff |= a ^ b;
128         }
129
130         return (diff == 0);
131 }
132
133 void TokenList::AddList(const std::string& tokenlist)
134 {
135         std::string token;
136         irc::spacesepstream tokenstream(tokenlist);
137         while (tokenstream.GetToken(token))
138         {
139                 if (token[0] == '-')
140                         Remove(token.substr(1));
141                 else
142                         Add(token);
143         }
144 }
145 void TokenList::Add(const std::string& token)
146 {
147         // If the token is empty or contains just whitespace it is invalid.
148         if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
149                 return;
150
151         // If the token is a wildcard entry then permissive mode has been enabled.
152         if (token == "*")
153         {
154                 permissive = true;
155                 tokens.clear();
156                 return;
157         }
158
159         // If we are in permissive mode then remove the token from the token list.
160         // Otherwise, add it to the token list.
161         if (permissive)
162                 tokens.erase(token);
163         else
164                 tokens.insert(token);
165 }
166
167 void TokenList::Clear()
168 {
169         permissive = false;
170         tokens.clear();
171 }
172
173 bool TokenList::Contains(const std::string& token) const
174 {
175         // If we are in permissive mode and the token is in the list
176         // then we don't have it.
177         if (permissive && tokens.find(token) != tokens.end())
178                 return false;
179
180         // If we are not in permissive mode and the token is not in
181         // the list then we don't have it.
182         if (!permissive && tokens.find(token) == tokens.end())
183                 return false;
184
185         // We have the token!
186         return true;
187 }
188
189 void TokenList::Remove(const std::string& token)
190 {
191         // If the token is empty or contains just whitespace it is invalid.
192         if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
193                 return;
194
195         // If the token is a wildcard entry then permissive mode has been disabled.
196         if (token == "*")
197         {
198                 permissive = false;
199                 tokens.clear();
200                 return;
201         }
202
203         // If we are in permissive mode then add the token to the token list.
204         // Otherwise, remove it from the token list.
205         if (permissive)
206                 tokens.insert(token);
207         else
208                 tokens.erase(token);
209 }
210
211 std::string TokenList::ToString() const
212 {
213         std::string buffer(permissive ? "* " : "-* ");
214         buffer.append(stdalgo::string::join(tokens));
215         return buffer;
216 }
217
218 bool TokenList::operator==(const TokenList& other) const
219 {
220         // Both sets must be in the same mode to be equal.
221         if (permissive != other.permissive)
222                 return false;
223
224         // Both sets must be the same size to be equal.
225         if (tokens.size() != other.tokens.size())
226                 return false;
227
228         for (insp::flat_set<std::string>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
229         {
230                 // Both sets must contain the same tokens to be equal.
231                 if (other.tokens.find(*iter) == other.tokens.end())
232                         return false;
233         }
234
235         return true;
236 }