summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/imap4d/mutf7.c
blob: 6d84b4efc11a75cfd4f04dbac47bcf76446ec8ea (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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <auth.h>
#include "imap4d.h"

/*
 * modified utf-7, as per imap4 spec
 * like utf-7, but substitues , for / in base 64,
 * does not allow escaped ascii characters.
 *
 * /lib/rfc/rfc2152 is utf-7
 * /lib/rfc/rfc1642 is obsolete utf-7
 *
 * test sequences from rfc1642
 *	'A≢Α.'		'A&ImIDkQ-.'
 *	'Hi Mom ☺!"	'Hi Mom &Jjo-!'
 *	'日本語'		'&ZeVnLIqe-'
 */

static uchar mt64d[256];
static char mt64e[64];

static void
initm64(void)
{
	int c, i;

	memset(mt64d, 255, 256);
	memset(mt64e, '=', 64);
	i = 0;
	for(c = 'A'; c <= 'Z'; c++){
		mt64e[i] = c;
		mt64d[c] = i++;
	}
	for(c = 'a'; c <= 'z'; c++){
		mt64e[i] = c;
		mt64d[c] = i++;
	}
	for(c = '0'; c <= '9'; c++){
		mt64e[i] = c;
		mt64d[c] = i++;
	}
	mt64e[i] = '+';
	mt64d['+'] = i++;
	mt64e[i] = ',';
	mt64d[','] = i;
}

int
encmutf7(char *out, int lim, char *in)
{
	Rune rr;
	ulong r, b;
	char *start = out;
	char *e = out + lim;
	int nb;

	if(mt64e[0] == 0)
		initm64();
	for(;;){
		r = *(uchar*)in;

		if(r < ' ' || r >= Runeself){
			if(r == '\0')
				break;
			if(out + 1 >= e)
				return -1;
			*out++ = '&';
			b = 0;
			nb = 0;
			for(;;){
				in += chartorune(&rr, in);
				r = rr;
				if(r == '\0' || r >= ' ' && r < Runeself)
					break;
				b = (b << 16) | r;
				for(nb += 16; nb >= 6; nb -= 6){
					if(out + 1 >= e)
						return -1;
					*out++ = mt64e[(b>>(nb-6))&0x3f];
				}
			}
			for(; nb >= 6; nb -= 6){
				if(out + 1 >= e)
					return -1;
				*out++ = mt64e[(b>>(nb-6))&0x3f];
			}
			if(nb){
				if(out + 1 >= e)
					return -1;
				*out++ = mt64e[(b<<(6-nb))&0x3f];
			}

			if(out + 1 >= e)
				return -1;
			*out++ = '-';
			if(r == '\0')
				break;
		}else
			in++;
		if(out + 1 >= e)
			return -1;
		*out = r;
		out++;
		if(r == '&')
			*out++ = '-';
	}

	if(out >= e)
		return -1;
	*out = '\0';
	return out - start;
}

int
decmutf7(char *out, int lim, char *in)
{
	Rune rr;
	char *start = out;
	char *e = out + lim;
	int c, b, nb;

	if(mt64e[0] == 0)
		initm64();
	for(;;){
		c = *in;

		if(c < ' ' || c >= Runeself){
			if(c == '\0')
				break;
			return -1;
		}
		if(c != '&'){
			if(out + 1 >= e)
				return -1;
			*out++ = c;
			in++;
			continue;
		}
		in++;
		if(*in == '-'){
			if(out + 1 >= e)
				return -1;
			*out++ = '&';
			in++;
			continue;
		}

		b = 0;
		nb = 0;
		while((c = *in++) != '-'){
			c = mt64d[c];
			if(c >= 64)
				return -1;
			b = (b << 6) | c;
			nb += 6;
			if(nb >= 16){
				rr = b >> (nb - 16);
				nb -= 16;
				if(out + UTFmax + 1 >= e && out + runelen(rr) + 1 >= e)
					return -1;
				out += runetochar(out, &rr);
			}
		}
		if(b & ((1 << nb) - 1))
			return -1;
	}

	if(out >= e)
		return -1;
	*out = '\0';
	return out - start;
}