File: mimedecode.c

package info (click to toggle)
2utf 1.04
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 416 kB
  • ctags: 339
  • sloc: ansic: 3,592; makefile: 313; sh: 40
file content (1542 lines) | stat: -rw-r--r-- 40,153 bytes parent folder | download
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
/*
 * This program comes with absolutely no guarantees and may
 * be used for whatever purpose,
 *
 *      Frederik H. Andersen - 1996
 *      Dansk Data Elektronik A/S
 *
 *      fha@dde.dk
 *
 */

/*
 * This program performs the decoding of transfer encoded text type
 * mime messages. The message in its entirety is read from stdin.
 * The decoded message is written to stdout; hence, this program
 * behaves as a filter which may be placed wherever convenient.
 * I particular like it in front of my slocal command in my .forward
 * file.
 *
 * It is assumed that the message has reached its point of final
 * delivery and at that point 8-bit text types can be handled
 * natively. Hence, the need for transfer-encodings is not present
 * any more.
 *
 * Only some cases are handled:
 *      - encoded header fields are decoded from QP or B encoding.
 *        The charset is assumed to be iso-8859-1
 *      - part or subparts of content-type text only are decoded
 *      - all other content-types are passed transparently
 *     
 */

/* extensive changes by Ricardas Cepas <rch@pub.osf.lt>
 *        The charsets are converted to UTF-8
 */

#define _POSIX_C_SOURCE 2

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>

/* Some defines. Should have gone into a file by itself.
 */
#define MIMED_VERSION "mimedecode version 1.8"

#define lower(c)        ( isupper(c) ? tolower(c) : (c) )

#include "2UTF.h"

/* content types: */
#define UNDEF		0
#define TEXT		1
#define MULT    	2
#define MULT_DIGEST    	3
#define MULT_PGP    	4
#define MESG    	5

/* transfer encodings: */
#define QP	1
#define B64	2
#define BIT7	3
#define BIT8	4
#define BINARY	5

#define UN 255

#define LWSP " \t\n"

unsigned char b64_map[256] =
{
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0x00 - 0x0f, NUL - SI */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0x10 - 0x1f, DLE - US */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, 62, UN, UN, UN, 63,	/* 0x20 - 0x2f, SP  - / */
  52, 53, 54, 55, 56, 57, 58, 59, 60, 61, UN, UN, UN, 0x3d, UN, UN,	/* 0x30 - 0x3f, 0   - ? */
  UN, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,		/* 0x40 - 0x4f, @   - O */
  15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, UN, UN, UN, UN, UN,	/* 0x50 - 0x5f, P   - _ */
  UN, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,	/* 0x60 - 0x6f, `   - o */
  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, UN, UN, UN, UN, UN,	/* 0x70 - 0x7f, p   - DEL */

  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0x80 - 0x8f */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0x90 - 0x9f */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0xA0 - 0xAf */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0xB0 - 0xBf */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0xC0 - 0xCf */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0xD0 - 0xDf */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0xE0 - 0xEf */
  UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN, UN,	/* 0xF0 - 0xFf */
};

char *especials = "()<>@,;:\\\"/[]?.=";

int reserve = 100;

/*******************************************************************/
int
parse_message (boundary)
/*******************************************************************/
     char *boundary;
{
  int c, saved_content_type;
  struct mime_header_type mhead =
  {0, 0, "\0", "\0", "\0"};
  int ret;

  if (Debug)
    if (boundary)
      fprintf (stderr, " - Entry parse_message with boundary=\"%s\" \n", boundary);
    else
      fprintf (stderr, " - Entry parse_message without boundary \n");

  /* parse header */
  if (!(ret = parse_header (&mhead)))
    {
      if (Debug)
	fprintf (stderr, "parse of message header failed: %d\n", ret);
      return (FALSE);
    }

  if (mhead.content_type == MULT || mhead.content_type == MULT_DIGEST)
    {
      if (Debug >= 2)
	{
	  fprintf (stderr, "message is multipart\n");
	  fprintf (stderr, "search header boundary line: %s\n", mhead.boundary);
	}

      /* search for the boundary line before parsing further */
      if ((ret = Seek_boundary (stdin, mhead.boundary, PUSH_BOUNDARY)) == TRUE)
	/* When the message is of type multipart we have to handle each
	 * part as an individual message.
	 * We use the boundary to identify the beginning of each part
	 */
	do
	  /* Check if part's own headers are missing */
	  if ((c = getc (stdin)) == '\n')
	    {
	      putchar (c);
	      if (mhead.content_type == MULT_DIGEST)
		/* default is message/rfc */
		ret = parse_message (mhead.boundary);
	      else
		{		/* default is text/plain; charset=us-ascii */
		  saved_content_type = mhead.content_type;
		  mhead.content_type = TEXT;
		  if (charset_p->USASCII_is_subset == NO)
		    Validate_charset ("us-ascii", 0);
		  ret = parse_body (&mhead, mhead.boundary);
		  mhead.content_type = saved_content_type;
		}
	    }
	  else
	    {
	      ungetc (c, stdin);
	      ret = parse_message (mhead.boundary);
	    }
	while (ret == TRUE);
      if (boundary)		/* && ret == END_BOUNDARY) */
	/* skip stuff after the last part of inner multipart message */
	ret = Seek_boundary (ret < OUTER_BOUNDARY ? stdin : NULL, boundary, 0);

    }
  else if (mhead.content_type == MESG)
    {
      /* When message is type message we have to handle the body as
       * an individual message.
       */
      ret = parse_message (boundary);
    }
  else
    /* plain message */
    {
      ret = parse_body (&mhead, boundary);
    }
  if (Debug)
    if (boundary)
      fprintf (stderr, " - parse_message with boundary=\"%s\" returned %i - \n", boundary, ret);
    else
      fprintf (stderr, " - parse_message without boundary returned %i - \n", ret);
  return (ret);
}



/*******************************************************************/
int
parse_body (mime_header_p, boundary)
/*******************************************************************/
/*     int type, encoding; */
     struct mime_header_type *mime_header_p;
     char *boundary;
{
  int c;
  int ret = FALSE;		/* ??? */

  /*struct charset_type *old_charset_p; */

  if (Debug)
    fprintf (stderr, " -- Entry parse_body -- \n");
  if (Debug >= 2)
    fprintf (stderr, " -- type: %d, enc: %d\n", \
	     mime_header_p->content_type, mime_header_p->transfer_encoding);

  if ((mime_header_p->content_type == TEXT) && \
      ((mime_header_p->transfer_encoding == QP) \
       ||(mime_header_p->transfer_encoding == B64) \
       ||(mime_header_p->transfer_encoding == BIT8) \
       ||(mime_header_p->transfer_encoding == BIT7) \
      ))
    {
      if (boundary)
	{
	  if (Debug >= 2)
	    fprintf (stderr, " -- with boundary: %s\n -- decoding -- \n", \
		     boundary);
	  if (mime_header_p->transfer_encoding == QP)
	    ret = decode_quoted_printable \
	      (stdin, charset_p->pipe, boundary);
	  else if (mime_header_p->transfer_encoding == B64)
	    ret = decode_base64 (stdin, charset_p->pipe, boundary);

	  if (ret == FALSE)
	    ret = Seek_boundary (stdin, boundary, 0);
	}
      else
	/* no boundary */
	{
	  if (Debug >= 2)
	    fprintf (stderr, " -- decoding -- \n");
	  if (mime_header_p->transfer_encoding == QP)
	    ret = decode_quoted_printable (stdin, charset_p->pipe, 0);
	  else if (mime_header_p->transfer_encoding == B64)
	    ret = decode_base64 (stdin, charset_p->pipe, 0);
	  else			/*if (mime_header_p->transfer_encoding == BIT8 \
				   || mime_header_p->transfer_encoding == BIT7) */
	    ret = 2, Pipe_to_UTF8 (stdin);
	}
    }
  else if (boundary)
    {
      if (Debug >= 2)
	fprintf (stderr, " -- with boundary: %s\n", boundary);
      if (Debug >= 2)
	fprintf (stderr, " -- skipping -- \n");
      /*  old_charset_p = charset_p;
         charset_p = &unknown_charset; */
      Validate_charset ("'unknown'", 0);
      ret = Seek_boundary (stdin, boundary, 0);
      Validate_charset (NULL, 0);
/*      charset_p = old_charset_p; */
    }
  else
    {
      if (Debug >= 2)
	fprintf (stderr, " -- skipping -- \n");
      Validate_charset ("'unknown'", 0);
      ret = 2;
      while ((c = getc (stdin)) != EOF)
	putc (c, charset_p->pipe);
    }
  return (ret);
}

char *
Add_to_buffer (struct buffer_type *buffer_p, char *string)
{
/*  size_t old_size = buffer_p->end - buffer_p->start + 1;

   if (Debug >= 9)
   fprintf (stderr, "2UTF: realloc %i += %i \n", \
   old_size, strlen (string));
   buffer_p->start = xrealloc (buffer_p->start, (old_size + strlen (string)));
   *//* buffer_p->start changes *//*
     return (buffer_p->end = Stpcpy (buffer_p->start + old_size - 1, string));
   */
  size_t length;

  length = strlen (string);
  if (buffer_p->next + length - buffer_p->end > reserve)
    {
      fprintf (stderr, "2UTF: %s %i\n", buffer_overflow, \
	       buffer_p->next + length - buffer_p->end - reserve);
      length = buffer_p->end - buffer_p->next - reserve;
    }
  memcpy (buffer_p->next, string, length);
  return (buffer_p->next += length);
}

/*******************************************************************/
int
parse_header (mhp)
/*******************************************************************/
     struct mime_header_type *mhp;
{
  unsigned char *cp;
  unsigned char *nlbp;
  int c;
  int ct_charset_unknown = FALSE;	/* Warning about unknown charset in ct */
  size_t ct_header_charset_name_begin = 0, ct_header_charset_name_end = 0;
  int ct_is_changing = FALSE;	/* Content charset parameter needs change */
  int ct_read = FALSE;		/* Content-type field read */
  int ct_written = FALSE;	/* Content-type written */
  int cte_read = FALSE;		/* Content-transfer-encoding read */
  int cte_written = FALSE;	/* Content-transfer-encoding written */
  int headers_8bit = FALSE;	/* non-MIME-encrypted headers */
  int quoted;
  struct buffer_type headers_buffer =
  {NULL, NULL, NULL, NULL};

#define REALLOC_CHUNK 4000
  line.buf = headers_buffer.next = headers_buffer.start \
    = headers_buffer.header_start = xrealloc (line.buf, line.length += reserve);
  headers_buffer.end = headers_buffer.start + line.length - reserve;
/*    = xmalloc (REALLOC_CHUNK * 2 + reserve);
   headers_buffer.end = headers_buffer.start + REALLOC_CHUNK * 2;
 */
  if (Debug)
    fprintf (stderr, " -- Entry parse_header - pass 1 -- \n");

  mhp->boundary[0] = '\001';
  mhp->content_type = UNDEF;
  mhp->transfer_encoding = BIT8;
  mhp->headers_charset[0] = '\0';
  mhp->content_charset[0] = '\0';

