Base64.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // CBase64.cpp: implementation of the CBase64 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "Base64.h"
  5. // Digits...
  6. static char Base64Digits[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  7. BOOL CBase64::m_Init = FALSE;
  8. char CBase64::m_DecodeTable[256];
  9. #ifndef PAGESIZE
  10. #define PAGESIZE 4096
  11. #endif
  12. #ifndef ROUNDTOPAGE
  13. #define ROUNDTOPAGE(a) (((a/4096)+1)*4096)
  14. #endif
  15. //////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17. //////////////////////////////////////////////////////////////////////
  18. CBase64::CBase64()
  19. : m_pDBuffer(NULL),
  20. m_pEBuffer(NULL),
  21. m_nDBufLen(0),
  22. m_nEBufLen(0),
  23. m_nDDataLen(0),
  24. m_nEDataLen(0)
  25. {
  26. }
  27. CBase64::~CBase64()
  28. {
  29. if(m_pDBuffer != NULL)
  30. delete [] m_pDBuffer;
  31. if(m_pEBuffer != NULL)
  32. delete [] m_pEBuffer;
  33. }
  34. LPCSTR CBase64::DecodedMessage() const
  35. {
  36. return (LPCSTR) m_pDBuffer;
  37. }
  38. LPCSTR CBase64::EncodedMessage() const
  39. {
  40. return (LPCSTR) m_pEBuffer;
  41. }
  42. void CBase64::AllocEncode(DWORD nSize)
  43. {
  44. if(m_nEBufLen < nSize)
  45. {
  46. if(m_pEBuffer != NULL)
  47. delete [] m_pEBuffer;
  48. m_nEBufLen = ROUNDTOPAGE(nSize);
  49. m_pEBuffer = new BYTE[m_nEBufLen];
  50. }
  51. ::ZeroMemory(m_pEBuffer, m_nEBufLen);
  52. m_nEDataLen = 0;
  53. }
  54. void CBase64::AllocDecode(DWORD nSize)
  55. {
  56. if(m_nDBufLen < nSize)
  57. {
  58. if(m_pDBuffer != NULL)
  59. delete [] m_pDBuffer;
  60. m_nDBufLen = ROUNDTOPAGE(nSize);
  61. m_pDBuffer = new BYTE[m_nDBufLen];
  62. }
  63. ::ZeroMemory(m_pDBuffer, m_nDBufLen);
  64. m_nDDataLen = 0;
  65. }
  66. void CBase64::SetEncodeBuffer(const PBYTE pBuffer, DWORD nBufLen)
  67. {
  68. DWORD i = 0;
  69. AllocEncode(nBufLen);
  70. while(i < nBufLen)
  71. {
  72. if(!_IsBadMimeChar(pBuffer[i]))
  73. {
  74. m_pEBuffer[m_nEDataLen] = pBuffer[i];
  75. m_nEDataLen++;
  76. }
  77. i++;
  78. }
  79. }
  80. void CBase64::SetDecodeBuffer(const PBYTE pBuffer, DWORD nBufLen)
  81. {
  82. AllocDecode(nBufLen);
  83. ::CopyMemory(m_pDBuffer, pBuffer, nBufLen);
  84. m_nDDataLen = nBufLen;
  85. }
  86. void CBase64::Encode(const PBYTE pBuffer, DWORD nBufLen)
  87. {
  88. SetDecodeBuffer(pBuffer, nBufLen);
  89. AllocEncode(nBufLen * 2);
  90. TempBucket Raw;
  91. DWORD nIndex = 0;
  92. while((nIndex + 3) <= nBufLen)
  93. {
  94. Raw.Clear();
  95. ::CopyMemory(&Raw, m_pDBuffer + nIndex, 3);
  96. Raw.nSize = 3;
  97. _EncodeToBuffer(Raw, m_pEBuffer + m_nEDataLen);
  98. nIndex += 3;
  99. m_nEDataLen += 4;
  100. }
  101. if(nBufLen > nIndex)
  102. {
  103. Raw.Clear();
  104. Raw.nSize = (BYTE) (nBufLen - nIndex);
  105. ::CopyMemory(&Raw, m_pDBuffer + nIndex, nBufLen - nIndex);
  106. _EncodeToBuffer(Raw, m_pEBuffer + m_nEDataLen);
  107. m_nEDataLen += 4;
  108. }
  109. }
  110. void CBase64::Encode(LPCSTR szMessage)
  111. {
  112. if(szMessage != NULL)
  113. CBase64::Encode((const PBYTE)szMessage, lstrlenA(szMessage));
  114. }
  115. void CBase64::Decode(const PBYTE pBuffer, DWORD dwBufLen)
  116. {
  117. if(!CBase64::m_Init)
  118. _Init();
  119. SetEncodeBuffer(pBuffer, dwBufLen);
  120. AllocDecode(dwBufLen);
  121. TempBucket Raw;
  122. DWORD nIndex = 0;
  123. while((nIndex + 4) <= m_nEDataLen)
  124. {
  125. Raw.Clear();
  126. Raw.nData[0] = CBase64::m_DecodeTable[m_pEBuffer[nIndex]];
  127. Raw.nData[1] = CBase64::m_DecodeTable[m_pEBuffer[nIndex + 1]];
  128. Raw.nData[2] = CBase64::m_DecodeTable[m_pEBuffer[nIndex + 2]];
  129. Raw.nData[3] = CBase64::m_DecodeTable[m_pEBuffer[nIndex + 3]];
  130. if(Raw.nData[2] == 255)
  131. Raw.nData[2] = 0;
  132. if(Raw.nData[3] == 255)
  133. Raw.nData[3] = 0;
  134. Raw.nSize = 4;
  135. _DecodeToBuffer(Raw, m_pDBuffer + m_nDDataLen);
  136. nIndex += 4;
  137. m_nDDataLen += 3;
  138. }
  139. // If nIndex < m_nEDataLen, then we got a decode message without padding.
  140. // We may want to throw some kind of warning here, but we are still required
  141. // to handle the decoding as if it was properly padded.
  142. if(nIndex < m_nEDataLen)
  143. {
  144. Raw.Clear();
  145. for(DWORD i = nIndex; i < m_nEDataLen; i++)
  146. {
  147. Raw.nData[i - nIndex] = CBase64::m_DecodeTable[m_pEBuffer[i]];
  148. Raw.nSize++;
  149. if(Raw.nData[i - nIndex] == 255)
  150. Raw.nData[i - nIndex] = 0;
  151. }
  152. _DecodeToBuffer(Raw, m_pDBuffer + m_nDDataLen);
  153. m_nDDataLen += (m_nEDataLen - nIndex);
  154. }
  155. }
  156. void CBase64::Decode(LPCSTR szMessage)
  157. {
  158. if(szMessage != NULL)
  159. CBase64::Decode((const PBYTE)szMessage, lstrlenA(szMessage));
  160. }
  161. DWORD CBase64::_DecodeToBuffer(const TempBucket &Decode, PBYTE pBuffer)
  162. {
  163. TempBucket Data;
  164. DWORD nCount = 0;
  165. _DecodeRaw(Data, Decode);
  166. for(int i = 0; i < 3; i++)
  167. {
  168. pBuffer[i] = Data.nData[i];
  169. if(pBuffer[i] != 255)
  170. nCount++;
  171. }
  172. return nCount;
  173. }
  174. void CBase64::_EncodeToBuffer(const TempBucket &Decode, PBYTE pBuffer)
  175. {
  176. TempBucket Data;
  177. _EncodeRaw(Data, Decode);
  178. for(int i = 0; i < 4; i++)
  179. pBuffer[i] = Base64Digits[Data.nData[i]];
  180. switch(Decode.nSize)
  181. {
  182. case 1:
  183. pBuffer[2] = '=';
  184. case 2:
  185. pBuffer[3] = '=';
  186. }
  187. }
  188. void CBase64::_DecodeRaw(TempBucket &Data, const TempBucket &Decode)
  189. {
  190. BYTE nTemp;
  191. Data.nData[0] = Decode.nData[0];
  192. Data.nData[0] <<= 2;
  193. nTemp = Decode.nData[1];
  194. nTemp >>= 4;
  195. nTemp &= 0x03;
  196. Data.nData[0] |= nTemp;
  197. Data.nData[1] = Decode.nData[1];
  198. Data.nData[1] <<= 4;
  199. nTemp = Decode.nData[2];
  200. nTemp >>= 2;
  201. nTemp &= 0x0F;
  202. Data.nData[1] |= nTemp;
  203. Data.nData[2] = Decode.nData[2];
  204. Data.nData[2] <<= 6;
  205. nTemp = Decode.nData[3];
  206. nTemp &= 0x3F;
  207. Data.nData[2] |= nTemp;
  208. }
  209. void CBase64::_EncodeRaw(TempBucket &Data, const TempBucket &Decode)
  210. {
  211. BYTE nTemp;
  212. Data.nData[0] = Decode.nData[0];
  213. Data.nData[0] >>= 2;
  214. Data.nData[1] = Decode.nData[0];
  215. Data.nData[1] <<= 4;
  216. nTemp = Decode.nData[1];
  217. nTemp >>= 4;
  218. Data.nData[1] |= nTemp;
  219. Data.nData[1] &= 0x3F;
  220. Data.nData[2] = Decode.nData[1];
  221. Data.nData[2] <<= 2;
  222. nTemp = Decode.nData[2];
  223. nTemp >>= 6;
  224. Data.nData[2] |= nTemp;
  225. Data.nData[2] &= 0x3F;
  226. Data.nData[3] = Decode.nData[2];
  227. Data.nData[3] &= 0x3F;
  228. }
  229. BOOL CBase64::_IsBadMimeChar(BYTE nData)
  230. {
  231. switch(nData)
  232. {
  233. case '\r': case '\n': case '\t': case ' ' :
  234. case '\b': case '\a': case '\f': case '\v':
  235. return TRUE;
  236. default:
  237. return FALSE;
  238. }
  239. }
  240. void CBase64::_Init()
  241. { // Initialize Decoding table.
  242. int i;
  243. for(i = 0; i < 256; i++)
  244. CBase64::m_DecodeTable[i] = -2;
  245. for(i = 0; i < 64; i++)
  246. {
  247. CBase64::m_DecodeTable[Base64Digits[i]] = (CHAR)i;
  248. CBase64::m_DecodeTable[Base64Digits[i]|0x80] = (CHAR)i;
  249. }
  250. CBase64::m_DecodeTable['='] = -1;
  251. CBase64::m_DecodeTable['='|0x80] = -1;
  252. CBase64::m_Init = TRUE;
  253. }