Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
kome::core::Base64 Class Reference

base64 conversion class More...

#include <Base64.h>

Static Public Member Functions

static int encode (void *src, const unsigned int srcLength, char *dest, const unsigned int destLength)
 encodes source buffer into the destination buffer More...
 
static int decode (char *src, const unsigned int srcLength, void *dest, const unsigned int destLength)
 decodes source buffer into the destination buffer More...
 

Protected Member Functions

 Base64 ()
 constructor
 
virtual ~Base64 ()
 destructor
 
void createMap ()
 creates encode and decode map
 

Static Protected Member Functions

static Base64getInstance ()
 gets base64 object More...
 

Protected Attributes

char m_encodeMap [64]
 
int m_decodeMap [256]
 

Detailed Description

base64 conversion class

Definition at line 23 of file Base64.h.

Member Function Documentation

static int kome::core::Base64::decode ( char *  src,
const unsigned int  srcLength,
void *  dest,
const unsigned int  destLength 
)
static

decodes source buffer into the destination buffer

Parameters
[in]srcsource buffer
[in]srcLengthbyte length of the source buffer
[out]destdestination buffer
[in]destLengthbyte length of the destination buffer
Returns
If negative value, destination buffer size is to small, otherwise returns the size of decoded data

Definition at line 128 of file Base64.cpp.

133  {
134  // get instance
135  Base64& base64 = getInstance();
136 
137  // cast
138  char* cDest = (char*)dest;
139 
140  // buffers
141  char dataBuff[ 3 ];
142  unsigned char base64Buff[ 4 ];
143 
144  // read
145  unsigned int srcPos = 0;
146  unsigned int destPos = 0;
147  unsigned int length = 0;
148 
149  while( srcPos < srcLength ) {
150  // get base64 data
151  unsigned char c = (unsigned char)src[ srcPos++ ];
152  if( base64.m_decodeMap[ c ] >= 0 ) {
153  base64Buff[ length++ ] = base64.m_decodeMap[ c ];
154  }
155 
156  // decode
157  if( length == 4 || c == '=' ) {
158  switch( length ) {
159  case 4:
160  dataBuff[ 2 ] = ( base64Buff[ 2 ] << 6 ) | ( base64Buff[ 3 ] );
161  case 3:
162  dataBuff[ 1 ] = ( base64Buff[ 1 ] << 4 ) | ( base64Buff[ 2 ] >> 2 );
163  case 2:
164  dataBuff[ 0 ] = ( base64Buff[ 0 ] << 2 ) | ( base64Buff[ 1 ] >> 4 );
165  default:
166  break;
167  }
168 
169  // store
170  if( length > 1 ) {
171  length--;
172  for( unsigned int i = 0; i < length; i++ ) {
173  if( destPos >= destLength ) {
174  return -1;
175  }
176  cDest[ destPos++ ] = dataBuff[ i ];
177  }
178  }
179 
180  length = 0;
181 
182  if( c == '=' ) {
183  srcPos = srcLength;
184  }
185  }
186  }
187 
188  return destPos;
189 }
int m_decodeMap[256]
Definition: Base64.h:42
base64 conversion class
Definition: Base64.h:23
static Base64 & getInstance()
gets base64 object
Definition: Base64.cpp:192

Here is the call graph for this function:

static unsigned int kome::core::Base64::encode ( void *  src,
const unsigned int  srcLength,
char *  dest,
const unsigned int  destLength 
)
static

encodes source buffer into the destination buffer

Parameters
[in]srcsource buffer
[in]srcLengthbyte length of the source buffer
[out]destdestination buffer
[in]destLengthbyte length of the destination buffer
Returns
If negative value, destination buffer size is too small, otherwise returns the size of encoded data

Definition at line 66 of file Base64.cpp.

71  {
72  // get instance
73  Base64& base64 = getInstance();
74 
75  // cast
76  char* cSrc = (char*)src;
77 
78  // buffers
79  unsigned char dataBuff[ 3 ];
80  unsigned char base64Buff[ 4 ];
81 
82  // read
83  unsigned int srcPos = 0;
84  unsigned int destPos = 0;
85  unsigned int encodeSize = 0;
86  while( srcPos < srcLength ) {
87  // fill zero
88  fillzero( dataBuff, sizeof( dataBuff ) );
89 
90  // get 3 bytes
91  unsigned int count = 0;
92  for( unsigned int i = 0; i < 3; i++ ) {
93  if( srcPos < srcLength ) {
94  dataBuff[ i ] = cSrc[ srcPos++ ];
95  count++;
96  }
97  }
98 
99  // get encode data
100  base64Buff[ 0 ] = base64.m_encodeMap[ ( dataBuff[ 0 ] >> 2 ) & 0x3f ];
101  base64Buff[ 1 ] = base64.m_encodeMap[ ( ( dataBuff[ 0 ] << 4 ) | ( dataBuff[ 1 ] >> 4 ) ) & 0x3f ];
102  base64Buff[ 2 ] = base64.m_encodeMap[ ( ( dataBuff[ 1 ] << 2 ) | ( dataBuff[ 2 ] >> 6 ) ) & 0x3f ];
103  base64Buff[ 3 ] = base64.m_encodeMap[ ( dataBuff[ 2 ] ) & 0x3f ];
104 
105  // store
106  for( unsigned int i = 0; i < 4; i++ ) {
107  // check the buffer size
108  if( destPos >= destLength ) {
109  return -1;
110  }
111 
112  // character
113  if( i > count ) {
114  dest[ destPos++ ] = '=';
115  }
116  else {
117  dest[ destPos++ ] = base64Buff[ i ];
118  }
119 
120  encodeSize++;
121  }
122  }
123 
124  return (int)destPos;
125 }
base64 conversion class
Definition: Base64.h:23
static Base64 & getInstance()
gets base64 object
Definition: Base64.cpp:192
void fillzero(void *p, size_t n)
This function fills a block of memory zeros.
char m_encodeMap[64]
Definition: Base64.h:39

Here is the call graph for this function:

static Base64 & kome::core::Base64::getInstance ( )
staticprotected

gets base64 object

Returns
base64 object (This is the only object.)

Definition at line 192 of file Base64.cpp.

192  {
193  // create object (This is the only object)
194  static Base64 base64;
195 
196  return base64;
197 }
base64 conversion class
Definition: Base64.h:23

Member Data Documentation

int kome::core::Base64::m_decodeMap[256]
protected

decode map

Definition at line 42 of file Base64.h.

char kome::core::Base64::m_encodeMap[64]
protected

encode map

Definition at line 39 of file Base64.h.


The documentation for this class was generated from the following files: