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  U32 index = 0;
51 
52  // Add the first word unaligned if necessary
53  const U32 offsetMod4 = offset % 4;
54  if (offsetMod4 != 0) {
55  const U8 wordLength = static_cast<U8>(min(length, 4 - offsetMod4));
56  this->addWordUnaligned(&data[index], static_cast<U8>(offset + index), wordLength);
57  index += wordLength;
58  }
59 
60  // Add the middle words aligned
61  for (; index + 4 <= length; index += 4) {
62  addWordAligned(&data[index]);
63  }
64 
65  // Add the last word unaligned if necessary
66  if (index < length) {
67  const U8 wordLength = static_cast<U8>(length - index);
68  this->addWordUnaligned(&data[index], static_cast<U8>(offset + index), wordLength);
69  }
70 }
71 
72 void Checksum ::addWordAligned(const U8* const word) {
73  for (U8 i = 0; i < 4; ++i) {
74  addByteAtOffset(word[i], i);
75  }
76 }
77 
78 void Checksum ::addWordUnaligned(const U8* word, const U8 position, const U8 length) {
79  FW_ASSERT(length < 4);
80  U8 offset = position % 4;
81  for (U8 i = 0; i < length; ++i) {
82  addByteAtOffset(word[i], offset);
83  ++offset;
84  if (offset == 4) {
85  offset = 0;
86  }
87  }
88 }
89 
90 void Checksum ::addByteAtOffset(const U8 byte, const U8 offset) {
91  FW_ASSERT(offset < 4);
92  const U32 addend = static_cast<U32>(byte) << (8 * (3 - offset));
93  this->m_value += addend;
94 }
95 
96 } // 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:53
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
#define FW_ASSERT(...)
Definition: Assert.hpp:14