File: encode.c

package info (click to toggle)
courier 0.60.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 52,288 kB
  • ctags: 12,677
  • sloc: ansic: 165,348; cpp: 24,820; sh: 16,410; perl: 6,839; makefile: 3,621; yacc: 289; sed: 16
file content (256 lines) | stat: -rw-r--r-- 5,067 bytes parent folder | download | duplicates (12)
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
/*
** Copyright 2003-2004 Double Precision, Inc.  See COPYING for
** distribution information.
*/

/*
** $Id: encode.c,v 1.5 2005/11/16 02:23:15 mrsam Exp $
*/
#include	"encode.h"
#include	<string.h>
#include	<stdlib.h>

static int quoted_printable(struct libmail_encode_info *,
			    const char *, size_t);
static int base64(struct libmail_encode_info *,
		  const char *, size_t);
static int eflush(struct libmail_encode_info *,
		 const char *, size_t);

void libmail_encode_start(struct libmail_encode_info *info,
			  const char *transfer_encoding,
			  int (*callback_func)(const char *, size_t, void *),
			  void *callback_arg)
{
	info->output_buf_cnt=0;
	info->input_buf_cnt=0;

	switch (*transfer_encoding) {
	case 'q':
	case 'Q':
		info->encoding_func=quoted_printable;
		info->input_buffer[0]=0; /* Recycle for qp encoding */
		break;
	case 'b':
	case 'B':
		info->encoding_func=base64;
		break;
	default:
		info->encoding_func=eflush;
		break;
	}
	info->callback_func=callback_func;
	info->callback_arg=callback_arg;
}

int libmail_encode(struct libmail_encode_info *info,
		   const char *ptr,
		   size_t cnt)
{
	return ((*info->encoding_func)(info, ptr, cnt));
}

int libmail_encode_end(struct libmail_encode_info *info)
{
	int rc=(*info->encoding_func)(info, NULL, 0);

	if (rc == 0 && info->output_buf_cnt > 0)
	{
		rc= (*info->callback_func)(info->output_buffer,
					   info->output_buf_cnt,
					   info->callback_arg);
		info->output_buf_cnt=0;
	}

	return rc;
}

static int eflush(struct libmail_encode_info *info, const char *ptr, size_t n)
{
	while (n > 0)
	{
		size_t i;

		if (info->output_buf_cnt == sizeof(info->output_buffer))
		{
			int rc= (*info->callback_func)(info->output_buffer,
						       info->output_buf_cnt,
						       info->callback_arg);

			info->output_buf_cnt=0;
			if (rc)
				return rc;
		}

		i=n;

		if (i > sizeof(info->output_buffer) - info->output_buf_cnt)
			i=sizeof(info->output_buffer) - info->output_buf_cnt;

		memcpy(info->output_buffer + info->output_buf_cnt, ptr, i);
		info->output_buf_cnt += i;
		ptr += i;
		n -= i;
	}
	return 0;
}

static int base64_flush(struct libmail_encode_info *);

static int base64(struct libmail_encode_info *info,
		  const char *buf, size_t n)
{
	if (!buf)
	{
		int rc=0;

		if (info->input_buf_cnt > 0)
			rc=base64_flush(info);

		return rc;
	}

	while (n)
	{
		size_t	i;

		if (info->input_buf_cnt == sizeof(info->input_buffer))
		{
			int rc=base64_flush(info);

			if (rc != 0)
				return rc;
		}

		i=n;
		if (i > sizeof(info->input_buffer) - info->input_buf_cnt)
			i=sizeof(info->input_buffer) - info->input_buf_cnt;

		memcpy(info->input_buffer + info->input_buf_cnt,
		       buf, i);
		info->input_buf_cnt += i;
		buf += i;
		n -= i;
	}
	return 0;
}

static const char base64tab[]=
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static int base64_flush(struct libmail_encode_info *info)
{
	int	a=0,b=0,c=0;
	int	i, j;
	int	d, e, f, g;
	char	output_buf[ sizeof(info->input_buffer) / 3 * 4+1];

	for (j=i=0; i<info->input_buf_cnt; i += 3)
	{
		a=(unsigned char)info->input_buffer[i];
		b= i+1 < info->input_buf_cnt ?
			(unsigned char)info->input_buffer[i+1]:0;
		c= i+2 < info->input_buf_cnt ?
			(unsigned char)info->input_buffer[i+2]:0;

		d=base64tab[ a >> 2 ];
		e=base64tab[ ((a & 3 ) << 4) | (b >> 4)];
		f=base64tab[ ((b & 15) << 2) | (c >> 6)];
		g=base64tab[ c & 63 ];
		if (i + 1 >= info->input_buf_cnt)	f='=';
		if (i + 2 >= info->input_buf_cnt) g='=';
		output_buf[j++]=d;
		output_buf[j++]=e;
		output_buf[j++]=f;
		output_buf[j++]=g;
	}

	info->input_buf_cnt=0;

	output_buf[j++]='\n';
	return eflush(info, output_buf, j);
}

static const char xdigit[]="0123456789ABCDEF";

static int quoted_printable(struct libmail_encode_info *info,
			    const char *p, size_t n)
{
	char local_buf[256];
	int local_buf_cnt=0;

#define QPUT(c) do { if (local_buf_cnt == sizeof(local_buf)) \
                     { int rc=eflush(info, local_buf, local_buf_cnt); \
			local_buf_cnt=0; if (rc) return (rc); } \
			local_buf[local_buf_cnt]=(c); ++local_buf_cnt; } while(0)

	if (!p)
		return (0);

	while (n)
	{


		/*
		** Repurpose input_buffer[0] as a flag whether the previous
		** character was a space.
		**
		** A space before a newline gets escaped.
		*/

		if (info->input_buffer[0])
		{
			if (*p == '\n')
			{
				QPUT('=');
				QPUT('2');
				QPUT('0');
			}
			else
			{
				QPUT(' ');
			}
			++info->input_buf_cnt;
		}

		info->input_buffer[0]=0;

		if (*p == ' ')
		{
			info->input_buffer[0]=1;
			p++;
			--n;
			continue;
		}

		if (info->input_buf_cnt > 72 && *p != '\n')
		{
			QPUT('=');
			QPUT('\n');
			info->input_buf_cnt=0;
		}

		if ( *p == '\n')
			info->input_buf_cnt=0;
		else if (*p < ' ' || *p == '=' || *p >= 0x7F)
		{
			QPUT('=');
			QPUT(xdigit[ (*p >> 4) & 15]);
			QPUT(xdigit[ *p & 15 ]);
			info->input_buf_cnt += 3;
			p++;
			--n;
			continue;
		}
		else info->input_buf_cnt++;

		QPUT( *p);
		p++;
		--n;
	}

	if (local_buf_cnt > 0)
		return eflush(info, local_buf, local_buf_cnt);

	return 0;
}