25 class Error :
public std::runtime_error
30 : std::runtime_error(what)
88 template<
class InputIterator>
90 readVarNumber(InputIterator& begin,
const InputIterator& end, uint64_t& number);
104 template<
class InputIterator>
106 readType(InputIterator& begin,
const InputIterator& end, uint32_t& type);
116 template<
class InputIterator>
118 readVarNumber(InputIterator& begin,
const InputIterator& end);
128 template<
class InputIterator>
130 readType(InputIterator& begin,
const InputIterator& end);
154 template<
class InputIterator>
180 template<
class InputIterator>
182 readVarNumber(InputIterator& begin,
const InputIterator& end, uint64_t& number)
187 uint8_t firstOctet = *begin;
189 if (firstOctet < 253)
193 else if (firstOctet == 253)
198 uint16_t value = *
reinterpret_cast<const uint16_t*
>(&*begin);
200 number = be16toh(value);
202 else if (firstOctet == 254)
207 uint32_t value = *
reinterpret_cast<const uint32_t*
>(&*begin);
209 number = be32toh(value);
216 uint64_t value = *
reinterpret_cast<const uint64_t*
>(&*begin);
219 number = be64toh(value);
225 template<
class InputIterator>
227 readType(InputIterator& begin,
const InputIterator& end, uint32_t& type)
231 if (!isOk || number > std::numeric_limits<uint32_t>::max())
236 type =
static_cast<uint64_t
>(number);
240 template<
class InputIterator>
245 throw Error(
"Empty buffer during TLV processing");
250 throw Error(
"Insufficient data during TLV processing");
257 readVarNumber<std::istream_iterator<uint8_t> >(std::istream_iterator<uint8_t>& begin,
258 const std::istream_iterator<uint8_t>& end,
264 uint8_t firstOctet = *begin;
266 if (firstOctet < 253)
270 else if (firstOctet == 253)
274 for (; begin != end && count < 2; ++count)
276 value = ((value << 8) | *begin);
283 else if (firstOctet == 254)
287 for (; begin != end && count < 4; ++count)
289 value = ((value << 8) | *begin);
300 for (; begin != end && count < 8; ++count)
302 value = ((value << 8) | *begin);
313 template<
class InputIterator>
315 readType(InputIterator& begin,
const InputIterator& end)
318 if (type > std::numeric_limits<uint32_t>::max())
320 throw Error(
"TLV type code exceeds allowed maximum");
323 return static_cast<uint32_t
>(type);
329 if (varNumber < 253) {
332 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
335 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
346 if (varNumber < 253) {
347 os.put(static_cast<char>(varNumber));
350 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
351 os.put(static_cast<char>(253));
352 uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
353 os.write(reinterpret_cast<const char*>(&value), 2);
356 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
357 os.put(static_cast<char>(254));
358 uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
359 os.write(reinterpret_cast<const char*>(&value), 4);
363 os.put(static_cast<char>(255));
364 uint64_t value = htobe64(varNumber);
365 os.write(reinterpret_cast<const char*>(&value), 8);
370 template<
class InputIterator>
378 throw Error(
"Insufficient data during TLV processing");
380 uint8_t value = *begin;
387 throw Error(
"Insufficient data during TLV processing");
389 uint16_t value = *
reinterpret_cast<const uint16_t*
>(&*begin);
391 return be16toh(value);
396 throw Error(
"Insufficient data during TLV processing");
398 uint32_t value = *
reinterpret_cast<const uint32_t*
>(&*begin);
400 return be32toh(value);
405 throw Error(
"Insufficient data during TLV processing");
407 uint64_t value = *
reinterpret_cast<const uint64_t*
>(&*begin);
409 return be64toh(value);
412 throw Error(
"Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)");
417 readNonNegativeInteger<std::istream_iterator<uint8_t> >(
size_t size,
418 std::istream_iterator<uint8_t>& begin,
419 const std::istream_iterator<uint8_t>& end)
425 throw Error(
"Insufficient data during TLV processing");
427 uint64_t value = *begin;
435 for (; begin != end && count < 2; ++count)
437 value = ((value << 8) | *begin);
442 throw Error(
"Insufficient data during TLV processing");
450 for (; begin != end && count < 4; ++count)
452 value = ((value << 8) | *begin);
457 throw Error(
"Insufficient data during TLV processing");
465 for (; begin != end && count < 8; ++count)
467 value = ((value << 8) | *begin);
472 throw Error(
"Insufficient data during TLV processing");
477 throw Error(
"Invalid length for nonNegativeInteger (only 1, 2, 4, and 8 are allowed)");
483 if (varNumber < 253) {
486 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
489 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
501 if (varNumber < 253) {
502 os.put(static_cast<char>(varNumber));
505 else if (varNumber <= std::numeric_limits<uint16_t>::max()) {
506 uint16_t value = htobe16(static_cast<uint16_t>(varNumber));
507 os.write(reinterpret_cast<const char*>(&value), 2);
510 else if (varNumber <= std::numeric_limits<uint32_t>::max()) {
511 uint32_t value = htobe32(static_cast<uint32_t>(varNumber));
512 os.write(reinterpret_cast<const char*>(&value), 4);
516 uint64_t value = htobe64(varNumber);
517 os.write(reinterpret_cast<const char*>(&value), 8);
527 #endif // NDN_TLV_HPP
uint64_t readNonNegativeInteger(size_t size, InputIterator &begin, const InputIterator &end)
Read nonNegativeInteger in NDN-TLV encoding.
Representing of SHA256-with-RSA signature in a data packet.
size_t writeVarNumber(std::ostream &os, uint64_t varNumber)
Write VAR-NUMBER to the specified stream.
size_t sizeOfVarNumber(uint64_t varNumber)
Get number of bytes necessary to hold value of VAR-NUMBER.
bool readVarNumber(InputIterator &begin, const InputIterator &end, uint64_t &number)
Read VAR-NUMBER in NDN-TLV encoding.
size_t sizeOfNonNegativeInteger(uint64_t varNumber)
Get number of bytes necessary to hold value of nonNegativeInteger.
An Interest holds a Name and other fields for an interest.
size_t writeNonNegativeInteger(std::ostream &os, uint64_t varNumber)
Write nonNegativeInteger to the specified stream.
Error(const std::string &what)
Abstraction implementing Interest selectors.
A Name holds an array of Name::Component and represents an NDN name.
bool readType(InputIterator &begin, const InputIterator &end, uint32_t &type)
Read TLV Type.
Class to represent Exclude component in NDN interests.