aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/upas/vf/unvf.c
blob: aecbca62de064d7fb9a50be9eb1a013ce12aec8c (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
/*
 * For decoding the files that get passed to validateattachment.
 * NOT a general mime decoder.
 */
#include <u.h>
#include <libc.h>
#include <bio.h>

enum { None, Base64, Quoted };
static int decquoted(char *out, char *in, char *e);

void
main(void)
{
	Biobuf b, b1;
	char *p, *encoding;
	int e, len;
	
	Binit(&b, 0, OREAD);
	Binit(&b1, 1, OWRITE);
	
	/* header */
	encoding = nil;
	while((p = Brdstr(&b, '\n', 1)) != nil){
		if(p[0] == 0)
			break;
		if(cistrncmp(p, "Content-Transfer-Encoding: ", 27) == 0)
			encoding = strdup(p+27);
		free(p);
	}

	e = None;
	if(encoding == nil)
		e = None;
	else if(strcmp(encoding, "base64") == 0)
		e = Base64;
	else if(strcmp(encoding, "quoted-printable") == 0)
		e = Quoted;

	while((p = Brdstr(&b, '\n', 0)) != nil){
		if(strncmp(p, "--", 2) == 0 && e != None)
			break;
		len = strlen(p);
		switch(e){
		case None:
			break;
		case Base64:
			len = dec64((uchar*)p, len, p, len);
			break;
		case Quoted:
			len = decquoted(p, p, p+len);
			break;
		}
		Bwrite(&b1, p, len);
		free(p);
	}
	exits(0);
}

/*
 *  decode quoted 
 */
enum
{
	Self=	1,
	Hex=	2
};
uchar	tableqp[256];

static void
initquoted(void)
{
	int c;

	memset(tableqp, 0, 256);
	for(c = ' '; c <= '<'; c++)
		tableqp[c] = Self;
	for(c = '>'; c <= '~'; c++)
		tableqp[c] = Self;
	tableqp['\t'] = Self;
	tableqp['='] = Hex;
}

static int
hex2int(int x)
{
	if(x >= '0' && x <= '9')
		return x - '0';
	if(x >= 'A' && x <= 'F')
		return (x - 'A') + 10;
	if(x >= 'a' && x <= 'f')
		return (x - 'a') + 10;
	return 0;
}

static char*
decquotedline(char *out, char *in, char *e)
{
	int c, soft;

	/* dump trailing white space */
	while(e >= in && (*e == ' ' || *e == '\t' || *e == '\r' || *e == '\n'))
		e--;

	/* trailing '=' means no newline */
	if(*e == '='){
		soft = 1;
		e--;
	} else
		soft = 0;

	while(in <= e){
		c = (*in++) & 0xff;
		switch(tableqp[c]){
		case Self:
			*out++ = c;
			break;
		case Hex:
			c = hex2int(*in++)<<4;
			c |= hex2int(*in++);
			*out++ = c;
			break;
		}
	}
	if(!soft)
		*out++ = '\n';
	*out = 0;

	return out;
}

static int
decquoted(char *out, char *in, char *e)
{
	char *p, *nl;

	if(tableqp[' '] == 0)
		initquoted();

	p = out;
	while((nl = strchr(in, '\n')) != nil && nl < e){
		p = decquotedline(p, in, nl);
		in = nl + 1;
	}
	if(in < e)
		p = decquotedline(p, in, e-1);

	/* make sure we end with a new line */
	if(*(p-1) != '\n'){
		*p++ = '\n';
		*p = 0;
	}

	return p - out;
}