github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/core/vm/common.go (about)

     1  package vm
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"github.com/neatio-net/neatio/utilities/common"
     7  	"github.com/neatio-net/neatio/utilities/common/math"
     8  )
     9  
    10  func calcMemSize64(off, l *big.Int) (uint64, bool) {
    11  	if !l.IsUint64() {
    12  		return 0, true
    13  	}
    14  	return calcMemSize64WithUint(off, l.Uint64())
    15  }
    16  
    17  func calcMemSize64WithUint(off *big.Int, length64 uint64) (uint64, bool) {
    18  
    19  	if length64 == 0 {
    20  		return 0, false
    21  	}
    22  
    23  	if !off.IsUint64() {
    24  		return 0, true
    25  	}
    26  	offset64 := off.Uint64()
    27  	val := offset64 + length64
    28  
    29  	return val, val < offset64
    30  }
    31  
    32  func getData(data []byte, start uint64, size uint64) []byte {
    33  	length := uint64(len(data))
    34  	if start > length {
    35  		start = length
    36  	}
    37  	end := start + size
    38  	if end > length {
    39  		end = length
    40  	}
    41  	return common.RightPadBytes(data[start:end], int(size))
    42  }
    43  
    44  func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
    45  	dlen := big.NewInt(int64(len(data)))
    46  
    47  	s := math.BigMin(start, dlen)
    48  	e := math.BigMin(new(big.Int).Add(s, size), dlen)
    49  	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
    50  }
    51  
    52  func bigUint64(v *big.Int) (uint64, bool) {
    53  	return v.Uint64(), !v.IsUint64()
    54  }
    55  
    56  func toWordSize(size uint64) uint64 {
    57  	if size > math.MaxUint64-31 {
    58  		return math.MaxUint64/32 + 1
    59  	}
    60  
    61  	return (size + 31) / 32
    62  }
    63  
    64  func allZero(b []byte) bool {
    65  	for _, byte := range b {
    66  		if byte != 0 {
    67  			return false
    68  		}
    69  	}
    70  	return true
    71  }