From b50e3f690e0178f5040e54352a6290875f4c2cf0 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Mon, 30 Apr 2012 16:54:02 +0200 Subject: [PATCH] First chunk in parsing LOC RRs --- types.go | 8 ++++---- zscan.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ zscan_rr.go | 2 +- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/types.go b/types.go index 43ab8a2d..50a4cf1c 100644 --- a/types.go +++ b/types.go @@ -618,9 +618,9 @@ func (rr *RR_LOC) String() string { } else { s += fmt.Sprintf("%.0fm ", float64(s1)) } - s += locCM((rr.Size&0xf0)>>4, rr.Size&0x0f) + "m " - s += locCM((rr.HorizPre&0xf0)>>4, rr.HorizPre&0x0f) + "m " - s += locCM((rr.VertPre&0xf0)>>4, rr.VertPre&0x0f) + "m" + s += cmToString((rr.Size&0xf0)>>4, rr.Size&0x0f) + "m " + s += cmToString((rr.HorizPre&0xf0)>>4, rr.HorizPre&0x0f) + "m " + s += cmToString((rr.VertPre&0xf0)>>4, rr.VertPre&0x0f) + "m" return s } @@ -1109,7 +1109,7 @@ func saltString(s string) string { return strings.ToUpper(s) } -func locCM(mantissa, exponent uint8) string { +func cmToString(mantissa, exponent uint8) string { switch exponent { case 0, 1: if exponent == 1 { diff --git a/zscan.go b/zscan.go index 84226155..af50e982 100644 --- a/zscan.go +++ b/zscan.go @@ -210,7 +210,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { // Discard, can happen when there is nothing on the // line except the RR type case _STRING: // First thing on the is the ttl - if ttl, ok := stringToTtl(l, f); !ok { + if ttl, ok := stringToTtl(l.token); !ok { t <- Token{Error: &ParseError{f, "not a TTL", l}} return } else { @@ -264,7 +264,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { t <- Token{Error: e} return } - if ttl, ok := stringToTtl(l, f); !ok { + if ttl, ok := stringToTtl(l.token); !ok { t <- Token{Error: &ParseError{f, "expecting $TTL value, not this...", l}} return } else { @@ -310,7 +310,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { h.Class = l.torc st = _EXPECT_ANY_NOCLASS_BL case _STRING: // TTL is this case - if ttl, ok := stringToTtl(l, f); !ok { + if ttl, ok := stringToTtl(l.token); !ok { t <- Token{Error: &ParseError{f, "not a TTL", l}} return } else { @@ -349,7 +349,7 @@ func parseZone(r io.Reader, origin, f string, t chan Token, include int) { case _EXPECT_ANY_NOCLASS: switch l.value { case _STRING: // TTL - if ttl, ok := stringToTtl(l, f); !ok { + if ttl, ok := stringToTtl(l.token); !ok { t <- Token{Error: &ParseError{f, "not a TTL", l}} return } else { @@ -698,10 +698,10 @@ func typeToInt(token string) (uint16, bool) { } // Parse things like 2w, 2m, etc, Return the time in seconds. -func stringToTtl(l lex, f string) (uint32, bool) { +func stringToTtl(token string) (uint32, bool) { s := uint32(0) i := uint32(0) - for _, c := range l.token { + for _, c := range token { switch c { case 's', 'S': s += i @@ -728,6 +728,46 @@ func stringToTtl(l lex, f string) (uint32, bool) { return s + i, true } +// Parse LOC records' [.][mM] into a +// mantissa exponent format. Token should contain the entire +// string (i.e. no spaces allowed) +func stringToCm(token string) (e, m uint8, ok bool) { + if token[len(token)-1] == 'M' || token[len(token)-1] == 'm' { + token = token[0 : len(token)-1] + } + s := strings.SplitN(token, ".", 2) + var meters, cmeters, val int + var err error + switch len(s) { + case 1: + if cmeters, err = strconv.Atoi(s[1]); err != nil { + return + } + fallthrough + case 0: + if meters, err = strconv.Atoi(s[0]); err != nil { + return + } + } + ok = true + if meters > 0 { + e = 2 + val = meters + } else { + e = 0 + val = cmeters + } + for val > 10 { + e++ + val /= 10 + } + if e > 9 { + ok = false + } + m = uint8(val) + return +} + func appendOrigin(name, origin string) string { if origin == "." { return name + origin diff --git a/zscan_rr.go b/zscan_rr.go index 89da90a2..53371fdb 100644 --- a/zscan_rr.go +++ b/zscan_rr.go @@ -245,7 +245,7 @@ func setSOA(h RR_Header, c chan lex, o, f string) (RR, *ParseError) { // Serial should be a number return nil, &ParseError{f, "bad SOA zone parameter", l} } - if v, ok = stringToTtl(l, f); !ok { + if v, ok = stringToTtl(l.token); !ok { return nil, &ParseError{f, "bad SOA zone parameter", l} }