2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
29 static const char hextable[] = "0123456789abcdef";
31 std::string BinToHex(const void* raw, size_t l)
33 const char* data = static_cast<const char*>(raw);
36 for (size_t i = 0; i < l; i++)
38 unsigned char c = data[i];
39 rv.push_back(hextable[c >> 4]);
40 rv.push_back(hextable[c & 0xF]);
45 static const char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
47 std::string BinToBase64(const std::string& data_str, const char* table, char pad)
53 uint8_t* data = (uint8_t*)data_str.data();
56 while (i + 2 < data_str.length())
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)]);
65 if (data_str.length() == i)
67 // no extra characters
69 else if (data_str.length() == i + 1)
71 buffer = data[i] << 16;
72 rv.push_back(table[0x3F & (buffer >> 18)]);
73 rv.push_back(table[0x3F & (buffer >> 12)]);
80 else if (data_str.length() == i + 2)
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)]);
92 std::string Base64ToBin(const std::string& data_str, const char* table)
99 const char* data = data_str.c_str();
103 const char* find = strchr(table, *data++);
104 if (!find || find >= table + 64)
106 buffer = (buffer << 6) | (find - table);
111 rv.push_back((buffer >> bitcount) & 0xFF);
117 bool InspIRCd::TimingSafeCompare(const std::string& one, const std::string& two)
119 if (one.length() != two.length())
122 unsigned int diff = 0;
123 for (std::string::const_iterator i = one.begin(), j = two.begin(); i != one.end(); ++i, ++j)
125 unsigned char a = static_cast<unsigned char>(*i);
126 unsigned char b = static_cast<unsigned char>(*j);
133 void TokenList::AddList(const std::string& tokenlist)
136 irc::spacesepstream tokenstream(tokenlist);
137 while (tokenstream.GetToken(token))
140 Remove(token.substr(1));
145 void TokenList::Add(const std::string& token)
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)
151 // If the token is a wildcard entry then permissive mode has been enabled.
159 // If we are in permissive mode then remove the token from the token list.
160 // Otherwise, add it to the token list.
164 tokens.insert(token);
167 void TokenList::Clear()
173 bool TokenList::Contains(const std::string& token) const
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())
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())
185 // We have the token!
189 void TokenList::Remove(const std::string& token)
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)
195 // If the token is a wildcard entry then permissive mode has been disabled.
203 // If we are in permissive mode then add the token to the token list.
204 // Otherwise, remove it from the token list.
206 tokens.insert(token);
211 std::string TokenList::ToString() const
213 std::string buffer(permissive ? "* " : "-* ");
214 buffer.append(stdalgo::string::join(tokens));
218 bool TokenList::operator==(const TokenList& other) const
220 // Both sets must be in the same mode to be equal.
221 if (permissive != other.permissive)
224 // Both sets must be the same size to be equal.
225 if (tokens.size() != other.tokens.size())
228 for (insp::flat_set<std::string>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
230 // Both sets must contain the same tokens to be equal.
231 if (other.tokens.find(*iter) == other.tokens.end())