1 | n/a | """Python 'uu_codec' Codec - UU content transfer encoding. |
---|
2 | n/a | |
---|
3 | n/a | This codec de/encodes from bytes to bytes. |
---|
4 | n/a | |
---|
5 | n/a | Written by Marc-Andre Lemburg (mal@lemburg.com). Some details were |
---|
6 | n/a | adapted from uu.py which was written by Lance Ellinghouse and |
---|
7 | n/a | modified by Jack Jansen and Fredrik Lundh. |
---|
8 | n/a | """ |
---|
9 | n/a | |
---|
10 | n/a | import codecs |
---|
11 | n/a | import binascii |
---|
12 | n/a | from io import BytesIO |
---|
13 | n/a | |
---|
14 | n/a | ### Codec APIs |
---|
15 | n/a | |
---|
16 | n/a | def uu_encode(input, errors='strict', filename='<data>', mode=0o666): |
---|
17 | n/a | assert errors == 'strict' |
---|
18 | n/a | infile = BytesIO(input) |
---|
19 | n/a | outfile = BytesIO() |
---|
20 | n/a | read = infile.read |
---|
21 | n/a | write = outfile.write |
---|
22 | n/a | |
---|
23 | n/a | # Encode |
---|
24 | n/a | write(('begin %o %s\n' % (mode & 0o777, filename)).encode('ascii')) |
---|
25 | n/a | chunk = read(45) |
---|
26 | n/a | while chunk: |
---|
27 | n/a | write(binascii.b2a_uu(chunk)) |
---|
28 | n/a | chunk = read(45) |
---|
29 | n/a | write(b' \nend\n') |
---|
30 | n/a | |
---|
31 | n/a | return (outfile.getvalue(), len(input)) |
---|
32 | n/a | |
---|
33 | n/a | def uu_decode(input, errors='strict'): |
---|
34 | n/a | assert errors == 'strict' |
---|
35 | n/a | infile = BytesIO(input) |
---|
36 | n/a | outfile = BytesIO() |
---|
37 | n/a | readline = infile.readline |
---|
38 | n/a | write = outfile.write |
---|
39 | n/a | |
---|
40 | n/a | # Find start of encoded data |
---|
41 | n/a | while 1: |
---|
42 | n/a | s = readline() |
---|
43 | n/a | if not s: |
---|
44 | n/a | raise ValueError('Missing "begin" line in input data') |
---|
45 | n/a | if s[:5] == b'begin': |
---|
46 | n/a | break |
---|
47 | n/a | |
---|
48 | n/a | # Decode |
---|
49 | n/a | while True: |
---|
50 | n/a | s = readline() |
---|
51 | n/a | if not s or s == b'end\n': |
---|
52 | n/a | break |
---|
53 | n/a | try: |
---|
54 | n/a | data = binascii.a2b_uu(s) |
---|
55 | n/a | except binascii.Error as v: |
---|
56 | n/a | # Workaround for broken uuencoders by /Fredrik Lundh |
---|
57 | n/a | nbytes = (((s[0]-32) & 63) * 4 + 5) // 3 |
---|
58 | n/a | data = binascii.a2b_uu(s[:nbytes]) |
---|
59 | n/a | #sys.stderr.write("Warning: %s\n" % str(v)) |
---|
60 | n/a | write(data) |
---|
61 | n/a | if not s: |
---|
62 | n/a | raise ValueError('Truncated input data') |
---|
63 | n/a | |
---|
64 | n/a | return (outfile.getvalue(), len(input)) |
---|
65 | n/a | |
---|
66 | n/a | class Codec(codecs.Codec): |
---|
67 | n/a | def encode(self, input, errors='strict'): |
---|
68 | n/a | return uu_encode(input, errors) |
---|
69 | n/a | |
---|
70 | n/a | def decode(self, input, errors='strict'): |
---|
71 | n/a | return uu_decode(input, errors) |
---|
72 | n/a | |
---|
73 | n/a | class IncrementalEncoder(codecs.IncrementalEncoder): |
---|
74 | n/a | def encode(self, input, final=False): |
---|
75 | n/a | return uu_encode(input, self.errors)[0] |
---|
76 | n/a | |
---|
77 | n/a | class IncrementalDecoder(codecs.IncrementalDecoder): |
---|
78 | n/a | def decode(self, input, final=False): |
---|
79 | n/a | return uu_decode(input, self.errors)[0] |
---|
80 | n/a | |
---|
81 | n/a | class StreamWriter(Codec, codecs.StreamWriter): |
---|
82 | n/a | charbuffertype = bytes |
---|
83 | n/a | |
---|
84 | n/a | class StreamReader(Codec, codecs.StreamReader): |
---|
85 | n/a | charbuffertype = bytes |
---|
86 | n/a | |
---|
87 | n/a | ### encodings module API |
---|
88 | n/a | |
---|
89 | n/a | def getregentry(): |
---|
90 | n/a | return codecs.CodecInfo( |
---|
91 | n/a | name='uu', |
---|
92 | n/a | encode=uu_encode, |
---|
93 | n/a | decode=uu_decode, |
---|
94 | n/a | incrementalencoder=IncrementalEncoder, |
---|
95 | n/a | incrementaldecoder=IncrementalDecoder, |
---|
96 | n/a | streamreader=StreamReader, |
---|
97 | n/a | streamwriter=StreamWriter, |
---|
98 | n/a | _is_text_encoding=False, |
---|
99 | n/a | ) |
---|