message-decoder.c revision 9ae664e94e6eeb5c1f900bb90642052633031832
/* Copyright (c) 2006-2007 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "buffer.h"
#include "base64.h"
#include "str.h"
#include "unichar.h"
#include "charset-utf8.h"
#include "quoted-printable.h"
#include "rfc822-parser.h"
#include "message-parser.h"
#include "message-header-decode.h"
#include "message-decoder.h"
enum content_type {
CONTENT_TYPE_UNKNOWN = 0,
};
/* base64 takes max 4 bytes per character, q-p takes max 3. */
#define MAX_ENCODING_BUF_SIZE 3
/* UTF-8 takes max 5 bytes per character. Not sure about others, but I'd think
10 is more than enough for everyone.. */
#define MAX_TRANSLATION_BUF_SIZE 10
struct message_decoder_context {
struct message_part *prev_part;
struct message_header_line hdr;
char *charset_trans_charset;
struct charset_translation *charset_trans;
unsigned int translation_size;
unsigned int encoding_size;
char *content_charset;
enum content_type content_type;
unsigned int dtcase:1;
unsigned int charset_utf8:1;
};
{
struct message_decoder_context *ctx;
return ctx;
}
{
}
static void
struct message_header_line *hdr)
{
struct rfc822_parser_context parser;
(void)rfc822_skip_lwsp(&parser);
case 4:
break;
case 6:
break;
case 16:
break;
}
}
static void
struct message_header_line *hdr)
{
struct rfc822_parser_context parser;
return;
(void)rfc822_skip_lwsp(&parser);
return;
break;
}
}
}
struct message_header_line *hdr,
struct message_block *output)
{
return FALSE;
}
);
}
}
return TRUE;
}
{
unsigned int data_wanted, skip;
/* @UNSAFE: move the previously untranslated bytes to trans_buf
and see if we have now enough data to get the next character
translated */
if (data_wanted > *size)
data_wanted = *size;
ctx->translation_size = 0;
}
static inline unsigned int
{
return 0;
for (i = 0; i < len; i++) {
return 0;
}
return len;
}
static const unsigned char *
{
/* find the first invalid utf8 sequence */
for (i = 0; i < size;) {
if (input[i] < 0x80)
i++;
else {
goto broken;
i += len;
}
}
/* we can use it as-is */
*output_size_r = size;
return input;
/* broken utf-8 input - skip the broken characters */
while (i < size) {
if (input[i] < 0x80) {
continue;
}
if (len == 0) {
i++;
continue;
}
i += len;
}
}
{
enum charset_flags flags;
if (ctx->charset_utf8)
return;
/* already have the correct translation selected */
return;
}
}
struct message_block *input,
struct message_block *output)
{
int ret;
if (ctx->encoding_size != 0) {
/* @UNSAFE */
}
switch (ctx->content_type) {
case CONTENT_TYPE_UNKNOWN:
/* just skip this body */
return FALSE;
case CONTENT_TYPE_BINARY:
break;
case CONTENT_TYPE_QP:
if (ctx->encoding_size != 0) {
}
break;
case CONTENT_TYPE_BASE64:
if (ctx->encoding_size != 0) {
/* corrupted base64 data, don't bother with
the rest of it */
return FALSE;
}
}
if (ret < 0) {
/* corrupted base64 data, don't bother with
the rest of it */
return FALSE;
}
if (ret == 0) {
/* end of base64 input */
}
break;
}
/* @UNSAFE */
ctx->encoding_size);
} else {
ctx->encoding_size = 0;
}
if (ctx->charset_utf8) {
} else {
}
/* unknown charset */
} else {
if (ctx->translation_size != 0)
sizeof(ctx->translation_buf));
}
}
return TRUE;
}
struct message_block *input,
struct message_block *output)
{
/* MIME part changed. */
}
else {
return TRUE;
}
}
{
ctx->encoding_size = 0;
}