Skip to content

Commit

Permalink
C++ casting and some fixes (seladb#1610)
Browse files Browse the repository at this point in the history
  • Loading branch information
lumiZGorlic authored and fxlb committed Oct 22, 2024
1 parent 1fea957 commit 9789273
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 60 deletions.
9 changes: 5 additions & 4 deletions Common++/src/GeneralUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace pcpp
dataStream << std::hex;
for (size_t i = 0; i < byteArrSize; ++i)
{
if (i >= (size_t)stringSizeLimit)
if (i >= static_cast<size_t>(stringSizeLimit))
break;

dataStream << std::setw(2) << std::setfill('0') << (int)byteArr[i];
dataStream << std::setw(2) << std::setfill('0') << static_cast<int>(byteArr[i]);
}

return dataStream.str();
Expand Down Expand Up @@ -69,10 +69,11 @@ namespace pcpp

char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen)
{
char* ptr = (char*)haystack;
char* ptr = const_cast<char*>(haystack);
while (needleLen <= (haystackLen - (ptr - haystack)))
{
if (nullptr != (ptr = (char*)memchr(ptr, (int)(*needle), haystackLen - (ptr - haystack))))
if (nullptr !=
(ptr = static_cast<char*>(memchr(ptr, static_cast<int>(*needle), haystackLen - (ptr - haystack)))))
{
// check if there is room to do a memcmp
if (needleLen > (haystackLen - (ptr - haystack)))
Expand Down
2 changes: 1 addition & 1 deletion Packet++/header/PacketTrailerLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace pcpp
{
/**
* @class PacketTrailerLayer
* A class for representing packet tailer (a.k.a footer or padding) which refers to supplemental data placed at the
* A class for representing packet trailer (a.k.a footer or padding) which refers to supplemental data placed at the
* end of a block of data being stored or transmitted, which may contain information for the handling of the data
* block, or just mark its end (taken from Wikipedia: https://en.wikipedia.org/wiki/Trailer_(computing) )
*
Expand Down
2 changes: 1 addition & 1 deletion Packet++/src/CotpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace pcpp
m_DataLen = headerLen;
m_Data = new uint8_t[headerLen];
memset(m_Data, 0, headerLen);
cotphdr* cotpHdr = (cotphdr*)m_Data;
cotphdr* cotpHdr = getCotpHeader();
cotpHdr->length = 0x02;
cotpHdr->pduType = 0x0f;
cotpHdr->tpduNumber = tpduNumber;
Expand Down
4 changes: 2 additions & 2 deletions Packet++/src/EthDot3Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace pcpp
m_Data = new uint8_t[headerLen];
memset(m_Data, 0, headerLen);

ether_dot3_header* ethHdr = (ether_dot3_header*)m_Data;
ether_dot3_header* ethHdr = getEthHeader();
destMac.copyTo(ethHdr->dstMac);
sourceMac.copyTo(ethHdr->srcMac);
ethHdr->length = be16toh(length);
Expand Down Expand Up @@ -51,7 +51,7 @@ namespace pcpp
* From: https://tools.ietf.org/html/rfc5342#section-2.3.2.1
* More: IEEE Std 802.3 Clause 3.2.6
*/
return be16toh(*(uint16_t*)(data + 12)) <= (uint16_t)0x05DC;
return be16toh(*reinterpret_cast<const uint16_t*>(data + 12)) <= static_cast<uint16_t>(0x05DC);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions Packet++/src/EthLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace pcpp
m_Data = new uint8_t[headerLen];
memset(m_Data, 0, headerLen);

ether_header* ethHdr = (ether_header*)m_Data;
ether_header* ethHdr = getEthHeader();
destMac.copyTo(ethHdr->dstMac);
sourceMac.copyTo(ethHdr->srcMac);
ethHdr->etherType = htobe16(etherType);
Expand Down Expand Up @@ -121,7 +121,7 @@ namespace pcpp
* From: https://tools.ietf.org/html/rfc5342#section-2.3.2.1
* More: IEEE Std 802.3 Clause 3.2.6
*/
return be16toh(*(uint16_t*)(data + 12)) >= (uint16_t)0x0600;
return be16toh(*reinterpret_cast<const uint16_t*>(data + 12)) >= static_cast<uint16_t>(0x0600);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions Packet++/src/NullLoopbackLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace pcpp

uint32_t NullLoopbackLayer::getFamily() const
{
uint32_t family = *(uint32_t*)m_Data;
uint32_t family = *(reinterpret_cast<uint32_t*>(m_Data));
if ((family & 0xFFFF0000) != 0)
{
if ((family & 0xFF000000) == 0 && (family & 0x00FF0000) < 0x00060000)
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace pcpp
uint32_t family = getFamily();
if (family > IEEE_802_3_MAX_LEN)
{
uint16_t ethType = (uint16_t)family;
uint16_t ethType = static_cast<uint16_t>(family);
switch (ethType)
{
case PCPP_ETHERTYPE_IP:
Expand Down
6 changes: 3 additions & 3 deletions Packet++/src/PPPoELayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace pcpp

void PPPoELayer::computeCalculateFields()
{
pppoe_header* pppoeHdr = (pppoe_header*)m_Data;
pppoe_header* pppoeHdr = getPPPoEHeader();
pppoeHdr->payloadLength = htobe16(m_DataLen - sizeof(pppoe_header));
}

Expand Down Expand Up @@ -75,7 +75,7 @@ namespace pcpp
return 0;
}

uint16_t pppNextProto = *(uint16_t*)(m_Data + sizeof(pppoe_header));
uint16_t pppNextProto = *reinterpret_cast<uint16_t*>(m_Data + sizeof(pppoe_header));
return be16toh(pppNextProto);
}

Expand All @@ -87,7 +87,7 @@ namespace pcpp
return;
}

uint16_t* pppProto = (uint16_t*)(m_Data + sizeof(pppoe_header));
uint16_t* pppProto = reinterpret_cast<uint16_t*>(m_Data + sizeof(pppoe_header));
*pppProto = htobe16(nextProtocol);
}

Expand Down
48 changes: 26 additions & 22 deletions Packet++/src/SipLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace pcpp
if (m_DataLen > headerLen)
{
int currentContentLength = getContentLength();
if (currentContentLength != (int)(m_DataLen - headerLen))
if (currentContentLength != static_cast<int>(m_DataLen - headerLen))
setContentLength(m_DataLen - headerLen);
}
}
Expand All @@ -107,7 +107,7 @@ namespace pcpp

SipRequestFirstLine::SipRequestFirstLine(SipRequestLayer* sipRequest) : m_SipRequest(sipRequest)
{
m_Method = parseMethod((char*)m_SipRequest->m_Data, m_SipRequest->getDataLen());
m_Method = parseMethod(reinterpret_cast<char*>(m_SipRequest->m_Data), m_SipRequest->getDataLen());
if (m_Method == SipRequestLayer::SipMethodUnknown)
{
m_UriOffset = -1;
Expand All @@ -119,10 +119,11 @@ namespace pcpp
parseVersion();

char* endOfFirstLine;
if ((endOfFirstLine = (char*)memchr((char*)(m_SipRequest->m_Data + m_VersionOffset), '\n',
m_SipRequest->m_DataLen - (size_t)m_VersionOffset)) != nullptr)
if ((endOfFirstLine =
static_cast<char*>(memchr(reinterpret_cast<char*>(m_SipRequest->m_Data + m_VersionOffset), '\n',
m_SipRequest->m_DataLen - static_cast<size_t>(m_VersionOffset)))) != nullptr)
{
m_FirstLineEndOffset = endOfFirstLine - (char*)m_SipRequest->m_Data + 1;
m_FirstLineEndOffset = endOfFirstLine - reinterpret_cast<char*>(m_SipRequest->m_Data) + 1;
m_IsComplete = true;
}
else
Expand Down Expand Up @@ -216,8 +217,8 @@ namespace pcpp
return;
}

char* data = (char*)(m_SipRequest->m_Data + m_UriOffset);
char* verPos = (char*)cross_platform_memmem(data, m_SipRequest->getDataLen() - m_UriOffset, " SIP/", 5);
char* data = reinterpret_cast<char*>(m_SipRequest->m_Data + m_UriOffset);
char* verPos = cross_platform_memmem(data, m_SipRequest->getDataLen() - m_UriOffset, " SIP/", 5);
if (verPos == nullptr)
{
m_Version = "";
Expand All @@ -226,7 +227,8 @@ namespace pcpp
}

// verify packet doesn't end before the version, meaning still left place for " SIP/x.y" (7 chars)
if ((uint16_t)(verPos + 7 - (char*)m_SipRequest->m_Data) > m_SipRequest->getDataLen())
if (static_cast<uint16_t>(verPos + 7 - reinterpret_cast<char*>(m_SipRequest->m_Data)) >
m_SipRequest->getDataLen())
{
m_Version = "";
m_VersionOffset = -1;
Expand All @@ -237,13 +239,13 @@ namespace pcpp
verPos++;

int endOfVerPos = 0;
while (((verPos + endOfVerPos) < (char*)(m_SipRequest->m_Data + m_SipRequest->m_DataLen)) &&
while (((verPos + endOfVerPos) < reinterpret_cast<char*>(m_SipRequest->m_Data + m_SipRequest->m_DataLen)) &&
((verPos + endOfVerPos)[0] != '\r') && ((verPos + endOfVerPos)[0] != '\n'))
endOfVerPos++;

m_Version = std::string(verPos, endOfVerPos);

m_VersionOffset = verPos - (char*)m_SipRequest->m_Data;
m_VersionOffset = verPos - reinterpret_cast<char*>(m_SipRequest->m_Data);
}

bool SipRequestFirstLine::setMethod(SipRequestLayer::SipMethod newMethod)
Expand Down Expand Up @@ -295,7 +297,8 @@ namespace pcpp
{
std::string result;
if (m_UriOffset != -1 && m_VersionOffset != -1)
result.assign((char*)(m_SipRequest->m_Data + m_UriOffset), m_VersionOffset - 1 - m_UriOffset);
result.assign(reinterpret_cast<char*>(m_SipRequest->m_Data + m_UriOffset),
m_VersionOffset - 1 - m_UriOffset);

// else first line is illegal, return empty string

Expand Down Expand Up @@ -396,15 +399,15 @@ namespace pcpp
if (size <= maxLengthToPrint)
{
char* firstLine = new char[size + 1];
strncpy(firstLine, (char*)m_Data, size);
strncpy(firstLine, reinterpret_cast<char*>(m_Data), size);
firstLine[size] = 0;
result += std::string(firstLine);
delete[] firstLine;
}
else
{
char firstLine[maxLengthToPrint + 1];
strncpy(firstLine, (char*)m_Data, maxLengthToPrint - 3);
strncpy(firstLine, reinterpret_cast<char*>(m_Data), maxLengthToPrint - 3);
firstLine[maxLengthToPrint - 3] = '.';
firstLine[maxLengthToPrint - 2] = '.';
firstLine[maxLengthToPrint - 1] = '.';
Expand Down Expand Up @@ -633,15 +636,15 @@ namespace pcpp
if (size <= maxLengthToPrint)
{
char* firstLine = new char[size + 1];
strncpy(firstLine, (char*)m_Data, size);
strncpy(firstLine, reinterpret_cast<char*>(m_Data), size);
firstLine[size] = 0;
result += std::string(firstLine);
delete[] firstLine;
}
else
{
char firstLine[maxLengthToPrint + 1];
strncpy(firstLine, (char*)m_Data, maxLengthToPrint - 3);
strncpy(firstLine, reinterpret_cast<char*>(m_Data), maxLengthToPrint - 3);
firstLine[maxLengthToPrint - 3] = '.';
firstLine[maxLengthToPrint - 2] = '.';
firstLine[maxLengthToPrint - 1] = '.';
Expand All @@ -668,7 +671,7 @@ namespace pcpp
int statusStringEndOffset = m_FirstLineEndOffset - 2;
if ((*(m_SipResponse->m_Data + statusStringEndOffset)) != '\r')
statusStringEndOffset++;
result.assign((char*)(m_SipResponse->m_Data + statusStringOffset),
result.assign(reinterpret_cast<char*>(m_SipResponse->m_Data + statusStringOffset),
statusStringEndOffset - statusStringOffset);
}

Expand Down Expand Up @@ -742,7 +745,7 @@ namespace pcpp
return;
}

char* verPos = (char*)m_SipResponse->m_Data;
char* verPos = reinterpret_cast<char*>(m_SipResponse->m_Data);
memcpy(verPos, newVersion.c_str(), newVersion.length());
m_Version = newVersion;
}
Expand Down Expand Up @@ -771,20 +774,21 @@ namespace pcpp

SipResponseFirstLine::SipResponseFirstLine(SipResponseLayer* sipResponse) : m_SipResponse(sipResponse)
{
m_Version = parseVersion((char*)m_SipResponse->m_Data, m_SipResponse->getDataLen());
m_Version = parseVersion(reinterpret_cast<char*>(m_SipResponse->m_Data), m_SipResponse->getDataLen());
if (m_Version == "")
{
m_StatusCode = SipResponseLayer::SipStatusCodeUnknown;
}
else
{
m_StatusCode = parseStatusCode((char*)m_SipResponse->m_Data, m_SipResponse->getDataLen());
m_StatusCode = parseStatusCode(reinterpret_cast<char*>(m_SipResponse->m_Data), m_SipResponse->getDataLen());
}

char* endOfFirstLine;
if ((endOfFirstLine = (char*)memchr((char*)(m_SipResponse->m_Data), '\n', m_SipResponse->m_DataLen)) != nullptr)
if ((endOfFirstLine = static_cast<char*>(
memchr(reinterpret_cast<char*>(m_SipResponse->m_Data), '\n', m_SipResponse->m_DataLen))) != nullptr)
{
m_FirstLineEndOffset = endOfFirstLine - (char*)m_SipResponse->m_Data + 1;
m_FirstLineEndOffset = endOfFirstLine - reinterpret_cast<char*>(m_SipResponse->m_Data) + 1;
m_IsComplete = true;
}
else
Expand Down Expand Up @@ -852,7 +856,7 @@ namespace pcpp
return "";
}

char* nextSpace = (char*)memchr(data, ' ', dataLen);
const char* nextSpace = static_cast<const char*>(memchr(data, ' ', dataLen));
if (nextSpace == nullptr)
return "";

Expand Down
8 changes: 4 additions & 4 deletions Packet++/src/TLVData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ namespace pcpp
TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint16_t recValue)
{
recValue = htobe16(recValue);
init(recType, (uint8_t*)&recValue, sizeof(uint16_t));
init(recType, reinterpret_cast<uint8_t*>(&recValue), sizeof(uint16_t));
}

TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint32_t recValue)
{
recValue = htobe32(recValue);
init(recType, (uint8_t*)&recValue, sizeof(uint32_t));
init(recType, reinterpret_cast<uint8_t*>(&recValue), sizeof(uint32_t));
}

TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const IPv4Address& recValue)
{
uint32_t recIntValue = recValue.toInt();
init(recType, (uint8_t*)&recIntValue, sizeof(uint32_t));
init(recType, reinterpret_cast<uint8_t*>(&recIntValue), sizeof(uint32_t));
}

TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const std::string& recValue, bool valueIsHexString)
Expand All @@ -57,7 +57,7 @@ namespace pcpp
}
else
{
uint8_t* recValueByteArr = (uint8_t*)recValue.c_str();
const uint8_t* recValueByteArr = reinterpret_cast<const uint8_t*>(recValue.c_str());
init(recType, recValueByteArr, recValue.length());
}
}
Expand Down
Loading

0 comments on commit 9789273

Please sign in to comment.