|
89 | 89 | octets after it, kept whole. Without either, the next word continues the |
90 | 90 | namespace, each bit numbered 32 past the same bit of the word before. |
91 | 91 | * **TLVs.** Bit 28 makes the rest of the region a list of type-length-value |
92 | | - items. An item's data is decoded where its type has a schema whose size it |
93 | | - matches, and kept raw otherwise; a vendor item is split into its header and |
94 | | - raw data. |
| 92 | + items. An item's data is decoded where its type has a schema it does not run |
| 93 | + past, and kept raw otherwise; a vendor item is split into its header and raw |
| 94 | + data. radiotap.org lets a producer leave out the octets that end an item, which |
| 95 | + are then zeros, so an item short of its schema, or of the vendor header, is |
| 96 | + decoded as though they were there. Its decoded data records the cut as |
| 97 | + ``__short_read__``, as :meth:`Schema.unpack <pcapkit.protocols.schema.schema.Schema.unpack>` |
| 98 | + does, so :meth:`Radiotap.make <pcapkit.protocols.link.radiotap.Radiotap.make>` |
| 99 | + writes the octets that were captured. |
95 | 100 | * **What is not decoded.** A radiotap-namespace bit no field is defined for -- |
96 | 101 | 18, FreeBSD's XChannel, which radiotap.org lists as unofficial, and every one |
97 | 102 | numbered 32 or more, the TLV-only types included -- or a field that runs |
@@ -497,8 +502,10 @@ def make(self, |
497 | 502 | fit in 32 bits; if ``fields`` is neither :class:`bytes` nor |
498 | 503 | fields; if the fields do not match the bits of ``present``, |
499 | 504 | a word but the last of which ends the chain, or a value does |
500 | | - not fit its subfield; or if ``it_len`` is given and does not |
501 | | - fit in 16 bits. |
| 505 | + not fit its subfield; if a TLV item's ``__short_read__`` names |
| 506 | + no field and an octet count, or cuts short a vendor header that |
| 507 | + data follows; or if ``it_len`` is given and does not fit in 16 |
| 508 | + bits. |
502 | 509 |
|
503 | 510 | """ |
504 | 511 | for name, value in (('it_version', it_version), ('it_pad', it_pad)): |
@@ -2053,66 +2060,123 @@ def _read_tlv_data(self, tlv_type: 'Enum_FieldType', data: 'bytes') -> 'Data_Fie |
2053 | 2060 |
|
2054 | 2061 | Returns: |
2055 | 2062 | A vendor TLV for the Vendor Namespace type, the field for a type |
2056 | | - whose schema ``data`` fills exactly, and the raw octets otherwise. |
| 2063 | + whose schema ``data`` does not run past, and the raw octets |
| 2064 | + otherwise. Data short of its schema, or of the vendor header, is |
| 2065 | + read with the octets it lacks as zeros, and the result records the |
| 2066 | + cut as ``__short_read__``, which :meth:`_make_tlv_data` replays; |
| 2067 | + it stays raw if the zero-padding limits of |
| 2068 | + :mod:`pcapkit.utilities.config` refuse the zeros. |
2057 | 2069 |
|
2058 | 2070 | """ |
| 2071 | + # NOTE: radiotap.org reads the octets a producer leaves off an item as zeros, so short data is unpacked |
| 2072 | + # at its schema's full length, to the end of a list item, and ``Schema.unpack`` records the cut (#2655). |
| 2073 | + layout = None # type: Optional[tuple[FieldParser, FieldConstructor, Type[Schema_Field]]] |
2059 | 2074 | if tlv_type == Enum_FieldType.Vendor_Namespace: |
2060 | | - size, _ = self._field_size(Schema_VendorTLV) |
2061 | | - if len(data) < size: |
| 2075 | + field_schema = Schema_VendorTLV # type: Type[Schema] |
| 2076 | + length, _ = self._field_size(field_schema) |
| 2077 | + octets = data[:length] |
| 2078 | + else: |
| 2079 | + layout = self._lookup_field(tlv_type) |
| 2080 | + if layout is None: |
2062 | 2081 | return data |
2063 | | - vendor = cast('Schema_VendorTLV', |
2064 | | - Schema_VendorTLV.unpack(data[:size], size, None)) # type: ignore[call-arg,misc] |
2065 | | - return Data_VendorTLV(oui=vendor.oui, subtype=vendor.subtype, presence_type=vendor.presence_type, |
2066 | | - reserved=vendor.reserved, data=data[size:]) |
2067 | | - |
2068 | | - layout = self._lookup_field(tlv_type) |
2069 | | - if layout is None: |
2070 | | - return data |
2071 | | - parser, _, field_schema = layout |
2072 | | - size, rest = self._field_size(field_schema) |
2073 | | - if len(data) < size or (not rest and len(data) != size): |
2074 | | - return data |
2075 | | - # NOTE: a list taking the rest refuses octets that are not whole items |
2076 | | - # of it; the refusal is handled here, so it is quiet. |
| 2082 | + field_schema = layout[2] |
| 2083 | + size, rest = self._field_size(field_schema) |
| 2084 | + if not rest and len(data) > size: |
| 2085 | + return data |
| 2086 | + length = max(len(data), size) |
| 2087 | + if rest and len(data) > size: |
| 2088 | + tail = next(field for field in field_schema.__fields__.values() if field.length < 0) |
| 2089 | + item = tail._item_type if isinstance(tail, ListField) else None # pylint: disable=protected-access |
| 2090 | + if item is not None and item.length > 0: |
| 2091 | + length += -(len(data) - size) % item.length |
| 2092 | + octets = data |
| 2093 | + |
| 2094 | + # NOTE: a limit on zero-padding, or a list taking the rest, may refuse |
| 2095 | + # what it is given; the refusal is handled here, so it is quiet. |
2077 | 2096 | with CaughtByCaller(FieldValueError): |
2078 | 2097 | try: |
2079 | | - schema = cast('Schema_Field', field_schema.unpack(data, len(data), None)) # type: ignore[call-arg,misc] |
| 2098 | + schema = cast('Schema', field_schema.unpack(octets, length, None)) # type: ignore[call-arg,misc] |
2080 | 2099 | except FieldValueError: |
2081 | 2100 | return data |
2082 | | - return parser(tlv_type, schema) |
| 2101 | + if layout is None: |
| 2102 | + vendor = cast('Schema_VendorTLV', schema) |
| 2103 | + entry = Data_VendorTLV(oui=vendor.oui, subtype=vendor.subtype, presence_type=vendor.presence_type, |
| 2104 | + reserved=vendor.reserved, data=data[length:]) # type: Data_Field | Data_VendorTLV |
| 2105 | + else: |
| 2106 | + entry = layout[0](tlv_type, cast('Schema_Field', schema)) |
| 2107 | + |
| 2108 | + record = schema.__dict__.get('__short_read__') |
| 2109 | + if record is not None: |
| 2110 | + entry.__update__({'__short_read__': record}) |
| 2111 | + return entry |
2083 | 2112 |
|
2084 | 2113 | def _make_tlv_data(self, tlv_type: 'Enum_FieldType', |
2085 | 2114 | data: 'Data_Field | Data_VendorTLV | Mapping[str, Any] | bytes') -> 'bytes': |
2086 | 2115 | """Encode a TLV item's data, the inverse of :meth:`_read_tlv_data`. |
2087 | 2116 |
|
2088 | 2117 | Args: |
2089 | 2118 | tlv_type: Type of the item. |
2090 | | - data: The item's data: raw octets, written as they are, or decoded. |
| 2119 | + data: The item's data: raw octets, written as they are, or decoded, |
| 2120 | + cut back as its ``__short_read__`` says when it has one. |
2091 | 2121 |
|
2092 | 2122 | Returns: |
2093 | 2123 | The item's data. |
2094 | 2124 |
|
2095 | 2125 | Raises: |
2096 | 2126 | ProtocolError: If decoded data is given for a type with no layout, |
2097 | | - or does not encode. |
| 2127 | + or does not encode; if its ``__short_read__`` is not a field |
| 2128 | + name and an octet count; or if a vendor TLV whose header is cut |
| 2129 | + short has data. |
2098 | 2130 |
|
2099 | 2131 | """ |
2100 | 2132 | if isinstance(data, (bytes, bytearray)): |
2101 | 2133 | return bytes(data) |
2102 | 2134 | if tlv_type == Enum_FieldType.Vendor_Namespace: |
2103 | 2135 | oui, vendor_data = self._make_vendor_data(tlv_type, Schema_VendorTLV, data) |
2104 | 2136 | values = {'reserved': 0, **data} |
2105 | | - return Schema_VendorTLV( |
| 2137 | + header = self._pack_tlv_data(tlv_type, Schema_VendorTLV( |
2106 | 2138 | oui=oui, |
2107 | 2139 | subtype=self._make_subfield(tlv_type, Schema_VendorTLV, values, 'subtype'), |
2108 | 2140 | presence_type=self._make_subfield(tlv_type, Schema_VendorTLV, values, 'presence_type'), |
2109 | 2141 | reserved=self._make_subfield(tlv_type, Schema_VendorTLV, values, 'reserved'), |
2110 | | - ).pack() + vendor_data |
| 2142 | + ), data) |
| 2143 | + if vendor_data and len(header) < self._field_size(Schema_VendorTLV)[0]: |
| 2144 | + raise ProtocolError(f'{self.alias}: invalid VendorTLV.data: {vendor_data!r}, ' |
| 2145 | + 'the vendor header before it is cut short') |
| 2146 | + return header + vendor_data |
2111 | 2147 | layout = self._lookup_field(tlv_type) |
2112 | 2148 | if layout is None: |
2113 | 2149 | raise ProtocolError(f'{self.alias}: invalid TLV.data: {tlv_type!r} defines no layout, ' |
2114 | 2150 | 'so its data must be bytes') |
2115 | | - return layout[1](tlv_type, data).pack() |
| 2151 | + return self._pack_tlv_data(tlv_type, layout[1](tlv_type, data), data) |
| 2152 | + |
| 2153 | + def _pack_tlv_data(self, tlv_type: 'Enum_FieldType', schema: 'Schema', |
| 2154 | + data: 'Data_Field | Data_VendorTLV | Mapping[str, Any]') -> 'bytes': |
| 2155 | + """Pack a TLV item's schema, cut back as :meth:`_read_tlv_data` recorded. |
| 2156 | +
|
| 2157 | + Args: |
| 2158 | + tlv_type: Type of the item, for the error message. |
| 2159 | + schema: The schema made from ``data``. |
| 2160 | + data: The item's decoded data, whose ``__short_read__``, if any, |
| 2161 | + names the field its octets ended inside and how many of that |
| 2162 | + field's octets there were. |
| 2163 | +
|
| 2164 | + Returns: |
| 2165 | + The packed schema, without the octets past the cut. |
| 2166 | +
|
| 2167 | + Raises: |
| 2168 | + ProtocolError: If ``__short_read__`` is not a field of ``schema`` |
| 2169 | + and a count of octets. |
| 2170 | +
|
| 2171 | + """ |
| 2172 | + record = data.get('__short_read__') |
| 2173 | + if record is not None: |
| 2174 | + if not (isinstance(record, (tuple, list)) and len(record) == 2 and record[0] in schema.__fields__ |
| 2175 | + and isinstance(record[1], int) and not isinstance(record[1], bool) and record[1] >= 0): |
| 2176 | + raise ProtocolError(f'{self.alias}: invalid {tlv_type.name}.__short_read__: {record!r}') |
| 2177 | + # NOTE: ``Schema.pack`` cuts its buffers back to a record it holds. |
| 2178 | + schema.__dict__['__short_read__'] = tuple(record) |
| 2179 | + return schema.pack() |
2116 | 2180 |
|
2117 | 2181 | def _make_vendor_data(self, field_type: 'Enum_FieldType', schema: 'Type[Schema]', |
2118 | 2182 | entry: 'Mapping[str, Any]') -> 'tuple[bytes, bytes]': |
|
0 commit comments