comparison checksum/dosum.c @ 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 13d2b8934445
children
comparison
equal deleted inserted replaced
10:5b21f127e957 11:3aa0f5a02342
64 return sum; 64 return sum;
65 } 65 }
66 66
67 uint32_t dosum(const char *buf, size_t len, size_t skipstart, size_t skiplen) { 67 uint32_t dosum(const char *buf, size_t len, size_t skipstart, size_t skiplen) {
68 struct sumstate s; 68 struct sumstate s;
69 unsigned fudge;
70 size_t numwords; 69 size_t numwords;
71 70
72 assert(skiplen%2==0); 71 assert(skiplen%2==0);
73 assert(skipstart==0 || skipstart+skiplen <= len); 72 assert(skipstart==0 || skipstart+skiplen <= len);
74 sumstate_init(&s, len); 73 sumstate_init(&s, len);
77 * It would be nice if we could count on skipstart being aligned. 76 * It would be nice if we could count on skipstart being aligned.
78 * But we can't. Nor is the filesize necessarily even. 77 * But we can't. Nor is the filesize necessarily even.
79 * 78 *
80 * The followed mangled logic that rounds some things up matches 79 * The followed mangled logic that rounds some things up matches
81 * what used to be done in an even uglier way. 80 * what used to be done in an even uglier way.
81 *
82 * To wit: if there's a skip area, we start after it and read pairs
83 * of octets, including a zero past the end of the file if the
84 * remaining data length after the skip area has odd size, so as to
85 * get the last byte. (The caller is responsible for making sure
86 * there's a zero there.) Then we read the beginning of the file up
87 * to the skip area, but round down so as to not read the first byte
88 * of the skip area if skipstart is odd. If skipstart is odd then we
89 * wedge in that last byte along with a zero.
90 *
91 * If there isn't a skip area, we read the whole thing in order,
92 * including the zero past the end if the size is odd.
82 */ 93 */
83 94
84 assert(sizeof(uint16_t)==2); 95 assert(sizeof(uint16_t)==2);
85 numwords = (len+1)/2; 96 numwords = (len+1)/2;
86 97
88 size_t p0 = skipstart+skiplen; 99 size_t p0 = skipstart+skiplen;
89 size_t l0 = (numwords - p0/2) * 2; 100 size_t l0 = (numwords - p0/2) * 2;
90 size_t p1 = 0; 101 size_t p1 = 0;
91 size_t l1 = (skipstart/2) * 2; 102 size_t l1 = (skipstart/2) * 2;
92 103
93 fudge = skipstart%2;
94 sumstate_sum(&s, buf+p0, l0); 104 sumstate_sum(&s, buf+p0, l0);
95 sumstate_sum(&s, buf+p1, l1); 105 sumstate_sum(&s, buf+p1, l1);
106 if (skipstart % 2 == 1) {
107 char tmp[2];
108
109 tmp[0] = buf[skipstart - 1];
110 tmp[1] = 0;
111 sumstate_sum(&s, tmp, 2);
112 }
96 } 113 }
97 else { 114 else {
98 sumstate_sum(&s, buf, numwords * 2); 115 sumstate_sum(&s, buf, numwords * 2);
99 } 116 }
100 117