/usr/share/perl5/RACE.pm is in libopensrs-perl 3.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | package RACE;
use strict;
use NAMEPREP;
use Unicode::String qw(utf8 ucs4 utf16);
Unicode::String->stringify_as('utf8');
my $RACEPrefix = "bq--";
my $STD13Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYX0123456789-";
my $Base32Chars = 'abcdefghijklmnopqrstuvwxyz234567';
my $USE_RACE = 1;
sub Initialise
{
my %args = @_;
NAMEPREP::Initialise(%args)
}
sub UseRace
{
my $arg = shift;
$USE_RACE = $arg;
}
sub DoRACE
{
my %args = @_;
my $fulldomainstring = $args{Domain};
#charset is a legacy argument. Not used any more
my $charset = $args{EncodingType};
my (@dmnparts,$icntr,$TheUCS4,$extension,$TheIn,$nameprephash);
my (%result,$racehash);
if ( (! defined $charset) or ($charset eq "") or (!$USE_RACE))
{
$result{OriginalDomain} = $fulldomainstring;
$result{ConvertedDomain} = $fulldomainstring;
$result{DomainConverted} = 0;
return \%result;
}
#assume input is in utf8
@dmnparts = split(/\./,$fulldomainstring);
$extension = lc $dmnparts[$#dmnparts];
$result{OriginalDomain} = $fulldomainstring;
$result{DomainConverted} = 0;
$result{ConvertedDomain} = "";
for ($icntr=0;$icntr<$#dmnparts;$icntr++)
{
my $u = $dmnparts[$icntr];
$nameprephash = NAMEPREP::nameprep(name => $u);
if (defined $nameprephash->{error})
{
$result{Error} = "Nameprep Error: " . $nameprephash->{error} . " in: " . $u ;
return \%result;
}
$result{MapOutput} .= $nameprephash->{mapoutput} . ".";
$result{NamepreppedDomain} .= $nameprephash->{output} . ".";
$result{UniHexCodes} .= $nameprephash->{hexin}. "." ;
$result{NameprepHexCodes} .= $nameprephash->{hexoutput} . ".";
$TheIn = $nameprephash->{output};
$TheUCS4 = utf8($TheIn)->ucs4;
$racehash = UCS4toRACE($TheUCS4);
if(defined $racehash->{error})
{
$result{Error} = "RACE Error: " . $racehash->{error};
return \%result;
}
if($racehash->{DomainConverted})
{
$result{DomainConverted} = 1;
$result{ConvertedDomain} .= $racehash->{result};
}
else
{
$result{DomainConverted} = 0;
$result{ConvertedDomain} .= lc $u;
}
$result{ConvertedDomain} .= ".";
undef $racehash;
undef $nameprephash;
}
$result{ConvertedDomain} .= $extension;
$result{NamepreppedDomain} .= $extension;
$result{UniHexCodes} .= $extension;
$result{NameprepHexCodes} .= $extension;
$result{MapOutput} .= $extension;
if($result{DomainConverted}!=1)
{
$result{ConvertedDomain} = lc $fulldomainstring;
}
return \%result;
}
sub UndoRACE
{
my %args = @_;
my $fullraceddomainstring = $args{Domain};
my $charset = $args{EncodingType};
my (@dmnparts,$extension,%result,$icntr,$TheUCS4,$racehash);
if ( (! defined $charset) or ($charset eq "") or (!$USE_RACE))
{
$result{OriginalDomain} = $fullraceddomainstring;
return \%result;
}
@dmnparts = split(/\./,$fullraceddomainstring);
$extension = lc $dmnparts[$#dmnparts];
for ($icntr=0;$icntr<$#dmnparts;$icntr++)
{
my $u = $dmnparts[$icntr];
$racehash = RACEtoUCS4($u);
if(not $racehash->{is_wrong_prefix})
{
if(defined $racehash->{error})
{
$result{Error} = $racehash->{error};
return \%result;
}
$result{OriginalDomain} .= ucs4($racehash->{OriginalDomain})->utf8;
$result{DomainConverted} = 1;
}
else
{
$result{OriginalDomain} .= $u;
}
$result{OriginalDomain} .= ".";
}
$result{OriginalDomain} .= $extension;
return \%result;
}
sub RACEtoUCS4 {
my $InString = lc(shift(@_));
my ($PostBase32, @DeArr, $i, $U1, $N1, $OutString, $LCheck,
$InputPointer, @UpperUniq, %UpperSeen,%result,@CheckArr);
# Strip any whitespace
$InString =~ s/\s*//g;
# Strip of the prefix string
unless(substr($InString, 0, length($RACEPrefix)) eq $RACEPrefix)
{
$result{error} = "The input to RACEtoUCS4 did not start with '$RACEPrefix'";
#we do not want to introduce an error if the prefix is bad.
$result{is_wrong_prefix} = 1;
return \%result;
}
$InString = substr($InString, length($RACEPrefix));
my $pb32hash = Base32Decode($InString);
if(defined $pb32hash->{error})
{
$result{error} = $pb32hash->{error};
return \%result;
}
$PostBase32 = $pb32hash->{result};
@DeArr = split(//, $PostBase32);
# Reverse the compression
$U1 = $DeArr[0]; # Step 1a
if($#DeArr < 1) # Step 1b
{
$result{error} = "The output of Base32Decode was zero length.";
return \%result;
}
unless ($U1 eq "\xd8") { # Step 1c
$i = 1;
until($i > $#DeArr) { # Step 2a
$N1 = $DeArr[$i++]; # Step 2b
unless($N1 eq "\xff") { # Step 2c
if(($U1 eq "\x00") and ($N1 eq "\x99")) # Step 3
{
$result{error} = "Found 0099 in the input to RACEtoUCS4, step 3.";
return \%result;
}
$OutString .= $U1 . $N1; # Step 4
} else {
if($i > $#DeArr) # Step 5
{
$result{error} = "Input in RACE string at octet $i too short at step 5";
return \%result;
}
$N1 = $DeArr[$i++]; # Step 6a
if($N1 eq "\x99") # Step 6b
{ $OutString .= $U1 . "\xff" }
else # Step 7
{ $OutString .= "\x00" . $N1 }
}
}
if((length($OutString) % 2) == 1) # Step 11
{
$result{error} = "The output of RACEtoUCS4 for compressed input was an odd number of characters at step 11.";
return \%result;
}
} else { # Was not compressed
$LCheck = substr(join('', @DeArr), 1); # Step 8a
if((length($LCheck) % 2 ) == 1 ) # Step 8b
{
$result{error} = "The output of RACEtoUCS4 for uncompressed input was an odd number of characters at step 8b";
return \%result;
}
# Do the step 9 check to be sure the right length was used
@CheckArr = split(//, $LCheck);
for($InputPointer = 0; $InputPointer <= $#CheckArr; $InputPointer += 2) {
unless ($UpperSeen{$CheckArr[$InputPointer]}) {
$UpperSeen{$CheckArr[$InputPointer]} = 1;
push (@UpperUniq, $CheckArr[$InputPointer])
}
}
# Should it have been compressed?
if( ($#UpperUniq == 0) or
( ($#UpperUniq == 1) and
(($UpperUniq[0] eq "\x00") or ($UpperUniq[1] eq "\x00"))
)
) {
$result{error} = "Input to RACEtoUCS4 failed during LCHECK format test in step 9.";
return \%result;
}
if((length($LCheck) % 2) == 1) # Step 10a
{
$result{error} = "The output of RACEtoUCS4 for uncompressed input was an odd number of characters at step 10a";
return \%result;
}
$OutString = $LCheck
}
if(CheckForSTD13Name($OutString))
{
$result{error} = "Found all-STD13 name before output of RACEtoUCS4";
return \%result;
}
if(CheckForBadSurrogates($OutString))
{
$result{error} = "Found bad surrogate before output of RACEtoUCS4";
return \%result;
}
$result{OriginalDomain} = utf16($OutString)->ucs4;
return \%result;
}
sub UCS4toRACE {
my $InString = shift(@_);
my (@InArr, $InStr, $InputPointer, $DoStep3, @UpperUniq, %UpperSeen);
my ($U1, $U2, $N1, $CompString,$PostBase32, %result, $DieOrd);
# Make an array of the UTF16 octets
@InArr = split(//, ucs4($InString)->utf16);
$InStr = join('', @InArr);
if(CheckForSTD13Name($InStr))
{
#the name contains only ascii chars
$result{result} = $InStr;
$result{DomainConverted} = 0;
return \%result;
}
# Prepare for steps 1 and 2 by making an array of the upper octets
for($InputPointer = 0; $InputPointer <= $#InArr; $InputPointer += 2) {
unless ($UpperSeen{$InArr[$InputPointer]}) {
$UpperSeen{$InArr[$InputPointer]} = 1;
push (@UpperUniq, $InArr[$InputPointer])
}
}
if($#UpperUniq == 0) { # Step 1
$U1 = $UpperUniq[0];
$DoStep3 = 0;
} elsif($#UpperUniq == 1) { # Step 2
if($UpperUniq[0] eq "\x00") {
$U1 = $UpperUniq[1];
$DoStep3 = 0;
} elsif($UpperUniq[1] eq "\x00") {
$U1 = $UpperUniq[0];
$DoStep3 = 0;
} else { $DoStep3 = 1 }
} else { $DoStep3 = 1 }
# Now output based on the value of $DoStep3
if($DoStep3) { # Step 3
$CompString = "\xd8" . join('', @InArr);
} else {
if(($U1 ge "\xd8") and ($U1 le "\xdf")) { # Step 4a
$DieOrd = sprintf("%04lX", ord($U1));
$result{error} = "Found invalid input to UCS4toRACE step 4a: $DieOrd.";
return \%result;
}
$CompString = $U1; # Step 4b
$InputPointer = 0;
while($InputPointer <= $#InArr) { # Step 5a
$U2 = $InArr[$InputPointer++]; $N1 = $InArr[$InputPointer++]; # Step 5b
if(($U2 eq "\x00") and ($N1 eq "\x99")) # Step 5c
{ $result{error} = "Found U+0099 in input stream to UCS4toRACE step 5c.";
return \%result;
}
if( ($U2 eq $U1) and ($N1 ne "\xff") ) # Step 6
{ $CompString .= $N1 }
elsif( ($U2 eq $U1) and ($N1 eq "\xff") ) # Step 7
{ $CompString .= "\xff\x99" }
else { $CompString .= "\xff" . $N1 } # Step 8
}
}
if(length($CompString) >= 37)
{
$result{error} = "Lenth of compressed string was >= 37 in UCS4toRACE.";
return \%result;
}
$PostBase32 = Base32Encode($CompString);
$result{DomainConverted} = 1;
$result{result} = "$RACEPrefix$PostBase32";
return \%result;
}
sub CheckForSTD13Name
{
# The input is in UTF-16
my $InCheck = shift(@_);
my (@CheckArr, $CheckPtr, $Lower, $Upper,);
@CheckArr = split(//, $InCheck);
$CheckPtr = 0;
until($CheckPtr > $#CheckArr) {
$Upper = $CheckArr[$CheckPtr++];
$Lower = $CheckArr[$CheckPtr++];
if(($Upper ne "\x00") or
(index($STD13Chars, $Lower) == -1) ) { return 0 }
}
return 1;
}
sub Base32Encode {
my($ToEncode) = shift(@_);
my ($i, $OutString, $CompBits, $FivePos, $FiveBitsString, $FiveIndex);
# Turn the compressed string into a string that represents the bits as
# 0 and 1. This is wasteful of space but easy to read and debug.
$CompBits = '';
foreach $i (split(//, $ToEncode)) { $CompBits .= unpack("B8", $i) };
# Pad the value with enough 0's to make it a multiple of 5
if((length($CompBits) % 5) != 0)
{ $CompBits .= '0' x (5 - (length($CompBits) % 5)) }; # Step 1a
$FivePos = 0; # Step 1b
do {
$FiveBitsString = substr($CompBits, $FivePos, 5); # Step 2
$FiveIndex = unpack("N", pack("B32", ('0' x 27) . $FiveBitsString));
$OutString .= substr($Base32Chars, $FiveIndex, 1); # Step 3
$FivePos += 5; # Step 4a
} until($FivePos == length($CompBits)); # Step 4b
return $OutString;
}
sub Base32Decode {
my ($ToDecode) = shift(@_);
my ($InputCheck, $OutString, $DeCompBits, $DeCompIndex, @DeArr, $i,
$PaddingLen, $PaddingContent,%result,$InChar, $Padding);
$InputCheck = length($ToDecode) % 8; # Step 1
if(($InputCheck == 1) or
($InputCheck == 3) or
($InputCheck == 6))
{
$result{error} = "Input to Base32Decode was a bad mod length: $InputCheck\n";
return \%result;
}
# $DeCompBits is a string that represents the bits as
# 0 and 1. This is wasteful of space but easy to read and debug.
$DeCompBits = '';
foreach $InChar (split(//, $ToDecode)) {
if(index($Base32Chars, $InChar) == -1) # Character not in base set
{
$result{error} = "Input to Base32Decode had a bad character: $InChar\n";
return \%result;
}
$DeCompIndex = pack("N", index($Base32Chars, $InChar));
$DeCompBits .= substr(unpack("B32", $DeCompIndex), 27);
}
# Step 5
$Padding = length($DeCompBits) % 8;
$PaddingContent = substr($DeCompBits, (length($DeCompBits) - $Padding));
unless(index($PaddingContent, '1') == -1)
{
$result{error} = "Found non-zero padding in Base32Decode\n";
return \%result;
}
# Break the decompressed string into octets for returning
@DeArr = ();
for($i = 0; $i < int(length($DeCompBits) / 8); $i++) {
$DeArr[$i] =
chr(unpack("N", pack("B32", ('0' x 24) . substr($DeCompBits, $i * 8, 8))));
}
$result{result} = join('', @DeArr);
return \%result;
}
sub CheckForBadSurrogates {
# The input is in UTF-16
my $InCheck = shift(@_);
my (@CheckArr, $CheckPtr, $Upper1, $Upper2,$HighSurr,$LowSurr);
@CheckArr = split(//, $InCheck);
$CheckPtr = 0;
$HighSurr = "\xD8\xD9\xDA\xDB";
$LowSurr = "\xDC\xDD\xDE\xDF";
until($CheckPtr > $#CheckArr) {
# Check for bad half-pair
if((($CheckPtr + 2 ) >= $#CheckArr) and
(index($HighSurr.$LowSurr, $CheckArr[$CheckPtr]) > -1 )) {
return 1;
}
last unless(defined($CheckArr[$CheckPtr + 4]));
$Upper1 = $CheckArr[$CheckPtr += 2];
$Upper2 = $CheckArr[$CheckPtr += 2];
if( ((index($HighSurr, $Upper1) > -1) and
(index($LowSurr, $Upper2) == -1))
or
((index($HighSurr, $Upper1) == -1) and
(index($LowSurr, $Upper2) > -1))) {
return 1;
}
}
return 0;
}
return 1;
|