summaryrefslogtreecommitdiff
path: root/kmime/kmime_header_parsing.cpp
blob: 6c530e2af44d3a5c53cc7a3a3580c0f4cdf7e2d0 (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
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
/*  -*- c++ -*-
    kmime_header_parsing.cpp

    KMime, the KDE Internet mail/usenet news message library.
    Copyright (c) 2001-2002 Marc Mutz <mutz@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kmime_header_parsing.h"

#include "kmime_codecs.h"
#include "kmime_headerfactory_p.h"
#include "kmime_headers.h"
#include "kmime_util.h"
#include "kmime_util_p.h"
#include "kmime_dateformatter.h"
#include "kmime_warning.h"

#include <kcharsets.h>

#include <QtCore/QTextCodec>
#include <QtCore/QMap>
#include <QtCore/QStringList>
#include <QtCore/QUrl>

#include <ctype.h> // for isdigit
#include <cassert>

using namespace KMime;
using namespace KMime::Types;

namespace KMime {

namespace Types {

// QUrl::fromAce is extremely expensive, so only use it when necessary.
// Fortunately, the presence of IDNA is readily detected with a substring match...
static inline QString QUrl_fromAce_wrapper( const QString & domain )
{
    if ( domain.contains( QLatin1String( "xn--" ) ) )
        return QUrl::fromAce( domain.toLatin1() );
    else
        return domain;
}

static QString addr_spec_as_string( const AddrSpec & as, bool pretty )
{
  if ( as.isEmpty() ) {
    return QString();
  }

  static QChar dotChar = QLatin1Char( '.' );
  static QChar backslashChar = QLatin1Char( '\\' );
  static QChar quoteChar = QLatin1Char( '"' );

  bool needsQuotes = false;
  QString result;
  result.reserve( as.localPart.length() + as.domain.length() + 1 );
  for ( int i = 0 ; i < as.localPart.length() ; ++i ) {
    const QChar ch = as.localPart.at( i );
    if ( ch == dotChar || isAText( ch.toLatin1() ) ) {
      result += ch;
    } else {
      needsQuotes = true;
      if ( ch == backslashChar || ch == quoteChar ) {
        result += backslashChar;
      }
      result += ch;
    }
  }
  const QString dom = pretty ? QUrl_fromAce_wrapper( as.domain ) : as.domain ;
  if ( needsQuotes ) {
    result = quoteChar + result + quoteChar;
  }
  if( dom.isEmpty() ) {
    return result;
  } else {
    result += QLatin1Char( '@' );
    result += dom;
    return result;
  }
}

QString AddrSpec::asString() const
{
    return addr_spec_as_string( *this, false );
}

QString AddrSpec::asPrettyString() const
{
    return addr_spec_as_string( *this, true );
}

bool AddrSpec::isEmpty() const
{
  return localPart.isEmpty() && domain.isEmpty();
}

QByteArray Mailbox::address() const
{
  QByteArray result;
  const QString asString = addr_spec_as_string( mAddrSpec, false );
  if ( !asString.isEmpty() ) {
    result = asString.toLatin1();
  }
  return result;
  //return mAddrSpec.asString().toLatin1();
}

AddrSpec Mailbox::addrSpec() const
{
  return mAddrSpec;
}

QString Mailbox::name() const
{
  return mDisplayName;
}

void Mailbox::setAddress( const AddrSpec &addr )
{
  mAddrSpec = addr;
}

void Mailbox::setAddress( const QByteArray &addr )
{
  const char *cursor = addr.constData();
  if ( !HeaderParsing::parseAngleAddr( cursor,
                                       cursor + addr.length(), mAddrSpec ) ) {
    if ( !HeaderParsing::parseAddrSpec( cursor, cursor + addr.length(),
                                        mAddrSpec ) ) {
      kWarning() << "Invalid address";
      return;
    }
  }
}

void Mailbox::setName( const QString &name )
{
  mDisplayName = removeBidiControlChars( name );
}

void Mailbox::setNameFrom7Bit( const QByteArray &name,
                               const QByteArray &defaultCharset )
{
  QByteArray cs;
  setName( decodeRFC2047String( name, cs, defaultCharset, false ) );
}

bool Mailbox::hasAddress() const
{
  return !mAddrSpec.isEmpty();
}

bool Mailbox::hasName() const
{
  return !mDisplayName.isEmpty();
}

QString Mailbox::prettyAddress() const
{
  return prettyAddress( QuoteNever );
}

QString Mailbox::prettyAddress( Quoting quoting ) const
{
  if ( !hasName() ) {
    return QLatin1String( address() );
  }
  QString s = name();
  if ( quoting != QuoteNever ) {
    addQuotes( s, quoting == QuoteAlways /*bool force*/ );
  }

  if ( hasAddress() ) {
    s += QLatin1String(" <") + QLatin1String( address() ) + QLatin1Char('>');
  }
  return s;
}

void Mailbox::fromUnicodeString( const QString &s )
{
  from7BitString( encodeRFC2047Sentence( s, "utf-8" ) );
}

void Mailbox::from7BitString( const QByteArray &s )
{
  const char *cursor = s.constData();
  HeaderParsing::parseMailbox( cursor, cursor + s.length(), *this );
}

QByteArray KMime::Types::Mailbox::as7BitString( const QByteArray &encCharset ) const
{
  if ( !hasName() ) {
    return address();
  }
  QByteArray rv;
  if ( isUsAscii( name() ) ) {
    QByteArray tmp = name().toLatin1();
    addQuotes( tmp, false );
    rv += tmp;
  } else {
    rv += encodeRFC2047String( name(), encCharset, true );
  }
  if ( hasAddress() ) {
    rv += " <" + address() + '>';
  }
  return rv;
}

} // namespace Types

