Coverage Report

Created: 2025-11-11 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/src/Layer.cpp
Line
Count
Source
1
1.86k
#define LOG_MODULE PacketLogModuleLayer
2
3
#include "Layer.h"
4
#include "Logger.h"
5
#include "Packet.h"
6
#include <cstring>
7
8
namespace pcpp
9
{
10
11
  Layer::~Layer()
12
4.24M
  {
13
4.24M
    if (!isAllocatedToPacket())
14
110k
      delete[] m_Data;
15
4.24M
  }
16
17
  Layer::Layer(const Layer& other)
18
110k
      : m_Packet(nullptr), m_Protocol(other.m_Protocol), m_NextLayer(nullptr), m_PrevLayer(nullptr),
19
110k
        m_IsAllocatedInPacket(false)
20
110k
  {
21
110k
    m_DataLen = other.getHeaderLen();
22
110k
    m_Data = new uint8_t[other.m_DataLen];
23
110k
    memcpy(m_Data, other.m_Data, other.m_DataLen);
24
110k
  }
25
26
  Layer& Layer::operator=(const Layer& other)
27
15.3k
  {
28
15.3k
    if (this == &other)
29
0
      return *this;
30
31
15.3k
    if (m_Data != nullptr)
32
15.3k
      delete[] m_Data;
33
34
15.3k
    m_DataLen = other.getHeaderLen();
35
15.3k
    m_Packet = nullptr;
36
15.3k
    m_Protocol = other.m_Protocol;
37
15.3k
    m_NextLayer = nullptr;
38
15.3k
    m_PrevLayer = nullptr;
39
15.3k
    m_Data = new uint8_t[other.m_DataLen];
40
15.3k
    m_IsAllocatedInPacket = false;
41
15.3k
    memcpy(m_Data, other.m_Data, other.m_DataLen);
42
43
15.3k
    return *this;
44
15.3k
  }
45
46
  bool Layer::isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const
47
14.0M
  {
48
14.0M
    return m_Protocol != UnknownProtocol && internal::protoFamilyContainsProtocol(protocolTypeFamily, m_Protocol);
49
14.0M
  }
50
51
  void Layer::copyData(uint8_t* toArr) const
52
0
  {
53
0
    memcpy(toArr, m_Data, m_DataLen);
54
0
  }
55
56
  bool Layer::extendLayer(int offsetInLayer, size_t numOfBytesToExtend)
57
212k
  {
58
212k
    if (m_Data == nullptr)
59
0
    {
60
0
      PCPP_LOG_ERROR("Layer's data is nullptr");
61
0
      return false;
62
0
    }
63
64
212k
    if (m_Packet == nullptr)
65
110k
    {
66
110k
      if (static_cast<size_t>(offsetInLayer) > m_DataLen)
67
934
      {
68
934
        PCPP_LOG_ERROR("Requested offset is larger than data length");
69
934
        return false;
70
934
      }
71
109k
      uint8_t* newData = new uint8_t[m_DataLen + numOfBytesToExtend];
72
109k
      memcpy(newData, m_Data, offsetInLayer);
73
109k
      memcpy(newData + offsetInLayer + numOfBytesToExtend, m_Data + offsetInLayer, m_DataLen - offsetInLayer);
74
109k
      delete[] m_Data;
75
109k
      m_Data = newData;
76
109k
      m_DataLen += numOfBytesToExtend;
77
109k
      return true;
78
110k
    }
79
80
102k
    return m_Packet->extendLayer(this, offsetInLayer, numOfBytesToExtend);
81
212k
  }
82
83
  bool Layer::shortenLayer(int offsetInLayer, size_t numOfBytesToShorten)
84
102k
  {
85
102k
    if (m_Data == nullptr)
86
0
    {
87
0
      PCPP_LOG_ERROR("Layer's data is nullptr");
88
0
      return false;
89
0
    }
90
91
102k
    if (static_cast<size_t>(offsetInLayer) + numOfBytesToShorten > m_DataLen)
92
0
    {
93
0
      PCPP_LOG_ERROR("Requested number of bytes to shorten is larger than data length");
94
0
      return false;
95
0
    }
96
97
102k
    if (m_Packet == nullptr)
98
6.35k
    {
99
6.35k
      if (static_cast<size_t>(offsetInLayer) >= m_DataLen)
100
0
      {
101
0
        PCPP_LOG_ERROR("Requested offset is larger than data length");
102
0
        return false;
103
0
      }
104
6.35k
      uint8_t* newData = new uint8_t[m_DataLen - numOfBytesToShorten];
105
6.35k
      memcpy(newData, m_Data, offsetInLayer);
106
6.35k
      memcpy(newData + offsetInLayer, m_Data + offsetInLayer + numOfBytesToShorten,
107
6.35k
             m_DataLen - offsetInLayer - numOfBytesToShorten);
108
6.35k
      delete[] m_Data;
109
6.35k
      m_Data = newData;
110
6.35k
      m_DataLen -= numOfBytesToShorten;
111
6.35k
      return true;
112
6.35k
    }
113
114
95.8k
    return m_Packet->shortenLayer(this, offsetInLayer, numOfBytesToShorten);
115
102k
  }
116
117
}  // namespace pcpp