rfc2047.c
3.95 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <mailutils/stream.h>
#include <mailutils/filter.h>
#include <mailutils/errno.h>
int
rfc2047_decode (const char *tocode, const char *input, char **ptostr)
{
int status = 0;
char *tmpcopy, *fromstr;
char *start_position = NULL;
char *buffer;
size_t bufsize;
size_t bufpos;
if (!tocode || !input || !ptostr)
return EINVAL;
/* Prepare a temporary copy of the input string (strtok_r is
going to modify it. */
tmpcopy = strdup (input);
if (!tmpcopy)
return ENOMEM;
fromstr = tmpcopy;
/* Allocate the buffer. It is assumed that encoded string is always
longer than it's decoded variant, so it's safe to use its length
as the first estimate */
bufsize = strlen (fromstr) + 1;
buffer = malloc (bufsize);
if (buffer == NULL)
{
free (tmpcopy);
return ENOMEM;
}
bufpos = 0;
while (*fromstr)
{
char *fromcode = NULL;
char *encoding_type = NULL;
char *encoded_text = NULL;
stream_t filter = NULL;
stream_t in_stream = NULL;
char *pbuffer = NULL;
const char *filter_type = NULL;
size_t nbytes = 0, size;
char *sp = NULL;
char *end_position = NULL;
start_position = strstr (fromstr, "=?");
if (!start_position)
break;
/* Copy the unencoded part */
nbytes = start_position - fromstr;
if (bufpos + nbytes > bufsize) /* just in case */
{
status = MU_ERR_BAD_2047_INPUT;
break;
}
memcpy (buffer + bufpos, fromstr, nbytes);
bufpos += nbytes;
fromcode = strtok_r (start_position + 2, "?", &sp);
encoding_type = strtok_r (NULL, "?", &sp);
encoded_text = strtok_r (NULL, "?", &sp);
if (sp[0] != '=')
{
status = MU_ERR_BAD_2047_INPUT;
break;
}
if (fromcode == NULL || encoding_type == NULL || encoded_text == NULL)
{
status = MU_ERR_BAD_2047_INPUT;
break;
}
size = strlen (encoded_text);
switch (toupper (encoding_type[0]))
{
case 'B':
filter_type = "base64";
break;
case 'Q':
filter_type = "quoted-printable";
break;
default:
status = MU_ERR_BAD_2047_INPUT;
break;
}
if (status != 0)
break;
memory_stream_create (&in_stream, 0, 0);
stream_write (in_stream, encoded_text, size, 0, NULL);
filter_create (&filter, in_stream, filter_type, MU_FILTER_DECODE,
MU_STREAM_READ);
while (stream_read (filter, buffer + bufpos, bufsize - bufpos, 0,
&nbytes) == 0 && nbytes)
{
/* FIXME: Need to convert character set */
bufpos += nbytes;
}
stream_close (filter);
stream_destroy (&filter, stream_get_owner (filter));
fromstr = sp + 1;
}
if (*fromstr)
{
size_t len = strlen (fromstr);
if (bufpos + len + 1 > bufsize) /* just in case */
status = MU_ERR_BAD_2047_INPUT;
else
{
memcpy (buffer + bufpos, fromstr, strlen (fromstr));
bufpos += strlen (fromstr);
}
}
buffer[bufpos++] = 0;
free (tmpcopy);
*ptostr = realloc (buffer, bufpos);
return status;
}