github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/accounts/abi/unpack.go (about) 1 package abi 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "math/big" 7 "reflect" 8 9 "github.com/quickchainproject/quickchain/common" 10 ) 11 12 // reads the integer based on its kind 13 func readInteger(kind reflect.Kind, b []byte) interface{} { 14 switch kind { 15 case reflect.Uint8: 16 return b[len(b)-1] 17 case reflect.Uint16: 18 return binary.BigEndian.Uint16(b[len(b)-2:]) 19 case reflect.Uint32: 20 return binary.BigEndian.Uint32(b[len(b)-4:]) 21 case reflect.Uint64: 22 return binary.BigEndian.Uint64(b[len(b)-8:]) 23 case reflect.Int8: 24 return int8(b[len(b)-1]) 25 case reflect.Int16: 26 return int16(binary.BigEndian.Uint16(b[len(b)-2:])) 27 case reflect.Int32: 28 return int32(binary.BigEndian.Uint32(b[len(b)-4:])) 29 case reflect.Int64: 30 return int64(binary.BigEndian.Uint64(b[len(b)-8:])) 31 default: 32 return new(big.Int).SetBytes(b) 33 } 34 } 35 36 // reads a bool 37 func readBool(word []byte) (bool, error) { 38 for _, b := range word[:31] { 39 if b != 0 { 40 return false, errBadBool 41 } 42 } 43 switch word[31] { 44 case 0: 45 return false, nil 46 case 1: 47 return true, nil 48 default: 49 return false, errBadBool 50 } 51 } 52 53 // A function type is simply the address with the function selection signature at the end. 54 // This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes) 55 func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) { 56 if t.T != FunctionTy { 57 return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array") 58 } 59 if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 { 60 err = fmt.Errorf("abi: got improperly encoded function type, got %v", word) 61 } else { 62 copy(funcTy[:], word[0:24]) 63 } 64 return 65 } 66 67 // through reflection, creates a fixed array to be read from 68 func readFixedBytes(t Type, word []byte) (interface{}, error) { 69 if t.T != FixedBytesTy { 70 return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array") 71 } 72 // convert 73 array := reflect.New(t.Type).Elem() 74 75 reflect.Copy(array, reflect.ValueOf(word[0:t.Size])) 76 return array.Interface(), nil 77 78 } 79 80 func getFullElemSize(elem *Type) int { 81 //all other should be counted as 32 (slices have pointers to respective elements) 82 size := 32 83 //arrays wrap it, each element being the same size 84 for elem.T == ArrayTy { 85 size *= elem.Size 86 elem = elem.Elem 87 } 88 return size 89 } 90 91 // iteratively unpack elements 92 func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) { 93 if size < 0 { 94 return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size) 95 } 96 if start+32*size > len(output) { 97 return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size) 98 } 99 100 // this value will become our slice or our array, depending on the type 101 var refSlice reflect.Value 102 103 if t.T == SliceTy { 104 // declare our slice 105 refSlice = reflect.MakeSlice(t.Type, size, size) 106 } else if t.T == ArrayTy { 107 // declare our array 108 refSlice = reflect.New(t.Type).Elem() 109 } else { 110 return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage") 111 } 112 113 // Arrays have packed elements, resulting in longer unpack steps. 114 // Slices have just 32 bytes per element (pointing to the contents). 115 elemSize := 32 116 if t.T == ArrayTy { 117 elemSize = getFullElemSize(t.Elem) 118 } 119 120 for i, j := start, 0; j < size; i, j = i+elemSize, j+1 { 121 122 inter, err := toGoType(i, *t.Elem, output) 123 if err != nil { 124 return nil, err 125 } 126 127 // append the item to our reflect slice 128 refSlice.Index(j).Set(reflect.ValueOf(inter)) 129 } 130 131 // return the interface 132 return refSlice.Interface(), nil 133 } 134 135 // toGoType parses the output bytes and recursively assigns the value of these bytes 136 // into a go type with accordance with the ABI spec. 137 func toGoType(index int, t Type, output []byte) (interface{}, error) { 138 if index+32 > len(output) { 139 return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32) 140 } 141 142 var ( 143 returnOutput []byte 144 begin, end int 145 err error 146 ) 147 148 // if we require a length prefix, find the beginning word and size returned. 149 if t.requiresLengthPrefix() { 150 begin, end, err = lengthPrefixPointsTo(index, output) 151 if err != nil { 152 return nil, err 153 } 154 } else { 155 returnOutput = output[index : index+32] 156 } 157 158 switch t.T { 159 case SliceTy: 160 return forEachUnpack(t, output, begin, end) 161 case ArrayTy: 162 return forEachUnpack(t, output, index, t.Size) 163 case StringTy: // variable arrays are written at the end of the return bytes 164 return string(output[begin : begin+end]), nil 165 case IntTy, UintTy: 166 return readInteger(t.Kind, returnOutput), nil 167 case BoolTy: 168 return readBool(returnOutput) 169 case AddressTy: 170 return common.BytesToAddress(returnOutput), nil 171 case HashTy: 172 return common.BytesToHash(returnOutput), nil 173 case BytesTy: 174 return output[begin : begin+end], nil 175 case FixedBytesTy: 176 return readFixedBytes(t, returnOutput) 177 case FunctionTy: 178 return readFunctionType(t, returnOutput) 179 default: 180 return nil, fmt.Errorf("abi: unknown type %v", t.T) 181 } 182 } 183 184 // interprets a 32 byte slice as an offset and then determines which indice to look to decode the type. 185 func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) { 186 bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32]) 187 bigOffsetEnd.Add(bigOffsetEnd, common.Big32) 188 outputLength := big.NewInt(int64(len(output))) 189 190 if bigOffsetEnd.Cmp(outputLength) > 0 { 191 return 0, 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", bigOffsetEnd, outputLength) 192 } 193 194 if bigOffsetEnd.BitLen() > 63 { 195 return 0, 0, fmt.Errorf("abi offset larger than int64: %v", bigOffsetEnd) 196 } 197 198 offsetEnd := int(bigOffsetEnd.Uint64()) 199 lengthBig := big.NewInt(0).SetBytes(output[offsetEnd-32 : offsetEnd]) 200 201 totalSize := big.NewInt(0) 202 totalSize.Add(totalSize, bigOffsetEnd) 203 totalSize.Add(totalSize, lengthBig) 204 if totalSize.BitLen() > 63 { 205 return 0, 0, fmt.Errorf("abi length larger than int64: %v", totalSize) 206 } 207 208 if totalSize.Cmp(outputLength) > 0 { 209 return 0, 0, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %v require %v", outputLength, totalSize) 210 } 211 start = int(bigOffsetEnd.Uint64()) 212 length = int(lengthBig.Uint64()) 213 return 214 }