package sshfx

// InitPacket defines the SSH_FXP_INIT packet.
type InitPacket struct {
	Version    uint32
	Extensions []*ExtensionPair
}

// MarshalBinary returns p as the binary encoding of p.
func ( *InitPacket) () ([]byte, error) {
	 := 1 + 4 // byte(type) + uint32(version)

	for ,  := range .Extensions {
		 += .Len()
	}

	 := NewBuffer(make([]byte, 4, 4+))
	.AppendUint8(uint8(PacketTypeInit))
	.AppendUint32(.Version)

	for ,  := range .Extensions {
		.MarshalInto()
	}

	.PutLength()

	return .Bytes(), nil
}

// UnmarshalBinary unmarshals a full raw packet out of the given data.
// It is assumed that the uint32(length) has already been consumed to receive the data.
// It is also assumed that the uint8(type) has already been consumed to which packet to unmarshal into.
func ( *InitPacket) ( []byte) ( error) {
	 := NewBuffer()

	* = InitPacket{
		Version: .ConsumeUint32(),
	}

	for .Len() > 0 {
		var  ExtensionPair
		if  := .UnmarshalFrom();  != nil {
			return 
		}

		.Extensions = append(.Extensions, &)
	}

	return .Err
}

// VersionPacket defines the SSH_FXP_VERSION packet.
type VersionPacket struct {
	Version    uint32
	Extensions []*ExtensionPair
}

// MarshalBinary returns p as the binary encoding of p.
func ( *VersionPacket) () ([]byte, error) {
	 := 1 + 4 // byte(type) + uint32(version)

	for ,  := range .Extensions {
		 += .Len()
	}

	 := NewBuffer(make([]byte, 4, 4+))
	.AppendUint8(uint8(PacketTypeVersion))
	.AppendUint32(.Version)

	for ,  := range .Extensions {
		.MarshalInto()
	}

	.PutLength()

	return .Bytes(), nil
}

// UnmarshalBinary unmarshals a full raw packet out of the given data.
// It is assumed that the uint32(length) has already been consumed to receive the data.
// It is also assumed that the uint8(type) has already been consumed to which packet to unmarshal into.
func ( *VersionPacket) ( []byte) ( error) {
	 := NewBuffer()

	* = VersionPacket{
		Version: .ConsumeUint32(),
	}

	for .Len() > 0 {
		var  ExtensionPair
		if  := .UnmarshalFrom();  != nil {
			return 
		}

		.Extensions = append(.Extensions, &)
	}

	return nil
}