📄 xuucode.pas
字号:
unit xUUCode;
interface
uses
SysUtils,Classes;
function UUEncode(InString: string):String;
function UUDecode(InString: string):String;
implementation
const
Offset = 32;
function UUEncode(InString: string):String;
const
CharsPerLine = 60;
BytesPerHunk = 3;
SixBitMask = $3F;
var
OutString : String;
Line: array[0..CharsPerLine-1] of Char;
Hunk: array[0..2] of Byte;
Chars: array[0..3] of Byte;
x,k,m,n: Integer; //x:LineLength; m : BytesNum; n : BytesInLine
procedure FlushLine;
var
I: Integer;
begin
OutString := OutString +char(n + Offset);
for I := 0 to x - 1 do OutString := OutString +Line[I];
OutString := OutString +#13#10;
x := 0;
n := 0;
end;
procedure FlushHunk;
var
I: Integer;
begin
if x = CharsPerLine then FlushLine;
Chars[0] := Byte(Hunk[0] shr 2);
Chars[1] := Byte((Hunk[0] shl 4) + (Hunk[1] shr 4));
Chars[2] := Byte((Hunk[1] shl 2) + (Hunk[2] shr 6));
Chars[3] := Byte(Hunk[2] and SixBitMask);
for I := 0 to 3 do
begin
Line[x] := Char((Chars[I] and SixBitMask) + Offset);
Inc(x);
end;
Inc(n, m);
m := 0
end;
begin
OutString := '';
n := 0;
x := 0;
m := 0;
k:=1;
while k <= Length(InString) do
begin
if m = BytesPerHunk then FlushHunk;
Hunk[m] := ord(InString[k]);
Inc(k);
Inc(m);
end;
if m > 0 then FlushHunk;
FlushLine;
if x > 0 then FlushLine;
Result := OutString;
end;
const
UUDECORD_LINE_TOO_SHORT = 1;
UUDECORD_ILLEGAL_CHARS =2;
function UUDecode(InString: string):String;
var
oList : TStringList;
Line: string;
x,y,m,n,i,j: Integer;
Chars: array[0..3] of Byte;
Hunk: array[0..2] of Byte;
procedure RaiseError(Msg: string);
begin
if y > 0 then Msg := '行号:' + IntToStr(y) + ',消息:' + Msg;
raise EInOutError.Create(Msg);
end;
function NextCh: Char;
begin
Inc(x);
if x > Length(Line) then RaiseError('行太短。'); //UUDECORD_LINE_TOO_SHORT
if not (Line[x] in [' '..'`']) then RaiseError('行中包含非法字符。'); //UUDECORD_ILLEGAL_CHARS
if Line[x] = '`' then Result := ' '
else Result := Line[x]
end;
begin
Result := '';
oList := TStringList.Create;
oList.Text :=InString;
try
for y:= 0 to oList.Count-1 do
begin
Line := oList[y];
if (Length(Line) = 0) or (Line[1] in [' ', '`']) then RaiseError('编码数据中有空行。');
x := 0;
m := 3;
n := Byte(NextCh) - Offset;
for i := 1 to n do
begin
if m = 3 then
begin
for j := 0 to 3 do Chars[j] := Byte(NextCh) - Offset;
Hunk[0] := Byte((Chars[0] shl 2) + (Chars[1] shr 4));
Hunk[1] := Byte((Chars[1] shl 4) + (Chars[2] shr 2));
Hunk[2] := Byte((Chars[2] shl 6) + Chars[3]);
m := 0
end;
Result := Result + Char(Hunk[m]);
Inc(m);
end;
end;
finally
oList.Free;
end;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -