Encode a string to be suitable for an RFC2231 header.
If the value is large, the list will contain continuation lines.
350{
351 if (!attribute || !value)
352 return 0;
353
354 size_t count = 0;
356 bool add_quotes = false;
357 bool free_src_value = false;
358 bool split = false;
359 int continuation_number = 0;
360 size_t dest_value_len = 0, max_value_len = 0, cur_value_len = 0;
361 char *cur = NULL, *charset = NULL, *src_value = NULL;
363
366
367
368 for (cur = value; *cur; cur++)
369 {
370 if ((*cur < 0x20) || (*cur >= 0x7f))
371 {
373 break;
374 }
375 }
376
378 {
381 if (c_charset && c_send_charset)
382 {
385 }
386 if (src_value)
387 free_src_value = true;
388 if (!charset)
389 charset =
mutt_str_dup(c_charset ? c_charset :
"unknown-8bit");
390 }
391 if (!src_value)
392 src_value = value;
393
394
397
398 for (cur = src_value; *cur; cur++)
399 {
400 dest_value_len++;
401
403 {
404
405 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
406 strchr("*'%", *cur))
407 {
408 dest_value_len += 2;
409 }
410 }
411 else
412 {
413
415 add_quotes = true;
416
417 if ((*cur == '\\') || (*cur == '"'))
418 dest_value_len++;
419 }
420 }
421
422
423 max_value_len = 78 -
424 1 -
427 1 -
428 (add_quotes ? 2 : 0) -
429 1;
430
431 if (max_value_len < 30)
432 max_value_len = 30;
433
434 if (dest_value_len > max_value_len)
435 {
436 split = true;
437 max_value_len -= 4;
438
439 }
440
441
442 cur = src_value;
444 {
447 }
448
449 while (*cur)
450 {
454
456 if (split)
460
461 while (*cur && (!split || (cur_value_len < max_value_len)))
462 {
464 {
465 if ((*cur < 0x20) || (*cur >= 0x7f) || strchr(
MimeSpecials, *cur) ||
466 strchr("*'%", *cur))
467 {
469 cur_value_len += 3;
470 }
471 else
472 {
474 cur_value_len++;
475 }
476 }
477 else
478 {
480 cur_value_len++;
481 if ((*cur == '\\') || (*cur == '"'))
482 cur_value_len++;
483 }
484
485 cur++;
486 }
487
490
492 cur_value_len = 0;
493 }
494
497
499 if (free_src_value)
501
503}
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.
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
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.