aboutsummaryrefslogtreecommitdiff
path: root/middleware/file/tree/less.go
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2016-09-07 11:10:16 +0100
committerGravatar GitHub <noreply@github.com> 2016-09-07 11:10:16 +0100
commitd1f17fa7e061d91aa0af7e1fb959a68db899c812 (patch)
tree0f828a0e7bd8ca86051a721dae48e8c19a0a2f0e /middleware/file/tree/less.go
parent684330fd28f65a7a8d1ad01fb4c6c5db78240f29 (diff)
downloadcoredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.tar.gz
coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.tar.zst
coredns-d1f17fa7e061d91aa0af7e1fb959a68db899c812.zip
Cleanup: put middleware helper functions in pkgs (#245)
Move all (almost all) Go files in middleware into their own packages. This makes for better naming and discoverability. Lot of changes elsewhere to make this change. The middleware.State was renamed to request.Request which is better, but still does not cover all use-cases. It was also moved out middleware because it is used by `dnsserver` as well. A pkg/dnsutil packages was added for shared, handy, dns util functions. All normalize functions are now put in normalize.go
Diffstat (limited to 'middleware/file/tree/less.go')
-rw-r--r--middleware/file/tree/less.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/middleware/file/tree/less.go b/middleware/file/tree/less.go
new file mode 100644
index 000000000..32d87b683
--- /dev/null
+++ b/middleware/file/tree/less.go
@@ -0,0 +1,59 @@
+package tree
+
+import (
+ "bytes"
+
+ "github.com/miekg/dns"
+)
+
+// less returns <0 when a is less than b, 0 when they are equal and
+// >0 when a is larger than b.
+// The function orders names in DNSSEC canonical order: RFC 4034s section-6.1
+//
+// See http://bert-hubert.blogspot.co.uk/2015/10/how-to-do-fast-canonical-ordering-of.html
+// for a blog article on this implementation.
+//
+// The values of a and b are *not* lowercased before the comparison!
+func less(a, b string) int {
+ i := 1
+ aj := len(a)
+ bj := len(b)
+ for {
+ ai, oka := dns.PrevLabel(a, i)
+ bi, okb := dns.PrevLabel(b, i)
+ if oka && okb {
+ return 0
+ }
+ // sadly this []byte will allocate... TODO(miek): check if this is needed
+ // for a name, otherwise compare the strings.
+ ab := []byte(a[ai:aj])
+ bb := []byte(b[bi:bj])
+ doDDD(ab)
+ doDDD(bb)
+
+ res := bytes.Compare(ab, bb)
+ if res != 0 {
+ return res
+ }
+
+ i++
+ aj, bj = ai, bi
+ }
+ return 0
+}
+
+func doDDD(b []byte) {
+ lb := len(b)
+ for i := 0; i < lb; i++ {
+ if i+3 < lb && b[i] == '\\' && isDigit(b[i+1]) && isDigit(b[i+2]) && isDigit(b[i+3]) {
+ b[i] = dddToByte(b[i:])
+ for j := i + 1; j < lb-3; j++ {
+ b[j] = b[j+3]
+ }
+ lb -= 3
+ }
+ }
+}
+
+func isDigit(b byte) bool { return b >= '0' && b <= '9' }
+func dddToByte(s []byte) byte { return (s[1]-'0')*100 + (s[2]-'0')*10 + (s[3] - '0') }