Encode a string to be suitable for an RFC2231 header.
If the value is large, the list will contain continuation lines.
352{
353 if (!attribute || !value)
354 return 0;
355
356 size_t count = 0;
358 bool add_quotes = false;
359 bool free_src_value = false;
360 bool split = false;
361 int continuation_number = 0;
362 size_t dest_value_len = 0, max_value_len = 0, cur_value_len = 0;
363 char *cur = NULL, *charset = NULL, *src_value = NULL;
365
368
369
370 for (cur = value; *cur; cur++)
371 {
372 if ((*cur < 0x20) || (*cur >= 0x7f))
373 {
375 break;
376 }
377 }
378
380 {
383 if (c_charset && c_send_charset)
384 {
387 }
388 if (src_value)
389 free_src_value = true;
390 if (!charset)
391 charset =
mutt_str_dup(c_charset ? c_charset :
"unknown-8bit");
392 }
393 if (!src_value)
394 src_value = value;
395
396
399
400 for (cur = src_value; *cur; cur++)
401 {
402 dest_value_len++;
403
405 {
406
407 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
408 strchr("*'%", *cur))
409 {
410 dest_value_len += 2;
411 }
412 }
413 else
414 {
415
417 add_quotes = true;
418
419 if ((*cur == '\\') || (*cur == '"'))
420 dest_value_len++;
421 }
422 }
423
424
425 max_value_len = 78 -
426 1 -
429 1 -
430 (add_quotes ? 2 : 0) -
431 1;
432
433 if (max_value_len < 30)
434 max_value_len = 30;
435
436 if (dest_value_len > max_value_len)
437 {
438 split = true;
439 max_value_len -= 4;
440
441 }
442
443
444 cur = src_value;
446 {
448 cur_value_len =
buf_len(cur_value);
449 }
450
451 while (*cur)
452 {
456
458 if (split)
462
463 while (*cur && (!split || (cur_value_len < max_value_len)))
464 {
466 {
467 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
468 strchr("*'%", *cur))
469 {
471 cur_value_len += 3;
472 }
473 else
474 {
476 cur_value_len++;
477 }
478 }
479 else
480 {
482 cur_value_len++;
483 if ((*cur == '\\') || (*cur == '"'))
484 cur_value_len++;
485 }
486
487 cur++;
488 }
489
492
494 cur_value_len = 0;
495 }
496
499
501 if (free_src_value)
503
505}
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.