  /* first pass */
  while (TRUE)
    {
      if ((c = getc (stdin)) == EOF)
	{
	  Validate_charset ("us-ascii", 0);
	  fwrite (headers_buffer.start, 1, \
	       headers_buffer.next - headers_buffer.start, charset_p->pipe);
	  return (FALSE);
	}
    again:
      *headers_buffer.next++ = c;
      while (headers_buffer.next >= headers_buffer.end)
	{			/* realloc more memory */
	  size_t old_size = headers_buffer.end - headers_buffer.start;
	  size_t next_offset = headers_buffer.next - headers_buffer.start;
	  size_t header_start_offset = \
	  headers_buffer.header_start - headers_buffer.start;

	  if (Debug >= 9)
	    fprintf (stderr, "2UTF: realloc %i += %i \n", \
		     old_size + reserve, REALLOC_CHUNK);
	  line.buf = headers_buffer.start = xrealloc (headers_buffer.start, \
					old_size + reserve + REALLOC_CHUNK);
	  line.length += REALLOC_CHUNK;
	  headers_buffer.end = headers_buffer.start + old_size + REALLOC_CHUNK;
	  headers_buffer.next = headers_buffer.start + next_offset;
	  headers_buffer.header_start = \
	    headers_buffer.start + header_start_offset;
	  if (line.length != old_size + reserve + REALLOC_CHUNK && Debug)
	    fprintf (stderr, "2UTF: memory allocation error: \n  %i != %i \n", \
		     line.length, old_size + reserve + REALLOC_CHUNK);
	}
      if ((c > 0x7F || (c != '\n' && c != '\t' && iscntrl (c))) \
	  &&headers_8bit == FALSE)
	{
	  headers_8bit = TRUE;
	  if (Debug >= 2)
	    fprintf (stderr, \
		 " -- 8 bit bytes or control codes in headers detected \n");
	}
      if (c == '\n')		/* end of line reached */
	{
	  if ((c = getc (stdin)) == EOF)
	    {
	      Add_to_buffer (&headers_buffer, "\n");
	      Validate_charset ("us-ascii", 0);
	      fwrite (headers_buffer.start, 1, \
		      headers_buffer.next - headers_buffer.start, \
		      charset_p->pipe);
	      return (FALSE);
	      break;
	    }
	  else if (c == ' ' || c == '\t')
	    /* if next line begins with space 
	     * it is continuation of the same header
	     */
	    goto again;
	  else
	    ungetc (c, stdin);
	  *headers_buffer.next = '\0';	/* zero terminate the line buf */
/*        headers_buffer.header_start = headers_buffer.next; */

	  if (Debug >= 4)
	    fprintf (stderr, "headers_buffer: %s\n", headers_buffer.header_start);

	  if (!Strcase_has_prefix (headers_buffer.header_start, "content-type:"))
	    {
	      ct_read = TRUE;
	      cp = headers_buffer.header_start + strlen ("Content-Type:");
	      cp += strspn (cp, LWSP);

	      /* we are only doing decoding of text types */
	      if (Strcase_has_prefix (cp, "text/") == 0)
		{
		  if (Debug >= 3)
		    fprintf (stderr, "Content IS text\n");

		  mhp->content_type = TEXT;

		  if ((cp = strchr (cp, ';')))
		    while ((cp = strchr (cp, ';')))
		      if (Strcase_has_prefix (cp += strspn (cp, "; \t\n"), \
			  "charset=") == 0)
			{
			  ct_header_charset_name_begin = (cp += 8) - headers_buffer.header_start;
			  cp += strspn (cp, "'\"");
			  if ((cp[0] == 'x' || cp[0] == 'X') \
			      &&cp[1] == '-')
			    cp += 2;
			  ct_header_charset_name_end = \
			    cp + strcspn (cp, ";\n\r") - headers_buffer.header_start;
			  strncpy (mhp->content_charset, cp, \
				   MIN (79, strcspn (cp, "*'\"; \t\n\r")));
			  mhp->content_charset[\
			     MIN (79, strcspn (cp, "*'\"; \t\n\r"))] = '\0';
			  if (Debug)
			    fprintf (stderr, " -- Content charset: %s\n", \
				     mhp->content_charset);

			  break;
			}
		}
	      else if (Strcase_has_prefix (cp, "multipart/") == 0)
		{
		  if (Strcase_has_prefix \
		      (cp += strlen ("multipart/"), "digest") == 0)
		    mhp->content_type = MULT_DIGEST;
		  else if (Strcase_has_prefix (cp, "pgp") == 0 \
		       ||Strcase_has_prefix (cp, "signed") == 0)
		    mhp->content_type = MULT_PGP;
		  else
		    mhp->content_type = MULT;

		  /* search for the boundary parameter */
		  while ((cp = strchr (cp, ';')))
		    if (Strcase_has_prefix (cp += 1 + strspn (1 + cp, LWSP), \
				     "boundary=") == 0)
		      /* skip ; and remove white space */
		      {
			cp += strlen ("boundary=");
			if (*cp == '"')
			  {
			    quoted = TRUE;
			    cp++;
			  }
			else
			  quoted = FALSE;
			*(cp = Stpncpy (mhp->boundary, cp, 78)) = '"';
			*++cp = '\0';
			*(cp = mhp->boundary + strcspn (mhp->boundary, \
				       quoted ? "\"\n" : "\"" LWSP)) = '\0';
			/* remove trailing white space */
			cp--;
			while ((char *) cp >= mhp->boundary \
			       &&(*cp == ' ' || *cp == '\t'))
			  *cp-- = '\0';

			if ((char *) cp <= mhp->boundary \
			    ||mhp->boundary[0] == '\0')
			  {
			    mhp->boundary[0] = '\0';
			    mhp->content_type = UNDEF;
			  }
			break;
		      }
		  if (cp == NULL)
		    {
		      if (Debug)
			fprintf (stderr, "boundary parameter missing ! \n");
		      mhp->content_type = UNDEF;
		      mhp->boundary[0] = '\0';
		    }
		}
	      else if (Strcase_has_prefix (cp, "message/") == 0)
		{
		  mhp->content_type = MESG;
		}
	      nlbp = headers_buffer.header_start;
	    }
	  else if (!Strcase_has_prefix (headers_buffer.header_start, "content-transfer-encoding:"))
	    {
	      cte_read = TRUE;

	      /* Remember the encoding in mhp! */
	      cp = headers_buffer.header_start + strlen ("content-transfer-encoding:");
	      cp += strspn (cp, LWSP);	/* remove white space */
	      {
		if (!Strcase_has_prefix (cp, "quoted-printable"))
		  {
		    if (Debug >= 3)
		      fprintf (stderr, "Transfer-encoding IS QP\n");
		    mhp->transfer_encoding = QP;
		  }
		else if (!Strcase_has_prefix (cp, "base64"))
		  {
		    if (Debug >= 3)
		      fprintf (stderr, "Transfer-encoding IS B64\n");
		    mhp->transfer_encoding = B64;
		  }
		else if (!Strcase_has_prefix (cp, "7bit"))
		  {
		    if (Debug >= 3)
		      fprintf (stderr, "Transfer-encoding IS 7bit\n");
		    mhp->transfer_encoding = BIT7;
		  }
		else if (!Strcase_has_prefix (cp, "8bit"))
		  {
		    if (Debug >= 3)
		      fprintf (stderr, "Transfer-encoding IS 8bit\n");
		    mhp->transfer_encoding = BIT8;
		  }
		else if (!Strcase_has_prefix (cp, "binary"))
		  {
		    if (Debug >= 3)
		      fprintf (stderr, "Transfer-encoding IS BINARY\n");
		    mhp->transfer_encoding = BINARY;
		  }
		else
		  {
		    if (Debug >= 3)
		      fprintf (stderr, "Transfer-encoding IS UNKNOWN\n");
		    mhp->transfer_encoding = UNDEF;
		  }
	      }
	      nlbp = NULL;
	    }
	  else
	    {
	      if ((nlbp = decode_header_line (QUERY_CHARSET_NAME, \
					      &headers_buffer)))
		{
		  if ((nlbp[0] == 'x' || nlbp[0] == 'X') \
		      &&nlbp[1] == '-')
		    nlbp += 2;
		  strncpy (mhp->headers_charset, nlbp, \
			   MIN (strcspn (nlbp, "*"), 79));
		  mhp->headers_charset[\
				       MIN (strcspn (nlbp, "*"), 79)] = '\0';
		  if (Debug)
		    fprintf (stderr, " -- Headers's charset: %s\n", \
			     mhp->headers_charset);
		}
	      nlbp = headers_buffer.header_start;
	    }

	  if (Debug >= 7)
	    fprintf (stderr, "Adding to buffer: %s\n", nlbp);

/* Generally, we will output _all_ lines as they are read (and
 * decoded).  The exception is the Content-transfer-encoding (cte)
 * and Content-Type header lines:
 * We will only output cte when the Content-type has been read or
 * when the header/body boundary have been reached.
 */
	  if (nlbp)
	    headers_buffer.header_start = headers_buffer.next;
	  else
	    headers_buffer.next = headers_buffer.header_start;

	  /* Have we reached the header/body boundary? */
	  if ((c = getc (stdin)) == '\n')
	    {
	      /* yes we have! */

	      if (Debug >= 2)
		fprintf (stderr, "body reached\n");

	      if (cte_read && !cte_written)
		{
		  if (!ct_read)
		    {
		      /* Content-type field is missing!! 
		       * Assume text/plain!
		       */
		      if (Debug >= 3)
			fprintf (stderr, "Content-type field is missing!!\n");
		      Add_to_buffer (&headers_buffer, \
				     "X-2UTF-Content-type: missing\n");
		      headers_buffer.header_start = headers_buffer.next;

		      ct_read = TRUE;
		      mhp->content_type = TEXT;
		    }

		  write_cte (&headers_buffer, \
			     mhp->transfer_encoding, mhp->content_type);
		  cte_written = TRUE;
		}
	      /* should we output the '\n' here? */
	      Add_to_buffer (&headers_buffer, "\n");
	      break;
	    }

	  /* 
	   * We have _not_ reached the header/body boundary
	   */
	  if (ct_read && cte_read & !cte_written)
	    {
	      write_cte (&headers_buffer, \
			 mhp->transfer_encoding, mhp->content_type);
	      cte_written = TRUE;
	    }
	  goto again;
	}
    }
  if (!ct_read)
    mhp->content_type = TEXT;
  /* second pass */

