| 1 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 7 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 9 | ) |
| 10 | |
| 11 | // Parse the $GENERATE statement as used in BIND9 zones. |
| 12 | // See http://www.zytrax.com/books/dns/ch8/generate.html for instance. |
| 13 | // We are called after '$GENERATE '. After which we expect: |
| 14 | // * the range (12-24/2) |
| 15 | // * lhs (ownername) |
| 16 | // * [[ttl][class]] |
| 17 | // * type |
| 18 | // * rhs (rdata) |
| 19 | // But we are lazy here, only the range is parsed *all* occurrences |
| 20 | // of $ after that are interpreted. |
| 21 | func (zp *ZoneParserscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#) generatescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#generate().(l lexscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#) (RRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR#, bool) { |
| 22 | token := l.tokenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#token. |
| 23 | step := int64(1) |
| 24 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexBytescip-go gomod github.com/golang/go/src go1.25.0 strings/IndexByte().(token, '/'); i >= 0 { |
| 25 | if i+1 == len(token) { |
| 26 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad step in $GENERATE range", l) |
| 27 | } |
| 28 | |
| 29 | s, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(token[i+1:], 10, 64) |
| 30 | if err != nil || s <= 0 { |
| 31 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad step in $GENERATE range", l) |
| 32 | } |
| 33 | |
| 34 | step = s |
| 35 | token = token[:i] |
| 36 | } |
| 37 | |
| 38 | startStr, endStr, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(token, "-") |
| 39 | if !ok { |
| 40 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad start-stop in $GENERATE range", l) |
| 41 | } |
| 42 | |
| 43 | start, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(startStr, 10, 64) |
| 44 | if err != nil { |
| 45 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad start in $GENERATE range", l) |
| 46 | } |
| 47 | |
| 48 | end, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(endStr, 10, 64) |
| 49 | if err != nil { |
| 50 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad stop in $GENERATE range", l) |
| 51 | } |
| 52 | if end < 0 || start < 0 || end < start || (end-start)/step > 65535 { |
| 53 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad range in $GENERATE range", l) |
| 54 | } |
| 55 | |
| 56 | // _BLANK |
| 57 | l, ok = zp.cscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#c..Nextscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/zlexer#Next().() |
| 58 | if !ok || l.valuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#value. != zBlankscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/zBlank. { |
| 59 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("garbage after $GENERATE range", l) |
| 60 | } |
| 61 | |
| 62 | // Create a complete new string, which we then parse again. |
| 63 | var s string |
| 64 | for l, ok := zp.cscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#c..Nextscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/zlexer#Next().(); ok; l, ok = zp.cscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#c..Nextscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/zlexer#Next().() { |
| 65 | if l.errscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#err. { |
| 66 | return zp.setParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#setParseError().("bad data in $GENERATE directive", l) |
| 67 | } |
| 68 | if l.valuescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#value. == zNewlinescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/zNewline. { |
| 69 | break |
| 70 | } |
| 71 | |
| 72 | s += l.tokenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#token. |
| 73 | } |
| 74 | |
| 75 | r := &generateReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#{ |
| 76 | sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.: s, |
| 77 | |
| 78 | curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur.: start, |
| 79 | startscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#start.: start, |
| 80 | endscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#end.: end, |
| 81 | stepscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#step.: step, |
| 82 | |
| 83 | filescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#file.: zp.filescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#file., |
| 84 | lexscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#lex.: &l, |
| 85 | } |
| 86 | zp.subscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#sub. = NewZoneParserscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/NewZoneParser().(r, zp.originscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#origin., zp.filescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#file.) |
| 87 | zp.subscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#sub..includeDepthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#includeDepth., zp.subscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#sub..includeAllowedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#includeAllowed. = zp.includeDepthscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#includeDepth., zp.includeAllowedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#includeAllowed. |
| 88 | zp.subscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#sub..generateDisallowedscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#generateDisallowed. = true |
| 89 | zp.subscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#sub..SetDefaultTTLscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#SetDefaultTTL().(defaultTtlscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/defaultTtl.) |
| 90 | return zp.subNextscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ZoneParser#subNext().() |
| 91 | } |
| 92 | |
| 93 | type generateReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader# struct { |
| 94 | sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s. string |
| 95 | siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si. int |
| 96 | |
| 97 | curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur. int64 |
| 98 | startscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#start. int64 |
| 99 | endscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#end. int64 |
| 100 | stepscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#step. int64 |
| 101 | |
| 102 | modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod. bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer# |
| 103 | |
| 104 | escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. bool |
| 105 | |
| 106 | eofscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#eof. bool |
| 107 | |
| 108 | filescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#file. string |
| 109 | lexscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#lex. *lexscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex# |
| 110 | } |
| 111 | |
| 112 | func (r *generateReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#) parseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#parseError().(msg string, end int) *ParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ParseError# { |
| 113 | r.eofscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#eof. = true // Make errors sticky. |
| 114 | |
| 115 | l := *r.lexscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#lex. |
| 116 | l.tokenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#token. = r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si.-1 : end] |
| 117 | l.columnscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/lex#column. += r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si. // l.column starts one zBLANK before r.s |
| 118 | |
| 119 | return &ParseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ParseError#{filescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ParseError#file.: r.filescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#file., errscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ParseError#err.: msg, lexscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ParseError#lex.: l} |
| 120 | } |
| 121 | |
| 122 | func (r *generateReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#) Readscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#Read().(p []byte) (int, error) { |
| 123 | // NewZLexer, through NewZoneParser, should use ReadByte and |
| 124 | // not end up here. |
| 125 | |
| 126 | panic("not implemented") |
| 127 | } |
| 128 | |
| 129 | func (r *generateReaderscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#) ReadBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#ReadByte().() (byte, error) { |
| 130 | if r.eofscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#eof. { |
| 131 | return 0, ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF. |
| 132 | } |
| 133 | if r.modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod..Lenscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Len().() > 0 { |
| 134 | return r.modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod..ReadBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#ReadByte().() |
| 135 | } |
| 136 | |
| 137 | if r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si. >= len(r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.) { |
| 138 | r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si. = 0 |
| 139 | r.curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur. += r.stepscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#step. |
| 140 | |
| 141 | r.eofscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#eof. = r.curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur. > r.endscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#end. || r.curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur. < 0 |
| 142 | return '\n', nil |
| 143 | } |
| 144 | |
| 145 | si := r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si. |
| 146 | r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si.++ |
| 147 | |
| 148 | switch r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[si] { |
| 149 | case '\\': |
| 150 | if r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. { |
| 151 | r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. = false |
| 152 | return '\\', nil |
| 153 | } |
| 154 | |
| 155 | r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. = true |
| 156 | return r.ReadBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#ReadByte().() |
| 157 | case '$': |
| 158 | if r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. { |
| 159 | r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. = false |
| 160 | return '$', nil |
| 161 | } |
| 162 | |
| 163 | mod := "%d" |
| 164 | |
| 165 | if si >= len(r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.)-1 { |
| 166 | // End of the string |
| 167 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&r.modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod., mod, r.curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur.) |
| 168 | return r.modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod..ReadBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#ReadByte().() |
| 169 | } |
| 170 | |
| 171 | if r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[si+1] == '$' { |
| 172 | r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si.++ |
| 173 | return '$', nil |
| 174 | } |
| 175 | |
| 176 | var offset int64 |
| 177 | |
| 178 | // Search for { and } |
| 179 | if r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[si+1] == '{' { |
| 180 | // Modifier block |
| 181 | sep := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[si+2:], "}") |
| 182 | if sep < 0 { |
| 183 | return 0, r.parseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#parseError().("bad modifier in $GENERATE", len(r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.)) |
| 184 | } |
| 185 | |
| 186 | var errMsg string |
| 187 | mod, offset, errMsg = modToPrintfscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/modToPrintf().(r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[si+2 : si+2+sep]) |
| 188 | if errMsg != "" { |
| 189 | return 0, r.parseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#parseError().(errMsg, si+3+sep) |
| 190 | } |
| 191 | if r.startscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#start.+offset < 0 || r.endscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#end.+offset > 1<<31-1 { |
| 192 | return 0, r.parseErrorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#parseError().("bad offset in $GENERATE", si+3+sep) |
| 193 | } |
| 194 | |
| 195 | r.siscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#si. += 2 + sep // Jump to it |
| 196 | } |
| 197 | |
| 198 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(&r.modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod., mod, r.curscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#cur.+offset) |
| 199 | return r.modscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#mod..ReadBytescip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#ReadByte().() |
| 200 | default: |
| 201 | if r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. { // Pretty useless here |
| 202 | r.escapescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#escape. = false |
| 203 | return r.ReadBytescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#ReadByte().() |
| 204 | } |
| 205 | |
| 206 | return r.sscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/generateReader#s.[si], nil |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Convert a $GENERATE modifier 0,0,d to something Printf can deal with. |
| 211 | func modToPrintfscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/modToPrintf().(s string) (string, int64, string) { |
| 212 | // Modifier is { offset [ ,width [ ,base ] ] } - provide default |
| 213 | // values for optional width and type, if necessary. |
| 214 | offStr, s, ok0 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(s, ",") |
| 215 | widthStr, s, ok1 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(s, ",") |
| 216 | base, _, ok2 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(s, ",") |
| 217 | if !ok0 { |
| 218 | widthStr = "0" |
| 219 | } |
| 220 | if !ok1 { |
| 221 | base = "d" |
| 222 | } |
| 223 | if ok2 { |
| 224 | return "", 0, "bad modifier in $GENERATE" |
| 225 | } |
| 226 | |
| 227 | switch base { |
| 228 | case "o", "d", "x", "X": |
| 229 | default: |
| 230 | return "", 0, "bad base in $GENERATE" |
| 231 | } |
| 232 | |
| 233 | offset, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseIntscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseInt().(offStr, 10, 64) |
| 234 | if err != nil { |
| 235 | return "", 0, "bad offset in $GENERATE" |
| 236 | } |
| 237 | |
| 238 | width, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseUint().(widthStr, 10, 8) |
| 239 | if err != nil { |
| 240 | return "", 0, "bad width in $GENERATE" |
| 241 | } |
| 242 | |
| 243 | if width == 0 { |
| 244 | return "%" + base, offset, "" |
| 245 | } |
| 246 | |
| 247 | return "%0" + widthStr + base, offset, "" |
| 248 | } |