One more helper function for compression

This commit is contained in:
Miek Gieben 2012-01-10 16:34:14 +01:00
parent 8f8633d9a0
commit 79d092bd29
2 changed files with 43 additions and 16 deletions

View File

@ -28,3 +28,14 @@ func TestCompareLabels(t *testing.T) {
t.Fail()
}
}
func TestOffLabelFromRight(t *testing.T) {
s1 := "www.miek.nl" // fqdn??
t.Log(offsetLabelFromRight(s1, 4))
t.Log(offsetLabelFromRight(s1, 3))
t.Log(offsetLabelFromRight(s1, 2))
t.Log(offsetLabelFromRight(s1, 1))
t.Log(offsetLabelFromRight(s1, 0))
t.Fail()
}

View File

@ -6,15 +6,15 @@ package dns
func SplitLabels(s string) []string {
last := byte('.')
k := 0
labels := make([]string, 0)
s = Fqdn(s) // Make fully qualified
labels := make([]string, 0)
s = Fqdn(s) // Make fully qualified
for i := 0; i < len(s); i++ {
if s[i] == '.' {
if last == '\\' {
// do nothing
break
}
labels = append(labels,s[k:i])
labels = append(labels, s[k:i])
k = i + 1 // + dot
}
last = s[i]
@ -32,19 +32,35 @@ func CompareLabels(s1, s2 string) (n int) {
l1 := SplitLabels(s1)
l2 := SplitLabels(s2)
x1 := len(l1)-1
x2 := len(l2)-1
x1 := len(l1) - 1
x2 := len(l2) - 1
for {
if x1 < 0 || x2 < 0 {
break
}
if l1[x1] == l2[x2] {
n++
} else {
break
}
x1--
x2--
if x1 < 0 || x2 < 0 {
break
}
if l1[x1] == l2[x2] {
n++
} else {
break
}
x1--
x2--
}
return
return
}
// This function is needed for easy handling of compression
// pointers
// www.miek.nl, 2, gives that start of miek.nl (which is
// 2 labels from the right)
// labeloffset of zero is fishy as is a labeloffset larger
// than the number of labels... TODO: make it an error?
func offsetLabelFromRight(s string, labeloffset int) int {
l := SplitLabels(s)
fromleft := len(l) - labeloffset
off := 0
for i := 0; i < fromleft; i++ {
off += len(l[i]) + 1
}
return off
}