  if (Debug)
    fprintf (stderr, " -- parse_header - pass 2 -- \n");

  if (Debug >= 4)
    fprintf (stderr, "headers charset: %s\ncontent charset: %s\n", \
	     mhp->headers_charset, mhp->content_charset);

/* 1) test if charset is known before changing Content-Type header
 * 2) set charset for non-MIME-encrypted headers - 
 * - assuming the same as Content's one
 */
  if (mhp->content_charset[0] != '\0' && \
      Strcasecmp (mhp->content_charset, "us-ascii") != 0)
    {
      if (Validate_charset (mhp->content_charset, IS))
	{
	  if (charset_p->type != UTF8)
	    ct_is_changing = TRUE;
	}
      else
	ct_charset_unknown = TRUE;
    }
  else if (mhp->headers_charset[0] != '\0')
    {
      Validate_charset (mhp->headers_charset, IS);
    }
  else
    Validate_charset ("us-ascii", 0);
/*
   if (ct_read && mhp->content_type == TEXT && \
   ct_header_charset_name_end)
   if (charset_p->type == KNOWN || \
   charset_p->type == BUF_PIPE || charset_p->type == NON_BUF_PIPE)
   ct_is_changing = TRUE;
   else if (charset_p->type == UNKNOWN && mhp->content_charset[0] != '\0' && \
   Strcasecmp (mhp->content_charset, "us-ascii") != 0)
   ct_charset_unknown = TRUE;
 */
  headers_buffer.end = headers_buffer.next;	/* no more Add_to_buffer() */
  headers_buffer.header_start = headers_buffer.next = headers_buffer.start;
  for (; headers_buffer.next <= headers_buffer.end; c = *headers_buffer.next++)
    {
      /*again2: *//* *headers_buffer.next++ = c; */
      if (c == '\n' && \
	  !(*headers_buffer.next == ' ' || *headers_buffer.next == '\t'))
	/* end of (folded?) header line reached */
	{
	  unsigned char saved_char;
	  struct buffer_type new_ct;

	  saved_char = *headers_buffer.next;
	  *headers_buffer.next = '\0';	/* zero terminate the line buf */

	  if (Debug >= 4)
	    fprintf (stderr, "header: %s\n", headers_buffer.header_start);

	  if (!ct_written && Strcase_has_prefix \
	      (headers_buffer.header_start, "Content-Type:") == 0)
	    {
	      ct_written = TRUE;
	      if (ct_is_changing)
		{
		  fputs ("X-2UTF-Old-", charset_p->pipe);
		  decode_header_line (DECODE, &headers_buffer);
		  /* changing charset name to UTF-8 */
		  if (ct_header_charset_name_begin > ct_header_charset_name_end)
		    Error (internal_err);
		  new_ct.start = new_ct.header_start = xmalloc (\
		   strlen (headers_buffer.header_start) + strlen ("UTF-8"));
		  strcpy (Stpcpy (Stpncpy (new_ct.start, \
					   headers_buffer.header_start, \
					   ct_header_charset_name_begin), \
				  "UTF-8"),
		  headers_buffer.header_start + ct_header_charset_name_end);
		  new_ct.next = new_ct.header_start \
		    +strlen (new_ct.header_start);
		  decode_header_line (DECODE, &new_ct);
		  free (new_ct.start);
		}
	      else
		{
		  decode_header_line (DECODE, &headers_buffer);
		  if (ct_charset_unknown)
		    fprintf (charset_p->pipe, \
			     "X-2UTF-Warning: " \
		       "Unknown charset in Content-Type header: \"%s\"\n", \
			     mhp->content_charset);
		}
	      if (mhp->boundary[0] == '\0')
		fputs ("X-2UTF-Warning: missing boundary parameter\n", \
		       charset_p->pipe);
	    }
	  else
	    decode_header_line (DECODE, &headers_buffer);
	  /* Have we reached the header/body boundary? */
	  if (saved_char == '\n')
	    {
	      /* yes we have! */

	      if (Debug >= 2)
		fprintf (stderr, "body reached\n");

	      /* should we output the '\n' here? */
	      putc ('\n', charset_p->pipe);
	      goto Return;
	    }
	  *headers_buffer.next = saved_char;
	  headers_buffer.header_start = headers_buffer.next;
	}
    }
/* set charset for message body */
Return:
  if (mhp->content_charset[0] != '\0'	/* && \
					   Strcasecmp (mhp->content_charset, "us-ascii") != 0 */ )
    Validate_charset (mhp->content_charset, 0);
  else if (mhp->headers_charset[0] != '\0')
    Validate_charset (mhp->headers_charset, 0);
  else
    Validate_charset ("us-ascii", 0);
  return (TRUE);
}



