face: NDNLP encoding change

FragIndex and FragCount fields are encoded as nonNegativeInteger.

refs #1283

Change-Id: I4654078c3bfd73c83ce06ce6a9cd97952a4f49f8
diff --git a/daemon/face/ndnlp-parse.cpp b/daemon/face/ndnlp-parse.cpp
index 1162f0e..5ce4fc5 100644
--- a/daemon/face/ndnlp-parse.cpp
+++ b/daemon/face/ndnlp-parse.cpp
@@ -20,7 +20,7 @@
   if (elements.size() < 2) {
     throw ParseError("NdnlpData element has incorrect number of children");
   }
-  
+
   const Block& sequenceElement = elements.front();
   if (sequenceElement.type() != tlv::NdnlpSequence) {
     throw ParseError("NdnlpSequence element is missing");
@@ -29,7 +29,7 @@
     throw ParseError("NdnlpSequence element has incorrect length");
   }
   m_seq = be64toh(*reinterpret_cast<const uint64_t*>(&*sequenceElement.value_begin()));
-  
+
   const Block& payloadElement = elements.back();
   if (payloadElement.type() != tlv::NdnlpPayload) {
     throw ParseError("NdnlpPayload element is missing");
@@ -44,24 +44,30 @@
   if (elements.size() != 4) {
     throw ParseError("NdnlpData element has incorrect number of children");
   }
-  
+
   const Block& fragIndexElement = elements.at(1);
   if (fragIndexElement.type() != tlv::NdnlpFragIndex) {
     throw ParseError("NdnlpFragIndex element is missing");
   }
-  if (fragIndexElement.value_size() != sizeof(uint16_t)) {
-    throw ParseError("NdnlpFragIndex element has incorrect length");
+  uint64_t fragIndex = ndn::readNonNegativeInteger(fragIndexElement);
+  if (fragIndex > std::numeric_limits<uint16_t>::max()) {
+    throw ParseError("NdnlpFragIndex is too large");
   }
-  m_fragIndex = be16toh(*reinterpret_cast<const uint16_t*>(&*fragIndexElement.value_begin()));
-  
+  m_fragIndex = static_cast<uint16_t>(fragIndex);
+
   const Block& fragCountElement = elements.at(2);
   if (fragCountElement.type() != tlv::NdnlpFragCount) {
     throw ParseError("NdnlpFragCount element is missing");
   }
-  if (fragCountElement.value_size() != sizeof(uint16_t)) {
-    throw ParseError("NdnlpFragCount element has incorrect length");
+  uint64_t fragCount = ndn::readNonNegativeInteger(fragCountElement);
+  if (fragCount > std::numeric_limits<uint16_t>::max()) {
+    throw ParseError("NdnlpFragCount is too large");
   }
-  m_fragCount = be16toh(*reinterpret_cast<const uint16_t*>(&*fragCountElement.value_begin()));
+  m_fragCount = static_cast<uint16_t>(fragCount);
+
+  if (m_fragIndex >= m_fragCount) {
+    throw ParseError("NdnlpFragIndex must be less than NdnlpFragCount");
+  }
 }
 
 } // namespace ndnlp