Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
base64.cpp
1 /*
2  base64.cpp and base64.h
3 
4  Copyright (C) 2004-2008 RenĂ© Nyffenegger
5 
6  This source code is provided 'as-is', without any express or implied
7  warranty. In no event will the author be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this source code must not be misrepresented; you must not
15  claim that you wrote the original source code. If you use this source code
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18 
19  2. Altered source versions must be plainly marked as such, and must not be
20  misrepresented as being the original source code.
21 
22  3. This notice may not be removed or altered from any source distribution.
23 
24  RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
25  Modified by Andreas Widen <andreas@harmonicflow.org>
26  - Added namespace HFCore
27  - Added class Base64 and made methods static
28  - Renamed 'base64_encode' -> encode
29  - Renamed 'base64_decode' -> decode
30  - Changed to use libhffwk internal data types
31  - Added Doxygen documentation
32  - Formatted code
33 */
34 
35 #include "base64.h"
36 #include <iostream>
37 
38 using namespace HFCore;
39 
40 #include "debug/CrtDbgNew.h"
41 
42 static const std::string base64_chars =
43  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
44  "abcdefghijklmnopqrstuvwxyz"
45  "0123456789+/";
46 
47 static inline bool is_base64(uint8_hf c)
48 {
49  return (isalnum(c) || (c == '+') || (c == '/'));
50 }
51 
52 std::string Base64::encode(const uint8_hf *bytes_to_encode, uint32_hf in_len)
53 {
54  std::string ret;
55  int32_hf i = 0;
56  int32_hf j = 0;
57  uint8_hf char_array_3[3];
58  uint8_hf char_array_4[4];
59 
60  while (in_len--)
61  {
62  char_array_3[i++] = *(bytes_to_encode++);
63  if (i == 3)
64  {
65  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
66  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
67  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
68  char_array_4[3] = char_array_3[2] & 0x3f;
69 
70  for (i = 0; (i < 4) ; i++)
71  ret += base64_chars[char_array_4[i]];
72  i = 0;
73  }
74  }
75 
76  if (i)
77  {
78  for (j = i; j < 3; j++)
79  char_array_3[j] = '\0';
80 
81  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
82  char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
83  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
84  char_array_4[3] = char_array_3[2] & 0x3f;
85 
86  for (j = 0; (j < i + 1); j++)
87  ret += base64_chars[char_array_4[j]];
88 
89  while ((i++ < 3))
90  ret += '=';
91 
92  }
93 
94  return ret;
95 
96 }
97 
98 std::string Base64::decode(const std::string &encoded_string)
99 {
100  int32_hf in_len = encoded_string.size();
101  int32_hf i = 0;
102  int32_hf j = 0;
103  int32_hf in_ = 0;
104  uint8_hf char_array_4[4], char_array_3[3];
105  std::string ret;
106 
107  while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
108  {
109  char_array_4[i++] = encoded_string[in_]; in_++;
110  if (i == 4)
111  {
112  for (i = 0; i < 4; i++)
113  char_array_4[i] = (uint8_hf)base64_chars.find(char_array_4[i]);
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  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
118 
119  for (i = 0; (i < 3); i++)
120  ret += char_array_3[i];
121  i = 0;
122  }
123  }
124 
125  if (i)
126  {
127  for (j = i; j < 4; j++)
128  char_array_4[j] = 0;
129 
130  for (j = 0; j < 4; j++)
131  char_array_4[j] = (uint8_hf)base64_chars.find(char_array_4[j]);
132 
133  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
134  char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
135  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
136 
137  for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
138  }
139 
140  return ret;
141 }
unsigned int uint32_hf
A type definition for unsigned int.
Definition: HFDataTypes.h:354
static std::string decode(const std::string &encoded_string)
Base64 decode a string.
Definition: base64.cpp:98
int int32_hf
A type definition for int.
Definition: HFDataTypes.h:349
static std::string encode(const uint8_hf *bytes_to_encode, uint32_hf in_len)
Base64 encode a string.
Definition: base64.cpp:52
Interface for encoding / decoding Base64 strings.
Definition: Actor.h:34
unsigned char uint8_hf
A type definition for unsigned char.
Definition: HFDataTypes.h:334