changeset 11:3aa0f5a02342

Remove unused variable; fix what it was supposed to be doing. (and document it a bit for the next time through here)
author David A. Holland
date Tue, 31 May 2022 00:54:12 -0400
parents 5b21f127e957
children aab9ff6af791
files checksum/dosum.c
diffstat 1 files changed, 19 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/checksum/dosum.c	Mon May 30 23:58:14 2022 -0400
+++ b/checksum/dosum.c	Tue May 31 00:54:12 2022 -0400
@@ -66,7 +66,6 @@
 
 uint32_t dosum(const char *buf, size_t len, size_t skipstart, size_t skiplen) {
   struct sumstate s;
-  unsigned fudge;
   size_t numwords;
 
   assert(skiplen%2==0);
@@ -79,6 +78,18 @@
    *
    * The followed mangled logic that rounds some things up matches
    * what used to be done in an even uglier way.
+   *
+   * To wit: if there's a skip area, we start after it and read pairs
+   * of octets, including a zero past the end of the file if the
+   * remaining data length after the skip area has odd size, so as to
+   * get the last byte. (The caller is responsible for making sure
+   * there's a zero there.) Then we read the beginning of the file up
+   * to the skip area, but round down so as to not read the first byte
+   * of the skip area if skipstart is odd. If skipstart is odd then we
+   * wedge in that last byte along with a zero.
+   *
+   * If there isn't a skip area, we read the whole thing in order,
+   * including the zero past the end if the size is odd.
    */
 
   assert(sizeof(uint16_t)==2);
@@ -90,9 +101,15 @@
     size_t p1 = 0;
     size_t l1 = (skipstart/2) * 2;
 
-    fudge = skipstart%2;
     sumstate_sum(&s, buf+p0, l0);
     sumstate_sum(&s, buf+p1, l1);
+    if (skipstart % 2 == 1) {
+       char tmp[2];
+
+       tmp[0] = buf[skipstart - 1];
+       tmp[1] = 0;
+       sumstate_sum(&s, tmp, 2);
+    }
   }
   else {
     sumstate_sum(&s, buf, numwords * 2);