namespace HeaderParsing {

// parse the encoded-word (scursor points to after the initial '=')
bool parseEncodedWord( const char* &scursor, const char * const send,
                       QString &result, QByteArray &language,
                       QByteArray &usedCS, const QByteArray &defaultCS,
                       bool forceCS )
{
  // make sure the caller already did a bit of the work.
  assert( *(scursor-1) == '=' );

  //
  // STEP 1:
  // scan for the charset/language portion of the encoded-word
  //

  char ch = *scursor++;

  if ( ch != '?' ) {
    // kDebug() << "first";
    //KMIME_WARN_PREMATURE_END_OF( EncodedWord );
    return false;
  }

  // remember start of charset (ie. just after the initial "=?") and
  // language (just after the first '*') fields:
  const char * charsetStart = scursor;
  const char * languageStart = 0;

  // find delimiting '?' (and the '*' separating charset and language
  // tags, if any):
  for ( ; scursor != send ; scursor++ ) {
    if ( *scursor == '?') {
      break;
    } else if ( *scursor == '*' && languageStart == 0 ) {
      languageStart = scursor + 1;
    }
  }

  // not found? can't be an encoded-word!
  if ( scursor == send || *scursor != '?' ) {
    // kDebug() << "second";
    KMIME_WARN_PREMATURE_END_OF( EncodedWord );
    return false;
  }

  // extract the language information, if any (if languageStart is 0,
  // language will be null, too):
  QByteArray maybeLanguage( languageStart, scursor - languageStart );
  // extract charset information (keep in mind: the size given to the
  // ctor is one off due to the \0 terminator):
  QByteArray maybeCharset( charsetStart,
                           ( languageStart ? languageStart - 1 : scursor ) - charsetStart );

  //
  // STEP 2:
  // scan for the encoding portion of the encoded-word
  //

  // remember start of encoding (just _after_ the second '?'):
  scursor++;
  const char * encodingStart = scursor;

  // find next '?' (ending the encoding tag):
  for ( ; scursor != send ; scursor++ ) {
    if ( *scursor == '?' ) {
      break;
    }
  }

  // not found? Can't be an encoded-word!
  if ( scursor == send || *scursor != '?' ) {
    // kDebug() << "third";
    KMIME_WARN_PREMATURE_END_OF( EncodedWord );
    return false;
  }

  // extract the encoding information:
  QByteArray maybeEncoding( encodingStart, scursor - encodingStart );

  // kDebug() << "parseEncodedWord: found charset == \"" << maybeCharset
  //         << "\"; language == \"" << maybeLanguage
  //         << "\"; encoding == \"" << maybeEncoding << "\"";

  //
  // STEP 3:
  // scan for encoded-text portion of encoded-word
  //

  // remember start of encoded-text (just after the third '?'):
  scursor++;
  const char * encodedTextStart = scursor;

  // find the '?=' sequence (ending the encoded-text):
  for ( ; scursor != send ; scursor++ ) {
    if ( *scursor == '?' ) {
      if ( scursor + 1 != send ) {
        if ( *( scursor + 1 ) != '=' ) { // We expect a '=' after the '?', but we got something else; ignore
          KMIME_WARN << "Stray '?' in q-encoded word, ignoring this.";
          continue;
        }
        else { // yep, found a '?=' sequence
          scursor += 2;
          break;
        }
      }
      else { // The '?' is the last char, but we need a '=' after it!
        KMIME_WARN_PREMATURE_END_OF( EncodedWord );
        return false;
      }
    }
  }

  if ( *( scursor - 2 ) != '?' || *( scursor - 1 ) != '=' ||
       scursor < encodedTextStart + 2 ) {
    KMIME_WARN_PREMATURE_END_OF( EncodedWord );
    return false;
  }

  // set end sentinel for encoded-text:
  const char * const encodedTextEnd = scursor - 2;

  //
  // STEP 4:
  // setup decoders for the transfer encoding and the charset
  //

  // try if there's a codec for the encoding found:
  Codec * codec = Codec::codecForName( maybeEncoding );
  if ( !codec ) {
    KMIME_WARN_UNKNOWN( Encoding, maybeEncoding );
    return false;
  }

  // get an instance of a corresponding decoder:
  Decoder * dec = codec->makeDecoder();
  assert( dec );

  // try if there's a (text)codec for the charset found:
  bool matchOK = false;
  QTextCodec *textCodec = 0;
  if ( forceCS || maybeCharset.isEmpty() ) {
    textCodec = KGlobal::charsets()->codecForName( QLatin1String( defaultCS ), matchOK );
    usedCS = cachedCharset( defaultCS );
  } else {
    textCodec = KGlobal::charsets()->codecForName( QLatin1String( maybeCharset ), matchOK );
    if ( !matchOK ) {  //no suitable codec found => use default charset
      textCodec = KGlobal::charsets()->codecForName( QLatin1String( defaultCS ), matchOK );
      usedCS = cachedCharset( defaultCS );
    } else {
      usedCS = cachedCharset( maybeCharset );
    }
  }

  if ( !matchOK || !textCodec ) {
    KMIME_WARN_UNKNOWN( Charset, maybeCharset );
    delete dec;
    return false;
  };

  // kDebug() << "mimeName(): \"" << textCodec->name() << "\"";

  // allocate a temporary buffer to store the 8bit text:
  int encodedTextLength = encodedTextEnd - encodedTextStart;
  QByteArray buffer;
  buffer.resize( codec->maxDecodedSizeFor( encodedTextLength ) );
  char *bbegin = buffer.data();
  char *bend = bbegin + buffer.length();

  //
  // STEP 5:
  // do the actual decoding
  //

  if ( !dec->decode( encodedTextStart, encodedTextEnd, bbegin, bend ) ) {
    KMIME_WARN << codec->name() << "codec lies about its maxDecodedSizeFor("
               << encodedTextLength << ")\nresult may be truncated";
  }

  result = textCodec->toUnicode( buffer.data(), bbegin - buffer.data() );

  // kDebug() << "result now: \"" << result << "\"";
  // cleanup:
  delete dec;
  language = maybeLanguage;

  return true;
}

static inline void eatWhiteSpace( const char* &scursor, const char * const send )
{
  while ( scursor != send &&
          ( *scursor == ' ' || *scursor == '\n' ||
            *scursor == '\t' || *scursor == '\r' ) )
    scursor++;
}

bool parseAtom( const char * &scursor, const char * const send,
                QString &result, bool allow8Bit )
{
  QPair<const char*,int> maybeResult;

  if ( parseAtom( scursor, send, maybeResult, allow8Bit ) ) {
    result += QString::fromLatin1( maybeResult.first, maybeResult.second );
    return true;
  }

  return false;
}

bool parseAtom( const char * &scursor, const char * const send,
                QPair<const char*,int> &result, bool allow8Bit )
{
  bool success = false;
  const char *start = scursor;

  while ( scursor != send ) {
    signed char ch = *scursor++;
    if ( ch > 0 && isAText( ch ) ) {
      // AText: OK
      success = true;
    } else if ( allow8Bit && ch < 0 ) {
      // 8bit char: not OK, but be tolerant.
      KMIME_WARN_8BIT( ch );
      success = true;
    } else {
      // CTL or special - marking the end of the atom:
      // re-set sursor to point to the offending
      // char and return:
      scursor--;
      break;
    }
  }
  result.first = start;
  result.second = scursor - start;
  return success;
}

// FIXME: Remove this and the other parseToken() method. add a new one where "result" is a
//        QByteArray.
bool parseToken( const char * &scursor, const char * const send,
                 QString &result, bool allow8Bit )
{
  QPair<const char*,int> maybeResult;

  if ( parseToken( scursor, send, maybeResult, allow8Bit ) ) {
    result += QString::fromLatin1( maybeResult.first, maybeResult.second );
    return true;
  }

  return false;
}

bool parseToken( const char * &scursor, const char * const send,
                 QPair<const char*,int> &result, bool allow8Bit )
{
  bool success = false;
  const char * start = scursor;

  while ( scursor != send ) {
    signed char ch = *scursor++;
    if ( ch > 0 && isTText( ch ) ) {
      // TText: OK
      success = true;
    } else if ( allow8Bit && ch < 0 ) {
      // 8bit char: not OK, but be tolerant.
      KMIME_WARN_8BIT( ch );
      success = true;
    } else {
      // CTL or tspecial - marking the end of the atom:
      // re-set sursor to point to the offending
      // char and return:
      scursor--;
      break;
    }
  }
  result.first = start;
  result.second = scursor - start;
  return success;
}

#define READ_ch_OR_FAIL if ( scursor == send ) {        \
    KMIME_WARN_PREMATURE_END_OF( GenericQuotedString ); \
    return false;                                       \
  } else {                                              \
    ch = *scursor++;                                    \
  }

// known issues:
//
// - doesn't handle quoted CRLF

