github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/vm/common.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package vm 19 20 import ( 21 "math/big" 22 23 "github.com/AigarNetwork/aigar/common" 24 "github.com/AigarNetwork/aigar/common/math" 25 ) 26 27 // calcMemSize64 calculates the required memory size, and returns 28 // the size and whether the result overflowed uint64 29 func calcMemSize64(off, l *big.Int) (uint64, bool) { 30 if !l.IsUint64() { 31 return 0, true 32 } 33 return calcMemSize64WithUint(off, l.Uint64()) 34 } 35 36 // calcMemSize64WithUint calculates the required memory size, and returns 37 // the size and whether the result overflowed uint64 38 // Identical to calcMemSize64, but length is a uint64 39 func calcMemSize64WithUint(off *big.Int, length64 uint64) (uint64, bool) { 40 // if length is zero, memsize is always zero, regardless of offset 41 if length64 == 0 { 42 return 0, false 43 } 44 // Check that offset doesn't overflow 45 if !off.IsUint64() { 46 return 0, true 47 } 48 offset64 := off.Uint64() 49 val := offset64 + length64 50 // if value < either of it's parts, then it overflowed 51 return val, val < offset64 52 } 53 54 // getData returns a slice from the data based on the start and size and pads 55 // up to size with zero's. This function is overflow safe. 56 func getData(data []byte, start uint64, size uint64) []byte { 57 length := uint64(len(data)) 58 if start > length { 59 start = length 60 } 61 end := start + size 62 if end > length { 63 end = length 64 } 65 return common.RightPadBytes(data[start:end], int(size)) 66 } 67 68 // getDataBig returns a slice from the data based on the start and size and pads 69 // up to size with zero's. This function is overflow safe. 70 func getDataBig(data []byte, start *big.Int, size *big.Int) []byte { 71 dlen := big.NewInt(int64(len(data))) 72 73 s := math.BigMin(start, dlen) 74 e := math.BigMin(new(big.Int).Add(s, size), dlen) 75 return common.RightPadBytes(data[s.Uint64():e.Uint64()], int(size.Uint64())) 76 } 77 78 // bigUint64 returns the integer casted to a uint64 and returns whether it 79 // overflowed in the process. 80 func bigUint64(v *big.Int) (uint64, bool) { 81 return v.Uint64(), !v.IsUint64() 82 } 83 84 // toWordSize returns the ceiled word size required for memory expansion. 85 func toWordSize(size uint64) uint64 { 86 if size > math.MaxUint64-31 { 87 return math.MaxUint64/32 + 1 88 } 89 90 return (size + 31) / 32 91 } 92 93 func allZero(b []byte) bool { 94 for _, byte := range b { 95 if byte != 0 { 96 return false 97 } 98 } 99 return true 100 }