Skip to main content

Read, edit, export, and rebuild .NET BinaryFormatter / NRBF streams.

Project description

dnbflib

dnbflib reads, indexes, edits, exports, and rebuilds .NET BinaryFormatter / NRBF binary streams in Python.

Warning

BinaryFormatter is unsafe for untrusted data. Use this library for inspection, migration, recovery, and editing of files you already trust. Do not accept arbitrary BinaryFormatter payloads from users or the network.

Status

The current maintained APIs are:

  • DNBFDocument for memory-conscious object graph traversal and edits.
  • export_dnbf_to_yaml / rebuild_yaml_export for lossless editable YAML packages.
  • DNBFRecordStore for SQLite-backed record storage and inspection.
  • DNBFWriter for rebuilding streams from raw records or writable record objects.

Requirements

  • Python 3.11 or newer.
  • pydatastreams, installed automatically by package managers.

Installation

From PyPI, once published:

pip install dnbflib

From a local checkout:

python -m pip install -e .

Object traversal

from dnbflib import DNBFDocument

with DNBFDocument.open("save.dat") as doc:
    life = doc.one(class_name="Life", where=lambda obj: obj.member("Name").value == "Alex")
    finances = life.member("Finances").deref()
    finances.member("BankBalance").set(123456)
    doc.write("edited.dat")

DNBFDocument.open() uses an mmap-backed source file and an offset index. Prefer doc.write(...) over doc.to_bytes() for large files because it streams output chunks.

If more than one object matches a class name, use one(..., where=...) to disambiguate:

with DNBFDocument.open("save.dat") as doc:
    life = doc.one(class_name="Life", where=lambda obj: obj.member("Name").value == "Alex")

YAML export

from dnbflib import export_dnbf_to_yaml, rebuild_yaml_export

export_dnbf_to_yaml("save.dat", "save_export")
rebuilt = rebuild_yaml_export("save_export")

The export is lossless by default. It writes a manifest.yaml index and nested per-record directories containing record.yaml, raw.bin, and decoded sidecar files when useful.

The repository also includes a command-line export example:

python examples/export_to_yaml.py save.dat save_export --verify

Record Store

from dnbflib import DNBFRecordStore, DNBFWriter, RecordTypeEnumeration

store = DNBFRecordStore(":memory:", source_bytes=b"\x0b")
store.add_raw_record(record_type=RecordTypeEnumeration.MessageEnd, offset=0, raw=b"\x0b")

rebuilt = DNBFWriter.from_record_store(store).to_bytes()
assert rebuilt == b"\x0b"

Public API

The top-level dnbflib package exports:

  • DNBFDocument, DNBFObjectNode, DNBFMemberNode
  • DNBFRecordStore, StoredRecord
  • DNBFWriter
  • export_dnbf_to_yaml, export_record_store_to_yaml, rebuild_yaml_export
  • RecordTypeEnumeration, BinaryTypeEnumeration, PrimitiveTypeEnumeration
  • BinaryObjectString
  • traversal errors such as ObjectNotFoundError, AmbiguousObjectError, MemberNotFoundError, and AmbiguousMemberError

Further details on this library are available in docs/index.html.

License

dnbflib is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dnbflib-1.0.0.tar.gz (37.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dnbflib-1.0.0-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file dnbflib-1.0.0.tar.gz.

File metadata

  • Download URL: dnbflib-1.0.0.tar.gz
  • Upload date:
  • Size: 37.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.11.9 HTTPX/0.28.1

File hashes

Hashes for dnbflib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 19e7cbf7e7d6d76721e57f321381b4aa6a0aa5c6154a8ec4d811d8b2ad1ea3f1
MD5 e284a0fc66dd4aafc78421458a11d47d
BLAKE2b-256 3e7a28dc6d39cef2ed0d4c57d16f15b0022a28083a0bd52aee401cb0f7be2aef

See more details on using hashes here.

File details

Details for the file dnbflib-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dnbflib-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.11.9 HTTPX/0.28.1

File hashes

Hashes for dnbflib-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b743188cd254121597d0499cf49f6d76971da4c23c75a5b40b73fa9bfd6e542
MD5 6a4644e500484bb48b7d4330cf29b514
BLAKE2b-256 defa0c67609ab4e43655dd7f00b7144a9c38e333e2a442989246dd1e9b49c1f7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page