2019-01-31 02:53:58 +00:00
|
|
|
// Package bytesutil defines helper methods for converting integers to byte slices.
|
|
|
|
package bytesutil
|
2018-11-08 16:52:51 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-23 16:53:50 +00:00
|
|
|
"fmt"
|
2018-11-08 16:52:51 +00:00
|
|
|
)
|
|
|
|
|
2022-11-02 18:04:40 +00:00
|
|
|
// ToBytes48Array is a convenience method for converting an array of
|
|
|
|
// byte slices to an array of fixed-sized byte arrays.
|
|
|
|
func ToBytes48Array(x [][]byte) [][48]byte {
|
|
|
|
y := make([][48]byte, len(x))
|
|
|
|
for i := range x {
|
|
|
|
y[i] = ToBytes48(x[i])
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2020-03-05 05:35:14 +00:00
|
|
|
// ToBool is a convenience method for converting a byte to a bool.
|
|
|
|
// This method will use the first bit of the 0 byte to generate the returned value.
|
|
|
|
func ToBool(x byte) bool {
|
|
|
|
return x&1 == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromBool is a convenience method for converting a bool to a byte.
|
|
|
|
// This method will use the first bit to generate the returned value.
|
|
|
|
func FromBool(x bool) byte {
|
|
|
|
if x {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-01-04 03:51:53 +00:00
|
|
|
// FromBytes48 is a convenience method for converting a fixed-size byte array
|
|
|
|
// to a byte slice.
|
|
|
|
func FromBytes48(x [48]byte) []byte {
|
|
|
|
return x[:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromBytes48Array is a convenience method for converting an array of
|
|
|
|
// fixed-size byte arrays to an array of byte slices.
|
|
|
|
func FromBytes48Array(x [][48]byte) [][]byte {
|
|
|
|
y := make([][]byte, len(x))
|
|
|
|
for i := range x {
|
|
|
|
y[i] = x[i][:]
|
|
|
|
}
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
|
2019-10-02 04:18:01 +00:00
|
|
|
// Trunc truncates the byte slices to 6 bytes.
|
2019-04-29 18:11:52 +00:00
|
|
|
func Trunc(x []byte) []byte {
|
2019-10-02 04:18:01 +00:00
|
|
|
if len(x) > 6 {
|
|
|
|
return x[:6]
|
2019-04-29 18:11:52 +00:00
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
2019-09-05 15:32:35 +00:00
|
|
|
|
2021-08-23 16:53:50 +00:00
|
|
|
// SafeCopyRootAtIndex takes a copy of an 32-byte slice in a slice of byte slices. Returns error if index out of range.
|
|
|
|
func SafeCopyRootAtIndex(input [][]byte, idx uint64) ([]byte, error) {
|
|
|
|
if input == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if uint64(len(input)) <= idx {
|
|
|
|
return nil, fmt.Errorf("index %d out of range", idx)
|
|
|
|
}
|
|
|
|
item := make([]byte, 32)
|
|
|
|
copy(item, input[idx])
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:41:01 +00:00
|
|
|
// SafeCopyBytes will copy and return a non-nil byte slice, otherwise it returns nil.
|
2020-02-14 01:03:51 +00:00
|
|
|
func SafeCopyBytes(cp []byte) []byte {
|
|
|
|
if cp != nil {
|
|
|
|
copied := make([]byte, len(cp))
|
|
|
|
copy(copied, cp)
|
|
|
|
return copied
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-10 16:41:01 +00:00
|
|
|
// SafeCopy2dBytes will copy and return a non-nil 2d byte slice, otherwise it returns nil.
|
2021-08-23 16:53:50 +00:00
|
|
|
func SafeCopy2dBytes(ary [][]byte) [][]byte {
|
2020-02-14 01:03:51 +00:00
|
|
|
if ary != nil {
|
|
|
|
copied := make([][]byte, len(ary))
|
|
|
|
for i, a := range ary {
|
|
|
|
copied[i] = SafeCopyBytes(a)
|
|
|
|
}
|
|
|
|
return copied
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-18 21:10:54 +00:00
|
|
|
|
2023-01-10 16:41:01 +00:00
|
|
|
// SafeCopy2d32Bytes will copy and return a non-nil 2d byte slice, otherwise it returns nil.
|
2022-01-24 10:24:38 +00:00
|
|
|
func SafeCopy2d32Bytes(ary [][32]byte) [][32]byte {
|
|
|
|
if ary != nil {
|
|
|
|
copied := make([][32]byte, len(ary))
|
|
|
|
copy(copied, ary)
|
|
|
|
return copied
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-18 21:10:54 +00:00
|
|
|
// ReverseBytes32Slice will reverse the provided slice's order.
|
|
|
|
func ReverseBytes32Slice(arr [][32]byte) [][32]byte {
|
|
|
|
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
|
|
|
|
arr[i], arr[j] = arr[j], arr[i]
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
2020-03-19 02:39:23 +00:00
|
|
|
|
|
|
|
// PadTo pads a byte slice to the given size. If the byte slice is larger than the given size, the
|
|
|
|
// original slice is returned.
|
|
|
|
func PadTo(b []byte, size int) []byte {
|
2023-01-10 16:41:01 +00:00
|
|
|
if len(b) >= size {
|
2020-03-19 02:39:23 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
return append(b, make([]byte, size-len(b))...)
|
2020-03-20 01:46:35 +00:00
|
|
|
}
|
2020-03-22 23:19:38 +00:00
|
|
|
|
2022-01-11 18:42:03 +00:00
|
|
|
// ReverseByteOrder Switch the endianness of a byte slice by reversing its order.
|
|
|
|
// this function does not modify the actual input bytes.
|
|
|
|
func ReverseByteOrder(input []byte) []byte {
|
|
|
|
b := make([]byte, len(input))
|
|
|
|
copy(b, input)
|
|
|
|
for i := 0; i < len(b)/2; i++ {
|
|
|
|
b[i], b[len(b)-i-1] = b[len(b)-i-1], b[i]
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|