}
void message_header_encode_q(const unsigned char *input, unsigned int len,
- string_t *output)
+ string_t *output, unsigned int first_line_len)
{
unsigned int i, line_len_left;
- str_append(output, "=?utf-8?q?");
line_len_left = MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN;
+
+ if (first_line_len >= MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN - 3) {
+ str_append(output, "\n\t");
+ line_len_left--;
+ } else {
+ line_len_left -= first_line_len;
+ }
+
+ str_append(output, "=?utf-8?q?");
for (i = 0; i < len; i++) {
if (line_len_left < 3) {
/* if we're not at the beginning of a character,
}
void message_header_encode_b(const unsigned char *input, unsigned int len,
- string_t *output)
+ string_t *output, unsigned int first_line_len)
{
- unsigned int line_len_left, max;
+ unsigned int line_len, line_len_left, max;
+
+ line_len = first_line_len;
+ if (line_len >= MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN) {
+ str_append(output, "\n\t");
+ line_len = 1;
+ }
- line_len_left = MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN;
for (;;) {
+ line_len_left = MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN - line_len;
max = MAX_BASE64_DECODED_SIZE(line_len_left);
do {
max--;
break;
str_append(output, "\n\t");
- line_len_left = MIME_MAX_LINE_LEN - MIME_WRAPPER_LEN - 1;
+ line_len = 1;
}
}
/* and do it */
str_append_data(output, input, first_idx);
- if (use_q)
- message_header_encode_q(input + first_idx, enc_len, output);
- else
- message_header_encode_b(input + first_idx, enc_len, output);
+ if (use_q) {
+ message_header_encode_q(input + first_idx, enc_len,
+ output, first_idx);
+ } else {
+ message_header_encode_b(input + first_idx, enc_len,
+ output, first_idx);
+ }
str_append_data(output, input + last_idx, len - last_idx);
}
#ifndef MESSAGE_HEADER_ENCODE_H
#define MESSAGE_HEADER_ENCODE_H
-/* Encode UTF-8 input into output wherever necessary. */
+/* Encode UTF-8 input into output wherever necessary using either Q or B
+ encoding depending on which takes less space (approximately). */
void message_header_encode(const char *input, string_t *output);
void message_header_encode_data(const unsigned char *input, unsigned int len,
string_t *output);
/* Encode the whole UTF-8 input using "Q" or "B" encoding into output.
- The output is split into multiple lines if necessary (max 76 chars/line). */
+ The output is split into multiple lines if necessary (max 76 chars/line).
+ The first line's length is given as parameter. */
void message_header_encode_q(const unsigned char *input, unsigned int len,
- string_t *output);
+ string_t *output, unsigned int first_line_len);
void message_header_encode_b(const unsigned char *input, unsigned int len,
- string_t *output);
+ string_t *output, unsigned int first_line_len);
#endif
str_append_c(str, ' ');
message_header_encode_q(str_data(input) + skip,
- str_len(input) - skip, str);
+ str_len(input) - skip, str,
+ i == 0 ? 0 : i+1);
test_assert(verify_q(str_c(str), i, !skip));
}
}
str_append_c(str, ' ');
message_header_encode_b(str_data(input) + skip,
- str_len(input) - skip, str);
+ str_len(input) - skip, str,
+ i == 0 ? 0 : i+1);
test_assert(verify_b(str_c(str), i, !skip));
}
}