/src/PcapPlusPlus/Packet++/src/TLVData.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "TLVData.h" |
2 | | #include "GeneralUtils.h" |
3 | | #include "EndianPortable.h" |
4 | | |
5 | | namespace pcpp |
6 | | { |
7 | | |
8 | | TLVRecordBuilder::TLVRecordBuilder() |
9 | 0 | { |
10 | 0 | m_RecType = 0; |
11 | 0 | m_RecValueLen = 0; |
12 | 0 | m_RecValue = NULL; |
13 | 0 | } |
14 | | |
15 | | TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const uint8_t* recValue, uint8_t recValueLen) |
16 | 0 | { |
17 | 0 | init(recType, recValue, recValueLen); |
18 | 0 | } |
19 | | |
20 | | TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint8_t recValue) |
21 | 0 | { |
22 | 0 | init(recType, &recValue, sizeof(uint8_t)); |
23 | 0 | } |
24 | | |
25 | | TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint16_t recValue) |
26 | 0 | { |
27 | 0 | recValue = htobe16(recValue); |
28 | 0 | init(recType, (uint8_t*)&recValue, sizeof(uint16_t)); |
29 | 0 | } |
30 | | |
31 | | TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint32_t recValue) |
32 | 0 | { |
33 | 0 | recValue = htobe32(recValue); |
34 | 0 | init(recType, (uint8_t*)&recValue, sizeof(uint32_t)); |
35 | 0 | } |
36 | | |
37 | | TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const IPv4Address& recValue) |
38 | 0 | { |
39 | 0 | uint32_t recIntValue = recValue.toInt(); |
40 | 0 | init(recType, (uint8_t*)&recIntValue, sizeof(uint32_t)); |
41 | 0 | } |
42 | | |
43 | | TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const std::string& recValue, bool valueIsHexString) |
44 | 0 | { |
45 | 0 | m_RecType = 0; |
46 | 0 | m_RecValueLen = 0; |
47 | 0 | m_RecValue = NULL; |
48 | |
|
49 | 0 | if (valueIsHexString) |
50 | 0 | { |
51 | 0 | uint8_t recValueByteArr[512]; |
52 | 0 | size_t byteArraySize = hexStringToByteArray(recValue, recValueByteArr, 512); |
53 | 0 | if (byteArraySize > 0) |
54 | 0 | { |
55 | 0 | init(recType, recValueByteArr, byteArraySize); |
56 | 0 | } |
57 | 0 | } |
58 | 0 | else |
59 | 0 | { |
60 | 0 | uint8_t* recValueByteArr = (uint8_t*)recValue.c_str(); |
61 | 0 | init(recType, recValueByteArr, recValue.length()); |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | void TLVRecordBuilder::copyData(const TLVRecordBuilder& other) |
66 | 0 | { |
67 | 0 | m_RecType = other.m_RecType; |
68 | 0 | m_RecValueLen = other.m_RecValueLen; |
69 | 0 | m_RecValue = NULL; |
70 | 0 | if (other.m_RecValue != NULL) |
71 | 0 | { |
72 | 0 | m_RecValue = new uint8_t[m_RecValueLen]; |
73 | 0 | memcpy(m_RecValue, other.m_RecValue, m_RecValueLen); |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | TLVRecordBuilder::TLVRecordBuilder(const TLVRecordBuilder& other) |
78 | 0 | { |
79 | 0 | copyData(other); |
80 | 0 | } |
81 | | |
82 | | TLVRecordBuilder& TLVRecordBuilder::operator=(const TLVRecordBuilder& other) |
83 | 0 | { |
84 | 0 | if (m_RecValue != NULL) |
85 | 0 | { |
86 | 0 | delete [] m_RecValue; |
87 | 0 | m_RecValue = NULL; |
88 | 0 | } |
89 | |
|
90 | 0 | copyData(other); |
91 | |
|
92 | 0 | return *this; |
93 | 0 | } |
94 | | |
95 | | TLVRecordBuilder::~TLVRecordBuilder() |
96 | 0 | { |
97 | 0 | if (m_RecValue != NULL) delete [] m_RecValue; |
98 | 0 | } |
99 | | |
100 | | void TLVRecordBuilder::init(uint32_t recType, const uint8_t* recValue, size_t recValueLen) |
101 | 0 | { |
102 | 0 | m_RecType = recType; |
103 | 0 | m_RecValueLen = recValueLen; |
104 | 0 | m_RecValue = new uint8_t[recValueLen]; |
105 | 0 | if (recValue != NULL) |
106 | 0 | memcpy(m_RecValue, recValue, recValueLen); |
107 | 0 | else |
108 | 0 | memset(m_RecValue, 0, recValueLen); |
109 | 0 | } |
110 | | |
111 | | |
112 | | |
113 | | } |