Add whitespace to packDomainName

This commit is contained in:
Tom Thorogood 2018-11-26 15:04:35 +10:30
parent 4c43711692
commit 03053758d4
No known key found for this signature in database
GPG Key ID: 86C63CDA416C6D2F
1 changed files with 14 additions and 0 deletions

14
msg.go
View File

@ -210,10 +210,12 @@ func packDomainName(s string, msg []byte, off int, compression map[string]int, c
// Compression // Compression
nameoffset := -1 nameoffset := -1
pointer := -1 pointer := -1
// Emit sequence of counted strings, chopping at dots. // Emit sequence of counted strings, chopping at dots.
begin := 0 begin := 0
var bs []byte var bs []byte
roBs, bsFresh, wasDot := s, true, false roBs, bsFresh, wasDot := s, true, false
loop: loop:
for i := 0; i < ls; i++ { for i := 0; i < ls; i++ {
var c byte var c byte
@ -222,22 +224,27 @@ loop:
} else { } else {
c = bs[i] c = bs[i]
} }
switch c { switch c {
case '\\': case '\\':
if bs == nil { if bs == nil {
bs = []byte(s) bs = []byte(s)
} }
copy(bs[i:ls-1], bs[i+1:]) copy(bs[i:ls-1], bs[i+1:])
ls-- ls--
if off+1 > lenmsg { if off+1 > lenmsg {
return lenmsg, labels, ErrBuf return lenmsg, labels, ErrBuf
} }
// check for \DDD // check for \DDD
if i+2 < ls && isDigit(bs[i]) && isDigit(bs[i+1]) && isDigit(bs[i+2]) { if i+2 < ls && isDigit(bs[i]) && isDigit(bs[i+1]) && isDigit(bs[i+2]) {
bs[i] = dddToByte(bs[i:]) bs[i] = dddToByte(bs[i:])
copy(bs[i+1:ls-2], bs[i+3:]) copy(bs[i+1:ls-2], bs[i+3:])
ls -= 2 ls -= 2
} }
bsFresh = false bsFresh = false
wasDot = false wasDot = false
case '.': case '.':
@ -246,18 +253,22 @@ loop:
return lenmsg, labels, ErrRdata return lenmsg, labels, ErrRdata
} }
wasDot = true wasDot = true
if i-begin >= 1<<6 { // top two bits of length must be clear if i-begin >= 1<<6 { // top two bits of length must be clear
return lenmsg, labels, ErrRdata return lenmsg, labels, ErrRdata
} }
// off can already (we're in a loop) be bigger than len(msg) // off can already (we're in a loop) be bigger than len(msg)
// this happens when a name isn't fully qualified // this happens when a name isn't fully qualified
if off+1+(i-begin) > lenmsg { if off+1+(i-begin) > lenmsg {
return lenmsg, labels, ErrBuf return lenmsg, labels, ErrBuf
} }
if compress && !bsFresh { if compress && !bsFresh {
roBs = string(bs) roBs = string(bs)
bsFresh = true bsFresh = true
} }
// Don't try to compress '.' // Don't try to compress '.'
// We should only compress when compress is true, but we should also still pick // We should only compress when compress is true, but we should also still pick
// up names that can be used for *future* compression(s). // up names that can be used for *future* compression(s).
@ -298,6 +309,7 @@ loop:
wasDot = false wasDot = false
} }
} }
// Root label is special // Root label is special
if bs == nil && len(s) == 1 && s[0] == '.' { if bs == nil && len(s) == 1 && s[0] == '.' {
return off, labels, nil return off, labels, nil
@ -305,6 +317,7 @@ loop:
if bs != nil && len(bs) == 1 && bs[0] == '.' { if bs != nil && len(bs) == 1 && bs[0] == '.' {
return off, labels, nil return off, labels, nil
} }
// If we did compression and we find something add the pointer here // If we did compression and we find something add the pointer here
if pointer != -1 { if pointer != -1 {
// We have two bytes (14 bits) to put the pointer in // We have two bytes (14 bits) to put the pointer in
@ -314,6 +327,7 @@ loop:
} else if msg != nil && off < len(msg) { } else if msg != nil && off < len(msg) {
msg[off] = 0 msg[off] = 0
} }
off++ off++
return off, labels, nil return off, labels, nil
} }