message-header-decode.c revision d65a556a5ec078cd7f1d0060adb16fc860d66b27
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenstatic bool split_encoded(const unsigned char *data, size_t *size_p,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen const unsigned char **text, size_t *text_size_r)
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* get charset */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen for (pos = 0; pos < size && data[pos] != '?'; pos++) ;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* get encoding */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen else if (data[pos] == 'B' || data[pos] == 'b')
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* get text */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (data[pos] != '?' || pos+1 >= size || data[pos+1] != '=')
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenmessage_header_decode_encoded(const unsigned char *data, size_t *size,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen const unsigned char *text;
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* first split the string charset?encoding?text?= */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (!split_encoded(data, size, &charset, &encoding,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen decodebuf = buffer_create_static_hard(pool_datastack_create(),
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen quoted_printable_decode(text, text_size, NULL, decodebuf);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (base64_decode(text, text_size, NULL, decodebuf) < 0) {
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* corrupted encoding */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen callback(decodebuf->data, decodebuf->used, charset, context);
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainenvoid message_header_decode(const unsigned char *data, size_t size,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (data[pos] == '=' && pos+1 < size && data[pos+1] == '?') {
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* encoded string beginning */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen /* send the unencoded data so far */
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (!callback(data + start_pos, pos - start_pos,
51cbc45fc1ac5dde29bc2adbb175945df1b4f7d4Timo Sirainen if (!message_header_decode_encoded(data + pos, &subsize,