2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
package xdr
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2014-02-24 12:29:30 +00:00
|
|
|
var ErrElementSizeExceeded = errors.New("element size exceeded")
|
2014-02-15 11:08:55 +00:00
|
|
|
|
|
|
|
type Reader struct {
|
2014-07-28 09:31:22 +00:00
|
|
|
r io.Reader
|
|
|
|
err error
|
|
|
|
b [8]byte
|
|
|
|
sb []byte
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReader(r io.Reader) *Reader {
|
|
|
|
return &Reader{
|
|
|
|
r: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadString() string {
|
2014-06-30 11:33:52 +00:00
|
|
|
if r.sb == nil {
|
|
|
|
r.sb = make([]byte, 64)
|
|
|
|
} else {
|
|
|
|
r.sb = r.sb[:cap(r.sb)]
|
|
|
|
}
|
|
|
|
r.sb = r.ReadBytesInto(r.sb)
|
|
|
|
return string(r.sb)
|
2014-02-20 16:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadStringMax(max int) string {
|
2014-06-30 11:33:52 +00:00
|
|
|
if r.sb == nil {
|
|
|
|
r.sb = make([]byte, 64)
|
|
|
|
} else {
|
|
|
|
r.sb = r.sb[:cap(r.sb)]
|
|
|
|
}
|
|
|
|
r.sb = r.ReadBytesMaxInto(max, r.sb)
|
|
|
|
return string(r.sb)
|
2014-02-20 16:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadBytes() []byte {
|
|
|
|
return r.ReadBytesInto(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadBytesMax(max int) []byte {
|
|
|
|
return r.ReadBytesMaxInto(max, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadBytesInto(dst []byte) []byte {
|
|
|
|
return r.ReadBytesMaxInto(0, dst)
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
func (r *Reader) ReadBytesMaxInto(max int, dst []byte) []byte {
|
2014-02-15 11:08:55 +00:00
|
|
|
if r.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
l := int(r.ReadUint32())
|
|
|
|
if r.err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2014-02-20 16:40:15 +00:00
|
|
|
if max > 0 && l > max {
|
|
|
|
r.err = ErrElementSizeExceeded
|
|
|
|
return nil
|
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
if l+pad(l) > len(dst) {
|
|
|
|
dst = make([]byte, l+pad(l))
|
|
|
|
} else {
|
|
|
|
dst = dst[:l+pad(l)]
|
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
var n int
|
|
|
|
n, r.err = io.ReadFull(r.r, dst)
|
2014-05-31 21:30:52 +00:00
|
|
|
if r.err != nil {
|
2014-06-04 08:08:25 +00:00
|
|
|
if debug {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd bytes (%d): %v", len(dst), r.err)
|
2014-06-04 08:08:25 +00:00
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
|
|
|
if n > maxDebugBytes {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd bytes (%d): %x...", len(dst), dst[:maxDebugBytes])
|
2014-05-17 06:43:01 +00:00
|
|
|
} else {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd bytes (%d): %x", len(dst), dst)
|
2014-05-17 06:43:01 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 11:08:55 +00:00
|
|
|
return dst[:l]
|
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
func (r *Reader) ReadBool() bool {
|
2014-07-13 07:16:40 +00:00
|
|
|
return r.ReadUint32() != 0
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 16:40:15 +00:00
|
|
|
func (r *Reader) ReadUint16() uint16 {
|
2014-07-13 07:16:40 +00:00
|
|
|
return uint16(r.ReadUint32())
|
2014-02-20 16:40:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 11:08:55 +00:00
|
|
|
func (r *Reader) ReadUint32() uint32 {
|
|
|
|
if r.err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-07-28 09:31:22 +00:00
|
|
|
_, r.err = io.ReadFull(r.r, r.b[:4])
|
2014-05-31 21:30:52 +00:00
|
|
|
if r.err != nil {
|
2014-06-04 08:08:25 +00:00
|
|
|
if debug {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd uint32: %v", r.err)
|
2014-06-04 08:08:25 +00:00
|
|
|
}
|
2014-05-11 22:55:43 +00:00
|
|
|
return 0
|
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
v := uint32(r.b[3]) | uint32(r.b[2])<<8 | uint32(r.b[1])<<16 | uint32(r.b[0])<<24
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd uint32=%d (0x%08x)", v, v)
|
2014-05-17 06:43:01 +00:00
|
|
|
}
|
|
|
|
return v
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) ReadUint64() uint64 {
|
|
|
|
if r.err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-07-28 09:31:22 +00:00
|
|
|
_, r.err = io.ReadFull(r.r, r.b[:8])
|
2014-05-31 21:30:52 +00:00
|
|
|
if r.err != nil {
|
2014-06-04 08:08:25 +00:00
|
|
|
if debug {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd uint64: %v", r.err)
|
2014-06-04 08:08:25 +00:00
|
|
|
}
|
2014-05-31 21:30:52 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
v := uint64(r.b[7]) | uint64(r.b[6])<<8 | uint64(r.b[5])<<16 | uint64(r.b[4])<<24 |
|
2014-02-15 11:08:55 +00:00
|
|
|
uint64(r.b[3])<<32 | uint64(r.b[2])<<40 | uint64(r.b[1])<<48 | uint64(r.b[0])<<56
|
2014-05-31 21:30:52 +00:00
|
|
|
|
2014-05-17 06:43:01 +00:00
|
|
|
if debug {
|
2014-07-28 09:31:22 +00:00
|
|
|
dl.Debugf("rd uint64=%d (0x%016x)", v, v)
|
2014-05-17 06:43:01 +00:00
|
|
|
}
|
|
|
|
return v
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 09:31:22 +00:00
|
|
|
type XDRError struct {
|
|
|
|
op string
|
|
|
|
err error
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 09:31:22 +00:00
|
|
|
func (e XDRError) Error() string {
|
|
|
|
return "xdr " + e.op + ": " + e.err.Error()
|
2014-02-15 11:08:55 +00:00
|
|
|
}
|
2014-05-28 10:30:47 +00:00
|
|
|
|
2014-07-28 09:31:22 +00:00
|
|
|
func (r *Reader) Error() error {
|
|
|
|
if r.err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return XDRError{"read", r.err}
|
2014-05-28 10:30:47 +00:00
|
|
|
}
|