common.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/common/math"
  20. "github.com/holiman/uint256"
  21. )
  22. // calcMemSize64 calculates the required memory size, and returns
  23. // the size and whether the result overflowed uint64
  24. func calcMemSize64(off, l *uint256.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 *uint256.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. offset64, overflow := off.Uint64WithOverflow()
  40. if overflow {
  41. return 0, true
  42. }
  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. // toWordSize returns the ceiled word size required for memory expansion.
  61. func toWordSize(size uint64) uint64 {
  62. if size > math.MaxUint64-31 {
  63. return math.MaxUint64/32 + 1
  64. }
  65. return (size + 31) / 32
  66. }
  67. func allZero(b []byte) bool {
  68. for _, byte := range b {
  69. if byte != 0 {
  70. return false
  71. }
  72. }
  73. return true
  74. }