|
| 1 | +import ctypes |
| 2 | +import platform |
| 3 | +import typing |
| 4 | +from collections.abc import ByteString |
| 5 | +from enum import IntEnum |
| 6 | +from functools import partial |
| 7 | +from io import IOBase |
| 8 | +from struct import Struct |
| 9 | +from zlib import crc32 as crc32_zlib |
| 10 | + |
| 11 | +from .enums import CompressionType |
| 12 | + |
| 13 | +# pylint:disable=too-few-public-methods |
| 14 | + |
| 15 | + |
| 16 | +def crc32(data: ByteString, value: int = 0) -> int: |
| 17 | + return (~crc32_zlib(data, value)) & 0xFFFFFFF |
| 18 | + |
| 19 | + |
| 20 | +def decodeHeader(compressed: ByteString): |
| 21 | + compressionType = CompressionType(compressed[0]) |
| 22 | + dictSizeLog = compressed[1] |
| 23 | + return compressionType, logIntoSize(dictSizeLog) |
| 24 | + |
| 25 | + |
| 26 | +def logIntoSize(log: int) -> int: |
| 27 | + return 1 << (log + 6) |
| 28 | + |
| 29 | + |
| 30 | +def maskIntoSize(mask: int) -> int: |
| 31 | + return logIntoSize(mask.bit_length()) |
| 32 | + |
| 33 | + |
| 34 | +def dictSizeIntoLog(dictSize: int) -> int: |
| 35 | + mask = 0 |
| 36 | + if dictSize < 128: |
| 37 | + raise ValueError("Dict sizes less than 128 are unsupported!") |
| 38 | + |
| 39 | + return (dictSize).bit_length() - 7 |
| 40 | + |
| 41 | + |
| 42 | +def logIntoMask(dictSizeLog: int) -> int: |
| 43 | + return (1 << dictSizeLog) - 1 |
| 44 | + |
| 45 | + |
| 46 | +def dictSizeIntoMask(dictSize: int) -> int: |
| 47 | + return logIntoMask(dictSizeIntoLog(dictSize)) |
| 48 | + |
| 49 | + |
| 50 | +class PklibError(IntEnum): |
| 51 | + ok = CMP_NO_ERROR = 0 |
| 52 | + invalidDictSize = CMP_INVALID_DICTSIZE = 1 |
| 53 | + invalidMode = CMP_INVALID_MODE = 2 |
| 54 | + badData = CMP_BAD_DATA = 3 |
| 55 | + abort = CMP_ABORT = 4 |
| 56 | + |
| 57 | + |
| 58 | +class CommonSizeConstants: |
| 59 | + __slots__ = ("OUT_BUFF_SIZE", "BUFF_SIZE") |
| 60 | + |
| 61 | + def __init__(self, OUT_BUFF_SIZE: int, BUFF_SIZE: int): |
| 62 | + self.OUT_BUFF_SIZE = OUT_BUFF_SIZE |
| 63 | + self.BUFF_SIZE = BUFF_SIZE |
| 64 | + |
| 65 | + |
| 66 | +class CommonSizeConstantsCtypes(ctypes.Structure): |
| 67 | + __slots__ = CommonSizeConstants.__slots__ + ("ownSize",) |
| 68 | + _fields_ = [ |
| 69 | + ("ownSize", ctypes.c_size_t), |
| 70 | + ("OUT_BUFF_SIZE", ctypes.c_size_t), |
| 71 | + ("BUFF_SIZE", ctypes.c_size_t), |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +class LUTSizeConstants: |
| 76 | + __slots__ = ("DIST_SIZES", "CH_BITS_ASC_SIZE", "LENS_SIZES") |
| 77 | + |
| 78 | + def __init__(self, DIST_SIZES: int, CH_BITS_ASC_SIZE: int, LENS_SIZES: int): |
| 79 | + self.DIST_SIZES = DIST_SIZES |
| 80 | + self.CH_BITS_ASC_SIZE = CH_BITS_ASC_SIZE |
| 81 | + self.LENS_SIZES = LENS_SIZES |
| 82 | + |
| 83 | + |
| 84 | +class LUTSizeConstantsCtypes(ctypes.Structure): |
| 85 | + __slots__ = LUTSizeConstants.__slots__ + ("ownSize",) |
| 86 | + _fields_ = [ |
| 87 | + ("ownSize", ctypes.c_size_t), |
| 88 | + ("DIST_SIZES", ctypes.c_size_t), |
| 89 | + ("CH_BITS_ASC_SIZE", ctypes.c_size_t), |
| 90 | + ("LENS_SIZES", ctypes.c_size_t), |
| 91 | + ] |
| 92 | + |
| 93 | + |
| 94 | +specializedSizeConstantsHeader = (("ownSize", ctypes.c_size_t),) |
| 95 | + |
| 96 | + |
| 97 | +def parseCommonSizeConstants(commonSizes: CommonSizeConstantsCtypes) -> CommonSizeConstants: |
| 98 | + if int(commonSizes.ownSize) != ctypes.sizeof(CommonSizeConstantsCtypes): |
| 99 | + raise ValueError("CommonSizeConstantsCtypes contents has changed!", commonSizes.ownSize, ctypes.sizeof(CommonSizeConstantsCtypes)) |
| 100 | + |
| 101 | + return CommonSizeConstants(int(commonSizes.OUT_BUFF_SIZE), int(commonSizes.BUFF_SIZE)) |
| 102 | + |
| 103 | + |
| 104 | +def _getSizeConstants1(lib, funcName, resCtypesType, resInternalType, parserFunc): |
| 105 | + getSpecializedSizeConstantsT = getattr(lib, funcName) |
| 106 | + getSpecializedSizeConstantsT.argtypes = [] |
| 107 | + getSpecializedSizeConstantsT.restype = resCtypesType |
| 108 | + sizeConstants = getSpecializedSizeConstantsT() |
| 109 | + |
| 110 | + return parserFunc(sizeConstants) |
| 111 | + |
| 112 | + |
| 113 | +def getCommonSizeConstants(lib): |
| 114 | + return _getSizeConstants1(lib, "getCommonSizeConstants", CommonSizeConstantsCtypes, CommonSizeConstants, parseCommonSizeConstants) |
| 115 | + |
| 116 | + |
| 117 | +def parseLUTSizeConstants(lutsSizes: LUTSizeConstantsCtypes) -> LUTSizeConstants: |
| 118 | + if int(lutsSizes.ownSize) != ctypes.sizeof(LUTSizeConstantsCtypes): |
| 119 | + raise ValueError("LUTSizeConstantsCtypes contents has changed!", lutsSizes.ownSize, ctypes.sizeof(LUTSizeConstantsCtypes)) |
| 120 | + |
| 121 | + return LUTSizeConstants(int(lutsSizes.DIST_SIZES), int(lutsSizes.CH_BITS_ASC_SIZE), int(lutsSizes.LENS_SIZES)) |
| 122 | + |
| 123 | + |
| 124 | +def getLUTSizeConstants(lib): |
| 125 | + return _getSizeConstants1(lib, "getLUTSizeConstants", LUTSizeConstantsCtypes, LUTSizeConstants, parseLUTSizeConstants) |
| 126 | + |
| 127 | + |
| 128 | +def inputCallbackStream(inputStream: IOBase, buf: ctypes.POINTER(ctypes.c_char_p), chunkSize: ctypes.POINTER(ctypes.c_uint), param: ctypes.POINTER(None)) -> ctypes.c_uint: # pylint:disable=unused-argument |
| 129 | + bufT = ctypes.c_byte * chunkSize[0] |
| 130 | + bufPtrT = ctypes.POINTER(bufT) |
| 131 | + bufArrPtr = ctypes.cast(buf, bufPtrT) |
| 132 | + |
| 133 | + countRead = inputStream.readinto(bufArrPtr[0]) |
| 134 | + return countRead |
| 135 | + |
| 136 | + |
| 137 | +def outputCallbackStream(outputStream: IOBase, buf: ctypes.POINTER(ctypes.c_char_p), chunkSize: ctypes.POINTER(ctypes.c_uint), param: ctypes.POINTER(None)) -> None: # pylint:disable=unused-argument |
| 138 | + bufT = ctypes.c_byte * chunkSize[0] |
| 139 | + bufPtrT = ctypes.POINTER(bufT) |
| 140 | + bufArrPtr = ctypes.cast(buf, bufPtrT) |
| 141 | + outputStream.write(bufArrPtr[0]) |
| 142 | + |
| 143 | + |
| 144 | +def _genCtypesFuncArgsList(f, skip: int = 0): |
| 145 | + return (f.__annotations__.get("return", None),) + tuple(f.__annotations__.get(argName, None) for argName in f.__code__.co_varnames[: f.__code__.co_argcount])[skip:] |
| 146 | + |
| 147 | + |
| 148 | +ReadFunT = ctypes.CFUNCTYPE(*_genCtypesFuncArgsList(inputCallbackStream, 1)) |
| 149 | +WriteFunT = ctypes.CFUNCTYPE(*_genCtypesFuncArgsList(outputCallbackStream, 1)) |
| 150 | + |
| 151 | + |
| 152 | +def getStreamCallbacks(inputStream: IOBase, outputStream: IOBase): |
| 153 | + icb = ReadFunT(partial(inputCallbackStream, inputStream)) |
| 154 | + ocb = WriteFunT(partial(outputCallbackStream, outputStream)) |
| 155 | + return icb, ocb |
| 156 | + |
| 157 | + |
| 158 | +def getLibraryFileName(nameNoPrefix): |
| 159 | + if platform.system() == "Windows": |
| 160 | + return "lib" + nameNoPrefix + ".dll" |
| 161 | + |
| 162 | + return "lib" + nameNoPrefix + ".so" |
| 163 | + |
| 164 | + |
| 165 | +def _initLibrary(func, internalStateStructName, specializedSizeConstantsFields: typing.Tuple[typing.Tuple[str, typing.Any]], getFieldsForInternalStateStructure): |
| 166 | + nameNoPrefix = func.__name__ |
| 167 | + nameNoPrefixFirstCapital = nameNoPrefix[0].upper() + nameNoPrefix[1:] |
| 168 | + |
| 169 | + lutsLib = ctypes.CDLL(getLibraryFileName("pkwareLUT")) |
| 170 | + commonLib = lutsLib |
| 171 | + |
| 172 | + lib = ctypes.CDLL(getLibraryFileName(nameNoPrefix)) |
| 173 | + |
| 174 | + class SpecializedSizeConstantsT: |
| 175 | + __slots__ = tuple(el[0] for el in specializedSizeConstantsFields) |
| 176 | + |
| 177 | + def __init__(self, **kwargs): |
| 178 | + for k, defaultV in specializedSizeConstantsFields: |
| 179 | + setattr(self, k, kwargs.get(k, defaultV)) |
| 180 | + |
| 181 | + SpecializedSizeConstantsT.__name__ = nameNoPrefixFirstCapital + "SizeConstantsT" |
| 182 | + |
| 183 | + class SpecializedSizeConstantsCtypesT(ctypes.Structure): |
| 184 | + __slots__ = SpecializedSizeConstantsT.__slots__ + ("ownSize",) |
| 185 | + _fields_ = specializedSizeConstantsHeader + tuple((el[0], ctypes.c_size_t) for el in specializedSizeConstantsFields) |
| 186 | + |
| 187 | + SpecializedSizeConstantsCtypesT.__name__ = nameNoPrefixFirstCapital + "SizeConstantsCtypesT" |
| 188 | + |
| 189 | + def _getSizeConstants(lib, nameNoPrefixFirstCapital) -> SpecializedSizeConstantsT: |
| 190 | + constantsGetterName = "get" + nameNoPrefixFirstCapital + "SizeConstants" |
| 191 | + if hasattr(lib, constantsGetterName): |
| 192 | + |
| 193 | + def parserFunc(sizeConstants): |
| 194 | + if int(sizeConstants.ownSize) != ctypes.sizeof(SpecializedSizeConstantsCtypesT): |
| 195 | + raise ValueError(SpecializedSizeConstantsCtypesT.__name__ + " contents has changed!", sizeConstants.ownSize, ctypes.sizeof(SpecializedSizeConstantsCtypesT)) |
| 196 | + |
| 197 | + kwargs = {} |
| 198 | + for k, _ in specializedSizeConstantsFields: |
| 199 | + kwargs[k] = int(getattr(sizeConstants, k)) |
| 200 | + |
| 201 | + return SpecializedSizeConstantsT(**kwargs) |
| 202 | + |
| 203 | + return _getSizeConstants1(lib, constantsGetterName, SpecializedSizeConstantsCtypesT, SpecializedSizeConstantsT, parserFunc) |
| 204 | + else: |
| 205 | + sizeConstants = SpecializedSizeConstantsT() |
| 206 | + return sizeConstants |
| 207 | + |
| 208 | + def constructInternalStateStruct(name, commonSizes, lutSizes, sizeConstants, getFieldsForInternalStateStructure): |
| 209 | + class InternalStateStructT(ctypes.Structure): |
| 210 | + _fields_ = getFieldsForInternalStateStructure(commonSizes, lutSizes, sizeConstants) |
| 211 | + __slots__ = tuple(el[0] for el in _fields_) |
| 212 | + |
| 213 | + InternalStateStructT.__name__ = name |
| 214 | + |
| 215 | + if sizeConstants.internalStructSize is not None: |
| 216 | + if sizeConstants.internalStructSize != ctypes.sizeof(InternalStateStructT): |
| 217 | + raise ValueError(InternalStateStructT.__name__ + " contents has changed!", sizeConstants.internalStructSize, ctypes.sizeof(InternalStateStructT)) |
| 218 | + else: |
| 219 | + sizeConstants.internalStructSize = ctypes.sizeof(InternalStateStructT) |
| 220 | + |
| 221 | + return InternalStateStructT |
| 222 | + |
| 223 | + lutSizes = getLUTSizeConstants(lutsLib) |
| 224 | + commonSizes = getCommonSizeConstants(commonLib) |
| 225 | + |
| 226 | + sizeConstants = _getSizeConstants(lib, nameNoPrefixFirstCapital) |
| 227 | + InternalStateStructT = constructInternalStateStruct(internalStateStructName, commonSizes, lutSizes, sizeConstants, getFieldsForInternalStateStructure) |
| 228 | + |
| 229 | + ctypesFunc = getattr(lib, nameNoPrefix) |
| 230 | + |
| 231 | + ctypesFunc.argtypes = [func.__annotations__[argName] for argName in func.__code__.co_varnames[: func.__code__.co_argcount]] |
| 232 | + ctypesFunc.restype = func.__annotations__["return"] |
| 233 | + |
| 234 | + return lib, InternalStateStructT, sizeConstants |
0 commit comments