Browse Source

[1603] add an extension to LableSequence: getDataLength() method.
in name compression we need this info (and in some cases we don't need
the data itself).

JINMEI Tatuya 13 years ago
parent
commit
03f1a5ea8f

+ 10 - 4
src/lib/dns/labelsequence.cc

@@ -21,17 +21,23 @@ namespace dns {
 
 const char*
 LabelSequence::getData(size_t *len) const {
+    *len = getDataLength();
+    return (&name_->ndata_[name_->offsets_[first_label_]]);
+}
+
+size_t
+LabelSequence::getDataLength() const {
     // If the labelsequence is absolute, the current last_label_ falls
     // out of the vector (since it points to the 'label' after the
     // root label, which doesn't exist; in that case, return
     // the length for the 'previous' label (the root label) plus
     // one (for the root label zero octet)
     if (isAbsolute()) {
-        *len = name_.offsets_[last_label_ - 1] - name_.offsets_[first_label_] + 1;
+        return (name_->offsets_[last_label_ - 1] -
+                name_->offsets_[first_label_] + 1);
     } else {
-        *len = name_.offsets_[last_label_] - name_.offsets_[first_label_];
+        return (name_->offsets_[last_label_] - name_->offsets_[first_label_]);
     }
-    return (&name_.ndata_[name_.offsets_[first_label_]]);
 }
 
 bool
@@ -70,7 +76,7 @@ LabelSequence::stripRight(size_t i) {
 
 bool
 LabelSequence::isAbsolute() const {
-    return (last_label_ == name_.offsets_.size());
+    return (last_label_ == name_->offsets_.size());
 }
 
 } // end namespace dns

+ 19 - 3
src/lib/dns/labelsequence.h

@@ -50,7 +50,7 @@ public:
     /// to the labels in the Name object).
     ///
     /// \param name The Name to construct a LabelSequence for
-    LabelSequence(const Name& name): name_(name),
+    LabelSequence(const Name& name): name_(&name),
                                      first_label_(0),
                                      last_label_(name.getLabelCount())
     {}
@@ -69,6 +69,22 @@ public:
     /// \return Pointer to the wire-format data of this label sequence
     const char* getData(size_t* len) const;
 
+    /// \brief Return the length of the wire-format data of this LabelSequence
+    ///
+    /// This method returns the number of octets for the data that would
+    /// be returned by the \c getData() method.
+    ///
+    /// Note that the return value of this method is always positive.
+    /// Note also that if the return value of this method is 1, it means the
+    /// sequence consists of the null label, i.e., a single "dot", and vice
+    /// versa.
+    ///
+    /// \note The data pointed to is only valid if the original Name
+    /// object is still in scope
+    ///
+    /// \return The length of the data of the label sequence in octets.
+    size_t getDataLength() const;
+
     /// \brief Compares two label sequences.
     ///
     /// Performs a (optionally case-insensitive) comparison between this
@@ -116,7 +132,7 @@ public:
     /// LabelSequence itself.
     ///
     /// \return Reference to the original Name object
-    const Name& getName() const { return (name_); }
+    const Name& getName() const { return (*name_); }
 
     /// \brief Checks whether the label sequence is absolute
     ///
@@ -124,7 +140,7 @@ public:
     bool isAbsolute() const;
 
 private:
-    const Name& name_;
+    const Name* name_;
     size_t first_label_;
     size_t last_label_;
 };

+ 3 - 0
src/lib/dns/tests/labelsequence_unittest.cc

@@ -124,6 +124,9 @@ getDataCheck(const char* expected_data, size_t expected_len,
     const char* data = ls.getData(&len);
     ASSERT_EQ(expected_len, len) << "Expected data: " << expected_data <<
                                     " name: " << ls.getName().toText();
+    EXPECT_EQ(expected_len, ls.getDataLength()) <<
+        "Expected data: " << expected_data <<
+        " name: " << ls.getName().toText();
     for (size_t i = 0; i < len; ++i) {
         EXPECT_EQ(expected_data[i], data[i]) << "Difference at pos " << i <<
                                                 ": Expected data: " <<