numbers.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package abi
  17. import (
  18. "math/big"
  19. "reflect"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. var big_t = reflect.TypeOf(&big.Int{})
  23. var ubig_t = reflect.TypeOf(&big.Int{})
  24. var byte_t = reflect.TypeOf(byte(0))
  25. var byte_ts = reflect.TypeOf([]byte(nil))
  26. var uint_t = reflect.TypeOf(uint(0))
  27. var uint8_t = reflect.TypeOf(uint8(0))
  28. var uint16_t = reflect.TypeOf(uint16(0))
  29. var uint32_t = reflect.TypeOf(uint32(0))
  30. var uint64_t = reflect.TypeOf(uint64(0))
  31. var int_t = reflect.TypeOf(int(0))
  32. var int8_t = reflect.TypeOf(int8(0))
  33. var int16_t = reflect.TypeOf(int16(0))
  34. var int32_t = reflect.TypeOf(int32(0))
  35. var int64_t = reflect.TypeOf(int64(0))
  36. var uint_ts = reflect.TypeOf([]uint(nil))
  37. var uint8_ts = reflect.TypeOf([]uint8(nil))
  38. var uint16_ts = reflect.TypeOf([]uint16(nil))
  39. var uint32_ts = reflect.TypeOf([]uint32(nil))
  40. var uint64_ts = reflect.TypeOf([]uint64(nil))
  41. var ubig_ts = reflect.TypeOf([]*big.Int(nil))
  42. var int_ts = reflect.TypeOf([]int(nil))
  43. var int8_ts = reflect.TypeOf([]int8(nil))
  44. var int16_ts = reflect.TypeOf([]int16(nil))
  45. var int32_ts = reflect.TypeOf([]int32(nil))
  46. var int64_ts = reflect.TypeOf([]int64(nil))
  47. var big_ts = reflect.TypeOf([]*big.Int(nil))
  48. // U256 will ensure unsigned 256bit on big nums
  49. func U256(n *big.Int) []byte {
  50. return common.LeftPadBytes(common.U256(n).Bytes(), 32)
  51. }
  52. func S256(n *big.Int) []byte {
  53. sint := common.S256(n)
  54. ret := common.LeftPadBytes(sint.Bytes(), 32)
  55. if sint.Cmp(common.Big0) < 0 {
  56. for i, b := range ret {
  57. if b == 0 {
  58. ret[i] = 1
  59. continue
  60. }
  61. break
  62. }
  63. }
  64. return ret
  65. }
  66. // S256 will ensure signed 256bit on big nums
  67. func U2U256(n uint64) []byte {
  68. return U256(big.NewInt(int64(n)))
  69. }
  70. func S2S256(n int64) []byte {
  71. return S256(big.NewInt(n))
  72. }
  73. // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
  74. func packNum(value reflect.Value, to byte) []byte {
  75. switch kind := value.Kind(); kind {
  76. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  77. if to == UintTy {
  78. return U2U256(value.Uint())
  79. } else {
  80. return S2S256(int64(value.Uint()))
  81. }
  82. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  83. if to == UintTy {
  84. return U2U256(uint64(value.Int()))
  85. } else {
  86. return S2S256(value.Int())
  87. }
  88. case reflect.Ptr:
  89. // This only takes care of packing and casting. No type checking is done here. It should be done prior to using this function.
  90. if to == UintTy {
  91. return U256(value.Interface().(*big.Int))
  92. } else {
  93. return S256(value.Interface().(*big.Int))
  94. }
  95. }
  96. return nil
  97. }
  98. // checks whether the given reflect value is signed. This also works for slices with a number type
  99. func isSigned(v reflect.Value) bool {
  100. switch v.Type() {
  101. case ubig_ts, big_ts, big_t, ubig_t:
  102. return true
  103. case int_ts, int8_ts, int16_ts, int32_ts, int64_ts, int_t, int8_t, int16_t, int32_t, int64_t:
  104. return true
  105. }
  106. return false
  107. }