📄 edcode.pas
字号:
unit EDCode;
interface
uses
Windows, SysUtils, Classes, Hutil32, Grobal2;
function EncodeMessage (smsg: TDefaultMessage): string;
function EncodeString (str: string): string;
function EncodeBuffer (buf: pChar; bufsize: integer): string;
function DecodeMessage (str: string): TDefaultMessage;
function DecodeString (str: string): string;
procedure DecodeBuffer (src: string; buf: PChar; bufsize: integer);
function MakeDefaultMsg(wIdent: Word;nRecog: Integer; wParam, wTag, wSeries: Word):TDefaultMessage;
var
CSEncode: TRTLCriticalSection;
implementation
var
EncBuf, TempBuf: PChar;
poslen:integer;
const
Code=$3c;
BufSize=8192;
function MakeDefaultMsg(wIdent: Word;nRecog: Integer; wParam, wTag, wSeries: Word):TDefaultMessage;
begin
Result.Recog:=nRecog;
Result.Ident:=wIdent;
Result.Param:=wParam;
Result.Tag:=wTag;
Result.Series:=wSeries;
end;
function MirEncode(pIn:PChar;Size:word;pOut:PChar):word;
var b1,bcal:byte;
bflag1,bflag2:byte;
i,iPtr,oPtr:word;
begin
b1:=0;
bCal:=0;
bFlag1:=0;
bFlag2:=0;
i:=0;
iPtr:=0;
oPtr:=0;
while iPtr<Size do
begin
b1:=Ord(pIn[iPtr])xor $eb;
inc(iPtr);
if i<2 then
begin
bCal:=b1;
bCal:=bCal shr 2;
bFlag1:=bCal;
bCal:=bCal and $3c;
b1:=b1 and 3;
bcal:=bcal or b1;
bcal:=bcal+$3b;
pOut[oPtr]:=Chr(bCal);
inc(oPtr);
bFlag2:=(bFlag1 and 3)or(bFlag2 shl 2);
end
else
begin
bCal:=b1;
bCal:=bCal and $3f;
bCal:=bCal+$3b;
pOut[oPtr]:=Chr(bCal);
inc(oPtr);
b1:=b1 shr 2;
b1:=b1 and $30;
b1:=b1 or bFlag2;
b1:=b1+$3b;
pOut[oPtr]:=chr(b1);
inc(oPtr);
bFlag2:=0;
end;
inc(i);
i:=i mod 3;
end;
pOut[oPtr]:=chr(0);
if i<>0 then
begin
pOut[oPtr]:=chr(bFlag2+$3b);
inc(oPtr);
pOut[oPtr]:=chr(0);
end;
Result:=oPtr;
end;
function MirDecode(pIn:String;Size:word;pOut:PChar):word;
var b1,b2,b3:byte;
c1,c2,c3,c4:byte;
i,oPtr:word;
x,y:word;
begin
i:=0;
oPtr:=0;
x:=Length(pIn) div 4;
if Length(pIn)>3 then
for i:=0 to x-1 do
begin
c1:=ord(pIn[i*4+1])-$3b;
c2:=ord(pIn[i*4+2])-$3b;
c3:=ord(pIn[i*4+3])-$3b;
c4:=ord(pIn[i*4+4])-$3b;
b1:=(c1 and $fc) shl 2; //11111100->11110000
b2:=(c1 and 3 ); //00000011
b3:=c4 and $c; //00001100
pOut[oPtr]:=chr((b1 or b2 or b3) xor $eb);
inc(oPtr);
b1:=(c2 and $fc) shl 2; //11111100->11110000
b2:=(c2 and 3 ); //00000011
b3:=(c4 and 3 )shl 2; //00000011 ->00001100
pOut[oPtr]:=chr((b1 or b2 or b3) xor $eb);
inc(oPtr);
b1:=(c4 and $30) shl 2; //00110000->11000000
pOut[oPtr]:=chr((c3 or b1)xor $eb);
inc(oPtr);
end;
y:=Length(pIn) mod 4;
if y=2 then
begin
c1:=ord(pIn[x*4+1])-$3b;
c2:=ord(pIn[x*4+2])-$3b;
b1:=(c1 and $fc) shl 2; //11111100->11110000
b2:=(c1 and 3 ); //00000011
b3:=(c2 and 3)shl 2; //00000011->00001100
pOut[oPtr]:=chr((b1 or b2 or b3) xor $eb);
inc(oPtr);
end;
if y=3 then
begin
c1:=ord(pIn[x*4+1])-$3b;
c2:=ord(pIn[x*4+2])-$3b;
c4:=ord(pIn[x*4+3])-$3b;
b1:=(c1 and $fc) shl 2; //11111100->11110000
b2:=(c1 and 3 ); //00000011
b3:=c4 and $c; //00001100
pOut[oPtr]:=chr((b1 or b2 or b3) xor $eb);
inc(oPtr);
b1:=(c2 and $fc) shl 2; //11111100->11110000
b2:=(c2 and 3 ); //00000011
b3:=(c4 and 3 )shl 2; //00000011 ->00001100
pOut[oPtr]:=chr((b1 or b2 or b3) xor $eb);
inc(oPtr);
end;
pOut[optr]:=#0;
result:=oPtr;
end;
function EncodeMessage (smsg: TDefaultMessage): string;
var
msg: TDefaultMessage;
EncBuf:pchar;
begin
try
EnterCriticalSection (CSencode);
Getmem(EncBuf,BufSize);
MirEncode(@sMsg,Sizeof(TDefaultMessage),EncBUf);
Result := StrPas(EncBUf);
Freemem(EncBuf);
finally
LeaveCriticalSection (CSencode);
end;
end;
function EncodeString (str: string): string;
var
msg: TDefaultMessage;
EncBuf:pchar;
begin
try
EnterCriticalSection (CSencode);
Result:='';
Getmem(EncBuf,BufSize);
MirEncode(Pchar(Str),Length(Str),EncBuf);
Result:=Strpas(EncBuf);
Freemem(EncBuf);
finally
LeaveCriticalSection (CSencode);
end;
end;
function EncodeBuffer (buf: pChar; bufsize: integer): string;
var
msg: TDefaultMessage;
EncBuf:pchar;
s:Integer;
begin
try
EnterCriticalSection (CSencode);
Getmem(EncBuf,BufSize);
MirEnCode(Buf,BufSize,EncBuf);
Result:=Strpas(EncBuf);
Freemem(EncBuf);
finally
LeaveCriticalSection (CSencode);
end;
end;
function DecodeMessage (str: string): TDefaultMessage;
var
msg: TDefaultMessage;
EncBuf:pchar;
begin
try
EnterCriticalSection (CSencode);
Getmem(EncBuf,BufSize);
MirDecode (str,BufSize, EncBuf);
Move (EncBuf^, msg, sizeof(TDefaultMessage));
Freemem(EncBuf);
Result := msg;
finally
LeaveCriticalSection (CSencode);
end;
end;
function DecodeString (str: string): String;
var
msg: TDefaultMessage;
EncBuf:pchar;
begin
try
EnterCriticalSection (CSencode);
Result:='';
Getmem(EncBuf,BufSize);
MirDecode (str,BufSize, EncBuf);
Result:=Strpas(EncBuf);
Freemem(EncBuf);
finally
LeaveCriticalSection (CSencode);
end;
end;
procedure DecodeBuffer (src: string; buf: PChar; bufsize: integer);
var
msg: TDefaultMessage;
EncBuf:pchar;
s:Integer;
begin
try
EnterCriticalSection (CSencode);
Getmem(EncBuf,BufSize);
s:=MirDecode (src,BufSize, EncBuf);
Move (EncBuf^, buf^, s);
Freemem(EncBuf);
finally
LeaveCriticalSection (CSencode);
end;
end;
initialization
begin
GetMem (EncBuf, 10240 + 100); //BUFFERSIZE + 100);
GetMem (TempBuf, 10240); //2048);
InitializeCriticalSection (CSEncode);
end;
finalization
begin
//FreeMem (EncBuf, BUFFERSIZE + 100);
//FreeMem (TempBuf, 2048);
DeleteCriticalSection (CSEncode);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -