124 static std::unique_ptr<Asn1Record>
decode(
const uint8_t* data,
size_t dataLen,
bool lazy =
true);
139 return m_IsConstructed;
155 return m_ValueLength;
161 return m_TotalLength;
171 template <
class Asn1RecordType> Asn1RecordType*
castAs()
173 auto result =
dynamic_cast<Asn1RecordType*
>(
this);
174 if (result ==
nullptr)
176 throw std::bad_cast();
185 bool m_IsConstructed =
false;
186 uint8_t m_TagType = 0;
188 size_t m_ValueLength = 0;
189 size_t m_TotalLength = 0;
191 uint8_t* m_EncodedValue =
nullptr;
195 static Asn1Record* decodeInternal(
const uint8_t* data,
size_t dataLen,
bool lazy);
197 virtual void decodeValue(uint8_t* data,
bool lazy) = 0;
198 virtual std::vector<uint8_t> encodeValue()
const = 0;
200 static Asn1Record* decodeTagAndCreateRecord(
const uint8_t* data,
size_t dataLen, uint8_t& tagLen);
201 uint8_t decodeLength(
const uint8_t* data,
size_t dataLen);
202 void decodeValueIfNeeded();
205 std::vector<uint8_t> encodeLength()
const;
207 virtual std::vector<std::string> toStringList();
241 decodeValueIfNeeded();
248 void decodeValue(uint8_t* data,
bool lazy)
override;
249 std::vector<uint8_t> encodeValue()
const override;
252 uint8_t* m_Value =
nullptr;
269 const std::vector<Asn1Record*>& subRecords);
282 decodeValueIfNeeded();
289 void decodeValue(uint8_t* data,
bool lazy)
override;
290 std::vector<uint8_t> encodeValue()
const override;
292 std::vector<std::string> toStringList()
override;
294 template <
typename Iterator>
void init(
Asn1TagClass tagClass, uint8_t tagType, Iterator begin, Iterator end)
297 m_TagClass = tagClass;
298 m_IsConstructed =
true;
300 size_t recordValueLength = 0;
301 for (Iterator recordIter = begin; recordIter != end; ++recordIter)
303 auto encodedRecord = (*recordIter)->encode();
304 auto copyRecord =
Asn1Record::decode(encodedRecord.data(), encodedRecord.size(),
false);
305 m_SubRecords.pushBack(std::move(copyRecord));
306 recordValueLength += encodedRecord.size();
309 m_ValueLength = recordValueLength;
310 m_TotalLength = recordValueLength + 1 + (m_ValueLength < 128 ? 1 : 2);
314 PointerVector<Asn1Record> m_SubRecords;
374 template <
typename T>
375 using EnableIfUnsignedIntegral =
376 std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value,
int>;
389 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T
getIntValue()
391 decodeValueIfNeeded();
392 return m_Value.getInt<T>();
396 PCPP_DEPRECATED(
"Use getIntValue instead")
399 return getIntValue<uint32_t>();
405 decodeValueIfNeeded();
406 return m_Value.toString();
412 void decodeValue(uint8_t* data,
bool lazy)
override;
413 std::vector<uint8_t> encodeValue()
const override;
415 std::vector<std::string> toStringList()
override;
423 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
explicit BigInt(T value)
425 m_Value = initFromInt(value);
428 explicit BigInt(
const std::string& value);
429 BigInt(
const BigInt& other);
431 template <
typename T, EnableIfUn
signedIntegral<T> = 0> BigInt& operator=(T value)
433 m_Value = initFromInt(value);
436 BigInt& operator=(
const std::string& value);
439 template <
typename T, EnableIfUn
signedIntegral<T> = 0> T getInt()
const
443 throw std::overflow_error(
"Value cannot fit into requested int type");
446 std::stringstream sstream;
447 sstream << std::hex << m_Value;
451 return static_cast<T
>(result);
454 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
bool canFit()
const
456 return sizeof(T) >= (m_Value.size() + 1) / 2;
460 std::vector<uint8_t> toBytes()
const;
465 static std::string initFromString(
const std::string& value);
467 template <
typename T, EnableIfUn
signedIntegral<T> = 0>
static std::string initFromInt(T value)
469 std::stringstream ss;
470 ss << std::hex << static_cast<uint64_t>(value);
512 decodeValueIfNeeded();
517 void decodeValue(uint8_t* data,
bool lazy)
override;
518 std::vector<uint8_t> encodeValue()
const override;
520 std::vector<std::string> toStringList()
override;
524 bool m_IsPrintable =
true;
543 decodeValueIfNeeded();
548 void decodeValue(uint8_t* data,
bool lazy)
override;
549 std::vector<uint8_t> encodeValue()
const override;
551 std::vector<std::string> toStringList()
override;
556 bool m_Value =
false;
570 void decodeValue(uint8_t* data,
bool lazy)
override
572 std::vector<uint8_t> encodeValue()
const override
Definition: Asn1Codec.h:532
Asn1BooleanRecord(bool value)
bool getValue()
Definition: Asn1Codec.h:541
Definition: Asn1Codec.h:260
PointerVector< Asn1Record > & getSubRecords()
Definition: Asn1Codec.h:280
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const PointerVector< Asn1Record > &subRecords)
Asn1ConstructedRecord(Asn1TagClass tagClass, uint8_t tagType, const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:481
Asn1EnumeratedRecord(uint32_t value)
Definition: Asn1Codec.h:216
const uint8_t * getValue()
Definition: Asn1Codec.h:239
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:370
Asn1IntegerRecord(uint64_t value)
T getIntValue()
Definition: Asn1Codec.h:389
uint32_t getValue()
Definition: Asn1Codec.h:397
std::string getValueAsString()
Definition: Asn1Codec.h:403
Asn1IntegerRecord(const std::string &value)
Definition: Asn1Codec.h:562
Asn1NullRecord()
A constructor to create a record of type Null.
Definition: Asn1Codec.h:496
Asn1OctetStringRecord(const std::string &value)
std::string getValue()
Definition: Asn1Codec.h:510
Asn1OctetStringRecord(const uint8_t *value, size_t valueLength)
Definition: Asn1Codec.h:359
Definition: Asn1Codec.h:115
uint8_t getTagType() const
Definition: Asn1Codec.h:147
std::vector< uint8_t > encode()
bool isConstructed() const
Definition: Asn1Codec.h:137
Asn1UniversalTagType getUniversalTagType() const
Asn1TagClass getTagClass() const
Definition: Asn1Codec.h:131
size_t getTotalLength() const
Definition: Asn1Codec.h:159
Asn1RecordType * castAs()
Definition: Asn1Codec.h:171
size_t getValueLength() const
Definition: Asn1Codec.h:153
static std::unique_ptr< Asn1Record > decode(const uint8_t *data, size_t dataLen, bool lazy=true)
Definition: Asn1Codec.h:320
Asn1SequenceRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SequenceRecord(const std::vector< Asn1Record * > &subRecords)
Definition: Asn1Codec.h:339
Asn1SetRecord(const PointerVector< Asn1Record > &subRecords)
Asn1SetRecord(const std::vector< Asn1Record * > &subRecords)
Definition: PointerVector.h:50
The main namespace for the PcapPlusPlus lib.
Asn1TagClass
An enum for representing ASN.1 tag class.
Definition: Asn1Codec.h:18
@ 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:31
@ 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.