F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Loading...
Searching...
No Matches
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
16static U32 min(const U32 a, const U32 b) {
17 return (a < b) ? a : b;
18}
19
20namespace CFDP {
21
22 Checksum ::
23 Checksum() : m_value(0)
24 {
25
26 }
27
28 Checksum ::
29 Checksum(const U32 value) : m_value(value)
30 {
31
32 }
33
34 Checksum ::
35 Checksum(const Checksum &original)
36 {
37 this->m_value = original.getValue();
38 }
39
40 Checksum ::
41 ~Checksum()
42 {
43
44 }
45
46 Checksum& Checksum ::
47 operator=(const Checksum& checksum)
48 {
49 this->m_value = checksum.m_value;
50 return *this;
51 }
52
53 bool Checksum ::
54 operator==(const Checksum& checksum) const
55 {
56 return this->m_value == checksum.m_value;
57 }
58
59 bool Checksum ::
60 operator!=(const Checksum& checksum) const
61 {
62 return not (*this == checksum);
63 }
64
65 U32 Checksum ::
66 getValue() const
67 {
68 return this->m_value;
69 }
70
71 void Checksum ::
72 update(
73 const U8 *const data,
74 const U32 offset,
75 const U32 length
76 )
77 {
78 U32 index = 0;
79
80 // Add the first word unaligned if necessary
81 const U32 offsetMod4 = offset % 4;
82 if (offsetMod4 != 0) {
83 const U8 wordLength = static_cast<U8>(min(length, 4 - offsetMod4));
84 this->addWordUnaligned(
85 &data[index],
86 static_cast<U8>(offset + index),
87 wordLength
88 );
89 index += wordLength;
90 }
91
92 // Add the middle words aligned
93 for ( ; index + 4 <= length; index += 4) {
94 addWordAligned(&data[index]);
95 }
96
97 // Add the last word unaligned if necessary
98 if (index < length) {
99 const U8 wordLength = static_cast<U8>(length - index);
100 this->addWordUnaligned(
101 &data[index],
102 static_cast<U8>(offset + index),
103 wordLength
104 );
105 }
106
107 }
108
109 void Checksum ::
110 addWordAligned(const U8 *const word)
111 {
112 for (U8 i = 0; i < 4; ++i) {
113 addByteAtOffset(word[i], i);
114 }
115 }
116
117 void Checksum ::
118 addWordUnaligned(
119 const U8 *word,
120 const U8 position,
121 const U8 length
122 )
123 {
124 FW_ASSERT(length < 4);
125 U8 offset = position % 4;
126 for (U8 i = 0; i < length; ++i) {
127 addByteAtOffset(word[i], offset);
128 ++offset;
129 if (offset == 4) {
130 offset = 0;
131 }
132 }
133 }
134
135 void Checksum ::
136 addByteAtOffset(
137 const U8 byte,
138 const U8 offset
139 )
140 {
141 FW_ASSERT(offset < 4);
142 const U32 addend = static_cast<U32>(byte) << (8*(3-offset));
143 this->m_value += addend;
144 }
145
146}
#define FW_ASSERT(...)
Definition Assert.hpp:14
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:30
static U32 min(const U32 a, const U32 b)
Definition Checksum.cpp:16
Class representing a 32-bit checksum as mandated by the CCSDS File Delivery Protocol.
Definition Checksum.hpp:53
U32 getValue() const
Get the checksum value.
Definition Checksum.cpp:66