Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Packet++/header/PayloadLayer.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "Layer.h"
4
5
/// @file
6
7
/// @namespace pcpp
8
/// @brief The main namespace for the PcapPlusPlus lib
9
namespace pcpp
10
{
11
  /// @class PayloadLayer
12
  /// Represents a generic or unknown layer or a packet payload
13
  class PayloadLayer : public Layer
14
  {
15
  public:
16
    /// A constructor that creates the layer from an existing packet raw data
17
    /// @param[in] data A pointer to the raw data
18
    /// @param[in] dataLen Size of the data in bytes
19
    /// @param[in] prevLayer A pointer to the previous layer
20
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
21
    PayloadLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
22
109k
        : Layer(data, dataLen, prevLayer, packet, GenericPayload)
23
109k
    {}
24
25
    /// A constructor that allocates a new payload
26
    /// @param[in] data A raw buffer that will be used as a payload. This data will be copied to the layer
27
    /// @param[in] dataLen The raw buffer length
28
    PayloadLayer(const uint8_t* data, size_t dataLen);
29
30
    /// A constructor that allocates a new payload from an hex stream
31
    /// @param[in] payloadAsHexStream A string that represents an hex stream of the payload. For example:
32
    /// 0001080006040002842b2b774c56c0a80078000000000000c0a8. In order for the hex stream to be valid it has to
33
    /// contain valid hex chars only (which means, for example, that it can't begin with "0x") and it also has to
34
    /// have an even number of chars (each char represents one nibble). If the string is not a valid hex stream an
35
    /// error will be printed to log and the payload layer will be empty (no data)
36
    explicit PayloadLayer(const std::string& payloadAsHexStream);
37
38
    ~PayloadLayer() override = default;
39
40
    /// Get a pointer to the payload data
41
    /// @return A pointer to the payload data
42
    uint8_t* getPayload() const
43
0
    {
44
0
      return m_Data;
45
0
    }
46
47
    /// Get the payload data length
48
    /// @return The payload data length in bytes
49
    size_t getPayloadLen() const
50
0
    {
51
0
      return m_DataLen;
52
0
    }
53
54
    // implement abstract methods
55
56
    /// Does nothing for this layer (PayloadLayer is always last)
57
    void parseNextLayer() override
58
109k
    {}
59
60
    /// @return Payload data length in bytes
61
    size_t getHeaderLen() const override
62
22.9k
    {
63
22.9k
      return m_DataLen;
64
22.9k
    }
65
66
    /// Does nothing for this layer
67
    void computeCalculateFields() override
68
20.6k
    {}
69
70
    /// Sets the payload of the PayloadLayer to the given pointer. This will resize (extend/shorten) the underlying
71
    /// packet respectively if there is one.
72
    /// @param[in] newPayload New payload that shall be set
73
    /// @param[in] newPayloadLength New length of payload
74
    void setPayload(const uint8_t* newPayload, size_t newPayloadLength);
75
76
    std::string toString() const override;
77
78
    OsiModelLayer getOsiModelLayer() const override
79
20.6k
    {
80
20.6k
      return OsiModelApplicationLayer;
81
20.6k
    }
82
83
    /// A static method that takes a byte array and detects if it can be parsed as a PayloadLayer.
84
    /// @param[in] data A byte array
85
    /// @param[in] dataLen The byte array size (in bytes)
86
    /// @return True if the data is non-nullptr and dataLen is greater than 0, or data is nullptr and dataLen is 0.
87
    static bool isDataValid(const uint8_t* data, size_t dataLen)
88
0
    {
89
0
      // PayloadLayer is special as it can be empty. So, it's valid if data is nullptr and dataLen is 0.
90
0
      return (data == nullptr) != (dataLen != 0);  // XOR
91
0
    };
92
  };
93
}  // namespace pcpp