sstruct.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. """sstruct.py -- SuperStruct
  2. Higher level layer on top of the struct module, enabling to
  3. bind names to struct elements. The interface is similar to
  4. struct, except the objects passed and returned are not tuples
  5. (or argument lists), but dictionaries or instances.
  6. Just like struct, we use fmt strings to describe a data
  7. structure, except we use one line per element. Lines are
  8. separated by newlines or semi-colons. Each line contains
  9. either one of the special struct characters ('@', '=', '<',
  10. '>' or '!') or a 'name:formatchar' combo (eg. 'myFloat:f').
  11. Repetitions, like the struct module offers them are not useful
  12. in this context, except for fixed length strings (eg. 'myInt:5h'
  13. is not allowed but 'myString:5s' is). The 'x' fmt character
  14. (pad byte) is treated as 'special', since it is by definition
  15. anonymous. Extra whitespace is allowed everywhere.
  16. The sstruct module offers one feature that the "normal" struct
  17. module doesn't: support for fixed point numbers. These are spelled
  18. as "n.mF", where n is the number of bits before the point, and m
  19. the number of bits after the point. Fixed point numbers get
  20. converted to floats.
  21. pack(fmt, object):
  22. 'object' is either a dictionary or an instance (or actually
  23. anything that has a __dict__ attribute). If it is a dictionary,
  24. its keys are used for names. If it is an instance, it's
  25. attributes are used to grab struct elements from. Returns
  26. a string containing the data.
  27. unpack(fmt, data, object=None)
  28. If 'object' is omitted (or None), a new dictionary will be
  29. returned. If 'object' is a dictionary, it will be used to add
  30. struct elements to. If it is an instance (or in fact anything
  31. that has a __dict__ attribute), an attribute will be added for
  32. each struct element. In the latter two cases, 'object' itself
  33. is returned.
  34. unpack2(fmt, data, object=None)
  35. Convenience function. Same as unpack, except data may be longer
  36. than needed. The returned value is a tuple: (object, leftoverdata).
  37. calcsize(fmt)
  38. like struct.calcsize(), but uses our own fmt strings:
  39. it returns the size of the data in bytes.
  40. """
  41. from fontTools.misc.fixedTools import fixedToFloat as fi2fl, floatToFixed as fl2fi
  42. from fontTools.misc.textTools import tobytes, tostr
  43. import struct
  44. import re
  45. __version__ = "1.2"
  46. __copyright__ = "Copyright 1998, Just van Rossum <just@letterror.com>"
  47. class Error(Exception):
  48. pass
  49. def pack(fmt, obj):
  50. formatstring, names, fixes = getformat(fmt, keep_pad_byte=True)
  51. elements = []
  52. if not isinstance(obj, dict):
  53. obj = obj.__dict__
  54. for name in names:
  55. value = obj[name]
  56. if name in fixes:
  57. # fixed point conversion
  58. value = fl2fi(value, fixes[name])
  59. elif isinstance(value, str):
  60. value = tobytes(value)
  61. elements.append(value)
  62. data = struct.pack(*(formatstring,) + tuple(elements))
  63. return data
  64. def unpack(fmt, data, obj=None):
  65. if obj is None:
  66. obj = {}
  67. data = tobytes(data)
  68. formatstring, names, fixes = getformat(fmt)
  69. if isinstance(obj, dict):
  70. d = obj
  71. else:
  72. d = obj.__dict__
  73. elements = struct.unpack(formatstring, data)
  74. for i in range(len(names)):
  75. name = names[i]
  76. value = elements[i]
  77. if name in fixes:
  78. # fixed point conversion
  79. value = fi2fl(value, fixes[name])
  80. elif isinstance(value, bytes):
  81. try:
  82. value = tostr(value)
  83. except UnicodeDecodeError:
  84. pass
  85. d[name] = value
  86. return obj
  87. def unpack2(fmt, data, obj=None):
  88. length = calcsize(fmt)
  89. return unpack(fmt, data[:length], obj), data[length:]
  90. def calcsize(fmt):
  91. formatstring, names, fixes = getformat(fmt)
  92. return struct.calcsize(formatstring)
  93. # matches "name:formatchar" (whitespace is allowed)
  94. _elementRE = re.compile(
  95. r"\s*" # whitespace
  96. r"([A-Za-z_][A-Za-z_0-9]*)" # name (python identifier)
  97. r"\s*:\s*" # whitespace : whitespace
  98. r"([xcbB?hHiIlLqQfd]|" # formatchar...
  99. r"[0-9]+[ps]|" # ...formatchar...
  100. r"([0-9]+)\.([0-9]+)(F))" # ...formatchar
  101. r"\s*" # whitespace
  102. r"(#.*)?$" # [comment] + end of string
  103. )
  104. # matches the special struct fmt chars and 'x' (pad byte)
  105. _extraRE = re.compile(r"\s*([x@=<>!])\s*(#.*)?$")
  106. # matches an "empty" string, possibly containing whitespace and/or a comment
  107. _emptyRE = re.compile(r"\s*(#.*)?$")
  108. _fixedpointmappings = {8: "b", 16: "h", 32: "l"}
  109. _formatcache = {}
  110. def getformat(fmt, keep_pad_byte=False):
  111. fmt = tostr(fmt, encoding="ascii")
  112. try:
  113. formatstring, names, fixes = _formatcache[fmt]
  114. except KeyError:
  115. lines = re.split("[\n;]", fmt)
  116. formatstring = ""
  117. names = []
  118. fixes = {}
  119. for line in lines:
  120. if _emptyRE.match(line):
  121. continue
  122. m = _extraRE.match(line)
  123. if m:
  124. formatchar = m.group(1)
  125. if formatchar != "x" and formatstring:
  126. raise Error("a special fmt char must be first")
  127. else:
  128. m = _elementRE.match(line)
  129. if not m:
  130. raise Error("syntax error in fmt: '%s'" % line)
  131. name = m.group(1)
  132. formatchar = m.group(2)
  133. if keep_pad_byte or formatchar != "x":
  134. names.append(name)
  135. if m.group(3):
  136. # fixed point
  137. before = int(m.group(3))
  138. after = int(m.group(4))
  139. bits = before + after
  140. if bits not in [8, 16, 32]:
  141. raise Error("fixed point must be 8, 16 or 32 bits long")
  142. formatchar = _fixedpointmappings[bits]
  143. assert m.group(5) == "F"
  144. fixes[name] = after
  145. formatstring = formatstring + formatchar
  146. _formatcache[fmt] = formatstring, names, fixes
  147. return formatstring, names, fixes
  148. def _test():
  149. fmt = """
  150. # comments are allowed
  151. > # big endian (see documentation for struct)
  152. # empty lines are allowed:
  153. ashort: h
  154. along: l
  155. abyte: b # a byte
  156. achar: c
  157. astr: 5s
  158. afloat: f; adouble: d # multiple "statements" are allowed
  159. afixed: 16.16F
  160. abool: ?
  161. apad: x
  162. """
  163. print("size:", calcsize(fmt))
  164. class foo(object):
  165. pass
  166. i = foo()
  167. i.ashort = 0x7FFF
  168. i.along = 0x7FFFFFFF
  169. i.abyte = 0x7F
  170. i.achar = "a"
  171. i.astr = "12345"
  172. i.afloat = 0.5
  173. i.adouble = 0.5
  174. i.afixed = 1.5
  175. i.abool = True
  176. data = pack(fmt, i)
  177. print("data:", repr(data))
  178. print(unpack(fmt, data))
  179. i2 = foo()
  180. unpack(fmt, data, i2)
  181. print(vars(i2))
  182. if __name__ == "__main__":
  183. _test()