changeset 183:4a7ab9e94f25 HEAD

size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more C99-like.
author Timo Sirainen <tss@iki.fi>
date Sun, 08 Sep 2002 16:20:28 +0300
parents 7fc832e6bab4
children 4223b9ed0c80
files acconfig.h configure.in src/imap/commands-util.c src/lib-imap/imap-bodystructure.c src/lib-storage/index/index-fetch.c src/lib-storage/index/index-search.c src/lib/base64.c src/lib/base64.h src/lib/fdpass.c src/lib/fdpass.h src/lib/hex-binary.c src/lib/hex-binary.h src/lib/imem.c src/lib/imem.h src/lib/iobuffer.c src/lib/iobuffer.h src/lib/md5.c src/lib/md5.h src/lib/mempool-alloconly.c src/lib/mempool-system.c src/lib/mempool.c src/lib/mempool.h src/lib/mmap-util.c src/lib/network.c src/lib/network.h src/lib/randgen.c src/lib/randgen.h src/lib/strfuncs.c src/lib/strfuncs.h src/lib/temp-mempool.c src/lib/temp-mempool.h src/lib/temp-string.c src/lib/temp-string.h src/lib/write-full.c
diffstat 34 files changed, 269 insertions(+), 246 deletions(-) [+]
line wrap: on
line diff
--- a/acconfig.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/acconfig.h	Sun Sep 08 16:20:28 2002 +0300
@@ -29,13 +29,19 @@
 #undef OFF_T_MAX
 
 /* printf()-format for uoff_t, eg. "u" or "lu" or "llu" */
-#undef UOFF_T_FORMAT
+#undef PRIuUOFF_T
 
 /* What type should be used for uoff_t */
 #undef UOFF_T_INT
 #undef UOFF_T_LONG
 #undef UOFF_T_LONG_LONG
 
+/* Maximum value for ssize_t */
+#undef SSIZE_T_MAX
+
+/* printf()-format for size_t, eg. "u" or "llu" */
+#undef PRIuSIZE_T
+
 /* What type should be used for largest_t */
 #undef LARGEST_T_LONG
 #undef LARGEST_T_LONG_LONG
--- a/configure.in	Sun Sep 08 16:08:26 2002 +0300
+++ b/configure.in	Sun Sep 08 16:20:28 2002 +0300
@@ -114,6 +114,7 @@
 AC_CHECK_SIZEOF(int)
 AC_CHECK_SIZEOF(long)
 AC_CHECK_SIZEOF(long long)
+AC_CHECK_SIZEOF(ssize_t)
 
 AC_MSG_CHECKING([size of off_t])
 sizeof_off_t=0
@@ -137,22 +138,32 @@
 if test x$sizeof_off_t = x$ac_cv_sizeof_long; then
   # try to use unsigned long always first
   AC_DEFINE_UNQUOTED(OFF_T_MAX, LONG_MAX)
-  AC_DEFINE_UNQUOTED(UOFF_T_FORMAT, "lu")
+  AC_DEFINE_UNQUOTED(PRIuUOFF_T, "lu")
   AC_DEFINE(UOFF_T_LONG)
 elif test x$sizeof_off_t = x$ac_cv_sizeof_int; then
   # next try int
   AC_DEFINE_UNQUOTED(OFF_T_MAX, INT_MAX)
-  AC_DEFINE_UNQUOTED(UOFF_T_FORMAT, "u")
+  AC_DEFINE_UNQUOTED(PRIuUOFF_T, "u")
   AC_DEFINE(UOFF_T_INT)
 elif test x$sizeof_off_t = x$ac_cv_sizeof_long_long; then
   # and finally long long
   AC_DEFINE_UNQUOTED(OFF_T_MAX, LLONG_MAX)