// FIXME: Why is result a QString? This should be a QByteArray, since at this level, we don't
//        know about encodings yet!
bool parseGenericQuotedString( const char* &scursor, const char * const send,
                               QString &result, bool isCRLF,
                               const char openChar, const char closeChar )
{
  char ch;
  // We are in a quoted-string or domain-literal or comment and the
  // cursor points to the first char after the openChar.
  // We will apply unfolding and quoted-pair removal.
  // We return when we either encounter the end or unescaped openChar
  // or closeChar.

  assert( *(scursor-1) == openChar || *(scursor-1) == closeChar );

  while ( scursor != send ) {
    ch = *scursor++;

    if ( ch == closeChar || ch == openChar ) {
      // end of quoted-string or another opening char:
      // let caller decide what to do.
      return true;
    }

    switch( ch ) {
    case '\\':      // quoted-pair
      // misses "\" CRLF LWSP-char handling, see rfc822, 3.4.5
      READ_ch_OR_FAIL;
      KMIME_WARN_IF_8BIT( ch );
      result += QLatin1Char( ch );
      break;
    case '\r':
      // ###
      // The case of lonely '\r' is easy to solve, as they're
      // not part of Unix Line-ending conventions.
      // But I see a problem if we are given Unix-native
      // line-ending-mails, where we cannot determine anymore
      // whether a given '\n' was part of a CRLF or was occurring
      // on it's own.
      READ_ch_OR_FAIL;
      if ( ch != '\n' ) {
        // CR on it's own...
        KMIME_WARN_LONE( CR );
        result += QLatin1Char('\r');
        scursor--; // points to after the '\r' again
      } else {
        // CRLF encountered.
        // lookahead: check for folding
        READ_ch_OR_FAIL;
        if ( ch == ' ' || ch == '\t' ) {
          // correct folding;
          // position cursor behind the CRLF WSP (unfolding)
          // and add the WSP to the result
          result += QLatin1Char( ch );
        } else {
          // this is the "shouldn't happen"-case. There is a CRLF
          // inside a quoted-string without it being part of FWS.
          // We take it verbatim.
          KMIME_WARN_NON_FOLDING( CRLF );
          result += QLatin1String( "\r\n" );
          // the cursor is decremented again, so's we need not
          // duplicate the whole switch here. "ch" could've been
          // everything (incl. openChar or closeChar).
          scursor--;
        }
      }
      break;
    case '\n':
      // Note: CRLF has been handled above already!
      // ### LF needs special treatment, depending on whether isCRLF
      // is true (we can be sure a lonely '\n' was meant this way) or
      // false ('\n' alone could have meant LF or CRLF in the original
      // message. This parser assumes CRLF iff the LF is followed by
      // either WSP (folding) or NULL (premature end of quoted-string;
      // Should be fixed, since NULL is allowed as per rfc822).
      READ_ch_OR_FAIL;
      if ( !isCRLF && ( ch == ' ' || ch == '\t' ) ) {
        // folding
        // correct folding
        result += QLatin1Char( ch );
      } else {
        // non-folding
        KMIME_WARN_LONE( LF );
        result += QLatin1Char( '\n' );
        // pos is decremented, so's we need not duplicate the whole
        // switch here. ch could've been everything (incl. <">, "\").
        scursor--;
      }
      break;
    case '=':
    {
      // ### Work around broken clients that send encoded words in quoted-strings
      //     For example, older KMail versions.
      if( scursor == send )
        break;

      const char *oldscursor = scursor;
      QString tmp;
      QByteArray lang, charset;
      if( *scursor++ == '?' ) {
        --scursor;
        if( parseEncodedWord( scursor, send, tmp, lang, charset ) ) {
          result += tmp;
          break;
        } else {
          scursor = oldscursor;
        }
      } else {
        scursor = oldscursor;
      }
      // fall through
    }
    default:
      KMIME_WARN_IF_8BIT( ch );
      result += QLatin1Char( ch );
    }
  }

  return false;
}

// known issues:
//
// - doesn't handle encoded-word inside comments.

bool parseComment( const char* &scursor, const char * const send,
                   QString &result, bool isCRLF, bool reallySave )
{
  int commentNestingDepth = 1;
  const char *afterLastClosingParenPos = 0;
  QString maybeCmnt;
  const char *oldscursor = scursor;

  assert( *(scursor-1) == '(' );

  while ( commentNestingDepth ) {
    QString cmntPart;
    if ( parseGenericQuotedString( scursor, send, cmntPart, isCRLF, '(', ')' ) ) {
      assert( *(scursor-1) == ')' || *(scursor-1) == '(' );
      // see the kdoc for above function for the possible conditions
      // we have to check:
      switch ( *(scursor-1) ) {
      case ')':
        if ( reallySave ) {
          // add the chunk that's now surely inside the comment.
          result += maybeCmnt;
          result += cmntPart;
          if ( commentNestingDepth > 1 ) {
            // don't add the outermost ')'...
            result += QLatin1Char( ')' );
          }
          maybeCmnt.clear();
        }
        afterLastClosingParenPos = scursor;
        --commentNestingDepth;
        break;
      case '(':
        if ( reallySave ) {
          // don't add to "result" yet, because we might find that we
          // are already outside the (broken) comment...
          maybeCmnt += cmntPart;
          maybeCmnt += QLatin1Char( '(' );
        }
        ++commentNestingDepth;
        break;
      default: assert( 0 );
      } // switch
    } else {
      // !parseGenericQuotedString, ie. premature end
      if ( afterLastClosingParenPos ) {
        scursor = afterLastClosingParenPos;
      } else {
        scursor = oldscursor;
      }
      return false;
    }
  } // while

  return true;
}

// known issues: none.

bool parsePhrase( const char* &scursor, const char * const send,
                  QString &result, bool isCRLF )
{
  enum {
    None, Phrase, Atom, EncodedWord, QuotedString
  } found = None;

  QString tmp;
  QByteArray lang, charset;
  const char *successfullyParsed = 0;
  // only used by the encoded-word branch
  const char *oldscursor;
  // used to suppress whitespace between adjacent encoded-words
  // (rfc2047, 6.2):
  bool lastWasEncodedWord = false;

  while ( scursor != send ) {
    char ch = *scursor++;
    switch ( ch ) {
    case '.': // broken, but allow for intorop's sake
      if ( found == None ) {
        --scursor;
        return false;
      } else {
        if ( scursor != send && ( *scursor == ' ' || *scursor == '\t' ) ) {
          result += QLatin1String( ". " );
        } else {
          result += QLatin1Char( '.' );
        }
        successfullyParsed = scursor;
      }
      break;
    case '"': // quoted-string
      tmp.clear();
      if ( parseGenericQuotedString( scursor, send, tmp, isCRLF, '"', '"' ) ) {
        successfullyParsed = scursor;
        assert( *(scursor-1) == '"' );
        switch ( found ) {
        case None:
          found = QuotedString;
          break;
        case Phrase:
        case Atom:
        case EncodedWord:
        case QuotedString:
          found = Phrase;
          result += QLatin1Char(' '); // rfc822, 3.4.4
          break;
        default:
          assert( 0 );
        }
        lastWasEncodedWord = false;
        result += tmp;
      } else {
        // premature end of quoted string.
        // What to do? Return leading '"' as special? Return as quoted-string?
        // We do the latter if we already found something, else signal failure.
        if ( found == None ) {
          return false;
        } else {
          result += QLatin1Char(' '); // rfc822, 3.4.4
          result += tmp;
          return true;
        }
      }
      break;
    case '(': // comment
      // parse it, but ignore content:
      tmp.clear();
      if ( parseComment( scursor, send, tmp, isCRLF,
                         false /*don't bother with the content*/ ) ) {
        successfullyParsed = scursor;
        lastWasEncodedWord = false; // strictly interpreting rfc2047, 6.2
      } else {
        if ( found == None ) {
          return false;
        } else {
          scursor = successfullyParsed;
          return true;
        }
      }
      break;
    case '=': // encoded-word
      tmp.clear();
      oldscursor = scursor;
      lang.clear();
      charset.clear();
      if ( parseEncodedWord( scursor, send, tmp, lang, charset ) ) {
        successfullyParsed = scursor;
        switch ( found ) {
        case None:
          found = EncodedWord;
          break;
        case Phrase:
        case EncodedWord:
        case Atom:
        case QuotedString:
          if ( !lastWasEncodedWord ) {
            result += QLatin1Char(' '); // rfc822, 3.4.4
          }
          found = Phrase;
          break;
        default: assert( 0 );
        }
        lastWasEncodedWord = true;
        result += tmp;
        break;
      } else {
        // parse as atom:
        scursor = oldscursor;
      }
      // fall though...

    default: //atom
      tmp.clear();
      scursor--;
      if ( parseAtom( scursor, send, tmp, true /* allow 8bit */ ) ) {
        successfullyParsed = scursor;
        switch ( found ) {
        case None:
          found = Atom;
          break;
        case Phrase:
        case Atom:
        case EncodedWord:
        case QuotedString:
          found = Phrase;
          result += QLatin1Char(' '); // rfc822, 3.4.4
          break;
        default:
          assert( 0 );
        }
        lastWasEncodedWord = false;
        result += tmp;
      } else {
        if ( found == None ) {
          return false;
        } else {
          scursor = successfullyParsed;
          return true;
        }
      }
    }
    eatWhiteSpace( scursor, send );
  }

  return found != None;
}

