Browse Source

[trac1130] Move some internal used functions to anonymouse namespace
Test whether it reaches the end of input before skip left spaces

Ocean Wang 13 years ago
parent
commit
253d1fc351

+ 103 - 90
src/lib/dns/rdata/generic/naptr_35.cc

@@ -32,6 +32,105 @@ using namespace isc::util;
 // BEGIN_ISC_NAMESPACE
 // BEGIN_RDATA_NAMESPACE
 
+namespace {
+/// Skip the left whitespaces of the input string
+///
+/// \param input_str The input string
+/// \param input_iterator From which the skipping started
+void
+skipLeftSpaces(const std::string& input_str,
+                    std::string::const_iterator& input_iterator)
+{
+    if (input_iterator >= input_str.end()) {
+        isc_throw(InvalidRdataText,
+                  "Invalid NAPTR text format, field is missing.");
+    }
+
+    if (!isspace(*input_iterator)) {
+        isc_throw(InvalidRdataText,
+                  "Invalid NAPTR text format, fields are not separated by space.");
+    }
+    // Skip white spaces
+    while (input_iterator < input_str.end() && isspace(*input_iterator)) {
+        ++input_iterator;
+    }
+}
+
+/// Get a <character-string> from a string
+///
+/// \param input_str The input string
+/// \param input_iterator The iterator from which to start extracting,
+///        the iterator will be updated to new position after the function
+///        is returned
+/// \return A std::string that contains the extracted <character-string>
+std::string
+getNextCharacterString(const std::string& input_str,
+                              std::string::const_iterator& input_iterator)
+{
+    string result;
+
+    // If the input string only contains white-spaces, it is an invalid
+    // <character-string>
+    if (input_iterator >= input_str.end()) {
+        isc_throw(InvalidRdataText, "Invalid NAPTR text format, \
+                  <character-string> field is missing.");
+    }
+
+    // Whether the <character-string> is seperated with doulble quotes symbol(")
+    bool quotes_seperated = (*input_iterator == '"');
+
+    if (quotes_seperated) {
+        ++input_iterator;
+    }
+
+    while(input_iterator < input_str.end()){
+        if (quotes_seperated) {
+            // If the <character-string> is seperated with quotes symbol and
+            // another quotes symbol is encountered, it is the end of the
+            // <character-string>
+            if (*input_iterator == '"') {
+                ++input_iterator;
+                break;
+            }
+        } else if (*input_iterator == ' ') {
+            // If the <character-string> is not seperated with quotes symbol,
+            // it is seperated with <space> char
+            break;
+        }
+
+        result.push_back(*input_iterator);
+
+        ++input_iterator;
+    }
+
+    if (result.size() > MAX_CHARSTRING_LEN) {
+        isc_throw(CharStringTooLong, "NAPTR <character-string> is too long");
+    }
+
+    return (result);
+}
+
+/// Get a <character-string> from a input buffer
+///
+/// \param buffer The input buffer
+/// \param len The input buffer total length
+/// \return A std::string that contains the extracted <character-string>
+std::string
+getNextCharacterString(InputBuffer& buffer, size_t len) {
+    uint8_t str_len = buffer.readUint8();
+
+    size_t pos = buffer.getPosition();
+    if (len - pos < str_len) {
+        isc_throw(InvalidRdataLength, "Invalid NAPTR string length");
+    }
+
+    uint8_t buf[MAX_CHARSTRING_LEN];
+    buffer.readData(buf, str_len);
+    return (string(buf, buf + str_len));
+}
+
+} // Anonymouse namespace
+
 NAPTR::NAPTR(InputBuffer& buffer, size_t len):
     replacement_(".")
 {
@@ -62,31 +161,19 @@ NAPTR::NAPTR(const std::string& naptr_str):
 
     string::const_iterator input_iterator = naptr_str.begin() + iss.tellg();
 
-    if (skipLeftSpaces(naptr_str, input_iterator) == 0) {
-        isc_throw(InvalidRdataText,
-                  "Invalid NAPTR text format, fields are not separated by space");
-    }
+    skipLeftSpaces(naptr_str, input_iterator);
 
     flags_ = getNextCharacterString(naptr_str, input_iterator);
 
-    if (skipLeftSpaces(naptr_str, input_iterator) == 0) {
-        isc_throw(InvalidRdataText,
-                  "Invalid NAPTR text format, fields are not separated by space");
-    }
+    skipLeftSpaces(naptr_str, input_iterator);
 
     services_ = getNextCharacterString(naptr_str, input_iterator);
 
-    if (skipLeftSpaces(naptr_str, input_iterator) == 0) {
-        isc_throw(InvalidRdataText,
-                  "Invalid NAPTR text format, fields are not separated by space");
-    }
+    skipLeftSpaces(naptr_str, input_iterator);
 
     regexp_ = getNextCharacterString(naptr_str, input_iterator);
 
-    if (skipLeftSpaces(naptr_str, input_iterator) == 0) {
-        isc_throw(InvalidRdataText,
-                  "Invalid NAPTR text format, fields are not separated by space");
-    }
+    skipLeftSpaces(naptr_str, input_iterator);
 
     if (input_iterator < naptr_str.end()) {
         string replacementStr(input_iterator, naptr_str.end());
@@ -223,79 +310,5 @@ NAPTR::getReplacement() const {
     return (replacement_);
 }
 
-std::string
-NAPTR::getNextCharacterString(const std::string& input_str,
-                              std::string::const_iterator& input_iterator)
-{
-    string result;
-
-    // If the input string only contains white-spaces, it is an invalid
-    // <character-string>
-    if (input_iterator >= input_str.end()) {
-        isc_throw(InvalidRdataText, "Invalid NAPTR text format, \
-                  <character-string> field is missing.");
-    }
-
-    // Whether the <character-string> is seperated with doulble quotes symbol(")
-    bool quotes_seperated = (*input_iterator == '"');
-
-    if (quotes_seperated) {
-        ++input_iterator;
-    }
-
-    while(input_iterator < input_str.end()){
-        if (quotes_seperated) {
-            // If the <character-string> is seperated with quotes symbol and
-            // another quotes symbol is encountered, it is the end of the
-            // <character-string>
-            if (*input_iterator == '"') {
-                ++input_iterator;
-                break;
-            }
-        } else if (*input_iterator == ' ') {
-            // If the <character-string> is not seperated with quotes symbol,
-            // it is seperated with <space> char
-            break;
-        }
-
-        result.push_back(*input_iterator);
-
-        ++input_iterator;
-    }
-
-    if (result.size() > MAX_CHARSTRING_LEN) {
-        isc_throw(CharStringTooLong, "NAPTR <character-string> is too long");
-    }
-
-    return (result);
-}
-
-std::string
-NAPTR::getNextCharacterString(InputBuffer& buffer, size_t len) {
-    uint8_t str_len = buffer.readUint8();
-
-    size_t pos = buffer.getPosition();
-    if (len - pos < str_len) {
-        isc_throw(InvalidRdataLength, "Invalid NAPTR string length");
-    }
-
-    uint8_t buf[MAX_CHARSTRING_LEN];
-    buffer.readData(buf, str_len);
-    return (string(buf, buf + str_len));
-}
-
-int
-NAPTR::skipLeftSpaces(const std::string& input_str,
-                    std::string::const_iterator& input_iterator)
-{
-    int space_count = 0;
-    // Skip white spaces
-    while (input_iterator < input_str.end() && isspace(*input_iterator)) {
-        ++input_iterator;
-        ++space_count;
-    }
-    return space_count;
-}
-
 // END_RDATA_NAMESPACE
 // END_ISC_NAMESPACE

+ 0 - 22
src/lib/dns/rdata/generic/naptr_35.h

@@ -46,28 +46,6 @@ public:
     const std::string& getRegexp() const;
     const Name& getReplacement() const;
 private:
-    /// Get a <character-string> from a string
-    ///
-    /// \param input_str The input string
-    /// \param input_iterator The iterator from which to start extracting, the iterator will be updated
-    ///        to new position after the function is returned
-    /// \return A std::string that contains the extracted <character-string>
-    std::string getNextCharacterString(const std::string& input_str, std::string::const_iterator& input_iterator);
-
-    /// Get a <character-string> from a input buffer
-    ///
-    /// \param buffer The input buffer
-    /// \param len The input buffer total length
-    /// \return A std::string that contains the extracted <character-string>
-    std::string getNextCharacterString(util::InputBuffer& buffer, size_t len);
-
-    /// Skip the left whitespaces of the input string
-    ///
-    /// \param input_str The input string
-    /// \param input_iterator From which the skipping started
-    /// \return How many spaces are skipped
-    int skipLeftSpaces(const std::string& input_str, std::string::const_iterator& input_iterator);
-
     uint16_t order_;
     uint16_t preference_;
     std::string flags_;

+ 2 - 0
src/lib/dns/tests/rdata_naptr_unittest.cc

@@ -104,6 +104,8 @@ TEST_F(Rdata_NAPTR_Test, badText) {
                  InvalidRdataText);
     EXPECT_THROW(const NAPTR naptr("100 10 \"S\"\"SIP\" \"\" _sip._udp.example.com."),
                  InvalidRdataText);
+    // Field cannot be missing
+    EXPECT_THROW(const NAPTR naptr("100 10 \"S\""), InvalidRdataText);
 
     // The <character-string> cannot exceed 255 characters
     string naptr_str;