1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
"""
This module defines packable types, that is types than can be easily
converted to a binary format as used in MachO headers.
"""
import struct
import sys
from itertools import chain, starmap
try:
from itertools import imap, izip
except ImportError:
izip, imap = zip, map
__all__ = """
sizeof
BasePackable
Structure
pypackable
p_char
p_byte
p_ubyte
p_short
p_ushort
p_int
p_uint
p_long
p_ulong
p_longlong
p_ulonglong
p_int8
p_uint8
p_int16
p_uint16
p_int32
p_uint32
p_int64
p_uint64
p_float
p_double
""".split()
def sizeof(s):
"""
Return the size of an object when packed
"""
if hasattr(s, "_size_"):
return s._size_
elif isinstance(s, bytes):
return len(s)
raise ValueError(s)
class MetaPackable(type):
"""
Fixed size struct.unpack-able types use from_tuple as their designated
initializer
"""
def from_mmap(cls, mm, ptr, **kw):
return cls.from_str(mm[ptr : ptr + cls._size_], **kw) # noqa: E203
def from_fileobj(cls, f, **kw):
return cls.from_str(f.read(cls._size_), **kw)
def from_str(cls, s, **kw):
endian = kw.get("_endian_", cls._endian_)
return cls.from_tuple(struct.unpack(endian + cls._format_, s), **kw)
def from_tuple(cls, tpl, **kw):
return cls(tpl[0], **kw)
class BasePackable(object):
_endian_ = ">"
def to_str(self):
raise NotImplementedError
def to_fileobj(self, f):
f.write(self.to_str())
def to_mmap(self, mm, ptr):
mm[ptr : ptr + self._size_] = self.to_str() # noqa: E203
# This defines a class with a custom metaclass, we'd normally
# use "class Packable(BasePackable, metaclass=MetaPackage)",
# but that syntax is not valid in Python 2 (and likewise the
# python 2 syntax is not valid in Python 3)
def _make():
def to_str(self):
cls = type(self)
endian = getattr(self, "_endian_", cls._endian_)
return struct.pack(endian + cls._format_, self)
return MetaPackable("Packable", (BasePackable,), {"to_str": to_str})
Packable = _make()
del _make
def pypackable(name, pytype, format):
"""
Create a "mix-in" class with a python type and a
Packable with the given struct format
"""
size, items = _formatinfo(format)
def __new__(cls, *args, **kwds):
if "_endian_" in kwds:
_endian_ = kwds.pop("_endian_")
else:
_endian_ = cls._endian_
result = pytype.__new__(cls, *args, **kwds)
result._endian_ = _endian_
return result
return type(Packable)(
name,
(pytype, Packable),
{"_format_": format, "_size_": size, "_items_": items, "__new__": __new__},
)
def _formatinfo(format):
"""
Calculate the size and number of items in a struct format.
"""
size = struct.calcsize(format)
return size, len(struct.unpack(format, b"\x00" * size))
class MetaStructure(MetaPackable):
"""
The metaclass of Structure objects that does all the magic.
Since we can assume that all Structures have a fixed size,
we can do a bunch of calculations up front and pack or
unpack the whole thing in one struct call.
"""
def __new__(cls, clsname, bases, dct):
fields = dct["_fields_"]
names = []
types = []
structmarks = []
format = ""
items = 0
size = 0
def struct_property(name, typ):
def _get(self):
return self._objects_[name]
def _set(self, obj):
if type(obj) is not typ:
obj = typ(obj)
self._objects_[name] = obj
return property(_get, _set, typ.__name__)
for name, typ in fields:
dct[name] = struct_property(name, typ)
names.append(name)
types.append(typ)
format += typ._format_
size += typ._size_
if typ._items_ > 1:
structmarks.append((items, typ._items_, typ))
items += typ._items_
dct["_structmarks_"] = structmarks
dct["_names_"] = names
dct["_types_"] = types
dct["_size_"] = size
dct["_items_"] = items
dct["_format_"] = format
return super(MetaStructure, cls).__new__(cls, clsname, bases, dct)
def from_tuple(cls, tpl, **kw):
values = []
current = 0
for begin, length, typ in cls._structmarks_:
if begin > current:
values.extend(tpl[current:begin])
current = begin + length
values.append(typ.from_tuple(tpl[begin:current], **kw))
values.extend(tpl[current:])
return cls(*values, **kw)
# See metaclass discussion earlier in this file
def _make():
class_dict = {}
class_dict["_fields_"] = ()
def as_method(function):
class_dict[function.__name__] = function
@as_method
def __init__(self, *args, **kwargs):
if len(args) == 1 and not kwargs and type(args[0]) is type(self):
kwargs = args[0]._objects_
args = ()
self._objects_ = {}
iargs = chain(izip(self._names_, args), kwargs.items())
for key, value in iargs:
if key not in self._names_ and key != "_endian_":
raise TypeError
setattr(self, key, value)
for key, typ in izip(self._names_, self._types_):
if key not in self._objects_:
self._objects_[key] = typ()
@as_method
def _get_packables(self):
for obj in imap(self._objects_.__getitem__, self._names_):
if hasattr(obj, "_get_packables"):
for obj in obj._get_packables():
yield obj
else:
yield obj
@as_method
def to_str(self):
return struct.pack(self._endian_ + self._format_, *self._get_packables())
@as_method
def __cmp__(self, other):
if type(other) is not type(self):
raise TypeError(
"Cannot compare objects of type %r to objects of type %r"
% (type(other), type(self))
)
if sys.version_info[0] == 2:
_cmp = cmp # noqa: F821
else:
def _cmp(a, b):
if a < b:
return -1
elif a > b:
return 1
elif a == b:
return 0
else:
raise TypeError()
for cmpval in starmap(
_cmp, izip(self._get_packables(), other._get_packables())
):
if cmpval != 0:
return cmpval
return 0
@as_method
def __eq__(self, other):
r = self.__cmp__(other)
return r == 0
@as_method
def __ne__(self, other):
r = self.__cmp__(other)
return r != 0
@as_method
def __lt__(self, other):
r = self.__cmp__(other)
return r < 0
@as_method
def __le__(self, other):
r = self.__cmp__(other)
return r <= 0
@as_method
def __gt__(self, other):
r = self.__cmp__(other)
return r > 0
@as_method
def __ge__(self, other):
r = self.__cmp__(other)
return r >= 0
@as_method
def __repr__(self):
result = []
result.append("<")
result.append(type(self).__name__)
for nm in self._names_:
result.append(" %s=%r" % (nm, getattr(self, nm)))
result.append(">")
return "".join(result)
return MetaStructure("Structure", (BasePackable,), class_dict)
Structure = _make()
del _make
try:
long
except NameError:
long = int
# export common packables with predictable names
p_char = pypackable("p_char", bytes, "c")
p_int8 = pypackable("p_int8", int, "b")
p_uint8 = pypackable("p_uint8", int, "B")
p_int16 = pypackable("p_int16", int, "h")
p_uint16 = pypackable("p_uint16", int, "H")
p_int32 = pypackable("p_int32", int, "i")
p_uint32 = pypackable("p_uint32", long, "I")
p_int64 = pypackable("p_int64", long, "q")
p_uint64 = pypackable("p_uint64", long, "Q")
p_float = pypackable("p_float", float, "f")
p_double = pypackable("p_double", float, "d")
# Deprecated names, need trick to emit deprecation warning.
p_byte = p_int8
p_ubyte = p_uint8
p_short = p_int16
p_ushort = p_uint16
p_int = p_long = p_int32
p_uint = p_ulong = p_uint32
p_longlong = p_int64
p_ulonglong = p_uint64
|