33 #include <boost/algorithm/string/trim.hpp> 39 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
44 "name::Component::Error must inherit from tlv::Error");
46 static const std::string&
49 static const std::string prefix{
"sha256digest="};
62 BOOST_THROW_EXCEPTION(
Error(
"Cannot construct name::Component from not a NameComponent " 63 "or ImplicitSha256DigestComponent TLV wire block"));
94 std::string trimmedString(escapedString + beginOffset, escapedString + endOffset);
95 boost::algorithm::trim(trimmedString);
100 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert to ImplicitSha256DigestComponent" 101 "(expected sha256 in hex encoding)"));
108 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert to a ImplicitSha256DigestComponent (invalid hex " 115 if (value.find_first_not_of(
".") == std::string::npos) {
117 if (value.size() <= 2)
119 BOOST_THROW_EXCEPTION(
Error(
"Illegal URI (name component cannot be . or ..)"));
122 return Component(reinterpret_cast<const uint8_t*>(&value[3]), value.size() - 3);
125 return Component(reinterpret_cast<const uint8_t*>(&value[0]), value.size());
142 bool gotNonDot =
false;
143 for (
size_t i = 0; i < valueSize; ++i) {
144 if (value[i] != 0x2e) {
152 for (
size_t i = 0; i < valueSize; ++i)
157 auto savedFlags = result.flags(std::ios::hex | std::ios::uppercase);
159 for (
size_t i = 0; i < valueSize; ++i) {
160 uint8_t x = value[i];
162 if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) ||
163 (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d ||
164 x == 0x2e || x == 0x5f)
170 result << static_cast<uint32_t>(x);
175 result.flags(savedFlags);
183 std::ostringstream os;
241 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have nonNegativeInteger value"));
250 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have the requested marker " 251 "or the value is not a nonNegativeInteger"));
253 Buffer::const_iterator valueBegin =
value_begin() + 1;
302 valueLength += estimator.prependByteArray(&marker, 1);
303 size_t totalLength = valueLength;
304 totalLength += estimator.prependVarNumber(valueLength);
308 encoder.prependNonNegativeInteger(number);
309 encoder.prependByteArray(&marker, 1);
310 encoder.prependVarNumber(valueLength);
313 return encoder.block();
337 using namespace time;
338 uint64_t
value = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count();
367 BOOST_THROW_EXCEPTION(
Error(
"Cannot create ImplicitSha256DigestComponent (input digest must be " +
377 BOOST_THROW_EXCEPTION(
Error(
"Cannot create ImplicitSha256DigestComponent (input digest must be " +
405 int cmpType =
type() - other.
type();
422 size_t totalLength = 0;
427 bool isOverflow =
true;
429 for (; isOverflow && i > 0; i--) {
430 uint8_t newValue =
static_cast<uint8_t
>((
value()[i - 1] + 1) & 0xFF);
431 totalLength += encoder.prependByte(newValue);
432 isOverflow = (newValue == 0);
434 totalLength += encoder.prependByteArray(
value(), i);
438 totalLength += encoder.appendByte(0);
441 encoder.prependVarNumber(totalLength);
442 encoder.prependVarNumber(
type());
444 return encoder.block();
447 template<encoding::Tag TAG>
451 size_t totalLength = 0;
454 totalLength += encoder.prependVarNumber(
value_size());
455 totalLength += encoder.prependVarNumber(
type());
473 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 isGeneric() const
Check if the component is GenericComponent.
Copyright (c) 2011-2015 Regents of the University of California.
uint64_t toSegmentOffset() const
Interpret as segment offset component using NDN naming conventions.
bool isTimestamp() const
Check if the component is timestamp per NDN naming conventions.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
static Component fromEscapedString(const char *escapedString, size_t beginOffset, size_t endOffset)
Create name::Component by decoding the escapedString between beginOffset and endOffset according to t...
const Block & wireEncode() const
Encode to a wire format.
std::string toUri() const
Convert *this by escaping characters according to the NDN URI Scheme.
static const size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
time::system_clock::TimePoint toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
bool isNumberWithMarker(uint8_t marker) const
Check if the component is NameComponentWithMarker per NDN naming conventions.
bool isSequenceNumber() const
Check if the component is sequence number 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()
Represents a TLV element of NDN packet format.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
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.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
size_t size() const
Get size of encoded wire, including Type-Length-Value.
static Component fromSegment(uint64_t segmentNo)
Create segment number component using NDN naming conventions.
a concept check for TLV abstraction with .wireEncode method
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, const Iterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
bool isVersion() const
Check if the component is version per NDN naming conventions.
shared_ptr< Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
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.
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block copying TLV-VALUE from raw buffer.
Component()
Create a new name::Component with an empty value.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
std::string unescape(const std::string &str)
Decode a percent-encoded string.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
uint32_t type() const
Get TLV-TYPE.
Component getSuccessor() const
static const uint8_t VERSION_MARKER
Version marker for NDN naming conventions.
Component holds a read-only name component value.
static Component fromImplicitSha256Digest(const ConstBufferPtr &digest)
Create ImplicitSha256DigestComponent component.
void wireDecode(const Block &wire)
Decode from the wire format.
static const uint8_t SEGMENT_MARKER
Segment marker for NDN naming conventions.
bool isNumber() const
Check if the component is nonNegativeInteger.
const uint8_t * wire() const
Get pointer to encoded wire.
bool hasWire() const
Check if the Block has fully encoded wire.
const uint8_t * value() const
Get pointer to TLV-VALUE.
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(Component)
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)
bool isSegmentOffset() const
Check if the component is segment offset per NDN naming conventions.
static const uint8_t SEQUENCE_NUMBER_MARKER
Sequence number marker for NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
bool equals(const Component &other) const
Check if this is the same component as other.
a concept check for TLV abstraction with .wireEncode method
Block()
Create an empty Block.
uint64_t toNumber() const
Interpret this name component as nonNegativeInteger.
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 ...
General-purpose automatically managed/resized buffer.
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
EncodingImpl< EncoderTag > EncodingBuffer
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
EncodingImpl< EstimatorTag > EncodingEstimator
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
Error that can be thrown from name::Component.
shared_ptr< const Buffer > ConstBufferPtr