home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Non-RPC / !Perl / lib / zip / MIME / Base64.pm next >
Text File  |  1998-07-15  |  5KB  |  171 lines

  1. #
  2. # $Id: Base64.pm,v 2.8 1998/07/15 11:04:46 aas Exp $
  3.  
  4. package MIME::Base64;
  5.  
  6. =head1 NAME
  7.  
  8. MIME::Base64 - Encoding and decoding of base64 strings
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.  use MIME::Base64;
  13.  
  14.  $encoded = encode_base64('Aladdin:open sesame');
  15.  $decoded = decode_base64($encoded);
  16.  
  17. =head1 DESCRIPTION
  18.  
  19. This module provides functions to encode and decode strings into the
  20. Base64 encoding specified in RFC 2045 - I<MIME (Multipurpose Internet
  21. Mail Extensions)>. The Base64 encoding is designed to represent
  22. arbitrary sequences of octets in a form that need not be humanly
  23. readable. A 65-character subset ([A-Za-z0-9+/=]) of US-ASCII is used,
  24. enabling 6 bits to be represented per printable character.
  25.  
  26. The following functions are provided:
  27.  
  28. =over 4
  29.  
  30. =item encode_base64($str, [$eol])
  31.  
  32. Encode data by calling the encode_base64() function.  The first
  33. argument is the string to encode.  The second argument is the line
  34. ending sequence to use (it is optional and defaults to C<"\n">).  The
  35. returned encoded string is broken into lines of no more than 76
  36. characters each and it will end with $eol unless it is empty.  Pass an
  37. empty string as second argument if you do not want the encoded string
  38. broken into lines.
  39.  
  40. =item decode_base64($str)
  41.  
  42. Decode a base64 string by calling the decode_base64() function.  This
  43. function takes a single argument which is the string to decode and
  44. returns the decoded data.  Any character not part of the legal Base64
  45. character set is ignored.
  46.  
  47. =back
  48.  
  49. If you prefer not to import these routines into your namespace you can
  50. call them as:
  51.  
  52.     use MIME::Base64 ();
  53.     $encoded = MIME::Base64::encode($decoded);
  54.     $decoded = MIME::Base64::decode($encoded);
  55.  
  56. =head1 EXAMPLES
  57.  
  58. If you want to encode a large file, you should encode it in chunks
  59. that are a multiple of 57 bytes.  This ensures that the base64 lines
  60. line up and that you do not end up with padding in the middle. 57
  61. bytes of data fills one complete base64 line (76 == 57*4/3):
  62.  
  63.    use MIME::Base64 qw(encode_base64);
  64.  
  65.    open(FILE, "/var/log/wtmp") or die "$!";
  66.    while (read(FILE, $buf, 60*57)) {
  67.        print encode_base64($buf);
  68.    }
  69.  
  70. or if you know you have enough memory
  71.  
  72.    use MIME::Base64 qw(encode_base64);
  73.    local($/) = undef;  # slurp
  74.    print encode_base64(<STDIN>);
  75.  
  76. =head1 COPYRIGHT
  77.  
  78. Copyright 1995-1998 Gisle Aas.
  79.  
  80. This library is free software; you can redistribute it and/or
  81. modify it under the same terms as Perl itself.
  82.  
  83. Distantly based on LWP::Base64 written by Martijn Koster
  84. <m.koster@nexor.co.uk> and Joerg Reichelt <j.reichelt@nexor.co.uk> and
  85. code posted to comp.lang.perl <3pd2lp$6gf@wsinti07.win.tue.nl> by Hans
  86. Mulder <hansm@wsinti07.win.tue.nl>
  87.  
  88. The XS implementation use code from metamail.  Copyright 1991 Bell
  89. Communications Research, Inc. (Bellcore)
  90.  
  91. =cut
  92.  
  93. use strict;
  94. use vars qw(@ISA @EXPORT $VERSION $OLD_CODE);
  95.  
  96. require Exporter;
  97. require DynaLoader;
  98. @ISA = qw(Exporter DynaLoader);
  99. @EXPORT = qw(encode_base64 decode_base64);
  100.  
  101. $VERSION = '2.06';
  102.  
  103. eval { bootstrap MIME::Base64 $VERSION; };
  104. if ($@) {
  105.     # can't bootstrap XS implementation, use perl implementation
  106.     *encode_base64 = \&old_encode_base64;
  107.     *decode_base64 = \&old_decode_base64;
  108.  
  109.     $OLD_CODE = $@;
  110.     #warn $@ if $^W;
  111. }
  112.  
  113. # Historically this module has been implemented as pure perl code.
  114. # The XS implementation runs about 20 times faster, but the Perl
  115. # code might be more portable, so it is still here.
  116.  
  117. use integer;
  118.  
  119. sub old_encode_base64 ($;$)
  120. {
  121.     my $res = "";
  122.     my $eol = $_[1];
  123.     $eol = "\n" unless defined $eol;
  124.     pos($_[0]) = 0;                          # ensure start at the beginning
  125.     while ($_[0] =~ /(.{1,45})/gs) {
  126.     $res .= substr(pack('u', $1), 1);
  127.     chop($res);
  128.     }
  129.     $res =~ tr|` -_|AA-Za-z0-9+/|;               # `# help emacs
  130.     # fix padding at the end
  131.     my $padding = (3 - length($_[0]) % 3) % 3;
  132.     $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
  133.     # break encoded string into lines of no more than 76 characters each
  134.     if (length $eol) {
  135.     $res =~ s/(.{1,76})/$1$eol/g;
  136.     }
  137.     $res;
  138. }
  139.  
  140.  
  141. sub old_decode_base64 ($)
  142. {
  143.     local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123]
  144.  
  145.     my $str = shift;
  146.     my $res = "";
  147.  
  148.     $str =~ tr|A-Za-z0-9+=/||cd;            # remove non-base64 chars
  149.     if (length($str) % 4) {
  150.     require Carp;
  151.     Carp::carp("Length of base64 data not a multiple of 4")
  152.     }
  153.     $str =~ s/=+$//;                        # remove padding
  154.     $str =~ tr|A-Za-z0-9+/| -_|;            # convert to uuencoded format
  155.     while ($str =~ /(.{1,60})/gs) {
  156.     my $len = chr(32 + length($1)*3/4); # compute length byte
  157.     $res .= unpack("u", $len . $1 );    # uudecode
  158.     }
  159.     $res;
  160. }
  161.  
  162. # Set up aliases so that these functions also can be called as
  163. #
  164. #    MIME::Base64::encode();
  165. #    MIME::Base64::decode();
  166.  
  167. *encode = \&encode_base64;
  168. *decode = \&decode_base64;
  169.  
  170. 1;
  171.