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