forked from benburkert/dns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
compression.go
177 lines (143 loc) · 3.09 KB
/
compression.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package dns
import (
"strings"
)
// Compressor encodes domain names.
type Compressor interface {
Length(...string) int
Pack([]byte, string) ([]byte, error)
}
// Decompressor decodes domain names.
type Decompressor interface {
Unpack([]byte) (string, []byte, error)
}
type compressor struct {
tbl map[string]int
offset int
}
func (c compressor) Length(names ...string) int {
var visited map[string]struct{}
if c.tbl != nil {
visited = make(map[string]struct{})
}
var n int
for _, name := range names {
n += c.length(name, visited)
}
return n
}
func (c compressor) length(name string, visited map[string]struct{}) int {
if name == "." || name == "" {
return 1
}
if c.tbl != nil {
if _, ok := c.tbl[name]; ok {
return 2
}
if _, ok := visited[name]; ok {
return 2
}
visited[name] = struct{}{}
}
pvt := strings.IndexByte(name, '.')
return pvt + 1 + c.length(name[pvt+1:], visited)
}
func (c compressor) Pack(b []byte, fqdn string) ([]byte, error) {
if fqdn == "." || fqdn == "" {
return append(b, 0x00), nil
}
if c.tbl != nil {
if idx, ok := c.tbl[fqdn]; ok {
ptr, err := pointerTo(idx)
if err != nil {
return nil, err
}
return append(b, ptr...), nil
}
}
pvt := strings.IndexByte(fqdn, '.')
if pvt == 0 {
return nil, errZeroSegLen
}
if pvt > 63 {
return nil, errSegTooLong
}
if c.tbl != nil {
idx := len(b) - c.offset
if int(uint16(idx)) != idx {
return nil, errInvalidPtr
}
c.tbl[fqdn] = idx
}
b = append(b, byte(pvt))
b = append(b, fqdn[:pvt]...)
return c.Pack(b, fqdn[pvt+1:])
}
type decompressor []byte
func (d decompressor) Unpack(b []byte) (string, []byte, error) {
name, b, err := d.unpack(make([]byte, 0, 32), b, nil)
if err != nil {
return "", nil, err
}
return string(name), b, nil
}
func (d decompressor) unpack(name, b []byte, visited []int) ([]byte, []byte, error) {
lenb := len(b)
if lenb == 0 {
return nil, nil, errBaseLen
}
if b[0] == 0x00 {
if len(name) == 0 {
return append(name, '.'), b[1:], nil
}
return name, b[1:], nil
}
if lenb < 2 {
return nil, nil, errBaseLen
}
if isPointer(b[0]) {
if d == nil {
return nil, nil, errBaseLen
}
ptr := nbo.Uint16(b[:2])
name, err := d.deref(name, ptr, visited)
if err != nil {
return nil, nil, err
}
return name, b[2:], nil
}
lenl, b := int(b[0]), b[1:]
if len(b) < lenl {
return nil, nil, errCalcLen
}
name = append(name, b[:lenl]...)
name = append(name, '.')
return d.unpack(name, b[lenl:], visited)
}
func (d decompressor) deref(name []byte, ptr uint16, visited []int) ([]byte, error) {
idx := int(ptr & 0x3FFF)
if len(d) < idx {
return nil, errInvalidPtr
}
if isPointer(d[idx]) {
return nil, errInvalidPtr
}
for _, v := range visited {
if idx == v {
return nil, errPtrCycle
}
}
name, _, err := d.unpack(name, d[idx:], append(visited, idx))
return name, err
}
func isPointer(b byte) bool { return b&0xC0 > 0 }
func pointerTo(idx int) ([]byte, error) {
ptr := uint16(idx)
if int(ptr) != idx {
return nil, errInvalidPtr
}
ptr |= 0xC000
buf := [2]byte{}
nbo.PutUint16(buf[:], ptr)
return buf[:], nil
}