-
Notifications
You must be signed in to change notification settings - Fork 2
/
rawbytes.go
61 lines (54 loc) · 1.41 KB
/
rawbytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package ndr
import (
"errors"
"fmt"
"reflect"
"strconv"
)
// type MyBytes []byte
// implement RawBytes interface
const (
sizeMethod = "Size"
)
// RawBytes interface should be implemented if reading just a number of bytes from the NDR stream
type RawBytes interface {
Size(interface{}) int
}
func rawBytesSize(parent reflect.Value, v reflect.Value) (int, error) {
sf := v.MethodByName(sizeMethod)
if !sf.IsValid() {
return 0, fmt.Errorf("could not find a method called %s on the implementation of RawBytes", sizeMethod)
}
in := []reflect.Value{parent}
f := sf.Call(in)
if f[0].Kind() != reflect.Int {
return 0, errors.New("the RawBytes size function did not return an integer")
}
return int(f[0].Int()), nil
}
func addSizeToTag(parent reflect.Value, v reflect.Value, tag reflect.StructTag) (reflect.StructTag, error) {
size, err := rawBytesSize(parent, v)
if err != nil {
return tag, err
}
ndrTag := parseTags(tag)
ndrTag.Map["size"] = strconv.Itoa(size)
return ndrTag.StructTag(), nil
}
func (dec *Decoder) readRawBytes(v reflect.Value, tag reflect.StructTag) error {
ndrTag := parseTags(tag)
sizeStr, ok := ndrTag.Map["size"]
if !ok {
return errors.New("size tag not available")
}
size, err := strconv.Atoi(sizeStr)
if err != nil {
return fmt.Errorf("size not valid: %v", err)
}
b, err := dec.readBytes(size)
if err != nil {
return err
}
v.Set(reflect.ValueOf(b).Convert(v.Type()))
return nil
}