Ignore:
Timestamp:
Dec 3, 2021, 11:47:11 AM (3 years ago)
Author:
chronos
Message:
  • Fixed: Long BASE64 encoded lines were not automatically wrapped to limit max line length.
  • Fixed: Missing quoted-printable encoding for saving files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UQuotedPrintable.pas

    r45 r46  
    2929  Keeper: Boolean;
    3030  Abort: Boolean;
    31   BufStream: TMemoryStream;
     31  InStream: TMemoryStream;
    3232  OutStream: TMemoryStream;
    3333begin
    3434  Result := '';
    35   BufStream := TMemoryStream.Create;
     35  InStream := TMemoryStream.Create;
    3636  OutStream := TMemoryStream.Create;
    3737  try
    3838  if Text <> '' then begin
    39     BufStream.Write(Text[1], Length(Text));
    40     BufStream.Position := 0;
     39    InStream.Write(Text[1], Length(Text));
     40    InStream.Position := 0;
    4141  end;
    4242  Abort := False;
     
    4848  { Skip any CR/LF's to get to the encoded stuff }
    4949  while True do begin
    50     if BufStream.Read(Char(InBuf[0]), 1) = 0then
     50    if InStream.Read(Char(InBuf[0]), 1) = 0then
    5151      Exit;
    5252    if ((InBuf[0] <> $0D) and (InBuf[0] <> $0A)) then begin
     
    7070      if (I > High(InBuf)) then
    7171        raise Exception.Create(SLineLengthErr);
    72       if BufStream.Read(Char(InBuf[I]), 1) = 0 then
     72      if InStream.Read(Char(InBuf[I]), 1) = 0 then
    7373        Break;
    7474      case InBuf[I] of
     
    195195  SetLength(Result, OutStream.Size);
    196196  OutStream.Position := 0;
    197   OutStream.Read(Result[1], Length(Result));
     197  if OutStream.Size > 0 then
     198    OutStream.Read(Result[1], Length(Result));
    198199  finally
    199200    OutStream.Free;
    200     BufStream.Free;
     201    InStream.Free;
    201202  end;
    202203end;
    203204
    204205function EncodeQuotedPrintable(Text: string): string;
     206var
     207  O, W: Integer;
     208  WordBuf, OutBuf: array[0..80] of AnsiChar;
     209  CurChar: AnsiChar;
     210  Abort: Boolean;
     211  InStream: TStream;
     212  OutStream: TMemoryStream;
     213
     214  procedure SendLine;
     215  begin
     216    if (OutBuf[O - 1] = #9) or (OutBuf[O - 1] = #32) then begin
     217      OutBuf[O] := '=';
     218      Inc(O);
     219    end;
     220    OutStream.Write(OutBuf, O);
     221    FillChar(OutBuf, SizeOf(OutBuf), #0);
     222    O := 0;
     223  end;
     224
     225  procedure AddWordToOutBuf;
     226  var
     227    J : Integer;
     228  begin
     229    if (O + W) > 74 then SendLine;
     230    for J := 0 to (W - 1) do begin
     231      OutBuf[O] := WordBuf[J];
     232      Inc(O);
     233    end;
     234    W := 0;
     235  end;
     236
     237  procedure AddHexToWord(B : Byte);
     238  const
     239    HexDigits : array[0..$F] of AnsiChar = '0123456789ABCDEF';
     240  begin
     241    if W > 73 then AddWordToOutBuf;
     242    WordBuf[W] := '=';
     243    WordBuf[W + 1] := HexDigits[B shr 4];
     244    WordBuf[W + 2] := HexDigits[B and $F];
     245    Inc(W, 3)
     246  end;
     247
    205248begin
    206   Result := Text;
     249  Result := '';
     250  InStream := TMemoryStream.Create;
     251  OutStream := TMemoryStream.Create;
     252  try
     253    if Text <> '' then begin
     254      InStream.Write(Text[1], Length(Text));
     255      InStream.Position := 0;
     256    end;
     257
     258    Abort := False;
     259    O := 0;
     260    W := 0;
     261    FillChar(OutBuf, SizeOf(OutBuf), #0);
     262    while (InStream.Read(CurChar, 1) = 1) and not Abort do begin
     263      if (Ord(CurChar) in [33..60, 62..126]) then begin
     264        WordBuf[W] := CurChar;
     265        Inc(W);
     266        if W > 74 then AddWordToOutBuf;
     267      end else if (CurChar = ' ') or (CurChar = #9) then begin
     268        WordBuf[W] := CurChar;
     269        Inc(W);
     270        AddWordToOutBuf;
     271      end else if (CurChar = #13) then begin
     272        AddWordToOutBuf;
     273        SendLine;
     274      end else if (CurChar = #10) then begin
     275        { Do nothing }
     276      end else begin
     277        AddHexToWord(Byte(CurChar));
     278      end;
     279    end;
     280    AddWordToOutBuf;
     281    OutStream.Write(OutBuf, O);
     282    SetLength(Result, OutStream.Size);
     283    OutStream.Position := 0;
     284    if OutStream.Size > 0 then
     285      OutStream.Read(Result[1], Length(Result));
     286  finally
     287    OutStream.Free;
     288    InStream.Free;
     289  end;
    207290end;
    208291
Note: See TracChangeset for help on using the changeset viewer.