| 1 | package Net::IDN::Punycode::PP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = "2.502"; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 15 | 2 | our @EXPORT = (); | |||||||||||||||||||||||||||
| 16 | 2 | our @EXPORT_OK = qw(encode_punycode decode_punycode); | |||||||||||||||||||||||||||
| 17 | 2 | our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use integer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use constant BASE => 36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use constant TMIN => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use constant TMAX => 26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use constant SKEW => 38; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | use constant DAMP => 700; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | use constant INITIAL_BIAS => 72; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | use constant INITIAL_N => 128; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | use constant UNICODE_MIN => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use constant UNICODE_MAX => 0x10FFFF; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | my $Delimiter = chr 0x2D; | |||||||||||||||||||||||||||
| 33 | 2 | my $BasicRE = "\x00-\x7f"; | |||||||||||||||||||||||||||
| 34 | 2 | my $PunyRE = "A-Za-z0-9"; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 464 | ▶ | sub _adapt { | ||||||||||||||||||||||||||
|
Subroutine
_adapt: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 37 | 464 | my($delta, $numpoints, $firsttime) = @_; | |||||||||||||||||||||||||||
| 38 | 464 | ▶ | $delta = int($firsttime ? $delta / DAMP : $delta / 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 464 | $delta += int($delta / $numpoints); | |||||||||||||||||||||||||||
| 40 | 464 | my $k = 0; | |||||||||||||||||||||||||||
| 41 | 464 | ▶ | while ($delta > int(((BASE - TMIN) * TMAX) / 2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 86 | $delta /= BASE - TMIN; | |||||||||||||||||||||||||||
| 43 | 86 | $k += BASE; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 464 | return $k + (((BASE - TMIN + 1) * $delta) / ($delta + SKEW)); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 21 | ▶ | sub decode_punycode { | ||||||||||||||||||||||||||
|
Subroutine
decode_punycode: called CC=15 SCAR=27.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 49 | 21 | ▶ | die("Usage: Net::IDN::Punycode::decode_punycode(input)") unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 2 | ▶ | no warnings 'utf8'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 21 | my $input = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 21 | my $n = INITIAL_N; | |||||||||||||||||||||||||||
| 55 | 21 | my $i = 0; | |||||||||||||||||||||||||||
| 56 | 21 | my $bias = INITIAL_BIAS; | |||||||||||||||||||||||||||
| 57 | 21 | my @output; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 21 | ▶ | return undef unless defined $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 21 | ▶ | return '' unless length $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 21 | ▶ | if($input =~ s/(.*)$Delimiter//os) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 10 | my $base_chars = $1; | |||||||||||||||||||||||||||
| 64 | 10 | ▶ | croak("non-base character in input for decode_punycode") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | if $base_chars =~ m/[^$BasicRE]/os; | ||||||||||||||||||||||||||||
| 66 | 10 | push @output, split //, $base_chars; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 21 | my $code = $input; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 21 | ▶ | croak('invalid digit in input for decode_punycode') if $code =~ m/[^$PunyRE]/os; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 21 | utf8::downgrade($input); ## handling failure of downgrade is more expensive than | |||||||||||||||||||||||||||
| 73 | ## doing the above regexp w/ utf8 semantics | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 21 | ▶ | while(length $code) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||
| 77 | 232 | my $oldi = $i; | |||||||||||||||||||||||||||
| 78 | 232 | my $w = 1; | |||||||||||||||||||||||||||
| 79 | LOOP: | ||||||||||||||||||||||||||||
| 80 | 232 | for (my $k = BASE; 1; $k += BASE) { | |||||||||||||||||||||||||||
| 81 | 431 | my $cp = substr($code, 0, 1, ''); | |||||||||||||||||||||||||||
| 82 | 431 | ▶ | croak("incomplete encoded code point in decode_punycode") if !defined $cp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 431 | my $digit = ord $cp; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | ## NB: this depends on the PunyRE catching invalid digit characters | ||||||||||||||||||||||||||||
| 86 | ## before they turn up here | ||||||||||||||||||||||||||||
| 87 | ## | ||||||||||||||||||||||||||||
| 88 | 431 | ▶ | $digit = $digit < 0x40 ? $digit + (26-0x30) : ($digit & 0x1f) -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 431 | $i += $digit * $w; | |||||||||||||||||||||||||||
| 91 | 431 | my $t = $k - $bias; | |||||||||||||||||||||||||||
| 92 | 431 | ▶ | $t = $t < TMIN ? TMIN : $t > TMAX ? TMAX : $t; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 431 | ▶ | last LOOP if $digit < $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 199 | $w *= (BASE - $t); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 232 | $bias = _adapt($i - $oldi, @output + 1, $oldi == 0); | |||||||||||||||||||||||||||
| 98 | 232 | $n += $i / (@output + 1); | |||||||||||||||||||||||||||
| 99 | 232 | $i = $i % (@output + 1); | |||||||||||||||||||||||||||
| 100 | 232 | ▶ | croak('invalid code point') if $n < UNICODE_MIN or $n > UNICODE_MAX; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$n < 0 or $n > 1114111
MC/DC0%0/2
$n < 0 or $n > 1114111
$n < 0
$n > 1114111
Truth table33%1/3
A or B
A= $n < 0B= $n > 1114111
| |||||||||||||||||||||||||||||
| 101 | 232 | splice(@output, $i, 0, chr($n)); | |||||||||||||||||||||||||||
| 102 | 232 | $i++; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 21 | return join '', @output; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 21 | ▶ | sub encode_punycode { | ||||||||||||||||||||||||||
|
Subroutine
encode_punycode: called CC=14 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 21 | ▶ | die("Usage: Net::IDN::Punycode::encode_punycode(input)") unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | no warnings 'utf8'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 21 | my $input = shift; | |||||||||||||||||||||||||||
| 112 | 21 | my $input_length = length $input; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | ## my $output = join '', $input =~ m/([$BasicRE]+)/og; ## slower | ||||||||||||||||||||||||||||
| 115 | 21 | my $output = $input; $output =~ s/[^$BasicRE]+//ogs; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 21 | my $h = my $bb = length $output; | |||||||||||||||||||||||||||
| 118 | 21 | ▶ | $output .= $Delimiter if $bb > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 21 | utf8::downgrade($output); ## no unnecessary use of utf8 semantics | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 21 | my @input = map ord, split //, $input; | |||||||||||||||||||||||||||
| 122 | 21 | my @chars = sort { $a<=> $b } grep { $_ >= INITIAL_N } @input; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 21 | my $n = INITIAL_N; | |||||||||||||||||||||||||||
| 125 | 21 | my $delta = 0; | |||||||||||||||||||||||||||
| 126 | 21 | my $bias = INITIAL_BIAS; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 21 | foreach my $m (@chars) { | |||||||||||||||||||||||||||
| 129 | 232 | ▶ | next if $m < $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 178 | $delta += ($m - $n) * ($h + 1); | |||||||||||||||||||||||||||
| 131 | 178 | $n = $m; | |||||||||||||||||||||||||||
| 132 | 178 | ▶ | for(my $i = 0; $i < $input_length; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 3432 | my $c = $input[$i]; | |||||||||||||||||||||||||||
| 135 | 3432 | ▶ | $delta++ if $c < $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 3432 | ▶ | if ($c == $n) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 232 | my $q = $delta; | |||||||||||||||||||||||||||
| 138 | LOOP: | ||||||||||||||||||||||||||||
| 139 | 232 | for (my $k = BASE; 1; $k += BASE) { | |||||||||||||||||||||||||||
| 140 | 431 | my $t = $k - $bias; | |||||||||||||||||||||||||||
| 141 | 431 | ▶ | $t = $t < TMIN ? TMIN : $t > TMAX ? TMAX : $t; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 431 | ▶ | last LOOP if $q < $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 199 | my $o = $t + (($q - $t) % (BASE - $t)); | |||||||||||||||||||||||||||
| 146 | 199 | ▶ | $output .= chr $o + ($o < 26 ? 0x61 : 0x30-26); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 199 | $q = int(($q - $t) / (BASE - $t)); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 232 | ▶ | croak("input exceeds punycode limit") if $q > BASE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 232 | ▶ | $output .= chr $q + ($q < 26 ? 0x61 : 0x30-26); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 232 | $bias = _adapt($delta, $h + 1, $h == $bb); | |||||||||||||||||||||||||||
| 154 | 232 | $delta = 0; | |||||||||||||||||||||||||||
| 155 | 232 | $h++; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 178 | $delta++; | |||||||||||||||||||||||||||
| 159 | 178 | $n++; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 21 | return $output; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | 1; | |||||||||||||||||||||||||||
| 165 | __END__ | ||||||||||||||||||||||||||||