2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
25 * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. The name of the author may not be used to endorse or promote products
37 * derived from this software without specific prior written permission.
39 * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
40 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
41 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
42 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
43 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
44 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
45 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
46 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
47 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
48 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 CoreExport size_t strlcat(char *dst, const char *src, size_t siz)
58 while (n-- != 0 && *d != '\0')
65 return(dlen + strlen(s));
79 return(dlen + (s - src)); /* count does not include NUL */
82 CoreExport size_t strlcpy(char *dst, const char *src, size_t siz)
88 /* Copy as many bytes as will fit */
89 if (n != 0 && --n != 0)
93 if ((*d++ = *s++) == 0)
98 /* Not enough room in dst, add NUL and traverse rest of src */
102 *d = '\0'; /* NUL-terminate dst */
106 return(s - src - 1); /* count does not include NUL */
110 CoreExport int charlcat(char* x,char y,int z)
127 CoreExport bool charremove(char* mp, char remove)
130 bool shift_down = false;
146 static const char hextable[] = "0123456789abcdef";
148 std::string BinToHex(const std::string& data)
150 int l = data.length();
153 for(int i=0; i < l; i++)
155 unsigned char c = data[i];
156 rv.append(1, hextable[c >> 4]);
157 rv.append(1, hextable[c & 0xF]);
162 static const char b64table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
164 std::string BinToBase64(const std::string& data_str, const char* table, char pad)
170 uint8_t* data = (uint8_t*)data_str.data();
173 while (i + 2 < data_str.length())
175 buffer = (data[i] << 16 | data[i+1] << 8 | data[i+2]);
176 rv.push_back(table[0x3F & (buffer >> 18)]);
177 rv.push_back(table[0x3F & (buffer >> 12)]);
178 rv.push_back(table[0x3F & (buffer >> 6)]);
179 rv.push_back(table[0x3F & (buffer >> 0)]);
182 if (data_str.length() == i)
184 // no extra characters
186 else if (data_str.length() == i + 1)
188 buffer = data[i] << 16;
189 rv.push_back(table[0x3F & (buffer >> 18)]);
190 rv.push_back(table[0x3F & (buffer >> 12)]);
197 else if (data_str.length() == i + 2)
199 buffer = (data[i] << 16 | data[i+1] << 8);
200 rv.push_back(table[0x3F & (buffer >> 18)]);
201 rv.push_back(table[0x3F & (buffer >> 12)]);
202 rv.push_back(table[0x3F & (buffer >> 6)]);
209 std::string Base64ToBin(const std::string& data_str, const char* table)
216 const char* data = data_str.c_str();
220 const char* find = strchr(table, *data++);
221 if (!find || find >= table + 64)
223 buffer = (buffer << 6) | (find - table);
228 rv.push_back((buffer >> bitcount) & 0xFF);