// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package extension

import (
	

	
	
	
)

const (
	supportedSignatureAlgorithmsHeaderSize = 6
)

// SupportedSignatureAlgorithms allows a Client/Server to
// negotiate what SignatureHash Algorithms they both support
//
// https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
type SupportedSignatureAlgorithms struct {
	SignatureHashAlgorithms []signaturehash.Algorithm
}

// TypeValue returns the extension TypeValue.
func ( SupportedSignatureAlgorithms) () TypeValue {
	return SupportedSignatureAlgorithmsTypeValue
}

// Marshal encodes the extension.
func ( *SupportedSignatureAlgorithms) () ([]byte, error) {
	 := make([]byte, supportedSignatureAlgorithmsHeaderSize)

	binary.BigEndian.PutUint16(, uint16(.TypeValue()))
	binary.BigEndian.PutUint16([2:], uint16(2+(len(.SignatureHashAlgorithms)*2))) //nolint:gosec // G115
	binary.BigEndian.PutUint16([4:], uint16(len(.SignatureHashAlgorithms)*2))     //nolint:gosec // G115
	for ,  := range .SignatureHashAlgorithms {
		 = append(, []byte{0x00, 0x00}...) //nolint:makezero // todo: fix
		[len()-2] = byte(.Hash)
		[len()-1] = byte(.Signature)
	}

	return , nil
}

// Unmarshal populates the extension from encoded data.
func ( *SupportedSignatureAlgorithms) ( []byte) error {
	if len() <= supportedSignatureAlgorithmsHeaderSize {
		return errBufferTooSmall
	} else if TypeValue(binary.BigEndian.Uint16()) != .TypeValue() {
		return errInvalidExtensionType
	}

	 := int(binary.BigEndian.Uint16([4:]) / 2)
	if supportedSignatureAlgorithmsHeaderSize+(*2) > len() {
		return errLengthMismatch
	}
	for  := 0;  < ; ++ {
		 := hash.Algorithm([supportedSignatureAlgorithmsHeaderSize+(*2)])
		 := signature.Algorithm([supportedSignatureAlgorithmsHeaderSize+(*2)+1])
		if ,  := hash.Algorithms()[];  {
			if ,  := signature.Algorithms()[];  {
				.SignatureHashAlgorithms = append(.SignatureHashAlgorithms, signaturehash.Algorithm{
					Hash:      ,
					Signature: ,
				})
			}
		}
	}

	return nil
}