22 #ifndef NDN_ENCODING_TLV_HPP
23 #define NDN_ENCODING_TLV_HPP
50 class Error :
public std::runtime_error
55 :
std::runtime_error(what)
152 template<
class InputIterator>
154 readVarNumber(InputIterator& begin,
const InputIterator& end, uint64_t& number);
168 template<
class InputIterator>
170 readType(InputIterator& begin,
const InputIterator& end, uint32_t& type);
180 template<
class InputIterator>
182 readVarNumber(InputIterator& begin,
const InputIterator& end);
192 template<
class InputIterator>
194 readType(InputIterator& begin,
const InputIterator& end);
218 template<
class InputIterator>
244 template<
class InputIterator>
246 readVarNumber(InputIterator& begin,
const InputIterator& end, uint64_t& number)
251 uint8_t firstOctet = *begin;
253 if (firstOctet < 253)
257 else if (firstOctet == 253)
262 uint16_t value = *
reinterpret_cast<const uint16_t*
>(&*begin);
264 number = be16toh(value);
266 else if (firstOctet == 254)
271 uint32_t value = *
reinterpret_cast<const uint32_t*
>(&*begin);
273 number = be32toh(value);
280 uint64_t value = *
reinterpret_cast<const uint64_t*
>(&*begin);
283 number = be64toh(value);
289 template<
class InputIterator>
291 readType(InputIterator& begin,
const InputIterator& end, uint32_t& type)
295 if (!isOk || number > std::numeric_limits<uint32_t>::max())
300 type =
static_cast<uint32_t
>(number);
304 template<
class InputIterator>
309 BOOST_THROW_EXCEPTION(
Error(
"Empty buffer during TLV processing"));
314 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
321 readVarNumber<std::istream_iterator<uint8_t>>(std::istream_iterator<uint8_t>& begin,
322 const std::istream_iterator<uint8_t>& end,
328 uint8_t firstOctet = *begin;
330 if (firstOctet < 253)
334 else if (firstOctet == 253)
338 for (; begin != end && count < 2; ++count)
340 value = ((value << 8) | *begin);
347 else if (firstOctet == 254)
351 for (; begin != end && count < 4; ++count)
353 value = ((value << 8) | *begin);
364 for (; begin != end && count < 8; ++count)
366 value = ((value << 8) | *begin);
377 template<
class InputIterator>
379 readType(InputIterator& begin,
const InputIterator& end)
382 if (type > std::numeric_limits<uint32_t>::max())
384 BOOST_THROW_EXCEPTION(
Error(
"TLV type code exceeds allowed maximum"));
387 return static_cast<uint32_t
>(type);
393 if (varNumber < 253) {
396 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
399 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
410 if (varNumber < 253) {
411 os.put(static_cast<char>(varNumber));
414 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
415 os.put(static_cast<char>(253));
416 uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
417 os.write(reinterpret_cast<const char*>(&value), 2);
420 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
421 os.put(static_cast<char>(254));
422 uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
423 os.write(reinterpret_cast<const char*>(&value), 4);
427 os.put(static_cast<char>(255));
428 uint64_t value = htobe64(varNumber);
429 os.write(reinterpret_cast<const char*>(&value), 8);
434 template<
class InputIterator>
442 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
444 uint8_t value = *begin;
451 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
453 uint16_t value = *
reinterpret_cast<const uint16_t*
>(&*begin);
455 return be16toh(value);
460 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
462 uint32_t value = *
reinterpret_cast<const uint32_t*
>(&*begin);
464 return be32toh(value);
469 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
471 uint64_t value = *
reinterpret_cast<const uint64_t*
>(&*begin);
473 return be64toh(value);
476 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"));
481 readNonNegativeInteger<std::istream_iterator<uint8_t> >(
size_t size,
482 std::istream_iterator<uint8_t>& begin,
483 const std::istream_iterator<uint8_t>& end)
489 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
491 uint64_t value = *begin;
499 for (; begin != end && count < 2; ++count)
501 value = ((value << 8) | *begin);
506 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
514 for (; begin != end && count < 4; ++count)
516 value = ((value << 8) | *begin);
521 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
529 for (; begin != end && count < 8; ++count)
531 value = ((value << 8) | *begin);
536 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
541 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"));
547 if (varNumber < 253) {
550 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
553 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
565 if (varNumber < 253) {
566 os.put(static_cast<char>(varNumber));
569 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
570 uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
571 os.write(reinterpret_cast<const char*>(&value), 2);
574 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
575 uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
576 os.write(reinterpret_cast<const char*>(&value), 4);
580 uint64_t value = htobe64(varNumber);
581 os.write(reinterpret_cast<const char*>(&value), 8);
590 #endif // NDN_ENCODING_TLV_HPP
Represent a SHA256-with-RSA signature.
Copyright (c) 2013-2016 Regents of the University of California.
Error(const std::string &what)
indicates a producer generated NACK
Represent a SHA256 digest.
represents an Interest packet
bool readType(InputIterator &begin, const InputIterator &end, uint32_t &type)
Read TLV Type.
size_t sizeOfNonNegativeInteger(uint64_t varNumber)
Get number of bytes necessary to hold value of nonNegativeInteger.
size_t writeVarNumber(std::ostream &os, uint64_t varNumber)
Write VAR-NUMBER to the specified stream.
bool readVarNumber(InputIterator &begin, const InputIterator &end, uint64_t &number)
Read VAR-NUMBER in NDN-TLV encoding.
size_t writeNonNegativeInteger(std::ostream &os, uint64_t varNumber)
Write nonNegativeInteger to the specified stream.
represents a Sha256WithEcdsa signature.
Abstraction implementing Interest selectors.
uint64_t readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
Name abstraction to represent an absolute name.
ContentTypeValue
indicates a possible value of ContentType field
indicates content is the actual data bits
indicates content is a public key
indicates content is another name which identifies actual data content
std::ostream & operator<<(std::ostream &os, const SignatureTypeValue &signatureType)
represents an error in TLV encoding or decoding
size_t sizeOfVarNumber(uint64_t varNumber)
Get number of bytes necessary to hold value of VAR-NUMBER.
Represents Exclude selector in NDN Interest.
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size