2279c5
diff -up sane-backends-1.0.27/backend/epson2_net.c.epsonds-issues sane-backends-1.0.27/backend/epson2_net.c
2279c5
--- sane-backends-1.0.27/backend/epson2_net.c.epsonds-issues	2016-10-06 02:02:57.000000000 +0200
2279c5
+++ sane-backends-1.0.27/backend/epson2_net.c	2020-07-28 15:04:58.385405722 +0200
2279c5
@@ -32,11 +32,12 @@
2279c5
 
2279c5
 #include "sane/sanei_debug.h"
2279c5
 
2279c5
-static int
2279c5
+static ssize_t
2279c5
 sanei_epson_net_read_raw(Epson_Scanner *s, unsigned char *buf, ssize_t wanted,
2279c5
 		       SANE_Status *status)
2279c5
 {
2279c5
-	int ready, read = -1;
2279c5
+	int ready;
2279c5
+	ssize_t read = -1;
2279c5
 	fd_set readable;
2279c5
 	struct timeval tv;
2279c5
 
2279c5
@@ -62,111 +63,136 @@ sanei_epson_net_read_raw(Epson_Scanner *
2279c5
 	return read;
2279c5
 }
2279c5
 
2279c5
-int
2279c5
-sanei_epson_net_read(Epson_Scanner *s, unsigned char *buf, ssize_t wanted,
2279c5
+static ssize_t
2279c5
+sanei_epson_net_read_buf(Epson_Scanner *s, unsigned char *buf, ssize_t wanted,
2279c5
 		       SANE_Status * status)
2279c5
 {
2279c5
-	ssize_t size;
2279c5
 	ssize_t read = 0;
2279c5
-	unsigned char header[12];
2279c5
 
2279c5
-	/* read from buffer, if available */
2279c5
-	if (s->netptr != s->netbuf) {
2279c5
-		DBG(23, "reading %lu from buffer at %p, %lu available\n",
2279c5
-			(u_long) wanted, s->netptr, (u_long) s->netlen);
2279c5
+	DBG(23, "%s: reading up to %lu from buffer at %p, %lu available\n",
2279c5
+		__func__, (u_long) wanted, s->netptr, (u_long) s->netlen);
2279c5
 
2279c5
-		memcpy(buf, s->netptr, wanted);
2279c5
-		read = wanted;
2279c5
+	if ((size_t) wanted > s->netlen) {
2279c5
+		*status = SANE_STATUS_IO_ERROR;
2279c5
+		wanted = s->netlen;
2279c5
+	}
2279c5
 
2279c5
-		s->netlen -= wanted;
2279c5
+	memcpy(buf, s->netptr, wanted);
2279c5
+	read = wanted;
2279c5
 
2279c5
-		if (s->netlen == 0) {
2279c5
-			DBG(23, "%s: freeing %p\n", __func__, s->netbuf);
2279c5
-			free(s->netbuf);
2279c5
-			s->netbuf = s->netptr = NULL;
2279c5
-			s->netlen = 0;
2279c5
-		}
2279c5
+	s->netptr += read;
2279c5
+	s->netlen -= read;
2279c5
 
2279c5
-		return read;
2279c5
+	if (s->netlen == 0) {
2279c5
+		DBG(23, "%s: freeing %p\n", __func__, s->netbuf);
2279c5
+		free(s->netbuf);
2279c5
+		s->netbuf = s->netptr = NULL;
2279c5
+		s->netlen = 0;
2279c5
+	}
2279c5
+
2279c5
+	return read;
2279c5
+}
2279c5
+
2279c5
+ssize_t
2279c5
+sanei_epson_net_read(Epson_Scanner *s, unsigned char *buf, ssize_t wanted,
2279c5
+		       SANE_Status * status)
2279c5
+{
2279c5
+	if (wanted < 0) {
2279c5
+		*status = SANE_STATUS_INVAL;
2279c5
+		return 0;
2279c5
+	}
2279c5
+
2279c5
+	size_t size;
2279c5
+	ssize_t read = 0;
2279c5
+	unsigned char header[12];
2279c5
+
2279c5
+	/* read from remainder of buffer */
2279c5
+	if (s->netptr) {
2279c5
+		return sanei_epson_net_read_buf(s, buf, wanted, status);
2279c5
 	}
2279c5
 
2279c5
 	/* receive net header */
2279c5
-	size = sanei_epson_net_read_raw(s, header, 12, status);
2279c5
-	if (size != 12) {
2279c5
+	read = sanei_epson_net_read_raw(s, header, 12, status);
2279c5
+	if (read != 12) {
2279c5
 		return 0;
2279c5
 	}
2279c5
 
2279c5
+	/* validate header */
2279c5
 	if (header[0] != 'I' || header[1] != 'S') {
2279c5
 		DBG(1, "header mismatch: %02X %02x\n", header[0], header[1]);
2279c5
 		*status = SANE_STATUS_IO_ERROR;
2279c5
 		return 0;
2279c5
 	}
2279c5
 
2279c5
+	/* parse payload size */
2279c5
 	size = be32atoh(&header[6]);
2279c5
 
2279c5
-	DBG(23, "%s: wanted = %lu, available = %lu\n", __func__,
2279c5
-		(u_long) wanted, (u_long) size);
2279c5
-
2279c5
 	*status = SANE_STATUS_GOOD;
2279c5
 
2279c5
-	if (size == wanted) {
2279c5
-
2279c5
-		DBG(15, "%s: full read\n", __func__);
2279c5
+	if (!s->netbuf) {
2279c5
+		DBG(15, "%s: direct read\n", __func__);
2279c5
+		DBG(23, "%s: wanted = %lu, available = %lu\n", __func__,
2279c5
+			(u_long) wanted, (u_long) size);
2279c5
 
2279c5
-		read = sanei_epson_net_read_raw(s, buf, size, status);
2279c5
-
2279c5
-		if (s->netbuf) {
2279c5
-			free(s->netbuf);
2279c5
-			s->netbuf = NULL;
2279c5
-			s->netlen = 0;
2279c5
+		if ((size_t) wanted > size) {
2279c5
+			wanted = size;
2279c5
 		}
2279c5
 
2279c5
-		if (read < 0) {
2279c5
-			return 0;
2279c5
-		}
2279c5
-		
2279c5
-/*	} else if (wanted < size && s->netlen == size) { */
2279c5
+		read = sanei_epson_net_read_raw(s, buf, wanted, status);
2279c5
 	} else {
2279c5
-		DBG(23, "%s: partial read\n", __func__);
2279c5
+		DBG(15, "%s: buffered read\n", __func__);
2279c5
+		DBG(23, "%s: bufferable = %lu, available = %lu\n", __func__,
2279c5
+			(u_long) s->netlen, (u_long) size);
2279c5
 
2279c5
-		read = sanei_epson_net_read_raw(s, s->netbuf, size, status);
2279c5
-		if (read != size) {
2279c5
-			return 0;
2279c5
+		if (s->netlen > size) {
2279c5
+			s->netlen = size;
2279c5
 		}
2279c5
 
2279c5
-		s->netlen = size - wanted;
2279c5
-		s->netptr += wanted;
2279c5
-		read = wanted;
2279c5
-
2279c5
-		DBG(23, "0,4 %02x %02x\n", s->netbuf[0], s->netbuf[4]);
2279c5
-		DBG(23, "storing %lu to buffer at %p, next read at %p, %lu bytes left\n",
2279c5
-			(u_long) size, s->netbuf, s->netptr, (u_long) s->netlen);
2279c5
+		/* fill buffer */
2279c5
+		read = sanei_epson_net_read_raw(s, s->netbuf, s->netlen, status);
2279c5
+		s->netptr = s->netbuf;
2279c5
+		s->netlen = (read > 0 ? read : 0);
2279c5
 
2279c5
-		memcpy(buf, s->netbuf, wanted);
2279c5
+		/* copy wanted part */
2279c5
+		read = sanei_epson_net_read_buf(s, buf, wanted, status);
2279c5
 	}
2279c5
 
2279c5
 	return read;
2279c5
 }
2279c5
 
2279c5
-
2279c5
-int
2279c5
+size_t
2279c5
 sanei_epson_net_write(Epson_Scanner *s, unsigned int cmd, const unsigned char *buf,
2279c5
 			size_t buf_size, size_t reply_len, SANE_Status *status)
2279c5
 {
2279c5
 	unsigned char *h1, *h2, *payload;
2279c5
 	unsigned char *packet = malloc(12 + 8 + buf_size);
2279c5
 
2279c5
-	/* XXX check allocation failure */
2279c5
+	if (!packet) {
2279c5
+		*status = SANE_STATUS_NO_MEM;
2279c5
+		return 0;
2279c5
+	}
2279c5
 
2279c5
 	h1 = packet;
2279c5
 	h2 = packet + 12;
2279c5
 	payload = packet + 12 + 8;
2279c5
 
2279c5
 	if (reply_len) {
2279c5
-		s->netbuf = s->netptr = malloc(reply_len);
2279c5
+		if (s->netbuf) {
2279c5
+			DBG(23, "%s, freeing %p, %ld bytes unprocessed\n",
2279c5
+				__func__, s->netbuf, (u_long) s->netlen);
2279c5
+			free(s->netbuf);
2279c5
+			s->netbuf = s->netptr = NULL;
2279c5
+			s->netlen = 0;
2279c5
+		}
2279c5
+		s->netbuf = malloc(reply_len);
2279c5
+		if (!s->netbuf) {
2279c5
+			free(packet);
2279c5
+			*status = SANE_STATUS_NO_MEM;
2279c5
+			return 0;
2279c5
+		}
2279c5
 		s->netlen = reply_len;
2279c5
-		DBG(24, "allocated %lu bytes at %p\n",
2279c5
-			(u_long) reply_len, s->netbuf);
2279c5
+		DBG(24, "%s: allocated %lu bytes at %p\n", __func__,
2279c5
+			(u_long) s->netlen, s->netbuf);
2279c5
 	}
2279c5
 
2279c5
 	DBG(24, "%s: cmd = %04x, buf = %p, buf_size = %lu, reply_len = %lu\n",
2279c5
diff -up sane-backends-1.0.27/backend/epson2_net.h.epsonds-issues sane-backends-1.0.27/backend/epson2_net.h
2279c5
--- sane-backends-1.0.27/backend/epson2_net.h.epsonds-issues	2016-10-06 02:02:57.000000000 +0200
2279c5
+++ sane-backends-1.0.27/backend/epson2_net.h	2020-07-28 14:51:59.666593530 +0200
2279c5
@@ -4,9 +4,9 @@
2279c5
 #include <sys/types.h>
2279c5
 #include "../include/sane/sane.h"
2279c5
 
2279c5
-extern int sanei_epson_net_read(struct Epson_Scanner *s, unsigned char *buf, ssize_t buf_size,
2279c5
+extern ssize_t sanei_epson_net_read(struct Epson_Scanner *s, unsigned char *buf, ssize_t buf_size,
2279c5
 				SANE_Status *status);
2279c5
-extern int sanei_epson_net_write(struct Epson_Scanner *s, unsigned int cmd, const unsigned char *buf,
2279c5
+extern size_t sanei_epson_net_write(struct Epson_Scanner *s, unsigned int cmd, const unsigned char *buf,
2279c5
 				size_t buf_size, size_t reply_len,
2279c5
 				SANE_Status *status);
2279c5
 extern SANE_Status sanei_epson_net_lock(struct Epson_Scanner *s);