Line # Revision Author
1 296 ahitrov package Convert::UU;
2
3 use strict;
4 BEGIN {require 5.004} # m//gc
5 use vars qw($VERSION @ISA @EXPORT_OK);
6 use Carp 'croak';
7
8 require Exporter;
9
10 @ISA = qw(Exporter);
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14 @EXPORT_OK = qw(
15 uudecode uuencode
16 );
17 $VERSION = '0.52';
18
19 #
20 # From comp.lang.perl 3/1/95.
21 # Posted by Hans Mulder (hansm@wsinti05.win.tue.nl)
22 #
23
24 sub uuencode {
25 croak("Usage: uuencode( {string|filehandle} [,filename] [, mode] )")
26 unless(@_ >= 1 && @_ <= 3);
27
28 my($in,$file,$mode) = @_;
29 $mode ||= "644";
30 $file ||= "uuencode.uu";
31
32 my($chunk,@result,$r);
33 if (
34 ref($in) eq 'IO::Handle' or
35 ref(\$in) eq "GLOB" or
36 ref($in) eq "GLOB" or
37 ref($in) eq 'FileHandle'
38 ) {
39 # local $^W = 0; # Why did I get use of undefined value here ?
40 binmode($in);
41 local $/;
42 $in = <$in>;
43 }
44 pos($in)=0;
45 while ($in =~ m/\G(.{1,45})/sgc) {
46 push @result, uuencode_chunk($1);
47 }
48 push @result, "`\n";
49 join "", "begin $mode $file\n", @result, "end\n";
50 }
51
52 sub uuencode_chunk {
53 my($string) = shift;
54 # for the Mac?
55 # my($mod3) = length($string) % 3;
56 # $string .= "\0", $mod3 -= 3 if $mod3;
57 my $encoded_string = pack("u", $string); # unix uuencode
58 # for the Mac?
59 # $encoded_string =~ s/.//; # remove length byte
60 # chop($encoded_string); # remove trailing \n
61 # $encoded_string =~ tr#`!-_#A-Za-z0-9+/#; # tr to mime alphabet
62 # substr($encoded_string, $mod3) =~ tr/A/=/; # adjust padding
63 $encoded_string;
64 }
65
66 sub uudecode {
67 croak("Usage: uudecode( {string|filehandle|array ref}) ")
68 unless(@_ == 1);
69 my($in) = @_;
70
71 my(@result,$file,$mode);
72 $mode = $file = "";
73 if (
74 ref($in) eq 'IO::Handle' or
75 ref(\$in) eq "GLOB" or
76 ref($in) eq "GLOB" or
77 ref($in) eq 'FileHandle'
78 ) {
79 local($\) = "\n";
80 binmode($in);
81 while (<$in>) {
82 if ($file eq "" and !$mode){
83 ($mode,$file) = ($1, $2) if /^begin\s+(\d+)\s+(.+)$/ ;
84 next;
85 }
86 last if /^end/;
87 push @result, uudecode_chunk($_);
88 }
89 } elsif (ref(\$in) eq "SCALAR") {
90 while ($in =~ m/\G(.*?(\n|\r|\r\n|\n\r))/gc) {
91 my $line = $1;
92 if ($file eq "" and !$mode){
93 ($mode,$file) = $line =~ /^begin\s+(\d+)\s+(.+)$/ ;
94 next;
95 }
96 next if $file eq "" and !$mode;
97 last if $line =~ /^end/;
98 push @result, uudecode_chunk($line);
99 }
100 } elsif (ref($in) eq "ARRAY") {
101 my $line;
102 foreach $line (@$in) {
103 if ($file eq "" and !$mode){
104 ($mode,$file) = $line =~ /^begin\s+(\d+)\s+(.+)$/ ;
105 next;
106 }
107 next if $file eq "" and !$mode;
108 last if $line =~ /^end/;
109 push @result, uudecode_chunk($line);
110 }
111 }
112 wantarray ? (join("",@result),$file,$mode) : join("",@result);
113 }
114
115 sub uudecode_chunk {
116 my($chunk) = @_;
117 # return "" if $chunk =~ /^(--|\#|CREATED)/; # the "#" was an evil
118 # bug: a "#" in column
119 # one is legal!
120 return "" if $chunk =~ /^(?:--|CREATED)/;
121 my $string = substr($chunk,0,int((((ord($chunk) - 32) & 077) + 2) / 3)*4+1);
122 # warn "DEBUG: string [$string]";
123 # my $return = unpack("u", $string);
124 # warn "DEBUG: return [$return]";
125 # $return;
126
127 my $ret = unpack("u", $string);
128 defined $ret ? $ret : "";
129 }
130
131 1;
132
133 __END__
134
135 =head1 NAME
136
137 Convert::UU, uuencode, uudecode - Perl module for uuencode and uudecode
138
139 =head1 SYNOPSIS
140
141 use Convert::UU qw(uudecode uuencode);
142 $encoded_string = uuencode($string,[$filename],[$mode]);
143 ($string,$filename,$mode) = uudecode($string);
144 $string = uudecode($string); # in scalar context
145
146 =head1 DESCRIPTION
147
148 uuencode() takes as the first argument a string that is to be
149 uuencoded. Note, that it is the string that is encoded, not a
150 filename. Alternatively a filehandle may be passed that must be opened
151 for reading. It returns the uuencoded string including C<begin> and
152 C<end>. Second and third argument are optional and specify filename and
153 mode. If unspecified these default to "uuencode.uu" and 644.
154
155 uudecode() takes a string as argument which will be uudecoded. If the
156 argument is a filehandle this handle will be read instead. If it is a
157 reference to an ARRAY, the elements are treated like lines that form a
158 string. Leading and trailing garbage will be ignored. The function
159 returns the uudecoded string for the first begin/end pair. In array
160 context it returns an array whose first element is the uudecoded
161 string, the second is the filename and the third is the mode.
162
163 =head1 EXPORT
164
165 Both uudecode and uuencode are in @EXPORT_OK.
166
167 =head1 AUTHOR
168
169 Andreas Koenig E<lt>andreas.koenig@anima.deE<gt>. With code integrated
170 that was posted to USENET from Hans Mulder and Randal L. Schwartz.
171
172 =head1 SEE ALSO
173
174 puuencode(1), puudecode(1) for examples of how to use this module.
175
176 =cut