summaryrefslogtreecommitdiffstats
path: root/mailnews/mime/src/mimei.cpp
blob: 568b6bca5efecd860abba6ededb7c77a3a57c6bc (plain)
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 *
 * 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 Original Code has been modified by IBM Corporation. Modifications made by IBM
 * described herein are Copyright (c) International Business Machines Corporation, 2000.
 * Modifications to Mozilla code or documentation identified per MPL Section 3.3
 *
 * Date             Modified by     Description of modification
 * 04/20/2000       IBM Corp.      OS/2 VisualAge build.
 */

#include "nsCOMPtr.h"
#include "mimeobj.h"  /*  MimeObject (abstract)              */
#include "mimecont.h"  /*   |--- MimeContainer (abstract)          */
#include "mimemult.h"  /*   |     |--- MimeMultipart (abstract)      */
#include "mimemmix.h"  /*   |     |     |--- MimeMultipartMixed      */
#include "mimemdig.h"  /*   |     |     |--- MimeMultipartDigest      */
#include "mimempar.h"  /*   |     |     |--- MimeMultipartParallel      */
#include "mimemalt.h"  /*   |     |     |--- MimeMultipartAlternative    */
#include "mimemrel.h"  /*   |     |     |--- MimeMultipartRelated      */
#include "mimemapl.h"  /*   |     |     |--- MimeMultipartAppleDouble    */
#include "mimesun.h"  /*   |     |     |--- MimeSunAttachment        */
#include "mimemsig.h"  /*   |     |     |--- MimeMultipartSigned (abstract)*/
#ifdef ENABLE_SMIME
#include "mimemcms.h"   /*   |     |           |---MimeMultipartSignedCMS   */
#endif
#include "mimecryp.h"  /*   |     |--- MimeEncrypted (abstract)      */
#ifdef ENABLE_SMIME
#include "mimecms.h"  /*   |     |     |--- MimeEncryptedPKCS7      */
#endif
#include "mimemsg.h"  /*   |     |--- MimeMessage              */
#include "mimeunty.h"  /*   |     |--- MimeUntypedText            */
#include "mimeleaf.h"  /*   |--- MimeLeaf (abstract)            */
#include "mimetext.h"  /*   |     |--- MimeInlineText (abstract)      */
#include "mimetpla.h"  /*   |     |     |--- MimeInlineTextPlain      */
#include "mimethpl.h"  /*   |     |     |     |--- M.I.TextHTMLAsPlaintext */
#include "mimetpfl.h"   /*   |     |     |--- MimeInlineTextPlainFlowed     */
#include "mimethtm.h"  /*   |     |     |--- MimeInlineTextHTML      */
#include "mimethsa.h"  /*   |     |     |     |--- M.I.TextHTMLSanitized   */
#include "mimeTextHTMLParsed.h" /*|     |     |--- M.I.TextHTMLParsed   */
#include "mimetric.h"  /*   |     |     |--- MimeInlineTextRichtext    */
#include "mimetenr.h"  /*   |     |     |     |--- MimeInlineTextEnriched  */
/* SUPPORTED VIA PLUGIN      |     |     |--- MimeInlineTextVCard           */
#include "mimeiimg.h"  /*   |     |--- MimeInlineImage            */
#include "mimeeobj.h"  /*   |     |--- MimeExternalObject          */
#include "mimeebod.h"  /*   |--- MimeExternalBody              */
                        /* If you add classes here,also add them to mimei.h */
#include "prlog.h"
#include "prmem.h"
#include "prenv.h"
#include "plstr.h"
#include "prlink.h"
#include "prprf.h"
#include "mimecth.h"
#include "mimebuf.h"
#include "nsIServiceManager.h"
#include "mimemoz2.h"
#include "nsIMimeContentTypeHandler.h"
#include "nsIComponentManager.h"
#include "nsCategoryManagerUtils.h"
#include "nsXPCOMCID.h"
#include "nsISimpleMimeConverter.h"
#include "nsSimpleMimeConverterStub.h"
#include "nsTArray.h"
#include "nsMimeStringResources.h"
#include "nsMimeTypes.h"
#include "nsMsgUtils.h"
#include "nsIPrefBranch.h"
#include "mozilla/Preferences.h"
#include "imgLoader.h"

#include "nsIMsgMailNewsUrl.h"
#include "nsIMsgHdr.h"

using namespace mozilla;

// forward declaration
void getMsgHdrForCurrentURL(MimeDisplayOptions *opts, nsIMsgDBHdr ** aMsgHdr);

#define  IMAP_EXTERNAL_CONTENT_HEADER "X-Mozilla-IMAP-Part"
#define  EXTERNAL_ATTACHMENT_URL_HEADER "X-Mozilla-External-Attachment-URL"

/* ==========================================================================
   Allocation and destruction
   ==========================================================================
 */
static int mime_classinit(MimeObjectClass *clazz);

/*
 * These are the necessary defines/variables for doing
 * content type handlers in external plugins.
 */
typedef struct {
  char        content_type[128];
  bool        force_inline_display;
} cthandler_struct;

nsTArray<cthandler_struct*> *ctHandlerList = NULL;

/*
 * This will return TRUE if the content_type is found in the
 * list, FALSE if it is not found.
 */
bool
find_content_type_attribs(const char *content_type,
                          bool       *force_inline_display)
{
  *force_inline_display = false;
  if (!ctHandlerList)
    return false;

  for (size_t i = 0; i < ctHandlerList->Length(); i++) 
  {
    cthandler_struct *ptr = ctHandlerList->ElementAt(i);
    if (PL_strcasecmp(content_type, ptr->content_type) == 0)
    {
      *force_inline_display = ptr->force_inline_display;
      return true;
    }
  }

  return false;
}

void
add_content_type_attribs(const char *content_type,
                         contentTypeHandlerInitStruct  *ctHandlerInfo)
{
  cthandler_struct    *ptr = NULL;
  bool                force_inline_display;

  if (find_content_type_attribs(content_type, &force_inline_display))
    return;

  if ( (!content_type) || (!ctHandlerInfo) )
    return;

  if (!ctHandlerList)
    ctHandlerList = new nsTArray<cthandler_struct*>();

  if (!ctHandlerList)
    return;

  ptr = (cthandler_struct *) PR_MALLOC(sizeof(cthandler_struct));
  if (!ptr)
    return;

  PL_strncpy(ptr->content_type, content_type, sizeof(ptr->content_type));
  ptr->force_inline_display = ctHandlerInfo->force_inline_display;
  ctHandlerList->AppendElement(ptr);
}

/*
 * This routine will find all content type handler for a specifc content
 * type (if it exists)
 */
bool
force_inline_display(const char *content_type)
{
  bool       force_inline_disp;

  find_content_type_attribs(content_type, &force_inline_disp);
  return (force_inline_disp);
}

/*
 * This routine will find all content type handler for a specifc content
 * type (if it exists) and is defined to the nsRegistry
 */
MimeObjectClass *
mime_locate_external_content_handler(const char *content_type,
                                     contentTypeHandlerInitStruct  *ctHandlerInfo)
{
  if (!content_type || !*(content_type)) // null or empty content type
    return nullptr;

  MimeObjectClass               *newObj = NULL;
  nsresult rv;

  nsAutoCString lookupID("@mozilla.org/mimecth;1?type=");
  nsAutoCString contentType;
  ToLowerCase(nsDependentCString(content_type), contentType);
  lookupID += contentType;

  nsCOMPtr<nsIMimeContentTypeHandler> ctHandler = do_CreateInstance(lookupID.get(), &rv);
  if (NS_FAILED(rv) || !ctHandler) {
    nsCOMPtr<nsICategoryManager> catman =
      do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
    if (NS_FAILED(rv))
      return nullptr;

    nsCString value;
    rv = catman->GetCategoryEntry(NS_SIMPLEMIMECONVERTERS_CATEGORY,
                                  contentType.get(), getter_Copies(value));
    if (NS_FAILED(rv) || value.IsEmpty())
      return nullptr;
    rv = MIME_NewSimpleMimeConverterStub(contentType.get(),
                                         getter_AddRefs(ctHandler));
    if (NS_FAILED(rv) || !ctHandler)
      return nullptr;
  }

  rv = ctHandler->CreateContentTypeHandlerClass(contentType.get(), ctHandlerInfo, &newObj);
  if (NS_FAILED(rv))
    return nullptr;

  add_content_type_attribs(contentType.get(), ctHandlerInfo);
  return newObj;
}

/* This is necessary to expose the MimeObject method outside of this DLL */
int
MIME_MimeObject_write(MimeObject *obj, const char *output, int32_t length, bool user_visible_p)
{
  return MimeObject_write(obj, output, length, user_visible_p);
}

MimeObject *
mime_new (MimeObjectClass *clazz, MimeHeaders *hdrs,
      const char *override_content_type)
{
  int size = clazz->instance_size;
  MimeObject *object;
  int status;

  /* Some assertions to verify that this isn't random junk memory... */
  NS_ASSERTION(clazz->class_name && strlen(clazz->class_name) > 0, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  NS_ASSERTION(size > 0 && size < 1000, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

  if (!clazz->class_initialized)
  {
    status = mime_classinit(clazz);
    if (status < 0) return 0;
  }

  NS_ASSERTION(clazz->initialize && clazz->finalize, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

  if (hdrs)
  {
    hdrs = MimeHeaders_copy (hdrs);
    if (!hdrs) return 0;
  }

  object = (MimeObject *) PR_MALLOC(size);
  if (!object) return 0;

  memset(object, 0, size);
  object->clazz = clazz;
  object->headers = hdrs;
  object->dontShowAsAttachment = false;

  if (override_content_type && *override_content_type)
    object->content_type = strdup(override_content_type);

  status = clazz->initialize(object);
  if (status < 0)
  {
    clazz->finalize(object);
    PR_Free(object);
    return 0;
  }

  return object;
}

void
mime_free (MimeObject *object)
{
# ifdef DEBUG__
  int i, size = object->clazz->instance_size;
  uint32_t *array = (uint32_t*) object;
# endif /* DEBUG */

  object->clazz->finalize(object);

# ifdef DEBUG__
  for (i = 0; i < (size / sizeof(*array)); i++)
  array[i] = (uint32_t) 0xDEADBEEF;
# endif /* DEBUG */

  PR_Free(object);
}


bool mime_is_allowed_class(const MimeObjectClass *clazz,
                             int32_t types_of_classes_to_disallow)
{
  if (types_of_classes_to_disallow == 0)
    return true;
  bool avoid_html = (types_of_classes_to_disallow >= 1);
  bool avoid_images = (types_of_classes_to_disallow >= 2);
  bool avoid_strange_content = (types_of_classes_to_disallow >= 3);
  bool allow_only_vanilla_classes = (types_of_classes_to_disallow == 100);

  if (allow_only_vanilla_classes)
    /* A "safe" class is one that is unlikely to have security bugs or to
       allow security exploits or one that is essential for the usefulness
       of the application, even for paranoid users.
       What's included here is more personal judgement than following
       strict rules, though, unfortunately.
       The function returns true only for known good classes, i.e. is a
       "whitelist" in this case.
       This idea comes from Georgi Guninski.
    */
    return
      (
        clazz == (MimeObjectClass *)&mimeInlineTextPlainClass ||
        clazz == (MimeObjectClass *)&mimeInlineTextPlainFlowedClass ||
        clazz == (MimeObjectClass *)&mimeInlineTextHTMLSanitizedClass ||
        clazz == (MimeObjectClass *)&mimeInlineTextHTMLAsPlaintextClass ||
           /* The latter 2 classes bear some risk, because they use the Gecko
              HTML parser, but the user has the option to make an explicit
              choice in this case, via html_as. */
        clazz == (MimeObjectClass *)&mimeMultipartMixedClass ||
        clazz == (MimeObjectClass *)&mimeMultipartAlternativeClass ||
        clazz == (MimeObjectClass *)&mimeMultipartDigestClass ||
        clazz == (MimeObjectClass *)&mimeMultipartAppleDoubleClass ||
        clazz == (MimeObjectClass *)&mimeMessageClass ||
        clazz == (MimeObjectClass *)&mimeExternalObjectClass ||
                               /*    mimeUntypedTextClass? -- does uuencode */
#ifdef ENABLE_SMIME
        clazz == (MimeObjectClass *)&mimeMultipartSignedCMSClass ||
        clazz == (MimeObjectClass *)&mimeEncryptedCMSClass ||
#endif
        clazz == 0
      );

  /* Contrairy to above, the below code is a "blacklist", i.e. it
     *excludes* some "bad" classes. */
  return
     !(
        (avoid_html
         && (
              clazz == (MimeObjectClass *)&mimeInlineTextHTMLParsedClass
                         /* Should not happen - we protect against that in
                            mime_find_class(). Still for safety... */
            )) ||
        (avoid_images
         && (
              clazz == (MimeObjectClass *)&mimeInlineImageClass
            )) ||
        (avoid_strange_content
         && (
              clazz == (MimeObjectClass *)&mimeInlineTextEnrichedClass ||
              clazz == (MimeObjectClass *)&mimeInlineTextRichtextClass ||
              clazz == (MimeObjectClass *)&mimeSunAttachmentClass ||
              clazz == (MimeObjectClass *)&mimeExternalBodyClass
            ))
      );
}

void getMsgHdrForCurrentURL(MimeDisplayOptions *opts, nsIMsgDBHdr ** aMsgHdr)
{
  *aMsgHdr = nullptr;

  if (!opts)
    return;

  mime_stream_data *msd = (mime_stream_data *) (opts->stream_closure);
  if (!msd)
    return;

  nsCOMPtr<nsIChannel> channel = msd->channel;  // note the lack of ref counting...
  if (channel)
  {
    nsCOMPtr<nsIURI> uri;
    nsCOMPtr<nsIMsgMessageUrl> msgURI;
    channel->GetURI(getter_AddRefs(uri));
    if (uri)
    {
      msgURI = do_QueryInterface(uri);
      if (msgURI)
      {
        msgURI->GetMessageHeader(aMsgHdr);
        if (*aMsgHdr)
          return;
        nsCString rdfURI;
        msgURI->GetUri(getter_Copies(rdfURI));
        if (!rdfURI.IsEmpty())
        {
          nsCOMPtr<nsIMsgDBHdr> msgHdr;
          GetMsgDBHdrFromURI(rdfURI.get(), getter_AddRefs(msgHdr));
          NS_IF_ADDREF(*aMsgHdr = msgHdr);
        }
      }
    }
  }

  return;
}

MimeObjectClass *
mime_find_class (const char *content_type, MimeHeaders *hdrs,
         MimeDisplayOptions *opts, bool exact_match_p)
{
  MimeObjectClass *clazz = 0;
  MimeObjectClass *tempClass = 0;
  contentTypeHandlerInitStruct  ctHandlerInfo;

  // Read some prefs
  nsIPrefBranch *prefBranch = GetPrefBranch(opts);
  int32_t html_as = 0;  // def. see below
  int32_t types_of_classes_to_disallow = 0;  /* Let only a few libmime classes
       process incoming data. This protects from bugs (e.g. buffer overflows)
       and from security loopholes (e.g. allowing unchecked HTML in some
       obscure classes, although the user has html_as > 0).
       This option is mainly for the UI of html_as.
       0 = allow all available classes
       1 = Use hardcoded blacklist to avoid rendering (incoming) HTML
       2 = ... and images
       3 = ... and some other uncommon content types
       4 = show all body parts
       100 = Use hardcoded whitelist to avoid even more bugs(buffer overflows).
           This mode will limit the features available (e.g. uncommon
           attachment types and inline images) and is for paranoid users.
       */
  if (opts && opts->format_out != nsMimeOutput::nsMimeMessageFilterSniffer &&
               opts->format_out != nsMimeOutput::nsMimeMessageDecrypt
               && opts->format_out != nsMimeOutput::nsMimeMessageAttach)
    if (prefBranch)
    {
      prefBranch->GetIntPref("mailnews.display.html_as", &html_as);
      prefBranch->GetIntPref("mailnews.display.disallow_mime_handlers",
                            &types_of_classes_to_disallow);
      if (types_of_classes_to_disallow > 0 && html_as == 0)
           // We have non-sensical prefs. Do some fixup.
        html_as = 1;
    }

  // First, check to see if the message has been marked as JUNK. If it has,
  // then force the message to be rendered as simple, unless this has been
  // called by a filtering routine.
  bool sanitizeJunkMail = false;

  // it is faster to read the pref first then figure out the msg hdr for the current url only if we have to
  // XXX instead of reading this pref every time, part of mime should be an observer listening to this pref change
  // and updating internal state accordingly. But none of the other prefs in this file seem to be doing that...=(
  if (prefBranch)
    prefBranch->GetBoolPref("mail.spam.display.sanitize", &sanitizeJunkMail);

  if (sanitizeJunkMail &&
      !(opts && opts->format_out == nsMimeOutput::nsMimeMessageFilterSniffer))
  {
    nsCOMPtr<nsIMsgDBHdr> msgHdr;
    getMsgHdrForCurrentURL(opts, getter_AddRefs(msgHdr));
    if (msgHdr)
    {
      nsCString junkScoreStr;
      (void) msgHdr->GetStringProperty("junkscore", getter_Copies(junkScoreStr));
      if (html_as == 0 && junkScoreStr.get() && atoi(junkScoreStr.get()) > 50)
        html_as = 3; // 3 == Simple HTML
    } // if msgHdr
  } // if we are supposed to sanitize junk mail

  /*
  * What we do first is check for an external content handler plugin.
  * This will actually extend the mime handling by calling a routine
  * which will allow us to load an external content type handler
  * for specific content types. If one is not found, we will drop back
  * to the default handler.
  */
  if ((tempClass = mime_locate_external_content_handler(content_type, &ctHandlerInfo)) != NULL)
  {
#ifdef MOZ_THUNDERBIRD
      // This is a case where we only want to add this property if we are a thunderbird build AND
      // we have found an external mime content handler for text/calendar
      // This will enable iMIP support in Lightning
      if ( hdrs && (!PL_strncasecmp(content_type, "text/calendar", 13)))
      {
          char *full_content_type = MimeHeaders_get(hdrs, HEADER_CONTENT_TYPE, false, false);
          if (full_content_type)
          {
              char *imip_method = MimeHeaders_get_parameter(full_content_type, "method", NULL, NULL);
              nsCOMPtr<nsIMsgDBHdr> msgHdr;
              getMsgHdrForCurrentURL(opts, getter_AddRefs(msgHdr));
              if (msgHdr)
                msgHdr->SetStringProperty("imip_method", (imip_method) ? imip_method : "nomethod");
              // PR_Free checks for null
              PR_Free(imip_method);
              PR_Free(full_content_type);
          }
      }
#endif

    if (types_of_classes_to_disallow > 0
        && (!PL_strncasecmp(content_type, "text/x-vcard", 12))
       )
      /* Use a little hack to prevent some dangerous plugins, which ship
         with Mozilla, to run.
         For the truely user-installed plugins, we rely on the judgement
         of the user. */
    {
      if (!exact_match_p)
        clazz = (MimeObjectClass *)&mimeExternalObjectClass; // As attachment
    }
    else
      clazz = (MimeObjectClass *)tempClass;
  }
  else
  {
    if (!content_type || !*content_type ||
        !PL_strcasecmp(content_type, "text"))  /* with no / in the type */
      clazz = (MimeObjectClass *)&mimeUntypedTextClass;

    /* Subtypes of text...
    */
    else if (!PL_strncasecmp(content_type,      "text/", 5))
    {
      if      (!PL_strcasecmp(content_type+5,    "html"))
      {
        if (opts &&
            (opts->format_out == nsMimeOutput::nsMimeMessageSaveAs ||
             opts->format_out == nsMimeOutput::nsMimeMessageFilterSniffer ||
             opts->format_out == nsMimeOutput::nsMimeMessageDecrypt ||
             opts->format_out == nsMimeOutput::nsMimeMessageAttach))
          // SaveAs in new modes doesn't work yet.
        {
          // Don't use the parsed HTML class if we're ...
          // - saving the HTML of a message
          // - getting message content for filtering
          // - snarfing attachments (nsMimeMessageDecrypt used in SnarfMsgAttachment)
          // - processing attachments (like deleting attachments).
          clazz = (MimeObjectClass *)&mimeInlineTextHTMLClass;
          types_of_classes_to_disallow = 0;
        }
        else if (html_as == 0 || html_as == 4) // Render sender's HTML
          clazz = (MimeObjectClass *)&mimeInlineTextHTMLParsedClass;
        else if (html_as == 1) // convert HTML to plaintext
          // Do a HTML->TXT->HTML conversion, see mimethpl.h.
          clazz = (MimeObjectClass *)&mimeInlineTextHTMLAsPlaintextClass;
        else if (html_as == 2) // display HTML source
          /* This is for the freaks. Treat HTML as plaintext,
             which will cause the HTML source to be displayed.
             Not very user-friendly, but some seem to want this. */
          clazz = (MimeObjectClass *)&mimeInlineTextPlainClass;
        else if (html_as == 3) // Sanitize
          // Strip all but allowed HTML
          clazz = (MimeObjectClass *)&mimeInlineTextHTMLSanitizedClass;
        else // Goofy pref
          /* User has an unknown pref value. Maybe he used a newer Mozilla
             with a new alternative to avoid HTML. Defaulting to option 1,
             which is less dangerous than defaulting to the raw HTML. */
          clazz = (MimeObjectClass *)&mimeInlineTextHTMLAsPlaintextClass;
      }
      else if (!PL_strcasecmp(content_type+5,    "enriched"))
        clazz = (MimeObjectClass *)&mimeInlineTextEnrichedClass;
      else if (!PL_strcasecmp(content_type+5,    "richtext"))
        clazz = (MimeObjectClass *)&mimeInlineTextRichtextClass;
      else if (!PL_strcasecmp(content_type+5,    "rtf"))
        clazz = (MimeObjectClass *)&mimeExternalObjectClass;
      else if (!PL_strcasecmp(content_type+5,    "plain"))
      {
        // Preliminary use the normal plain text
        clazz = (MimeObjectClass *)&mimeInlineTextPlainClass;

        if (opts && opts->format_out != nsMimeOutput::nsMimeMessageFilterSniffer
          && opts->format_out != nsMimeOutput::nsMimeMessageAttach
          && opts->format_out != nsMimeOutput::nsMimeMessageRaw)
        {
          bool disable_format_flowed = false;
          if (prefBranch)
            prefBranch->GetBoolPref("mailnews.display.disable_format_flowed_support",
                                    &disable_format_flowed);

          if(!disable_format_flowed)
          {
            // Check for format=flowed, damn, it is already stripped away from
            // the contenttype!
            // Look in headers instead even though it's expensive and clumsy
            // First find Content-Type:
            char *content_type_row =
              (hdrs
               ? MimeHeaders_get(hdrs, HEADER_CONTENT_TYPE,
                                 false, false)
               : 0);
            // Then the format parameter if there is one.
            // I would rather use a PARAM_FORMAT but I can't find the right
            // place to put the define. The others seems to be in net.h
            // but is that really really the right place? There is also
            // a nsMimeTypes.h but that one isn't included. Bug?
            char *content_type_format =
              (content_type_row
               ? MimeHeaders_get_parameter(content_type_row, "format", NULL,NULL)
               : 0);

            if (content_type_format && !PL_strcasecmp(content_type_format,
                                                          "flowed"))
              clazz = (MimeObjectClass *)&mimeInlineTextPlainFlowedClass;
            PR_FREEIF(content_type_format);
            PR_FREEIF(content_type_row);
          }
        }
      }
      else if (!exact_match_p)
        clazz = (MimeObjectClass *)&mimeInlineTextPlainClass;
    }

    /* Subtypes of multipart...
    */
    else if (!PL_strncasecmp(content_type,      "multipart/", 10))
    {
      // When html_as is 4, we want all MIME parts of the message to
      // show up in the displayed message body, if they are MIME types
      // that we know how to display, and also in the attachment pane
      // if it's appropriate to put them there. Both
      // multipart/alternative and multipart/related play games with
      // hiding various MIME parts, and we don't want that to happen,
      // so we prevent that by parsing those MIME types as
      // multipart/mixed, which won't mess with anything.
      //
      // When our output format is nsMimeOutput::nsMimeMessageAttach,
      // i.e., we are reformatting the message to remove attachments,
      // we are in a similar boat. The code for deleting
      // attachments properly in that mode is in mimemult.cpp
      // functions which are inherited by mimeMultipartMixedClass but
      // not by mimeMultipartAlternativeClass or
      // mimeMultipartRelatedClass. Therefore, to ensure that
      // everything is handled properly, in this context too we parse
      // those MIME types as multipart/mixed.
      bool basic_formatting = (html_as == 4) ||
        (opts && opts->format_out == nsMimeOutput::nsMimeMessageAttach);
      if      (!PL_strcasecmp(content_type+10,  "alternative"))
        clazz = basic_formatting ? (MimeObjectClass *)&mimeMultipartMixedClass :
          (MimeObjectClass *)&mimeMultipartAlternativeClass;
      else if (!PL_strcasecmp(content_type+10,  "related"))
        clazz = basic_formatting ? (MimeObjectClass *)&mimeMultipartMixedClass :
          (MimeObjectClass *)&mimeMultipartRelatedClass;
      else if (!PL_strcasecmp(content_type+10,  "digest"))
        clazz = (MimeObjectClass *)&mimeMultipartDigestClass;
      else if (!PL_strcasecmp(content_type+10,  "appledouble") ||
               !PL_strcasecmp(content_type+10,  "header-set"))
        clazz = (MimeObjectClass *)&mimeMultipartAppleDoubleClass;
      else if (!PL_strcasecmp(content_type+10,  "parallel"))
        clazz = (MimeObjectClass *)&mimeMultipartParallelClass;
      else if (!PL_strcasecmp(content_type+10,  "mixed"))
        clazz = (MimeObjectClass *)&mimeMultipartMixedClass;
#ifdef ENABLE_SMIME
      else if (!PL_strcasecmp(content_type+10,  "signed"))
      {
      /* Check that the "protocol" and "micalg" parameters are ones we
        know about. */
        char *ct = (hdrs
          ? MimeHeaders_get(hdrs, HEADER_CONTENT_TYPE,
                            false, false)
                    : 0);
        char *proto = (ct
          ? MimeHeaders_get_parameter(ct, PARAM_PROTOCOL, NULL, NULL)
          : 0);
        char *micalg = (ct
          ? MimeHeaders_get_parameter(ct, PARAM_MICALG, NULL, NULL)
          : 0);

          if (proto
              && (
                  (/* is a signature */
                   !PL_strcasecmp(proto, APPLICATION_XPKCS7_SIGNATURE)
                   ||
                   !PL_strcasecmp(proto, APPLICATION_PKCS7_SIGNATURE))
                  && micalg
                  && (!PL_strcasecmp(micalg, PARAM_MICALG_MD5) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_MD5_2) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA1) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA1_2) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA1_3) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA1_4) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA1_5) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA256) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA256_2) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA256_3) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA384) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA384_2) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA384_3) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA512) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA512_2) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_SHA512_3) ||
                      !PL_strcasecmp(micalg, PARAM_MICALG_MD2))))
            clazz = (MimeObjectClass *)&mimeMultipartSignedCMSClass;
          else
            clazz = 0;
        PR_FREEIF(proto);
        PR_FREEIF(micalg);
        PR_FREEIF(ct);
      }
