Delphi的Decode解码函数

待我称王封你为后i 2021-11-23 19:04 439阅读 0赞

用法:

uses Decode.pas

……

var

str : String;

…..

str := DecodeLine7Bit(‘=?gb2312?B?0MK9qCDOxNfWzsS1tS50eHQ=?=’);

…..

*********************************

//Decode.pas

unit Decode;

interface

uses

  1. SysUtils;

function DecodeLineUUCODE(const Buffer: String; Decoded: PChar): Integer;

function DecodeLineBASE64(const Buffer: String; Decoded: PChar): Integer;

function DecodeQuotedPrintable(Texto: String): String;

function DecodeLine7Bit(Texto: String): String;

implementation

// Decode an UUCODE encoded line

function DecodeLineUUCODE(const Buffer: String; Decoded: PChar): Integer;

const

CHARS_PER_LINE = 80;

Table: String = ‘`!”#$%&’’()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[[\]^_‘;

var

A24Bits: array[0..8 * CHARS_PER_LINE] of Boolean;

i, j, k, b: Word;

LineLen, ActualLen: Byte;

function p_ByteFromTable(Ch: Char): Byte;

var

  1. ij: Integer;

begin

  1. ij := Pos(Ch, Table);
  2. if (ij > 64) or (ij = 0) then begin
  3. if Ch = \#32 then
  4. Result := 0
  5. else
  6. raise Exception.Create('UUCODE: Message format error');
  7. end
  8. else
  9. Result := ij - 1;

end;

begin

if Buffer = ‘’ then begin

  1. Result := 0;
  2. Exit;

end;

LineLen := p_ByteFromTable(Buffer[1]);

ActualLen := 4 * LineLen div 3;

FillChar(A24Bits, 8 * CHARS_PER_LINE + 1, 0);

Result := LineLen;

if ActualLen <> (4 * CHARS_PER_LINE div 3) then

  1. ActualLen := Length(Buffer) - 1;

k := 0;

for i := 2 to ActualLen + 1 do begin

  1. b := p\_ByteFromTable(Buffer\[i\]);
  2. for j := 5 downto 0 do begin
  3. A24Bits\[k\] := b and (1 shl j) > 0;
  4. Inc(k);
  5. end;

end;

k := 0;

for i := 1 to CHARS_PER_LINE do begin

  1. b := 0;
  2. for j := 7 downto 0 do begin
  3. if A24Bits\[k\] then b := b or (1 shl j);
  4. Inc(k);
  5. end;
  6. Decoded\[i-1\] := Char(b);

end;

end;

// Decode a BASE64 encoded line

function DecodeLineBASE64(const Buffer: String; Decoded: PChar): Integer;

var

A1: array[1..4] of Byte;

B1: array[1..3] of Byte;

I, J: Integer;

BytePtr, RealBytes: Integer;

begin

BytePtr := 0;

Result := 0;

for J := 1 to Length(Buffer) do begin

  1. Inc(BytePtr);
  2. case Buffer\[J\] of
  3. 'A'..'Z': A1\[BytePtr\] := Ord(Buffer\[J\])-65;
  4. 'a'..'z': A1\[BytePtr\] := Ord(Buffer\[J\])-71;
  5. '0'..'9': A1\[BytePtr\] := Ord(Buffer\[J\])+4;
  6. '+': A1\[BytePtr\] := 62;
  7. '/': A1\[BytePtr\] := 63;
  8. '=': A1\[BytePtr\] := 64;
  9. end;
  10. if BytePtr = 4 then begin
  11. BytePtr := 0;
  12. RealBytes := 3;
  13. if A1\[1\] = 64 then RealBytes:=0;
  14. if A1\[3\] = 64 then begin
  15. A1\[3\] := 0;
  16. A1\[4\] := 0;
  17. RealBytes := 1;
  18. end;
  19. if A1\[4\] = 64 then begin
  20. A1\[4\] := 0;
  21. RealBytes := 2;
  22. end;
  23. B1\[1\] := A1\[1\]\*4 + (A1\[2\] div 16);
  24. B1\[2\] := (A1\[2\] mod 16)\*16+(A1\[3\] div 4);
  25. B1\[3\] := (A1\[3\] mod 4)\*64 + A1\[4\];
  26. for I := 1 to RealBytes do begin
  27. Decoded\[Result+I-1\] := Chr(B1\[I\]);
  28. end;
  29. Inc(Result, RealBytes);
  30. end;

end;

end;

// Decode a quoted-printable encoded string

function DecodeQuotedPrintable(Texto: String): String;

var

nPos: Integer;

nLastPos: Integer;

lFound: Boolean;

begin

Result := Texto;

lFound := True;

nLastPos := 0;

while lFound do begin

  1. lFound := False;
  2. if nLastPos < Length(Result) then
  3. nPos := Pos('=', Copy(Result, nLastPos+1, Length(Result)-nLastPos))+nLastPPos
  4. else
  5. nPos := 0;
  6. if (nPos < (Length(Result)-1)) and (nPos > nLastPos) then begin
  7. if (Result\[nPos+1\] in \['A'..'F', '0'..'9'\]) and (Result\[nPos+2\] in \['A'..''F', '0'..'9'\]) then begin
  8. Insert(Char(StrToInt('$'+Result\[nPos+1\]+Result\[nPos+2\])), Result, nPos);
  9. Delete(Result, nPos+1, 3);
  10. end
  11. else begin
  12. if (Result\[nPos+1\] = \#13) and (Result\[nPos+2\] = \#10) then begin
  13. Delete(Result, nPos, 3);
  14. end
  15. else begin
  16. if (Result\[nPos+1\] = \#10) and (Result\[nPos+2\] = \#13) then begin
  17. Delete(Result, nPos, 3);
  18. end
  19. else begin
  20. if (Result\[nPos+1\] = \#13) and (Result\[nPos+2\] <> \#10) then begin
  21. Delete(Result, nPos, 2);
  22. end
  23. else begin
  24. if (Result\[nPos+1\] = \#10) and (Result\[nPos+2\] <> \#13) then begin
  25. Delete(Result, nPos, 2);
  26. end;
  27. end;
  28. end;
  29. end;
  30. end;
  31. lFound := True;
  32. nLastPos := nPos;
  33. end
  34. else begin
  35. if nPos = Length(Result) then begin
  36. Delete(Result, nPos, 1);
  37. end;
  38. end;

end;

end;

// Decode an ISO8859-1 encoded line e.g. =?iso-8859-1?x?xxxxxx=?=

function DecodeLine7Bit(Texto: String): String;

var

Buffer: PChar;

Encoding: Char;

Size: Integer;

nPos1: Integer;

nPos2: Integer;

begin

Result := Trim(Texto);

if Length(Result) < 4 then begin

  1. Exit;

end;

if (Result[1] <> ‘=’) or (Result[2] <> ‘?’) then begin

  1. Exit;

end;

nPos1 := Pos(‘?’, Copy(Result, 3, Length(Result)-2))+2;

nPos2 := Pos(‘?=’, Result);

if (nPos1 > 0) and (nPos2 > nPos1) then begin

  1. Result := Copy(Result, nPos1+1, nPos2-nPos1-1);
  2. if (Result\[2\] = '?') and (UpCase(Result\[1\]) in \['B', 'Q', 'U'\]) then begin
  3. Encoding := UpCase(Result\[1\]);
  4. Result := Copy(Result, 3, Length(Result)-2);
  5. end
  6. else begin
  7. Encoding := 'Q';
  8. end;
  9. case Encoding of
  10. 'B': begin
  11. GetMem(Buffer, Length(Result));
  12. Size := DecodeLineBASE64(Result, Buffer);
  13. Buffer\[Size\] := \#0;
  14. Result := String(Buffer);
  15. end;
  16. 'Q': begin
  17. while Pos('\_', Result) > 0 do
  18. Result\[Pos('\_', Result)\] := \#32;
  19. Result := DecodeQuotedPrintable(Result);
  20. end;
  21. 'U': begin
  22. GetMem(Buffer, Length(Result));
  23. Size := DecodeLineUUCODE(Result, Buffer);
  24. Buffer\[Size\] := \#0;
  25. Result := String(Buffer);
  26. end;
  27. end;

end;

end;

转载于:https://www.cnblogs.com/MaxWoods/archive/2010/07/01/1768960.html

发表评论

表情:
评论列表 (有 0 条评论,439人围观)

还没有评论,来说两句吧...

相关阅读

    相关 decode函数

    做一个项目,这个项目是用DB2数据库的,sql中用到了decode函数。 举个例子说明下什么意思: select name,decode(gender,'1',男,'2',