Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.7 KiB

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