📄 codecwizarddlg.h
字号:
EXITON(pvInput==NULL || nInputSize<=0,0);
int nPairs;
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;
nPairs = p - (PSTR)by;
if(nPairs>nInputSize) nPairs = nInputSize;
nInputSize -= nPairs;
nPairs >>= 2;//nPairs /= 4;
for(;nPairs>0 && *by;--nPairs,nDecodedSize+=3,by+=4)
{
byOutput += (BYTE)((g_strUTF7DecodeChar[by[0]] << 2) | ((g_strUTF7DecodeChar[by[1]] >> 4) & 0x3));
byOutput += (BYTE)((g_strUTF7DecodeChar[by[1]] << 4) | ((g_strUTF7DecodeChar[by[2]] >> 2) & 0xf));
byOutput += (BYTE)((g_strUTF7DecodeChar[by[2]] << 6) | (g_strUTF7DecodeChar[by[3]] & 0x3f));
}
by = (BYTE *)(p + 2);//skip "\r\n"
}
return nDecodedSize;
};
public:
UTF7Codec(){};
~UTF7Codec(){};
};
//===UTF7Codec===
//===Trans8Codec===
class CSCLSAPI Trans8Codec : public MIMECodec
{
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
BYTE temp;
BYTE b;
//transfer by 8
BYTE *by = (PBYTE)pvInput;
for(int nSize=nInputSize>>3;nSize>0;--nSize)
{
b = 0x80; //0
temp = by[0] & b;
temp |= (by[1] & b) >> 0x01;
temp |= (by[2] & b) >> 0x02;
temp |= (by[3] & b) >> 0x03;
temp |= (by[4] & b) >> 0x04;
temp |= (by[5] & b) >> 0x05;
temp |= (by[6] & b) >> 0x06;
temp |= (by[7] & b) >> 0x07;
byOutput += temp; //0
b >>= 1; //1
temp = (by[0] & b) << 0x01;
temp |= (by[1] & b);
temp |= (by[2] & b) >> 0x01;
temp |= (by[3] & b) >> 0x02;
temp |= (by[4] & b) >> 0x03;
temp |= (by[5] & b) >> 0x04;
temp |= (by[6] & b) >> 0x05;
temp |= (by[7] & b) >> 0x06;
byOutput += temp; //1
b >>= 1; //2
temp = (by[0] & b) << 0x02;
temp |= (by[1] & b) << 0x01;
temp |= (by[2] & b);
temp |= (by[3] & b) >> 0x01;
temp |= (by[4] & b) >> 0x02;
temp |= (by[5] & b) >> 0x03;
temp |= (by[6] & b) >> 0x04;
temp |= (by[7] & b) >> 0x05;
byOutput += temp; //2
b >>= 1; //3
temp = (by[0] & b) << 0x03;
temp |= (by[1] & b) << 0x02;
temp |= (by[2] & b) << 0x01;
temp |= (by[3] & b);
temp |= (by[4] & b) >> 0x01;
temp |= (by[5] & b) >> 0x02;
temp |= (by[6] & b) >> 0x03;
temp |= (by[7] & b) >> 0x04;
byOutput += temp; //3
b >>= 1; //4
temp = (by[0] & b) << 0x04;
temp |= (by[1] & b) << 0x03;
temp |= (by[2] & b) << 0x02;
temp |= (by[3] & b) << 0x01;
temp |= (by[4] & b);
temp |= (by[5] & b) >> 0x01;
temp |= (by[6] & b) >> 0x02;
temp |= (by[7] & b) >> 0x03;
byOutput += temp; //4
b >>= 1; //5
temp = (by[0] & b) << 0x05;
temp |= (by[1] & b) << 0x04;
temp |= (by[2] & b) << 0x03;
temp |= (by[3] & b) << 0x02;
temp |= (by[4] & b) << 0x01;
temp |= (by[5] & b);
temp |= (by[6] & b) >> 0x01;
temp |= (by[7] & b) >> 0x02;
byOutput += temp; //5
b >>= 1; //6
temp = (by[0] & b) << 0x06;
temp |= (by[1] & b) << 0x05;
temp |= (by[2] & b) << 0x04;
temp |= (by[3] & b) << 0x03;
temp |= (by[4] & b) << 0x02;
temp |= (by[5] & b) << 0x01;
temp |= (by[6] & b);
temp |= (by[7] & b) >> 0x01;
byOutput += temp; //6
b >>= 1; //7
temp = (by[0] & b) << 0x07;
temp |= (by[1] & b) << 0x06;
temp |= (by[2] & b) << 0x05;
temp |= (by[3] & b) << 0x04;
temp |= (by[4] & b) << 0x03;
temp |= (by[5] & b) << 0x02;
temp |= (by[6] & b) << 0x01;
temp |= (by[7] & b);
byOutput += temp; //7
by += 8;
}
//transfer by 4
for(nSize=(nInputSize % 8)>>2;nSize>0;--nSize)
{
b = 0x80; //0
temp = by[0] & b;
temp |= (by[1] & b) >> 0x01;
temp |= (by[2] & b) >> 0x02;
temp |= (by[3] & b) >> 0x03;
b >>= 4;
temp |= (by[0] & b);
temp |= (by[1] & b) >> 0x01;
temp |= (by[2] & b) >> 0x02;
temp |= (by[3] & b) >> 0x03;
byOutput += temp; //0
b <<= 3; //1
temp = (by[0] & b) << 0x01;
temp |= (by[1] & b);
temp |= (by[2] & b) >> 0x01;
temp |= (by[3] & b) >> 0x02;
b >>= 4;
temp |= (by[0] & b) << 0x01;
temp |= (by[1] & b);
temp |= (by[2] & b) >> 0x01;
temp |= (by[3] & b) >> 0x02;
byOutput += temp; //1
b <<= 3; //2
temp = (by[0] & b) << 0x02;
temp |= (by[1] & b) << 0x01;
temp |= (by[2] & b);
temp |= (by[3] & b) >> 0x01;
b >>= 4;
temp |= (by[0] & b) << 0x02;
temp |= (by[1] & b) << 0x01;
temp |= (by[2] & b);
temp |= (by[3] & b) >> 0x01;
byOutput += temp; //2
b <<= 3; //3
temp = (by[0] & b) << 0x03;
temp |= (by[1] & b) << 0x02;
temp |= (by[2] & b) << 0x01;
temp |= (by[3] & b);
b >>= 4;
temp |= (by[0] & b) << 0x03;
temp |= (by[1] & b) << 0x02;
temp |= (by[2] & b) << 0x01;
temp |= (by[3] & b);
byOutput += temp; //3
by += 4;
}
//transfer by 1
for(nSize=(nInputSize % 4);nSize>0;--nSize,by++)
{
byOutput += (BYTE)((*by>>4) | (*by<<4));
}
return nInputSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
return Encode(pvInput,nInputSize,byOutput);
};
public:
Trans8Codec()
{
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("TRANS8Codec");
#endif
};
~Trans8Codec()
{
};
};
//===Trans8Codec===
//===Trans2sCodec===
class CSCLSAPI Trans2sCodec : public MIMECodec
{
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE temp;
int nSize = nInputSize >> 1;
BYTE *by = (BYTE *)pvInput;
// 7 6 5 4 3 2 1 0
// 3 2 1 0 4 d c b
// f e d c 5 e f a
// b a 9 8 6 7 8 9
for(;nSize>0;--nSize,by += 2)
{
temp = by[0] << 4;
temp |= (by[0] >> 1) & 0x08;
temp |= (by[1] >> 3 ) & 0x07;
byOutput += temp;
temp = (by[0] << 2) & 0x80;
temp |= by[1] & 0x40;
temp |= (by[1] >> 2) & 0x20;
temp |= (by[1] << 2) & 0x10;
temp |= (by[0] >> 3) & 0x08;
temp |= (by[0] >> 5) & 0x04;
temp |= (by[1] << 1) & 0x02;
temp |= (by[1] >> 1) & 0x01;
byOutput += temp;
}
// 7 6 5 4 3 2 1 0 4 5 2 3 6 7 0 1
if(nSize=nInputSize)
{
temp = (by[0] << 3) & 0xa0;
temp |= (by[0] << 1) & 0x52;
//temp |= (by[0] << 3) & 0x20;
//temp |= (by[0] << 1) & 0x10;
temp |= (by[0] >> 3) & 0x08;
temp |= (by[0] >> 5) & 0x04;
//temp |= (by[0] << 1) & 0x02;
temp |= (by[0] >> 1) & 0x01;
byOutput += temp;
}
return nInputSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE temp;
int nSize = nInputSize >> 1;
BYTE *by = (BYTE *)pvInput;
// 3 2 1 0 7 6 5 4
// 4 d c b 3 2 1 0
// 5 e f a f e d c
// 6 7 8 9 b a 9 8
for(;nSize>0;--nSize,by += 2)
{
temp = by[0] >> 4;
temp |= (by[1] << 1) & 0x80;
temp |= (by[1] >> 1) & 0x40;
temp |= (by[1] >> 2) & 0x20;
temp |= (by[0] << 1) & 0x10;
byOutput += temp;
temp = (by[1] << 2) & 0x80;
temp |= by[1] & 0x40;
temp |= (by[0] >> 1) & 0x30;
temp |= (by[0] << 3) & 0x08;
temp |= (by[1] << 2) & 0x04;
temp |= (by[1] << 1) & 0x02;
temp |= (by[1] >> 1) & 0x01;
byOutput += temp;
}
// 4 5 2 3 6 7 0 1 7 6 5 4 3 2 1 0
if(nSize=nInputSize)
{
temp = (by[0] << 5) & 0x80;
temp |= (by[0] << 3) & 0x40;
temp |= (by[0] >> 1) & 0x29;
temp |= (by[0] >> 3) & 0x14;
//temp |= (by[0] >> 1) & 0x08;
//temp |= (by[0] >> 3) & 0x04;
temp |= (by[0] << 1) & 0x02;
//temp |= (by[0] >> 1) & 0x01;
byOutput += temp;
}
return nInputSize;
};
public:
Trans2sCodec()
{
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("TRANS2Codec\n");
#endif
};
~Trans2sCodec()
{
};
};
//===Trans2sCodec===
//===Trans1CycleCodec===
#define CSCLS_OPTIMIZE_FOR_X86
class CSCLSAPI Trans1CycleCodec : public MIMECodec
{
int m_nCycleDist;
public:
int TCGetCycleDistance() const
{
return m_nCycleDist;
};
void TCSetCycleDistance(int nCycleDist)
{
assert(nCycleDist>0 && nCycleDist<8);
m_nCycleDist = nCycleDist;
};
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
int nEncodedSize = nInputSize;
#ifndef CSCLS_OPTIMIZE_FOR_X86
BYTE *by = (BYTE *)pvInput;
register int c = m_nCycleDist;
register int d = 8 - c;
BYTE byMask = (BYTE)(1 << c) - (BYTE)1;
for(;nInputSize>0;--nInputSize)
{
byOutput += (BYTE)((*by>>c) | ((*by<<d) & byMask));
}
#else
EXITON(byOutput.EnsureAlloced(byOutput.GetDataSize() + nInputSize)==FALSE,0);
int nCycleDist = m_nCycleDist;
BYTE *by = byOutput.GetDataPBYTE() + byOutput.GetDataSize();
_asm
{
push ebx
push ecx
push esi
push edi
lea esi,pvInput
mov edi,by
mov ebx,nInputSize
ror ebx,1
mov ecx,nCycleDist
and ecx,0x07
rotate_loop1:
mov eax,[esi]
rol eax,cl
mov dword ptr [edi],eax
add esi,4
add edi,4
dec ebx
jnz rotate_loop1
mov ebx,nInputSize
and ebx,0x03
jz rotate_done
rotate_loop2:
mov al,byte ptr [esi]
rol al,cl
mov byte ptr [edi],al
inc esi
inc edi
dec ebx
jnz rotate_loop2
rotate_done:
pop edi
pop esi
pop ecx
pop ebx
}
byOutput.SetDataSize(byOutput.GetDataSize() + nInputSize);
#endif
return nEncodedSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
int nEncodedSize = nInputSize;
#ifndef CSCLS_OPTIMIZE_FOR_X86
BYTE *by = (BYTE *)pvInput;
register int c = m_nCycleDist;
register int d = 8 - c;
BYTE byMask = (BYTE)(1 << d) - (BYTE)1;
for(;nInputSize>0;--nInputSize)
{
byOutput += (BYTE)(((*by<<c) & byMask) | (*by>>d));
}
#else
EXITON(byOutput.EnsureAlloced(byOutput.GetDataSize() + nInputSize)==FALSE,0);
int nCycleDist = m_nCycleDist;
BYTE *by = byOutput.GetDataPBYTE() + byOutput.GetDataSize();
_asm
{
push ebx
push ecx
push esi
push edi
lea esi,pvInput
mov edi,by
mov ebx,nInputSize
ror ebx,1
mov ecx,nCycleDist
and ecx,0x07
rotate_loop1:
mov eax,[esi]
ror eax,cl
mov dword ptr [edi],eax
add esi,4
add edi,4
dec ebx
jnz rotate_loop1
mov ebx,nInputSize
and ebx,0x03
jz rotate_done
rotate_loop2:
mov al,byte ptr [esi]
ror al,cl
mov byte ptr [edi],al
inc esi
inc edi
dec ebx
jnz rotate_loop2
rotate_done:
pop edi
pop esi
pop ecx
pop ebx
}
byOutput.SetDataSize(byOutput.GetDataSize() + nInputSize);
#endif
return nEncodedSize;
};
public:
Trans1CycleCodec() : m_nCycleDist(4)
{
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("TRANS1Codec\n");
#endif
};
~Trans1CycleCodec()
{
};
};
//===Trans1CycleCodec===
//===RLECodec===
class CSCLSAPI RLECodec : public MIMECodec
{
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
int nEncodedSize = nInputSize;
BYTE *p = (BYTE *)pvInput;
BYTE *q;
int nSame;
int nLenPos;
for(;nInputSize>0;)
{
q = p;
nSame = 1;
byOutput += (BYTE)(nSame + 0x80);
nLenPos = byOutput.GetDataSize() - 1;
byOutput += *p++;
--nInputSize;
if(nInputSize<=0)
{
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"※发现最后一个单独的数据:%02x\n",*(p-1));
TraceFile.Write(g_buf);
#endif
break;
}
if(*p==*q)
{
for(;*p==*q && nInputSize>0 && nSame<=0x7f;++nSame,--nInputSize) ++p;
byOutput[nLenPos] = (BYTE)nSame;
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"■发现%02x个相同的数据:%02x\n",nSame,*q);
TraceFile.Write(g_buf);
#endif
}
else
{
for(;*p!=*q && nInputSize>0 && nSame<0x7f;++nSame,--nInputSize,++q)
{
if(nInputSize>1 && p[0]!=p[1]) byOutput += *p++;
else break;
}
byOutput[nLenPos] = (BYTE)(nSame + 0x80);
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"☆发现%02x个不相同的数据:%02x-%02x\n",nSame,byOutput[nLenPos+1],*(p-1));
TraceFile.Write(g_buf);
#endif
}
}
return nEncodedSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
int nEncodedSize = nInputSize;
int nSame;
BYTE *p = (PBYTE)pvInput;
for(;nInputSize>0;)
{
nSame = (int)(*p++);
--nInputSize;
if(nSame<=0x80)
{
assert(nInputSize>=1);
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"■发现%02x个相同的数据:%02x\n",nSame,*p);
TraceFile.Write(g_buf);
#endif
--nInputSize;
for(;nSame>0;--nSame) byOutput += *p;
++p;
}
else
{
//nSame = (BYTE)(~nSame);
//++nSame;
nSame -= 0x80;
assert(nInputSize>=nSame);
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"☆发现%02x个不相同的数据:%02x",nSame,*p);
TraceFile.Write(g_buf);
#endif
nInputSize -= nSame; //nSame<0
for(;nSame>0;--nSame) byOutput += *p++;
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"-%02x\n",*(p-1));
TraceFile.Write(g_buf);
#endif
}
}
return nEncodedSize;
};
public:
RLECodec()
{
#ifdef CSLCS_TRACE_CODEC
TraceFile.Write("RLECodec\n");
#endif
};
~RLECodec()
{
};
};
//===RLECodec===
//===RLEHexCodec===
class CSCLSAPI RLEHexCodec : public MIMECodec
{
private:
void RHCGenerateSameRecord(BitArray& bi,BitArray& bo,BYTE& x,BYTE& y)
{
int nSame = 2;
for(;nSame<=7;++nSame)
{
if(bi.InputQuart(y)!=4 || y!=x) break;
}
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"■发现%02x个相同的数据:%02x\n",nSame,x);
TraceFile.Write(g_buf);
#endif
bo.OutputQuart((BYTE)(nSame<<4));
bo.OutputQuart((BYTE)(x<<4));
x = y;
};
void RHCGenerateDiffRecord(BitArray& bi,BitArray& bo,BYTE& x,BYTE& y)
{
bo.OutputQuart((BYTE)0x90);
int nBitPos = bo.GetDataSize();
BOOL bLow = (bo.GetDataSizeBits() % 8)==0;
bo.OutputQuart((BYTE)(x<<4));
int nSame = 1;
BYTE z = 0x00;
#ifdef CSLCS_TRACE_CODEC
BYTE byEnd = x;
#endif
for(;nSame<7;++nSame)
{
if(bi.InputQuart(z)!=4 || y==z) break;
bo.OutputQuart((BYTE)(y<<4));
#ifdef CSLCS_TRACE_CODEC
byEnd = y;
#endif
y = z;
++z; //make y!=z
}
#ifdef CSLCS_TRACE_CODEC
sprintf(g_buf,"☆发现%02x个不相同的数据:%02x-%02x\n",nSame,x,byEnd);
TraceFile.Write(g_buf);
#endif
if(nSame>1)
{
nSame += 0x08;
if(bLow)
{
bo[nBitPos] &= 0xf0;
bo[nBitPos] |= (BYTE)nSame;
}
else
{
bo[nBitPos] &= 0x0f;
bo[nBitPos] |= (BYTE)(nSame<<4);
}
}
if(y==z)
{
x = y;
y = z;
RHCGenerateSameRecord(bi,bo,x,y);
}
else x = y;
};
public:
virtual int Encode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE x,y;
BitArray bi;
BitArray bo;
bi.Clone(pvInput,nInputSize);
x = y = (BYTE)0x00;
nInputSize = bi.GetDataSizeBits() >> 2;
bi.InputQuart(x);
for(;bi.InputQuart(y)==4;)
{
if(x==y) RHCGenerateSameRecord(bi,bo,x,y);//same found
else RHCGenerateDiffRecord(bi,bo,x,y);
}
RHCGenerateDiffRecord(bi,bo,x,y);
bo.FlushOutput();
byOutput.Append(bo.GetDataPBYTE(),bo.GetDataSize());
return nInputSize;
};
virtual int Decode(LPCVOID pvInput,int nInputSize,ByteArray& byOutput)
{
EXITON(pvInput==NULL || nInputSize<=0,0);
BYTE x,y;
BitArray bi;
BitArray bo;
bi.Clone(pvInput,nInputSize);
int nEncodedSize = nInputSize;
nInputSize = bi.GetDataSizeBits() >> 2;
x = y = 0x00;
for(;nInputSize>1;)
{
bi.InputQuart(x);
--nInputSize;
assert(nInputSize>=2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -