Skip to content

gh-118761: email.quoprimime removing re import #132046

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
use cache for hex to char + instead of single character append use sl…
…ices
  • Loading branch information
Marius-Juston committed Apr 3, 2025
commit 3ada67a5a2c3c11af0c64f9434ffc9cfa0b0e905
33 changes: 26 additions & 7 deletions Lib/email/quoprimime.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ def decode(encoded, eol=NL):
body_decode = decode
decodestring = decode


_HEX_TO_CHAR = {}
for i in range(256):
key_lower = f"{i:02x}"
char_val = chr(i)
_HEX_TO_CHAR[key_lower] = char_val

# Header decoding is done a bit differently
def header_decode(s):
"""Decode a string encoded with RFC 2045 MIME header 'Q' encoding.
Expand All @@ -288,15 +295,27 @@ def header_decode(s):
"""
# Check for regex =[a-fA-F0-9]{2}
result = []

s_len = len(s)
i =0
i = 0
last_append = 0
s = s.replace("_", " ")

while i < s_len:
if (c := s[i]) == '=' and i + 2 < s_len and s[i + 1] in hexdigits and s[i + 2] in hexdigits:
result.append(unquote(s[i: i + 3]))
i += 3
continue
result.append(' ' if c == '_' else c)
if s[i] == '=' and i + 2 < s_len:
hex_str = s[i + 1:i + 3].lower()
if hex_str in _HEX_TO_CHAR:
if last_append < i:
result.append(s[last_append:i])
result.append(_HEX_TO_CHAR[hex_str])
i += 3
last_append = i
continue
i += 1

if last_append == 0:
return s

if last_append < s_len:
result.append(s[last_append:])

return ''.join(result)
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy