F´ Flight Software - C/C++ Documentation NASA-v1.6.0
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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() : value(0)
24 {
25
26 }
27
28 Checksum ::
29 Checksum(const U32 value) : value(value)
30 {
31
32 }
33
34 Checksum ::
35 Checksum(const Checksum &original)
36 {
37 this->value = original.getValue();
38 }
39
40 Checksum ::
41 ~Checksum()
42 {
43
44 }
45
46 Checksum& Checksum ::
47 operator=(const Checksum& checksum)
48 {
49 this->value = checksum.value;
50 return *this;
51 }
52
53 bool Checksum ::
54 operator==(const Checksum& checksum) const
55 {
56 return this->value == checksum.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->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 // Add the last word unaligned if necessary
97 if (index < length) {
98 const U8 wordLength = static_cast<U8>(length - index);
99 this->addWordUnaligned(
100 &data[index],
101 static_cast<U8>(offset + index),
102 wordLength
103 );
104 }
105
106 }
107
108 void Checksum ::
109 addWordAligned(const U8 *const word)
110 {
111 for (U8 i = 0; i < 4; ++i)
112 addByteAtOffset(word[i], i);
113 }
114
115 void Checksum ::
116 addWordUnaligned(
117 const U8 *word,
118 const U8 position,
119 const U8 length
120 )
121 {
122 FW_ASSERT(length < 4);
123 U8 offset = position % 4;
124 for (U8 i = 0; i < length; ++i) {
125 addByteAtOffset(word[i], offset);
126 ++offset;
127 if (offset == 4)
128 offset = 0;
129 }
130 }
131
132 void Checksum ::
133 addByteAtOffset(
134 const U8 byte,
135 const U8 offset
136 )
137 {
138 FW_ASSERT(offset < 4);
139 const U32 addend = byte << (8*(3-offset));
140 this->value += addend;
141 }
142
143}
#define FW_ASSERT(...)
Definition Assert.hpp:7
uint8_t U8
8-bit unsigned integer
Definition BasicTypes.h:26
static U32 min(const U32 a, const U32 b)
Definition Checksum.cpp:16
Class representing a CFDP checksum.
Definition Checksum.hpp:23
U32 getValue() const
Get the checksum value.
Definition Checksum.cpp:66