aim-oscar-server/oscar/snac.go

66 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-12-16 19:45:32 -05:00
package oscar
import (
"bytes"
"encoding"
"encoding/binary"
"fmt"
)
var _ encoding.BinaryUnmarshaler = &SNAC{}
var _ encoding.BinaryMarshaler = &SNAC{}
type SNACHeader struct {
Family uint16
Subtype uint16
Flags uint16
RequestID uint32
}
type SNAC struct {
Header SNACHeader
2021-12-16 17:41:17 -05:00
Data Buffer
}
2021-12-16 17:41:17 -05:00
func NewSNAC(family uint16, subtype uint16) *SNAC {
return &SNAC{
Header: SNACHeader{
Family: family,
Subtype: subtype,
Flags: 0,
RequestID: 0,
},
}
}
func (s *SNAC) MarshalBinary() ([]byte, error) {
2021-12-16 17:41:17 -05:00
buf := Buffer{}
binary.Write(&buf, binary.BigEndian, s.Header)
2021-12-16 17:41:17 -05:00
b := s.Data.Bytes()
n, err := buf.Write(b)
if n != len(b) {
return nil, fmt.Errorf("needed to write %d bytes to buffer but wrote %d", len(b), n)
}
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s *SNAC) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
if err := binary.Read(buf, binary.BigEndian, &s.Header); err != nil {
return err
}
2021-12-16 17:41:17 -05:00
s.Data.Write(buf.Bytes())
return nil
}
func (s *SNAC) String() string {
return fmt.Sprintf("SNAC(%#x, %#x)", s.Header.Family, s.Header.Subtype)
}