// FIXME: This should probably by QByteArray &result instead?
bool parseDotAtom( const char* &scursor, const char * const send,
                   QString &result, bool isCRLF )
{
  eatCFWS( scursor, send, isCRLF );

  // always points to just after the last atom parsed:
  const char *successfullyParsed;

  QString tmp;
  if ( !parseAtom( scursor, send, tmp, false /* no 8bit */ ) ) {
    return false;
  }
  result += tmp;
  successfullyParsed = scursor;

  while ( scursor != send ) {

    // end of header or no '.' -> return
    if ( scursor == send || *scursor != '.' ) {
      return true;
    }
    scursor++; // eat '.'

    if ( scursor == send || !isAText( *scursor ) ) {
      // end of header or no AText, but this time following a '.'!:
      // reset cursor to just after last successfully parsed char and
      // return:
      scursor = successfullyParsed;
      return true;
    }

    // try to parse the next atom:
    QString maybeAtom;
    if ( !parseAtom( scursor, send, maybeAtom, false /*no 8bit*/ ) ) {
      scursor = successfullyParsed;
      return true;
    }

    result += QLatin1Char('.');
    result += maybeAtom;
    successfullyParsed = scursor;
  }

  scursor = successfullyParsed;
  return true;
}

void eatCFWS( const char* &scursor, const char * const send, bool isCRLF )
{
  QString dummy;

  while ( scursor != send ) {
    const char *oldscursor = scursor;

    char ch = *scursor++;

    switch( ch ) {
    case ' ':
    case '\t': // whitespace
    case '\r':
    case '\n': // folding
      continue;

    case '(': // comment
      if ( parseComment( scursor, send, dummy, isCRLF, false /*don't save*/ ) ) {
        continue;
      }
      scursor = oldscursor;
      return;

    default:
      scursor = oldscursor;
      return;
    }
  }
}

bool parseDomain( const char* &scursor, const char * const send,
                  QString &result, bool isCRLF )
{
  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  // domain := dot-atom / domain-literal / atom *("." atom)
  //
  // equivalent to:
  // domain = dot-atom / domain-literal,
  // since parseDotAtom does allow CFWS between atoms and dots

  if ( *scursor == '[' ) {
    // domain-literal:
    QString maybeDomainLiteral;
    // eat '[':
    scursor++;
    while ( parseGenericQuotedString( scursor, send, maybeDomainLiteral,
                                      isCRLF, '[', ']' ) ) {
      if ( scursor == send ) {
        // end of header: check for closing ']':
        if ( *(scursor-1) == ']' ) {
          // OK, last char was ']':
          result = maybeDomainLiteral;
          return true;
        } else {
          // not OK, domain-literal wasn't closed:
          return false;
        }
      }
      // we hit openChar in parseGenericQuotedString.
      // include it in maybeDomainLiteral and keep on parsing:
      if ( *(scursor-1) == '[' ) {
        maybeDomainLiteral += QLatin1Char('[');
        continue;
      }
      // OK, real end of domain-literal:
      result = maybeDomainLiteral;
      return true;
    }
  } else {
    // dot-atom:
    QString maybeDotAtom;
    if ( parseDotAtom( scursor, send, maybeDotAtom, isCRLF ) ) {
      result = maybeDotAtom;
      // Domain may end with '.', if so preserve it'
      if ( scursor != send && *scursor == '.' ) {
        result += QLatin1Char('.');
        scursor++;
      }
      return true;
    }
  }
  return false;
}

bool parseObsRoute( const char* &scursor, const char* const send,
                    QStringList &result, bool isCRLF, bool save )
{
  while ( scursor != send ) {
    eatCFWS( scursor, send, isCRLF );
    if ( scursor == send ) {
      return false;
    }

    // empty entry:
    if ( *scursor == ',' ) {
      scursor++;
      if ( save ) {
        result.append( QString() );
      }
      continue;
    }

    // empty entry ending the list:
    if ( *scursor == ':' ) {
      scursor++;
      if ( save ) {
        result.append( QString() );
      }
      return true;
    }

    // each non-empty entry must begin with '@':
    if ( *scursor != '@' ) {
      return false;
    } else {
      scursor++;
    }

    QString maybeDomain;
    if ( !parseDomain( scursor, send, maybeDomain, isCRLF ) ) {
      return false;
    }
    if ( save ) {
      result.append( maybeDomain );
    }

    // eat the following (optional) comma:
    eatCFWS( scursor, send, isCRLF );
    if ( scursor == send ) {
      return false;
    }
    if ( *scursor == ':' ) {
      scursor++;
      return true;
    }
    if ( *scursor == ',' ) {
      scursor++;
    }
  }

  return false;
}

bool parseAddrSpec( const char* &scursor, const char * const send,
                    AddrSpec &result, bool isCRLF )
{
  //
  // STEP 1:
  // local-part := dot-atom / quoted-string / word *("." word)
  //
  // this is equivalent to:
  // local-part := word *("." word)

  QString maybeLocalPart;
  QString tmp;

  while ( scursor != send ) {
    // first, eat any whitespace
    eatCFWS( scursor, send, isCRLF );

    char ch = *scursor++;
    switch ( ch ) {
    case '.': // dot
      maybeLocalPart += QLatin1Char('.');
      break;

    case '@':
      goto SAW_AT_SIGN;
      break;

    case '"': // quoted-string
      tmp.clear();
      if ( parseGenericQuotedString( scursor, send, tmp, isCRLF, '"', '"' ) ) {
        maybeLocalPart += tmp;
      } else {
        return false;
      }
      break;

    default: // atom
      scursor--; // re-set scursor to point to ch again
      tmp.clear();
      if ( parseAtom( scursor, send, tmp, false /* no 8bit */ ) ) {
        maybeLocalPart += tmp;
      } else {
        return false; // parseAtom can only fail if the first char is non-atext.
      }
      break;
    }
  }

  return false;

  //
  // STEP 2:
  // domain
  //

SAW_AT_SIGN:

  assert( *(scursor-1) == '@' );

  QString maybeDomain;
  if ( !parseDomain( scursor, send, maybeDomain, isCRLF ) ) {
    return false;
  }

  result.localPart = maybeLocalPart;
  result.domain = maybeDomain;

  return true;
}

