Debian Codegraph @

plugins/wcx/base64/src/mimeinln.pas

raw
1 {==============================================================================|
2 | Project : Ararat Synapse | 001.001.011 |
3 |==============================================================================|
4 | Content: Inline MIME support procedures and functions |
5 |==============================================================================|
6 | Copyright (c)1999-2017, Lukas Gebauer |
7 | All rights reserved. |
8 | |
9 | Redistribution and use in source and binary forms, with or without |
10 | modification, are permitted provided that the following conditions are met: |
11 | |
12 | Redistributions of source code must retain the above copyright notice, this |
13 | list of conditions and the following disclaimer. |
14 | |
15 | Redistributions in binary form must reproduce the above copyright notice, |
16 | this list of conditions and the following disclaimer in the documentation |
17 | and/or other materials provided with the distribution. |
18 | |
19 | Neither the name of Lukas Gebauer nor the names of its contributors may |
20 | be used to endorse or promote products derived from this software without |
21 | specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
24 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
27 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
29 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
30 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
33 | DAMAGE. |
34 |==============================================================================|
35 | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
36 | Portions created by Lukas Gebauer are Copyright (c)2000-2006. |
37 | All Rights Reserved. |
38 |==============================================================================|
39 | Contributor(s): Copyright (C) 2022 Alexander Koblov ([email protected]) |
40 |==============================================================================|
41 | History: see HISTORY.HTM from distribution package |
42 | (Found at URL: http://www.ararat.cz/synapse/) |
43 |==============================================================================}
44
45 {:@abstract(Utilities for inline MIME)
46 Support for Inline MIME encoding and decoding.
47
48 Used RFC: RFC-2047, RFC-2231
49 }
50
51 {$IFDEF FPC}
52 {$MODE DELPHI}
53 {$ENDIF}
54 {$H+}
55
56 {$IFDEF UNICODE}
57 {$WARN IMPLICIT_STRING_CAST OFF}
58 {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
59 {$ENDIF}
60
61 unit MimeInLn;
62
63 interface
64
65 uses
66 SysUtils, Classes;
67
68 type
69 TSpecials = set of AnsiChar;
70
71 const
72 SpecialChar: TSpecials =
73 ['=', '(', ')', '[', ']', '<', '>', ':', ';', ',', '@', '/', '?', '\',
74 '"', '_'];
75 NonAsciiChar: TSpecials =
76 [#0..#31, #127..#255];
77
78 {:Returns a portion of the "Value" string located to the left of the "Delimiter"
79 string. If a delimiter is not found, results is original string.}
80 function SeparateLeft(const Value, Delimiter: string): string;
81
82 {:Returns the portion of the "Value" string located to the right of the
83 "Delimiter" string. If a delimiter is not found, results is original string.}
84 function SeparateRight(const Value, Delimiter: string): string;
85
86 {:Returns parameter value from string in format:
87 parameter1="value1"; parameter2=value2}
88 function GetParameter(const Value, Parameter: string): string;
89
90 {:Fetch string from left of Value string. This function ignore delimitesr inside
91 quotations.}
92 function FetchEx(var Value: string; const Delimiter, Quotation: string): string;
93
94 {:Decodes mime inline encoding (i.e. in headers).}
95 function InlineDecode(const Value: string): string;
96
97 {:Encodes string to MIME inline encoding.}
98 function InlineEncode(const Value: string): string;
99
100 {:Returns @true, if "Value" contains characters which require inline coding.}
101 function NeedInline(const Value: AnsiString): boolean;
102
103 {:Decodes mime inline encoding similar to @link(InlineDecode), but it checks
104 first that "Value" encoded by inline coding.}
105 function InlineDecodeEx(const Value: string): string;
106
107 {:Inline MIME encoding similar to @link(InlineEncode), but it checks first that
108 "Value" contains characters which require inline coding.}
109 function InlineEncodeEx(const Value: string): string;
110
111 implementation
112
113 uses
114 Base64, LConvEncoding, DCConvertEncoding;
115
116 {==============================================================================}
117
118 function UnquoteStr(const Value: string; Quote: Char): string;
119 var
120 n: integer;
121 inq, dq: Boolean;
122 c, cn: char;
123 begin
124 Result := '';
125 if Value = '' then
126 Exit;
127 if Value = Quote + Quote then
128 Exit;
129 inq := False;
130 dq := False;
131 for n := 1 to Length(Value) do
132 begin
133 c := Value[n];
134 if n <> Length(Value) then
135 cn := Value[n + 1]
136 else
137 cn := #0;
138 if c = quote then
139 if dq then
140 dq := False
141 else
142 if not inq then
143 inq := True
144 else
145 if cn = quote then
146 begin
147 Result := Result + Quote;
148 dq := True;
149 end
150 else
151 inq := False
152 else
153 Result := Result + c;
154 end;
155 end;
156
157 {==============================================================================}
158
159 function FetchEx(var Value: string; const Delimiter, Quotation: string): string;
160 var
161 b: Boolean;
162 begin
163 Result := '';
164 b := False;
165 while Length(Value) > 0 do
166 begin
167 if b then
168 begin
169 if Pos(Quotation, Value) = 1 then
170 b := False;
171 Result := Result + Value[1];
172 Delete(Value, 1, 1);
173 end
174 else
175 begin
176 if Pos(Delimiter, Value) = 1 then
177 begin
178 Delete(Value, 1, Length(delimiter));
179 break;
180 end;
181 b := Pos(Quotation, Value) = 1;
182 Result := Result + Value[1];
183 Delete(Value, 1, 1);
184 end;
185 end;
186 end;
187
188 {==============================================================================}
189
190 function SeparateLeft(const Value, Delimiter: string): string;
191 var
192 x: Integer;
193 begin
194 x := Pos(Delimiter, Value);
195 if x < 1 then
196 Result := Value
197 else
198 Result := Copy(Value, 1, x - 1);
199 end;
200
201 {==============================================================================}
202
203 function SeparateRight(const Value, Delimiter: string): string;
204 var
205 x: Integer;
206 begin
207 x := Pos(Delimiter, Value);
208 if x > 0 then
209 x := x + Length(Delimiter) - 1;
210 Result := Copy(Value, x + 1, Length(Value) - x);
211 end;
212
213 {==============================================================================}
214
215 function GetParameter(const Value, Parameter: string): string;
216 var
217 s: string;
218 v: string;
219 begin
220 Result := '';
221 v := Value;
222 while v <> '' do
223 begin
224 s := Trim(FetchEx(v, ';', '"'));
225 if Pos(Uppercase(parameter), Uppercase(s)) = 1 then
226 begin
227 Delete(s, 1, Length(Parameter));
228 s := Trim(s);
229 if s = '' then
230 Break;
231 if s[1] = '=' then
232 begin
233 Result := Trim(SeparateRight(s, '='));
234 Result := UnquoteStr(Result, '"');
235 break;
236 end;
237 end;
238 end;
239 end;
240
241 {==============================================================================}
242
243 function DecodeTriplet(const Value: AnsiString; Delimiter: AnsiChar): AnsiString;
244 var
245 x, l, lv: Integer;
246 c: AnsiChar;
247 b: Byte;
248 bad: Boolean;
249 begin
250 lv := Length(Value);
251 SetLength(Result, lv);
252 x := 1;
253 l := 1;
254 while x <= lv do
255 begin
256 c := Value[x];
257 Inc(x);
258 if c <> Delimiter then
259 begin
260 Result[l] := c;
261 Inc(l);
262 end
263 else
264 if x < lv then
265 begin
266 Case Value[x] Of
267 #13:
268 if (Value[x + 1] = #10) then
269 Inc(x, 2)
270 else
271 Inc(x);
272 #10:
273 if (Value[x + 1] = #13) then
274 Inc(x, 2)
275 else
276 Inc(x);
277 else
278 begin
279 bad := False;
280 Case Value[x] Of
281 '0'..'9': b := (Byte(Value[x]) - 48) Shl 4;
282 'a'..'f', 'A'..'F': b := ((Byte(Value[x]) And 7) + 9) shl 4;
283 else
284 begin
285 b := 0;
286 bad := True;
287 end;
288 end;
289 Case Value[x + 1] Of
290 '0'..'9': b := b Or (Byte(Value[x + 1]) - 48);
291 'a'..'f', 'A'..'F': b := b Or ((Byte(Value[x + 1]) And 7) + 9);
292 else
293 bad := True;
294 end;
295 if bad then
296 begin
297 Result[l] := c;
298 Inc(l);
299 end
300 else
301 begin
302 Inc(x, 2);
303 Result[l] := AnsiChar(b);
304 Inc(l);
305 end;
306 end;
307 end;
308 end
309 else
310 break;
311 end;
312 Dec(l);
313 SetLength(Result, l);
314 end;
315
316 {==============================================================================}
317
318 function DecodeQuotedPrintable(const Value: AnsiString): AnsiString;
319 begin
320 Result := DecodeTriplet(Value, '=');
321 end;
322
323 {==============================================================================}
324
325 function EncodeTriplet(const Value: AnsiString; Delimiter: AnsiChar;
326 Specials: TSpecials): AnsiString;
327 var
328 n, l: Integer;
329 s: AnsiString;
330 c: AnsiChar;
331 begin
332 SetLength(Result, Length(Value) * 3);
333 l := 1;
334 for n := 1 to Length(Value) do
335 begin
336 c := Value[n];
337 if c in Specials then
338 begin
339 Result[l] := Delimiter;
340 Inc(l);
341 s := IntToHex(Ord(c), 2);
342 Result[l] := s[1];
343 Inc(l);
344 Result[l] := s[2];
345 Inc(l);
346 end
347 else
348 begin
349 Result[l] := c;
350 Inc(l);
351 end;
352 end;
353 Dec(l);
354 SetLength(Result, l);
355 end;
356
357 {==============================================================================}
358
359 function EncodeQuotedPrintable(const Value: AnsiString): AnsiString;
360 begin
361 Result := EncodeTriplet(Value, '=', ['='] + NonAsciiChar);
362 end;
363
364 {==============================================================================}
365
366 function EncodeSafeQuotedPrintable(const Value: AnsiString): AnsiString;
367 begin
368 Result := EncodeTriplet(Value, '=', SpecialChar + NonAsciiChar);
369 end;
370
371 {==============================================================================}
372
373 function InlineDecode(const Value: string): string;
374 var
375 s, su, e, v: string;
376 x, y, z, n: Integer;
377 b: Boolean;
378 c: Char;
379
380 function SearchEndInline(const Value: string; be: Integer): Integer;
381 var
382 n, q: Integer;
383 begin
384 q := 0;
385 Result := 0;
386 for n := be + 2 to Length(Value) - 1 do
387 if Value[n] = '?' then
388 begin
389 Inc(q);
390 if (q > 2) and (Value[n + 1] = '=') then
391 begin
392 Result := n;
393 Break;
394 end;
395 end;
396 end;
397
398 begin
399 Result := '';
400 v := Value;
401 x := Pos('=?', v);
402 y := SearchEndInline(v, x);
403 // fix for broken coding
404 // with begin, but not with end.
405 if (x > 0) and (y <= 0) then
406 y := Length(Result);
407 while (y > x) and (x > 0) do
408 begin
409 s := Copy(v, 1, x - 1);
410 if Trim(s) <> '' then
411 Result := Result + s;
412 s := Copy(v, x, y - x + 2);
413 Delete(v, 1, y + 1);
414 su := Copy(s, 3, Length(s) - 4);
415 z := Pos('?', su);
416 if (Length(su) >= (z + 2)) and (su[z + 2] = '?') then
417 begin
418 e := SeparateLeft(Copy(su, 1, z - 1), '*');
419 c := UpperCase(su)[z + 1];
420 su := Copy(su, z + 3, Length(su) - z - 2);
421 if c = 'B' then
422 begin
423 s := DecodeStringBase64(su);
424 s := ConvertEncodingToUTF8(s, e, b);
425 if not b then Exit(EmptyStr);
426 end;
427 if c = 'Q' then
428 begin
429 s := '';
430 for n := 1 to Length(su) do
431 if su[n] = '_' then
432 s := s + ' '
433 else
434 s := s + su[n];
435 s := DecodeQuotedPrintable(s);
436 s := ConvertEncodingToUTF8(s, e, b);
437 if not b then Exit(EmptyStr);
438 end;
439 end;
440 Result := Result + s;
441 x := Pos('=?', v);
442 y := SearchEndInline(v, x);
443 end;
444 Result := Result + v;
445 end;
446
447 {==============================================================================}
448
449 function InlineEncode(const Value: string): string;
450 var
451 s, s1, e: string;
452 n: Integer;
453 begin
454 s := Value;
455 e := 'UTF-8';
456 s := EncodeSafeQuotedPrintable(s);
457 s1 := '';
458 Result := '';
459 for n := 1 to Length(s) do
460 if s[n] = ' ' then
461 begin
462 s1 := s1 + '_';
463 if Length(s1) > 32 then
464 begin
465 if Result <> '' then
466 Result := Result + ' ';
467 Result := Result + '=?' + e + '?Q?' + s1 + '?=';
468 s1 := '';
469 end;
470 end
471 else
472 s1 := s1 + s[n];
473 if s1 <> '' then
474 begin
475 if Result <> '' then
476 Result := Result + ' ';
477 Result := Result + '=?' + e + '?Q?' + s1 + '?=';
478 end;
479 end;
480
481 {==============================================================================}
482
483 function NeedInline(const Value: AnsiString): boolean;
484 var
485 n: Integer;
486 begin
487 Result := False;
488 for n := 1 to Length(Value) do
489 if Value[n] in (SpecialChar + NonAsciiChar - ['_']) then
490 begin
491 Result := True;
492 Break;
493 end;
494 end;
495
496 {==============================================================================}
497
498 function InlineDecodeEx(const Value: string): string;
499 begin
500 if Pos('=?', Value) > 0 then
501 Result := InlineDecode(Value)
502 else
503 Result := CeSysToUtf8(Value);
504 end;
505
506 {==============================================================================}
507
508 function InlineEncodeEx(const Value: string): string;
509 begin
510 if NeedInline(Value) then
511 Result := InlineEncode(Value)
512 else
513 Result := Value;
514 end;
515
516 end.