diff --git a/pygrf/act.py b/pygrf/act.py index e0ff269..006dcc5 100644 --- a/pygrf/act.py +++ b/pygrf/act.py @@ -20,6 +20,7 @@ class Layer(NamedTuple): class Frame(NamedTuple): layers: Tuple[Layer] trigger: int = -1 + anchors: Tuple[Point] = () class Animation(NamedTuple): @@ -99,11 +100,16 @@ def parse_frame(stream): trigger, = struct.unpack('= 0x203: count, = struct.unpack('> 3) & 7] & Decrypter._mask[j & 7]) != 0: + block[(i >> 3) & 7] |= Decrypter._mask[i & 7] + + src[:8] = block + + @staticmethod + def _round_function(src: bytearray): + """ FS-box and TP table """ + block = bytearray(8) + + # 1:L Group permutation + block[0] = (src[7] << 5 | src[4] >> 3) & 0x3f + block[1] = (src[4] << 1 | src[5] >> 7) & 0x3f + block[2] = (src[4] << 5 | src[5] >> 3) & 0x3f + block[3] = (src[5] << 1 | src[6] >> 7) & 0x3f + block[4] = (src[5] << 5 | src[6] >> 3) & 0x3f + block[5] = (src[6] << 1 | src[7] >> 7) & 0x3f + block[6] = (src[6] << 5 | src[7] >> 3) & 0x3f + block[7] = (src[7] << 1 | src[4] >> 7) & 0x3f + + # 2: S-boxes + for i in range(len(Decrypter._s_table)): + block[i] = (Decrypter._s_table[i][block[i * 2]] & 0xf0) | (Decrypter._s_table[i][block[i * 2 + 1]] & 0x0f) + + # 3: Reset 4 last value + block[4] = 0 + block[5] = 0 + block[6] = 0 + block[7] = 0 + + # 4: TP permutation + for i in range(len(Decrypter._tp_table)): + j = Decrypter._tp_table[i] - 1 + if (block[(j >> 3)] & Decrypter._mask[j & 7]) != 0: + block[4 + (i >> 3)] |= Decrypter._mask[i & 7] + + # 5: XOR original block + src[0] ^= block[4] + src[1] ^= block[5] + src[2] ^= block[6] + src[3] ^= block[7] + + @staticmethod + def _fp(src): + """ Last permutation """ + block = bytearray(8) + + for i in range(len(Decrypter._fp_table)): + j = Decrypter._fp_table[i] - 1 + if (src[(j >> 3) & 7] & Decrypter._mask[j & 7]) != 0: + block[(i >> 3) & 7] |= Decrypter._mask[i & 7] + + src[0:8] = block[0:8] + + @staticmethod + def decrypt_file_data(data: bytearray, type: bool, cycle: int, offset: int, length: int) -> bytearray: + if length % 8 != 0: + ideal_size_compressed = ((length // 8) + 1) * 8 + data_fixed = bytearray(ideal_size_compressed) + + data_fixed[:length] = data[offset:offset + length] + Decrypter.decode_file_data(data_fixed, type, cycle) + data[offset:offset + length] = data_fixed[:length] + else: + Decrypter.decode_file_data(data, type, cycle, offset, length) + return data + + @staticmethod + def decode_file_data(data: bytearray, type: bool, cycle: int, offset: int = 0, length: int = -1): + cnt = 0 + + length = length if length >= 0 else len(data) + + if cycle < 3: + cycle = 3 + elif cycle < 5: + cycle += 1 + elif cycle < 7: + cycle += 9 + else: + cycle += 15 + + # loop data file + for lop in range(0, length // 8): + if lop < 20 or (not type and lop % cycle == 0): + Decrypter.des_decode_block(data, offset) + else: + if cnt == 7 and not type: + tmp = data[offset:offset + 8] + cnt = 0 + + # Sort bytes + data[offset] = tmp[3] + data[offset + 1] = tmp[4] + data[offset + 2] = tmp[6] + data[offset + 3] = tmp[0] + data[offset + 4] = tmp[1] + data[offset + 5] = tmp[2] + data[offset + 6] = tmp[5] + + a = tmp[7] + a = { + 0x00: 0x2b, 0x2b: 0x00, 0x01: 0x68, 0x68: 0x01, 0x48: 0x77, 0x77: 0x48, + 0x60: 0xff, 0xff: 0x60, 0x6c: 0x80, 0x80: 0x6c, 0xb9: 0xc0, 0xc0: 0xb9, + 0xeb: 0xfe, 0xfe: 0xeb + }.get(a, a) + + data[offset + 7] = a + + cnt += 1 + offset += 8 + + @staticmethod + def des_decode_block(src: bytearray, i: int): + block = bytearray(src[i:i+8]) + + Decrypter._ip(block) + Decrypter._round_function(block) + Decrypter._fp(block) + + src[i:i+8] = block + + + diff --git a/pygrf/grf.py b/pygrf/grf.py index 1701b55..36af2b0 100644 --- a/pygrf/grf.py +++ b/pygrf/grf.py @@ -8,6 +8,7 @@ import zlib from . import filetypes from .exceptions import GRFParseError +from .decrypt import Decrypter # the grf versions that are supported @@ -24,6 +25,7 @@ # file flags FILE_IS_FILE = 1 +FILE_ENCRYPT = [3, 5] Header = collections.namedtuple('GRFHeader', ( @@ -240,11 +242,30 @@ def __init__(self, filename, header_data, stream): if self.header.real_size == 0: self.data = b'' else: - self.data = zlib.decompress(stream.read(self.header.archived_size)) + self.data = stream.read(self.header.archived_size) + if self.header.flag in FILE_ENCRYPT: + self.cycle = self.get_cycle(self.filename, self.header.compressed_size) + self.data = Decrypter.decrypt_file_data(bytearray(self.data), self.cycle == 0, self.cycle, 0, self.header.archived_size) + + self.data = zlib.decompress(self.data) super().__init__(self.data) def __eq__(self, other): return other.filename == self.filename and other.data == self.data + + @staticmethod + def get_cycle(filename: str, compress_size: int): + cycle = 0 + file_extension = filename.split('.')[-1] + + if file_extension and file_extension not in ['gnd', 'gat', 'act', 'str']: + cycle = 1 + k = 10 + while compress_size >= k: + cycle += 1 + k *= 10 + + return cycle class Index: diff --git a/pygrf/spr.py b/pygrf/spr.py index c8c38cc..a75a759 100644 --- a/pygrf/spr.py +++ b/pygrf/spr.py @@ -1,4 +1,5 @@ """ spr file parsing """ +import io import struct from typing import Tuple, Sequence from itertools import chain @@ -97,6 +98,14 @@ def get_image(self, index) -> Image: return self.parse_pal(self.pal_offset(index)) index -= pal return self.parse_rgb(self.rgb_offset(index)) + + def set_palette(self, palette): + colors: Sequence[Color] = [] + for i in range(0, len(palette), 4): + r, g, b, _ = palette[i:i+4] + a = 0 if i == 0 else 255 + colors.append(Color(r, g, b, a)) + self.palette = colors class Spr100(SprParser): @@ -194,10 +203,11 @@ def parse_pal(self, offset): return Image(width, height, pixels) -class SPR: +class SPR(io.BytesIO): """ a container for sprite images """ def __init__(self, data: bytes): + super().__init__(data) self.header = parse_header(data, b'SP') self.parser = _get_parser(data, self.header) @@ -214,6 +224,9 @@ def __getitem__(self, index) -> Image: if index < 0 or index >= length: raise IndexError return self.parser.get_image(index) + + def set_palette(self, palette): + self.parser.set_palette(palette) @property def version(self) -> int: