kdbxtool.parsing.context¶
Parsing and building context classes for binary data.
This module provides structured helpers for reading and writing binary data with good error messages and type safety.
Classes
|
Stateful writer for building binary data. |
|
Stateful reader for binary data with error context tracking. |
- class kdbxtool.parsing.context.ParseContext(data, offset=0, _path=<factory>)[source]¶
Bases:
objectStateful reader for binary data with error context tracking.
Tracks the current offset and a path of nested scopes for error messages. When parsing fails, error messages include the full path to the failure point.
Example
ctx = ParseContext(data) with ctx.scope(“header”):
magic = ctx.read(8, “magic”) with ctx.scope(“version”):
major = ctx.read_u16(“major”)
# Error would show: “Unexpected EOF at header/version/major, offset 10”
- read(n, name='')[source]¶
Read n bytes from current position.
- Parameters:
- Returns:
The bytes read
- Raises:
CorruptedDataError – If not enough bytes available
- Return type:
- read_bytes_prefixed(name='')[source]¶
Read length-prefixed bytes (4-byte little-endian length prefix).
- skip(n, name='')[source]¶
Skip n bytes.
- Parameters:
- Raises:
CorruptedDataError – If not enough bytes available
- Return type:
None
- class kdbxtool.parsing.context.BuildContext(_parts=<factory>)[source]¶
Bases:
objectStateful writer for building binary data.
Accumulates bytes in a list and joins them efficiently at the end.
Example
ctx = BuildContext() ctx.write(MAGIC_BYTES) ctx.write_u32(version) ctx.write_tlv(FIELD_TYPE, field_data) result = ctx.build()
- write_u16(value)[source]¶
Write unsigned 16-bit little-endian integer.
- Parameters:
value (int)
- Return type:
None
- write_u32(value)[source]¶
Write unsigned 32-bit little-endian integer.
- Parameters:
value (int)
- Return type:
None
- write_u64(value)[source]¶
Write unsigned 64-bit little-endian integer.
- Parameters:
value (int)
- Return type:
None
- write_bytes_prefixed(data)[source]¶
Write length-prefixed bytes (4-byte little-endian length prefix).
- Parameters:
data (bytes)
- Return type:
None