IBR-DTNSuite  0.8
ibrcommon/ibrcommon/data/Base64.cpp
Go to the documentation of this file.
00001 /*
00002  * Base64.cpp
00003  *
00004  *  Created on: 08.11.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "ibrcommon/data/Base64.h"
00009 
00010 #define _0000_0011 0x03
00011 #define _1111_1100 0xFC
00012 #define _1111_0000 0xF0
00013 #define _0011_0000 0x30
00014 #define _0011_1100 0x3C
00015 #define _0000_1111 0x0F
00016 #define _1100_0000 0xC0
00017 #define _0011_1111 0x3F
00018 
00019 namespace ibrcommon
00020 {
00021         const char Base64::encodeCharacterTable[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00022 
00023         size_t Base64::getLength(size_t length)
00024         {
00025                 // encoding = byte * (4/3)
00026                 // calculate base64 length for given plaintext length
00027                 // all bytes we have to read
00028                 size_t ret = (length / 3) * 4;
00029 
00030                 // plus one group if there are carry bytes
00031                 if ((length % 3) > 0) ret += 4;
00032 
00033                 return ret;
00034         }
00035 
00036         Base64::Group::Group()
00037         {
00038                 zero();
00039         }
00040 
00041         Base64::Group::~Group()
00042         {
00043         }
00044 
00045         void Base64::Group::zero()
00046         {
00047                 _data[0] = 0;
00048                 _data[1] = 0;
00049                 _data[2] = 0;
00050         }
00051 
00052         uint8_t Base64::Group::get_0() const
00053         {
00054                 return _data[0];
00055         }
00056 
00057         uint8_t Base64::Group::get_1() const
00058         {
00059                 return _data[1];
00060         }
00061 
00062         uint8_t Base64::Group::get_2() const
00063         {
00064                 return _data[2];
00065         }
00066 
00067         void Base64::Group::set_0(uint8_t val)
00068         {
00069                 _data[0] = val;
00070         }
00071 
00072         void Base64::Group::set_1(uint8_t val)
00073         {
00074                 _data[1] = val;
00075         }
00076 
00077         void Base64::Group::set_2(uint8_t val)
00078         {
00079                 _data[2] = val;
00080         }
00081 
00082         int Base64::Group::b64_0() const
00083         {
00084                 return (_data[0] & _1111_1100) >> 2;
00085         }
00086 
00087         int Base64::Group::b64_1() const
00088         {
00089                 return ((_data[0] & _0000_0011) << 4) + ((_data[1] & _1111_0000)>>4);
00090         }
00091 
00092         int Base64::Group::b64_2() const
00093         {
00094                 return ((_data[1] & _0000_1111) << 2) + ((_data[2] & _1100_0000)>>6);
00095         }
00096 
00097         int Base64::Group::b64_3() const
00098         {
00099                 return (_data[2] & _0011_1111);
00100         }
00101 
00102         void Base64::Group::b64_0(int val)
00103         {
00104                 _data[0] = ((val & _0011_1111) << 2) | (_0000_0011 & _data[0]);
00105         }
00106 
00107         void Base64::Group::b64_1(int val)
00108         {
00109                 _data[0] = ((val & _0011_0000) >> 4) | (_1111_1100 & _data[0]);
00110                 _data[1] = ((val & _0000_1111) << 4) | (_0000_1111 & _data[1]);
00111         }
00112 
00113         void Base64::Group::b64_2(int val)
00114         {
00115                 _data[1] = ((val & _0011_1100) >> 2) | (_1111_0000 & _data[1]);
00116                 _data[2] = ((val & _0000_0011) << 6) | (_0011_1111 & _data[2]);
00117         }
00118 
00119         void Base64::Group::b64_3(int val)
00120         {
00121                 _data[2] = (val & _0011_1111) | (_1100_0000 & _data[2]);
00122         }
00123 
00124         int Base64::getCharType(int _C)
00125         {
00126                 if(encodeCharacterTable[62] == _C)
00127                         return 62;
00128 
00129                 if(encodeCharacterTable[63] == _C)
00130                         return 63;
00131 
00132                 if((encodeCharacterTable[0] <= _C) && (encodeCharacterTable[25] >= _C))
00133                         return _C - encodeCharacterTable[0];
00134 
00135                 if((encodeCharacterTable[26] <= _C) && (encodeCharacterTable[51] >= _C))
00136                         return _C - encodeCharacterTable[26] + 26;
00137 
00138                 if((encodeCharacterTable[52] <= _C) && (encodeCharacterTable[61] >= _C))
00139                         return _C - encodeCharacterTable[52] + 52;
00140 
00141                 if(_C == ((int)'='))
00142                         return Base64::EQUAL_CHAR;
00143 
00144                 return Base64::UNKOWN_CHAR;
00145         }
00146 } /* namespace dtn */