OSBase64.cpp
Go to the documentation of this file.
1 /* $Id: OSBase64.cpp 5284 2017-12-08 13:52:50Z stefan $ */
15 #include "OSBase64.h"
16 #include <sstream>
17 
18 /*
19 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
20  "abcdefghijklmnopqrstuvwxyz"
21  "0123456789+/";
22 */
23 
24 namespace
25 {
26 
27 const char *base64_char_cstr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
28  "abcdefghijklmnopqrstuvwxyz"
29  "0123456789+/";
30 
31 }
32 
33 std::string Base64::encodeb64(char* bytes, int string_size)
34 {
35  unsigned char out_byte1, out_byte2, out_byte3, out_byte4;
36  unsigned char in_byte1, in_byte2, in_byte3;
37 
38  const std::string base64_chars = base64_char_cstr ;
39 
40  std::ostringstream outStr;
41  int remainder = string_size%3;
42  // get a number divisible by 3
43  int test = string_size - remainder;
44  while(test > 0)
45  {
46  in_byte1 = *(bytes++);
47  in_byte2 = *(bytes++);
48  in_byte3 = *(bytes++);
49  out_byte1 = (in_byte1 >> 2);
50  out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
51  out_byte3 = (in_byte2 & 0x0f) << 2 | (in_byte3 >> 6);
52  out_byte4 = in_byte3 & 0x3f;
53  outStr << base64_chars[out_byte1];
54  outStr << base64_chars[out_byte2];
55  outStr << base64_chars[out_byte3];
56  outStr << base64_chars[out_byte4] ;
57  test = test - 3;
58  }
59  // now take care of padding
60  if(remainder > 0)
61  {
62  in_byte1 = *(bytes++);
63  in_byte3 = '\0';
64  out_byte1 = (in_byte1 >> 2);
65  outStr << base64_chars[out_byte1];
66  if(remainder == 1)
67  {
68  in_byte2 = '\0';
69  out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
70  outStr << base64_chars[out_byte2];
71  outStr << '=';
72  }
73  else
74  {
75  in_byte2 = *(bytes++);
76  out_byte2 = (in_byte1 & 0x03) << 4 | (in_byte2 >> 4);
77  out_byte3 = (in_byte2 & 0x0f) << 2 | (in_byte3 >> 6);
78  outStr << base64_chars[out_byte2];
79  outStr << base64_chars[out_byte3];
80  }
81  outStr << '=';
82  }
83  return outStr.str();
84 
85 }//encodeb64
86 
87 std::string Base64::decodeb64(char* b64bytes)
88 {
89  unsigned char out_byte1, out_byte2, out_byte3;
90  unsigned char in_byte1, in_byte2, in_byte3, in_byte4;
91  std::ostringstream outStr;
92 
93  const std::string base64_chars = base64_char_cstr ;
94 
95  // first take of the non-padded bytes
96  while(*b64bytes != '=' && *b64bytes != '\0')
97  {
98  in_byte1 = base64_chars.find(*(b64bytes++));
99  in_byte2 = base64_chars.find(*(b64bytes++));
100  in_byte3 = base64_chars.find(*(b64bytes++));
101  in_byte4 = base64_chars.find(*(b64bytes++));
102  out_byte1 = (in_byte1 << 2) | ((in_byte2 & 0x30) >> 4) ;
103  out_byte2 = ((in_byte2 & 0x0f) << 4) | ((in_byte3 & 0x3c) >> 2) ;
104  out_byte3 = ((in_byte3 & 0x03) << 6) | in_byte4 ;
105  outStr << out_byte1;
106  outStr << out_byte2;
107  outStr << out_byte3;
108  }
109  // now take into account the padding
110  if(*b64bytes == '=')
111  {
112  in_byte1 = base64_chars.find(*(b64bytes++));
113  in_byte2 = base64_chars.find(*(b64bytes++));
114  in_byte4 = '\0';
115  out_byte1 = (in_byte1 << 2) | ((in_byte2 & 0x30) >> 4) ;
116  outStr << out_byte1;
117  *b64bytes++;
118  if(*b64bytes == '\0') //if null we had only one = in the padding
119  {
120  in_byte3 = base64_chars.find(*(b64bytes++));
121  out_byte2 = ((in_byte2 & 0x0f) << 4) | ((in_byte3 & 0x3c) >> 2) ;
122  out_byte3 = ((in_byte3 & 0x03) << 6) | in_byte4 ;
123  outStr << out_byte2;
124  }
125  else // we have == in the padding
126  {
127  in_byte3 = '\0';
128  out_byte2 = ((in_byte2 & 0x0f) << 4) | ((in_byte3 & 0x3c) >> 2) ;
129  out_byte3 = ((in_byte3 & 0x03) << 6) | in_byte4 ;
130  //outStr << out_byte2;
131  //outStr << out_byte3;
132  }
133  }
134  return outStr.str();
135 } // decodeb64
136 
137 
138 
static std::string encodeb64(char *bytes, int size)
encode the data in base 64
Definition: OSBase64.cpp:33
static std::string decodeb64(char *b64bytes)
decode the data in base 64
Definition: OSBase64.cpp:87