Commit 3c5aed39 3c5aed3965c70e6e1a894005b1adc7a407d0aae9 by Sergey Poznyakoff

Re-implement parser for RFC-2231 header fields.

The new implementation is able to return all parameters at once,
in an associative array. A subset of parameters can be requested.
The data are automatically converted to the output charset.

In addition, RFC-2047 parser is extended to support language
specifications (RFC-2231, chapter 5).

* include/mailutils/message.h (MU_MIMEHDR_CSINFO)
(MU_MIMEHDR_MULTILINE): Remove, not public anymore.
(mu_mimehdr_get_param,mu_mimehdr_aget_param)
(mu_message_aget_attachment_name)
(mu_message_get_attachment_name): Remove pflags agrument.
* include/mailutils/mime.h (mu_mime_param): New struct.
(mu_rfc2047_decode_param)
(mu_mime_header_parse,mu_mime_header_parse_subset): New proto.
* libmailutils/base/rfc2047.c (_rfc2047_decode_param): New
auxiliary function.  Use memory stream to collect data.
(mu_rfc2047_decode): Rewrite as a wrapper around the above.
(mu_rfc2047_decode_param): New function.
* libmailutils/filter/decode.c (mu_decode_filter_args): Pass actual
(instead of maximal) number of arguments to mu_filter_chain_create.
* libmailutils/mime/mimehdr.c: Rewrite from scratch.
* libmailutils/tests/.gitignore: Add mimehdr.
* libmailutils/tests/Makefile.am (noinst_PROGRAMS): Add mimehdr.
(TESTSUITE_AT): Add mimehdr.at.
* libmailutils/tests/mimehdr.at: New test.
* libmailutils/tests/mimehdr.c: New test program.
* libmailutils/tests/testsuite.at: Include.
1 parent c22d07a3
......@@ -162,31 +162,20 @@ extern int mu_mime_io_buffer_aget_charset (mu_mime_io_buffer_t info,
const char **charset);
/* Bit values for *pflags in functions below */
#define MU_MIMEHDR_MULTILINE 0x01 /* Parameter was multiline */
#define MU_MIMEHDR_CSINFO 0x02 /* Parameter contains charset/language
info */
extern int mu_mimehdr_get_disp (const char *str, char *buf, size_t bufsz,
size_t *retsz);
extern int mu_mimehdr_aget_disp (const char *str, char **pvalue);
extern int mu_mimehdr_get_param (const char *str, const char *param,
char *buf, size_t bufsz, size_t *retsz,
int *pflags);
char *buf, size_t bufsz, size_t *retsz);
extern int mu_mimehdr_aget_param (const char *str, const char *param,
char **pval, int *pflags);
extern int mu_mimehdr_decode_param (const char *value, int csinfo,
const char *charset,
char **pval, char **plang);
char **pval);
extern int mu_mimehdr_aget_decoded_param (const char *str, const char *param,
const char *charset,
char **pval, char **plang);
extern int mu_message_get_attachment_name (mu_message_t, char *name,
size_t bufsz, size_t* sz,
int *pflags);
extern int mu_message_aget_attachment_name (mu_message_t, char **name,
int *pflags);
size_t bufsz, size_t* sz);
extern int mu_message_aget_attachment_name (mu_message_t, char **name);
extern int mu_message_aget_decoded_attachment_name (mu_message_t msg,
const char *charset,
char **name,
......
......@@ -29,6 +29,13 @@
extern "C" {
#endif
struct mu_mime_param
{
char *lang;
char *cset;
char *value;
};
int mu_mime_create (mu_mime_t *pmime, mu_message_t msg, int flags);
void mu_mime_destroy (mu_mime_t *pmime);
void mu_mime_ref (mu_mime_t mime);
......@@ -49,13 +56,22 @@ int mu_rfc2047_decode (const char *tocode, const char *fromstr,
int mu_rfc2047_encode (const char *charset, const char *encoding,
const char *text, char **result);
int mu_rfc2047_decode_param (const char *tocode, const char *input,
struct mu_mime_param *param);
int mu_base64_encode (const unsigned char *input, size_t input_len,
unsigned char **output, size_t * output_len);
int mu_base64_decode (const unsigned char *input, size_t input_len,
unsigned char **output, size_t * output_len);
int mu_mime_header_parse (const char *text, char *charset, char **pvalue,
mu_assoc_t *paramtab);
int mu_mime_header_parse_subset (const char *text, const char *charset,
char **pvalue,
mu_assoc_t assoc);
#ifdef __cplusplus
}
#endif
......
......@@ -28,20 +28,9 @@
#include <mailutils/stream.h>
#include <mailutils/filter.h>
#include <mailutils/errno.h>
#include <mailutils/mime.h>
#include <mailutils/util.h>
static int
realloc_buffer (char **bufp, size_t *bufsizep, size_t incr)
{
size_t newsize = *bufsizep + incr;
char *newp = realloc (*bufp, newsize);
if (newp == NULL)
return 1;
*bufp = newp;
*bufsizep = newsize;
return 0;
}
int
getword (char **pret, const char **pstr, int delim)
{
......@@ -65,52 +54,32 @@ getword (char **pret, const char **pstr, int delim)
return 0;
}
int
mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
static int
_rfc2047_decode_param (const char *tocode, const char *input,
struct mu_mime_param *param)
{
int status = 0;
const char *fromstr;
char *buffer;
size_t bufsize;
size_t bufpos;
size_t run_count = 0;
char *fromcode = NULL;
char *encoding_type = NULL;
char *encoded_text = NULL;
char *tocodetmp = NULL;
mu_stream_t str;
#define BUFINC 128
#define CHKBUF(count) do { \
if (bufpos+count >= bufsize) \
{ \
size_t s = bufpos + count - bufsize; \
if (s < BUFINC) \
s = BUFINC; \
if (realloc_buffer (&buffer, &bufsize, s)) \
{ \
free (buffer); \
free (fromcode); \
free (encoding_type); \
free (encoded_text); \
return ENOMEM; \
} \
} \
} while (0)
if (!input)
return EINVAL;
if (!ptostr)
return MU_ERR_OUT_PTR_NULL;
memset (param, 0, sizeof (*param));
fromstr = input;
status = mu_memory_stream_create (&str, MU_STREAM_RDWR);
if (status)
return status;
/* 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)
return ENOMEM;
bufpos = 0;
if (tocode && (param->cset = strdup (tocode)) == NULL)
{
mu_stream_destroy (&str);
return ENOMEM;
}
fromstr = input;
while (*fromstr)
{
......@@ -119,13 +88,39 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
mu_stream_t filter = NULL;
mu_stream_t in_stream = NULL;
const char *filter_type = NULL;
size_t nbytes = 0, size;
size_t size;
const char *sp = fromstr + 2;
char tmp[128];
char *lang;
status = getword (&fromcode, &sp, '?');
if (status)
break;
lang = strchr (fromcode, '*');
if (lang)
*lang++ = 0;
if (!param->cset)
{
param->cset = strdup (fromcode);
if (!param->cset)
{
status = ENOMEM;
break;
}
}
if (lang && !param->lang && (param->lang = strdup (lang)) == NULL)
{
status = ENOMEM;
break;
}
if (!tocode)
{
if ((tocodetmp = strdup (fromcode)) == NULL)
{
status = ENOMEM;
break;
}
tocode = tocodetmp;
}
status = getword (&encoding_type, &sp, '?');
if (status)
break;
......@@ -162,22 +157,12 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
mu_static_memory_stream_create (&in_stream, encoded_text, size);
mu_stream_seek (in_stream, 0, MU_SEEK_SET, NULL);
status = mu_decode_filter (&filter, in_stream, filter_type, fromcode,
tocode);
status = mu_decode_filter (&filter, in_stream, filter_type,
fromcode, tocode);
mu_stream_unref (in_stream);
if (status != 0)
break;
while ((status =
mu_stream_read (filter, tmp, sizeof (tmp), &nbytes)) == 0
&& nbytes)
{
CHKBUF (nbytes);
memcpy (buffer + bufpos, tmp, nbytes);
bufpos += nbytes;
}
mu_stream_close (filter);
status = mu_stream_copy (str, filter, 0, NULL);
mu_stream_destroy (&filter);
if (status)
......@@ -198,44 +183,89 @@ mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
{
if (--run_count)
{
CHKBUF (run_count);
memcpy (buffer + bufpos, fromstr - run_count, run_count);
bufpos += run_count;
status = mu_stream_write (str, fromstr - run_count,
run_count, NULL);
if (status)
break;
run_count = 0;
}
CHKBUF (1);
buffer[bufpos++] = *fromstr++;
status = mu_stream_write (str, fromstr, 1, NULL);
if (status)
break;
fromstr++;
}
}
else
{
CHKBUF (1);
buffer[bufpos++] = *fromstr++;
status = mu_stream_write (str, fromstr, 1, NULL);
if (status)
break;
fromstr++;
}
}
if (*fromstr)
{
size_t len = strlen (fromstr);
CHKBUF (len);
memcpy (buffer + bufpos, fromstr, len);
bufpos += len;
}
if (status == 0 && *fromstr)
status = mu_stream_write (str, fromstr, strlen (fromstr), NULL);
CHKBUF (1);
buffer[bufpos++] = 0;
free (fromcode);
free (encoding_type);
free (encoded_text);
free (tocodetmp);
if (status == 0)
{
mu_off_t size;
if (status)
free (buffer);
else
*ptostr = realloc (buffer, bufpos);
mu_stream_size (str, &size);
param->value = malloc (size + 1);
if (!param->value)
status = ENOMEM;
else
{
mu_stream_seek (str, 0, MU_SEEK_SET, NULL);
status = mu_stream_read (str, param->value, size, NULL);
param->value[size] = 0;
}
}
mu_stream_destroy (&str);
return status;
}
int
mu_rfc2047_decode_param (const char *tocode, const char *input,
struct mu_mime_param *param)
{
int rc;
struct mu_mime_param tmp;
if (!input)
return EINVAL;
if (!param)
return MU_ERR_OUT_PTR_NULL;
rc = _rfc2047_decode_param (tocode, input, &tmp);
if (rc == 0)
*param = tmp;
return rc;
}
int
mu_rfc2047_decode (const char *tocode, const char *input, char **ptostr)
{
int rc;
struct mu_mime_param param;
if (!input)
return EINVAL;
if (!ptostr)
return MU_ERR_OUT_PTR_NULL;
rc = _rfc2047_decode_param (tocode, input, &param);
free (param.cset);
free (param.lang);
if (rc == 0)
*ptostr = param.value;
return rc;
}
/**
Encode a header according to RFC 2047
......
......@@ -73,7 +73,7 @@ mu_decode_filter_args (mu_stream_t *pfilter, mu_stream_t input,
rc = mu_filter_chain_create (pfilter, input,
MU_FILTER_DECODE, MU_STREAM_READ,
xargc, xargv);
i, xargv);
free (xargv);
return rc;
}
......
/* GNU Mailutils -- a suite of utilities for electronic mail
/* Operations on RFC-2231-compliant mail headers fields.
GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2009, 2010, 2011
Free Software Foundation, Inc.
......@@ -15,6 +16,7 @@
You should have received a copy of the GNU Lesser General
Public License along with this library. If not,
see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
......@@ -32,386 +34,693 @@
#include <mailutils/mime.h>
#include <mailutils/filter.h>
#include <mailutils/util.h>
#include <mailutils/wordsplit.h>
#include <mailutils/assoc.h>
#include <mailutils/iterator.h>
#include <mailutils/diag.h>
#include <mailutils/nls.h>
#define MU_MIMEHDR_MULTILINE 0x01 /* Parameter was multiline */
#define MU_MIMEHDR_CSINFO 0x02 /* Parameter contains charset/language
info */
/* Free members of struct mu_mime_param, but do not free it itself. */
static void
_mu_mime_param_free (struct mu_mime_param *p)
{
free (p->lang);
free (p->cset);
free (p->value);
}
/* See RFC 2045, 5.1. Syntax of the Content-Type Header Field */
#define _ISSPECIAL(c) !!strchr ("()<>@,;:\\\"/[]?=", c)
/* Treat ITEM as a pointer to struct mu_mime_param and reclaim all
memory associated with it.
/* _header_get_param - an auxiliary function to extract values from
Content-Type, Content-Disposition and similar headers.
This is intended for use as a destroy_item method of assoc tables and
lists. */
static void
_mu_mime_param_free_item (void *item)
{
_mu_mime_param_free (item);
free (item);
}
Arguments:
/* Recode a string between two charsets.
FIELD_BODY Header value, complying to RFCs 2045, 2183, 2231.3;
DISP Disposition. Unless it is NULL, the disposition part
of FIELD_BODY is compared with it. If they differ,
the function returns MU_ERR_NOENT.
PARAM Name of the parameter to extract from FIELD_BODY;
BUF Where to extract the value to;
BUFSZ Size of BUF;
PRET Pointer to the memory location for the return buffer (see
below).
PLEN Pointer to the return size.
PFLAGS On return, flags describing the parameter are stored there.
The MU_MIMEHDR_MULTILINE bit is set if the parameter value
was multiline (RFC 2231.3). The MU_MIMEHDR_CSINFO bit is set
if the parameter value includes charset/language
information (RFC 2231.4).
The function parses FIELD_BODY and extracts the value of the parameter
PARAM.
If BUF is not NULL and BUFSZ is not 0, the extracted value is stored into
BUF. At most BUFSZ-1 bytes are copied.
Otherwise, if PRET is not NULL, the function allocates enough memory to
hold the extracted value, copies there the result, and stores the
pointer to the allocated memory into the location pointed to by PRET.
If PLEN is not NULL, the size of the extracted value (without terminating
NUL character) is stored there.
If BUF==NULL *and* PRET==NULL, no memory is allocated, but PLEN is
honored anyway, i.e. unless it is NULL it receives size of the result.
This can be used to estimate the needed buffer size.
Return values:
0 on success.
MU_ERR_NOENT, requested parameter not found, or disposition does
not match DISP.
MU_ERR_PARSE, if FIELD_BODY does not comply to any of the abovemntioned
RFCs.
ENOMEM , if unable to allocate memory.
Input:
TEXT - A string.
ICS - Charset of TEXT.
OCS - Charset to convert TEXT to.
Output:
PRESULT - On success, the pointer to the resulting string is stored here.
*/
static int
_recode_string (char *text, const char *ics, const char *ocs, char **presult)
{
mu_stream_t istr, ostr, cvt;
mu_off_t size;
char *decoded;
int rc;
rc = mu_static_memory_stream_create (&istr, text, strlen (text));
if (rc)
return rc;
rc = mu_memory_stream_create (&ostr, 0);
if (rc)
return rc;
rc = mu_decode_filter (&cvt, istr, NULL, ics, ocs);
mu_stream_unref (istr);
if (rc)
{
mu_stream_unref (ostr);
return rc;
}
rc = mu_stream_copy (ostr, cvt, 0, &size);
mu_stream_unref (cvt);
if (rc)
{
mu_stream_unref (ostr);
return rc;
}
/* Internal flag used by _header_get_param to delay increasing
estimated continuation index. */
#define _MU_MIMEHDR_INCR_CIND 0x8000
decoded = malloc (size + 1);
if (!decoded)
{
mu_stream_unref (ostr);
return ENOMEM;
}
int
_header_get_param (const char *field_body,
const char *disp,
const char *param,
char *buf, size_t bufsz,
char **pret, size_t *plen,
int *pflags)
mu_stream_seek (ostr, 0, MU_SEEK_SET, NULL);
rc = mu_stream_read (ostr, decoded, size, NULL);
mu_stream_unref (ostr);
if (rc)
free (decoded);
else
{
decoded[size] = 0;
*presult = decoded;
}
return rc;
}
/* Structure for composing continued parameters.
See RFC 2231, Section 3, "Parameter Value Continuations" */
struct param_continuation
{
int res = MU_ERR_NOENT; /* Return value, pessimistic default */
size_t param_len = strlen (param);
char *p;
size_t size;
char *mem = NULL; /* Allocated memory storage */
size_t retlen = 0; /* Total number of bytes copied */
unsigned long cind = 0; /* Expected continued parameter index.
See RFC 2231, Section 3,
"Parameter Value Continuations" */
int flags = 0;
if (field_body == NULL)
return EINVAL;
char *param_name; /* Parameter name */
size_t param_length; /* Length of param_name */
mu_stream_t param_value; /* Its value (memory stream) */
int param_cind; /* Expected continued parameter index. */
/* Language/character set information */
const char *param_lang;
const char *param_cset;
};
if (bufsz == 0) /* Make sure buf value is meaningful */
buf = NULL;
static int
free_param_continuation (struct param_continuation *p)
{
free (p->param_name);
mu_stream_destroy (&p->param_value);
/* param_lang and param_cset are handled separately */
memset (p, 0, sizeof (*p));
}
/* Auxiliary function to store the data collected in CONT into ASSOC.
If SUBSET is True, ASSOC is populated with empty mu_mime_param
structures. In this case data will be stored only if CONT->param_name
is already in ASSOC. If OUTCHARSET is not NULL, the value from
CONT->param_value will be recoded to that charset before storing it. */
static int
flush_param (struct param_continuation *cont, mu_assoc_t assoc, int subset,
const char *outcharset)
{
int rc;
struct mu_mime_param param, *param_slot = NULL;
mu_off_t size;
p = strchr (field_body, ';');
if (!p)
return MU_ERR_NOENT;
/* Allow for possible whitespace before the semicolon */
for (size = p - field_body;
size > 0 && mu_isblank (field_body[size-1]); size--)
;
/* Remove surrounding quotes.
FIXME: unescape the quoted contents. */
if (field_body[0] == '"' && field_body[size-1] == '"')
if (subset)
{
field_body++;
size -= 2;
param_slot = mu_assoc_ref (assoc, cont->param_name);
if (!param_slot)
return 0;
}
if (disp && mu_c_strncasecmp (field_body, disp, size))
return MU_ERR_NOENT;
while (p && *p)
if (cont->param_lang)
{
char *v, *e, *ep, *cp;
size_t len, escaped_chars = 0;
if (*p != ';')
param.lang = strdup (cont->param_lang);
if (!param.lang)
return ENOMEM;
}
else
param.lang = NULL;
if (outcharset || cont->param_cset)
{
param.cset = strdup (outcharset ? outcharset : cont->param_cset);
if (!param.cset)
{
res = MU_ERR_PARSE;
break;
free (param.lang);
return ENOMEM;
}
/* walk upto start of param */
p = mu_str_skip_class (p + 1, MU_CTYPE_SPACE);
}
rc = mu_stream_size (cont->param_value, &size);
if (rc == 0)
{
param.value = malloc (size + 1);
if (!param.value)
rc = ENOMEM;
}
/* Reportedly, some MUAs insert several semicolons */
if (*p == ';')
continue;
if (rc == 0)
{
rc = mu_stream_seek (cont->param_value, 0, MU_SEEK_SET, NULL);
if (rc == 0)
rc = mu_stream_read (cont->param_value, param.value, size, NULL);
param.value[size] = 0;
}
if (rc)
{
free (param.lang);
free (param.cset);
return rc;
}
/* Ignore stray characters */
if (_ISSPECIAL (*p))
if (cont->param_cset && outcharset &&
mu_c_strcasecmp (cont->param_cset, outcharset))
{
char *tmp;
rc = _recode_string (param.value, cont->param_cset, outcharset, &tmp);
free (param.value);
if (rc)
{
p = strchr (p, ';');
continue;
free (param.lang);
free (param.cset);
return rc;
}
param.value = tmp;
}
if ((ep = strchr (p, '=')) == NULL)
break;
/* Allow for optional whitespace after '=' */
v = mu_str_skip_class (ep + 1, MU_CTYPE_SPACE);
/* Find end of the parameter */
if (*v == '"')
if (param_slot)
{
*param_slot = param;
}
else
{
rc = mu_assoc_install (assoc, cont->param_name, &param);
if (rc)
_mu_mime_param_free (&param);
}
return rc;
}
/* Create and initialize an empty associative array for parameters. */
int
mu_mime_param_assoc_create (mu_assoc_t *paramtab)
{
mu_assoc_t assoc;
int rc = mu_assoc_create (&assoc, sizeof (struct mu_mime_param),
MU_ASSOC_ICASE);
if (rc == 0)
mu_assoc_set_free (assoc, _mu_mime_param_free_item);
*paramtab = assoc;
return rc;
}
/* Add an empty structure for the slot NAME in ASSOC. */
int
mu_mime_param_assoc_add (mu_assoc_t assoc, const char *name)
{
struct mu_mime_param param;
memset (&param, 0, sizeof param);
return mu_assoc_install (assoc, name, &param);
}
/* See FIXME near the end of _mime_header_parse, below. */
static int
_remove_entry (void *item, void *data)
{
struct mu_mime_param *p = item;
mu_assoc_t assoc = data;
mu_assoc_remove_ref (assoc, p);
return 0;
}
/* A working horse of this module. Parses input string, which should
be a header field value complying to RFCs 2045, 2183, 2231.3.
Input:
TEXT - The string.
ASSOC - Associative array of parameters indexed by their names.
SUBSET - If true, store only those parameters that are already
in ASSOC.
Output:
PVALUE - Unless NULL, a pointer to the field value is stored here on
success.
ASSOC - Unless NULL, parameters are stored here.
Either PVALUE or ASSOC (but not both) can be NULL, meaning that the
corresponding data are of no interest to the caller.
*/
static int
_mime_header_parse (const char *text, char **pvalue,
mu_assoc_t assoc, const char *outcharset, int subset)
{
int rc;
struct mu_wordsplit ws;
struct param_continuation cont;
size_t i;
ws.ws_delim = " \t\r\n;";
ws.ws_escape = "\\\"";
if (mu_wordsplit (text, &ws,
MU_WRDSF_DELIM | MU_WRDSF_ESCAPE |
MU_WRDSF_NOVAR | MU_WRDSF_NOCMD |
MU_WRDSF_DQUOTE | MU_WRDSF_SQUEEZE_DELIMS |
MU_WRDSF_RETURN_DELIMS | MU_WRDSF_WS))
{
mu_debug (MU_DEBCAT_MIME, MU_DEBUG_ERROR,
(_("wordsplit: %s"), mu_wordsplit_strerror (&ws)));
return MU_ERR_PARSE;
}
if (!assoc)
{
if (!pvalue)
return MU_ERR_OUT_PTR_NULL;
*pvalue = strdup (ws.ws_wordv[i]);
mu_wordsplit_free (&ws);
if (!*pvalue)
return ENOMEM;
return 0;
}
memset (&cont, 0, sizeof (cont));
for (i = 1; i < ws.ws_wordc; i++)
{
size_t klen;
char *key;
char *val;
const char *lang = NULL;
const char *cset = NULL;
char *langp = NULL;
char *csetp = NULL;
char *p;
char *decoded;
int flags = 0;
struct mu_mime_param param;
key = ws.ws_wordv[i];
if (key[0] == ';')
/* Reportedly, some MUAs insert several semicolons */
continue;
p = strchr (key, '=');
if (!p)
val = "";
else
{
/* Quoted string */
for (e = ++v; *e != '"'; e++)
*p++ = 0;
val = p;
}
klen = strlen (key);
if (klen == 0)
continue;
p = strchr (key, '*');
if (p)
{
/* It is a parameter value continuation (RFC 2231, Section 3)
or parameter value character set and language information
(ibid., Section 4). */
klen = p - key;
if (p[1])
{
if (*e == 0) /* Malformed header */
if (mu_isdigit (p[1]))
{
res = MU_ERR_PARSE;
break;
}
if (*e == '\\')
{
if (*++e == 0)
char *q;
unsigned long n = strtoul (p + 1, &q, 10);
if (*q && *q != '*')
{
res = MU_ERR_PARSE;
break;
mu_debug (MU_DEBCAT_MIME, MU_DEBUG_TRACE0,
(_("malformed parameter name %s: skipping"),
key));
continue;
}
escaped_chars++;
if (n != cont.param_cind)
{
mu_debug (MU_DEBCAT_MIME, MU_DEBUG_TRACE0,
(_("continuation index out of sequence in %s: "
"skipping"),
key));
/* Ignore this parameter. Another possibility would be
to drop the continuation assembled so far. That makes
little difference, because the string is malformed
anyway.
We try to continue just to gather as many information
as possible from this mess.
*/
continue;
}
if (n == 0)
{
cont.param_name = malloc (klen + 1);
if (!cont.param_name)
{
rc = ENOMEM;
break;
}
cont.param_length = klen;
memcpy (cont.param_name, key, klen);
cont.param_name[klen] = 0;
rc = mu_memory_stream_create (&cont.param_value,
MU_STREAM_RDWR);
if (rc)
break;
}
else if (cont.param_length != klen ||
memcmp (cont.param_name, key, klen))
{
mu_debug (MU_DEBCAT_MIME, MU_DEBUG_TRACE0,
(_("continuation name mismatch: %s: "
"skipping"),
key));
continue;
}
if (*q == '*')
flags |= MU_MIMEHDR_CSINFO;
cont.param_cind++;
flags |= MU_MIMEHDR_MULTILINE;
}
}
if (res == MU_ERR_PARSE)
break;
len = e - v;
e++;
else
{
flags |= MU_MIMEHDR_CSINFO;
*p = 0;
}
}
else
else if (cont.param_name)
{
for (e = v + 1; *e && !(*e == ';' || mu_isspace (*e)); e++)
;
len = e - v;
rc = flush_param (&cont, assoc, subset, outcharset);
free_param_continuation (&cont);
if (rc)
break;
}
if (flags & MU_MIMEHDR_CSINFO)
{
p = strchr (val, '\'');
if (p)
{
char *q = strchr (p + 1, '\'');
if (q)
{
cset = val;
*p++ = 0;
lang = p;
*q++ = 0;
val = q;
}
}
/* Is it our parameter? */
if (mu_c_strncasecmp (p, param, param_len))
{ /* nope, jump to next */
p = strchr (e, ';');
continue;
if ((flags & MU_MIMEHDR_MULTILINE) && cont.param_cind == 1)
{
cont.param_lang = lang;
cont.param_cset = cset;
}
}
cp = p + param_len;
if (*cp == '*')
if (flags & MU_MIMEHDR_CSINFO)
{
cp++;
/* It is a parameter value continuation (RFC 2231, Section 3)
or parameter value character set and language information
(ibid., Section 4). */
if (mu_isdigit (*cp))
{
/* See if the index is OK */
char *tmp;
char *end;
unsigned long n = strtoul (cp, &end, 10);
if (*end == '*')
{
flags |= MU_MIMEHDR_CSINFO;
end++;
}
if (n != cind)
rc = mu_str_url_decode (&tmp, val);
if (rc)
break;
if (!(flags & MU_MIMEHDR_MULTILINE))
{
if (!outcharset || mu_c_strcasecmp (cset, outcharset) == 0)
decoded = tmp;
else
{
res = MU_ERR_PARSE;
break;
rc = _recode_string (tmp, cset, outcharset, &decoded);
free (tmp);
}
/* Everything OK, mark this as a multiline (continued)
parameter. We also need to increment the estimation,
but it cannot be done right now because its value is
used below to decide whether to do flag cleanup
on error. So we set _MU_MIMEHDR_INCR_CIND flag instead
and increment cind later. */
flags |= (MU_MIMEHDR_MULTILINE|_MU_MIMEHDR_INCR_CIND);
/* And point cp to the last character: there are more
checks ahead. */
cp = end;
if (rc)
break;
}
else
flags |= MU_MIMEHDR_CSINFO;
decoded = tmp;
}
/* Allow for optional whitespace before '=' */
cp = mu_str_skip_class (cp, MU_CTYPE_SPACE);
/* cp must now point to the equals sign */
if (cp != ep)
else
{
/* Clean up everything, unless we're in the middle of a
parameter continuation. */
if (cind == 0)
flags = 0;
/* Try next parameter */
p = strchr (e, ';');
continue;
rc = mu_rfc2047_decode_param (outcharset, val, &param);
if (rc)
return rc;
cset = csetp = param.cset;
lang = langp = param.lang;
decoded = param.value;
}
if (flags & _MU_MIMEHDR_INCR_CIND)
val = decoded;
if (flags & MU_MIMEHDR_MULTILINE)
{
/* Increase the estimation. */
flags &= ~_MU_MIMEHDR_INCR_CIND;
cind++;
rc = mu_stream_write (cont.param_value, val, strlen (val),
NULL);
free (decoded);
free (csetp);
free (langp);
if (rc)
break;
continue;
}
res = 0; /* Indicate success */
/* Prepare P for the next iteration */
p = e;
/* Escape characters that appear in quoted-pairs are
semantically "invisible" (RFC 2822, Section 3.2.2,
"Quoted characters") */
len -= escaped_chars;
/* Adjust len if nearing end of the buffer */
if (bufsz && len >= bufsz)
len = bufsz - 1;
if (pret)
memset (&param, 0, sizeof (param));
if (lang)
{
/* The caller wants us to allocate the memory */
if (!buf && !mem)
{
mem = malloc (len + 1);
if (!mem)
{
res = ENOMEM;
break;
}
buf = mem;
}
else if (mem)
param.lang = strdup (lang);
if (!param.lang)
rc = ENOMEM;
else
{
/* If we got here, it means we are iterating over
a parameter value continuation, and cind=0 has
already been passed. Reallocate the memory to
accomodate next chunk of data. */
char *newmem = realloc (mem, retlen + len + 1);
if (!newmem)
param.cset = strdup (cset);
if (!param.cset)
{
res = ENOMEM;
break;
free (param.lang);
rc = ENOMEM;
}
buf = mem = newmem;
}
}
if (buf)
free (csetp);
free (langp);
if (rc)
{
free (decoded);
break;
}
param.value = strdup (val);
free (decoded);
if (!param.value)
{
/* Actually copy the data. Buf is not NULL either because
the user passed it as an argument, or because we allocated
memory for it. */
if (escaped_chars)
_mu_mime_param_free (&param);
rc = ENOMEM;
break;
}
if (subset)
{
struct mu_mime_param *p = mu_assoc_ref (assoc, key);
if (p)
*p = param;
}
else
{
rc = mu_assoc_install (assoc, key, &param);
if (rc)
{
int i;
for (i = 0; i < len; i++)
{
if (*v == '\\')
++v;
buf[retlen + i] = *v++;
}
_mu_mime_param_free (&param);
break;
}
else
memcpy (buf + retlen, v, len);
}
/* Adjust total result size ... */
retlen += len;
/* ... and remaining buffer size, if necessary */
if (bufsz)
}
if (rc == 0 && cont.param_name)
rc = flush_param (&cont, assoc, subset, outcharset);
free_param_continuation (&cont);
if (rc == 0)
{
if (pvalue)
{
bufsz -= len;
if (bufsz == 0)
break;
*pvalue = strdup (ws.ws_wordv[0]);
if (!*pvalue)
rc = ENOMEM;
}
}
if (res == 0)
mu_wordsplit_free (&ws);
if (subset)
{
/* Everything OK, prepare the returned data. */
if (buf)
buf[retlen] = 0;
if (plen)
*plen = retlen;
if (pret)
*pret = mem;
if (pflags)
*pflags = flags;
/* Eliminate empty elements.
FIXME: What I wanted to do initially is commented out, because
unfortunately iterator_ctl is not defined for assoc tables...
*/
#if 0
mu_iterator_t itr;
rc = mu_assoc_get_iterator (assoc, &itr);
if (rc == 0)
{
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *name;
struct mu_mime_param *p;
mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
if (!p->value)
mu_iterator_ctl (itr, mu_itrctl_delete, NULL);
}
mu_iterator_destroy (&itr);
}
#else
/* ... Instead, the following kludgy approach is taken: */
mu_iterator_t itr;
mu_list_t elist;
rc = mu_list_create (&elist);
if (rc)
return rc;
rc = mu_assoc_get_iterator (assoc, &itr);
if (rc == 0)
{
for (mu_iterator_first (itr); rc == 0 && !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *name;
struct mu_mime_param *p;
mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
if (!p->value)
rc = mu_list_append (elist, p);
}
mu_iterator_destroy (&itr);
}
if (rc == 0)
mu_list_foreach (elist, _remove_entry, assoc);
mu_list_destroy (&elist);
#endif
}
else if (mem)
free (mem);
return res;
return rc;
}
static size_t
disp_segment_len (const char *str)
/* Parse header value from TEXT and return its value and a subset of
parameters.
Input:
TEXT - Header value.
CSET - Output charset. Can be NULL, in which case no conversions
take place.
ASSOC - Parameter array initialized with empty slots for those
parameters, which are wanted on output. It should be
created using mu_mime_param_assoc_create and populated
using mu_mime_param_assoc_add.
Output:
PVALUE - A pointer to the field value is stored here on success.
ASSOC - Receives available parameters matching the input subset.
Either PVALUE or ASSOC (but not both) can be NULL, meaning that the
corresponding data are of no interest to the caller.
*/
int
mu_mime_header_parse_subset (const char *text, const char *cset,
char **pvalue, mu_assoc_t assoc)
{
char *p = strchr (str, ';');
size_t size;
if (!p)
size = strlen (str);
else
size = p - str;
while (size > 0 && mu_isblank (str[size-1]))
size--;
return size;
return _mime_header_parse (text, pvalue, assoc, cset, 1);
}
/* STR is a value of a structured MIME header, e.g. Content-Type.
/* Parse header value from TEXT and return its value and parameters.
Input:
TEXT - Header value.
CSET - Output charset. Can be NULL, in which case no conversions
take place.
Output:
PVALUE - A pointer to the field value is stored here on success.
PASSOC - Receives an associative array of parameters.
Either PVALUE or PASSOC (but not both) can be NULL, meaning that the
corresponding data are of no interest to the caller.
*/
int
mu_mime_header_parse (const char *text, char *cset, char **pvalue,
mu_assoc_t *passoc)
{
int rc;
mu_assoc_t assoc;
rc = mu_mime_param_assoc_create (&assoc);
if (rc == 0)
{
rc = _mime_header_parse (text, pvalue, assoc, cset, 0);
if (rc || !passoc)
mu_assoc_destroy (&assoc);
else
*passoc = assoc;
}
return rc;
}
/* TEXT is a value of a structured MIME header, e.g. Content-Type.
This function returns the `disposition part' of it. In other
words, it returns disposition, if STR is a Content-Disposition
words, it returns disposition, if TEXT is a Content-Disposition
value, and `type/subtype' part, if it is a Content-Type value.
*/
int
mu_mimehdr_get_disp (const char *str, char *buf, size_t bufsz, size_t *retsz)
mu_mimehdr_get_disp (const char *text, char *buf, size_t bufsz, size_t *retsz)
{
size_t size;
str = mu_str_skip_class (str, MU_CTYPE_BLANK);
size = disp_segment_len (str);
if (size > 2 && str[0] == '"' && str[size-1] == '"')
int rc;
char *value;
rc = mu_mime_header_parse (text, NULL, &value, NULL);
if (rc == 0)
{
str++;
size -= 2;
size_t size = strlen (value);
if (size > bufsz)
size = bufsz;
if (buf)
size = mu_cpystr (buf, value, size);
if (retsz)
*retsz = size;
}
if (buf)
size = mu_cpystr (buf, str, size);
if (retsz)
*retsz = size;
free (value);
return 0;
}
/* Same as mu_mimehdr_get_disp, but allocates memory */
int
mu_mimehdr_aget_disp (const char *str, char **pvalue)
mu_mimehdr_aget_disp (const char *text, char **pvalue)
{
char *p;
size_t size;
str = mu_str_skip_class (str, MU_CTYPE_BLANK);
size = disp_segment_len (str);
if (size > 2 && str[0] == '"' && str[size-1] == '"')
{
str++;
size -= 2;
}
p = malloc (size + 1);
if (!p)
return ENOMEM;
memcpy (p, str, size);
p[size] = 0;
*pvalue = p;
return 0;
return mu_mime_header_parse (text, NULL, pvalue, NULL);
}
/* Get the value of the parameter PARAM from STR, which must be
/* Get the value of parameter NAME from STR, which must be
a value of a structured MIME header.
At most BUFSZ-1 of data are stored in BUF. A terminating NUL
character is appended to it.
......@@ -419,202 +728,100 @@ mu_mimehdr_aget_disp (const char *str, char **pvalue)
Unless NULL, RETSZ is filled with the actual length of the
returned data (not including the NUL terminator).
Unless PFLAGS is null it will contain, on return, the flags describing
the parameter. The MU_MIMEHDR_MULTILINE bit is set if the parameter value
was multiline (RFC 2231.3). The MU_MIMEHDR_CSINFO bit is set if the
parameter value includes charset/language information (RFC 2231.4).
BUF may be NULL, in which case the function will only fill
RETSZ and PFLAGS, as described above. */
RETSZ, as described above. */
int
mu_mimehdr_get_param (const char *str, const char *param,
char *buf, size_t bufsz, size_t *retsz,
int *pflags)
mu_mimehdr_get_param (const char *str, const char *name,
char *buf, size_t bufsz, size_t *retsz)
{
return _header_get_param (str, NULL, param, buf, bufsz, NULL, retsz,
pflags);
int rc;
char *value;
rc = mu_mimehdr_aget_param (str, name, &value);
if (rc == 0)
{
size_t size = strlen (value);
if (size > bufsz)
size = bufsz;
if (buf)
size = mu_cpystr (buf, value, size);
if (retsz)
*retsz = size;
}
free (value);
return rc;
}
/* Same as mu_mimehdr_get_param, but allocates memory. */
int
mu_mimehdr_aget_param (const char *str, const char *param,
char **pval, int *pflags)
mu_mimehdr_aget_param (const char *str, const char *name, char **pval)
{
return _header_get_param (str, NULL, param, NULL, 0, pval, NULL, pflags);
return mu_mimehdr_aget_decoded_param (str, name, NULL, pval, NULL);
}
/* Decode a parameter value. Arguments:
Input:
VALUE Parameter value.
FLAGS Flags obtained from a previous call to one of the functions
above.
CHARSET Output charset.
Output:
PVAL A pointer to the decoded value is stored there.
The memory is allocated using malloc.
PLANG If language information was present in VALUE, its
malloc'ed copy is stored in the memory location pointed
to by this variable. If there was no language information,
*PLANG is set to NULL.
Both PVAL and PLANG may be NULL if that particular piece of information
is not needed. */
/* Similar to mu_mimehdr_aget_param, but the returned value is decoded
according to the CHARSET. Unless PLANG is NULL, it receives malloc'ed
language name from STR. If there was no language name, *PLANG is set
to NULL.
*/
int
mu_mimehdr_decode_param (const char *value, int flags,
const char *charset, char **pval, char **plang)
mu_mimehdr_aget_decoded_param (const char *str, const char *name,
const char *charset,
char **pval, char **plang)
{
char *decoded;
mu_assoc_t assoc;
int rc;
char *lang = NULL;
char *data;
if (flags == 0)
{
rc = mu_rfc2047_decode (charset, value, &decoded);
if (rc)
return rc;
}
else
rc = mu_mime_param_assoc_create (&assoc);
if (rc == 0)
{
rc = mu_str_url_decode (&decoded, value);
if (rc)
return rc;
if ((flags & MU_MIMEHDR_CSINFO)
&& (lang = strchr (decoded, '\''))
&& (data = strchr (lang + 1, '\'')))
rc = mu_mime_param_assoc_add (assoc, name);
if (rc == 0)
{
char *source_cs = decoded;
*lang++ = 0;
*data++ = 0;
lang = lang[0] ? strdup (lang) : NULL;
if (source_cs[0] && charset && mu_c_strcasecmp (source_cs, charset))
rc = mu_mime_header_parse_subset (str, charset, NULL, assoc);
if (rc == 0)
{
char *outval = NULL;
mu_stream_t instr = NULL;
mu_stream_t outstr = NULL;
mu_stream_t cvt = NULL;
char iobuf[512];
do
struct mu_mime_param *param = mu_assoc_ref (assoc, name);
if (!param)
rc = MU_ERR_NOENT;
else
{
size_t total = 0, pos;
size_t nbytes;
rc = mu_static_memory_stream_create (&instr, data,
strlen (data));
if (rc)
break;
rc = mu_memory_stream_create (&outstr, 0);
if (rc)
break;
rc = mu_decode_filter (&cvt, instr, NULL,
source_cs, charset);
if (rc)
break;
while (mu_stream_read (cvt, iobuf, sizeof (iobuf),
&nbytes) == 0
&& nbytes)
{
rc = mu_stream_write (outstr, iobuf, nbytes, NULL);
if (rc)
break;
total += nbytes;
}
if (rc)
break;
outval = malloc (total + 1);
if (!outval)
*pval = param->value;
if (plang)
{
rc = ENOMEM;
break;
*plang = param->lang;
param->lang = NULL;
}
mu_stream_seek (outstr, 0, MU_SEEK_SET, NULL);
pos = 0;
while (mu_stream_read (outstr, outval + pos,
total - pos, &nbytes) == 0
&& nbytes)
pos += nbytes;
outval[pos] = 0;
param->value = NULL;
}
while (0);
mu_stream_close (cvt);
mu_stream_destroy (&cvt);
mu_stream_close (instr);
mu_stream_destroy (&instr);
mu_stream_close (outstr);
mu_stream_destroy (&outstr);
free (decoded);
if (rc)
{
/* Cleanup after an error. */
free (lang);
free (outval);
return rc;
}
decoded = outval;
}
else
memmove (decoded, data, strlen (data) + 1);
}
}
if (pval)
*pval = decoded;
else
free (decoded);
if (plang)
*plang = lang;
return 0;
}
/* Similar to mu_mimehdr_aget_param, but the returned value is decoded
according to the CHARSET. Unless PLANG is NULL, it receives malloc'ed
language name from STR. If there was no language name, *PLANG is set
to NULL.
*/
int
mu_mimehdr_aget_decoded_param (const char *str, const char *param,
const char *charset,
char **pval, char **plang)
{
char *value;
int rc;
int flags;
rc = mu_mimehdr_aget_param (str, param, &value, &flags);
if (rc == 0)
{
rc = mu_mimehdr_decode_param (value, flags, charset, pval, plang);
free (value);
mu_assoc_destroy (&assoc);
}
return rc;
}
/* Get the attachment name from MSG. See _header_get_param, for a
description of the rest of arguments. */
/* Get the attachment name from a message.
Input:
MSG - The input message.
CHARSET - Character set to recode output values to. Can be NULL.
Output:
PBUF - Output value.
PSZ - Its size in bytes, not counting the terminating zero.
PLANG - Language the name is written in, if provided in the header.
Either PSZ or PLAN (or both) can be NULL.
*/
static int
_get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
char **pbuf, size_t *sz, int *pflags)
_get_attachment_name (mu_message_t msg, const char *charset,
char **pbuf, size_t *psz, char **plang)
{
int ret = EINVAL;
mu_header_t hdr;
char *value = NULL;
mu_assoc_t assoc;
if (!msg)
return ret;
......@@ -622,7 +829,8 @@ _get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
if ((ret = mu_message_get_header (msg, &hdr)) != 0)
return ret;
ret = mu_header_aget_value_unfold (hdr, "Content-Disposition", &value);
ret = mu_header_aget_value_unfold (hdr, MU_HEADER_CONTENT_DISPOSITION,
&value);
/* If the header wasn't there, we'll fall back to Content-Type, but
other errors are fatal. */
......@@ -631,33 +839,88 @@ _get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
if (ret == 0 && value != NULL)
{
ret = _header_get_param (value, "attachment",
"filename", buf, bufsz, pbuf, sz, pflags);
free (value);
value = NULL;
if (ret == 0 || ret != MU_ERR_NOENT)
ret = mu_mime_param_assoc_create (&assoc);
if (ret)
return ret;
ret = mu_mime_param_assoc_add (assoc, "filename");
if (ret == 0)
{
char *disp;
ret = mu_mime_header_parse_subset (value, charset, &disp, assoc);
if (ret == 0)
{
struct mu_mime_param *param;
if (mu_c_strcasecmp (disp, "attachment") == 0 &&
(param = mu_assoc_ref (assoc, "filename")))
{
*pbuf = param->value;
if (psz)
*psz = strlen (*pbuf);
param->value = NULL;
if (plang)
{
*plang = param->lang;
param->lang = NULL;
}
}
else
ret = MU_ERR_NOENT;
free (disp);
mu_assoc_destroy (&assoc);
}
}
}
free (value);
if (ret == 0)
return ret;
/* If we didn't get the name, we fall back on the Content-Type name
parameter. */
free (value);
ret = mu_header_aget_value_unfold (hdr, "Content-Type", &value);
ret = mu_header_aget_value_unfold (hdr, MU_HEADER_CONTENT_TYPE, &value);
if (ret == 0)
ret = _header_get_param (value, NULL, "name", buf, bufsz, pbuf, sz,
pflags);
free (value);
{
ret = mu_mime_param_assoc_create (&assoc);
if (ret)
return ret;
ret = mu_mime_param_assoc_add (assoc, "name");
if (ret == 0)
{
ret = mu_mime_header_parse_subset (value, charset, NULL, assoc);
if (ret == 0)
{
struct mu_mime_param *param;
if ((param = mu_assoc_ref (assoc, "name")))
{
*pbuf = param->value;
if (psz)
*psz = strlen (*pbuf);
param->value = NULL;
if (plang)
{
*plang = param->lang;
param->lang = NULL;
}
}
else
ret = MU_ERR_NOENT;
}
}
free (value);
}
return ret;
}
int
mu_message_aget_attachment_name (mu_message_t msg, char **name, int *pflags)
mu_message_aget_attachment_name (mu_message_t msg, char **name)
{
if (name == NULL)
return MU_ERR_OUT_PTR_NULL;
return _get_attachment_name (msg, NULL, 0, name, NULL, pflags);
return _get_attachment_name (msg, NULL, name, NULL, NULL);
}
int
......@@ -666,21 +929,28 @@ mu_message_aget_decoded_attachment_name (mu_message_t msg,
char **pval,
char **plang)
{
char *value;
int flags;
int rc = mu_message_aget_attachment_name (msg, &value, &flags);
if (rc == 0)
{
rc = mu_mimehdr_decode_param (value, flags, charset, pval, plang);
free (value);
}
return rc;
if (pval == NULL)
return MU_ERR_OUT_PTR_NULL;
return _get_attachment_name (msg, charset, pval, NULL, plang);
}
int
mu_message_get_attachment_name (mu_message_t msg, char *buf, size_t bufsz,
size_t *sz, int *pflags)
size_t *sz)
{
return _get_attachment_name (msg, buf, bufsz, NULL, sz, pflags);
char *tmp;
size_t size;
int rc = _get_attachment_name (msg, NULL, &tmp, &size, NULL);
if (rc == 0)
{
if (size > bufsz)
size = bufsz;
if (buf)
size = mu_cpystr (buf, tmp, size);
if (sz)
*sz = size;
}
free (tmp);
return rc;
}
......
......@@ -16,6 +16,7 @@ fsfolder
imapio
listop
mailcap
mimehdr
prop
scantime
strftime
......
......@@ -51,6 +51,7 @@ noinst_PROGRAMS = \
imapio\
listop\
mailcap\
mimehdr\
prop\
scantime\
strftime\
......@@ -88,6 +89,7 @@ TESTSUITE_AT = \
linecon.at\
list.at\
mailcap.at\
mimehdr.at\
prop.at\
scantime.at\
strftime.at\
......
# This file is part of GNU Mailutils. -*- Autotest -*-
# Copyright (C) 2011 Free Software Foundation, Inc.
#
# GNU Mailutils is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, 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
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
# Warning: This text contains 8-bit UTF-8
AT_BANNER(RFC 2231 header fields)
dnl ---------------------------------------------------------------------
dnl MIMEHDR([NAME], [KW], [OPT], [INPUT], [STDOUT = `'], [STDERR = `'])
dnl
m4_pushdef([MIMEHDR],[
m4_pushdef([MU_TEST_GROUP],[mimehdr])
m4_pushdef([MU_TEST_KEYWORDS],[mimehdr rfc2231])
m4_pushdef([MU_TEST_COMMAND],[mimehdr $3])
MU_GENERIC_TEST([$1],[$2],[$4],[],[$5],[$6])
m4_popdef([MU_TEST_COMMAND])
m4_popdef([MU_TEST_KEYWORDS])
m4_popdef([MU_TEST_GROUP])
])
dnl ---------------------------------------------------------------------
MIMEHDR([simple],[mimehdr00 mimehdr-simple],
[],
[message/external-body; access-type=URL;
URL="ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
],
[message/external-body
access-type=URL
URL=ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar
])
MIMEHDR([continuation],[mimehdr01 mimehdr-cont mimehdr-cont-00],
[],
[message/external-body; access-type=URL;
URL*0="ftp://";
URL*1="cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar"
],
[message/external-body
access-type=URL
URL=ftp://cs.utk.edu/pub/moore/bulk-mailer/bulk-mailer.tar
])
MIMEHDR([charset (2047)],[mimehdr02 mimehdr-charset-rfc2047 mimehdr-charset-00],
[],
[attachment; charset=utf-8;
filename==?UTF-8?B?zrHPgc+HzrXOr86/IM6zzrnOsSDPhM63zr0gzrTOv866zrnOvM6xz4POr86x==?=
],
[attachment
charset=utf-8
filename=αρχείο για την δοκιμασία
])
MIMEHDR([charset with language (2047)],[mimehdr03 mimehdr-charset-rfc2047 mimehdr-charset-01],
[],
[attachment; charset=utf-8;
filename==?UTF-8*el?B?zrHPgc+HzrXOr86/IM6zzrnOsSDPhM63zr0gzrTOv866zrnOvM6xz4POr86x==?=
],
[attachment
charset=utf-8
filename(lang:el/UTF-8)=αρχείο για την δοκιμασία
])
MIMEHDR([no charset (2231)],[mimehdr04 mimehdr-no-charset-rfc2231 mimehdr-nocharset-00],
[],
[attachment; charset=utf-8;
filename*=%CE%B1%CF%81%CF%87%CE%B5%CE%AF%CE%BF%20%CE%B3%CE%B9%CE%B1%20%CF%84%CE%B7%CE%BD%20%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
],
[attachment
charset=utf-8
filename=αρχείο για την δοκιμασία
])
MIMEHDR([charset (2231)],[mimehdr05 mimehdr-charset-rfc2231 mimehdr-charset-rfc2231-00 mimehdr-charset-03],
[],
[attachment; charset=utf-8;
filename*=UTF-8''%CE%B1%CF%81%CF%87%CE%B5%CE%AF%CE%BF%20%CE%B3%CE%B9%CE%B1%20%CF%84%CE%B7%CE%BD%20%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
],
[attachment
charset=utf-8
filename(lang:/UTF-8)=αρχείο για την δοκιμασία
])
MIMEHDR([charset with language (2231)],[mimehdr06 mimehdr-charset-rfc2231 mimehdr-charset-rfc2231-01 mimehdr-charset-04],
[],
[attachment; charset=utf-8;
filename*=UTF-8'el_GR'%CE%B1%CF%81%CF%87%CE%B5%CE%AF%CE%BF%20%CE%B3%CE%B9%CE%B1%20%CF%84%CE%B7%CE%BD%20%CE%B4%CE%BF%CE%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
],
[attachment
charset=utf-8
filename(lang:el_GR/UTF-8)=αρχείο για την δοκιμασία
])
MIMEHDR([charset with language and continuation (2231)],[mimehdr07 mimehdr-charset-rfc2231 mimehdr-charset-rfc2231-02 mimehdr-charset-05],
[],
[attachment; charset=utf-8;
filename*00*=UTF-8'el_GR'%CE%B1%CF%81%CF%87%CE%B5;
filename*01*=%CE%AF%CE%BF%20%CE%B3%CE;
filename*02*=%B9%CE%B1%20%CF%84%CE%B7;
filename*03*=%CE%BD%20%CE%B4%CE%BF%CE;
filename*04*=%BA%CE%B9%CE%BC%CE%B1%CF%83%CE%AF%CE%B1
],
[attachment
charset=utf-8
filename(lang:el_GR/UTF-8)=αρχείο για την δοκιμασία
])
MIMEHDR([combined charset, lang and cset],[mimehdr08 mimehdr-comb mimehdr-charset-rfc2231],
[],
[application/x-stuff
title*0*=us-ascii'en'This%20is%20even%20more%20
title*1*=%2A%2A%2Afun%2A%2A%2A%20
title*2="isn't it!"
],
[application/x-stuff
title(lang:en/us-ascii)=This is even more ***fun*** isn't it!
])
m4_popdef([MIMEHDR])
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2005, 2007, 2009, 2010, 2011 Free Software Foundation,
Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, 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 General Public License for more details.
You should have received a copy of the GNU 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/assoc.h>
#include <mailutils/header.h>
#include <mailutils/message.h>
#include <mailutils/mime.h>
#include <mailutils/iterator.h>
#include <mailutils/stream.h>
#include <mailutils/stdstream.h>
#include <mailutils/util.h>
#include <mailutils/cstr.h>
#include <mailutils/cctype.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
struct named_param
{
const char *name;
struct mu_mime_param const *param;
};
static int
sort_names (void const *a, void const *b)
{
struct named_param const *pa = a;
struct named_param const *pb = b;
return mu_c_strcasecmp (pa->name, pb->name);
}
static int
print_named_param (void *item, void *data)
{
struct named_param const *p = item;
struct mu_mime_param const *param = p->param;
mu_printf ("%s", p->name);
if (param->lang)
mu_printf ("(lang:%s/%s)", param->lang, param->cset);
mu_printf ("=%s\n", param->value);
return 0;
}
int
main (int argc, char **argv)
{
int i;
mu_stream_t tmp;
mu_transport_t trans[2];
char *value;
mu_assoc_t assoc;
mu_iterator_t itr;
mu_list_t list;
char *charset = NULL;
mu_set_program_name (argv[0]);
for (i = 1; i < argc; i++)
{
char *opt = argv[i];
if (strncmp (opt, "-debug=", 7) == 0)
mu_debug_parse_spec (opt + 7);
else if (strncmp (opt, "-charset=", 9) == 0)
charset = opt + 9;
else if (strcmp (opt, "-h") == 0 || strcmp (opt, "-help") == 0)
{
mu_printf ("usage: %s [-charset=cs] [-debug=SPEC]", mu_program_name);
return 0;
}
else
{
mu_error ("unknown option %s", opt);
return 1;
}
}
if (i != argc)
{
mu_error ("too many arguments");
return 1;
}
MU_ASSERT (mu_memory_stream_create (&tmp, MU_STREAM_RDWR));
MU_ASSERT (mu_stream_copy (tmp, mu_strin, 0, NULL));
MU_ASSERT (mu_stream_write (tmp, "", 1, NULL));
MU_ASSERT (mu_stream_ioctl (tmp, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET,
trans));
MU_ASSERT (mu_mime_header_parse ((char*)trans[0], charset, &value, &assoc));
mu_printf ("%s\n", value);
MU_ASSERT (mu_list_create (&list));
MU_ASSERT (mu_assoc_get_iterator (assoc, &itr));
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
const char *name;
struct mu_mime_param *p;
struct named_param *np;
mu_iterator_current_kv (itr, (const void **)&name, (void**)&p);
np = malloc (sizeof (*np));
if (!np)
abort ();
np->name = name;
np->param = p;
MU_ASSERT (mu_list_append (list, np));
}
mu_iterator_destroy (&itr);
mu_list_sort (list, sort_names);
mu_list_foreach (list, print_named_param, NULL);
return 0;
}
......@@ -96,4 +96,6 @@ m4_include([imapio.at])
m4_include([scantime.at])
m4_include([strftime.at])
m4_include([fsaf.at])
\ No newline at end of file
m4_include([fsaf.at])
m4_include([mimehdr.at])
......