github.com/annchain/OG@v0.0.9/vm/eth/common/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 common
    18  
    19  import (
    20  	math2 "github.com/annchain/OG/arefactor/common/math"
    21  	"math/big"
    22  )
    23  
    24  // calculates the memory size required for a step
    25  func CalcMemSize(off, l *big.Int) *big.Int {
    26  	if l.Sign() == 0 {
    27  		return Big0
    28  	}
    29  
    30  	return new(big.Int).Add(off, l)
    31  }
    32  
    33  // getData returns a slice from the data based on the start and size and pads
    34  // up to size with zero's. This function is overflow safe.
    35  func GetData(data []byte, start uint64, size uint64) []byte {
    36  	length := uint64(len(data))
    37  	if start > length {
    38  		start = length
    39  	}
    40  	end := start + size
    41  	if end > length {
    42  		end = length
    43  	}
    44  	return RightPadBytes(data[start:end], int(size))
    45  }
    46  
    47  // getDataBig returns a slice from the data based on the start and size and pads
    48  // up to size with zero's. This function is overflow safe.
    49  func GetDataBig(data []byte, start *big.Int, size *big.Int) []byte {
    50  	dlen := big.NewInt(int64(len(data)))
    51  
    52  	s := math2.BigMin(start, dlen)
    53  	e := math2.BigMin(new(big.Int).Add(s, size), dlen)
    54  	return RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64()))
    55  }
    56  
    57  // bigUint64 returns the integer casted to a uint64 and returns whether it
    58  // overflowed in the process.
    59  func BigUint64(v *big.Int) (uint64, bool) {
    60  	return v.Uint64(), v.BitLen() > 64
    61  }
    62  
    63  // toWordSize returns the ceiled word size required for memory expansion.
    64  func ToWordSize(size uint64) uint64 {
    65  	if size > math2.BiggerUint64-31 {
    66  		return math2.BiggerUint64/32 + 1
    67  	}
    68  
    69  	return (size + 31) / 32
    70  }
    71  
    72  func AllZero(b []byte) bool {
    73  	for _, byte := range b {
    74  		if byte != 0 {
    75  			return false
    76  		}
    77  	}
    78  	return true
    79  }