Acme-Collector64

 view release on metacpan or  search on metacpan

lib/Acme/Collector64.pm  view on Meta::CPAN

    my $output = '';
    my $i = 0;
    while ($i < length $input) {
        my ($enc1, $enc2, $enc3, $enc4);
        for my $enc ($enc1, $enc2, $enc3, $enc4) {
            $enc = index $self->{index_table}, substr($input, $i++, 1);
        }
        my $chr1 = ($enc1 << 2) | ($enc2 >> 4);
        my $chr2 = (($enc2 & 15) << 4) | ($enc3 >> 2);
        my $chr3 = (($enc3 & 3) << 6) | $enc4;
        $output .= chr $chr1;
        if ($enc3 != 64) {
            $output .= chr $chr2;
        }
        if ($enc4 != 64) {
            $output .= chr $chr3;
        }
    }
    return $output;
}

1;
__END__

=encoding utf-8

=head1 NAME

Acme::Collector64 - Yet Another Base64?

=head1 SYNOPSIS

    use utf8;
    use Acme::Collector64;

    my $japanese64 = Acme::Collector64->new(
        index_table => 'あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもらりるれろがぎぐげござじずぜぞばびぶべぼぱぴぷぺぽやゆよわ=',
    );

    $japanese64->encode('Hello, world!');
    $japanese64->decode('てきにごふきやごけくほずへれぞりけち==');

=head1 DESCRIPTION

Let's make your own Base64!

=head1 METHODS

=over 4

=item my $c64 = Acme::Collector64->new([\%args])

Create new instance of Acme::Collector64.

=over 4

=item index_table

This is user definable index table. You have to define 65-character string.

index_table by default is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".

=back

=item $c64->encode($data)

This function takes B<binary string> to encode and return the encoded string.

=item $c64->decode($string)

This function takes B<text string> to decode and return the decoded data.

=back

=head1 AUTHOR

Takumi Akiyama E<lt>t.akiym at gmail.comE<gt>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut



( run in 1.966 second using v1.01-cache-2.11-cpan-97f6503c9c8 )