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