34 #include <boost/lexical_cast.hpp> 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="};
62 BOOST_THROW_EXCEPTION(
Error(
"Cannot construct name::Component from not a NameComponent " 63 "or ImplicitSha256DigestComponent TLV wire block"));
95 std::string trimmedString(escapedString + beginOffset, escapedString + endOffset);
101 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert to ImplicitSha256DigestComponent" 102 "(expected sha256 in hex encoding)"));
109 BOOST_THROW_EXCEPTION(
Error(
"Cannot convert to a ImplicitSha256DigestComponent (invalid hex " 116 if (value.find_first_not_of(
".") == std::string::npos) {
118 if (value.size() <= 2)
120 BOOST_THROW_EXCEPTION(
Error(
"Illegal URI (name component cannot be . or ..)"));
123 return Component(reinterpret_cast<const uint8_t*>(&value[3]), value.size() - 3);
126 return Component(reinterpret_cast<const uint8_t*>(&value[0]), value.size());
143 bool gotNonDot =
false;
144 for (
size_t i = 0; i < valueSize; ++i) {
145 if (value[i] != 0x2e) {
153 for (
size_t i = 0; i < valueSize; ++i)
158 std::ios::fmtflags saveFlags = result.flags(std::ios::hex | std::ios::uppercase);
160 for (
size_t i = 0; i < valueSize; ++i) {
161 uint8_t x = value[i];
163 if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) ||
164 (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d ||
165 x == 0x2e || x == 0x5f)
171 result << static_cast<uint32_t>(x);
176 result.flags(saveFlags);
184 std::ostringstream os;
242 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have nonNegativeInteger value"));
251 BOOST_THROW_EXCEPTION(
Error(
"Name component does not have the requested marker " 252 "or the value is not a nonNegativeInteger"));
254 Buffer::const_iterator valueBegin =
value_begin() + 1;
303 valueLength += estimator.prependByteArray(&marker, 1);
304 size_t totalLength = valueLength;
305 totalLength += estimator.prependVarNumber(valueLength);
309 encoder.prependNonNegativeInteger(number);
310 encoder.prependByteArray(&marker, 1);
311 encoder.prependVarNumber(valueLength);
314 return encoder.block();
338 using namespace time;
339 uint64_t
value = duration_cast<microseconds>(timePoint -
getUnixEpoch()).count();
368 BOOST_THROW_EXCEPTION(
Error(
"Cannot create ImplicitSha256DigestComponent (input digest must be " +
378 BOOST_THROW_EXCEPTION(
Error(
"Cannot create ImplicitSha256DigestComponent (input digest must be " 409 size_t totalLength = 0;
414 bool isOverflow =
true;
416 for (; isOverflow && i > 0; i--) {
417 uint8_t newValue =
static_cast<uint8_t
>((
value()[i - 1] + 1) & 0xFF);
418 totalLength += encoder.prependByte(newValue);
419 isOverflow = (newValue == 0);
421 totalLength += encoder.prependByteArray(
value(), i);
425 totalLength += encoder.appendByte(0);
428 encoder.prependVarNumber(totalLength);
429 encoder.prependVarNumber(
type());
431 return encoder.block();
435 template<encoding::Tag TAG>
439 size_t totalLength = 0;
442 totalLength += encoder.prependVarNumber(
value_size());
443 totalLength += encoder.prependVarNumber(
type());
448 Component::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder)
const;
451 Component::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder)
const;
465 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.
EncodingImpl< EstimatorTag > EncodingEstimator
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 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()
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.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block type type containing non-negative integer value.
Buffer::const_iterator value_begin() const
Buffer::const_iterator value_end() const
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.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
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.
bool isVersion() const
Check if the component is version per NDN naming conventions.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
EncodingImpl< EncoderTag > EncodingBuffer
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 type type with value from a buffer value of size length.
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 .
std::string unescape(const std::string &str)
Decode a percent-encoded string.
uint64_t readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
size_t value_size() const
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
bool hasWire() const
Check if the Block has fully encoded wire.
void trim(std::string &str)
Modify str in place to erase whitespace on the left and right.
const uint8_t * value() const
static Component fromVersion(uint64_t version)
Create version component using NDN naming conventions.
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.
shared_ptr< const Buffer > ConstBufferPtr
shared_ptr< const Buffer > fromHex(const std::string &hexString)
Convert the hex string to buffer.
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 ...
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) ...
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
Error that can be thrown from name::Component.