bool parseAngleAddr( const char* &scursor, const char * const send,
                     AddrSpec &result, bool isCRLF )
{
  // first, we need an opening angle bracket:
  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send || *scursor != '<' ) {
    return false;
  }
  scursor++; // eat '<'

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  if ( *scursor == '@' || *scursor == ',' ) {
    // obs-route: parse, but ignore:
    KMIME_WARN << "obsolete source route found! ignoring.";
    QStringList dummy;
    if ( !parseObsRoute( scursor, send, dummy,
                         isCRLF, false /* don't save */ ) ) {
      return false;
    }
    // angle-addr isn't complete until after the '>':
    if ( scursor == send ) {
      return false;
    }
  }

  // parse addr-spec:
  AddrSpec maybeAddrSpec;
  if ( !parseAddrSpec( scursor, send, maybeAddrSpec, isCRLF ) ) {
    return false;
  }

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send || *scursor != '>' ) {
    return false;
  }
  scursor++;

  result = maybeAddrSpec;
  return true;

}

static QString stripQuotes( const QString &input )
{
  const QLatin1Char quotes( '"' );
  if ( input.startsWith( quotes ) && input.endsWith( quotes ) ) {
    QString stripped( input.mid( 1, input.size() - 2 ) );
    return stripped;
  }
  else return input;
}

bool parseMailbox( const char* &scursor, const char * const send,
                   Mailbox &result, bool isCRLF )
{
  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  AddrSpec maybeAddrSpec;
  QString maybeDisplayName;

  // first, try if it's a vanilla addr-spec:
  const char * oldscursor = scursor;
  if ( parseAddrSpec( scursor, send, maybeAddrSpec, isCRLF ) ) {
    result.setAddress( maybeAddrSpec );
    // check for the obsolete form of display-name (as comment):
    eatWhiteSpace( scursor, send );
    if ( scursor != send && *scursor == '(' ) {
      scursor++;
      if ( !parseComment( scursor, send, maybeDisplayName, isCRLF, true /*keep*/ ) ) {
        return false;
      }
    }
    result.setName( stripQuotes( maybeDisplayName ) );
    return true;
  }
  scursor = oldscursor;

  // second, see if there's a display-name:
  if ( !parsePhrase( scursor, send, maybeDisplayName, isCRLF ) ) {
    // failed: reset cursor, note absent display-name
    maybeDisplayName.clear();
    scursor = oldscursor;
  } else {
    // succeeded: eat CFWS
    eatCFWS( scursor, send, isCRLF );
    if ( scursor == send ) {
      return false;
    }
  }

  // third, parse the angle-addr:
  if ( !parseAngleAddr( scursor, send, maybeAddrSpec, isCRLF ) ) {
    return false;
  }

  if ( maybeDisplayName.isNull() ) {
    // check for the obsolete form of display-name (as comment):
    eatWhiteSpace( scursor, send );
    if ( scursor != send && *scursor == '(' ) {
      scursor++;
      if ( !parseComment( scursor, send, maybeDisplayName, isCRLF, true /*keep*/ ) ) {
        return false;
      }
    }
  }

  result.setName( stripQuotes( maybeDisplayName ) );
  result.setAddress( maybeAddrSpec );
  return true;
}

bool parseGroup( const char* &scursor, const char * const send,
                 Address &result, bool isCRLF )
{
  // group         := display-name ":" [ mailbox-list / CFWS ] ";" [CFWS]
  //
  // equivalent to:
  // group   := display-name ":" [ obs-mbox-list ] ";"

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  // get display-name:
  QString maybeDisplayName;
  if ( !parsePhrase( scursor, send, maybeDisplayName, isCRLF ) ) {
    return false;
  }

  // get ":":
  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send || *scursor != ':' ) {
    return false;
  }

  // KDE5 TODO: Don't expose displayName as public, but rather add setter for it that
  //            automatically calls removeBidiControlChars
  result.displayName = removeBidiControlChars( maybeDisplayName );

  // get obs-mbox-list (may contain empty entries):
  scursor++;
  while ( scursor != send ) {
    eatCFWS( scursor, send, isCRLF );
    if ( scursor == send ) {
      return false;
    }

    // empty entry:
    if ( *scursor == ',' ) {
      scursor++;
      continue;
    }

    // empty entry ending the list:
    if ( *scursor == ';' ) {
      scursor++;
      return true;
    }

    Mailbox maybeMailbox;
    if ( !parseMailbox( scursor, send, maybeMailbox, isCRLF ) ) {
      return false;
    }
    result.mailboxList.append( maybeMailbox );

    eatCFWS( scursor, send, isCRLF );
    // premature end:
    if ( scursor == send ) {
      return false;
    }
    // regular end of the list:
    if ( *scursor == ';' ) {
      scursor++;
      return true;
    }
    // eat regular list entry separator:
    if ( *scursor == ',' ) {
      scursor++;
    }
  }
  return false;
}

bool parseAddress( const char* &scursor, const char * const send,
                   Address &result, bool isCRLF )
{
  // address       := mailbox / group

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  // first try if it's a single mailbox:
  Mailbox maybeMailbox;
  const char * oldscursor = scursor;
  if ( parseMailbox( scursor, send, maybeMailbox, isCRLF ) ) {
    // yes, it is:
    result.displayName.clear();
    result.mailboxList.append( maybeMailbox );
    return true;
  }
  scursor = oldscursor;

  Address maybeAddress;

  // no, it's not a single mailbox. Try if it's a group:
  if ( !parseGroup( scursor, send, maybeAddress, isCRLF ) ) {
    return false;
  }

  result = maybeAddress;
  return true;
}

bool parseAddressList( const char* &scursor, const char * const send,
                       AddressList &result, bool isCRLF )
{
  while ( scursor != send ) {
    eatCFWS( scursor, send, isCRLF );
    // end of header: this is OK.
    if ( scursor == send ) {
      return true;
    }
    // empty entry: ignore:
    if ( *scursor == ',' ) {
      scursor++;
      continue;
    }
    // broken clients might use ';' as list delimiter, accept that as well
    if ( *scursor == ';' ) {
      scursor++;
      continue;
    }

    // parse one entry
    Address maybeAddress;
    if ( !parseAddress( scursor, send, maybeAddress, isCRLF ) ) {
      return false;
    }
    result.append( maybeAddress );

    eatCFWS( scursor, send, isCRLF );
    // end of header: this is OK.
    if ( scursor == send ) {
      return true;
    }
    // comma separating entries: eat it.
    if ( *scursor == ',' ) {
      scursor++;
    }
  }
  return true;
}

static QString asterisk = QString::fromLatin1( "*0*", 1 );
static QString asteriskZero = QString::fromLatin1( "*0*", 2 );
//static QString asteriskZeroAsterisk = QString::fromLatin1( "*0*", 3 );

