view src/lib-mail/message-size.c @ 903:fd8888f6f037 HEAD

Naming style changes, finally got tired of most of the typedefs. Also the previous enum -> macro change reverted so that we don't use the highest bit anymore, that's incompatible with old indexes so they will be rebuilt.
author Timo Sirainen <tss@iki.fi>
date Sun, 05 Jan 2003 15:09:51 +0200
parents e49f7397af98
children e0dbaabbc22b
line wrap: on
line source

/* Copyright (C) 2002 Timo Sirainen */

#include "lib.h"
#include "istream.h"
#include "message-parser.h"
#include "message-size.h"

void message_get_header_size(struct istream *input, struct message_size *hdr)
{
	const unsigned char *msg;
	size_t i, size, startpos, missing_cr_count;

	memset(hdr, 0, sizeof(struct message_size));

	missing_cr_count = 0; startpos = 0;
	while (i_stream_read_data(input, &msg, &size, startpos) > 0) {
		for (i = startpos; i < size; i++) {
			if (msg[i] != '\n')
				continue;

			hdr->lines++;
			if (i == 0 || msg[i-1] != '\r') {
				/* missing CR */
				missing_cr_count++;
			}

			if (i == 0 || (i == 1 && msg[i-1] == '\r')) {
				/* no headers at all */
				break;
			}

			if ((i > 0 && msg[i-1] == '\n') ||
			    (i > 1 && msg[i-2] == '\n' && msg[i-1] == '\r')) {
				/* \n\n or \n\r\n - end of headers */
				break;
			}
		}

		if (i < size) {
			/* end of header */
			startpos = i+1;
			break;
		}

		/* leave the last two characters, they may be \r\n */
		startpos = size == 1 ? 1 : 2;
		i_stream_skip(input, i - startpos);

		hdr->physical_size += i - startpos;
	}
	i_stream_skip(input, startpos);
	hdr->physical_size += startpos;

	hdr->virtual_size = hdr->physical_size + missing_cr_count;
	i_assert(hdr->virtual_size >= hdr->physical_size);
}

void message_get_body_size(struct istream *input, struct message_size *body,
			   uoff_t max_virtual_size, int *last_cr)
{
	const unsigned char *msg;
	size_t i, size, startpos, missing_cr_count;
	int cr;

	memset(body, 0, sizeof(struct message_size));

	cr = 0;
	missing_cr_count = 0; startpos = 0;
	while (max_virtual_size != 0 &&
	       i_stream_read_data(input, &msg, &size, startpos) > 0) {
		cr = 0;
		for (i = startpos; i < size && max_virtual_size > 0; i++) {
			max_virtual_size--;

			if (msg[i] == '\n') {
				if (i == 0 || msg[i-1] != '\r') {
					/* missing CR */
					missing_cr_count++;

					if (max_virtual_size == 0) {
						cr = 2;
						break;
					}

					max_virtual_size--;
				}

				/* increase after making sure we didn't break
				   at virtual \r */
				body->lines++;
			}
		}

		if (cr == 0 && i > 0 && msg[i-1] == '\r')
			cr = 1;

		/* leave the last character, it may be \r */
		i_stream_skip(input, i - 1);
		startpos = 1;

		body->physical_size += i - 1;
	}
	i_stream_skip(input, startpos);
	body->physical_size += startpos;

	body->virtual_size = body->physical_size + missing_cr_count;
	i_assert(body->virtual_size >= body->physical_size);

	if (last_cr != NULL)
		*last_cr = cr;
}

void message_skip_virtual(struct istream *input, uoff_t virtual_skip,
			  struct message_size *msg_size, int *cr_skipped)
{
	const unsigned char *msg;
	size_t i, size, startpos;

	*cr_skipped = FALSE;
	if (virtual_skip == 0)
		return;

	startpos = 0;
	while (i_stream_read_data(input, &msg, &size, startpos) > 0) {
		for (i = startpos; i < size && virtual_skip > 0; i++) {
			virtual_skip--;

			if (msg[i] == '\r') {
				/* CR */
				if (virtual_skip == 0)
					*cr_skipped = TRUE;
			} else if (msg[i] == '\n') {
				/* LF */
				if (i == 0 || msg[i-1] != '\r') {
					/* missing CR */
					if (msg_size != NULL)
						msg_size->virtual_size++;

					if (virtual_skip == 0) {
						/* CR/LF boundary */
						*cr_skipped = TRUE;
						break;
					}

					virtual_skip--;
				}

				/* increase after making sure we didn't break
				   at virtual \r */
				if (msg_size != NULL)
					msg_size->lines++;
			}
		}

		if (msg_size != NULL) {
			msg_size->physical_size += i;
			msg_size->virtual_size += i;
		}

		if (i < size) {
			i_stream_skip(input, i);
			break;
		}

		/* leave the last character, it may be \r */
		i_stream_skip(input, i - 1);
		startpos = 1;
	}
}

void message_size_add(struct message_size *dest,
		      const struct message_size *src)
{
	dest->virtual_size += src->virtual_size;
	dest->physical_size += src->physical_size;
	dest->lines += src->lines;
}