Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
Base64.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "Base64.h"
14 
15 
16 using namespace kome::core;
17 
18 
19 #include <crtdbg.h>
20 #ifdef _DEBUG
21  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
22  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
23 #endif // _DEBUG
24 
25 
26 
27 // constructor
29  createMap();
30 }
31 
32 // destructor
34 }
35 
36 // create encode map
38  // create encode map
39  m_encodeMap[ 0x00 ] = 'A'; m_encodeMap[ 0x01 ] = 'B'; m_encodeMap[ 0x02 ] = 'C'; m_encodeMap[ 0x03 ] = 'D';
40  m_encodeMap[ 0x04 ] = 'E'; m_encodeMap[ 0x05 ] = 'F'; m_encodeMap[ 0x06 ] = 'G'; m_encodeMap[ 0x07 ] = 'H';
41  m_encodeMap[ 0x08 ] = 'I'; m_encodeMap[ 0x09 ] = 'J'; m_encodeMap[ 0x0A ] = 'K'; m_encodeMap[ 0x0B ] = 'L';
42  m_encodeMap[ 0x0C ] = 'M'; m_encodeMap[ 0x0D ] = 'N'; m_encodeMap[ 0x0E ] = 'O'; m_encodeMap[ 0x0F ] = 'P';
43  m_encodeMap[ 0x10 ] = 'Q'; m_encodeMap[ 0x11 ] = 'R'; m_encodeMap[ 0x12 ] = 'S'; m_encodeMap[ 0x13 ] = 'T';
44  m_encodeMap[ 0x14 ] = 'U'; m_encodeMap[ 0x15 ] = 'V'; m_encodeMap[ 0x16 ] = 'W'; m_encodeMap[ 0x17 ] = 'X';
45  m_encodeMap[ 0x18 ] = 'Y'; m_encodeMap[ 0x19 ] = 'Z'; m_encodeMap[ 0x1A ] = 'a'; m_encodeMap[ 0x1B ] = 'b';
46  m_encodeMap[ 0x1C ] = 'c'; m_encodeMap[ 0x1D ] = 'd'; m_encodeMap[ 0x1E ] = 'e'; m_encodeMap[ 0x1F ] = 'f';
47  m_encodeMap[ 0x20 ] = 'g'; m_encodeMap[ 0x21 ] = 'h'; m_encodeMap[ 0x22 ] = 'i'; m_encodeMap[ 0x23 ] = 'j';
48  m_encodeMap[ 0x24 ] = 'k'; m_encodeMap[ 0x25 ] = 'l'; m_encodeMap[ 0x26 ] = 'm'; m_encodeMap[ 0x27 ] = 'n';
49  m_encodeMap[ 0x28 ] = 'o'; m_encodeMap[ 0x29 ] = 'p'; m_encodeMap[ 0x2A ] = 'q'; m_encodeMap[ 0x2B ] = 'r';
50  m_encodeMap[ 0x2C ] = 's'; m_encodeMap[ 0x2D ] = 't'; m_encodeMap[ 0x2E ] = 'u'; m_encodeMap[ 0x2F ] = 'v';
51  m_encodeMap[ 0x30 ] = 'w'; m_encodeMap[ 0x31 ] = 'x'; m_encodeMap[ 0x32 ] = 'y'; m_encodeMap[ 0x33 ] = 'z';
52  m_encodeMap[ 0x34 ] = '0'; m_encodeMap[ 0x35 ] = '1'; m_encodeMap[ 0x36 ] = '2'; m_encodeMap[ 0x37 ] = '3';
53  m_encodeMap[ 0x38 ] = '4'; m_encodeMap[ 0x39 ] = '5'; m_encodeMap[ 0x3A ] = '6'; m_encodeMap[ 0x3B ] = '7';
54  m_encodeMap[ 0x3C ] = '8'; m_encodeMap[ 0x3D ] = '9'; m_encodeMap[ 0x3E ] = '+'; m_encodeMap[ 0x3F ] = '/';
55 
56  // create decode map
57  for( unsigned int i = 0; i < 256; i++ ) {
58  m_decodeMap[ i ] = -1;
59  }
60  for( unsigned int i = 0; i < 64; i++ ) {
61  m_decodeMap[ m_encodeMap[ i ] ] = i;
62  }
63 }
64 
65 // encode
67  void* src,
68  const unsigned int srcLength,
69  char* dest,
70  const unsigned int destLength
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 }
126 
127 // decode
129  char* src,
130  const unsigned int srcLength,
131  void* dest,
132  const unsigned int destLength
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 }
190 
191 // get instance
193  // create object (This is the only object)
194  static Base64 base64;
195 
196  return base64;
197 }
static int decode(char *src, const unsigned int srcLength, void *dest, const unsigned int destLength)
decodes source buffer into the destination buffer
Definition: Base64.cpp:128
void createMap()
creates encode and decode map
Definition: Base64.cpp:37
int m_decodeMap[256]
Definition: Base64.h:42
base64 conversion class
Definition: Base64.h:23
static int encode(void *src, const unsigned int srcLength, char *dest, const unsigned int destLength)
encodes source buffer into the destination buffer
Definition: Base64.cpp:66
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
virtual ~Base64()
destructor
Definition: Base64.cpp:33
interfarces of Base64 class
Base64()
constructor
Definition: Base64.cpp:28