made some members non-inline to decrease size
[senf.git] / senf / Packets / 80221Bundle / TLVParser.cc
index ad14cb5..373478b 100644 (file)
@@ -27,7 +27,6 @@
 //#include "TLVParser.ih"
 
 // Custom includes
-#include <iomanip>
 #include <senf/Utils/hexdump.hh>
 #include <senf/Utils/Format.hh>
 #include <senf/Utils/String.hh>
@@ -41,6 +40,26 @@ SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHStatusTLVParser );
 SENF_PACKET_TLV_REGISTRY_REGISTER( senf::MIHValidTimeIntervalTLVParser );
 
 //-/////////////////////////////////////////////////////////////////////////////////////////////////
+// MIHBaseTLVParser
+
+prefix_ void senf::MIHBaseTLVParser::validateType(boost::uint8_t expectedType)
+    const
+{
+    if (! check( 1 + senf::bytes(length_()) + length()) )
+        throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
+    if (type() != expectedType)
+        throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
+}
+
+prefix_ void senf::MIHBaseTLVParser::validateTypeLength(boost::uint8_t expectedType, MIHTLVLengthParser::value_type expectedLength)
+    const
+{
+    validateType( expectedLength);
+    if (length() != expectedLength)
+        throw InvalidMIHPacketException("invalid length in TLV.") << " Type: " << senf::str(type());
+}
+
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::MIHBaseListTLVParser
 
 prefix_ void senf::MIHBaseListTLVParser::maxListSize(MIHTLVLengthParser::value_type maxl)
@@ -168,6 +187,16 @@ prefix_ void senf::MIHFSrcIdTLVParser::dump(std::ostream & os)
     MIHFIdTLVParser::dump(os);
 }
 
+prefix_ void senf::MIHFSrcIdTLVParser::validate()
+    const
+{
+    if (! check( 1 + senf::bytes(length_()) + length()) )
+        throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
+    if (type() != typeId)
+        throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
+}
+
+
 //-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::MIHFDstIdTLVParser
 
@@ -179,6 +208,15 @@ prefix_ void senf::MIHFDstIdTLVParser::dump(std::ostream & os)
     MIHFIdTLVParser::dump(os);
 }
 
+prefix_ void senf::MIHFDstIdTLVParser::validate()
+    const
+{
+    if (! check( 1 + senf::bytes(length_()) + length()) )
+        throw InvalidMIHPacketException("truncated TLV.") << " Type: " << senf::str(type());
+    if (type() != typeId)
+        throw InvalidMIHPacketException("invalid TLV type: ") << senf::str(type());
+}
+
 //-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::MIHStatusTLVParser
 
@@ -211,6 +249,14 @@ prefix_ void senf::MIHStatusTLVParser::dump(std::ostream & os)
     os << " (???; invalid value!)" << std::endl;
 }
 
+prefix_ void senf::MIHStatusTLVParser::validate()
+    const
+{
+    validateTypeLength( typeId, 1);
+    if (value() >= 4)
+        throw InvalidMIHPacketException("invalid value in MIHStatusTLV ") << senf::str(value());
+}
+
 //-/////////////////////////////////////////////////////////////////////////////////////////////////
 // senf::MIHRegisterReqCodeTLVParser
 
@@ -234,12 +280,12 @@ prefix_ void senf::MIHRegisterReqCodeTLVParser::dump(std::ostream & os)
     os << " (???; invalid value!)" << std::endl;
 }
 
-prefix_ std::pair<bool, std::string> senf::MIHRegisterReqCodeTLVParser::validate()
+prefix_ void senf::MIHRegisterReqCodeTLVParser::validate()
     const
 {
-    if (length() != 1) return std::make_pair(false, "invalid length in MIHRegisterReqCodeTLV " + senf::str(length()));
-    if (value()  >= 2) return std::make_pair(false, "invalid value in MIHRegisterReqCodeTLV " + senf::str(value()));
-    return std::make_pair(true, "");
+    validateTypeLength( typeId, 1);
+    if (value() >= 2)
+        throw InvalidMIHPacketException("invalid value in MIHRegisterReqCodeTLV ") << senf::str(value());
 }
 
 //-/////////////////////////////////////////////////////////////////////////////////////////////////
@@ -257,11 +303,10 @@ prefix_ void senf::MIHValidTimeIntervalTLVParser::dump(std::ostream & os)
        << ( value()==0 ? " (infinite)" : " seconds") << std::endl;
 }
 
-prefix_ std::pair<bool, std::string> senf::MIHValidTimeIntervalTLVParser::validate()
+prefix_ void senf::MIHValidTimeIntervalTLVParser::validate()
     const
 {
-    if (length() != 4) return std::make_pair(false, "invalid length in MIHValidTimeIntervalTLV " + senf::str(length()));
-    return std::make_pair(true, "");
+    validateTypeLength( typeId, 4);
 }
 
 //-/////////////////////////////////////////////////////////////////////////////////////////////////