]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[1641] consolidated toText() code for NSEC and NSEC3 bitmaps with cleanups.
authorJINMEI Tatuya <jinmei@isc.org>
Sat, 11 Feb 2012 03:24:15 +0000 (19:24 -0800)
committerJINMEI Tatuya <jinmei@isc.org>
Sat, 11 Feb 2012 03:24:15 +0000 (19:24 -0800)
src/lib/dns/rdata/generic/detail/nsec_bitmap.cc
src/lib/dns/rdata/generic/detail/nsec_bitmap.h
src/lib/dns/rdata/generic/nsec3_50.cc
src/lib/dns/rdata/generic/nsec_47.cc

index b332f24d6f1484e566fd62d21e4a6bad4d92a8fd..a289e35d9df357096fec2e32de9f47fd2c8a41d8 100644 (file)
@@ -18,6 +18,7 @@
 #include <dns/rdata.h>
 #include <dns/rrtype.h>
 
+#include <cassert>
 #include <sstream>
 #include <vector>
 #include <stdint.h>
@@ -115,6 +116,37 @@ buildBitmapsFromText(const char* const rrtype_name,
         }
     }
 }
+
+void
+bitmapsToText(const vector<uint8_t>& typebits, ostringstream& oss) {
+    // In the following loop we use string::at() rather than operator[].
+    // Since the index calculation is a bit complicated, it will be safer
+    // and easier to find a bug (if any).  Note that this conversion method
+    // is generally not expected to be very efficient, so the slight overhead
+    // of at() should be acceptable.
+    const size_t typebits_len = typebits.size();
+    size_t len = 0;
+    for (size_t i = 0; i < typebits_len; i += len) {
+        assert(i + 2 <= typebits.size());
+        const unsigned int block = typebits.at(i);
+        len = typebits.at(i + 1);
+        assert(len > 0 && len <= 32);
+        i += 2;
+        for (size_t j = 0; j < len; ++j) {
+            if (typebits.at(i + j) == 0) {
+                continue;
+            }
+            for (size_t k = 0; k < 8; ++k) {
+                if ((typebits.at(i + j) & (0x80 >> k)) == 0) {
+                    continue;
+                }
+                const unsigned int t = block * 256 + j * 8 + k;
+                assert(t < 65536);
+                oss << " " << RRType(t);
+            }
+        }
+    }
+}
 }
 }
 }
index ed43260274003a2a1d9cd0eae437f7a45090fd06..fdb100c607b40b26bbbe64f720a1c9b04f479664 100644 (file)
@@ -23,7 +23,8 @@ namespace rdata {
 namespace generic {
 namespace detail {
 namespace nsec {
-/// Check if a given "type bitmap" for NSEC/NSEC3 is valid.
+
+/// \brief Check if a given "type bitmap" for NSEC/NSEC3 is valid.
 ///
 /// This helper function checks given wire format data (stored in a
 /// \c std::vector) is a valid type bitmaps used for the NSEC and NSEC3 RRs
@@ -44,6 +45,9 @@ void checkRRTypeBitmaps(const char* const rrtype_name,
 void buildBitmapsFromText(const char* const rrtype_name,
                           std::istringstream& iss,
                           std::vector<uint8_t>& typebits);
+
+void bitmapsToText(const std::vector<uint8_t>& typebits,
+                   std::ostringstream& oss);
 }
 }
 }
index 1d6384a6ef13a5bf19ffdae6c32f7dad41e11a86..84f6b901b4c1094a6e283d96bdbd518a0e1671c9 100644 (file)
@@ -195,26 +195,7 @@ NSEC3::~NSEC3() {
 string
 NSEC3::toText() const {
     ostringstream s;
-    int len = 0;
-    for (size_t i = 0; i < impl_->typebits_.size(); i += len) {
-        assert(i + 2 <= impl_->typebits_.size());
-        int window = impl_->typebits_[i];
-        len = impl_->typebits_[i + 1];
-        assert(len > 0 && len <= 32);
-        i += 2;
-        for (int j = 0; j < len; j++) {
-            if (impl_->typebits_[i + j] == 0) {
-                continue;
-            }
-            for (int k = 0; k < 8; k++) {
-                if ((impl_->typebits_[i + j] & (0x80 >> k)) == 0) {
-                    continue;
-                }
-                int t = window * 256 + j * 8 + k;
-                s << " " << RRType(t).toText();
-            }
-        }
-    }
+    bitmapsToText(impl_->typebits_, s);
 
     using namespace boost;
     return (lexical_cast<string>(static_cast<int>(impl_->hashalg_)) +
index 66fb23c3a1deeee452f13631dd953b81a456b9f9..08825db29870b33fbe749d5b70a1ae6d0fe053d6 100644 (file)
@@ -111,34 +111,8 @@ NSEC::~NSEC() {
 string
 NSEC::toText() const {
     ostringstream s;
-    int len = 0;
     s << impl_->nextname_;
-
-    // In the following loop we use string::at() rather than operator[].
-    // Since the index calculation is a bit complicated, it will be safer
-    // and easier to find a bug (if any).  Note that this conversion method
-    // is generally not expected to be very efficient, so the slight overhead
-    // of at() should be acceptable.
-    for (size_t i = 0; i < impl_->typebits_.size(); i += len) {
-        assert(i + 2 <= impl_->typebits_.size());
-        const int block = impl_->typebits_.at(i);
-        len = impl_->typebits_.at(i + 1);
-        assert(len > 0 && len <= 32);
-        i += 2;
-        for (int j = 0; j < len; j++) {
-            if (impl_->typebits_.at(i + j) == 0) {
-                continue;
-            }
-            for (int k = 0; k < 8; k++) {
-                if ((impl_->typebits_.at(i + j) & (0x80 >> k)) == 0) {
-                    continue;
-                }
-                const int t = block * 256 + j * 8 + k;
-                s << " " << RRType(t);
-            }
-        }
-    }
-
+    bitmapsToText(impl_->typebits_, s);
     return (s.str());
 }