| 1 | package Net::IDN::PP 0.03; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A pure-Perl implementation of the Punycode algorithm for encoding internationalized domain names (IDNs) | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 19 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 7 | 19 | my $name = pop; | |||||||||||||||||||||||||||
| 8 | 19 | return join('.', map { encode_label($_) } split(/\./, $name)); | |||||||||||||||||||||||||||
| 9 | } | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 11 | my $name = pop; | |||||||||||||||||||||||||||
| 13 | 11 | return join('.', map { decode_label($_) } split(/\./, $name)); | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 34 | ▶ | sub encode_label { | ||||||||||||||||||||||||||
|
Subroutine
encode_label: called CC=13 SCAR=25.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 34 | my $label = lc(pop); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # if ASCII-only: return as-is | ||||||||||||||||||||||||||||
| 20 | 34 | ▶ | return $label if ($label =~ /^[\x00-\x7F]+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # extract code points | ||||||||||||||||||||||||||||
| 23 | 16 | my @cps = unpack("U*", $label); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Initialize @output with only the ASCII code points | ||||||||||||||||||||||||||||
| 26 | 16 | my @output = map { chr($_) } grep { $_ < 0x80 } @cps; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 16 | my $basic_length = scalar(@output); | |||||||||||||||||||||||||||
| 29 | 16 | my $output_str = join('', @output); | |||||||||||||||||||||||||||
| 30 | 16 | ▶ | $output_str .= '-' if ($basic_length > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Punycode parameters | ||||||||||||||||||||||||||||
| 33 | 16 | my $n = 128; | |||||||||||||||||||||||||||
| 34 | 16 | my $delta = 0; | |||||||||||||||||||||||||||
| 35 | 16 | my $bias = 72; | |||||||||||||||||||||||||||
| 36 | 16 | my $h = $basic_length; | |||||||||||||||||||||||||||
| 37 | 16 | my $len = scalar(@cps); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 16 | ▶ | while ($h < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | # Find the minimum code point >= n | ||||||||||||||||||||||||||||
| 41 | 27 | my $m = 0x10FFFF; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 27 | for my $cp (@cps) { | |||||||||||||||||||||||||||
| 44 | 151 | ▶ | $m = $cp if $cp >= $n && $cp < $m; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cp >= $n and $cp < $m
MC/DC100%2/2
$cp >= $n and $cp < $m
$cp >= $n
$cp < $m
Truth table100%3/3
A and B
A= $cp >= $nB= $cp < $m
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 27 | my $inc = ($m - $n) * ($h + 1); | |||||||||||||||||||||||||||
| 48 | 27 | $delta += $inc; | |||||||||||||||||||||||||||
| 49 | 27 | $n = $m; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 27 | for my $cp (@cps) { | |||||||||||||||||||||||||||
| 52 | 151 | ▶ | if ($cp < $n) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 53 | 99 | $delta++; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | } elsif ($cp == $n) { | ||||||||||||||||||||||||||||
| 56 | 28 | my $q = $delta; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 28 | for (my $k = 36; ; $k += 36) { | |||||||||||||||||||||||||||
| 59 | 67 | my $t; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 67 | ▶ | if ($k <= $bias) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 62 | 32 | $t = 1; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | } elsif ($k >= $bias + 26) { | ||||||||||||||||||||||||||||
| 65 | 34 | $t = 26; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||
| 68 | 1 | $t = $k - $bias; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 67 | ▶ | last if ($q < $t); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 39 | my $code = $t + (($q - $t) % (36 - $t)); | |||||||||||||||||||||||||||
| 75 | 39 | $output_str .= encode_digit($code); | |||||||||||||||||||||||||||
| 76 | 39 | $q = int(($q - $t) / (36 - $t)); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 28 | $output_str .= encode_digit($q); | |||||||||||||||||||||||||||
| 80 | 28 | $bias = adapt($delta, $h + 1, $h == $basic_length); | |||||||||||||||||||||||||||
| 81 | 28 | $delta = 0; | |||||||||||||||||||||||||||
| 82 | 28 | $h++; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 27 | $delta++; | |||||||||||||||||||||||||||
| 87 | 27 | $n++; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 16 | return q{xn--}.$output_str; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 67 | ▶ | sub encode_digit { | ||||||||||||||||||||||||||
|
Subroutine
encode_digit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 67 | my $d = shift; | |||||||||||||||||||||||||||
| 95 | 67 | return chr($d + 22 + 75 * ($d < 26)); # 0..25 = a..z, 26..35 = 0..9 | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 33 | ▶ | sub decode_digit { | ||||||||||||||||||||||||||
|
Subroutine
decode_digit: called CC=5 SCAR=17.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 99 | 33 | my $ch = lc(shift); | |||||||||||||||||||||||||||
| 100 | 33 | my $cp = ord($ch); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # 'a'..'z' => 0..25 | ||||||||||||||||||||||||||||
| 103 | 33 | ▶ | return $cp - 97 if ($cp >= 97 && $cp <= 122); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cp >= 97 and $cp <= 122
MC/DC50%1/2
$cp >= 97 and $cp <= 122
$cp >= 97
$cp <= 122
Truth table66%2/3
A and B
A= $cp >= 97B= $cp <= 122
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # '0'..'9' => 26..35 | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | return $cp - 22 if ($cp >= 48 && $cp <= 57); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$cp >= 48 and $cp <= 57
MC/DC0%0/2
$cp >= 48 and $cp <= 57
$cp >= 48
$cp <= 57
Truth table33%1/3
A and B
A= $cp >= 48B= $cp <= 57
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | return 36; # invalid | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 40 | ▶ | sub adapt { | ||||||||||||||||||||||||||
|
Subroutine
adapt: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 40 | my ($delta, $numpoints, $first_time) = @_; | |||||||||||||||||||||||||||
| 114 | 40 | ▶ | $delta = int($first_time ? $delta / 700 : $delta / 2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 40 | $delta += int($delta / $numpoints); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 40 | my $k = 0; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 40 | ▶ | while ($delta > 455) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $delta = int($delta / 35); | |||||||||||||||||||||||||||
| 121 | 0 | $k += 36; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 40 | return $k + int((36 * $delta) / ($delta + 38)); | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 15 | ▶ | sub decode_label { | ||||||||||||||||||||||||||
|
Subroutine
decode_label: called CC=8 SCAR=20.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 15 | my $label = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 15 | ▶ | return $label unless ($label =~ /^xn--/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 9 | my $input = lc(substr($label, 4)); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 9 | my @output; | |||||||||||||||||||||||||||
| 135 | 9 | my $dash_pos = rindex($input, '-'); | |||||||||||||||||||||||||||
| 136 | 9 | ▶ | if ($dash_pos != -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 8 | my $basic = substr($input, 0, $dash_pos); | |||||||||||||||||||||||||||
| 138 | 8 | @output = map { ord($_) } split(//, $basic); | |||||||||||||||||||||||||||
| 139 | 8 | $input = substr($input, $dash_pos + 1); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 9 | my $n = 128; | |||||||||||||||||||||||||||
| 143 | 9 | my $i = 0; | |||||||||||||||||||||||||||
| 144 | 9 | my $bias = 72; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 9 | my $in_idx = 0; | |||||||||||||||||||||||||||
| 147 | 9 | my $in_len = length($input); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 9 | ▶ | while ($in_idx < $in_len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 12 | my $oldi = $i; | |||||||||||||||||||||||||||
| 151 | 12 | my $w = 1; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 12 | for (my $k = 36; ; $k += 36) { | |||||||||||||||||||||||||||
| 154 | 33 | ▶ | last if ($in_idx >= $in_len); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 33 | my $c = substr($input, $in_idx++, 1); | |||||||||||||||||||||||||||
| 157 | 33 | my $d = decode_digit($c); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 33 | $i += $d * $w; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 33 | my $t; | |||||||||||||||||||||||||||
| 162 | 33 | ▶ | if ($k <= $bias) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 163 | 18 | $t = 1; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | } elsif ($k >= $bias + 26) { | ||||||||||||||||||||||||||||
| 166 | 14 | $t = 26; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | } else { | ||||||||||||||||||||||||||||
| 169 | 1 | $t = $k - $bias; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 33 | ▶ | last if ($d < $t); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 21 | $w *= (36 - $t); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 12 | my $out_len_plus_1 = scalar(@output) + 1; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 12 | $bias = adapt($i - $oldi, $out_len_plus_1, $oldi == 0); | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 12 | my $q = int($i / $out_len_plus_1); | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 12 | $n += $q; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 12 | $i = $i % $out_len_plus_1; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 12 | splice(@output, $i, 0, $n); | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 12 | $i++; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 9 | return join('', map { chr($_) } @output); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1 | 1; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | __END__ | ||||||||||||||||||||||||||||