Commit | Line | Data |
6fba102d |
1 | # |
2 | # $Id: QuotedPrint.pm,v 2.3 1997/12/02 10:24:27 aas Exp $ |
3 | |
4 | package MIME::QuotedPrint; |
5 | |
6 | =head1 NAME |
7 | |
8 | MIME::QuotedPrint - Encoding and decoding of quoted-printable strings |
9 | |
10 | =head1 SYNOPSIS |
11 | |
12 | use MIME::QuotedPrint; |
13 | |
14 | $encoded = encode_qp($decoded); |
15 | $decoded = decode_qp($encoded); |
16 | |
17 | =head1 DESCRIPTION |
18 | |
19 | This module provides functions to encode and decode strings into the |
20 | Quoted-Printable encoding specified in RFC 2045 - I<MIME (Multipurpose |
21 | Internet Mail Extensions)>. The Quoted-Printable encoding is intended |
22 | to represent data that largely consists of bytes that correspond to |
23 | printable characters in the ASCII character set. Non-printable |
24 | characters (as defined by english americans) are represented by a |
25 | triplet consisting of the character "=" followed by two hexadecimal |
26 | digits. |
27 | |
28 | The following functions are provided: |
29 | |
30 | =over 4 |
31 | |
32 | =item encode_qp($str) |
33 | |
34 | This function will return an encoded version of the string given as |
35 | argument. |
36 | |
37 | Note that encode_qp() does not change newlines C<"\n"> to the CRLF |
38 | sequence even though this might be considered the right thing to do |
39 | (RFC 2045 (Q-P Rule #4)). |
40 | |
41 | =item decode_qp($str); |
42 | |
43 | This function will return the plain text version of the string given |
44 | as argument. |
45 | |
46 | =back |
47 | |
48 | |
49 | If you prefer not to import these routines into your namespace you can |
50 | call them as: |
51 | |
52 | use MIME::QuotedPrint (); |
53 | $encoded = MIME::QuotedPrint::encode($decoded); |
54 | $decoded = MIME::QuotedPrint::decode($encoded); |
55 | |
56 | =head1 COPYRIGHT |
57 | |
58 | Copyright 1995-1997 Gisle Aas. |
59 | |
60 | This library is free software; you can redistribute it and/or |
61 | modify it under the same terms as Perl itself. |
62 | |
63 | =cut |
64 | |
65 | use strict; |
66 | use vars qw(@ISA @EXPORT $VERSION); |
95635e5f |
67 | if (ord('A') == 193) { # on EBCDIC machines we need translation help |
46891979 |
68 | require Encode; |
95635e5f |
69 | } |
6fba102d |
70 | |
71 | require Exporter; |
72 | @ISA = qw(Exporter); |
73 | @EXPORT = qw(encode_qp decode_qp); |
74 | |
75 | $VERSION = sprintf("%d.%02d", q$Revision: 2.3 $ =~ /(\d+)\.(\d+)/); |
76 | |
6fba102d |
77 | sub encode_qp ($) |
78 | { |
79 | my $res = shift; |
95635e5f |
80 | # Do not mention ranges such as $res =~ s/([^ \t\n!-<>-~])/sprintf("=%02X", ord($1))/eg; |
81 | # since that will not even compile on an EBCDIC machine (where ord('!') > ord('<')). |
82 | if (ord('A') == 193) { # EBCDIC style machine |
83 | if (ord('[') == 173) { |
84 | $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp1047',$1))))/eg; # rule #2,#3 |
85 | $res =~ s/([ \t]+)$/ |
86 | join('', map { sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp1047',$_)))) } |
87 | split('', $1) |
88 | )/egm; # rule #3 (encode whitespace at eol) |
89 | } |
90 | elsif (ord('[') == 187) { |
91 | $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('posix-bc',$1))))/eg; # rule #2,#3 |
92 | $res =~ s/([ \t]+)$/ |
93 | join('', map { sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('posix-bc',$_)))) } |
94 | split('', $1) |
95 | )/egm; # rule #3 (encode whitespace at eol) |
96 | } |
97 | elsif (ord('[') == 186) { |
98 | $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp37',$1))))/eg; # rule #2,#3 |
99 | $res =~ s/([ \t]+)$/ |
100 | join('', map { sprintf("=%02X", ord(Encode::encode('iso-8859-1',Encode::decode('cp37',$_)))) } |
101 | split('', $1) |
102 | )/egm; # rule #3 (encode whitespace at eol) |
103 | } |
104 | } |
105 | else { # ASCII style machine |
106 | $res =~ s/([^ \t\n!"#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~])/sprintf("=%02X", ord($1))/eg; # rule #2,#3 |
107 | $res =~ s/([ \t]+)$/ |
108 | join('', map { sprintf("=%02X", ord($_)) } |
109 | split('', $1) |
110 | )/egm; # rule #3 (encode whitespace at eol) |
111 | } |
6fba102d |
112 | |
113 | # rule #5 (lines must be shorter than 76 chars, but we are not allowed |
114 | # to break =XX escapes. This makes things complicated :-( ) |
115 | my $brokenlines = ""; |
116 | $brokenlines .= "$1=\n" |
117 | while $res =~ s/(.*?^[^\n]{73} (?: |
118 | [^=\n]{2} (?! [^=\n]{0,1} $) # 75 not followed by .?\n |
119 | |[^=\n] (?! [^=\n]{0,2} $) # 74 not followed by .?.?\n |
120 | | (?! [^=\n]{0,3} $) # 73 not followed by .?.?.?\n |
121 | ))//xsm; |
122 | |
123 | "$brokenlines$res"; |
124 | } |
125 | |
126 | |
127 | sub decode_qp ($) |
128 | { |
129 | my $res = shift; |
130 | $res =~ s/[ \t]+?(\r?\n)/$1/g; # rule #3 (trailing space must be deleted) |
131 | $res =~ s/=\r?\n//g; # rule #5 (soft line breaks) |
95635e5f |
132 | if (ord('A') == 193) { # EBCDIC style machine |
133 | if (ord('[') == 173) { |
134 | $res =~ s/=([\da-fA-F]{2})/Encode::encode('cp1047',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge; |
135 | } |
136 | elsif (ord('[') == 187) { |
137 | $res =~ s/=([\da-fA-F]{2})/Encode::encode('posix-bc',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge; |
138 | } |
139 | elsif (ord('[') == 186) { |
140 | $res =~ s/=([\da-fA-F]{2})/Encode::encode('cp37',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge; |
141 | } |
142 | } |
143 | else { # ASCII style machine |
144 | $res =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge; |
145 | } |
6fba102d |
146 | $res; |
147 | } |
148 | |
149 | # Set up aliases so that these functions also can be called as |
150 | # |
151 | # MIME::QuotedPrint::encode(); |
152 | # MIME::QuotedPrint::decode(); |
153 | |
154 | *encode = \&encode_qp; |
155 | *decode = \&decode_qp; |
156 | |
7d85a32c |
157 | # Methods for use as a PerlIO layer object |
158 | |
159 | sub PUSHED |
160 | { |
161 | my ($class,$mode) = @_; |
162 | # When writing we buffer the data |
163 | my $write = ''; |
164 | return bless \$write,$class; |
165 | } |
166 | |
167 | sub FILL |
168 | { |
169 | my ($obj,$fh) = @_; |
170 | my $line = <$fh>; |
171 | return (defined $line) ? decode_qp($line) : undef; |
7d85a32c |
172 | } |
173 | |
174 | sub WRITE |
175 | { |
176 | my ($obj,$buf,$fh) = @_; |
177 | $$obj .= encode_qp($buf); |
178 | return length($buf); |
179 | } |
180 | |
181 | sub FLUSH |
182 | { |
183 | my ($obj,$fh) = @_; |
184 | print $fh $$obj or return -1; |
185 | $$obj = ''; |
186 | return 0; |
187 | } |
188 | |
189 | |
6fba102d |
190 | 1; |