Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 1 | # Copyright 2021 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 15 | import collections |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 16 | import struct |
| 17 | |
| 18 | |
| 19 | class PackedStruct(object): |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 20 | """Class representing a C style packed structure. |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 21 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 22 | Derived classes need to provide a dictionary where the keys are the attributes |
| 23 | and the values are the format characters for each field. e.g. |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 24 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 25 | class Foo(PackedStruct): |
| 26 | _FIELDS = { |
| 27 | x: 'I', |
| 28 | name: '64s', |
| 29 | } |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 30 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 31 | In this case Foo.x will represent an "unsigned int" C value, while Foo.name |
| 32 | will be a "char[64]" C value. |
| 33 | """ |
| 34 | _FIELDS: collections.OrderedDict |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 35 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 36 | def __init__(self, *args, **kwargs): |
| 37 | self._fmt = '<' + ''.join(fmt for fmt in self._FIELDS.values()) |
| 38 | for name in self._FIELDS: |
| 39 | setattr(self, name, None) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 40 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 41 | for name, val in zip(self._FIELDS.keys(), args): |
| 42 | setattr(self, name, val) |
| 43 | for name, val in kwargs.items(): |
| 44 | setattr(self, name, val) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 45 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 46 | def __repr__(self): |
| 47 | return '{} {{\n'.format(self.__class__.__name__) + ',\n'.join( |
| 48 | ' {!r}: {!r}'.format(k, getattr(self, k)) |
| 49 | for k in self._FIELDS) + '\n}' |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 50 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 51 | def __str__(self): |
| 52 | return struct.pack(self._fmt, *(getattr(self, x) for x in self._FIELDS)) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 53 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 54 | def __bytes__(self): |
| 55 | return struct.pack(self._fmt, *(getattr(self, x) for x in self._FIELDS)) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 56 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 57 | def __len__(self): |
| 58 | return struct.calcsize(self._fmt) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 59 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 60 | @classmethod |
| 61 | def from_bytes(cls, data): |
| 62 | fmt_str = '<' + ''.join(fmt for fmt in cls._FIELDS.values()) |
| 63 | return cls(*struct.unpack(fmt_str, data)) |