// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package net

const hexDigit = "0123456789abcdef"

// A HardwareAddr represents a physical hardware address.
type HardwareAddr []byte

func ( HardwareAddr) () string {
	if len() == 0 {
		return ""
	}
	 := make([]byte, 0, len()*3-1)
	for ,  := range  {
		if  > 0 {
			 = append(, ':')
		}
		 = append(, hexDigit[>>4])
		 = append(, hexDigit[&0xF])
	}
	return string()
}

// ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet
// IP over InfiniBand link-layer address using one of the following formats:
//
//	00:00:5e:00:53:01
//	02:00:5e:10:00:00:00:01
//	00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01
//	00-00-5e-00-53-01
//	02-00-5e-10-00-00-00-01
//	00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01
//	0000.5e00.5301
//	0200.5e10.0000.0001
//	0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001
func ( string) ( HardwareAddr,  error) {
	if len() < 14 {
		goto 
	}

	if [2] == ':' || [2] == '-' {
		if (len()+1)%3 != 0 {
			goto 
		}
		 := (len() + 1) / 3
		if  != 6 &&  != 8 &&  != 20 {
			goto 
		}
		 = make(HardwareAddr, )
		for ,  := 0, 0;  < ; ++ {
			var  bool
			if [],  = xtoi2([:], [2]); ! {
				goto 
			}
			 += 3
		}
	} else if [4] == '.' {
		if (len()+1)%5 != 0 {
			goto 
		}
		 := 2 * (len() + 1) / 5
		if  != 6 &&  != 8 &&  != 20 {
			goto 
		}
		 = make(HardwareAddr, )
		for ,  := 0, 0;  < ;  += 2 {
			var  bool
			if [],  = xtoi2([:+2], 0); ! {
				goto 
			}
			if [+1],  = xtoi2([+2:], [4]); ! {
				goto 
			}
			 += 5
		}
	} else {
		goto 
	}
	return , nil

:
	return nil, &AddrError{Err: "invalid MAC address", Addr: }
}