bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2008-2018 Dovecot authors, see the included COPYING file */
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainenstatic int rfc2231_parameter_cmp(const struct rfc2231_parameter *r1,
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainenstatic void rfc2231_escape(string_t *dest, const char *src)
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainenint rfc2231_parse(struct rfc822_parser_context *ctx,
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen const char *const **result_r)
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(struct rfc2231_parameter) rfc2231_params_arr;
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen const struct rfc2231_parameter *rfc2231_params;
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* Get a list of all parameters. RFC 2231 uses key*<n>[*]=value pairs,
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen which we want to merge to a key[*]=value pair. Save them to a
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen separate array. */
7a62083331fbbb73121d16d6e13fc3dbc5720aa7Timo Sirainen while ((ret = rfc822_parse_content_param(ctx, &key, &value)) != 0) {
7a62083331fbbb73121d16d6e13fc3dbc5720aa7Timo Sirainen /* try to continue anyway.. */
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen if (*p != '*')
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen if (*p != '\0')
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* No RFC 2231 parameters */
31a574fda352ef4f71dbff9c30e15e4744e132c0Timo Sirainen array_append_zero(&result); /* NULL-terminate */
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* Merge the RFC 2231 parameters. Since their order isn't guaranteed to
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen be ascending, start by sorting them. */
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen array_sort(&rfc2231_params_arr, rfc2231_parameter_cmp);
c9dea5c23355dea35c6fa423de69f6507852efe4Timo Sirainen rfc2231_params = array_get(&rfc2231_params_arr, &count);
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* keys are now sorted primarily by their name and secondarily by
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen their index. If any indexes are missing, fallback to assuming
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen these aren't RFC 2231 encoded parameters. */
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen for (j = i; j < count; j++) {
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* missing indexes */
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* missing indexes */
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen for (j = i; j < next; j++) {
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen /* everything was successful */
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen if (!rfc2231_params[i].extended && have_extended)
dabb4cec4cf9bdb34013de682b08f1284cfb670fTimo Sirainen for (j = i; j < next; j++) {
31a574fda352ef4f71dbff9c30e15e4744e132c0Timo Sirainen array_append_zero(&result); /* NULL-terminate */