/*
 * General format of an encoded header field (`[ ]' denotes optional fields):
 *
 * header_field: <text>=?<charset>[*<language>]?<encoding>?<encoded_chars>?=<text>
 *
 * `X-' charset prefix denotes non-standard charset for private use.
 *
 */

#define HUNT	0
#define START	1
#define CHARS	2
#define ENCD	3
#define Q_FIELD	4
#define DEC1	5
#define DEC2	6
#define B_FIELD	7
#define END	8


#define binhex(c) ( (c > '9') ? c-'A'+10 : c-'0')


/* max MIME charset parameter is 70 bytes */
unsigned char rendered_charset_name[80];

/*******************************************************************/
char *
decode_header_line (action, headers_buffer_p)
/*******************************************************************/
     enum action_type action;
     struct buffer_type *headers_buffer_p;
{
  int state = HUNT;
  int c, c1 = 0, c2, b1 = 0, b2, index, tmp;	/* =0 ??? */
  unsigned char *buf = headers_buffer_p->header_start;

  size_t charset_name_length = 0;
  unsigned char charset_name[80];
  unsigned char *charp = charset_name;
  const int max_encoding_length = 1;
  unsigned char encoding[max_encoding_length + 1];
  unsigned char *encp = encoding;
  unsigned char space_between_enc_words[80]="";

  rendered_charset_name[0] = '\0';
  while (buf < headers_buffer_p->next)
    {
      c = *buf++;
      if (Debug > 7)
	{
	  print_state (state);
	  fprintf (stderr, ", c=`%c\'\n", (char) c);
	}

      if (state == END)
	{
	  if (action == DECODE)
	    {
	      /* string may expand while converting */
	      Validate_charset (NULL, 0);	/* pop previous charset */
	    }
	}

      if (state == HUNT)
	{
	  if (c == ' ' || c == '\t' || c == '(')
	    {
	      state = START;
	      /* *retp++ = c; */
	      if (action == DECODE)
		PUTC_IN_UTF8 (c);
	      continue;
	    }
	}

      switch (c)
	{
	case '=':
	  {
	    switch (state)
	      {
	      case START:
		if ((tmp = *buf++) == '?')
		  {
		    state = CHARS;
		    charp = charset_name;
		  }
		else
		  {
		    if (action == DECODE)
		      {
			PUTC_IN_UTF8 (c);
			PUTC_IN_UTF8 (tmp);
		      }
		    state = HUNT;
		  }
		continue;

	      case CHARS:
		goto it_is_not_charset_name;

	      case Q_FIELD:
		state = DEC1;
		continue;

	      case DEC1:
		/* *retp++ = '='; */
		PUTC_IN_UTF8 ('=');
		continue;

	      case B_FIELD:
		/* padding */
		continue;

	      case END:
		if ((index=strspn (buf, LWSP)) \
		  && buf[index] == '=' && buf[index+1] == '?')
		{ /* space between adjacent encoded-words should be skipped */
		  memcpy (space_between_enc_words, buf, MIN (79, index));
		  space_between_enc_words[MIN (79, index)]='\0';
		  state = CHARS;
		  charp = charset_name;
		  buf += index + 2;
		}
		else
		{
		  space_between_enc_words[0]='\0';
		  state = HUNT;
		}
		continue;
	      }
	  }
	  break;

	case '?':
	  {
	    switch (state)
	      {
	      case START:
		/* *retp++ = c; */
		if (action == DECODE)
		  PUTC_IN_UTF8 (c);
		state = HUNT;
		continue;

	      case CHARS:
		/* we do not ignore the charset specification.
		 * discard X- prefix for non-standard charsets.
		 */
		*charp = '\0';
		charset_name_length = charp - charset_name;
		strcpy (rendered_charset_name, charset_name);
		if ((rendered_charset_name[0] == 'x' \
		     ||rendered_charset_name[0] == 'X') \
		    &&rendered_charset_name[1] == '-')
		  memmove (rendered_charset_name, \
			   rendered_charset_name + 2, 78);
		/* ignore language suffix after `*' */
		if (strchr (rendered_charset_name, '*'))
		  *strchr (rendered_charset_name, '*') = '\0';

		if (action == QUERY_CHARSET_NAME)
		  {
		    return (rendered_charset_name);
		  }
		if (Debug >= 4)
		  {
		    fprintf (stderr, "charset: %s\n", charset_name);
		    fprintf (stderr, "rendered charset: %s\n", \
			     rendered_charset_name);
		  }

		state = ENCD;
		continue;

	      case ENCD:
		*encp = '\0';
		encp = encoding;
		if (Debug > 7)
		  fprintf (stderr, "encoding: %s\n", encoding);
		if (!(Validate_charset (rendered_charset_name, 0) \
		      &&(*encoding == 'q' || *encoding == 'Q' \
			 ||*encoding == 'b' || *encoding == 'B')))
		  {
		    if (Debug > 3)
		      fprintf (stderr, \
			       "unknown charset `%s\' or encoding `%c\'\n", \
			       rendered_charset_name, encoding[0]);
		    Output_consumed_chars (space_between_enc_words, \
					   charset_name, charset_name_length, \
					   encoding);
		    PUTC_IN_UTF8 ('?');
		    Validate_charset (NULL, 0);		/* pop previous charset */
		    charp = charset_name;
		    encp = encoding;
		    state = HUNT;
		    continue;
		  }

		if ((*encoding == 'q') || (*encoding == 'Q'))
		  {
		    state = Q_FIELD;
		  }
		else if ((*encoding == 'b') || (*encoding == 'B'))
		  {
		    int c1, c2, c3, c4;

		    state = B_FIELD;

		    while ((buf < headers_buffer_p->next) && (*buf != '\?'))
		      {
			/* find first valid c1 */
			while ((buf < headers_buffer_p->next) \
			       &&((c1 = b64_map[(unsigned int) *buf]) == UN))
			  buf++;
			c1 = *buf;

			if (buf < headers_buffer_p->next)
			  buf++;

			/* find second valid c2 */
			while ((buf < headers_buffer_p->next) \
			       &&((c2 = b64_map[(unsigned int) *buf]) == UN))
			  buf++;
			c2 = *buf;

			if (buf < headers_buffer_p->next)
			  buf++;

			/* find third valid c3 */
			while ((buf < headers_buffer_p->next) \
			       &&((c3 = b64_map[(unsigned int) *buf]) == UN))
			  buf++;
			c3 = *buf;

			if (buf < headers_buffer_p->next)
			  buf++;

			/* find fourth valid c4 */
			while ((buf < headers_buffer_p->next) \
			       &&((c4 = b64_map[(unsigned int) *buf]) == UN))
			  buf++;
			c4 = *buf;

			if (buf < headers_buffer_p->next)
			  buf++;

			if ((c1 == '=') || (c2 == '=') || (c1 == '\0') || (c2 == '\0'))
			  {
			    break;
			  }

			/* *retp++ = ((unsigned int) b64_map[c1] << 2) | (((unsigned int) b64_map[c2] & 0x30) >> 4); */
			PUTC_IN_UTF8 (((unsigned int) b64_map[c1] << 2) \
			       |(((unsigned int) b64_map[c2] & 0x30) >> 4));

			if ((c3 == '=') || (c3 == '\0'))
			  {
			    break;
			  }

			/* *retp++ = (((unsigned int) b64_map[c2] & 0xF) << 4) | (((unsigned int) b64_map[c3] & 0x3C) >> 2); */
			PUTC_IN_UTF8 ((((unsigned int) b64_map[c2] & 0xF) << 4) \
			       |(((unsigned int) b64_map[c3] & 0x3C) >> 2));
			if ((c4 == '=') || (c4 == '\0'))
			  {
			    break;
			  }

			/* *retp++ = (((unsigned int) b64_map[c3] & 0x3) << 6) | ((unsigned int) b64_map[c4]); */
			PUTC_IN_UTF8 ((((unsigned int) b64_map[c3] & 0x3) << 6) \
				      |((unsigned int) b64_map[c4]));
		      }

		    /* *retp = '\0'; */
		  }
		continue;

	      case B_FIELD:
	      case Q_FIELD:
		state = END;
		continue;

	      case DEC1:
		/* output the consumed char */
		/* *retp++ = '='; */
		PUTC_IN_UTF8 ('=');
		state = END;
		continue;

	      case DEC2:
		/* output the consumed chars */
		/* *retp++ = '=';
		   *retp++ = c1; */
		PUTC_IN_UTF8 ('=');
		PUTC_IN_UTF8 (c1);
		state = END;
		continue;
	      }
	  }
	  break;

	default:
	  {
	    if (state == START)
	      {
		/* *retp++ = c; */
		if (action == DECODE)
		  PUTC_IN_UTF8 (c);
		if (!(c == ' ' || c == '\t' || c == '('))
		  state = HUNT;
		continue;
	      }

	    if (state == CHARS)
	      {
		if (charp - charset_name < 79)
		  *charp++ = c;
		else
		  {
		  it_is_not_charset_name:
		    if (action == DECODE)
		      {
			*charp = '\0';
			charset_name_length = charp - charset_name;
			Output_consumed_chars (space_between_enc_words, \
			    			charset_name, \
					     charset_name_length, NULL);
			PUTC_IN_UTF8 (c);
		      }
		    charp = charset_name;
		    state = HUNT;
		  }
		continue;
	      }

	    if (state == ENCD)
	      {
		if (encp - encoding < max_encoding_length)
		  *encp++ = c;
		else
		  {
		    if (action == DECODE)
		      {
			*encp = '\0';
			Output_consumed_chars (space_between_enc_words, \
					      charset_name, \
					     charset_name_length, encoding);
			PUTC_IN_UTF8 (c);
		      }
		    charp = charset_name;
		    encp = encoding;
		    state = HUNT;
		  }
		continue;
	      }

	    if ((state == Q_FIELD) && (c == '_'))
	      {
		/* *retp++ = ' '; */
		PUTC_IN_UTF8 (' ');
		continue;
	      }

	    if (state == DEC1)
	      {
		if (isxdigit (c))
		  {
		    c1 = c;
		    c = toupper (c1);
		    b1 = binhex (c);
		    state = DEC2;
		    continue;
		  }
		else
		  {
		    /* *retp++ = '='; */
		    PUTC_IN_UTF8 ('=');
		    state = Q_FIELD;
		  }
		break;
	      }

	    if (state == DEC2)
	      {
		if (isxdigit (c))
		  {
		    c2 = toupper (c);
		    b2 = binhex (c2);
		    c = b1 << 4 | b2;
		    /* *retp++ = c; */
		    PUTC_IN_UTF8 (c);
		    state = Q_FIELD;
		    continue;
		  }
		else
		  {
		    /* *retp++ = c1; */
		    PUTC_IN_UTF8 (c1);
		    state = Q_FIELD;
		  }
		break;
	      }

	    if (state == END)
	      {
		fprintf (stderr, "END reached");
		state = HUNT;
	      }
	  }
	  break;
	}
      /* *retp++ = c; */
      if (action == DECODE)
	PUTC_IN_UTF8 (c);
    }