-  AC_DEFINE_UNQUOTED(UOFF_T_FORMAT, "llu")
+  AC_DEFINE_UNQUOTED(PRIuUOFF_T, "llu")
   AC_DEFINE(UOFF_T_LONG_LONG)
 else
   AC_ERROR([Couldn't find integer type for off_t])
 fi
 
+if test x$ac_cv_sizeof_ssize_t = x$ac_cv_sizeof_int; then
+  AC_DEFINE_UNQUOTED(SSIZE_T_MAX, INT_MAX)
+  AC_DEFINE_UNQUOTED(PRIuSIZE_T, "u")
+elif test x$ac_cv_sizeof_ssize_t = x$ac_cv_sizeof_long_long; then
+  AC_DEFINE_UNQUOTED(SSIZE_T_MAX, LLONG_MAX)
+  AC_DEFINE_UNQUOTED(PRIuSIZE_T, "llu")
+else
+  AC_ERROR([Couldn't find integer type for ssize_t])
+fi
+
 if test x$ac_cv_sizeof_long_long != x0; then
   AC_DEFINE(LARGEST_T_LONG_LONG)
 else
--- a/src/imap/commands-util.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/imap/commands-util.c	Sun Sep 08 16:20:28 2002 +0300
@@ -77,7 +77,7 @@
 			      unsigned int uid __attr_unused__, void *context)
 {
 	Client *client = context;
-	char str[MAX_INT_STRLEN+20];
+	char str[MAX_LARGEST_T_STRLEN+20];
 
 	i_snprintf(str, sizeof(str), "* %u EXPUNGE", seq);
 	client_send_line(client, str);
@@ -100,7 +100,7 @@
 static int client_sync_full(Client *client, int expunge)
 {
 	unsigned int messages;
-	char str[MAX_INT_STRLEN+20];
+	char str[MAX_LARGEST_T_STRLEN+20];
 
 	if (client->mailbox == NULL)
 		return TRUE;
--- a/src/lib-imap/imap-bodystructure.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib-imap/imap-bodystructure.c	Sun Sep 08 16:20:28 2002 +0300
@@ -266,7 +266,7 @@
 		t_string_append_c(str, ')');
 	}
 
-	t_string_printfa(str, " %s %s %s %"UOFF_T_FORMAT,
+	t_string_printfa(str, " %s %s %s %"PRIuUOFF_T,
 			 NVL(data->content_id, "NIL"),
 			 NVL(data->content_description, "NIL"),
 			 NVL(data->content_transfer_encoding, "\"8bit\""),
--- a/src/lib-storage/index/index-fetch.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib-storage/index/index-fetch.c	Sun Sep 08 16:20:28 2002 +0300
@@ -65,7 +65,7 @@
 		return;
 	}
 
-	t_string_printfa(ctx->str, " RFC822.SIZE %"UOFF_T_FORMAT,
+	t_string_printfa(ctx->str, " RFC822.SIZE %"PRIuUOFF_T,
 			 hdr_size.virtual_size + body_size.virtual_size);
 }
 
@@ -107,7 +107,7 @@
 		return;
 	}
 
-	str = t_strdup_printf(" RFC822 {%"UOFF_T_FORMAT"}\r\n",
+	str = t_strdup_printf(" RFC822 {%"PRIuUOFF_T"}\r\n",
 			      hdr_size.virtual_size + body_size.virtual_size);
 	if (ctx->first) str++; else ctx->first = FALSE;
 	(void)io_buffer_send(ctx->outbuf, str, strlen(str));
@@ -129,7 +129,7 @@
 		return;
 	}
 
-	str = t_strdup_printf(" RFC822.HEADER {%"UOFF_T_FORMAT"}\r\n",
+	str = t_strdup_printf(" RFC822.HEADER {%"PRIuUOFF_T"}\r\n",
 			      hdr_size.virtual_size);
 	if (ctx->first) str++; else ctx->first = FALSE;
 	(void)io_buffer_send(ctx->outbuf, str, strlen(str));
@@ -148,7 +148,7 @@
 		return;
 	}
 
-	str = t_strdup_printf(" RFC822.TEXT {%"UOFF_T_FORMAT"}\r\n",
+	str = t_strdup_printf(" RFC822.TEXT {%"PRIuUOFF_T"}\r\n",
 			      body_size.virtual_size);
 	if (ctx->first) str++; else ctx->first = FALSE;
 	(void)io_buffer_send(ctx->outbuf, str, strlen(str));
--- a/src/lib-storage/index/index-search.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib-storage/index/index-search.c	Sun Sep 08 16:20:28 2002 +0300
@@ -661,7 +661,7 @@
 	SearchIndexContext ctx;
 	MailIndexRecord *rec;
 	unsigned int first_seq, last_seq, seq;
-	char num[MAX_INT_STRLEN+10];
+	char num[MAX_LARGEST_T_STRLEN+10];
 
 	if (ibox->synced_messages_count == 0)
 		return;
--- a/src/lib/base64.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/base64.c	Sun Sep 08 16:20:28 2002 +0300
@@ -45,7 +45,7 @@
 static const char basis_64[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
-const char *base64_encode(const unsigned char *data, unsigned int size)
+const char *base64_encode(const unsigned char *data, size_t size)
 {
 	char *buffer, *p;
 	int c1, c2, c3;
--- a/src/lib/base64.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/base64.h	Sun Sep 08 16:20:28 2002 +0300
@@ -2,7 +2,7 @@
 #define __BASE64_H
 
 /* Translates binary data into base64. Allocates memory from temporary pool. */
-const char *base64_encode(const unsigned char *data, unsigned int size);
+const char *base64_encode(const unsigned char *data, size_t size);
 
 /* Translates base64 data into binary modifying the data itself.
    Returns size of the binary data, or -1 if error occured. */
--- a/src/lib/fdpass.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/fdpass.c	Sun Sep 08 16:20:28 2002 +0300
@@ -43,13 +43,15 @@
 	(CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
 #endif
 
-int fd_send(int handle, int send_fd, const void *data, int size)
+int fd_send(int handle, int send_fd, const void *data, size_t size)
 {
         struct msghdr msg;
         struct iovec iov;
         struct cmsghdr *cmsg;
 	int *fdptr;
-        char buf[CMSG_SPACE(sizeof(int))];
+	char buf[CMSG_SPACE(sizeof(int))];
+
+	i_assert(size < SSIZE_T_MAX);
 
 	memset(&msg, 0, sizeof (struct msghdr));
 
@@ -69,7 +71,7 @@
 	return sendmsg(handle, &msg, 0);
 }
 
-int fd_read(int handle, void *data, int size, int *fd)
+int fd_read(int handle, void *data, size_t size, int *fd)
 {
 	struct msghdr msg;
 	struct iovec iov;
@@ -77,6 +79,8 @@
 	int ret;
 	char buf[CMSG_SPACE(sizeof(int))];
 
+	i_assert(size < SSIZE_T_MAX);
+
 	memset(&msg, 0, sizeof (struct msghdr));
 
 	msg.msg_control = buf;
--- a/src/lib/fdpass.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/fdpass.h	Sun Sep 08 16:20:28 2002 +0300
@@ -2,10 +2,10 @@
 #define __FDPASS_H
 
 /* Returns number of bytes sent, -1 if error. */
-int fd_send(int handle, int send_fd, const void *data, int size);
+int fd_send(int handle, int send_fd, const void *data, size_t size);
 
 /* Returns number of bytes read, or -1 if error. fd is set only
    if return value is larger than 0. */
-int fd_read(int handle, void *data, int size, int *fd);
+int fd_read(int handle, void *data, size_t size, int *fd);
 
 #endif
--- a/src/lib/hex-binary.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/hex-binary.c	Sun Sep 08 16:20:28 2002 +0300
@@ -26,10 +26,11 @@
 #include "lib.h"
 #include "hex-binary.h"
 
-const char *binary_to_hex(const unsigned char *data, unsigned int size)
+const char *binary_to_hex(const unsigned char *data, size_t size)
 {
-	unsigned int i, value;
 	char *buf, *p;
+	size_t i;
+	int value;
 
 	buf = p = t_malloc(size * 2 + 1);
 	for (i = 0; i < size; i++) {
@@ -44,9 +45,10 @@
 	return buf;
 }
 
-int hex_to_binary(const char *data, unsigned char *dest)
+ssize_t hex_to_binary(const char *data, unsigned char *dest)
 {
-	int size, value;
+	size_t size;
+	int value;
 
 	size = 0;
 	while (*data != '\0') {
@@ -73,5 +75,5 @@
 		data++;
 	}
 
-	return size;
+	return (ssize_t)size;
 }
--- a/src/lib/hex-binary.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/hex-binary.h	Sun Sep 08 16:20:28 2002 +0300
@@ -3,12 +3,12 @@
 
 /* Convert binary to lowercased hex digits allocating return value from
    temporary memory pool */
-const char *binary_to_hex(const unsigned char *data, unsigned int size);
+const char *binary_to_hex(const unsigned char *data, size_t size);
 
 /* Convert hex to binary. data and dest may point to same value.
    Returns TRUE if successful. Returns number of bytes written to dest,
    or -1 if error occured. Make sure dest is at least half the size of
    strlen(data). */
-int hex_to_binary(const char *data, unsigned char *dest);
+ssize_t hex_to_binary(const char *data, unsigned char *dest);
 
 #endif
--- a/src/lib/imem.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/imem.c	Sun Sep 08 16:20:28 2002 +0300
@@ -27,7 +27,7 @@
 
 Pool default_pool;
 
-void *i_malloc(unsigned int size)
+void *i_malloc(size_t size)
 {
         return p_malloc(default_pool, size);
 }
@@ -37,7 +37,7 @@
         p_free(default_pool, mem);
 }
 
-void *i_realloc(void *mem, unsigned int size)
+void *i_realloc(void *mem, size_t size)
 {
         return p_realloc(default_pool, mem, size);
 }
@@ -57,7 +57,7 @@
 	return p_strdup_until(default_pool, str, end);
 }
 
-char *i_strndup(const char *str, unsigned int max_chars)
+char *i_strndup(const char *str, size_t max_chars)
 {
         return p_strndup(default_pool, str, max_chars);
 }
@@ -89,7 +89,7 @@
 	va_list args;
         const char *temp;
 	char *ret;
-        unsigned int len;
+        size_t len;
 
 	va_start(args, str1);
 
--- a/src/lib/imem.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/imem.h	Sun Sep 08 16:20:28 2002 +0300
@@ -6,15 +6,15 @@
 /* For easy allocation of memory from default memory pool. */
 #define i_new(type, count) \
         ((type *) i_malloc((unsigned) sizeof(type) * (count)))
-void *i_malloc(unsigned int size);
+void *i_malloc(size_t size);
 void i_free(void *mem);
-void *i_realloc(void *mem, unsigned int size);
+void *i_realloc(void *mem, size_t size);
 
 /* string functions */
 char *i_strdup(const char *str);
 char *i_strdup_empty(const char *str); /* like i_strdup(), but if str == "", return NULL */
 char *i_strdup_until(const char *str, const char *end); /* *end isn't included */
-char *i_strndup(const char *str, unsigned int max_chars);
+char *i_strndup(const char *str, size_t max_chars);
 char *i_strdup_printf(const char *format, ...) __attr_format__(1, 2);
 char *i_strdup_vprintf(const char *format, va_list args);
 void i_strdup_replace(char **dest, const char *str);
--- a/src/lib/iobuffer.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/iobuffer.c	Sun Sep 08 16:20:28 2002 +0300
@@ -32,23 +32,25 @@
 
 #include <unistd.h>
 
+#define MAX_SSIZE_T(size) ((size) < SSIZE_T_MAX ? (size_t)(size) : SSIZE_T_MAX)
+
 typedef struct {
 	IOLoop ioloop;
 	IOBuffer *outbuf;
 
 	const char *data;
-	unsigned int size;
+	uoff_t size;
 	IOBuffer *inbuf;
 
 	int timeout;
 	int last_block;
 } IOBufferBlockContext;
 
-static unsigned int mmap_pagesize = 0;
-static unsigned int mmap_pagemask = 0;
+static size_t mmap_pagesize = 0;
+static size_t mmap_pagemask = 0;
 
 IOBuffer *io_buffer_create(int fd, Pool pool, int priority,
-			   unsigned int max_buffer_size)
+			   size_t max_buffer_size)
 {
 	IOBuffer *buf;
 
@@ -63,8 +65,7 @@
 	return buf;
 }
 
-IOBuffer *io_buffer_create_file(int fd, Pool pool,
-				unsigned int max_buffer_size)
+IOBuffer *io_buffer_create_file(int fd, Pool pool, size_t max_buffer_size)
 {
 	IOBuffer *buf;
 
@@ -73,7 +74,7 @@
         return buf;
 }
 
-IOBuffer *io_buffer_create_mmap(int fd, Pool pool, unsigned int block_size,
+IOBuffer *io_buffer_create_mmap(int fd, Pool pool, size_t block_size,
 				uoff_t size)
 {
 	IOBuffer *buf;
@@ -109,8 +110,8 @@
 		start_offset = stop_offset;
 
 	if (size > (uoff_t) (stop_offset-start_offset)) {
-		i_warning("Trying to create IOBuffer with size %"UOFF_T_FORMAT
-			  " but we have only %"UOFF_T_FORMAT" bytes available "
+		i_warning("Trying to create IOBuffer with size %"PRIuUOFF_T
+			  " but we have only %"PRIuUOFF_T" bytes available "
 			  "in file", size, stop_offset-start_offset);
 		size = stop_offset-start_offset;
 	}
@@ -188,14 +189,14 @@
         return newbuf;
 }
 
-void io_buffer_set_max_size(IOBuffer *buf, unsigned int max_size)
+void io_buffer_set_max_size(IOBuffer *buf, size_t max_size)
 {
 	i_assert(!buf->mmaped);
 
 	buf->max_buffer_size = max_size;
 }
 
-void io_buffer_set_blocking(IOBuffer *buf, unsigned int max_size,
+void io_buffer_set_blocking(IOBuffer *buf, size_t max_size,
 			    int timeout_msecs, TimeoutFunc timeout_func,
 			    void *context)
 {
@@ -208,11 +209,11 @@
 		buf->max_buffer_size = max_size;
 }
 
-static int my_write(int fd, const void *buf, unsigned int size)
+static ssize_t my_write(int fd, const void *buf, size_t size)
 {
-	int ret;
+	ssize_t ret;
 
-	i_assert(size <= INT_MAX);
+	i_assert(size <= SSIZE_T_MAX);
 
 	if (size == 0)
 		return 0;
@@ -276,15 +277,18 @@
 
 static void block_loop_send(IOBufferBlockContext *ctx)
 {
-	int ret;
+	size_t size;
+	ssize_t ret;
 
 	if (ctx->outbuf->skip != ctx->outbuf->pos) {
 		buf_send_real(ctx->outbuf);
 	} else {
 		/* send the data */
+		size = MAX_SSIZE_T(ctx->size);
+
 		ret = !ctx->outbuf->file ?
-			net_transmit(ctx->outbuf->fd, ctx->data, ctx->size) :
-			my_write(ctx->outbuf->fd, ctx->data, ctx->size);
+			net_transmit(ctx->outbuf->fd, ctx->data, size) :
+			my_write(ctx->outbuf->fd, ctx->data, size);
 
 		if (ret < 0) {
 			ctx->outbuf->closed = TRUE;
@@ -357,7 +361,7 @@
 }
 
 static int io_buffer_send_blocking(IOBuffer *buf, const void *data,
-				   unsigned int size)
+				   size_t size)
 {
         IOBufferBlockContext ctx;
 
@@ -379,7 +383,7 @@
 	buf->corked = TRUE;
 }
 
-static void buffer_alloc_more(IOBuffer *buf, unsigned int size)
+static void buffer_alloc_more(IOBuffer *buf, size_t size)
 {
 	i_assert(!buf->mmaped);
 
@@ -412,13 +416,13 @@
 	buf->skip = 0;
 }
 
-int io_buffer_send(IOBuffer *buf, const void *data, unsigned int size)
+int io_buffer_send(IOBuffer *buf, const void *data, size_t size)
 {
 	int i, corked, ret;
 
 	i_assert(!buf->receive);
         i_assert(data != NULL);
-	i_assert(size < INT_MAX);
+	i_assert(size <= SSIZE_T_MAX);
 	buf->transmit = TRUE;
 
 	if (buf->closed)
@@ -479,19 +483,19 @@
 static void block_loop_sendfile(IOBufferBlockContext *ctx)
 {
 	uoff_t offset;
-	int ret;
+	ssize_t ret;
 
 	i_assert(ctx->inbuf->offset < OFF_T_MAX);
 
 	offset = ctx->inbuf->offset;
-	ret = safe_sendfile(ctx->outbuf->fd, ctx->inbuf->fd,
-			    &offset, ctx->size);
+	ret = safe_sendfile(ctx->outbuf->fd, ctx->inbuf->fd, &offset,
+			    MAX_SSIZE_T(ctx->size));
 	if (ret < 0) {
 		if (errno != EINTR && errno != EAGAIN)
 			ctx->outbuf->closed = TRUE;
 		ret = 0;
 	}
-	io_buffer_skip(ctx->inbuf, (unsigned int)ret);
+	io_buffer_skip(ctx->inbuf, (size_t)ret);
 
 	ctx->size -= ret;
 	if (ctx->outbuf->closed || ctx->size == 0)
@@ -499,11 +503,11 @@
 }
 
 static int io_buffer_sendfile(IOBuffer *outbuf, IOBuffer *inbuf,
-			      unsigned int size)
+			      uoff_t long_size)
 {
         IOBufferBlockContext ctx;
 	uoff_t offset;
-	int ret;
+	ssize_t ret;
 
 	i_assert(inbuf->offset < OFF_T_MAX);
 
@@ -511,15 +515,16 @@
 
 	/* first try if we can do it with a single sendfile() call */
 	offset = inbuf->offset;
-	ret = safe_sendfile(outbuf->fd, inbuf->fd, &offset, size);
+	ret = safe_sendfile(outbuf->fd, inbuf->fd, &offset,
+			    MAX_SSIZE_T(long_size));
 	if (ret < 0) {
 		if (errno != EINTR && errno != EAGAIN)
 			return -1;
 		ret = 0;
 	}
-	io_buffer_skip(inbuf, (unsigned int)ret);
+	io_buffer_skip(inbuf, (size_t)ret);
 
-	if ((unsigned int) ret == size) {
+	if ((uoff_t) ret == long_size) {
 		/* yes, all sent */
 		return 1;
 	}
@@ -527,7 +532,7 @@
 	memset(&ctx, 0, sizeof(ctx));
 
 	ctx.inbuf = inbuf;
-	ctx.size = size - ret;
+	ctx.size = long_size - ret;
 
 	ret = io_buffer_ioloop(outbuf, &ctx, block_loop_sendfile);
 	if (ret < 0 && errno == EINVAL) {
@@ -541,8 +546,8 @@
 static void block_loop_copy(IOBufferBlockContext *ctx)
 {
 	unsigned char *in_data;
-	unsigned int size, full_size, sent_size, data_size;
-	int ret;
+	size_t size, full_size, sent_size, data_size;
+	ssize_t ret;
 
 	while ((ret = io_buffer_read_data(ctx->inbuf, &in_data,
 					  &size, 0)) <= 0) {
@@ -569,17 +574,16 @@
 	io_buffer_skip(ctx->inbuf, sent_size);
 }
 
-int io_buffer_send_iobuffer(IOBuffer *outbuf, IOBuffer *inbuf,
-			    unsigned int size)
+int io_buffer_send_iobuffer(IOBuffer *outbuf, IOBuffer *inbuf, uoff_t size)
 {
 	IOBufferBlockContext ctx;
 	int ret;
 
-	i_assert(size < INT_MAX);
+	i_assert(size < OFF_T_MAX);
 
 	ret = io_buffer_sendfile(outbuf, inbuf, size);
-	if (ret >= 0 || errno != EINVAL)
-		return ret;
+	if (ret > 0 || errno != EINVAL)
+		return ret < 0 ? -1 : 1;
 
 	/* sendfile() not supported (with this fd), fallback to
 	   regular sending */
@@ -618,10 +622,10 @@
 	buf->flush_context = context;
 }
 
-static int io_buffer_read_mmaped(IOBuffer *buf, unsigned int size)
+static int io_buffer_read_mmaped(IOBuffer *buf, size_t size)
 {
 	uoff_t stop_offset;
-	unsigned int aligned_skip;
+	size_t aligned_skip;
 
 	stop_offset = buf->start_offset + buf->size;
 	if (stop_offset - buf->mmap_offset <= buf->buffer_size) {
@@ -663,14 +667,14 @@
 
 int io_buffer_read(IOBuffer *buf)
 {
-        return io_buffer_read_max(buf, UINT_MAX);
+        return io_buffer_read_max(buf, SSIZE_T_MAX);
 }
 
-int io_buffer_read_max(IOBuffer *buf, unsigned int size)
+int io_buffer_read_max(IOBuffer *buf, size_t size)
 {
-	int ret;
+	ssize_t ret;
 
-	i_assert(size <= INT_MAX || size == UINT_MAX);
+	i_assert(size <= SSIZE_T_MAX);
 	i_assert(!buf->transmit);
 	buf->receive = TRUE;
 
@@ -725,17 +729,17 @@
 {
 	IOBufferBlockContext *ctx = context;
 
-	if (io_buffer_read_max(ctx->inbuf, ctx->size) != 0) {
+	if (io_buffer_read_max(ctx->inbuf, (size_t)ctx->size) != 0) {
 		/* got data / error */
 		io_loop_stop(ctx->ioloop);
 	}
 }
 
-int io_buffer_read_blocking(IOBuffer *buf, unsigned int size)
+ssize_t io_buffer_read_blocking(IOBuffer *buf, size_t size)
 {
         IOBufferBlockContext ctx;
 	Timeout to;
-	int ret;
+	ssize_t ret;
 
 	/* first check if we can get some data */
 	ret = io_buffer_read_max(buf, size);
@@ -771,12 +775,13 @@
 
 	io_loop_destroy(ctx.ioloop);
 
-	return buf->pos > buf->skip ? 1 : -1;
+	return buf->pos > buf->skip ?
+		(ssize_t) (buf->pos-buf->skip) : -1;
 }
 
 void io_buffer_skip(IOBuffer *buf, uoff_t size)
 {
-	int ret;
+	ssize_t ret;
 
 	buf->offset += size;
 
@@ -803,7 +808,7 @@
 			size -= ret;
 		}
 
-		(void)io_buffer_read_max(buf, (unsigned int)size);
+		(void)io_buffer_read_max(buf, (size_t)size);
 	}
 }
 
@@ -853,7 +858,7 @@
 {
 	/* FIXME: buf->offset isn't updated right.. (skip_lf thing?) */
 	unsigned char *ret_buf;
-        unsigned int i;
+        size_t i;
 
         i_assert(buf != NULL);
 
@@ -880,7 +885,7 @@
         return ret_buf;
 }
 
-unsigned char *io_buffer_get_data(IOBuffer *buf, unsigned int *size)
+unsigned char *io_buffer_get_data(IOBuffer *buf, size_t *size)
 {
 	io_buffer_skip_lf(buf);
 
@@ -893,10 +898,10 @@
         return buf->buffer + buf->skip;
 }
 
-int io_buffer_read_data(IOBuffer *buf, unsigned char **data,
-			unsigned int *size, unsigned int threshold)
+ssize_t io_buffer_read_data(IOBuffer *buf, unsigned char **data,
+			    size_t *size, size_t threshold)
 {
-	int ret;
+	ssize_t ret;
 
 	if (buf->pos - buf->skip > threshold)
 		ret = 1;
@@ -914,10 +919,10 @@
 	return ret;
 }
 
-unsigned char *io_buffer_get_space(IOBuffer *buf, unsigned int size)
+unsigned char *io_buffer_get_space(IOBuffer *buf, size_t size)
 {
 	i_assert(size > 0);
-	i_assert(size <= INT_MAX);
+	i_assert(size <= SSIZE_T_MAX);
 	i_assert(!buf->receive);
 	buf->transmit = TRUE;
 
@@ -940,11 +945,11 @@
         return buf->buffer + buf->pos;
 }
 
-int io_buffer_send_buffer(IOBuffer *buf, unsigned int size)
+int io_buffer_send_buffer(IOBuffer *buf, size_t size)
 {
-	int ret;
+	ssize_t ret;
 
-	i_assert(size <= INT_MAX);
+	i_assert(size <= SSIZE_T_MAX);
 	i_assert(!buf->receive);
 
 	if (buf->pos == 0 && !buf->corked) {
@@ -959,7 +964,7 @@
 		}
 
 		buf->offset += ret;
-		if ((unsigned int) ret == size) {
+		if ((size_t)ret == size) {
                         /* all sent */
 			return 1;
 		}
@@ -977,7 +982,7 @@
         return 1;
 }
 
-int io_buffer_set_data(IOBuffer *buf, const void *data, unsigned int size)
+int io_buffer_set_data(IOBuffer *buf, const void *data, size_t size)
 {
 	i_assert(!buf->mmaped);
 
--- a/src/lib/iobuffer.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/iobuffer.h	Sun Sep 08 16:20:28 2002 +0300
@@ -27,11 +27,11 @@
 	void *flush_context;
 
 	unsigned char *buffer;
-        unsigned int cr_lookup_pos; /* used only when reading a line */
+        size_t cr_lookup_pos; /* used only when reading a line */
 
 	off_t mmap_offset;
-	unsigned int pos, skip;
-	unsigned int buffer_size, max_buffer_size;
+	size_t pos, skip;
+	size_t buffer_size, max_buffer_size;
 
 	unsigned int file:1; /* reading/writing a file */
 	unsigned int mmaped:1; /* reading a file with mmap() */
@@ -47,13 +47,12 @@
 /* Create an I/O buffer. It can be used for either sending or receiving data,
    NEVER BOTH AT SAME TIME. */
 IOBuffer *io_buffer_create(int fd, Pool pool, int priority,
-			   unsigned int max_buffer_size);
+			   size_t max_buffer_size);
 /* Same as io_buffer_create(), but specify that we're reading/writing file. */
-IOBuffer *io_buffer_create_file(int fd, Pool pool,
-				unsigned int max_buffer_size);
+IOBuffer *io_buffer_create_file(int fd, Pool pool, size_t max_buffer_size);
 /* Read the file by mmap()ing it in blocks. stop_offset specifies where to
    stop reading, or 0 to end of file. */
-IOBuffer *io_buffer_create_mmap(int fd, Pool pool, unsigned int block_size,
+IOBuffer *io_buffer_create_mmap(int fd, Pool pool, size_t block_size,
 				uoff_t size);
 /* Destroy a buffer. */
 void io_buffer_destroy(IOBuffer *buf);
@@ -69,7 +68,7 @@
    buffer will be transferred to new buffer. */
 IOBuffer *io_buffer_set_pool(IOBuffer *buf, Pool pool);
 /* Change the maximum size for buffer to grow. */
-void io_buffer_set_max_size(IOBuffer *buf, unsigned int max_size);
+void io_buffer_set_max_size(IOBuffer *buf, size_t max_size);
 /* Change buffer's blocking state. The blocking state in fd itself isn't
    changed, and it's not needed to be blocking. This affects two things:
 
@@ -83,7 +82,7 @@
 
    timeout_func is called with both cases when timeout occurs.
 */
-void io_buffer_set_blocking(IOBuffer *buf, unsigned int max_size,
+void io_buffer_set_blocking(IOBuffer *buf, size_t max_size,
 			    int timeout_msecs, TimeoutFunc timeout_func,
 			    void *context);
 
@@ -92,12 +91,12 @@
 void io_buffer_cork(IOBuffer *buf);
 
 /* Returns 1 if all was ok, -1 if disconnected, -2 if buffer is full */
-int io_buffer_send(IOBuffer *buf, const void *data, unsigned int size);
+int io_buffer_send(IOBuffer *buf, const void *data, size_t size);
 /* Send data from input buffer to output buffer using the fastest
-   possible method. Returns 1 if all was ok, -1 if disconnected.
+   possible method. Returns 1 if all sent or -1 if disconnected.
    Note that this function may block. */
 int io_buffer_send_iobuffer(IOBuffer *outbuf, IOBuffer *inbuf,
-			    unsigned int size);
+			    uoff_t long_size);
 /* Flush the output buffer, blocks until all is sent. If
    io_buffer_set_send_blocking() is called, it's timeout settings are used. */
 void io_buffer_send_flush(IOBuffer *buf);
@@ -109,14 +108,14 @@
 
 /* Returns number of bytes read if read was ok,
    -1 if disconnected / EOF, -2 if the buffer is full */
-int io_buffer_read(IOBuffer *buf);
+ssize_t io_buffer_read(IOBuffer *buf);
 /* Like io_buffer_read(), but don't read more than specified size. */
-int io_buffer_read_max(IOBuffer *buf, unsigned int size);
+ssize_t io_buffer_read_max(IOBuffer *buf, size_t size);
 /* Blocking read, doesn't return until at least one byte is read, or until
    socket is disconnected or timeout has occured. Note that the fd must be
    nonblocking, or the timeout doesn't work. If you don't want limit size,
-   set it to (unsigned int)-1. Returns 1 if data was found, -1 if error. */
-int io_buffer_read_blocking(IOBuffer *buf, unsigned int size);
+   set it to (size_t)-1. Returns number of bytes read, or -1 if error. */
+ssize_t io_buffer_read_blocking(IOBuffer *buf, size_t size);
 /* Skip forward a number of bytes */
 void io_buffer_skip(IOBuffer *buf, uoff_t size);
 /* Seek to specified position from beginning of file. This works only for
@@ -128,23 +127,23 @@
 char *io_buffer_next_line(IOBuffer *buf);
 /* Returns pointer to beginning of data in buffer,
    or NULL if there's no data. */
-unsigned char *io_buffer_get_data(IOBuffer *buf, unsigned int *size);
+unsigned char *io_buffer_get_data(IOBuffer *buf, size_t *size);
 /* Like io_buffer_get_data(), but read it when needed. There always must be
    more than `threshold' bytes in buffer. Returns 1 if data was read, 0 if
    read was interrupted or nonblocking, -1 if EOF / error */
 int io_buffer_read_data(IOBuffer *buf, unsigned char **data,
-			unsigned int *size, unsigned int threshold);
+			size_t *size, size_t threshold);
 
 /* Returns a pointer to buffer wanted amount of space,
    or NULL if size is too big. */
-unsigned char *io_buffer_get_space(IOBuffer *buf, unsigned int size);
+unsigned char *io_buffer_get_space(IOBuffer *buf, size_t size);
 /* Send data saved to buffer from io_buffer_get_space().
    Returns -1 if disconnected. */
-int io_buffer_send_buffer(IOBuffer *buf, unsigned int size);
+int io_buffer_send_buffer(IOBuffer *buf, size_t size);
 
 /* Put data to buffer as if it was received.
    Returns 1 if successful, -2 if buffer isn't big enough. */
-int io_buffer_set_data(IOBuffer *buf, const void *data, unsigned int size);
+int io_buffer_set_data(IOBuffer *buf, const void *data, size_t size);
 /* Returns TRUE if there's nothing in buffer. */
 int io_buffer_is_empty(IOBuffer *buf);
 
--- a/src/lib/md5.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/md5.c	Sun Sep 08 16:20:28 2002 +0300
@@ -66,7 +66,7 @@
  * This processes one or more 64-byte data blocks, but does NOT update
  * the bit counters.  There're no alignment requirements.
  */
-static const void *body(MD5Context *ctx, const void *data, unsigned int size)
+static const void *body(MD5Context *ctx, const void *data, size_t size)
 {
 	const unsigned char *ptr;
 	MD5_u32plus a, b, c, d;
@@ -184,7 +184,7 @@
 	ctx->hi = 0;
 }
 
-void md5_update(MD5Context *ctx, const void *data, unsigned int size)
+void md5_update(MD5Context *ctx, const void *data, size_t size)
 {
 	MD5_u32plus saved_lo;
 	unsigned long used, free;
@@ -269,8 +269,7 @@
 	memset(ctx, 0, sizeof(ctx));
 }
 
-void md5_get_digest(const void *data, unsigned int size,
-		    unsigned char result[16])
+void md5_get_digest(const void *data, size_t size, unsigned char result[16])
 {
 	MD5Context ctx;
 
--- a/src/lib/md5.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/md5.h	Sun Sep 08 16:20:28 2002 +0300
@@ -20,10 +20,9 @@
 } MD5Context;
 
 void md5_init(MD5Context *ctx);
-void md5_update(MD5Context *ctx, const void *data, unsigned int size);
+void md5_update(MD5Context *ctx, const void *data, size_t size);
 void md5_final(MD5Context *ctx, unsigned char result[16]);
 
-void md5_get_digest(const void *data, unsigned int size,
-		    unsigned char result[16]);
+void md5_get_digest(const void *data, size_t size, unsigned char result[16]);
 
 #endif
--- a/src/lib/mempool-alloconly.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/mempool-alloconly.c	Sun Sep 08 16:20:28 2002 +0300
@@ -29,7 +29,7 @@
 
 #include <stdlib.h>
 
-#define MAX_ALLOC_SIZE (UINT_MAX - sizeof(unsigned int))
+#define MAX_ALLOC_SIZE SSIZE_T_MAX
 
 typedef struct _PoolBlock PoolBlock;
 
@@ -38,7 +38,7 @@
 	int refcount;
 
 	PoolBlock *block;
-	unsigned int last_alloc_size;
+	size_t last_alloc_size;
 
 	char name[MEM_ALIGN_SIZE]; /* variable size */
 } AlloconlyPool;
@@ -47,8 +47,8 @@
 struct _PoolBlock {
 	PoolBlock *prev;
 
-	unsigned int size;
-	unsigned int left;
+	size_t size;
+	size_t left;
 
 	/* unsigned char data[]; */
 };
@@ -59,7 +59,7 @@
 
 typedef struct {
 	union {
-		unsigned int size;
+		size_t size;
 		unsigned char alignment[MEM_ALIGN_SIZE];
 	} size;
 	unsigned char data[MEM_ALIGN_SIZE]; /* variable size */
@@ -69,11 +69,10 @@
 static struct Pool static_alloconly_pool;
 static void pool_alloconly_clear(Pool pool);
 
-static void block_alloc(AlloconlyPool *pool, unsigned int size);
-static void *pool_alloconly_realloc_min(Pool pool, void *mem,
-					unsigned int size);
+static void block_alloc(AlloconlyPool *pool, size_t size);
+static void *pool_alloconly_realloc_min(Pool pool, void *mem, size_t size);
 
-Pool pool_alloconly_create(const char *name, unsigned int size)
+Pool pool_alloconly_create(const char *name, size_t size)
 {
 	AlloconlyPool *apool;
 	int len;
@@ -115,7 +114,7 @@
 		pool_alloconly_destroy(apool);
 }
 
-static void block_alloc(AlloconlyPool *apool, unsigned int size)
+static void block_alloc(AlloconlyPool *apool, size_t size)
 {
 	PoolBlock *block;
 
@@ -135,7 +134,7 @@
 	block->left = block->size;
 }
 
-static void *pool_alloconly_malloc(Pool pool, unsigned int size)
+static void *pool_alloconly_malloc(Pool pool, size_t size)
 {
 	AlloconlyPool *apool = (AlloconlyPool *) pool;
 	PoolAlloc *alloc;
@@ -162,14 +161,14 @@
 	/* ignore */
 }
 
-static void *pool_alloconly_realloc(Pool pool, void *mem, unsigned int size)
+static void *pool_alloconly_realloc(Pool pool, void *mem, size_t size)
 {
 	/* there's no point in shrinking the memory usage,
 	   so just do the same as realloc_min() */
 	return pool_alloconly_realloc_min(pool, mem, size);
 }
 
-static int pool_try_grow(AlloconlyPool *apool, void *mem, unsigned int size)
+static int pool_try_grow(AlloconlyPool *apool, void *mem, size_t size)
 {
 	/* see if we want to grow the memory we allocated last */
 	if (POOL_BLOCK_DATA(apool->block) + (apool->block->size -
@@ -187,12 +186,12 @@
 	return FALSE;
 }
 
-static void *pool_alloconly_realloc_min(Pool pool, void *mem, unsigned int size)
+static void *pool_alloconly_realloc_min(Pool pool, void *mem, size_t size)
 {
 	AlloconlyPool *apool = (AlloconlyPool *) pool;
 	PoolAlloc *alloc;
 	unsigned char *new_mem;
-	unsigned int old_size;
+	size_t old_size;
 
 	if (mem == NULL) {
 		alloc = NULL;
--- a/src/lib/mempool-system.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/mempool-system.c	Sun Sep 08 16:20:28 2002 +0300
@@ -28,11 +28,11 @@
 
 #include <stdlib.h>
 
-#define MAX_ALLOC_SIZE (UINT_MAX - sizeof(unsigned int))
+#define MAX_ALLOC_SIZE SSIZE_T_MAX
 
 typedef struct {
 	union {
-		unsigned int size;
+		size_t size;
 		unsigned char alignment[MEM_ALIGN_SIZE];
 	} size;
 	/* void data[]; */
@@ -50,7 +50,7 @@
 {
 }
 
-static void *pool_system_malloc(Pool pool __attr_unused__, unsigned int size)
+static void *pool_system_malloc(Pool pool __attr_unused__, size_t size)
 {
 	PoolAlloc *alloc;
 
@@ -72,10 +72,10 @@
 }
 
 static void *pool_system_realloc(Pool pool __attr_unused__, void *mem,
-				 unsigned int size)
+				 size_t size)
 {
 	PoolAlloc *alloc;
-	unsigned int old_size;
+	size_t old_size;
 	char *rmem;
 
 	if (mem == NULL) {
@@ -102,10 +102,10 @@
         return rmem;
 }
 
-static void *pool_system_realloc_min(Pool pool, void *mem, unsigned int size)
+static void *pool_system_realloc_min(Pool pool, void *mem, size_t size)
 {
 	PoolAlloc *alloc;
-        unsigned int old_size;
+        size_t old_size;
 
 	if (mem == NULL)
 		old_size = 0;
--- a/src/lib/mempool.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/mempool.c	Sun Sep 08 16:20:28 2002 +0300
@@ -26,9 +26,9 @@
 #include "lib.h"
 #include "mempool.h"
 
-Pool pool_alloconly_create(const char *name, unsigned int size);
+Pool pool_alloconly_create(const char *name, size_t size);
 
-Pool pool_create(const char *name, unsigned int size, int allocfree)
+Pool pool_create(const char *name, size_t size, int allocfree)
 {
 	if (allocfree)
 		return system_pool;
--- a/src/lib/mempool.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/mempool.h	Sun Sep 08 16:20:28 2002 +0300
@@ -15,13 +15,13 @@
 	void (*ref)(Pool pool);
 	void (*unref)(Pool pool);
 
-	void *(*malloc)(Pool pool, unsigned int size);
+	void *(*malloc)(Pool pool, size_t size);
 	void (*free)(Pool pool, void *mem);
 
 	/* reallocate the `mem' to be exactly `size' */
-	void *(*realloc)(Pool pool, void *mem, unsigned int size);
+	void *(*realloc)(Pool pool, void *mem, size_t size);
 	/* reallocate the `mem' to be at least `size' if it wasn't previously */
-	void *(*realloc_min)(Pool pool, void *mem, unsigned int size);
+	void *(*realloc_min)(Pool pool, void *mem, size_t size);
 
 	/* Frees all the memory in pool. NOTE: system_pool doesn't support
 	   this and crashes if it's used */
@@ -33,7 +33,7 @@
 
 /* If allocfree is FALSE, p_free() has no effect. Note that `size' specifies
    the initial malloc()ed block size, part of it is used internally. */
-Pool pool_create(const char *name, unsigned int size, int allocfree);
+Pool pool_create(const char *name, size_t size, int allocfree);
 
 /* Pools should be used through these macros: */
 #define pool_ref(pool) (pool)->ref(pool)
--- a/src/lib/mmap-util.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/mmap-util.c	Sun Sep 08 16:20:28 2002 +0300
@@ -34,7 +34,7 @@
 	if (size == -1)
 		return MAP_FAILED;
 
-	if (size > INT_MAX) {
+	if (size > SSIZE_T_MAX) {
 		/* too large file to map into memory */
 		errno = EFBIG;
 		return MAP_FAILED;
@@ -44,7 +44,7 @@
 	if (*length == 0)
 		return NULL;
 
-	i_assert(*length > 0 && *length < INT_MAX);
+	i_assert(*length > 0 && *length < SSIZE_T_MAX);
 
 	return mmap(NULL, *length, access, MAP_SHARED, fd, 0);
 }
--- a/src/lib/network.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/network.c	Sun Sep 08 16:20:28 2002 +0300
@@ -342,13 +342,13 @@
 }
 
 /* Read data from socket, return number of bytes read, -1 = error */
-int net_receive(int fd, void *buf, unsigned int len)
+ssize_t net_receive(int fd, void *buf, size_t len)
 {
-	int ret;
+	ssize_t ret;
 
 	i_assert(fd >= 0);
 	i_assert(buf != NULL);
-	i_assert(len <= INT_MAX);
+	i_assert(len <= SSIZE_T_MAX);
 
 	ret = recv(fd, buf, len, 0);
 	if (ret == 0)
@@ -361,13 +361,13 @@
 }
 
 /* Transmit data, return number of bytes sent, -1 = error */
-int net_transmit(int fd, const void *data, unsigned int len)
+ssize_t net_transmit(int fd, const void *data, size_t len)
 {
-        int ret;
+        ssize_t ret;
 
 	i_assert(fd >= 0);
 	i_assert(data != NULL);
-	i_assert(len <= INT_MAX);
+	i_assert(len <= SSIZE_T_MAX);
 
 	ret = send(fd, data, len, 0);
 	if (ret == -1 && (errno == EINTR || errno == EPIPE || errno == EAGAIN))
--- a/src/lib/network.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/network.h	Sun Sep 08 16:20:28 2002 +0300
@@ -65,9 +65,9 @@
 int net_accept(int fd, IPADDR *addr, int *port);
 
 /* Read data from socket, return number of bytes read, -1 = error */
-int net_receive(int fd, void *buf, unsigned int len);
+ssize_t net_receive(int fd, void *buf, size_t len);
 /* Transmit data, return number of bytes sent, -1 = error */
-int net_transmit(int fd, const void *data, unsigned int len);
+ssize_t net_transmit(int fd, const void *data, size_t len);
 
 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need
    to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
--- a/src/lib/randgen.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/randgen.c	Sun Sep 08 16:20:28 2002 +0300
@@ -32,13 +32,13 @@
 static int init_refcount = 0;
 static int urandom_fd;
 
-void random_fill(void *buf, unsigned int size)
+void random_fill(void *buf, size_t size)
 {
-	unsigned int pos;
-	int ret;
+	size_t pos;
+	ssize_t ret;
 
 	i_assert(init_refcount > 0);
-	i_assert(size < INT_MAX);
+	i_assert(size < SSIZE_T_MAX);
 
 	for (pos = 0; pos < size; pos += ret) {
 		ret = read(urandom_fd, (char *) buf + pos, size - pos);
--- a/src/lib/randgen.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/randgen.h	Sun Sep 08 16:20:28 2002 +0300
@@ -1,7 +1,7 @@
 #ifndef __RANDGEN_H
 #define __RANDGEN_H
 
-void random_fill(void *buf, unsigned int size);
+void random_fill(void *buf, size_t size);
 
 /* may be called multiple times */
 void random_init(void);
--- a/src/lib/strfuncs.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/strfuncs.c	Sun Sep 08 16:20:28 2002 +0300
@@ -33,9 +33,9 @@
 
 #define STRCONCAT_BUFSIZE 512
 
-typedef void *(*ALLOC_FUNC)(Pool, unsigned int);
+typedef void *(*ALLOC_FUNC)(Pool, size_t);
 
-static void *tp_malloc(Pool pool __attr_unused__, unsigned int size)
+static void *tp_malloc(Pool pool __attr_unused__, size_t size)
 {
         return t_malloc(size);
 }
@@ -85,9 +85,9 @@
 #  define HONOUR_LONGS 0
 #endif
 
-unsigned int printf_string_upper_bound(const char *format, va_list args)
+size_t printf_string_upper_bound(const char *format, va_list args)
 {
-  int len = 1;
+  size_t len = 1;
 
   if (!format)
     return len;
@@ -385,13 +385,13 @@
 {
 	const char *errstr;
 	char *buf;
-	unsigned int pos, alloc, errlen;
+	size_t pos, alloc, errlen;
 
 	errstr = strerror(errno);
 	errlen = strlen(errstr);
 
-	pos = (unsigned int) (p-fmt);
-	i_assert(pos < INT_MAX);
+	pos = (size_t) (p-fmt);
+	i_assert(pos < SSIZE_T_MAX);
 
 	alloc = pos + errlen + 128;
 	buf = t_buffer_get(alloc);
@@ -438,14 +438,14 @@
 	return fmt;
 }
 
-int i_snprintf(char *str, unsigned int max_chars, const char *format, ...)
+int i_snprintf(char *str, size_t max_chars, const char *format, ...)
 {
 #ifdef HAVE_VSNPRINTF
 	va_list args;
 	int ret;
 
 	i_assert(str != NULL);
-	i_assert(max_chars < INT_MAX);
+	i_assert(max_chars < SSIZE_T_MAX);
 	i_assert(format != NULL);
 
 	va_start(args, format);
@@ -464,7 +464,7 @@
         int len;
 
 	i_assert(str != NULL);
-	i_assert(max_chars < INT_MAX);
+	i_assert(max_chars < SSIZE_T_MAX);
 	i_assert(format != NULL);
 
 	va_start(args, format);
@@ -484,7 +484,7 @@
 
 #define STRDUP_CORE(alloc_func, str) STMT_START { \
 	void *mem;				\
-	unsigned int len;			\
+	size_t len;				\
 						\
 	for (len = 0; (str)[len] != '\0'; )	\
 		len++;				\
@@ -554,13 +554,13 @@
 
 char *p_strdup_until(Pool pool, const char *start, const char *end)
 {
-	unsigned int size;
+	size_t size;
 	char *mem;
 
 	i_assert(start <= end);
 
-	size = (unsigned int) (end-start);
-	i_assert(size < UINT_MAX);
+	size = (size_t) (end-start);
+	i_assert(size < SSIZE_T_MAX);
 
 	mem = p_malloc(pool, size + 1);
 	memcpy(mem, start, size);
@@ -569,13 +569,13 @@
 
 const char *t_strdup_until(const char *start, const char *end)
 {
-	unsigned int size;
+	size_t size;
 	char *mem;
 
 	i_assert(start <= end);
 
-	size = (unsigned int) (end-start);
-	i_assert(size < UINT_MAX);
+	size = (size_t) (end-start);
+	i_assert(size < SSIZE_T_MAX);
 
 	mem = t_malloc(size + 1);
 	memcpy(mem, start, size);
@@ -583,14 +583,13 @@
 	return mem;
 }
 
-static inline char *
-strndup_core(const char *str, unsigned int max_chars,
-	     ALLOC_FUNC alloc, Pool pool)
+static inline char *strndup_core(const char *str, size_t max_chars,
+				 ALLOC_FUNC alloc, Pool pool)
 {
 	char *mem;
-	unsigned int len;
+	size_t len;
 
-	i_assert(max_chars < INT_MAX);
+	i_assert(max_chars < SSIZE_T_MAX);
 
 	if (str == NULL)
 		return NULL;
@@ -605,12 +604,12 @@
 	return mem;
 }
 
-char *p_strndup(Pool pool, const char *str, unsigned int max_chars)
+char *p_strndup(Pool pool, const char *str, size_t max_chars)
 {
         return strndup_core(str, max_chars, pool->malloc, pool);
 }
 
-const char *t_strndup(const char *str, unsigned int max_chars)
+const char *t_strndup(const char *str, size_t max_chars)
 {
         return strndup_core(str, max_chars, tp_malloc, NULL);
 }
@@ -677,11 +676,11 @@
 }
 
 const char *temp_strconcat(const char *str1, va_list args,
-			   unsigned int *ret_len)
+			   size_t *ret_len)
 {
 	const char *str;
         char *temp;
-	unsigned int full_len, len, bufsize;
+	size_t full_len, len, bufsize;
 
 	if (str1 == NULL)
 		return NULL;
@@ -720,7 +719,7 @@
 	va_list args;
         const char *temp;
 	char *ret;
-        unsigned int len;
+        size_t len;
 
 	va_start(args, str1);
 
@@ -740,7 +739,7 @@
 {
 	va_list args;
 	const char *ret;
-        unsigned int len;
+        size_t len;
 
 	va_start(args, str1);
 
@@ -857,7 +856,7 @@
 {
         char **array;
 	char *str;
-        int alloc_len, len;
+        size_t alloc_len, len;
 
         i_assert(*separators != '\0');
 
@@ -896,7 +895,7 @@
 {
         const char *arg;
         char *data;
-	unsigned int alloc_len, arg_len, full_len;
+	size_t alloc_len, arg_len, full_len;
 	int i;
 
 	if (args[0] == NULL)
--- a/src/lib/strfuncs.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/strfuncs.h	Sun Sep 08 16:20:28 2002 +0300
@@ -4,14 +4,14 @@
 #define is_empty_str(str) \
         ((str) == NULL || (str)[0] == '\0')
 
-unsigned int printf_string_upper_bound(const char *format, va_list args);
-int i_snprintf(char *str, unsigned int max_chars, const char *format, ...)
+size_t printf_string_upper_bound(const char *format, va_list args);
+int i_snprintf(char *str, size_t max_chars, const char *format, ...)
 	__attr_format__(3, 4);
 
 char *p_strdup(Pool pool, const char *str);
 char *p_strdup_empty(Pool pool, const char *str); /* return NULL if str = "" */
 char *p_strdup_until(Pool pool, const char *start, const char *end); /* *end isn't included */
-char *p_strndup(Pool pool, const char *str, unsigned int max_chars);
+char *p_strndup(Pool pool, const char *str, size_t max_chars);
 char *p_strdup_printf(Pool pool, const char *format, ...) __attr_format__(2, 3);
 char *p_strdup_vprintf(Pool pool, const char *format, va_list args);
 void p_strdup_replace(Pool pool, char **dest, const char *str);
@@ -24,7 +24,7 @@
 char *t_strdup_noconst(const char *str);
 const char *t_strdup_empty(const char *str); /* return NULL if str = "" */
 const char *t_strdup_until(const char *start, const char *end); /* *end isn't included */
-const char *t_strndup(const char *str, unsigned int max_chars);
+const char *t_strndup(const char *str, size_t max_chars);
 const char *t_strdup_printf(const char *format, ...) __attr_format__(1, 2);
 const char *t_strdup_vprintf(const char *format, va_list args);
 const int *t_intarrdup(const int *arr);
@@ -58,7 +58,6 @@
 void dec2str(char *buffer, size_t size, largest_t number);
 
 /* INTERNAL */
-const char *temp_strconcat(const char *str1, va_list args,
-			   unsigned int *ret_len);
+const char *temp_strconcat(const char *str1, va_list args, size_t *ret_len);
 
 #endif
--- a/src/lib/temp-mempool.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/temp-mempool.c	Sun Sep 08 16:20:28 2002 +0300
@@ -35,7 +35,7 @@
 /* max. number of bytes to even try to allocate. This is done just to avoid
    allocating less memory than was actually requested because of integer
    overflows. */
-#define MAX_ALLOC_SIZE (UINT_MAX - (MEM_ALIGN_SIZE-1))
+#define MAX_ALLOC_SIZE SSIZE_T_MAX
 
 /* Initial pool size - this should be kept in a size that doesn't exceed
    in a normal use to keep it fast. */
@@ -47,7 +47,7 @@
 struct _MemBlock {
 	MemBlock *next;
 
-	unsigned int size, left;
+	size_t size, left;
 	/* unsigned char data[]; */
 };
 
@@ -77,7 +77,7 @@
 static int last_alloc_size;
 
 static MemBlock *last_buffer_block;
-static unsigned int last_buffer_size;
+static size_t last_buffer_size;
 
 int t_push(void)
 {
@@ -156,10 +156,10 @@
         return stack_pos;
 }
 
-static MemBlock *mem_block_alloc(unsigned int min_size)
+static MemBlock *mem_block_alloc(size_t min_size)
 {
 	MemBlock *block;
-	unsigned int prev_size, alloc_size;
+	size_t prev_size, alloc_size;
 
 	prev_size = current_block == NULL ? 0 : current_block->size;
 	alloc_size = nearest_power(prev_size + min_size);
@@ -167,7 +167,7 @@
 	block = malloc(SIZEOF_MEMBLOCK + alloc_size);
 	if (block == NULL) {
 		i_panic("mem_block_alloc(): "
-			"Out of memory when allocating %u bytes",
+			"Out of memory when allocating %"PRIuSIZE_T" bytes",
 			SIZEOF_MEMBLOCK + alloc_size);
 	}
 	block->size = alloc_size;
@@ -176,7 +176,7 @@
 	return block;
 }
 
-static void *t_malloc_real(unsigned int size, int permanent)
+static void *t_malloc_real(size_t size, int permanent)
 {
 	MemBlock *block;
         void *ret;
@@ -227,12 +227,12 @@
         return MEM_BLOCK_DATA(current_block);
 }
 
-void *t_malloc(unsigned int size)
+void *t_malloc(size_t size)
 {
         return t_malloc_real(size, TRUE);
 }
 
-void *t_malloc0(unsigned int size)
+void *t_malloc0(size_t size)
 {
 	void *mem;
 
@@ -241,7 +241,7 @@
         return mem;
 }
 
-int t_try_grow(void *mem, unsigned int size)
+int t_try_grow(void *mem, size_t size)
 {
 	/* see if we want to grow the memory we allocated last */
 	if (MEM_BLOCK_DATA(current_block) +
@@ -260,7 +260,7 @@
 	return FALSE;
 }
 
-void *t_buffer_get(unsigned int size)
+void *t_buffer_get(size_t size)
 {
 	void *ret;
 
@@ -271,9 +271,9 @@
 	return ret;
 }
 
-void *t_buffer_reget(void *buffer, unsigned int size)
+void *t_buffer_reget(void *buffer, size_t size)
 {
-	unsigned int old_size;
+	size_t old_size;
         void *new_buffer;
 
 	old_size = last_buffer_size;
@@ -287,7 +287,7 @@
         return new_buffer;
 }
 
-void t_buffer_alloc(unsigned int size)
+void t_buffer_alloc(size_t size)
 {
 	i_assert(last_buffer_block != NULL);
 	i_assert(last_buffer_size >= size);
@@ -399,7 +399,7 @@
 	}
 }
 
-void *t_malloc(unsigned int size)
+void *t_malloc(size_t size)
 {
 	void *mem;
 
@@ -408,7 +408,7 @@
 	return mem;
 }
 
-void *t_malloc0(unsigned int size)
+void *t_malloc0(size_t size)
 {
 	void *mem;
 
@@ -417,7 +417,7 @@
 	return mem;
 }
 
-int t_try_grow(void *mem, unsigned int size)
+int t_try_grow(void *mem, size_t size)
 {
 	void *new_mem;
 
@@ -429,13 +429,13 @@
 	return FALSE;
 }
 
-void *t_buffer_get(unsigned int size)
+void *t_buffer_get(size_t size)
 {
 	buffer_mem = realloc(buffer_mem, size);
 	return buffer_mem;
 }
 
-void *t_buffer_reget(void *buffer, unsigned int size)
+void *t_buffer_reget(void *buffer, size_t size)
 {
 	i_assert(buffer == buffer_mem);
 
@@ -443,7 +443,7 @@
 	return buffer_mem;
 }
 
-void t_buffer_alloc(unsigned int size)
+void t_buffer_alloc(size_t size)
 {
 	void *mem;
 
--- a/src/lib/temp-mempool.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/temp-mempool.h	Sun Sep 08 16:20:28 2002 +0300
@@ -13,11 +13,11 @@
    create functions that return 'const xxx*' types and use t_malloc()
    internally in them. This is a lot safer, since usually compiler
    warns if you try to place them in xxx*. See strfuncs.c for examples. */
-void *t_malloc(unsigned int size);
-void *t_malloc0(unsigned int size);
+void *t_malloc(size_t size);
+void *t_malloc0(size_t size);
 
 /* Try growing allocated memory. Returns TRUE if successful. */
-int t_try_grow(void *mem, unsigned int size);
+int t_try_grow(void *mem, size_t size);
 
 #define t_new(type, count) \
 	((type *) t_malloc0((unsigned) sizeof(type) * (count)))
@@ -31,19 +31,19 @@
    new one (or do some other trickery). See t_buffer_reget(). */
 #define t_buffer_get_type(type, size) \
 	t_buffer_get(sizeof(type) * (size))
-void *t_buffer_get(unsigned int size);
+void *t_buffer_get(size_t size);
 
 /* Grow the buffer, memcpy()ing the memory to new location if needed. */
 #define t_buffer_reget_type(buffer, type, size) \
 	t_buffer_reget(buffer, sizeof(type) * (size))
-void *t_buffer_reget(void *buffer, unsigned int size);
+void *t_buffer_reget(void *buffer, size_t size);
 
 /* Make given t_buffer_get()ed buffer permanent. Note that size MUST be
    less or equal than the size you gave with last t_buffer_get() or the
    result will be undefined. */
 #define t_buffer_alloc_type(type, size) \
         t_buffer_alloc(sizeof(type) * (size))
-void t_buffer_alloc(unsigned int size);
+void t_buffer_alloc(size_t size);
 
 void temp_mempool_init(void);
 void temp_mempool_deinit(void);
--- a/src/lib/temp-string.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/temp-string.c	Sun Sep 08 16:20:28 2002 +0300
@@ -30,12 +30,12 @@
 
 typedef struct {
 	char *str;
-	unsigned int len;
+	size_t len;
 
-	unsigned int alloc_size;
+	size_t alloc_size;
 } RealTempString;
 
-TempString *t_string_new(unsigned int initial_size)
+TempString *t_string_new(size_t initial_size)
 {
 	RealTempString *rstr;
 
@@ -49,15 +49,16 @@
 	return (TempString *) rstr;
 }
 
-static void t_string_inc(TempString *tstr, unsigned int size)
+static void t_string_inc(TempString *tstr, size_t size)
 {
 	RealTempString *rstr = (RealTempString *) tstr;
 	char *str;
 
 	size += rstr->len + 1;
-	if (size <= rstr->len || size > INT_MAX) {
+	if (size <= rstr->len || size > SSIZE_T_MAX) {
 		/* overflow */
-		i_panic("t_string_inc(): Out of memory for %u bytes", size);
+		i_panic("t_string_inc(): Out of memory %"PRIuSIZE_T" bytes",
+			size);
 	}
 
 	if (size > rstr->alloc_size) {
@@ -77,9 +78,9 @@
 	t_string_append_n(tstr, str, strlen(str));
 }
 
-void t_string_append_n(TempString *tstr, const char *str, unsigned int size)
+void t_string_append_n(TempString *tstr, const char *str, size_t size)
 {
-	i_assert(size < INT_MAX);
+	i_assert(size < SSIZE_T_MAX);
 
 	t_string_inc(tstr, size);
 	memcpy(tstr->str + tstr->len, str, size);
@@ -108,7 +109,7 @@
 	va_end(args);
 }
 
-void t_string_erase(TempString *tstr, unsigned int pos, unsigned int len)
+void t_string_erase(TempString *tstr, size_t pos, size_t len)
 {
 	i_assert(pos < tstr->len && tstr->len - pos >= len);
 
@@ -116,7 +117,7 @@
 		tstr->len - pos - len + 1);
 }
 
-void t_string_truncate(TempString *tstr, unsigned int len)
+void t_string_truncate(TempString *tstr, size_t len)
 {
 	i_assert(len <= tstr->len);
 
--- a/src/lib/temp-string.h	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/temp-string.h	Sun Sep 08 16:20:28 2002 +0300
@@ -6,14 +6,14 @@
 
 struct _TempString {
 	char *str;
-	unsigned int len;
+	size_t len;
 };
 
-TempString *t_string_new(unsigned int initial_size);
+TempString *t_string_new(size_t initial_size);
 
 /* Append string/character */
 void t_string_append(TempString *tstr, const char *str);
-void t_string_append_n(TempString *tstr, const char *str, unsigned int size);
+void t_string_append_n(TempString *tstr, const char *str, size_t size);
 void t_string_append_c(TempString *tstr, char chr);
 
 /* Insert string/character (FIXME: not implemented) */
@@ -26,7 +26,7 @@
 	__attr_format__(2, 3);
 
 /* Erase/truncate */
-void t_string_erase(TempString *tstr, unsigned int pos, unsigned int len);
-void t_string_truncate(TempString *tstr, unsigned int len);
+void t_string_erase(TempString *tstr, size_t pos, size_t len);
+void t_string_truncate(TempString *tstr, size_t len);
 
 #endif
--- a/src/lib/write-full.c	Sun Sep 08 16:08:26 2002 +0300
+++ b/src/lib/write-full.c	Sun Sep 08 16:20:28 2002 +0300
@@ -23,8 +23,8 @@
     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include <errno.h>
-#include <limits.h>
+#include "lib.h"
+
 #include <unistd.h>
 
 int write_full(int fd, const void *data, size_t size)
@@ -32,7 +32,7 @@
 	ssize_t ret;
 
 	while (size > 0) {
-		ret = write(fd, data, size < INT_MAX ? size : INT_MAX);
+		ret = write(fd, data, size < SSIZE_T_MAX ? size : SSIZE_T_MAX);
 		if (ret < 0)
 			return -1;