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.

115 lines
3.0 KiB

  1. #
  2. # $Id: QuotedPrint.pm,v 2.3 1997/12/02 10:24:27 aas Exp $
  3. package MIME::QuotedPrint;
  4. =head1 NAME
  5. MIME::QuotedPrint - Encoding and decoding of quoted-printable strings
  6. =head1 SYNOPSIS
  7. use MIME::QuotedPrint;
  8. $encoded = encode_qp($decoded);
  9. $decoded = decode_qp($encoded);
  10. =head1 DESCRIPTION
  11. This module provides functions to encode and decode strings into the
  12. Quoted-Printable encoding specified in RFC 2045 - I<MIME (Multipurpose
  13. Internet Mail Extensions)>. The Quoted-Printable encoding is intended
  14. to represent data that largely consists of bytes that correspond to
  15. printable characters in the ASCII character set. Non-printable
  16. characters (as defined by english americans) are represented by a
  17. triplet consisting of the character "=" followed by two hexadecimal
  18. digits.
  19. The following functions are provided:
  20. =over 4
  21. =item encode_qp($str)
  22. This function will return an encoded version of the string given as
  23. argument.
  24. Note that encode_qp() does not change newlines C<"\n"> to the CRLF
  25. sequence even though this might be considered the right thing to do
  26. (RFC 2045 (Q-P Rule #4)).
  27. =item decode_qp($str);
  28. This function will return the plain text version of the string given
  29. as argument.
  30. =back
  31. If you prefer not to import these routines into your namespace you can
  32. call them as:
  33. use MIME::QuotedPrint ();
  34. $encoded = MIME::QuotedPrint::encode($decoded);
  35. $decoded = MIME::QuotedPrint::decode($encoded);
  36. =head1 COPYRIGHT
  37. Copyright 1995-1997 Gisle Aas.
  38. This library is free software; you can redistribute it and/or
  39. modify it under the same terms as Perl itself.
  40. =cut
  41. use strict;
  42. use vars qw(@ISA @EXPORT $VERSION);
  43. require Exporter;
  44. @ISA = qw(Exporter);
  45. @EXPORT = qw(encode_qp decode_qp);
  46. $VERSION = sprintf("%d.%02d", q$Revision: 2.3 $ =~ /(\d+)\.(\d+)/);
  47. sub encode_qp ($)
  48. {
  49. my $res = shift;
  50. $res =~ s/([^ \t\n!-<>-~])/sprintf("=%02X", ord($1))/eg; # rule #2,#3
  51. $res =~ s/([ \t]+)$/
  52. join('', map { sprintf("=%02X", ord($_)) }
  53. split('', $1)
  54. )/egm; # rule #3 (encode whitespace at eol)
  55. # rule #5 (lines must be shorter than 76 chars, but we are not allowed
  56. # to break =XX escapes. This makes things complicated :-( )
  57. my $brokenlines = "";
  58. $brokenlines .= "$1=\n"
  59. while $res =~ s/(.*?^[^\n]{73} (?:
  60. [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n
  61. |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n
  62. | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n
  63. ))//xsm;
  64. "$brokenlines$res";
  65. }
  66. sub decode_qp ($)
  67. {
  68. my $res = shift;
  69. $res =~ s/[ \t]+?(\r?\n)/$1/g; # rule #3 (trailing space must be deleted)
  70. $res =~ s/=\r?\n//g; # rule #5 (soft line breaks)
  71. $res =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge;
  72. $res;
  73. }
  74. # Set up aliases so that these functions also can be called as
  75. #
  76. # MIME::QuotedPrint::encode();
  77. # MIME::QuotedPrint::decode();
  78. *encode = \&encode_qp;
  79. *decode = \&decode_qp;
  80. 1;