AnCH Framework  0.1
Another C++ Hack Framework
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
base64.hpp
1 /*
2  ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3  Copyright (C) 2012 Vincent Lachenal
4 
5  This file is part of ANCH Framework.
6 
7  ANCH Framework is free software: you can redistribute it and/or modify
8  it under the terms of the GNU Lesser General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  ANCH Framework is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19 */
20 #ifndef _ANCH_CRYPTO_B2T_BASE64_H_
21 #define _ANCH_CRYPTO_B2T_BASE64_H_
22 
23 #include <iostream>
24 #include <sstream>
25 
26 #include <string.h>
27 
28 namespace anch {
29  namespace crypto {
30 
40  class Base64 {
41  // Attributes +
42  private:
44  const static char _alphabet[];
45 
47  const static char _padding;
48  // Attributes -
49 
50  // Constructors +
51  private:
55  Base64();
56  // Constructors -
57 
58  // Destructor +
59  public:
63  virtual ~Base64();
64  // Destructor -
65 
66 
67  // Encoding methods +
68  public:
75  template<class CharT, class Traits>
76  static void encode(std::basic_istream<CharT,Traits>& data,
77  std::basic_ostream<CharT,Traits>& output)
78  noexcept {
79  if(data) {
80  char buffer[1024];
81  while(!data.eof()) {
82  data.read(buffer, 1024);
83  encode(reinterpret_cast<uint8_t*>(buffer), data.gcount(), output);
84  }
85  }
86  }
87 
95  template<class CharT, class Traits>
96  static std::string encode(std::basic_istream<CharT,Traits>& data) noexcept {
97  std::ostringstream out;
98  encode(data, out);
99  return out.str();
100  }
101 
108  template<class CharT, class Traits, class Allocator>
109  static void encode(const std::basic_string<CharT,Traits,Allocator>& data,
110  std::basic_ostream<CharT,Traits>& output) noexcept {
111  encode(reinterpret_cast<const uint8_t*>(data.data()), data.length(), output);
112  }
113 
121  template<class CharT, class Traits, class Allocator>
122  static std::string encode(const std::basic_string<CharT,Traits,Allocator>& data)
123  noexcept {
124  std::ostringstream out;
125  encode(data, out);
126  return out.str();
127  }
128 
136  template<class CharT, class Traits>
137  static void encode(const uint8_t* data, uint64_t length,
138  std::basic_ostream<CharT,Traits>& output)
139  noexcept {
140  char buffer[5];
141  buffer[4] = '\0';
142  while(length > 0) {
143  uint8_t read = encode(data, length, buffer);
144  output << buffer;
145  data += read;
146  length -= read;
147  }
148  }
149 
158  static std::string encode(const uint8_t* data, uint64_t length) noexcept {
159  std::ostringstream out;
160  encode(data, length, out);
161  return out.str();
162  }
163 
164  private:
174  static uint8_t encode(const uint8_t* data, uint64_t length, char buffer[5]) noexcept;
175  // Encoding methods -
176 
177 
178  // Decoding methods +
179  public:
186  template<class CharT, class Traits>
187  static void decode(std::istream& data,
188  std::basic_ostream<CharT,Traits>& output)
189  noexcept {
190  if(data) {
191  char buffer[1024];
192  while(!data.eof()) {
193  data.read(buffer, 1024);
194  decode(buffer, data.gcount(), output);
195  }
196  }
197  }
198 
206  static std::string decode(std::istream& data) noexcept {
207  std::ostringstream out;
208  decode(data, out);
209  return out.str();
210  }
211 
218  template<class CharT, class Traits>
219  static void decode(const std::string& data,
220  std::basic_ostream<CharT,Traits>& output) noexcept {
221  decode(data.data(), data.length(), output);
222  }
223 
231  static std::string decode(const std::string& data)
232  noexcept {
233  std::ostringstream out;
234  decode(data, out);
235  return out.str();
236  }
237 
245  template<class CharT, class Traits>
246  static void decode(const char* data, uint64_t length,
247  std::basic_ostream<CharT,Traits>& output)
248  noexcept {
249  uint8_t buffer[4];
250  buffer[3] = '\0';
251  while(length > 0) {
252  memset(buffer, '\0', 3);
253  decode(data, length, buffer);
254  output << buffer;
255  data += 4;
256  length -= 4;
257  }
258  }
259 
268  static std::string decode(const char* data, uint64_t length)
269  noexcept {
270  std::ostringstream out;
271  decode(data, length, out);
272  return out.str();
273  }
274 
275  private:
285  static void decode(const char* data, uint64_t length, uint8_t buffer[4]) noexcept;
286  // Decoding methods -
287 
288  };
289 
290  }
291 }
292 
293 #endif // _ANCH_CRYPTO_B2T_BASE64_H_
static std::string encode(const uint8_t *data, uint64_t length) noexcept
Definition: base64.hpp:158
static void encode(const uint8_t *data, uint64_t length, std::basic_ostream< CharT, Traits > &output) noexcept
Definition: base64.hpp:137
static std::string encode(std::basic_istream< CharT, Traits > &data) noexcept
Definition: base64.hpp:96
virtual ~Base64()
Definition: base64.cpp:48
static std::string decode(const std::string &data) noexcept
Definition: base64.hpp:231
AnCH framework base namespace.
Definition: base64.hpp:28
static std::string decode(const char *data, uint64_t length) noexcept
Definition: base64.hpp:268
static void encode(std::basic_istream< CharT, Traits > &data, std::basic_ostream< CharT, Traits > &output) noexcept
Definition: base64.hpp:76
static void encode(const std::basic_string< CharT, Traits, Allocator > &data, std::basic_ostream< CharT, Traits > &output) noexcept
Definition: base64.hpp:109
static std::string encode(const std::basic_string< CharT, Traits, Allocator > &data) noexcept
Definition: base64.hpp:122
static std::string decode(std::istream &data) noexcept
Definition: base64.hpp:206
static void decode(std::istream &data, std::basic_ostream< CharT, Traits > &output) noexcept
Definition: base64.hpp:187
static void decode(const std::string &data, std::basic_ostream< CharT, Traits > &output) noexcept
Definition: base64.hpp:219
Base64 algorithm implementation.
Definition: base64.hpp:40
static void decode(const char *data, uint64_t length, std::basic_ostream< CharT, Traits > &output) noexcept
Definition: base64.hpp:246