F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
CRC32.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 
15 static_assert(sizeof(unsigned long) >= sizeof(U32), "CRC32 cannot fit in CRC32 library chosen types");
16 
17 namespace Utils {
18 
20  this->init();
21 }
22 
24 
25 void Hash ::hash(const void* const data, const FwSizeType len, HashBuffer& buffer) {
26  HASH_HANDLE_TYPE local_hash_handle;
27  local_hash_handle = 0xffffffffL;
28  FW_ASSERT(data);
29  char c;
30  for (FwSizeType index = 0; index < len; index++) {
31  c = static_cast<const char*>(data)[index];
32  local_hash_handle = static_cast<HASH_HANDLE_TYPE>(update_crc_32(local_hash_handle, c));
33  }
34  HashBuffer bufferOut;
35  // For CRC32 we need to return the one's complement of the result:
36  Fw::SerializeStatus status = bufferOut.serializeFrom(~(local_hash_handle));
37  FW_ASSERT(Fw::FW_SERIALIZE_OK == status);
38  buffer = bufferOut;
39 }
40 
41 void Hash ::init() {
42  this->hash_handle = 0xffffffffL;
43 }
44 
45 void Hash ::update(const void* const data, FwSizeType len) {
46  FW_ASSERT(data);
47  char c;
48  for (FwSizeType index = 0; index < len; index++) {
49  c = static_cast<const char*>(data)[index];
50  this->hash_handle = static_cast<HASH_HANDLE_TYPE>(update_crc_32(this->hash_handle, c));
51  }
52 }
53 
54 void Hash ::final(HashBuffer& buffer) {
55  HashBuffer bufferOut;
56  // For CRC32 we need to return the one's complement of the result:
57  Fw::SerializeStatus status = bufferOut.serializeFrom(~(this->hash_handle));
58  FW_ASSERT(Fw::FW_SERIALIZE_OK == status);
59  buffer = bufferOut;
60 }
61 
62 void Hash ::final(U32& hashvalue) {
63  FW_ASSERT(sizeof(this->hash_handle) == sizeof(U32));
64  // For CRC32 we need to return the one's complement of the result:
65  hashvalue = ~(this->hash_handle);
66 }
67 
69  Fw::SerializeStatus status = value.deserializeTo(this->hash_handle);
70  FW_ASSERT(Fw::FW_SERIALIZE_OK == status);
71  // Expecting `value` to already be one's complement; so doing one's complement
72  // here for correct hash updates
73  this->hash_handle = ~this->hash_handle;
74 }
75 } // namespace Utils
void update(const void *const data, const FwSizeType len)
Definition: CRC32.cpp:45
Serialization/Deserialization operation was successful.
PlatformSizeType FwSizeType
SerializeStatus deserializeTo(U8 &val, Endianness mode=Endianness::BIG)
deserialize 8-bit unsigned int
#define HASH_HANDLE_TYPE
Definition: CRC32.hpp:12
void init()
Definition: CRC32.cpp:41
SerializeStatus
forward declaration for string
void final(HashBuffer &buffer)
Definition: CRC32.cpp:54
unsigned long update_crc_32(unsigned long crc, char c)
Definition: lib_crc.c:272
void setHashValue(HashBuffer &value)
Definition: CRC32.cpp:68
SerializeStatus serializeFrom(U8 val, Endianness mode=Endianness::BIG)
serialize 8-bit unsigned int
static void hash(const void *data, const FwSizeType len, HashBuffer &buffer)
Definition: CRC32.cpp:25
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
#define FW_ASSERT(...)
Definition: Assert.hpp:14