1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_TOOLS_INETMIME_HXX
#define INCLUDED_TOOLS_INETMIME_HXX
#include <boost/ptr_container/ptr_vector.hpp>
#include <tools/toolsdllapi.h>
#include <rtl/alloc.h>
#include <rtl/character.hxx>
#include <rtl/string.hxx>
#include <rtl/strbuf.hxx>
#include <rtl/ustring.hxx>
#include <rtl/tencinfo.h>
#include <tools/debug.hxx>
#include <tools/errcode.hxx>
class DateTime;
class INetContentTypeParameterList;
class INetMIMECharsetList_Impl;
class INetMIMEOutputSink;
class TOOLS_DLLPUBLIC INetMIME
{
public:
enum { SOFT_LINE_LENGTH_LIMIT = 76,
HARD_LINE_LENGTH_LIMIT = 998 };
/** The various types of message header field bodies, with respect to
encoding and decoding them.
@descr At the moment, five different types of header fields suffice
to describe how to encoded and decode any known message header field
body, but need for more types may arise in the future as new header
fields are introduced.
@descr The following is an exhaustive list of all the header fields
currently known to our implementation. For every header field, it
includes a 'canonic' (with regard to capitalization) name, a grammar
rule for the body (using RFC 822 and RFC 2234 conventions), a list of
relevant sources of information, and the HeaderFieldType value to use
with that header field. The list is based on RFC 2076 and draft-
palme-mailext-headers-02.txt (see also <http://www.dsv.su.se/~jpalme/
ietf/jp-ietf-home.html#anchor1003783>).
Approved: address ;RFC 1036; HEADER_FIELD_ADDRESS
bcc: #address ;RFCs 822, 2047; HEADER_FIELD_ADDRESS
cc: 1#address ;RFCs 822, 2047; HEADER_FIELD_ADDRESS
Comments: *text ;RFCs 822, RFC 2047; HEADER_FIELD_TEXT
Content-Base: absoluteURI ;RFC 2110; HEADER_FIELD_TEXT
Content-Description: *text ;RFC 2045, RFC 2047; HEADER_FIELD_TEXT
Content-Disposition: disposition-type *(";" disposition-parm)
;RFC 1806; HEADER_FIELD_STRUCTURED
Content-ID: msg-id ;RFC 2045, RFC 2047; HEADER_FIELD_MESSAGE_ID
Content-Location: absoluteURI / relativeURI ;RFC 2110;
HEADER_FIELD_TEXT
Content-Transfer-Encoding: mechanism ;RFC 2045, RFC 2047;
HEADER_FIELD_STRUCTURED
Content-Type: type "/" subtype *(";" parameter) ;RFC 2045, RFC 2047;
HEADER_FIELD_STRUCTURED
Control: *text ;RFC 1036; HEADER_FIELD_TEXT
Date: date-time ;RFC 822, RFC 1123, RFC 2047; HEADER_FIELD_STRUCTURED
Distribution: 1#atom ;RFC 1036; HEADER_FIELD_STRUCTURED
Encrypted: 1#2word ;RFC 822, RFC 2047; HEADER_FIELD_STRUCTURED
Expires: date-time ;RFC 1036; HEADER_FIELD_STRUCTURED
Followup-To: 1#(atom *("." atom)) ;RFC 1036; HEADER_FIELD_STRUCTURED
From: mailbox / 1#mailbox ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
In-Reply-To: *(phrase / msg-id) ;RFC 822, RFC 2047;
HEADER_FIELD_ADDRESS
Keywords: #phrase ;RFC 822, RFC 2047; HEADER_FIELD_PHRASE
MIME-Version: 1*DIGIT "." 1*DIGIT ;RFC 2045, RFC 2047;
HEADER_FIELD_STRUCTURED
Message-ID: msg-id ;RFC 822, RFC 2047; HEADER_FIELD_MESSAGE_ID
Newsgroups: 1#(atom *("." atom)) ;RFC 1036, RFC 2047;
HEADER_FIELD_STRUCTURED
Organization: *text ;RFC 1036; HEADER_FIELD_TEXT
Received: ["from" domain] ["by" domain] ["via" atom] *("with" atom)
["id" msg-id] ["for" addr-spec] ";" date-time ;RFC 822, RFC 1123,
RFC 2047; HEADER_FIELD_STRUCTURED
References: *(phrase / msg-id) ;RFC 822, RFC 2047;
HEADER_FIELD_ADDRESS
Reply-To: 1#address ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Resent-Date: date-time ;RFC 822, RFC 1123, RFC 2047;
HEADER_FIELD_STRUCTURED
Resent-From: mailbox / 1#mailbox ;RFC 822, RFC 2047;
HEADER_FIELD_ADDRESS
Resent-Message-ID: msg-id ;RFC 822, RFC 2047; HEADER_FIELD_MESSAGE_ID
Resent-Reply-To: 1#address ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Resent-Sender: mailbox ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Resent-To: 1#address ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Resent-bcc: #address ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Resent-cc: 1#address ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Return-path: route-addr / ("<" ">") ;RFC 822, RFC 1123, RFC 2047;
HEADER_FIELD_STRUCTURED
Return-Receipt-To: address ;Not Internet standard;
HEADER_FIELD_ADDRES
Sender: mailbox ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
Subject: *text ;RFC 822, RFC 2047; HEADER_FIELD_TEXT
Summary: *text ;RFC 1036; HEADER_FIELD_TEXT
To: 1#address ;RFC 822, RFC 2047; HEADER_FIELD_ADDRESS
X-CHAOS-Marked: "YES" / "NO" ;local; HEADER_FIELD_STRUCTURED
X-CHAOS-Read: "YES" / "NO" ;local; HEADER_FIELD_STRUCTURED
X-CHAOS-Recipients: #*("<" atom word ">") ;local;
HEADER_FIELD_STRUCTURED
X-CHAOS-Size: 1*DIGIT ;local; HEADER_FIELD_STRUCTURED
X-Mailer: *text ;Not Internet standard; HEADER_FIELD_TEXT
X-Mozilla-Status: 4HEXDIG ;Mozilla; HEADER_FIELD_STRUCTURED
X-Newsreader: *text ;Not Internet standard; HEADER_FIELD_TEXT
X-Priority: "1" / "2" / "3" / "4" / "5" ;Not Internet standard;
HEADER_FIELD_STRUCTURED
Xref: sub-domain
1*((atom / string) *("." (atom / string)) ":" msg-number)
;RFCs 1036, 2047, local; HEADER_FIELD_STRUCTURED
*/
enum HeaderFieldType
{
HEADER_FIELD_TEXT,
HEADER_FIELD_STRUCTURED,
HEADER_FIELD_PHRASE,
HEADER_FIELD_MESSAGE_ID,
HEADER_FIELD_ADDRESS
};
/** Check for ISO 8859-1 character.
@param nChar Some UCS-4 character.
@return True if nChar is a ISO 8859-1 character (0x00--0xFF).
*/
static inline bool isISO88591(sal_uInt32 nChar);
/** Check for US-ASCII control character.
@param nChar Some UCS-4 character.
@return True if nChar is a US-ASCII control character (US-ASCII
0x00--0x1F or 0x7F).
*/
static inline bool isControl(sal_uInt32 nChar);
/** Check for US-ASCII white space character.
@param nChar Some UCS-4 character.
@return True if nChar is a US-ASCII white space character (US-ASCII
0x09 or 0x20).
*/
static inline bool isWhiteSpace(sal_uInt32 nChar);
/** Check for US-ASCII visible character.
@param nChar Some UCS-4 character.
@return True if nChar is a US-ASCII visible character (US-ASCII
0x21--0x7E).
*/
static inline bool isVisible(sal_uInt32 nChar);
/** Check for US-ASCII Base 64 digit character.
@param nChar Some UCS-4 character.
@return True if nChar is a US-ASCII Base 64 digit character (US-ASCII
'A'--'Z', 'a'--'z', '0'--'9', '+', or '/').
*/
static inline bool isBase64Digit(sal_uInt32 nChar);
/** Check whether some character is valid within an RFC 822 <atom>.
@param nChar Some UCS-4 character.
@return True if nChar is valid within an RFC 822 <atom> (US-ASCII
'A'--'Z', 'a'--'z', '0'--'9', '!', '#', '$', '%', '&', ''', '*', '+',
'-', '/', '=', '?', '^', '_', '`', '{', '|', '}', or '~').
*/
static bool isAtomChar(sal_uInt32 nChar);
/** Check whether some character is valid within an RFC 2045 <token>.
@param nChar Some UCS-4 character.
@return True if nChar is valid within an RFC 2047 <token> (US-ASCII
'A'--'Z', 'a'--'z', '0'--'9', '!', '#', '$', '%', '&', ''', '*', '+',
'-', '.', '^', '_', '`', '{', '|', '}', or '~').
*/
static bool isTokenChar(sal_uInt32 nChar);
/** Check whether some character is valid within an RFC 2047 <token>.
@param nChar Some UCS-4 character.
@return True if nChar is valid within an RFC 2047 <token> (US-ASCII
'A'--'Z', 'a'--'z', '0'--'9', '!', '#', '$', '%', '&', ''', '*', '+',
'-', '^', '_', '`', '{', '|', '}', or '~').
*/
static bool isEncodedWordTokenChar(sal_uInt32 nChar);
/** Check whether some character is valid within an RFC 2060 <atom>.
@param nChar Some UCS-4 character.
@return True if nChar is valid within an RFC 2060 <atom> (US-ASCII
'A'--'Z', 'a'--'z', '0'--'9', '!', '#', '$', '&', ''', '+', ',', '-',
'.', '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`',
'|', '}', or '~').
*/
static bool isIMAPAtomChar(sal_uInt32 nChar);
/** Get the digit weight of a US-ASCII character.
@param nChar Some UCS-4 character.
@return If nChar is a US-ASCII (decimal) digit character (US-ASCII
'0'--'9'), return the corresponding weight (0--9); otherwise,
return -1.
*/
static inline int getWeight(sal_uInt32 nChar);
/** Get the hexadecimal digit weight of a US-ASCII character.
@param nChar Some UCS-4 character.
@return If nChar is a US-ASCII hexadecimal digit character (US-ASCII
'0'--'9', 'A'--'F', or 'a'--'f'), return the corresponding weight
(0--15); otherwise, return -1.
*/
static inline int getHexWeight(sal_uInt32 nChar);
/** Get the Base 64 digit weight of a US-ASCII character.
@param nChar Some UCS-4 character.
@return If nChar is a US-ASCII Base 64 digit character (US-ASCII
'A'--'F', or 'a'--'f', '0'--'9', '+', or '/'), return the
corresponding weight (0--63); if nChar is the US-ASCII Base 64 padding
character (US-ASCII '='), return -1; otherwise, return -2.
*/
static inline int getBase64Weight(sal_uInt32 nChar);
/** Get a hexadecimal digit encoded as US-ASCII.
@param nWeight Must be in the range 0--15, inclusive.
@return The canonic (i.e., upper case) hexadecimal digit
corresponding to nWeight (US-ASCII '0'--'9' or 'A'--'F').
*/
static sal_uInt32 getHexDigit(int nWeight);
static inline bool isHighSurrogate(sal_uInt32 nUTF16);
static inline bool isLowSurrogate(sal_uInt32 nUTF16);
static inline sal_uInt32 toUTF32(sal_Unicode cHighSurrogate,
sal_Unicode cLowSurrogate);
/** Check two US-ASCII strings for equality, ignoring case.
@param pBegin1 Points to the start of the first string, must not be
null.
@param pEnd1 Points past the end of the first string, must be >=
pBegin1.
@param pString2 Points to the start of the null terminated second
string, must not be null.
@return True if the two strings are equal, ignoring the case of US-
ASCII alphabetic characters (US-ASCII 'A'--'Z' and 'a'--'z').
*/
static bool equalIgnoreCase(const sal_Char * pBegin1,
const sal_Char * pEnd1,
const sal_Char * pString2);
/** Check two US-ASCII strings for equality, ignoring case.
@param pBegin1 Points to the start of the first string, must not be
null.
@param pEnd1 Points past the end of the first string, must be >=
pBegin1.
@param pString2 Points to the start of the null terminated second
string, must not be null.
@return True if the two strings are equal, ignoring the case of US-
ASCII alphabetic characters (US-ASCII 'A'--'Z' and 'a'--'z').
*/
static bool equalIgnoreCase(const sal_Unicode * pBegin1,
const sal_Unicode * pEnd1,
const sal_Char * pString2);
static inline bool startsWithLineBreak(const sal_Char * pBegin,
const sal_Char * pEnd);
static inline bool startsWithLineBreak(const sal_Unicode * pBegin,
const sal_Unicode * pEnd);
static inline bool startsWithLineFolding(const sal_Char * pBegin,
const sal_Char * pEnd);
static inline bool startsWithLineFolding(const sal_Unicode * pBegin,
const sal_Unicode * pEnd);
static bool startsWithLinearWhiteSpace(const sal_Char * pBegin,
const sal_Char * pEnd);
static const sal_Unicode * skipLinearWhiteSpace(const sal_Unicode *
pBegin,
const sal_Unicode * pEnd);
static const sal_Unicode * skipComment(const sal_Unicode * pBegin,
const sal_Unicode * pEnd);
static const sal_Unicode * skipLinearWhiteSpaceComment(const sal_Unicode *
pBegin,
const sal_Unicode *
pEnd);
static inline bool needsQuotedStringEscape(sal_uInt32 nChar);
static const sal_Char * skipQuotedString(const sal_Char * pBegin,
const sal_Char * pEnd);
static const sal_Unicode * skipQuotedString(const sal_Unicode * pBegin,
const sal_Unicode * pEnd);
static bool scanUnsigned(const sal_Unicode *& rBegin,
const sal_Unicode * pEnd, bool bLeadingZeroes,
sal_uInt32 & rValue);
static const sal_Unicode * scanQuotedBlock(const sal_Unicode * pBegin,
const sal_Unicode * pEnd,
sal_uInt32 nOpening,
sal_uInt32 nClosing,
sal_Size & rLength,
bool & rModify);
static sal_Unicode const * scanParameters(sal_Unicode const * pBegin,
sal_Unicode const * pEnd,
INetContentTypeParameterList *
pParameters);
static inline rtl_TextEncoding translateToMIME(rtl_TextEncoding
eEncoding);
static inline rtl_TextEncoding translateFromMIME(rtl_TextEncoding
eEncoding);
static const sal_Char * getCharsetName(rtl_TextEncoding eEncoding);
static rtl_TextEncoding getCharsetEncoding(const sal_Char * pBegin,
const sal_Char * pEnd);
static inline bool isMIMECharsetEncoding(rtl_TextEncoding eEncoding);
static INetMIMECharsetList_Impl *
createPreferredCharsetList(rtl_TextEncoding eEncoding);
static sal_Unicode * convertToUnicode(const sal_Char * pBegin,
const sal_Char * pEnd,
rtl_TextEncoding eEncoding,
sal_Size & rSize);
static sal_Char * convertFromUnicode(const sal_Unicode * pBegin,
const sal_Unicode * pEnd,
rtl_TextEncoding eEncoding,
sal_Size & rSize);
/** Get the number of octets required to encode an UCS-4 character using
UTF-8 encoding.
@param nChar Some UCS-4 character.
@return The number of octets required (in the range 1--6, inclusive).
*/
static inline int getUTF8OctetCount(sal_uInt32 nChar);
static inline void writeEscapeSequence(INetMIMEOutputSink & rSink,
sal_uInt32 nChar);
static void writeUTF8(INetMIMEOutputSink & rSink, sal_uInt32 nChar);
static void writeHeaderFieldBody(INetMIMEOutputSink & rSink,
HeaderFieldType eType,
const OUString& rBody,
rtl_TextEncoding ePreferredEncoding,
bool bInitialSpace = true);
static bool translateUTF8Char(const sal_Char *& rBegin,
const sal_Char * pEnd,
rtl_TextEncoding eEncoding,
sal_uInt32 & rCharacter);
static OUString decodeHeaderFieldBody(HeaderFieldType eType,
const OString& rBody);
// #i70651#: Prevent warnings on Mac OS X.
#ifdef MACOSX
#pragma GCC system_header
#endif
/** Get the UTF-32 character at the head of a UTF-16 encoded string.
@param rBegin Points to the start of the UTF-16 encoded string, must
not be null. On exit, it points past the first UTF-32 character's
encoding.
@param pEnd Points past the end of the UTF-16 encoded string, must be
strictly greater than rBegin.
@return The UCS-4 character at the head of the UTF-16 encoded string.
If the string does not start with the UTF-16 encoding of a UCS-32
character, the first UTF-16 value is returned.
*/
static inline sal_uInt32 getUTF32Character(const sal_Unicode *& rBegin,
const sal_Unicode * pEnd);
/** Put the UTF-16 encoding of a UTF-32 character into a buffer.
@param pBuffer Points to a buffer, must not be null.
@param nUTF32 An UTF-32 character, must be in the range 0..0x10FFFF.
@return A pointer past the UTF-16 characters put into the buffer
(i.e., pBuffer + 1 or pBuffer + 2).
*/
static inline sal_Unicode * putUTF32Character(sal_Unicode * pBuffer,
sal_uInt32 nUTF32);
};
// static
inline bool INetMIME::isISO88591(sal_uInt32 nChar)
{
return nChar <= 0xFF;
}
// static
inline bool INetMIME::isControl(sal_uInt32 nChar)
{
return nChar <= 0x1F || nChar == 0x7F;
}
// static
inline bool INetMIME::isWhiteSpace(sal_uInt32 nChar)
{
return nChar == '\t' || nChar == ' ';
}
// static
inline bool INetMIME::isVisible(sal_uInt32 nChar)
{
return nChar >= '!' && nChar <= '~';
}
// static
inline bool INetMIME::isBase64Digit(sal_uInt32 nChar)
{
return rtl::isAsciiUpperCase(nChar) || rtl::isAsciiLowerCase(nChar) || rtl::isAsciiDigit(nChar)
|| nChar == '+' || nChar == '/';
}
// static
inline int INetMIME::getWeight(sal_uInt32 nChar)
{
return rtl::isAsciiDigit(nChar) ? int(nChar - '0') : -1;
}
// static
inline int INetMIME::getHexWeight(sal_uInt32 nChar)
{
return rtl::isAsciiDigit(nChar) ? int(nChar - '0') :
nChar >= 'A' && nChar <= 'F' ? int(nChar - 'A' + 10) :
nChar >= 'a' && nChar <= 'f' ? int(nChar - 'a' + 10) : -1;
}
// static
inline int INetMIME::getBase64Weight(sal_uInt32 nChar)
{
return rtl::isAsciiUpperCase(nChar) ? int(nChar - 'A') :
rtl::isAsciiLowerCase(nChar) ? int(nChar - 'a' + 26) :
rtl::isAsciiDigit(nChar) ? int(nChar - '0' + 52) :
nChar == '+' ? 62 :
nChar == '/' ? 63 :
nChar == '=' ? -1 : -2;
}
// static
inline bool INetMIME::isHighSurrogate(sal_uInt32 nUTF16)
{
return nUTF16 >= 0xD800 && nUTF16 <= 0xDBFF;
}
// static
inline bool INetMIME::isLowSurrogate(sal_uInt32 nUTF16)
{
return nUTF16 >= 0xDC00 && nUTF16 <= 0xDFFF;
}
// static
inline sal_uInt32 INetMIME::toUTF32(sal_Unicode cHighSurrogate,
sal_Unicode cLowSurrogate)
{
DBG_ASSERT(isHighSurrogate(cHighSurrogate)
&& isLowSurrogate(cLowSurrogate),
"INetMIME::toUTF32(): Bad chars");
return ((sal_uInt32(cHighSurrogate) & 0x3FF) << 10)
| (sal_uInt32(cLowSurrogate) & 0x3FF);
}
// static
inline bool INetMIME::startsWithLineBreak(const sal_Char * pBegin,
const sal_Char * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIME::startsWithLineBreak(): Bad sequence");
return pEnd - pBegin >= 2 && pBegin[0] == 0x0D && pBegin[1] == 0x0A;
// CR, LF
}
// static
inline bool INetMIME::startsWithLineBreak(const sal_Unicode * pBegin,
const sal_Unicode * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIME::startsWithLineBreak(): Bad sequence");
return pEnd - pBegin >= 2 && pBegin[0] == 0x0D && pBegin[1] == 0x0A;
// CR, LF
}
// static
inline bool INetMIME::startsWithLineFolding(const sal_Char * pBegin,
const sal_Char * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIME::startsWithLineFolding(): Bad sequence");
return pEnd - pBegin >= 3 && pBegin[0] == 0x0D && pBegin[1] == 0x0A
&& isWhiteSpace(pBegin[2]); // CR, LF
}
// static
inline bool INetMIME::startsWithLineFolding(const sal_Unicode * pBegin,
const sal_Unicode * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIME::startsWithLineFolding(): Bad sequence");
return pEnd - pBegin >= 3 && pBegin[0] == 0x0D && pBegin[1] == 0x0A
&& isWhiteSpace(pBegin[2]); // CR, LF
}
// static
inline bool INetMIME::startsWithLinearWhiteSpace(const sal_Char * pBegin,
const sal_Char * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIME::startsWithLinearWhiteSpace(): Bad sequence");
return pBegin != pEnd
&& (isWhiteSpace(*pBegin) || startsWithLineFolding(pBegin, pEnd));
}
// static
inline bool INetMIME::needsQuotedStringEscape(sal_uInt32 nChar)
{
return nChar == '"' || nChar == '\\';
}
// static
inline rtl_TextEncoding INetMIME::translateToMIME(rtl_TextEncoding eEncoding)
{
#if defined WNT
return eEncoding == RTL_TEXTENCODING_MS_1252 ?
RTL_TEXTENCODING_ISO_8859_1 : eEncoding;
#else // WNT
return eEncoding;
#endif // WNT
}
// static
inline rtl_TextEncoding INetMIME::translateFromMIME(rtl_TextEncoding
eEncoding)
{
#if defined WNT
return eEncoding == RTL_TEXTENCODING_ISO_8859_1 ?
RTL_TEXTENCODING_MS_1252 : eEncoding;
#else
return eEncoding;
#endif
}
// static
inline bool INetMIME::isMIMECharsetEncoding(rtl_TextEncoding eEncoding)
{
return ( rtl_isOctetTextEncoding(eEncoding) == sal_True );
}
// static
inline int INetMIME::getUTF8OctetCount(sal_uInt32 nChar)
{
DBG_ASSERT(nChar < 0x80000000, "INetMIME::getUTF8OctetCount(): Bad char");
return nChar < 0x80 ? 1 :
nChar < 0x800 ? 2 :
nChar <= 0x10000 ? 3 :
nChar <= 0x200000 ? 4 :
nChar <= 0x4000000 ? 5 : 6;
}
// static
inline sal_uInt32 INetMIME::getUTF32Character(const sal_Unicode *& rBegin,
const sal_Unicode * pEnd)
{
DBG_ASSERT(rBegin && rBegin < pEnd,
"INetMIME::getUTF32Character(): Bad sequence");
if (rBegin + 1 < pEnd && rBegin[0] >= 0xD800 && rBegin[0] <= 0xDBFF
&& rBegin[1] >= 0xDC00 && rBegin[1] <= 0xDFFF)
{
sal_uInt32 nUTF32 = sal_uInt32(*rBegin++ & 0x3FF) << 10;
return (nUTF32 | (*rBegin++ & 0x3FF)) + 0x10000;
}
else
return *rBegin++;
}
// static
inline sal_Unicode * INetMIME::putUTF32Character(sal_Unicode * pBuffer,
sal_uInt32 nUTF32)
{
DBG_ASSERT(nUTF32 <= 0x10FFFF, "INetMIME::putUTF32Character(): Bad char");
if (nUTF32 < 0x10000)
*pBuffer++ = sal_Unicode(nUTF32);
else
{
nUTF32 -= 0x10000;
*pBuffer++ = sal_Unicode(0xD800 | (nUTF32 >> 10));
*pBuffer++ = sal_Unicode(0xDC00 | (nUTF32 & 0x3FF));
}
return pBuffer;
}
class INetMIMEOutputSink
{
public:
static sal_uInt32 const NO_LINE_LENGTH_LIMIT = SAL_MAX_UINT32;
private:
sal_uInt32 m_nColumn;
sal_uInt32 m_nLineLengthLimit;
protected:
/** Write a sequence of octets.
@param pBegin Points to the start of the sequence, must not be null.
@param pEnd Points past the end of the sequence, must be >= pBegin.
*/
virtual void writeSequence(const sal_Char * pBegin,
const sal_Char * pEnd) = 0;
/** Write a null terminated sequence of octets (without the terminating
null).
@param pOctets A null terminated sequence of octets, must not be
null.
@return The length of pOctets (without the terminating null).
*/
virtual sal_Size writeSequence(const sal_Char * pSequence);
/** Write a sequence of octets.
@descr The supplied sequence of UCS-4 characters is interpreted as a
sequence of octets. It is an error if any of the elements of the
sequence has a numerical value greater than 255.
@param pBegin Points to the start of the sequence, must not be null.
@param pEnd Points past the end of the sequence, must be >= pBegin.
*/
virtual void writeSequence(const sal_uInt32 * pBegin,
const sal_uInt32 * pEnd);
/** Write a sequence of octets.
@descr The supplied sequence of Unicode characters is interpreted as
a sequence of octets. It is an error if any of the elements of the
sequence has a numerical value greater than 255.
@param pBegin Points to the start of the sequence, must not be null.
@param pEnd Points past the end of the sequence, must be >= pBegin.
*/
virtual void writeSequence(const sal_Unicode * pBegin,
const sal_Unicode * pEnd);
public:
INetMIMEOutputSink(sal_uInt32 nTheColumn = 0,
sal_uInt32 nTheLineLengthLimit
= INetMIME::SOFT_LINE_LENGTH_LIMIT):
m_nColumn(nTheColumn), m_nLineLengthLimit(nTheLineLengthLimit) {}
virtual ~INetMIMEOutputSink() {}
/** Get the current column.
@return The current column (starting from zero).
*/
sal_uInt32 getColumn() const { return m_nColumn; }
sal_uInt32 getLineLengthLimit() const { return m_nLineLengthLimit; }
void setLineLengthLimit(sal_uInt32 nTheLineLengthLimit)
{ m_nLineLengthLimit = nTheLineLengthLimit; }
virtual ErrCode getError() const;
/** Write a sequence of octets.
@param pBegin Points to the start of the sequence, must not be null.
@param pEnd Points past the end of the sequence, must be >= pBegin.
*/
inline void write(const sal_Char * pBegin, const sal_Char * pEnd);
/** Write a sequence of octets.
@param pBegin Points to the start of the sequence, must not be null.
@param nLength The length of the sequence.
*/
void write(const sal_Char * pBegin, sal_Size nLength)
{ write(pBegin, pBegin + nLength); }
/** Write a sequence of octets.
@descr The supplied sequence of UCS-4 characters is interpreted as a
sequence of octets. It is an error if any of the elements of the
sequence has a numerical value greater than 255.
@param pBegin Points to the start of the sequence, must not be null.
@param pEnd Points past the end of the sequence, must be >= pBegin.
*/
inline void write(const sal_uInt32 * pBegin, const sal_uInt32 * pEnd);
/** Write a sequence of octets.
@descr The supplied sequence of Unicode characters is interpreted as
a sequence of octets. It is an error if any of the elements of the
sequence has a numerical value greater than 255.
@param pBegin Points to the start of the sequence, must not be null.
@param pEnd Points past the end of the sequence, must be >= pBegin.
*/
inline void write(const sal_Unicode * pBegin, const sal_Unicode * pEnd);
/** Write a sequence of octets.
@param rOctets A OString, interpreted as a sequence of octets.
@param nBegin The offset of the first character to write.
@param nEnd The offset past the last character to write.
*/
void write(const OString& rOctets, sal_Int32 nBegin, sal_Int32 nEnd)
{
writeSequence(rOctets.getStr() + nBegin, rOctets.getStr() + nEnd);
m_nColumn += nEnd - nBegin;
}
/** Write a single octet.
@param nOctet Some octet.
@return This instance.
*/
inline INetMIMEOutputSink & operator <<(sal_Char nOctet);
/** Write a null terminated sequence of octets (without the terminating
null).
@param pOctets A null terminated sequence of octets, must not be
null.
@return This instance.
*/
inline INetMIMEOutputSink & operator <<(const sal_Char * pOctets);
/** Write a sequence of octets.
@param rOctets A OString, interpreted as a sequence of octets.
@return This instance.
*/
INetMIMEOutputSink & operator <<(const OString& rOctets)
{
writeSequence(rOctets.getStr(), rOctets.getStr() + rOctets.getLength());
m_nColumn += rOctets.getLength();
return *this;
}
/** Call a manipulator function.
@param pManipulator A manipulator function.
@return Whatever the manipulator function returns.
*/
INetMIMEOutputSink &
operator <<(INetMIMEOutputSink & (* pManipulator)(INetMIMEOutputSink &))
{ return pManipulator(*this); }
/** Write a line end (CR LF).
*/
void writeLineEnd();
/** A manipulator function that writes a line end (CR LF).
@param rSink Some sink.
@return The sink rSink.
*/
static inline INetMIMEOutputSink & endl(INetMIMEOutputSink & rSink);
};
inline void INetMIMEOutputSink::write(const sal_Char * pBegin,
const sal_Char * pEnd)
{
writeSequence(pBegin, pEnd);
m_nColumn += pEnd - pBegin;
}
inline void INetMIMEOutputSink::write(const sal_uInt32 * pBegin,
const sal_uInt32 * pEnd)
{
writeSequence(pBegin, pEnd);
m_nColumn += pEnd - pBegin;
}
inline void INetMIMEOutputSink::write(const sal_Unicode * pBegin,
const sal_Unicode * pEnd)
{
writeSequence(pBegin, pEnd);
m_nColumn += pEnd - pBegin;
}
inline INetMIMEOutputSink & INetMIMEOutputSink::operator <<(sal_Char nOctet)
{
writeSequence(&nOctet, &nOctet + 1);
++m_nColumn;
return *this;
}
inline INetMIMEOutputSink & INetMIMEOutputSink::operator <<(const sal_Char *
pOctets)
{
m_nColumn += writeSequence(pOctets);
return *this;
}
// static
inline INetMIMEOutputSink & INetMIMEOutputSink::endl(INetMIMEOutputSink &
rSink)
{
rSink.writeLineEnd();
return rSink;
}
// static
inline void INetMIME::writeEscapeSequence(INetMIMEOutputSink & rSink,
sal_uInt32 nChar)
{
DBG_ASSERT(nChar <= 0xFF, "INetMIME::writeEscapeSequence(): Bad char");
rSink << '=' << sal_uInt8(getHexDigit(nChar >> 4))
<< sal_uInt8(getHexDigit(nChar & 15));
}
class INetMIMEStringOutputSink: public INetMIMEOutputSink
{
OStringBuffer m_aBuffer;
using INetMIMEOutputSink::writeSequence;
virtual void writeSequence(const sal_Char * pBegin,
const sal_Char * pEnd);
public:
inline INetMIMEStringOutputSink(sal_uInt32 nColumn = 0,
sal_uInt32 nLineLengthLimit
= INetMIME::SOFT_LINE_LENGTH_LIMIT):
INetMIMEOutputSink(nColumn, nLineLengthLimit) {}
virtual ErrCode getError() const;
OString takeBuffer()
{
return m_aBuffer.makeStringAndClear();
}
};
class INetMIMEEncodedWordOutputSink
{
public:
enum Context { CONTEXT_TEXT = 1,
CONTEXT_COMMENT = 2,
CONTEXT_PHRASE = 4 };
enum Space { SPACE_NO, SPACE_ENCODED, SPACE_ALWAYS };
private:
enum { BUFFER_SIZE = 256 };
enum Coding { CODING_NONE, CODING_QUOTED, CODING_ENCODED,
CODING_ENCODED_TERMINATED };
enum EncodedWordState { STATE_INITIAL, STATE_FIRST_EQUALS,
STATE_FIRST_QUESTION, STATE_CHARSET,
STATE_SECOND_QUESTION, STATE_ENCODING,
STATE_THIRD_QUESTION, STATE_ENCODED_TEXT,
STATE_FOURTH_QUESTION, STATE_SECOND_EQUALS,
STATE_BAD };
INetMIMEOutputSink & m_rSink;
Context m_eContext;
Space m_eInitialSpace;
sal_uInt32 m_nExtraSpaces;
INetMIMECharsetList_Impl * m_pEncodingList;
sal_Unicode * m_pBuffer;
sal_uInt32 m_nBufferSize;
sal_Unicode * m_pBufferEnd;
Coding m_ePrevCoding;
rtl_TextEncoding m_ePrevMIMEEncoding;
Coding m_eCoding;
sal_uInt32 m_nQuotedEscaped;
EncodedWordState m_eEncodedWordState;
inline bool needsEncodedWordEscape(sal_uInt32 nChar) const;
void finish(bool bWriteTrailer);
public:
inline INetMIMEEncodedWordOutputSink(INetMIMEOutputSink & rTheSink,
Context eTheContext,
Space eTheInitialSpace,
rtl_TextEncoding ePreferredEncoding);
~INetMIMEEncodedWordOutputSink();
INetMIMEEncodedWordOutputSink & operator <<(sal_uInt32 nChar);
inline void write(const sal_Char * pBegin, const sal_Char * pEnd);
inline void write(const sal_Unicode * pBegin, const sal_Unicode * pEnd);
inline bool flush();
};
inline INetMIMEEncodedWordOutputSink::INetMIMEEncodedWordOutputSink(
INetMIMEOutputSink & rTheSink, Context eTheContext,
Space eTheInitialSpace, rtl_TextEncoding ePreferredEncoding):
m_rSink(rTheSink),
m_eContext(eTheContext),
m_eInitialSpace(eTheInitialSpace),
m_nExtraSpaces(0),
m_pEncodingList(INetMIME::createPreferredCharsetList(ePreferredEncoding)),
m_ePrevCoding(CODING_NONE),
m_eCoding(CODING_NONE),
m_nQuotedEscaped(0),
m_eEncodedWordState(STATE_INITIAL)
{
m_nBufferSize = BUFFER_SIZE;
m_pBuffer = static_cast< sal_Unicode * >(rtl_allocateMemory(
m_nBufferSize
* sizeof (sal_Unicode)));
m_pBufferEnd = m_pBuffer;
}
inline void INetMIMEEncodedWordOutputSink::write(const sal_Char * pBegin,
const sal_Char * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIMEEncodedWordOutputSink::write(): Bad sequence");
while (pBegin != pEnd)
operator <<(*pBegin++);
}
inline void INetMIMEEncodedWordOutputSink::write(const sal_Unicode * pBegin,
const sal_Unicode * pEnd)
{
DBG_ASSERT(pBegin && pBegin <= pEnd,
"INetMIMEEncodedWordOutputSink::write(): Bad sequence");
while (pBegin != pEnd)
operator <<(*pBegin++);
}
inline bool INetMIMEEncodedWordOutputSink::flush()
{
finish(true);
return m_ePrevCoding != CODING_NONE;
}
struct INetContentTypeParameter
{
/** The name of the attribute, in US-ASCII encoding and converted to lower
case. If a parameter value is split as described in RFC 2231, there
will only be one item for the complete parameter, with the attribute
name lacking any section suffix.
*/
const OString m_sAttribute;
/** The optional character set specification (see RFC 2231), in US-ASCII
encoding and converted to lower case.
*/
const OString m_sCharset;
/** The optional language specification (see RFC 2231), in US-ASCII
encoding and converted to lower case.
*/
const OString m_sLanguage;
/** The attribute value. If the value is a quoted-string, it is
'unpacked.' If a character set is specified, and the value can be
converted to Unicode, this is done. Also, if no character set is
specified, it is first tried to convert the value from UTF-8 encoding
to Unicode, and if that doesn't work (because the value is not in
UTF-8 encoding), it is converted from ISO-8859-1 encoding to Unicode
(which will always work). But if a character set is specified and the
value cannot be converted from that character set to Unicode, special
action is taken to produce a value that can possibly be transformed
back into its original form: Any 8-bit character from a non-encoded
part of the original value is directly converted to Unicode
(effectively handling it as if it was ISO-8859-1 encoded), and any
8-bit character from an encoded part of the original value is mapped
to the range U+F800..U+F8FF at the top of the Corporate Use Subarea
within Unicode's Private Use Area (effectively adding 0xF800 to the
character's numeric value).
*/
const OUString m_sValue;
/** This is true if the value is successfully converted to Unicode, and
false if the value is a special mixture of ISO-LATIN-1 characters and
characters from Unicode's Private Use Area.
*/
const bool m_bConverted;
INetContentTypeParameter(const OString& rTheAttribute,
const OString& rTheCharset, const OString& rTheLanguage,
const OUString& rTheValue, bool bTheConverted)
: m_sAttribute(rTheAttribute)
, m_sCharset(rTheCharset)
, m_sLanguage(rTheLanguage)
, m_sValue(rTheValue)
, m_bConverted(bTheConverted)
{
}
};
class TOOLS_DLLPUBLIC INetContentTypeParameterList
{
public:
void Clear();
void Insert(INetContentTypeParameter * pParameter, sal_uIntPtr nIndex)
{
maEntries.insert(maEntries.begin()+nIndex,pParameter);
}
void Append(INetContentTypeParameter *pParameter)
{
maEntries.push_back(pParameter);
}
inline const INetContentTypeParameter * GetObject(sal_uIntPtr nIndex) const
{
return &(maEntries[nIndex]);
}
const INetContentTypeParameter * find(const OString& rAttribute) const;
private:
boost::ptr_vector<INetContentTypeParameter> maEntries;
};
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|