Last change
on this file was 97, checked in by chronos, 2 years ago |
- Fixed: Quoted-printable encoded text was not handled correctly for multiple lines.
|
File size:
1.9 KB
|
Line | |
---|
1 | unit UQuotedPrintable;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils;
|
---|
9 |
|
---|
10 | function DecodeQuotedPrintable(Text: string; IgnoreErrors: Boolean = False): string;
|
---|
11 | function EncodeQuotedPrintable(Text: string; EncodeSpaces: Boolean = False): string;
|
---|
12 |
|
---|
13 | const
|
---|
14 | QuotedPrintableEscapeCharacter = '=';
|
---|
15 |
|
---|
16 | implementation
|
---|
17 |
|
---|
18 | uses
|
---|
19 | UCommon;
|
---|
20 |
|
---|
21 | resourcestring
|
---|
22 | SDecodeError = 'Decode error';
|
---|
23 |
|
---|
24 | function DecodeQuotedPrintable(Text: string; IgnoreErrors: Boolean = False): string;
|
---|
25 | var
|
---|
26 | I: Integer;
|
---|
27 | J: Integer;
|
---|
28 | C: Char;
|
---|
29 | IntValue: Integer;
|
---|
30 | begin
|
---|
31 | Result := '';
|
---|
32 | SetLength(Result, Length(Text));
|
---|
33 | I := 1;
|
---|
34 | J := 1;
|
---|
35 | while I <= Length(Text) do begin
|
---|
36 | C := Text[I];
|
---|
37 | if C = QuotedPrintableEscapeCharacter then begin
|
---|
38 | if ((I + 2) <= Length(Text)) and TryHexToInt(Text[I + 1] + Text[I + 2], IntValue) then begin
|
---|
39 | Result[J] := Chr(IntValue);
|
---|
40 | Inc(I, 2);
|
---|
41 | Inc(J);
|
---|
42 | SetLength(Result, Length(Result) - 2);
|
---|
43 | end else begin
|
---|
44 | if not IgnoreErrors then raise Exception.Create(SDecodeError)
|
---|
45 | else begin
|
---|
46 | Result[J] := '?';
|
---|
47 | Inc(J);
|
---|
48 | end;
|
---|
49 | end;
|
---|
50 | end else begin
|
---|
51 | Result[J] := C;
|
---|
52 | Inc(J);
|
---|
53 | end;
|
---|
54 | Inc(I);
|
---|
55 | end;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | function EncodeQuotedPrintable(Text: string; EncodeSpaces: Boolean): string;
|
---|
59 | var
|
---|
60 | I: Integer;
|
---|
61 | J: Integer;
|
---|
62 | C: Char;
|
---|
63 | LowerLimit: Char;
|
---|
64 | const
|
---|
65 | HexDigits : array[0..$F] of AnsiChar = '0123456789ABCDEF';
|
---|
66 | begin
|
---|
67 | if EncodeSpaces then LowerLimit := #33
|
---|
68 | else LowerLimit := #32;
|
---|
69 | Result := '';
|
---|
70 | SetLength(Result, Length(Text));
|
---|
71 | I := 1;
|
---|
72 | J := 1;
|
---|
73 | while I <= Length(Text) do begin
|
---|
74 | C := Text[I];
|
---|
75 | if (C = QuotedPrintableEscapeCharacter) or (C < LowerLimit) or (C > #126) then begin
|
---|
76 | SetLength(Result, Length(Result) + 2);
|
---|
77 | Result[J] := QuotedPrintableEscapeCharacter;
|
---|
78 | Result[J + 1] := HexDigits[Ord(C) shr 4];
|
---|
79 | Result[J + 2] := HexDigits[Ord(C) and $f];
|
---|
80 | Inc(J, 3);
|
---|
81 | end else begin
|
---|
82 | Result[J] := C;
|
---|
83 | Inc(J);
|
---|
84 | end;
|
---|
85 | Inc(I);
|
---|
86 | end;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | end.
|
---|
90 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.