Fix unintended sign extensions detected by Coverity.

Coverity 700703, 700704, 700705, 700706, 700707, 700708, 700709,
700710, 700711, 700712, 700713, 700714, 700715, 700716, 700717.

Most likely, the uses of "unsigned long" in the code should be changed
to uint32_t (or less likely, uint64_t).