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