dovecot: mail_index_map_register_ext() now takes ext_offset inst...

dovecot at dovecot.org dovecot at dovecot.org
Tue Aug 7 14:23:16 EEST 2007


details:   http://hg.dovecot.org/dovecot/rev/e6b1432e353f
changeset: 6222:e6b1432e353f
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Aug 07 14:23:11 2007 +0300
description:
mail_index_map_register_ext() now takes ext_offset instead of hdr_offset.
hdr_offset is calculated from ext_offset. Both are stored to the ext struct.

diffstat:

3 files changed, 26 insertions(+), 38 deletions(-)
src/lib-index/mail-index-map.c      |   44 ++++++++++++++---------------------
src/lib-index/mail-index-private.h  |    3 +-
src/lib-index/mail-index-sync-ext.c |   17 ++++---------

diffs (168 lines):

diff -r 01d09f75af4a -r e6b1432e353f src/lib-index/mail-index-map.c
--- a/src/lib-index/mail-index-map.c	Tue Aug 07 14:16:06 2007 +0300
+++ b/src/lib-index/mail-index-map.c	Tue Aug 07 14:23:11 2007 +0300
@@ -58,9 +58,15 @@ uint32_t mail_index_map_lookup_ext(struc
 	return (uint32_t)-1;
 }
 
