diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-01-09 00:21:21 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-12 16:16:53 -0700 |
commit | 5bdff4a4fb6c497f3a28c2fc5ba5280233df979e (patch) | |
tree | 1785d98eada9e8bf7a818988ad994c8c998f5480 /lib/binstruct/unmarshal.go | |
parent | 573316e3c2ddd91fd0f36d2251f9660b4f98bebc (diff) |
binstruct: Begone with the indirection of binint
Diffstat (limited to 'lib/binstruct/unmarshal.go')
-rw-r--r-- | lib/binstruct/unmarshal.go | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/lib/binstruct/unmarshal.go b/lib/binstruct/unmarshal.go index 61c2a4a..4cb8a59 100644 --- a/lib/binstruct/unmarshal.go +++ b/lib/binstruct/unmarshal.go @@ -1,13 +1,16 @@ -// 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 package binstruct import ( + "encoding/binary" "errors" "fmt" "reflect" + + "git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil" ) type Unmarshaler interface { @@ -40,12 +43,34 @@ func UnmarshalWithoutInterface(dat []byte, dstPtr any) (int, error) { dst := _dstPtr.Elem() switch dst.Kind() { - case reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64: - typ := intKind2Type[dst.Kind()] - newDstPtr := reflect.New(typ) - n, err := Unmarshal(dat, newDstPtr.Interface()) - dst.Set(newDstPtr.Elem().Convert(dst.Type())) - return n, err + case reflect.Uint8, reflect.Int8: + if err := binutil.NeedNBytes(dat, sizeof8); err != nil { + return 0, err + } + val := reflect.ValueOf(dat[0]) + dst.Set(val.Convert(dst.Type())) + return sizeof8, nil + case reflect.Uint16, reflect.Int16: + if err := binutil.NeedNBytes(dat, sizeof16); err != nil { + return 0, err + } + val := reflect.ValueOf(binary.LittleEndian.Uint16(dat[:sizeof16])) + dst.Set(val.Convert(dst.Type())) + return sizeof16, nil + case reflect.Uint32, reflect.Int32: + if err := binutil.NeedNBytes(dat, sizeof32); err != nil { + return 0, err + } + val := reflect.ValueOf(binary.LittleEndian.Uint32(dat[:sizeof32])) + dst.Set(val.Convert(dst.Type())) + return sizeof32, nil + case reflect.Uint64, reflect.Int64: + if err := binutil.NeedNBytes(dat, sizeof64); err != nil { + return 0, err + } + val := reflect.ValueOf(binary.LittleEndian.Uint64(dat[:sizeof64])) + dst.Set(val.Convert(dst.Type())) + return sizeof64, nil case reflect.Ptr: elemPtr := reflect.New(dst.Type().Elem()) n, err := Unmarshal(dat, elemPtr.Interface()) |