  if (state == CHARS)
    {
      *charp = '\0';
      charset_name_length = charp - charset_name;
      Output_consumed_chars (space_between_enc_words, charset_name, \
			     charset_name_length, NULL);
    }
  else if (state == ENCD)
    {
      *encp = '\0';
      encp = encoding;

      if (action == DECODE)
	{
	  *encp = '\0';
	  Output_consumed_chars (space_between_enc_words, charset_name, \
				 charset_name_length, encoding);
	}
    }
  /* *retp++ = c; *//* remember the '\0' terminator */
  if (action == QUERY_CHARSET_NAME)
    return (NULL);
  return (rendered_charset_name);
}



/*******************************************************************/
void
print_state (state)
/*******************************************************************/
     int state;
{
  switch (state)
    {
    case HUNT:
      fprintf (stderr, " state HUNT");
      break;

    case START:
      fprintf (stderr, " state START");
      break;

    case CHARS:
      fprintf (stderr, " state CHARS");
      break;

    case ENCD:
      fprintf (stderr, " state ENCD");
      break;

    case Q_FIELD:
      fprintf (stderr, " state Q_FIELD");
      break;

    case B_FIELD:
      fprintf (stderr, " state B_FIELD");
      break;

    case DEC1:
      fprintf (stderr, " state DEC1");
      break;

    case DEC2:
      fprintf (stderr, " state DEC2");
      break;

    case END:
      fprintf (stderr, " state END");
      break;

    default:
      fprintf (stderr, " unknown state; %d", state);
      break;
    }
}



/******************************************************************* 
int
casncmp (s1, s2, n)
 ******************************************************************* 
     register const char *s1, *s2;
     register n;
{
  if (s1 == s2)
    return (0);
  while ((--n >= 0) && (lower (*s1) == lower (*s2)))
    {
      s2++;
      if (*s1++ == '\0')
	return (0);
    }
  return ((n < 0) ? 0 : (lower (*s1) - lower (*s2)));
}
*/


