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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2009-2012 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <mailutils/filter.h>
#define ISWS(c) ((c)==' ' || (c)=='\t')
static enum mu_filter_result
_qp_decoder (void *xd,
enum mu_filter_command cmd,
struct mu_filter_io *iobuf)
{
char c;
size_t consumed = 0;
size_t wscount = 0;
size_t nbytes = 0;
const char *iptr;
size_t isize;
char *optr;
size_t osize;
int underscore_special = *(int*)xd;
switch (cmd)
{
case mu_filter_init:
case mu_filter_done:
return mu_filter_ok;
default:
break;
}
iptr = iobuf->input;
isize = iobuf->isize;
optr = iobuf->output;
osize = iobuf->osize;
while (consumed < isize && nbytes < osize)
{
c = *iptr++;
if (ISWS(c))
{
wscount++;
consumed++;
}
else
{
/* Octets with values of 9 and 32 MAY be
represented as US-ASCII TAB (HT) and SPACE characters,
respectively, but MUST NOT be so represented at the end
of an encoded line. Any TAB (HT) or SPACE characters
on an encoded line MUST thus be followed on that line
by a printable character. */
if (wscount)
{
if (c != '\r' && c != '\n')
{
size_t sz;
if (consumed >= isize)
break;
if (nbytes + wscount > osize)
sz = osize - nbytes;
else
sz = wscount;
memcpy(optr, iptr - wscount - 1, sz);
optr += sz;
nbytes += sz;
if (wscount > sz)
{
wscount -= sz;
break;
}
}
wscount = 0;
if (nbytes == osize)
break;
}
if (c == '=')
{
/* There must be 2 more characters before I consume this. */
if (consumed + 2 >= isize)
break;
else
{
/* you get =XX where XX are hex characters. */
char chr[3];
int new_c;
chr[2] = 0;
chr[0] = *iptr++;
/* Ignore LF. */
if (chr[0] != '\n')
{
chr[1] = *iptr++;
new_c = strtoul (chr, NULL, 16);
*optr++ = new_c;
nbytes++;
consumed += 3;
}
else
consumed += 2;
}
}
/* CR character. */
else if (c == '\r')
{
/* There must be at least 1 more character before
I consume this. */
if (consumed + 1 >= isize)
{
if (cmd == mu_filter_lastbuf)
consumed++;
break;
}
else
{
iptr++; /* Skip the CR character. */
*optr++ = '\n';
nbytes++;
consumed += 2;
}
}
else if (underscore_special && c == '_')
{
*optr++ = ' ';
nbytes++;
consumed++;
}
else
{
*optr++ = c;
nbytes++;
consumed++;
}
}
}
iobuf->isize = consumed - wscount;
iobuf->osize = nbytes;
return mu_filter_ok;
}
static enum mu_filter_result
_qp_encoder (void *xd,
enum mu_filter_command cmd,
struct mu_filter_io *iobuf)
{
unsigned int c;
size_t consumed = 0;
size_t nbytes = 0;
static const char _hexdigits[] = "0123456789ABCDEF";
const char *iptr;
size_t isize;
char *optr;
size_t osize;
int underscore_special = *(int*)xd;
switch (cmd)
{
case mu_filter_init:
case mu_filter_done:
return mu_filter_ok;
default:
break;
}
iptr = iobuf->input;
isize = iobuf->isize;
optr = iobuf->output;
osize = iobuf->osize;
nbytes = 0;
/* Strategy: check if we have enough room in the output buffer only
once the required size has been computed. If there is not enough,
return and hope that the caller will free up the output buffer a
bit. */
while (consumed < isize)
{
int simple_char;
/* candidate byte to convert */
c = *(unsigned char*) iptr;
if (underscore_special && c == '_')
simple_char = 0;
else
simple_char = (c >= 32 && c <= 60)
|| (c >= 62 && c <= 126)
|| c == '\t'
|| c == '\n';
if (simple_char)
{
/* a non-quoted character uses up one byte */
if (nbytes + 1 > osize)
break;
if (underscore_special && c == ' ')
*optr++ = '_';
else
*optr++ = c;
nbytes++;
iptr++;
consumed++;
}
else
{
/* a quoted character uses up three bytes */
if (nbytes + 3 > osize)
break;
*optr++ = '=';
*optr++ = _hexdigits[(c >> 4) & 0xf];
*optr++ = _hexdigits[c & 0xf];
nbytes += 3;
/* we've actuall used up one byte of input */
iptr++;
consumed++;
}
}
iobuf->isize = consumed;
iobuf->osize = nbytes;
return mu_filter_ok;
}
static int
alloc_qp (void **pret, int mode MU_ARG_UNUSED, int argc, const char **argv)
{
int *x = malloc (sizeof (*x));
if (!x)
return ENOMEM;
*x = 0;
*pret = x;
return 0;
}
static struct _mu_filter_record _qp_filter = {
"quoted-printable",
alloc_qp,
_qp_encoder,
_qp_decoder
};
mu_filter_record_t mu_qp_filter = &_qp_filter;
static int
alloc_Q (void **pret, int mode MU_ARG_UNUSED, int argc, const char **argv)
{
int *x = malloc (sizeof (*x));
if (!x)
return ENOMEM;
*x = 1;
*pret = x;
return 0;
}
static struct _mu_filter_record _Q_filter = {
"Q",
alloc_Q,
_qp_encoder,
_qp_decoder
};
mu_filter_record_t mu_rfc_2047_Q_filter = &_Q_filter;
|