github.com/core-coin/go-core/v2@v2.1.9/core/vm/common.go (about)

     1  // Copyright 2014 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package vm
    18  
    19  import (
    20  	"github.com/core-coin/uint256"
    21  
    22  	"github.com/core-coin/go-core/v2/common"
    23  	"github.com/core-coin/go-core/v2/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 *uint256.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 *uint256.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  	offset64, overflow := off.Uint64WithOverflow()
    45  	if overflow {
    46  		return 0, true
    47  	}
    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  // toWordSize returns the ceiled word size required for memory expansion.
    68  func toWordSize(size uint64) uint64 {
    69  	if size > math.MaxUint64-31 {
    70  		return math.MaxUint64/32 + 1
    71  	}
    72  
    73  	return (size + 31) / 32
    74  }
    75  
    76  func allZero(b []byte) bool {
    77  	for _, byte := range b {
    78  		if byte != 0 {
    79  			return false
    80  		}
    81  	}
    82  	return true
    83  }