Encode a string to be suitable for an RFC2231 header.
If the value is large, the list will contain continuation lines.
345{
346 if (!attribute || !value)
347 return 0;
348
349 size_t count = 0;
351 bool add_quotes = false;
352 bool free_src_value = false;
353 bool split = false;
354 int continuation_number = 0;
355 size_t dest_value_len = 0, max_value_len = 0, cur_value_len = 0;
356 char *cur = NULL, *charset = NULL, *src_value = NULL;
358
361
362
363 for (cur = value; *cur; cur++)
364 {
365 if ((*cur < 0x20) || (*cur >= 0x7f))
366 {
368 break;
369 }
370 }
371
373 {
376 if (c_charset && c_send_charset)
377 {
380 }
381 if (src_value)
382 free_src_value = true;
383 if (!charset)
384 charset =
mutt_str_dup(c_charset ? c_charset :
"unknown-8bit");
385 }
386 if (!src_value)
387 src_value = value;
388
389
392
393 for (cur = src_value; *cur; cur++)
394 {
395 dest_value_len++;
396
398 {
399
400 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
401 strchr("*'%", *cur))
402 {
403 dest_value_len += 2;
404 }
405 }
406 else
407 {
408
410 add_quotes = true;
411
412 if ((*cur == '\\') || (*cur == '"'))
413 dest_value_len++;
414 }
415 }
416
417
418 max_value_len = 78 -
419 1 -
422 1 -
423 (add_quotes ? 2 : 0) -
424 1;
425
426 if (max_value_len < 30)
427 max_value_len = 30;
428
429 if (dest_value_len > max_value_len)
430 {
431 split = true;
432 max_value_len -= 4;
433
434 }
435
436
437 cur = src_value;
439 {
442 }
443
444 while (*cur)
445 {
449
451 if (split)
455
456 while (*cur && (!split || (cur_value_len < max_value_len)))
457 {
459 {
460 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
461 strchr("*'%", *cur))
462 {
464 cur_value_len += 3;
465 }
466 else
467 {
469 cur_value_len++;
470 }
471 }
472 else
473 {
475 cur_value_len++;
476 if ((*cur == '\\') || (*cur == '"'))
477 cur_value_len++;
478 }
479
480 cur++;
481 }
482
485
487 cur_value_len = 0;
488 }
489
492
494 if (free_src_value)
496
498}
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
size_t mutt_buffer_len(const struct Buffer *buf)
Calculate the length of a Buffer.
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
int mutt_buffer_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
char * mutt_buffer_strdup(const struct Buffer *buf)
Copy a Buffer's string.
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.
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from 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.