message-parser.c revision e658f2bfa3bb4555dbea5ae08d2ba090305ae2fe
/* Copyright (C) 2002 Timo Sirainen */
#include "lib.h"
#include "buffer.h"
#include "istream.h"
#include "str.h"
#include "strescape.h"
#include "message-content-parser.h"
#include "message-parser.h"
#include "message-size.h"
struct message_boundary {
struct message_boundary *next;
struct message_part *part;
const char *boundary;
};
struct parser_context {
struct message_part *part;
char *last_boundary;
char *last_content_type;
struct message_boundary *boundaries;
void *context;
};
struct message_header_parser_ctx {
struct message_header_line line;
struct message_size *hdr_size;
};
static struct message_part *
struct parser_context *parser_ctx);
static struct message_part *
struct message_size *body_size);
static struct message_part *
struct message_boundary *boundaries,
struct message_size *boundary_size);
struct message_part *part)
{
dest->physical_size +=
dest->virtual_size +=
}
static struct message_part *
{
/* set child position */
part->physical_pos =
return part;
}
void *context)
{
const char *str;
return;
}
}
}
static void
int value_quoted, void *context)
{
return;
if (value_quoted)
}
}
static struct message_part *
struct parser_context *parser_ctx)
{
struct message_boundary *b;
/* multipart message. add new boundary */
parser_ctx->boundaries = b;
/* reset fields */
/* skip the data before the first boundary */
&parent_part->body_size);
/* now, parse the parts */
while (next_part == parent_part) {
/* new child */
/* update our size */
if (next_part != parent_part)
break;
/* skip the boundary */
&parent_part->body_size);
}
/* remove boundary */
return next_part;
}
#define MUTEX_FLAGS \
static struct message_part *
{
struct message_header_parser_ctx *hdr_ctx;
struct message_header_line *hdr;
/* call the user-defined header parser */
}
continue;
}
/* we need to know the boundary */
}
}
}
/* when there's no content-type specified and we're
content-type */
} else {
}
}
headers again, this works pretty much the same as
} else {
/* normal message, read until the next boundary */
}
return next_part;
}
{
const unsigned char *msg;
startpos = 0;
if (msg[i] == '\n') {
if (!skip_lf) {
i--;
startpos = i;
goto __break;
}
msg_size->virtual_size++;
}
startpos = i+1;
goto __break;
}
}
/* leave the last character, it may be \r */
startpos = 1;
}
}
}
}
static struct message_boundary *
{
while (boundaries != NULL) {
return boundaries;
}
return NULL;
}
/* read until next boundary is found. if skip_over = FALSE, stop at the
[\r]\n before the boundary, otherwise leave it right after the known
boundary so the ending "--" can be checked. */
static struct message_boundary *
struct message_boundary *boundaries,
{
struct message_boundary *boundary;
const unsigned char *msg;
if (msg[i] != '\n')
continue;
/* possible boundary */
i - line_start - 2);
break;
}
/* missing CR */
}
line_start = i+1;
}
break;
if (i - line_start > 128 &&
/* long partial line, see if it's a boundary.
RFC-2046 says that the boundaries must be
70 chars without "--" or less. We allow
a bit larger.. */
i - line_start - 2);
break;
/* nope, we can skip over the line, just
leave the last char since it may be \r */
i--;
} else {
/* leave the last line to buffer, it may be
boundary */
i = line_start;
if (i > 0) i--; /* leave the \r\n too */
if (i > 0) i--;
line_start -= i;
}
i_stream_skip(input, i);
msg_size->physical_size += i;
msg_size->virtual_size += i;
}
/* possible boundary without line feed at end */
}
if (skip_over) {
/* leave the pointer right after the boundary */
/* leave the \r\n before the boundary */
line_start--;
line_start--;
else
}
}
return boundary;
}
static struct message_part *
struct message_size *msg_size)
{
struct message_boundary *boundary;
struct message_size body_size;
if (boundaries == NULL) {
return NULL;
} else {
}
}
/* skip data until next boundary is found. if it's end boundary,
skip the footer as well. */
static struct message_part *
struct message_boundary *boundaries,
struct message_size *boundary_size)
{
struct message_boundary *boundary;
const unsigned char *msg;
int end_boundary;
return NULL;
/* now, see if it's end boundary */
/* skip the rest of the line */
if (end_boundary) {
/* skip the footer */
}
}
void *context)
{
struct message_part *part;
struct parser_context parser_ctx;
return part;
}
struct message_size *hdr_size,
{
struct message_header_parser_ctx *hdr_ctx;
struct message_header_line *hdr;
}
struct message_header_parser_ctx *
{
struct message_header_parser_ctx *ctx;
return ctx;
}
{
}
struct message_header_line *
{
const unsigned char *msg;
int ret;
return NULL;
}
/* save the first line */
else {
}
}
colon_pos = 0;
} else {
/* new header line */
}
for (;;) {
if (ret != 0) {
/* we want to know one byte in advance to find out
if it's multiline header */
} else {
parse_size = size;
}
if (ret == -1) {
/* error / EOF with no bytes */
return NULL;
}
/* a) line is larger than input buffer
b) header ended unexpectedly */
/* header name is huge. just skip it. */
TRUE);
continue;
}
/* go back to last LWSP if found. */
size = i;
break;
}
}
break;
}
/* find ':' */
for (i = startpos; i < parse_size; i++) {
if (msg[i] <= ':') {
if (msg[i] == ':') {
colon_pos = i;
break;
}
if (msg[i] == '\n') {
/* end of headers, or error */
break;
}
}
}
}
/* find '\n' */
for (i = startpos; i < parse_size; i++) {
if (msg[i] == '\n')
break;
}
if (i < parse_size) {
/* got a line */
/* missing CR */
size = i;
} else {
size = i-1;
}
break;
}
startpos = i;
}
/* end of headers */
/* missing ':', assume the whole line is name */
} else {
/* get value, skip only first LWSP after ':' */
}
/* get name, skip LWSP before ':' */
colon_pos--;
}
/* first header line, set full_value = value */
} else if (line->use_full_value) {
/* continue saving the full value */
&line->full_value_len);
} else {
/* we didn't want full_value, and this is a continued line. */
line->full_value_len = 0;
}
/* always reset it */
}
return line;
}