590 lines
14 KiB
Go
590 lines
14 KiB
Go
package ssob
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"errors"
|
|
"reflect"
|
|
)
|
|
|
|
func UnmarshalString(in []byte) (ret string, n int, err error) {
|
|
if len(in) < 4 {
|
|
return "", 0, errors.New("ssob: Invalid input to decode string")
|
|
}
|
|
l := int32(binary.BigEndian.Uint32(in))
|
|
|
|
if len(in[4:]) < int(l) {
|
|
return "", 0, errors.New("ssob: Invalid length of string")
|
|
}
|
|
return string(in[4 : l+4]), int(l) + 4, nil
|
|
}
|
|
|
|
func UnmarshalBool(in []byte) (ret bool, n int, err error) {
|
|
if len(in) < 1 {
|
|
return false, 0, errors.New("ssob: Invalid input to decode bool")
|
|
}
|
|
|
|
if in[0] == byte(0) {
|
|
return true, 1, nil
|
|
}
|
|
return false, 1, nil
|
|
}
|
|
|
|
func UnmarshalInt8(in []byte) (ret int8, n int, err error) {
|
|
if len(in) < 1 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode int8")
|
|
}
|
|
|
|
return int8(in[0]), 1, nil
|
|
}
|
|
|
|
func UnmarshalUint8(in []byte) (ret uint8, n int, err error) {
|
|
if len(in) < 1 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode uint8")
|
|
}
|
|
|
|
return uint8(in[0]), 1, nil
|
|
}
|
|
|
|
func UnmarshalInt16(in []byte) (ret int16, n int, err error) {
|
|
if len(in) < 2 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode int16")
|
|
}
|
|
|
|
return int16(binary.BigEndian.Uint16(in[0:2])), 2, nil
|
|
}
|
|
|
|
func UnmarshalUint16(in []byte) (ret uint16, n int, err error) {
|
|
if len(in) < 2 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode uint16")
|
|
}
|
|
|
|
return binary.BigEndian.Uint16(in[0:2]), 2, nil
|
|
}
|
|
|
|
func UnmarshalInt32(in []byte) (ret int32, n int, err error) {
|
|
if len(in) < 4 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode int32")
|
|
}
|
|
|
|
return int32(binary.BigEndian.Uint32(in[0:4])), 4, nil
|
|
}
|
|
|
|
func UnmarshalUint32(in []byte) (ret uint32, n int, err error) {
|
|
if len(in) < 4 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode uint32")
|
|
}
|
|
|
|
return binary.BigEndian.Uint32(in[0:4]), 4, nil
|
|
}
|
|
|
|
func UnmarshalFloat32(in []byte) (ret float32, n int, err error) {
|
|
if len(in) < 4 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode float32")
|
|
}
|
|
|
|
return float32(binary.BigEndian.Uint64(in[0:4])), 4, nil
|
|
}
|
|
|
|
func UnmarshalInt64(in []byte) (ret int64, n int, err error) {
|
|
if len(in) < 8 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode int64")
|
|
}
|
|
|
|
return int64(binary.BigEndian.Uint64(in[0:8])), 8, nil
|
|
}
|
|
|
|
func UnmarshalUint64(in []byte) (ret uint64, n int, err error) {
|
|
if len(in) < 8 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode uint64")
|
|
}
|
|
|
|
return binary.BigEndian.Uint64(in[0:8]), 8, nil
|
|
}
|
|
|
|
func UnmarshalFloat64(in []byte) (ret float64, n int, err error) {
|
|
if len(in) < 8 {
|
|
return 0, 0, errors.New("ssob: Invalid input to decode float64")
|
|
}
|
|
|
|
return float64(binary.BigEndian.Uint64(in[0:8])), 8, nil
|
|
}
|
|
|
|
type unmarshalFunc func(e interface{}, in []byte) (n int, err error)
|
|
|
|
var decoderCache map[string]unmarshalFunc
|
|
|
|
func init() {
|
|
decoderCache = make(map[string]unmarshalFunc)
|
|
RegisterDecoder("bool", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*bool); ok {
|
|
*i, n, err = UnmarshalBool(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected bool")
|
|
})
|
|
RegisterDecoder("int8", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*int8); ok {
|
|
*i, n, err = UnmarshalInt8(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected int8")
|
|
})
|
|
RegisterDecoder("uint8", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*uint8); ok {
|
|
*i, n, err = UnmarshalUint8(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected uint8")
|
|
})
|
|
RegisterDecoder("int16", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*int16); ok {
|
|
*i, n, err = UnmarshalInt16(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected int16")
|
|
})
|
|
RegisterDecoder("uint16", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*uint16); ok {
|
|
*i, n, err = UnmarshalUint16(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected uint16")
|
|
})
|
|
RegisterDecoder("int32", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*int32); ok {
|
|
*i, n, err = UnmarshalInt32(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected int32")
|
|
})
|
|
RegisterDecoder("uint32", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*uint32); ok {
|
|
*i, n, err = UnmarshalUint32(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected uint32")
|
|
})
|
|
RegisterDecoder("float32", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*float32); ok {
|
|
*i, n, err = UnmarshalFloat32(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected float32")
|
|
})
|
|
RegisterDecoder("int64", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*int64); ok {
|
|
*i, n, err = UnmarshalInt64(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected int64")
|
|
})
|
|
RegisterDecoder("uint64", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*uint64); ok {
|
|
*i, n, err = UnmarshalUint64(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected uint64")
|
|
})
|
|
RegisterDecoder("float64", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*float64); ok {
|
|
*i, n, err = UnmarshalFloat64(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected float64")
|
|
})
|
|
RegisterDecoder("string", func(e interface{}, in []byte) (n int, err error) {
|
|
if i, ok := e.(*string); ok {
|
|
*i, n, err = UnmarshalString(in)
|
|
return n, err
|
|
}
|
|
return 0, errors.New("ssob: Incompatible type - expected string")
|
|
})
|
|
}
|
|
|
|
func decRegister(e interface{}) (err error) {
|
|
t := reflect.TypeOf(e)
|
|
switch t.Kind() {
|
|
case reflect.Invalid:
|
|
return errors.New("ssob: Invalid type")
|
|
case reflect.Array:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
t := reflect.TypeOf(e)
|
|
v := reflect.ValueOf(e)
|
|
pos := 0
|
|
|
|
if t.Kind() != reflect.Ptr || v.IsNil() {
|
|
return 0, errors.New("ssob: Cannot unmarshal to nil pointer")
|
|
}
|
|
|
|
l, r, err := UnmarshalInt32(in)
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
|
|
ti := v.Elem()
|
|
if int(l) != ti.Len() {
|
|
return pos, errors.New("ssob: Invalid array length. Expected " + string(l) + " got " + string(ti.Len()))
|
|
}
|
|
for i := 0; i < int(l); i++ {
|
|
e := reflect.New(reflect.TypeOf(ti.Interface()).Elem())
|
|
r, err := unmarshal(e.Interface(), in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
ti.Index(i).Set(reflect.Indirect(e))
|
|
}
|
|
return pos, nil
|
|
}
|
|
decoderCache[string(t.Kind())] = f
|
|
case reflect.Slice:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
t := reflect.TypeOf(e)
|
|
v := reflect.ValueOf(e)
|
|
pos := 0
|
|
|
|
if t.Kind() != reflect.Ptr || v.IsNil() {
|
|
return 0, errors.New("ssob: Cannot unmarshal to nil pointer")
|
|
}
|
|
|
|
l, r, err := UnmarshalInt32(in)
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
|
|
ti := v.Elem()
|
|
ti.Set(reflect.MakeSlice(reflect.TypeOf(ti.Interface()), 0, ti.Cap()))
|
|
for i := 0; i < int(l); i++ {
|
|
e := reflect.New(reflect.TypeOf(ti.Interface()).Elem())
|
|
r, err := unmarshal(e.Interface(), in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
ti.Set(reflect.Append(ti, reflect.Indirect(e)))
|
|
}
|
|
return pos, nil
|
|
}
|
|
decoderCache[string(t.Kind())] = f
|
|
case reflect.Struct:
|
|
zt := reflect.New(t)
|
|
n := zt.Elem().NumField()
|
|
mfields := []int{}
|
|
for i := 0; i < n; i++ {
|
|
if zt.Elem().Field(i).CanSet() {
|
|
mfields = append(mfields, i)
|
|
}
|
|
}
|
|
if len(mfields) == 0 {
|
|
return errors.New("ssob: No exported fields for " + string(t.Name()))
|
|
}
|
|
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
t := reflect.TypeOf(e)
|
|
v := reflect.ValueOf(e)
|
|
pos := 0
|
|
|
|
if t.Kind() != reflect.Ptr || v.IsNil() {
|
|
return 0, errors.New("ssob: Cannot unmarshal to nil pointer")
|
|
}
|
|
|
|
vi := reflect.Indirect(v)
|
|
for _, i := range mfields {
|
|
ni, err := unmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
|
pos += ni
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
}
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Map:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
t := reflect.TypeOf(e)
|
|
v := reflect.ValueOf(e)
|
|
pos := 0
|
|
|
|
if t.Kind() != reflect.Ptr || v.IsNil() {
|
|
return 0, errors.New("ssob: Cannot unmarshal to nil pointer")
|
|
}
|
|
|
|
l, r, err := UnmarshalInt32(in)
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
|
|
vi := reflect.Indirect(v)
|
|
vt := reflect.TypeOf(vi.Interface())
|
|
tk := vt.Key()
|
|
tv := vt.Elem()
|
|
for i := int32(0); i < l; i++ {
|
|
ek := reflect.New(tk)
|
|
ev := reflect.New(tv)
|
|
r, err := unmarshal(ek.Interface(), in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
r, err = unmarshal(ev.Interface(), in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
vi.SetMapIndex(reflect.Indirect(ek), reflect.Indirect(ev))
|
|
}
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[string(t.Kind())] = f
|
|
case reflect.Bool:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := bool(false)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetBool(bool(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Uint8:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := uint8(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetUint(uint64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Uint16:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := uint16(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetUint(uint64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Uint32:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := uint32(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetUint(uint64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Uint64:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := uint64(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetUint(uint64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Int8:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := int8(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetInt(int64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Int16:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := int16(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetInt(int64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Int32:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := int32(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetInt(int64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.Int64:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := int64(0)
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetInt(int64(i))
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
case reflect.String:
|
|
f := func(e interface{}, in []byte) (n int, err error) {
|
|
v := reflect.ValueOf(e)
|
|
i := string("")
|
|
pos := 0
|
|
|
|
r, err := unmarshalBaseType(&i, in[pos:])
|
|
pos += r
|
|
if err != nil {
|
|
return pos, err
|
|
}
|
|
v.Elem().SetString(i)
|
|
|
|
return pos, nil
|
|
}
|
|
decoderCache[t.Name()] = f
|
|
default:
|
|
return errors.New("ssob: Unknown type " + string(t.Name()))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func unmarshalBaseType(e interface{}, in []byte) (n int, err error) {
|
|
switch t := e.(type) {
|
|
case *bool:
|
|
return decoderCache["bool"](t, in)
|
|
case *int8:
|
|
return decoderCache["int8"](t, in)
|
|
case *uint8:
|
|
return decoderCache["uint8"](t, in)
|
|
case *int16:
|
|
return decoderCache["int16"](t, in)
|
|
case *uint16:
|
|
return decoderCache["uint16"](t, in)
|
|
case *int32:
|
|
return decoderCache["int32"](t, in)
|
|
case *uint32:
|
|
return decoderCache["uint32"](t, in)
|
|
case *int64:
|
|
return decoderCache["int64"](t, in)
|
|
case *uint64:
|
|
return decoderCache["uint64"](t, in)
|
|
case *float32:
|
|
return decoderCache["float32"](t, in)
|
|
case *float64:
|
|
return decoderCache["float64"](t, in)
|
|
case *string:
|
|
return decoderCache["string"](t, in)
|
|
default:
|
|
return 0, errors.New("ssob: No base type")
|
|
}
|
|
}
|
|
|
|
func unmarshal(e interface{}, in []byte) (n int, err error) {
|
|
n, err = unmarshalBaseType(e, in)
|
|
if err == nil {
|
|
return n, err
|
|
}
|
|
|
|
var key string
|
|
t := reflect.TypeOf(e)
|
|
v := reflect.ValueOf(e)
|
|
if t.Kind() != reflect.Ptr || v.IsNil() {
|
|
return 0, errors.New("ssob: Need a pointer that is fully allocated for unmarshalling")
|
|
}
|
|
|
|
p := reflect.Indirect(v)
|
|
if p.Kind() != reflect.Ptr {
|
|
switch p.Kind() {
|
|
case reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32,
|
|
reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32,
|
|
reflect.Uint64, reflect.String, reflect.Struct:
|
|
key = reflect.TypeOf(p.Interface()).Name()
|
|
default:
|
|
key = string(p.Kind())
|
|
}
|
|
|
|
if f, ok := decoderCache[key]; ok {
|
|
return f(v.Interface(), in)
|
|
} else {
|
|
err = decRegister(p.Interface())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return unmarshal(e, in)
|
|
}
|
|
}
|
|
if !p.Elem().IsValid() {
|
|
err = allocType(v)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
p = reflect.Indirect(v)
|
|
}
|
|
|
|
return unmarshal(p.Interface(), in)
|
|
}
|
|
|
|
func RegisterDecoder(name string, f func(e interface{}, in []byte) (n int, err error)) {
|
|
decoderCache[name] = f
|
|
}
|