#endif

      if (!clazz && !exact_match_p)
        /* Treat all unknown multipart subtypes as "multipart/mixed" */
        clazz = (MimeObjectClass *)&mimeMultipartMixedClass;

      /* If we are sniffing a message, let's treat alternative parts as mixed */
      if (opts && opts->format_out == nsMimeOutput::nsMimeMessageFilterSniffer)
        if (clazz == (MimeObjectClass *)&mimeMultipartAlternativeClass)
          clazz = (MimeObjectClass *)&mimeMultipartMixedClass;
    }

    /* Subtypes of message...
    */
    else if (!PL_strncasecmp(content_type,      "message/", 8))
    {
      if      (!PL_strcasecmp(content_type+8,    "rfc822") ||
        !PL_strcasecmp(content_type+8,    "news"))
        clazz = (MimeObjectClass *)&mimeMessageClass;
      else if (!PL_strcasecmp(content_type+8,    "external-body"))
        clazz = (MimeObjectClass *)&mimeExternalBodyClass;
      else if (!PL_strcasecmp(content_type+8,    "partial"))
        /* I guess these are most useful as externals, for now... */
        clazz = (MimeObjectClass *)&mimeExternalObjectClass;
      else if (!exact_match_p)
        /* Treat all unknown message subtypes as "text/plain" */
        clazz = (MimeObjectClass *)&mimeInlineTextPlainClass;
    }

    /* The magic image types which we are able to display internally...
    */
    else if (!PL_strncasecmp(content_type,    "image/", 6)) {
        if (imgLoader::SupportImageWithMimeType(content_type, AcceptedMimeTypes::IMAGES_AND_DOCUMENTS))
          clazz = (MimeObjectClass *)&mimeInlineImageClass;
        else
          clazz = (MimeObjectClass *)&mimeExternalObjectClass;
    }

