From d257e47a6c6b41ba727b196ac96c05ab91bd9d65 Mon Sep 17 00:00:00 2001
From: Sergey Poznyakoff <gray@gnu.org>
Date: Fri, 7 Apr 2023 11:23:37 +0300
Subject: [PATCH 3/4] Fix calculation of CRC in copy-out mode.

* src/copyout.c (read_for_checksum): Fix type of the file_size argument.
Rewrite the reading loop.

Original patch by Stefano Babic <sbabic@denx.de>

Upstream-Status: Backport [a1b2f7871c3ae5113e0102b870b15ea06a8f0e3d]
Signed-off-by: Marek Vasut <marex@denx.de>
---
 src/copyout.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/copyout.c b/src/copyout.c
index 8b0beb6..f1ff351 100644
--- a/src/copyout.c
+++ b/src/copyout.c
@@ -34,27 +34,25 @@
    compute and return a checksum for them.  */
 
 static uint32_t
-read_for_checksum (int in_file_des, int file_size, char *file_name)
+read_for_checksum (int in_file_des, off_t file_size, char *file_name)
 {
   uint32_t crc;
-  char buf[BUFSIZ];
-  int bytes_left;
-  int bytes_read;
-  int i;
+  unsigned char buf[BUFSIZ];
+  ssize_t bytes_read;
+  ssize_t i;
 
   crc = 0;
 
-  for (bytes_left = file_size; bytes_left > 0; bytes_left -= bytes_read)
+  while (file_size > 0)
     {
       bytes_read = read (in_file_des, buf, BUFSIZ);
       if (bytes_read < 0)
 	error (PAXEXIT_FAILURE, errno, _("cannot read checksum for %s"), file_name);
       if (bytes_read == 0)
 	break;
-      if (bytes_left < bytes_read)
-        bytes_read = bytes_left;
-      for (i = 0; i < bytes_read; ++i)
+      for (i = 0; i < bytes_read; i++)
 	crc += buf[i] & 0xff;
+      file_size -= bytes_read;
     }
   if (lseek (in_file_des, 0L, SEEK_SET))
     error (PAXEXIT_FAILURE, errno, _("cannot read checksum for %s"), file_name);
-- 
2.39.2