// FIXME: Get rid of the very ugly "QStringOrQPair" thing. At this level, we are supposed to work
//        on byte arrays, not strings! The result parameter should be a simple
//        QPair<QByteArray,QByteArray>, which is the attribute name and the value.
bool parseParameter( const char* &scursor, const char * const send,
                     QPair<QString,QStringOrQPair> &result, bool isCRLF )
{
  // parameter = regular-parameter / extended-parameter
  // regular-parameter = regular-parameter-name "=" value
  // extended-parameter =
  // value = token / quoted-string
  //
  // note that rfc2231 handling is out of the scope of this function.
  // Therefore we return the attribute as QString and the value as
  // (start,length) tupel if we see that the value is encoded
  // (trailing asterisk), for parseParameterList to decode...

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  //
  // parse the parameter name:
  //
  // FIXME: maybeAttribute should be a QByteArray
  QString maybeAttribute;
  if ( !parseToken( scursor, send, maybeAttribute, false /* no 8bit */ ) ) {
    return false;
  }

  eatCFWS( scursor, send, isCRLF );
  // premature end: not OK (haven't seen '=' yet).
  if ( scursor == send || *scursor != '=' ) {
    return false;
  }
  scursor++; // eat '='

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    // don't choke on attribute=, meaning the value was omitted:
    if ( maybeAttribute.endsWith( asterisk ) ) {
      KMIME_WARN << "attribute ends with \"*\", but value is empty!"
        "Chopping away \"*\".";
      maybeAttribute.truncate( maybeAttribute.length() - 1 );
    }
    result = qMakePair( maybeAttribute.toLower(), QStringOrQPair() );
    return true;
  }

  const char * oldscursor = scursor;

  //
  // parse the parameter value:
  //
  QStringOrQPair maybeValue;
  if ( *scursor == '"' ) {
    // value is a quoted-string:
    scursor++;
    if ( maybeAttribute.endsWith( asterisk ) ) {
      // attributes ending with "*" designate extended-parameters,
      // which cannot have quoted-strings as values. So we remove the
      // trailing "*" to not confuse upper layers.
      KMIME_WARN << "attribute ends with \"*\", but value is a quoted-string!"
        "Chopping away \"*\".";
      maybeAttribute.truncate( maybeAttribute.length() - 1 );
    }

    if ( !parseGenericQuotedString( scursor, send, maybeValue.qstring, isCRLF ) ) {
      scursor = oldscursor;
      result = qMakePair( maybeAttribute.toLower(), QStringOrQPair() );
      return false; // this case needs further processing by upper layers!!
    }
  } else {
    // value is a token:
    if ( !parseToken( scursor, send, maybeValue.qpair, false /* no 8bit */ ) ) {
      scursor = oldscursor;
      result = qMakePair( maybeAttribute.toLower(), QStringOrQPair() );
      return false; // this case needs further processing by upper layers!!
    }
  }

  result = qMakePair( maybeAttribute.toLower(), maybeValue );
  return true;
}

// FIXME: Get rid of QStringOrQPair: Use a simply QMap<QByteArray, QByteArray> for "result"
//        instead!
bool parseRawParameterList( const char* &scursor, const char * const send,
                            QMap<QString,QStringOrQPair> &result,
                            bool isCRLF )
{
  // we use parseParameter() consecutively to obtain a map of raw
  // attributes to raw values. "Raw" here means that we don't do
  // rfc2231 decoding and concatenation. This is left to
  // parseParameterList(), which will call this function.
  //
  // The main reason for making this chunk of code a separate
  // (private) method is that we can deal with broken parameters
  // _here_ and leave the rfc2231 handling solely to
  // parseParameterList(), which will still be enough work.

  while ( scursor != send ) {
    eatCFWS( scursor, send, isCRLF );
    // empty entry ending the list: OK.
    if ( scursor == send ) {
      return true;
    }
    // empty list entry: ignore.
    if ( *scursor == ';' ) {
      scursor++;
      continue;
    }

    QPair<QString,QStringOrQPair> maybeParameter;
    if ( !parseParameter( scursor, send, maybeParameter, isCRLF ) ) {
      // we need to do a bit of work if the attribute is not
      // NULL. These are the cases marked with "needs further
      // processing" in parseParameter(). Specifically, parsing of the
      // token or the quoted-string, which should represent the value,
      // failed. We take the easy way out and simply search for the
      // next ';' to start parsing again. (Another option would be to
      // take the text between '=' and ';' as value)
      if ( maybeParameter.first.isNull() ) {
        return false;
      }
      while ( scursor != send ) {
        if ( *scursor++ == ';' ) {
          goto IS_SEMICOLON;
        }
      }
      // scursor == send case: end of list.
      return true;
    IS_SEMICOLON:
      // *scursor == ';' case: parse next entry.
      continue;
    }
    // successful parsing brings us here:
    result.insert( maybeParameter.first, maybeParameter.second );

    eatCFWS( scursor, send, isCRLF );
    // end of header: ends list.
    if ( scursor == send ) {
      return true;
    }
    // regular separator: eat it.
    if ( *scursor == ';' ) {
      scursor++;
    }
  }
  return true;
}

static void decodeRFC2231Value( Codec* &rfc2231Codec,
                                QTextCodec* &textcodec,
                                bool isContinuation, QString &value,
                                QPair<const char*,int> &source, QByteArray& charset )
{
  //
  // parse the raw value into (charset,language,text):
  //

  const char * decBegin = source.first;
  const char * decCursor = decBegin;
  const char * decEnd = decCursor + source.second;

  if ( !isContinuation ) {
    // find the first single quote
    while ( decCursor != decEnd ) {
      if ( *decCursor == '\'' ) {
        break;
      } else {
        decCursor++;
      }
    }

    if ( decCursor == decEnd ) {
      // there wasn't a single single quote at all!
      // take the whole value to be in latin-1:
      KMIME_WARN << "No charset in extended-initial-value."
        "Assuming \"iso-8859-1\".";
      value += QString::fromLatin1( decBegin, source.second );
      return;
    }

    charset = QByteArray( decBegin, decCursor - decBegin );

    const char * oldDecCursor = ++decCursor;
    // find the second single quote (we ignore the language tag):
    while ( decCursor != decEnd ) {
      if ( *decCursor == '\'' ) {
        break;
      } else {
        decCursor++;
      }
    }
    if ( decCursor == decEnd ) {
      KMIME_WARN << "No language in extended-initial-value."
        "Trying to recover.";
      decCursor = oldDecCursor;
    } else {
      decCursor++;
    }

    // decCursor now points to the start of the
    // "extended-other-values":

    //
    // get the decoders:
    //

    bool matchOK = false;
    textcodec = KGlobal::charsets()->codecForName( QLatin1String( charset ), matchOK );
    if ( !matchOK ) {
      textcodec = 0;
      KMIME_WARN_UNKNOWN( Charset, charset );
    }
  }

  if ( !rfc2231Codec ) {
    rfc2231Codec = Codec::codecForName("x-kmime-rfc2231");
    assert( rfc2231Codec );
  }

  if ( !textcodec ) {
    value += QString::fromLatin1( decCursor, decEnd - decCursor );
    return;
  }

  Decoder * dec = rfc2231Codec->makeDecoder();
  assert( dec );

  //
  // do the decoding:
  //

  QByteArray buffer;
  buffer.resize( rfc2231Codec->maxDecodedSizeFor( decEnd - decCursor ) );
  QByteArray::Iterator bit = buffer.begin();
  QByteArray::ConstIterator bend = buffer.end();

  if ( !dec->decode( decCursor, decEnd, bit, bend ) ) {
    KMIME_WARN << rfc2231Codec->name()
               << "codec lies about its maxDecodedSizeFor()" << endl
               << "result may be truncated";
  }

  value += textcodec->toUnicode( buffer.begin(), bit - buffer.begin() );

  // kDebug() << "value now: \"" << value << "\"";
  // cleanup:
  delete dec;
}

// known issues:
//  - permutes rfc2231 continuations when the total number of parts
//    exceeds 10 (other-sections then becomes *xy, ie. two digits)