#ifdef ENABLE_SMIME
    else if (!PL_strcasecmp(content_type, APPLICATION_XPKCS7_MIME)
             || !PL_strcasecmp(content_type, APPLICATION_PKCS7_MIME)) {

      if (Preferences::GetBool("mailnews.p7m_subparts_external", true) &&
          opts->is_child) {
        // We do not allow encrypted parts except as top level.
        // Allowing them would leak the plain text in case the part is
        // cleverly hidden and the decrypted content gets included in
        // replies and forwards.
        clazz = (MimeObjectClass *)&mimeSuppressedCryptoClass;
      } else {
        char *ct =
            hdrs ? MimeHeaders_get(hdrs, HEADER_CONTENT_TYPE, false, false)
                 : nullptr;
        char *st =
            ct ? MimeHeaders_get_parameter(ct, "smime-type", nullptr, nullptr)
               : nullptr;

        /* by default, assume that it is an encrypted message */
        clazz = (MimeObjectClass *)&mimeEncryptedCMSClass;

        /* if the smime-type parameter says that it's a certs-only or
           compressed file, then show it as an attachment, however
           (MimeEncryptedCMS doesn't handle these correctly) */
        if (st && (!PL_strcasecmp(st, "certs-only") ||
                   !PL_strcasecmp(st, "compressed-data")))
          clazz = (MimeObjectClass *)&mimeExternalObjectClass;
        else {
          /* look at the file extension... less reliable, but still covered
             by the S/MIME specification (RFC 3851, section 3.2.1)  */
          char *name = (hdrs ? MimeHeaders_get_name(hdrs, opts) : nullptr);
          if (name) {
            char *suf = PL_strrchr(name, '.');
            bool p7mExternal = false;

            if (prefBranch)
              prefBranch->GetBoolPref("mailnews.p7m_external", &p7mExternal);
            if (suf &&
                ((!PL_strcasecmp(suf, ".p7m") && p7mExternal) ||
                 !PL_strcasecmp(suf, ".p7c") || !PL_strcasecmp(suf, ".p7z")))
              clazz = (MimeObjectClass *)&mimeExternalObjectClass;
          }
          PR_Free(name);
        }
        PR_Free(st);
        PR_Free(ct);
      }
    }
#endif
    /* A few types which occur in the real world and which we would otherwise
    treat as non-text types (which would be bad) without this special-case...
    */
    else if (!PL_strcasecmp(content_type,      APPLICATION_PGP) ||
             !PL_strcasecmp(content_type,      APPLICATION_PGP2))
      clazz = (MimeObjectClass *)&mimeInlineTextPlainClass;

    else if (!PL_strcasecmp(content_type,      SUN_ATTACHMENT))
      clazz = (MimeObjectClass *)&mimeSunAttachmentClass;

    /* Everything else gets represented as a clickable link.
    */
    else if (!exact_match_p)
      clazz = (MimeObjectClass *)&mimeExternalObjectClass;

    if (!mime_is_allowed_class(clazz, types_of_classes_to_disallow))
    {
      /* Do that check here (not after the if block), because we want to allow
         user-installed plugins. */
      if(!exact_match_p)
        clazz = (MimeObjectClass *)&mimeExternalObjectClass;
      else
        clazz = 0;
    }
  }

#ifdef ENABLE_SMIME
  // see bug #189988
  if (opts && opts->format_out == nsMimeOutput::nsMimeMessageDecrypt &&
       (clazz != (MimeObjectClass *)&mimeEncryptedCMSClass)) {
    clazz = (MimeObjectClass *)&mimeExternalObjectClass;
  }
