| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <Fw/FPrimeBasicTypes.hpp> | ||
| 2 | #include <Fw/Types/Assert.hpp> | ||
| 3 | #include <Fw/Types/Serializable.hpp> | ||
| 4 | #include <Fw/Types/StringType.hpp> | ||
| 5 | #include <cstdio> | ||
| 6 | #include <cstring> // memcpy | ||
| 7 | #ifdef BUILD_UT | ||
| 8 | #include <Fw/Types/String.hpp> | ||
| 9 | #include <iomanip> | ||
| 10 | #endif | ||
| 11 | |||
| 12 | namespace Fw { | ||
| 13 | |||
| 14 | 101291 | Serializable::Serializable() {} | |
| 15 | |||
| 16 | 202950 | Serializable::~Serializable() {} | |
| 17 | |||
| 18 | // ---------------------------------------------------------------------- | ||
| 19 | #if FW_SERIALIZABLE_TO_STRING || FW_ENABLE_TEXT_LOGGING || BUILD_UT | ||
| 20 | |||
| 21 | ✗ | void Serializable::toString(StringBase& text) const { | |
| 22 | ✗ | text = "NOSPEC"; // set to not specified. | |
| 23 | ✗ | } | |
| 24 | |||
| 25 | #endif | ||
| 26 | |||
| 27 | #ifdef BUILD_UT | ||
| 28 | std::ostream& operator<<(std::ostream& os, const Serializable& val) { | ||
| 29 | Fw::String out; | ||
| 30 | val.toString(out); | ||
| 31 | |||
| 32 | os << out; | ||
| 33 | |||
| 34 | return os; | ||
| 35 | } | ||
| 36 | #endif | ||
| 37 | |||
| 38 | 84472 | SerialBufferBase::~SerialBufferBase() {} | |
| 39 | |||
| 40 | 42225 | LinearBufferBase::LinearBufferBase(U8* buffAddr, Serializable::SizeType capacity) | |
| 41 | 42225 | : m_buffAddr(buffAddr), m_capacity(capacity), m_serLoc(0), m_deserLoc(0) { | |
| 42 | // Ensure buffAddr & capacity are consistent | ||
| 43 | 42247 | FW_ASSERT((buffAddr == nullptr) == (capacity == 0), static_cast<FwAssertArgType>(capacity)); | |
| 44 | 42247 | } | |
| 45 | |||
| 46 | 84466 | LinearBufferBase::~LinearBufferBase() {} | |
| 47 | |||
| 48 | ✗ | void LinearBufferBase::copyFrom(const LinearBufferBase& src) { | |
| 49 | ✗ | this->m_serLoc = src.m_serLoc; | |
| 50 | ✗ | this->m_deserLoc = src.m_deserLoc; | |
| 51 | ✗ | const U8* srcBuffAddr = src.m_buffAddr; | |
| 52 | ✗ | U8* thisBuffAddr = this->m_buffAddr; | |
| 53 | ✗ | FW_ASSERT(srcBuffAddr != nullptr); | |
| 54 | ✗ | FW_ASSERT(thisBuffAddr != nullptr); | |
| 55 | // destination has to be same or bigger | ||
| 56 | ✗ | FW_ASSERT(src.m_serLoc <= this->m_capacity, static_cast<FwAssertArgType>(src.m_serLoc), | |
| 57 | static_cast<FwAssertArgType>(this->m_capacity)); | ||
| 58 | ✗ | (void)memcpy(thisBuffAddr, srcBuffAddr, static_cast<size_t>(this->m_serLoc)); | |
| 59 | ✗ | } | |
| 60 | |||
| 61 | // Copy constructor doesn't make sense in this virtual class as there is nothing to copy. Derived classes should | ||
| 62 | // call the empty constructor and then call their own copy function | ||
| 63 | ✗ | LinearBufferBase& LinearBufferBase::operator=(const LinearBufferBase& src) { // lgtm[cpp/rule-of-two] | |
| 64 | ✗ | this->copyFrom(src); | |
| 65 | ✗ | return *this; | |
| 66 | } | ||
| 67 | |||
| 68 | // serialization routines | ||
| 69 | |||
| 70 | 172903 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(U8 val, Endianness mode) { | |
| 71 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 172903 times.
|
172903 | if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->m_capacity) { |
| 72 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 73 | } | ||
| 74 | 172903 | U8* buffAddr = this->m_buffAddr; | |
| 75 | 172903 | FW_ASSERT(buffAddr != nullptr); | |
| 76 | 172905 | buffAddr[this->m_serLoc] = val; | |
| 77 | 172905 | this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 78 | 172905 | this->m_deserLoc = 0; | |
| 79 | |||
| 80 | 172905 | return FW_SERIALIZE_OK; | |
| 81 | } | ||
| 82 | |||
| 83 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(I8 val, Endianness mode) { | |
| 84 | ✗ | return serializeFrom(static_cast<U8>(val), mode); | |
| 85 | } | ||
| 86 | |||
| 87 | #if FW_HAS_16_BIT == 1 | ||
| 88 | 19316 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(U16 val, Endianness mode) { | |
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19316 times.
|
19316 | if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->m_capacity) { |
| 90 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 91 | } | ||
| 92 | 19316 | U8* buffAddr = this->m_buffAddr; | |
| 93 | 19316 | FW_ASSERT(buffAddr != nullptr); | |
| 94 |
1/3✓ Branch 0 taken 19315 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
19315 | switch (mode) { |
| 95 | 19315 | case Endianness::BIG: | |
| 96 | // MSB first | ||
| 97 | 19315 | buffAddr[this->m_serLoc + 0] = static_cast<U8>(val >> 8); | |
| 98 | 19315 | buffAddr[this->m_serLoc + 1] = static_cast<U8>(val); | |
| 99 | 19315 | break; | |
| 100 | ✗ | case Endianness::LITTLE: | |
| 101 | // LSB first | ||
| 102 | ✗ | buffAddr[this->m_serLoc + 0] = static_cast<U8>(val); | |
| 103 | ✗ | buffAddr[this->m_serLoc + 1] = static_cast<U8>(val >> 8); | |
| 104 | ✗ | break; | |
| 105 | ✗ | default: | |
| 106 | ✗ | FW_ASSERT(false); | |
| 107 | ✗ | break; | |
| 108 | } | ||
| 109 | 19315 | this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 110 | 19315 | this->m_deserLoc = 0; | |
| 111 | 19315 | return FW_SERIALIZE_OK; | |
| 112 | } | ||
| 113 | |||
| 114 | 8269 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(I16 val, Endianness mode) { | |
| 115 | 8269 | return serializeFrom(static_cast<U16>(val), mode); | |
| 116 | } | ||
| 117 | #endif | ||
| 118 | #if FW_HAS_32_BIT == 1 | ||
| 119 | 31922 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(U32 val, Endianness mode) { | |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 31922 times.
|
31922 | if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->m_capacity) { |
| 121 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 122 | } | ||
| 123 | 31922 | U8* buffAddr = this->m_buffAddr; | |
| 124 | 31922 | FW_ASSERT(buffAddr != nullptr); | |
| 125 |
1/3✓ Branch 0 taken 31922 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
31922 | switch (mode) { |
| 126 | 31922 | case Endianness::BIG: | |
| 127 | // MSB first | ||
| 128 | 31922 | buffAddr[this->m_serLoc + 0] = static_cast<U8>(val >> 24); | |
| 129 | 31922 | buffAddr[this->m_serLoc + 1] = static_cast<U8>(val >> 16); | |
| 130 | 31922 | buffAddr[this->m_serLoc + 2] = static_cast<U8>(val >> 8); | |
| 131 | 31922 | buffAddr[this->m_serLoc + 3] = static_cast<U8>(val); | |
| 132 | 31922 | break; | |
| 133 | ✗ | case Endianness::LITTLE: | |
| 134 | // LSB first | ||
| 135 | ✗ | buffAddr[this->m_serLoc + 0] = static_cast<U8>(val); | |
| 136 | ✗ | buffAddr[this->m_serLoc + 1] = static_cast<U8>(val >> 8); | |
| 137 | ✗ | buffAddr[this->m_serLoc + 2] = static_cast<U8>(val >> 16); | |
| 138 | ✗ | buffAddr[this->m_serLoc + 3] = static_cast<U8>(val >> 24); | |
| 139 | ✗ | break; | |
| 140 | ✗ | default: | |
| 141 | ✗ | FW_ASSERT(false); | |
| 142 | ✗ | break; | |
| 143 | } | ||
| 144 | 31922 | this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 145 | 31922 | this->m_deserLoc = 0; | |
| 146 | 31922 | return FW_SERIALIZE_OK; | |
| 147 | } | ||
| 148 | |||
| 149 | 10753 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(I32 val, Endianness mode) { | |
| 150 | 10753 | return serializeFrom(static_cast<U32>(val), mode); | |
| 151 | } | ||
| 152 | #endif | ||
| 153 | |||
| 154 | #if FW_HAS_64_BIT == 1 | ||
| 155 | 6111 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(U64 val, Endianness mode) { | |
| 156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6111 times.
|
6111 | if (this->m_serLoc + static_cast<Serializable::SizeType>(sizeof(val)) - 1 >= this->m_capacity) { |
| 157 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 158 | } | ||
| 159 | 6111 | U8* buffAddr = this->m_buffAddr; | |
| 160 | 6111 | FW_ASSERT(buffAddr != nullptr); | |
| 161 |
1/3✓ Branch 0 taken 6110 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
6110 | switch (mode) { |
| 162 | 6110 | case Endianness::BIG: | |
| 163 | // MSB first | ||
| 164 | 6110 | buffAddr[this->m_serLoc + 0] = static_cast<U8>(val >> 56); | |
| 165 | 6110 | buffAddr[this->m_serLoc + 1] = static_cast<U8>(val >> 48); | |
| 166 | 6110 | buffAddr[this->m_serLoc + 2] = static_cast<U8>(val >> 40); | |
| 167 | 6110 | buffAddr[this->m_serLoc + 3] = static_cast<U8>(val >> 32); | |
| 168 | 6110 | buffAddr[this->m_serLoc + 4] = static_cast<U8>(val >> 24); | |
| 169 | 6110 | buffAddr[this->m_serLoc + 5] = static_cast<U8>(val >> 16); | |
| 170 | 6110 | buffAddr[this->m_serLoc + 6] = static_cast<U8>(val >> 8); | |
| 171 | 6110 | buffAddr[this->m_serLoc + 7] = static_cast<U8>(val); | |
| 172 | 6110 | break; | |
| 173 | ✗ | case Endianness::LITTLE: | |
| 174 | // LSB first | ||
| 175 | ✗ | buffAddr[this->m_serLoc + 0] = static_cast<U8>(val); | |
| 176 | ✗ | buffAddr[this->m_serLoc + 1] = static_cast<U8>(val >> 8); | |
| 177 | ✗ | buffAddr[this->m_serLoc + 2] = static_cast<U8>(val >> 16); | |
| 178 | ✗ | buffAddr[this->m_serLoc + 3] = static_cast<U8>(val >> 24); | |
| 179 | ✗ | buffAddr[this->m_serLoc + 4] = static_cast<U8>(val >> 32); | |
| 180 | ✗ | buffAddr[this->m_serLoc + 5] = static_cast<U8>(val >> 40); | |
| 181 | ✗ | buffAddr[this->m_serLoc + 6] = static_cast<U8>(val >> 48); | |
| 182 | ✗ | buffAddr[this->m_serLoc + 7] = static_cast<U8>(val >> 56); | |
| 183 | ✗ | break; | |
| 184 | ✗ | default: | |
| 185 | ✗ | FW_ASSERT(false); | |
| 186 | ✗ | break; | |
| 187 | } | ||
| 188 | 6110 | this->m_serLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 189 | 6110 | this->m_deserLoc = 0; | |
| 190 | 6110 | return FW_SERIALIZE_OK; | |
| 191 | } | ||
| 192 | |||
| 193 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(I64 val, Endianness mode) { | |
| 194 | ✗ | return serializeFrom(static_cast<U64>(val), mode); | |
| 195 | } | ||
| 196 | #endif | ||
| 197 | |||
| 198 | 2 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(F64 val, Endianness mode) { | |
| 199 | // floating point values need to be byte-swapped as well, so copy to U64 and use that routine | ||
| 200 | U64 u64Val; | ||
| 201 | 2 | (void)memcpy(&u64Val, &val, sizeof(val)); | |
| 202 |
1/1✓ Branch 1 taken 2 times.
|
4 | return this->serializeFrom(u64Val, mode); |
| 203 | } | ||
| 204 | |||
| 205 | 1252 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(F32 val, Endianness mode) { | |
| 206 | // floating point values need to be byte-swapped as well, so copy to U32 and use that routine | ||
| 207 | U32 u32Val; | ||
| 208 | 1252 | (void)memcpy(&u32Val, &val, sizeof(val)); | |
| 209 |
1/1✓ Branch 1 taken 1252 times.
|
2504 | return this->serializeFrom(u32Val, mode); |
| 210 | } | ||
| 211 | |||
| 212 | 1418 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(bool val, Endianness mode) { | |
| 213 | // booleans are encoded as a single byte | ||
| 214 | static_assert(FW_SERIALIZE_TRUE_VALUE <= 0xFF && FW_SERIALIZE_FALSE_VALUE <= 0xFF, | ||
| 215 | "boolean wire values must fit in one byte"); | ||
| 216 | static_assert(static_cast<int>(FW_SERIALIZE_TRUE_VALUE) != static_cast<int>(FW_SERIALIZE_FALSE_VALUE), | ||
| 217 | "boolean wire values must be distinct"); | ||
| 218 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1412 times.
|
1418 | const U8 byteVal = val ? static_cast<U8>(FW_SERIALIZE_TRUE_VALUE) : static_cast<U8>(FW_SERIALIZE_FALSE_VALUE); |
| 219 | 1418 | return this->serializeFrom(byteVal, mode); | |
| 220 | } | ||
| 221 | |||
| 222 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(const void* val, Endianness mode) { | |
| 223 | // pointers are serialized as their integer representation | ||
| 224 | ✗ | return this->serializeFrom(reinterpret_cast<PlatformPointerCastType>(val), mode); | |
| 225 | } | ||
| 226 | |||
| 227 | 70 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(const U8* buff, | |
| 228 | Serializable::SizeType length, | ||
| 229 | Endianness endianMode) { | ||
| 230 | 70 | return this->serializeFrom(buff, static_cast<FwSizeType>(length), Serialization::INCLUDE_LENGTH, endianMode); | |
| 231 | } | ||
| 232 | |||
| 233 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus | ||
| 234 | 5276 | LinearBufferBase::serializeFrom(const U8* buff, | |
| 235 | FwSizeType length, | ||
| 236 | Serialization::t lengthMode, | ||
| 237 | Endianness endianMode) { // First serialize length | ||
| 238 | SerializeStatus stat; | ||
| 239 |
2/2✓ Branch 0 taken 110 times.
✓ Branch 1 taken 5166 times.
|
5276 | if (lengthMode == Serialization::INCLUDE_LENGTH) { |
| 240 | 110 | stat = this->serializeFrom(static_cast<FwSizeStoreType>(length), endianMode); | |
| 241 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 110 times.
|
110 | if (stat != FW_SERIALIZE_OK) { |
| 242 | ✗ | return stat; | |
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | // make sure we have enough space | ||
| 247 | // Note: m_serLoc <= m_capacity is a class invariant, so this subtraction cannot underflow. | ||
| 248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5276 times.
|
5276 | if (length > this->m_capacity - this->m_serLoc) { |
| 249 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 250 | } | ||
| 251 | |||
| 252 |
2/2✓ Branch 0 taken 5166 times.
✓ Branch 1 taken 110 times.
|
5276 | if (length > 0) { |
| 253 | 5166 | FW_ASSERT(buff != nullptr); | |
| 254 | } | ||
| 255 | 5276 | U8* buffAddr = this->m_buffAddr; | |
| 256 | 5276 | FW_ASSERT(buffAddr != nullptr); | |
| 257 | // copy buffer to our buffer; memmove is used because the source may overlap this buffer | ||
| 258 | 5274 | (void)memmove(&buffAddr[this->m_serLoc], buff, static_cast<size_t>(length)); | |
| 259 | 5274 | this->m_serLoc += static_cast<Serializable::SizeType>(length); | |
| 260 | 5274 | this->m_deserLoc = 0; | |
| 261 | |||
| 262 | 5274 | return FW_SERIALIZE_OK; | |
| 263 | } | ||
| 264 | |||
| 265 | 15720 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(const Serializable& val, | |
| 266 | Endianness mode) { | ||
| 267 | 15720 | return val.serializeTo(*this, mode); | |
| 268 | } | ||
| 269 | |||
| 270 | 2143 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeFrom(const LinearBufferBase& val, | |
| 271 | Endianness mode) { | ||
| 272 | 2143 | Serializable::SizeType size = val.getSize(); | |
| 273 | // Note: m_serLoc <= m_capacity is a class invariant, so this subtraction cannot underflow. | ||
| 274 | 2144 | Serializable::SizeType available = this->m_capacity - this->m_serLoc; | |
| 275 |
2/4✓ Branch 0 taken 2144 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2144 times.
|
2144 | if (size > available || static_cast<Serializable::SizeType>(sizeof(FwSizeStoreType)) > available - size) { |
| 276 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 277 | } | ||
| 278 | |||
| 279 | // First, serialize size | ||
| 280 | 2144 | SerializeStatus stat = this->serializeFrom(static_cast<FwSizeStoreType>(size), mode); | |
| 281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2139 times.
|
2139 | if (stat != FW_SERIALIZE_OK) { |
| 282 | ✗ | return stat; | |
| 283 | } | ||
| 284 | |||
| 285 | 2139 | U8* thisBuffAddr = this->m_buffAddr; | |
| 286 | 2139 | const U8* valBuffAddr = val.getBuffAddr(); | |
| 287 | 2140 | FW_ASSERT(thisBuffAddr != nullptr); | |
| 288 | 2140 | FW_ASSERT(valBuffAddr != nullptr); | |
| 289 | // serialize buffer | ||
| 290 | 2141 | (void)memcpy(&thisBuffAddr[this->m_serLoc], valBuffAddr, static_cast<size_t>(size)); | |
| 291 | 2141 | this->m_serLoc += size; | |
| 292 | 2141 | this->m_deserLoc = 0; | |
| 293 | |||
| 294 | 2141 | return FW_SERIALIZE_OK; | |
| 295 | } | ||
| 296 | |||
| 297 | 8 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeSize(const FwSizeType size, Endianness mode) { | |
| 298 | 8 | SerializeStatus status = FW_SERIALIZE_OK; | |
| 299 |
3/6✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 8 times.
|
8 | if ((size < std::numeric_limits<FwSizeStoreType>::min()) || (size > std::numeric_limits<FwSizeStoreType>::max())) { |
| 300 | ✗ | status = FW_SERIALIZE_FORMAT_ERROR; | |
| 301 | } | ||
| 302 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (status == FW_SERIALIZE_OK) { |
| 303 | 8 | status = this->serializeFrom(static_cast<FwSizeStoreType>(size), mode); | |
| 304 | } | ||
| 305 | 8 | return status; | |
| 306 | } | ||
| 307 | |||
| 308 | // deserialization routines | ||
| 309 | |||
| 310 | 11675 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(U8& val, Endianness mode) { | |
| 311 | // check for room | ||
| 312 | 11675 | const Serializable::SizeType size = this->m_serLoc; | |
| 313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11675 times.
|
11675 | if (size == this->m_deserLoc) { |
| 314 | ✗ | return FW_DESERIALIZE_BUFFER_EMPTY; | |
| 315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11675 times.
|
11675 | } else if (size - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) { |
| 316 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 317 | } | ||
| 318 | // read from current location | ||
| 319 | 11675 | U8* buffAddr = this->m_buffAddr; | |
| 320 | 11675 | FW_ASSERT(buffAddr != nullptr); | |
| 321 | 11675 | val = buffAddr[this->m_deserLoc + 0]; | |
| 322 | 11675 | this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 323 | 11675 | return FW_SERIALIZE_OK; | |
| 324 | } | ||
| 325 | |||
| 326 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(I8& val, Endianness mode) { | |
| 327 | // check for room | ||
| 328 | ✗ | const Serializable::SizeType size = this->m_serLoc; | |
| 329 | ✗ | if (size == this->m_deserLoc) { | |
| 330 | ✗ | return FW_DESERIALIZE_BUFFER_EMPTY; | |
| 331 | ✗ | } else if (size - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) { | |
| 332 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 333 | } | ||
| 334 | // read from current location | ||
| 335 | ✗ | U8* buffAddr = this->m_buffAddr; | |
| 336 | ✗ | FW_ASSERT(buffAddr != nullptr); | |
| 337 | ✗ | val = static_cast<I8>(buffAddr[this->m_deserLoc + 0]); | |
| 338 | ✗ | this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 339 | ✗ | return FW_SERIALIZE_OK; | |
| 340 | } | ||
| 341 | |||
| 342 | #if FW_HAS_16_BIT == 1 | ||
| 343 | 25268 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(U16& val, Endianness mode) { | |
| 344 | // check for room | ||
| 345 | 25268 | const Serializable::SizeType size = this->m_serLoc; | |
| 346 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25268 times.
|
25268 | if (size == this->m_deserLoc) { |
| 347 | ✗ | return FW_DESERIALIZE_BUFFER_EMPTY; | |
| 348 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 25268 times.
|
25268 | } else if (size - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) { |
| 349 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 350 | } | ||
| 351 | // read from current location | ||
| 352 | 25268 | U8* buffAddr = this->m_buffAddr; | |
| 353 | 25268 | FW_ASSERT(buffAddr != nullptr); | |
| 354 |
1/3✓ Branch 0 taken 25272 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
25272 | switch (mode) { |
| 355 | 25272 | case Endianness::BIG: | |
| 356 | // MSB first | ||
| 357 | 25272 | val = static_cast<U16>(((buffAddr[this->m_deserLoc + 1]) << 0) | ((buffAddr[this->m_deserLoc + 0]) << 8)); | |
| 358 | 25272 | break; | |
| 359 | ✗ | case Endianness::LITTLE: | |
| 360 | // LSB first | ||
| 361 | ✗ | val = static_cast<U16>(((buffAddr[this->m_deserLoc + 0]) << 0) | ((buffAddr[this->m_deserLoc + 1]) << 8)); | |
| 362 | ✗ | break; | |
| 363 | ✗ | default: | |
| 364 | ✗ | FW_ASSERT(false); | |
| 365 | ✗ | break; | |
| 366 | } | ||
| 367 | 25272 | this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 368 | 25272 | return FW_SERIALIZE_OK; | |
| 369 | } | ||
| 370 | |||
| 371 | 8242 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(I16& val, Endianness mode) { | |
| 372 | 8242 | U16 unsignVal = 0; | |
| 373 |
1/1✓ Branch 1 taken 8185 times.
|
8242 | SerializeStatus res = deserializeTo(unsignVal, mode); |
| 374 |
1/2✓ Branch 0 taken 8198 times.
✗ Branch 1 not taken.
|
8185 | if (res == SerializeStatus::FW_SERIALIZE_OK) { |
| 375 | 8198 | val = static_cast<I16>(unsignVal); | |
| 376 | } | ||
| 377 | 8185 | return res; | |
| 378 | } | ||
| 379 | #endif | ||
| 380 | #if FW_HAS_32_BIT == 1 | ||
| 381 | 17980 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(U32& val, Endianness mode) { | |
| 382 | // check for room | ||
| 383 | 17980 | const Serializable::SizeType size = this->m_serLoc; | |
| 384 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17980 times.
|
17980 | if (size == this->m_deserLoc) { |
| 385 | ✗ | return FW_DESERIALIZE_BUFFER_EMPTY; | |
| 386 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17980 times.
|
17980 | } else if (size - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) { |
| 387 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 388 | } | ||
| 389 | // read from current location | ||
| 390 | 17980 | U8* buffAddr = this->m_buffAddr; | |
| 391 | 17980 | FW_ASSERT(buffAddr != nullptr); | |
| 392 |
1/3✓ Branch 0 taken 17963 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
17963 | switch (mode) { |
| 393 | 17963 | case Endianness::BIG: | |
| 394 | // MSB first | ||
| 395 | 17963 | val = (static_cast<U32>(buffAddr[this->m_deserLoc + 3]) << 0) | | |
| 396 | 17963 | (static_cast<U32>(buffAddr[this->m_deserLoc + 2]) << 8) | | |
| 397 | 17963 | (static_cast<U32>(buffAddr[this->m_deserLoc + 1]) << 16) | | |
| 398 | 17963 | (static_cast<U32>(buffAddr[this->m_deserLoc + 0]) << 24); | |
| 399 | 17963 | break; | |
| 400 | ✗ | case Endianness::LITTLE: | |
| 401 | // LSB first | ||
| 402 | ✗ | val = (static_cast<U32>(buffAddr[this->m_deserLoc + 0]) << 0) | | |
| 403 | ✗ | (static_cast<U32>(buffAddr[this->m_deserLoc + 1]) << 8) | | |
| 404 | ✗ | (static_cast<U32>(buffAddr[this->m_deserLoc + 2]) << 16) | | |
| 405 | ✗ | (static_cast<U32>(buffAddr[this->m_deserLoc + 3]) << 24); | |
| 406 | ✗ | break; | |
| 407 | ✗ | default: | |
| 408 | ✗ | FW_ASSERT(false); | |
| 409 | ✗ | break; | |
| 410 | } | ||
| 411 | 17963 | this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 412 | 17963 | return FW_SERIALIZE_OK; | |
| 413 | } | ||
| 414 | |||
| 415 | 10176 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(I32& val, Endianness mode) { | |
| 416 | 10176 | U32 unsignVal = 0; | |
| 417 |
1/1✓ Branch 1 taken 10149 times.
|
10176 | SerializeStatus res = deserializeTo(unsignVal, mode); |
| 418 |
1/2✓ Branch 0 taken 10172 times.
✗ Branch 1 not taken.
|
10149 | if (res == SerializeStatus::FW_SERIALIZE_OK) { |
| 419 | 10172 | val = static_cast<I32>(unsignVal); | |
| 420 | } | ||
| 421 | 10149 | return res; | |
| 422 | } | ||
| 423 | #endif | ||
| 424 | |||
| 425 | #if FW_HAS_64_BIT == 1 | ||
| 426 | |||
| 427 | 5024 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(U64& val, Endianness mode) { | |
| 428 | // check for room | ||
| 429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5024 times.
|
5024 | if (this->m_serLoc == this->m_deserLoc) { |
| 430 | ✗ | return FW_DESERIALIZE_BUFFER_EMPTY; | |
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5024 times.
|
5024 | } else if (this->m_serLoc - this->m_deserLoc < static_cast<Serializable::SizeType>(sizeof(val))) { |
| 432 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 433 | } | ||
| 434 | // read from current location | ||
| 435 | 5024 | U8* buffAddr = this->m_buffAddr; | |
| 436 | 5024 | FW_ASSERT(buffAddr != nullptr); | |
| 437 |
1/3✓ Branch 0 taken 5023 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
5023 | switch (mode) { |
| 438 | 5023 | case Endianness::BIG: | |
| 439 | // MSB first | ||
| 440 | 5023 | val = (static_cast<U64>(buffAddr[this->m_deserLoc + 7]) << 0) | | |
| 441 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 6]) << 8) | | |
| 442 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 5]) << 16) | | |
| 443 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 4]) << 24) | | |
| 444 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 3]) << 32) | | |
| 445 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 2]) << 40) | | |
| 446 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 1]) << 48) | | |
| 447 | 5023 | (static_cast<U64>(buffAddr[this->m_deserLoc + 0]) << 56); | |
| 448 | 5023 | break; | |
| 449 | ✗ | case Endianness::LITTLE: | |
| 450 | // LSB first | ||
| 451 | ✗ | val = (static_cast<U64>(buffAddr[this->m_deserLoc + 0]) << 0) | | |
| 452 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 1]) << 8) | | |
| 453 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 2]) << 16) | | |
| 454 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 3]) << 24) | | |
| 455 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 4]) << 32) | | |
| 456 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 5]) << 40) | | |
| 457 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 6]) << 48) | | |
| 458 | ✗ | (static_cast<U64>(buffAddr[this->m_deserLoc + 7]) << 56); | |
| 459 | ✗ | break; | |
| 460 | ✗ | default: | |
| 461 | ✗ | FW_ASSERT(false); | |
| 462 | ✗ | break; | |
| 463 | } | ||
| 464 | 5023 | this->m_deserLoc += static_cast<Serializable::SizeType>(sizeof(val)); | |
| 465 | 5023 | return FW_SERIALIZE_OK; | |
| 466 | } | ||
| 467 | |||
| 468 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(I64& val, Endianness mode) { | |
| 469 | U64 unsignVal; | ||
| 470 | ✗ | SerializeStatus res = deserializeTo(unsignVal, mode); | |
| 471 | ✗ | if (res == SerializeStatus::FW_SERIALIZE_OK) { | |
| 472 | ✗ | val = static_cast<I64>(unsignVal); | |
| 473 | } | ||
| 474 | ✗ | return res; | |
| 475 | } | ||
| 476 | #endif | ||
| 477 | |||
| 478 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(F64& val, Endianness mode) { | |
| 479 | // deserialize as 64-bit int to handle endianness | ||
| 480 | U64 tempVal; | ||
| 481 | ✗ | SerializeStatus stat = this->deserializeTo(tempVal, mode); | |
| 482 | ✗ | if (stat != FW_SERIALIZE_OK) { | |
| 483 | ✗ | return stat; | |
| 484 | } | ||
| 485 | // copy to argument | ||
| 486 | ✗ | (void)memcpy(&val, &tempVal, sizeof(val)); | |
| 487 | |||
| 488 | ✗ | return FW_SERIALIZE_OK; | |
| 489 | } | ||
| 490 | |||
| 491 | 1407 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(bool& val, Endianness mode) { | |
| 492 | 1407 | U8 byteVal = 0; | |
| 493 |
1/1✓ Branch 1 taken 1407 times.
|
1407 | const SerializeStatus stat = this->deserializeTo(byteVal, mode); |
| 494 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1407 times.
|
1407 | if (stat != FW_SERIALIZE_OK) { |
| 495 | ✗ | return stat; | |
| 496 | } | ||
| 497 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1406 times.
|
1407 | if (FW_SERIALIZE_TRUE_VALUE == byteVal) { |
| 498 | 1 | val = true; | |
| 499 |
1/2✓ Branch 0 taken 1406 times.
✗ Branch 1 not taken.
|
1406 | } else if (FW_SERIALIZE_FALSE_VALUE == byteVal) { |
| 500 | 1406 | val = false; | |
| 501 | } else { | ||
| 502 | // leave the malformed byte unconsumed | ||
| 503 | ✗ | this->m_deserLoc -= static_cast<Serializable::SizeType>(sizeof(byteVal)); | |
| 504 | ✗ | return FW_DESERIALIZE_FORMAT_ERROR; | |
| 505 | } | ||
| 506 | 1407 | return FW_SERIALIZE_OK; | |
| 507 | } | ||
| 508 | |||
| 509 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(void*& val, Endianness mode) { | |
| 510 | // Deserialize as pointer cast, then convert to void* | ||
| 511 | ✗ | PlatformPointerCastType pointerCastVal = 0; | |
| 512 | ✗ | const SerializeStatus stat = this->deserializeTo(pointerCastVal, mode); | |
| 513 | ✗ | if (stat == FW_SERIALIZE_OK) { | |
| 514 | ✗ | val = reinterpret_cast<void*>(pointerCastVal); | |
| 515 | } | ||
| 516 | ✗ | return stat; | |
| 517 | } | ||
| 518 | |||
| 519 | 4 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(F32& val, Endianness mode) { | |
| 520 | // deserialize as 32-bit int to handle endianness | ||
| 521 | 4 | U32 tempVal = 0; | |
| 522 |
1/1✓ Branch 1 taken 4 times.
|
4 | SerializeStatus stat = this->deserializeTo(tempVal, mode); |
| 523 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (stat != FW_SERIALIZE_OK) { |
| 524 | ✗ | return stat; | |
| 525 | } | ||
| 526 | 4 | (void)memcpy(&val, &tempVal, sizeof(val)); | |
| 527 | |||
| 528 | 4 | return FW_SERIALIZE_OK; | |
| 529 | } | ||
| 530 | |||
| 531 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(U8* buff, | |
| 532 | Serializable::SizeType& length, | ||
| 533 | Endianness endianMode) { | ||
| 534 | ✗ | FwSizeType length_in_out = static_cast<FwSizeType>(length); | |
| 535 | ✗ | SerializeStatus status = this->deserializeTo(buff, length_in_out, Serialization::INCLUDE_LENGTH, endianMode); | |
| 536 | ✗ | length = static_cast<Serializable::SizeType>(length_in_out); | |
| 537 | ✗ | return status; | |
| 538 | } | ||
| 539 | |||
| 540 | 26 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(U8* buff, | |
| 541 | Serializable::SizeType& length, | ||
| 542 | Serialization::t lengthMode, | ||
| 543 | Endianness endianMode) { | ||
| 544 | 26 | U8* buffAddr = this->m_buffAddr; | |
| 545 | 26 | FW_ASSERT(buffAddr != nullptr); | |
| 546 | |||
| 547 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 16 times.
|
26 | if (lengthMode == Serialization::INCLUDE_LENGTH) { |
| 548 | 10 | FwSizeStoreType storedLength = 0; | |
| 549 | |||
| 550 |
1/1✓ Branch 1 taken 10 times.
|
10 | SerializeStatus stat = this->deserializeTo(storedLength, endianMode); |
| 551 | |||
| 552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (stat != FW_SERIALIZE_OK) { |
| 553 | ✗ | return stat; | |
| 554 | } | ||
| 555 | |||
| 556 | // make sure it fits | ||
| 557 |
4/7✓ Branch 1 taken 10 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
|
10 | if ((storedLength > this->getDeserializeSizeLeft()) || (storedLength > length)) { |
| 558 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 559 | } | ||
| 560 | |||
| 561 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if (storedLength > 0) { |
| 562 | 10 | FW_ASSERT(buff != nullptr); | |
| 563 | } | ||
| 564 | 10 | (void)memcpy(buff, &buffAddr[this->m_deserLoc], static_cast<size_t>(storedLength)); | |
| 565 | |||
| 566 | 10 | length = static_cast<FwSizeType>(storedLength); | |
| 567 | |||
| 568 | } else { | ||
| 569 | // make sure enough is left | ||
| 570 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
|
16 | if (length > this->getDeserializeSizeLeft()) { |
| 571 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 572 | } | ||
| 573 | |||
| 574 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if (length > 0) { |
| 575 | 16 | FW_ASSERT(buff != nullptr); | |
| 576 | } | ||
| 577 | 16 | (void)memcpy(buff, &buffAddr[this->m_deserLoc], static_cast<size_t>(length)); | |
| 578 | } | ||
| 579 | |||
| 580 | 26 | this->m_deserLoc += static_cast<Serializable::SizeType>(length); | |
| 581 | 26 | return FW_SERIALIZE_OK; | |
| 582 | } | ||
| 583 | |||
| 584 | 6963 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(Serializable& val, Endianness mode) { | |
| 585 | 6963 | return val.deserializeFrom(*this, mode); | |
| 586 | } | ||
| 587 | |||
| 588 | 2144 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeTo(LinearBufferBase& val, Endianness mode) { | |
| 589 | 2144 | U8* valBuffAddr = val.getBuffAddr(); | |
| 590 | 2143 | FW_ASSERT(valBuffAddr != nullptr); | |
| 591 | 2143 | SerializeStatus stat = FW_SERIALIZE_OK; | |
| 592 | |||
| 593 | 2143 | FwSizeStoreType storedLength = 0; | |
| 594 | |||
| 595 |
1/1✓ Branch 1 taken 2142 times.
|
2143 | stat = this->deserializeTo(storedLength, mode); |
| 596 | |||
| 597 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2142 times.
|
2142 | if (stat != FW_SERIALIZE_OK) { |
| 598 | ✗ | return stat; | |
| 599 | } | ||
| 600 | |||
| 601 | // make sure destination has enough room | ||
| 602 | |||
| 603 |
5/8✓ Branch 1 taken 2144 times.
✓ Branch 3 taken 2144 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 2143 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2143 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 2143 times.
|
2142 | if ((storedLength > val.getCapacity()) || (storedLength > this->getDeserializeSizeLeft())) { |
| 604 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 605 | } | ||
| 606 | |||
| 607 | 2143 | U8* thisBuffAddr = this->m_buffAddr; | |
| 608 | 2143 | FW_ASSERT(thisBuffAddr != nullptr); | |
| 609 | 2143 | (void)memcpy(valBuffAddr, &thisBuffAddr[this->m_deserLoc], static_cast<size_t>(storedLength)); | |
| 610 | |||
| 611 |
1/1✓ Branch 1 taken 2142 times.
|
2143 | stat = val.setBuffLen(storedLength); |
| 612 | |||
| 613 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2142 times.
|
2142 | if (stat != FW_SERIALIZE_OK) { |
| 614 | ✗ | return stat; | |
| 615 | } | ||
| 616 | |||
| 617 | 2142 | this->m_deserLoc += storedLength; | |
| 618 | |||
| 619 | 2142 | return FW_SERIALIZE_OK; | |
| 620 | } | ||
| 621 | |||
| 622 | 2 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeSize(FwSizeType& size, Endianness mode) { | |
| 623 | 2 | FwSizeStoreType storedSize = 0; | |
| 624 |
1/1✓ Branch 1 taken 2 times.
|
2 | Fw::SerializeStatus status = this->deserializeTo(storedSize, mode); |
| 625 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (status == FW_SERIALIZE_OK) { |
| 626 | 2 | size = static_cast<FwSizeType>(storedSize); | |
| 627 | } | ||
| 628 | 2 | return status; | |
| 629 | } | ||
| 630 | |||
| 631 | 43462 | void LinearBufferBase::resetSer() { | |
| 632 | 43462 | this->m_deserLoc = 0; | |
| 633 | 43462 | this->m_serLoc = 0; | |
| 634 | 43462 | } | |
| 635 | |||
| 636 | 41609 | void LinearBufferBase::resetDeser() { | |
| 637 | 41609 | this->m_deserLoc = 0; | |
| 638 | 41609 | } | |
| 639 | |||
| 640 | 239 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serializeSkip(FwSizeType numBytesToSkip) { | |
| 641 | 239 | Fw::SerializeStatus status = FW_SERIALIZE_OK; | |
| 642 | // compute new ser loc | ||
| 643 | 239 | const FwSizeType newSerLoc = this->m_serLoc + numBytesToSkip; | |
| 644 | // check for room | ||
| 645 |
1/2✓ Branch 0 taken 239 times.
✗ Branch 1 not taken.
|
239 | if (newSerLoc <= this->m_capacity) { |
| 646 | // update ser loc | ||
| 647 | 239 | this->m_serLoc = static_cast<Serializable::SizeType>(newSerLoc); | |
| 648 | } else { | ||
| 649 | ✗ | status = FW_SERIALIZE_NO_ROOM_LEFT; | |
| 650 | } | ||
| 651 | 239 | return status; | |
| 652 | } | ||
| 653 | |||
| 654 | 1457 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserializeSkip(FwSizeType numBytesToSkip) { | |
| 655 | // check for room | ||
| 656 | 1457 | const Serializable::SizeType size = this->m_serLoc; | |
| 657 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1457 times.
|
1457 | if (size == this->m_deserLoc) { |
| 658 | ✗ | return FW_DESERIALIZE_BUFFER_EMPTY; | |
| 659 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1457 times.
|
1457 | } else if (size - this->m_deserLoc < numBytesToSkip) { |
| 660 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 661 | } | ||
| 662 | // update location in buffer to skip the value | ||
| 663 | 1457 | this->m_deserLoc += static_cast<Serializable::SizeType>(numBytesToSkip); | |
| 664 | 1457 | return FW_SERIALIZE_OK; | |
| 665 | } | ||
| 666 | |||
| 667 | 239 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::moveSerToOffset(FwSizeType offset) { | |
| 668 | // Reset serialization | ||
| 669 | 239 | this->resetSer(); | |
| 670 | // Advance to offset | ||
| 671 | 239 | return this->serializeSkip(offset); | |
| 672 | } | ||
| 673 | 1457 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::moveDeserToOffset(FwSizeType offset) { | |
| 674 | // Reset deserialization | ||
| 675 | 1457 | this->resetDeser(); | |
| 676 | // Advance to offset | ||
| 677 | 1457 | return this->deserializeSkip(offset); | |
| 678 | } | ||
| 679 | |||
| 680 | 27705 | Serializable::SizeType LinearBufferBase::getSize() const { | |
| 681 | 27705 | return this->m_serLoc; | |
| 682 | } | ||
| 683 | |||
| 684 | 3814 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::setBuff(const U8* src, Serializable::SizeType length) { | |
| 685 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3814 times.
|
3814 | if (this->m_capacity < length) { |
| 686 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 687 | } else { | ||
| 688 | 3814 | FW_ASSERT(src); | |
| 689 | 3814 | U8* buffAddr = this->m_buffAddr; | |
| 690 | 3814 | FW_ASSERT(buffAddr != nullptr); | |
| 691 | 3814 | (void)memcpy(buffAddr, src, static_cast<size_t>(length)); | |
| 692 | 3814 | this->m_serLoc = length; | |
| 693 | 3814 | this->m_deserLoc = 0; | |
| 694 | 3814 | return FW_SERIALIZE_OK; | |
| 695 | } | ||
| 696 | } | ||
| 697 | |||
| 698 | 16941 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::setBuffLen(Serializable::SizeType length) { | |
| 699 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16941 times.
|
16941 | if (this->m_capacity < length) { |
| 700 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 701 | } else { | ||
| 702 | 16941 | this->m_serLoc = length; | |
| 703 | 16941 | this->m_deserLoc = 0; | |
| 704 | 16941 | return FW_SERIALIZE_OK; | |
| 705 | } | ||
| 706 | } | ||
| 707 | |||
| 708 | 4441 | Serializable::SizeType LinearBufferBase::getDeserializeSizeLeft() const { | |
| 709 | 4441 | FW_ASSERT(this->m_serLoc >= this->m_deserLoc, static_cast<FwAssertArgType>(this->m_serLoc), | |
| 710 | static_cast<FwAssertArgType>(this->m_deserLoc)); | ||
| 711 | 4442 | return this->m_serLoc - this->m_deserLoc; | |
| 712 | } | ||
| 713 | |||
| 714 | ✗ | Serializable::SizeType LinearBufferBase::getSerializeSizeLeft() const { | |
| 715 | ✗ | FW_ASSERT(this->m_capacity >= this->m_serLoc, static_cast<FwAssertArgType>(this->m_capacity), | |
| 716 | static_cast<FwAssertArgType>(this->m_serLoc)); | ||
| 717 | ✗ | return this->m_capacity - this->m_serLoc; | |
| 718 | } | ||
| 719 | |||
| 720 | 37 | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::copyRaw(SerialBufferBase& dest, | |
| 721 | Serializable::SizeType size) { | ||
| 722 | // make sure there is sufficient size in destination | ||
| 723 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
|
37 | if (dest.getCapacity() < size) { |
| 724 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 725 | } | ||
| 726 | // make sure there is sufficient buffer in source | ||
| 727 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
|
37 | if (this->getDeserializeSizeLeft() < size) { |
| 728 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 729 | } | ||
| 730 | |||
| 731 | // otherwise, set destination buffer to data from deserialization pointer plus size | ||
| 732 | 37 | U8* buffAddr = this->m_buffAddr; | |
| 733 | |||
| 734 | 37 | FW_ASSERT(buffAddr); | |
| 735 | 37 | SerializeStatus stat = dest.setBuff(&buffAddr[this->m_deserLoc], size); | |
| 736 |
1/2✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
|
37 | if (stat == FW_SERIALIZE_OK) { |
| 737 | 37 | this->m_deserLoc += size; | |
| 738 | } | ||
| 739 | 37 | return stat; | |
| 740 | } | ||
| 741 | |||
| 742 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::copyRawOffset(SerialBufferBase& dest, | |
| 743 | Serializable::SizeType size) { | ||
| 744 | // make sure there is sufficient size in destination | ||
| 745 | ✗ | const Serializable::SizeType destCapacity = dest.getCapacity(); | |
| 746 | ✗ | const Serializable::SizeType destSize = dest.getSize(); | |
| 747 | ✗ | if (destCapacity < size + destSize) { | |
| 748 | ✗ | return FW_SERIALIZE_NO_ROOM_LEFT; | |
| 749 | } | ||
| 750 | // make sure there is sufficient buffer in source | ||
| 751 | ✗ | if (this->getDeserializeSizeLeft() < size) { | |
| 752 | ✗ | return FW_DESERIALIZE_SIZE_MISMATCH; | |
| 753 | } | ||
| 754 | |||
| 755 | // otherwise, serialize bytes to destination without writing length | ||
| 756 | ✗ | U8* buffAddr = this->m_buffAddr; | |
| 757 | ✗ | SerializeStatus stat = dest.serializeFrom(&buffAddr[this->m_deserLoc], size, Serialization::OMIT_LENGTH); | |
| 758 | ✗ | if (stat == FW_SERIALIZE_OK) { | |
| 759 | ✗ | this->m_deserLoc += size; | |
| 760 | } | ||
| 761 | ✗ | return stat; | |
| 762 | } | ||
| 763 | |||
| 764 | // return address of buffer not yet deserialized. This is used | ||
| 765 | // to copy the remainder of a buffer. | ||
| 766 | 4 | const U8* LinearBufferBase::getBuffAddrLeft() const { | |
| 767 | 4 | const U8* buffAddr = this->m_buffAddr; | |
| 768 | 4 | FW_ASSERT(buffAddr != nullptr); | |
| 769 | 4 | return &buffAddr[this->m_deserLoc]; | |
| 770 | } | ||
| 771 | |||
| 772 | //!< gets address of end of serialization. Used to manually place data at the end | ||
| 773 | 8513 | U8* LinearBufferBase::getBuffAddrSer() { | |
| 774 | 8513 | U8* buffAddr = this->m_buffAddr; | |
| 775 | 8513 | FW_ASSERT(buffAddr != nullptr); | |
| 776 | 8517 | return &buffAddr[this->m_serLoc]; | |
| 777 | } | ||
| 778 | |||
| 779 | #ifdef BUILD_UT | ||
| 780 | bool LinearBufferBase::operator==(const LinearBufferBase& other) const { | ||
| 781 | if (this->m_serLoc != other.getSize()) { | ||
| 782 | return false; | ||
| 783 | } | ||
| 784 | |||
| 785 | const U8* us = this->m_buffAddr; | ||
| 786 | const U8* them = other.getBuffAddr(); | ||
| 787 | |||
| 788 | FW_ASSERT(us); | ||
| 789 | FW_ASSERT(them); | ||
| 790 | |||
| 791 | for (Serializable::SizeType byte = 0; byte < this->m_serLoc; byte++) { | ||
| 792 | if (us[byte] != them[byte]) { | ||
| 793 | return false; | ||
| 794 | } | ||
| 795 | } | ||
| 796 | |||
| 797 | return true; | ||
| 798 | } | ||
| 799 | |||
| 800 | std::ostream& operator<<(std::ostream& os, const LinearBufferBase& buff) { | ||
| 801 | const U8* us = buff.getBuffAddr(); | ||
| 802 | |||
| 803 | FW_ASSERT(us); | ||
| 804 | |||
| 805 | for (Serializable::SizeType byte = 0; byte < buff.getSize(); byte++) { | ||
| 806 | os << "[" << std::setw(2) << std::hex << std::setfill('0') << us[byte] << "]" << std::dec; | ||
| 807 | } | ||
| 808 | |||
| 809 | return os; | ||
| 810 | } | ||
| 811 | #endif | ||
| 812 | |||
| 813 | 10291 | ExternalSerializeBuffer::ExternalSerializeBuffer(U8* buffPtr, Serializable::SizeType size) | |
| 814 | 10291 | : LinearBufferBase(nullptr, 0) { | |
| 815 |
1/1✓ Branch 1 taken 10291 times.
|
10291 | this->setExtBuffer(buffPtr, size); |
| 816 | 10291 | } | |
| 817 | |||
| 818 | 9062 | ExternalSerializeBuffer::ExternalSerializeBuffer() : LinearBufferBase(nullptr, 0) { | |
| 819 |
1/1✓ Branch 1 taken 9062 times.
|
9062 | this->clear(); |
| 820 | 9062 | } | |
| 821 | |||
| 822 | 23389 | void ExternalSerializeBuffer::setExtBuffer(U8* buffPtr, Serializable::SizeType size) { | |
| 823 | // Allow setting buffer with null pointer and zero size (for initialization/reset) | ||
| 824 | // but reject null pointer with non-zero size | ||
| 825 | 23389 | FW_ASSERT((buffPtr != nullptr) || (size == 0), static_cast<FwAssertArgType>(size)); | |
| 826 | 23389 | this->clear(); | |
| 827 | 23389 | this->setBuffAddrInternal(buffPtr); | |
| 828 | 23389 | this->setCapacityInternal(size); | |
| 829 | 23389 | } | |
| 830 | |||
| 831 | 32454 | void ExternalSerializeBuffer::clear() { | |
| 832 | 32454 | this->resetSer(); | |
| 833 | 32454 | this->resetDeser(); | |
| 834 | 32454 | this->setBuffAddrInternal(nullptr); | |
| 835 | 32454 | this->setCapacityInternal(0); | |
| 836 | 32454 | } | |
| 837 | |||
| 838 | // ---------------------------------------------------------------------- | ||
| 839 | // Deprecated method implementations for backward compatibility | ||
| 840 | // ---------------------------------------------------------------------- | ||
| 841 | |||
| 842 | ✗ | Serializable::SizeType LinearBufferBase::getBuffLength() const { | |
| 843 | ✗ | return this->m_serLoc; | |
| 844 | } | ||
| 845 | |||
| 846 | ✗ | Serializable::SizeType LinearBufferBase::getBuffLeft() { | |
| 847 | ✗ | return this->getDeserializeSizeLeft(); | |
| 848 | } | ||
| 849 | |||
| 850 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(U8 val) { | |
| 851 | ✗ | return this->serializeFrom(val); | |
| 852 | } | ||
| 853 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(I8 val) { | |
| 854 | ✗ | return this->serializeFrom(val); | |
| 855 | } | ||
| 856 | #if FW_HAS_16_BIT == 1 | ||
| 857 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(U16 val) { | |
| 858 | ✗ | return this->serializeFrom(val); | |
| 859 | } | ||
| 860 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(I16 val) { | |
| 861 | ✗ | return this->serializeFrom(val); | |
| 862 | } | ||
| 863 | #endif | ||
| 864 | #if FW_HAS_32_BIT == 1 | ||
| 865 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(U32 val) { | |
| 866 | ✗ | return this->serializeFrom(val); | |
| 867 | } | ||
| 868 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(I32 val) { | |
| 869 | ✗ | return this->serializeFrom(val); | |
| 870 | } | ||
| 871 | #endif | ||
| 872 | #if FW_HAS_64_BIT == 1 | ||
| 873 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(U64 val) { | |
| 874 | ✗ | return this->serializeFrom(val); | |
| 875 | } | ||
| 876 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(I64 val) { | |
| 877 | ✗ | return this->serializeFrom(val); | |
| 878 | } | ||
| 879 | #endif | ||
| 880 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(F32 val) { | |
| 881 | ✗ | return this->serializeFrom(val); | |
| 882 | } | ||
| 883 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(F64 val) { | |
| 884 | ✗ | return this->serializeFrom(val); | |
| 885 | } | ||
| 886 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(bool val) { | |
| 887 | ✗ | return this->serializeFrom(val); | |
| 888 | } | ||
| 889 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(const void* val) { | |
| 890 | ✗ | return this->serializeFrom(val); | |
| 891 | } | ||
| 892 | |||
| 893 | // Deprecated method for backward compatibility | ||
| 894 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(const U8* buff, | |
| 895 | FwSizeType length, | ||
| 896 | bool noLength) { | ||
| 897 | ✗ | const Serialization::t mode = noLength ? Serialization::OMIT_LENGTH : Serialization::INCLUDE_LENGTH; | |
| 898 | ✗ | return this->serializeFrom(buff, length, mode); | |
| 899 | } | ||
| 900 | |||
| 901 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(const U8* buff, FwSizeType length) { | |
| 902 | ✗ | return this->serializeFrom(buff, length); | |
| 903 | } | ||
| 904 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(const U8* buff, | |
| 905 | FwSizeType length, | ||
| 906 | Serialization::t mode) { | ||
| 907 | ✗ | return this->serializeFrom(buff, length, mode); | |
| 908 | } | ||
| 909 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(const Serializable& val) { | |
| 910 | ✗ | return this->serializeFrom(val); | |
| 911 | } | ||
| 912 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::serialize(const LinearBufferBase& val) { | |
| 913 | ✗ | return this->serializeFrom(val); | |
| 914 | } | ||
| 915 | |||
| 916 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U8& val) { | |
| 917 | ✗ | return this->deserializeTo(val); | |
| 918 | } | ||
| 919 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(I8& val) { | |
| 920 | ✗ | return this->deserializeTo(val); | |
| 921 | } | ||
| 922 | #if FW_HAS_16_BIT == 1 | ||
| 923 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U16& val) { | |
| 924 | ✗ | return this->deserializeTo(val); | |
| 925 | } | ||
| 926 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(I16& val) { | |
| 927 | ✗ | return this->deserializeTo(val); | |
| 928 | } | ||
| 929 | #endif | ||
| 930 | #if FW_HAS_32_BIT == 1 | ||
| 931 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U32& val) { | |
| 932 | ✗ | return this->deserializeTo(val); | |
| 933 | } | ||
| 934 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(I32& val) { | |
| 935 | ✗ | return this->deserializeTo(val); | |
| 936 | } | ||
| 937 | #endif | ||
| 938 | #if FW_HAS_64_BIT == 1 | ||
| 939 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U64& val) { | |
| 940 | ✗ | return this->deserializeTo(val); | |
| 941 | } | ||
| 942 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(I64& val) { | |
| 943 | ✗ | return this->deserializeTo(val); | |
| 944 | } | ||
| 945 | #endif | ||
| 946 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(F32& val) { | |
| 947 | ✗ | return this->deserializeTo(val); | |
| 948 | } | ||
| 949 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(F64& val) { | |
| 950 | ✗ | return this->deserializeTo(val); | |
| 951 | } | ||
| 952 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(bool& val) { | |
| 953 | ✗ | return this->deserializeTo(val); | |
| 954 | } | ||
| 955 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(void*& val) { | |
| 956 | ✗ | return this->deserializeTo(val); | |
| 957 | } | ||
| 958 | |||
| 959 | // Deprecated method for backward compatibility | ||
| 960 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U8* buff, | |
| 961 | FwSizeType& length, | ||
| 962 | bool noLength) { | ||
| 963 | ✗ | const Serialization::t mode = noLength ? Serialization::OMIT_LENGTH : Serialization::INCLUDE_LENGTH; | |
| 964 | ✗ | return this->deserializeTo(buff, length, mode); | |
| 965 | } | ||
| 966 | |||
| 967 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U8* buff, FwSizeType& length) { | |
| 968 | ✗ | return this->deserializeTo(buff, length, Serialization::INCLUDE_LENGTH); | |
| 969 | } | ||
| 970 | |||
| 971 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(U8* buff, | |
| 972 | FwSizeType& length, | ||
| 973 | Serialization::t mode) { | ||
| 974 | ✗ | return this->deserializeTo(buff, length, mode); | |
| 975 | } | ||
| 976 | |||
| 977 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(Serializable& val) { | |
| 978 | ✗ | return this->deserializeTo(val); | |
| 979 | } | ||
| 980 | ✗ | FW_SERIALIZE_FORCE_INLINE_LBB SerializeStatus LinearBufferBase::deserialize(LinearBufferBase& val) { | |
| 981 | ✗ | return this->deserializeTo(val); | |
| 982 | } | ||
| 983 | |||
| 984 | ✗ | Serializable::SizeType ExternalSerializeBuffer::getBuffCapacity() const { | |
| 985 | ✗ | return this->getCapacity(); | |
| 986 | } | ||
| 987 | |||
| 988 | } // namespace Fw | ||
| 989 |