github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/accounts/abi/numbers.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package abi 18 19 import ( 20 "math/big" 21 "reflect" 22 23 "github.com/ethereum/go-ethereum/common" 24 ) 25 26 var big_t = reflect.TypeOf(&big.Int{}) 27 var ubig_t = reflect.TypeOf(&big.Int{}) 28 var byte_t = reflect.TypeOf(byte(0)) 29 var byte_ts = reflect.TypeOf([]byte(nil)) 30 var uint_t = reflect.TypeOf(uint(0)) 31 var uint8_t = reflect.TypeOf(uint8(0)) 32 var uint16_t = reflect.TypeOf(uint16(0)) 33 var uint32_t = reflect.TypeOf(uint32(0)) 34 var uint64_t = reflect.TypeOf(uint64(0)) 35 var int_t = reflect.TypeOf(int(0)) 36 var int8_t = reflect.TypeOf(int8(0)) 37 var int16_t = reflect.TypeOf(int16(0)) 38 var int32_t = reflect.TypeOf(int32(0)) 39 var int64_t = reflect.TypeOf(int64(0)) 40 41 var uint_ts = reflect.TypeOf([]uint(nil)) 42 var uint8_ts = reflect.TypeOf([]uint8(nil)) 43 var uint16_ts = reflect.TypeOf([]uint16(nil)) 44 var uint32_ts = reflect.TypeOf([]uint32(nil)) 45 var uint64_ts = reflect.TypeOf([]uint64(nil)) 46 var ubig_ts = reflect.TypeOf([]*big.Int(nil)) 47 48 var int_ts = reflect.TypeOf([]int(nil)) 49 var int8_ts = reflect.TypeOf([]int8(nil)) 50 var int16_ts = reflect.TypeOf([]int16(nil)) 51 var int32_ts = reflect.TypeOf([]int32(nil)) 52 var int64_ts = reflect.TypeOf([]int64(nil)) 53 var big_ts = reflect.TypeOf([]*big.Int(nil)) 54 55 // U256 will ensure unsigned 256bit on big nums 56 func U256(n *big.Int) []byte { 57 return common.LeftPadBytes(common.U256(n).Bytes(), 32) 58 } 59 60 func S256(n *big.Int) []byte { 61 sint := common.S256(n) 62 ret := common.LeftPadBytes(sint.Bytes(), 32) 63 if sint.Cmp(common.Big0) < 0 { 64 for i, b := range ret { 65 if b == 0 { 66 ret[i] = 1 67 continue 68 } 69 break 70 } 71 } 72 73 return ret 74 } 75 76 // S256 will ensure signed 256bit on big nums 77 func U2U256(n uint64) []byte { 78 return U256(big.NewInt(int64(n))) 79 } 80 81 func S2S256(n int64) []byte { 82 return S256(big.NewInt(n)) 83 } 84 85 // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation 86 func packNum(value reflect.Value, to byte) []byte { 87 switch kind := value.Kind(); kind { 88 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 89 if to == UintTy { 90 return U2U256(value.Uint()) 91 } else { 92 return S2S256(int64(value.Uint())) 93 } 94 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 95 if to == UintTy { 96 return U2U256(uint64(value.Int())) 97 } else { 98 return S2S256(value.Int()) 99 } 100 case reflect.Ptr: 101 // This only takes care of packing and casting. No type checking is done here. It should be done prior to using this function. 102 if to == UintTy { 103 return U256(value.Interface().(*big.Int)) 104 } else { 105 return S256(value.Interface().(*big.Int)) 106 } 107 108 } 109 110 return nil 111 } 112 113 // checks whether the given reflect value is signed. This also works for slices with a number type 114 func isSigned(v reflect.Value) bool { 115 switch v.Type() { 116 case ubig_ts, big_ts, big_t, ubig_t: 117 return true 118 case int_ts, int8_ts, int16_ts, int32_ts, int64_ts, int_t, int8_t, int16_t, int32_t, int64_t: 119 return true 120 } 121 return false 122 }