/usr/share/perl5/Convert/BaseN.pm is in libconvert-basen-perl 0.01-2.
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 | package Convert::BaseN;
use warnings;
use strict;
our $VERSION = sprintf "%d.%02d", q$Revision: 0.1 $ =~ /(\d+)/g;
use Carp;
sub _make_tr($$;$) {
my ( $from, $to, $opt ) = @_;
$opt ||= '';
my $tr = eval qq{ sub{ \$_[0] =~ tr#$from#$to#$opt } };
croak $@ if $@;
$tr;
}
my %h2q = qw{ 0 00 1 01 2 02 3 03 4 10 5 11 6 12 7 13
8 20 9 21 a 22 b 23 c 30 d 31 e 32 f 33 };
my %q2h = reverse %h2q;
my %o2b = qw{ 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111 };
my %b2o = reverse %o2b;
my %v2b = do {
my $i = 0;
map { $_ => sprintf( "%05b", $i++ ) } ( '0' .. '9', 'A' .. 'V' );
};
my %b2v = reverse %v2b;
my %gen_decoders = (
2 => sub {
my ( $chars ) = @_;
my $tr = $chars ? _make_tr( $chars, '01' ) : undef;
sub {
my $str = shift;
$tr->($str) if $tr;
$str =~ tr/01//cd;
scalar pack "B*", $str;
}
},
4 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( $chars, '0123' ) : undef;
sub {
my $str = shift;
$tr->($str) if $tr;
$str =~ tr/0123//cd;
$str =~ s/(..)/$q2h{$1}/g;
scalar pack "H*", $str;
}
},
8 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( $chars, '0-7=' ) : undef;
sub {
my $str = shift;
$tr->($str) if $tr;
$str =~ tr/0-7//cd;
$str =~ s/(.)/$o2b{$1}/g;
my $padlen = (length $str) % 8;
$str =~ s/0{$padlen}\z//;
scalar pack "B*", $str;
}
},
16 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( $chars, '0-9a-f' ) : undef;
sub {
my $str = shift;
$tr->($str) if $tr;
$str =~ tr/0-9a-f//cd;
scalar pack "H*", lc $str;
}
},
32 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( $chars, '0-9A-V=' ) : undef;
sub {
my $str = shift;
$tr->($str) if $tr;
$str =~ tr/0-9A-V//cd;
$str =~ s/(.)/$v2b{$1}/g;
my $padlen = (length $str) % 8;
$str =~ s/0{$padlen}\z//;
scalar pack "B*", $str;
}
},
64 => sub {
require MIME::Base64;
my ($chars) = @_;
my $tr = $chars ? _make_tr( $chars, '0-9A-Za-z+/=' ) : undef;
sub {
my $str = shift;
$tr->($str) if $tr;
MIME::Base64::decode($str);
}
}
);
sub _fold_line {
my ( $str, $lf, $cpl ) = @_;
$lf = "\n" unless defined $lf;
# warn ord $lf;
return $str unless $lf;
$cpl ||= 76;
$str =~ s/(.{$cpl})/$1$lf/gms;
$str;
}
my %gen_encoders = (
2 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( '01', $chars ) : undef;
sub ($;$$) {
my ( $str, $lf, $cpl ) = @_;
my $ret = unpack "B*", $str;
$tr->($ret) if $tr;
_fold_line( $ret, $lf, $cpl );
}
},
4 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( '0123', $chars ) : undef;
sub ($;$) {
my ( $str, $lf, $cpl ) = @_;
my $ret = unpack "H*", $str;
$ret =~ s/(.)/$h2q{$1}/g;
$tr->($ret) if $tr;
_fold_line( $ret, $lf, $cpl );
}
},
8 => sub {
my ( $chars, $nopad ) = @_;
my $tr = $chars ? _make_tr( '0-7=', $chars ) : undef;
sub ($;$$) {
my ( $str, $lf, $cpl ) = @_;
my $ret = unpack "B*", $str;
$ret .= 0 while ( length $ret ) % 3;
$ret =~ s/(...)/$b2o{$1}/g;
$nopad or do{ $ret .= '=' while ( length $ret ) % 8 };
$tr->($ret) if $tr;
_fold_line( $ret, $lf, $cpl );
}
},
16 => sub {
my ($chars) = @_;
my $tr = $chars ? _make_tr( '0-9a-f', $chars ) : undef;
sub ($;$$) {
my ( $str, $lf, $cpl ) = @_;
my $ret = unpack "H*", $str;
$tr->($ret) if $tr;
_fold_line( $ret, $lf, $cpl );
}
},
32 => sub {
my ( $chars, $nopad ) = @_;
my $tr = $chars ? _make_tr( '0-9A-V=', $chars ) : undef;
sub ($;$$) {
my ( $str, $lf, $cpl ) = @_;
my $ret = unpack "B*", $str;
$ret .= 0 while ( length $ret ) % 5;
$ret =~ s/(.....)/$b2v{$1}/g;
$nopad or do{ $ret .= '=' while ( length $ret ) % 8 };
$tr->($ret) if $tr;
_fold_line( $ret, $lf, $cpl );
}
},
64 => sub {
require MIME::Base64;
my ( $chars, $nopad ) = @_;
my $tr = $chars ? _make_tr( '0-9A-Za-z+/=', $chars ) : undef;
sub ($;$$) {
my ( $str, $lf, $cpl ) = @_;
$str =
defined $lf
? _fold_line( MIME::Base64::encode( $str, '' ), $lf, $cpl )
: MIME::Base64::encode( $str, $lf );
$str =~ tr/=//d if $nopad;
$tr->($str) if $tr;
$str;
}
}
);
sub _base64_decode_any {
require MIME::Base64;
my $str = shift;
$str =~ tr{\-\_\+\,\[\]}{+/+/+/};
local $^W = 0; # in case the string is not padded
MIME::Base64::decode($str);
}
our %named_decoder = (
base2 => $gen_decoders{2}->(),
base4 => $gen_decoders{4}->(),
DNA => $gen_decoders{4}->('ACGT'),
RNA => $gen_decoders{4}->('UGCA'),
base8 => $gen_decoders{8}->(),
base16 => $gen_decoders{16}->('0-9A-F'),
base32 => $gen_decoders{32}->('A-Z2-7='),
base32hex => $gen_decoders{32}->(),
base64 => \&_base64_decode_any,
base64_url => \&_base64_decode_any,
base64_imap => \&_base64_decode_any,
base64_ircu => \&_base64_decode_any,
);
our %named_encoder = (
base2 => $gen_encoders{2}->(),
base4 => $gen_encoders{4}->(),
DNA => $gen_encoders{4}->('ACGT'),
RNA => $gen_encoders{4}->('UGCA'),
base8 => $gen_encoders{8}->(),
base16 => $gen_encoders{16}->('0-9A-F'),
base32 => $gen_encoders{32}->('A-Z2-7='),
base32hex => $gen_encoders{32}->(),
base64 => $gen_encoders{64}->(),
base64_url => $gen_encoders{64}->( '0-9A-Za-z\-\_=', 1 ),
base64_imap => $gen_encoders{64}->('0-9A-Za-z\+\,='),
base64_ircu => $gen_encoders{64}->('0-9A-Za-z\[\]='),
);
sub new {
my $pkg = shift;
my %opt = @_ == 1 ? ( name => shift ) : @_;
my ( $encoder, $decoder );
if ( $opt{name} ) {
$decoder = $named_decoder{ $opt{name} };
$encoder = $named_encoder{ $opt{name} };
croak "$opt{name} unknown" unless $decoder and $encoder;
}
else {
eval {
my $nopad = exists $opt{padding} ? !$opt{padding}
: $opt{nopadding};
$decoder = $gen_decoders{ $opt{base} }->( $opt{chars} );
$encoder = $gen_encoders{ $opt{base} }->( $opt{chars}, $nopad );
};
croak "base $opt{base} unknown" if $@;
}
bless {
decoder => $decoder,
encoder => $encoder,
}, $pkg;
}
sub decode { my $self = shift; $self->{decoder}->(@_) }
sub encode { my $self = shift; $self->{encoder}->(@_) }
if (__FILE__ eq $0){
my ($bn, $encoded);
$bn = __PACKAGE__->new(base => 2, chars => '<>');
$encoded = $bn->encode("dankogai", " ");
warn $encoded;
warn $bn->decode($encoded);
$bn = __PACKAGE__->new(base => 4, chars => 'ACGT');
$encoded = $bn->encode("dankogai", " ");
warn $encoded;
warn $bn->decode($encoded);
$bn = __PACKAGE__->new(base => 8, chars => 'abcdefgh=');
$encoded = $bn->encode("dankogai");
warn $encoded;
warn $bn->decode($encoded);
warn length $bn->decode($encoded);
$bn = __PACKAGE__->new(base => 16, chars => '0-9A-F');
$encoded = $bn->encode("dankogai", " ");
warn $encoded;
$bn = __PACKAGE__->new(base => 32);
$encoded = $bn->encode("dankogai");
warn $encoded;
warn $bn->decode($encoded);
warn length $bn->decode($encoded);
$bn = __PACKAGE__->new(base => 32, chars => 'A-Z2-7=');
$encoded = $bn->encode("dankogai");
warn $encoded;
warn $bn->decode($encoded);
warn length $bn->decode($encoded);
$bn = __PACKAGE__->new(base => 64);
$encoded = $bn->encode("dankogai");
warn $encoded;
warn $bn->decode($encoded);
$bn = __PACKAGE__->new(base => 64,chars => '0-9A-Za-z\-_=');
$encoded = $bn->encode(join("", map {chr} 0x21 .. 0x7e), "\n", 40);
warn $encoded;
warn $bn->decode($encoded);
warn scalar unpack "H*", $bn->decode('-__-');
$bn = __PACKAGE__->new('base69');
#warn $bn->encode("dankogai");
#$bn = __PACKAGE__->new(name => 'base4');
#$bn = __PACKAGE__->new(name => 'basex');
#$bn = __PACKAGE__->new(base => 17);
}
1; # End of Convert::BaseN
=head1 NAME
Convert::BaseN - encoding and decoding of base{2,4,8,16,32,64} strings
=head1 VERSION
$Id: BaseN.pm,v 0.1 2008/06/16 17:34:27 dankogai Exp dankogai $
=cut
=head1 SYNOPSIS
use Convert::BaseN;
# by name
my $cb = Convert::BaseN->new('base64');
my $cb = Convert::BaseN->new( name => 'base64' );
# or base
my $cb = Convert::BaseN->new( base => 64 );
my $cb_url = Convert::BaseN->new(
base => 64,
chars => '0-9A-Za-z\-_='
);
# encode and decode
$encoded = $cb->encode($data);
$decoded = $cb->decode($encoded);
=head1 EXPORT
Nothing. Instead of that, this module builds I<transcoder object> for
you and you use its C<decode> and C<encode> methods to get the job
done.
=head1 FUNCTIONS
=head2 new
Create the transcoder object.
# by name
my $cb = Convert::BaseN->new('base64');
my $cb = Convert::BaseN->new( name => 'base64' );
# or base
my $cb = Convert::BaseN->new( base => 64 );
my $cb_url = Convert::BaseN->new(
base => 64,
chars => '0-9A-Za-z\-_='
);
You can pick the decoder by name or create your own by specifying base
and character map.
=over 2
=item base
Must be 2, 4, 16, 32 or 64.
=item chars
Specifiles the character map. The format is the same as C<tr>.
# DNA is coded that way.
my $dna = Convert::BaseN->new( base => 4, chars => 'ACGT' );
=item padding
=item nopadding
Specifies if padding (adding '=' or other chars) is required when
encoding. default is yes.
# url-safe Base64
my $b64url = Convert::BaseN->new(
base => 64, chars => '0-9A-Za-z\-_=', padding => 0;
);
=item name
When specified, the following pre-defined encodings will be used.
=over 2
=item base2
base 2 encoding. C<perl> is C<01110000011001010111001001101100>.
=item base4
=item DNA
=item RNA
base 4 encodings. C<perl> is:
base4: 1300121113021230
DNA: CTAACGCCCTAGCGTA
RNA: GAUUGCGGGAUCGCAU
base 16 encoding. C<perl> is C<7065726c>.
=item base32
=item base32hex
base 32 encoding mentioned in RFC4648. C<perl> is:
base32: OBSXE3A==
base32hex: E1IN4R0==
=item base64
=item base64_url
=item base64_imap
=item base64_ircu
base 64 encoding, as in L<MIME::Base64>. They differ only in
characters to represent number 62 and 63 as follows.
base64: +/
base64_url: -_
base64_imap: +,
base64_ircu: []
for all predefined base 64 variants, C<decode> accept ANY form of those.
=back
=back
=head2 decode
Does decode
my $decoded = $cb->decode($data)
=head2 encode
Does encode.
# line folds every 76 octets, like MIME::Base64::encode
my $encoded = $cb->encode($data);
# no line folding (compatibile w/ MIME::Base64)
my $encoded = $cb->encode($data, "");
# line folding by CRLF, every 40 octets
my $encoded = $cb->encode($data, "\r\n", 40);
=head1 SEE ALSO
RFC4648 L<http://tools.ietf.org/html/rfc4648>
Wikipedia L<http://en.wikipedia.org/wiki/Base64>
L<http://www.centricorp.com/papers/base64.htm>
L<MIME::Base64>
L<MIME::Base32>
L<MIME::Base64::URLSafe>
=head1 AUTHOR
Dan Kogai, C<< <dankogai at dan.co.jp> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-convert-basen at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Convert-BaseN>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Convert::BaseN
You can also look for information at:
=over 4
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Convert-BaseN>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Convert-BaseN>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Convert-BaseN>
=item * Search CPAN
L<http://search.cpan.org/dist/Convert-BaseN>
=back
=head1 ACKNOWLEDGEMENTS
N/A
=head1 COPYRIGHT & LICENSE
Copyright 2008 Dan Kogai, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|