]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspstring.cpp
Merge insp20
[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 }