2018-05-24 23:47:16 -05:00
|
|
|
// Copyright 2018 Google LLC. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Package binaryutil contains convenience wrappers around encoding/binary.
|
|
|
|
package binaryutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2022-02-10 01:29:02 -06:00
|
|
|
"unsafe"
|
2018-05-24 23:47:16 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ByteOrder is like binary.ByteOrder, but allocates memory and returns byte
|
|
|
|
// slices, for convenience.
|
|
|
|
type ByteOrder interface {
|
|
|
|
PutUint16(v uint16) []byte
|
|
|
|
PutUint32(v uint32) []byte
|
2018-06-23 14:12:14 -05:00
|
|
|
PutUint64(v uint64) []byte
|
|
|
|
Uint32(b []byte) uint32
|
|
|
|
Uint64(b []byte) uint64
|
2018-05-24 23:47:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NativeEndian is either little endian or big endian, depending on the native
|
|
|
|
// endian-ness, and allocates memory and returns byte slices, for convenience.
|
|
|
|
var NativeEndian ByteOrder = &nativeEndian{}
|
|
|
|
|
|
|
|
type nativeEndian struct{}
|
|
|
|
|
|
|
|
func (nativeEndian) PutUint16(v uint16) []byte {
|
|
|
|
buf := make([]byte, 2)
|
2022-02-10 01:29:02 -06:00
|
|
|
*(*uint16)(unsafe.Pointer(&buf[0])) = v
|
2018-05-24 23:47:16 -05:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nativeEndian) PutUint32(v uint32) []byte {
|
|
|
|
buf := make([]byte, 4)
|
2022-02-10 01:29:02 -06:00
|
|
|
*(*uint32)(unsafe.Pointer(&buf[0])) = v
|
2018-05-24 23:47:16 -05:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2018-06-23 14:12:14 -05:00
|
|
|
func (nativeEndian) PutUint64(v uint64) []byte {
|
|
|
|
buf := make([]byte, 8)
|
2022-02-10 01:29:02 -06:00
|
|
|
*(*uint64)(unsafe.Pointer(&buf[0])) = v
|
2018-06-23 14:12:14 -05:00
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nativeEndian) Uint32(b []byte) uint32 {
|
2022-02-10 01:29:02 -06:00
|
|
|
return *(*uint32)(unsafe.Pointer(&b[0]))
|
2018-06-23 14:12:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nativeEndian) Uint64(b []byte) uint64 {
|
2022-02-10 01:29:02 -06:00
|
|
|
return *(*uint64)(unsafe.Pointer(&b[0]))
|
2018-06-23 14:12:14 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 23:47:16 -05:00
|
|
|
// BigEndian is like binary.BigEndian, but allocates memory and returns byte
|
|
|
|
// slices, for convenience.
|
|
|
|
var BigEndian ByteOrder = &bigEndian{}
|
|
|
|
|
|
|
|
type bigEndian struct{}
|
|
|
|
|
|
|
|
func (bigEndian) PutUint16(v uint16) []byte {
|
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.BigEndian.PutUint16(buf, v)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bigEndian) PutUint32(v uint32) []byte {
|
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(buf, v)
|
|
|
|
return buf
|
|
|
|
}
|
2018-06-23 14:12:14 -05:00
|
|
|
|
|
|
|
func (bigEndian) PutUint64(v uint64) []byte {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(buf, v)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bigEndian) Uint32(b []byte) uint32 {
|
|
|
|
return binary.BigEndian.Uint32(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bigEndian) Uint64(b []byte) uint64 {
|
|
|
|
return binary.BigEndian.Uint64(b)
|
|
|
|
}
|