github.com/annchain/OG@v0.0.9/common/bytes.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 contains various helper functions. 18 package common 19 20 import ( 21 "encoding/hex" 22 "github.com/annchain/OG/arefactor/common/utilfuncs" 23 ) 24 25 // ToHex returns the hex representation of b, prefixed with '0x'. 26 // For empty slices, the return value is "0x0". 27 // 28 // Deprecated: use hexutil.Encode instead. 29 func ToHex(b []byte) string { 30 hexstr := Bytes2Hex(b) 31 if len(hexstr) == 0 { 32 hexstr = "0" 33 } 34 return "0x" + hexstr 35 } 36 37 // FromHex returns the bytes represented by the hexadecimal string s. 38 // s may be prefixed with "0x". 39 func FromHex(s string) ([]byte, error) { 40 if len(s) > 1 { 41 if s[0:2] == "0x" || s[0:2] == "0X" { 42 s = s[2:] 43 } 44 } 45 if len(s)%2 == 1 { 46 s = "0" + s 47 } 48 return Hex2Bytes(s) 49 } 50 51 // CopyBytes returns an exact copy of the provided bytes. 52 func CopyBytes(b []byte) (copiedBytes []byte) { 53 if b == nil { 54 return nil 55 } 56 copiedBytes = make([]byte, len(b)) 57 copy(copiedBytes, b) 58 59 return 60 } 61 62 // hasHexPrefix validates str begins with '0x' or '0X'. 63 func hasHexPrefix(str string) bool { 64 return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') 65 } 66 67 // isHexCharacter returns bool of c being a valid hexadecimal. 68 func isHexCharacter(c byte) bool { 69 return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') 70 } 71 72 // isHex validates whether each byte is valid hexadecimal string. 73 func isHex(str string) bool { 74 if len(str)%2 != 0 { 75 return false 76 } 77 for _, c := range []byte(str) { 78 if !isHexCharacter(c) { 79 return false 80 } 81 } 82 return true 83 } 84 85 // Bytes2Hex returns the hexadecimal encoding of d. 86 func Bytes2Hex(d []byte) string { 87 return hex.EncodeToString(d) 88 } 89 90 // Hex2Bytes returns the bytes represented by the hexadecimal string str. 91 func Hex2Bytes(str string) ([]byte, error) { 92 h, err := hex.DecodeString(str) 93 return h, err 94 } 95 96 // Hex2BytesNoError panics if hex is wrong. Use it ONLY in test code 97 func Hex2BytesNoError(str string) []byte { 98 h, err := Hex2Bytes(str) 99 utilfuncs.PanicIfError(err, "hex2bytes") 100 return h 101 } 102 103 // Hex2BytesFixed returns bytes of a specified fixed length flen. 104 func Hex2BytesFixed(str string, flen int) []byte { 105 h, _ := hex.DecodeString(str) 106 if len(h) == flen { 107 return h 108 } 109 if len(h) > flen { 110 return h[len(h)-flen:] 111 } 112 hh := make([]byte, flen) 113 copy(hh[flen-len(h):flen], h[:]) 114 return hh 115 } 116 117 // RightPadBytes zero-pads slice to the right up to length l. 118 func RightPadBytes(slice []byte, l int) []byte { 119 if l <= len(slice) { 120 return slice 121 } 122 123 padded := make([]byte, l) 124 copy(padded, slice) 125 126 return padded 127 } 128 129 // LeftPadBytes zero-pads slice to the left up to length l. 130 func LeftPadBytes(slice []byte, l int) []byte { 131 if l <= len(slice) { 132 return slice 133 } 134 135 padded := make([]byte, l) 136 copy(padded[l-len(slice):], slice) 137 138 return padded 139 } 140 141 // IsSameBytes compares two byte slice. Return true if they are the 142 // same, otherwise false. 143 func IsSameBytes(s, t []byte) bool { 144 if (s == nil) != (t == nil) { 145 return false 146 } 147 if len(s) != len(t) { 148 return false 149 } 150 for i := range s { 151 if s[i] != t[i] { 152 return false 153 } 154 } 155 return true 156 } 157 158 // GetInt32 get an int32 from byte array with a start position. 159 func GetInt32(b []byte, pos int) int32 { 160 return int32(b[pos]) | int32(b[pos+1])<<8 | int32(b[pos+2])<<16 | int32(b[pos+3])<<24 161 } 162 163 // SetInt32 set an int32 into byte array at a position. 164 func SetInt32(b []byte, pos int, i int32) { 165 b[pos] = byte(i) 166 b[pos+1] = byte(i >> 8) 167 b[pos+2] = byte(i >> 16) 168 b[pos+3] = byte(i >> 24) 169 } 170 171 // ByteInt32 convert an int32 to byte array. 172 func ByteInt32(i int32) []byte { 173 b := make([]byte, 4) 174 175 b[0] = byte(i) 176 b[1] = byte(i >> 8) 177 b[2] = byte(i >> 16) 178 b[3] = byte(i >> 24) 179 180 return b 181 }