+
+def IsAppleInternal():
+ """ check if apple_internal modules are available
+ returns: True if apple_internal module is present
+ """
+ import imp
+ try:
+ imp.find_module("apple_internal")
+ retval = True
+ except ImportError:
+ retval = False
+ return retval
+
+def print_hex_data(data, begin_offset=0, desc=""):
+ """ print on stdout "hexdump -C < data" like output
+ params:
+ data - bytearray or array of int where each int < 255
+ begin_offset - int offset that should be printed in left column
+ desc - str optional description to print on the first line to describe data
+ """
+ if desc:
+ print "{}:".format(desc)
+ index = 0
+ total_len = len(data)
+ hex_buf = ""
+ char_buf = ""
+ while index < total_len:
+ hex_buf += " {:02x}".format(data[index])
+ if data[index] < 0x20 or data[index] > 0x7e:
+ char_buf += "."
+ else:
+ char_buf += "{:c}".format(data[index])
+ index += 1
+ if index and index < total_len and index % 8 == 0:
+ hex_buf += " "
+ if index > 1 and index < total_len and (index % 16) == 0:
+ print "{:08x} {: <50s} |{: <16s}|".format(begin_offset + index - 16, hex_buf, char_buf)
+ hex_buf = ""
+ char_buf = ""
+ print "{:08x} {: <50s} |{: <16s}|".format(begin_offset + index - 16, hex_buf, char_buf)
+ return
+
+def Ones(x):
+ return (1 << x)-1
+
+def StripPAC(x, TySz):
+ sign_mask = 1 << 55
+ ptr_mask = Ones(64-TySz)
+ pac_mask = ~ptr_mask
+ sign = x & sign_mask
+ if sign:
+ return (x | pac_mask) + 2**64
+ else:
+ return x & ptr_mask