27 #include "ndn-cxx/impl/name-component-types.hpp"
33 #include <boost/logic/tribool.hpp>
43 return g_conventionEncoding;
52 g_conventionEncoding = convention;
55 NDN_THROW(std::invalid_argument(
"Unknown naming convention"));
62 return g_conventionDecoding;
68 g_conventionDecoding = convention;
72 canDecodeMarkerConvention() noexcept
78 canDecodeTypedConvention() noexcept
98 :
Block(type, std::move(buffer))
115 Component::ensureValid()
const
120 getComponentTypeTable().get(
type()).check(*
this);
124 parseUriEscapedValue(uint32_t type, std::string_view input)
126 std::ostringstream oss;
128 std::string value = oss.str();
129 if (value.find_first_not_of(
'.') == std::string::npos) {
130 if (value.size() < 3) {
131 NDN_THROW(Component::Error(
"Illegal URI (name component cannot be . or ..)"));
133 return Component(type, {
reinterpret_cast<const uint8_t*
>(value.data()), value.size() - 3});
135 return Component(type, {
reinterpret_cast<const uint8_t*
>(value.data()), value.size()});
141 size_t equalPos = input.
find(
'=');
142 if (equalPos == std::string_view::npos) {
146 auto typePrefix = input.substr(0, equalPos);
147 auto type = std::strtoul(typePrefix.data(),
nullptr, 10);
150 return parseUriEscapedValue(
static_cast<uint32_t
>(
type), input.substr(equalPos + 1));
153 auto ct = getComponentTypeTable().findByUriPrefix(typePrefix);
155 NDN_THROW(
Error(
"Unknown TLV-TYPE '" + std::string(typePrefix) +
"' in NameComponent URI"));
157 return ct->parseAltUriValue(input.substr(equalPos + 1));
163 static const auto wantAltEnv = []() -> boost::tribool {
164 const char* env = std::getenv(
"NDN_NAME_ALT_URI");
166 return boost::indeterminate;
167 else if (env[0] ==
'0')
169 else if (env[0] ==
'1')
172 return boost::indeterminate;
176 static const bool wantAlt = boost::indeterminate(wantAltEnv) ? false : bool(wantAltEnv);
180 static const bool wantAlt = boost::indeterminate(wantAltEnv) ? true : bool(wantAltEnv);
191 if (wantAltUri(format)) {
192 getComponentTypeTable().get(
type()).writeUri(os, *
this);
195 ComponentType().writeUri(os, *
this);
202 std::ostringstream os;
219 return (value_size() == 2 || value_size() == 3 ||
220 value_size() == 5 || value_size() == 9) && value()[0] == marker;
264 NDN_THROW(
Error(
"Name component does not have NonNegativeInteger value"));
273 NDN_THROW(
Error(
"Name component does not have the requested marker "
274 "or the value is not a NonNegativeInteger"));
357 valueLength += estimator.prependBytes({marker});
358 size_t totalLength = valueLength;
359 totalLength += estimator.prependVarNumber(valueLength);
363 encoder.prependNonNegativeInteger(number);
364 encoder.prependBytes({marker});
365 encoder.prependVarNumber(valueLength);
439 int cmpType =
type() - other.
type();
456 auto [isOverflow, successor] = getComponentTypeTable().
get(
type()).getSuccessor(*
this);
462 auto value = getComponentTypeTable().get(
type).getMinValue();
466 template<encoding::Tag TAG>
470 size_t totalLength = 0;
472 totalLength += encoder.prependBytes(
value_bytes());
474 totalLength += encoder.prependVarNumber(
value_size());
475 totalLength += encoder.prependVarNumber(
type());
Represents a TLV element of the NDN packet format.
const uint8_t * data() const
Returns a raw pointer to the beginning of the encoded wire, i.e., the whole TLV.
const_iterator value_end() const noexcept
Get end iterator of TLV-VALUE.
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
size_t size() const
Returns the size of the encoded wire, i.e., of the whole TLV.
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
span< const uint8_t > value_bytes() const noexcept
Return a read-only view of TLV-VALUE as a contiguous range of bytes.
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
const_iterator value_begin() const noexcept
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, i.e., the TLV-LENGTH.
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
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.
bool isTimestamp() const noexcept
Check if the component is a timestamp per NDN naming conventions.
const Block & wireEncode() const
Encode to TLV wire format.
Component getSuccessor() const
Get the successor of this name 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 isImplicitSha256Digest() const noexcept
Check if the component is an ImplicitSha256DigestComponent.
static Component fromUri(std::string_view input)
Construct a NameComponent from its string representation in NDN URI format.
bool isSegment() const noexcept
Check if the component is a segment number per NDN naming conventions.
uint64_t toNumberWithMarker(uint8_t marker) const
Interpret this name component as a NameComponentWithMarker.
bool isParametersSha256Digest() const noexcept
Check if the component is a 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 isByteOffset() const noexcept
Check if the component is a byte offset 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 isNumberWithMarker(uint8_t marker) const noexcept
Check if the component is a NameComponentWithMarker per NDN naming conventions rev1.
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 TLV wire format.
bool empty() const noexcept
time::system_clock::time_point toTimestamp() const
Interpret as timestamp component using NDN naming conventions.
bool isVersion() const noexcept
Check if the component is a version per NDN naming conventions.
bool isSequenceNumber() const noexcept
Check if the component is a sequence number per NDN naming conventions.
bool isNumber() const noexcept
Check if the component is a NonNegativeInteger.
int compare(const Component &other) const
Compare this component to other using NDN canonical ordering.
uint64_t toNumber() const
Interpret this name component as a NonNegativeInteger.
uint64_t toVersion() const
Interpret as version component using NDN naming conventions.
::boost::chrono::time_point< system_clock > time_point
static constexpr size_t DIGEST_SIZE
Length in bytes of a SHA-256 digest.
#define NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ClassName)
EncodingImpl< EstimatorTag > EncodingEstimator
Block makeStringBlock(uint32_t type, std::string_view value)
Create a TLV block containing a string.
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
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() noexcept
Return which Naming Conventions style to use while encoding.
void setConventionEncoding(Convention convention)
Set which Naming Conventions style to use while encoding.
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'.
Convention getConventionDecoding() noexcept
Return which Naming Conventions style(s) to accept while decoding.
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
@ ParametersSha256DigestComponent
@ ImplicitSha256DigestComponent
constexpr uint64_t readNonNegativeInteger(size_t size, Iterator &begin, Iterator end)
Read a NonNegativeInteger in NDN-TLV encoding.
std::string unescape(std::string_view str)
Decode a percent-encoded string.
std::shared_ptr< const Buffer > ConstBufferPtr
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept