Commit cdc5f8f4 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent f076b1b8
......@@ -1456,7 +1456,7 @@ overflow:
// 43. StoreObject
func (p *StoreObject) NEOEncodedLen() int {
return 58 + len(p.Data)*1
return 58 + len(p.Data)
}
func (p *StoreObject) NEOEncode(data []byte) {
......@@ -1487,11 +1487,8 @@ func (p *StoreObject) NEOEncode(data []byte) {
l := uint32(len(p.Data))
binary.BigEndian.PutUint32(data[37:], l)
data = data[41:]
for i := 0; uint32(i) < l; i++ {
a := &p.Data[i]
(data[0:])[0] = (*a)
data = data[1:]
}
copy(data, p.Data)
data = data[l:]
}
binary.BigEndian.PutUint64(data[0:], uint64(p.DataSerial))
binary.BigEndian.PutUint64(data[8:], uint64(p.Tid))
......@@ -1529,17 +1526,13 @@ func (p *StoreObject) NEODecode(data []byte) (int, error) {
{
l := binary.BigEndian.Uint32(data[37:])
data = data[41:]
nread += 41
if uint32(len(data)) < 17+l*1 {
nread += 41 + l
if uint32(len(data)) < 17+l {
goto overflow
}
nread += l * 1
p.Data = make([]byte, l)
for i := 0; uint32(i) < l; i++ {
a := &p.Data[i]
(*a) = (data[0:])[0]
data = data[1:]
}
copy(p.Data, data[:l])
data = data[l:]
}
p.DataSerial = Tid(binary.BigEndian.Uint64(data[0:]))
p.Tid = Tid(binary.BigEndian.Uint64(data[8:]))
......@@ -1743,7 +1736,7 @@ overflow:
// 49. AnswerGetObject
func (p *AnswerGetObject) NEOEncodedLen() int {
return 57 + len(p.Data)*1
return 57 + len(p.Data)
}
func (p *AnswerGetObject) NEOEncode(data []byte) {
......@@ -1775,11 +1768,8 @@ func (p *AnswerGetObject) NEOEncode(data []byte) {
l := uint32(len(p.Data))
binary.BigEndian.PutUint32(data[45:], l)
data = data[49:]
for i := 0; uint32(i) < l; i++ {
a := &p.Data[i]
(data[0:])[0] = (*a)
data = data[1:]
}
copy(data, p.Data)
data = data[l:]
}
binary.BigEndian.PutUint64(data[0:], uint64(p.DataSerial))
}
......@@ -1816,17 +1806,13 @@ func (p *AnswerGetObject) NEODecode(data []byte) (int, error) {
{
l := binary.BigEndian.Uint32(data[45:])
data = data[49:]
nread += 49
if uint32(len(data)) < 8+l*1 {
nread += 49 + l
if uint32(len(data)) < 8+l {
goto overflow
}
nread += l * 1
p.Data = make([]byte, l)
for i := 0; uint32(i) < l; i++ {
a := &p.Data[i]
(*a) = (data[0:])[0]
data = data[1:]
}
copy(p.Data, data[:l])
data = data[l:]
}
p.DataSerial = Tid(binary.BigEndian.Uint64(data[0:]))
return 8 + int(nread), nil
......
......@@ -222,8 +222,8 @@ type CodecCodeGen interface {
genSlice(path string, typ *types.Slice, obj types.Object)
genMap(path string, typ *types.Map, obj types.Object)
// XXX particular case of slice
genStrBytes(path string)
// particular case of slice or array with 1-byte elem
genSlice1(path string, typ types.Type)
// get generated code.
// for top-level functions this is whole function including return and closing }
......@@ -514,12 +514,12 @@ func (d *decoder) genBasic(assignto string, typ *types.Basic, userType types.Typ
// len u32
// [len]byte
// TODO []byte support
func (s *sizer) genStrBytes(path string) {
func (s *sizer) genSlice1(path string, typ types.Type) {
s.size.Add(4)
s.size.AddExpr("len(%s)", path)
}
func (e *encoder) genStrBytes(path string) {
func (e *encoder) genSlice1(path string, typ types.Type) {
e.emit("{")
e.emit("l := uint32(len(%s))", path)
e.genBasic("l", types.Typ[types.Uint32], nil)
......@@ -530,7 +530,7 @@ func (e *encoder) genStrBytes(path string) {
e.n = 0
}
func (d *decoder) genStrBytes(assignto string) {
func (d *decoder) genSlice1(assignto string, typ types.Type) {
d.emit("{")
d.genBasic("l:", types.Typ[types.Uint32], nil)
......@@ -541,7 +541,22 @@ func (d *decoder) genStrBytes(assignto string) {
d.overflowCheckpoint()
d.overflowCheckSize.AddExpr("l")
switch t := typ.(type) {
case *types.Basic:
if t.Kind() != types.String {
log.Panicf("bad basic type in slice1: %v", t)
}
d.emit("%v= string(data[:l])", assignto)
case *types.Slice:
// TODO not copy, but reference data from original
d.emit("%v= make(%v, l)", assignto, typeName(typ))
d.emit("copy(%v, data[:l])", assignto)
default:
log.Panicf("bad type in slice1: %v", typ)
}
d.emit("data = data[l:]")
d.emit("}")
}
......@@ -776,7 +791,7 @@ func codegenType(path string, typ types.Type, obj types.Object, codegen CodecCod
switch u := typ.Underlying().(type) {
case *types.Basic:
if u.Kind() == types.String {
codegen.genStrBytes(path)
codegen.genSlice1(path, u)
break
}
......@@ -793,14 +808,24 @@ func codegenType(path string, typ types.Type, obj types.Object, codegen CodecCod
}
case *types.Array:
// TODO optimize for [...]byte
elemSize, _ := typeSizeFixed(u.Elem())
// [...]byte or [...]uint8 - just straight copy
if false && elemSize == 1 {
//codegen.genStrBytes(path+"[:]") // FIXME
codegen.genSlice1(path, u) // FIXME
} else {
var i int64 // XXX because `u.Len() int64`
for i = 0; i < u.Len(); i++ {
codegenType(fmt.Sprintf("%v[%v]", path, i), u.Elem(), obj, codegen)
}
}
case *types.Slice:
if elemSize, _ := typeSizeFixed(u.Elem()); elemSize == 1 {
codegen.genSlice1(path, u)
} else {
codegen.genSlice(path, u, obj)
}
case *types.Map:
codegen.genMap(path, u, obj)
......
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