40 lines · python
1import binascii2import subprocess3 4 5def get_command_output(command):6 try:7 return subprocess.check_output(8 command, shell=True, universal_newlines=True9 ).rstrip()10 except subprocess.CalledProcessError as e:11 return e.output12 13 14def bitcast_to_string(b: bytes) -> str:15 """16 Take a bytes object and return a string. The returned string contains the17 exact same bytes as the input object. (latin1 <-> unicode transformation is18 an identity operation for the first 256 code points).19 """20 return b.decode("latin1")21 22 23def bitcast_to_bytes(s: str) -> bytes:24 """25 Take a string and return a bytes object. The returned object contains the26 exact same bytes as the input string. (latin1 <-> unicode transformation isi27 an identity operation for the first 256 code points).28 """29 return s.encode("latin1")30 31 32def unhexlify(hexstr):33 """Hex-decode a string. The result is always a string."""34 return bitcast_to_string(binascii.unhexlify(hexstr))35 36 37def hexlify(data):38 """Hex-encode string data. The result if always a string."""39 return bitcast_to_string(binascii.hexlify(bitcast_to_bytes(data)))40