From a299ba98ac34db31e6d01fe7c1993b3b9ca60eff Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Thu, 9 Nov 2023 23:37:40 -0500 Subject: [PATCH] sao: fix erronius length errors --- titles/sao/handlers/base.py | 9 +-------- titles/sao/handlers/helpers.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/titles/sao/handlers/base.py b/titles/sao/handlers/base.py index 99ab53d..afaa2cb 100644 --- a/titles/sao/handlers/base.py +++ b/titles/sao/handlers/base.py @@ -19,9 +19,7 @@ class SaoRequestHeader: class SaoBaseRequest: def __init__(self, header: SaoRequestHeader, data: bytes) -> None: self.header = header - if self.header.data_len != len(data): - logging.getLogger('sao').error(f"Expected {self.header.data_len} data bytes byt got {len(data)}!") - # TODO: Raise an error here + # TODO: Length check class SaoResponseHeader: def __init__(self, cmd_id: int) -> None: @@ -2046,15 +2044,10 @@ class SaoSynthesizeEnhancementHeroLogRequest(SaoBaseRequest): off += origin_user_hero_log_id[1] self.material_common_reward_user_data_list: List[MaterialCommonRewardUserData] - - if len(data) <= off: - self.material_common_reward_user_data_count = 0 - return self.material_common_reward_user_data_count = decode_int(data, off) off += INT_OFF - for _ in range(self.material_common_reward_user_data_count): mat = MaterialCommonRewardUserData(data, off) off += mat.get_size() diff --git a/titles/sao/handlers/helpers.py b/titles/sao/handlers/helpers.py index 38823cc..ccedb33 100644 --- a/titles/sao/handlers/helpers.py +++ b/titles/sao/handlers/helpers.py @@ -40,6 +40,21 @@ def decode_str(data: bytes, offset: int) -> Tuple[str, int]: logging.getLogger('sao').error(f"Failed to parse {data[offset:]} as string!") return ("", 0) +def encode_byte(data: int) -> bytes: + return struct.pack("!B", data) + +def encode_short(data: int) -> bytes: + return struct.pack("!H", data) + +def encode_int(data: int) -> bytes: + return struct.pack("!I", data) + +def encode_long(data: int) -> bytes: + return struct.pack("!L", data) + +def encode_bigint(data: int) -> bytes: + return struct.pack("!Q", data) + def encode_str(s: str) -> bytes: try: str_bytes = s.encode("utf-16-le", errors="replace")