+static size_t get_ext_size(size_t name_len)
+{
+	size_t size = sizeof(struct mail_index_ext_header) + name_len;
+	return MAIL_INDEX_HEADER_SIZE_ALIGN(size);
+}
+
 uint32_t
 mail_index_map_register_ext(struct mail_index_map *map, const char *name,
-			    uint32_t hdr_offset, uint32_t hdr_size,
+			    uint32_t ext_offset, uint32_t hdr_size,
 			    uint32_t record_offset, uint32_t record_size,
 			    uint32_t record_align, uint32_t reset_id)
 {
@@ -77,7 +83,8 @@ mail_index_map_register_ext(struct mail_
 
 	ext = array_append_space(&map->extensions);
 	ext->name = p_strdup(map->extension_pool, name);
-	ext->hdr_offset = hdr_offset;
+	ext->ext_offset = ext_offset;
+	ext->hdr_offset = ext_offset + get_ext_size(strlen(name));
 	ext->hdr_size = hdr_size;
 	ext->record_offset = record_offset;
 	ext->record_size = record_size;
@@ -95,27 +102,13 @@ mail_index_map_register_ext(struct mail_
 	return idx;
 }
 
-static bool size_check(size_t *size_left, size_t size)
-{
-	if (size > *size_left)
-		return FALSE;
-	*size_left -= size;
-	return TRUE;
-}
-
-static size_t get_align(size_t name_len)
-{
-	size_t size = sizeof(struct mail_index_ext_header) + name_len;
-	return MAIL_INDEX_HEADER_SIZE_ALIGN(size) - size;
-}
-
 static int mail_index_parse_extensions(struct mail_index_map *map)
 {
 	struct mail_index *index = map->index;
 	const struct mail_index_ext_header *ext_hdr;
 	unsigned int i, old_count;
 	const char *name;
-	uint32_t ext_id, offset, name_offset;
+	uint32_t ext_id, ext_offset, offset, name_offset;
 	size_t size_left;
 
 	/* extension headers always start from 64bit offsets, so if base header
@@ -134,6 +127,7 @@ static int mail_index_parse_extensions(s
 		array_append(&map->ext_id_map, &ext_id, 1);
 
 	for (i = 0; offset < map->hdr.header_size; i++) {
+		ext_offset = offset;
 		ext_hdr = CONST_PTR_OFFSET(map->hdr_base, offset);
 
 		/* Extension header contains:
@@ -144,19 +138,17 @@ static int mail_index_parse_extensions(s
 		   - 64bit alignment padding
 		*/
 		size_left = map->hdr.header_size - offset;
-		if (!size_check(&size_left, sizeof(*ext_hdr)) ||
-		    !size_check(&size_left, ext_hdr->name_size) ||
-		    !size_check(&size_left, get_align(ext_hdr->name_size)) ||
-		    !size_check(&size_left, ext_hdr->hdr_size)) {
+		if (size_left < sizeof(*ext_hdr) ||
+		    size_left < get_ext_size(ext_hdr->name_size) +
+		    ext_hdr->hdr_size) {
 			mail_index_set_error(index, "Corrupted index file %s: "
 				"Header extension goes outside header",
 				index->filepath);
 			return -1;
 		}
 
-		offset += sizeof(*ext_hdr);
-		name_offset = offset;
-		offset += ext_hdr->name_size + get_align(ext_hdr->name_size);
+		name_offset = offset + sizeof(*ext_hdr);
+		offset += get_ext_size(ext_hdr->name_size);
 
 		t_push();
 		name = t_strndup(CONST_PTR_OFFSET(map->hdr_base, name_offset),
@@ -201,8 +193,8 @@ static int mail_index_parse_extensions(s
 			return -1;
 		}
 
-		mail_index_map_register_ext(map, name,
-					    offset, ext_hdr->hdr_size,
+		mail_index_map_register_ext(map, name, ext_offset,
+					    ext_hdr->hdr_size,
 					    ext_hdr->record_offset,
 					    ext_hdr->record_size,
 					    ext_hdr->record_align,
diff -r 01d09f75af4a -r e6b1432e353f src/lib-index/mail-index-private.h
--- a/src/lib-index/mail-index-private.h	Tue Aug 07 14:16:06 2007 +0300
+++ b/src/lib-index/mail-index-private.h	Tue Aug 07 14:23:11 2007 +0300
@@ -48,6 +48,7 @@ struct mail_index_ext {
 	const char *name;
 	uint32_t index_idx; /* index ext_id */
 	uint32_t reset_id;
+	uint32_t ext_offset; /* points to beginning of mail_index_ext_header */
 	uint32_t hdr_offset; /* points to mail_index_ext_header.data[] */
 	uint32_t hdr_size; /* size of mail_index_ext_header.data[] */
 	uint16_t record_offset;
@@ -287,7 +288,7 @@ uint32_t mail_index_map_lookup_ext(struc
 				   const char *name);
 uint32_t
 mail_index_map_register_ext(struct mail_index_map *map, const char *name,
-			    uint32_t hdr_offset, uint32_t hdr_size,
+			    uint32_t ext_offset, uint32_t hdr_size,
 			    uint32_t record_offset, uint32_t record_size,
 			    uint32_t record_align, uint32_t reset_id);
 bool mail_index_map_get_ext_idx(struct mail_index_map *map,
diff -r 01d09f75af4a -r e6b1432e353f src/lib-index/mail-index-sync-ext.c
--- a/src/lib-index/mail-index-sync-ext.c	Tue Aug 07 14:16:06 2007 +0300
+++ b/src/lib-index/mail-index-sync-ext.c	Tue Aug 07 14:23:11 2007 +0300
@@ -123,16 +123,11 @@ get_ext_header(struct mail_index_map *ma
 get_ext_header(struct mail_index_map *map, const struct mail_index_ext *ext)
 {
 	struct mail_index_ext_header *ext_hdr;
-	uint32_t offset;
 	void *hdr_base;
 
 	/* do some kludgy jumping to get to it. */
-	offset = ext->hdr_offset -
-		MAIL_INDEX_HEADER_SIZE_ALIGN(sizeof(*ext_hdr) +
-					     strlen(ext->name));
-
 	hdr_base = buffer_get_modifiable_data(map->hdr_copy_buf, NULL);
-	ext_hdr = PTR_OFFSET(hdr_base, offset);
+	ext_hdr = PTR_OFFSET(hdr_base, ext->ext_offset);
 	i_assert(memcmp((char *)(ext_hdr + 1),
 			ext->name, strlen(ext->name)) == 0);
 	return ext_hdr;
@@ -392,6 +387,8 @@ int mail_index_sync_ext_intro(struct mai
 	map = mail_index_sync_get_atomic_map(ctx);
 
 	hdr_buf = map->hdr_copy_buf;
+	i_assert(hdr_buf->used == map->hdr.header_size);
+
 	if (MAIL_INDEX_HEADER_SIZE_ALIGN(hdr_buf->used) != hdr_buf->used) {
 		/* we need to add padding between base header and extensions */
 		buffer_append_zero(hdr_buf,
@@ -401,11 +398,9 @@ int mail_index_sync_ext_intro(struct mai
 
 	/* register record offset initially using zero,
 	   sync_ext_reorder() will fix it. */
-	hdr_offset = hdr_buf->used + sizeof(ext_hdr) + strlen(name);
-	hdr_offset = MAIL_INDEX_HEADER_SIZE_ALIGN(hdr_offset);
-	ext_id = mail_index_map_register_ext(map, name, hdr_offset, u->hdr_size,
-					     0, u->record_size, u->record_align,
-					     u->reset_id);
+	ext_id = mail_index_map_register_ext(map, name, hdr_buf->used,
+					     u->hdr_size, 0, u->record_size,
+					     u->record_align, u->reset_id);
 
 	ext = array_idx(&map->extensions, ext_id);
 


More information about the dovecot-cvs mailing list