140 static std::unique_ptr<Asn1Record>
decode(
const uint8_t* data,
size_t dataLen,
bool lazy =
true);
155 return m_IsConstructed;
171 return m_ValueLength;
177 return m_TotalLength;
187 template <
class Asn1RecordType> Asn1RecordType*
castAs()
189 auto result =
dynamic_cast<Asn1RecordType*
>(
this);
190 if (result ==
nullptr)
192 throw std::bad_cast();
201 bool m_IsConstructed =
false;
202 uint8_t m_TagType = 0;
204 size_t m_ValueLength = 0;
205 size_t m_TotalLength = 0;
220 decodeValueIfNeeded();
224 static std::unique_ptr<Asn1Record> decodeTagAndCreateRecord(
const uint8_t* data,
size_t dataLen,
226 uint8_t decodeLength(
const uint8_t* data,
size_t dataLen);
227 void decodeValueIfNeeded()
const;
229 uint8_t encodeTag()
const;
230 std::vector<uint8_t> encodeLength()
const;
233 virtual std::vector<std::string> toStringList()
const;
238 void setEncodedValue(uint8_t
const* dataSource,
241 mutable uint8_t
const* m_EncodedValue =
nullptr;
273 decodeValueIfNeeded();
274 return m_Value.get();
284 mutable std::unique_ptr<uint8_t[]> m_Value =
nullptr;
301 const std::vector<Asn1Record*>& subRecords);
314 decodeValueIfNeeded();
324 std::vector<std::string> toStringList()
const override;
326 template <
typename Iterator>
void init(
Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
329 m_TagClass = tagClass;
330 m_IsConstructed =
true;
332 size_t recordValueLength = 0;
333 for (Iterator recordIter = begin; recordIter != end; ++recordIter)
335 auto encodedRecord = (*recordIter)->encode();
336 auto copyRecord =
Asn1Record::decode(encodedRecord.data(), encodedRecord.size(), LazySubRecordDecoding);
337 m_SubRecords.pushBack(std::move(copyRecord));
338 recordValueLength += encodedRecord.size();
341 m_ValueLength = recordValueLength;
342 m_TotalLength = recordValueLength + 1 + (m_ValueLength < 128 ? 1 : 2);
347 static constexpr
bool LazySubRecordDecoding =
false;
349 mutable PointerVector<Asn1Record> m_SubRecords;
409 template <
typename T>
410 using EnableIfUnsignedIntegral =
411 std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
int>;
424 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T
getIntValue()
const
426 decodeValueIfNeeded();
427 return m_Value.getInt<T>();
431 PCPP_DEPRECATED(
"Use getIntValue instead")
434 return getIntValue<uint32_t>();
440 decodeValueIfNeeded();
441 return m_Value.toString();
450 std::vector<std::string> toStringList()
const override;
458 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
explicit BigInt(T value)
460 m_Value = initFromInt(value);
463 explicit BigInt(
const std::string& value);
464 BigInt(
const BigInt& other);
466 template <
typename T, EnableIfUn
signedIntegral<T> = 0> BigInt& operator=(T value)
468 m_Value = initFromInt(value);
471 BigInt& operator=(
const std::string& value);
474 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T getInt()
const
478 throw std::overflow_error(
"Value cannot fit into requested int type");
481 std::stringstream sstream;
482 sstream << std::hex << m_Value;
486 return static_cast<T
>(result);
489 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
bool canFit()
const
491 return sizeof(T) >= (m_Value.size() + 1) / 2;
495 std::vector<uint8_t> toBytes()
const;
500 static std::string initFromString(
const std::string& value);
502 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
static std::string initFromInt(T value)
504 std::stringstream ss;
505 ss << std::hex << static_cast<uint64_t>(value);
510 mutable BigInt m_Value;
537 decodeValueIfNeeded();
545 explicit Asn1StringRecord(
const std::string& value) : Asn1PrimitiveRecord(TagType), m_Value(value)
547 m_ValueLength = value.size();
548 m_TotalLength = m_ValueLength + 2;
553 m_Value = std::string(
reinterpret_cast<char const*
>(data), m_ValueLength);
557 return { m_Value.begin(), m_Value.end() };
560 std::vector<std::string> toStringList()
const override
562 return { Asn1Record::toStringList().front() +
", Value: " +
getValue() };
565 mutable std::string m_Value;
575 using Asn1StringRecord::Asn1StringRecord;
594 mutable bool m_IsPrintable =
true;
659 decodeValueIfNeeded();
667 std::vector<std::string> toStringList()
const override;
672 mutable bool m_Value =
false;
721 return m_Components == other.m_Components;
728 return m_Components != other.m_Components;
748 std::vector<uint32_t> m_Components;
765 decodeValueIfNeeded();
773 std::vector<std::string> toStringList()
const override;
791 std::chrono::system_clock::time_point
getValue(
const std::string& timezone =
"Z")
const
793 decodeValueIfNeeded();
794 return adjustTimezones(m_Value,
"Z", timezone);
803 std::string
getValueAsString(
const std::string& format =
"%Y-%m-%d %H:%M:%S",
const std::string& timezone =
"Z",
804 bool includeMilliseconds =
false)
const;
809 const std::string& timezone);
811 mutable std::chrono::system_clock::time_point m_Value;
813 std::vector<std::string> toStringList()
const override;
815 static void validateTimezone(
const std::string& timezone);
816 static std::chrono::system_clock::time_point adjustTimezones(
const std::chrono::system_clock::time_point& value,
817 const std::string& fromTimezone,
818 const std::string& toTimezone);
831 explicit Asn1UtcTimeRecord(
const std::chrono::system_clock::time_point& value,
bool withSeconds =
true);
839 mutable bool m_WithSeconds =
true;
855 const std::string& timezone =
"Z");
863 mutable std::string m_Timezone;
881 decodeValueIfNeeded();
882 return m_Value.toString();
888 decodeValueIfNeeded();
889 return m_Value.toBytes();
896 std::vector<std::string> toStringList()
const override;
903 explicit BitSet(
const std::string& value);
904 BitSet(
const uint8_t* data,
size_t numBits);
906 BitSet& operator=(
const std::string& value);
908 size_t sizeInBytes()
const;
910 std::vector<uint8_t> toBytes()
const;
911 size_t getNumBits()
const
917 void initFromString(
const std::string& value);
919 std::vector<std::bitset<8>> m_Data;
920 size_t m_NumBits = 0;
923 Asn1BitStringRecord() =
default;
925 mutable BitSet m_Value;
Asn1LoadPolicy
Policy for when to evaluate (decode) ASN.1 record values. Determines whether the value is decoded imm...
Definition: Asn1Codec.h:118
@ Lazy
The value is evaluated on first access (lazy decoding).
@ Eager
The value is evaluated immediately on construction (eager decoding).
Definition: Asn1Codec.h:869
std::string getValue()
Definition: Asn1Codec.h:879
std::vector< uint8_t > getVecValue()
Definition: Asn1Codec.h:886
Asn1BitStringRecord(const std::string &value)
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
Definition: Asn1Codec.h:648
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
bool getValue() const
Definition: Asn1Codec.h:657
Asn1BooleanRecord(bool value)
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Definition: Asn1Codec.h:292
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
PointerVector< Asn1Record > & getSubRecords()
Definition: Asn1Codec.h:312
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const PointerVector< Asn1Record > &subRecords)
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:516
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:845
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Asn1GeneralizedTimeRecord(const std::chrono::system_clock::time_point &value, const std::string &timezone="Z")
Definition: Asn1Codec.h:248
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
const uint8_t * getValue()
Definition: Asn1Codec.h:271
Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const uint8_t *value, size_t valueLen)
Asn1GenericRecord(Asn1TagClass tagClass, bool isConstructed, uint8_t tagType, const std::string &value)
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
Definition: Asn1Codec.h:632
Asn1IA5StringRecord(const std::string &value)
Definition: Asn1Codec.h:638
Definition: Asn1Codec.h:405
T getIntValue() const
Definition: Asn1Codec.h:424
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Asn1IntegerRecord(uint64_t value)
std::string getValueAsString() const
Definition: Asn1Codec.h:438
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
uint32_t getValue()
Definition: Asn1Codec.h:432
Asn1IntegerRecord(const std::string &value)
Definition: Asn1Codec.h:678
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
Definition: Asn1Codec.h:686
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Definition: Asn1Codec.h:688
Asn1NullRecord()
A constructor to create a record of type Null.
Definition: Asn1Codec.h:697
std::vector< uint8_t > toBytes() const
bool operator==(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:719
std::string toString() const
Asn1ObjectIdentifier(const uint8_t *data, size_t dataLen)
bool operator!=(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:726
Asn1ObjectIdentifier(const std::string &oidString)
const std::vector< uint32_t > & getComponents() const
Definition: Asn1Codec.h:712
Definition: Asn1Codec.h:754
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
const Asn1ObjectIdentifier & getValue() const
Definition: Asn1Codec.h:763
Asn1ObjectIdentifierRecord(const Asn1ObjectIdentifier &value)
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Definition: Asn1Codec.h:571
Asn1OctetStringRecord(const std::string &value)
Definition: Asn1Codec.h:584
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Asn1OctetStringRecord(const uint8_t *value, size_t valueLength)
Definition: Asn1Codec.h:394
Definition: Asn1Codec.h:616
Asn1PrintableStringRecord(const std::string &value)
Definition: Asn1Codec.h:622
Definition: Asn1Codec.h:131
std::string toString() const
uint8_t getTagType() const
Definition: Asn1Codec.h:163
virtual void decodeValue(uint8_t const *data) const =0
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
bool isConstructed() const
Definition: Asn1Codec.h:153
Asn1UniversalTagType getUniversalTagType() const
Asn1TagClass getTagClass() const
Definition: Asn1Codec.h:147
size_t getTotalLength() const
Definition: Asn1Codec.h:175
Asn1RecordType * castAs()
Definition: Asn1Codec.h:187
std::vector< uint8_t > encode() const
size_t getValueLength() const
Definition: Asn1Codec.h:169
std::vector< uint8_t > encodeValueSafe() const
Encodes the record value into a byte array, ensuring that the value is decoded first if needed.
Definition: Asn1Codec.h:218
static std::unique_ptr< Asn1Record > decode(const uint8_t *data, size_t dataLen, bool lazy=true)
virtual std::vector< uint8_t > encodeValue() const =0
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Definition: Asn1Codec.h:355
Asn1SequenceRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SequenceRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:374
Asn1SetRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SetRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:532
std::string getValue() const
Definition: Asn1Codec.h:535
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
Definition: Asn1Codec.h:551
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
Definition: Asn1Codec.h:555
Definition: Asn1Codec.h:785
std::chrono::system_clock::time_point getValue(const std::string &timezone="Z") const
Definition: Asn1Codec.h:791
std::string getValueAsString(const std::string &format="%Y-%m-%d %H:%M:%S", const std::string &timezone="Z", bool includeMilliseconds=false) const
Definition: Asn1Codec.h:600
Asn1UTF8StringRecord(const std::string &value)
Definition: Asn1Codec.h:606
Definition: Asn1Codec.h:824
std::vector< uint8_t > encodeValue() const override
Encodes the record value into a byte array Prefer using encodeValueSafe() to ensure the value is deco...
void decodeValue(uint8_t const *data) const override
Decodes the record value from a byte array into the mutable cache variables. This method is marked as...
Asn1UtcTimeRecord(const std::chrono::system_clock::time_point &value, bool withSeconds=true)
Definition: PointerVector.h:50
The main namespace for the PcapPlusPlus lib.
Asn1TagClass
An enum for representing ASN.1 tag class.
Definition: Asn1Codec.h:20
@ ContextSpecific
The Context-Specific tag class.
@ Private
The Private tag class.
@ Universal
The Universal tag class.
@ Application
The Application tag class.
Asn1UniversalTagType
An enum for representing ASN.1 Universal tag types.
Definition: Asn1Codec.h:33
@ BitString
The universal tag type for Bit String.
@ Enumerated
The universal tag type for Enumerated.
@ ObjectIdentifierIRI
The universal tag type for Object Identifier Internationalized Resource Identifier (IRI)
@ ObjectDescriptor
The universal tag type for Object Descriptor.
@ TimeOfDay
The universal tag type for Time of Day.
@ GraphicString
The universal tag type for GraphicString.
@ UTCTime
The universal tag type for UTC time.
@ Boolean
The universal tag type for Boolean.
@ EmbeddedPDV
The universal tag type for Embedded-PDV.
@ OctetString
The universal tag type for Octet String.
@ UniversalString
The universal tag type for UniversalString.
@ Sequence
The universal tag type Sequence.
@ Date
The universal tag type for Date.
@ RelativeObjectIdentifierIRI
The universal tag type for Relative Object Identifier Internationalized Resource Identifier (IRI)
@ PrintableString
The universal tag type for Printable String.
@ CharacterString
The universal tag type for CharacterString.
@ RelativeObjectIdentifier
The universal tag type for Relative Object Identifier.
@ Set
The universal tag type for Set.
@ ObjectIdentifier
The universal tag type for Object Identifier.
@ IA5String
The universal tag type for IA5String.
@ VideotexString
The universal tag type for Videotex String.
@ NotApplicable
A non-applicable value.
@ Real
The universal tag type for Real.
@ DateTime
The universal tag type for Date-Time.
@ EndOfContent
The reserved identifier for the End-of-Contents marker in an indefinite length encoding.
@ Reserved
A reserved value.
@ Integer
The universal tag type for Integer.
@ Time
The universal tag type for Time.
@ External
The universal tag type for External.
@ BMPString
The universal tag type for BMPString.
@ UTF8String
The universal tag type for UTF8 String.
@ Null
The universal tag type for Null.
@ GeneralizedTime
The universal tag type for Generalized time.
@ T61String
The universal tag type for T61String.
@ Duration
The universal tag type for Duration.
@ VisibleString
The universal tag type for VisibleString.
@ GeneralString
The universal tag type for GeneralString.
@ NumericString
The universal tag type for Numeric String.