89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package ipp
|
|
|
|
import "encoding/binary"
|
|
|
|
type setOfStrings struct {
|
|
name string
|
|
values []string
|
|
vTag tag
|
|
}
|
|
|
|
func NewSetOfStrings(name string, t tag, values []string) *setOfStrings {
|
|
s := new(setOfStrings)
|
|
s.name = name
|
|
s.vTag = t
|
|
s.values = values //make([]string, 0)
|
|
return s
|
|
}
|
|
|
|
func (s setOfStrings) String() string {
|
|
r := s.name + " :"
|
|
for _, v := range s.values {
|
|
r = r + " " + v
|
|
}
|
|
return r
|
|
}
|
|
func (s *setOfStrings) valueTag() tag {
|
|
return s.vTag
|
|
}
|
|
|
|
// func (k *keyWord) unmarshal(byteStream io.Reader) {
|
|
// if len(k.values) == 0 {
|
|
// var v string
|
|
// k.name, v = unmarshalSingleValue(byteStream)
|
|
// k.values = append(k.values, v)
|
|
// } else {
|
|
// var v string
|
|
// _, v = unmarshalSingleValue(byteStream)
|
|
// k.values = append(k.values, v)
|
|
// }
|
|
// }
|
|
|
|
func (s *setOfStrings) marshal() []byte {
|
|
l := 5 + len(s.name) + len(s.values[0])
|
|
for i := range s.values[1:] {
|
|
l += 5 + len(s.values[i+1])
|
|
}
|
|
res := make([]byte, l)
|
|
p := 0
|
|
res[p] = byte(s.vTag)
|
|
p += 1
|
|
binary.BigEndian.PutUint16(res[p:p+2], uint16(len(s.name)))
|
|
p += 2
|
|
copy(res[p:], []byte(s.name))
|
|
p += len(s.name)
|
|
binary.BigEndian.PutUint16(res[p:p+2], uint16(len(s.values[0])))
|
|
p += 2
|
|
copy(res[p:], []byte(s.values[0]))
|
|
p += len(s.values[0])
|
|
for i := range s.values[1:] {
|
|
res[p] = byte(s.vTag)
|
|
p += 1
|
|
binary.BigEndian.PutUint16(res[p:p+2], uint16(0))
|
|
p = p + 2
|
|
binary.BigEndian.PutUint16(res[p:p+2], uint16(len(s.values[i+1])))
|
|
p = p + 2
|
|
copy(res[p:], []byte(s.values[i+1]))
|
|
p += len(s.values[i+1])
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func (s *setOfStrings) AddValue(v string) {
|
|
s.values = append(s.values, v)
|
|
}
|
|
|
|
func (s *setOfStrings) size() int {
|
|
l := 1 + 2 // The value tag (0x44) + name-length field (2 bytes)
|
|
l += len(s.name)
|
|
l += 2 // value-length field (2 bytes)
|
|
l += len(s.values[0])
|
|
// Add all additional values
|
|
for _, v := range s.values[1:] {
|
|
l += 1 + 4 // The value tag (0x44) + 2 length fields (2 bytes)
|
|
l += len(v)
|
|
}
|
|
return l
|
|
}
|