/*******************************************************************/
int
decode_quoted_printable (infp, outfp, boundary)
/*******************************************************************/
     FILE *infp;
     FILE *outfp;
     char *boundary;
{
  unsigned char *lbp = line.buf;
  int c, length, ret;

  if (Debug)
    fprintf (stderr, " --- Entry decode_quoted_print --- \n");
  if (boundary && (Debug >= 2))
    fprintf (stderr, " --- with boundary: %s\n", boundary);

  while ((length = Getline (&line.buf, &line.length, infp)) != -1)
/*  while ((c = getc (infp)) != EOF) */
    {
      lbp = line.buf;

      if (boundary && (line.buf[0] == '-') && (line.buf[1] == '-'))
	{
	  if ((ret = Str_is_boundary (line.buf+2, boundary, FALSE)))
	    {
	      if (ret < OUTER_BOUNDARY)
		fprintf (outfp, "%s", line.buf);
	      return (ret);
	    }
	}

      while (lbp - line.buf < length)
	{
	  if ((c = *lbp++) == '=')
	    {			/* c == '=' */
	      int c1 = *lbp++;
	      int c2;

	      if (!(lbp - line.buf < length))
		/*        if (c1 == '\0') */
		break;

	      c2 = *lbp++;
	      /*        if (c2 == '\0')
	         break; */


	      /* check for soft CRLF */
	      if (c1 == '\r')
		{
		  /* this is an error? : c2 = getc(infp); */
		  if (c2 != '\n')	/* not a CRLF */
		    lbp--;	/* put back the char after the =<CR> */
		  continue;
		}

	      /* check for soft newline */
	      if (c1 == '\n')
		{
		  lbp--;	/* put back the char after the newline */
		  continue;
		}

	      /* check for == -> = */
	      if (c1 == '=')
		{
		  putc (c1, outfp);
		  lbp--;	/* put back the char after the == */
		  continue;
		}

	      /* make sure it's =XX */
	      if (!isxdigit (c1) || !isxdigit (c2))
		continue;

	      /* we have two hex digits, so decode them */
	      if (isdigit (c1))
		c1 -= '0';
	      else if (islower (c1))
		{
		  c1 -= 'a';
		  c1 += 10;
		}
	      else
		{
		  c1 -= 'A';
		  c1 += 10;
		}
	      if (isdigit (c2))
		c2 -= '0';
	      else if (islower (c2))
		{
		  c2 -= 'a';
		  c2 += 10;
		}
	      else
		{
		  c2 -= 'A';
		  c2 += 10;
		}
	      PUTC_IN_UTF8 (((c1 << 4) | c2));
	    }
	  else
	    putc (c, outfp);
	}			/* end while */
      lbp = line.buf;
    }
  return (boundary ? FALSE : END_BOUNDARY);
}





/*******************************************************************/
int
nextgetc (infp)
/*******************************************************************/
     FILE *infp;
{
  int c;

  while ((c = getc (infp)) != EOF)
    {
      if (c == '-')
	{
	  c = getc (infp);
	  if (c == '-')
	    {
	      /* Two '-'s in a row - must be a boundary! */
	      if (Debug >= 2)
		fprintf (stderr, " ----- boundary line prefix found\n");
	      return (127);
	    }
	}

      if (b64_map[c] != UN)
	break;
    }
  return c;
}



/*******************************************************************/
int
decode_base64 (infp, outfp, boundary)
/*******************************************************************/
     FILE *infp;
     FILE *outfp;
     char *boundary;
{
  int c1, c2, c3, c4;
  int ret;
  ssize_t length;

  c1 = c2 = c3 = c4 = 0;

  if (Debug)
    fprintf (stderr, " --- Entry decode_base64 --- \n");

  for (;;)
    {
      ret = 0;

      if (((c1 = nextgetc (infp)) == 127)
	  || ((c2 = nextgetc (infp)) == 127)
	  || ((c3 = nextgetc (infp)) == 127)
	  || ((c4 = nextgetc (infp)) == 127))
	{
	  if (Debug >= 2)
	    fprintf (stderr, " --- boundary line prefix found: %d\n", ret);
	  putc ('-', outfp);
	  putc ('-', outfp);
	  if (!boundary || -1 == \
	      (length = Getline (&line.buf, &line.length, infp)))
	    return (FALSE);
	  if ((ret = Str_is_boundary (line.buf, boundary, FALSE)))
	    {
	      if (ret < OUTER_BOUNDARY)
		fprintf (outfp, "%s", line.buf);
	      return (ret);
	    }
	  return (Seek_boundary (infp, boundary, FALSE));
	}

      if ((c1 == '=') || (c2 == '=') || (c1 == EOF) || (c2 == EOF))
	{
	  break;
	}

      PUTC_IN_UTF8 ((((unsigned int) b64_map[c1] << 2) | (((unsigned int) b64_map[c2] & 0x30) >> 4)));
      if ((c3 == '=') || (c3 == EOF))
	{
	  break;
	}

      PUTC_IN_UTF8 (((((unsigned int) b64_map[c2] & 0XF) << 4) | (((unsigned int) b64_map[c3] & 0x3C) >> 2)));

      if ((c4 == '=') || (c4 == EOF))
	{
	  break;
	}

      PUTC_IN_UTF8 (((((unsigned int) b64_map[c3] & 0x03) << 6) | (unsigned int) b64_map[c4]));
    }
  if (Debug >= 2)
    fprintf (stderr, " --- end of base64 \n");
  if (boundary)
    return (Seek_boundary (infp, boundary, 0));
  else
    while ((getc (infp)) != EOF);
/*      putc (c, outfp); */
  return (END_BOUNDARY);
}



/*******************************************************************/
void
write_cte (headers_buffer_p, transfer_encoding, content_type)
/*******************************************************************/
     struct buffer_type *headers_buffer_p;
     int transfer_encoding, content_type;
{
  if (content_type == TEXT)
    {
      switch (transfer_encoding)
	{
	case QP:
	  Add_to_buffer (headers_buffer_p, "X-2UTF-Content-transfer-encoding: fixed from quoted-printable\n");
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: 8bit\n");
	  break;

	case B64:
	  Add_to_buffer (headers_buffer_p, "X-2UTF-Content-transfer-encoding: fixed from base64\n");
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: 8bit\n");
	  break;

	case BIT7:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: 7bit\n");
	  break;

	case BIT8:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: 8bit\n");
	  break;

	case BINARY:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: binary\n");
	  break;

	default:
	  if (Debug >= 3)
	    fprintf (stderr, "Transfer-encoding is UNKNOWN\n");
	  break;
	}
    }
  else
    /* ! TEXT */
    {
      /* remember to output the Content-transfer-encoding
       * header field in case type is NOT text.
       */
      switch (transfer_encoding)
	{
	case QP:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: quoted-printable\n");
	  break;

	case B64:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: base64\n");
	  break;

	case BIT7:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: 7bit\n");
	  break;

	case BIT8:
	  Add_to_buffer (headers_buffer_p, "Content-Transfer-Encoding: 8bit\n");
	  break;
	}
    }
  headers_buffer_p->header_start = headers_buffer_p->next;
}