F´ Flight Software - C/C++ Documentation  devel
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 
18  Hash()
19  {
20  this->init();
21  }
22 
24  ~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 = static_cast<HASH_HANDLE_TYPE>(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 ::
47  init()
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 = static_cast<HASH_HANDLE_TYPE>(update_crc_32(this->hash_handle, c));
60  }
61  }
62 
63  void Hash ::
64  final(HashBuffer& buffer)
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 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:55
#define HASH_HANDLE_TYPE
Definition: CRC32.hpp:12
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
A container class for holding a hash buffer.
Definition: HashBuffer.hpp:26
static void hash(const void *data, const NATIVE_INT_TYPE len, HashBuffer &buffer)
Definition: CRC32.cpp:29
void init()
Definition: CRC32.cpp:47
void update(const void *const data, const NATIVE_INT_TYPE len)
Definition: CRC32.cpp:53
void setHashValue(HashBuffer &value)
Definition: CRC32.cpp:82
void final(HashBuffer &buffer)
Definition: CRC32.cpp:64
unsigned long update_crc_32(unsigned long crc, char c)
Definition: lib_crc.c:271
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.