├── .gitignore ├── LICENSE ├── README.md ├── build.bat ├── examples ├── compressor │ └── main.py └── decompressor │ └── main.py ├── renovate.json ├── requirements.txt ├── sc_compression ├── __init__.py ├── __main__.py ├── __pycache__ │ ├── __init__.cpython-38.pyc │ └── __init__.cpython-39.pyc ├── compressor.py ├── decompressor.py ├── exceptions │ ├── __init__.py │ └── unknown_file_magic.py ├── signatures.py ├── support │ ├── __init__.py │ ├── lzham.exe │ └── lzham.py └── utils │ ├── __init__.py │ ├── __pycache__ │ ├── __init__.cpython-38.pyc │ ├── reader.cpython-38.pyc │ └── writer.cpython-38.pyc │ ├── common.py │ ├── reader.py │ └── writer.py ├── setup.py ├── test_upload.cmd └── upload.cmd /.gitignore: -------------------------------------------------------------------------------- 1 | # IDE project directories 2 | /.idea/ 3 | 4 | # Setuptools directories 5 | /sc_compression.egg-info/ 6 | /build/ 7 | /dist/ 8 | 9 | # Scripts directories 10 | /examples/**/out/ 11 | /examples/**/in/ 12 | 13 | # Python compiled files 14 | *.pyc 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Danila Schelkov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SC Compression 2 | 3 | A module for compression like in Supercell games. 4 | 5 | ![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/xcoder-tool/sc-compression?include_prereleases) 6 | - 7 | 8 | TestPyPi - https://test.pypi.org/project/sc-compression/
9 | PyPi - https://pypi.org/project/sc-compression/ 10 | 11 | ## Supported compressions: 12 | - LZMA 13 | - SC 14 | - SCLZ 15 | - SIG 16 | - ZSTD 17 | -------------------------------------------------------------------------------- /build.bat: -------------------------------------------------------------------------------- 1 | @ECHO off 2 | SET FOLDER="dist" 3 | DEL /F/Q/S "%FOLDER%" > NUL 4 | RMDIR /Q/S "%FOLDER%" 5 | 6 | python setup.py sdist bdist_wheel -------------------------------------------------------------------------------- /examples/compressor/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from sc_compression.signatures import Signatures 4 | from sc_compression import compress 5 | 6 | if not os.path.exists("in"): 7 | os.mkdir("in") 8 | 9 | if not os.path.exists("out"): 10 | os.mkdir("out") 11 | 12 | 13 | for filename in os.listdir("in"): 14 | with open("in/" + filename, "rb") as f: 15 | file_data = f.read() 16 | f.close() 17 | with open("out/" + filename, "wb") as f: 18 | f.write(compress(file_data, Signatures.SC, 3)) 19 | f.close() 20 | -------------------------------------------------------------------------------- /examples/decompressor/main.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from sc_compression import decompress 4 | 5 | if not os.path.exists("in"): 6 | os.mkdir("in") 7 | 8 | if not os.path.exists("out"): 9 | os.mkdir("out") 10 | 11 | 12 | for filename in os.listdir("in"): 13 | with open("in/" + filename, "rb") as f: 14 | file_data = f.read() 15 | f.close() 16 | with open("out/" + filename, "wb") as f: 17 | f.write(decompress(file_data)[0]) 18 | f.close() 19 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | zstandard 2 | -------------------------------------------------------------------------------- /sc_compression/__init__.py: -------------------------------------------------------------------------------- 1 | from .compressor import Compressor 2 | from .decompressor import Decompressor 3 | from .signatures import Signatures 4 | 5 | __all__ = ["Decompressor", "Compressor", "Signatures", "decompress", "compress"] 6 | 7 | 8 | def compress(buffer: bytes, signature: Signatures, file_version: int = None) -> bytes: 9 | return Compressor().compress(buffer, signature, file_version) 10 | 11 | 12 | def decompress(buffer: bytes) -> tuple[bytes, Signatures, int]: 13 | _decompressor = Decompressor() 14 | decompressed = _decompressor.decompress(buffer) 15 | 16 | return decompressed, _decompressor.signature, _decompressor.file_version 17 | -------------------------------------------------------------------------------- /sc_compression/__main__.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import os 3 | from typing import Callable 4 | 5 | from sc_compression import compress, decompress 6 | from sc_compression.signatures import Signatures 7 | 8 | 9 | def main() -> int: 10 | argument_parser = argparse.ArgumentParser( 11 | description="A module for compression like in Supercell games." 12 | ) 13 | argument_parser.add_argument( 14 | "-d", "--decompress", help="Decompress all files", action="store_true" 15 | ) 16 | argument_parser.add_argument( 17 | "-c", "--compress", help="Compress all files", action="store_true" 18 | ) 19 | argument_parser.add_argument( 20 | "-s", 21 | "--signature", 22 | choices=[signature.name for signature in Signatures], 23 | help="Required signature name", 24 | type=str, 25 | default=Signatures.SC.name, 26 | ) 27 | argument_parser.add_argument( 28 | "-f", 29 | "--files", 30 | help="Files to be processed", 31 | type=str, 32 | nargs="+", 33 | required=True, 34 | ) 35 | argument_parser.add_argument( 36 | "-S", 37 | "--suffix", 38 | help="Suffix for processed files", 39 | type=str, 40 | default=None, 41 | ) 42 | 43 | args = argument_parser.parse_args() 44 | if args.decompress: 45 | process_files( 46 | args.files, 47 | lambda data: decompress(data)[0], 48 | args.suffix or "decompressed", 49 | ) 50 | elif args.compress: 51 | signature = Signatures[args.signature] 52 | 53 | process_files( 54 | args.files, 55 | lambda data: compress(data, signature, 1), 56 | args.suffix or "compressed", 57 | ) 58 | else: 59 | argument_parser.print_help() 60 | 61 | return 0 62 | 63 | 64 | def get_absolute_file_path(file_path: str) -> str: 65 | if os.path.isabs(file_path): 66 | return file_path 67 | 68 | return os.path.join(os.getcwd(), file_path) 69 | 70 | 71 | def get_directory(file_path: str) -> str: 72 | return os.path.dirname(file_path) 73 | 74 | 75 | def get_base_name(file_path: str) -> str: 76 | return os.path.splitext(file_path)[0] 77 | 78 | 79 | def get_extension(file_path: str) -> str: 80 | return os.path.splitext(file_path)[1][1:] 81 | 82 | 83 | def join_filename(*parts: str, divider: str = ".") -> str: 84 | return divider.join(part for part in parts if len(part) > 0) 85 | 86 | 87 | def process_files(files: list[str], action: Callable[[bytes], bytes], suffix: str): 88 | for filename in files: 89 | path = get_absolute_file_path(filename) 90 | directory = get_directory(path) 91 | base_name = get_base_name(path) 92 | extension = get_extension(path) 93 | 94 | if os.path.isfile(path): 95 | with open(path, "rb") as file: 96 | data = action(file.read()) 97 | 98 | processed_filename = join_filename(base_name, suffix, extension) 99 | with open(os.path.join(directory, processed_filename), "wb") as file: 100 | file.write(data) 101 | 102 | 103 | if __name__ == "__main__": 104 | exit(main()) 105 | -------------------------------------------------------------------------------- /sc_compression/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /sc_compression/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /sc_compression/compressor.py: -------------------------------------------------------------------------------- 1 | import lzma 2 | from hashlib import md5 3 | 4 | from sc_compression.signatures import Signatures 5 | from sc_compression.utils import ByteWriter 6 | 7 | try: 8 | import lzham 9 | except ImportError: 10 | from platform import system as get_system_name 11 | 12 | lzham = None 13 | if get_system_name() == "Windows": 14 | from sc_compression.support.lzham import LZHAM 15 | 16 | lzham = LZHAM 17 | 18 | try: 19 | import zstandard 20 | except ImportError: 21 | zstandard = None 22 | 23 | 24 | class Compressor: 25 | lzham_filters = {"dict_size_log2": 18} 26 | lzma_filters = [ 27 | { 28 | "id": lzma.FILTER_LZMA1, 29 | "dict_size": 256 * 1024, 30 | "lc": 3, 31 | "lp": 0, 32 | "pb": 2, 33 | "mode": lzma.MODE_NORMAL, 34 | }, 35 | ] 36 | 37 | def compress( 38 | self, 39 | data: bytes, 40 | signature: Signatures, 41 | file_version: int = None, 42 | metadata: bytes | None = None, 43 | ) -> bytes: 44 | uncompressed_size = len(data) 45 | 46 | if file_version is None: 47 | file_version = 3 if zstandard and signature != Signatures.SCLZ else 1 48 | 49 | if ( 50 | signature == Signatures.ZSTD 51 | and not zstandard 52 | or signature == Signatures.SCLZ 53 | and not lzham 54 | ): 55 | signature = Signatures.SC 56 | 57 | writer = ByteWriter("little") 58 | if signature is Signatures.NONE: 59 | return data 60 | elif signature in (Signatures.LZMA, Signatures.SIG) or ( 61 | signature == Signatures.SC and file_version != 3 62 | ): 63 | compressed = lzma.compress( 64 | data, format=lzma.FORMAT_ALONE, filters=self.lzma_filters 65 | ) 66 | 67 | writer.write(compressed[:5]) 68 | 69 | writer.write_int32(uncompressed_size) 70 | 71 | writer.write(compressed[13:]) 72 | 73 | compressed = writer.buffer 74 | elif signature == Signatures.SCLZ and lzham: 75 | compressed = lzham.compress(data, filters=self.lzham_filters) 76 | 77 | writer.write(b"SCLZ") 78 | writer.write_u_int8(self.lzham_filters["dict_size_log2"]) 79 | writer.write_int32(uncompressed_size) 80 | writer.write(compressed) 81 | 82 | compressed = writer.buffer 83 | elif signature in (Signatures.SC, Signatures.ZSTD) and zstandard is not None: 84 | compressor = zstandard.ZstdCompressor() 85 | compressed = compressor.compress(data) 86 | else: 87 | raise TypeError("Unknown Signature!") 88 | 89 | compressed = self._write_header( 90 | compressed, data, file_version, signature, metadata 91 | ) 92 | 93 | return compressed 94 | 95 | @staticmethod 96 | def _write_header( 97 | compressed: bytes, 98 | data: bytes, 99 | file_version: int, 100 | signature: Signatures, 101 | metadata: bytes | None, 102 | ) -> bytes: 103 | if signature in (Signatures.SC, Signatures.SCLZ): 104 | writer = ByteWriter("big") 105 | writer.write(b"SC") 106 | if file_version <= 4: 107 | writer.write_int32(file_version) 108 | if file_version == 4: 109 | writer.write_int32(1) 110 | 111 | data_hash = md5(data).digest() 112 | 113 | writer.write_int32(len(data_hash)) 114 | writer.write(data_hash) 115 | else: 116 | writer.set_endian("little") 117 | writer.write_int32(file_version) 118 | 119 | if metadata is None: 120 | metadata = b"" 121 | 122 | writer.write_int32(len(metadata)) 123 | writer.write(metadata) 124 | 125 | return writer.buffer + compressed 126 | elif signature == Signatures.SIG: 127 | writer = ByteWriter("big") 128 | writer.write(b"Sig:") 129 | writer.write(b"\x00" * 64) # sha64 130 | return writer.buffer + compressed 131 | 132 | return compressed 133 | -------------------------------------------------------------------------------- /sc_compression/decompressor.py: -------------------------------------------------------------------------------- 1 | import lzma 2 | 3 | from sc_compression.exceptions import UnknownFileMagicException 4 | from sc_compression.signatures import ( 5 | Signatures, 6 | get_signature, 7 | MAGIC_SC, 8 | MAGIC_ZSTD, 9 | MAGIC_SCLZ, 10 | ) 11 | from sc_compression.utils import ByteReader 12 | 13 | try: 14 | import lzham 15 | except ImportError: 16 | from platform import system as get_system_name 17 | 18 | lzham = None 19 | if get_system_name() == "Windows": 20 | from sc_compression.support.lzham import LZHAM 21 | 22 | lzham = LZHAM 23 | 24 | try: 25 | import zstandard 26 | except ImportError: 27 | zstandard = None 28 | 29 | 30 | class Decompressor: 31 | def __init__(self): 32 | self.signature = Signatures.NONE 33 | self.file_version = -1 34 | self.hash = None 35 | 36 | def decompress(self, buffer: bytes) -> bytes: 37 | self.signature = get_signature(buffer) 38 | if self.signature == Signatures.NONE: 39 | return buffer 40 | 41 | if self.signature == Signatures.SC: 42 | reader = ByteReader(buffer, "big") 43 | 44 | self.check_magic(MAGIC_SC, reader=reader) 45 | 46 | self.file_version = reader.read_int32() 47 | if self.file_version == 4: 48 | self.file_version = reader.read_int32() 49 | 50 | if self.file_version == 0x05000000 or self.file_version == 0x06000000: 51 | reader.set_endian("little") 52 | 53 | if self.file_version == 0x06000000: 54 | reader.read_int16() 55 | 56 | metadata_table_offset = reader.read_int32() # offset to metadata vtable 57 | reader.read(metadata_table_offset) # metadata 58 | else: 59 | hash_length = reader.read_int32() 60 | self.hash = reader.read(hash_length) 61 | 62 | return self.decompress(reader.read_all_bytes()) 63 | elif self.signature == Signatures.SIG: 64 | return self.decompress(buffer[68:]) 65 | elif self.signature == Signatures.SCLZ and lzham is not None: 66 | reader = ByteReader(buffer, "little") 67 | 68 | self.check_magic(MAGIC_SCLZ, reader=reader) 69 | 70 | dict_size_log2 = reader.read_u_int8() 71 | uncompressed_size = reader.read_int32() 72 | 73 | filters = {"dict_size_log2": dict_size_log2} 74 | return lzham.decompress(reader.read_all_bytes(), uncompressed_size, filters) 75 | elif self.signature == Signatures.LZMA: 76 | decompressor = lzma.LZMADecompressor() 77 | compressed = buffer[:5] + b"\xff" * 8 + buffer[9:] 78 | return decompressor.decompress(compressed) 79 | elif self.signature == Signatures.ZSTD and zstandard is not None: 80 | self.check_magic(MAGIC_ZSTD, buffer=buffer) 81 | 82 | decompressor = zstandard.ZstdDecompressor() 83 | return decompressor.decompress(buffer) 84 | else: 85 | raise TypeError(self.signature) 86 | 87 | @staticmethod 88 | def check_magic( 89 | expected_magic: bytes, 90 | reader: ByteReader | None = None, 91 | buffer: bytes | None = None, 92 | ) -> None: 93 | if reader is not None: 94 | magic = reader.read(len(expected_magic)) 95 | elif buffer is not None: 96 | magic = buffer[: len(expected_magic)] 97 | else: 98 | raise Exception("Cannot find buffer to get magic from.") 99 | 100 | if magic != expected_magic: 101 | raise UnknownFileMagicException(magic, magic, expected_magic) 102 | -------------------------------------------------------------------------------- /sc_compression/exceptions/__init__.py: -------------------------------------------------------------------------------- 1 | from .unknown_file_magic import UnknownFileMagicException 2 | -------------------------------------------------------------------------------- /sc_compression/exceptions/unknown_file_magic.py: -------------------------------------------------------------------------------- 1 | class UnknownFileMagicException(Exception): 2 | def __init__(self, magic: bytes, expected_magic: bytes, *args): 3 | super().__init__( 4 | f"Unknown file magic: {magic.hex()}, while expected was {expected_magic.hex()}", 5 | *args, 6 | ) 7 | -------------------------------------------------------------------------------- /sc_compression/signatures.py: -------------------------------------------------------------------------------- 1 | import enum 2 | import re 3 | from enum import IntEnum 4 | 5 | MAGIC_SC: bytes = b"SC" 6 | MAGIC_SCLZ: bytes = b"SCLZ" 7 | MAGIC_SIG: bytes = b"Sig:" 8 | MAGIC_ZSTD: bytes = b"\x28\xb5\x2f\xfd" 9 | 10 | 11 | @enum.unique 12 | class Signatures(IntEnum): 13 | NONE = 0 14 | LZMA = 1 15 | SC = 2 16 | SCLZ = 3 17 | SIG = 4 18 | ZSTD = 5 19 | 20 | 21 | def get_signature(buffer: bytes) -> Signatures: 22 | signature = Signatures.NONE 23 | 24 | if re.match(b"\x00\x00?\x00", buffer[1:5]): 25 | signature = Signatures.LZMA 26 | elif buffer.startswith(MAGIC_ZSTD): 27 | signature = Signatures.ZSTD 28 | 29 | if buffer.startswith(MAGIC_SCLZ): 30 | signature = Signatures.SCLZ 31 | elif buffer.startswith(MAGIC_SC): 32 | signature = Signatures.SC 33 | elif buffer.startswith(MAGIC_SIG): 34 | signature = Signatures.SIG 35 | 36 | return signature 37 | -------------------------------------------------------------------------------- /sc_compression/support/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/support/__init__.py -------------------------------------------------------------------------------- /sc_compression/support/lzham.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/support/lzham.exe -------------------------------------------------------------------------------- /sc_compression/support/lzham.py: -------------------------------------------------------------------------------- 1 | from sc_compression.utils.writer import Writer 2 | from tempfile import mktemp 3 | from os import remove, system, path 4 | 5 | 6 | class LZHAM: 7 | @staticmethod 8 | def decompress(data, uncompressed_size, filters): 9 | writer = Writer("little") 10 | writer.write(b"LZH\x30") 11 | writer.write_int8(filters["dict_size_log2"]) 12 | writer.write_u_int32(uncompressed_size) 13 | writer.write_u_int32(0) 14 | writer.write(data) 15 | 16 | temp_file_path = mktemp(".lzham") 17 | with open(temp_file_path, "wb") as file: 18 | file.write(writer.buffer) 19 | 20 | decompressed_path = mktemp(".lzham") 21 | if system( 22 | f"{path.dirname(__file__)}/lzham.exe " 23 | f"-c -d{filters['dict_size_log2']} " 24 | f"d {temp_file_path} {decompressed_path} > nul 2>&1" 25 | ): 26 | remove(temp_file_path) 27 | remove(decompressed_path) 28 | return None 29 | with open(decompressed_path, "rb") as file: 30 | decompressed = file.read() 31 | 32 | remove(temp_file_path) 33 | remove(decompressed_path) 34 | 35 | return decompressed 36 | 37 | @staticmethod 38 | def compress(data, filters): 39 | temp_file_path = mktemp(".data") 40 | with open(temp_file_path, "wb") as file: 41 | file.write(data) 42 | 43 | compressed_path = mktemp(".lzham") 44 | if system( 45 | f"{path.dirname(__file__)}/lzham.exe " 46 | f"-c -d{filters['dict_size_log2']} " 47 | f"c {temp_file_path} {compressed_path} > nul 2>&1" 48 | ): 49 | remove(temp_file_path) 50 | remove(compressed_path) 51 | return None 52 | with open(compressed_path, "rb") as file: 53 | compressed = file.read()[13:] 54 | 55 | remove(temp_file_path) 56 | remove(compressed_path) 57 | 58 | return compressed 59 | -------------------------------------------------------------------------------- /sc_compression/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .reader import ByteReader 2 | from .writer import ByteWriter 3 | 4 | __all__ = ["ByteWriter", "ByteReader"] 5 | -------------------------------------------------------------------------------- /sc_compression/utils/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/utils/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /sc_compression/utils/__pycache__/reader.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/utils/__pycache__/reader.cpython-38.pyc -------------------------------------------------------------------------------- /sc_compression/utils/__pycache__/writer.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xcoder-tool/sc-compression/87b345914d74e14736bb049a2e6c1f54b5f3832d/sc_compression/utils/__pycache__/writer.cpython-38.pyc -------------------------------------------------------------------------------- /sc_compression/utils/common.py: -------------------------------------------------------------------------------- 1 | from typing import Literal 2 | 3 | from typing_extensions import TypeAlias 4 | 5 | Endian: TypeAlias = Literal["big", "little"] 6 | 7 | 8 | def get_endian_sign(endian: Literal["big", "little"]) -> Literal[">", "<"]: 9 | if endian == "big": 10 | return ">" 11 | elif endian == "little": 12 | return "<" 13 | 14 | raise NotImplementedError(f"Unknown endian requested: {endian}") 15 | -------------------------------------------------------------------------------- /sc_compression/utils/reader.py: -------------------------------------------------------------------------------- 1 | from io import BufferedReader, BytesIO 2 | from struct import unpack 3 | 4 | from .common import get_endian_sign, Endian 5 | 6 | 7 | class ByteReader: 8 | def __init__(self, initial_bytes: bytes, endian: Endian): 9 | # noinspection PyTypeChecker 10 | self._internal_reader = BufferedReader(BytesIO(initial_bytes)) 11 | self._endian_sign = get_endian_sign(endian) 12 | 13 | def set_endian(self, endian: Endian) -> None: 14 | self._endian_sign = get_endian_sign(endian) 15 | 16 | def seek(self, position: int) -> None: 17 | self._internal_reader.seek(position) 18 | 19 | def tell(self) -> int: 20 | return self._internal_reader.tell() 21 | 22 | def read(self, size: int) -> bytes: 23 | return self._internal_reader.read(size) 24 | 25 | def read_all_bytes(self) -> bytes: 26 | return self._internal_reader.read() 27 | 28 | def read_u_int64(self) -> int: 29 | return unpack(f"{self._endian_sign}Q", self.read(8))[0] 30 | 31 | def read_int64(self) -> int: 32 | return unpack(f"{self._endian_sign}q", self.read(8))[0] 33 | 34 | def read_u_int32(self) -> int: 35 | return unpack(f"{self._endian_sign}I", self.read(4))[0] 36 | 37 | def read_int32(self) -> int: 38 | return unpack(f"{self._endian_sign}i", self.read(4))[0] 39 | 40 | def read_u_int16(self) -> int: 41 | return unpack(f"{self._endian_sign}H", self.read(2))[0] 42 | 43 | def read_int16(self) -> int: 44 | return unpack(f"{self._endian_sign}h", self.read(2))[0] 45 | 46 | def read_u_int8(self) -> int: 47 | return unpack(f"{self._endian_sign}B", self.read(1))[0] 48 | 49 | def read_int8(self) -> int: 50 | return unpack(f"{self._endian_sign}b", self.read(1))[0] 51 | -------------------------------------------------------------------------------- /sc_compression/utils/writer.py: -------------------------------------------------------------------------------- 1 | from struct import pack 2 | 3 | from .common import get_endian_sign, Endian 4 | 5 | 6 | class ByteWriter: 7 | def __init__(self, endian: Endian): 8 | self._buffer = bytearray() 9 | self._endian_sign = get_endian_sign(endian) 10 | 11 | def set_endian(self, endian: Endian) -> None: 12 | self._endian_sign = get_endian_sign(endian) 13 | 14 | def write(self, value: bytes) -> None: 15 | self._buffer += value 16 | 17 | def write_double(self, value: float) -> None: 18 | self.write(pack(f"{self._endian_sign}d", value)) 19 | 20 | def write_float(self, value: float) -> None: 21 | self.write(pack(f"{self._endian_sign}f", value)) 22 | 23 | def write_u_int64(self, integer: int): 24 | self.write(pack(f"{self._endian_sign}Q", integer)) 25 | 26 | def write_int64(self, integer: int) -> None: 27 | self.write(pack(f"{self._endian_sign}q", integer)) 28 | 29 | def write_u_int32(self, integer: int) -> None: 30 | self.write(pack(f"{self._endian_sign}I", integer)) 31 | 32 | def write_int32(self, integer: int) -> None: 33 | self.write(pack(f"{self._endian_sign}i", integer)) 34 | 35 | def write_u_int16(self, integer: int) -> None: 36 | self.write(pack(f"{self._endian_sign}H", integer)) 37 | 38 | def write_int16(self, integer: int) -> None: 39 | self.write(pack(f"{self._endian_sign}h", integer)) 40 | 41 | def write_u_int8(self, integer: int) -> None: 42 | self.write(pack(f"{self._endian_sign}B", integer)) 43 | 44 | def write_int8(self, integer: int) -> None: 45 | self.write(pack(f"{self._endian_sign}b", integer)) 46 | 47 | @property 48 | def buffer(self) -> bytes: 49 | return self._buffer 50 | 51 | @property 52 | def position(self): 53 | return len(self._buffer) 54 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | with open("README.md") as fh: 4 | long_description = fh.read() 5 | 6 | with open("requirements.txt") as fh: 7 | requirements = [line.rstrip("\n") for line in fh.readlines() if line != ""] 8 | 9 | setuptools.setup( 10 | name="sc-compression", 11 | version="0.6.6", 12 | author="Vorono4ka", 13 | author_email="crowo4ka@gmail.com", 14 | description="SC Compression", 15 | long_description=long_description, 16 | long_description_content_type="text/markdown", 17 | url="https://github.com/xcoder-tool/sc-compression", 18 | license="GPLv3", 19 | entry_points={ 20 | "console_scripts": ["sc-compression=sc_compression.__main__:main"], 21 | }, 22 | packages=setuptools.find_packages(), 23 | classifiers=[ 24 | "Programming Language :: Python :: 3", 25 | "License :: OSI Approved :: GNU General Public License v3 (GPLv3)", 26 | "Operating System :: OS Independent", 27 | ], 28 | python_requires=">=3.5", 29 | install_requires=requirements, 30 | package_data={ 31 | "": ["support/lzham.exe"], 32 | }, 33 | ) 34 | -------------------------------------------------------------------------------- /test_upload.cmd: -------------------------------------------------------------------------------- 1 | python -m twine upload --repository sc-compression-testpypi dist/* -------------------------------------------------------------------------------- /upload.cmd: -------------------------------------------------------------------------------- 1 | python -m twine upload --repository sc-compression-testpypi dist/* 2 | python -m twine upload --repository sc-compression-pypi dist/* --------------------------------------------------------------------------------