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 
18 namespace Utils {
19 
20  Hash ::
22  {
23  this->init();
24  }
25 
26  Hash ::
28  {
29  }
30 
31  void Hash ::
32  hash(const void *const data, const FwSizeType len, HashBuffer& buffer)
33  {
34  HASH_HANDLE_TYPE local_hash_handle;
35  local_hash_handle = 0xffffffffL;
36  FW_ASSERT(data);
37  char c;
38  for(FwSizeType index = 0; index < len; index++) {
39  c = static_cast<const char*>(data)[index];
40  local_hash_handle = static_cast<HASH_HANDLE_TYPE>(update_crc_32(local_hash_handle, c));
41  }
42  HashBuffer bufferOut;
43  // For CRC32 we need to return the one's complement of the result:
44  Fw::SerializeStatus status = bufferOut.serialize(~(local_hash_handle));
45  FW_ASSERT( Fw::FW_SERIALIZE_OK == status );
46  buffer = bufferOut;
47  }
48 
49  void Hash ::
51  {
52  this->hash_handle = 0xffffffffL;
53  }
54 
55  void Hash ::
56  update(const void *const data, FwSizeType len)
57  {
58  FW_ASSERT(data);
59  char c;
60  for(FwSizeType index = 0; index < len; index++) {
61  c = static_cast<const char*>(data)[index];
62  this->hash_handle = static_cast<HASH_HANDLE_TYPE>(update_crc_32(this->hash_handle, c));
63  }
64  }
65 
66  void Hash ::
68  {
69  HashBuffer bufferOut;
70  // For CRC32 we need to return the one's complement of the result:
71  Fw::SerializeStatus status = bufferOut.serialize(~(this->hash_handle));
72  FW_ASSERT( Fw::FW_SERIALIZE_OK == status );
73  buffer = bufferOut;
74  }
75 
76  void Hash ::
77  final(U32 &hashvalue)
78  {
79  FW_ASSERT(sizeof(this->hash_handle) == sizeof(U32));
80  // For CRC32 we need to return the one's complement of the result:
81  hashvalue = ~(this->hash_handle);
82  }
83 
84  void Hash ::
86  {
87  Fw::SerializeStatus status = value.deserialize(this->hash_handle);
88  FW_ASSERT( Fw::FW_SERIALIZE_OK == status );
89  // Expecting `value` to already be one's complement; so doing one's complement
90  // here for correct hash updates
91  this->hash_handle = ~this->hash_handle;
92  }
93 }
void update(const void *const data, const FwSizeType len)
Definition: CRC32.cpp:56
Serialization/Deserialization operation was successful.
PlatformSizeType FwSizeType
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
#define HASH_HANDLE_TYPE
Definition: CRC32.hpp:12
void init()
Definition: CRC32.cpp:50
SerializeStatus
forward declaration for string
void final(HashBuffer &buffer)
Definition: CRC32.cpp:67
unsigned long update_crc_32(unsigned long crc, char c)
Definition: lib_crc.c:271
void setHashValue(HashBuffer &value)
Definition: CRC32.cpp:85
static void hash(const void *data, const FwSizeType len, HashBuffer &buffer)
Definition: CRC32.cpp:32
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
#define FW_ASSERT(...)
Definition: Assert.hpp:14