base64.cpp
Go to the documentation of this file.
1 /*
2  base64.cpp and base64.h
3 
4  base64 encoding and decoding with C++.
5 
6  Version: 1.01.00
7 
8  Copyright (C) 2004-2017 RenĂ© Nyffenegger
9 
10  This source code is provided 'as-is', without any express or implied
11  warranty. In no event will the author be held liable for any damages
12  arising from the use of this software.
13 
14  Permission is granted to anyone to use this software for any purpose,
15  including commercial applications, and to alter it and redistribute it
16  freely, subject to the following restrictions:
17 
18  1. The origin of this source code must not be misrepresented; you must not
19  claim that you wrote the original source code. If you use this source code
20  in a product, an acknowledgment in the product documentation would be
21  appreciated but is not required.
22 
23  2. Altered source versions must be plainly marked as such, and must not be
24  misrepresented as being the original source code.
25 
26  3. This notice may not be removed or altered from any source distribution.
27 
28  RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
29 
30 */
31 
32 #include "../include/swarmio/transport/base64.h"
33 #include <iostream>
34 
35 static const std::string base64_chars =
36  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
37  "abcdefghijklmnopqrstuvwxyz"
38  "0123456789+/";
39 
40 
41 static inline bool is_base64(unsigned char c) {
42  return (isalnum(c) || (c == '+') || (c == '/'));
43 }
44 
45 std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
46  std::string ret;
47  int i = 0;
48  int j = 0;
49  unsigned char char_array_3[3];
50  unsigned char char_array_4[4];
51 
52  while (in_len--) {
53  char_array_3[i++] = *(bytes_to_encode++);
54  if (i == 3) {
55  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
56  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
57  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
58  char_array_4[3] = char_array_3[2] & 0x3f;
59 
60  for(i = 0; (i <4) ; i++)
61  ret += base64_chars[char_array_4[i]];
62  i = 0;
63  }
64  }
65 
66  if (i)
67  {
68  for(j = i; j < 3; j++)
69  char_array_3[j] = '\0';
70 
71  char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
72  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
73  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
74 
75  for (j = 0; (j < i + 1); j++)
76  ret += base64_chars[char_array_4[j]];
77 
78  while((i++ < 3))
79  ret += '=';
80 
81  }
82 
83  return ret;
84 
85 }
86 
87 std::string base64_decode(std::string const& encoded_string) {
88  size_t in_len = encoded_string.size();
89  int i = 0;
90  int j = 0;
91  int in_ = 0;
92  unsigned char char_array_4[4], char_array_3[3];
93  std::string ret;
94 
95  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
96  char_array_4[i++] = encoded_string[in_]; in_++;
97  if (i ==4) {
98  for (i = 0; i <4; i++)
99  char_array_4[i] = base64_chars.find(char_array_4[i]) & 0xff;
100 
101  char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4);
102  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
103  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
104 
105  for (i = 0; (i < 3); i++)
106  ret += char_array_3[i];
107  i = 0;
108  }
109  }
110 
111  if (i) {
112  for (j = 0; j < i; j++)
113  char_array_4[j] = base64_chars.find(char_array_4[j]) & 0xff;
114 
115  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
116  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
117 
118  for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
119  }
120 
121  return ret;
122 }
static const std::string base64_chars
Definition: base64.cpp:35
static bool is_base64(unsigned char c)
Definition: base64.cpp:41
std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition: base64.cpp:45
std::string base64_decode(std::string const &encoded_string)
Definition: base64.cpp:87


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:47