Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
branches: [ "main", "actions" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:

permissions:
contents: read
Expand Down
9 changes: 7 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ version = "0.0.1"
authors = [ {name="buherator", email="buherator@silentsignal.hu"}, ]
description = "Kaitai Struct parser of MS-NFX"
readme = "README.md"
requires-python = ">=3.0"
dependencies = ["kaitaistruct@git+https://github.com/kaitai-io/kaitai_struct_python_runtime.git"] # until 0.11 is officially released
requires-python = ">=3.8"
dependencies = ["kaitaistruct>=0.11"]

# For Git-based dependency fetching
[tool.hatch.metadata]
Expand All @@ -19,3 +19,8 @@ allow-direct-references = true
pythonpath = [
"src"
]

[dependency-groups]
dev = [
"pytest>=8.3.5",
]
33 changes: 24 additions & 9 deletions src/nbfx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import struct
from .nbfx import Nbfx
from kaitaistruct import KaitaiStream, KaitaiStruct, ConsistencyError, EndOfStreamError
from kaitaistruct import KaitaiStream, KaitaiStruct, ConsistencyError, EndOfStreamError, ConsistencyNotCheckedError
from io import BytesIO

__all__ = [
Expand Down Expand Up @@ -128,7 +128,7 @@ def nbfx_import_values(nbfx: Nbfx, values) -> Nbfx:
nbfx_set_string(nbfx_str, val[1])
nbfx.dictionary_table.entries.entry[val[0]] = nbfx_str
dict_len = len(kaitai_serialize(nbfx.dictionary_table.entries))
nbfx.dictionary_table.size = nbfx_get_multibyte_int31(dict_len)
nbfx.dictionary_table.size = nbfx_get_multibyte_int31(dict_len, nbfx.dictionary_table)

for val in values["Chars"]:
charstr=None
Expand All @@ -145,31 +145,39 @@ def nbfx_import_values(nbfx: Nbfx, values) -> Nbfx:
charstr=Nbfx.Chars8Text()
charstr.string = val[1]
charstr.length = len(val[1])
charstr._parent=nbfx.records[val[0]]
charstr._root=nbfx.records[val[0]]._root
charstr._check()
nbfx.records[val[0]].rec_body = charstr
nbfx.records[val[0]]._check()

for val in values["Number"]:
nbfx.records[val[0]].rec_body.value=val[1]

nbfx.dictionary_table.entries._check()
nbfx.dictionary_table._check()
nbfx._check()
return nbfx


def nbfx_serialize(nbfx: Nbfx) -> bytes:
return kaitai_serialize(nbfx)


def kaitai_serialize(obj: KaitaiStruct) -> bytes:
def kaitai_serialize(obj: KaitaiStream) -> bytes:
# nbfx._check()
# Still an ugly hack to determine expected output size
final_size = obj._io.size()
try:
# This may need increasing for large messages!
_test_io = KaitaiStream(BytesIO(bytearray(102400)))
obj._check()
_test_io = KaitaiStream(BytesIO(bytearray(1024000)))
obj._write(_test_io)
# print(obj._io.size(),obj._io.pos())
except:
except ConsistencyError as e: # TODO remove Pokemon handler!
final_size = _test_io.pos()
# print("crash override", final_size)

if final_size==0:
pass
_out_io = KaitaiStream(BytesIO(bytearray(final_size)))
obj._write(_out_io)
return _out_io.to_byte_array()
Expand All @@ -178,14 +186,21 @@ def kaitai_serialize(obj: KaitaiStruct) -> bytes:
def nbfx_set_string(nbfx_str: Nbfx.NbfxString, value: str):
nbfx_str.str = value
# nbfx_set_multibyte_int31(nbfx_str.str_len, len(value))
nbfx_str.str_len = nbfx_get_multibyte_int31(len(value))
nbfx_str.str_len = nbfx_get_multibyte_int31(len(value), nbfx_str)
nbfx_str._check()


def nbfx_get_multibyte_int31(value: int) -> Nbfx.MultiByteInt31:
def nbfx_get_multibyte_int31(value: int, parent: Nbfx) -> Nbfx.MultiByteInt31:
mb = OldMultiByteInt31()
mb.value = value
#print(repr(mb.to_bytes()))
mb_io = KaitaiStream(BytesIO(mb.to_bytes()))
nbfx_int = Nbfx.MultiByteInt31(mb_io)
nbfx_int._read()
nbfx_int._parent=parent
nbfx_int._root=parent._root
for mb in nbfx_int.multibytes:
mb._root=nbfx_int._root
mb._check()
nbfx_int._check()
return nbfx_int
Loading