F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Checksum.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title CFDP/Checksum/Checksum.cpp
3 // \author bocchino
4 // \brief cpp file for CFDP checksum class
5 //
6 // \copyright
7 // Copyright 2009-2016, by the California Institute of Technology.
8 // ALL RIGHTS RESERVED. United States Government Sponsorship
9 // acknowledged.
10 //
11 // ======================================================================
12 
14 #include "Fw/Types/Assert.hpp"
15 
16 static U32 min(const U32 a, const U32 b) {
17  return (a < b) ? a : b;
18 }
19 
20 namespace CFDP {
21 
22 Checksum ::Checksum() : m_value(0) {}
23 
24 Checksum ::Checksum(const U32 value) : m_value(value) {}
25 
26 Checksum ::Checksum(const Checksum& original) {
27  this->m_value = original.getValue();
28 }
29 
31 
33  this->m_value = checksum.m_value;
34  return *this;
35 }
36 
37 bool Checksum ::operator==(const Checksum& checksum) const {
38  return this->m_value == checksum.m_value;
39 }
40 
41 bool Checksum ::operator!=(const Checksum& checksum) const {
42  return not(*this == checksum);
43 }
44 
45 U32 Checksum ::getValue() const {
46  return this->m_value;
47 }
48 
49 void Checksum ::update(const U8* const data, const U32 offset, const U32 length) {
50  FW_ASSERT(data != nullptr);
51  U32 index = 0;
52 
53  // Add the first word unaligned if necessary
54  const U32 offsetMod4 = offset % 4;
55  if (offsetMod4 != 0) {
56  const U8 wordLength = static_cast<U8>(min(length, 4 - offsetMod4));
57  this->addWordUnaligned(&data[index], static_cast<U8>(offset + index), wordLength);
58  index += wordLength;
59  }
60 
61  // Add the middle words aligned
62  // End of the whole 4-byte words remaining after index, i.e. the largest index + 4*k <= length
63  const U32 alignedEnd = index + (((length - index) / 4) * 4);
64  for (; index < alignedEnd; index += 4) {
65  addWordAligned(&data[index]);
66  }
67 
68  // Add the last word unaligned if necessary
69  if (index < length) {
70  const U8 wordLength = static_cast<U8>(length - index);
71  this->addWordUnaligned(&data[index], static_cast<U8>(offset + index), wordLength);
72  }
73 }
74 
75 void Checksum ::addWordAligned(const U8* const word) {
76  FW_ASSERT(word != nullptr);
77  for (U8 i = 0; i < 4; ++i) {
78  addByteAtOffset(word[i], i);
79  }
80 }
81 
82 void Checksum ::addWordUnaligned(const U8* word, const U8 position, const U8 length) {
83  FW_ASSERT(word != nullptr);
84  FW_ASSERT(length < 4);
85  U8 offset = position % 4;
86  for (U8 i = 0; i < length; ++i) {
87  addByteAtOffset(word[i], offset);
88  ++offset;
89  if (offset == 4) {
90  offset = 0;
91  }
92  }
93 }
94 
95 void Checksum ::addByteAtOffset(const U8 byte, const U8 offset) {
96  FW_ASSERT(offset < 4);
97  const U32 addend = static_cast<U32>(byte) << (8 * (3 - offset));
98  this->m_value += addend;
99 }
100 
101 } // namespace CFDP
Checksum & operator=(const Checksum &checksum)
Assign checksum to this.
Definition: Checksum.cpp:32
Checksum()
Construct a fresh Checksum object.
Definition: Checksum.cpp:22
bool operator!=(const Checksum &checksum) const
Compare checksum and this for inequality.
Definition: Checksum.cpp:41
Class representing a 32-bit checksum as mandated by the CCSDS File Delivery Protocol.
Definition: Checksum.hpp:53
~Checksum()
Destroy a Checksum object.
Definition: Checksum.cpp:30
void update(const U8 *const data, const U32 offset, const U32 length)
Definition: Checksum.cpp:49
U32 getValue() const
Get the checksum value.
Definition: Checksum.cpp:45
bool operator==(const Checksum &checksum) const
Compare checksum and this for equality.
Definition: Checksum.cpp:37
uint8_t U8
8-bit unsigned integer
Definition: BasicTypes.h:54
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
#define FW_ASSERT(...)
Definition: Assert.hpp:14