python3-10.pyscalpel.encoding
Utilities for encoding data.
1""" 2 Utilities for encoding data. 3""" 4 5from urllib.parse import unquote_to_bytes as urllibdecode 6from _internal_mitmproxy.utils import strutils 7 8 9# str/bytes conversion helpers from mitmproxy/http.py: 10# https://github.com/mitmproxy/mitmproxy/blob/main/mitmproxy/http.py#:~:text=def-,_native,-(x%3A 11def always_bytes(data: str | bytes | int, encoding="latin-1") -> bytes: 12 """Convert data to bytes 13 14 Args: 15 data (str | bytes | int): The data to convert 16 17 Returns: 18 bytes: The converted bytes 19 """ 20 if isinstance(data, int): 21 data = str(data) 22 return strutils.always_bytes(data, encoding, "surrogateescape") 23 24 25def always_str(data: str | bytes | int, encoding="latin-1") -> str: 26 """Convert data to string 27 28 Args: 29 data (str | bytes | int): The data to convert 30 31 Returns: 32 str: The converted string 33 """ 34 if isinstance(data, int): 35 return str(data) 36 return strutils.always_str(data, encoding, "surrogateescape") 37 38 39 40def urlencode_all(data: bytes | str, encoding="latin-1") -> bytes: 41 """URL Encode all bytes in the given bytes object""" 42 return "".join(f"%{b:02X}" for b in always_bytes(data, encoding)).encode(encoding) 43 44 45def urldecode(data: bytes | str, encoding="latin-1") -> bytes: 46 """URL Decode all bytes in the given bytes object""" 47 return urllibdecode(always_bytes(data, encoding))
def
always_bytes(data: str | bytes | int, encoding='latin-1') -> bytes:
12def always_bytes(data: str | bytes | int, encoding="latin-1") -> bytes: 13 """Convert data to bytes 14 15 Args: 16 data (str | bytes | int): The data to convert 17 18 Returns: 19 bytes: The converted bytes 20 """ 21 if isinstance(data, int): 22 data = str(data) 23 return strutils.always_bytes(data, encoding, "surrogateescape")
Convert data to bytes
Args: data (str | bytes | int): The data to convert
Returns: bytes: The converted bytes
def
always_str(data: str | bytes | int, encoding='latin-1') -> str:
26def always_str(data: str | bytes | int, encoding="latin-1") -> str: 27 """Convert data to string 28 29 Args: 30 data (str | bytes | int): The data to convert 31 32 Returns: 33 str: The converted string 34 """ 35 if isinstance(data, int): 36 return str(data) 37 return strutils.always_str(data, encoding, "surrogateescape")
Convert data to string
Args: data (str | bytes | int): The data to convert
Returns: str: The converted string
def
urlencode_all(data: bytes | str, encoding='latin-1') -> bytes:
41def urlencode_all(data: bytes | str, encoding="latin-1") -> bytes: 42 """URL Encode all bytes in the given bytes object""" 43 return "".join(f"%{b:02X}" for b in always_bytes(data, encoding)).encode(encoding)
URL Encode all bytes in the given bytes object
def
urldecode(data: bytes | str, encoding='latin-1') -> bytes:
46def urldecode(data: bytes | str, encoding="latin-1") -> bytes: 47 """URL Decode all bytes in the given bytes object""" 48 return urllibdecode(always_bytes(data, encoding))
URL Decode all bytes in the given bytes object