Coverage Report

Created: 2026-12-01 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Common++/header/Serializers.h
Line
Count
Source
1
#pragma once
2
3
#include <cstdint>
4
#include <iosfwd>
5
#include <string>
6
#include <type_traits>
7
#include <vector>
8
9
/// @file
10
11
/// @namespace pcpp
12
/// @brief The main namespace for the PcapPlusPlus lib
13
namespace pcpp
14
{
15
  /// @struct FieldDescriptor
16
  /// Describes a serializable field.
17
  struct FieldDescriptor
18
  {
19
    /// Stable numeric identifier of the field.
20
    const uint16_t id;
21
22
    /// Name of the field.
23
    const std::string name;
24
25
    /// Function returning the descriptors of the field's children, or nullptr if the field has no children.
26
    std::vector<FieldDescriptor> (*const getChildren)();
27
28
    /// Create a field descriptor.
29
    /// @param[in] fieldId The stable numeric identifier of the field
30
    /// @param[in] fieldName The name of the field
31
    /// @param[in] fieldChildren A function returning the field's child descriptors, or nullptr if the field has no
32
    /// children
33
    FieldDescriptor(uint16_t fieldId, std::string fieldName,
34
                    std::vector<FieldDescriptor> (*fieldChildren)() = nullptr)
35
3.37k
        : id(fieldId), name(std::move(fieldName)), getChildren(fieldChildren)
36
3.37k
    {}
37
38
    /// Check whether the field has child fields.
39
    /// @return True if the field has child fields, false otherwise
40
    bool hasChildren() const
41
0
    {
42
0
      return getChildren != nullptr;
43
0
    }
44
  };
45
46
  /// @struct ObjectFieldDescriptor
47
  /// Describes a serializable object field and its child fields.
48
  template <typename Derived> struct ObjectFieldDescriptor : FieldDescriptor
49
  {
50
    /// Create an object field descriptor.
51
    /// @param[in] fieldId The stable numeric identifier of the field
52
    /// @param[in] fieldName The name of the field
53
    ObjectFieldDescriptor(uint16_t fieldId, std::string fieldName)
54
132
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
132
    {}
pcpp::ObjectFieldDescriptor<pcpp::Packet::SerializedFields::TimestampObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::IPv6Layer::SerializedFields::IPv6ExtensionObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::StpConfigurationBPDULayer::BridgeIdSerializedFields>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
18
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
18
    {}
pcpp::ObjectFieldDescriptor<pcpp::TelnetLayer::SerializedFields::CommandObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::BgpOpenMessageLayer::SerializedFields::OptionalParameterObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::BgpUpdateMessageLayer::SerializedFields::PrefixAndIpObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
12
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
12
    {}
pcpp::ObjectFieldDescriptor<pcpp::BgpUpdateMessageLayer::SerializedFields::PathAttributeObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::DnsLayer::SerializedFields::QueryObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::DnsLayer::SerializedFields::ResourceObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::GeneveLayer::SerializedFields::OptionObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::HttpMessage::SerializedFields::HeaderFieldObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::IgmpV3ReportLayer::SerializedFields::GroupRecordObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::MySqlLayer::SerializedFields::MessageObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::MySqlLayer::SerializedFields::ErrorMessageObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::MySqlLayer::SerializedFields::QueryMessageObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::SipLayer::SerializedFields::HeaderFieldObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::SomeIpSdLayer::SerializedFields::EntryObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::SomeIpSdLayer::SerializedFields::OptionObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
pcpp::ObjectFieldDescriptor<pcpp::SdpLayer::SerializedFields::HeaderFieldObject>::ObjectFieldDescriptor(unsigned short, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)
Line
Count
Source
54
6
        : FieldDescriptor(fieldId, std::move(fieldName), &Derived::all)
55
6
    {}
56
  };
57
58
  class ScopeBase;
59
  class ArrayScope;
60
  class ObjectScope;
61
62
  namespace internal
63
  {
64
    template <typename T>
65
    using EnableIfSignedIntegral =
66
        std::enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value && !std::is_same<T, bool>::value,
67
                         int>;
68
69
    template <typename T>
70
    using EnableIfUnsignedIntegral =
71
        std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value && !std::is_same<T, bool>::value,
72
                         int>;
73
  }  // namespace internal
74
75
  /// @class ISerializer
76
  /// Interface for streaming structured data to a serializer.
77
  class ISerializer
78
  {
79
    friend class ScopeBase;
80
    friend class ArrayScope;
81
    friend class ObjectScope;
82
83
  public:
84
    /// Destroy the serializer.
85
0
    virtual ~ISerializer() = default;
86
87
    /// Open an array field.
88
    /// @param[in] field The descriptor of the array field
89
    /// @return A scope representing the newly opened array
90
    ArrayScope writeArray(const FieldDescriptor& field);
91
92
    /// Open an object field.
93
    /// @param[in] field The descriptor of the object field
94
    /// @return A scope representing the newly opened object
95
    ObjectScope writeObject(const FieldDescriptor& field);
96
97
  protected:
98
    // --- Scalar field writes (canonical virtuals) ---
99
    virtual void writeField(const FieldDescriptor& field, const std::string& value) = 0;
100
    virtual void writeField(const FieldDescriptor& field, int64_t value) = 0;
101
    virtual void writeField(const FieldDescriptor& field, uint64_t value) = 0;
102
    virtual void writeField(const FieldDescriptor& field, double value) = 0;
103
    virtual void writeField(const FieldDescriptor& field, bool value) = 0;
104
    virtual void writeNullField(const FieldDescriptor& field) = 0;
105
106
    virtual void writeField(const FieldDescriptor& field, const char* value)
107
0
    {
108
0
      writeField(field, std::string(value));
109
0
    }
110
111
    template <typename T, internal::EnableIfSignedIntegral<T> = 0>
112
    void writeField(const FieldDescriptor& field, T value)
113
    {
114
      writeField(field, static_cast<int64_t>(value));
115
    }
116
117
    template <typename T, internal::EnableIfUnsignedIntegral<T> = 0>
118
    void writeField(const FieldDescriptor& field, T value)
119
    {
120
      writeField(field, static_cast<uint64_t>(value));
121
    }
122
123
    virtual void writeHexField(const FieldDescriptor& field, uint64_t value) = 0;
124
125
    template <typename T, std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, int> = 0>
126
    void writeHexField(const FieldDescriptor& field, T value)
127
    {
128
      writeHexField(field, static_cast<uint64_t>(value));
129
    }
130
131
    virtual void startObject(const FieldDescriptor& field) = 0;
132
    virtual void endObject() = 0;
133
    virtual void startArray(const FieldDescriptor& field) = 0;
134
    virtual void endArray() = 0;
135
136
    void enforceOneRoot();
137
138
    bool m_ShouldEnforceOneRoot = true;
139
140
  private:
141
    bool m_RootWritten = false;
142
  };
143
144
  /// @class ScopeBase
145
  /// Base class for serializer scopes.
146
  class ScopeBase : public ISerializer
147
  {
148
  public:
149
    using ISerializer::writeField;
150
151
    /// Write a string field.
152
    /// @param[in] field The descriptor of the field
153
    /// @param[in] value The string value to write
154
    void writeField(const FieldDescriptor& field, const std::string& value) override;
155
156
    /// Write a signed int64 field.
157
    /// @param[in] field The descriptor of the field
158
    /// @param[in] value The signed integer value to write
159
    void writeField(const FieldDescriptor& field, int64_t value) override;
160
161
    /// Write an unsigned int64 field.
162
    /// @param[in] field The descriptor of the field
163
    /// @param[in] value The unsigned integer value to write
164
    void writeField(const FieldDescriptor& field, uint64_t value) override;
165
166
    /// Write a floating-point field.
167
    /// @param[in] field The descriptor of the field
168
    /// @param[in] value The floating-point value to write
169
    void writeField(const FieldDescriptor& field, double value) override;
170
171
    /// Write a boolean field.
172
    /// @param[in] field The descriptor of the field
173
    /// @param[in] value The boolean value to write
174
    void writeField(const FieldDescriptor& field, bool value) override;
175
176
    /// Write a null field.
177
    /// @param[in] field The descriptor of the field
178
    void writeNullField(const FieldDescriptor& field) override;
179
180
    /// Write an unsigned integer field in hexadecimal notation.
181
    /// @param[in] field The descriptor of the field
182
    /// @param[in] value The unsigned integer value to write
183
    void writeHexField(const FieldDescriptor& field, uint64_t value) override;
184
185
    template <typename T, internal::EnableIfSignedIntegral<T> = 0>
186
    void writeField(const FieldDescriptor& field, T value)
187
0
    {
188
0
      writeField(field, static_cast<int64_t>(value));
189
0
    }
Unexecuted instantiation: _ZN4pcpp9ScopeBase10writeFieldIiTnNSt3__19enable_ifIXaaaasr3std11is_integralIT_EE5valuesr3std9is_signedIS4_EE5valuentsr3std7is_sameIS4_bEE5valueEiE4typeELi0EEEvRKNS_15FieldDescriptorES4_
Unexecuted instantiation: _ZN4pcpp9ScopeBase10writeFieldIaTnNSt3__19enable_ifIXaaaasr3std11is_integralIT_EE5valuesr3std9is_signedIS4_EE5valuentsr3std7is_sameIS4_bEE5valueEiE4typeELi0EEEvRKNS_15FieldDescriptorES4_
190
191
    template <typename T, internal::EnableIfUnsignedIntegral<T> = 0>
192
    void writeField(const FieldDescriptor& field, T value)
193
0
    {
194
0
      writeField(field, static_cast<uint64_t>(value));
195
0
    }
Unexecuted instantiation: _ZN4pcpp9ScopeBase10writeFieldItTnNSt3__19enable_ifIXaaaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valuentsr3std7is_sameIS4_bEE5valueEiE4typeELi0EEEvRKNS_15FieldDescriptorES4_
Unexecuted instantiation: _ZN4pcpp9ScopeBase10writeFieldIjTnNSt3__19enable_ifIXaaaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valuentsr3std7is_sameIS4_bEE5valueEiE4typeELi0EEEvRKNS_15FieldDescriptorES4_
Unexecuted instantiation: _ZN4pcpp9ScopeBase10writeFieldIhTnNSt3__19enable_ifIXaaaasr3std11is_integralIT_EE5valuesr3std11is_unsignedIS4_EE5valuentsr3std7is_sameIS4_bEE5valueEiE4typeELi0EEEvRKNS_15FieldDescriptorES4_
196
197
  protected:
198
    void startObject(const FieldDescriptor& field) override;
199
    void endObject() override;
200
    void startArray(const FieldDescriptor& field) override;
201
    void endArray() override;
202
203
0
    explicit ScopeBase(ISerializer* serializer) : m_Serializer(serializer)
204
0
    {
205
0
      m_ShouldEnforceOneRoot = false;
206
0
    }
207
208
    ISerializer* m_Serializer;
209
  };
210
211
  /// @class ArrayScope
212
  /// RAII scope for a serialized array.
213
  class ArrayScope : public ScopeBase
214
  {
215
  public:
216
    using ScopeBase::writeField;
217
218
    /// Create an array scope.
219
    /// @param[in] serializer The serializer owning the array
220
    /// @param[in] field The descriptor of the array field
221
    ArrayScope(ISerializer* serializer, const FieldDescriptor& field);
222
223
    /// Close the array.
224
    ~ArrayScope() override;
225
  };
226
227
  /// @class ObjectScope
228
  /// RAII scope for a serialized object.
229
  class ObjectScope : public ScopeBase
230
  {
231
  public:
232
    using ScopeBase::writeField;
233
234
    /// Create an object scope.
235
    /// @param[in] serializer The serializer owning the object
236
    /// @param[in] field The descriptor of the object field
237
    ObjectScope(ISerializer* serializer, const FieldDescriptor& field);
238
239
    /// Close the object.
240
    ~ObjectScope() override;
241
  };
242
243
  /// @class JsonSerializer
244
  /// Serializer that writes JSON.
245
  class JsonSerializer : public ISerializer
246
  {
247
  public:
248
    using ISerializer::writeField;
249
    using ISerializer::writeHexField;
250
251
    /// Create a JSON serializer.
252
    ///
253
    /// By default the serializer produces minified JSON.
254
    /// If indent is non-negative, the serializer produces pretty-printed JSON with the specified number of spaces
255
    /// per indentation level.
256
    ///
257
    /// @param[in] out The output stream to which JSON is written
258
    /// @param[in] indent The number of spaces to use for each indentation level (negative for minified JSON)
259
    explicit JsonSerializer(std::ostream& out, int indent = -1);
260
261
  protected:
262
    void writeField(const FieldDescriptor& field, const std::string& value) override;
263
    void writeField(const FieldDescriptor& field, int64_t value) override;
264
    void writeField(const FieldDescriptor& field, uint64_t value) override;
265
    void writeField(const FieldDescriptor& field, double value) override;
266
    void writeField(const FieldDescriptor& field, bool value) override;
267
    void writeNullField(const FieldDescriptor& field) override;
268
    void writeHexField(const FieldDescriptor& field, uint64_t value) override;
269
    void startObject(const FieldDescriptor& field) override;
270
    void endObject() override;
271
    void startArray(const FieldDescriptor& field) override;
272
    void endArray() override;
273
274
  private:
275
    enum class Context
276
    {
277
      Array,
278
      Object
279
    };
280
281
    void prepareNext()
282
0
    {
283
0
      writeSeparatorIfNeeded();
284
      // Skip newline/indent if we are at root level.
285
0
      if (!m_ContextStack.empty())
286
0
      {
287
0
        writeNewlineAndIndent();
288
0
      }
289
0
    }
290
    void writeSeparatorIfNeeded();
291
    void writeKey(const std::string& name);
292
    void writeNewlineAndIndent();
293
294
    std::ostream& m_Out;
295
    std::vector<Context> m_ContextStack;
296
    std::vector<bool> m_FirstAtLevel;
297
    int m_IndentPerLevel = -1;
298
  };
299
300
  /// @class YamlSerializer
301
  /// Serializer that writes YAML.
302
  class YamlSerializer : public ISerializer
303
  {
304
  public:
305
    using ISerializer::writeField;
306
    using ISerializer::writeHexField;
307
308
    /// Create a YAML serializer.
309
    /// @param[in] out The output stream to which YAML is written
310
    explicit YamlSerializer(std::ostream& out);
311
312
  protected:
313
    void writeField(const FieldDescriptor& field, const std::string& value) override;
314
    void writeField(const FieldDescriptor& field, int64_t value) override;
315
    void writeField(const FieldDescriptor& field, uint64_t value) override;
316
    void writeField(const FieldDescriptor& field, double value) override;
317
    void writeField(const FieldDescriptor& field, bool value) override;
318
    void writeNullField(const FieldDescriptor& field) override;
319
    void writeHexField(const FieldDescriptor& field, uint64_t value) override;
320
321
    void startObject(const FieldDescriptor& field) override;
322
    void endObject() override;
323
    void startArray(const FieldDescriptor& field) override;
324
    void endArray() override;
325
326
  private:
327
    enum class Context
328
    {
329
      EmptyArray,
330
      Array,
331
      Object
332
    };
333
334
    void writeIndent();
335
    void writeNewlineIfNeeded();
336
    // Called by startObject()/startArray(), BEFORE pushing the new
337
    // context: writes the line introducing the container (a bare "-"
338
    // for an array element, "name:" for an object member, or nothing
339
    // at the document root) — content follows on subsequent, more
340
    // deeply indented lines.
341
    void writeContainerHeader(const std::string& name);
342
    // Called by writeField()/writeNullField()/writeHexField(): writes
343
    // everything on the current line up to (not including) the value
344
    // itself — "- " inside an array, "name: " inside an object or at
345
    // the root. Caller writes the actual value immediately after.
346
    void writeFieldPrefix(const std::string& name);
347
348
    bool isArrayContext() const;
349
350
    std::ostream& m_Out;
351
    std::vector<Context> m_ContextStack;
352
    bool m_WriteNewLine = false;
353
    bool m_WriteIdent = true;
354
  };
355
356
  /// @class XmlSerializer
357
  /// Serializer that writes XML.
358
  class XmlSerializer : public ISerializer
359
  {
360
  public:
361
    using ISerializer::writeField;
362
    using ISerializer::writeHexField;
363
364
    /// Create an XML serializer.
365
    /// @param[in] out The output stream to which YAML is written
366
    explicit XmlSerializer(std::ostream& out);
367
368
  protected:
369
    void writeField(const FieldDescriptor& field, const std::string& value) override;
370
    void writeField(const FieldDescriptor& field, int64_t value) override;
371
    void writeField(const FieldDescriptor& field, uint64_t value) override;
372
    void writeField(const FieldDescriptor& field, double value) override;
373
    void writeField(const FieldDescriptor& field, bool value) override;
374
    void writeNullField(const FieldDescriptor& field) override;
375
    void writeHexField(const FieldDescriptor& field, uint64_t value) override;
376
377
    void startObject(const FieldDescriptor& field) override;
378
    void endObject() override;
379
    void startArray(const FieldDescriptor& field) override;
380
    void endArray() override;
381
382
  private:
383
    struct Context
384
    {
385
      std::string name;  // Element name
386
      bool isArray;      // True if this is an array container
387
    };
388
389
    // Core writing methods
390
    void writeOpenTag(const std::string& name);
391
    void writeCloseTag(const std::string& name);
392
    void writeValueElement(const std::string& name, const std::string& value, bool isNull = false);
393
    void writeIndent();
394
395
    // XML escaping (handles &, <, >, ", ')
396
    static std::string escapeXML(const std::string& s);
397
398
    // Checks if a string is safe to use as an XML name
399
    static bool isValidXMLName(const std::string& name);
400
401
    // Returns `name` if it's a valid XML element name, otherwise
402
    // `fallback`. Centralizes the isValidXMLName()-check-plus-fallback
403
    // pattern that every writeField()/startObject()/startArray()
404
    // overload below needs.
405
    static std::string resolveElementName(const std::string& name, const char* fallback);
406
407
    // State
408
    std::ostream& m_Out;
409
    std::vector<Context> m_ContextStack;
410
411
    // Optimization: pre-allocate indentation strings
412
    mutable std::vector<std::string> m_IndentCache;
413
  };
414
}  // namespace pcpp