IBR-DTNSuite  0.10
Base64.cpp
Go to the documentation of this file.
1 /*
2  * Base64.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "ibrcommon/data/Base64.h"
23 
24 #define _0000_0011 0x03
25 #define _1111_1100 0xFC
26 #define _1111_0000 0xF0
27 #define _0011_0000 0x30
28 #define _0011_1100 0x3C
29 #define _0000_1111 0x0F
30 #define _1100_0000 0xC0
31 #define _0011_1111 0x3F
32 
33 namespace ibrcommon
34 {
35  const char Base64::encodeCharacterTable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
36 
37  size_t Base64::getLength(size_t length)
38  {
39  // encoding = byte * (4/3)
40  // calculate base64 length for given plaintext length
41  // all bytes we have to read
42  size_t ret = (length / 3) * 4;
43 
44  // plus one group if there are carry bytes
45  if ((length % 3) > 0) ret += 4;
46 
47  return ret;
48  }
49 
51  {
52  zero();
53  }
54 
56  {
57  }
58 
60  {
61  _data[0] = 0;
62  _data[1] = 0;
63  _data[2] = 0;
64  }
65 
66  uint8_t Base64::Group::get_0() const
67  {
68  return _data[0];
69  }
70 
71  uint8_t Base64::Group::get_1() const
72  {
73  return _data[1];
74  }
75 
76  uint8_t Base64::Group::get_2() const
77  {
78  return _data[2];
79  }
80 
81  void Base64::Group::set_0(uint8_t val)
82  {
83  _data[0] = val;
84  }
85 
86  void Base64::Group::set_1(uint8_t val)
87  {
88  _data[1] = val;
89  }
90 
91  void Base64::Group::set_2(uint8_t val)
92  {
93  _data[2] = val;
94  }
95 
97  {
98  return (_data[0] & _1111_1100) >> 2;
99  }
100 
102  {
103  return ((_data[0] & _0000_0011) << 4) + ((_data[1] & _1111_0000)>>4);
104  }
105 
107  {
108  return ((_data[1] & _0000_1111) << 2) + ((_data[2] & _1100_0000)>>6);
109  }
110 
112  {
113  return (_data[2] & _0011_1111);
114  }
115 
116  void Base64::Group::b64_0(int val)
117  {
118  _data[0] = static_cast<uint8_t>( ((val & _0011_1111) << 2) | (_0000_0011 & _data[0]) );
119  }
120 
121  void Base64::Group::b64_1(int val)
122  {
123  _data[0] = static_cast<uint8_t>( ((val & _0011_0000) >> 4) | (_1111_1100 & _data[0]) );
124  _data[1] = static_cast<uint8_t>( ((val & _0000_1111) << 4) | (_0000_1111 & _data[1]) );
125  }
126 
127  void Base64::Group::b64_2(int val)
128  {
129  _data[1] = static_cast<uint8_t>( ((val & _0011_1100) >> 2) | (_1111_0000 & _data[1]) );
130  _data[2] = static_cast<uint8_t>( ((val & _0000_0011) << 6) | (_0011_1111 & _data[2]) );
131  }
132 
133  void Base64::Group::b64_3(int val)
134  {
135  _data[2] = static_cast<uint8_t>( (val & _0011_1111) | (_1100_0000 & _data[2]) );
136  }
137 
139  {
140  if(encodeCharacterTable[62] == _C)
141  return 62;
142 
143  if(encodeCharacterTable[63] == _C)
144  return 63;
145 
146  if((encodeCharacterTable[0] <= _C) && (encodeCharacterTable[25] >= _C))
147  return _C - encodeCharacterTable[0];
148 
149  if((encodeCharacterTable[26] <= _C) && (encodeCharacterTable[51] >= _C))
150  return _C - encodeCharacterTable[26] + 26;
151 
152  if((encodeCharacterTable[52] <= _C) && (encodeCharacterTable[61] >= _C))
153  return _C - encodeCharacterTable[52] + 52;
154 
155  if(_C == ((int)'='))
156  return Base64::EQUAL_CHAR;
157 
158  return Base64::UNKOWN_CHAR;
159  }
160 } /* namespace dtn */