rpc_unicode_string.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package mstypes
  2. import (
  3. "encoding/binary"
  4. "gopkg.in/jcmturner/gokrb5.v4/ndr"
  5. )
  6. // RPCUnicodeString implements https://msdn.microsoft.com/en-us/library/cc230365.aspx
  7. type RPCUnicodeString struct {
  8. Length uint16 // The length, in bytes, of the string pointed to by the Buffer member, not including the terminating null character if any. The length MUST be a multiple of 2. The length SHOULD equal the entire size of the Buffer, in which case there is no terminating null character. Any method that accesses this structure MUST use the Length specified instead of relying on the presence or absence of a null character.
  9. MaximumLength uint16 // The maximum size, in bytes, of the string pointed to by Buffer. The size MUST be a multiple of 2. If not, the size MUST be decremented by 1 prior to use. This value MUST not be less than Length.
  10. BufferPrt uint32 // A pointer to a string buffer. If MaximumLength is greater than zero, the buffer MUST contain a non-null value.
  11. Value string
  12. }
  13. // ReadRPCUnicodeString reads a RPCUnicodeString from the bytes slice.
  14. func ReadRPCUnicodeString(b *[]byte, p *int, e *binary.ByteOrder) (RPCUnicodeString, error) {
  15. l := ndr.ReadUint16(b, p, e)
  16. ml := ndr.ReadUint16(b, p, e)
  17. if ml < l || l%2 != 0 || ml%2 != 0 {
  18. return RPCUnicodeString{}, ndr.Malformed{EText: "Invalid data for RPC_UNICODE_STRING"}
  19. }
  20. ptr := ndr.ReadUint32(b, p, e)
  21. return RPCUnicodeString{
  22. Length: l,
  23. MaximumLength: ml,
  24. BufferPrt: ptr,
  25. }, nil
  26. }
  27. // UnmarshalString populates a golang string into the RPCUnicodeString struct.
  28. func (s *RPCUnicodeString) UnmarshalString(b *[]byte, p *int, e *binary.ByteOrder) (err error) {
  29. s.Value, err = ndr.ReadConformantVaryingString(b, p, e)
  30. return
  31. }