📄 codecwizarddlg.h
字号:
if(m_nDataSize==nIdx)
{
EXITON(EnsureAlloced(m_nDataSize+4),FALSE);
m_nDataSize += 4;
}
*((DWORD *)(m_byData + nIdx)) = dw;
return TRUE;
};
BOOL ReplaceAt(int nIdx,char c){return ReplaceAt(nIdx,(BYTE)c);};
BOOL ReplaceAt(int nIdx,short sn){return ReplaceAt(nIdx,(WORD)sn);};
BOOL ReplaceAt(int nIdx,int n){return ReplaceAt(nIdx,(DWORD)n);};
BOOL ReplaceAt(int nIdx,unsigned int un){return ReplaceAt(nIdx,(DWORD)un);};
BOOL ReplaceAt(int nIdx,LPCVOID pvData,int nDataSize)
{
EXITON(nIdx<0 || nDataSize<=0 || m_nDataSize<nIdx,FALSE);
if(m_nDataSize==nIdx)
{
EXITON(EnsureAlloced(m_nDataSize+nDataSize),FALSE);
m_nDataSize += nDataSize;
}
memcpy(m_byData+nIdx,pvData,nDataSize);
return TRUE;
};
int Find(BYTE by)
{
EXITON(m_nDataSize<=0,-1);
for(int i=0;i<m_nDataSize;++i) if(m_byData[i]==by) return i;
return -1;
};
void ZeroTerminate()
{
if(m_nTotalSize - m_nDataSize < 1) Grow(1);
else m_byData[m_nDataSize] = '\0';
};
BOOL Insert(int nBefore,LPCVOID pvData,int nDataSize)
{
EXITON(!pvData || nDataSize<=0 || nBefore<0 || nBefore>=m_nDataSize,FALSE);
EXITON(EnsureAlloced(m_nDataSize+nDataSize)==FALSE,FALSE);
memmove(m_byData + nBefore + nDataSize,m_byData + nBefore,m_nDataSize - nBefore);
memcpy(m_byData + nBefore,pvData,nDataSize);
m_nDataSize += nDataSize;
assert(m_nDataSize<=m_nTotalSize);
return TRUE;
};
public:
ByteArray& operator<<(BYTE by){Append(&by,1);return *this;};
ByteArray& operator<<(WORD w){Append(&w,2);return *this;};
ByteArray& operator<<(DWORD dw){Append(&dw,4);return *this;};
ByteArray& operator<<(char c){Append(&c,1);return *this;};
ByteArray& operator<<(short sn){Append(&sn,2);return *this;};
ByteArray& operator<<(int n){Append(&n,4);return *this;};
ByteArray& operator<<(unsigned int un){Append(&un,4);return *this;};
ByteArray& operator<<(LPCSTR strData){Append(strData);return *this;};
public:
ByteArray(int nGrowSize=_DEFAULT_GROWSIZE) :
m_nGrowSize(nGrowSize),
m_nDataSize(0),
m_nTotalSize(0),
m_byData(NULL){assert(nGrowSize>0);};
BOOL operator==(const ByteArray& a)
{
EXITON(m_nDataSize!=a.m_nDataSize,FALSE);
EXITON(m_nDataSize==0,FALSE);
return memcmp(m_byData,a.m_byData,m_nDataSize)==0;
};
BOOL operator!=(const ByteArray& a){return !(*this==a);};
ByteArray(const ByteArray& a){*this = a;};
~ByteArray(){Free();};
};
//===ByteArray===
//===BitArray===
class CSCLSAPI BitArray : public ByteArray
{
BYTE m_byMask;
BYTE m_byRest;
private:
BOOL GetAByte(BYTE& byValue)
{
if(GetDataSize()<=0) return FALSE;
byValue = *GetDataPBYTE();
TrimLeft(1);
return TRUE;
};
public:
int GetBitPos(int nBitIdx) const
{
assert(nBitIdx>=0 && nBitIdx<GetDataSizeBits());
nBitIdx >>= 3;//nBitIdx /= 8;
return nBitIdx;
};
int GetDataSizeBits() const
{
int nBits = 0;
BYTE byMask = m_byMask << 1;
for(;byMask;++nBits) byMask <<= 1;
nBits += GetDataSize() * 8;
return nBits;
};
void Clear()
{
ByteArray::Clear();
m_byMask = 0x80;
m_byRest = 0x00;
};
void OutputABit(BYTE byBit)
{
if(byBit) m_byRest |= m_byMask;
m_byMask >>= 1;
if(m_byMask==0)
{
Append(&m_byRest,1);
m_byMask = 0x80;
m_byRest = 0x00;
}
};
void OutputBits(DWORD dwCode,int nBitCount)
{
assert(nBitCount<=32);
for(;nBitCount>0;--nBitCount)
{
OutputABit((BYTE)((dwCode & 0x80000000)>>24));
dwCode <<= 1;
}
};
void OutputQuart(BYTE byQuart)
{
switch(m_byMask)
{
case 0x80:
m_byRest = byQuart;
m_byMask = 0x08;
break;
case 0x08:
m_byRest |= byQuart >> 4;
Append(&m_byRest,1);
m_byMask = 0x80;
m_byRest = 0x00;
break;
default:
OutputBits((DWORD)byQuart,4);
}
};
BOOL InputABit(BYTE& byValue)
{
if(m_byMask==0x80)
{
if(!GetAByte(m_byRest)) return FALSE;
}
byValue = (m_byRest & m_byMask) ? 0x01 : 0x00;
m_byMask >>= 1;
if(m_byMask==0x00) m_byMask = 0x80;
return TRUE;
};
int InputBits(DWORD& dwValue,int nBitCount)
{
BYTE by;
int nObtained = 0;
dwValue = 0;
for(;nBitCount>0;--nBitCount,++nObtained)
{
if(!InputABit(by)) break;
dwValue |= (DWORD)by;
dwValue <<= 1;
}
return nObtained;
};
int InputQuart(BYTE& byQuart)
{
switch(m_byMask)
{
case 0x80:
if(!GetAByte(m_byRest)) return 0;
byQuart = (BYTE)(m_byRest >> 4);
m_byRest &= 0x0f;
m_byMask = 0x08;
break;
case 0x08:
byQuart = (BYTE)m_byRest;
m_byMask = 0x80;
m_byRest = 0x00;
break;
default:
{
DWORD dw = 0;
int nSize = InputBits(dw,4);
byQuart = (BYTE)(dw >> 24);
return nSize;
}
}
return 4;
};
void PushbackABit(BYTE byBit)
{
if(byBit) m_byRest |= m_byMask;
m_byMask >>= 1;
if(m_byMask==0)
{
Insert(0,&m_byRest,1);
m_byMask = 0x80;
m_byRest = 0x00;
}
};
void PushbackBits(DWORD dwCode,int nBitCount)
{
assert(nBitCount<=32);
for(;nBitCount>0;--nBitCount)
{
PushbackABit((BYTE)((dwCode & 0x80000000)>>24));
dwCode <<= 1;
}
};
void PushbackQuart(BYTE byValue)
{
switch(m_byMask)
{
case 0x80:
m_byRest = (BYTE)(byValue>>4);
m_byMask = 0x08;
break;
case 0x08:
m_byRest <<= 4;
m_byRest |= (BYTE)(byValue>>4);
m_byMask = 0x80;
break;
default:
PushbackBits((DWORD)byValue,4);
}
};
void DiscardABit()
{
switch(m_byMask)
{
case 0x80:
m_byRest = *(GetDataPBYTE() + GetDataSize() - 1);
TrimRight(1);
m_byRest &= 0xfe;
*(GetDataPBYTE() + GetDataSize() - 1) = m_byRest;
m_byRest >>= 1;
m_byMask = 0x1;
break;
default:
m_byRest >>= 1;
m_byMask <<= 1;
if(!m_byMask) m_byMask = 0x80;
}
};
void DiscardBits(int nBitCount)
{
assert(nBitCount<=32);
for(;nBitCount>0;--nBitCount)
{
DiscardABit();
}
};
void DiscardQuart()
{
DiscardBits(4);
};
void FlushOutput()
{
if(m_byMask!=0x80)
{
Append(&m_byRest,1);
Append(&m_byMask,1);
}
else Append((PVOID)"",1);
};
public:
BitArray()
{
m_byMask = 0x80;
m_byRest = 0x00;
};
~BitArray()
{
};
};
//===BitArray===
#define CSLCS_TRACE_CODEC
#ifdef CSLCS_TRACE_CODEC
extern DiskFile TraceFile;
extern char g_buf[512];
#endif
static const char *g_strHex = "0123456789abcdef";
static const BYTE g_strUTF7EncodeChar[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const BYTE g_strUTF7DecodeChar[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//00
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//10
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x3f,//20
0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x00,0x00,0x00,0x00,0x00,0x00,//30
0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,//40
0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x00,0x00,0x00,0x00,0x00,//50
0x00,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,//60
0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00,//70
};
//===MIMECodec===
class CSCLSAPI MIMECodec
{
MIMECodec *m_pCodec;
public:
MIMECodec* GetCodec() const{return m_pCodec;};
void SetCodec(MIMECodec *p){m_pCodec = p;};
public:
inline BYTE HexToBin(char c)
{
return c<='9' ? c - '0' : c<='F' ? c - 'A' + 10 : c - 'a' + 10;
};
inline BOOL IsHexChar(char c)
{
return (c>='0' && c<='9') || (c>='A' && c<='F') || (c>='a' && c<='f');
};
inline BOOL HexToBin(BYTE by){return HexToBin((char)by);};
inline BOOL IsHexChar(BYTE by){return IsHexChar((char)by);};
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
if(m_pCodec) return m_pCodec->Encode(pvInput,nInputSize,byOutput);
return 0;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
if(m_pCodec) return m_pCodec->Decode(pvInput,nInputSize,byOutput);
return 0;
};
int Encodef(LPCVOID pvInput,int nInputSize,DiskFile& df)
{
EXITON(!pvInput || nInputSize<=0,0);
char buf[8 * 1024];
int nLen = 0;
int nEncodeSize = 0;
ByteArray ba;
ba.Attach((BYTE *)buf,8*1024,0);
int nTotal = 0;
PSTR p = (PSTR)pvInput;
for(;nInputSize>0;)
{
if(nInputSize>1 * 1024) nLen = 1 * 1024;
else nLen = nInputSize;
nEncodeSize = Encode(p,nLen,ba);
nTotal += df.Write(buf,nEncodeSize);
ba.Clear();
nInputSize -= nLen;
p += nLen;
}
ba.Detach(NULL,NULL);
return nTotal;
};
int Decodef(LPCVOID pvInput,int nInputSize,DiskFile& df)
{
EXITON(!pvInput || nInputSize<=0,0);
char buf[8 * 1024];
int nLen = 0;
int nEncodeSize = 0;
ByteArray ba;
ba.Attach((BYTE *)buf,8*1024,0);
int nTotal = 0;
PSTR p = (PSTR)pvInput;
for(;nInputSize>0;)
{
if(nInputSize>1 * 1024) nLen = 1 * 1024;
else nLen = nInputSize;
nEncodeSize = Decode(p,nLen,ba);
nTotal += df.Write(buf,nEncodeSize);
ba.Clear();
nInputSize -= nLen;
p += nLen;
}
ba.Detach(NULL,NULL);
return nTotal;
};
int Encodeff(DiskFile& di,DiskFile& df)
{
char buf[8 * 1024];
char temp[1 * 1024];
int nLen = 0;
int nEncodeSize = 0;
ByteArray ba;
ba.Attach((BYTE *)buf,8*1024,0);
int nTotal = 0;
for(;(nLen=di.Read(temp,1*1024))!=0;)
{
nEncodeSize = Encode(temp,nLen,ba);
nTotal += df.Write(buf,nEncodeSize);
ba.Clear();
}
ba.Detach(NULL,NULL);
return nTotal;
};
int Decodeff(DiskFile& di,DiskFile& df)
{
char buf[8 * 1024];
char temp[1 * 1024];
int nLen = 0;
int nEncodeSize = 0;
ByteArray ba;
ba.Attach((BYTE *)buf,8*1024,0);
int nTotal = 0;
for(;(nLen=di.Read(temp,1*1024))!=0;)
{
nEncodeSize = Decode(temp,nLen,ba);
nTotal += df.Write(buf,nEncodeSize);
ba.Clear();
}
ba.Detach(NULL,NULL);
return nTotal;
};
public:
MIMECodec() : m_pCodec(NULL){};
~MIMECodec(){};
};
//===MIMECodec===
//===HexCodec===
class CSCLSAPI HexCodec : public MIMECodec
{
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
int i;
int nEncodedSize = 0;
BYTE *by = (BYTE *)pvInput;
for(;nInputSize>0;)
{
for(i=0;i<32 && nInputSize>0;++i,--nInputSize,nEncodedSize+=2,++by)
{
byOutput += (BYTE)g_strHex[*by >> 4];
byOutput += (BYTE)g_strHex[*by & 0xf];
}
//byOutput.Append("\r\n");
//if(i>=32) byOutput += (BYTE)'\t';
}
return nEncodedSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE b;
int nDecodedSize = 0;
BOOL bFinished = FALSE;
BYTE *by = (BYTE *)pvInput;
PSTR p = NULL;
for(;nInputSize>0 && bFinished==FALSE;)
{
p = strstr((PSTR)by,"\r\n");
if(p==NULL)
{
p = (PSTR)by;
p += nInputSize;
bFinished = TRUE;
}
for(;(*by=='\r' || *by=='\n' || *by=='\t' || *p==' ') && nInputSize>0 ;++by,--nInputSize);
if(nInputSize<=0 || *by=='\0') break;
for(;by<(BYTE *)p && *by && nInputSize>0;++nDecodedSize,nInputSize-=2)
{
assert(IsHexChar(by[0]) && IsHexChar(by[1]));
b = HexToBin(*by++);
b <<= 4;
b |= HexToBin(*by++);
byOutput += (BYTE)b;
}
by = (BYTE *)(p + 2);//skip "\r\n"
}
return nDecodedSize;
};
public:
HexCodec(){};
~HexCodec(){};
};
//===HexCodec===
//===Hex2CCodec===
class CSCLSAPI Hex2CCodec : public MIMECodec
{
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
int i;
int nEncodedSize = 0;
BYTE *by = (BYTE *)pvInput;
for(;nInputSize>0;)
{
for(i=0;i<16 && nInputSize>0;++i,--nInputSize,nEncodedSize+=2,++by)
{
byOutput += (PCSTR)"0x";
byOutput += (BYTE)g_strHex[*by >> 4];
byOutput += (BYTE)g_strHex[*by & 0xf];
byOutput += (BYTE)',';
}
if(i>=16) byOutput.Append("\r\n");
//if(i>=32) byOutput += (BYTE)'\t';
}
return nEncodedSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE b;
int nDecodedSize = 0;
BOOL bFinished = FALSE;
BYTE *by = (BYTE *)pvInput;
PSTR p = NULL;
for(;nInputSize>0 && bFinished==FALSE;)
{
p = strstr((PSTR)by,"\r\n");
if(p==NULL)
{
p = (PSTR)by;
p += nInputSize;
bFinished = TRUE;
}
for(;(*by=='\r' || *by=='\n' || *by=='\t' || *by==' ') && nInputSize>0 ;++by,--nInputSize);
if(nInputSize<=0 || *by=='\0') break;
for(;by<(BYTE *)p && *by && nInputSize>0;++nDecodedSize,nInputSize-=5)
{
assert(by[0]=='0' && by[1]=='x');
by += 2;
assert(IsHexChar(by[0]) && IsHexChar(by[1]));
b = HexToBin(*by++);
b <<= 4;
b |= HexToBin(*by++);
byOutput += (BYTE)b;
assert(*by==',');
++by;
}
by = (BYTE *)(p + 2);//skip "\r\n"
}
return nDecodedSize;
};
public:
Hex2CCodec(){};
~Hex2CCodec(){};
};
//===Hex2CCodec===
//===URLCodec===
class CSCLSAPI URLCodec : public MIMECodec
{
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
int nEncodedSize = 0;
PBYTE p = (PBYTE)pvInput;
for(;*p;++nEncodedSize)
{
if((*p>='0' && *p<='9') || (*p>='A' && *p<='Z') || (*p>='a' && *p<='z')
|| *p=='-' || *p=='.' || *p=='_') byOutput += (BYTE)*p++;
else if(*p==' ') ACT2(byOutput+=(BYTE)'+',++p);
else
{
byOutput += (BYTE)'%';
byOutput += (BYTE)g_strHex[(*p) >> 4];
byOutput += (BYTE)g_strHex[(*p) & 0xf];
++p;
}
}
return nEncodedSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE b;
PBYTE p = (PBYTE)pvInput;
int nDecoded = 0;
for(;*p && nInputSize>0;++nDecoded,--nInputSize)
{
switch(*p)
{
case '+':
byOutput += (BYTE)' ';
++p;
break;
case '%':
++p;//skip '%'
assert(IsHexChar(p[0]) && IsHexChar(p[1]) && nInputSize>=3);
b = HexToBin(*p++);
b <<= 4;
b |= HexToBin(*p++);
byOutput += (BYTE)b;
nInputSize -= 2;//another is dec'd in for-loop
break;
default:
byOutput += (BYTE)*p++;
}
}
return nDecoded;
};
public:
URLCodec(){};
~URLCodec(){};
};
//===URLCodec===
//===UTF7Codec===
class CSCLSAPI UTF7Codec : public MIMECodec
{
public:
inline BOOL IsUTF7Char(char c)
{
return (strchr((LPCSTR)g_strUTF7EncodeChar,c)!=NULL || c=='=');
};
inline BOOL IsUTF7Char(BYTE by)
{
return IsUTF7Char((char)by);
};
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
int i;
int nEncodedSize = 0;
int nPairs = nInputSize/3;
BYTE *by = (BYTE *)pvInput;
for(;nPairs>0;)
{
for(i=0;i<48 && nPairs>0;++i,--nPairs,nEncodedSize+=4,by+=3)
{
byOutput += (BYTE)g_strUTF7EncodeChar[by[0]>>2];
byOutput += (BYTE)g_strUTF7EncodeChar[((by[0] & 0x3)<<4) | (by[1]>>4)];
byOutput += (BYTE)g_strUTF7EncodeChar[((by[1] & 0xf)<<2) | (by[2]>>6)];
byOutput += (BYTE)g_strUTF7EncodeChar[by[2] & 0x3f];
}
//byOutput.Append("\r\n");
//if(i>=48) byOutput += (BYTE)'\t';
}
switch(nInputSize%3)
{
case 2:
byOutput += (BYTE)g_strUTF7EncodeChar[by[0]>>2];
byOutput += (BYTE)g_strUTF7EncodeChar[((by[0] & 0x3)<<4) | (by[1]>>4)];
byOutput += (BYTE)g_strUTF7EncodeChar[(by[1] & 0xf)<<2];
byOutput += (BYTE)'=';
nEncodedSize += 4;
break;
case 1:
byOutput += (BYTE)g_strUTF7EncodeChar[by[0]>>2];
byOutput += (BYTE)g_strUTF7EncodeChar[(by[0] & 0x3)<<4];
byOutput += (BYTE)'=';
byOutput += (BYTE)'=';
nEncodedSize += 4;
break;
default:
byOutput.TrimRight(2);//remove extra "\r\n"
}
return nEncodedSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -