def decode_hex_string(string, raw=False): ''' Takes a hex string (\\x..\\x..\\x..) as input and converts it back into either an utf-8 formatted string or a raw bytes value. Parameters: string (string) Input hex string raw (boolean) Return as bytes Returns: string (string/bytes) Decoded output ''' plain = string.replace('\\x', '') try: b_array = bytearray.fromhex(plain) except Exception: raise VveException("Input is no valid hex string.") if raw: return bytes(b_array) try: return b_array.decode('utf-8') except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.")
def decode_binary(string, raw=False): ''' Takes a binary formatted string (containing only zeros and ones) and tries to decode it as an utf-8 formatted string. Output can also be obtained as raw bytes if raw=True. Parameters: string (string) Input binary string raw (boolean) Return as bytes Returns: string (string/bytes) decoded output ''' if re.search('[^01]', string): raise VveException("Input string is not binary.") b_array = bytearray() for i in range(0, len(string), 8): integer = int(string[i:i + 8], 2) b_array.append(integer) if raw: return bytes(b_array) try: return b_array.decode('utf-8') except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.")
def string_length_hex(string): ''' A length function for hex inputs (plain 0x.. and \\x.. format). Parameters: string (string) input string Returns: None ''' hex_format = _get_hex_format(string) if hex_format == 'number': length = len(string) // 2 - 1 elif hex_format == 'plain': length = len(string) // 2 elif hex_format == 'formatted': length = len(string) // 4 else: raise VveException(f"Input string '{string}' is not in hex format.") vim.command('redraw') print(length)
def decode_ascii(string, raw=False): ''' Opposite operation to encode_ascii. Searches for \\xXX sequences inside the input string and converts them to their raw byte value. Afterwards, the whole string is converted to bytes. If raw=True, this bytes object is returned. Otherwise, the bytearray will be decoded as utf-8. Paramaters: string (string) Input string raw (boolean) True -> type(output)=bytes Returns: decoded (string/bytes) Decoded output ''' string = re.sub(r'\\x[0-9a-fA-F]{2}', _replace_hex, string) decoded = encode_mixed(string) if raw: return decoded try: decoded = decoded.decode('utf-8') except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.") return decoded
def decode_html_full(string, raw=False): ''' Applies HTML decoding to the input string. Parameters: string (string) Input string raw (boolean) Return as bytes Returns: string (string/bytes) HTML decoded output ''' string = re.sub(r'&#x[0-9a-fA-F]{2};', _replace_html_hex, string) string = re.sub(r'&#[0-9]{1,6};', _replace_html_dec, string) string = re.sub(r'&[a-zA-Z]+;', _replace_html_ent, string) decoded = encode_mixed(string) if raw: return decoded try: decoded = decoded.decode('utf-8') except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.") return decoded
def decode_base64(string, raw=False): ''' Takes a base64 input string and returns the decoded data. Padding errors are corrected automatically (theoretically). Output can be obtained as string or bytes depending on the value of raw. Parameters: string (string) Input string raw (boolean) Return as bytes Returns: decoded (string/bytes) Base64 decoded data ''' byte_form = string.encode('utf-8') count = 0 while count < 3: try: decoded = base64.b64decode(byte_form, validate=True) if raw: return decoded decoded = decoded.decode('utf-8') break except binascii.Error: byte_form += b'=' decoded = string count += 1 continue except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.") if count >= 3: raise VveException("Input is no valid base64.") return decoded
def encode_xml(string): ''' Applies XML encoding to the input string for XML special characters. Parameters: string (string) Input string Returns: string (string) XML encoded output ''' if isinstance(string, bytes): raise VveException("XML encoding is only supported for UTF-8 data.") return xml.sax.saxutils.escape(string)
def encode_html(string): ''' Applies HTML encoding to special characrers inside the input string. Parameters: string (string) Input string Returns: string (string) HTML encoded output ''' if isinstance(string, bytes): raise VveException("HTML encoding is only supported for UTF-8 data.") return html.escape(string)
def decode_hex(string, raw=False): ''' Takes a stream of hex numbers (e.g. A01FE92...) and converts it, either into an utf-8 formatted string or a raw bytes value. Parameters: string (string) Input hex stream raw (boolean) Return as bytes Returns: string (string/bytes) UTF-8 formatted output ''' try: b_array = bytearray.fromhex(string) except Exception: raise VveException('Input is not in hex format.') if raw: return bytes(b_array) try: return b_array.decode('utf-8') except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.")
def decode_url_full(string, raw=False): ''' Applies URL decoding to the input string. Output can be obtained as utf-8 string or raw byes. Parameters: string (string) Input string raw (boolean) Return as bytes Returns: string (string/bytes) URL decoded output ''' string = re.sub(r'%[0-9a-fA-F]{2}', _replace_url, string) string = string.replace("+", " ") decoded = encode_mixed(string) if raw: return decoded try: return decoded.decode('utf-8') except Exception: raise VveException("Decoded result cannot be encoded as UTF-8.")