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