33 #include <boost/lexical_cast.hpp> 38 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Component>));
42 static_assert(std::is_base_of<tlv::Error, Component::Error>::value,
43 "name::Component::Error must inherit from tlv::Error");
45 static const std::string&
48 static const std::string prefix{
"sha256digest="};
61 BOOST_THROW_EXCEPTION(
Error(
"Cannot construct name::Component from not a NameComponent " 62 "or ImplicitSha256DigestComponent TLV wire block"));
94 std::string trimmedString(escapedString + beginOffset, escapedString + endOffset);
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 std::ios::fmtflags saveFlags = 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(saveFlags);
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();
448 template<encoding::Tag TAG>
452 size_t totalLength = 0;
455 totalLength += encoder.prependVarNumber(
value_size());
456 totalLength += encoder.prependVarNumber(
type());
478 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 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...
EncodingImpl< EstimatorTag > EncodingEstimator
size_t value_size() const
bool isNumberWithMarker(uint8_t marker) const
Check if the component is NameComponentWithMarker per NDN naming conventions.
static const size_t SHA256_DIGEST_SIZE
number of octets in a SHA256 digest
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
Class representing a wire element of NDN-TLV packet format.
void printHex(std::ostream &os, const uint8_t *buffer, size_t length, bool isUpperCase)
Output the hex representation of the bytes in array to the output stream os.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
const uint8_t * value() const
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block type type containing non-negative integer value.
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
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
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
EncodingImpl< EncoderTag > EncodingBuffer
Block makeBinaryBlock(uint32_t type, const uint8_t *value, size_t length)
Create a TLV block type type with value from a buffer value of size length.
bool isSegment() const
Check if the component is segment number per NDN naming conventions.
Component()
Create a new name::Component with an empty value.
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block type type with value from a string value.
Buffer::const_iterator value_end() const
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.
uint64_t readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
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.
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.
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.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
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.
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.
shared_ptr< const Buffer > ConstBufferPtr
shared_ptr< const Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
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 ...
Class representing a general-use automatically managed/resized buffer.
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
const system_clock::TimePoint & getUnixEpoch()
Get system_clock::TimePoint representing UNIX time epoch (00:00:00 on Jan 1, 1970) ...
Error that can be thrown from name::Component.