InspIRCd  4.0
inspstring.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2013, 2018 Sadie Powell <[email protected]>
5  * Copyright (C) 2013 Daniel Vassdal <[email protected]>
6  * Copyright (C) 2013 Attila Molnar <[email protected]>
7  * Copyright (C) 2012 Robby <[email protected]>
8  * Copyright (C) 2010 Daniel De Graaf <[email protected]>
9  * Copyright (C) 2008 Robin Burchell <[email protected]>
10  * Copyright (C) 2008 Pippijn van Steenhoven <[email protected]>
11  * Copyright (C) 2007, 2010 Craig Edwards <[email protected]>
12  * Copyright (C) 2007 Dennis Friis <[email protected]>
13  *
14  * This file is part of InspIRCd. InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 
28 #pragma once
29 
30 #include <cstring>
31 
33 #define VAFORMAT(ret, last, format) \
34  do { \
35  va_list _vaList; \
36  va_start(_vaList, last); \
37  ret.assign(InspIRCd::Format(_vaList, format)); \
38  va_end(_vaList); \
39  } while (false)
40 
41 
42 namespace Base64
43 {
45  inline const char* TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46 
53  CoreExport std::string Decode(const void* data, size_t length, const char* table = nullptr);
54 
60  inline std::string Decode(const std::string& data, const char* table = nullptr)
61  {
62  return Decode(data.c_str(), data.length(), table);
63  }
64 
72  CoreExport std::string Encode(const void* data, size_t length, const char* table = nullptr, char padding = 0);
73 
80  inline std::string Encode(const std::string& data, const char* table = nullptr, char padding = 0)
81  {
82  return Encode(data.c_str(), data.length(), table, padding);
83  }
84 }
85 
86 namespace Hex
87 {
89  inline const char* TABLE_LOWER = "0123456789abcdef";
90 
92  inline const char* TABLE_UPPER = "0123456789ABCDEF";
93 
101  CoreExport std::string Encode(const void* data, size_t length, const char* table = nullptr, char separator = 0);
102 
109  inline std::string Encode(const std::string& data, const char* table = nullptr, char separator = 0)
110  {
111  return Encode(data.c_str(), data.length(), table);
112  }
113 }
114 
115 namespace Percent
116 {
118  inline const char* TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~";
119 
125  CoreExport std::string Decode(const void* data, size_t length);
126 
132  inline std::string Decode(const std::string& data)
133  {
134  return Decode(data.c_str(), data.length());
135  }
136 
144  CoreExport std::string Encode(const void* data, size_t length, const char* table = nullptr, char padding = 0);
145 
152  inline std::string Encode(const std::string& data, const char* table = nullptr, char padding = 0)
153  {
154  return Encode(data.c_str(), data.length(), table, padding);
155  }
156 }