1#! /usr/bin/env python3
2
3# Copyright 1994 by Lance Ellinghouse
4# Cathedral City, California Republic, United States of America.
5#                        All Rights Reserved
6# Permission to use, copy, modify, and distribute this software and its
7# documentation for any purpose and without fee is hereby granted,
8# provided that the above copyright notice appear in all copies and that
9# both that copyright notice and this permission notice appear in
10# supporting documentation, and that the name of Lance Ellinghouse
11# not be used in advertising or publicity pertaining to distribution
12# of the software without specific, written prior permission.
13# LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
14# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
15# FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE
16# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20#
21# Modified by Jack Jansen, CWI, July 1995:
22# - Use binascii module to do the actual line-by-line conversion
23#   between ascii and binary. This results in a 1000-fold speedup. The C
24#   version is still 5 times faster, though.
25# - Arguments more compliant with python standard
26
27"""Implementation of the UUencode and UUdecode functions.
28
29encode(in_file, out_file [,name, mode], *, backtick=False)
30decode(in_file [, out_file, mode, quiet])
31"""
32
33import binascii
34import os
35import sys
36
37__all__ = ["Error", "encode", "decode"]
38
39class Error(Exception):
40    pass
41
42def encode(in_file, out_file, name=None, mode=None, *, backtick=False):
43    """Uuencode file"""
44    #
45    # If in_file is a pathname open it and change defaults
46    #
47    opened_files = []
48    try:
49        if in_file == '-':
50            in_file = sys.stdin.buffer
51        elif isinstance(in_file, str):
52            if name is None:
53                name = os.path.basename(in_file)
54            if mode is None:
55                try:
56                    mode = os.stat(in_file).st_mode
57                except AttributeError:
58                    pass
59            in_file = open(in_file, 'rb')
60            opened_files.append(in_file)
61        #
62        # Open out_file if it is a pathname
63        #
64        if out_file == '-':
65            out_file = sys.stdout.buffer
66        elif isinstance(out_file, str):
67            out_file = open(out_file, 'wb')
68            opened_files.append(out_file)
69        #
70        # Set defaults for name and mode
71        #
72        if name is None:
73            name = '-'
74        if mode is None:
75            mode = 0o666
76
77        #
78        # Remove newline chars from name
79        #
80        name = name.replace('\n','\\n')
81        name = name.replace('\r','\\r')
82
83        #
84        # Write the data
85        #
86        out_file.write(('begin %o %s\n' % ((mode & 0o777), name)).encode("ascii"))
87        data = in_file.read(45)
88        while len(data) > 0:
89            out_file.write(binascii.b2a_uu(data, backtick=backtick))
90            data = in_file.read(45)
91        if backtick:
92            out_file.write(b'`\nend\n')
93        else:
94            out_file.write(b' \nend\n')
95    finally:
96        for f in opened_files:
97            f.close()
98
99
100def decode(in_file, out_file=None, mode=None, quiet=False):
101    """Decode uuencoded file"""
102    #
103    # Open the input file, if needed.
104    #
105    opened_files = []
106    if in_file == '-':
107        in_file = sys.stdin.buffer
108    elif isinstance(in_file, str):
109        in_file = open(in_file, 'rb')
110        opened_files.append(in_file)
111
112    try:
113        #
114        # Read until a begin is encountered or we've exhausted the file
115        #
116        while True:
117            hdr = in_file.readline()
118            if not hdr:
119                raise Error('No valid begin line found in input file')
120            if not hdr.startswith(b'begin'):
121                continue
122            hdrfields = hdr.split(b' ', 2)
123            if len(hdrfields) == 3 and hdrfields[0] == b'begin':
124                try:
125                    int(hdrfields[1], 8)
126                    break
127                except ValueError:
128                    pass
129        if out_file is None:
130            # If the filename isn't ASCII, what's up with that?!?
131            out_file = hdrfields[2].rstrip(b' \t\r\n\f').decode("ascii")
132            if os.path.exists(out_file):
133                raise Error('Cannot overwrite existing file: %s' % out_file)
134        if mode is None:
135            mode = int(hdrfields[1], 8)
136        #
137        # Open the output file
138        #
139        if out_file == '-':
140            out_file = sys.stdout.buffer
141        elif isinstance(out_file, str):
142            fp = open(out_file, 'wb')
143            os.chmod(out_file, mode)
144            out_file = fp
145            opened_files.append(out_file)
146        #
147        # Main decoding loop
148        #
149        s = in_file.readline()
150        while s and s.strip(b' \t\r\n\f') != b'end':
151            try:
152                data = binascii.a2b_uu(s)
153            except binascii.Error as v:
154                # Workaround for broken uuencoders by /Fredrik Lundh
155                nbytes = (((s[0]-32) & 63) * 4 + 5) // 3
156                data = binascii.a2b_uu(s[:nbytes])
157                if not quiet:
158                    sys.stderr.write("Warning: %s\n" % v)
159            out_file.write(data)
160            s = in_file.readline()
161        if not s:
162            raise Error('Truncated input file')
163    finally:
164        for f in opened_files:
165            f.close()
166
167def test():
168    """uuencode/uudecode main program"""
169
170    import optparse
171    parser = optparse.OptionParser(usage='usage: %prog [-d] [-t] [input [output]]')
172    parser.add_option('-d', '--decode', dest='decode', help='Decode (instead of encode)?', default=False, action='store_true')
173    parser.add_option('-t', '--text', dest='text', help='data is text, encoded format unix-compatible text?', default=False, action='store_true')
174
175    (options, args) = parser.parse_args()
176    if len(args) > 2:
177        parser.error('incorrect number of arguments')
178        sys.exit(1)
179
180    # Use the binary streams underlying stdin/stdout
181    input = sys.stdin.buffer
182    output = sys.stdout.buffer
183    if len(args) > 0:
184        input = args[0]
185    if len(args) > 1:
186        output = args[1]
187
188    if options.decode:
189        if options.text:
190            if isinstance(output, str):
191                output = open(output, 'wb')
192            else:
193                print(sys.argv[0], ': cannot do -t to stdout')
194                sys.exit(1)
195        decode(input, output)
196    else:
197        if options.text:
198            if isinstance(input, str):
199                input = open(input, 'rb')
200            else:
201                print(sys.argv[0], ': cannot do -t from stdin')
202                sys.exit(1)
203        encode(input, output)
204
205if __name__ == '__main__':
206    test()
207