--- clamav-devel/clamav-milter/clamav-milter.c	Fri Apr  9 10:29:00 2004
+++ clamav-devel-jm/clamav-milter/clamav-milter.c	Sun Apr 11 00:18:40 2004
@@ -633,7 +633,8 @@
 	u_char	*body;		/* body of the message if Sflag is set */
 	size_t	bodyLen;	/* number of bytes in body */
 	header_list_t headers;	/* Message headers */
-	long	numBytes;	/* Number of bytes sent so far */
+	size_t	numBytes;	/* Number of bytes sent so far */
+	char	*envid;		/* Sendmail envid ($i) */
 };
 
 static	int		pingServer(int serverNumber);
@@ -649,7 +650,7 @@
 static	sfsistat	clamfi_close(SMFICTX *ctx);
 static	void		clamfi_cleanup(SMFICTX *ctx);
 static	void		clamfi_free(struct privdata *privdata);
-static	int		clamfi_send(const struct privdata *privdata, size_t len, const char *format, ...);
+static	size_t		clamfi_send(struct privdata *privdata, size_t len, const char *format, ...);
 static	char		*strrcpy(char *dest, const char *source);
 static	int		clamd_recv(int sock, char *buf, size_t len);
 static	off_t		updateSigFile(void);
@@ -720,7 +721,7 @@
 				 * number of seconds to wait for clamd to
 				 * respond, see ReadTimeout in clamav.conf
 				 */
-static	long	streamMaxLength = -1;	/* StreamMaxLength from clamav.conf */
+static	size_t	streamMaxLength = -1;	/* StreamMaxLength from clamav.conf */
 static	int	logClean = 1;	/*
 				 * Add clean items to the log file
 				 */
@@ -1696,9 +1697,7 @@
 	privdata->cmdSocket = -1;	/* 0.4 */
 
 	privdata->from = strdup(argv[0]);
-
-	if(streamMaxLength > 0L)
-		privdata->numBytes = (long)(strlen(argv[0]) + 6);
+	privdata->envid = smfi_getsymval(ctx,"i");
 
 	if(hflag)
 		privdata->headers = header_list_new();
@@ -1731,9 +1730,6 @@
 	privdata->to[privdata->numTo] = strdup(argv[0]);
 	privdata->to[++privdata->numTo] = NULL;
 
-	if(streamMaxLength > 0L)
-		privdata->numBytes += (long)(strlen(argv[0]) + 4);
-
 	return SMFIS_CONTINUE;
 }
 
@@ -1765,9 +1761,6 @@
 		return cl_error;
 	}
 
-	if(streamMaxLength > 0L)
-		privdata->numBytes += (long)(strlen(headerf) + strlen(headerv) + 3);
-
 	if(hflag)
 		header_list_add(privdata->headers, headerf, headerv);
 
@@ -1800,8 +1793,6 @@
 		clamfi_cleanup(ctx);
 		return cl_error;
 	}
