package quic

import (
	
	
	
)

const (
	extTypeSNI = 0
	extTypeECH = 0xfe0d
)

// findSNIAndECH parses the given byte slice as a ClientHello, and locates:
// - the position and length of the Server Name Indication (SNI) extension,
// - the position of the Encrypted Client Hello (ECH) extension.
// If no SNI extension is found, it returns -1 for the SNI position.
// If no ECH extension is found, it returns -1 for the ECH position.
func findSNIAndECH( []byte) (, ,  int,  error) {
	if len() < 4 {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	if [0] != 1 {
		return 0, 0, 0, errors.New("not a ClientHello")
	}
	 := int([1])<<16 | int([2])<<8 | int([3])
	if len() != 4+ {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}

	 := 4
	// Skip protocol version (2 bytes)
	if +2 > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 += 2
	// skip random (32 bytes)
	if +32 > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 += 32
	// session ID
	if +1 > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 := int([])
	++
	if + > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 += 
	// cipher suites
	if +2 > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 := int(binary.BigEndian.Uint16([:]))
	 += 2
	if + > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 += 
	// compression methods
	if +1 > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 := int([])
	++
	if + > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 += 

	// extensions
	if +2 > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 := int(binary.BigEndian.Uint16([:]))
	 += 2
	if + > len() {
		return 0, 0, 0, io.ErrUnexpectedEOF
	}
	 := 
	 := [ : +]

	// parse extensions
	var  int
	 = -1
	 = -1
	for +4 <=  {
		 := binary.BigEndian.Uint16([:])
		 := int(binary.BigEndian.Uint16([+2:]))
		if +4+ >  {
			return 0, 0, 0, io.ErrUnexpectedEOF
		}
		switch  {
		case extTypeSNI:
			if  != -1 {
				return 0, 0, 0, errors.New("multiple SNI extensions")
			}
			 := [+4 : +4+]
			if len() < 2 {
				return 0, 0, 0, io.ErrUnexpectedEOF
			}
			 := int(binary.BigEndian.Uint16())
			if len() != 2+ {
				return 0, 0, 0, io.ErrUnexpectedEOF
			}
			 := 2
			for +3 <= +2 {
				 := []
				 = int(binary.BigEndian.Uint16([+1:]))
				if +3+ > len() {
					return 0, 0, 0, io.ErrUnexpectedEOF
				}
				if  == 0 { // host_name
					 =  +  + 4 +  + 3
					break // stop after first host_name
				}
				 += 3 + 
			}
			if  == 0 {
				return 0, 0, 0, errors.New("SNI host_name not found")
			}
		case extTypeECH:
			if  != -1 {
				return 0, 0, 0, errors.New("multiple ECH extensions")
			}
			 =  + 
		}
		 += 4 + 
		if  != -1 &&  != -1 {
			break
		}
	}
	return , , , nil
}