Coverage Report

Created: 2026-07-16 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/src/Layer.cpp
Line
Count
Source
1
5.99k
#define LOG_MODULE PacketLogModuleLayer
2
3
#include "Layer.h"
4
#include "Logger.h"
5
#include "Packet.h"
6
#include <cstring>
7
#include <stdexcept>
8
9
namespace pcpp
10
{
11
12
  Layer::~Layer()
13
4.53M
  {
14
4.53M
    if (!isAllocatedToPacket())
15
140k
      delete[] m_Data;
16
4.53M
  }
17
18
140k
  Layer::Layer(const Layer& other) : m_Protocol(other.m_Protocol), m_NextLayer(nullptr), m_PrevLayer(nullptr)
19
140k
  {
20
140k
    m_DataLen = other.getHeaderLen();
21
140k
    m_Data = new uint8_t[other.m_DataLen];
22
140k
    memcpy(m_Data, other.m_Data, other.m_DataLen);
23
140k
  }
24
25
  Layer& Layer::operator=(const Layer& other)
26
10.4k
  {
27
10.4k
    if (this == &other)
28
0
      return *this;
29
30
    // Should this really always delete m_Data? What if the layer is attached to a packet?
31
10.4k
    if (m_Data != nullptr)
32
10.4k
      delete[] m_Data;
33
34
    // Reset allocation info as the layer is considered copied and not attached to any packet.
35
10.4k
    m_AllocationInfo = internal::LayerAllocationInfo{};
36
37
10.4k
    m_DataLen = other.getHeaderLen();
38
10.4k
    m_Protocol = other.m_Protocol;
39
10.4k
    m_NextLayer = nullptr;
40
10.4k
    m_PrevLayer = nullptr;
41
10.4k
    m_Data = new uint8_t[other.m_DataLen];
42
10.4k
    memcpy(m_Data, other.m_Data, other.m_DataLen);
43
44
10.4k
    return *this;
45
10.4k
  }
46
47
  bool Layer::isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const
48
14.9M
  {
49
14.9M
    return m_Protocol != UnknownProtocol && internal::protoFamilyContainsProtocol(protocolTypeFamily, m_Protocol);
50
14.9M
  }
51
52
  void Layer::copyData(uint8_t* toArr) const
53
0
  {
54
0
    memcpy(toArr, m_Data, m_DataLen);
55
0
  }
56
57
  size_t Layer::copyData(uint8_t* dest, size_t destSize) const
58
0
  {
59
0
    size_t bytesToCopy = (std::min)(destSize, m_DataLen);
60
0
    memcpy(dest, m_Data, bytesToCopy);
61
0
    return bytesToCopy;
62
0
  }
63
64
  void Layer::allocData(size_t dataLen, bool zeroInit)
65
0
  {
66
0
    if (m_Data != nullptr)
67
0
    {
68
0
      throw std::runtime_error(
69
0
          "Layer already has allocated data. Use extendLayer or shortenLayer to modify the data length.");
70
0
    }
71
72
0
    if (m_AllocationInfo.attachedPacket != nullptr)
73
0
    {
74
      // TODO: Allocate the layer data through the attached packet
75
0
      throw std::logic_error("not implemented");
76
0
    }
77
78
0
    m_Data = new uint8_t[dataLen];
79
0
    m_DataLen = dataLen;
80
0
    if (zeroInit)
81
0
    {
82
0
      std::fill_n(m_Data, m_DataLen, 0);
83
0
    }
84
0
  }
85
86
  bool Layer::extendLayer(int offsetInLayer, size_t numOfBytesToExtend)
87
163k
  {
88
163k
    if (m_Data == nullptr)
89
0
    {
90
0
      PCPP_LOG_ERROR("Layer's data is nullptr");
91
0
      return false;
92
0
    }
93
94
163k
    if (getAttachedPacket() == nullptr)
95
105k
    {
96
105k
      if (static_cast<size_t>(offsetInLayer) > m_DataLen)
97
2.99k
      {
98
2.99k
        PCPP_LOG_ERROR("Requested offset is larger than data length");
99
2.99k
        return false;
100
2.99k
      }
101
102k
      uint8_t* newData = new uint8_t[m_DataLen + numOfBytesToExtend];
102
102k
      memcpy(newData, m_Data, offsetInLayer);
103
102k
      memcpy(newData + offsetInLayer + numOfBytesToExtend, m_Data + offsetInLayer, m_DataLen - offsetInLayer);
104
102k
      delete[] m_Data;
105
102k
      m_Data = newData;
106
102k
      m_DataLen += numOfBytesToExtend;
107
102k
      return true;
108
105k
    }
109
110
57.6k
    return getAttachedPacket()->extendLayer(this, offsetInLayer, numOfBytesToExtend);
111
163k
  }
112
113
  bool Layer::shortenLayer(int offsetInLayer, size_t numOfBytesToShorten)
114
62.9k
  {
115
62.9k
    if (m_Data == nullptr)
116
0
    {
117
0
      PCPP_LOG_ERROR("Layer's data is nullptr");
118
0
      return false;
119
0
    }
120
121
62.9k
    if (static_cast<size_t>(offsetInLayer) + numOfBytesToShorten > m_DataLen)
122
5
    {
123
5
      PCPP_LOG_ERROR("Requested number of bytes to shorten is larger than data length");
124
5
      return false;
125
5
    }
126
127
62.9k
    if (getAttachedPacket() == nullptr)
128
1.59k
    {
129
1.59k
      if (static_cast<size_t>(offsetInLayer) >= m_DataLen)
130
0
      {
131
0
        PCPP_LOG_ERROR("Requested offset is larger than data length");
132
0
        return false;
133
0
      }
134
1.59k
      uint8_t* newData = new uint8_t[m_DataLen - numOfBytesToShorten];
135
1.59k
      memcpy(newData, m_Data, offsetInLayer);
136
1.59k
      memcpy(newData + offsetInLayer, m_Data + offsetInLayer + numOfBytesToShorten,
137
1.59k
             m_DataLen - offsetInLayer - numOfBytesToShorten);
138
1.59k
      delete[] m_Data;
139
1.59k
      m_Data = newData;
140
1.59k
      m_DataLen -= numOfBytesToShorten;
141
1.59k
      return true;
142
1.59k
    }
143
144
61.3k
    return getAttachedPacket()->shortenLayer(this, offsetInLayer, numOfBytesToShorten);
145
62.9k
  }
146
147
}  // namespace pcpp