]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspstring.cpp
Merge branch 'insp20' into master.
[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 }
141 void TokenList::Add(const std::string& token)
142 {
143         // If the token is empty or contains just whitespace it is invalid.
144         if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
145                 return;
146
147         // If the token is a wildcard entry then permissive mode has been enabled.
148         if (token == "*")
149         {
150                 permissive = true;
151                 tokens.clear();
152                 return;
153         }
154
155         // If we are in permissive mode then remove the token from the token list.
156         // Otherwise, add it to the token list.
157         if (permissive)
158                 tokens.erase(token);
159         else
160                 tokens.insert(token);
161 }
162
163 void TokenList::Clear()
164 {
165         permissive = false;
166         tokens.clear();
167 }
168
169 bool TokenList::Contains(const std::string& token) const
170 {
171         // If we are in permissive mode and the token is in the list
172         // then we don't have it.
173         if (permissive && tokens.find(token) != tokens.end())
174                 return false;
175
176         // If we are not in permissive mode and the token is not in
177         // the list then we don't have it.
178         if (!permissive && tokens.find(token) == tokens.end())
179                 return false;
180
181         // We have the token!
182         return true;
183 }
184
185 void TokenList::Remove(const std::string& token)
186 {
187         // If the token is empty or contains just whitespace it is invalid.
188         if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
189                 return;
190
191         // If the token is a wildcard entry then permissive mode has been disabled.
192         if (token == "*")
193         {
194                 permissive = false;
195                 tokens.clear();
196                 return;
197         }
198
199         // If we are in permissive mode then add the token to the token list.
200         // Otherwise, remove it from the token list.
201         if (permissive)
202                 tokens.insert(token);
203         else
204                 tokens.erase(token);
205 }
206
207 std::string TokenList::ToString() const
208 {
209         std::string buffer(permissive ? "*" : "-*");
210         for (insp::flat_set<std::string>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
211         {
212                 buffer.push_back(' ');
213                 buffer.append(*iter);
214         }
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 }