GCC Code Coverage Report


Directory: ./
File: CFDP/Checksum/Checksum.cpp
Date: 2026-09-03 21:13:48
Exec Total Coverage
Lines: 50 52 96.2%
Functions: 12 13 92.3%
Branches: 14 16 87.5%

Line Branch Exec Source
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
13 #include "CFDP/Checksum/Checksum.hpp"
14 #include "Fw/Types/Assert.hpp"
15
16 12 static U32 min(const U32 a, const U32 b) {
17
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 return (a < b) ? a : b;
18 }
19
20 namespace CFDP {
21
22 13184 Checksum ::Checksum() : m_value(0) {}
23
24 13140 Checksum ::Checksum(const U32 value) : m_value(value) {}
25
26 1 Checksum ::Checksum(const Checksum& original) {
27 1 this->m_value = original.getValue();
28 1 }
29
30 26325 Checksum ::~Checksum() {}
31
32 13165 Checksum& Checksum ::operator=(const Checksum& checksum) {
33 13165 this->m_value = checksum.m_value;
34 13165 return *this;
35 }
36
37 7 bool Checksum ::operator==(const Checksum& checksum) const {
38 7 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 59 U32 Checksum ::getValue() const {
46 59 return this->m_value;
47 }
48
49 100 void Checksum ::update(const U8* const data, const U32 offset, const U32 length) {
50 100 FW_ASSERT(data != nullptr);
51 100 U32 index = 0;
52
53 // Add the first word unaligned if necessary
54 100 const U32 offsetMod4 = offset % 4;
55
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 88 times.
100 if (offsetMod4 != 0) {
56 12 const U8 wordLength = static_cast<U8>(min(length, 4 - offsetMod4));
57 12 this->addWordUnaligned(&data[index], static_cast<U8>(offset + index), wordLength);
58 12 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 100 const U32 alignedEnd = index + (((length - index) / 4) * 4);
64
2/2
✓ Branch 0 taken 24286 times.
✓ Branch 1 taken 100 times.
24386 for (; index < alignedEnd; index += 4) {
65 24286 addWordAligned(&data[index]);
66 }
67
68 // Add the last word unaligned if necessary
69
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 74 times.
100 if (index < length) {
70 26 const U8 wordLength = static_cast<U8>(length - index);
71 26 this->addWordUnaligned(&data[index], static_cast<U8>(offset + index), wordLength);
72 }
73 100 }
74
75 24286 void Checksum ::addWordAligned(const U8* const word) {
76 24286 FW_ASSERT(word != nullptr);
77
2/2
✓ Branch 0 taken 97144 times.
✓ Branch 1 taken 24286 times.
121430 for (U8 i = 0; i < 4; ++i) {
78 97144 addByteAtOffset(word[i], i);
79 }
80 24286 }
81
82 38 void Checksum ::addWordUnaligned(const U8* word, const U8 position, const U8 length) {
83 38 FW_ASSERT(word != nullptr);
84 38 FW_ASSERT(length < 4);
85 38 U8 offset = position % 4;
86
2/2
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 38 times.
118 for (U8 i = 0; i < length; ++i) {
87 80 addByteAtOffset(word[i], offset);
88 80 ++offset;
89
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 68 times.
80 if (offset == 4) {
90 12 offset = 0;
91 }
92 }
93 38 }
94
95 97224 void Checksum ::addByteAtOffset(const U8 byte, const U8 offset) {
96 97224 FW_ASSERT(offset < 4);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97224 times.
97224 const U32 addend = static_cast<U32>(byte) << (8 * (3 - offset));
98 97224 this->m_value += addend;
99 97224 }
100
101 } // namespace CFDP
102