Debian Codegraph @

sort_rdata.go

raw
1 package dns
2
3 import (
4 "bytes"
5 "encoding/base32"
6 "encoding/base64"
7 "encoding/hex"
8
9 "codeberg.org/miekg/dns/deleg"
10 "codeberg.org/miekg/dns/internal/pack"
11 "codeberg.org/miekg/dns/pkg/pool"
12 "codeberg.org/miekg/dns/svcb"
13 )
14
15 // comparename compares owernames in rdata, which is a difference compare that canonical because the
16 // actual wire data needs to be looked up, so we pack the names and compare them both.
17 func comparename(a, b string) int {
18 if a == "" && b == "" {
19 return 0
20 }
21 // optimize: before getting to the wiredata compare first label length, if not equal we already
22 // have a sorting. We only care about equal.
23 // TODO(miek): we might get away with no allocations and no wire data here...?
24 an, _ := dnsutilNext(a, 0)
25 bn, _ := dnsutilNext(b, 0)
26 if an < bn {
27 return -1
28 }
29 if an > bn {
30 return +1
31 }
32
33 abuf := comparePool.Get()
34 bbuf := comparePool.Get()
35 resize(&abuf, len(a)+1)
36 resize(&bbuf, len(b)+1)
37
38 aoff, _ := pack.Name(a, abuf, 0, nil, false)
39 boff, _ := pack.Name(b, bbuf, 0, nil, false)
40
41 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
42
43 comparePool.Put(abuf)
44 comparePool.Put(bbuf)
45 return x
46 }
47
48 func comparebase64(a, b string) int {
49 abuf := comparePool.Get()
50 bbuf := comparePool.Get()
51 resize(&abuf, base64.StdEncoding.EncodedLen(len(a)))
52 resize(&bbuf, base64.StdEncoding.EncodedLen(len(b)))
53
54 aoff, _ := base64.StdEncoding.Decode(abuf, []byte(a))
55 boff, _ := base64.StdEncoding.Decode(bbuf, []byte(b))
56
57 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
58
59 comparePool.Put(abuf)
60 comparePool.Put(bbuf)
61 return x
62 }
63
64 func comparebase32(a, b string) int {
65 abuf := comparePool.Get()
66 bbuf := comparePool.Get()
67 resize(&abuf, base32.HexEncoding.EncodedLen(len(a)))
68 resize(&bbuf, base32.HexEncoding.EncodedLen(len(b)))
69
70 aoff, _ := base32.HexEncoding.WithPadding(base32.NoPadding).Decode(abuf, []byte(a))
71 boff, _ := base32.HexEncoding.WithPadding(base32.NoPadding).Decode(bbuf, []byte(b))
72
73 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
74
75 comparePool.Put(abuf)
76 comparePool.Put(bbuf)
77 return x
78 }
79
80 func comparehex(a, b string) int {
81 abuf := comparePool.Get()
82 bbuf := comparePool.Get()
83 resize(&abuf, hex.EncodedLen(len(a)))
84 resize(&bbuf, hex.EncodedLen(len(b)))
85
86 aoff, _ := hex.Decode(abuf, []byte(a))
87 boff, _ := hex.Decode(bbuf, []byte(b))
88
89 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
90
91 comparePool.Put(abuf)
92 comparePool.Put(bbuf)
93 return x
94 }
95
96 func comparepair(a, b []svcb.Pair) int {
97 abuf := comparePool.Get()
98 bbuf := comparePool.Get()
99
100 size := 0
101 for i := range a {
102 size += a[i].Len()
103 }
104 resize(&abuf, size)
105 size = 0
106 for i := range b {
107 size += b[i].Len()
108 }
109 resize(&bbuf, size)
110
111 aoff, _ := svcb.Pack(a, abuf, 0)
112 boff, _ := svcb.Pack(b, bbuf, 0)
113
114 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
115
116 comparePool.Put(abuf)
117 comparePool.Put(bbuf)
118 return x
119 }
120
121 func compareinfo(a, b []deleg.Info) int {
122 abuf := comparePool.Get()
123 bbuf := comparePool.Get()
124 size := 0
125 for i := range a {
126 size += a[i].Len()
127 }
128 resize(&abuf, size)
129 size = 0
130 for i := range b {
131 size += b[i].Len()
132 }
133 resize(&bbuf, size)
134
135 aoff, _ := deleg.Pack(a, abuf, 0)
136 boff, _ := deleg.Pack(b, bbuf, 0)
137
138 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
139
140 comparePool.Put(abuf)
141 comparePool.Put(bbuf)
142 return x
143 }
144
145 var comparePool = pool.New(4096)
146
147 // resize resized the buffer we got from the pool to the size needed.
148 // If we resize beyond the comparepool's size it will not be put back into
149 // the pool, but with 4096 those odds should be small.
150 func resize(buf *[]byte, size int) {
151 if cap(*buf) >= size {
152 return
153 }
154 *buf = make([]byte, size)
155 }