Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/DRipper Free/pytransform/__init__.py
Views: 299
# These module alos are used by protection code, so that protection1# code needn't import anything2import os3import platform4import sys5import struct67# Because ctypes is new from Python 2.5, so pytransform doesn't work8# before Python 2.59#10from ctypes import cdll, c_char, c_char_p, c_int, c_void_p, \11pythonapi, py_object, PYFUNCTYPE, CFUNCTYPE12from fnmatch import fnmatch1314#15# Support Platforms16#17plat_path = 'platforms'1819plat_table = (20('windows', ('windows', 'cygwin-*')),21('darwin', ('darwin', 'ios')),22('linux', ('linux*',)),23('freebsd', ('freebsd*', 'openbsd*')),24('poky', ('poky',)),25)2627arch_table = (28('x86', ('i?86', )),29('x86_64', ('x64', 'x86_64', 'amd64', 'intel')),30('arm', ('armv5',)),31('armv6', ('armv6l',)),32('armv7', ('armv7l',)),33('ppc64', ('ppc64le',)),34('mips32', ('mips',)),35('aarch32', ('aarch32',)),36('aarch64', ('aarch64', 'arm64'))37)3839#40# Hardware type41#42HT_HARDDISK, HT_IFMAC, HT_IPV4, HT_IPV6, HT_DOMAIN = range(5)4344#45# Global46#47_pytransform = None484950class PytransformError(Exception):51pass525354def dllmethod(func):55def wrap(*args, **kwargs):56return func(*args, **kwargs)57return wrap585960@dllmethod61def version_info():62prototype = PYFUNCTYPE(py_object)63dlfunc = prototype(('version_info', _pytransform))64return dlfunc()656667@dllmethod68def init_pytransform():69major, minor = sys.version_info[0:2]70# Python2.5 no sys.maxsize but sys.maxint71# bitness = 64 if sys.maxsize > 2**32 else 3272prototype = PYFUNCTYPE(c_int, c_int, c_int, c_void_p)73init_module = prototype(('init_module', _pytransform))74ret = init_module(major, minor, pythonapi._handle)75if (ret & 0xF000) == 0x1000:76raise PytransformError('Initialize python wrapper failed (%d)'77% (ret & 0xFFF))78return ret798081@dllmethod82def init_runtime():83prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int)84_init_runtime = prototype(('init_runtime', _pytransform))85return _init_runtime(0, 0, 0, 0)868788@dllmethod89def encrypt_code_object(pubkey, co, flags, suffix=''):90_pytransform.set_option(6, suffix.encode())91prototype = PYFUNCTYPE(py_object, py_object, py_object, c_int)92dlfunc = prototype(('encrypt_code_object', _pytransform))93return dlfunc(pubkey, co, flags)949596@dllmethod97def generate_license_file(filename, priname, rcode, start=-1, count=1):98prototype = PYFUNCTYPE(c_int, c_char_p, c_char_p, c_char_p, c_int, c_int)99dlfunc = prototype(('generate_project_license_files', _pytransform))100return dlfunc(filename.encode(), priname.encode(), rcode.encode(),101start, count) if sys.version_info[0] == 3 \102else dlfunc(filename, priname, rcode, start, count)103104105@dllmethod106def generate_license_key(prikey, keysize, rcode):107prototype = PYFUNCTYPE(py_object, c_char_p, c_int, c_char_p)108dlfunc = prototype(('generate_license_key', _pytransform))109return dlfunc(prikey, keysize, rcode) if sys.version_info[0] == 2 \110else dlfunc(prikey, keysize, rcode.encode())111112113@dllmethod114def get_registration_code():115prototype = PYFUNCTYPE(py_object)116dlfunc = prototype(('get_registration_code', _pytransform))117return dlfunc()118119120@dllmethod121def get_expired_days():122prototype = PYFUNCTYPE(py_object)123dlfunc = prototype(('get_expired_days', _pytransform))124return dlfunc()125126127@dllmethod128def clean_obj(obj, kind):129prototype = PYFUNCTYPE(c_int, py_object, c_int)130dlfunc = prototype(('clean_obj', _pytransform))131return dlfunc(obj, kind)132133134def clean_str(*args):135tdict = {136'str': 0,137'bytearray': 1,138'unicode': 2139}140for obj in args:141k = tdict.get(type(obj).__name__)142if k is None:143raise RuntimeError('Can not clean object: %s' % obj)144clean_obj(obj, k)145146147def get_hd_info(hdtype, size=256):148if hdtype not in range(HT_DOMAIN + 1):149raise RuntimeError('Invalid parameter hdtype: %s' % hdtype)150t_buf = c_char * size151buf = t_buf()152if (_pytransform.get_hd_info(hdtype, buf, size) == -1):153raise PytransformError('Get hardware information failed')154return buf.value.decode()155156157def show_hd_info():158return _pytransform.show_hd_info()159160161def assert_armored(*names):162prototype = PYFUNCTYPE(py_object, py_object)163dlfunc = prototype(('assert_armored', _pytransform))164165def wrapper(func):166def wrap_execute(*args, **kwargs):167dlfunc(names)168return func(*args, **kwargs)169return wrap_execute170return wrapper171172173def get_license_info():174info = {175'ISSUER': None,176'EXPIRED': None,177'HARDDISK': None,178'IFMAC': None,179'IFIPV4': None,180'DOMAIN': None,181'DATA': None,182'CODE': None,183}184rcode = get_registration_code().decode()185if rcode.startswith('*VERSION:'):186index = rcode.find('\n')187info['ISSUER'] = rcode[9:index].split('.')[0].replace('-sn-1.txt', '')188rcode = rcode[index+1:]189190index = 0191if rcode.startswith('*TIME:'):192from time import ctime193index = rcode.find('\n')194info['EXPIRED'] = ctime(float(rcode[6:index]))195index += 1196197if rcode[index:].startswith('*FLAGS:'):198index += len('*FLAGS:') + 1199info['FLAGS'] = ord(rcode[index - 1])200201prev = None202start = index203for k in ['HARDDISK', 'IFMAC', 'IFIPV4', 'DOMAIN', 'FIXKEY', 'CODE']:204index = rcode.find('*%s:' % k)205if index > -1:206if prev is not None:207info[prev] = rcode[start:index]208prev = k209start = index + len(k) + 2210info['CODE'] = rcode[start:]211i = info['CODE'].find(';')212if i > 0:213info['DATA'] = info['CODE'][i+1:]214info['CODE'] = info['CODE'][:i]215return info216217218def get_license_code():219return get_license_info()['CODE']220221222def get_user_data():223return get_license_info()['DATA']224225226def _match_features(patterns, s):227for pat in patterns:228if fnmatch(s, pat):229return True230231232def _gnu_get_libc_version():233try:234prototype = CFUNCTYPE(c_char_p)235ver = prototype(('gnu_get_libc_version', cdll.LoadLibrary('')))()236return ver.decode().split('.')237except Exception:238pass239240241def format_platform(platid=None):242if platid:243return os.path.normpath(platid)244245plat = platform.system().lower()246mach = platform.machine().lower()247248for alias, platlist in plat_table:249if _match_features(platlist, plat):250plat = alias251break252253if plat == 'linux':254cname, cver = platform.libc_ver()255if cname == 'musl':256plat = 'musl'257elif cname == 'libc':258plat = 'android'259elif cname == 'glibc':260v = _gnu_get_libc_version()261if v and len(v) >= 2 and (int(v[0]) * 100 + int(v[1])) < 214:262plat = 'centos6'263264for alias, archlist in arch_table:265if _match_features(archlist, mach):266mach = alias267break268269if plat == 'windows' and mach == 'x86_64':270bitness = struct.calcsize('P'.encode()) * 8271if bitness == 32:272mach = 'x86'273274return os.path.join(plat, mach)275276277# Load _pytransform library278def _load_library(path=None, is_runtime=0, platid=None, suffix='', advanced=0):279path = os.path.dirname(__file__) if path is None \280else os.path.normpath(path)281282plat = platform.system().lower()283name = '_pytransform' + suffix284if plat == 'linux':285filename = os.path.abspath(os.path.join(path, name + '.so'))286elif plat == 'darwin':287filename = os.path.join(path, name + '.dylib')288elif plat == 'windows':289filename = os.path.join(path, name + '.dll')290elif plat == 'freebsd':291filename = os.path.join(path, name + '.so')292else:293raise PytransformError('Platform %s not supported' % plat)294295if platid is not None and os.path.isfile(platid):296filename = platid297elif platid is not None or not os.path.exists(filename) or not is_runtime:298libpath = platid if platid is not None and os.path.isabs(platid) else \299os.path.join(path, plat_path, format_platform(platid))300filename = os.path.join(libpath, os.path.basename(filename))301302if not os.path.exists(filename):303raise PytransformError('Could not find "%s"' % filename)304305try:306m = cdll.LoadLibrary(filename)307except Exception as e:308if sys.flags.debug:309print('Load %s failed:\n%s' % (filename, e))310raise311312# Removed from v4.6.1313# if plat == 'linux':314# m.set_option(-1, find_library('c').encode())315316if not os.path.abspath('.') == os.path.abspath(path):317m.set_option(1, path.encode() if sys.version_info[0] == 3 else path)318319# Required from Python3.6320m.set_option(2, sys.byteorder.encode())321322if sys.flags.debug:323m.set_option(3, c_char_p(1))324m.set_option(4, c_char_p(not is_runtime))325326# Disable advanced mode by default327m.set_option(5, c_char_p(not advanced))328329# Set suffix for private package330if suffix:331m.set_option(6, suffix.encode())332333return m334335336def pyarmor_init(path=None, is_runtime=0, platid=None, suffix='', advanced=0):337global _pytransform338_pytransform = _load_library(path, is_runtime, platid, suffix, advanced)339return init_pytransform()340341342def pyarmor_runtime(path=None, suffix='', advanced=0):343pyarmor_init(path, is_runtime=1, suffix=suffix, advanced=advanced)344init_runtime()345346# ----------------------------------------------------------347# End of pytransform348# ----------------------------------------------------------349350#351# Not available from v5.6352#353354355def generate_capsule(licfile):356prikey, pubkey, prolic = _generate_project_capsule()357capkey, newkey = _generate_pytransform_key(licfile, pubkey)358return prikey, pubkey, capkey, newkey, prolic359360361@dllmethod362def _generate_project_capsule():363prototype = PYFUNCTYPE(py_object)364dlfunc = prototype(('generate_project_capsule', _pytransform))365return dlfunc()366367368@dllmethod369def _generate_pytransform_key(licfile, pubkey):370prototype = PYFUNCTYPE(py_object, c_char_p, py_object)371dlfunc = prototype(('generate_pytransform_key', _pytransform))372return dlfunc(licfile.encode() if sys.version_info[0] == 3 else licfile,373pubkey)374375376#377# Deprecated functions from v5.1378#379@dllmethod380def encrypt_project_files(proname, filelist, mode=0):381prototype = PYFUNCTYPE(c_int, c_char_p, py_object, c_int)382dlfunc = prototype(('encrypt_project_files', _pytransform))383return dlfunc(proname.encode(), filelist, mode)384385386def generate_project_capsule(licfile):387prikey, pubkey, prolic = _generate_project_capsule()388capkey = _encode_capsule_key_file(licfile)389return prikey, pubkey, capkey, prolic390391392@dllmethod393def _encode_capsule_key_file(licfile):394prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p)395dlfunc = prototype(('encode_capsule_key_file', _pytransform))396return dlfunc(licfile.encode(), None)397398399@dllmethod400def encrypt_files(key, filelist, mode=0):401t_key = c_char * 32402prototype = PYFUNCTYPE(c_int, t_key, py_object, c_int)403dlfunc = prototype(('encrypt_files', _pytransform))404return dlfunc(t_key(*key), filelist, mode)405406407@dllmethod408def generate_module_key(pubname, key):409t_key = c_char * 32410prototype = PYFUNCTYPE(py_object, c_char_p, t_key, c_char_p)411dlfunc = prototype(('generate_module_key', _pytransform))412return dlfunc(pubname.encode(), t_key(*key), None)413414#415# Compatible for PyArmor v3.0416#417@dllmethod418def old_init_runtime(systrace=0, sysprofile=1, threadtrace=0, threadprofile=1):419'''Only for old version, before PyArmor 3'''420pyarmor_init(is_runtime=1)421prototype = PYFUNCTYPE(c_int, c_int, c_int, c_int, c_int)422_init_runtime = prototype(('init_runtime', _pytransform))423return _init_runtime(systrace, sysprofile, threadtrace, threadprofile)424425426@dllmethod427def import_module(modname, filename):428'''Only for old version, before PyArmor 3'''429prototype = PYFUNCTYPE(py_object, c_char_p, c_char_p)430_import_module = prototype(('import_module', _pytransform))431return _import_module(modname.encode(), filename.encode())432433434@dllmethod435def exec_file(filename):436'''Only for old version, before PyArmor 3'''437prototype = PYFUNCTYPE(c_int, c_char_p)438_exec_file = prototype(('exec_file', _pytransform))439return _exec_file(filename.encode())440441442