- Added Udf::descriptor_tag::set_checksums() template.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5573 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Tyler Dauwalder
2003-12-05 05:39:11 +00:00
parent 1210ab70ac
commit 5a97c04e6b
2 changed files with 43 additions and 8 deletions
@@ -306,8 +306,6 @@ descriptor_tag::dump() const
location, not the mapped physical disk block.
\param calculateCrc Whether or not to perform the crc calculation
on the descriptor data following the tag.
\todo Calc the CRC.
*/
status_t
descriptor_tag::init_check(uint32 block, bool calculateCrc)
@@ -325,14 +323,15 @@ descriptor_tag::init_check(uint32 block, bool calculateCrc)
}
PRINT(("crc (in structure) == %d\n", crc()));
PRINT(("crc_length (in structure) == %d\n", crc_length()));
// location
status_t error = (block == location()) ? B_OK : B_NO_INIT;
// checksum
if (!error) {
uint32 sum = 0;
for (int i = 0; i <= 3; i++)
sum += ((uint8*)this)[i];
sum += reinterpret_cast<uint8*>(this)[i];
for (int i = 5; i <= 15; i++)
sum += ((uint8*)this)[i];
sum += reinterpret_cast<uint8*>(this)[i];
error = sum % 256 == checksum() ? B_OK : B_NO_INIT;
}
// crc
@@ -342,10 +341,8 @@ descriptor_tag::init_check(uint32 block, bool calculateCrc)
error = _crc == crc() ? B_OK : B_NO_INIT;
}
RETURN(error);
}
//----------------------------------------------------------------------
// primary_volume_descriptor
//----------------------------------------------------------------------
@@ -446,6 +446,44 @@ public:
void set_crc_length(uint16 crc_length) { _crc_length = B_HOST_TO_LENDIAN_INT16(crc_length); }
void set_location(uint32 location) { _location = B_HOST_TO_LENDIAN_INT32(location); }
/*! \brief Calculates and sets the crc length, crc checksumm, and
checksum for the tag.
This function should not be called until all member variables in
the descriptor_tag's enclosing descriptor and all member variables
in the descriptor_tag itself other than crc_length, crc, and checksum
have been set (since the checksum is based off of values in the
descriptor_tag, and the crc is based off the values in and the
size of the enclosing descriptor).
\param The tag's enclosing descriptor.
\param The size of the tag's enclosing descriptor (including the
tag); only necessary if different from sizeof(Descriptor).
*/
template <class Descriptor>
void
set_checksums(Descriptor &descriptor, uint16 size = sizeof(Descriptor))
{
// check that this tag is actually owned by
// the given descriptor
if (this == &descriptor.tag())
{
// crc_length, based off provided descriptor
set_crc_length(size - sizeof(descriptor_tag));
// crc
uint16 crc = Udf::calculate_crc(reinterpret_cast<uint8*>(this)
+ sizeof(descriptor_tag), crc_length());
set_crc(crc);
// checksum (which depends on the other two values)
uint32 sum = 0;
for (int i = 0; i <= 3; i++)
sum += reinterpret_cast<uint8*>(this)[i];
for (int i = 5; i <= 15; i++)
sum += reinterpret_cast<uint8*>(this)[i];
set_checksum(sum % 256);
}
}
private:
uint16 _id;
uint16 _version;