Changeset 46 for trunk/UQuotedPrintable.pas
- Timestamp:
- Dec 3, 2021, 11:47:11 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UQuotedPrintable.pas
r45 r46 29 29 Keeper: Boolean; 30 30 Abort: Boolean; 31 BufStream: TMemoryStream;31 InStream: TMemoryStream; 32 32 OutStream: TMemoryStream; 33 33 begin 34 34 Result := ''; 35 BufStream := TMemoryStream.Create;35 InStream := TMemoryStream.Create; 36 36 OutStream := TMemoryStream.Create; 37 37 try 38 38 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; 41 41 end; 42 42 Abort := False; … … 48 48 { Skip any CR/LF's to get to the encoded stuff } 49 49 while True do begin 50 if BufStream.Read(Char(InBuf[0]), 1) = 0then50 if InStream.Read(Char(InBuf[0]), 1) = 0then 51 51 Exit; 52 52 if ((InBuf[0] <> $0D) and (InBuf[0] <> $0A)) then begin … … 70 70 if (I > High(InBuf)) then 71 71 raise Exception.Create(SLineLengthErr); 72 if BufStream.Read(Char(InBuf[I]), 1) = 0 then72 if InStream.Read(Char(InBuf[I]), 1) = 0 then 73 73 Break; 74 74 case InBuf[I] of … … 195 195 SetLength(Result, OutStream.Size); 196 196 OutStream.Position := 0; 197 OutStream.Read(Result[1], Length(Result)); 197 if OutStream.Size > 0 then 198 OutStream.Read(Result[1], Length(Result)); 198 199 finally 199 200 OutStream.Free; 200 BufStream.Free;201 InStream.Free; 201 202 end; 202 203 end; 203 204 204 205 function EncodeQuotedPrintable(Text: string): string; 206 var 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 205 248 begin 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; 207 290 end; 208 291
Note:
See TracChangeset
for help on using the changeset viewer.