本文整理汇总了Python中ctypes.LittleEndianStructure方法的典型用法代码示例。如果您正苦于以下问题:Python ctypes.LittleEndianStructure方法的具体用法?Python ctypes.LittleEndianStructure怎么用?Python ctypes.LittleEndianStructure使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ctypes
的用法示例。
在下文中一共展示了ctypes.LittleEndianStructure方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def __init__(self, ei_class, ei_data):
if ei_class not in (ELFCLASS32, ELFCLASS64):
raise ValueError("Unknown ei_class=%d" % ei_class)
if ei_data not in (ELFDATA2LSB, ELFDATA2MSB):
raise ValueError("Unknown ei_data=%d" % ei_data)
self.ei_class = ei_class
self.ei_data = ei_data
if self.ei_class == ELFCLASS32:
self.wordsize = 32
elif self.ei_class == ELFCLASS64:
self.wordsize = 64
if self.ei_data == ELFDATA2LSB:
self.structure = LittleEndianStructure
self.endianess = "LSB"
elif self.ei_data == ELFDATA2MSB:
self.structure = BigEndianStructure
self.endianess = "MSB"
示例2: get_base_class_and_magic_number
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def get_base_class_and_magic_number(lib_file, seek=None):
if seek is None:
seek = lib_file.tell()
else:
lib_file.seek(seek)
magic_number = ctypes.c_uint32.from_buffer_copy(
lib_file.read(ctypes.sizeof(ctypes.c_uint32))).value
# Handle wrong byte order
if magic_number in [FAT_CIGAM, FAT_CIGAM_64, MH_CIGAM, MH_CIGAM_64]:
if sys.byteorder == "little":
BaseClass = ctypes.BigEndianStructure
else:
BaseClass = ctypes.LittleEndianStructure
magic_number = swap32(magic_number)
else:
BaseClass = ctypes.Structure
lib_file.seek(seek)
return BaseClass, magic_number
示例3: test_little_endian_structure
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def test_little_endian_structure(self):
class PaddedStruct(ctypes.LittleEndianStructure):
_fields_ = [
('a', ctypes.c_uint8),
('b', ctypes.c_uint16)
]
expected = np.dtype([
('a', '<B'),
('b', '<H')
], align=True)
self.check(PaddedStruct, expected)
示例4: test_little_endian_structure_packed
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def test_little_endian_structure_packed(self):
class LittleEndStruct(ctypes.LittleEndianStructure):
_fields_ = [
('one', ctypes.c_uint8),
('two', ctypes.c_uint32)
]
_pack_ = 1
expected = np.dtype([('one', 'u1'), ('two', '<u4')])
self.check(LittleEndStruct, expected)
示例5: __new__
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def __new__(cls, is_big_endian = False, is_64_bits = False):
actual_class = type(
cls.__name__,
(
BigEndianStructure
if is_big_endian
else LittleEndianStructure,
VariableEndiannessAndWordsizeStructure,
),
{
**{name: getattr(cls, name) for name in dir(cls) if '__' not in name or name == '__init__'},
'is_big_endian': is_big_endian,
'is_64_bits': is_64_bits,
'_pack_': True,
'_fields_': [
(
field[0], field[1] if is_64_bits else {
c_int64: c_int32,
c_uint64: c_uint32
}.get(field[1], field[1]), field[2] if len(field) > 2 else None
)[:3 if len(field) > 2 else 2]
for field
in cls._fields_
]
}
)
return actual_class()
示例6: GenRegX
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def GenRegX(letter: str):
assert letter in REG_LETTERS
# ctypes' docs (16.12.1.11. Structure/union alignment and byte order) say LittleEndianUnion exists,
# like LittleEndianStructure but it doesn't
class _Reg32(ctypes.Union):
_pack_ = 1
_fields_ = [
(f'e{letter}x', udword),
(letter + 'x', uword)
]
return _Reg32
示例7: _calculate_key2
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def _calculate_key2(password: bytes, cycles: int, salt: bytes, digest: str):
"""Calculate 7zip AES encryption key.
It utilize ctypes and memoryview buffer and zero-copy technology on Python."""
if digest not in ('sha256'):
raise ValueError('Unknown digest method for password protection.')
assert cycles <= 0x3f
if cycles == 0x3f:
key = bytes(bytearray(salt + password + bytes(32))[:32]) # type: bytes
else:
rounds = 1 << cycles
m = _hashlib.new(digest)
length = len(salt) + len(password)
class RoundBuf(ctypes.LittleEndianStructure):
_pack_ = 1
_fields_ = [
('saltpassword', ctypes.c_ubyte * length),
('round', ctypes.c_uint64)
]
buf = RoundBuf()
for i, c in enumerate(salt + password):
buf.saltpassword[i] = c
buf.round = 0
mv = memoryview(buf) # type: ignore # noqa
while buf.round < rounds:
m.update(mv)
buf.round += 1
key = m.digest()[:32]
return key
示例8: create_from_buffer
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def create_from_buffer(cls, buffer: IOBase, file_header=None):
if type(buffer) in [bytes, bytearray]:
buffer = BytesIO(buffer)
# Read bytes up until the variable-sized data
base_bytes = buffer.read(cls.TX.offset)
n_bytes = ctypes.c_uint32.from_buffer_copy(base_bytes, cls.NumberOfBytes.offset).value
n_tx = ctypes.c_uint16.from_buffer_copy(base_bytes, cls.Ntx.offset).value
n_rx = ctypes.c_uint16.from_buffer_copy(base_bytes, cls.Nrx.offset).value
# Read remaining bytes
remaining_bytes = buffer.read(n_bytes - cls.TX.offset + cls.NumberOfBytes.size)
# Create new class dynamically with string array at the correct size
new_name = cls.__name__ + '_ntx{}_nrx{}'.format(n_tx, n_rx)
new_fields = cls._fields_.copy()
tx_idx = [i for i, (name, fieldtype) in enumerate(cls._fields_) if name == 'TX'][0]
rx_idx = [i for i, (name, fieldtype) in enumerate(cls._fields_) if name == 'RX'][0]
new_fields[tx_idx] = ('TX', KMRawRangeAngle78_TX * n_tx)
new_fields[rx_idx] = ('RX', KMRawRangeAngle78_RX * n_rx)
new_cls = type(new_name, (ctypes.LittleEndianStructure,), {
'__str__': cls.__str__,
'_pack_': cls._pack_,
'_fields_': new_fields
})
all_bytes = base_bytes + remaining_bytes
obj = new_cls.from_buffer_copy(all_bytes)
# Checksum (not crc16, but a straight sum of bytes with overflow)
chk = (sum(all_bytes[new_cls.DatagramType.offset:new_cls.EndID.offset]) & 0xFFFF)
if chk != obj.Checksum:
warning_str = '{}: Checksum failed'.format(cls.__name__)
warnings.warn(warning_str)
return obj
示例9: unpack_dataruns
# 需要导入模块: import ctypes [as 别名]
# 或者: from ctypes import LittleEndianStructure [as 别名]
def unpack_dataruns(str):
dataruns = []
numruns = 0
pos = 0
prevoffset = 0
error = ''
c_uint8 = ctypes.c_uint8
class Length_bits(ctypes.LittleEndianStructure):
_fields_ = [
("lenlen", c_uint8, 4),
("offlen", c_uint8, 4),
]
class Lengths(ctypes.Union):
_fields_ = [("b", Length_bits),
("asbyte", c_uint8)]
lengths = Lengths()
# mftutils.hexdump(str,':',16)
while (True):
lengths.asbyte = struct.unpack("B", str[pos])[0]
pos += 1
if lengths.asbyte == 0x00:
break
if (lengths.b.lenlen > 6 or lengths.b.lenlen == 0):
error = "Datarun oddity."
break
len = bitparse.parse_little_endian_signed(str[pos:pos + lengths.b.lenlen])
# print lengths.b.lenlen, lengths.b.offlen, len
pos += lengths.b.lenlen
if (lengths.b.offlen > 0):
offset = bitparse.parse_little_endian_signed(str[pos:pos + lengths.b.offlen])
offset = offset + prevoffset
prevoffset = offset
pos += lengths.b.offlen
else: # Sparse
offset = 0
pos += 1
dataruns.append([len, offset])
numruns += 1
# print "Lenlen: %d Offlen: %d Len: %d Offset: %d" % (lengths.b.lenlen, lengths.b.offlen, len, offset)
return numruns, dataruns, error