Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Individually encoded base64 decoded separately #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions lamson/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,18 @@ def attempt_decoding(charset, dec):


def apply_charset_to_header(charset, encoding, data):
# individually encoded base64 must be decoded separately,
# but the characterset decoding into unicode must be done jointly
if not isinstance(data, list):
data = [data]
if encoding == 'b' or encoding == 'B':
dec = email.base64mime.decode(data.encode('ascii'))
dec = [email.base64mime.decode(d.encode('ascii')) for d in data]
elif encoding == 'q' or encoding == 'Q':
dec = email.quoprimime.header_decode(data.encode('ascii'))
dec = [email.quoprimime.header_decode(d.encode('ascii')) for d in data]
else:
raise EncodingError("Invalid header encoding %r should be 'Q' or 'B'." % encoding)

return attempt_decoding(charset, dec)
return attempt_decoding(charset, "".join(dec))



Expand Down Expand Up @@ -474,8 +478,10 @@ def _parse_charset_header(data):
while True:
if not oddness:
left, enc_header, enc_data, continued = scanner.next()
enc_data = [enc_data]
else:
left, enc_header, enc_data, continued = oddness
enc_data = [enc_data]
oddness = None

while continued:
Expand All @@ -485,7 +491,9 @@ def _parse_charset_header(data):
assert not ed, "Parsing error, give Zed this: %r" % data
oddness = (" " + l.lstrip(), eh, ed, continued)
elif eh[0] == enc_header[0] and eh[1] == enc_header[1]:
enc_data += ed
# individually encoded base64 must be decoded separately,
# but the characterset decoding into unicode must be done jointly
enc_data.append(ed)
else:
# odd case, it's continued but not from the same base64
# need to stack this for the next loop, and drop the \n\s+
Expand Down