macRes.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. from io import BytesIO
  2. import struct
  3. from fontTools.misc import sstruct
  4. from fontTools.misc.textTools import bytesjoin, tostr
  5. from collections import OrderedDict
  6. from collections.abc import MutableMapping
  7. class ResourceError(Exception):
  8. pass
  9. class ResourceReader(MutableMapping):
  10. """Reader for Mac OS resource forks.
  11. Parses a resource fork and returns resources according to their type.
  12. If run on OS X, this will open the resource fork in the filesystem.
  13. Otherwise, it will open the file itself and attempt to read it as
  14. though it were a resource fork.
  15. The returned object can be indexed by type and iterated over,
  16. returning in each case a list of py:class:`Resource` objects
  17. representing all the resources of a certain type.
  18. """
  19. def __init__(self, fileOrPath):
  20. """Open a file
  21. Args:
  22. fileOrPath: Either an object supporting a ``read`` method, an
  23. ``os.PathLike`` object, or a string.
  24. """
  25. self._resources = OrderedDict()
  26. if hasattr(fileOrPath, "read"):
  27. self.file = fileOrPath
  28. else:
  29. try:
  30. # try reading from the resource fork (only works on OS X)
  31. self.file = self.openResourceFork(fileOrPath)
  32. self._readFile()
  33. return
  34. except (ResourceError, IOError):
  35. # if it fails, use the data fork
  36. self.file = self.openDataFork(fileOrPath)
  37. self._readFile()
  38. @staticmethod
  39. def openResourceFork(path):
  40. if hasattr(path, "__fspath__"): # support os.PathLike objects
  41. path = path.__fspath__()
  42. with open(path + "/..namedfork/rsrc", "rb") as resfork:
  43. data = resfork.read()
  44. infile = BytesIO(data)
  45. infile.name = path
  46. return infile
  47. @staticmethod
  48. def openDataFork(path):
  49. with open(path, "rb") as datafork:
  50. data = datafork.read()
  51. infile = BytesIO(data)
  52. infile.name = path
  53. return infile
  54. def _readFile(self):
  55. self._readHeaderAndMap()
  56. self._readTypeList()
  57. def _read(self, numBytes, offset=None):
  58. if offset is not None:
  59. try:
  60. self.file.seek(offset)
  61. except OverflowError:
  62. raise ResourceError("Failed to seek offset ('offset' is too large)")
  63. if self.file.tell() != offset:
  64. raise ResourceError("Failed to seek offset (reached EOF)")
  65. try:
  66. data = self.file.read(numBytes)
  67. except OverflowError:
  68. raise ResourceError("Cannot read resource ('numBytes' is too large)")
  69. if len(data) != numBytes:
  70. raise ResourceError("Cannot read resource (not enough data)")
  71. return data
  72. def _readHeaderAndMap(self):
  73. self.file.seek(0)
  74. headerData = self._read(ResourceForkHeaderSize)
  75. sstruct.unpack(ResourceForkHeader, headerData, self)
  76. # seek to resource map, skip reserved
  77. mapOffset = self.mapOffset + 22
  78. resourceMapData = self._read(ResourceMapHeaderSize, mapOffset)
  79. sstruct.unpack(ResourceMapHeader, resourceMapData, self)
  80. self.absTypeListOffset = self.mapOffset + self.typeListOffset
  81. self.absNameListOffset = self.mapOffset + self.nameListOffset
  82. def _readTypeList(self):
  83. absTypeListOffset = self.absTypeListOffset
  84. numTypesData = self._read(2, absTypeListOffset)
  85. (self.numTypes,) = struct.unpack(">H", numTypesData)
  86. absTypeListOffset2 = absTypeListOffset + 2
  87. for i in range(self.numTypes + 1):
  88. resTypeItemOffset = absTypeListOffset2 + ResourceTypeItemSize * i
  89. resTypeItemData = self._read(ResourceTypeItemSize, resTypeItemOffset)
  90. item = sstruct.unpack(ResourceTypeItem, resTypeItemData)
  91. resType = tostr(item["type"], encoding="mac-roman")
  92. refListOffset = absTypeListOffset + item["refListOffset"]
  93. numRes = item["numRes"] + 1
  94. resources = self._readReferenceList(resType, refListOffset, numRes)
  95. self._resources[resType] = resources
  96. def _readReferenceList(self, resType, refListOffset, numRes):
  97. resources = []
  98. for i in range(numRes):
  99. refOffset = refListOffset + ResourceRefItemSize * i
  100. refData = self._read(ResourceRefItemSize, refOffset)
  101. res = Resource(resType)
  102. res.decompile(refData, self)
  103. resources.append(res)
  104. return resources
  105. def __getitem__(self, resType):
  106. return self._resources[resType]
  107. def __delitem__(self, resType):
  108. del self._resources[resType]
  109. def __setitem__(self, resType, resources):
  110. self._resources[resType] = resources
  111. def __len__(self):
  112. return len(self._resources)
  113. def __iter__(self):
  114. return iter(self._resources)
  115. def keys(self):
  116. return self._resources.keys()
  117. @property
  118. def types(self):
  119. """A list of the types of resources in the resource fork."""
  120. return list(self._resources.keys())
  121. def countResources(self, resType):
  122. """Return the number of resources of a given type."""
  123. try:
  124. return len(self[resType])
  125. except KeyError:
  126. return 0
  127. def getIndices(self, resType):
  128. """Returns a list of indices of resources of a given type."""
  129. numRes = self.countResources(resType)
  130. if numRes:
  131. return list(range(1, numRes + 1))
  132. else:
  133. return []
  134. def getNames(self, resType):
  135. """Return list of names of all resources of a given type."""
  136. return [res.name for res in self.get(resType, []) if res.name is not None]
  137. def getIndResource(self, resType, index):
  138. """Return resource of given type located at an index ranging from 1
  139. to the number of resources for that type, or None if not found.
  140. """
  141. if index < 1:
  142. return None
  143. try:
  144. res = self[resType][index - 1]
  145. except (KeyError, IndexError):
  146. return None
  147. return res
  148. def getNamedResource(self, resType, name):
  149. """Return the named resource of given type, else return None."""
  150. name = tostr(name, encoding="mac-roman")
  151. for res in self.get(resType, []):
  152. if res.name == name:
  153. return res
  154. return None
  155. def close(self):
  156. if not self.file.closed:
  157. self.file.close()
  158. class Resource(object):
  159. """Represents a resource stored within a resource fork.
  160. Attributes:
  161. type: resource type.
  162. data: resource data.
  163. id: ID.
  164. name: resource name.
  165. attr: attributes.
  166. """
  167. def __init__(
  168. self, resType=None, resData=None, resID=None, resName=None, resAttr=None
  169. ):
  170. self.type = resType
  171. self.data = resData
  172. self.id = resID
  173. self.name = resName
  174. self.attr = resAttr
  175. def decompile(self, refData, reader):
  176. sstruct.unpack(ResourceRefItem, refData, self)
  177. # interpret 3-byte dataOffset as (padded) ULONG to unpack it with struct
  178. (self.dataOffset,) = struct.unpack(">L", bytesjoin([b"\0", self.dataOffset]))
  179. absDataOffset = reader.dataOffset + self.dataOffset
  180. (dataLength,) = struct.unpack(">L", reader._read(4, absDataOffset))
  181. self.data = reader._read(dataLength)
  182. if self.nameOffset == -1:
  183. return
  184. absNameOffset = reader.absNameListOffset + self.nameOffset
  185. (nameLength,) = struct.unpack("B", reader._read(1, absNameOffset))
  186. (name,) = struct.unpack(">%ss" % nameLength, reader._read(nameLength))
  187. self.name = tostr(name, encoding="mac-roman")
  188. ResourceForkHeader = """
  189. > # big endian
  190. dataOffset: L
  191. mapOffset: L
  192. dataLen: L
  193. mapLen: L
  194. """
  195. ResourceForkHeaderSize = sstruct.calcsize(ResourceForkHeader)
  196. ResourceMapHeader = """
  197. > # big endian
  198. attr: H
  199. typeListOffset: H
  200. nameListOffset: H
  201. """
  202. ResourceMapHeaderSize = sstruct.calcsize(ResourceMapHeader)
  203. ResourceTypeItem = """
  204. > # big endian
  205. type: 4s
  206. numRes: H
  207. refListOffset: H
  208. """
  209. ResourceTypeItemSize = sstruct.calcsize(ResourceTypeItem)
  210. ResourceRefItem = """
  211. > # big endian
  212. id: h
  213. nameOffset: h
  214. attr: B
  215. dataOffset: 3s
  216. reserved: L
  217. """
  218. ResourceRefItemSize = sstruct.calcsize(ResourceRefItem)