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

package handshake

import (
	

	
)

// HeaderLength msg_len for Handshake messages assumes an extra
// 12 bytes for sequence, fragment and version information vs TLS
const HeaderLength = 12

// Header is the static first 12 bytes of each RecordLayer
// of type Handshake. These fields allow us to support message loss, reordering, and
// message fragmentation,
//
// https://tools.ietf.org/html/rfc6347#section-4.2.2
type Header struct {
	Type            Type
	Length          uint32 // uint24 in spec
	MessageSequence uint16
	FragmentOffset  uint32 // uint24 in spec
	FragmentLength  uint32 // uint24 in spec
}

// Marshal encodes the Header
func ( *Header) () ([]byte, error) {
	 := make([]byte, HeaderLength)

	[0] = byte(.Type)
	util.PutBigEndianUint24([1:], .Length)
	binary.BigEndian.PutUint16([4:], .MessageSequence)
	util.PutBigEndianUint24([6:], .FragmentOffset)
	util.PutBigEndianUint24([9:], .FragmentLength)
	return , nil
}

// Unmarshal populates the header from encoded data
func ( *Header) ( []byte) error {
	if len() < HeaderLength {
		return errBufferTooSmall
	}

	.Type = Type([0])
	.Length = util.BigEndianUint24([1:])
	.MessageSequence = binary.BigEndian.Uint16([4:])
	.FragmentOffset = util.BigEndianUint24([6:])
	.FragmentLength = util.BigEndianUint24([9:])
	return nil
}