/src/PcapPlusPlus/Packet++/src/Layer.cpp
Line | Count | Source |
1 | 10.5k | #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 | 9.05M | { |
14 | 9.05M | if (!isAllocatedToPacket()) |
15 | 246k | delete[] m_Data; |
16 | 9.05M | } |
17 | | |
18 | 246k | Layer::Layer(const Layer& other) : m_Protocol(other.m_Protocol), m_NextLayer(nullptr), m_PrevLayer(nullptr) |
19 | 246k | { |
20 | 246k | m_DataLen = other.getHeaderLen(); |
21 | 246k | m_Data = new uint8_t[other.m_DataLen]; |
22 | 246k | memcpy(m_Data, other.m_Data, other.m_DataLen); |
23 | 246k | } |
24 | | |
25 | | Layer& Layer::operator=(const Layer& other) |
26 | 22.6k | { |
27 | 22.6k | 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 | 22.6k | if (m_Data != nullptr) |
32 | 22.6k | delete[] m_Data; |
33 | | |
34 | | // Reset allocation info as the layer is considered copied and not attached to any packet. |
35 | 22.6k | m_AllocationInfo = internal::LayerAllocationInfo{}; |
36 | | |
37 | 22.6k | m_DataLen = other.getHeaderLen(); |
38 | 22.6k | m_Protocol = other.m_Protocol; |
39 | 22.6k | m_NextLayer = nullptr; |
40 | 22.6k | m_PrevLayer = nullptr; |
41 | 22.6k | m_Data = new uint8_t[other.m_DataLen]; |
42 | 22.6k | memcpy(m_Data, other.m_Data, other.m_DataLen); |
43 | | |
44 | 22.6k | return *this; |
45 | 22.6k | } |
46 | | |
47 | | bool Layer::isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const |
48 | 29.9M | { |
49 | 29.9M | return m_Protocol != UnknownProtocol && internal::protoFamilyContainsProtocol(protocolTypeFamily, m_Protocol); |
50 | 29.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 | 357k | { |
88 | 357k | if (m_Data == nullptr) |
89 | 0 | { |
90 | 0 | PCPP_LOG_ERROR("Layer's data is nullptr"); |
91 | 0 | return false; |
92 | 0 | } |
93 | | |
94 | 357k | if (getAttachedPacket() == nullptr) |
95 | 214k | { |
96 | 214k | if (static_cast<size_t>(offsetInLayer) > m_DataLen) |
97 | 5.29k | { |
98 | 5.29k | PCPP_LOG_ERROR("Requested offset is larger than data length"); |
99 | 5.29k | return false; |
100 | 5.29k | } |
101 | 208k | uint8_t* newData = new uint8_t[m_DataLen + numOfBytesToExtend]; |
102 | 208k | memcpy(newData, m_Data, offsetInLayer); |
103 | 208k | memcpy(newData + offsetInLayer + numOfBytesToExtend, m_Data + offsetInLayer, m_DataLen - offsetInLayer); |
104 | 208k | delete[] m_Data; |
105 | 208k | m_Data = newData; |
106 | 208k | m_DataLen += numOfBytesToExtend; |
107 | 208k | return true; |
108 | 214k | } |
109 | | |
110 | 143k | return getAttachedPacket()->extendLayer(this, offsetInLayer, numOfBytesToExtend); |
111 | 357k | } |
112 | | |
113 | | bool Layer::shortenLayer(int offsetInLayer, size_t numOfBytesToShorten) |
114 | 149k | { |
115 | 149k | if (m_Data == nullptr) |
116 | 0 | { |
117 | 0 | PCPP_LOG_ERROR("Layer's data is nullptr"); |
118 | 0 | return false; |
119 | 0 | } |
120 | | |
121 | 149k | if (static_cast<size_t>(offsetInLayer) + numOfBytesToShorten > m_DataLen) |
122 | 4 | { |
123 | 4 | PCPP_LOG_ERROR("Requested number of bytes to shorten is larger than data length"); |
124 | 4 | return false; |
125 | 4 | } |
126 | | |
127 | 149k | if (getAttachedPacket() == nullptr) |
128 | 10.8k | { |
129 | 10.8k | 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 | 10.8k | uint8_t* newData = new uint8_t[m_DataLen - numOfBytesToShorten]; |
135 | 10.8k | memcpy(newData, m_Data, offsetInLayer); |
136 | 10.8k | memcpy(newData + offsetInLayer, m_Data + offsetInLayer + numOfBytesToShorten, |
137 | 10.8k | m_DataLen - offsetInLayer - numOfBytesToShorten); |
138 | 10.8k | delete[] m_Data; |
139 | 10.8k | m_Data = newData; |
140 | 10.8k | m_DataLen -= numOfBytesToShorten; |
141 | 10.8k | return true; |
142 | 10.8k | } |
143 | | |
144 | 138k | return getAttachedPacket()->shortenLayer(this, offsetInLayer, numOfBytesToShorten); |
145 | 149k | } |
146 | | |
147 | | } // namespace pcpp |