# Convert any known MAC format to any other known format. sub ConvertMac($$$) { use strict; use warnings; my $mac = shift; my $form = shift; my $debug = shift; if ( $debug > 4 ) { print "\t\t\t\tConvertMac: Converting $mac to $form.\n"; } # First, convert the MAC to the common XX:XX:XX:XX:XX:XX format. START: { # Suppose it's just flat hex? if ( $mac =~ /^$nosepmacpattern$/ ) { # Just convert it and we're done. $mac = join ':', map sprintf("%02X", ord($_)), split //, pack('H12', $mac); # We can escape, since $nosepmacpattern enforces the length, and the above formats it. last START; } # Convert dotted-decimal to hex first (ignore the dots). if ( $mac =~ /^$decmacpattern$/ ) { $mac =~ s/(\d+)/sprintf("%02X", $1)/eg; } # Now convert hex separated by anything to our standard format. $mac = join ':', map sprintf("%02X", hex($_)), split /[^[:xdigit:]]/, $mac; # Test for valid data (suppose >2 hex digits between separators or >6 items). if ( $mac !~ /^$colonmacpattern$/ ) { return('Error'); } # Don't return here, even if ( $form eq 'colon'). See below. } # Now, convert to the form requested. FINISH: { # This is the most common, so we'll put it first. if ( $form eq 'colon' ) { # Don't return here either. Look below. last FINISH; } # This is really easy. if ( $form eq 'nosep' ) { $mac =~ s/://g; $mac = lc($mac); last FINISH; } if ( $form eq 'blank' ) { $mac =~ s/:/ /g; last FINISH; } if ( $form eq 'dash' ) { $mac =~ s/:/-/g; last FINISH; } if ( $form eq 'dec' ) { $mac = join '.', map hex($_), split(/:/, $mac); last FINISH; } if ( $form eq 'invert' ) { $mac = join ':', map uc(unpack('H*', pack('b*', unpack('B*', pack('H*', $_))))), split(/:/, $mac); last FINISH; } if ( $form eq 'varcolon' ) { $mac =~ s/0([[:xdigit:]])/$1/g; $mac = lc($mac); last FINISH; } } if ( $debug > 4 ) { print "\t\t\t\t\tConvertMac: Returned value is $mac.\n"; } # Always return here so the debug stuff always runs and so if we even want to do # anything else down here, we won't circumvent it by escaping early. return($mac); }