Skip to content

binary.py

ofrak.core.binary

BinaryPatchConfig (ComponentConfig) dataclass

Config for the BinaryPatchModifier.

Attributes:

Name Type Description
offset int

physical offset from beginning of resource where the patch should be applied

patch_bytes bytes

the raw bytes to patch

BinaryPatchModifier (Modifier)

Patch a binary at the target offset with the given patch bytes.

modify(self, resource, config) async

Patch the resource at the target offset with the given patch bytes.

Parameters:

Name Type Description Default
resource Resource

the resource to patch

required
config BinaryPatchConfig

contains the offset at which to patch and the patch bytes to apply

required

Exceptions:

Type Description
ModifierError

if the binary patch overflows the original size of the resource

Source code in ofrak/core/binary.py
async def modify(self, resource: Resource, config: BinaryPatchConfig) -> None:
    """
    Patch the resource at the target offset with the given patch bytes.

    :param resource: the resource to patch
    :param config: contains the offset at which to patch and the patch bytes to apply

    :raises ModifierError: if the binary patch overflows the original size of the resource
    """
    resource_size = await resource.get_data_length()
    if len(config.patch_bytes) > resource_size - config.offset:
        raise ModifierError(
            f"The binary patch, {config}, overflows the original size of the resource "
            f"{resource.get_id().hex()}."
        )
    resource.queue_patch(config.get_range(), config.patch_bytes)
    return

GenericBinary (ResourceView)

A generic binary blob.

GenericText (GenericBinary)

A binary that consists of lines of text.