27 #include "ndn-cxx/impl/name-component-types.hpp"
33 #include <boost/logic/tribool.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");
51 return g_conventionEncoding;
60 g_conventionEncoding = convention;
63 NDN_THROW(std::invalid_argument(
"Unknown naming convention"));
70 return g_conventionDecoding;
76 g_conventionDecoding = convention;
80 canDecodeMarkerConvention()
86 canDecodeTypedConvention()
94 static const auto wantAltEnv = []() -> boost::tribool {
95 const char* env = std::getenv(
"NDN_NAME_ALT_URI");
97 return boost::indeterminate;
98 else if (env[0] ==
'0')
100 else if (env[0] ==
'1')
103 return boost::indeterminate;
107 static const bool wantAlt = boost::indeterminate(wantAltEnv) ? false : bool(wantAltEnv);
111 static const bool wantAlt = boost::indeterminate(wantAltEnv) ? true : bool(wantAltEnv);
120 Component::ensureValid()
const
125 detail::getComponentTypeTable().get(
type()).check(*
this);
141 :
Block(type, std::move(buffer))
163 parseUriEscapedValue(uint32_t type,
const char* input,
size_t len)
165 std::ostringstream oss;
167 std::string value = oss.str();
168 if (value.find_first_not_of(
'.') == std::string::npos) {
169 if (value.size() < 3) {
172 return Component(type,
reinterpret_cast<const uint8_t*
>(value.data()), value.size() - 3);
174 return Component(type,
reinterpret_cast<const uint8_t*
>(value.data()), value.size());
180 size_t equalPos = input.
find(
'=');
181 if (equalPos == std::string::npos) {
185 auto typePrefix = input.substr(0, equalPos);
186 auto type = std::strtoul(typePrefix.data(),
nullptr, 10);
189 size_t valuePos = equalPos + 1;
190 return parseUriEscapedValue(
static_cast<uint32_t
>(
type),
191 input.data() + valuePos, input.size() - valuePos);
194 auto ct = detail::getComponentTypeTable().findByUriPrefix(typePrefix);
196 NDN_THROW(
Error(
"Unknown TLV-TYPE '" + typePrefix +
"' in NameComponent URI"));
198 return ct->parseAltUriValue(input.substr(equalPos + 1));
204 if (wantAltUri(format)) {
205 detail::getComponentTypeTable().get(
type()).writeUri(os, *
this);
208 detail::ComponentType().writeUri(os, *
this);
215 std::ostringstream os;
277 NDN_THROW(
Error(
"Name component does not have NonNegativeInteger value"));
286 NDN_THROW(
Error(
"Name component does not have the requested marker "
287 "or the value is not a NonNegativeInteger"));
370 valueLength += estimator.prependBytes({marker});
371 size_t totalLength = valueLength;
372 totalLength += estimator.prependVarNumber(valueLength);
376 encoder.prependNonNegativeInteger(number);
377 encoder.prependBytes({marker});
378 encoder.prependVarNumber(valueLength);
381 return encoder.block();
436 return detail::getComponentType1().match(*
this);
442 return detail::getComponentType1().create(std::move(digest));
448 return detail::getComponentType1().create(digest);
454 return detail::getComponentType2().match(*
this);
460 return detail::getComponentType2().create(std::move(digest));
466 return detail::getComponentType2().create(digest);
490 int cmpType =
type() - other.
type();
507 bool isOverflow =
false;
509 std::tie(isOverflow, successor) = detail::getComponentTypeTable().
get(
type()).getSuccessor(*
this);
515 auto value = detail::getComponentTypeTable().get(
type).getMinValue();
519 template<encoding::Tag TAG>
523 size_t totalLength = 0;
526 totalLength += encoder.prependVarNumber(
value_size());
527 totalLength += encoder.prependVarNumber(
type());
545 const_cast<Component&
>(*this) = buffer.block();
Represents a TLV element of the NDN packet format.
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
uint32_t type() const
Return the TLV-TYPE of the Block.
size_t size() const
Return the size of the encoded wire, i.e.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
a concept check for TLV abstraction with .wireDecode method and constructible from Block
a concept check for TLV abstraction with .wireEncode method
a concept check for TLV abstraction with .wireEncode method
constexpr size_t prependNonNegativeInteger(uint64_t n) const noexcept
Prepend n in NonNegativeInteger encoding.
Represents a name component.
uint64_t toSegment() const
Interpret as segment number component using NDN naming conventions.
static Component fromSegment(uint64_t segmentNo)
Create a segment number component using NDN naming conventions.
const Block & wireEncode() const
Encode to a wire format.
Component getSuccessor() const
Get the successor of this name component.
bool isGeneric() const
Check if the component is GenericNameComponent.
static Component fromEscapedString(const char *input, size_t beginOffset, size_t endOffset)
Decode NameComponent from a URI component.
static Component fromNumber(uint64_t number, uint32_t type=tlv::GenericNameComponent)
Create a component encoded as NonNegativeInteger.
static Component fromTimestamp(const time::system_clock::time_point &timePoint)
Create a timestamp component using NDN naming conventions.
bool isByteOffset() const
Check if the component is a byte offset per NDN naming conventions.
bool isSegment() const
Check if the component is a segment number per NDN naming conventions.
static Component fromParametersSha256Digest(ConstBufferPtr digest)
Create ParametersSha256DigestComponent component.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as NameComponentWithMarker.
bool isTimestamp() const
Check if the component is a timestamp per NDN naming conventions.
bool isParametersSha256Digest() const
Check if the component is ParametersSha256DigestComponent.
static Component fromSequenceNumber(uint64_t seqNo)
Create a sequence number component using NDN naming conventions.
uint64_t toByteOffset() const
Interpret as byte offset component using NDN naming conventions.
static Component fromVersion(uint64_t version)
Create a version component using NDN naming conventions.
uint64_t toSequenceNumber() const
Interpret as sequence number component using NDN naming conventions.
bool isSequenceNumber() const
Check if the component is a sequence number per NDN naming conventions.
Component(uint32_t type=tlv::GenericNameComponent)
Construct a NameComponent of TLV-TYPE type and with empty TLV-VALUE.
static Component fromByteOffset(uint64_t offset)
Create a byte offset component using NDN naming conventions.
bool isVersion() const
Check if the component is a version per NDN naming conventions.
bool isNumberWithMarker(uint8_t marker) const
Check if the component is a NameComponentWithMarker per NDN naming conventions rev1.
bool isNumber() const
Check if the component is a NonNegativeInteger.
static Component fromNumberWithMarker(uint8_t marker, uint64_t number)
Create a component encoded as NameComponentWithMarker.
void toUri(std::ostream &os, UriFormat format=UriFormat::DEFAULT) const
Write *this to the output stream, escaping characters according to the NDN URI format.
void wireDecode(const Block &wire)
Decode from the wire format.
time::system_clock::time_point toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
static Component fromImplicitSha256Digest(ConstBufferPtr digest)
Create ImplicitSha256DigestComponent component.
bool equals(const Component &other) const
Check if this is the same component as other.
int compare(const Component &other) const
Compare this to the other Component using NDN canonical ordering.
uint64_t toNumber() const
Interpret this name component as NonNegativeInteger.
bool isImplicitSha256Digest() const
Check if the component is ImplicitSha256DigestComponent.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
boost::chrono::time_point< system_clock > time_point
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
EncodingImpl< EstimatorTag > EncodingEstimator
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Block makeNonNegativeIntegerBlock(uint32_t type, uint64_t value)
Create a TLV block containing a non-negative integer.
Block makeBinaryBlock(uint32_t type, span< const uint8_t > value)
Create a TLV block copying the TLV-VALUE from a byte range.
EncodingImpl< EncoderTag > EncodingBuffer
Block makeStringBlock(uint32_t type, const std::string &value)
Create a TLV block containing a string.
std::string to_string(const errinfo_stacktrace &x)
Convention
Identify a style of NDN Naming Conventions.
@ MARKER
Component markers (revision 1)
@ TYPED
Typed name components (revision 3)
void setConventionDecoding(Convention convention)
Set which Naming Conventions style(s) to accept while decoding.
Convention getConventionEncoding()
Return which Naming Conventions style to use while encoding.
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
Convention getConventionDecoding()
Return which Naming Conventions style(s) to accept while decoding.
UriFormat
Format used for the URI representation of a name.
@ ALTERNATE
Always prefer the alternate format when available.
@ ENV_OR_ALTERNATE
Same as UriFormat::ALTERNATE, unless NDN_NAME_ALT_URI environment variable is set to '0'.
@ ENV_OR_CANONICAL
Same as UriFormat::CANONICAL, unless NDN_NAME_ALT_URI environment variable is set to '1'.
const system_clock::time_point & getUnixEpoch()
Return a system_clock::time_point representing the UNIX time epoch, i.e., 00:00:00 UTC on 1 January 1...
boost::chrono::microseconds microseconds
@ ByteOffsetNameComponent
@ SequenceNumNameComponent
uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read a NonNegativeInteger in NDN-TLV encoding.
shared_ptr< const Buffer > ConstBufferPtr
std::string unescape(const std::string &str)
Decode a percent-encoded string.
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept