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 aoff, _ := pack.Name(a, abuf, 0, nil, false)
36 boff, _ := pack.Name(b, bbuf, 0, nil, false)
37
38 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
39
40 comparePool.Put(abuf)
41 comparePool.Put(bbuf)
42 return x
43 }
44
45 func comparebase64(a, b string) int {
46 abuf := comparePool.Get()
47 bbuf := comparePool.Get()
48 aoff, _ := base64.StdEncoding.Decode(abuf, []byte(a))
49 boff, _ := base64.StdEncoding.Decode(bbuf, []byte(b))
50
51 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
52
53 comparePool.Put(abuf)
54 comparePool.Put(bbuf)
55 return x
56 }
57
58 func comparebase32(a, b string) int {
59 abuf := comparePool.Get()
60 bbuf := comparePool.Get()
61 aoff, _ := base32.HexEncoding.WithPadding(base32.NoPadding).Decode(abuf, []byte(a))
62 boff, _ := base32.HexEncoding.WithPadding(base32.NoPadding).Decode(bbuf, []byte(b))
63
64 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
65
66 comparePool.Put(abuf)
67 comparePool.Put(bbuf)
68 return x
69 }
70
71 func comparehex(a, b string) int {
72 abuf := comparePool.Get()
73 bbuf := comparePool.Get()
74 aoff, _ := hex.Decode(abuf, []byte(a))
75 boff, _ := hex.Decode(bbuf, []byte(b))
76
77 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
78
79 comparePool.Put(abuf)
80 comparePool.Put(bbuf)
81 return x
82 }
83
84 func comparepair(a, b []svcb.Pair) int {
85 abuf := comparePool.Get()
86 bbuf := comparePool.Get()
87 aoff, _ := svcb.Pack(a, abuf, 0)
88 boff, _ := svcb.Pack(b, bbuf, 0)
89
90 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
91
92 comparePool.Put(abuf)
93 comparePool.Put(bbuf)
94 return x
95 }
96
97 func compareinfo(a, b []deleg.Info) int {
98 abuf := comparePool.Get()
99 bbuf := comparePool.Get()
100 aoff, _ := deleg.Pack(a, abuf, 0)
101 boff, _ := deleg.Pack(b, bbuf, 0)
102
103 x := bytes.Compare(abuf[:aoff], bbuf[:boff])
104
105 comparePool.Put(abuf)
106 comparePool.Put(bbuf)
107 return x
108 }
109
110 var comparePool = pool.New(DefaultMsgSize)