Python ctypes.LittleEndianStructure() Examples
The following are 19
code examples of ctypes.LittleEndianStructure().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ctypes
, or try the search function
.
Example #1
Source File: elf.py From wsym with MIT License | 6 votes |
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"
Example #2
Source File: macosx_libfile.py From rules_pip with MIT License | 6 votes |
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
Example #3
Source File: test_dtype.py From pySINDy with MIT License | 5 votes |
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)
Example #4
Source File: test_dtype.py From twitter-stock-recommendation with MIT License | 5 votes |
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)
Example #5
Source File: test_dtype.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
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)
Example #6
Source File: kongsberg.py From pyxtf with MIT License | 5 votes |
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
Example #7
Source File: test_dtype.py From coffeegrindsize with MIT License | 5 votes |
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)
Example #8
Source File: test_dtype.py From coffeegrindsize with MIT License | 5 votes |
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)
Example #9
Source File: helpers.py From py7zr with GNU Lesser General Public License v2.1 | 5 votes |
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
Example #10
Source File: Registers.py From PyVM with MIT License | 5 votes |
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
Example #11
Source File: test_dtype.py From recruit with Apache License 2.0 | 5 votes |
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)
Example #12
Source File: elf.py From vmlinux-to-elf with GNU General Public License v3.0 | 5 votes |
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()
Example #13
Source File: test_dtype.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #14
Source File: test_dtype.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
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)
Example #15
Source File: test_dtype.py From GraphicDesignPatternByPython with MIT License | 5 votes |
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)
Example #16
Source File: test_dtype.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #17
Source File: test_dtype.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
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)
Example #18
Source File: test_dtype.py From recruit with Apache License 2.0 | 5 votes |
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)
Example #19
Source File: mft.py From Fastir_Collector with GNU General Public License v3.0 | 4 votes |
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