~ubuntu-dev/ubuntu/lucid/dovecot/lucid-201002101901

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
/* Copyright (c) 2009 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "str.h"
#include "base64.h"
#include "message-header-encode.h"

#define MIME_WRAPPER_LEN (strlen("=?utf-8?q?""?="))
#define MIME_MAX_LINE_LEN 76

#define IS_LWSP(c) \
	((c) == ' ' || (c) == '\t' || (c) == '\n')

static bool input_idx_need_encoding(const unsigned char *input, unsigned int i)
{
	if ((input[i] & 0x80) != 0)
		return TRUE;

	if (input[i] == '=' && input[i+1] == '?' &&
	    (i == 0 || IS_LWSP(input[i-1])))
		return TRUE;
	return FALSE;
}

static unsigned int str_last_line_len(string_t *str)
{
	const unsigned char *data = str_data(str);
	unsigned int i = str_len(str);

	while (i > 0 && data[i-1] != '\n')
		i--;
	return str_len(str) - i;
}

void message_header_encode_q(const unsigned char *input, unsigned int len,
			     string_t *output)
{
	unsigned int i, line_len, line_len_left;

	line_len = str_last_line_len(output);
	if (line_len >= MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN - 3) {
		str_append(output, "\n\t");
		line_len = 1;
	}

	str_append(output, "=?utf-8?q?");
	line_len_left = MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN - line_len;
	for (i = 0; i < len; i++) {
		if (line_len_left < 3) {
			/* if we're not at the beginning of a character,
			   go backwards until we are */
			while ((input[i] & 0xc0) == 0x80) {
				str_truncate(output, str_len(output)-3);
				i--;
			}
			str_append(output, "?=\n\t=?utf-8?q?");
			line_len_left = MIME_MAX_LINE_LEN -
				MIME_WRAPPER_LEN - 1;
		}
		switch (input[i]) {
		case ' ':
			str_append_c(output, '_');
			break;
		case '=':
		case '?':
		case '_':
			line_len_left -= 2;
			str_printfa(output, "=%2X", input[i]);
			break;
		default:
			if (input[i] < 32 || (input[i] & 0x80) != 0) {
				line_len_left -= 2;
				str_printfa(output, "=%2X", input[i]);
			} else {
				str_append_c(output, input[i]);
			}
			break;
		}
		line_len_left--;
	}
	str_append(output, "?=");
}

void message_header_encode_b(const unsigned char *input, unsigned int len,
			     string_t *output)
{
	unsigned int line_len, line_len_left, max;

	line_len = str_last_line_len(output);
	if (line_len >= MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN) {
		str_append(output, "\n\t");
		line_len = 1;
	}

	for (;;) {
		line_len_left = MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN - line_len;
		max = MAX_BASE64_DECODED_SIZE(line_len_left);
		do {
			max--;
			if (max > len)
				max = len;
			else {
				/* all of it doesn't fit. find a character where we
				   can split it from. */
				while (max > 0 && (input[max] & 0xc0) == 0x80)
					max--;
			}
		} while (MAX_BASE64_ENCODED_SIZE(max) > line_len_left &&
			 max > 0);

		if (max > 0) {
			str_append(output, "=?utf-8?b?");
			base64_encode(input, max, output);
			str_append(output, "?=");
		}

		input += max;
		len -= max;

		if (len == 0)
			break;

		str_append(output, "\n\t");
		line_len = 1;
	}
}

void message_header_encode(const char *_input, string_t *output)
{
	const unsigned char *input = (const unsigned char *)_input;
	unsigned int i, first_idx, last_idx;
	unsigned int enc_chars, enc_len, base64_len, q_len;
	bool use_q;

	/* find the first word that needs encoding */
	for (i = 0; input[i] != '\0'; i++) {
		if (input_idx_need_encoding(input, i))
			break;
	}
	if (input[i] == '\0') {
		/* no encoding necessary */
		str_append(output, _input);
		return;
	}
	first_idx = i;
	while (first_idx > 0 && !IS_LWSP(input[first_idx-1]))
		first_idx--;

	/* find the last word that needs encoding */
	last_idx = ++i; enc_chars = 1;
	for (; input[i] != '\0'; i++) {
		if (input_idx_need_encoding(input, i)) {
			last_idx = i + 1;
			enc_chars++;
		}
	}
	while (input[last_idx] != '\0' && !IS_LWSP(input[last_idx]))
		last_idx++;

	/* figure out if we should use Q or B encoding. Prefer Q if it's not
	   too much larger. */
	enc_len = last_idx - first_idx;
	base64_len = MAX_BASE64_ENCODED_SIZE(enc_len);
	q_len = enc_len + enc_chars*3;
	use_q = q_len*2/3 <= base64_len;

	/* and do it */
	str_append_n(output, input, first_idx);
	if (use_q)
		message_header_encode_q(input + first_idx, enc_len, output);
	else
		message_header_encode_b(input + first_idx, enc_len, output);
	str_append(output, _input + last_idx);
}