github.com/pkg/sftp@v1.13.6/internal/encoding/ssh/filexfer/extensions.go (about)

     1  package sshfx
     2  
     3  // ExtensionPair defines the extension-pair type defined in draft-ietf-secsh-filexfer-13.
     4  // This type is backwards-compatible with how draft-ietf-secsh-filexfer-02 defines extensions.
     5  //
     6  // Defined in: https://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-4.2
     7  type ExtensionPair struct {
     8  	Name string
     9  	Data string
    10  }
    11  
    12  // Len returns the number of bytes e would marshal into.
    13  func (e *ExtensionPair) Len() int {
    14  	return 4 + len(e.Name) + 4 + len(e.Data)
    15  }
    16  
    17  // MarshalInto marshals e onto the end of the given Buffer.
    18  func (e *ExtensionPair) MarshalInto(buf *Buffer) {
    19  	buf.AppendString(e.Name)
    20  	buf.AppendString(e.Data)
    21  }
    22  
    23  // MarshalBinary returns e as the binary encoding of e.
    24  func (e *ExtensionPair) MarshalBinary() ([]byte, error) {
    25  	buf := NewBuffer(make([]byte, 0, e.Len()))
    26  	e.MarshalInto(buf)
    27  	return buf.Bytes(), nil
    28  }
    29  
    30  // UnmarshalFrom unmarshals an ExtensionPair from the given Buffer into e.
    31  func (e *ExtensionPair) UnmarshalFrom(buf *Buffer) (err error) {
    32  	*e = ExtensionPair{
    33  		Name: buf.ConsumeString(),
    34  		Data: buf.ConsumeString(),
    35  	}
    36  
    37  	return buf.Err
    38  }
    39  
    40  // UnmarshalBinary decodes the binary encoding of ExtensionPair into e.
    41  func (e *ExtensionPair) UnmarshalBinary(data []byte) error {
    42  	return e.UnmarshalFrom(NewBuffer(data))
    43  }