summaryrefslogtreecommitdiff
path: root/lib/binstruct/marshal.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:17:02 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-12 16:17:02 -0700
commitcfcc753dc8906817e15b1b7c36b4dc12462d12e4 (patch)
treef5d2aa0caaa4cb336017ba7595c3425f4aa00bfc /lib/binstruct/marshal.go
parent29b6b9f997913f13a0bff8bb1278a61302413615 (diff)
parentf76faa4b8debd9c94751a03dd65e46c80a340a82 (diff)
Merge branch 'lukeshu/fast'
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: