Commit 2ed6cfd8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 171422b2
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
// //
// See COPYING file for full licensing terms. // See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options. // See https://www.nexedi.com/licensing for rationale and options.
//go:generate stringer -output zproto-str.go -type ErrorCode,ClusterState,NodeType,NodeState proto.go
//go:generate stringer -output zproto-str.go -type ErrorCode,ClusterState,NodeType,NodeState proto.go packed.go
package neo package neo
// supporting code for types defined in proto.go // supporting code for types defined in proto.go
......
...@@ -243,7 +243,7 @@ func (a *Address) neoEncode(b []byte) int { ...@@ -243,7 +243,7 @@ func (a *Address) neoEncode(b []byte) int {
return n return n
} }
func (a *Address) neoDecode(b []byte) (int, bool) { func (a *Address) neoDecode(b []byte) (uint32, bool) {
n, ok := string_neoDecode(&a.Host, b) n, ok := string_neoDecode(&a.Host, b)
if !ok { if !ok {
return 0, false return 0, false
...@@ -1025,7 +1025,7 @@ type Truncate struct { ...@@ -1025,7 +1025,7 @@ type Truncate struct {
type customCodec interface { type customCodec interface {
neoEncodedLen() int neoEncodedLen() int
neoEncode(buf []byte) (nwrote int) neoEncode(buf []byte) (nwrote int)
neoDecode(data []byte) (nread int, ok bool) neoDecode(data []byte) (nread uint32, ok bool) // XXX uint32 or int here?
} }
func byte2bool(b byte) bool { func byte2bool(b byte) bool {
...@@ -1074,7 +1074,7 @@ func string_neoEncode(s string, data []byte) int { ...@@ -1074,7 +1074,7 @@ func string_neoEncode(s string, data []byte) int {
return 4 + l return 4 + l
} }
func string_neoDecode(sp *string, data []byte) (nread int, ok bool) { func string_neoDecode(sp *string, data []byte) (nread uint32, ok bool) {
if len(data) < 4 { if len(data) < 4 {
return 0, false return 0, false
} }
...@@ -1085,5 +1085,5 @@ func string_neoDecode(sp *string, data []byte) (nread int, ok bool) { ...@@ -1085,5 +1085,5 @@ func string_neoDecode(sp *string, data []byte) (nread int, ok bool) {
} }
*sp = string(data[:l]) *sp = string(data[:l])
return 4 + int(l), true return 4 + l, true
} }
...@@ -1091,21 +1091,23 @@ func (s *sizer) genCustom(path string) { ...@@ -1091,21 +1091,23 @@ func (s *sizer) genCustom(path string) {
func (e *encoder) genCustom(path string) { func (e *encoder) genCustom(path string) {
e.emit("{") e.emit("{")
e.emit("n := %s.neoEncode(data[%v:])", e.n) e.emit("n := %s.neoEncode(data[%v:])", path, e.n)
e.emit("data = data[%v + n:]", e.n) e.emit("data = data[%v + n:]", e.n)
e.emit("}") e.emit("}")
e.n = 0 e.n = 0
} }
func (d *decoder) genCustom(path string) { func (d *decoder) genCustom(path string) {
d.resetPos()
// make sure we check for overflow previous-code before proceeding to custom decoder. // make sure we check for overflow previous-code before proceeding to custom decoder.
d.overflowCheck() d.overflowCheck()
d.resetPos()
d.emit("{") d.emit("{")
d.emit("n, ok := %s.neoDecode(data)") d.emit("n, ok := %s.neoDecode(data)", path)
d.emit("if !ok { goto overflow }") d.emit("if !ok { goto overflow }")
d.emit("data = data[n:]") d.emit("data = data[n:]")
d.emit("%v += n", d.var_("nread"))
d.emit("}") d.emit("}")
// insert overflow checkpoint after custom decoder so that overflow // insert overflow checkpoint after custom decoder so that overflow
...@@ -1118,7 +1120,8 @@ func (d *decoder) genCustom(path string) { ...@@ -1118,7 +1120,8 @@ func (d *decoder) genCustom(path string) {
// obj is object that uses this type in source program (so in case of an error // obj is object that uses this type in source program (so in case of an error
// we can point to source location for where it happened) // we can point to source location for where it happened)
func codegenType(path string, typ types.Type, obj types.Object, codegen CodeGenerator) { func codegenType(path string, typ types.Type, obj types.Object, codegen CodeGenerator) {
if types.Implements(typ, neo_customCodec) { if types.Implements(typ, neo_customCodec) ||
types.Implements(types.NewPointer(typ), neo_customCodec) {
codegen.genCustom(path) codegen.genCustom(path)
return return
} }
......
...@@ -64,24 +64,20 @@ func (*RequestIdentification) neoMsgCode() uint16 { ...@@ -64,24 +64,20 @@ func (*RequestIdentification) neoMsgCode() uint16 {
} }
func (p *RequestIdentification) neoMsgEncodedLen() int { func (p *RequestIdentification) neoMsgEncodedLen() int {
return 26 + len(p.Address.Host) + len(p.ClusterName) return 20 + p.Address.neoEncodedLen() + len(p.ClusterName)
} }
func (p *RequestIdentification) neoMsgEncode(data []byte) { func (p *RequestIdentification) neoMsgEncode(data []byte) {
binary.BigEndian.PutUint32(data[0:], uint32(int32(p.NodeType))) binary.BigEndian.PutUint32(data[0:], uint32(int32(p.NodeType)))
binary.BigEndian.PutUint32(data[4:], uint32(int32(p.UUID))) binary.BigEndian.PutUint32(data[4:], uint32(int32(p.UUID)))
{ {
l := uint32(len(p.Address.Host)) n := p.Address.neoEncode(data[8:])
binary.BigEndian.PutUint32(data[8:], l) data = data[8+n:]
data = data[12:]
copy(data, p.Address.Host)
data = data[l:]
} }
binary.BigEndian.PutUint16(data[0:], p.Address.Port)
{ {
l := uint32(len(p.ClusterName)) l := uint32(len(p.ClusterName))
binary.BigEndian.PutUint32(data[2:], l) binary.BigEndian.PutUint32(data[0:], l)
data = data[6:] data = data[4:]
copy(data, p.ClusterName) copy(data, p.ClusterName)
data = data[l:] data = data[l:]
} }
...@@ -90,25 +86,26 @@ func (p *RequestIdentification) neoMsgEncode(data []byte) { ...@@ -90,25 +86,26 @@ func (p *RequestIdentification) neoMsgEncode(data []byte) {
func (p *RequestIdentification) neoMsgDecode(data []byte) (int, error) { func (p *RequestIdentification) neoMsgDecode(data []byte) (int, error) {
var nread uint32 var nread uint32
if uint32(len(data)) < 12 { if uint32(len(data)) < 8 {
goto overflow goto overflow
} }
p.NodeType = NodeType(int32(binary.BigEndian.Uint32(data[0:]))) p.NodeType = NodeType(int32(binary.BigEndian.Uint32(data[0:])))
p.UUID = NodeUUID(int32(binary.BigEndian.Uint32(data[4:]))) p.UUID = NodeUUID(int32(binary.BigEndian.Uint32(data[4:])))
data = data[8:]
{ {
l := binary.BigEndian.Uint32(data[8:]) n, ok := p.Address.neoDecode(data)
data = data[12:] if !ok {
if uint32(len(data)) < 6+l {
goto overflow goto overflow
} }
nread += 6 + l data = data[n:]
p.Address.Host = string(data[:l]) nread += n
data = data[l:] }
if uint32(len(data)) < 4 {
goto overflow
} }
p.Address.Port = binary.BigEndian.Uint16(data[0:])
{ {
l := binary.BigEndian.Uint32(data[2:]) l := binary.BigEndian.Uint32(data[0:])
data = data[6:] data = data[4:]
if uint32(len(data)) < 8+l { if uint32(len(data)) < 8+l {
goto overflow goto overflow
} }
...@@ -259,9 +256,9 @@ func (p *NotPrimaryMaster) neoMsgEncodedLen() int { ...@@ -259,9 +256,9 @@ func (p *NotPrimaryMaster) neoMsgEncodedLen() int {
var size int var size int
for i := 0; i < len(p.KnownMasterList); i++ { for i := 0; i < len(p.KnownMasterList); i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
size += len((*a).Address.Host) size += (*a).neoEncodedLen()
} }
return 8 + len(p.KnownMasterList)*6 + size return 8 + size
} }
func (p *NotPrimaryMaster) neoMsgEncode(data []byte) { func (p *NotPrimaryMaster) neoMsgEncode(data []byte) {
...@@ -273,14 +270,10 @@ func (p *NotPrimaryMaster) neoMsgEncode(data []byte) { ...@@ -273,14 +270,10 @@ func (p *NotPrimaryMaster) neoMsgEncode(data []byte) {
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
{ {
l := uint32(len((*a).Address.Host)) n := (*a).neoEncode(data[0:])
binary.BigEndian.PutUint32(data[0:], l) data = data[0+n:]
data = data[4:]
copy(data, (*a).Address.Host)
data = data[l:]
} }
binary.BigEndian.PutUint16(data[0:], (*a).Address.Port) data = data[0:]
data = data[2:]
} }
} }
} }
...@@ -297,23 +290,15 @@ func (p *NotPrimaryMaster) neoMsgDecode(data []byte) (int, error) { ...@@ -297,23 +290,15 @@ func (p *NotPrimaryMaster) neoMsgDecode(data []byte) (int, error) {
p.KnownMasterList = make([]struct{ Address }, l) p.KnownMasterList = make([]struct{ Address }, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.KnownMasterList[i] a := &p.KnownMasterList[i]
if uint32(len(data)) < 4 {
goto overflow
}
{ {
l := binary.BigEndian.Uint32(data[0:]) n, ok := (*a).neoDecode(data)
data = data[4:] if !ok {
if uint32(len(data)) < 2+l {
goto overflow goto overflow
} }
nread += 2 + l data = data[n:]
(*a).Address.Host = string(data[:l]) nread += n
data = data[l:]
} }
(*a).Address.Port = binary.BigEndian.Uint16(data[0:])
data = data[2:]
} }
nread += l * 4
} }
return 8 + int(nread), nil return 8 + int(nread), nil
...@@ -331,9 +316,9 @@ func (p *NotifyNodeInformation) neoMsgEncodedLen() int { ...@@ -331,9 +316,9 @@ func (p *NotifyNodeInformation) neoMsgEncodedLen() int {
var size int var size int
for i := 0; i < len(p.NodeList); i++ { for i := 0; i < len(p.NodeList); i++ {
a := &p.NodeList[i] a := &p.NodeList[i]
size += len((*a).Addr.Host) size += (*a).Addr.neoEncodedLen()
} }
return 12 + len(p.NodeList)*26 + size return 12 + len(p.NodeList)*20 + size
} }
func (p *NotifyNodeInformation) neoMsgEncode(data []byte) { func (p *NotifyNodeInformation) neoMsgEncode(data []byte) {
...@@ -346,17 +331,13 @@ func (p *NotifyNodeInformation) neoMsgEncode(data []byte) { ...@@ -346,17 +331,13 @@ func (p *NotifyNodeInformation) neoMsgEncode(data []byte) {
a := &p.NodeList[i] a := &p.NodeList[i]
binary.BigEndian.PutUint32(data[0:], uint32(int32((*a).Type))) binary.BigEndian.PutUint32(data[0:], uint32(int32((*a).Type)))
{ {
l := uint32(len((*a).Addr.Host)) n := (*a).Addr.neoEncode(data[4:])
binary.BigEndian.PutUint32(data[4:], l) data = data[4+n:]
data = data[8:]
copy(data, (*a).Addr.Host)
data = data[l:]
} }
binary.BigEndian.PutUint16(data[0:], (*a).Addr.Port) binary.BigEndian.PutUint32(data[0:], uint32(int32((*a).UUID)))
binary.BigEndian.PutUint32(data[2:], uint32(int32((*a).UUID))) binary.BigEndian.PutUint32(data[4:], uint32(int32((*a).State)))
binary.BigEndian.PutUint32(data[6:], uint32(int32((*a).State))) float64_NEOEncode(data[8:], (*a).IdTimestamp)
float64_NEOEncode(data[10:], (*a).IdTimestamp) data = data[16:]
data = data[18:]
} }
} }
} }
...@@ -373,27 +354,28 @@ func (p *NotifyNodeInformation) neoMsgDecode(data []byte) (int, error) { ...@@ -373,27 +354,28 @@ func (p *NotifyNodeInformation) neoMsgDecode(data []byte) (int, error) {
p.NodeList = make([]NodeInfo, l) p.NodeList = make([]NodeInfo, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.NodeList[i] a := &p.NodeList[i]
if uint32(len(data)) < 8 { if uint32(len(data)) < 4 {
goto overflow goto overflow
} }
(*a).Type = NodeType(int32(binary.BigEndian.Uint32(data[0:]))) (*a).Type = NodeType(int32(binary.BigEndian.Uint32(data[0:])))
data = data[4:]
{ {
l := binary.BigEndian.Uint32(data[4:]) n, ok := (*a).Addr.neoDecode(data)
data = data[8:] if !ok {
if uint32(len(data)) < 18+l {
goto overflow goto overflow
} }
nread += 18 + l data = data[n:]
(*a).Addr.Host = string(data[:l]) nread += n
data = data[l:]
} }
(*a).Addr.Port = binary.BigEndian.Uint16(data[0:]) if uint32(len(data)) < 16 {
(*a).UUID = NodeUUID(int32(binary.BigEndian.Uint32(data[2:]))) goto overflow
(*a).State = NodeState(int32(binary.BigEndian.Uint32(data[6:])))
(*a).IdTimestamp = float64_NEODecode(data[10:])
data = data[18:]
} }
nread += l * 8 (*a).UUID = NodeUUID(int32(binary.BigEndian.Uint32(data[0:])))
(*a).State = NodeState(int32(binary.BigEndian.Uint32(data[4:])))
(*a).IdTimestamp = float64_NEODecode(data[8:])
data = data[16:]
}
nread += l * 20
} }
return 12 + int(nread), nil return 12 + int(nread), nil
...@@ -2398,9 +2380,9 @@ func (p *AnswerNodeList) neoMsgEncodedLen() int { ...@@ -2398,9 +2380,9 @@ func (p *AnswerNodeList) neoMsgEncodedLen() int {
var size int var size int
for i := 0; i < len(p.NodeList); i++ { for i := 0; i < len(p.NodeList); i++ {
a := &p.NodeList[i] a := &p.NodeList[i]
size += len((*a).Addr.Host) size += (*a).Addr.neoEncodedLen()
} }
return 4 + len(p.NodeList)*26 + size return 4 + len(p.NodeList)*20 + size
} }
func (p *AnswerNodeList) neoMsgEncode(data []byte) { func (p *AnswerNodeList) neoMsgEncode(data []byte) {
...@@ -2412,17 +2394,13 @@ func (p *AnswerNodeList) neoMsgEncode(data []byte) { ...@@ -2412,17 +2394,13 @@ func (p *AnswerNodeList) neoMsgEncode(data []byte) {
a := &p.NodeList[i] a := &p.NodeList[i]
binary.BigEndian.PutUint32(data[0:], uint32(int32((*a).Type))) binary.BigEndian.PutUint32(data[0:], uint32(int32((*a).Type)))
{ {
l := uint32(len((*a).Addr.Host)) n := (*a).Addr.neoEncode(data[4:])
binary.BigEndian.PutUint32(data[4:], l) data = data[4+n:]
data = data[8:]
copy(data, (*a).Addr.Host)
data = data[l:]
} }
binary.BigEndian.PutUint16(data[0:], (*a).Addr.Port) binary.BigEndian.PutUint32(data[0:], uint32(int32((*a).UUID)))
binary.BigEndian.PutUint32(data[2:], uint32(int32((*a).UUID))) binary.BigEndian.PutUint32(data[4:], uint32(int32((*a).State)))
binary.BigEndian.PutUint32(data[6:], uint32(int32((*a).State))) float64_NEOEncode(data[8:], (*a).IdTimestamp)
float64_NEOEncode(data[10:], (*a).IdTimestamp) data = data[16:]
data = data[18:]
} }
} }
} }
...@@ -2438,27 +2416,28 @@ func (p *AnswerNodeList) neoMsgDecode(data []byte) (int, error) { ...@@ -2438,27 +2416,28 @@ func (p *AnswerNodeList) neoMsgDecode(data []byte) (int, error) {
p.NodeList = make([]NodeInfo, l) p.NodeList = make([]NodeInfo, l)
for i := 0; uint32(i) < l; i++ { for i := 0; uint32(i) < l; i++ {
a := &p.NodeList[i] a := &p.NodeList[i]
if uint32(len(data)) < 8 { if uint32(len(data)) < 4 {
goto overflow goto overflow
} }
(*a).Type = NodeType(int32(binary.BigEndian.Uint32(data[0:]))) (*a).Type = NodeType(int32(binary.BigEndian.Uint32(data[0:])))
data = data[4:]
{ {
l := binary.BigEndian.Uint32(data[4:]) n, ok := (*a).Addr.neoDecode(data)
data = data[8:] if !ok {
if uint32(len(data)) < 18+l {
goto overflow goto overflow
} }
nread += 18 + l data = data[n:]
(*a).Addr.Host = string(data[:l]) nread += n
data = data[l:]
} }
(*a).Addr.Port = binary.BigEndian.Uint16(data[0:]) if uint32(len(data)) < 16 {
(*a).UUID = NodeUUID(int32(binary.BigEndian.Uint32(data[2:]))) goto overflow
(*a).State = NodeState(int32(binary.BigEndian.Uint32(data[6:])))
(*a).IdTimestamp = float64_NEODecode(data[10:])
data = data[18:]
} }
nread += l * 8 (*a).UUID = NodeUUID(int32(binary.BigEndian.Uint32(data[0:])))
(*a).State = NodeState(int32(binary.BigEndian.Uint32(data[4:])))
(*a).IdTimestamp = float64_NEODecode(data[8:])
data = data[16:]
}
nread += l * 20
} }
return 4 + int(nread), nil return 4 + int(nread), nil
...@@ -3072,7 +3051,7 @@ func (*CheckPartition) neoMsgCode() uint16 { ...@@ -3072,7 +3051,7 @@ func (*CheckPartition) neoMsgCode() uint16 {
} }
func (p *CheckPartition) neoMsgEncodedLen() int { func (p *CheckPartition) neoMsgEncodedLen() int {
return 30 + len(p.Source.UpstreamName) + len(p.Source.Address.Host) return 24 + len(p.Source.UpstreamName) + p.Source.Address.neoEncodedLen()
} }
func (p *CheckPartition) neoMsgEncode(data []byte) { func (p *CheckPartition) neoMsgEncode(data []byte) {
...@@ -3085,15 +3064,11 @@ func (p *CheckPartition) neoMsgEncode(data []byte) { ...@@ -3085,15 +3064,11 @@ func (p *CheckPartition) neoMsgEncode(data []byte) {
data = data[l:] data = data[l:]
} }
{ {
l := uint32(len(p.Source.Address.Host)) n := p.Source.Address.neoEncode(data[0:])
binary.BigEndian.PutUint32(data[0:], l) data = data[0+n:]
data = data[4:]
copy(data, p.Source.Address.Host)
data = data[l:]
} }
binary.BigEndian.PutUint16(data[0:], p.Source.Address.Port) binary.BigEndian.PutUint64(data[0:], uint64(p.MinTID))
binary.BigEndian.PutUint64(data[2:], uint64(p.MinTID)) binary.BigEndian.PutUint64(data[8:], uint64(p.MaxTID))
binary.BigEndian.PutUint64(data[10:], uint64(p.MaxTID))
} }
func (p *CheckPartition) neoMsgDecode(data []byte) (int, error) { func (p *CheckPartition) neoMsgDecode(data []byte) (int, error) {
...@@ -3105,27 +3080,27 @@ func (p *CheckPartition) neoMsgDecode(data []byte) (int, error) { ...@@ -3105,27 +3080,27 @@ func (p *CheckPartition) neoMsgDecode(data []byte) (int, error) {
{ {
l := binary.BigEndian.Uint32(data[4:]) l := binary.BigEndian.Uint32(data[4:])
data = data[8:] data = data[8:]
if uint32(len(data)) < 4+l { if uint32(len(data)) < l {
goto overflow goto overflow
} }
nread += 4 + l nread += l
p.Source.UpstreamName = string(data[:l]) p.Source.UpstreamName = string(data[:l])
data = data[l:] data = data[l:]
} }
{ {
l := binary.BigEndian.Uint32(data[0:]) n, ok := p.Source.Address.neoDecode(data)
data = data[4:] if !ok {
if uint32(len(data)) < 18+l {
goto overflow goto overflow
} }
nread += 18 + l data = data[n:]
p.Source.Address.Host = string(data[:l]) nread += n
data = data[l:]
} }
p.Source.Address.Port = binary.BigEndian.Uint16(data[0:]) if uint32(len(data)) < 16 {
p.MinTID = zodb.Tid(binary.BigEndian.Uint64(data[2:])) goto overflow
p.MaxTID = zodb.Tid(binary.BigEndian.Uint64(data[10:])) }
return 8 + int(nread), nil p.MinTID = zodb.Tid(binary.BigEndian.Uint64(data[0:]))
p.MaxTID = zodb.Tid(binary.BigEndian.Uint64(data[8:]))
return 24 + int(nread), nil
overflow: overflow:
return 0, ErrDecodeOverflow return 0, ErrDecodeOverflow
......
// Code generated by "stringer -output zproto-str.go -type ErrorCode,ClusterState,NodeType,NodeState proto.go"; DO NOT EDIT. // Code generated by "stringer -output zproto-str.go -type ErrorCode,ClusterState,NodeType,NodeState proto.go packed.go"; DO NOT EDIT.
package neo package neo
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment