common.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2014 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. package vm
  17. import (
  18. "math/big"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/common/math"
  21. )
  22. // calcMemSize64 calculates the required memory size, and returns
  23. // the size and whether the result overflowed uint64
  24. func calcMemSize64(off, l *big.Int) (uint64, bool) {
  25. if !l.IsUint64() {
  26. return 0, true
  27. }
  28. return calcMemSize64WithUint(off, l.Uint64())
  29. }
  30. // calcMemSize64WithUint calculates the required memory size, and returns
  31. // the size and whether the result overflowed uint64
  32. // Identical to calcMemSize64, but length is a uint64
  33. func calcMemSize64WithUint(off *big.Int, length64 uint64) (uint64, bool) {
  34. // if length is zero, memsize is always zero, regardless of offset
  35. if length64 == 0 {
  36. return 0, false
  37. }
  38. // Check that offset doesn't overflow
  39. if !off.IsUint64() {
  40. return 0, true
  41. }
  42. offset64 := off.Uint64()
  43. val := offset64 + length64
  44. // if value < either of it's parts, then it overflowed
  45. return val, val < offset64
  46. }
  47. // getData returns a slice from the data based on the start and size and pads
  48. // up to size with zero's. This function is overflow safe.
  49. func getData(data []byte, start uint64, size uint64) []byte {
  50. length := uint64(len(data))
  51. if start > length {
  52. start = length
  53. }
  54. end := start + size
  55. if end > length {
  56. end = length
  57. }
  58. return common.RightPadBytes(data[start:end], int(size))
  59. }
  60. // getDataBig returns a slice from the data based on the start and size and pads
  61. // up to size with zero's. This function is overflow safe.
  62. func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
  63. dlen := big.NewInt(int64(len(data)))
  64. s := math.BigMin(start, dlen)
  65. e := math.BigMin(new(big.Int).Add(s, size), dlen)
  66. return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
  67. }
  68. // bigUint64 returns the integer casted to a uint64 and returns whether it
  69. // overflowed in the process.
  70. func bigUint64(v *big.Int) (uint64, bool) {
  71. return v.Uint64(), !v.IsUint64()
  72. }
  73. // toWordSize returns the ceiled word size required for memory expansion.
  74. func toWordSize(size uint64) uint64 {
  75. if size > math.MaxUint64-31 {
  76. return math.MaxUint64/32 + 1
  77. }
  78. return (size + 31) / 32
  79. }
  80. func allZero(b []byte) bool {
  81. for _, byte := range b {
  82. if byte != 0 {
  83. return false
  84. }
  85. }
  86. return true
  87. }