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>();
434 return getIntValue<uint32_t>();
442 decodeValueIfNeeded();
443 return m_Value.toString(removeLeadingZeros);
452 std::vector<std::string> toStringList()
const override;
460 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
461 explicit BigInt(T value) : m_Value(initFromInt(value))
464 explicit BigInt(
const std::string& value);
465 BigInt(
const BigInt& other);
467 template <
typename T, EnableIfUn
signedIntegral<T> = 0> BigInt& operator=(T value)
469 m_Value = initFromInt(value);
472 BigInt& operator=(
const std::string& value);
475 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T getInt()
const
479 throw std::overflow_error(
"Value cannot fit into requested int type");
482 std::stringstream sstream;
483 sstream << std::hex << m_Value;
487 return static_cast<T
>(result);
490 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
bool canFit()
const
492 return sizeof(T) >= (m_Value.size() + 1) / 2;
495 std::string
toString(
bool removeLeadingZeros =
false)
const;
496 std::vector<uint8_t> toBytes()
const;
501 static std::string initFromString(
const std::string& value);
503 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
static std::string initFromInt(T value)
505 std::stringstream ss;
506 ss << std::hex << static_cast<uint64_t>(value);
511 mutable BigInt m_Value;
538 decodeValueIfNeeded();
546 explicit Asn1StringRecord(
const std::string& value) : Asn1PrimitiveRecord(TagType), m_Value(value)
548 m_ValueLength = value.size();
549 m_TotalLength = m_ValueLength + 2;
554 m_Value = std::string(
reinterpret_cast<char const*
>(data), m_ValueLength);
558 return { m_Value.begin(), m_Value.end() };
561 std::vector<std::string> toStringList()
const override
563 return { Asn1Record::toStringList().front() +
", Value: " +
getValue() };
566 mutable std::string m_Value;
576 using Asn1StringRecord::Asn1StringRecord;
595 mutable bool m_IsPrintable =
true;
660 decodeValueIfNeeded();
668 std::vector<std::string> toStringList()
const override;
673 mutable bool m_Value =
false;
722 return m_Components == other.m_Components;
729 return m_Components != other.m_Components;
749 std::vector<uint32_t> m_Components;
766 decodeValueIfNeeded();
774 std::vector<std::string> toStringList()
const override;
792 std::chrono::system_clock::time_point
getValue(
const std::string& timezone =
"Z")
const
794 decodeValueIfNeeded();
795 return adjustTimezones(m_Value,
"Z", timezone);
804 std::string
getValueAsString(
const std::string& format =
"%Y-%m-%d %H:%M:%S",
const std::string& timezone =
"Z",
805 bool includeMilliseconds =
false)
const;
810 const std::string& timezone);
812 mutable std::chrono::system_clock::time_point m_Value;
814 std::vector<std::string> toStringList()
const override;
816 static void validateTimezone(
const std::string& timezone);
817 static std::chrono::system_clock::time_point adjustTimezones(
const std::chrono::system_clock::time_point& value,
818 const std::string& fromTimezone,
819 const std::string& toTimezone);
832 explicit Asn1UtcTimeRecord(
const std::chrono::system_clock::time_point& value,
bool withSeconds =
true);
840 mutable bool m_WithSeconds =
true;
856 const std::string& timezone =
"Z");
864 mutable std::string m_Timezone;
882 decodeValueIfNeeded();
883 return m_Value.toString();
889 decodeValueIfNeeded();
890 return m_Value.toBytes();
897 std::vector<std::string> toStringList()
const override;
904 explicit BitSet(
const std::string& value);
905 BitSet(
const uint8_t* data,
size_t numBits);
907 BitSet& operator=(
const std::string& value);
909 size_t sizeInBytes()
const;
911 std::vector<uint8_t> toBytes()
const;
912 size_t getNumBits()
const
918 void initFromString(
const std::string& value);
920 std::vector<std::bitset<8>> m_Data;
921 size_t m_NumBits = 0;
924 Asn1BitStringRecord() =
default;
926 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:870
std::string getValue()
Definition: Asn1Codec.h:880
std::vector< uint8_t > getVecValue()
Definition: Asn1Codec.h:887
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:649
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:658
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:517
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:846
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:633
Asn1IA5StringRecord(const std::string &value)
Definition: Asn1Codec.h:639
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)
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
std::string getValueAsString(bool removeLeadingZeros=false) const
Definition: Asn1Codec.h:440
Asn1IntegerRecord(const std::string &value)
Definition: Asn1Codec.h:679
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:687
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:689
Asn1NullRecord()
A constructor to create a record of type Null.
Definition: Asn1Codec.h:698
std::vector< uint8_t > toBytes() const
bool operator==(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:720
std::string toString() const
Asn1ObjectIdentifier(const uint8_t *data, size_t dataLen)
bool operator!=(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:727
Asn1ObjectIdentifier(const std::string &oidString)
const std::vector< uint32_t > & getComponents() const
Definition: Asn1Codec.h:713
Definition: Asn1Codec.h:755
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:764
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:572
Asn1OctetStringRecord(const std::string &value)
Definition: Asn1Codec.h:585
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:617
Asn1PrintableStringRecord(const std::string &value)
Definition: Asn1Codec.h:623
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:533
std::string getValue() const
Definition: Asn1Codec.h:536
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:552
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:556
Definition: Asn1Codec.h:786
std::chrono::system_clock::time_point getValue(const std::string &timezone="Z") const
Definition: Asn1Codec.h:792
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:601
Asn1UTF8StringRecord(const std::string &value)
Definition: Asn1Codec.h:607
Definition: Asn1Codec.h:825
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.
Definition: AssertionUtils.h:19
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.
@ PCPP_DEPRECATED
Deprecated typo, use OsiModelSessionLayer instead.
Definition: ProtocolType.h:432
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.