-	if(streamMaxLength > 0L)
-		privdata->numBytes++;
 
 	/*
 	 * See if the e-mail is only going to members of the list
@@ -1858,17 +1849,6 @@
 	cli_dbgmsg("clamfi_envbody: %u bytes\n", len);
 #endif
 
-	if(streamMaxLength > 0L) {
-		privdata->numBytes += (long)len;
-		if(privdata->numBytes > streamMaxLength) {
-			if(use_syslog)
-				syslog(LOG_NOTICE, "%s: Message more than StreamMaxLength (%ld) bytes - not scanned\n",
-					smfi_getsymval(ctx, "i"),
-					streamMaxLength);
-			clamfi_cleanup(ctx);	/* not needed, but just to be safe */
-			return SMFIS_ACCEPT;
-		}
-	}
 	if(clamfi_send(privdata, len, (char *)bodyp) < 0) {
 		clamfi_cleanup(ctx);	/* not needed, but just to be safe */
 		return cl_error;
@@ -1894,7 +1874,6 @@
 {
 	int rc = SMFIS_CONTINUE;
 	char *ptr;
-	const char *sendmailId;
 	struct privdata *privdata = (struct privdata *)smfi_getpriv(ctx);
 	char mess[128];
 
@@ -1970,23 +1949,26 @@
 	close(privdata->cmdSocket);
 	privdata->cmdSocket = -1;
 
-	sendmailId = smfi_getsymval(ctx, "i");
 
 	if(strstr(mess, "ERROR") != NULL) {
 		if(strstr(mess, "Size exceeded") != NULL) {
 			/*
 			 * Clamd has stopped on StreamMaxLength before us
+			 *
+			 * With my other patch this should never happen unless
+			 * clamd has a different StreamMaxLength value then we do
+			 *
 			 */
 			if(use_syslog)
 				syslog(LOG_NOTICE, "%s: Message more than StreamMaxLength (%ld) bytes - not scanned\n",
-					sendmailId, streamMaxLength);
+					privdata->envid, streamMaxLength);
 			clamfi_cleanup(ctx);	/* not needed, but just to be safe */
 			return SMFIS_ACCEPT;
 		}
 
-		cli_warnmsg("%s: %s\n", sendmailId, mess);
+		cli_warnmsg("%s: %s\n", privdata->envid, mess);
 		if(use_syslog)
-			syslog(LOG_ERR, "%s: %s\n", sendmailId, mess);
+			syslog(LOG_ERR, "%s: %s\n", privdata->envid, mess);
 		clamfi_cleanup(ctx);
 		return cl_error;
 	}
@@ -2002,7 +1984,7 @@
 		if(use_syslog && logClean)
 			/* Include the sendmail queue ID in the log */
 			syslog(LOG_NOTICE, "%s: clean message from %s",
-				sendmailId,
+				privdata->envid,
 				(privdata->from) ? privdata->from : "an unknown sender");
 
 		if(privdata->body) {
@@ -2056,7 +2038,7 @@
 
 		if(use_syslog)
 			/* Include the sendmail queue ID in the log */
-			syslog(LOG_NOTICE, "%s: %s %s", sendmailId, mess, err);
+			syslog(LOG_NOTICE, "%s: %s %s", privdata->envid, mess, err);
 #ifdef	CL_DEBUG
 		cli_dbgmsg("%s\n", err);
 #endif
@@ -2115,7 +2097,7 @@
 					 * information
 					 */
 					fprintf(sendmail, "The message %s sent from %s to\n\t",
-						sendmailId, from);
+						privdata->envid, from);
 				else
 					fprintf(sendmail, "A message sent from %s to\n\t",
 						from);
@@ -2322,14 +2304,16 @@
 }
 
 /*
- * Returns < 0 for failure
+ * Return number of bytes written or < 0 for failure
+ * 
  */
-static int
-clamfi_send(const struct privdata *privdata, size_t len, const char *format, ...)
+static size_t
+clamfi_send(struct privdata *privdata, size_t len, const char *format, ...)
 {
 	char output[BUFSIZ];
 	const char *ptr;
-
+	size_t numBytes = 0;
+	
 	assert(format != NULL);
 
 	if(len > 0)
@@ -2362,7 +2346,24 @@
 	}
 #endif
 
-	while(len > 0) {
+	if(!quarantine_dir && (
+		len + privdata->numBytes > streamMaxLength) 
+	   ){
+		if(use_syslog && privdata->numBytes != streamMaxLength){
+                        char buf[1024];
+                        syslog(LOG_INFO,"%s: size exceeded max of %u , "
+					"already wrote %u, will write %u more instead "
+					"of len %u",
+                                        privdata->envid,
+                                        streamMaxLength,
+                                        privdata->numBytes,
+                                        streamMaxLength - privdata->numBytes,
+					len);
+                }
+		len = (streamMaxLength - privdata->numBytes);
+	}
+
+	while(len > 0 ) {
 		const int nbytes = (quarantine_dir) ?
 			write(privdata->dataSocket, ptr, len) :
 			send(privdata->dataSocket, ptr, len, 0);
@@ -2388,10 +2389,12 @@
 
 			return -1;
 		}
+		numBytes += nbytes;
+		privdata->numBytes += nbytes;
 		len -= nbytes;
 		ptr = &ptr[nbytes];
 	}
-	return 0;
+	return numBytes;
 }
 
 /*