bool parseParameterListWithCharset( const char* &scursor,
                                                const char * const send,
                                                QMap<QString,QString> &result,
                                                QByteArray& charset, bool isCRLF )
{
// parse the list into raw attribute-value pairs:
  QMap<QString,QStringOrQPair> rawParameterList;
  if (!parseRawParameterList( scursor, send, rawParameterList, isCRLF ) ) {
    return false;
  }

  if ( rawParameterList.isEmpty() ) {
    return true;
  }

  // decode rfc 2231 continuations and alternate charset encoding:

  // NOTE: this code assumes that what QMapIterator delivers is sorted
  // by the key!

  Codec * rfc2231Codec = 0;
  QTextCodec * textcodec = 0;
  QString attribute;
  QString value;
  enum Mode {
    NoMode = 0x0, Continued = 0x1, Encoded = 0x2
  };

  enum EncodingMode {
    NoEncoding,
    RFC2047,
    RFC2231
  };

  QMap<QString,QStringOrQPair>::Iterator it, end = rawParameterList.end();

  for ( it = rawParameterList.begin() ; it != end ; ++it ) {
    if ( attribute.isNull() || !it.key().startsWith( attribute ) ) {
      //
      // new attribute:
      //

      // store the last attribute/value pair in the result map now:
      if ( !attribute.isNull() ) {
        result.insert( attribute, value );
      }
      // and extract the information from the new raw attribute:
      value.clear();
      attribute = it.key();
      int mode = NoMode;
      EncodingMode encodingMode = NoEncoding;

      // is the value rfc2331-encoded?
      if ( attribute.endsWith( asterisk ) ) {
        attribute.truncate( attribute.length() - 1 );
        mode |= Encoded;
        encodingMode = RFC2231;
      }
      // is the value rfc2047-encoded?
      if( !(*it).qstring.isNull() && (*it).qstring.contains( QLatin1String( "=?" ) ) ) {
        mode |= Encoded;
        encodingMode = RFC2047;
      }
      // is the value continued?
      if ( attribute.endsWith( asteriskZero ) ) {
        attribute.truncate( attribute.length() - 2 );
        mode |= Continued;
      }
      //
      // decode if necessary:
      //
      if ( mode & Encoded ) {
        if ( encodingMode == RFC2231 ) {
          decodeRFC2231Value( rfc2231Codec, textcodec,
                              false, /* isn't continuation */
                              value, (*it).qpair, charset );
        }
        else if ( encodingMode == RFC2047 ) {
          value += decodeRFC2047String( (*it).qstring.toLatin1(), charset );
        }
      } else {
        // not encoded.
        if ( (*it).qpair.first ) {
          value += QString::fromLatin1( (*it).qpair.first, (*it).qpair.second );
        } else {
          value += (*it).qstring;
        }
      }

      //
      // shortcut-processing when the value isn't encoded:
      //

      if ( !(mode & Continued) ) {
        // save result already:
        result.insert( attribute, value );
        // force begin of a new attribute:
        attribute.clear();
      }
    } else { // it.key().startsWith( attribute )
      //
      // continuation
      //

      // ignore the section and trust QMap to have sorted the keys:
      if ( it.key().endsWith( asterisk ) ) {
        // encoded
        decodeRFC2231Value( rfc2231Codec, textcodec,
                            true, /* is continuation */
                            value, (*it).qpair, charset );
      } else {
        // not encoded
        if ( (*it).qpair.first ) {
          value += QString::fromLatin1( (*it).qpair.first, (*it).qpair.second );
        } else {
          value += (*it).qstring;
        }
      }
    }
  }

  // write last attr/value pair:
  if ( !attribute.isNull() ) {
    result.insert( attribute, value );
  }

  return true;
}


bool parseParameterList( const char* &scursor, const char * const send,
                         QMap<QString,QString> &result, bool isCRLF )
{
  QByteArray charset;
  return parseParameterListWithCharset( scursor, send, result, charset, isCRLF );
}

static const char * const stdDayNames[] = {
  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const int stdDayNamesLen = sizeof stdDayNames / sizeof *stdDayNames;

static bool parseDayName( const char* &scursor, const char * const send )
{
  // check bounds:
  if ( send - scursor < 3 ) {
    return false;
  }

  for ( int i = 0 ; i < stdDayNamesLen ; ++i ) {
    if ( qstrnicmp( scursor, stdDayNames[i], 3 ) == 0 ) {
      scursor += 3;
      // kDebug() << "found" << stdDayNames[i];
      return true;
    }
  }

  return false;
}

static const char * const stdMonthNames[] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static const int stdMonthNamesLen =
                              sizeof stdMonthNames / sizeof *stdMonthNames;

static bool parseMonthName( const char* &scursor, const char * const send,
                            int &result )
{
  // check bounds:
  if ( send - scursor < 3 ) {
    return false;
  }

  for ( result = 0 ; result < stdMonthNamesLen ; ++result ) {
    if ( qstrnicmp( scursor, stdMonthNames[result], 3 ) == 0 ) {
      scursor += 3;
      return true;
    }
  }

  // not found:
  return false;
}

static const struct {
  const char * tzName;
  long int secsEastOfGMT;
} timeZones[] = {
  // rfc 822 timezones:
  { "GMT", 0 },
  { "UT", 0 },
  { "EDT", -4*3600 },
  { "EST", -5*3600 },
  { "MST", -5*3600 },
  { "CST", -6*3600 },
  { "MDT", -6*3600 },
  { "MST", -7*3600 },
  { "PDT", -7*3600 },
  { "PST", -8*3600 },
  // common, non-rfc-822 zones:
  { "CET", 1*3600 },
  { "MET", 1*3600 },
  { "UTC", 0 },
  { "CEST", 2*3600 },
  { "BST", 1*3600 },
  // rfc 822 military timezones:
  { "Z", 0 },
  { "A", -1*3600 },
  { "B", -2*3600 },
  { "C", -3*3600 },
  { "D", -4*3600 },
  { "E", -5*3600 },
  { "F", -6*3600 },
  { "G", -7*3600 },
  { "H", -8*3600 },
  { "I", -9*3600 },
  // J is not used!
  { "K", -10*3600 },
  { "L", -11*3600 },
  { "M", -12*3600 },
  { "N", 1*3600 },
  { "O", 2*3600 },
  { "P", 3*3600 },
  { "Q", 4*3600 },
  { "R", 5*3600 },
  { "S", 6*3600 },
  { "T", 7*3600 },
  { "U", 8*3600 },
  { "V", 9*3600 },
  { "W", 10*3600 },
  { "X", 11*3600 },
  { "Y", 12*3600 },
};
static const int timeZonesLen = sizeof timeZones / sizeof *timeZones;

static bool parseAlphaNumericTimeZone( const char* &scursor,
                                       const char * const send,
                                       long int &secsEastOfGMT,
                                       bool &timeZoneKnown )
{
  QPair<const char*,int> maybeTimeZone( 0, 0 );
  if ( !parseToken( scursor, send, maybeTimeZone, false /*no 8bit*/ ) ) {
    return false;
  }
  for ( int i = 0 ; i < timeZonesLen ; ++i ) {
    if ( qstrnicmp( timeZones[i].tzName,
                    maybeTimeZone.first, maybeTimeZone.second ) == 0 ) {
      scursor += maybeTimeZone.second;
      secsEastOfGMT = timeZones[i].secsEastOfGMT;
      timeZoneKnown = true;
      return true;
    }
  }

  // don't choke just because we don't happen to know the time zone
  KMIME_WARN_UNKNOWN( time zone,
                      QByteArray( maybeTimeZone.first, maybeTimeZone.second ) );
  secsEastOfGMT = 0;
  timeZoneKnown = false;
  return true;
}

// parse a number and return the number of digits parsed:
int parseDigits( const char* &scursor, const char * const send, int &result )
{
  result = 0;
  int digits = 0;
  for ( ; scursor != send && isdigit( *scursor ) ; scursor++, digits++ ) {
    result *= 10;
    result += int( *scursor - '0' );
  }
  return digits;
}

static bool parseTimeOfDay( const char* &scursor, const char * const send,
                            int &hour, int &min, int &sec, bool isCRLF=false )
{
  // time-of-day := 2DIGIT [CFWS] ":" [CFWS] 2DIGIT [ [CFWS] ":" 2DIGIT ]

  //
  // 2DIGIT representing "hour":
  //
  if ( !parseDigits( scursor, send, hour ) ) {
    return false;
  }

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send || *scursor != ':' ) {
    return false;
  }
  scursor++; // eat ':'

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  //
  // 2DIGIT representing "minute":
  //
  if ( !parseDigits( scursor, send, min ) ) {
    return false;
  }

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return true; // seconds are optional
  }

  //
  // let's see if we have a 2DIGIT representing "second":
  //
  if ( *scursor == ':' ) {
    // yepp, there are seconds:
    scursor++; // eat ':'
    eatCFWS( scursor, send, isCRLF );
    if ( scursor == send ) {
      return false;
    }

    if ( !parseDigits( scursor, send, sec ) ) {
      return false;
    }
  } else {
    sec = 0;
  }

  return true;
}

