Convert-Base32-0.05/0000700000175300001440000000000011327375713012736 5ustar ericusersConvert-Base32-0.05/MANIFEST0000644000175300001440000000025411321444322014065 0ustar ericusersChanges MANIFEST Makefile.PL README lib/Convert/Base32.pm t/00_enc_dec.t t/01_error_checks.t META.yml Module meta-data (added by MakeMaker) Convert-Base32-0.05/lib/0000700000175300001440000000000011327375713013504 5ustar ericusersConvert-Base32-0.05/lib/Convert/0000700000175300001440000000000011327375713015124 5ustar ericusersConvert-Base32-0.05/lib/Convert/Base32.pm0000644000175300001440000001020011327374252016501 0ustar ericuserspackage Convert::Base32; use strict; #use warnings; use Carp qw( ); use Exporter qw( ); use vars qw( $VERSION @ISA @EXPORT ); $VERSION = '0.05'; push @ISA, 'Exporter'; @EXPORT = qw( encode_base32 decode_base32 ); my @syms = ( 'a'..'z', '2'..'7' ); my %bits2char; my @char2bits; for (0..$#syms) { my $sym = $syms[$_]; my $bin = sprintf('%05b', $_); $char2bits[ ord lc $sym ] = $bin; $char2bits[ ord uc $sym ] = $bin; do { $bits2char{$bin} = $sym; } while $bin =~ s/(.+)0\z/$1/s; } sub encode_base32_pre58($) { length($_[0]) == bytes::length($_[0]) or Carp::croak('Data contains non-bytes'); my $str = unpack('B*', $_[0]); if (length($str) < 8*1024) { return join '', @bits2char{ $str =~ /.{1,5}/g }; } else { # Slower, but uses less memory $str =~ s/(.{5})/$bits2char{$1}/sg; return $str; } } sub encode_base32_perl58($) { $_[0] =~ tr/\x00-\xFF//c and Carp::croak('Data contains non-bytes'); my $str = unpack('B*', $_[0]); if (length($str) < 8*1024) { return join '', @bits2char{ unpack '(a5)*', $str }; } else { # Slower, but uses less memory $str =~ s/(.{5})/$bits2char{$1}/sg; return $str; } } sub decode_base32_pre58($) { ( length($_[0]) != bytes::length($_[0]) || $_[0] =~ tr/a-zA-Z2-7//c ) and Carp::croak('Data contains non-base32 characters'); my $str; if (length($_[0]) < 8*1024) { $str = join '', @char2bits[ unpack 'C*', $_[0] ]; } else { # Slower, but uses less memory ($str = $_[0]) =~ s/(.)/$char2bits[ord($1)]/sg; } my $padding = length($str) % 8; $padding < 5 or Carp::croak('Length of data invalid'); $str =~ s/0{$padding}\z// or Carp::croak('Padding bits at the end of output buffer are not all zero'); return pack('B*', $str); } sub decode_base32_perl58($) { $_[0] =~ tr/a-zA-Z2-7//c and Carp::croak('Data contains non-base32 characters'); my $str; if (length($_[0]) < 8*1024) { $str = join '', @char2bits[ unpack 'C*', $_[0] ]; } else { # Slower, but uses less memory ($str = $_[0]) =~ s/(.)/$char2bits[ord($1)]/sg; } my $padding = length($str) % 8; $padding < 5 or Carp::croak('Length of data invalid'); $str =~ s/0{$padding}\z// or Carp::croak('Padding bits at the end of output buffer are not all zero'); return pack('B*', $str); } if ($] lt '5.800000') { require bytes; *encode_base32 = \&encode_base32_pre58; *decode_base32 = \&decode_base32_pre58; } else { *encode_base32 = \&encode_base32_perl58; *decode_base32 = \&decode_base32_perl58; } 1; __END__ =head1 NAME Convert::Base32 - Encoding and decoding of base32 strings =head1 SYNOPSIS use Convert::Base32; $encoded = encode_base32("\x3a\x27\x0f\x93"); $decoded = decode_base32($encoded); =head1 DESCRIPTION This module provides functions to convert string from / to Base32 encoding, specified in RACE internet-draft. The Base32 encoding is designed to encode non-ASCII characters in DNS-compatible host name parts. See http://tools.ietf.org/html/draft-ietf-idn-race-03 for more details. =head1 FUNCTIONS Following functions are provided; like C, they are in B<@EXPORT> array. See L for details. =over 4 =item encode_base32($str) Encode data by calling the encode_base32() function. This function takes a string of bytes to encode and returns the encoded base32 string. =item decode_base32($str) Decode a base32 string by calling the decode_base32() function. This function takes a string to decode and returns the decoded string. This function might throw the exceptions such as "Data contains non-base32 characters", "Length of data invalid" and "Padding bits at the end of output buffer are not all zero". decode_base32 differs from the specification in that upper case letters are treated as their lower case equivalent rather than producing an error. =head1 AUTHOR Tatsuhiko Miyagawa Eric Brine This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =head1 SEE ALSO http://www.ietf.org/internet-drafts/draft-ietf-idn-race-03.txt, L, L. =cut Convert-Base32-0.05/t/0000700000175300001440000000000011327375713013201 5ustar ericusersConvert-Base32-0.05/t/00_enc_dec.t0000644000175300001440000000251511321213016015237 0ustar ericusersuse strict; use warnings; use Test::More; use Convert::Base32 qw( encode_base32 decode_base32 ); my @tests = ( [ "\x3a\x27\x0f\x93" => 'hitq7ey' ], [ "\x3a\x27\x0f\x93\x2a" => 'hitq7ezk' ], ); { my @syms = ( 'a'..'z', '2'..'7' ); my $e; my $d; for (0..$#syms) { my $sym = $syms[$_]; $e .= "aaaaaaa$sym"; $d .= pack('C5', 0,0,0,0,$_); } push @tests, [ $d, $e ]; } { my $d = join '', map chr, 0..255; my $e = join '', qw( aaaqeayeaudaocajbifqydiob4ibceqtcqkrmfyy denbwha5dypsaijcemsckjrhfausukzmfuxc6mbr giztinjwg44dsor3hq6t4p2aifbegrcfizduqskk jnge2tspkbiveu2ukvlfowczljnvyxk6l5qgcytd mrswmz3infvgw3dnnzxxa4lson2hk5txpb4xu634 pv7h7aebqkbyjbmgq6eitculrsgy5d4qsgjjhfev s2lzrgm2tooj3hu7ucq2fi5euwtkpkfjvkv2zlno v6yldmvtws23nn5yxg5lxpf5x274bqocypcmlrwh zde4vs6mzxhm7ugr2lj5jvow27mntww33to55x7a 4hrohzhf43t6r2pk5pwo33xp6dy7f47u6x3pp6hz 7l57z7p674 ); push @tests, [ $d, $e ]; } plan tests => 3 * @tests; sub hexify { my $s = $_[0]; $s =~ s/(.)/ sprintf '%02X ', ord($1) /seg; chop $s; return $s; } for (@tests) { my $d = $_->[0]; my $e = lc($_->[1]); my $E = uc($_->[1]); is encode_base32($d), $e, "encode ".hexify($d); is hexify(decode_base32($e)), hexify($d), "decode $e"; is hexify(decode_base32($E)), hexify($d), "decode $E"; } Convert-Base32-0.05/t/01_error_checks.t0000644000175300001440000000346611327370140016347 0ustar ericusersuse strict; use warnings; use Test::More; use Test::Exception; use Convert::Base32 qw( encode_base32 decode_base32 ); my @tests = ( [ '' => 0 ], [ a => 'bad len' ], [ ae => 1 ], [ ac => 'bad padding' ], [ ab => 'bad padding' ], [ aaa => 'bad len' ], [ aaaq => 2 ], [ aaai => 'bad padding' ], [ aaae => 'bad padding' ], [ aaac => 'bad padding' ], [ aaab => 'bad padding' ], [ aaaac => 3 ], [ aaaab => 'bad padding' ], [ aaaaaa => 'bad len' ], [ aaaaaai => 4 ], [ aaaaaae => 'bad padding' ], [ aaaaaac => 'bad padding' ], [ aaaaaab => 'bad padding' ], ); plan tests => 2*@tests + 2*512; for (@tests) { my ($e, $dlen) = @$_; if ($dlen =~ /^[0-9]+\z/) { lives_and { is length(decode_base32($e)), $dlen } "$e (ok)"; } else { dies_ok { decode_base32($e) } "$e ($dlen)"; } } for (@tests) { my ($e, $dlen) = @$_; $e = "aaaaaaaa$e"; if ($dlen =~ /^[0-9]+\z/) { lives_and { is length(decode_base32($e)), 5+$dlen } "$e (ok)"; } else { dies_ok { decode_base32($e) } "$e ($dlen)"; } } my %syms = map { $_ => 1 } ( 'a'..'z', 'A'..'Z', '2'..'7' ); for my $o (0..511) { my $c = chr($o); if ( ( $o >= ord('a') && $o <= ord('z') ) || ( $o >= ord('A') && $o <= ord('Z') ) || ( $o >= ord('2') && $o <= ord('7') ) ) { lives_ok { decode_base32("aaaaaaa$c") } sprintf('decode U+%04X (ok)', $o); } else { dies_ok { decode_base32("aaaaaaa$c") } sprintf('decode U+%04X (bad)', $o) } if ($o < 256) { lives_ok { encode_base32($c) } sprintf('encode U+%04X (ok)', $o); } else { dies_ok { encode_base32($c) } sprintf('encode U+%04X (bad)', $o); } } Convert-Base32-0.05/META.yml0000600000175300001440000000102211327375713014204 0ustar ericusers--- #YAML:1.0 name: Convert-Base32 version: 0.05 abstract: Encoding and decoding of base32 strings author: [] license: unknown distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: Test::Exception: 0 Test::More: 0 requires: {} no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Convert-Base32-0.05/Changes0000644000175300001440000000102111321443520014217 0ustar ericusersRevision history for Perl extension Convert::Base32. 0.04 Thu Jan 07 2009 - Documentation fixes 0.03 Wed Jan 06 2009 - Allow uppercase letters in input [RT#53015] - Fixed bug in error check [RT#53014] - Added check for invalid input to encode_base32. - Improved tests. - Increased speed of decoding. - Increased speed of encoding. 0.02 Wed Jul 18 17:29:25 JST 2001 - Separated from Convert::RACE - Went on CPAN 0.01 Wed Jul 18 17:26:08 2001 - original version; created by h2xs 1.19 Convert-Base32-0.05/Makefile.PL0000644000175300001440000000106411327366640014722 0ustar ericusersuse strict; use warnings; use ExtUtils::MakeMaker qw( WriteMakefile ); my %build_requires = ( 'Test::More' => 0, # For testing. 'Test::Exception' => 0, # For testing. ); WriteMakefile( NAME => 'Convert::Base32', VERSION_FROM => 'lib/Convert/Base32.pm', ABSTRACT_FROM => 'lib/Convert/Base32.pm', ( $ExtUtils::MakeMaker::VERSION lt '6.56' ? ( PREREQ_PM => \%build_requires ) : ( BUILD_REQUIRES => \%build_requires ) ), dist => { COMPRESS => 'gzip -9f' }, clean => { FILES => 'Convert-Base32-*' }, ); Convert-Base32-0.05/README0000644000175300001440000000302707325244550013627 0ustar ericusersNAME Convert::Base32 - Encoding and decoding of base32 strings SYNOPSIS use Convert::Base32; $encoded = encode_base32("\x3a\x27\x0f\x93"); $decoded = decode_base32($encoded); DESCRIPTION This module provides functions to convert string from / to Base32 encoding, specified in RACE internet-draft. The Base32 encoding is designed to encode non-ASCII characters in DNS-compatible host name parts. See http://www.ietf.org/internet-drafts/draft-ietf-idn-race-03.txt for more details. FUNCTIONS Following functions are provided; like "MIME::Base64", they are in @EXPORT array. See the Exporter manpage for details. encode_base32($str) Encode data by calling the encode_base32() function. This function takes a string to encode and returns the encoded base32 string. decode_base32($str) Decode a base32 string by calling the decode_base32() function. This function takes a string to decode and returns the decoded string. This function might throw the exceptions such as "Data contains non-base32 characters", "Length of data invalid" and "PADDING number of bits at the end of output buffer are not all zero". AUTHOR Tatsuhiko Miyagawa This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO http://www.ietf.org/internet-drafts/draft-ietf-idn-race-03.txt, the MIME::Base64 manpage, the Convert::RACE manpage.