LCOV - code coverage report
Current view: top level - src/net - Message.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 12 13 92.3 %
Date: 2025-04-27 01:14:20 Functions: 2 2 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** Copyright (C) 2023 Sylvain Fargier
       3                 :            : **
       4                 :            : ** This software is provided 'as-is', without any express or implied
       5                 :            : ** warranty.  In no event will the authors be held liable for any damages
       6                 :            : ** arising from the use of this software.
       7                 :            : **
       8                 :            : ** Permission is granted to anyone to use this software for any purpose,
       9                 :            : ** including commercial applications, and to alter it and redistribute it
      10                 :            : ** freely, subject to the following restrictions:
      11                 :            : **
      12                 :            : ** 1. The origin of this software must not be misrepresented; you must not
      13                 :            : **    claim that you wrote the original software. If you use this software
      14                 :            : **    in a product, an acknowledgment in the product documentation would be
      15                 :            : **    appreciated but is not required.
      16                 :            : ** 2. Altered source versions must be plainly marked as such, and must not be
      17                 :            : **    misrepresented as being the original software.
      18                 :            : ** 3. This notice may not be removed or altered from any source distribution.
      19                 :            : **
      20                 :            : ** Created on: 2023-12-14T15:43:37
      21                 :            : ** Author: Sylvain Fargier <fargier.sylvain@gmail.com>
      22                 :            : */
      23                 :            : 
      24                 :            : #include "Message.hpp"
      25                 :            : 
      26                 :            : #include <cinttypes>
      27                 :            : #include <cstdint>
      28                 :            : #include <cstring>
      29                 :            : 
      30                 :            : #include <endian.h>
      31                 :            : 
      32                 :            : #include "../Exception.hpp"
      33                 :            : 
      34                 :            : namespace ccut {
      35                 :            : namespace net {
      36                 :            : 
      37                 :            : static const std::string s_logCat{"ccut:net"};
      38                 :            : 
      39                 :         46 : bool Message::isValid() const
      40                 :            : {
      41                 :         46 :     return from.isValid() || to.isValid() || data;
      42                 :            : }
      43                 :            : 
      44                 :         75 : uint16_t cksum(const void *data, size_t len, uint16_t initial)
      45                 :            : {
      46                 :         75 :     const uint16_t *addr = static_cast<const uint16_t *>(data);
      47                 :         75 :     uint32_t sum = ~initial & 0xFFFF;
      48                 :            : 
      49                 :        547 :     while (len > 1)
      50                 :            :     {
      51                 :        472 :         sum += *addr++;
      52                 :        472 :         len -= 2;
      53                 :            :     }
      54                 :            : 
      55                 :         75 :     if (len > 0)
      56                 :            : #if BYTE_ORDER == LITTLE_ENDIAN
      57                 :          0 :         sum += *reinterpret_cast<const uint8_t *>(addr);
      58                 :            : #else
      59                 :            :         sum += *reinterpret_cast<const uint8_t *>(addr << 8);
      60                 :            : #endif
      61                 :            : 
      62                 :        100 :     while (sum >> 16)
      63                 :         25 :         sum = (sum & 0xffff) + (sum >> 16);
      64                 :            : 
      65                 :         75 :     return ~sum;
      66                 :            : }
      67                 :            : 
      68                 :            : } // namespace net
      69                 :            : } // namespace ccut

Generated by: LCOV version 1.14