bool parseTime( const char* &scursor, const char * send,
                int &hour, int &min, int &sec, long int &secsEastOfGMT,
                bool &timeZoneKnown, bool isCRLF )
{
  // time := time-of-day CFWS ( zone / obs-zone )
  //
  // obs-zone    := "UT" / "GMT" /
  //                "EST" / "EDT" / ; -0500 / -0400
  //                "CST" / "CDT" / ; -0600 / -0500
  //                "MST" / "MDT" / ; -0700 / -0600
  //                "PST" / "PDT" / ; -0800 / -0700
  //                "A"-"I" / "a"-"i" /
  //                "K"-"Z" / "k"-"z"

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  if ( !parseTimeOfDay( scursor, send, hour, min, sec, isCRLF ) ) {
    return false;
  }

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    timeZoneKnown = false;
    secsEastOfGMT = 0;
    return true; // allow missing timezone
  }

  timeZoneKnown = true;
  if ( *scursor == '+' || *scursor == '-' ) {
    // remember and eat '-'/'+':
    const char sign = *scursor++;
    // numerical timezone:
    int maybeTimeZone;
    if ( parseDigits( scursor, send, maybeTimeZone ) != 4 ) {
      return false;
    }
    secsEastOfGMT = 60 * ( maybeTimeZone / 100 * 60 + maybeTimeZone % 100 );
    if ( sign == '-' ) {
      secsEastOfGMT *= -1;
      if ( secsEastOfGMT == 0 ) {
        timeZoneKnown = false; // -0000 means indetermined tz
      }
    }
  } else {
    // maybe alphanumeric timezone:
    if ( !parseAlphaNumericTimeZone( scursor, send, secsEastOfGMT, timeZoneKnown ) ) {
      return false;
    }
  }
  return true;
}

bool parseDateTime( const char* &scursor, const char * const send,
                    KDateTime &result, bool isCRLF )
{
  // Parsing date-time; strict mode:
  //
  // date-time   := [ [CFWS] day-name [CFWS] "," ]                      ; wday
  // (expanded)     [CFWS] 1*2DIGIT CFWS month-name CFWS 2*DIGIT [CFWS] ; date
  //                time
  //
  // day-name    := "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
  // month-name  := "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
  //                "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"

  result = KDateTime();
  QDateTime maybeDateTime;

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  //
  // let's see if there's a day-of-week:
  //
  if ( parseDayName( scursor, send ) ) {
    eatCFWS( scursor, send, isCRLF );
    if ( scursor == send ) {
      return false;
    }
    // day-name should be followed by ',' but we treat it as optional:
    if ( *scursor == ',' ) {
      scursor++; // eat ','
      eatCFWS( scursor, send, isCRLF );
    }
  }

  //
  // 1*2DIGIT representing "day" (of month):
  //
  int maybeDay;
  if ( !parseDigits( scursor, send, maybeDay ) ) {
    return false;
  }

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  //
  // month-name:
  //
  int maybeMonth = 0;
  if ( !parseMonthName( scursor, send, maybeMonth ) ) {
    return false;
  }
  if ( scursor == send ) {
    return false;
  }
  assert( maybeMonth >= 0 ); assert( maybeMonth <= 11 );
  ++maybeMonth; // 0-11 -> 1-12

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  //
  // 2*DIGIT representing "year":
  //
  int maybeYear;
  if ( !parseDigits( scursor, send, maybeYear ) ) {
    return false;
  }
  // RFC 2822 4.3 processing:
  if ( maybeYear < 50 ) {
    maybeYear += 2000;
  } else if ( maybeYear < 1000 ) {
    maybeYear += 1900;
  }
  // else keep as is
  if ( maybeYear < 1900 ) {
    return false; // rfc2822, 3.3
  }

  eatCFWS( scursor, send, isCRLF );
  if ( scursor == send ) {
    return false;
  }

  maybeDateTime.setDate( QDate( maybeYear, maybeMonth, maybeDay ) );

  //
  // time
  //
  int maybeHour, maybeMinute, maybeSecond;
  long int secsEastOfGMT;
  bool timeZoneKnown = true;

  if ( !parseTime( scursor, send,
                   maybeHour, maybeMinute, maybeSecond,
                   secsEastOfGMT, timeZoneKnown, isCRLF ) ) {
    return false;
  }

  maybeDateTime.setTime( QTime( maybeHour, maybeMinute, maybeSecond ) );
  if ( !maybeDateTime.isValid() )
    return false;

  result = KDateTime( maybeDateTime, KDateTime::Spec( KDateTime::OffsetFromUTC, secsEastOfGMT ) );
  if ( !result.isValid() )
    return false;
  return true;
}

Headers::Base *extractFirstHeader( QByteArray &head )
{
  int endOfFieldBody = 0;
  bool folded = false;
  Headers::Base *header = 0;

  int startOfFieldBody = head.indexOf( ':' );
  const int endOfFieldHeader = startOfFieldBody;

  if ( startOfFieldBody > -1 ) {    //there is another header
    startOfFieldBody++; //skip the ':'
    if ( head[startOfFieldBody] == ' ' ) { // skip the space after the ':', if there
      startOfFieldBody++;
    }
    endOfFieldBody = findHeaderLineEnd( head, startOfFieldBody, &folded );

    QByteArray rawType = head.left( endOfFieldHeader );
    QByteArray rawFieldBody = head.mid( startOfFieldBody, endOfFieldBody - startOfFieldBody );
    if ( folded ) {
      rawFieldBody = unfoldHeader( rawFieldBody );
    }
    // We might get an invalid mail without a field name, don't crash on that.
    if ( !rawType.isEmpty() ) {
      header = HeaderFactory::self()->createHeader( rawType );
    }
    if( !header ) {
      //kWarning() << "Returning Generic header of type" << rawType;
      header = new Headers::Generic( rawType.constData() );
    }
    header->from7BitString( rawFieldBody );

    head.remove( 0, endOfFieldBody + 1 );
  } else {
    head.clear();
  }

  return header;
}

void extractHeaderAndBody( const QByteArray &content, QByteArray &header, QByteArray &body )
{
  header.clear();
  body.clear();

  // empty header
  if ( content.startsWith( '\n' ) ) {
    body = content.right( content.length() - 1 );
    return;
  }

  int pos = content.indexOf( "\n\n", 0 );
  if ( pos > -1 ) {
    header = content.left( ++pos );  //header *must* end with "\n" !!
    body = content.mid( pos + 1, content.length() - pos - 1 );
  } else {
    header = content;
  }
}

Headers::Base::List parseHeaders( const QByteArray &head )
{
  Headers::Base::List ret;
  Headers::Base *h;

  QByteArray copy = head;
  while( ( h = extractFirstHeader( copy ) ) ) {
    ret << h;
  }

  return ret;
}

} // namespace HeaderParsing

} // namespace KMime