Skip to content

Commit

Permalink
Fixed decompression error
Browse files Browse the repository at this point in the history
+ JSON export: raw bytes will be hexlified first
  • Loading branch information
MatrixEditor committed Jan 14, 2024
1 parent 7776313 commit 488b0ff
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
project = 'fsapi-tools'
copyright = '2022, MatrixEditor'
author = 'MatrixEditor'
release = '2.0.3'
release = '2.0.4'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
2 changes: 1 addition & 1 deletion fsapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
>>> python3 -m fsapi --help
"""

__version__ = "2.0.3"
__version__ = "2.0.4"
__author__ = 'MatrixEditor'
4 changes: 3 additions & 1 deletion fsapi/isu/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import pathlib
import dataclasses
import json
import binascii

from colorama import Fore

Expand All @@ -39,7 +40,7 @@
class BytesJSONEncoder(json.JSONEncoder):
def default(self, o: Any) -> Any:
if isinstance(o, bytes):
return o.decode(errors="replace")
return binascii.hexlify(o).decode(errors="replace")
return super().default(o)


Expand Down Expand Up @@ -206,6 +207,7 @@ def _run(argv, target: pathlib.Path, pp: DataclassPrinter):
Fore.RED, "[!] Could not decompress core:", str(core.errors)
)
print(pp_msg)
return
fp.write(data)
else:
msg = "[out] Saved core to"
Expand Down
16 changes: 10 additions & 6 deletions fsapi/isu/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import enum
import construct as cs

from lzallright import LZOCompressor, LZOError
from lzallright import LZOCompressor, LZOError, InputNotConsumed
from construct_dataclasses import dataclass_struct, csfield, tfield, subcsfield


Expand Down Expand Up @@ -110,8 +110,8 @@ class ISUDataField:
name_length: int = csfield(cs.Int16ul)
flags: int = csfield(cs.Int16ul)
name: str = csfield(cs.PaddedString(cs.this.name_length, "utf-8"))
value: int | None = csfield(cs.If(cs.this.length == 32, cs.Int32ul))
unknown_2: int | None = csfield(cs.If(cs.this.length == 32, cs.Int32ul))
value: int | None = csfield(cs.If(cs.this.length == 32, cs.Int32ul))


@dataclass_struct
Expand Down Expand Up @@ -231,7 +231,7 @@ class ISU(metaclass=isu_t):
>>> decomp_data = core.decompress(decomp_size.value, verify=True)
"""

def __init__(self, stream: io.IOBase | mmap.mmap) -> None:
def __init__(self, stream: t.Union[io.IOBase, mmap.mmap]) -> None:
if isinstance(stream, io.IOBase):
self.stream = mmap.mmap(stream.fileno(), 0)
elif isinstance(stream, mmap.mmap):
Expand Down Expand Up @@ -433,7 +433,7 @@ class isu_core_t:
:param errors: Optional list of error messages (default: empty list).
"""

def __init__(self, isu: ISU, data: bytes, compressed=True, *errors) -> None:
def __init__(self, isu: ISU, data: bytes, *errors, compressed=True) -> None:
self.errors = list(errors)
self.data = data
self.isu = isu
Expand Down Expand Up @@ -469,8 +469,12 @@ def decompress(self, decomp_size: int, verify=False) -> bytes:
self.data, output_size_hint=decomp_size
)
except LZOError as err:
self.errors.append(err)
decomp_buf = bytes() # to prevent errors
if isinstance(err, InputNotConsumed):
# decompression finished with leftover data
_, decomp_buf = err.args
else:
self.errors.append(err)
decomp_buf = bytes() # to prevent errors

if len(decomp_buf) != decomp_size and verify:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "fsapi-tools"
version = "2.0.3"
version = "2.0.4"
description="Frontier Smart Firmware Tools and FSAPI Implementation."
authors = [
{ name="MatrixEditor", email="[email protected]" },
Expand Down

0 comments on commit 488b0ff

Please sign in to comment.