|
| 1 | +package obi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/binary" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | +) |
| 9 | + |
| 10 | +func decodeImpl(data []byte, v interface{}) ([]byte, error) { |
| 11 | + rv := reflect.ValueOf(v) |
| 12 | + if rv.Kind() != reflect.Ptr || rv.IsNil() { |
| 13 | + return nil, errors.New("obi: decode into non-ptr type") |
| 14 | + } |
| 15 | + ev := rv.Elem() |
| 16 | + switch ev.Kind() { |
| 17 | + case reflect.Uint8: |
| 18 | + val, rem, err := DecodeUnsigned8(data) |
| 19 | + ev.SetUint(uint64(val)) |
| 20 | + return rem, err |
| 21 | + case reflect.Uint16: |
| 22 | + val, rem, err := DecodeUnsigned16(data) |
| 23 | + ev.SetUint(uint64(val)) |
| 24 | + return rem, err |
| 25 | + case reflect.Uint32: |
| 26 | + val, rem, err := DecodeUnsigned32(data) |
| 27 | + ev.SetUint(uint64(val)) |
| 28 | + return rem, err |
| 29 | + case reflect.Uint64: |
| 30 | + val, rem, err := DecodeUnsigned64(data) |
| 31 | + ev.SetUint(uint64(val)) |
| 32 | + return rem, err |
| 33 | + case reflect.Int8: |
| 34 | + val, rem, err := DecodeSigned8(data) |
| 35 | + ev.SetInt(int64(val)) |
| 36 | + return rem, err |
| 37 | + case reflect.Int16: |
| 38 | + val, rem, err := DecodeSigned16(data) |
| 39 | + ev.SetInt(int64(val)) |
| 40 | + return rem, err |
| 41 | + case reflect.Int32: |
| 42 | + val, rem, err := DecodeSigned32(data) |
| 43 | + ev.SetInt(int64(val)) |
| 44 | + return rem, err |
| 45 | + case reflect.Int64: |
| 46 | + val, rem, err := DecodeSigned64(data) |
| 47 | + ev.SetInt(int64(val)) |
| 48 | + return rem, err |
| 49 | + case reflect.String: |
| 50 | + val, rem, err := DecodeString(data) |
| 51 | + ev.SetString(val) |
| 52 | + return rem, err |
| 53 | + case reflect.Slice: |
| 54 | + if ev.Type().Elem().Kind() == reflect.Uint8 { |
| 55 | + val, rem, err := DecodeBytes(data) |
| 56 | + ev.SetBytes(val) |
| 57 | + return rem, err |
| 58 | + } |
| 59 | + length, rem, err := DecodeUnsigned32(data) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + slice := reflect.MakeSlice(ev.Type(), int(length), int(length)) |
| 64 | + for idx := 0; idx < int(length); idx++ { |
| 65 | + var err error |
| 66 | + rem, err = decodeImpl(rem, slice.Index(idx).Addr().Interface()) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + } |
| 71 | + ev.Set(slice) |
| 72 | + return rem, nil |
| 73 | + case reflect.Struct: |
| 74 | + rem := data |
| 75 | + for idx := 0; idx < ev.NumField(); idx++ { |
| 76 | + var err error |
| 77 | + rem, err = decodeImpl(rem, ev.Field(idx).Addr().Interface()) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + } |
| 82 | + return rem, nil |
| 83 | + default: |
| 84 | + return nil, fmt.Errorf("obi: unsupported value type: %s", ev.Kind()) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Decode uses obi encoding scheme to decode the given input(s). |
| 89 | +func Decode(data []byte, v ...interface{}) error { |
| 90 | + var err error |
| 91 | + rem := data |
| 92 | + for _, each := range v { |
| 93 | + rem, err = decodeImpl(rem, each) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + } |
| 98 | + if len(rem) != 0 { |
| 99 | + return errors.New("obi: not all data was consumed while decoding") |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// MustDecode uses obi encoding scheme to decode the given input. Panics on error. |
| 105 | +func MustDecode(data []byte, v ...interface{}) { |
| 106 | + err := Decode(data, v...) |
| 107 | + if err != nil { |
| 108 | + panic(err) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// DecodeUnsigned16 decodes the input bytes into `uint8` and returns the remaining bytes. |
| 113 | +func DecodeUnsigned8(data []byte) (uint8, []byte, error) { |
| 114 | + if len(data) < 1 { |
| 115 | + return 0, nil, errors.New("obi: out of range") |
| 116 | + } |
| 117 | + return data[0], data[1:], nil |
| 118 | +} |
| 119 | + |
| 120 | +// DecodeUnsigned16 decodes the input bytes into `uint16` and returns the remaining bytes. |
| 121 | +func DecodeUnsigned16(data []byte) (uint16, []byte, error) { |
| 122 | + if len(data) < 2 { |
| 123 | + return 0, nil, errors.New("obi: out of range") |
| 124 | + } |
| 125 | + return binary.BigEndian.Uint16(data[:2]), data[2:], nil |
| 126 | +} |
| 127 | + |
| 128 | +// DecodeUnsigned32 decodes the input bytes into `uint32` and returns the remaining bytes. |
| 129 | +func DecodeUnsigned32(data []byte) (uint32, []byte, error) { |
| 130 | + if len(data) < 4 { |
| 131 | + return 0, nil, errors.New("obi: out of range") |
| 132 | + } |
| 133 | + return binary.BigEndian.Uint32(data[:4]), data[4:], nil |
| 134 | +} |
| 135 | + |
| 136 | +// DecodeUnsigned64 decodes the input bytes into `uint64` and returns the remaining bytes. |
| 137 | +func DecodeUnsigned64(data []byte) (uint64, []byte, error) { |
| 138 | + if len(data) < 8 { |
| 139 | + return 0, nil, errors.New("obi: out of range") |
| 140 | + } |
| 141 | + return binary.BigEndian.Uint64(data[:8]), data[8:], nil |
| 142 | +} |
| 143 | + |
| 144 | +// DecodeSigned8 decodes the input bytes into `uint64` and returns the remaining bytes. |
| 145 | +func DecodeSigned8(data []byte) (int8, []byte, error) { |
| 146 | + unsigned, rem, err := DecodeUnsigned8(data) |
| 147 | + return int8(unsigned), rem, err |
| 148 | +} |
| 149 | + |
| 150 | +// DecodeSigned16 decodes the input bytes into `uint64` and returns the remaining bytes. |
| 151 | +func DecodeSigned16(data []byte) (int16, []byte, error) { |
| 152 | + unsigned, rem, err := DecodeUnsigned16(data) |
| 153 | + return int16(unsigned), rem, err |
| 154 | +} |
| 155 | + |
| 156 | +// DecodeSigned32 decodes the input bytes into `uint64` and returns the remaining bytes. |
| 157 | +func DecodeSigned32(data []byte) (int32, []byte, error) { |
| 158 | + unsigned, rem, err := DecodeUnsigned32(data) |
| 159 | + return int32(unsigned), rem, err |
| 160 | +} |
| 161 | + |
| 162 | +// DecodeSigned64 decodes the input bytes into `uint64` and returns the remaining bytes. |
| 163 | +func DecodeSigned64(data []byte) (int64, []byte, error) { |
| 164 | + unsigned, rem, err := DecodeUnsigned64(data) |
| 165 | + return int64(unsigned), rem, err |
| 166 | +} |
| 167 | + |
| 168 | +// DecodeBytes decodes the input bytes and returns bytes result and the remaining bytes. |
| 169 | +func DecodeBytes(data []byte) ([]byte, []byte, error) { |
| 170 | + length, rem, err := DecodeUnsigned32(data) |
| 171 | + if err != nil { |
| 172 | + return nil, nil, err |
| 173 | + } |
| 174 | + if uint32(len(rem)) < length { |
| 175 | + return nil, nil, errors.New("obi: out of range") |
| 176 | + } |
| 177 | + return rem[:length], rem[length:], nil |
| 178 | +} |
| 179 | + |
| 180 | +// DecodeString decodes the input bytes and returns string result and the remaining bytes. |
| 181 | +func DecodeString(data []byte) (string, []byte, error) { |
| 182 | + length, rem, err := DecodeUnsigned32(data) |
| 183 | + if err != nil { |
| 184 | + return "", nil, err |
| 185 | + } |
| 186 | + if uint32(len(rem)) < length { |
| 187 | + return "", nil, errors.New("obi: out of range") |
| 188 | + } |
| 189 | + return string(rem[:length]), rem[length:], nil |
| 190 | +} |
0 commit comments