| 1 |
package rdata |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"time" |
| 8 |
|
| 9 |
"codeberg.org/miekg/dns/internal/ddd" |
| 10 |
"codeberg.org/miekg/dns/internal/dnsstring" |
| 11 |
) |
| 12 |
|
| 13 |
// cmToM takes a cm value expressed in RFC 1876 SIZE mantissa/exponent |
| 14 |
// format and returns a string in m (two decimals for the cm). |
| 15 |
func cmToM(x uint8) string { |
| 16 |
m := x & 0xF0 >> 4 |
| 17 |
e := x & 0x0F |
| 18 |
|
| 19 |
if e < 2 { |
| 20 |
if e == 1 { |
| 21 |
m *= 10 |
| 22 |
} |
| 23 |
|
| 24 |
return fmt.Sprintf("0.%02d", m) |
| 25 |
} |
| 26 |
|
| 27 |
var s strings.Builder |
| 28 |
fmt.Fprintf(&s, "%d", m) |
| 29 |
for e > 2 { |
| 30 |
s.WriteString("0") |
| 31 |
e-- |
| 32 |
} |
| 33 |
return s.String() |
| 34 |
} |
| 35 |
|
| 36 |
// sprint write the rdata to sb with spaces between the elements. |
| 37 |
func sprintData(sb *strings.Builder, sx ...string) { |
| 38 |
for i, s := range sx { |
| 39 |
sb.WriteString(s) |
| 40 |
if i < len(sx)-1 { |
| 41 |
sb.WriteByte(' ') |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func typeToString(t uint16) string { |
| 47 |
if t1, ok := dnsstring.TypeToString[uint16(t)]; ok { |
| 48 |
return t1 |
| 49 |
} |
| 50 |
return "TYPE" + strconv.Itoa(int(t)) |
| 51 |
} |
| 52 |
|
| 53 |
// saltToString converts a NSECX salt to uppercase and returns "-" when it is empty. |
| 54 |
func saltToString(s string) string { |
| 55 |
if s == "" { |
| 56 |
return "-" |
| 57 |
} |
| 58 |
return strings.ToUpper(s) |
| 59 |
} |
| 60 |
|
| 61 |
func euiToString(eui uint64, bits int) (hex string) { |
| 62 |
switch bits { |
| 63 |
case 64: |
| 64 |
hex = fmt.Sprintf("%16.16x", eui) |
| 65 |
hex = hex[0:2] + "-" + hex[2:4] + "-" + hex[4:6] + "-" + hex[6:8] + |
| 66 |
"-" + hex[8:10] + "-" + hex[10:12] + "-" + hex[12:14] + "-" + hex[14:16] |
| 67 |
case 48: |
| 68 |
hex = fmt.Sprintf("%12.12x", eui) |
| 69 |
hex = hex[0:2] + "-" + hex[2:4] + "-" + hex[4:6] + "-" + hex[6:8] + |
| 70 |
"-" + hex[8:10] + "-" + hex[10:12] |
| 71 |
} |
| 72 |
return |
| 73 |
} |
| 74 |
|
| 75 |
func sprintTxt(txt []string) string { |
| 76 |
sb := builderPool.Get() |
| 77 |
defer builderPool.Put(sb) |
| 78 |
|
| 79 |
for i, s := range txt { |
| 80 |
sb.Grow(3 + len(s)) |
| 81 |
if i > 0 { |
| 82 |
sb.WriteString(` "`) |
| 83 |
} else { |
| 84 |
sb.WriteByte('"') |
| 85 |
} |
| 86 |
for j := 0; j < len(s); { |
| 87 |
b, n := ddd.Next(s, j) |
| 88 |
if n == 0 { |
| 89 |
break |
| 90 |
} |
| 91 |
writeTxtByte(&sb, b) |
| 92 |
j += n |
| 93 |
} |
| 94 |
sb.WriteByte('"') |
| 95 |
} |
| 96 |
return sb.String() |
| 97 |
} |
| 98 |
|
| 99 |
func writeTxtByte(sb *strings.Builder, b byte) { |
| 100 |
switch { |
| 101 |
case b == '"' || b == '\\': |
| 102 |
sb.WriteByte('\\') |
| 103 |
sb.WriteByte(b) |
| 104 |
case b < ' ' || b > '~': |
| 105 |
sb.WriteString(ddd.Escape(b)) |
| 106 |
default: |
| 107 |
sb.WriteByte(b) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// splitN splits a string into N sized string chunks. |
| 112 |
func splitN(s string, n int) []string { |
| 113 |
if len(s) < n { |
| 114 |
return []string{s} |
| 115 |
} |
| 116 |
sx := []string{} |
| 117 |
p, i := 0, n |
| 118 |
for { |
| 119 |
if i <= len(s) { |
| 120 |
sx = append(sx, s[p:i]) |
| 121 |
} else { |
| 122 |
sx = append(sx, s[p:]) |
| 123 |
break |
| 124 |
|
| 125 |
} |
| 126 |
p, i = p+n, i+n |
| 127 |
} |
| 128 |
return sx |
| 129 |
} |
| 130 |
|
| 131 |
// Translate the TSIG time signed into a date. There is no need for RFC1982 calculations as this date is 48 bits. |
| 132 |
func tsigTimeToString(t uint64) string { |
| 133 |
ti := time.Unix(int64(t), 0).UTC() |
| 134 |
return ti.Format("20060102150405") |
| 135 |
} |