summaryrefslogtreecommitdiff
path: root/pkg/binstruct/size.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-05-30 12:00:54 -0400
committerLuke Shumaker <lukeshu@lukeshu.com>2022-05-30 12:00:54 -0400
commit703e98f2759148aa2d6ac80f2519ae8e41da3e95 (patch)
tree746c17fb2e155ed8448b3ac44aced0672fc83a95 /pkg/binstruct/size.go
parent8576e5f207f9d3b7c6324ed71a3ca6a005f9ae7c (diff)
ahhhhhhh
Diffstat (limited to 'pkg/binstruct/size.go')
-rw-r--r--pkg/binstruct/size.go29
1 files changed, 18 insertions, 11 deletions
diff --git a/pkg/binstruct/size.go b/pkg/binstruct/size.go
index 519c2fe..f6b3241 100644
--- a/pkg/binstruct/size.go
+++ b/pkg/binstruct/size.go
@@ -10,27 +10,34 @@ type StaticSizer interface {
}
func StaticSize(obj any) int {
- return staticSize(reflect.TypeOf(obj))
+ sz, err := staticSize(reflect.TypeOf(obj))
+ if err != nil {
+ panic(err)
+ }
+ return sz
}
var staticSizerType = reflect.TypeOf((*StaticSizer)(nil)).Elem()
-func staticSize(typ reflect.Type) int {
+func staticSize(typ reflect.Type) (int, error) {
if typ.Implements(staticSizerType) {
- return reflect.New(typ).Elem().Interface().(StaticSizer).BinaryStaticSize()
- }
- if szer, ok := obj.(StaticSizer); ok {
- return szer.BinaryStaticSize()
+ return reflect.New(typ).Elem().Interface().(StaticSizer).BinaryStaticSize(), nil
}
switch typ.Kind() {
+ case reflect.Uint8, reflect.Int8:
+ return 1, nil
case reflect.Ptr:
- return StaticSize(typ.Elem())
+ return staticSize(typ.Elem())
case reflect.Array:
- return StaticSize(typ.Elem()) * typ.Len()
+ elemSize, err := staticSize(typ.Elem())
+ if err != nil {
+ return 0, err
+ }
+ return elemSize * typ.Len(), nil
case reflect.Struct:
- // TODO
+ return getStructHandler(typ).Size, nil
default:
- panic(fmt.Errorf("type=%v does not implement binfmt.StaticSizer and kind=%v is not a supported statically-sized kind",
- typ, typ.Kind()))
+ return 0, fmt.Errorf("type=%v does not implement binfmt.StaticSizer and kind=%v is not a supported statically-sized kind",
+ typ, typ.Kind())
}
}