Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small python3 helper for dealing with kernel slides and basic hexadecimal arithmetic
## Global Variables
KernelSlide = 0x0
## Helper Functions
def isHex(val):
try:
int(val, 16)
return True
except ValueError:
return False
## End of Helper Functions
## Menu Options
def slideMenu(option):
global KernelSlide
## Set slide (s)
if option == "s":
newSlide = input("Please enter the slide: ")
if (isHex(newSlide) == False):
print("That is not valid hex.")
return
KernelSlide = int(newSlide, 16)
print("Kernel slide set to %s" % hex(KernelSlide))
# Is this right? May not be 0x1000000
if (KernelSlide % 0x1000000 != 0):
print("That slide is not a multiple of 0x1000000. Are you sure it's valid?")
## Get slide (g)
if (option == "g"):
if (KernelSlide == 0x0):
print("Kernel slide is currently not set.")
return
print("Current slide is: %s" % hex(KernelSlide))
## Slide (add) value (a)
if (option == "a"):
if (KernelSlide == 0x0):
print("Kernel side is currently not set.")
return
value = input("Value to slide: ")
if (isHex(value) == False):
print("That is not valid hex.")
return
calculation = int(value, 16) + KernelSlide
print("Slid value: %s" % hex(calculation))
## Unslide (subtratct) value (u)
if (option == "u"):
if (KernelSlide == 0x10):
print("Kernel slide is currently not set.")
return
value = input("Value to unslide: ")
if (isHex(value) == False):
print("That is not valid hex.")
return
calculation = int(value, 16) - KernelSlide
print("Unslid value: %s" % hex(calculation))
def calculationMenu(option):
## Add values (a)
if (option == "a"):
values = input("Values to add: ")
parts = values.split()
if (len(parts) < 2):
print("Invalid format. Please enter the values split by a space.")
print("eg: 0x10 0x30 0x2A")
return
for item in parts:
if (isHex(item) == False):
print("'%s' is not valid hex." % item)
return
answer = 0
for item in parts:
answer += int(item, 16)
print("Answer: %s" % hex(answer))
## Subtrace values (s)
if (option == "s"):
values = input("Values to subtract: ")
parts = values.split()
if (len(parts) < 2):
print("Invalid format. Please enter the values split by a space.")
print("eg: 0x10 0x30 0x2A")
for item in parts:
if (isHex(item) == False):
print("'%s' is not valid hex." % item)
return
answer = int(parts[0], 16)
for item in parts[1:]:
answer -= int(item, 16)
print("Answer: %s" % hex(answer))
## End of Menu Options
## Main Loop
def mainLoop():
print("")
option = input("Options: [S]lide, [C]alc, [Q]uit: ").lower()
if (option != "s" and
option != "c" and
option != "q"):
print("Unrecognized option '%s'" % option)
return
## Slide menu (s)
if (option == "s"):
minorOption = input("Slide: [S]et, [G]et, [A]dd, [U]nslide: ").lower()
if (minorOption != "s" and
minorOption != "g" and
minorOption != "a" and
minorOption != "u"):
print("Unrecognized option '%s'" % minorOption)
return
slideMenu(minorOption)
return
## Calculation menu (c)
if (option == "c"):
minorOption = input("Calculate: [A]dd, [S]ubtract: ").lower()
if (minorOption != "a" and
minorOption != "s"):
print("Unrecognized option '%s'" % minorOption)
return
calculationMenu(minorOption)
return
if (option == "q"):
print("Bye! \n")
exit()
return
## End of Main Loop
## Main Program
while True:
mainLoop()
## End of Main Program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment