39 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
43 static_assert(std::is_base_of<tlv::Error, Component::Error>::value,
44 "name::Component::Error must inherit from tlv::Error");
46 static const std::string&
49 static const std::string prefix{
"sha256digest="};
54 Component::ensureValid()
const 57 BOOST_THROW_EXCEPTION(Error(
"TLV-TYPE " +
to_string(
type()) +
" is not a valid NameComponent"));
60 BOOST_THROW_EXCEPTION(Error(
"ImplicitSha256DigestComponent TLV-LENGTH must be " +
106 BOOST_THROW_EXCEPTION(
Component::Error(
"Cannot convert to a ImplicitSha256DigestComponent " 107 "(invalid hex encoding)"));
115 size_t equalPos = input.find(
'=');
116 if (equalPos != std::string::npos) {
122 long parsedType = std::strtol(input.data(),
nullptr, 10);
125 to_string(parsedType).size() != equalPos) {
126 BOOST_THROW_EXCEPTION(
Error(
"Incorrect TLV-TYPE in NameComponent URI"));
128 type =
static_cast<uint32_t
>(parsedType);
129 input.erase(0, equalPos + 1);
133 if (
value.find_first_not_of(
'.') == std::string::npos) {
134 if (
value.size() < 3) {
135 BOOST_THROW_EXCEPTION(
Error(
"Illegal URI (name component cannot be . or ..)"));
165 std::ostringstream os;
223 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have nonNegativeInteger value"));
232 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have the requested marker " 233 "or the value is not a nonNegativeInteger"));
235 Buffer::const_iterator valueBegin =
value_begin() + 1;
284 valueLength += estimator.prependByteArray(&marker, 1);
285 size_t totalLength = valueLength;
286 totalLength += estimator.prependVarNumber(valueLength);
290 encoder.prependNonNegativeInteger(number);
291 encoder.prependByteArray(&marker, 1);
292 encoder.prependVarNumber(valueLength);
295 return encoder.block();
319 using namespace time;
320 uint64_t
value = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count();
349 BOOST_THROW_EXCEPTION(
Error(
"Cannot create ImplicitSha256DigestComponent (input digest must be " +
359 BOOST_THROW_EXCEPTION(
Error(
"Cannot create ImplicitSha256DigestComponent (input digest must be " +
387 int cmpType =
type() - other.
type();
404 size_t totalLength = 0;
409 bool isOverflow =
true;
411 for (; isOverflow && i > 0; i--) {
412 uint8_t newValue =
static_cast<uint8_t
>((
value()[i - 1] + 1) & 0xFF);
413 totalLength += encoder.prependByte(newValue);
414 isOverflow = (newValue == 0);
416 totalLength += encoder.prependByteArray(
value(), i);
420 totalLength += encoder.appendByte(0);
423 encoder.prependVarNumber(totalLength);
424 encoder.prependVarNumber(
type());
426 return encoder.block();
429 template<encoding::Tag TAG>
433 size_t totalLength = 0;
436 totalLength += encoder.prependVarNumber(
value_size());
437 totalLength += encoder.prependVarNumber(
type());
455 const_cast<Component&
>(*this) = buffer.block();
static Component fromNumber(uint64_t number)
Create a component encoded as nonNegativeInteger.
static Component fromSequenceNumber(uint64_t seqNo)
Create sequence number component using NDN naming conventions.
bool hasWire() const
Check if the Block has fully encoded wire.
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Convert *this by escaping characters according to the NDN URI Scheme.
bool isSequenceNumber() const
Check if the component is sequence number per NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
static const size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Component(uint32_t type=tlv::GenericNameComponent)
Construct a NameComponent of TLV-TYPE type, using empty TLV-VALUE.
bool isNumberWithMarker(uint8_t marker) const
Check if the component is NameComponentWithMarker per NDN naming conventions.
static Component fromTimestamp(const time::system_clock::TimePoint &timePoint)
Create sequence number component using NDN naming conventions.
static const std::string & getSha256DigestUriPrefix()
const uint8_t * wire() const
Get pointer to encoded wire.
Represents a TLV element of NDN packet format.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
const uint8_t * value() const
Get pointer to TLV-VALUE.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
element_iterator erase(element_const_iterator position)
Erase a sub element.
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
static Component fromSegmentOffset(uint64_t offset)
Create segment offset component using NDN naming conventions.
static const uint8_t SEGMENT_OFFSET_MARKER
Segment offset marker for NDN naming conventions.
bool isNumber() const
Check if the component is nonNegativeInteger.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
bool isSegmentOffset() const
Check if the component is segment offset per NDN naming conventions.
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
Component getSuccessor() const
a concept check for TLV abstraction with .wireEncode method
size_t size() const
Get size of encoded wire, including Type-Length-Value.
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, const Iterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
static Component parseSha256DigestUri(std::string input)
uint64_t toSegmentOffset() const
Interpret as segment offset component using NDN naming conventions.
std::string unescape(const std::string &str)
Decode a percent-encoded string.
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
std::string escape(const std::string &str)
Percent-encode a string.
bool equals(const Component &other) const
Check if this is the same component as other.
const Block & wireEncode() const
Encode to a wire format.
uint64_t toNumber() const
Interpret this name component as nonNegativeInteger.
static const uint8_t VERSION_MARKER
Version marker for NDN naming conventions.
Represents a name component.
static Component fromImplicitSha256Digest(const ConstBufferPtr &digest)
Create ImplicitSha256DigestComponent component.
void wireDecode(const Block &wire)
Decode from the wire format.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
static const uint8_t SEGMENT_MARKER
Segment marker for NDN naming conventions.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Component)
bool isTimestamp() const
Check if the component is timestamp per NDN naming conventions.
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
void printHex(std::ostream &os, uint64_t num, bool wantUpperCase)
Output the hex representation of num to the output stream os.
std::string to_string(const V &v)
static const uint8_t SEQUENCE_NUMBER_MARKER
Sequence number marker for NDN naming conventions.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
bool isGeneric() const
Check if the component is GenericComponent.
bool isVersion() const
Check if the component is version per NDN naming conventions.
time::system_clock::TimePoint toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
a concept check for TLV abstraction with .wireEncode method
Block()
Create an empty Block.
static const uint8_t TIMESTAMP_MARKER
Timestamp marker for NDN naming conventions.
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
EncodingImpl< EstimatorTag > EncodingEstimator
shared_ptr< const Buffer > ConstBufferPtr