github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/core/vm/common.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package vm
    13  
    14  import (
    15  	"math/big"
    16  
    17  	"github.com/Sberex/go-sberex/common"
    18  	"github.com/Sberex/go-sberex/common/math"
    19  )
    20  
    21  // calculates the memory size required for a step
    22  func calcMemSize(off, l *big.Int) *big.Int {
    23  	if l.Sign() == 0 {
    24  		return common.Big0
    25  	}
    26  
    27  	return new(big.Int).Add(off, l)
    28  }
    29  
    30  // getData returns a slice from the data based on the start and size and pads
    31  // up to size with zero's. This function is overflow safe.
    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  // getDataBig returns a slice from the data based on the start and size and pads
    45  // up to size with zero's. This function is overflow safe.
    46  func getDataBig(data []byte, start *big.Int, size *big.Int) []byte {
    47  	dlen := big.NewInt(int64(len(data)))
    48  
    49  	s := math.BigMin(start, dlen)
    50  	e := math.BigMin(new(big.Int).Add(s, size), dlen)
    51  	return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
    52  }
    53  
    54  // bigUint64 returns the integer casted to a uint64 and returns whether it
    55  // overflowed in the process.
    56  func bigUint64(v *big.Int) (uint64, bool) {
    57  	return v.Uint64(), v.BitLen() > 64
    58  }
    59  
    60  // toWordSize returns the ceiled word size required for memory expansion.
    61  func toWordSize(size uint64) uint64 {
    62  	if size > math.MaxUint64-31 {
    63  		return math.MaxUint64/32 + 1
    64  	}
    65  
    66  	return (size + 31) / 32
    67  }
    68  
    69  func allZero(b []byte) bool {
    70  	for _, byte := range b {
    71  		if byte != 0 {
    72  			return false
    73  		}
    74  	}
    75  	return true
    76  }