| 14 | ### Codec APIs |
| 15 | |
| 16 | def uu_encode(input, errors='strict', filename='<data>', mode=0o666): |
| 17 | assert errors == 'strict' |
| 18 | infile = BytesIO(input) |
| 19 | outfile = BytesIO() |
| 20 | read = infile.read |
| 21 | write = outfile.write |
| 22 | |
| 23 | # Remove newline chars from filename |
| 24 | filename = filename.replace('\n','\\n') |
| 25 | filename = filename.replace('\r','\\r') |
| 26 | |
| 27 | # Encode |
| 28 | write(('begin %o %s\n' % (mode & 0o777, filename)).encode('ascii')) |
| 29 | chunk = read(45) |
| 30 | while chunk: |
| 31 | write(binascii.b2a_uu(chunk)) |
| 32 | chunk = read(45) |
| 33 | write(b' \nend\n') |
| 34 | |
| 35 | return (outfile.getvalue(), len(input)) |
| 36 | |
| 37 | def uu_decode(input, errors='strict'): |
| 38 | assert errors == 'strict' |