github.com/klaytn/klaytn@v1.12.1/blockchain/vm/common.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/vm/common.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package vm
    22  
    23  import (
    24  	"github.com/holiman/uint256"
    25  	"github.com/klaytn/klaytn/common"
    26  	"github.com/klaytn/klaytn/common/math"
    27  )
    28  
    29  // calcMemSize64 calculates the required memory size, and returns
    30  // the size and whether the result overflowed uint64
    31  func calcMemSize64(off, l *uint256.Int) (uint64, bool) {
    32  	if !l.IsUint64() {
    33  		return 0, true
    34  	}
    35  	return calcMemSize64WithUint(off, l.Uint64())
    36  }
    37  
    38  // calcMemSize64WithUint calculates the required memory size, and returns
    39  // the size and whether the result overflowed uint64
    40  // Identical to calcMemSize64, but length is a uint64
    41  func calcMemSize64WithUint(off *uint256.Int, length64 uint64) (uint64, bool) {
    42  	// if length is zero, memsize is always zero, regardless of offset
    43  	if length64 == 0 {
    44  		return 0, false
    45  	}
    46  	// Check that offset doesn't overflow
    47  	offset64, overflow := off.Uint64WithOverflow()
    48  	if overflow {
    49  		return 0, true
    50  	}
    51  	val := offset64 + length64
    52  	// if value < either of it's parts, then it overflowed
    53  	return val, val < offset64
    54  }
    55  
    56  // getData returns a slice from the data based on the start and size and pads
    57  // up to size with zero's. This function is overflow safe.
    58  func getData(data []byte, start uint64, size uint64) []byte {
    59  	length := uint64(len(data))
    60  	if start > length {
    61  		start = length
    62  	}
    63  	end := start + size
    64  	if end > length {
    65  		end = length
    66  	}
    67  	return common.RightPadBytes(data[start:end], int(size))
    68  }
    69  
    70  // toWordSize returns the ceiled word size required for memory expansion.
    71  func toWordSize(size uint64) uint64 {
    72  	if size > math.MaxUint64-31 {
    73  		return math.MaxUint64/32 + 1
    74  	}
    75  
    76  	return (size + 31) / 32
    77  }
    78  
    79  func allZero(b []byte) bool {
    80  	for _, byte := range b {
    81  		if byte != 0 {
    82  			return false
    83  		}
    84  	}
    85  	return true
    86  }