/[pdpsoft]/nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.10-1/base64.pm
ViewVC logotype

Annotation of /nl.nikhef.pdp.fetchcrl/tags/fetch-crl-3.0.10-1/base64.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1768 - (hide annotations) (download) (as text)
Fri Jun 11 19:45:08 2010 UTC (11 years, 5 months ago) by davidg
Original Path: trunk/fetchcrl/base64.pm
File MIME type: application/x-perl
File size: 1326 byte(s)
rewrite of this helper module to make sure it its with the copyright

1 davidg 1758 #
2 davidg 1768 # Library inspired by the Perl 4 code from base64.pl by A. P. Barrett
3     # <barrett@ee.und.ac.za>, October 1993, and subsequent changes by
4     # Earl Hood <earl@earlhood.com> to use MIME::Base64 if available.
5 davidg 1758 #
6    
7     package base64;
8    
9 davidg 1768 my $use_MIMEBase64 = eval { require MIME::Base64; };
10 davidg 1758
11     sub b64decode
12     {
13 davidg 1768 return &MIME::Base64::decode_base64(@_) if $use_MIMEBase64;
14 davidg 1758
15 davidg 1768 local($^W) = 0;
16     use integer; # should be faster and more accurate
17 davidg 1758
18 davidg 1768 ( my $str = shift ) =~ tr|A-Za-z0-9+=/||cd;
19     $str =~ s/=+$//;
20     $str =~ tr|A-Za-z0-9+/| -_|; # collapse base64 unto continuous set of chars
21     # (by convention the uuencode set, for unpack)
22 davidg 1758
23 davidg 1768 return "" unless $str;
24     return unpack("u", join('', map( chr(32 + length($_)*3/4) . $_,
25     $str =~ /(.{1,60})/gs) ) );
26 davidg 1758 }
27    
28     sub b64encode
29     {
30 davidg 1768 return &MIME::Base64::encode_base64(@_) if $use_MIMEBase64;
31 davidg 1758
32     local ($_) = shift;
33 davidg 1768 local($^W) = 0;
34     use integer; # should be faster and more accurate
35 davidg 1758
36 davidg 1768 my $result = pack("u", $_[0]);
37     $result =~ s/^.//mg;
38     $result =~ s/\n//g;
39 davidg 1758
40 davidg 1768 $result =~ tr| -_|A-Za-z0-9+/|;
41     my $padding = (3 - length($_[0]) % 3) % 3;
42 davidg 1758
43 davidg 1768 $result =~ s/.{$padding}$/'=' x $padding/e if $padding;
44     if (length $eol) {
45     $result =~ s/(.{1,76})/$1$eol/g;
46 davidg 1758 }
47 davidg 1768 return $result;
48 davidg 1758 }
49    
50     1;

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28