Encode a string to be suitable for an RFC2231 header.
If the value is large, the list will contain continuation lines.
354{
355 if (!attribute || !value)
356 return 0;
357
358 size_t count = 0;
360 bool add_quotes = false;
361 bool free_src_value = false;
362 bool split = false;
363 int continuation_number = 0;
364 size_t dest_value_len = 0, max_value_len = 0, cur_value_len = 0;
365 char *cur = NULL, *charset = NULL, *src_value = NULL;
367
370
371
372 for (cur = value; *cur; cur++)
373 {
374 if ((*cur < 0x20) || (*cur >= 0x7f))
375 {
377 break;
378 }
379 }
380
382 {
385 if (c_charset && c_send_charset)
386 {
389 }
390 if (src_value)
391 free_src_value = true;
392 if (!charset)
393 charset =
mutt_str_dup(c_charset ? c_charset :
"unknown-8bit");
394 }
395 if (!src_value)
396 src_value = value;
397
398
401
402 for (cur = src_value; *cur; cur++)
403 {
404 dest_value_len++;
405
407 {
408
409 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
410 strchr("*'%", *cur))
411 {
412 dest_value_len += 2;
413 }
414 }
415 else
416 {
417
419 add_quotes = true;
420
421 if ((*cur == '\\') || (*cur == '"'))
422 dest_value_len++;
423 }
424 }
425
426
427 max_value_len = 78 -
428 1 -
431 1 -
432 (add_quotes ? 2 : 0) -
433 1;
434
435 if (max_value_len < 30)
436 max_value_len = 30;
437
438 if (dest_value_len > max_value_len)
439 {
440 split = true;
441 max_value_len -= 4;
442
443 }
444
445
446 cur = src_value;
448 {
450 cur_value_len =
buf_len(cur_value);
451 }
452
453 while (*cur)
454 {
458
460 if (split)
464
465 while (*cur && (!split || (cur_value_len < max_value_len)))
466 {
468 {
469 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
470 strchr("*'%", *cur))
471 {
473 cur_value_len += 3;
474 }
475 else
476 {
478 cur_value_len++;
479 }
480 }
481 else
482 {
484 cur_value_len++;
485 if ((*cur == '\\') || (*cur == '"'))
486 cur_value_len++;
487 }
488
489 cur++;
490 }
491
494
496 cur_value_len = 0;
497 }
498
501
503 if (free_src_value)
505
507}
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.