common.go 3 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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

obscuren's avatar
obscuren committed
17
package vm
18 19

import (
20 21
	"math/big"

obscuren's avatar
obscuren committed
22
	"github.com/ethereum/go-ethereum/common"
23
	"github.com/ethereum/go-ethereum/common/math"
24 25
)

26 27 28 29 30
// calcMemSize64 calculates the required memory size, and returns
// the size and whether the result overflowed uint64
func calcMemSize64(off, l *big.Int) (uint64, bool) {
	if !l.IsUint64() {
		return 0, true
31
	}
32 33
	return calcMemSize64WithUint(off, l.Uint64())
}
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// calcMemSize64WithUint calculates the required memory size, and returns
// the size and whether the result overflowed uint64
// Identical to calcMemSize64, but length is a uint64
func calcMemSize64WithUint(off *big.Int, length64 uint64) (uint64, bool) {
	// if length is zero, memsize is always zero, regardless of offset
	if length64 == 0 {
		return 0, false
	}
	// Check that offset doesn't overflow
	if !off.IsUint64() {
		return 0, true
	}
	offset64 := off.Uint64()
	val := offset64 + length64
	// if value < either of it's parts, then it overflowed
	return val, val < offset64
51 52
}

53 54
// getData returns a slice from the data based on the start and size and pads
// up to size with zero's. This function is overflow safe.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
func getData(data []byte, start uint64, size uint64) []byte {
	length := uint64(len(data))
	if start > length {
		start = length
	}
	end := start + size
	if end > length {
		end = length
	}
	return common.RightPadBytes(data[start:end], int(size))
}

// getDataBig returns a slice from the data based on the start and size and pads
// up to size with zero's. This function is overflow safe.
func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
obscuren's avatar
obscuren committed
70
	dlen := big.NewInt(int64(len(data)))
71

72 73
	s := math.BigMin(start, dlen)
	e := math.BigMin(new(big.Int).Add(s, size), dlen)
obscuren's avatar
obscuren committed
74
	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
75
}
obscuren's avatar
obscuren committed
76

77 78 79
// bigUint64 returns the integer casted to a uint64 and returns whether it
// overflowed in the process.
func bigUint64(v *big.Int) (uint64, bool) {
80
	return v.Uint64(), !v.IsUint64()
81 82 83 84 85 86
}

// toWordSize returns the ceiled word size required for memory expansion.
func toWordSize(size uint64) uint64 {
	if size > math.MaxUint64-31 {
		return math.MaxUint64/32 + 1
obscuren's avatar
obscuren committed
87 88
	}

89
	return (size + 31) / 32
obscuren's avatar
obscuren committed
90
}
91 92 93 94 95 96 97 98 99

func allZero(b []byte) bool {
	for _, byte := range b {
		if byte != 0 {
			return false
		}
	}
	return true
}