Encode a string to be suitable for an RFC2231 header.
If the value is large, the list will contain continuation lines.
355{
356 if (!attribute || !value)
357 return 0;
358
359 size_t count = 0;
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;
368
371
372
373 for (cur = value; *cur; cur++)
374 {
375 if ((*cur < 0x20) || (*cur >= 0x7f))
376 {
378 break;
379 }
380 }
381
383 {
386 if (c_charset && c_send_charset)
387 {
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
402
403 for (cur = src_value; *cur; cur++)
404 {
405 dest_value_len++;
406
408 {
409
410 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
411 strchr("*'%", *cur))
412 {
413 dest_value_len += 2;
414 }
415 }
416 else
417 {
418
420 add_quotes = true;
421
422 if ((*cur == '\\') || (*cur == '"'))
423 dest_value_len++;
424 }
425 }
426
427
428 max_value_len = 78 -
429 1 -
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;
443
444 }
445
446
447 cur = src_value;
449 {
451 cur_value_len =
buf_len(cur_value);
452 }
453
454 while (*cur)
455 {
459
461 if (split)
465
466 while (*cur && (!split || (cur_value_len < max_value_len)))
467 {
469 {
470 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
471 strchr("*'%", *cur))
472 {
474 cur_value_len += 3;
475 }
476 else
477 {
479 cur_value_len++;
480 }
481 }
482 else
483 {
485 cur_value_len++;
486 if ((*cur == '\\') || (*cur == '"'))
487 cur_value_len++;
488 }
489
490 cur++;
491 }
492
495
497 cur_value_len = 0;
498 }
499
502
504 if (free_src_value)
506
508}
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.
char * mutt_str_dup(const char *str)
Copy a string, safely.
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
struct Parameter * mutt_param_new(void)
Create a new Parameter.
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.