summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pkg/binstruct/binint.go14
-rw-r--r--pkg/binstruct/binstruct_test.go32
-rw-r--r--pkg/binstruct/marshal.go7
-rw-r--r--pkg/binstruct/size.go6
-rw-r--r--pkg/binstruct/unmarshal.go10
5 files changed, 36 insertions, 33 deletions
diff --git a/pkg/binstruct/binint.go b/pkg/binstruct/binint.go
index fb6374c..105dcfa 100644
--- a/pkg/binstruct/binint.go
+++ b/pkg/binstruct/binint.go
@@ -23,7 +23,13 @@ type (
I64be = binint.I64be
)
-var (
- u8Type = reflect.TypeOf(U8(0))
- i8Type = reflect.TypeOf(I8(0))
-)
+var intKind2Type = map[reflect.Kind]reflect.Type{
+ reflect.Uint8: reflect.TypeOf(U8(0)),
+ reflect.Int8: reflect.TypeOf(I8(0)),
+ reflect.Uint16: reflect.TypeOf(U16le(0)),
+ reflect.Int16: reflect.TypeOf(I16le(0)),
+ reflect.Uint32: reflect.TypeOf(U32le(0)),
+ reflect.Int32: reflect.TypeOf(I32le(0)),
+ reflect.Uint64: reflect.TypeOf(U64le(0)),
+ reflect.Int64: reflect.TypeOf(I64le(0)),
+}
diff --git a/pkg/binstruct/binstruct_test.go b/pkg/binstruct/binstruct_test.go
index 1070d06..00c9e37 100644
--- a/pkg/binstruct/binstruct_test.go
+++ b/pkg/binstruct/binstruct_test.go
@@ -6,31 +6,27 @@ import (
"github.com/stretchr/testify/assert"
"lukeshu.com/btrfs-tools/pkg/binstruct"
- . "lukeshu.com/btrfs-tools/pkg/binstruct/binint"
)
func TestSmoke(t *testing.T) {
- type UUID [16]U8
- type PhysicalAddr I64le
- func (PhysicalAddr) BinaryStaticSize() int { return I64le(0).BinaryStaticSize() }
- func (x PhysicalAddr) MarshalBinary() ([]byte, error) { return I64le(x).MarshalBinary() }
- func (x *PhysicalAddr) UnmarshalBinary([]byte) (int, error) { return I64le(x).UnmarshalBinary(dat) }
+ type UUID [16]byte
+ type PhysicalAddr int64
type DevItem struct {
- DeviceID U64le `bin:"off=0x0, siz=0x8"` // device id
+ DeviceID uint64 `bin:"off=0x0, siz=0x8"` // device id
- NumBytes U64le `bin:"off=0x8, siz=0x8"` // number of bytes
- NumBytesUsed U64le `bin:"off=0x10, siz=0x8"` // number of bytes used
+ NumBytes uint64 `bin:"off=0x8, siz=0x8"` // number of bytes
+ NumBytesUsed uint64 `bin:"off=0x10, siz=0x8"` // number of bytes used
- IOOptimalAlign U32le `bin:"off=0x18, siz=0x4"` // optimal I/O align
- IOOptimalWidth U32le `bin:"off=0x1c, siz=0x4"` // optimal I/O width
- IOMinSize U32le `bin:"off=0x20, siz=0x4"` // minimal I/O size (sector size)
+ IOOptimalAlign uint32 `bin:"off=0x18, siz=0x4"` // optimal I/O align
+ IOOptimalWidth uint32 `bin:"off=0x1c, siz=0x4"` // optimal I/O width
+ IOMinSize uint32 `bin:"off=0x20, siz=0x4"` // minimal I/O size (sector size)
- Type U64le `bin:"off=0x24, siz=0x8"` // type
- Generation U64le `bin:"off=0x2c, siz=0x8"` // generation
- StartOffset U64le `bin:"off=0x34, siz=0x8"` // start offset
- DevGroup U32le `bin:"off=0x3c, siz=0x4"` // dev group
- SeekSpeed U8 `bin:"off=0x40, siz=0x1"` // seek speed
- Bandwidth U8 `bin:"off=0x41, siz=0x1"` // bandwidth
+ Type uint64 `bin:"off=0x24, siz=0x8"` // type
+ Generation uint64 `bin:"off=0x2c, siz=0x8"` // generation
+ StartOffset uint64 `bin:"off=0x34, siz=0x8"` // start offset
+ DevGroup uint32 `bin:"off=0x3c, siz=0x4"` // dev group
+ SeekSpeed uint8 `bin:"off=0x40, siz=0x1"` // seek speed
+ Bandwidth uint8 `bin:"off=0x41, siz=0x1"` // bandwidth
DevUUID UUID `bin:"off=0x42, siz=0x10"` // device UUID
FSUUID UUID `bin:"off=0x52, siz=0x10"` // FS UUID
diff --git a/pkg/binstruct/marshal.go b/pkg/binstruct/marshal.go
index 90f30b6..684d2f3 100644
--- a/pkg/binstruct/marshal.go
+++ b/pkg/binstruct/marshal.go
@@ -18,10 +18,9 @@ func Marshal(obj any) ([]byte, error) {
func MarshalWithoutInterface(obj any) ([]byte, error) {
val := reflect.ValueOf(obj)
switch val.Kind() {
- case reflect.Uint8:
- return val.Convert(u8Type).Interface().(Marshaler).MarshalBinary()
- case reflect.Int8:
- return val.Convert(i8Type).Interface().(Marshaler).MarshalBinary()
+ case reflect.Uint8, reflect.Int8, reflect.Uint16, reflect.Int16, reflect.Uint32, reflect.Int32, reflect.Uint64, reflect.Int64:
+ typ := intKind2Type[val.Kind()]
+ return val.Convert(typ).Interface().(Marshaler).MarshalBinary()
case reflect.Ptr:
return Marshal(val.Elem().Interface())
case reflect.Array:
diff --git a/pkg/binstruct/size.go b/pkg/binstruct/size.go
index f6b3241..8846c23 100644
--- a/pkg/binstruct/size.go
+++ b/pkg/binstruct/size.go
@@ -26,6 +26,12 @@ func staticSize(typ reflect.Type) (int, error) {
switch typ.Kind() {
case reflect.Uint8, reflect.Int8:
return 1, nil
+ case reflect.Uint16, reflect.Int16:
+ return 2, nil
+ case reflect.Uint32, reflect.Int32:
+ return 4, nil
+ case reflect.Uint64, reflect.Int64:
+ return 8, nil
case reflect.Ptr:
return staticSize(typ.Elem())
case reflect.Array:
diff --git a/pkg/binstruct/unmarshal.go b/pkg/binstruct/unmarshal.go
index ca2f4b2..1959d45 100644
--- a/pkg/binstruct/unmarshal.go
+++ b/pkg/binstruct/unmarshal.go
@@ -24,13 +24,9 @@ func UnmarshalWithoutInterface(dat []byte, dstPtr any) (int, error) {
dst := _dstPtr.Elem()
switch dst.Kind() {
- case reflect.Uint8:
- newDstPtr := reflect.New(u8Type)
- n, err := Unmarshal(dat, newDstPtr.Interface())
- dst.Set(newDstPtr.Elem().Convert(dst.Type()))
- return n, err
- case reflect.Int8:
- newDstPtr := reflect.New(i8Type)
+ 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