py3devil1pld.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import ctypes
  2. import os, sys
  3. # This is the folder containing the whole library.
  4. sys.path.append(
  5. os.path.abspath(
  6. os.path.join(
  7. os.path.dirname(__file__), "../../")))
  8. from lib3ddevil1.bindings import libc
  9. del os, sys
  10. #--------------------------------------+
  11. # Basic Struct
  12. #--------------------------------------+
  13. class PldHeader(ctypes.Structure):
  14. _pack_ = 1
  15. _fields_ = [
  16. ("numOffset", ctypes.c_int),
  17. ("offsets", ctypes.POINTER(ctypes.c_int))
  18. ]
  19. class Devil1PLD_FN(ctypes.Structure):
  20. _fields_ = [
  21. ("getheader" , ctypes.CFUNCTYPE(
  22. ctypes.c_bool,
  23. ctypes.POINTER(PldHeader),
  24. ctypes.c_char_p)),
  25. ("sizeofsector", ctypes.CFUNCTYPE(
  26. ctypes.c_int,
  27. ctypes.POINTER(PldHeader),
  28. ctypes.c_int)),
  29. ("printheader" , ctypes.CFUNCTYPE(None,
  30. ctypes.POINTER(PldHeader)))
  31. ]
  32. devil1pld = Devil1PLD_FN.in_dll(libc, "DEVIL1PLD")
  33. del libc
  34. #--------------------------------------+
  35. # Pythonic Object
  36. #--------------------------------------+
  37. class pyPldHeader:
  38. def __init__(self, filedata = None):
  39. # Store C Struct in order to call C functions
  40. self.cstruct = PldHeader()
  41. if filedata:
  42. if not devil1pld.getheader(ctypes.byref(self.cstruct), filedata):
  43. raise RuntimeError("failed to get .pld header")
  44. self.eof = len(filedata)
  45. def show(self):
  46. devil1pld.printheader(ctypes.byref(self.cstruct))
  47. return
  48. def getnumoffsets(self):
  49. return self.cstruct.numOffsets
  50. # return pythonic list of offsets
  51. def getoffsets(self):
  52. return self.cstruct.offsets[:self.cstruct.numOffset]
  53. def sizeofsector(self, i):
  54. ptr = ctypes.byref(self.cstruct)
  55. return devil1pld.sizeofsector(ptr, i, self.eof)