Scott Baker | 611f6bd | 2019-10-18 13:45:19 -0700 | [diff] [blame] | 1 | package ndr |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "reflect" |
| 6 | ) |
| 7 | |
| 8 | const ( |
| 9 | subStringArrayTag = `ndr:"varying,X-subStringArray"` |
| 10 | subStringArrayValue = "X-subStringArray" |
| 11 | ) |
| 12 | |
| 13 | func uint16SliceToString(a []uint16) string { |
| 14 | s := make([]rune, len(a), len(a)) |
| 15 | for i := range s { |
| 16 | s[i] = rune(a[i]) |
| 17 | } |
| 18 | if len(s) > 0 { |
| 19 | // Remove any null terminator |
| 20 | if s[len(s)-1] == rune(0) { |
| 21 | s = s[:len(s)-1] |
| 22 | } |
| 23 | } |
| 24 | return string(s) |
| 25 | } |
| 26 | |
| 27 | func (dec *Decoder) readVaryingString(def *[]deferedPtr) (string, error) { |
| 28 | a := new([]uint16) |
| 29 | v := reflect.ValueOf(a) |
| 30 | var t reflect.StructTag |
| 31 | err := dec.fillUniDimensionalVaryingArray(v.Elem(), t, def) |
| 32 | if err != nil { |
| 33 | return "", err |
| 34 | } |
| 35 | s := uint16SliceToString(*a) |
| 36 | return s, nil |
| 37 | } |
| 38 | |
| 39 | func (dec *Decoder) readConformantVaryingString(def *[]deferedPtr) (string, error) { |
| 40 | a := new([]uint16) |
| 41 | v := reflect.ValueOf(a) |
| 42 | var t reflect.StructTag |
| 43 | err := dec.fillUniDimensionalConformantVaryingArray(v.Elem(), t, def) |
| 44 | if err != nil { |
| 45 | return "", err |
| 46 | } |
| 47 | s := uint16SliceToString(*a) |
| 48 | return s, nil |
| 49 | } |
| 50 | |
| 51 | func (dec *Decoder) readStringsArray(v reflect.Value, tag reflect.StructTag, def *[]deferedPtr) error { |
| 52 | d, _ := sliceDimensions(v.Type()) |
| 53 | ndrTag := parseTags(tag) |
| 54 | var m []int |
| 55 | //var ms int |
| 56 | if ndrTag.HasValue(TagConformant) { |
| 57 | for i := 0; i < d; i++ { |
| 58 | m = append(m, int(dec.precedingMax())) |
| 59 | } |
| 60 | //common max size |
| 61 | _ = dec.precedingMax() |
| 62 | //ms = int(n) |
| 63 | } |
| 64 | tag = reflect.StructTag(subStringArrayTag) |
| 65 | err := dec.fillVaryingArray(v, tag, def) |
| 66 | if err != nil { |
| 67 | return fmt.Errorf("could not read string array: %v", err) |
| 68 | } |
| 69 | return nil |
| 70 | } |