NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
rfc2231.c
Go to the documentation of this file.
1
25
37
38#include "config.h"
39#include <limits.h>
40#include <stdbool.h>
41#include <string.h>
42#include "mutt/lib.h"
43#include "config/lib.h"
44#include "core/lib.h"
45#include "rfc2231.h"
46#include "mime.h"
47#include "parameter.h"
48#include "rfc2047.h"
49
54{
55 char *attribute;
56 char *value;
57 int index;
58 bool encoded;
60};
61
66static void purge_empty_parameters(struct ParameterList *pl)
67{
68 struct Parameter *np = NULL, *tmp = NULL;
69 TAILQ_FOREACH_SAFE(np, pl, entries, tmp)
70 {
71 if (!np->attribute || !np->value)
72 {
73 TAILQ_REMOVE(pl, np, entries);
75 }
76 }
77}
78
86static char *get_charset(char *value, char *charset, size_t chslen)
87{
88 char *t = strchr(value, '\'');
89 if (!t)
90 {
91 charset[0] = '\0';
92 return value;
93 }
94
95 *t = '\0';
96 mutt_str_copy(charset, value, chslen);
97
98 char *u = strchr(t + 1, '\'');
99 if (u)
100 return u + 1;
101 return t + 1;
102}
103
109static void decode_one(char *dest, char *src)
110{
111 char *d = NULL;
112
113 for (d = dest; *src; src++)
114 {
115 if ((src[0] == '%') && mutt_isxdigit(src[1]) && mutt_isxdigit(src[2]))
116 {
117 *d++ = (hexval(src[1]) << 4) | hexval(src[2]);
118 src += 2;
119 }
120 else
121 {
122 *d++ = *src;
123 }
124 }
125
126 *d = '\0';
127}
128
133static struct Rfc2231Parameter *parameter_new(void)
134{
135 return MUTT_MEM_CALLOC(1, struct Rfc2231Parameter);
136}
137
146static void list_insert(struct Rfc2231Parameter **list, struct Rfc2231Parameter *par)
147{
148 struct Rfc2231Parameter **last = list;
149 struct Rfc2231Parameter *p = *list;
150
151 while (p)
152 {
153 const int c = mutt_str_cmp(par->attribute, p->attribute);
154 if ((c < 0) || ((c == 0) && (par->index <= p->index)))
155 break;
156
157 last = &p->next;
158 p = p->next;
159 }
160
161 par->next = p;
162 *last = par;
163}
164
169static void parameter_free(struct Rfc2231Parameter **ptr)
170{
171 if (!ptr || !*ptr)
172 return;
173
174 struct Rfc2231Parameter *p = *ptr;
175 FREE(&p->attribute);
176 FREE(&p->value);
177
178 FREE(ptr);
179}
180
186static void join_continuations(struct ParameterList *pl, struct Rfc2231Parameter *par)
187{
188 char attribute[256] = { 0 };
189 char charset[256] = { 0 };
190
191 const char *const c_charset = cc_charset();
192 while (par)
193 {
194 char *value = NULL;
195 size_t l = 0;
196
198
199 const bool encoded = par->encoded;
200 char *valp = NULL;
201 if (encoded)
202 valp = get_charset(par->value, charset, sizeof(charset));
203 else
204 valp = par->value;
205
206 do
207 {
208 if (encoded && par->encoded)
209 decode_one(par->value, valp);
210
211 const size_t vl = strlen(par->value);
212
213 MUTT_MEM_REALLOC(&value, l + vl + 1, char);
214 strcpy(value + l, par->value);
215 l += vl;
216
217 struct Rfc2231Parameter *q = par->next;
218 parameter_free(&par);
219 par = q;
220 if (par)
221 valp = par->value;
222 } while (par && (mutt_str_equal(par->attribute, attribute)));
223
224 if (encoded)
225 {
228 }
229
230 struct Parameter *np = mutt_param_new();
232 np->value = value;
233 TAILQ_INSERT_HEAD(pl, np, entries);
234 }
235}
236
241void rfc2231_decode_parameters(struct ParameterList *pl)
242{
243 if (!pl)
244 return;
245
246 struct Rfc2231Parameter *conthead = NULL;
247 struct Rfc2231Parameter *conttmp = NULL;
248
249 char *s = NULL, *t = NULL;
250 char charset[256] = { 0 };
251
252 bool encoded;
253 int index;
254 bool dirty = false; /* set when we may have created empty parameters. */
255
257
258 struct Parameter *np = NULL, *tmp = NULL;
259 const bool c_rfc2047_parameters = cs_subset_bool(NeoMutt->sub, "rfc2047_parameters");
260 const struct Slist *c_assumed_charset = cc_assumed_charset();
261 const char *c_charset = cc_charset();
262
263 TAILQ_FOREACH_SAFE(np, pl, entries, tmp)
264 {
265 s = strchr(np->attribute, '*');
266 if (!s)
267 {
268 /* Single value, non encoded:
269 * attr=value
270 */
271 /* Using RFC2047 encoding in MIME parameters is explicitly
272 * forbidden by that document. Nevertheless, it's being
273 * generated by some software, including certain Lotus Notes to
274 * Internet Gateways. So we actually decode it. */
275
276 if (c_rfc2047_parameters && np->value && strstr(np->value, "=?"))
277 {
278 rfc2047_decode(&np->value);
279 }
280 else if (!slist_is_empty(c_assumed_charset))
281 {
282 mutt_ch_convert_nonmime_string(c_assumed_charset, c_charset, &np->value);
283 }
284 }
285 else if (s[1] == '\0')
286 {
287 /* Single value with encoding:
288 * attr*=us-ascii''the%20value
289 */
290 s[0] = '\0';
291
292 s = get_charset(np->value, charset, sizeof(charset));
293 decode_one(np->value, s);
294 mutt_ch_convert_string(&np->value, charset, c_charset, MUTT_ICONV_HOOK_FROM);
296 dirty = true;
297 }
298 else
299 {
300 /* A parameter continuation, which may or may not be encoded:
301 * attr*0=value
302 * -or-
303 * attr*0*=us-ascii''the%20value
304 */
305 s[0] = '\0';
306 s++; /* let s point to the first character of index. */
307 for (t = s; (t[0] != '\0') && mutt_isdigit(t[0]); t++)
308 ; // do nothing
309
310 encoded = (t[0] == '*');
311 t[0] = '\0';
312
313 /* RFC2231 says that the index starts at 0 and increments by 1,
314 * thus an overflow should never occur in a valid message, thus
315 * the value INT_MAX in case of overflow does not really matter
316 * (the goal is just to avoid undefined behaviour). */
317 if (!mutt_str_atoi_full(s, &index))
318 index = INT_MAX;
319
320 conttmp = parameter_new();
321 conttmp->attribute = np->attribute;
322 conttmp->value = np->value;
323 conttmp->encoded = encoded;
324 conttmp->index = index;
325
326 np->attribute = NULL;
327 np->value = NULL;
328 TAILQ_REMOVE(pl, np, entries);
329 FREE(&np);
330
331 list_insert(&conthead, conttmp);
332 }
333 }
334
335 if (conthead)
336 {
337 join_continuations(pl, conthead);
338 dirty = true;
339 }
340
341 if (dirty)
343}
344
354size_t rfc2231_encode_string(struct ParameterList *head, const char *attribute, char *value)
355{
356 if (!attribute || !value)
357 return 0;
358
359 size_t count = 0;
360 bool encode = false;
361 bool add_quotes = false;
362 bool free_src_value = false;
363 bool split = false;
364 int continuation_number = 0;
365 size_t dest_value_len = 0, max_value_len = 0, cur_value_len = 0;
366 char *cur = NULL, *charset = NULL, *src_value = NULL;
367 struct Parameter *current = NULL;
368
369 struct Buffer *cur_attribute = buf_pool_get();
370 struct Buffer *cur_value = buf_pool_get();
371
372 // Perform charset conversion
373 for (cur = value; *cur; cur++)
374 {
375 if ((*cur < 0x20) || (*cur >= 0x7f))
376 {
377 encode = true;
378 break;
379 }
380 }
381
382 if (encode)
383 {
384 const struct Slist *const c_send_charset = cs_subset_slist(NeoMutt->sub, "send_charset");
385 const char *c_charset = cc_charset();
386 if (c_charset && c_send_charset)
387 {
388 charset = mutt_ch_choose(c_charset, c_send_charset, value,
389 mutt_str_len(value), &src_value, NULL);
390 }
391 if (src_value)
392 free_src_value = true;
393 if (!charset)
394 charset = mutt_str_dup(c_charset ? c_charset : "unknown-8bit");
395 }
396 if (!src_value)
397 src_value = value;
398
399 // Count the size the resultant value will need in total
400 if (encode)
401 dest_value_len = mutt_str_len(charset) + 2; /* charset'' prefix */
402
403 for (cur = src_value; *cur; cur++)
404 {
405 dest_value_len++;
406
407 if (encode)
408 {
409 /* These get converted to %xx so need a total of three chars */
410 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(MimeSpecials, *cur) ||
411 strchr("*'%", *cur))
412 {
413 dest_value_len += 2;
414 }
415 }
416 else
417 {
418 /* rfc822_cat() will add outer quotes if it finds MimeSpecials. */
419 if (!add_quotes && strchr(MimeSpecials, *cur))
420 add_quotes = true;
421 /* rfc822_cat() will add a backslash if it finds '\' or '"'. */
422 if ((*cur == '\\') || (*cur == '"'))
423 dest_value_len++;
424 }
425 }
426
427 // Determine if need to split into parameter value continuations
428 max_value_len = 78 - // rfc suggested line length
429 1 - // Leading tab on continuation line
430 mutt_str_len(attribute) - // attribute
431 (encode ? 1 : 0) - // '*' encoding marker
432 1 - // '='
433 (add_quotes ? 2 : 0) - // "...."
434 1; // ';'
435
436 if (max_value_len < 30)
437 max_value_len = 30;
438
439 if (dest_value_len > max_value_len)
440 {
441 split = true;
442 max_value_len -= 4; /* '*n' continuation number and extra encoding
443 * space to keep loop below simpler */
444 }
445
446 // Generate list of parameter continuations
447 cur = src_value;
448 if (encode)
449 {
450 buf_printf(cur_value, "%s''", charset);
451 cur_value_len = buf_len(cur_value);
452 }
453
454 while (*cur)
455 {
456 current = mutt_param_new();
457 TAILQ_INSERT_TAIL(head, current, entries);
458 count++;
459
460 buf_strcpy(cur_attribute, attribute);
461 if (split)
462 buf_add_printf(cur_attribute, "*%d", continuation_number++);
463 if (encode)
464 buf_addch(cur_attribute, '*');
465
466 while (*cur && (!split || (cur_value_len < max_value_len)))
467 {
468 if (encode)
469 {
470 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(MimeSpecials, *cur) ||
471 strchr("*'%", *cur))
472 {
473 buf_add_printf(cur_value, "%%%02X", (unsigned char) *cur);
474 cur_value_len += 3;
475 }
476 else
477 {
478 buf_addch(cur_value, *cur);
479 cur_value_len++;
480 }
481 }
482 else
483 {
484 buf_addch(cur_value, *cur);
485 cur_value_len++;
486 if ((*cur == '\\') || (*cur == '"'))
487 cur_value_len++;
488 }
489
490 cur++;
491 }
492
493 current->attribute = buf_strdup(cur_attribute);
494 current->value = buf_strdup(cur_value);
495
496 buf_reset(cur_value);
497 cur_value_len = 0;
498 }
499
500 buf_pool_release(&cur_attribute);
501 buf_pool_release(&cur_value);
502
503 FREE(&charset);
504 if (free_src_value)
505 FREE(&src_value);
506
507 return count;
508}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
Definition helpers.c:242
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
const char * cc_charset(void)
Get the cached value of $charset.
const struct Slist * cc_assumed_charset(void)
Get the cached value of $assumed_charset.
Convenience wrapper for the core headers.
bool mutt_isxdigit(int arg)
Wrapper for isxdigit(3)
Definition ctype.c:111
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition ctype.c:66
int mutt_mb_filter_unprintable(char **s)
Replace unprintable characters.
Definition mbyte.c:424
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
#define MUTT_MEM_REALLOC(pptr, n, type)
Definition memory.h:55
const char MimeSpecials[]
Characters that need special treatment in MIME.
Definition mime.c:67
Constants and macros for managing MIME encoding.
#define hexval(ch)
Convert hexadecimal character to its integer value.
Definition mime.h:82
char * mutt_ch_choose(const char *fromcode, const struct Slist *charsets, const char *u, size_t ulen, char **d, size_t *dlen)
Figure the best charset to encode a string.
Definition charset.c:1094
int mutt_ch_convert_nonmime_string(const struct Slist *const assumed_charset, const char *charset, char **ps)
Try to convert a string using a list of character sets.
Definition charset.c:317
int mutt_ch_convert_string(char **ps, const char *from, const char *to, uint8_t flags)
Convert a string between encodings.
Definition charset.c:817
#define MUTT_ICONV_HOOK_FROM
apply charset-hooks to fromcode
Definition charset.h:67
Convenience wrapper for the library headers.
bool slist_is_empty(const struct Slist *list)
Is the slist empty?
Definition slist.c:140
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition string.c:403
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:583
void mutt_param_free_one(struct Parameter **p)
Free a Parameter.
Definition parameter.c:49
struct Parameter * mutt_param_new(void)
Create a new Parameter.
Definition parameter.c:40
Store attributes associated with a MIME part.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:792
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:866
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:901
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition queue.h:853
void rfc2047_decode(char **pd)
Decode any RFC2047-encoded header fields.
Definition rfc2047.c:665
static int encode(const char *d, size_t dlen, int col, const char *fromcode, const struct Slist *charsets, char **e, size_t *elen, const char *specials)
RFC2047-encode a string.
Definition rfc2047.c:430
RFC2047 MIME extensions encoding / decoding routines.
static void purge_empty_parameters(struct ParameterList *pl)
Remove any ill-formed Parameters from a list.
Definition rfc2231.c:66
static void join_continuations(struct ParameterList *pl, struct Rfc2231Parameter *par)
Process continuation parameters.
Definition rfc2231.c:186
static char * get_charset(char *value, char *charset, size_t chslen)
Get the charset from an RFC2231 header.
Definition rfc2231.c:86
static void parameter_free(struct Rfc2231Parameter **ptr)
Free an Rfc2231Parameter.
Definition rfc2231.c:169
static void list_insert(struct Rfc2231Parameter **list, struct Rfc2231Parameter *par)
Insert parameter into an ordered list.
Definition rfc2231.c:146
void rfc2231_decode_parameters(struct ParameterList *pl)
Decode a Parameter list.
Definition rfc2231.c:241
static struct Rfc2231Parameter * parameter_new(void)
Create a new Rfc2231Parameter.
Definition rfc2231.c:133
size_t rfc2231_encode_string(struct ParameterList *head, const char *attribute, char *value)
Encode a string to be suitable for an RFC2231 header.
Definition rfc2231.c:354
static void decode_one(char *dest, char *src)
Decode one percent-encoded character.
Definition rfc2231.c:109
RFC2231 MIME Charset routines.
String manipulation buffer.
Definition buffer.h:36
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
Attribute associated with a MIME part.
Definition parameter.h:33
char * attribute
Parameter name.
Definition parameter.h:34
char * value
Parameter value.
Definition parameter.h:35
MIME section parameter.
Definition rfc2231.c:54
bool encoded
Is the value encoded?
Definition rfc2231.c:58
int index
Index number in the list.
Definition rfc2231.c:57
char * value
Attribute value.
Definition rfc2231.c:56
struct Rfc2231Parameter * next
Linked list.
Definition rfc2231.c:59
char * attribute
Attribute name.
Definition rfc2231.c:55
String list.
Definition slist.h:37
struct ListHead head
List containing values.
Definition slist.h:38
size_t count
Number of values in list.
Definition slist.h:39