github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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/binary"
    22  	"encoding/hex"
    23  	"unsafe"
    24  )
    25  
    26  func ToHex(b []byte) string {
    27  	hex := Bytes2Hex(b)
    28  	// Prefer output of "0x0" instead of "0x"
    29  	if len(hex) == 0 {
    30  		hex = "0"
    31  	}
    32  	return "0x" + hex
    33  }
    34  
    35  func FromHex(s string) []byte {
    36  	if len(s) > 1 {
    37  		if s[0:2] == "0x" || s[0:2] == "0X" {
    38  			s = s[2:]
    39  		}
    40  	}
    41  	if len(s)%2 == 1 {
    42  		s = "0" + s
    43  	}
    44  	return Hex2Bytes(s)
    45  }
    46  
    47  // Copy bytes
    48  //
    49  // Returns an exact copy of the provided bytes
    50  func CopyBytes(b []byte) (copiedBytes []byte) {
    51  	if b == nil {
    52  		return nil
    53  	}
    54  	copiedBytes = make([]byte, len(b))
    55  	copy(copiedBytes, b)
    56  
    57  	return
    58  }
    59  
    60  func hasHexPrefix(str string) bool {
    61  	return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
    62  }
    63  
    64  func isHexCharacter(c byte) bool {
    65  	return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
    66  }
    67  
    68  func isHex(str string) bool {
    69  	if len(str)%2 != 0 {
    70  		return false
    71  	}
    72  	for _, c := range []byte(str) {
    73  		if !isHexCharacter(c) {
    74  			return false
    75  		}
    76  	}
    77  	return true
    78  }
    79  
    80  func Bytes2Hex(d []byte) string {
    81  	return hex.EncodeToString(d)
    82  }
    83  
    84  func Hex2Bytes(str string) []byte {
    85  	h, _ := hex.DecodeString(str)
    86  
    87  	return h
    88  }
    89  
    90  func Hex2BytesFixed(str string, flen int) []byte {
    91  	h, _ := hex.DecodeString(str)
    92  	if len(h) == flen {
    93  		return h
    94  	} else {
    95  		if len(h) > flen {
    96  			return h[len(h)-flen:]
    97  		} else {
    98  			hh := make([]byte, flen)
    99  			copy(hh[flen-len(h):flen], h[:])
   100  			return hh
   101  		}
   102  	}
   103  }
   104  
   105  func RightPadBytes(slice []byte, l int) []byte {
   106  	if l <= len(slice) {
   107  		return slice
   108  	}
   109  
   110  	padded := make([]byte, l)
   111  	copy(padded, slice)
   112  
   113  	return padded
   114  }
   115  
   116  func LeftPadBytes(slice []byte, l int) []byte {
   117  	if l <= len(slice) {
   118  		return slice
   119  	}
   120  
   121  	padded := make([]byte, l)
   122  	copy(padded[l-len(slice):], slice)
   123  
   124  	return padded
   125  }
   126  
   127  const INT_SIZE int = int(unsafe.Sizeof(0))
   128  
   129  //decide the system endian; true means big endian
   130  func isBigEndian() bool {
   131  	var i int = 0x1
   132  	bs := (*[INT_SIZE]byte)(unsafe.Pointer(&i))
   133  	if bs[0] == 0 {
   134  		return false
   135  	}
   136  
   137  	return true
   138  }
   139  
   140  func Bytes2Uint64(b []byte) uint64 {
   141  
   142  	data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
   143  	blen := len(b)
   144  
   145  	for i := 1; i <= blen; i++ {
   146  		data[8-i] = b[len(b)-i]
   147  	}
   148  
   149  	if isBigEndian() {
   150  		return binary.BigEndian.Uint64(data)
   151  	} else {
   152  		return binary.LittleEndian.Uint64(data)
   153  	}
   154  }