|
| 1 | +from itertools import cycle |
| 2 | +from pathlib import Path |
| 3 | +from typing import Union |
| 4 | + |
| 5 | + |
| 6 | +class Eco2: |
| 7 | + header = ( |
| 8 | + (2, 'SF_type'), |
| 9 | + (10, 'UI_version'), |
| 10 | + (10, 'LG_version'), |
| 11 | + (100, 'name'), |
| 12 | + (256, 'desc'), |
| 13 | + (19, 'make_time'), |
| 14 | + (19, 'edit_time'), |
| 15 | + (8, 'unknown'), |
| 16 | + ) |
| 17 | + key = (172, 41, 85, 66) |
| 18 | + encoding = 'UTF-8' |
| 19 | + value_ext = '.xml' |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def decrypt_bytes(cls, data: bytes): |
| 23 | + return bytes((d ^ k for d, k in zip(data, cycle(cls.key)))) |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def encrypt_bytes(cls, data: bytes): |
| 27 | + return cls.decrypt_bytes(data) |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def header_length(cls): |
| 31 | + return sum([x[0] for x in cls.header]) |
| 32 | + |
| 33 | + @staticmethod |
| 34 | + def _decode_chunk(b: bytes, length: int): |
| 35 | + data = b[:length] |
| 36 | + bnext = b[length:] |
| 37 | + |
| 38 | + return data, bnext |
| 39 | + |
| 40 | + @classmethod |
| 41 | + def _decode_header(cls, data: bytes): |
| 42 | + header = {} |
| 43 | + b = data |
| 44 | + for length, name in cls.header: |
| 45 | + value, b = cls._decode_chunk(b=b, length=length) |
| 46 | + header[name] = value |
| 47 | + |
| 48 | + return header |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def _decrypt_eco2_data(cls, data: bytes): |
| 52 | + decrypted = cls.decrypt_bytes(data) |
| 53 | + hl = cls.header_length() |
| 54 | + |
| 55 | + header_bytes = decrypted[:hl] |
| 56 | + value_bytes = decrypted[hl:] |
| 57 | + value = value_bytes.decode() |
| 58 | + |
| 59 | + return header_bytes, value |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def _write_value(cls, path: Path, value: str): |
| 63 | + path.write_text(value.replace('\r\n', '\n'), encoding=cls.encoding) |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def _read_value(cls, path: Path): |
| 67 | + return path.read_text(encoding=cls.encoding).replace('\n', '\r\n') |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def decrypt(cls, |
| 71 | + path: Union[str, Path], |
| 72 | + save_dir=None, |
| 73 | + header_name=None, |
| 74 | + value_name=None): |
| 75 | + path = Path(path) |
| 76 | + save_dir = path.parent if save_dir is None else Path(save_dir) |
| 77 | + if not save_dir.exists(): |
| 78 | + raise FileNotFoundError(save_dir) |
| 79 | + |
| 80 | + if header_name is None: |
| 81 | + header_name = 'header' |
| 82 | + if value_name is None: |
| 83 | + value_name = path.stem |
| 84 | + |
| 85 | + data = path.read_bytes() |
| 86 | + bheader, value = cls._decrypt_eco2_data(data) |
| 87 | + |
| 88 | + header_path = save_dir.joinpath(header_name) |
| 89 | + header_path.write_bytes(bheader) |
| 90 | + |
| 91 | + value_path = save_dir.joinpath(value_name + cls.value_ext) |
| 92 | + cls._write_value(path=value_path, value=value) |
| 93 | + |
| 94 | + @classmethod |
| 95 | + def encrypt(cls, header_path, value_path, save_path=None): |
| 96 | + if save_path is None: |
| 97 | + save_path = 'output.eco' |
| 98 | + |
| 99 | + header_path = Path(header_path) |
| 100 | + value_path = Path(value_path) |
| 101 | + save_path = Path(save_path) |
| 102 | + |
| 103 | + bheader = header_path.read_bytes() |
| 104 | + |
| 105 | + value = cls._read_value(path=value_path) |
| 106 | + bvalue = value.encode() |
| 107 | + |
| 108 | + data = bheader + bvalue |
| 109 | + encrypted = cls.encrypt_bytes(data) |
| 110 | + |
| 111 | + save_path.write_bytes(encrypted) |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def encrypt_dir(cls, header_path, value_path, save_dir=None): |
| 115 | + header_path = Path(header_path) |
| 116 | + value_path = Path(value_path) |
| 117 | + |
| 118 | + save_dir = value_path if save_dir is None else Path(save_dir) |
| 119 | + if not save_dir.is_dir(): |
| 120 | + save_dir = save_dir.parent |
| 121 | + |
| 122 | + if value_path.is_dir(): |
| 123 | + vps = value_path.glob(f'*{cls.value_ext}') |
| 124 | + else: |
| 125 | + vps = [value_path] |
| 126 | + |
| 127 | + for vp in vps: |
| 128 | + cls.encrypt(header_path=header_path, |
| 129 | + value_path=vp, |
| 130 | + save_path=save_dir.joinpath(f'{vp.stem}.eco')) |
0 commit comments