126 static std::unique_ptr<Asn1Record>
decode(
const uint8_t* data,
size_t dataLen,
bool lazy =
true);
141 return m_IsConstructed;
157 return m_ValueLength;
163 return m_TotalLength;
173 template <
class Asn1RecordType> Asn1RecordType*
castAs()
175 auto result =
dynamic_cast<Asn1RecordType*
>(
this);
176 if (result ==
nullptr)
178 throw std::bad_cast();
187 bool m_IsConstructed =
false;
188 uint8_t m_TagType = 0;
190 size_t m_ValueLength = 0;
191 size_t m_TotalLength = 0;
193 uint8_t
const* m_EncodedValue =
nullptr;
197 static std::unique_ptr<Asn1Record> decodeInternal(
const uint8_t* data,
size_t dataLen,
bool lazy);
199 virtual void decodeValue(uint8_t
const* data,
bool lazy) = 0;
200 virtual std::vector<uint8_t> encodeValue()
const = 0;
202 static std::unique_ptr<Asn1Record> decodeTagAndCreateRecord(
const uint8_t* data,
size_t dataLen,
204 uint8_t decodeLength(
const uint8_t* data,
size_t dataLen);
205 void decodeValueIfNeeded();
208 std::vector<uint8_t> encodeLength()
const;
210 virtual std::vector<std::string> toStringList();
244 decodeValueIfNeeded();
245 return m_Value.get();
251 void decodeValue(uint8_t
const* data,
bool lazy)
override;
252 std::vector<uint8_t> encodeValue()
const override;
255 std::unique_ptr<uint8_t[]> m_Value =
nullptr;
272 const std::vector<Asn1Record*>& subRecords);
285 decodeValueIfNeeded();
292 void decodeValue(uint8_t
const* data,
bool lazy)
override;
293 std::vector<uint8_t> encodeValue()
const override;
295 std::vector<std::string> toStringList()
override;
297 template <
typename Iterator>
void init(
Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
300 m_TagClass = tagClass;
301 m_IsConstructed =
true;
303 size_t recordValueLength = 0;
304 for (Iterator recordIter = begin; recordIter != end; ++recordIter)
306 auto encodedRecord = (*recordIter)->encode();
307 auto copyRecord =
Asn1Record::decode(encodedRecord.data(), encodedRecord.size(),
false);
308 m_SubRecords.pushBack(std::move(copyRecord));
309 recordValueLength += encodedRecord.size();
312 m_ValueLength = recordValueLength;
313 m_TotalLength = recordValueLength + 1 + (m_ValueLength < 128 ? 1 : 2);
317 PointerVector<Asn1Record> m_SubRecords;
377 template <
typename T>
378 using EnableIfUnsignedIntegral =
379 std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
int>;
392 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T
getIntValue()
394 decodeValueIfNeeded();
395 return m_Value.getInt<T>();
399 PCPP_DEPRECATED(
"Use getIntValue instead")
402 return getIntValue<uint32_t>();
408 decodeValueIfNeeded();
409 return m_Value.toString();
415 void decodeValue(uint8_t
const* data,
bool lazy)
override;
416 std::vector<uint8_t> encodeValue()
const override;
418 std::vector<std::string> toStringList()
override;
426 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
explicit BigInt(T value)
428 m_Value = initFromInt(value);
431 explicit BigInt(
const std::string& value);
432 BigInt(
const BigInt& other);
434 template <
typename T, EnableIfUn
signedIntegral<T> = 0> BigInt& operator=(T value)
436 m_Value = initFromInt(value);
439 BigInt& operator=(
const std::string& value);
442 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T getInt()
const
446 throw std::overflow_error(
"Value cannot fit into requested int type");
449 std::stringstream sstream;
450 sstream << std::hex << m_Value;
454 return static_cast<T
>(result);
457 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
bool canFit()
const
459 return sizeof(T) >= (m_Value.size() + 1) / 2;
463 std::vector<uint8_t> toBytes()
const;
468 static std::string initFromString(
const std::string& value);
470 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
static std::string initFromInt(T value)
472 std::stringstream ss;
473 ss << std::hex << static_cast<uint64_t>(value);
505 decodeValueIfNeeded();
513 explicit Asn1StringRecord(
const std::string& value) : Asn1PrimitiveRecord(TagType), m_Value(value)
515 m_ValueLength = value.size();
516 m_TotalLength = m_ValueLength + 2;
519 void decodeValue(uint8_t
const* data,
bool lazy)
override
521 m_Value = std::string(
reinterpret_cast<char const*
>(data), m_ValueLength);
523 std::vector<uint8_t> encodeValue()
const override
525 return { m_Value.begin(), m_Value.end() };
528 std::vector<std::string> toStringList()
override
530 return { Asn1Record::toStringList().front() +
", Value: " +
getValue() };
543 using Asn1StringRecord::Asn1StringRecord;
556 void decodeValue(uint8_t
const* data,
bool lazy)
override;
557 std::vector<uint8_t> encodeValue()
const override;
562 bool m_IsPrintable =
true;
627 decodeValueIfNeeded();
632 void decodeValue(uint8_t
const* data,
bool lazy)
override;
633 std::vector<uint8_t> encodeValue()
const override;
635 std::vector<std::string> toStringList()
override;
640 bool m_Value =
false;
654 void decodeValue(uint8_t
const* data,
bool lazy)
override
656 std::vector<uint8_t> encodeValue()
const override
689 return m_Components == other.m_Components;
696 return m_Components != other.m_Components;
716 std::vector<uint32_t> m_Components;
733 decodeValueIfNeeded();
738 void decodeValue(uint8_t
const* data,
bool lazy)
override;
739 std::vector<uint8_t> encodeValue()
const override;
741 std::vector<std::string> toStringList()
override;
759 std::chrono::system_clock::time_point
getValue(
const std::string& timezone =
"Z")
761 decodeValueIfNeeded();
762 return adjustTimezones(m_Value,
"Z", timezone);
771 std::string
getValueAsString(
const std::string& format =
"%Y-%m-%d %H:%M:%S",
const std::string& timezone =
"Z",
772 bool includeMilliseconds =
false);
777 const std::string& timezone);
779 std::chrono::system_clock::time_point m_Value;
781 std::vector<std::string> toStringList()
override;
783 static void validateTimezone(
const std::string& timezone);
784 static std::chrono::system_clock::time_point adjustTimezones(
const std::chrono::system_clock::time_point& value,
785 const std::string& fromTimezone,
786 const std::string& toTimezone);
799 explicit Asn1UtcTimeRecord(
const std::chrono::system_clock::time_point& value,
bool withSeconds =
true);
802 void decodeValue(uint8_t
const* data,
bool lazy)
override;
803 std::vector<uint8_t> encodeValue()
const override;
807 bool m_WithSeconds =
true;
823 const std::string& timezone =
"Z");
826 void decodeValue(uint8_t
const* data,
bool lazy)
override;
827 std::vector<uint8_t> encodeValue()
const override;
831 std::string m_Timezone;
849 decodeValueIfNeeded();
850 return m_Value.toString();
856 decodeValueIfNeeded();
857 return m_Value.toBytes();
861 void decodeValue(uint8_t
const* data,
bool lazy)
override;
862 std::vector<uint8_t> encodeValue()
const override;
864 std::vector<std::string> toStringList()
override;
871 explicit BitSet(
const std::string& value);
872 BitSet(
const uint8_t* data,
size_t numBits);
874 BitSet& operator=(
const std::string& value);
876 size_t sizeInBytes()
const;
878 std::vector<uint8_t> toBytes()
const;
879 size_t getNumBits()
const
885 void initFromString(
const std::string& value);
887 std::vector<std::bitset<8>> m_Data;
888 size_t m_NumBits = 0;
Definition: Asn1Codec.h:837
std::string getValue()
Definition: Asn1Codec.h:847
std::vector< uint8_t > getVecValue()
Definition: Asn1Codec.h:854
Asn1BitStringRecord(const std::string &value)
Definition: Asn1Codec.h:616
Asn1BooleanRecord(bool value)
bool getValue()
Definition: Asn1Codec.h:625
Definition: Asn1Codec.h:263
PointerVector< Asn1Record > & getSubRecords()
Definition: Asn1Codec.h:283
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const PointerVector< Asn1Record > &subRecords)
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:484
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:813
Asn1GeneralizedTimeRecord(const std::chrono::system_clock::time_point &value, const std::string &timezone="Z")
Definition: Asn1Codec.h:219
const uint8_t * getValue()
Definition: Asn1Codec.h:242
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)
Definition: Asn1Codec.h:600
Asn1IA5StringRecord(const std::string &value)
Definition: Asn1Codec.h:606
Definition: Asn1Codec.h:373
Asn1IntegerRecord(uint64_t value)
T getIntValue()
Definition: Asn1Codec.h:392
uint32_t getValue()
Definition: Asn1Codec.h:400
std::string getValueAsString()
Definition: Asn1Codec.h:406
Asn1IntegerRecord(const std::string &value)
Definition: Asn1Codec.h:646
Asn1NullRecord()
A constructor to create a record of type Null.
Definition: Asn1Codec.h:665
std::vector< uint8_t > toBytes() const
bool operator==(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:687
std::string toString() const
Asn1ObjectIdentifier(const uint8_t *data, size_t dataLen)
bool operator!=(const Asn1ObjectIdentifier &other) const
Definition: Asn1Codec.h:694
Asn1ObjectIdentifier(const std::string &oidString)
const std::vector< uint32_t > & getComponents() const
Definition: Asn1Codec.h:680
Definition: Asn1Codec.h:722
const Asn1ObjectIdentifier & getValue()
Definition: Asn1Codec.h:731
Asn1ObjectIdentifierRecord(const Asn1ObjectIdentifier &value)
Definition: Asn1Codec.h:539
Asn1OctetStringRecord(const std::string &value)
Definition: Asn1Codec.h:552
Asn1OctetStringRecord(const uint8_t *value, size_t valueLength)
Definition: Asn1Codec.h:362
Definition: Asn1Codec.h:584
Asn1PrintableStringRecord(const std::string &value)
Definition: Asn1Codec.h:590
Definition: Asn1Codec.h:117
uint8_t getTagType() const
Definition: Asn1Codec.h:149
std::vector< uint8_t > encode()
bool isConstructed() const
Definition: Asn1Codec.h:139
Asn1UniversalTagType getUniversalTagType() const
Asn1TagClass getTagClass() const
Definition: Asn1Codec.h:133
size_t getTotalLength() const
Definition: Asn1Codec.h:161
Asn1RecordType * castAs()
Definition: Asn1Codec.h:173
size_t getValueLength() const
Definition: Asn1Codec.h:155
static std::unique_ptr< Asn1Record > decode(const uint8_t *data, size_t dataLen, bool lazy=true)
Definition: Asn1Codec.h:323
Asn1SequenceRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SequenceRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:342
Asn1SetRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SetRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:500
std::string getValue()
Definition: Asn1Codec.h:503
Definition: Asn1Codec.h:753
std::string getValueAsString(const std::string &format="%Y-%m-%d %H:%M:%S", const std::string &timezone="Z", bool includeMilliseconds=false)
std::chrono::system_clock::time_point getValue(const std::string &timezone="Z")
Definition: Asn1Codec.h:759
Definition: Asn1Codec.h:568
Asn1UTF8StringRecord(const std::string &value)
Definition: Asn1Codec.h:574
Definition: Asn1Codec.h:792
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.