Tileson  1.3.0
A helpful json parser for Tiled maps
Base64.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 22.03.2020.
3 // Based on: https://github.com/ReneNyffenegger/cpp-base64
4 //
5 
6 #ifndef TILESON_BASE64_HPP
7 #define TILESON_BASE64_HPP
8 #include <string>
9 #include <iostream>
10 
11 namespace tson
12 {
13  class Base64
14  {
15  public:
16  inline static std::string Encode(unsigned char const *, unsigned int len);
17 
18  inline static std::string Decode(std::string const &s);
19 
20  };
21 
22  static const std::string base64_chars =
23  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24  "abcdefghijklmnopqrstuvwxyz"
25  "0123456789+/";
26 
27  static inline bool is_base64(unsigned char c)
28  {
29  return (isalnum(c) || (c == '+') || (c == '/'));
30  }
31 }
32 
33 std::string tson::Base64::Encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
34  std::string ret;
35  int i = 0;
36  int j = 0;
37  unsigned char char_array_3[3];
38  unsigned char char_array_4[4];
39 
40  while (in_len--) {
41  char_array_3[i++] = *(bytes_to_encode++);
42  if (i == 3) {
43  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
44  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
45  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
46  char_array_4[3] = char_array_3[2] & 0x3f;
47 
48  for(i = 0; (i <4) ; i++)
49  ret += base64_chars[char_array_4[i]];
50  i = 0;
51  }
52  }
53 
54  if (i)
55  {
56  for(j = i; j < 3; j++)
57  char_array_3[j] = '\0';
58 
59  char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
60  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
61  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
62 
63  for (j = 0; (j < i + 1); j++)
64  ret += base64_chars[char_array_4[j]];
65 
66  while((i++ < 3))
67  ret += '=';
68 
69  }
70 
71  return ret;
72 
73 }
74 
75 std::string tson::Base64::Decode(std::string const& encoded_string) {
76  size_t in_len = encoded_string.size();
77  int i = 0;
78  int j = 0;
79  int in_ = 0;
80  unsigned char char_array_4[4], char_array_3[3];
81  std::string ret;
82 
83  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
84  char_array_4[i++] = encoded_string[in_]; in_++;
85  if (i ==4) {
86  for (i = 0; i <4; i++)
87  char_array_4[i] = base64_chars.find(char_array_4[i]) & 0xff;
88 
89  char_array_3[0] = ( char_array_4[0] << 2 ) + ((char_array_4[1] & 0x30) >> 4);
90  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
91  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
92 
93  for (i = 0; (i < 3); i++)
94  ret += char_array_3[i];
95  i = 0;
96  }
97  }
98 
99  if (i) {
100  for (j = 0; j < i; j++)
101  char_array_4[j] = base64_chars.find(char_array_4[j]) & 0xff;
102 
103  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
104  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
105 
106  for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
107  }
108 
109  return ret;
110 }
111 
112 #endif //TILESON_BASE64_HPP
Definition: Base64.hpp:14
static std::string Encode(unsigned char const *, unsigned int len)
Definition: Base64.hpp:33
static std::string Decode(std::string const &s)
Definition: Base64.hpp:75
Definition: Base64.hpp:12