Text Fields¶
Bytes-Based Fields¶
- class pcapkit.corekit.fields.strings.BytesField(length, default=NO_VALUE, callback=lambda *_: ...)[source]¶
Bases:
_TextField[bytes]Bytes value for protocol fields.
- Parameters:
length (
int|Callable[[dict[str,Any]],int]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.default (
bytes|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- class pcapkit.corekit.fields.strings.PaddingField(length=None, default=NO_VALUE, callback=lambda *_: ..., *, align=None, after=None)[source]¶
Bases:
BytesFieldBytes value for protocol fields.
- Parameters:
length (
int|Callable[[dict[str,Any]],int] |None) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument. Left out whenalignsizes the field.default (
bytes|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.align (
int|None) – Boundary, in octets, that the padding reaches, given withafterin place oflength. The field is thenpad_length()octets long.after (
str|Callable[[dict[str,Any]],int] |None) – What the padding follows: the name of an earlier field whose value is its length in octets, or a callable that accepts the current packet and returns that length.
- Raises:
FieldValueError – If
lengthandalignare both given or both left out, if only one ofalignandafteris given, ifafteris neither astrnor a callable, or ifalignis not positive.
Padding packs back as the octets it was given, so a schema unpacked from non-zero padding rebuilds byte for byte. An absent or empty value packs as zeros – the default for a freshly built packet – and a value of another width is truncated or zero-filled to the field’s, as
struct’ssformat does.Changed in version 1.6.0: Takes
alignandafteras an alternative tolength(#2614).
String-Based Fields¶
- class pcapkit.corekit.fields.strings.StringField(length, default=NO_VALUE, encoding=None, errors='strict', unquote=False, callback=lambda *_: ...)[source]¶
Bases:
_TextField[str]String value for protocol fields.
- Parameters:
length (
int|Callable[[dict[str,Any]],int]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.default (
str|NoValueType) – Field default value, if any.encoding (
str|None) – The encoding with which to decode thebytes, and with which astris encoded when packing. If not provided,pcapkitwill first try detecting its encoding usingchardet. The fallback encoding is UTF-8.errors (
Literal['strict','ignore','replace']) – The error handling scheme to use for the handling of decoding errors. The default is'strict'. Should decoding fail under it, the field retries with'replace'rather than raising, so an octet the charset cannot decode reads as U+FFFD – the octets themselves are not lost, see below. A codec that raises under'replace'as well, such asidna, which supports'strict'alone, decodes as UTF-8 with'replace'instead, and its octets are not lost either. Other possible values are'ignore'and'replace'as well as any other name registered withcodecs.register_error()that can handleUnicodeDecodeError.unquote (
bool) – Whether to unquote the decoded string as a URL. Should decoding fail, the method will try again replacing'%'with'\x'then decoding theurlas'utf-8'with'replace'for error handling.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
Unpacking is lossless. Where re-encoding the decoded
strwould not reproduce the octets it came from – a detected charset other than UTF-8, an octet the charset cannot decode, a lossyerrorsscheme, or a URL quoting that differs from the original – the field returns aDecodedString, which remembers those octets and packs back as exactly them. The decoded text is the plain decoding, U+FFFD and all: a consumer that only reads the text, such as a dumper writing it back out as UTF-8, sees an ordinarystr. A length of-1is resolved to the number of encoded octets.- _encode(value)[source]¶
Encode a
strvalue the waypre_process()packs it.
- post_process(value, packet)[source]¶
Process field value after parsing (unpacked).
- Parameters:
- Return type:
- Returns:
Processed field value; a
DecodedStringcarryingvaluewherepre_process()would not otherwise reproduce it.
- class pcapkit.corekit.fields.strings.DecodedString(value, raw)[source]¶
Bases:
strDecoded
StringFieldvalue that remembers its octets.StringFieldreturns one where encoding the decoded text would not reproduce the octets it was decoded from, so that the field packs it back as exactly those octets. It is otherwise an ordinarystr, and deliberately so: it compares and hashes as its text, since the text is the value andrawis provenance. Two of them with the same text and different octets therefore compare equal – the alternative, mixingrawinto__eq__(), would makeinfo.comment == 'café'false against a plainstrliteral, which is the comparison callers actually write.Any string operation on one returns a plain
str, which a field then packs with its own encoding; that is intended, as an edited value is no longer the text those octets held.
- class pcapkit.corekit.fields.strings.BitField(length, default=NO_VALUE, namespace=None, callback=lambda *_: ...)[source]¶
Bases:
_TextField[dict[str,Any]]Bit value for protocol fields.
- Parameters:
length (
int) – Field size (in bytes).default (
dict[str,Any] |NoValueType) – Field default value, if any.namespace (
dict[str,Tuple[int,int]] |None) – Field namespace (a dict mapping field name to a tuple of start index, and length of the subfield).callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
Bits that no subfield names are kept as captured: where any of them is set, the unpacked value carries them under an
__unnamed__key, as an integer over the whole field with every named bit cleared, and packing writes them back (#1487). The key is absent when they are all zero, so a value built without it packs them as zeros.- UNNAMED = '__unnamed__'¶
Key of the unpacked value that carries the unnamed bits.
Internal Definitions¶
- class pcapkit.corekit.fields.strings._TextField[_T: (str, bytes, dict[str, Any])](length, default=NO_VALUE, callback=lambda *_: ...)[source]¶
-
Internal text value for protocol fields.
- Parameters:
length (
int|Callable[[dict[str,Any]],int]) – Field size (in bytes); if a callable is given, it should return an integer value and accept the current packet as its only argument.default (
_T|NoValueType) – Field default value, if any.callback (
Callable[[Self,dict[str,Any]],None]) – Callback function to be called uponself.__call__.
- __call__(packet)[source]¶
Update field attributes.
- Parameters:
- Return type:
- Returns:
New instance of
_TextField.
This method will return a new instance of
_TextFieldinstead of updating the current instance.