def uudecode(buffer: Union[bytes, str]): """ uudecode an input buffer; based on python library uu but with support for byte stream :param buffer: :return: """ # Create in_file from buffer in_file = io.BytesIO(buffer) out_file = io.BytesIO() while True: hdr = in_file.readline() if not hdr.startswith(b'begin'): continue hdrfields = hdr.split(b' ', 2) if len(hdrfields) == 3 and hdrfields[0] == b'begin': try: int(hdrfields[1], 8) break except ValueError: pass s = in_file.readline() while s and s.strip(b' \t\r\n\f') != b'end': try: data = binascii.a2b_uu(s) except binascii.Error as _: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((s[0] - 32) & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) out_file.write(data) s = in_file.readline() return out_file.getvalue()
def uu_decode(input, errors='strict'): from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(str(input)) outfile = StringIO() readline = infile.readline write = outfile.write while 1: s = readline() if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break while 1: s = readline() if not s or s == 'end\n': break try: data = a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3 data = a2b_uu(s[:nbytes]) write(data) if not s: raise ValueError, 'Truncated input data' return (outfile.getvalue(), len(input))
def _decode_uu(encoded): """Decode uuencoded data.""" decoded_lines = [] encoded_lines_iter = iter(encoded.splitlines()) for line in encoded_lines_iter: if line.startswith(b"begin "): mode, _, path = line.removeprefix(b"begin ").partition(b" ") try: int(mode, base=8) except ValueError: continue else: break else: raise ValueError("`begin` line not found") for line in encoded_lines_iter: if not line: raise ValueError("Truncated input") elif line.strip(b' \t\r\n\f') == b'end': break try: decoded_line = binascii.a2b_uu(line) except binascii.Error: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((line[0] - 32) & 63) * 4 + 5) // 3 decoded_line = binascii.a2b_uu(line[:nbytes]) decoded_lines.append(decoded_line) return b''.join(decoded_lines)
def _uu_decode(data): from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(input) outfile = StringIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() #print '...',s, if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break # Decode while 1: s = readline() if not s or \ s == 'end\n': break try: data = a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3 data = a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data)
def _assemble(nzf, path, dupe): if os.path.exists(path): unique_path = get_unique_filename(path) if dupe: path = unique_path else: renamer(path, unique_path) fout = open(path, 'ab') if cfg.quick_check(): md5 = new_md5() else: md5 = None _type = nzf.type decodetable = nzf.decodetable for articlenum in decodetable: sleep(0.001) article = decodetable[articlenum] data = ArticleCache.do.load_article(article) if not data: logging.info(T('%s missing'), article) else: # yenc data already decoded, flush it out if _type == 'yenc': fout.write(data) if md5: md5.update(data) # need to decode uu data now elif _type == 'uu': data = data.split('\r\n') chunks = [] for line in data: if not line: continue if line == '-- ' or line.startswith('Posted via '): continue try: tmpdata = binascii.a2b_uu(line) chunks.append(tmpdata) except binascii.Error, msg: # Workaround for broken uuencoders by # /Fredrik Lundh nbytes = (((ord(line[0]) - 32) & 63) * 4 + 5) / 3 try: tmpdata = binascii.a2b_uu(line[:nbytes]) chunks.append(tmpdata) except binascii.Error, msg: logging.info('Decode failed in part %s: %s', article.article, msg) data = ''.join(chunks) fout.write(data) if md5: md5.update(data)
def uu_decode(input, errors='strict'): assert errors == 'strict' infile = BytesIO(input) outfile = BytesIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() if not s: raise ValueError('Missing "begin" line in input data') if s[:5] == b'begin': break # Decode while True: s = readline() if not s or s == b'end\n': break try: data = binascii.a2b_uu(s) except binascii.Error as v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0]) - 32) & 63) * 4 + 5) / 3 data = binascii.a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data) if not s: raise ValueError('Truncated input data') return (outfile.getvalue(), len(input))
def decode(in_file, out_file=None, mode=None, quiet=0): """Decode uuencoded file""" # # Open the input file, if needed. # if in_file == "-": in_file = sys.stdin elif isinstance(in_file, basestring): in_file = open(in_file) # # Read until a begin is encountered or we've exhausted the file # while True: hdr = in_file.readline() if not hdr: raise Error("No valid begin line found in input file") if not hdr.startswith("begin"): continue hdrfields = hdr.split(" ", 2) if len(hdrfields) == 3 and hdrfields[0] == "begin": try: int(hdrfields[1], 8) break except ValueError: pass if out_file is None: out_file = hdrfields[2].rstrip() if os.path.exists(out_file): raise Error("Cannot overwrite existing file: %s" % out_file) if mode is None: mode = int(hdrfields[1], 8) # # Open the output file # opened = False if out_file == "-": out_file = sys.stdout elif isinstance(out_file, basestring): fp = open(out_file, "wb") try: os.chmod(out_file, mode) except AttributeError: pass out_file = fp opened = True # # Main decoding loop # s = in_file.readline() while s and s.strip() != "end": try: data = binascii.a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0]) - 32) & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) if not quiet: sys.stderr.write("Warning: %s\n" % v) out_file.write(data) s = in_file.readline()
def uu_decode(input, errors = 'strict'): from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(str(input)) outfile = StringIO() readline = infile.readline write = outfile.write while 1: s = readline() if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break while 1: s = readline() if not s or s == 'end\n': break try: data = a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3 data = a2b_uu(s[:nbytes]) write(data) if not s: raise ValueError, 'Truncated input data' return (outfile.getvalue(), len(input))
def uu_decode(input, errors='strict'): assert errors == 'strict' infile = BytesIO(input) outfile = BytesIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() if not s: raise ValueError('Missing "begin" line in input data') if s[:5] == b'begin': break # Decode while True: s = readline() if not s or s == b'end\n': break try: data = binascii.a2b_uu(s) except binascii.Error as v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((s[0]-32) & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data) if not s: raise ValueError('Truncated input data') return (outfile.getvalue(), len(input))
def decode(in_file, out_file=None, mode=None, quiet=0): """Decode uuencoded file""" opened_files = [] if in_file == '-': in_file = sys.stdin elif isinstance(in_file, basestring): in_file = open(in_file) opened_files.append(in_file) try: while True: hdr = in_file.readline() if not hdr: raise Error('No valid begin line found in input file') if not hdr.startswith('begin'): continue hdrfields = hdr.split(' ', 2) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: int(hdrfields[1], 8) break except ValueError: pass if out_file is None: out_file = hdrfields[2].rstrip() if os.path.exists(out_file): raise Error('Cannot overwrite existing file: %s' % out_file) if mode is None: mode = int(hdrfields[1], 8) if out_file == '-': out_file = sys.stdout elif isinstance(out_file, basestring): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp opened_files.append(out_file) s = in_file.readline() while s and s.strip() != 'end': try: data = binascii.a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) if not quiet: sys.stderr.write('Warning: %s\n' % v) out_file.write(data) s = in_file.readline() if not s: raise Error('Truncated input file') finally: for f in opened_files: f.close() return
def test_uu(self): if self.type2test is not str and test_support.due_to_ironpython_bug( "http://ironpython.codeplex.com/workitem/28171" ): return MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i : i + MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu("\x7f"), "\x00" * 31) self.assertEqual(binascii.a2b_uu("\x80"), "\x00" * 32) self.assertEqual(binascii.a2b_uu("\xff"), "\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * "!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu("x"), "!> \n")
def test_uu(self): if self.type2test is not str and test_support.due_to_ironpython_bug( "http://ironpython.codeplex.com/workitem/28171"): return MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i:i + MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu("\x7f"), "\x00" * 31) self.assertEqual(binascii.a2b_uu("\x80"), "\x00" * 32) self.assertEqual(binascii.a2b_uu("\xff"), "\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * "!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu('x'), '!> \n')
def _uu_decode(data): from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(input) outfile = StringIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() #print '...',s, if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break # Decode while 1: s = readline() if not s or \ s == 'end\n': break try: data = a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0]) - 32) & 63) * 4 + 5) / 3 data = a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data)
def decode(in_file, out_file = None, mode = None, quiet = 0): """Decode uuencoded file""" opened_files = [] if in_file == '-': in_file = sys.stdin elif isinstance(in_file, basestring): in_file = open(in_file) opened_files.append(in_file) try: while True: hdr = in_file.readline() if not hdr: raise Error('No valid begin line found in input file') if not hdr.startswith('begin'): continue hdrfields = hdr.split(' ', 2) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: int(hdrfields[1], 8) break except ValueError: pass if out_file is None: out_file = hdrfields[2].rstrip() if os.path.exists(out_file): raise Error('Cannot overwrite existing file: %s' % out_file) if mode is None: mode = int(hdrfields[1], 8) if out_file == '-': out_file = sys.stdout elif isinstance(out_file, basestring): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp opened_files.append(out_file) s = in_file.readline() while s and s.strip() != 'end': try: data = binascii.a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) if not quiet: sys.stderr.write('Warning: %s\n' % v) out_file.write(data) s = in_file.readline() if not s: raise Error('Truncated input file') finally: for f in opened_files: f.close() return
def decode(in_file, out_file=None, mode=None, quiet=0): """Decode uuencoded file""" # # Open the input file, if needed. # if in_file == '-': in_file = sys.stdin elif isinstance(in_file, basestring): in_file = open(in_file) # # Read until a begin is encountered or we've exhausted the file # while True: hdr = in_file.readline() if not hdr: raise Error('No valid begin line found in input file') if not hdr.startswith('begin'): continue hdrfields = hdr.split(' ', 2) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: int(hdrfields[1], 8) break except ValueError: pass if out_file is None: out_file = hdrfields[2].rstrip() if os.path.exists(out_file): raise Error('Cannot overwrite existing file: %s' % out_file) if mode is None: mode = int(hdrfields[1], 8) # # Open the output file # opened = False if out_file == '-': out_file = sys.stdout elif isinstance(out_file, basestring): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) #@UndefinedVariable except AttributeError: pass out_file = fp opened = True # # Main decoding loop # s = in_file.readline() while s and s.strip() != 'end': try: data = binascii.a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) if not quiet: sys.stderr.write("Warning: %s\n" % v) out_file.write(data) s = in_file.readline()
def uu_decode(input, errors='strict'): assert errors == 'strict' infile = BytesIO(input) outfile = BytesIO() readline = infile.readline write = outfile.write while 1: s = readline() if not s: raise ValueError('Missing "begin" line in input data') if s[:5] == b'begin': break while True: s = readline() if not s or s == b'end\n': break try: data = binascii.a2b_uu(s) except binascii.Error as v: nbytes = ((s[0] - 32 & 63) * 4 + 5) // 3 data = binascii.a2b_uu(s[:nbytes]) write(data) if not s: raise ValueError('Truncated input data') return outfile.getvalue(), len(input)
def decode(self, article): """ Decode uuencoded file """ # # Read until a begin is encountered or we've exhausted the file # counter = 0 # Main decoding loop # for s in article._data[counter:]: if s.strip() == "end": break try: data = binascii.a2b_uu(s) article.decoded_data += data except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0]) - 32) & 63) * 4 + 5) // 3 try: data = binascii.a2b_uu(s[:nbytes]) article.decoded_data += data warn("Warning UUDecoder: %s" % v) except binascii.Error, v: warn("UUDecoded crashed one byte!")
def decode(in_file, out_file=None, mode=None, quiet=0): """Decode uuencoded file""" # # Open the input file, if needed. # if in_file == '-': in_file = sys.stdin elif isinstance(in_file, StringType): in_file = open(in_file) # # Read until a begin is encountered or we've exhausted the file # while 1: hdr = in_file.readline() if not hdr: raise Error, 'No valid begin line found in input file' if hdr[:5] != 'begin': continue hdrfields = hdr.split(" ", 2) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: int(hdrfields[1], 8) start_pos = in_file.tell() - len (hdr) break except ValueError: pass if out_file is None: out_file = hdrfields[2].rstrip() if os.path.exists(out_file): raise Error, 'Cannot overwrite existing file: %s' % out_file if mode is None: mode = int(hdrfields[1], 8) # # Open the output file # if out_file == '-': out_file = sys.stdout elif isinstance(out_file, StringType): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp # # Main decoding loop # s = in_file.readline() while s and s.strip() != 'end': try: data = binascii.a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3 data = binascii.a2b_uu(s[:nbytes]) if not quiet: sys.stderr.write("Warning: %s\n" % str(v)) out_file.write(data) s = in_file.readline()
def decode(in_file, out_file=None, mode=None, quiet=0): if in_file == '-': in_file = sys.stdin elif isinstance(in_file, StringType): in_file = open(in_file) while None: hdr = in_file.readline() if not hdr: raise Error, 'No valid begin line found in input file' if hdr[:5] != 'begin': continue hdrfields = hdr.split(' ', 2) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: int(hdrfields[1], 8) except ValueError: pass if out_file is None: out_file = hdrfields[2].rstrip() if os.path.exists(out_file): raise Error, 'Cannot overwrite existing file: %s' % out_file if mode is None: mode = int(hdrfields[1], 8) if out_file == '-': out_file = sys.stdout elif isinstance(out_file, StringType): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp s = in_file.readline() while s and s.strip() != 'end': try: data = binascii.a2b_uu(s) except binascii.Error: v = None nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3 data = binascii.a2b_uu(s[:nbytes]) if not quiet: sys.stderr.write('Warning: %s\n' % str(v)) out_file.write(data) s = in_file.readline() if not s: raise Error, 'Truncated input file'
def decode(in_file, out_file=None, mode=None): """Decode uuencoded file""" # # Open the input file, if needed. # if in_file == '-': in_file = sys.stdin elif type(in_file) == type(''): in_file = open(in_file) # # Read until a begin is encountered or we've exhausted the file # while 1: hdr = in_file.readline() if not hdr: raise Error, 'No valid begin line found in input file' if hdr[:5] != 'begin': continue hdrfields = string.split(hdr) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: string.atoi(hdrfields[1], 8) break except ValueError: pass if out_file == None: out_file = hdrfields[2] if mode == None: mode = string.atoi(hdrfields[1], 8) # # Open the output file # if out_file == '-': out_file = sys.stdout elif type(out_file) == type(''): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp # # Main decoding loop # s = in_file.readline() while s and s != 'end\n': try: data = binascii.a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3 data = binascii.a2b_uu(s[:nbytes]) sys.stderr.write("Warning: %s\n" % str(v)) out_file.write(data) s = in_file.readline()
def decode(in_file, out_file=None, mode=None): """Decode uuencoded file""" # # Open the input file, if needed. # if in_file == '-': in_file = sys.stdin elif type(in_file) == type(''): in_file = open(in_file) # # Read until a begin is encountered or we've exhausted the file # while 1: hdr = in_file.readline() if not hdr: raise Error, 'No valid begin line found in input file' if hdr[:5] != 'begin': continue hdrfields = string.split(hdr) if len(hdrfields) == 3 and hdrfields[0] == 'begin': try: string.atoi(hdrfields[1], 8) break except ValueError: pass if out_file == None: out_file = hdrfields[2] if mode == None: mode = string.atoi(hdrfields[1], 8) # # Open the output file # if out_file == '-': out_file = sys.stdout elif type(out_file) == type(''): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp # # Main decoding loop # s = in_file.readline() while s and s != 'end\n': try: data = binascii.a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0]) - 32) & 63) * 4 + 5) / 3 data = binascii.a2b_uu(s[:nbytes]) sys.stderr.write("Warning: %s\n" % str(v)) out_file.write(data) s = in_file.readline()
def datadecode(self, ddata): if str(proto).lower() == "udp": if encodedata == "base85": __builtin__.data = b64.b85decode(ddata[0]) if encodedata == "base64": __builtin__.data = b64.b64decode(ddata[0]) if encodedata == "base32": __builtin__.data = b64.b32decode(ddata[0]) if encodedata == "base16": __builtin__.data = b64.b16decode(ddata[0]) if encodedata == "rot13": __builtin__.data = str(ddata[0]).decode('rot13') if encodedata == "xor": key = 'filterbuster' __builtin__.data = ''.join(chr(ord(c) ^ ord(k)) for c, k in izip(ddata[0], cycle(key))) if encodedata == "url": pass # do url if encodedata == "lzma": __builtin__.data = lzma.decompress(ddata) if encodedata in ["gz", "gzip", "zlib"]: __builtin__.data = ddata.encode("zlib") if encodedata in ["binary", "bytestring"]: __builtin__.data = binascii.a2b_uu(ddata) if encodedata in ["plain", "plaintext", "cleartext", "clear"]: __builtin__.data = ddata[0] if str(proto).lower() == "tcp": if encodedata == "base85": __builtin__.data = b64.b85decode(ddata) if encodedata == "base64": __builtin__.data = b64.b64decode(ddata) if encodedata == "base32": __builtin__.data = b64.b32decode(ddata) if encodedata == "base16": __builtin__.data = b64.b16decode(ddata) if encodedata == "rot13": __builtin__.data = str(ddata).decode('rot13') if encodedata == "xor": key = 'filterbuster' __builtin__.data = ''.join(chr(ord(c) ^ ord(k)) for c, k in izip(ddata, cycle(key))) if encodedata == "url": pass # do url if encodedata == "lzma": __builtin__.data = lzma.decompress(ddata) if encodedata in ["gz", "gzip", "zlib"]: __builtin__.data = ddata.encode("zlib") if encodedata in ["binary", "bytestring"]: __builtin__.data = binascii.a2b_uu(ddata) if encodedata in ["plain", "plaintext", "cleartext", "clear"]: __builtin__.data = ddata return ddata
def uu_decode(input,errors='strict'): """ Decodes the object input and returns a tuple (output object, length consumed). input must be an object which provides the bf_getreadbuf buffer slot. Python strings, buffer objects and memory mapped files are examples of objects providing this slot. errors defines the error handling to apply. It defaults to 'strict' handling which is the only currently supported error handling for this codec. Note: filename and file mode information in the input data is ignored. """ assert errors == 'strict' from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(str(input)) outfile = StringIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() if not s: raise ValueError('Missing "begin" line in input data') if s[:5] == 'begin': break # Decode while 1: s = readline() if not s or \ s == 'end\n': break try: data = a2b_uu(s) except binascii.Error as v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3 data = a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data) if not s: raise ValueError('Truncated input data') return (outfile.getvalue(), len(input))
def rob_uu_decode(text): res = '' lines = text.strip().split('\n') for line in lines: decoded_line = a2b_uu(line) res += decoded_line return res
def read_ram(addr, length): s.write("R %u %u\r\n" % (addr, length)) _check_return_code() data = bytearray() while True: chunk = bytearray() nlines = 0 while (len(chunk) + len(data)) < length and nlines < 20: l = s.readline() # Apparently uuencoding by bootloader is broken # Took following workaround from cython's uu module nbytes = (((ord(l[0]) - 32) & 63) * 4 + 5) // 3 l = l[:nbytes] chunk += bytearray(binascii.a2b_uu(l)) nlines += 1 good_csum = int(s.readline()) if good_csum == _compute_checksum(chunk): s.write("OK\r\n") data += chunk else: logging.info("Checksum mismatch during read... retrying") s.write("RESEND\r\n") if len(data) == length: return bytearray(data)
def decode(self, ctext: T) -> Optional[U]: """ UUEncode (Unix to Unix Encoding) is a symmetric encryption based on conversion of binary data (split into 6-bit blocks) into ASCII characters. This function decodes the input string 'ctext' if it has been encoded using 'uuencoder' It will return None otherwise """ logger.trace("Attempting UUencode") result = "" try: # UUencoded messages may begin with prefix "begin" and end with suffix "end" # In that case, we use the codecs module in Python ctext_strip = ctext.strip() if ctext_strip.startswith("begin") and ctext_strip.endswith("end"): result = decode(bytes(ctext, "utf-8"), "uu").decode() else: # If there isn't a "being" prefix and "end" suffix, we use the binascii module instead # It is possible that the ctext has multiple lines, so convert each line and append ctext_split = list(filter(None, ctext.splitlines())) for i in range(0, len(ctext_split)): result += a2b_uu(ctext_split[i]).decode("utf-8") logger.debug(f"UUencode successful, returning '{result}'") return result except Exception: logger.trace("Failed to decode UUencode") return None
def uu_to_modemconfig(uustr): '''Take UUEncoded tar.gz data in the form of a string, return the ASCII contents of default.conf, which is really quasi-XML data''' # Python changed the way it handles strings and binary data to be # more explicit in Python 3. As a result, when taking ASCII-based # UU data and converting to binary, things get trickier. The # binascii module in Python is well suited to handle this. # Take unicode string and convert each UU line to binary, stripping # the first and last line in the process. The 'begin' and 'end' # characters on these lines are illegal chars in the binascii # module (but not the uu module that we used to use). blines = b'' for line in uustr.splitlines()[1:-1]: blines += binascii.a2b_uu(line) # Write down uu-decoded binary data, which is a .tgz file with NamedTemporaryFile(suffix='.tgz', mode='wb') as tgz: tgz.write(blines) tgz.seek(0) # Read .tgz file with tarfile.open(name=tgz.name, mode='r') as tar: # Unpack default.conf with tar.extractfile('default.conf') as filep: modemconfig = filep.read().decode() # decode to ascii return modemconfig
def uu_decode(input,errors='strict'): """ Decodes the object input and returns a tuple (output object, length consumed). input must be an object which provides the bf_getreadbuf buffer slot. Python strings, buffer objects and memory mapped files are examples of objects providing this slot. errors defines the error handling to apply. It defaults to 'strict' handling which is the only currently supported error handling for this codec. Note: filename and file mode information in the input data is ignored. """ assert errors == 'strict' from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(str(input)) outfile = StringIO() readline = infile.readline write = outfile.write # Find start of encoded data while 1: s = readline() if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break # Decode while 1: s = readline() if not s or \ s == 'end\n': break try: data = a2b_uu(s) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(s[0])-32) & 63) * 4 + 5) // 3 data = a2b_uu(s[:nbytes]) #sys.stderr.write("Warning: %s\n" % str(v)) write(data)
def from_uu(data): """ 解uu编码 :param data: uu编码的字符串 :return: 字符串 """ r = binascii.a2b_uu(data) return force_text(r)
def uu_decode(input, errors = 'strict'): """ Decodes the object input and returns a tuple (output object, length consumed). input must be an object which provides the bf_getreadbuf buffer slot. Python strings, buffer objects and memory mapped files are examples of objects providing this slot. errors defines the error handling to apply. It defaults to 'strict' handling which is the only currently supported error handling for this codec. Note: filename and file mode information in the input data is ignored. """ if not errors == 'strict': raise AssertionError from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(str(input)) outfile = StringIO() readline = infile.readline write = outfile.write while 1: s = readline() if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break while 1: s = readline() if not s or s == 'end\n': break try: data = a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3 data = a2b_uu(s[:nbytes]) write(data) raise s or ValueError, 'Truncated input data' return (outfile.getvalue(), len(input))
def uu_decode(input, errors='strict'): """ Decodes the object input and returns a tuple (output object, length consumed). input must be an object which provides the bf_getreadbuf buffer slot. Python strings, buffer objects and memory mapped files are examples of objects providing this slot. errors defines the error handling to apply. It defaults to 'strict' handling which is the only currently supported error handling for this codec. Note: filename and file mode information in the input data is ignored. """ if not errors == 'strict': raise AssertionError from cStringIO import StringIO from binascii import a2b_uu infile = StringIO(str(input)) outfile = StringIO() readline = infile.readline write = outfile.write while 1: s = readline() if not s: raise ValueError, 'Missing "begin" line in input data' if s[:5] == 'begin': break while 1: s = readline() if not s or s == 'end\n': break try: data = a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3 data = a2b_uu(s[:nbytes]) write(data) raise s or ValueError, 'Truncated input data' return (outfile.getvalue(), len(input))
def str2hex(s): return str(binascii.b2a_hex(binascii.a2b_uu(s))) #given = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d" #expected = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t" #print ("Result = %s" % hextobase64(given)) #print ("Expected = %s" % expected)
def hashData(self): hashUserIp = [hashlib.sha1(binascii.a2b_uu(userip.split(':')[-1])).hexdigest() for userip in list(self.rawData['userip'].values)] # hash userip hashUserId = [hashlib.sha1(binascii.a2b_hex((userid))).hexdigest() for userid in list(self.rawData['userid'].values) if pd.notna(userid) and userid != 'na'] # hash userid # print([index for index in range(len(pd.notna(df['userid']).values)) if pd.notna(df['userid']).values[index]]) return hashUserIp,hashUserId
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.data[i:i + MAX_UU] a = binascii.b2a_uu(b) lines.append(a) res = bytes() for line in lines: b = binascii.a2b_uu(line) res += b self.assertEqual(res, self.data) self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00" * 31) self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00" * 32) self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * b"!")
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.data[i : i + MAX_UU] a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: b = binascii.a2b_uu(line) res += b self.assertEqual(res, self.data) self.assertEqual(binascii.a2b_uu("\x7f"), "\x00" * 31) self.assertEqual(binascii.a2b_uu("\x80"), "\x00" * 32) self.assertEqual(binascii.a2b_uu("\xff"), "\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * "!")
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i:i + MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = bytes() for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu(b'\x7f'), b'\x00' * 31) self.assertEqual(binascii.a2b_uu(b'\x80'), b'\x00' * 32) self.assertEqual(binascii.a2b_uu(b'\xff'), b'\x00' * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, b'\xff\x00') self.assertRaises(binascii.Error, binascii.a2b_uu, b'!!!!') self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * b'!') self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n')
def encode(string): ascii_arr = [] bin_arr = [] tripled_arr = [] for char in string: ascii_arr.append(ord(char)) for ascii_val in ascii_arr bin_arr.append(binascii.a2b_uu(ascii_val)) print(ascii_arr)
def lessBit(image,fText): width,height = image.size maxLetters = width*height*3 message = fText.read() binMessage = binascii.a2b_uu(message) if maxLetters < message.length: return 0 for i in range(width): for j in range(height): r,g,b = image.getpixel((i,j)) return newImage
def uudecode(phenny, input): """uudecode""" for x in phenny.bot.commands["high"].values(): if x[0].__name__ == "aa_hook": if x[0](phenny, input): return # Abort function if not input.group(2): return phenny.reply("Nothing to decode.") q = input.group(2).encode('utf-8') try: return phenny.say(binascii.a2b_uu(q + '\n')) except BaseException as e: return phenny.reply(e.message)
def UUDecode(dataList): """ UUDecode the specified list of data, returning results as a list """ buffer = [] # All whitespace and EOMs (.) should be stripped from the end at this point. Now, # strip the uuencode 'end' string and or whitespace (including grave accents) until we # have nothing but uuencoded data and its headers if dataList[-1][:3] == 'end': dataList.pop(-1) while dataList[-1] == '' or dataList[-1] == '`': dataList.pop(-1) # Any line before this index should precede with 'M' notMLines = len(dataList) - 1 index = -1 for line in dataList: index += 1 if (index <= 5 and line[:6] == 'begin ') or \ (index < notMLines and line[:1] != 'M'): notMLines -= 1 continue # From pyNewsleecher. Which ripped it from python's uu module (with maybe extra # overhead stripped out) try: data = binascii.a2b_uu(line) buffer.append(data) except binascii.Error, msg: # Workaround for broken uuencoders by /Fredrik Lundh try: #warn('UUEncode workaround') nbytes = (((ord(line[0])-32) & 63) * 4 + 5) / 3 data = binascii.a2b_uu(line[:nbytes]) buffer.append(data) except binascii.Error, msg: debug('UUDecode failed, line: ' + repr(line)) raise
def decode(encoding, fields): #encoding: An encoding for field in fields: if encoding == 'base64': field.value = binascii.a2b_base64(field.value) elif encoding == 'hex': field.value = binascii.a2b_hex(field.value) elif encoding == 'hqx': field.value = binascii.a2b_hqx(field.value) elif encoding == 'qp': field.value = binascii.a2b_qp(field.value) elif encoding == 'uu': field.value = binascii.a2b_uu(field.value) yield field
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.data[i:i+MAX_UU] a = binascii.b2a_uu(b) lines.append(a) res = bytes() for line in lines: b = binascii.a2b_uu(line) res += b self.assertEqual(res, self.data) self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n')
def uudecode(self, encoded): # The first char of the line is the length. The ISP tends to repeat the # last byte for padding which weirds out python so round the size of the # line up to a multiple of 3. size = encoded[0] - 0x20 pad = size % 3 if pad: encoded = bytes([encoded[0] + (3 - pad)]) + encoded[1:] decoded = binascii.a2b_uu(encoded) # Now drop the padding bytes from the decoded data decoded = decoded[:size] #print(f'{encoded} -> {decoded}') return decoded
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.data[i:i + MAX_UU] a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: b = binascii.a2b_uu(line) res += b self.assertEqual(res, self.data) self.assertEqual(binascii.a2b_uu("\x7f"), "\x00" * 31) self.assertEqual(binascii.a2b_uu("\x80"), "\x00" * 32) self.assertEqual(binascii.a2b_uu("\xff"), "\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * "!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu('x'), '!> \n')
def is_SSH_req(string): if string == None: return False try : if 'OpenSSH_' in base64.b64decode(string): return True except: pass try : if 'OpenSSH_' in base64.b32decode(string): return True except: pass try : if 'OpenSSH_' in base64.b16decode(string): return True except: pass try : if 'OpenSSH_' in binascii.a2b_uu(string): return True except: pass try : if 'OpenSSH_' in binascii.a2b_base64(string): return True except: pass try : if 'OpenSSH_' in binascii.a2b_qp(string): return True except: pass try : if 'OpenSSH_' in binascii.a2b_hqx(string): return True except: pass try : if 'OpenSSH_' in binascii.a2b_hex(string): return True except: pass try : if 'OpenSSH_' in string: return True except: pass return False
def uu_decode(input, errors='strict'): infile = BytesIO(input) outfile = BytesIO() readline = infile.readline write = outfile.write while True: s = readline() if not s: raise ValueError('Missing "begin" line in input data') if s[:5] == b'begin': break while True: s = readline() if not s or s == b'end\n': break try: data = binascii.a2b_uu(s) except binascii.Error as v: nbytes = ((ord(s[0]) - 32 & 63)*4 + 5)/3 data = binascii.a2b_uu(s[:nbytes]) write(data) if not s: raise ValueError('Truncated input data') return (outfile.getvalue(), len(input))
def decode_file(self, filename): fin = open(filename, "r") fout = open("c:\\\\bin\\file.out", "a") counter = 0 for line in fin: line = line.strip() if not line: continue if counter > 5: counter = 0 break hdrfields = line.split(" ", 2) if len(hdrfields) == 3 and hdrfields[0] == "begin": break counter += 1 print "header finished after %i" % counter for line in fin: line = line.strip() if line.strip() == "end": break try: data = binascii.a2b_uu(line) fout.write(data) except binascii.Error, v: # Workaround for broken uuencoders by /Fredrik Lundh nbytes = (((ord(line[0]) - 32) & 63) * 4 + 5) // 3 try: warn("Warning UUDecoder: %s, counter: %i" % (v, counter)) warn("debug: %s--" % line) data = binascii.a2b_uu(line[:nbytes]) fout.write(data) except binascii.Error, v: warn("UUDecoded crashed one byte!")
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i:i + MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = bytes() for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00" * 31) self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00" * 32) self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * b"!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n')
def test_uu(self): MAX_UU = 45 lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i : i + MAX_UU]) a = binascii.b2a_uu(b) lines.append(a) res = "" for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu("\x7f"), "\x00" * 31) self.assertEqual(binascii.a2b_uu("\x80"), "\x00" * 32) self.assertEqual(binascii.a2b_uu("\xff"), "\x00" * 31) self.assertRaises(binascii.Error, binascii.a2b_uu, "\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, "!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46 * "!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu("x"), "!> \n")
def decode(in_file, out_file=None, mode=None): """Decode uuencoded file""" # # Open the input file, if needed. # if in_file == '-': in_file = sys.stdin elif type(in_file) == type(''): in_file = open(in_file) # # Read the header line, and fill in optional args if needed # hdr = in_file.readline() if not hdr: raise Error, 'Empty input file' hdrfields = string.split(hdr) if len(hdrfields) <> 3 or hdrfields[0] <> 'begin': raise Error, ('Incorrect uu header line', hdr) if out_file == None: out_file = hdrfields[2] if mode == None: mode = string.atoi(hdrfields[1]) # # Open the output file # if out_file == '-': out_file = sys.stdout elif type(out_file) == type(''): fp = open(out_file, 'wb') try: os.path.chmod(out_file, mode) except AttributeError: pass out_file = fp # # Main decoding loop # str = in_file.readline() while str and str != 'end\n': out_file.write(binascii.a2b_uu(str)) str = in_file.readline() if not str: raise Error, 'Truncated input file'
def test_uu(self): MAX_UU = 45 for backtick in (True, False): lines = [] for i in range(0, len(self.data), MAX_UU): b = self.type2test(self.rawdata[i:i+MAX_UU]) a = binascii.b2a_uu(b, backtick=backtick) lines.append(a) res = bytes() for line in lines: a = self.type2test(line) b = binascii.a2b_uu(a) res += b self.assertEqual(res, self.rawdata) self.assertEqual(binascii.a2b_uu(b"\x7f"), b"\x00"*31) self.assertEqual(binascii.a2b_uu(b"\x80"), b"\x00"*32) self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31) self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00") self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!") self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") # Issue #7701 (crash on a pydebug build) self.assertEqual(binascii.b2a_uu(b'x'), b'!> \n') self.assertEqual(binascii.b2a_uu(b''), b' \n') self.assertEqual(binascii.b2a_uu(b'', backtick=True), b'`\n') self.assertEqual(binascii.a2b_uu(b' \n'), b'') self.assertEqual(binascii.a2b_uu(b'`\n'), b'') self.assertEqual(binascii.b2a_uu(b'\x00Cat'), b'$ $-A= \n') self.assertEqual(binascii.b2a_uu(b'\x00Cat', backtick=True), b'$`$-A=```\n') self.assertEqual(binascii.a2b_uu(b'$`$-A=```\n'), binascii.a2b_uu(b'$ $-A= \n')) with self.assertRaises(TypeError): binascii.b2a_uu(b"", True)
def __LoadCachedResponses(self, base_dir): """Preload all cached responses.""" print 'Loading data...' for browser in os.listdir(base_dir): filesread = 0 if not os.path.isdir(os.path.join(base_dir, browser)): continue self.__responses[browser] = {} print 'Reading responses for', browser, for key in os.listdir(os.path.join(base_dir, browser)): sys.stdout.flush() f = open(os.path.join(base_dir, browser, key)) cachedresponse = json.load(f) f.close() if filesread % 10 == 0: print '.', repl = '' if cachedresponse['body']: lines = cachedresponse['body'].split('\n') else: lines = [] for line in lines: repl += binascii.a2b_uu(line) # signal that we are in replay mode. # uudecode here, or uuencode in php inserts a lot of 0 chars. # will be fixed when we get rid of php. repl = repl.replace(chr(0), '') repl = re.sub('<script>', '<script>DEV_BENCHMARK_REPLAY=1;', repl) self.__responses[browser][key] = { 'code': cachedresponse['code'], 'header': cachedresponse['header'], 'body': repl } filesread += 1 print filesread, 'read.'
"""Test the binascii C module.""" from test_support import verify, verbose import binascii # Show module doc string print binascii.__doc__ # Show module exceptions print binascii.Error print binascii.Incomplete # Check presence and display doc strings of all functions funcs = [] for suffix in "base64", "hqx", "uu": prefixes = ["a2b_", "b2a_"] if suffix == "hqx": prefixes.extend(["crc_", "rlecode_", "rledecode_"]) for prefix in prefixes: name = prefix + suffix funcs.append(getattr(binascii, name)) for func in funcs: print "%-15s: %s" % (func.__name__, func.__doc__) # Create binary test data testdata = "The quick brown fox jumps over the lazy dog.\r\n" for i in range(256): # Be slow so we don't depend on other modules testdata = testdata + chr(i) testdata = testdata + "\r\nHello world.\n" # Test base64 with valid data print "base64 test" MAX_BASE64 = 57 lines = [] for i in range(0, len(testdata), MAX_BASE64):
b = binascii.a2b_base64(line) res = res + b assert res == testdata # Test uu print "uu test" MAX_UU = 45 lines = [] for i in range(0, len(testdata), MAX_UU): b = testdata[i:i+MAX_UU] a = binascii.b2a_uu(b) lines.append(a) print a, res = "" for line in lines: b = binascii.a2b_uu(line) res = res + b assert res == testdata # Test crc32() crc = binascii.crc32("Test the CRC-32 of") crc = binascii.crc32(" this string.", crc) if crc != 1571220330: print "binascii.crc32() failed." # The hqx test is in test_binhex.py # test hexlification s = '{s\005\000\000\000worldi\002\000\000\000s\005\000\000\000helloi\001\000\000\0000' t = binascii.b2a_hex(s) u = binascii.a2b_hex(t)
def decode(self, s): return binascii.a2b_uu(s)