impl. the recent changes from the reflect package

This commit is contained in:
Miek Gieben 2011-08-08 09:49:25 +02:00
parent a3bd25fad3
commit d6716f693d
1 changed files with 38 additions and 45 deletions

83
msg.go
View File

@ -360,51 +360,44 @@ func packStructValue(val reflect.Value, msg []byte, off int) (off1 int, ok bool)
} }
case reflect.Struct: case reflect.Struct:
off, ok = packStructValue(fv, msg, off) off, ok = packStructValue(fv, msg, off)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: case reflect.Uint8:
i := fv.Uint() if off+1 > len(msg) {
switch fv.Type().Kind() { //fmt.Fprintf(os.Stderr, "dns: overflow packing uint8")
default: return len(msg), false
//fmt.Fprintf(os.Stderr, "dns: unknown packing type %v\n", f.Type) }
return len(msg), false msg[off] = byte(i)
case reflect.Uint8: off++
if off+1 > len(msg) { case reflect.Uint16:
//fmt.Fprintf(os.Stderr, "dns: overflow packing uint8") if off+2 > len(msg) {
return len(msg), false //fmt.Fprintf(os.Stderr, "dns: overflow packing uint16")
} return len(msg), false
msg[off] = byte(i) }
off++ msg[off] = byte(i >> 8)
case reflect.Uint16: msg[off+1] = byte(i)
if off+2 > len(msg) { off += 2
//fmt.Fprintf(os.Stderr, "dns: overflow packing uint16") case reflect.Uint32:
return len(msg), false if off+4 > len(msg) {
} //fmt.Fprintf(os.Stderr, "dns: overflow packing uint32")
msg[off] = byte(i >> 8) return len(msg), false
msg[off+1] = byte(i) }
off += 2 msg[off] = byte(i >> 24)
case reflect.Uint32: msg[off+1] = byte(i >> 16)
if off+4 > len(msg) { msg[off+2] = byte(i >> 8)
//fmt.Fprintf(os.Stderr, "dns: overflow packing uint32") msg[off+3] = byte(i)
return len(msg), false off += 4
} case reflect.Uint64:
msg[off] = byte(i >> 24) // Only used in TSIG, where it stops at 48 bits, so we discard the upper 16
msg[off+1] = byte(i >> 16) if off+6 > len(msg) {
msg[off+2] = byte(i >> 8) //fmt.Fprintf(os.Stderr, "dns: overflow packing uint64")
msg[off+3] = byte(i) return len(msg), false
off += 4 }
case reflect.Uint64: msg[off] = byte(i >> 40)
// Only used in TSIG, where it stops at 48 bits, so we discard the upper 16 msg[off+1] = byte(i >> 32)
if off+6 > len(msg) { msg[off+2] = byte(i >> 24)
//fmt.Fprintf(os.Stderr, "dns: overflow packing uint64") msg[off+3] = byte(i >> 16)
return len(msg), false msg[off+4] = byte(i >> 8)
} msg[off+5] = byte(i)
msg[off] = byte(i >> 40) off += 6
msg[off+1] = byte(i >> 32)
msg[off+2] = byte(i >> 24)
msg[off+3] = byte(i >> 16)
msg[off+4] = byte(i >> 8)
msg[off+5] = byte(i)
off += 6
}
case reflect.String: case reflect.String:
// There are multiple string encodings. // There are multiple string encodings.
// The tag distinguishes ordinary strings from domain names. // The tag distinguishes ordinary strings from domain names.