2 # Copyright 1994 by Lance Ellinghouse
3 # Cathedral City, California Republic, United States of America.
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose and without fee is hereby granted,
7 # provided that the above copyright notice appear in all copies and that
8 # both that copyright notice and this permission notice appear in
9 # supporting documentation, and that the name of Lance Ellinghouse
10 # not be used in advertising or publicity pertaining to distribution
11 # of the software without specific, written prior permission.
12 # LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
13 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
14 # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
15 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
18 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 # This file implements the UUencode and UUdecode functions.
22 # encode(filename, mode, in_file, out_file)
23 # decode(filename, mode, in_file)
24 # decode(in_file, out_file)
27 # encode a single char to always be printable
29 if type(ch
) == type(''):
30 a
= ch
[:1] # only 1 char
32 raise ValueError, 'need to pass in at least 1 char'
34 elif type(ch
) == type(0):
37 raise TypeError, 'must pass in an integer or single character'
38 return chr((a
& 077) + ord(' '))
40 # input 3 chars, output 4 encoded chars
43 raise ValueError, 'can only accept strings of 3 chars'
52 c2
= (p0
<< 4) & 060 |
(p1
>> 4) & 017
53 c3
= (p1
<< 2) & 074 |
(p2
>> 6) & 03
61 # pass in 45 bytes max, returns 62 bytes encoded
64 raise ValueError, 'cannot handle more than 45 chars at once'
69 rtn
= rtn
+ _outenc(str[i
:(i
+3)])
74 # encode a fileobject and write out to a file object
75 def encode(filename
, mode
, in_file
, out_file
):
76 out_file
.write('begin %o %s\n' % ((mode
&0777),filename
))
77 str = in_file
.read(45)
79 out_file
.write(_encode(str))
80 str = in_file
.read(45)
81 out_file
.write(' \nend\n')
84 # def decode a single char from printable to possibly non-printable
86 if type(ch
) != type('') or len(ch
) != 1:
87 raise ValueError, 'need to pass in a single char'
91 # input 4 chars encoded, output 3 chars unencoded
94 raise ValueError, 'can only accept strings of 4 chars'
95 p0
, p1
, p2
, p3
= 0, 0, 0, 0
104 c1
= p0
<< 2 |
(p1
& 060) >> 4
105 c2
= (p1
& 017) << 4 |
(p2
& 074) >> 2
106 c3
= (p2
& 03) << 6 |
(p3
& 077)
112 # pass in 62 bytes and return 45 bytes unencoded
115 raise ValueError, 'cannot handle more than 62 chars at once'
116 length
= _DEC(str[0])
119 while len(rtn
) < length
:
120 rtn
= rtn
+ _outdec(str[i
:(i
+4)])
124 # decode(filename, mode, in_file)
125 # decode(in_file, out_file)
132 filename
, mode
, in_file
= args
133 if type(filename
) != type(''):
135 if type(mode
) != type(0):
138 _
= getattr(in_file
,'readline')
139 except AttributeError:
141 def _setup(out_file
,args
):
142 filename
, mode
, in_file
= args
143 # open file as specified and assign out_file for later use
144 out_file
= open(filename
,'w',mode
)
146 _
= in_file
.readline()
147 return (out_file
,_out_file_orig
)
149 in_file
, out_file
= args
151 _
= getattr(in_file
,'readline')
152 _
= getattr(out_file
,'write')
153 except AttributeError:
155 def _setup(out_file
, args
):
156 in_file
, out_file
= args
157 # Toss the 'begin mode filename' part.. not needed
158 _
= in_file
.readline()
160 return (out_file
,_out_file_orig
)
164 _
= getattr(in_file
,'readline')
165 except AttributeError:
167 def _setup(out_file
, args
):
170 # open file as specified in uu file and
171 # assign out_file for later use
172 i
= in_file
.readline()
175 raise IOError, 'input file not in UUencoded format'
176 [dummy
, mode
, filename
] = strop
.split(i
)
177 mode
= strop
.atoi(mode
, 8)
178 out_file
= open(filename
,'w',mode
)
180 return (out_file
,_out_file_orig
)
182 raise SyntaxError, 'must be (filename, mode, in_file) or (in_file,out_file) or (in_file)'
183 out_file
, _out_file_orig
= _setup(out_file
, args
)
184 str = in_file
.readline()
185 while len(str) > 0 and str != ' \n' and str != 'end\n':
186 out_file
.write(_decode(str))
187 str = in_file
.readline()
188 if _out_file_orig
== 0:
195 if sys
.argv
[1:2] == ['-d']:
197 decode(open(sys
.argv
[2]), sys
.stdout
)
199 decode(sys
.stdin
, sys
.stdout
)
200 elif sys
.argv
[1:2] == ['-e']:
207 encode(file, 0644, fp
, sys
.stdout
)
209 print 'usage: uu -d [file]; (to decode)'
210 print 'or: uu -e [file]; (to encode)'
213 if __name__
== '__main__':