B
brandon
Note: I'm not very familiar with C++, only C.
The following description and code snippet are from
http://freedos-32.sourceforge.net/lean/specification.php
It's about calculating the checksum for a LEAN file system structure.
"...The sensitive structure must be cast to an array of 32-bit unsigned
integers, then each element of this array must be summed, rotating the
sum one bit to right at every iteration... The data parameter is a
pointer to the sensitive structure, including the checksum field in its
first 4 bytes. The size parameter is the size in bytes of the structure
pointed by data, including the 4 bytes for the checksum field... The
function skips the first 4 bytes when computing the checksum."
uint32_t computeChecksum(const void* data, size_t size)
{
uint32_t res = 0;
const uint32_t* d = static_cast<const uint32_t*>(data);
assert((size & (sizeof(uint32_t) - 1)) == 0);
size /= sizeof(uint32_t);
for (size_t i = 1; i != size; ++i) res = (res << 31) + (res >> 1)
+ d;
return res;
}
The structure consists of uint32_t's and uint64_t's. Doesn't this snippet
rely on the structure being stored in memory in a particular, machine/
compiler specific way (e.g. endianness)?
Thanks.
The following description and code snippet are from
http://freedos-32.sourceforge.net/lean/specification.php
It's about calculating the checksum for a LEAN file system structure.
"...The sensitive structure must be cast to an array of 32-bit unsigned
integers, then each element of this array must be summed, rotating the
sum one bit to right at every iteration... The data parameter is a
pointer to the sensitive structure, including the checksum field in its
first 4 bytes. The size parameter is the size in bytes of the structure
pointed by data, including the 4 bytes for the checksum field... The
function skips the first 4 bytes when computing the checksum."
uint32_t computeChecksum(const void* data, size_t size)
{
uint32_t res = 0;
const uint32_t* d = static_cast<const uint32_t*>(data);
assert((size & (sizeof(uint32_t) - 1)) == 0);
size /= sizeof(uint32_t);
for (size_t i = 1; i != size; ++i) res = (res << 31) + (res >> 1)
+ d;
return res;
}
The structure consists of uint32_t's and uint64_t's. Doesn't this snippet
rely on the structure being stored in memory in a particular, machine/
compiler specific way (e.g. endianness)?
Thanks.