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