-
Notifications
You must be signed in to change notification settings - Fork 1
/
mime.c
1736 lines (1617 loc) · 37.6 KB
/
mime.c
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
/* mime.c
* vi: set sw=4 ts=8 ai sm noet:
*/
#include "EXTERN.h"
#include "common.h"
#include "list.h"
#include "hash.h"
#include "cache.h"
#include "head.h"
#include "search.h"
#include "art.h"
#include "artio.h"
#include "artstate.h"
#include "ng.h"
#include "term.h"
#include "decode.h"
#include "respond.h"
#include "env.h"
#include "color.h"
#include "util.h"
#include "util2.h"
#include "utf.h"
#include "backpage.h"
#include "charsubst.h"
#include "INTERN.h"
#include "mime.h"
#include "mime.ih"
static char text_plain[] = "text/plain";
#ifdef USE_UTF_HACK
#define CODE_POINT_MAX 0x7FFFFFFFL
#else
#define CODE_POINT_MAX 0x7F
#endif
void
mime_init()
{
char* s;
char* t;
char* mcname;
mimecap_list = new_list(0,-1,sizeof(MIMECAP_ENTRY),40,LF_ZERO_MEM,NULL);
if ((mcname = getenv("MIMECAPS")) == NULL)
mcname = getval("MAILCAPS", MIMECAP);
mcname = s = savestr(mcname);
do {
if ((t = index(s, ':')) != NULL)
*t++ = '\0';
if (*s)
mime_ReadMimecap(s);
s = t;
} while (s && *s);
free(mcname);
}
void
mime_ReadMimecap(mcname)
char* mcname;
{
FILE* fp;
char* bp;
char* s;
char* t;
char* arg;
int buflen = 2048;
int linelen;
MIMECAP_ENTRY* mcp;
int i;
if ((fp = fopen(filexp(mcname), "r")) == NULL)
return;
bp = safemalloc(buflen);
for (i = mimecap_list->high; !feof(fp); ) {
*(s = bp) = '\0';
linelen = 0;
while (fgets(s, buflen - linelen, fp)) {
if (*s == '#')
continue;
linelen += strlen(s);
if (linelen == 0)
continue;
if (bp[linelen-1] == '\n') {
if (--linelen == 0)
continue;
if (bp[linelen-1] != '\\') {
bp[linelen] = '\0';
break;
}
bp[--linelen] = '\0';
}
if (linelen+1024 > buflen) {
buflen *= 2;
bp = saferealloc(bp, buflen);
}
s = bp + linelen;
}
for (s = bp; isspace(*s); s++) ;
if (!*s)
continue;
t = mime_ParseEntryArg(&s);
if (!s) {
fprintf(stderr, "trn: Ignoring invalid mimecap entry: %s\n", bp);
continue;
}
mcp = mimecap_ptr(++i);
mcp->contenttype = savestr(t);
mcp->command = savestr(mime_ParseEntryArg(&s));
while (s) {
t = mime_ParseEntryArg(&s);
if ((arg = index(t, '=')) != NULL) {
char* f = arg+1;
while (arg != t && isspace(arg[-1])) arg--;
*arg++ = '\0';
while (isspace(*f)) f++;
if (*f == '"')
f = cpytill(arg,f+1,'"');
else
arg = f;
}
if (*t) {
if (strcaseEQ(t, "needsterminal"))
mcp->flags |= MCF_NEEDSTERMINAL;
else if (strcaseEQ(t, "copiousoutput"))
mcp->flags |= MCF_COPIOUSOUTPUT;
else if (arg && strcaseEQ(t, "test"))
mcp->testcommand = savestr(arg);
else if (arg && strcaseEQ(t, "description"))
mcp->label = savestr(arg);
else if (arg && strcaseEQ(t, "label"))
mcp->label = savestr(arg); /* bogus old name for description */
}
}
}
mimecap_list->high = i;
free(bp);
fclose(fp);
}
static char*
mime_ParseEntryArg(cpp)
char** cpp;
{
char* s = *cpp;
char* f;
char* t;
while (isspace(*s)) s++;
for (f = t = s; *f && *f != ';'; ) {
if (*f == '\\') {
if (*++f == '%')
*t++ = '%';
else if (!*f)
break;
}
*t++ = *f++;
}
while (isspace(*f) || *f == ';') f++;
if (!*f)
f = NULL;
while (t != s && isspace(t[-1])) t--;
*t = '\0';
*cpp = f;
return s;
}
MIMECAP_ENTRY*
mime_FindMimecapEntry(contenttype, skip_flags)
char* contenttype;
int skip_flags;
{
MIMECAP_ENTRY* mcp;
int i;
for (i = 0; i <= mimecap_list->high; i++) {
mcp = mimecap_ptr(i);
if (!(mcp->flags & skip_flags)
&& mime_TypesMatch(contenttype, mcp->contenttype)) {
if (!mcp->testcommand)
return mcp;
if (mime_Exec(mcp->testcommand) == 0)
return mcp;
}
}
return NULL;
}
bool
mime_TypesMatch(ct,pat)
char* ct;
char* pat;
{
char* s = index(pat,'/');
int len = (s? s - pat : strlen(pat));
bool iswild = (!s || strEQ(s+1,"*"));
return strcaseEQ(ct,pat)
|| (iswild && strncaseEQ(ct,pat,len) && ct[len] == '/');
}
int
mime_Exec(cmd)
char* cmd;
{
char* f;
char* t;
for (f = cmd, t = cmd_buf; *f && t-cmd_buf < CBUFLEN-2; f++) {
if (*f == '%') {
switch (*++f) {
case 's':
safecpy(t, decode_filename, CBUFLEN-(t-cmd_buf));
t += strlen(t);
break;
case 't':
*t++ = '\'';
safecpy(t, mime_section->type_name, CBUFLEN-(t-cmd_buf)-1);
t += strlen(t);
*t++ = '\'';
break;
case '{': {
char* s = index(f, '}');
char* p;
if (!s)
return -1;
f++;
*s = '\0';
p = mime_FindParam(mime_section->type_params, f);
*s = '}'; /* restore */
f = s;
*t++ = '\'';
safecpy(t, p, CBUFLEN-(t-cmd_buf)-1);
t += strlen(t);
*t++ = '\'';
break;
}
case '%':
*t++ = '%';
break;
case 'n':
case 'F':
return -1;
}
}
else
*t++ = *f;
}
*t = '\0';
return doshell(sh, cmd_buf);
}
void
mime_InitSections()
{
while (mime_PopSection()) ;
mime_ClearStruct(mime_section);
mime_state = NOT_MIME;
}
void
mime_PushSection()
{
MIME_SECT* mp = (MIME_SECT*)safemalloc(sizeof (MIME_SECT));
bzero((char*)mp, sizeof (MIME_SECT));
mp->prev = mime_section;
mime_section = mp;
}
bool
mime_PopSection()
{
MIME_SECT* mp = mime_section->prev;
if (mp) {
mime_ClearStruct(mime_section);
free((char*)mime_section);
mime_section = mp;
mime_state = mp->type;
return TRUE;
}
mime_state = mime_article.type;
return FALSE;
}
/* Free up this mime structure's resources */
void
mime_ClearStruct(mp)
MIME_SECT* mp;
{
safefree0(mp->filename);
safefree0(mp->type_name);
safefree0(mp->type_params);
safefree0(mp->boundary);
safefree0(mp->html_blks);
mp->type = NOT_MIME;
mp->encoding = MENCODE_NONE;
mp->part = mp->total = mp->boundary_len = mp->flags = mp->html
= mp->html_blkcnt = 0;
mp->html_line_start = 0;
}
/* Setup mime_article structure based on article's headers */
void
mime_SetArticle()
{
char* s;
mime_InitSections();
/*$$ Check mime version #? */
multimedia_mime = FALSE;
is_mime = (htype[MIMEVER_LINE].flags & HT_MAGIC)
&& htype[MIMEVER_LINE].minpos >= 0;
s = fetchlines(art,CONTTYPE_LINE);
mime_ParseType(mime_section,s);
free(s);
if (is_mime) {
s = fetchlines(art,CONTXFER_LINE);
mime_ParseEncoding(mime_section,s);
free(s);
s = fetchlines(art,CONTDISP_LINE);
mime_ParseDisposition(mime_section,s);
free(s);
mime_state = mime_section->type;
if (mime_state == NOT_MIME
|| (mime_state == TEXT_MIME && mime_section->encoding == MENCODE_NONE))
is_mime = FALSE;
else if (!mime_section->type_name)
mime_section->type_name = savestr(text_plain);
}
}
/* Use the Content-Type to set values in the mime structure */
void
mime_ParseType(mp, s)
MIME_SECT* mp;
char* s;
{
char* t;
safefree0(mp->type_name);
safefree0(mp->type_params);
mp->type_params = mime_ParseParams(s);
if (!*s) {
mp->type = NOT_MIME;
return;
}
mp->type_name = savestr(s);
t = mime_FindParam(mp->type_params,"name");
if (t) {
safefree(mp->filename);
mp->filename = savestr(t);
}
if (strncaseEQ(s, "text", 4)) {
mp->type = TEXT_MIME;
s += 4;
if (*s++ != '/')
return;
#ifdef USE_UTF_HACK
utf_init(mime_FindParam(mp->type_params,"charset"), "utf-8"); /*FIXME*/
#endif
if (strncaseEQ(s, "html", 4))
mp->type = HTMLTEXT_MIME;
else if (strncaseEQ(s, "x-vcard", 7))
mp->type = UNHANDLED_MIME;
return;
}
if (strncaseEQ(s, "message/", 8)) {
s += 8;
mp->type = MESSAGE_MIME;
if (strcaseEQ(s, "partial")) {
t = mime_FindParam(mp->type_params,"id");
if (!t)
return;
safefree(mp->filename);
mp->filename = savestr(t);
t = mime_FindParam(mp->type_params,"number");
if (t)
mp->part = (short)atoi(t);
t = mime_FindParam(mp->type_params,"total");
if (t)
mp->total = (short)atoi(t);
if (!mp->total) {
mp->part = 0;
return;
}
return;
}
return;
}
if (strncaseEQ(s, "multipart/", 10)) {
s += 10;
t = mime_FindParam(mp->type_params,"boundary");
if (!t) {
mp->type = UNHANDLED_MIME;
return;
}
if (strncaseEQ(s, "alternative", 11))
mp->flags |= MSF_ALTERNATIVE;
safefree(mp->boundary);
mp->boundary = savestr(t);
mp->boundary_len = (short)strlen(t);
mp->type = MULTIPART_MIME;
return;
}
if (strncaseEQ(s, "image/", 6)) {
mp->type = IMAGE_MIME;
return;
}
if (strncaseEQ(s, "audio/", 6)) {
mp->type = AUDIO_MIME;
return;
}
mp->type = UNHANDLED_MIME;
}
/* Use the Content-Disposition to set values in the mime structure */
void
mime_ParseDisposition(mp, s)
MIME_SECT* mp;
char* s;
{
char* params;
params = mime_ParseParams(s);
if (strcaseEQ(s,"inline"))
mp->flags |= MSF_INLINE;
s = mime_FindParam(params,"filename");
if (s) {
safefree(mp->filename);
mp->filename = savestr(s);
}
safefree(params);
}
/* Use the Content-Transfer-Encoding to set values in the mime structure */
void
mime_ParseEncoding(mp, s)
MIME_SECT* mp;
char* s;
{
s = mime_SkipWhitespace(s);
if (!*s) {
mp->encoding = MENCODE_NONE;
return;
}
if (*s == '7' || *s == '8') {
if (strncaseEQ(s+1, "bit", 3)) {
s += 4;
mp->encoding = MENCODE_NONE;
}
}
else if (strncaseEQ(s, "quoted-printable", 16)) {
s += 16;
mp->encoding = MENCODE_QPRINT;
}
else if (strncaseEQ(s, "binary", 6)) {
s += 6;
mp->encoding = MENCODE_NONE;
}
else if (strncaseEQ(s, "base64", 6)) {
s += 6;
mp->encoding = MENCODE_BASE64;
}
else if (strncaseEQ(s, "x-uue", 5)) {
s += 5;
mp->encoding = MENCODE_UUE;
if (strncaseEQ(s, "ncode", 5))
s += 5;
}
else {
mp->encoding = MENCODE_UNHANDLED;
return;
}
if (*s != '\0' && !isspace(*s) && *s != ';' && *s != '(')
mp->encoding = MENCODE_UNHANDLED;
}
/* Parse a multipart mime header and affect the *mime_section structure */
void
mime_ParseSubheader(ifp, next_line)
FILE* ifp;
char* next_line;
{
static char* line = NULL;
static int line_size = 0;
char* s;
int pos, linetype, len;
mime_ClearStruct(mime_section);
mime_section->type = TEXT_MIME;
for (;;) {
for (pos = 0; ; pos += strlen(line+pos)) {
len = pos + (next_line? strlen(next_line) : 0) + LBUFLEN;
if (line_size < len) {
line_size = len + LBUFLEN;
line = saferealloc(line, line_size);
}
if (next_line) {
safecpy(line+pos, next_line, line_size - pos);
next_line = NULL;
}
else if (ifp) {
if (!fgets(line + pos, LBUFLEN, ifp))
break;
}
else if (!readart(line + pos, LBUFLEN))
break;
if (line[0] == '\n')
break;
if (pos && line[pos] != ' ' && line[pos] != '\t') {
next_line = line + pos;
line[pos-1] = '\0';
break;
}
}
s = index(line,':');
if (s == NULL)
break;
linetype = set_line_type(line,s);
switch (linetype) {
case CONTTYPE_LINE:
mime_ParseType(mime_section,s+1);
break;
case CONTXFER_LINE:
mime_ParseEncoding(mime_section,s+1);
break;
case CONTDISP_LINE:
mime_ParseDisposition(mime_section,s+1);
break;
case CONTNAME_LINE:
safefree(mime_section->filename);
s = mime_SkipWhitespace(s+1);
mime_section->filename = savestr(s);
break;
#if 0
case CONTLEN_LINE:
mime_section->content_len = atol(s+1);
break;
#endif
}
}
mime_state = mime_section->type;
if (!mime_section->type_name)
mime_section->type_name = savestr(text_plain);
}
void
mime_SetState(bp)
char* bp;
{
int ret;
if (mime_state == BETWEEN_MIME) {
mime_ParseSubheader((FILE*)NULL,bp);
*bp = '\0';
if (mime_section->prev->flags & MSF_ALTERNADONE)
mime_state = ALTERNATE_MIME;
else if (mime_section->prev->flags & MSF_ALTERNATIVE)
mime_section->prev->flags |= MSF_ALTERNADONE;
}
while (mime_state == MESSAGE_MIME) {
mime_PushSection();
mime_ParseSubheader((FILE*)NULL,*bp? bp : (char*)NULL);
*bp = '\0';
}
if (mime_state == MULTIPART_MIME) {
mime_PushSection();
mime_state = SKIP_MIME; /* Skip anything before 1st part */
}
ret = mime_EndOfSection(bp);
switch (ret) {
case 0:
break;
case 1:
while (!mime_section->prev->boundary_len)
mime_PopSection();
mime_state = BETWEEN_MIME;
break;
case 2:
while (!mime_section->prev->boundary_len)
mime_PopSection();
mime_PopSection();
mime_state = END_OF_MIME;
break;
}
}
int
mime_EndOfSection(bp)
char* bp;
{
MIME_SECT* mp = mime_section->prev;
while (mp && !mp->boundary_len)
mp = mp->prev;
if (mp) {
/* have we read all the data in this part? */
if (bp[0] == '-' && bp[1] == '-'
&& strnEQ(bp+2,mp->boundary,mp->boundary_len)) {
int len = 2 + mp->boundary_len;
/* have we found the last boundary? */
if (bp[len] == '-' && bp[len+1] == '-'
&& (bp[len+2] == '\n' || bp[len+2] == '\0'))
return 2;
return bp[len] == '\n' || bp[len] == '\0';
}
}
return 0;
}
/* Return a saved string of all the extra parameters on this mime
* header line. The passed-in string is transformed into just the
* first word on the line.
*/
char*
mime_ParseParams(str)
char* str;
{
char* s;
char* t;
char* e;
s = e = mime_SkipWhitespace(str);
while (*e && *e != ';' && !isspace(*e) && *e != '(') e++;
t = savestr(mime_SkipWhitespace(e));
*e = '\0';
if (s != str)
safecpy(str, s, e - s + 1);
str = s = t;
while (*s == ';') {
s = mime_SkipWhitespace(s+1);
while (*s && *s != ';' && *s != '(' && *s != '=' && !isspace(*s))
*t++ = *s++;
s = mime_SkipWhitespace(s);
if (*s == '=') {
*t++ = *s;
s = mime_SkipWhitespace(s+1);
if (*s == '"') {
s = cpytill(t,s+1,'"');
if (*s == '"')
s++;
t += strlen(t);
}
else
while (*s && *s != ';' && !isspace(*s) && *s != '(')
*t++ = *s++;
}
*t++ = '\0';
}
*t = '\0';
return str;
}
char*
mime_FindParam(s, param)
char* s;
char* param;
{
int param_len = strlen(param);
while (s && *s) {
if (strncaseEQ(s, param, param_len) && s[param_len] == '=')
return s + param_len + 1;
s += strlen(s) + 1;
}
return NULL;
}
/* Skip whitespace and RFC-822 comments. */
char*
mime_SkipWhitespace(s)
char* s;
{
int comment_level = 0;
while (*s) {
if (*s == '(') {
s++;
comment_level++;
while (comment_level) {
switch (*s++) {
case '\0':
return s-1;
case '\\':
s++;
break;
case '(':
comment_level++;
break;
case ')':
comment_level--;
break;
}
}
}
else if (!isspace(*s))
break;
else
s++;
}
return s;
}
void
mime_DecodeArticle(view)
bool_int view;
{
MIMECAP_ENTRY* mcp = NULL;
seekart(savefrom);
*art_line = '\0';
while (1) {
if (mime_state != MESSAGE_MIME || !mime_section->total) {
if (!readart(art_line,sizeof art_line))
break;
mime_SetState(art_line);
}
switch (mime_state) {
case BETWEEN_MIME:
case END_OF_MIME:
break;
case TEXT_MIME:
case HTMLTEXT_MIME:
case ISOTEXT_MIME:
case MESSAGE_MIME:
/* $$ Check for uuencoded file here? */
mime_state = SKIP_MIME;
/* FALL THROUGH */
case SKIP_MIME: {
MIME_SECT* mp = mime_section;
while ((mp = mp->prev) != NULL && !mp->boundary_len) ;
if (!mp)
return;
break;
}
default:
if (view) {
mcp = mime_FindMimecapEntry(mime_section->type_name,0);
if (!mcp) {
printf("No view method for %s -- skipping.\n",
mime_section->type_name);
mime_state = SKIP_MIME;
break;
}
}
mime_state = DECODE_MIME;
if (decode_piece(mcp, *art_line == '\n'? NULL : art_line) != 0) {
mime_SetState(art_line);
if (mime_state == DECODE_MIME)
mime_state = SKIP_MIME;
}
else {
if (*msg) {
newline();
fputs(msg,stdout);
}
mime_state = SKIP_MIME;
}
newline();
break;
}
}
}
void
mime_Description(mp, s, limit)
MIME_SECT* mp;
char* s;
int limit;
{
char* fn = decode_fix_fname(mp->filename);
int len, flen = strlen(fn);
limit -= 2; /* leave room for the trailing ']' and '\n' */
sprintf(s, "[Attachment type=%s, name=", mp->type_name);
len = strlen(s);
if (len + flen <= limit)
sprintf(s+len, "%s]\n", fn);
else if (len+3 >= limit)
strcpy(s+limit-3, "...]\n");
else {
#if 0
sprintf(s+len, "...%s]\n", fn + flen - (limit-(len+3)));
#else
safecpy(s+len, fn, limit - (len+3));
strcat(s, "...]\n");
#endif
}
}
#define XX 255
static Uchar index_hex[256] = {
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,XX,XX, XX,XX,XX,XX,
XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,10,11,12, 13,14,15,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
};
int
qp_decodestring(t, f, in_header)
char* t;
char* f;
bool_int in_header;
{
char* save_t = t;
while (*f) {
switch (*f) {
case '_':
if (in_header) {
*t++ = ' ';
f++;
}
else
*t++ = *f++;
break;
case '=': /* decode a hex-value */
if (f[1] == '\n') {
f += 2;
break;
}
if (index_hex[(Uchar)f[1]] != XX && index_hex[(Uchar)f[2]] != XX) {
*t = (index_hex[(Uchar)f[1]] << 4) + index_hex[(Uchar)f[2]];
f += 3;
if (*t != '\r')
t++;
break;
}
/* FALL THROUGH */
default:
*t++ = *f++;
break;
}
}
*t = '\0';
return t - save_t;
}
int
qp_decode(ifp,state)
FILE* ifp;
int state;
{
static FILE* ofp = NULL;
int c1, c2;
if (state == DECODE_DONE) {
if (ofp)
fclose(ofp);
ofp = NULL;
return state;
}
if (state == DECODE_START) {
char* filename = decode_fix_fname(mime_section->filename);
ofp = fopen(filename, FOPEN_WB);
if (!ofp)
return DECODE_ERROR;
erase_line(0);
printf("Decoding %s", filename);
if (nowait_fork)
fflush(stdout);
else
newline();
}
while ((c1 = mime_getc(ifp)) != EOF) {
check_c1:
if (c1 == '=') {
c1 = mime_getc(ifp);
if (c1 == '\n')
continue;
if (index_hex[(Uchar)c1] == XX) {
putc('=', ofp);
goto check_c1;
}
c2 = mime_getc(ifp);
if (index_hex[(Uchar)c2] == XX) {
putc('=', ofp);
putc(c1, ofp);
c1 = c2;
goto check_c1;
}
c1 = (index_hex[(Uchar)c1] << 4) | index_hex[(Uchar)c2];
if (c1 != '\r')
putc(c1, ofp);
}
else
putc(c1, ofp);
}
return DECODE_MAYBEDONE;
}
static Uchar index_b64[256] = {
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,62, XX,XX,XX,63,
52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX,
XX, 0, 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,XX, XX,XX,XX,XX,
XX,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,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
};
int
b64_decodestring(t, f)
char* t;
char* f;
{
char* save_t = t;
Uchar ch1, ch2;
while (*f && *f != '=') {
ch1 = index_b64[(Uchar)*f++];
if (ch1 == XX)
continue;
do {
if (!*f || *f == '=')
goto dbl_break;
ch2 = index_b64[(Uchar)*f++];
} while (ch2 == XX);
*t++ = (ch1 << 2) | (ch2 >> 4);
do {
if (!*f || *f == '=')
goto dbl_break;
ch1 = index_b64[(Uchar)*f++];
} while (ch1 == XX);
*t++ = ((ch2 & 0x0f) << 4) | (ch1 >> 2);
do {
if (!*f || *f == '=')
goto dbl_break;
ch2 = index_b64[(Uchar)*f++];
} while (ch2 == XX);
*t++ = ((ch1 & 0x03) << 6) | ch2;
}
dbl_break:
*t = '\0';
return t - save_t;
}
int
b64_decode(ifp, state)
FILE* ifp;
int state;
{
static FILE* ofp = NULL;
int c1, c2, c3, c4;
if (state == DECODE_DONE) {
all_done:
if (ofp)
fclose(ofp);
ofp = NULL;
return state;
}
if (state == DECODE_START) {
char* filename = decode_fix_fname(mime_section->filename);
ofp = fopen(filename, FOPEN_WB);