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