Jinoh Kang (@iamahuman) commented about dlls/ws2_32/tests/sock.c:
sum += s;
carry = s > sum;
count -= 2;
- }
- sum += carry; /* This won't produce another carry */
- sum = (sum & 0xffff) + (sum >> 16);
- if (count) sum += *data; /* LE-only */
- sum = (sum & 0xffff) + (sum >> 16);
- /* fold in any carry */
- sum = (sum & 0xffff) + (sum >> 16);
- check = ~sum;
- return check;
+}
It's much simpler to implement 1's complement checksum without the use of an explicit `carry` variable. How about the following:
```suggestion:-25+0 static UINT16 chksum(BYTE *data, unsigned int count) { UINT16 *ptr = (UINT16 *)data, *end = ptr + count / 2; unsigned int sum = 0;
while (ptr != end) { sum += *ptr++; sum = (sum & 0xffff) + (sum >> 16); }
if (count % 2) { sum += *(BYTE *)ptr; /* LE-only */ sum = (sum & 0xffff) + (sum >> 16); }
return ~sum; } ```
Note that `sum` is in range `[0, 0xfffe]` at the start of each iteration. After addition, the range extends to `[0, 0x1fffd]`; however, since `0xfffd + 0x1 = 0xfffe`, the range folds back to `[0, 0xfffe]`.
Ditto for tests in ntdll.