fprime/Utils/Hash/libcrc/CRC32.cpp
M Starch ec08d43dd3
Removes NATIVE_INT_TYPE, NATIVE_UINT_TYPE, and POINTER_CAST from Fw (#3286)
* NATIVE_INT_TYPE use in toString

* NATIVE_INT_TYPE use in SimpleObjRegistry

* NATIVE_INT_TYPE use in Asserts

* NATIVE_INT_TYPE use in Fw/Comp

* NATIVE_INT_TYPE use in getCapacity

* NATIVE_INT_TYPE use in getEntries

* NATIVE_INT_TYPE use in size/length

* NATIVE_INT_TYPE use in FILE_NAME_ARG

* NATIVE_INT_TYPE use in Fw (misc)

* NATIVE_INT_TYPE use in identifier

* NATIVE_INT_TYPE use in Fw (misc II)

* POINTER_CAST in Buffer

* POINTER_CAST in Serializable

* sp

* Removing no longer used DefaultTypes.hpp

* Fixes to accomidate Fw refactor

* Unit-test and CI fixes

* Fixing review comments - pt 1
2025-03-04 14:42:48 -08:00

91 lines
2.6 KiB
C++

// ======================================================================
// \title CRC32.cpp
// \author dinkel
// \brief cpp file for CRC32 implementation of Hash class
//
// \copyright
// Copyright 2009-2015, by the California Institute of Technology.
// ALL RIGHTS RESERVED. United States Government Sponsorship
// acknowledged.
//
// ======================================================================
#include <Utils/Hash/Hash.hpp>
namespace Utils {
Hash ::
Hash()
{
this->init();
}
Hash ::
~Hash()
{
}
void Hash ::
hash(const void *const data, const FwSizeType len, HashBuffer& buffer)
{
HASH_HANDLE_TYPE local_hash_handle;
local_hash_handle = 0xffffffffL;
FW_ASSERT(data);
char c;
for(FwSizeType index = 0; index < len; index++) {
c = static_cast<const char*>(data)[index];
local_hash_handle = static_cast<HASH_HANDLE_TYPE>(update_crc_32(local_hash_handle, c));
}
HashBuffer bufferOut;
// For CRC32 we need to return the one's complement of the result:
Fw::SerializeStatus status = bufferOut.serialize(~(local_hash_handle));
FW_ASSERT( Fw::FW_SERIALIZE_OK == status );
buffer = bufferOut;
}
void Hash ::
init()
{
this->hash_handle = 0xffffffffL;
}
void Hash ::
update(const void *const data, FwSizeType len)
{
FW_ASSERT(data);
char c;
for(FwSizeType index = 0; index < len; index++) {
c = static_cast<const char*>(data)[index];
this->hash_handle = static_cast<HASH_HANDLE_TYPE>(update_crc_32(this->hash_handle, c));
}
}
void Hash ::
final(HashBuffer& buffer)
{
HashBuffer bufferOut;
// For CRC32 we need to return the one's complement of the result:
Fw::SerializeStatus status = bufferOut.serialize(~(this->hash_handle));
FW_ASSERT( Fw::FW_SERIALIZE_OK == status );
buffer = bufferOut;
}
void Hash ::
final(U32 &hashvalue)
{
FW_ASSERT(sizeof(this->hash_handle) == sizeof(U32));
// For CRC32 we need to return the one's complement of the result:
hashvalue = ~(this->hash_handle);
}
void Hash ::
setHashValue(HashBuffer &value)
{
Fw::SerializeStatus status = value.deserialize(this->hash_handle);
FW_ASSERT( Fw::FW_SERIALIZE_OK == status );
// Expecting `value` to already be one's complement; so doing one's complement
// here for correct hash updates
this->hash_handle = ~this->hash_handle;
}
}