Encode a string to be suitable for an RFC2231 header.
If the value is large, the list will contain continuation lines.
356{
357 if (!attribute || !value)
358 return 0;
359
360 size_t count = 0;
362 bool add_quotes = false;
363 bool free_src_value = false;
364 bool split = false;
365 int continuation_number = 0;
366 size_t dest_value_len = 0, max_value_len = 0, cur_value_len = 0;
367 char *cur = NULL, *charset = NULL, *src_value = NULL;
369
372
373
374 for (cur = value; *cur; cur++)
375 {
376 if ((*cur < 0x20) || (*cur >= 0x7f))
377 {
379 break;
380 }
381 }
382
384 {
387 if (c_charset && c_send_charset)
388 {
391 }
392 if (src_value)
393 free_src_value = true;
394 if (!charset)
395 charset =
mutt_str_dup(c_charset ? c_charset :
"unknown-8bit");
396 }
397 if (!src_value)
398 src_value = value;
399
400
403
404 for (cur = src_value; *cur; cur++)
405 {
406 dest_value_len++;
407
409 {
410
411 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
412 strchr("*'%", *cur))
413 {
414 dest_value_len += 2;
415 }
416 }
417 else
418 {
419
421 add_quotes = true;
422
423 if ((*cur == '\\') || (*cur == '"'))
424 dest_value_len++;
425 }
426 }
427
428
429 max_value_len = 78 -
430 1 -
433 1 -
434 (add_quotes ? 2 : 0) -
435 1;
436
437 if (max_value_len < 30)
438 max_value_len = 30;
439
440 if (dest_value_len > max_value_len)
441 {
442 split = true;
443 max_value_len -= 4;
444
445 }
446
447
448 cur = src_value;
450 {
452 cur_value_len =
buf_len(cur_value);
453 }
454
455 while (*cur)
456 {
460
462 if (split)
466
467 while (*cur && (!split || (cur_value_len < max_value_len)))
468 {
470 {
471 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
472 strchr("*'%", *cur))
473 {
475 cur_value_len += 3;
476 }
477 else
478 {
480 cur_value_len++;
481 }
482 }
483 else
484 {
486 cur_value_len++;
487 if ((*cur == '\\') || (*cur == '"'))
488 cur_value_len++;
489 }
490
491 cur++;
492 }
493
496
498 cur_value_len = 0;
499 }
500
503
505 if (free_src_value)
507
509}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
const struct Slist * cs_subset_slist(const struct ConfigSubset *sub, const char *name)
Get a string-list config item by name.
const char MimeSpecials[]
Characters that need special treatment in MIME.
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.
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
#define TAILQ_INSERT_TAIL(head, elm, field)
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.
String manipulation buffer.
struct ListHead head
List containing values.
size_t count
Number of values in list.