summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/miekg/dns/compress_generate.go
blob: 9a136c414ad7f79b211b8562911dac0583ff2cdf (plain)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//+build ignore

// compression_generate.go is meant to run with go generate. It will use
// go/{importer,types} to track down all the RR struct types. Then for each type
// it will look to see if there are (compressible) names, if so it will add that
// type to compressionLenHelperType and comressionLenSearchType which "fake" the
// compression so that Len() is fast.
package main

import (
	"bytes"
	"fmt"
	"go/format"
	"go/importer"
	"go/types"
	"log"
	"os"
)

var packageHdr = `
// Code generated by "go run compress_generate.go"; DO NOT EDIT.

package dns

`

// getTypeStruct will take a type and the package scope, and return the
// (innermost) struct if the type is considered a RR type (currently defined as
// those structs beginning with a RR_Header, could be redefined as implementing
// the RR interface). The bool return value indicates if embedded structs were
// resolved.
func getTypeStruct(t types.Type, scope *types.Scope) (*types.Struct, bool) {
	st, ok := t.Underlying().(*types.Struct)
	if !ok {
		return nil, false
	}
	if st.Field(0).Type() == scope.Lookup("RR_Header").Type() {
		return st, false
	}
	if st.Field(0).Anonymous() {
		st, _ := getTypeStruct(st.Field(0).Type(), scope)
		return st, true
	}
	return nil, false
}

func main() {
	// Import and type-check the package
	pkg, err := importer.Default().Import("github.com/miekg/dns")
	fatalIfErr(err)
	scope := pkg.Scope()

	var domainTypes []string  // Types that have a domain name in them (either compressible or not).
	var cdomainTypes []string // Types that have a compressible domain name in them (subset of domainType)
Names:
	for _, name := range scope.Names() {
		o := scope.Lookup(name)
		if o == nil || !o.Exported() {
			continue
		}
		st, _ := getTypeStruct(o.Type(), scope)
		if st == nil {
			continue
		}
		if name == "PrivateRR" {
			continue
		}

		if scope.Lookup("Type"+o.Name()) == nil && o.Name() != "RFC3597" {
			log.Fatalf("Constant Type%s does not exist.", o.Name())
		}

		for i := 1; i < st.NumFields(); i++ {
			if _, ok := st.Field(i).Type().(*types.Slice); ok {
				if st.Tag(i) == `dns:"domain-name"` {
					domainTypes = append(domainTypes, o.Name())
					continue Names
				}
				if st.Tag(i) == `dns:"cdomain-name"` {
					cdomainTypes = append(cdomainTypes, o.Name())
					domainTypes = append(domainTypes, o.Name())
					continue Names
				}
				continue
			}

			switch {
			case st.Tag(i) == `dns:"domain-name"`:
				domainTypes = append(domainTypes, o.Name())
				continue Names
			case st.Tag(i) == `dns:"cdomain-name"`:
				cdomainTypes = append(cdomainTypes, o.Name())
				domainTypes = append(domainTypes, o.Name())
				continue Names
			}
		}
	}

	b := &bytes.Buffer{}
	b.WriteString(packageHdr)

	// compressionLenHelperType - all types that have domain-name/cdomain-name can be used for compressing names

	fmt.Fprint(b, "func compressionLenHelperType(c map[string]int, r RR, initLen int) int {\n")
	fmt.Fprint(b, "currentLen := initLen\n")
	fmt.Fprint(b, "switch x := r.(type) {\n")
	for _, name := range domainTypes {
		o := scope.Lookup(name)
		st, _ := getTypeStruct(o.Type(), scope)

		fmt.Fprintf(b, "case *%s:\n", name)
		for i := 1; i < st.NumFields(); i++ {
			out := func(s string) {
				fmt.Fprintf(b, "currentLen -= len(x.%s) + 1\n", st.Field(i).Name())
				fmt.Fprintf(b, "currentLen += compressionLenHelper(c, x.%s, currentLen)\n", st.Field(i).Name())
			}

			if _, ok := st.Field(i).Type().(*types.Slice); ok {
				switch st.Tag(i) {
				case `dns:"domain-name"`:
					fallthrough
				case `dns:"cdomain-name"`:
					// For HIP we need to slice over the elements in this slice.
					fmt.Fprintf(b, `for i := range x.%s {
	currentLen -= len(x.%s[i]) + 1
}
`, st.Field(i).Name(), st.Field(i).Name())
					fmt.Fprintf(b, `for i := range x.%s {
	currentLen += compressionLenHelper(c, x.%s[i], currentLen)
}
`, st.Field(i).Name(), st.Field(i).Name())
				}
				continue
			}

			switch {
			case st.Tag(i) == `dns:"cdomain-name"`:
				fallthrough
			case st.Tag(i) == `dns:"domain-name"`:
				out(st.Field(i).Name())
			}
		}
	}
	fmt.Fprintln(b, "}\nreturn currentLen - initLen\n}\n\n")

	// compressionLenSearchType - search cdomain-tags types for compressible names.

	fmt.Fprint(b, "func compressionLenSearchType(c map[string]int, r RR) (int, bool, int) {\n")
	fmt.Fprint(b, "switch x := r.(type) {\n")
	for _, name := range cdomainTypes {
		o := scope.Lookup(name)
		st, _ := getTypeStruct(o.Type(), scope)

		fmt.Fprintf(b, "case *%s:\n", name)
		j := 1
		for i := 1; i < st.NumFields(); i++ {
			out := func(s string, j int) {
				fmt.Fprintf(b, "k%d, ok%d, sz%d := compressionLenSearch(c, x.%s)\n", j, j, j, st.Field(i).Name())
			}

			// There are no slice types with names that can be compressed.

			switch {
			case st.Tag(i) == `dns:"cdomain-name"`:
				out(st.Field(i).Name(), j)
				j++
			}
		}
		k := "k1"
		ok := "ok1"
		sz := "sz1"
		for i := 2; i < j; i++ {
			k += fmt.Sprintf(" + k%d", i)
			ok += fmt.Sprintf(" && ok%d", i)
			sz += fmt.Sprintf(" + sz%d", i)
		}
		fmt.Fprintf(b, "return %s, %s, %s\n", k, ok, sz)
	}
	fmt.Fprintln(b, "}\nreturn 0, false, 0\n}\n\n")

	// gofmt
	res, err := format.Source(b.Bytes())
	if err != nil {
		b.WriteTo(os.Stderr)
		log.Fatal(err)
	}

	f, err := os.Create("zcompress.go")
	fatalIfErr(err)
	defer f.Close()
	f.Write(res)
}

func fatalIfErr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}