diff --git a/lib/db/leveldb_xdr.go b/lib/db/leveldb_xdr.go index fe9f9399..1348c3cb 100644 --- a/lib/db/leveldb_xdr.go +++ b/lib/db/leveldb_xdr.go @@ -5,9 +5,6 @@ package db import ( - "bytes" - "io" - "github.com/calmh/xdr" ) @@ -22,10 +19,8 @@ fileVersion Structure: \ Vector Structure \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of device | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ device (variable length) \ +\ device (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -37,13 +32,15 @@ struct fileVersion { */ -func (o fileVersion) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o fileVersion) XDRSize() int { + return o.version.XDRSize() + + 4 + len(o.device) + xdr.Padding(len(o.device)) } func (o fileVersion) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o fileVersion) MustMarshalXDR() []byte { @@ -54,37 +51,22 @@ func (o fileVersion) MustMarshalXDR() []byte { return bs } -func (o fileVersion) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o fileVersion) EncodeXDRInto(xw *xdr.Writer) (int, error) { - _, err := o.version.EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err +func (o fileVersion) MarshalXDRInto(m *xdr.Marshaller) error { + if err := o.version.MarshalXDRInto(m); err != nil { + return err } - xw.WriteBytes(o.device) - return xw.Tot(), xw.Error() -} - -func (o *fileVersion) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalBytes(o.device) + return m.Error } func (o *fileVersion) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *fileVersion) DecodeXDRFrom(xr *xdr.Reader) error { - (&o.version).DecodeXDRFrom(xr) - o.device = xr.ReadBytes() - return xr.Error() +func (o *fileVersion) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + (&o.version).UnmarshalXDRFrom(u) + o.device = u.UnmarshalBytes() + return u.Error } /* @@ -108,13 +90,14 @@ struct versionList { */ -func (o versionList) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o versionList) XDRSize() int { + return 4 + xdr.SizeOfSlice(o.versions) } func (o versionList) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o versionList) MustMarshalXDR() []byte { @@ -125,43 +108,35 @@ func (o versionList) MustMarshalXDR() []byte { return bs } -func (o versionList) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o versionList) EncodeXDRInto(xw *xdr.Writer) (int, error) { - xw.WriteUint32(uint32(len(o.versions))) +func (o versionList) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalUint32(uint32(len(o.versions))) for i := range o.versions { - _, err := o.versions[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.versions[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *versionList) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *versionList) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *versionList) DecodeXDRFrom(xr *xdr.Reader) error { - _versionsSize := int(xr.ReadUint32()) +func (o *versionList) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + _versionsSize := int(u.UnmarshalUint32()) if _versionsSize < 0 { return xdr.ElementSizeExceeded("versions", _versionsSize, 0) + } else if _versionsSize == 0 { + o.versions = nil + } else { + if _versionsSize <= len(o.versions) { + o.versions = o.versions[:_versionsSize] + } else { + o.versions = make([]fileVersion, _versionsSize) + } + for i := range o.versions { + (&o.versions[i]).UnmarshalXDRFrom(u) + } } - o.versions = make([]fileVersion, _versionsSize) - for i := range o.versions { - (&o.versions[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } diff --git a/lib/db/truncated.go b/lib/db/truncated.go index d61f1745..a1d31977 100644 --- a/lib/db/truncated.go +++ b/lib/db/truncated.go @@ -7,8 +7,6 @@ package db import ( - "bytes" - "github.com/calmh/xdr" "github.com/syncthing/syncthing/lib/protocol" ) @@ -18,32 +16,32 @@ type FileInfoTruncated struct { } func (o *FileInfoTruncated) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + return o.UnmarshalXDRFrom(&xdr.Unmarshaller{Data: bs}) } -func (o *FileInfoTruncated) DecodeXDRFrom(xr *xdr.Reader) error { - o.Name = xr.ReadStringMax(8192) - o.Flags = xr.ReadUint32() - o.Modified = int64(xr.ReadUint64()) - (&o.Version).DecodeXDRFrom(xr) - o.LocalVersion = int64(xr.ReadUint64()) - _BlocksSize := int(xr.ReadUint32()) +func (o *FileInfoTruncated) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Name = u.UnmarshalStringMax(8192) + o.Flags = u.UnmarshalUint32() + o.Modified = int64(u.UnmarshalUint64()) + (&o.Version).UnmarshalXDRFrom(u) + o.LocalVersion = int64(u.UnmarshalUint64()) + _BlocksSize := int(u.UnmarshalUint32()) if _BlocksSize < 0 { return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) - } - if _BlocksSize > 1000000 { - return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) + } else if _BlocksSize == 0 { + o.Blocks = nil + } else { + if _BlocksSize > 1000000 { + return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) + } + for i := 0; i < _BlocksSize; i++ { + size := int64(u.UnmarshalUint32()) + o.CachedSize += size + u.UnmarshalBytes() + } } - buf := make([]byte, 64) - for i := 0; i < _BlocksSize; i++ { - size := xr.ReadUint32() - o.CachedSize += int64(size) - xr.ReadBytesMaxInto(64, buf) - } - return xr.Error() + return u.Error } func BlocksToSize(num int) int64 { diff --git a/lib/discover/localpackets_xdr.go b/lib/discover/localpackets_xdr.go index dde5a3d5..d353a137 100644 --- a/lib/discover/localpackets_xdr.go +++ b/lib/discover/localpackets_xdr.go @@ -5,9 +5,6 @@ package discover import ( - "bytes" - "io" - "github.com/calmh/xdr" ) @@ -40,13 +37,16 @@ struct Announce { */ -func (o Announce) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Announce) XDRSize() int { + return 4 + + o.This.XDRSize() + + 4 + xdr.SizeOfSlice(o.Extra) } func (o Announce) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Announce) MustMarshalXDR() []byte { @@ -57,58 +57,49 @@ func (o Announce) MustMarshalXDR() []byte { return bs } -func (o Announce) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Announce) EncodeXDRInto(xw *xdr.Writer) (int, error) { - xw.WriteUint32(o.Magic) - _, err := o.This.EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err +func (o Announce) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalUint32(o.Magic) + if err := o.This.MarshalXDRInto(m); err != nil { + return err } if l := len(o.Extra); l > 16 { - return xw.Tot(), xdr.ElementSizeExceeded("Extra", l, 16) + return xdr.ElementSizeExceeded("Extra", l, 16) } - xw.WriteUint32(uint32(len(o.Extra))) + m.MarshalUint32(uint32(len(o.Extra))) for i := range o.Extra { - _, err := o.Extra[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Extra[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *Announce) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *Announce) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Announce) DecodeXDRFrom(xr *xdr.Reader) error { - o.Magic = xr.ReadUint32() - (&o.This).DecodeXDRFrom(xr) - _ExtraSize := int(xr.ReadUint32()) +func (o *Announce) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Magic = u.UnmarshalUint32() + (&o.This).UnmarshalXDRFrom(u) + _ExtraSize := int(u.UnmarshalUint32()) if _ExtraSize < 0 { return xdr.ElementSizeExceeded("Extra", _ExtraSize, 16) + } else if _ExtraSize == 0 { + o.Extra = nil + } else { + if _ExtraSize > 16 { + return xdr.ElementSizeExceeded("Extra", _ExtraSize, 16) + } + if _ExtraSize <= len(o.Extra) { + o.Extra = o.Extra[:_ExtraSize] + } else { + o.Extra = make([]Device, _ExtraSize) + } + for i := range o.Extra { + (&o.Extra[i]).UnmarshalXDRFrom(u) + } } - if _ExtraSize > 16 { - return xdr.ElementSizeExceeded("Extra", _ExtraSize, 16) - } - o.Extra = make([]Device, _ExtraSize) - for i := range o.Extra { - (&o.Extra[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -118,10 +109,8 @@ Device Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of ID | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ ID (variable length) \ +\ ID (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Number of Addresses | @@ -146,13 +135,16 @@ struct Device { */ -func (o Device) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Device) XDRSize() int { + return 4 + len(o.ID) + xdr.Padding(len(o.ID)) + + 4 + xdr.SizeOfSlice(o.Addresses) + + 4 + xdr.SizeOfSlice(o.Relays) } func (o Device) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Device) MustMarshalXDR() []byte { @@ -163,77 +155,75 @@ func (o Device) MustMarshalXDR() []byte { return bs } -func (o Device) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Device) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o Device) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.ID); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("ID", l, 32) + return xdr.ElementSizeExceeded("ID", l, 32) } - xw.WriteBytes(o.ID) + m.MarshalBytes(o.ID) if l := len(o.Addresses); l > 16 { - return xw.Tot(), xdr.ElementSizeExceeded("Addresses", l, 16) + return xdr.ElementSizeExceeded("Addresses", l, 16) } - xw.WriteUint32(uint32(len(o.Addresses))) + m.MarshalUint32(uint32(len(o.Addresses))) for i := range o.Addresses { - _, err := o.Addresses[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Addresses[i].MarshalXDRInto(m); err != nil { + return err } } if l := len(o.Relays); l > 16 { - return xw.Tot(), xdr.ElementSizeExceeded("Relays", l, 16) + return xdr.ElementSizeExceeded("Relays", l, 16) } - xw.WriteUint32(uint32(len(o.Relays))) + m.MarshalUint32(uint32(len(o.Relays))) for i := range o.Relays { - _, err := o.Relays[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Relays[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *Device) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *Device) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Device) DecodeXDRFrom(xr *xdr.Reader) error { - o.ID = xr.ReadBytesMax(32) - _AddressesSize := int(xr.ReadUint32()) +func (o *Device) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.ID = u.UnmarshalBytesMax(32) + _AddressesSize := int(u.UnmarshalUint32()) if _AddressesSize < 0 { return xdr.ElementSizeExceeded("Addresses", _AddressesSize, 16) + } else if _AddressesSize == 0 { + o.Addresses = nil + } else { + if _AddressesSize > 16 { + return xdr.ElementSizeExceeded("Addresses", _AddressesSize, 16) + } + if _AddressesSize <= len(o.Addresses) { + o.Addresses = o.Addresses[:_AddressesSize] + } else { + o.Addresses = make([]Address, _AddressesSize) + } + for i := range o.Addresses { + (&o.Addresses[i]).UnmarshalXDRFrom(u) + } } - if _AddressesSize > 16 { - return xdr.ElementSizeExceeded("Addresses", _AddressesSize, 16) - } - o.Addresses = make([]Address, _AddressesSize) - for i := range o.Addresses { - (&o.Addresses[i]).DecodeXDRFrom(xr) - } - _RelaysSize := int(xr.ReadUint32()) + _RelaysSize := int(u.UnmarshalUint32()) if _RelaysSize < 0 { return xdr.ElementSizeExceeded("Relays", _RelaysSize, 16) + } else if _RelaysSize == 0 { + o.Relays = nil + } else { + if _RelaysSize > 16 { + return xdr.ElementSizeExceeded("Relays", _RelaysSize, 16) + } + if _RelaysSize <= len(o.Relays) { + o.Relays = o.Relays[:_RelaysSize] + } else { + o.Relays = make([]Relay, _RelaysSize) + } + for i := range o.Relays { + (&o.Relays[i]).UnmarshalXDRFrom(u) + } } - if _RelaysSize > 16 { - return xdr.ElementSizeExceeded("Relays", _RelaysSize, 16) - } - o.Relays = make([]Relay, _RelaysSize) - for i := range o.Relays { - (&o.Relays[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -243,10 +233,8 @@ Address Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of URL | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ URL (variable length) \ +\ URL (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -257,13 +245,14 @@ struct Address { */ -func (o Address) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Address) XDRSize() int { + return 4 + len(o.URL) + xdr.Padding(len(o.URL)) } func (o Address) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Address) MustMarshalXDR() []byte { @@ -274,35 +263,21 @@ func (o Address) MustMarshalXDR() []byte { return bs } -func (o Address) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Address) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o Address) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.URL); l > 2083 { - return xw.Tot(), xdr.ElementSizeExceeded("URL", l, 2083) + return xdr.ElementSizeExceeded("URL", l, 2083) } - xw.WriteString(o.URL) - return xw.Tot(), xw.Error() -} - -func (o *Address) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalString(o.URL) + return m.Error } func (o *Address) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Address) DecodeXDRFrom(xr *xdr.Reader) error { - o.URL = xr.ReadStringMax(2083) - return xr.Error() +func (o *Address) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.URL = u.UnmarshalStringMax(2083) + return u.Error } /* @@ -312,10 +287,8 @@ Relay Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of URL | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ URL (variable length) \ +\ URL (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Latency | @@ -329,13 +302,14 @@ struct Relay { */ -func (o Relay) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Relay) XDRSize() int { + return 4 + len(o.URL) + xdr.Padding(len(o.URL)) + 4 } func (o Relay) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Relay) MustMarshalXDR() []byte { @@ -346,35 +320,21 @@ func (o Relay) MustMarshalXDR() []byte { return bs } -func (o Relay) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Relay) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o Relay) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.URL); l > 2083 { - return xw.Tot(), xdr.ElementSizeExceeded("URL", l, 2083) + return xdr.ElementSizeExceeded("URL", l, 2083) } - xw.WriteString(o.URL) - xw.WriteUint32(uint32(o.Latency)) - return xw.Tot(), xw.Error() -} - -func (o *Relay) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalString(o.URL) + m.MarshalUint32(uint32(o.Latency)) + return m.Error } func (o *Relay) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Relay) DecodeXDRFrom(xr *xdr.Reader) error { - o.URL = xr.ReadStringMax(2083) - o.Latency = int32(xr.ReadUint32()) - return xr.Error() +func (o *Relay) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.URL = u.UnmarshalStringMax(2083) + o.Latency = int32(u.UnmarshalUint32()) + return u.Error } diff --git a/lib/protocol/message_xdr.go b/lib/protocol/message_xdr.go index e2ea7ca6..4b4aff73 100644 --- a/lib/protocol/message_xdr.go +++ b/lib/protocol/message_xdr.go @@ -5,9 +5,6 @@ package protocol import ( - "bytes" - "io" - "github.com/calmh/xdr" ) @@ -18,10 +15,8 @@ IndexMessage Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Folder | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Folder (variable length) \ +\ Folder (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Number of Files | @@ -49,13 +44,16 @@ struct IndexMessage { */ -func (o IndexMessage) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o IndexMessage) XDRSize() int { + return 4 + len(o.Folder) + xdr.Padding(len(o.Folder)) + + 4 + xdr.SizeOfSlice(o.Files) + 4 + + 4 + xdr.SizeOfSlice(o.Options) } func (o IndexMessage) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o IndexMessage) MustMarshalXDR() []byte { @@ -66,79 +64,77 @@ func (o IndexMessage) MustMarshalXDR() []byte { return bs } -func (o IndexMessage) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o IndexMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o IndexMessage) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.Folder); l > 256 { - return xw.Tot(), xdr.ElementSizeExceeded("Folder", l, 256) + return xdr.ElementSizeExceeded("Folder", l, 256) } - xw.WriteString(o.Folder) + m.MarshalString(o.Folder) if l := len(o.Files); l > 1000000 { - return xw.Tot(), xdr.ElementSizeExceeded("Files", l, 1000000) + return xdr.ElementSizeExceeded("Files", l, 1000000) } - xw.WriteUint32(uint32(len(o.Files))) + m.MarshalUint32(uint32(len(o.Files))) for i := range o.Files { - _, err := o.Files[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Files[i].MarshalXDRInto(m); err != nil { + return err } } - xw.WriteUint32(o.Flags) + m.MarshalUint32(o.Flags) if l := len(o.Options); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Options", l, 64) + return xdr.ElementSizeExceeded("Options", l, 64) } - xw.WriteUint32(uint32(len(o.Options))) + m.MarshalUint32(uint32(len(o.Options))) for i := range o.Options { - _, err := o.Options[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Options[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *IndexMessage) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *IndexMessage) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *IndexMessage) DecodeXDRFrom(xr *xdr.Reader) error { - o.Folder = xr.ReadStringMax(256) - _FilesSize := int(xr.ReadUint32()) +func (o *IndexMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Folder = u.UnmarshalStringMax(256) + _FilesSize := int(u.UnmarshalUint32()) if _FilesSize < 0 { return xdr.ElementSizeExceeded("Files", _FilesSize, 1000000) + } else if _FilesSize == 0 { + o.Files = nil + } else { + if _FilesSize > 1000000 { + return xdr.ElementSizeExceeded("Files", _FilesSize, 1000000) + } + if _FilesSize <= len(o.Files) { + o.Files = o.Files[:_FilesSize] + } else { + o.Files = make([]FileInfo, _FilesSize) + } + for i := range o.Files { + (&o.Files[i]).UnmarshalXDRFrom(u) + } } - if _FilesSize > 1000000 { - return xdr.ElementSizeExceeded("Files", _FilesSize, 1000000) - } - o.Files = make([]FileInfo, _FilesSize) - for i := range o.Files { - (&o.Files[i]).DecodeXDRFrom(xr) - } - o.Flags = xr.ReadUint32() - _OptionsSize := int(xr.ReadUint32()) + o.Flags = u.UnmarshalUint32() + _OptionsSize := int(u.UnmarshalUint32()) if _OptionsSize < 0 { return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } else if _OptionsSize == 0 { + o.Options = nil + } else { + if _OptionsSize > 64 { + return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } + if _OptionsSize <= len(o.Options) { + o.Options = o.Options[:_OptionsSize] + } else { + o.Options = make([]Option, _OptionsSize) + } + for i := range o.Options { + (&o.Options[i]).UnmarshalXDRFrom(u) + } } - if _OptionsSize > 64 { - return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) - } - o.Options = make([]Option, _OptionsSize) - for i := range o.Options { - (&o.Options[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -148,10 +144,8 @@ FileInfo Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Name | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Name (variable length) \ +\ Name (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Flags | @@ -187,13 +181,16 @@ struct FileInfo { */ -func (o FileInfo) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o FileInfo) XDRSize() int { + return 4 + len(o.Name) + xdr.Padding(len(o.Name)) + 4 + 8 + + o.Version.XDRSize() + 8 + + 4 + xdr.SizeOfSlice(o.Blocks) } func (o FileInfo) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o FileInfo) MustMarshalXDR() []byte { @@ -204,67 +201,58 @@ func (o FileInfo) MustMarshalXDR() []byte { return bs } -func (o FileInfo) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o FileInfo) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o FileInfo) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.Name); l > 8192 { - return xw.Tot(), xdr.ElementSizeExceeded("Name", l, 8192) + return xdr.ElementSizeExceeded("Name", l, 8192) } - xw.WriteString(o.Name) - xw.WriteUint32(o.Flags) - xw.WriteUint64(uint64(o.Modified)) - _, err := o.Version.EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + m.MarshalString(o.Name) + m.MarshalUint32(o.Flags) + m.MarshalUint64(uint64(o.Modified)) + if err := o.Version.MarshalXDRInto(m); err != nil { + return err } - xw.WriteUint64(uint64(o.LocalVersion)) + m.MarshalUint64(uint64(o.LocalVersion)) if l := len(o.Blocks); l > 1000000 { - return xw.Tot(), xdr.ElementSizeExceeded("Blocks", l, 1000000) + return xdr.ElementSizeExceeded("Blocks", l, 1000000) } - xw.WriteUint32(uint32(len(o.Blocks))) + m.MarshalUint32(uint32(len(o.Blocks))) for i := range o.Blocks { - _, err := o.Blocks[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Blocks[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *FileInfo) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *FileInfo) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *FileInfo) DecodeXDRFrom(xr *xdr.Reader) error { - o.Name = xr.ReadStringMax(8192) - o.Flags = xr.ReadUint32() - o.Modified = int64(xr.ReadUint64()) - (&o.Version).DecodeXDRFrom(xr) - o.LocalVersion = int64(xr.ReadUint64()) - _BlocksSize := int(xr.ReadUint32()) +func (o *FileInfo) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Name = u.UnmarshalStringMax(8192) + o.Flags = u.UnmarshalUint32() + o.Modified = int64(u.UnmarshalUint64()) + (&o.Version).UnmarshalXDRFrom(u) + o.LocalVersion = int64(u.UnmarshalUint64()) + _BlocksSize := int(u.UnmarshalUint32()) if _BlocksSize < 0 { return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) + } else if _BlocksSize == 0 { + o.Blocks = nil + } else { + if _BlocksSize > 1000000 { + return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) + } + if _BlocksSize <= len(o.Blocks) { + o.Blocks = o.Blocks[:_BlocksSize] + } else { + o.Blocks = make([]BlockInfo, _BlocksSize) + } + for i := range o.Blocks { + (&o.Blocks[i]).UnmarshalXDRFrom(u) + } } - if _BlocksSize > 1000000 { - return xdr.ElementSizeExceeded("Blocks", _BlocksSize, 1000000) - } - o.Blocks = make([]BlockInfo, _BlocksSize) - for i := range o.Blocks { - (&o.Blocks[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -276,10 +264,8 @@ BlockInfo Structure: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Hash | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Hash (variable length) \ +\ Hash (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -291,13 +277,15 @@ struct BlockInfo { */ -func (o BlockInfo) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o BlockInfo) XDRSize() int { + return 4 + + 4 + len(o.Hash) + xdr.Padding(len(o.Hash)) } func (o BlockInfo) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o BlockInfo) MustMarshalXDR() []byte { @@ -308,37 +296,23 @@ func (o BlockInfo) MustMarshalXDR() []byte { return bs } -func (o BlockInfo) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o BlockInfo) EncodeXDRInto(xw *xdr.Writer) (int, error) { - xw.WriteUint32(uint32(o.Size)) +func (o BlockInfo) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalUint32(uint32(o.Size)) if l := len(o.Hash); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Hash", l, 64) + return xdr.ElementSizeExceeded("Hash", l, 64) } - xw.WriteBytes(o.Hash) - return xw.Tot(), xw.Error() -} - -func (o *BlockInfo) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalBytes(o.Hash) + return m.Error } func (o *BlockInfo) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *BlockInfo) DecodeXDRFrom(xr *xdr.Reader) error { - o.Size = int32(xr.ReadUint32()) - o.Hash = xr.ReadBytesMax(64) - return xr.Error() +func (o *BlockInfo) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Size = int32(u.UnmarshalUint32()) + o.Hash = u.UnmarshalBytesMax(64) + return u.Error } /* @@ -348,16 +322,12 @@ RequestMessage Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Folder | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Folder (variable length) \ +\ Folder (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Name | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Name (variable length) \ +\ Name (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | @@ -366,10 +336,8 @@ RequestMessage Structure: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Size | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Hash | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Hash (variable length) \ +\ Hash (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Flags | @@ -394,13 +362,17 @@ struct RequestMessage { */ -func (o RequestMessage) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o RequestMessage) XDRSize() int { + return 4 + len(o.Folder) + xdr.Padding(len(o.Folder)) + + 4 + len(o.Name) + xdr.Padding(len(o.Name)) + 8 + 4 + + 4 + len(o.Hash) + xdr.Padding(len(o.Hash)) + 4 + + 4 + xdr.SizeOfSlice(o.Options) } func (o RequestMessage) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o RequestMessage) MustMarshalXDR() []byte { @@ -411,72 +383,64 @@ func (o RequestMessage) MustMarshalXDR() []byte { return bs } -func (o RequestMessage) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o RequestMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o RequestMessage) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.Folder); l > 256 { - return xw.Tot(), xdr.ElementSizeExceeded("Folder", l, 256) + return xdr.ElementSizeExceeded("Folder", l, 256) } - xw.WriteString(o.Folder) + m.MarshalString(o.Folder) if l := len(o.Name); l > 8192 { - return xw.Tot(), xdr.ElementSizeExceeded("Name", l, 8192) + return xdr.ElementSizeExceeded("Name", l, 8192) } - xw.WriteString(o.Name) - xw.WriteUint64(uint64(o.Offset)) - xw.WriteUint32(uint32(o.Size)) + m.MarshalString(o.Name) + m.MarshalUint64(uint64(o.Offset)) + m.MarshalUint32(uint32(o.Size)) if l := len(o.Hash); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Hash", l, 64) + return xdr.ElementSizeExceeded("Hash", l, 64) } - xw.WriteBytes(o.Hash) - xw.WriteUint32(o.Flags) + m.MarshalBytes(o.Hash) + m.MarshalUint32(o.Flags) if l := len(o.Options); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Options", l, 64) + return xdr.ElementSizeExceeded("Options", l, 64) } - xw.WriteUint32(uint32(len(o.Options))) + m.MarshalUint32(uint32(len(o.Options))) for i := range o.Options { - _, err := o.Options[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Options[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *RequestMessage) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *RequestMessage) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *RequestMessage) DecodeXDRFrom(xr *xdr.Reader) error { - o.Folder = xr.ReadStringMax(256) - o.Name = xr.ReadStringMax(8192) - o.Offset = int64(xr.ReadUint64()) - o.Size = int32(xr.ReadUint32()) - o.Hash = xr.ReadBytesMax(64) - o.Flags = xr.ReadUint32() - _OptionsSize := int(xr.ReadUint32()) +func (o *RequestMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Folder = u.UnmarshalStringMax(256) + o.Name = u.UnmarshalStringMax(8192) + o.Offset = int64(u.UnmarshalUint64()) + o.Size = int32(u.UnmarshalUint32()) + o.Hash = u.UnmarshalBytesMax(64) + o.Flags = u.UnmarshalUint32() + _OptionsSize := int(u.UnmarshalUint32()) if _OptionsSize < 0 { return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } else if _OptionsSize == 0 { + o.Options = nil + } else { + if _OptionsSize > 64 { + return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } + if _OptionsSize <= len(o.Options) { + o.Options = o.Options[:_OptionsSize] + } else { + o.Options = make([]Option, _OptionsSize) + } + for i := range o.Options { + (&o.Options[i]).UnmarshalXDRFrom(u) + } } - if _OptionsSize > 64 { - return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) - } - o.Options = make([]Option, _OptionsSize) - for i := range o.Options { - (&o.Options[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -486,10 +450,8 @@ ResponseMessage Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Data | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Data (variable length) \ +\ Data (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Code | @@ -503,13 +465,14 @@ struct ResponseMessage { */ -func (o ResponseMessage) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o ResponseMessage) XDRSize() int { + return 4 + len(o.Data) + xdr.Padding(len(o.Data)) + 4 } func (o ResponseMessage) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o ResponseMessage) MustMarshalXDR() []byte { @@ -520,34 +483,20 @@ func (o ResponseMessage) MustMarshalXDR() []byte { return bs } -func (o ResponseMessage) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o ResponseMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { - xw.WriteBytes(o.Data) - xw.WriteUint32(uint32(o.Code)) - return xw.Tot(), xw.Error() -} - -func (o *ResponseMessage) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) +func (o ResponseMessage) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalBytes(o.Data) + m.MarshalUint32(uint32(o.Code)) + return m.Error } func (o *ResponseMessage) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *ResponseMessage) DecodeXDRFrom(xr *xdr.Reader) error { - o.Data = xr.ReadBytes() - o.Code = int32(xr.ReadUint32()) - return xr.Error() +func (o *ResponseMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Data = u.UnmarshalBytes() + o.Code = int32(u.UnmarshalUint32()) + return u.Error } /* @@ -557,22 +506,16 @@ ClusterConfigMessage Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Device Name | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Device Name (variable length) \ +\ Device Name (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Client Name | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Client Name (variable length) \ +\ Client Name (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Client Version | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Client Version (variable length) \ +\ Client Version (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Number of Folders | @@ -599,13 +542,18 @@ struct ClusterConfigMessage { */ -func (o ClusterConfigMessage) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o ClusterConfigMessage) XDRSize() int { + return 4 + len(o.DeviceName) + xdr.Padding(len(o.DeviceName)) + + 4 + len(o.ClientName) + xdr.Padding(len(o.ClientName)) + + 4 + len(o.ClientVersion) + xdr.Padding(len(o.ClientVersion)) + + 4 + xdr.SizeOfSlice(o.Folders) + + 4 + xdr.SizeOfSlice(o.Options) } func (o ClusterConfigMessage) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o ClusterConfigMessage) MustMarshalXDR() []byte { @@ -616,87 +564,85 @@ func (o ClusterConfigMessage) MustMarshalXDR() []byte { return bs } -func (o ClusterConfigMessage) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o ClusterConfigMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o ClusterConfigMessage) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.DeviceName); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("DeviceName", l, 64) + return xdr.ElementSizeExceeded("DeviceName", l, 64) } - xw.WriteString(o.DeviceName) + m.MarshalString(o.DeviceName) if l := len(o.ClientName); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("ClientName", l, 64) + return xdr.ElementSizeExceeded("ClientName", l, 64) } - xw.WriteString(o.ClientName) + m.MarshalString(o.ClientName) if l := len(o.ClientVersion); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("ClientVersion", l, 64) + return xdr.ElementSizeExceeded("ClientVersion", l, 64) } - xw.WriteString(o.ClientVersion) + m.MarshalString(o.ClientVersion) if l := len(o.Folders); l > 1000000 { - return xw.Tot(), xdr.ElementSizeExceeded("Folders", l, 1000000) + return xdr.ElementSizeExceeded("Folders", l, 1000000) } - xw.WriteUint32(uint32(len(o.Folders))) + m.MarshalUint32(uint32(len(o.Folders))) for i := range o.Folders { - _, err := o.Folders[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Folders[i].MarshalXDRInto(m); err != nil { + return err } } if l := len(o.Options); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Options", l, 64) + return xdr.ElementSizeExceeded("Options", l, 64) } - xw.WriteUint32(uint32(len(o.Options))) + m.MarshalUint32(uint32(len(o.Options))) for i := range o.Options { - _, err := o.Options[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Options[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *ClusterConfigMessage) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *ClusterConfigMessage) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *ClusterConfigMessage) DecodeXDRFrom(xr *xdr.Reader) error { - o.DeviceName = xr.ReadStringMax(64) - o.ClientName = xr.ReadStringMax(64) - o.ClientVersion = xr.ReadStringMax(64) - _FoldersSize := int(xr.ReadUint32()) +func (o *ClusterConfigMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.DeviceName = u.UnmarshalStringMax(64) + o.ClientName = u.UnmarshalStringMax(64) + o.ClientVersion = u.UnmarshalStringMax(64) + _FoldersSize := int(u.UnmarshalUint32()) if _FoldersSize < 0 { return xdr.ElementSizeExceeded("Folders", _FoldersSize, 1000000) + } else if _FoldersSize == 0 { + o.Folders = nil + } else { + if _FoldersSize > 1000000 { + return xdr.ElementSizeExceeded("Folders", _FoldersSize, 1000000) + } + if _FoldersSize <= len(o.Folders) { + o.Folders = o.Folders[:_FoldersSize] + } else { + o.Folders = make([]Folder, _FoldersSize) + } + for i := range o.Folders { + (&o.Folders[i]).UnmarshalXDRFrom(u) + } } - if _FoldersSize > 1000000 { - return xdr.ElementSizeExceeded("Folders", _FoldersSize, 1000000) - } - o.Folders = make([]Folder, _FoldersSize) - for i := range o.Folders { - (&o.Folders[i]).DecodeXDRFrom(xr) - } - _OptionsSize := int(xr.ReadUint32()) + _OptionsSize := int(u.UnmarshalUint32()) if _OptionsSize < 0 { return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } else if _OptionsSize == 0 { + o.Options = nil + } else { + if _OptionsSize > 64 { + return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } + if _OptionsSize <= len(o.Options) { + o.Options = o.Options[:_OptionsSize] + } else { + o.Options = make([]Option, _OptionsSize) + } + for i := range o.Options { + (&o.Options[i]).UnmarshalXDRFrom(u) + } } - if _OptionsSize > 64 { - return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) - } - o.Options = make([]Option, _OptionsSize) - for i := range o.Options { - (&o.Options[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -706,10 +652,8 @@ Folder Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of ID | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ ID (variable length) \ +\ ID (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Number of Devices | @@ -737,13 +681,16 @@ struct Folder { */ -func (o Folder) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Folder) XDRSize() int { + return 4 + len(o.ID) + xdr.Padding(len(o.ID)) + + 4 + xdr.SizeOfSlice(o.Devices) + 4 + + 4 + xdr.SizeOfSlice(o.Options) } func (o Folder) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Folder) MustMarshalXDR() []byte { @@ -754,79 +701,77 @@ func (o Folder) MustMarshalXDR() []byte { return bs } -func (o Folder) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Folder) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o Folder) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.ID); l > 256 { - return xw.Tot(), xdr.ElementSizeExceeded("ID", l, 256) + return xdr.ElementSizeExceeded("ID", l, 256) } - xw.WriteString(o.ID) + m.MarshalString(o.ID) if l := len(o.Devices); l > 1000000 { - return xw.Tot(), xdr.ElementSizeExceeded("Devices", l, 1000000) + return xdr.ElementSizeExceeded("Devices", l, 1000000) } - xw.WriteUint32(uint32(len(o.Devices))) + m.MarshalUint32(uint32(len(o.Devices))) for i := range o.Devices { - _, err := o.Devices[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Devices[i].MarshalXDRInto(m); err != nil { + return err } } - xw.WriteUint32(o.Flags) + m.MarshalUint32(o.Flags) if l := len(o.Options); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Options", l, 64) + return xdr.ElementSizeExceeded("Options", l, 64) } - xw.WriteUint32(uint32(len(o.Options))) + m.MarshalUint32(uint32(len(o.Options))) for i := range o.Options { - _, err := o.Options[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Options[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *Folder) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *Folder) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Folder) DecodeXDRFrom(xr *xdr.Reader) error { - o.ID = xr.ReadStringMax(256) - _DevicesSize := int(xr.ReadUint32()) +func (o *Folder) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.ID = u.UnmarshalStringMax(256) + _DevicesSize := int(u.UnmarshalUint32()) if _DevicesSize < 0 { return xdr.ElementSizeExceeded("Devices", _DevicesSize, 1000000) + } else if _DevicesSize == 0 { + o.Devices = nil + } else { + if _DevicesSize > 1000000 { + return xdr.ElementSizeExceeded("Devices", _DevicesSize, 1000000) + } + if _DevicesSize <= len(o.Devices) { + o.Devices = o.Devices[:_DevicesSize] + } else { + o.Devices = make([]Device, _DevicesSize) + } + for i := range o.Devices { + (&o.Devices[i]).UnmarshalXDRFrom(u) + } } - if _DevicesSize > 1000000 { - return xdr.ElementSizeExceeded("Devices", _DevicesSize, 1000000) - } - o.Devices = make([]Device, _DevicesSize) - for i := range o.Devices { - (&o.Devices[i]).DecodeXDRFrom(xr) - } - o.Flags = xr.ReadUint32() - _OptionsSize := int(xr.ReadUint32()) + o.Flags = u.UnmarshalUint32() + _OptionsSize := int(u.UnmarshalUint32()) if _OptionsSize < 0 { return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } else if _OptionsSize == 0 { + o.Options = nil + } else { + if _OptionsSize > 64 { + return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } + if _OptionsSize <= len(o.Options) { + o.Options = o.Options[:_OptionsSize] + } else { + o.Options = make([]Option, _OptionsSize) + } + for i := range o.Options { + (&o.Options[i]).UnmarshalXDRFrom(u) + } } - if _OptionsSize > 64 { - return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) - } - o.Options = make([]Option, _OptionsSize) - for i := range o.Options { - (&o.Options[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -836,32 +781,26 @@ Device Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of ID | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ ID (variable length) \ +\ ID (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Name | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Name (variable length) \ +\ Name (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Number of Addresses | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Addresses | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Addresses (variable length) \ +/ / +\ Addresses (length + padded data) \ +/ / / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Compression | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Cert Name | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Cert Name (variable length) \ +\ Cert Name (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | @@ -891,13 +830,18 @@ struct Device { */ -func (o Device) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Device) XDRSize() int { + return 4 + len(o.ID) + xdr.Padding(len(o.ID)) + + 4 + len(o.Name) + xdr.Padding(len(o.Name)) + + 4 + xdr.SizeOfSlice(o.Addresses) + 4 + + 4 + len(o.CertName) + xdr.Padding(len(o.CertName)) + 8 + 4 + + 4 + xdr.SizeOfSlice(o.Options) } func (o Device) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Device) MustMarshalXDR() []byte { @@ -908,90 +852,92 @@ func (o Device) MustMarshalXDR() []byte { return bs } -func (o Device) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Device) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o Device) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.ID); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("ID", l, 32) + return xdr.ElementSizeExceeded("ID", l, 32) } - xw.WriteBytes(o.ID) + m.MarshalBytes(o.ID) if l := len(o.Name); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Name", l, 64) + return xdr.ElementSizeExceeded("Name", l, 64) } - xw.WriteString(o.Name) + m.MarshalString(o.Name) if l := len(o.Addresses); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Addresses", l, 64) + return xdr.ElementSizeExceeded("Addresses", l, 64) } - xw.WriteUint32(uint32(len(o.Addresses))) + m.MarshalUint32(uint32(len(o.Addresses))) for i := range o.Addresses { - xw.WriteString(o.Addresses[i]) + m.MarshalString(o.Addresses[i]) } - xw.WriteUint32(o.Compression) + m.MarshalUint32(o.Compression) if l := len(o.CertName); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("CertName", l, 64) + return xdr.ElementSizeExceeded("CertName", l, 64) } - xw.WriteString(o.CertName) - xw.WriteUint64(uint64(o.MaxLocalVersion)) - xw.WriteUint32(o.Flags) + m.MarshalString(o.CertName) + m.MarshalUint64(uint64(o.MaxLocalVersion)) + m.MarshalUint32(o.Flags) if l := len(o.Options); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Options", l, 64) + return xdr.ElementSizeExceeded("Options", l, 64) } - xw.WriteUint32(uint32(len(o.Options))) + m.MarshalUint32(uint32(len(o.Options))) for i := range o.Options { - _, err := o.Options[i].EncodeXDRInto(xw) - if err != nil { - return xw.Tot(), err + if err := o.Options[i].MarshalXDRInto(m); err != nil { + return err } } - return xw.Tot(), xw.Error() -} - -func (o *Device) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + return m.Error } func (o *Device) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Device) DecodeXDRFrom(xr *xdr.Reader) error { - o.ID = xr.ReadBytesMax(32) - o.Name = xr.ReadStringMax(64) - _AddressesSize := int(xr.ReadUint32()) +func (o *Device) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.ID = u.UnmarshalBytesMax(32) + o.Name = u.UnmarshalStringMax(64) + _AddressesSize := int(u.UnmarshalUint32()) if _AddressesSize < 0 { return xdr.ElementSizeExceeded("Addresses", _AddressesSize, 64) + } else if _AddressesSize == 0 { + o.Addresses = nil + } else { + if _AddressesSize > 64 { + return xdr.ElementSizeExceeded("Addresses", _AddressesSize, 64) + } + if _AddressesSize <= len(o.Addresses) { + for i := _AddressesSize; i < len(o.Addresses); i++ { + o.Addresses[i] = "" + } + o.Addresses = o.Addresses[:_AddressesSize] + } else { + o.Addresses = make([]string, _AddressesSize) + } + for i := range o.Addresses { + o.Addresses[i] = u.UnmarshalStringMax(2083) + } } - if _AddressesSize > 64 { - return xdr.ElementSizeExceeded("Addresses", _AddressesSize, 64) - } - o.Addresses = make([]string, _AddressesSize) - for i := range o.Addresses { - o.Addresses[i] = xr.ReadStringMax(2083) - } - o.Compression = xr.ReadUint32() - o.CertName = xr.ReadStringMax(64) - o.MaxLocalVersion = int64(xr.ReadUint64()) - o.Flags = xr.ReadUint32() - _OptionsSize := int(xr.ReadUint32()) + o.Compression = u.UnmarshalUint32() + o.CertName = u.UnmarshalStringMax(64) + o.MaxLocalVersion = int64(u.UnmarshalUint64()) + o.Flags = u.UnmarshalUint32() + _OptionsSize := int(u.UnmarshalUint32()) if _OptionsSize < 0 { return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } else if _OptionsSize == 0 { + o.Options = nil + } else { + if _OptionsSize > 64 { + return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) + } + if _OptionsSize <= len(o.Options) { + o.Options = o.Options[:_OptionsSize] + } else { + o.Options = make([]Option, _OptionsSize) + } + for i := range o.Options { + (&o.Options[i]).UnmarshalXDRFrom(u) + } } - if _OptionsSize > 64 { - return xdr.ElementSizeExceeded("Options", _OptionsSize, 64) - } - o.Options = make([]Option, _OptionsSize) - for i := range o.Options { - (&o.Options[i]).DecodeXDRFrom(xr) - } - return xr.Error() + return u.Error } /* @@ -1001,16 +947,12 @@ Option Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Key | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Key (variable length) \ +\ Key (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Value | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Value (variable length) \ +\ Value (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -1022,13 +964,15 @@ struct Option { */ -func (o Option) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Option) XDRSize() int { + return 4 + len(o.Key) + xdr.Padding(len(o.Key)) + + 4 + len(o.Value) + xdr.Padding(len(o.Value)) } func (o Option) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Option) MustMarshalXDR() []byte { @@ -1039,40 +983,26 @@ func (o Option) MustMarshalXDR() []byte { return bs } -func (o Option) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Option) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o Option) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.Key); l > 64 { - return xw.Tot(), xdr.ElementSizeExceeded("Key", l, 64) + return xdr.ElementSizeExceeded("Key", l, 64) } - xw.WriteString(o.Key) + m.MarshalString(o.Key) if l := len(o.Value); l > 1024 { - return xw.Tot(), xdr.ElementSizeExceeded("Value", l, 1024) + return xdr.ElementSizeExceeded("Value", l, 1024) } - xw.WriteString(o.Value) - return xw.Tot(), xw.Error() -} - -func (o *Option) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalString(o.Value) + return m.Error } func (o *Option) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Option) DecodeXDRFrom(xr *xdr.Reader) error { - o.Key = xr.ReadStringMax(64) - o.Value = xr.ReadStringMax(1024) - return xr.Error() +func (o *Option) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Key = u.UnmarshalStringMax(64) + o.Value = u.UnmarshalStringMax(1024) + return u.Error } /* @@ -1082,10 +1012,8 @@ CloseMessage Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Reason | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Reason (variable length) \ +\ Reason (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Code | @@ -1099,13 +1027,14 @@ struct CloseMessage { */ -func (o CloseMessage) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o CloseMessage) XDRSize() int { + return 4 + len(o.Reason) + xdr.Padding(len(o.Reason)) + 4 } func (o CloseMessage) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o CloseMessage) MustMarshalXDR() []byte { @@ -1116,37 +1045,23 @@ func (o CloseMessage) MustMarshalXDR() []byte { return bs } -func (o CloseMessage) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o CloseMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o CloseMessage) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.Reason); l > 1024 { - return xw.Tot(), xdr.ElementSizeExceeded("Reason", l, 1024) + return xdr.ElementSizeExceeded("Reason", l, 1024) } - xw.WriteString(o.Reason) - xw.WriteUint32(uint32(o.Code)) - return xw.Tot(), xw.Error() -} - -func (o *CloseMessage) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalString(o.Reason) + m.MarshalUint32(uint32(o.Code)) + return m.Error } func (o *CloseMessage) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *CloseMessage) DecodeXDRFrom(xr *xdr.Reader) error { - o.Reason = xr.ReadStringMax(1024) - o.Code = int32(xr.ReadUint32()) - return xr.Error() +func (o *CloseMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Reason = u.UnmarshalStringMax(1024) + o.Code = int32(u.UnmarshalUint32()) + return u.Error } /* @@ -1160,10 +1075,9 @@ struct EmptyMessage { */ -func (o EmptyMessage) EncodeXDR(w io.Writer) (int, error) { - return 0, nil +func (o EmptyMessage) XDRSize() int { + return 0 } - func (o EmptyMessage) MarshalXDR() ([]byte, error) { return nil, nil } @@ -1172,15 +1086,7 @@ func (o EmptyMessage) MustMarshalXDR() []byte { return nil } -func (o EmptyMessage) AppendXDR(bs []byte) ([]byte, error) { - return bs, nil -} - -func (o EmptyMessage) EncodeXDRInto(xw *xdr.Writer) (int, error) { - return xw.Tot(), xw.Error() -} - -func (o *EmptyMessage) DecodeXDR(r io.Reader) error { +func (o EmptyMessage) MarshalXDRInto(m *xdr.Marshaller) error { return nil } @@ -1188,6 +1094,6 @@ func (o *EmptyMessage) UnmarshalXDR(bs []byte) error { return nil } -func (o *EmptyMessage) DecodeXDRFrom(xr *xdr.Reader) error { - return xr.Error() +func (o *EmptyMessage) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + return nil } diff --git a/lib/protocol/vector_xdr.go b/lib/protocol/vector_xdr.go index 17dacbc7..7b9089fb 100644 --- a/lib/protocol/vector_xdr.go +++ b/lib/protocol/vector_xdr.go @@ -7,37 +7,29 @@ import "github.com/calmh/xdr" // This stuff is hacked up manually because genxdr doesn't support 'type // Vector []Counter' declarations and it was tricky when I tried to add it... -type xdrWriter interface { - WriteUint32(uint32) (int, error) - WriteUint64(uint64) (int, error) -} -type xdrReader interface { - ReadUint32() uint32 - ReadUint64() uint64 -} - -// EncodeXDRInto encodes the vector as an XDR object into the given XDR -// encoder. -func (v Vector) EncodeXDRInto(w xdrWriter) (int, error) { - w.WriteUint32(uint32(len(v))) +func (v Vector) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalUint32(uint32(len(v))) for i := range v { - w.WriteUint64(uint64(v[i].ID)) - w.WriteUint64(v[i].Value) + m.MarshalUint64(uint64(v[i].ID)) + m.MarshalUint64(v[i].Value) } - return 4 + 16*len(v), nil + return m.Error } -// DecodeXDRFrom decodes the XDR objects from the given reader into itself. -func (v *Vector) DecodeXDRFrom(r xdrReader) error { - l := int(r.ReadUint32()) +func (v *Vector) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + l := int(u.UnmarshalUint32()) if l > 1e6 { return xdr.ElementSizeExceeded("number of counters", l, 1e6) } n := make(Vector, l) for i := range n { - n[i].ID = ShortID(r.ReadUint64()) - n[i].Value = r.ReadUint64() + n[i].ID = ShortID(u.UnmarshalUint64()) + n[i].Value = u.UnmarshalUint64() } *v = n - return nil + return u.Error +} + +func (v Vector) XDRSize() int { + return 4 + 16*len(v) } diff --git a/lib/relay/protocol/packets_xdr.go b/lib/relay/protocol/packets_xdr.go index 9eef6c0f..dcbe013c 100644 --- a/lib/relay/protocol/packets_xdr.go +++ b/lib/relay/protocol/packets_xdr.go @@ -5,9 +5,6 @@ package protocol import ( - "bytes" - "io" - "github.com/calmh/xdr" ) @@ -34,13 +31,14 @@ struct header { */ -func (o header) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o header) XDRSize() int { + return 4 + 4 + 4 } func (o header) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o header) MustMarshalXDR() []byte { @@ -51,36 +49,22 @@ func (o header) MustMarshalXDR() []byte { return bs } -func (o header) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o header) EncodeXDRInto(xw *xdr.Writer) (int, error) { - xw.WriteUint32(o.magic) - xw.WriteUint32(uint32(o.messageType)) - xw.WriteUint32(uint32(o.messageLength)) - return xw.Tot(), xw.Error() -} - -func (o *header) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) +func (o header) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalUint32(o.magic) + m.MarshalUint32(uint32(o.messageType)) + m.MarshalUint32(uint32(o.messageLength)) + return m.Error } func (o *header) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *header) DecodeXDRFrom(xr *xdr.Reader) error { - o.magic = xr.ReadUint32() - o.messageType = int32(xr.ReadUint32()) - o.messageLength = int32(xr.ReadUint32()) - return xr.Error() +func (o *header) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.magic = u.UnmarshalUint32() + o.messageType = int32(u.UnmarshalUint32()) + o.messageLength = int32(u.UnmarshalUint32()) + return u.Error } /* @@ -94,10 +78,9 @@ struct Ping { */ -func (o Ping) EncodeXDR(w io.Writer) (int, error) { - return 0, nil +func (o Ping) XDRSize() int { + return 0 } - func (o Ping) MarshalXDR() ([]byte, error) { return nil, nil } @@ -106,15 +89,7 @@ func (o Ping) MustMarshalXDR() []byte { return nil } -func (o Ping) AppendXDR(bs []byte) ([]byte, error) { - return bs, nil -} - -func (o Ping) EncodeXDRInto(xw *xdr.Writer) (int, error) { - return xw.Tot(), xw.Error() -} - -func (o *Ping) DecodeXDR(r io.Reader) error { +func (o Ping) MarshalXDRInto(m *xdr.Marshaller) error { return nil } @@ -122,8 +97,8 @@ func (o *Ping) UnmarshalXDR(bs []byte) error { return nil } -func (o *Ping) DecodeXDRFrom(xr *xdr.Reader) error { - return xr.Error() +func (o *Ping) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + return nil } /* @@ -137,10 +112,9 @@ struct Pong { */ -func (o Pong) EncodeXDR(w io.Writer) (int, error) { - return 0, nil +func (o Pong) XDRSize() int { + return 0 } - func (o Pong) MarshalXDR() ([]byte, error) { return nil, nil } @@ -149,15 +123,7 @@ func (o Pong) MustMarshalXDR() []byte { return nil } -func (o Pong) AppendXDR(bs []byte) ([]byte, error) { - return bs, nil -} - -func (o Pong) EncodeXDRInto(xw *xdr.Writer) (int, error) { - return xw.Tot(), xw.Error() -} - -func (o *Pong) DecodeXDR(r io.Reader) error { +func (o Pong) MarshalXDRInto(m *xdr.Marshaller) error { return nil } @@ -165,8 +131,8 @@ func (o *Pong) UnmarshalXDR(bs []byte) error { return nil } -func (o *Pong) DecodeXDRFrom(xr *xdr.Reader) error { - return xr.Error() +func (o *Pong) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + return nil } /* @@ -180,10 +146,9 @@ struct JoinRelayRequest { */ -func (o JoinRelayRequest) EncodeXDR(w io.Writer) (int, error) { - return 0, nil +func (o JoinRelayRequest) XDRSize() int { + return 0 } - func (o JoinRelayRequest) MarshalXDR() ([]byte, error) { return nil, nil } @@ -192,15 +157,7 @@ func (o JoinRelayRequest) MustMarshalXDR() []byte { return nil } -func (o JoinRelayRequest) AppendXDR(bs []byte) ([]byte, error) { - return bs, nil -} - -func (o JoinRelayRequest) EncodeXDRInto(xw *xdr.Writer) (int, error) { - return xw.Tot(), xw.Error() -} - -func (o *JoinRelayRequest) DecodeXDR(r io.Reader) error { +func (o JoinRelayRequest) MarshalXDRInto(m *xdr.Marshaller) error { return nil } @@ -208,8 +165,8 @@ func (o *JoinRelayRequest) UnmarshalXDR(bs []byte) error { return nil } -func (o *JoinRelayRequest) DecodeXDRFrom(xr *xdr.Reader) error { - return xr.Error() +func (o *JoinRelayRequest) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + return nil } /* @@ -223,10 +180,9 @@ struct RelayFull { */ -func (o RelayFull) EncodeXDR(w io.Writer) (int, error) { - return 0, nil +func (o RelayFull) XDRSize() int { + return 0 } - func (o RelayFull) MarshalXDR() ([]byte, error) { return nil, nil } @@ -235,15 +191,7 @@ func (o RelayFull) MustMarshalXDR() []byte { return nil } -func (o RelayFull) AppendXDR(bs []byte) ([]byte, error) { - return bs, nil -} - -func (o RelayFull) EncodeXDRInto(xw *xdr.Writer) (int, error) { - return xw.Tot(), xw.Error() -} - -func (o *RelayFull) DecodeXDR(r io.Reader) error { +func (o RelayFull) MarshalXDRInto(m *xdr.Marshaller) error { return nil } @@ -251,8 +199,8 @@ func (o *RelayFull) UnmarshalXDR(bs []byte) error { return nil } -func (o *RelayFull) DecodeXDRFrom(xr *xdr.Reader) error { - return xr.Error() +func (o *RelayFull) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + return nil } /* @@ -262,10 +210,8 @@ JoinSessionRequest Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Key | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Key (variable length) \ +\ Key (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -276,13 +222,14 @@ struct JoinSessionRequest { */ -func (o JoinSessionRequest) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o JoinSessionRequest) XDRSize() int { + return 4 + len(o.Key) + xdr.Padding(len(o.Key)) } func (o JoinSessionRequest) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o JoinSessionRequest) MustMarshalXDR() []byte { @@ -293,35 +240,21 @@ func (o JoinSessionRequest) MustMarshalXDR() []byte { return bs } -func (o JoinSessionRequest) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o JoinSessionRequest) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o JoinSessionRequest) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.Key); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("Key", l, 32) + return xdr.ElementSizeExceeded("Key", l, 32) } - xw.WriteBytes(o.Key) - return xw.Tot(), xw.Error() -} - -func (o *JoinSessionRequest) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalBytes(o.Key) + return m.Error } func (o *JoinSessionRequest) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *JoinSessionRequest) DecodeXDRFrom(xr *xdr.Reader) error { - o.Key = xr.ReadBytesMax(32) - return xr.Error() +func (o *JoinSessionRequest) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Key = u.UnmarshalBytesMax(32) + return u.Error } /* @@ -333,10 +266,8 @@ Response Structure: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Code | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Message | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Message (variable length) \ +\ Message (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -348,13 +279,15 @@ struct Response { */ -func (o Response) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o Response) XDRSize() int { + return 4 + + 4 + len(o.Message) + xdr.Padding(len(o.Message)) } func (o Response) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o Response) MustMarshalXDR() []byte { @@ -365,34 +298,20 @@ func (o Response) MustMarshalXDR() []byte { return bs } -func (o Response) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o Response) EncodeXDRInto(xw *xdr.Writer) (int, error) { - xw.WriteUint32(uint32(o.Code)) - xw.WriteString(o.Message) - return xw.Tot(), xw.Error() -} - -func (o *Response) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) +func (o Response) MarshalXDRInto(m *xdr.Marshaller) error { + m.MarshalUint32(uint32(o.Code)) + m.MarshalString(o.Message) + return m.Error } func (o *Response) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *Response) DecodeXDRFrom(xr *xdr.Reader) error { - o.Code = int32(xr.ReadUint32()) - o.Message = xr.ReadString() - return xr.Error() +func (o *Response) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.Code = int32(u.UnmarshalUint32()) + o.Message = u.UnmarshalString() + return u.Error } /* @@ -402,10 +321,8 @@ ConnectRequest Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of ID | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ ID (variable length) \ +\ ID (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -416,13 +333,14 @@ struct ConnectRequest { */ -func (o ConnectRequest) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o ConnectRequest) XDRSize() int { + return 4 + len(o.ID) + xdr.Padding(len(o.ID)) } func (o ConnectRequest) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o ConnectRequest) MustMarshalXDR() []byte { @@ -433,35 +351,21 @@ func (o ConnectRequest) MustMarshalXDR() []byte { return bs } -func (o ConnectRequest) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o ConnectRequest) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o ConnectRequest) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.ID); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("ID", l, 32) + return xdr.ElementSizeExceeded("ID", l, 32) } - xw.WriteBytes(o.ID) - return xw.Tot(), xw.Error() -} - -func (o *ConnectRequest) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalBytes(o.ID) + return m.Error } func (o *ConnectRequest) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *ConnectRequest) DecodeXDRFrom(xr *xdr.Reader) error { - o.ID = xr.ReadBytesMax(32) - return xr.Error() +func (o *ConnectRequest) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.ID = u.UnmarshalBytesMax(32) + return u.Error } /* @@ -471,25 +375,19 @@ SessionInvitation Structure: 0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of From | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ From (variable length) \ +\ From (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Key | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Key (variable length) \ +\ Key (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| Length of Address | -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ / / -\ Address (variable length) \ +\ Address (length + padded data) \ / / +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -| 0x0000 | Port | +| 16 zero bits | Port | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Server Socket (V=0 or 1) |V| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -505,13 +403,16 @@ struct SessionInvitation { */ -func (o SessionInvitation) EncodeXDR(w io.Writer) (int, error) { - var xw = xdr.NewWriter(w) - return o.EncodeXDRInto(xw) +func (o SessionInvitation) XDRSize() int { + return 4 + len(o.From) + xdr.Padding(len(o.From)) + + 4 + len(o.Key) + xdr.Padding(len(o.Key)) + + 4 + len(o.Address) + xdr.Padding(len(o.Address)) + 4 + 4 } func (o SessionInvitation) MarshalXDR() ([]byte, error) { - return o.AppendXDR(make([]byte, 0, 128)) + buf := make([]byte, o.XDRSize()) + m := &xdr.Marshaller{Data: buf} + return buf, o.MarshalXDRInto(m) } func (o SessionInvitation) MustMarshalXDR() []byte { @@ -522,47 +423,33 @@ func (o SessionInvitation) MustMarshalXDR() []byte { return bs } -func (o SessionInvitation) AppendXDR(bs []byte) ([]byte, error) { - var aw = xdr.AppendWriter(bs) - var xw = xdr.NewWriter(&aw) - _, err := o.EncodeXDRInto(xw) - return []byte(aw), err -} - -func (o SessionInvitation) EncodeXDRInto(xw *xdr.Writer) (int, error) { +func (o SessionInvitation) MarshalXDRInto(m *xdr.Marshaller) error { if l := len(o.From); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("From", l, 32) + return xdr.ElementSizeExceeded("From", l, 32) } - xw.WriteBytes(o.From) + m.MarshalBytes(o.From) if l := len(o.Key); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("Key", l, 32) + return xdr.ElementSizeExceeded("Key", l, 32) } - xw.WriteBytes(o.Key) + m.MarshalBytes(o.Key) if l := len(o.Address); l > 32 { - return xw.Tot(), xdr.ElementSizeExceeded("Address", l, 32) + return xdr.ElementSizeExceeded("Address", l, 32) } - xw.WriteBytes(o.Address) - xw.WriteUint16(o.Port) - xw.WriteBool(o.ServerSocket) - return xw.Tot(), xw.Error() -} - -func (o *SessionInvitation) DecodeXDR(r io.Reader) error { - xr := xdr.NewReader(r) - return o.DecodeXDRFrom(xr) + m.MarshalBytes(o.Address) + m.MarshalUint16(o.Port) + m.MarshalBool(o.ServerSocket) + return m.Error } func (o *SessionInvitation) UnmarshalXDR(bs []byte) error { - var br = bytes.NewReader(bs) - var xr = xdr.NewReader(br) - return o.DecodeXDRFrom(xr) + u := &xdr.Unmarshaller{Data: bs} + return o.UnmarshalXDRFrom(u) } - -func (o *SessionInvitation) DecodeXDRFrom(xr *xdr.Reader) error { - o.From = xr.ReadBytesMax(32) - o.Key = xr.ReadBytesMax(32) - o.Address = xr.ReadBytesMax(32) - o.Port = xr.ReadUint16() - o.ServerSocket = xr.ReadBool() - return xr.Error() +func (o *SessionInvitation) UnmarshalXDRFrom(u *xdr.Unmarshaller) error { + o.From = u.UnmarshalBytesMax(32) + o.Key = u.UnmarshalBytesMax(32) + o.Address = u.UnmarshalBytesMax(32) + o.Port = u.UnmarshalUint16() + o.ServerSocket = u.UnmarshalBool() + return u.Error }