F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
HashImpl.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CRC32.cpp
3 // \author dinkel
4 // \brief cpp file for CRC32 implementation of Hash class
5 //
6 // \copyright
7 // Copyright 2009-2015, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
13 #include <Utils/Hash/Hash.hpp>
14 #include "Crc32.hpp"
15 
16 namespace Utils {
17 
19  this->init();
20 }
21 
22 Hash ::~Hash() = default;
23 
24 void Hash ::hash(const void* const data, const FwSizeType len, HashBuffer& buffer) {
25  Hash crc32;
26  crc32.update(data, len);
27  crc32.finalize(buffer);
28 }
29 
30 void Hash ::init() {
31  this->hash_handle = 0xffffffffL;
32 }
33 
34 void Hash ::update(const void* const data, FwSizeType len) {
35  static_assert(sizeof(Utils::Hash::hash_handle) == sizeof(U32), "hash handle size must match CRC32 size");
36  FW_ASSERT(data);
37  this->hash_handle = crc32_ieee802_3_update(static_cast<const U8*>(data), len, this->hash_handle);
38 }
39 
40 void Hash ::finalize(HashBuffer& buffer) const {
41  HashBuffer bufferOut;
42  // For CRC32 we need to return the one's complement of the result:
43  Fw::SerializeStatus status = bufferOut.serializeFrom(~(this->hash_handle));
44  FW_ASSERT(Fw::FW_SERIALIZE_OK == status);
45  buffer = bufferOut;
46 }
47 
48 void Hash ::finalize(U32& hashvalue) const {
49  // For CRC32 we need to return the one's complement of the result:
50  hashvalue = ~(this->hash_handle);
51 }
52 
54  Fw::SerializeStatus status = value.deserializeTo(this->hash_handle);
55  FW_ASSERT(Fw::FW_SERIALIZE_OK == status);
56  // Expecting `value` to already be one's complement; so doing one's complement
57  // here for correct hash updates
58  this->hash_handle = ~this->hash_handle;
59 }
60 
61 void Hash ::setHashValue(U32 value) {
62  this->hash_handle = ~value;
63 }
64 
65 } // namespace Utils
void update(const void *const data, const FwSizeType len)
Definition: HashImpl.cpp:34
Serialization/Deserialization operation was successful.
SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG) override
Serialize an 8-bit unsigned integer value.
PlatformSizeType FwSizeType
void init()
Definition: HashImpl.cpp:30
SerializeStatus
forward declaration for string
void setHashValue(HashBuffer &value)
Definition: HashImpl.cpp:53
A generic interface for creating and comparing hash values.
Definition: Hash.hpp:24
static void hash(const void *data, const FwSizeType len, HashBuffer &buffer)
Definition: HashImpl.cpp:24
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
U32 crc32_ieee802_3_update(const U8 *data, FwSizeType length, U32 crc)
Definition: Crc32.cpp:172
SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG) override
Deserialize an 8-bit unsigned integer value.
void finalize(HashBuffer &buffer) const
Definition: HashImpl.cpp:40
#define FW_ASSERT(...)
Definition: Assert.hpp:14