bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainenmessage_header_decode_encoded(const unsigned char *data, size_t size,
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* data should contain "charset?encoding?text?=" */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen for (i = 0; i < size; i++) {
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* invalid block */
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainen if (quoted_printable_q_decode(data + start_pos[1] + 1,
e4f1a5fdad77884e1de516521504c15dc936fa9dTimo Sirainen /* we skipped over some invalid data */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* contains invalid data. show what we got so far. */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* unknown encoding */
2ac5f36aa7c2e7a07ba8815d43a6d7483f62e74cTimo Sirainenstatic bool is_only_lwsp(const unsigned char *data, size_t size)
b014857be9961acf2d37ef7b76d941b20cc8c2d1Timo Sirainen for (i = 0; i < size; i++) {
847aeef259d42e2f14cf126699e28291e6e1fb53Timo Sirainenvoid message_header_decode(const unsigned char *data, size_t size,
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* =?charset?Q|B?text?= */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* encoded string beginning */
b014857be9961acf2d37ef7b76d941b20cc8c2d1Timo Sirainen !is_only_lwsp(data+start_pos, pos-start_pos)) {
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* send the unencoded data so far */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen if (!callback(data + start_pos, pos - start_pos,
fab050cbfdf3da692441d2e2fb4b2a4c6ac9e0daTimo Sirainen decodebuf = buffer_create_dynamic(default_pool,
2a3fc652e13a574ca14ff2405b5c29a59232db49Timo Sirainen ret = message_header_decode_encoded(data + pos, size - pos,
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen /* decodebuf contains <charset> NUL <text> */
812ac1e2570c600a086c09b24d250224a822a97dTimo Sirainen if (!callback(CONST_PTR_OFFSET(decodebuf->data,
d65a556a5ec078cd7f1d0060adb16fc860d66b27Timo Sirainen (void)callback(data + start_pos, size - start_pos,
72388282bf6718c39af34cfcf51438910f9d62daTimo Sirainendecode_utf8_callback(const unsigned char *data, size_t size,
72388282bf6718c39af34cfcf51438910f9d62daTimo Sirainen if (charset == NULL || charset_is_utf8(charset)) {
72388282bf6718c39af34cfcf51438910f9d62daTimo Sirainen /* ASCII / UTF-8 */
b516a7812b9acc04522869fead3aa6d2787dcdc6Timo Sirainen if (uni_utf8_get_valid_data(data, size, ctx->dest))
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen if (charset_to_utf8_begin(charset, ctx->normalizer, &t) < 0) {
af6c7862e6160ffaecec458f4cec43b94272ad57Timo Sirainen /* data probably still contains some valid ASCII characters.
af6c7862e6160ffaecec458f4cec43b94272ad57Timo Sirainen append them. */
af6c7862e6160ffaecec458f4cec43b94272ad57Timo Sirainen if (uni_utf8_get_valid_data(data, size, ctx->dest))
72388282bf6718c39af34cfcf51438910f9d62daTimo Sirainen /* ignore any errors */
055f4599bba1874fa1148a8fa488517fa077619cTimo Sirainen (void)charset_to_utf8(t, data, &size, ctx->dest);
82d3a1d1594ed93d04d7bf999027b3e5104de6e4Timo Sirainenvoid message_header_decode_utf8(const unsigned char *data, size_t size,
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen buffer_t *dest, normalizer_func_t *normalizer)