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