Better XDR diagnostics

This commit is contained in:
Jakob Borg 2014-05-31 23:30:52 +02:00
parent b6f580cbc2
commit 23b27fa24a

View File

@ -47,6 +47,8 @@ func (r *Reader) ReadBytesMaxInto(max int, dst []byte) []byte {
return nil
}
r.last = time.Now()
s := r.tot
l := int(r.ReadUint32())
if r.err != nil {
return nil
@ -55,19 +57,26 @@ func (r *Reader) ReadBytesMaxInto(max int, dst []byte) []byte {
r.err = ErrElementSizeExceeded
return nil
}
if l+pad(l) > len(dst) {
dst = make([]byte, l+pad(l))
} else {
dst = dst[:l+pad(l)]
}
var n int
n, r.err = io.ReadFull(r.r, dst)
if r.err != nil {
dl.Debugf("@0x%x: rd bytes (%d): %v", s, len(dst), r.err)
return nil
}
r.tot += n
if debug {
if n > maxDebugBytes {
dl.Debugf("rd bytes (%d): %x...", n, dst[:maxDebugBytes])
dl.Debugf("@0x%x: rd bytes (%d): %x...", s, len(dst), dst[:maxDebugBytes])
} else {
dl.Debugf("rd bytes (%d): %x", n, dst)
dl.Debugf("@0x%x: rd bytes (%d): %x", s, len(dst), dst)
}
}
return dst[:l]
@ -78,45 +87,67 @@ func (r *Reader) ReadUint16() uint16 {
return 0
}
r.last = time.Now()
_, r.err = io.ReadFull(r.r, r.b[:4])
r.tot += 4
s := r.tot
var n int
n, r.err = io.ReadFull(r.r, r.b[:4])
r.tot += n
if r.err != nil {
dl.Debugf("@0x%x: rd uint16:", r.tot, r.err)
return 0
}
v := uint16(r.b[1]) | uint16(r.b[0])<<8
if debug {
dl.Debugf("rd uint16=%d", v)
dl.Debugf("@0x%x: rd uint16=%d (0x%04x)", s, v, v)
}
return v
}
func (r *Reader) ReadUint32() uint32 {
var n int
if r.err != nil {
return 0
}
r.last = time.Now()
s := r.tot
var n int
n, r.err = io.ReadFull(r.r, r.b[:4])
if n < 4 {
r.tot += n
if r.err != nil {
dl.Debugf("@0x%x: rd uint32:", r.tot, r.err)
return 0
}
r.tot += n
v := uint32(r.b[3]) | uint32(r.b[2])<<8 | uint32(r.b[1])<<16 | uint32(r.b[0])<<24
if debug {
dl.Debugf("rd uint32=%d", v)
dl.Debugf("@0x%x: rd uint32=%d (0x%08x)", s, v, v)
}
return v
}
func (r *Reader) ReadUint64() uint64 {
var n int
if r.err != nil {
return 0
}
r.last = time.Now()
s := r.tot
var n int
n, r.err = io.ReadFull(r.r, r.b[:8])
r.tot += n
if r.err != nil {
dl.Debugf("@0x%x: rd uint64:", r.tot, r.err)
return 0
}
v := uint64(r.b[7]) | uint64(r.b[6])<<8 | uint64(r.b[5])<<16 | uint64(r.b[4])<<24 |
uint64(r.b[3])<<32 | uint64(r.b[2])<<40 | uint64(r.b[1])<<48 | uint64(r.b[0])<<56
if debug {
dl.Debugf("rd uint64=%d", v)
dl.Debugf("@0x%x: rd uint64=%d (0x%016x)", s, v, v)
}
return v
}