summaryrefslogtreecommitdiff
path: root/lib/binstruct/marshal.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-09 00:21:21 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:16:53 -0700
commit5bdff4a4fb6c497f3a28c2fc5ba5280233df979e (patch)
tree1785d98eada9e8bf7a818988ad994c8c998f5480 /lib/binstruct/marshal.go
parent573316e3c2ddd91fd0f36d2251f9660b4f98bebc (diff)
binstruct: Begone with the indirection of binint
Diffstat (limited to 'lib/binstruct/marshal.go')
-rw-r--r--lib/binstruct/marshal.go46
1 files changed, 34 insertions, 12 deletions
diff --git a/lib/binstruct/marshal.go b/lib/binstruct/marshal.go
index 78a4bb5..48e2f1d 100644
--- a/lib/binstruct/marshal.go
+++ b/lib/binstruct/marshal.go
@@ -1,4 +1,4 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
@@ -6,6 +6,7 @@ package binstruct
import (
"encoding"
+ "encoding/binary"
"fmt"
"reflect"
)
@@ -30,17 +31,38 @@ func Marshal(obj any) ([]byte, error) {
func MarshalWithoutInterface(obj any) ([]byte, error) {
val := reflect.ValueOf(obj)
switch val.Kind() {
- case reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64:
- typ := intKind2Type[val.Kind()]
- dat, err := val.Convert(typ).Interface().(Marshaler).MarshalBinary()
- if err != nil {
- err = &UnmarshalError{
- Type: typ,
- Method: "MarshalBinary",
- Err: err,
- }
- }
- return dat, err
+ case reflect.Uint8:
+ var buf [sizeof8]byte
+ buf[0] = byte(val.Uint())
+ return buf[:], nil
+ case reflect.Int8:
+ var buf [sizeof8]byte
+ buf[0] = byte(val.Int())
+ return buf[:], nil
+ case reflect.Uint16:
+ var buf [sizeof16]byte
+ binary.LittleEndian.PutUint16(buf[:], uint16(val.Uint()))
+ return buf[:], nil
+ case reflect.Int16:
+ var buf [sizeof16]byte
+ binary.LittleEndian.PutUint16(buf[:], uint16(val.Int()))
+ return buf[:], nil
+ case reflect.Uint32:
+ var buf [sizeof32]byte
+ binary.LittleEndian.PutUint32(buf[:], uint32(val.Uint()))
+ return buf[:], nil
+ case reflect.Int32:
+ var buf [sizeof32]byte
+ binary.LittleEndian.PutUint32(buf[:], uint32(val.Int()))
+ return buf[:], nil
+ case reflect.Uint64:
+ var buf [sizeof64]byte
+ binary.LittleEndian.PutUint64(buf[:], val.Uint())
+ return buf[:], nil
+ case reflect.Int64:
+ var buf [sizeof64]byte
+ binary.LittleEndian.PutUint64(buf[:], uint64(val.Int()))
+ return buf[:], nil
case reflect.Ptr:
return Marshal(val.Elem().Interface())
case reflect.Array: