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