package httpsfv

import (
	
	
	
)

// ErrInvalidBinaryFormat is returned when the binary format is invalid.
var ErrInvalidBinaryFormat = errors.New("invalid binary format")

// marshalBinary serializes as defined in
// https://httpwg.org/specs/rfc9651.html#ser-binary.
func marshalBinary( *strings.Builder,  []byte) error {
	if  := .WriteByte(':');  != nil {
		return 
	}

	 := make([]byte, base64.StdEncoding.EncodedLen(len()))
	base64.StdEncoding.Encode(, )

	if ,  := .Write();  != nil {
		return 
	}

	return .WriteByte(':')
}

// parseBinary parses as defined in
// https://httpwg.org/specs/rfc9651.html#parse-binary.
func parseBinary( *scanner) ([]byte, error) {
	if .eof() || .data[.off] != ':' {
		return nil, &UnmarshalError{.off, ErrInvalidBinaryFormat}
	}
	.off++

	 := .off

	for !.eof() {
		 := .data[.off]
		if  == ':' {
			// base64decode
			,  := base64.StdEncoding.DecodeString(.data[:.off])
			if  != nil {
				return nil, &UnmarshalError{.off, }
			}
			.off++

			return , nil
		}

		if !isAlpha() && !isDigit() &&  != '+' &&  != '/' &&  != '=' {
			return nil, &UnmarshalError{.off, ErrInvalidBinaryFormat}
		}
		.off++
	}

	return nil, &UnmarshalError{.off, ErrInvalidBinaryFormat}
}