github.com/Cleverse/go-ethereum@v0.0.0-20220927095127-45113064e7f2/core/vm/common.go (about)

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