#endif

  if (!exact_match_p)
    NS_ASSERTION(clazz, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  if (!clazz) return 0;

  NS_ASSERTION(clazz, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

  if (clazz && !clazz->class_initialized)
  {
    int status = mime_classinit(clazz);
    if (status < 0) return 0;
  }

  return clazz;
}


MimeObject *
mime_create (const char *content_type, MimeHeaders *hdrs,
       MimeDisplayOptions *opts, bool forceInline /* = false */)
{
  /* If there is no Content-Disposition header, or if the Content-Disposition
   is ``inline'', then we display the part inline (and let mime_find_class()
   decide how.)

   If there is any other Content-Disposition (either ``attachment'' or some
   disposition that we don't recognise) then we always display the part as
   an external link, by using MimeExternalObject to display it.

   But Content-Disposition is ignored for all containers except `message'.
   (including multipart/mixed, and multipart/digest.)  It's not clear if
   this is to spec, but from a usability standpoint, I think it's necessary.
   */

  MimeObjectClass *clazz = 0;
  char *content_disposition = 0;
  MimeObject *obj = 0;
  char *override_content_type = 0;

  /* We've had issues where the incoming content_type is invalid, of a format:
     content_type="=?windows-1252?q?application/pdf" (bug 659355)
     We decided to fix that by simply trimming the stuff before the ?
  */
  if (content_type)
  {
    const char *lastQuestion = strrchr(content_type, '?');
    if (lastQuestion)
      content_type = lastQuestion + 1;  // the substring after the last '?'
  }

  /* There are some clients send out all attachments with a content-type
   of application/octet-stream.  So, if we have an octet-stream attachment,
   try to guess what type it really is based on the file extension.  I HATE
   that we have to do this...
  */
  if (hdrs && opts && opts->file_type_fn &&

    /* ### mwelch - don't override AppleSingle */
    (content_type ? PL_strcasecmp(content_type, APPLICATION_APPLEFILE) : true) &&
    /* ## davidm Apple double shouldn't use this #$%& either. */
    (content_type ? PL_strcasecmp(content_type, MULTIPART_APPLEDOUBLE) : true) &&
    (!content_type ||
     !PL_strcasecmp(content_type, APPLICATION_OCTET_STREAM) ||
     !PL_strcasecmp(content_type, UNKNOWN_CONTENT_TYPE)))
  {
    char *name = MimeHeaders_get_name(hdrs, opts);
    if (name)
    {
      override_content_type = opts->file_type_fn (name, opts->stream_closure);
      // appledouble isn't a valid override content type, and makes
      // attachments invisible.
      if (!PL_strcasecmp(override_content_type, MULTIPART_APPLEDOUBLE))
        override_content_type = nullptr;
      PR_FREEIF(name);

      // Workaroung for saving '.eml" file encoded with base64.
      // Do not override with message/rfc822 whenever Transfer-Encoding is
      // base64 since base64 encoding of message/rfc822 is invalid.
      // Our MimeMessageClass has no capability to decode it.
      if (!PL_strcasecmp(override_content_type, MESSAGE_RFC822)) {
        nsCString encoding;
        encoding.Adopt(MimeHeaders_get(hdrs,
                                       HEADER_CONTENT_TRANSFER_ENCODING,
                                       true, false));
        if (encoding.LowerCaseEqualsLiteral(ENCODING_BASE64))
          override_content_type = nullptr;
      }

      // If we get here and it is not the unknown content type from the
      // file name, let's do some better checking not to inline something bad
      if (override_content_type &&
          *override_content_type &&
          (PL_strcasecmp(override_content_type, UNKNOWN_CONTENT_TYPE)))
        content_type = override_content_type;
    }
  }

  clazz = mime_find_class(content_type, hdrs, opts, false);

  NS_ASSERTION(clazz, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  if (!clazz) goto FAIL;

  if (opts && opts->part_to_load)
  /* Always ignore Content-Disposition when we're loading some specific
     sub-part (which may be within some container that we wouldn't otherwise
     descend into, if the container itself had a Content-Disposition of
     `attachment'. */
  content_disposition = 0;

  else if (mime_subclass_p(clazz,(MimeObjectClass *)&mimeContainerClass) &&
       !mime_subclass_p(clazz,(MimeObjectClass *)&mimeMessageClass))
  /* Ignore Content-Disposition on all containers except `message'.
     That is, Content-Disposition is ignored for multipart/mixed objects,
     but is obeyed for message/rfc822 objects. */
  content_disposition = 0;

  else
  {
    /* Check to see if the plugin should override the content disposition
       to make it appear inline. One example is a vcard which has a content
       disposition of an "attachment;" */
    if (force_inline_display(content_type))
      NS_MsgSACopy(&content_disposition, "inline");
    else
      content_disposition = (hdrs
                 ? MimeHeaders_get(hdrs, HEADER_CONTENT_DISPOSITION, true, false)
                 : 0);
  }

  if (!content_disposition || !PL_strcasecmp(content_disposition, "inline"))
    ;  /* Use the class we've got. */
  else
  {
    // override messages that have content disposition set to "attachment"
    // even though we probably should show them inline.
    if (  (clazz != (MimeObjectClass *)&mimeInlineTextClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextPlainClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextPlainFlowedClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextHTMLClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextHTMLParsedClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextHTMLSanitizedClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextHTMLAsPlaintextClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextRichtextClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineTextEnrichedClass) &&
          (clazz != (MimeObjectClass *)&mimeMessageClass) &&
          (clazz != (MimeObjectClass *)&mimeInlineImageClass) ) {
      // not a special inline type, so show as attachment
      // However, mimeSuppressedCryptoClass is treated identically as
      // mimeExternalObjectClass, let's not lose that type information.
      if (clazz != (MimeObjectClass *)&mimeSuppressedCryptoClass) {
        clazz = (MimeObjectClass *)&mimeExternalObjectClass;
      }
    }
  }

  /* If the option `Show Attachments Inline' is off, now would be the time to change our mind... */
  /* Also, if we're doing a reply (i.e. quoting the body), then treat that according to preference. */
  if (opts && ((!opts->show_attachment_inline_p && !forceInline) ||
               (!opts->quote_attachment_inline_p &&
                (opts->format_out == nsMimeOutput::nsMimeMessageQuoting ||
                 opts->format_out == nsMimeOutput::nsMimeMessageBodyQuoting))))
  {
    if (mime_subclass_p(clazz, (MimeObjectClass *)&mimeInlineTextClass))
    {
      /* It's a text type.  Write it only if it's the *first* part
         that we're writing, and then only if it has no "filename"
         specified (the assumption here being, if it has a filename,
         it wasn't simply typed into the text field -- it was actually
         an attached document.) */
      if (opts->state && opts->state->first_part_written_p)
        clazz = (MimeObjectClass *)&mimeExternalObjectClass;
      else
      {
        /* If there's a name, then write this as an attachment. */
        char *name = (hdrs ? MimeHeaders_get_name(hdrs, opts) : nullptr);
        if (name)
        {
          clazz = (MimeObjectClass *)&mimeExternalObjectClass;
          PR_Free(name);
        }
      }
    }
    else
      if (mime_subclass_p(clazz,(MimeObjectClass *)&mimeContainerClass) &&
           !mime_subclass_p(clazz,(MimeObjectClass *)&mimeMessageClass))
        /* Multipart subtypes are ok, except for messages; descend into
           multiparts, and defer judgement.

           Encrypted blobs are just like other containers (make the crypto
           layer invisible, and treat them as simple containers.  So there's
           no easy way to save encrypted data directly to disk; it will tend
           to always be wrapped inside a message/rfc822.  That's ok.) */
          ;
        else if (opts && opts->part_to_load &&
                  mime_subclass_p(clazz,(MimeObjectClass *)&mimeMessageClass))
          /* Descend into messages only if we're looking for a specific sub-part. */
            ;
          else
          {
            /* Anything else, and display it as a link (and cause subsequent
               text parts to also be displayed as links.) */
            clazz = (MimeObjectClass *)&mimeExternalObjectClass;
          }
  }

  PR_FREEIF(content_disposition);
  obj = mime_new (clazz, hdrs, content_type);

 FAIL:

  /* If we decided to ignore the content-type in the headers of this object
   (see above) then make sure that our new content-type is stored in the
   object itself.  (Or free it, if we're in an out-of-memory situation.)
   */
  if (override_content_type)
  {
    if (obj)
    {
      PR_FREEIF(obj->content_type);
      obj->content_type = override_content_type;
    }
    else
    {
      PR_Free(override_content_type);
    }
  }

  return obj;
}



static int mime_classinit_1(MimeObjectClass *clazz, MimeObjectClass *target);

static int
mime_classinit(MimeObjectClass *clazz)
{
  int status;
  if (clazz->class_initialized)
  return 0;

  NS_ASSERTION(clazz->class_initialize, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  if (!clazz->class_initialize)
  return -1;

  /* First initialize the superclass.
   */
  if (clazz->superclass && !clazz->superclass->class_initialized)
  {
    status = mime_classinit(clazz->superclass);
    if (status < 0) return status;
  }

  /* Now run each of the superclass-init procedures in turn,
   parentmost-first. */
  status = mime_classinit_1(clazz, clazz);
  if (status < 0) return status;

  /* Now we're done. */
  clazz->class_initialized = true;
  return 0;
}

static int
mime_classinit_1(MimeObjectClass *clazz, MimeObjectClass *target)
{
  int status;
  if (clazz->superclass)
  {
    status = mime_classinit_1(clazz->superclass, target);
    if (status < 0) return status;
  }
  return clazz->class_initialize(target);
}


bool
mime_subclass_p(MimeObjectClass *child, MimeObjectClass *parent)
{
  if (child == parent)
  return true;
  else if (!child->superclass)
  return false;
  else
  return mime_subclass_p(child->superclass, parent);
}

bool
mime_typep(MimeObject *obj, MimeObjectClass *clazz)
{
  return mime_subclass_p(obj->clazz, clazz);
}



/* URL munging
 */


/* Returns a string describing the location of the part (like "2.5.3").
   This is not a full URL, just a part-number.
 */
char *
mime_part_address(MimeObject *obj)
{
  if (!obj->parent)
  return strdup("0");
  else
  {
    /* Find this object in its parent. */
    int32_t i, j = -1;
    char buf [20];
    char *higher = 0;
    MimeContainer *cont = (MimeContainer *) obj->parent;
    NS_ASSERTION(mime_typep(obj->parent,
                    (MimeObjectClass *)&mimeContainerClass), "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
    for (i = 0; i < cont->nchildren; i++)
    if (cont->children[i] == obj)
      {
      j = i+1;
      break;
      }
    if (j == -1)
    {
      NS_ERROR("No children under MeimContainer");
      return 0;
    }

    PR_snprintf(buf, sizeof(buf), "%ld", j);
    if (obj->parent->parent)
    {
      higher = mime_part_address(obj->parent);
      if (!higher) return 0;  /* MIME_OUT_OF_MEMORY */
    }

    if (!higher)
    return strdup(buf);
    else
    {
      uint32_t slen = strlen(higher) + strlen(buf) + 3;
      char *s = (char *)PR_MALLOC(slen);
      if (!s)
      {
        PR_Free(higher);
        return 0;  /* MIME_OUT_OF_MEMORY */
      }
      PL_strncpyz(s, higher, slen);
      PL_strcatn(s, slen, ".");
      PL_strcatn(s, slen, buf);
      PR_Free(higher);
      return s;
    }
  }
}


/* Returns a string describing the location of the *IMAP* part (like "2.5.3").
   This is not a full URL, just a part-number.
   This part is explicitly passed in the X-Mozilla-IMAP-Part header.
   Return value must be freed by the caller.
 */
char *
mime_imap_part_address(MimeObject *obj)
{
  if (!obj || !obj->headers)
    return 0;
  else
    return MimeHeaders_get(obj->headers, IMAP_EXTERNAL_CONTENT_HEADER, false, false);
}

/* Returns a full URL if the current mime object has a EXTERNAL_ATTACHMENT_URL_HEADER
   header.
   Return value must be freed by the caller.
*/
char *
mime_external_attachment_url(MimeObject *obj)
{
  if (!obj || !obj->headers)
    return 0;
  else
    return MimeHeaders_get(obj->headers, EXTERNAL_ATTACHMENT_URL_HEADER, false, false);
}

#ifdef ENABLE_SMIME
/* Asks whether the given object is one of the cryptographically signed
   or encrypted objects that we know about.  (MimeMessageClass uses this
   to decide if the headers need to be presented differently.)
 */
bool
mime_crypto_object_p(MimeHeaders *hdrs, bool clearsigned_counts, MimeDisplayOptions *opts)
{
  char *ct;
  MimeObjectClass *clazz;

  if (!hdrs) return false;

  ct = MimeHeaders_get (hdrs, HEADER_CONTENT_TYPE, true, false);
  if (!ct) return false;

  /* Rough cut -- look at the string before doing a more complex comparison. */
  if (PL_strcasecmp(ct, MULTIPART_SIGNED) &&
    PL_strncasecmp(ct, "application/", 12))
  {
    PR_Free(ct);
    return false;
  }

  /* It's a candidate for being a crypto object.  Let's find out for sure... */
  clazz = mime_find_class(ct, hdrs, opts, true);
  PR_Free(ct);

  if (clazz == ((MimeObjectClass *)&mimeEncryptedCMSClass))
  return true;
  else if (clearsigned_counts &&
       clazz == ((MimeObjectClass *)&mimeMultipartSignedCMSClass))
  return true;
  else
  return false;
}

#endif // ENABLE_SMIME

/* Puts a part-number into a URL.  If append_p is true, then the part number
   is appended to any existing part-number already in that URL; otherwise,
   it replaces it.
 */
char *
mime_set_url_part(const char *url, const char *part, bool append_p)
{
  const char *part_begin = 0;
  const char *part_end = 0;
  bool got_q = false;
  const char *s;
  char *result;

  if (!url || !part) return 0;

  nsAutoCString urlString(url);
  int32_t typeIndex = urlString.Find("?type=application/x-message-display");
  if (typeIndex != -1)
  {
    urlString.Cut(typeIndex, sizeof("?type=application/x-message-display") - 1);
    if (urlString.CharAt(typeIndex) == '&')
      urlString.Replace(typeIndex, 1, '?');
    url = urlString.get();
  }

  for (s = url; *s; s++)
  {
    if (*s == '?')
    {
      got_q = true;
      if (!PL_strncasecmp(s, "?part=", 6))
      part_begin = (s += 6);
    }
    else if (got_q && *s == '&' && !PL_strncasecmp(s, "&part=", 6))
    part_begin = (s += 6);

    if (part_begin)
    {
      for (; (*s && *s != '?' && *s != '&'); s++)
      ;
      part_end = s;
      break;
          }
  }

  uint32_t resultlen = strlen(url) + strlen(part) + 10;
  result = (char *) PR_MALLOC(resultlen);
  if (!result) return 0;

  if (part_begin)
  {
    if (append_p)
    {
      memcpy(result, url, part_end - url);
      result [part_end - url]     = '.';
      result [part_end - url + 1] = 0;
    }
    else
    {
      memcpy(result, url, part_begin - url);
      result [part_begin - url] = 0;
    }
  }
  else
  {
    PL_strncpyz(result, url, resultlen);
    if (got_q)
    PL_strcatn(result, resultlen, "&part=");
    else
    PL_strcatn(result, resultlen, "?part=");
  }

  PL_strcatn(result, resultlen, part);

  if (part_end && *part_end)
  PL_strcatn(result, resultlen, part_end);

  /* Semi-broken kludge to omit a trailing "?part=0". */
  {
  int L = strlen(result);
  if (L > 6 &&
    (result[L-7] == '?' || result[L-7] == '&') &&
    !strcmp("part=0", result + L - 6))
    result[L-7] = 0;
  }

  return result;
}



/* Puts an *IMAP* part-number into a URL.
   Strips off any previous *IMAP* part numbers, since they are absolute, not relative.
 */
char *
mime_set_url_imap_part(const char *url, const char *imappart, const char *libmimepart)
{
  char *result = 0;
  char *whereCurrent = PL_strstr(url, "/;section=");
  if (whereCurrent)
  {
    *whereCurrent = 0;
  }

  uint32_t resultLen = strlen(url) + strlen(imappart) + strlen(libmimepart) + 17;
  result = (char *) PR_MALLOC(resultLen);
  if (!result) return 0;

  PL_strncpyz(result, url, resultLen);
  PL_strcatn(result, resultLen, "/;section=");
  PL_strcatn(result, resultLen, imappart);
  PL_strcatn(result, resultLen, "?part=");
  PL_strcatn(result, resultLen, libmimepart);

  if (whereCurrent)
    *whereCurrent = '/';

  return result;
}


/* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
   number matches, and returns the MimeObject (else NULL.)
   (part is not a URL -- it's of the form "1.3.5".)
 */
MimeObject *
mime_address_to_part(const char *part, MimeObject *obj)
{
  /* Note: this is an N^2 operation, but the number of parts in a message
   shouldn't ever be large enough that this really matters... */

  bool match;

  if (!part || !*part)
  {
    match = !obj->parent;
  }
  else
  {
    char *part2 = mime_part_address(obj);
    if (!part2) return 0;  /* MIME_OUT_OF_MEMORY */
    match = !strcmp(part, part2);
    PR_Free(part2);
  }

  if (match)
  {
    /* These are the droids we're looking for. */
    return obj;
  }
  else if (!mime_typep(obj, (MimeObjectClass *) &mimeContainerClass))
  {
    /* Not a container, pull up, pull up! */
    return 0;
  }
  else
  {
    int32_t i;
    MimeContainer *cont = (MimeContainer *) obj;
    for (i = 0; i < cont->nchildren; i++)
    {
      MimeObject *o2 = mime_address_to_part(part, cont->children[i]);
      if (o2) return o2;
    }
    return 0;
  }
}

/* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
   number matches; if one is found, returns the Content-Name of that part.
   Else returns NULL.  (part is not a URL -- it's of the form "1.3.5".)
 */
char *
mime_find_content_type_of_part(const char *part, MimeObject *obj)
{
  char *result = 0;

  obj = mime_address_to_part(part, obj);
  if (!obj) return 0;

  result = (obj->headers ? MimeHeaders_get(obj->headers, HEADER_CONTENT_TYPE, true, false) : 0);

  return result;
}

/* Given a part ID, looks through the MimeObject tree for a sub-part whose ID
   number matches; if one is found, returns the Content-Name of that part.
   Else returns NULL.  (part is not a URL -- it's of the form "1.3.5".)
 */
char *
mime_find_suggested_name_of_part(const char *part, MimeObject *obj)
{
  char *result = 0;

  obj = mime_address_to_part(part, obj);
  if (!obj) return 0;

  result = (obj->headers ? MimeHeaders_get_name(obj->headers, obj->options) : 0);

  /* If this part doesn't have a name, but this part is one fork of an
   AppleDouble, and the AppleDouble itself has a name, then use that. */
  if (!result &&
    obj->parent &&
    obj->parent->headers &&
    mime_typep(obj->parent,
         (MimeObjectClass *) &mimeMultipartAppleDoubleClass))
  result = MimeHeaders_get_name(obj->parent->headers, obj->options);

  /* Else, if this part is itself an AppleDouble, and one of its children
   has a name, then use that (check data fork first, then resource.) */
  if (!result &&
    mime_typep(obj, (MimeObjectClass *) &mimeMultipartAppleDoubleClass))
  {
    MimeContainer *cont = (MimeContainer *) obj;
    if (cont->nchildren > 1 &&
      cont->children[1] &&
      cont->children[1]->headers)
    result = MimeHeaders_get_name(cont->children[1]->headers, obj->options);

    if (!result &&
      cont->nchildren > 0 &&
      cont->children[0] &&
      cont->children[0]->headers)
    result = MimeHeaders_get_name(cont->children[0]->headers, obj->options);
  }

  /* Ok, now we have the suggested name, if any.
   Now we remove any extensions that correspond to the
   Content-Transfer-Encoding.  For example, if we see the headers

    Content-Type: text/plain
    Content-Disposition: inline; filename=foo.text.uue
    Content-Transfer-Encoding: x-uuencode

   then we would look up (in mime.types) the file extensions which are
   associated with the x-uuencode encoding, find that "uue" is one of
   them, and remove that from the end of the file name, thus returning
   "foo.text" as the name.  This is because, by the time this file ends
   up on disk, its content-transfer-encoding will have been removed;
   therefore, we should suggest a file name that indicates that.
   */
  if (result && obj->encoding && *obj->encoding)
  {
    int32_t L = strlen(result);
    const char **exts = 0;

    /*
     I'd like to ask the mime.types file, "what extensions correspond
     to obj->encoding (which happens to be "x-uuencode") but doing that
     in a non-sphagetti way would require brain surgery.  So, since
     currently uuencode is the only content-transfer-encoding which we
     understand which traditionally has an extension, we just special-
     case it here!  Icepicks in my forehead!

     Note that it's special-cased in a similar way in libmsg/compose.c.
     */
    if (!PL_strcasecmp(obj->encoding, ENCODING_UUENCODE))
    {
      static const char *uue_exts[] = { "uu", "uue", 0 };
      exts = uue_exts;
    }

    while (exts && *exts)
    {
      const char *ext = *exts;
      int32_t L2 = strlen(ext);
      if (L > L2 + 1 &&              /* long enough */
        result[L - L2 - 1] == '.' &&      /* '.' in right place*/
        !PL_strcasecmp(ext, result + (L - L2)))  /* ext matches */
      {
        result[L - L2 - 1] = 0;    /* truncate at '.' and stop. */
        break;
      }
      exts++;
    }
  }

  return result;
}

/* Parse the various "?" options off the URL and into the options struct.
 */
int
mime_parse_url_options(const char *url, MimeDisplayOptions *options)
{
  const char *q;

  if (!url || !*url) return 0;
  if (!options) return 0;

  MimeHeadersState default_headers = options->headers;

  q = PL_strrchr (url, '?');
  if (! q) return 0;
  q++;
  while (*q)
  {
    const char *end, *value, *name_end;
    for (end = q; *end && *end != '&'; end++)
      ;
    for (value = q; *value != '=' && value < end; value++)
      ;
    name_end = value;
    if (value < end) value++;
    if (name_end <= q)
      ;
    else if (!PL_strncasecmp ("headers", q, name_end - q))
    {
      if (end > value && !PL_strncasecmp ("only", value, end-value))
        options->headers = MimeHeadersOnly;
      else if (end > value && !PL_strncasecmp ("none", value, end-value))
        options->headers = MimeHeadersNone;
      else if (end > value && !PL_strncasecmp ("all", value, end - value))
        options->headers = MimeHeadersAll;
      else if (end > value && !PL_strncasecmp ("some", value, end - value))
        options->headers = MimeHeadersSome;
      else if (end > value && !PL_strncasecmp ("micro", value, end - value))
        options->headers = MimeHeadersMicro;
      else if (end > value && !PL_strncasecmp ("cite", value, end - value))
        options->headers = MimeHeadersCitation;
      else if (end > value && !PL_strncasecmp ("citation", value, end-value))
        options->headers = MimeHeadersCitation;
      else
        options->headers = default_headers;
    }
    else if (!PL_strncasecmp ("part", q, name_end - q) &&
      options->format_out != nsMimeOutput::nsMimeMessageBodyQuoting)
    {
      PR_FREEIF (options->part_to_load);
      if (end > value)
      {
        options->part_to_load = (char *) PR_MALLOC(end - value + 1);
        if (!options->part_to_load)
          return MIME_OUT_OF_MEMORY;
        memcpy(options->part_to_load, value, end-value);
        options->part_to_load[end-value] = 0;
      }
    }
    else if (!PL_strncasecmp ("rot13", q, name_end - q))
    {
      options->rot13_p = end <= value || !PL_strncasecmp ("true", value, end - value);
    }
    else if (!PL_strncasecmp ("emitter", q, name_end - q))
    {
      if ((end > value) && !PL_strncasecmp ("js", value, end - value))
      {
        // the js emitter needs to hear about nested message bodies
        //  in order to build a proper representation.
        options->notify_nested_bodies = true;
        // show_attachment_inline_p has the side-effect of letting the
        //  emitter see all parts of a multipart/alternative, which it
        //  really appreciates.
        options->show_attachment_inline_p = true;
        // however, show_attachment_inline_p also results in a few
        //  subclasses writing junk into the body for display purposes.
        // put a stop to these shenanigans by enabling write_pure_bodies.
        //  current offenders are:
        //  - MimeInlineImage
        options->write_pure_bodies = true;
        // we don't actually care about the data in the attachments, just the
        // metadata (i.e. size)
        options->metadata_only = true;
      }
    }

    q = end;
    if (*q)
      q++;
  }


/* Compatibility with the "?part=" syntax used in the old (Mozilla 2.0)
   MIME parser.

   Basically, the problem is that the old part-numbering code was totally
   busted: here's a comparison of the old and new numberings with a pair
   of hypothetical messages (one with a single part, and one with nested
   containers.)
   NEW:      OLD:  OR:
   message/rfc822
   image/jpeg           1         0     0

  message/rfc822
  multipart/mixed      1         0     0
  text/plain         1.1       1     1
  image/jpeg         1.2       2     2
  message/rfc822     1.3       -     3
  text/plain       1.3.1     3     -
  message/rfc822     1.4       -     4
  multipart/mixed  1.4.1     4     -
  text/plain     1.4.1.1   4.1   -
  image/jpeg     1.4.1.2   4.2   -
  text/plain         1.5       5     5

 The "NEW" column is how the current code counts.  The "OLD" column is
 what "?part=" references would do in 3.0b4 and earlier; you'll see that
 you couldn't directly refer to the child message/rfc822 objects at all!
 But that's when it got really weird, because if you turned on
 "Attachments As Links" (or used a URL like "?inline=false&part=...")
 then you got a totally different numbering system (seen in the "OR"
 column.)  Gag!

 So, the problem is, ClariNet had been using these part numbers in their
 HTML news feeds, as a sleazy way of transmitting both complex HTML layouts
 and images using NNTP as transport, without invoking HTTP.

 The following clause is to provide some small amount of backward
 compatibility.  By looking at that table, one can see that in the new
 model, "part=0" has no meaning, and neither does "part=2" or "part=3"
 and so on.

 "part=1" is ambiguous between the old and new way, as is any part
 specification that has a "." in it.

 So, the compatibility hack we do here is: if the part is "0", then map
 that to "1".  And if the part is >= "2", then prepend "1." to it (so that
 we map "2" to "1.2", and "3" to "1.3".)

 This leaves the URLs compatible in the cases of:

 = single part messages
 = references to elements of a top-level multipart except the first

 and leaves them incompatible for:

 = the first part of a top-level multipart
 = all elements deeper than the outermost part

 Life s#$%s when you don't properly think out things that end up turning
 into de-facto standards...
 */

 if (options->part_to_load &&
   !PL_strchr(options->part_to_load, '.'))    /* doesn't contain a dot */
 {
   if (!strcmp(options->part_to_load, "0"))    /* 0 */
   {
     PR_Free(options->part_to_load);
     options->part_to_load = strdup("1");
     if (!options->part_to_load)
       return MIME_OUT_OF_MEMORY;
   }
   else if (strcmp(options->part_to_load, "1"))  /* not 1 */
   {
     const char *prefix = "1.";
     uint32_t slen = strlen(options->part_to_load) + strlen(prefix) + 1;
     char *s = (char *) PR_MALLOC(slen);
     if (!s) return MIME_OUT_OF_MEMORY;
     PL_strncpyz(s, prefix, slen);
     PL_strcatn(s, slen, options->part_to_load);
     PR_Free(options->part_to_load);
     options->part_to_load = s;
   }
 }

  return 0;
}


/* Some output-generation utility functions...
 */

int
MimeOptions_write(MimeHeaders *hdrs, MimeDisplayOptions *opt, const char *data,
                  int32_t length, bool user_visible_p)
{
  int status = 0;
  void* closure = 0;
  if (!opt || !opt->output_fn || !opt->state)
  return 0;

  closure = opt->output_closure;
  if (!closure) closure = opt->stream_closure;

//  PR_ASSERT(opt->state->first_data_written_p);

  if (opt->state->separator_queued_p && user_visible_p)
  {
    opt->state->separator_queued_p = false;
    if (opt->state->separator_suppressed_p)
      opt->state->separator_suppressed_p = false;
    else {
      const char *sep = "<BR><FIELDSET CLASS=\"mimeAttachmentHeader\">";
      int lstatus = opt->output_fn(sep, strlen(sep), closure);
      opt->state->separator_suppressed_p = false;
      if (lstatus < 0) return lstatus;

      nsCString name;
      name.Adopt(MimeHeaders_get_name(hdrs, opt));
      MimeHeaders_convert_header_value(opt, name, false);

      if (!name.IsEmpty()) {
          sep = "<LEGEND CLASS=\"mimeAttachmentHeaderName\">";
          lstatus = opt->output_fn(sep, strlen(sep), closure);
          opt->state->separator_suppressed_p = false;
          if (lstatus < 0) return lstatus;

          nsCString escapedName;
          escapedName.Adopt(MsgEscapeHTML(name.get()));

          lstatus = opt->output_fn(escapedName.get(),
                                   escapedName.Length(), closure);
          opt->state->separator_suppressed_p = false;
          if (lstatus < 0) return lstatus;

          sep = "</LEGEND>";
          lstatus = opt->output_fn(sep, strlen(sep), closure);
          opt->state->separator_suppressed_p = false;
          if (lstatus < 0) return lstatus;
      }

      sep = "</FIELDSET><BR/>";
      lstatus = opt->output_fn(sep, strlen(sep), closure);
      opt->state->separator_suppressed_p = false;
      if (lstatus < 0) return lstatus;
    }
  }
  if (user_visible_p)
  opt->state->separator_suppressed_p = false;

  if (length > 0)
  {
    status = opt->output_fn(data, length, closure);
    if (status < 0) return status;
  }

  return 0;
}

int
MimeObject_write(MimeObject *obj, const char *output, int32_t length,
         bool user_visible_p)
{
  if (!obj->output_p) return 0;

  // if we're stripping attachments, check if any parent is not being ouput
  if (obj->options->format_out == nsMimeOutput::nsMimeMessageAttach)
  {
    // if true, mime generates a separator in html - we don't want that.
    user_visible_p = false;

    for (MimeObject *parent = obj->parent; parent; parent = parent->parent)
    {
      if (!parent->output_p)
        return 0;
    }
  }
  if (!obj->options->state->first_data_written_p)
  {
    int status = MimeObject_output_init(obj, 0);
    if (status < 0) return status;
    NS_ASSERTION(obj->options->state->first_data_written_p, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  }

  return MimeOptions_write(obj->headers, obj->options, output, length, user_visible_p);
}

int
MimeObject_write_separator(MimeObject *obj)
{
  if (obj->options && obj->options->state &&
      // we never want separators if we are asking for pure bodies
      !obj->options->write_pure_bodies)
    obj->options->state->separator_queued_p = true;
  return 0;
}

int
MimeObject_output_init(MimeObject *obj, const char *content_type)
{
  if (obj &&
    obj->options &&
    obj->options->state &&
    !obj->options->state->first_data_written_p)
  {
    int status;
    const char *charset = 0;
    char *name = 0, *x_mac_type = 0, *x_mac_creator = 0;

    if (!obj->options->output_init_fn)
    {
      obj->options->state->first_data_written_p = true;
      return 0;
    }

    if (obj->headers)
    {
      char *ct;
      name = MimeHeaders_get_name(obj->headers, obj->options);

      ct = MimeHeaders_get(obj->headers, HEADER_CONTENT_TYPE,
                 false, false);
      if (ct)
      {
        x_mac_type   = MimeHeaders_get_parameter(ct, PARAM_X_MAC_TYPE, NULL, NULL);
        x_mac_creator= MimeHeaders_get_parameter(ct, PARAM_X_MAC_CREATOR, NULL, NULL);
        /* if don't have a x_mac_type and x_mac_creator, we need to try to get it from its parent */
        if (!x_mac_type && !x_mac_creator && obj->parent && obj->parent->headers)
        {
          char * ctp = MimeHeaders_get(obj->parent->headers, HEADER_CONTENT_TYPE, false, false);
          if (ctp)
          {
            x_mac_type   = MimeHeaders_get_parameter(ctp, PARAM_X_MAC_TYPE, NULL, NULL);
            x_mac_creator= MimeHeaders_get_parameter(ctp, PARAM_X_MAC_CREATOR, NULL, NULL);
            PR_Free(ctp);
          }
        }

        if (!(obj->options->override_charset)) {
          char *charset = MimeHeaders_get_parameter(ct, "charset", nullptr, nullptr);
          if (charset)
          {
            PR_FREEIF(obj->options->default_charset);
            obj->options->default_charset = charset;
          }
        }
        PR_Free(ct);
      }
    }

    if (mime_typep(obj, (MimeObjectClass *) &mimeInlineTextClass))
    charset = ((MimeInlineText *)obj)->charset;

    if (!content_type)
    content_type = obj->content_type;
    if (!content_type)
    content_type = TEXT_PLAIN;

    //
    // Set the charset on the channel we are dealing with so people know
    // what the charset is set to. Do this for quoting/Printing ONLY!
    //
    extern void   ResetChannelCharset(MimeObject *obj);
    if ( (obj->options) &&
         ( (obj->options->format_out == nsMimeOutput::nsMimeMessageQuoting) ||
           (obj->options->format_out == nsMimeOutput::nsMimeMessageBodyQuoting) ||
           (obj->options->format_out == nsMimeOutput::nsMimeMessageSaveAs) ||
           (obj->options->format_out == nsMimeOutput::nsMimeMessagePrintOutput) ) )
      ResetChannelCharset(obj);

    status = obj->options->output_init_fn (content_type, charset, name,
                       x_mac_type, x_mac_creator,
                       obj->options->stream_closure);
    PR_FREEIF(name);
    PR_FREEIF(x_mac_type);
    PR_FREEIF(x_mac_creator);
    obj->options->state->first_data_written_p = true;
    return status;
  }
  return 0;
}

char *
mime_get_base_url(const char *url)
{
  if (!url)
    return nullptr;

  const char *s = strrchr(url, '?');
  if (s && !strncmp(s, "?type=application/x-message-display", sizeof("?type=application/x-message-display") - 1))
  {
    const char *nextTerm = strchr(s, '&');
    s = (nextTerm) ? nextTerm : s + strlen(s) - 1;
  }
  // we need to keep the ?number part of the url, or we won't know
  // which local message the part belongs to.
  if (s && *s && *(s+1) && !strncmp(s + 1, "number=", sizeof("number=") - 1))
  {
    const char *nextTerm = strchr(++s, '&');
    s = (nextTerm) ? nextTerm : s + strlen(s) - 1;
  }
  char *result = (char *) PR_MALLOC(strlen(url) + 1);
  NS_ASSERTION(result, "out of memory");
  if (!result)
    return nullptr;

  memcpy(result, url, s - url);
  result[s - url] = 0;
  return result;
}