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>();
442 decodeValueIfNeeded();
443 return m_Value.toString(removeLeadingZeros);
452 std::vector<std::string> toStringList()
const override;
460 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
explicit BigInt(T value)
462 m_Value = initFromInt(value);
465 explicit BigInt(
const std::string& value);
466 BigInt(
const BigInt& other);
468 template <
typename T, EnableIfUn
signedIntegral<T> = 0> BigInt& operator=(T value)
470 m_Value = initFromInt(value);
473 BigInt& operator=(
const std::string& value);
476 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T getInt()
const
480 throw std::overflow_error(
"Value cannot fit into requested int type");
483 std::stringstream sstream;
484 sstream << std::hex << m_Value;
488 return static_cast<T
>(result);
491 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
bool canFit()
const
493 return sizeof(T) >= (m_Value.size() + 1) / 2;
496 std::string
toString(
bool removeLeadingZeros =
false)
const;
497 std::vector<uint8_t> toBytes()
const;
502 static std::string initFromString(
const std::string& value);
504 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
static std::string initFromInt(T value)
506 std::stringstream ss;
507 ss << std::hex << static_cast<uint64_t>(value);
512 mutable BigInt m_Value;
539 decodeValueIfNeeded();
547 explicit Asn1StringRecord(
const std::string& value) : Asn1PrimitiveRecord(TagType), m_Value(value)
549 m_ValueLength = value.size();
550 m_TotalLength = m_ValueLength + 2;
555 m_Value = std::string(
reinterpret_cast<char const*
>(data), m_ValueLength);
559 return { m_Value.begin(), m_Value.end() };
562 std::vector<std::string> toStringList()
const override
564 return { Asn1Record::toStringList().front() +
", Value: " +
getValue() };
567 mutable std::string m_Value;
577 using Asn1StringRecord::Asn1StringRecord;
596 mutable bool m_IsPrintable =
true;
661 decodeValueIfNeeded();
669 std::vector<std::string> toStringList()
const override;
674 mutable bool m_Value =
false;
723 return m_Components == other.m_Components;
730 return m_Components != other.m_Components;
750 std::vector<uint32_t> m_Components;
767 decodeValueIfNeeded();
775 std::vector<std::string> toStringList()
const override;
793 std::chrono::system_clock::time_point
getValue(
const std::string& timezone =
"Z")
const
795 decodeValueIfNeeded();
796 return adjustTimezones(m_Value,
"Z", timezone);
805 std::string
getValueAsString(
const std::string& format =
"%Y-%m-%d %H:%M:%S",
const std::string& timezone =
"Z",
806 bool includeMilliseconds =
false)
const;
811 const std::string& timezone);
813 mutable std::chrono::system_clock::time_point m_Value;
815 std::vector<std::string> toStringList()
const override;
817 static void validateTimezone(
const std::string& timezone);
818 static std::chrono::system_clock::time_point adjustTimezones(
const std::chrono::system_clock::time_point& value,
819 const std::string& fromTimezone,
820 const std::string& toTimezone);
833 explicit Asn1UtcTimeRecord(
const std::chrono::system_clock::time_point& value,
bool withSeconds =
true);
841 mutable bool m_WithSeconds =
true;
857 const std::string& timezone =
"Z");
865 mutable std::string m_Timezone;
883 decodeValueIfNeeded();
884 return m_Value.toString();
890 decodeValueIfNeeded();
891 return m_Value.toBytes();
898 std::vector<std::string> toStringList()
const override;
905 explicit BitSet(
const std::string& value);
906 BitSet(
const uint8_t* data,
size_t numBits);
908 BitSet& operator=(
const std::string& value);
910 size_t sizeInBytes()
const;
912 std::vector<uint8_t> toBytes()
const;
913 size_t getNumBits()
const
919 void initFromString(
const std::string& value);
921 std::vector<std::bitset<8>> m_Data;
922 size_t m_NumBits = 0;
925 Asn1BitStringRecord() =
default;
927 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:871
std::string getValue()
Definition: Asn1Codec.h:881
std::vector< uint8_t > getVecValue()
Definition: Asn1Codec.h:888
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:650
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:659
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:518
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:847
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:634
Asn1IA5StringRecord(const std::string &value)
Definition: Asn1Codec.h:640
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:680
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:688
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:690
Asn1NullRecord()
A constructor to create a record of type Null.
Definition: Asn1Codec.h:699
std::vector< uint8_t > toBytes() const
bool operator==(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:721
std::string toString() const
Asn1ObjectIdentifier(const uint8_t *data, size_t dataLen)
bool operator!=(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:728
Asn1ObjectIdentifier(const std::string &oidString)
const std::vector< uint32_t > & getComponents() const
Definition: Asn1Codec.h:714
Definition: Asn1Codec.h:756
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:765
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:573
Asn1OctetStringRecord(const std::string &value)
Definition: Asn1Codec.h:586
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:618
Asn1PrintableStringRecord(const std::string &value)
Definition: Asn1Codec.h:624
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:534
std::string getValue() const
Definition: Asn1Codec.h:537
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:553
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:557
Definition: Asn1Codec.h:787
std::chrono::system_clock::time_point getValue(const std::string &timezone="Z") const
Definition: Asn1Codec.h:793
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:602
Asn1UTF8StringRecord(const std::string &value)
Definition: Asn1Codec.h:608
Definition: Asn1Codec.h:826
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.
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.