22 #ifndef NDN_ENCODING_TLV_HPP 23 #define NDN_ENCODING_TLV_HPP 50 class Error :
public std::runtime_error
55 :
std::runtime_error(what)
149 template<
class InputIterator>
151 readVarNumber(InputIterator& begin,
const InputIterator& end, uint64_t& number);
165 template<
class InputIterator>
167 readType(InputIterator& begin,
const InputIterator& end, uint32_t& type);
177 template<
class InputIterator>
179 readVarNumber(InputIterator& begin,
const InputIterator& end);
189 template<
class InputIterator>
191 readType(InputIterator& begin,
const InputIterator& end);
215 template<
class InputIterator>
241 template<
class InputIterator>
243 readVarNumber(InputIterator& begin,
const InputIterator& end, uint64_t& number)
248 uint8_t firstOctet = *begin;
250 if (firstOctet < 253)
254 else if (firstOctet == 253)
259 uint16_t value = *
reinterpret_cast<const uint16_t*
>(&*begin);
261 number = be16toh(value);
263 else if (firstOctet == 254)
268 uint32_t value = *
reinterpret_cast<const uint32_t*
>(&*begin);
270 number = be32toh(value);
277 uint64_t value = *
reinterpret_cast<const uint64_t*
>(&*begin);
280 number = be64toh(value);
286 template<
class InputIterator>
288 readType(InputIterator& begin,
const InputIterator& end, uint32_t& type)
292 if (!isOk || number > std::numeric_limits<uint32_t>::max())
297 type =
static_cast<uint32_t
>(number);
301 template<
class InputIterator>
306 BOOST_THROW_EXCEPTION(
Error(
"Empty buffer during TLV processing"));
311 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
318 readVarNumber<std::istream_iterator<uint8_t>>(std::istream_iterator<uint8_t>& begin,
319 const std::istream_iterator<uint8_t>& end,
325 uint8_t firstOctet = *begin;
327 if (firstOctet < 253)
331 else if (firstOctet == 253)
335 for (; begin != end && count < 2; ++count)
337 value = ((value << 8) | *begin);
344 else if (firstOctet == 254)
348 for (; begin != end && count < 4; ++count)
350 value = ((value << 8) | *begin);
361 for (; begin != end && count < 8; ++count)
363 value = ((value << 8) | *begin);
374 template<
class InputIterator>
376 readType(InputIterator& begin,
const InputIterator& end)
379 if (type > std::numeric_limits<uint32_t>::max())
381 BOOST_THROW_EXCEPTION(
Error(
"TLV type code exceeds allowed maximum"));
384 return static_cast<uint32_t
>(type);
390 if (varNumber < 253) {
393 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
396 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
407 if (varNumber < 253) {
408 os.put(static_cast<char>(varNumber));
411 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
412 os.put(static_cast<char>(253));
413 uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
414 os.write(reinterpret_cast<const char*>(&value), 2);
417 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
418 os.put(static_cast<char>(254));
419 uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
420 os.write(reinterpret_cast<const char*>(&value), 4);
424 os.put(static_cast<char>(255));
425 uint64_t value = htobe64(varNumber);
426 os.write(reinterpret_cast<const char*>(&value), 8);
431 template<
class InputIterator>
439 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
441 uint8_t value = *begin;
448 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
450 uint16_t value = *
reinterpret_cast<const uint16_t*
>(&*begin);
452 return be16toh(value);
457 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
459 uint32_t value = *
reinterpret_cast<const uint32_t*
>(&*begin);
461 return be32toh(value);
466 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
468 uint64_t value = *
reinterpret_cast<const uint64_t*
>(&*begin);
470 return be64toh(value);
473 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"));
478 readNonNegativeInteger<std::istream_iterator<uint8_t> >(
size_t size,
479 std::istream_iterator<uint8_t>& begin,
480 const std::istream_iterator<uint8_t>& end)
486 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
488 uint64_t value = *begin;
496 for (; begin != end && count < 2; ++count)
498 value = ((value << 8) | *begin);
503 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
511 for (; begin != end && count < 4; ++count)
513 value = ((value << 8) | *begin);
518 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
526 for (; begin != end && count < 8; ++count)
528 value = ((value << 8) | *begin);
533 BOOST_THROW_EXCEPTION(
Error(
"Insufficient data during TLV processing"));
538 BOOST_THROW_EXCEPTION(
Error(
"Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)"));
544 if (varNumber < 253) {
547 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
550 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
562 if (varNumber < 253) {
563 os.put(static_cast<char>(varNumber));
566 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
567 uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
568 os.write(reinterpret_cast<const char*>(&value), 2);
571 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
572 uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
573 os.write(reinterpret_cast<const char*>(&value), 4);
577 uint64_t value = htobe64(varNumber);
578 os.write(reinterpret_cast<const char*>(&value), 8);
587 #endif // NDN_ENCODING_TLV_HPP
Represent a SHA256-with-RSA signature.
Copyright (c) 2011-2015 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
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