www.pudn.com > ZJMailer > TextBuffer.cpp
// TextBuffer.cpp: implementation of the CTextBuffer class.
//
//////////////////////////////////////////////////////////////////////
// CTextBuffer & CBase64Buffer classes Implementation files
//
// Author: ÕÅ ½Ü (codez)
//
// Finished At Oct 2nd, 2002.
//
//==============================================================
// note:
// The CTextBuffer & CBase64Buffer classes are made by codez,
// you can modify and/or distribute this file, but do not remove
// this header please.
//
// This file is provided "as is" with no expressed or implied
// warranty.
//==============================================================
//
// If there is any bugs, please let me know.
// Thanks!
//
//
// Contact me:
//
// Email: jay_zephyr2002@yahoo.com.cn
// Home Page: http://www.xlrj.com/codez
//==============================================================
#include "stdafx.h"
#include "TextBuffer.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTextBuffer::CTextBuffer()
{
m_innerBuffer = NULL;
m_innerLength = 0;
}
CTextBuffer::CTextBuffer(TCHAR * text)
{
USES_CONVERSION;
m_innerBuffer = NULL;
m_innerLength = 0;
SetBuffer(T2A(text));
}
CTextBuffer::~CTextBuffer()
{
ReleaseBuffer();
}
bool CTextBuffer::SetBuffer(TCHAR * text)
{
USES_CONVERSION;
int len = _tcslen(text);
if (AllocBuffer(len))
{
lstrcpy(m_innerBuffer, T2A(text));
return true;
}
return false;
}
void CTextBuffer::ReleaseBuffer()
{
if (m_innerBuffer)
{
delete []m_innerBuffer;
m_innerBuffer = NULL;
m_innerLength = 0;
}
}
char * CTextBuffer::GetBuffer()
{
return m_innerBuffer;
}
bool CTextBuffer::AllocBuffer(int size)
{
if (size<=0)
return false;
char * tmpBuffer = new char[size+1];
if (tmpBuffer)
{
ReleaseBuffer();
m_innerBuffer = tmpBuffer;
m_innerLength = size;
return true;
}
return false;
}
int CTextBuffer::GetBufferLength() const
{
return m_innerLength;
}
//////////////////////////////////////////////////////////////////////
// CBase64Buffer Class
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBase64Buffer::CBase64Buffer()
{
}
CBase64Buffer::CBase64Buffer(TCHAR * text):CTextBuffer(text)
{
}
CBase64Buffer::~CBase64Buffer()
{
}
bool CBase64Buffer::Encode()
{
// Base64 Encode table
char Base64Tab[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int tmpLen, tmpLoop, tmpMod, tmpPad,
tmpIdxSrc, tmpIdxObj;
char * tmpBuf = NULL, tmpIdx;
tmpLen = m_innerLength;
if (tmpLen<=0)
return false;
// now, calculate Encoded Buffer param
__asm{
// save used register value
push eax
push ecx
push edx
// initialize the div number
xor edx,edx
mov eax, tmpLen
// set dived number
mov ecx, 3
div ecx
mov tmpLoop, eax
mov tmpMod, edx
// pop up
pop edx
pop ecx
pop eax
}
switch(tmpMod)
{
case 0:
tmpPad = 0;
tmpLen = 0;
break;
case 1:
tmpPad = 2;
tmpLen = 4;
break;
case 2:
tmpPad = 1;
tmpLen = 4;
break;
}
tmpLen += (tmpLoop<<2); // tmpLoop * 4;
tmpBuf = new char[tmpLen+1];
if (!tmpBuf)
return false;
tmpBuf[tmpLen]=0;
for (int i=0;i>2) & 0x3f;
break;
case 1:
ret = ((m_innerBuffer[idx]<<4) & 0x30) |
((m_innerBuffer[idx+1]>>4) & 0x0f);
break;
case 2:
ret = ((m_innerBuffer[idx+1]<<2) & 0x3c) |
((m_innerBuffer[idx+2]>>6) &0x03);
break;
case 3:
ret = m_innerBuffer[idx+2] & 0x3f;
}
return ret;
}
bool CBase64Buffer::Decode()
{
int tmpLen, tmpLoop, tmpIdxSrc, tmpIdxObj;
BYTE szBt[4];
if (m_innerLength<=0)
return false;
tmpLoop = m_innerLength>>2; // m_innerLength / 4;
tmpLen = (tmpLoop<<2) - tmpLoop; // tmpLoop * 3;
if (m_innerBuffer[m_innerLength-1]=='=')
{
tmpLen--;
if (m_innerBuffer[m_innerLength-2]=='=')
tmpLen--;
}
char * tmpBuf = new char[tmpLen+1];
tmpBuf[tmpLen]=0;
if (!tmpBuf)
return false;
for (int i=0;i>4) & 0x03);
if (szBt[2]!=0x80)
{
tmpBuf[tmpIdxObj+1] = ((szBt[1]<<4) & 0xf0) |
((szBt[2]>>2) & 0x0f);
}
else
{
tmpBuf[tmpIdxObj+1] = (szBt[1]<<4) & 0xf0;
delete []m_innerBuffer;
m_innerBuffer = tmpBuf;
m_innerLength = tmpLen;
return true;
}
if (szBt[3]!=0x80)
{
tmpBuf[tmpIdxObj+2] = ((szBt[2]<<6) & 0xc0) |
(szBt[3] & 0x3f);
}
else
{
tmpBuf[tmpIdxObj+2] = (szBt[2]<<6) & 0xc0;
delete []m_innerBuffer;
m_innerBuffer = tmpBuf;
m_innerLength = tmpLen;
return true;
}
}
delete []m_innerBuffer;
m_innerBuffer = tmpBuf;
m_innerLength = tmpLen;
return true;
}
BYTE CBase64Buffer::FindTab(char ch)
{
char Base64Tab[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
BYTE ret;
if ( (ch>='A') && (ch<='Z') )
{
ret = ch-'A';
}
else if ( (ch>='a') && (ch<='z') )
{
ret = ch - 'a';
ret += 26;
}
else if ( (ch>='0') && (ch<='9') )
{
ret = ch - '0';
ret += 52;
}
else
{
switch(ch)
{
case '+':
ret = 62;
break;
case '/':
ret = 63;
break;
case '=':
ret = 0x80; // user defined code
break;
default:
ret = 0xff; // user defined code
}
}
return ret;
}