GCC Code Coverage Report


Directory: ./
File: CFDP/Checksum/Checksum.cpp
Date: 2026-11-03 22:10:14
Exec Total Coverage
Lines: 47 52 90.4%
Functions: 11 13 84.6%
Branches: 13 14 92.9%

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